import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
from torch._inductor.runtime.triton_helpers import libdevice
import torch.nn as nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor


@triton.jit
def triton_poi_fused_bitwise_not_isinf_masked_fill_max_pool2d_with_indices_relu_threshold_backward_0(
    in_ptr0, in_ptr1, out_ptr0, out_ptr1, out_ptr2, out_ptr3, out_ptr4,
    xnumel, XBLOCK: tl.constexpr):
    xnumel = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex
    x0 = xindex % 4
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp3 = tl.full([1], 0, tl.int32)
    tmp4 = triton_helpers.maximum(tmp3, tmp2)
    tmp5 = tl.full([1], False, tl.int1)
    tmp6 = float('-inf')
    tmp7 = tl.where(tmp5, tmp6, tmp4)
    tmp8 = 0.0
    tmp9 = tmp4 <= tmp8
    tmp10 = tl.full([1], 0, tl.int8)
    tmp11 = libdevice.isinf(tmp7).to(tl.int1)
    tmp12 = tl.where(tmp11, tmp8, tmp7)
    tl.store(out_ptr0 + x2, tmp7, xmask)
    tl.store(out_ptr1 + x2, tmp9, xmask)
    tl.store(out_ptr2 + x2, tmp10, xmask)
    tl.store(out_ptr3 + x2, tmp11, xmask)
    tl.store(out_ptr4 + x2, tmp12, xmask)


def call(args):
    primals_1, primals_2, primals_3 = args
    args.clear()
    assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_2, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_3, (4,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = extern_kernels.convolution(primals_1, primals_2, stride=(1, 
            1), padding=(0, 0), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf0, (4, 4, 1, 1), (4, 1, 1, 1))
        buf1 = empty_strided_cuda((4, 4, 1), (4, 1, 1), torch.float32)
        buf5 = empty_strided_cuda((4, 4, 1), (4, 1, 1), torch.bool)
        buf2 = empty_strided_cuda((4, 4, 1, 1), (4, 1, 1, 1), torch.int8)
        buf3 = empty_strided_cuda((4, 4), (4, 1), torch.bool)
        buf4 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_bitwise_not_isinf_masked_fill_max_pool2d_with_indices_relu_threshold_backward_0[
            grid(16)](buf0, primals_3, buf1, buf5, buf2, buf3, buf4, 16,
            XBLOCK=16, num_warps=1, num_stages=1)
        del buf0
        del primals_3
    return buf4, primals_1, primals_2, reinterpret_tensor(buf1, (4, 4, 1, 1
        ), (4, 1, 1, 1), 0), buf2, buf3, buf5


class CNNLayerNew(nn.Module):

    def __init__(self, input_size, in_channels, out_channels, kernel_width,
        act_fun=nn.ReLU, drop_prob=0.1):
        """Initilize CNN layer.

        Args:
            input_size [int]: embedding dim or the last dim of the input
            in_channels [int]: number of channels for inputs
            out_channels [int]: number of channels for outputs
            kernel_width [int]: the width on sequence for the first dim of kernel
            act_fun [torch.nn.modules.activation]: activation function
            drop_prob [float]: drop out ratio
        """
        super(CNNLayerNew, self).__init__()
        self.input_size = input_size
        self.in_channels = in_channels
        self.out_channels = out_channels
        self.kernel_width = kernel_width
        self.conv = nn.Conv2d(in_channels, out_channels, (kernel_width,
            input_size))
        self.drop_out = nn.Dropout(drop_prob)
        assert callable(act_fun), TypeError(
            "Type error of 'act_fun', use functions like nn.ReLU/nn.Tanh.")
        self.act_fun = act_fun()

    def forward(self, input_0):
        primals_1 = self.conv.weight
        primals_3 = self.conv.bias
        primals_2 = input_0
        output = call([primals_1, primals_2, primals_3])
        return output[0]


Measured latency_ms: 0.030039