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
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda


@triton.jit
def triton_poi_fused_copy_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 384
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 6
    x1 = xindex // 6
    x2 = xindex
    tmp0 = x0
    tmp1 = tl.full([1], 5, tl.int64)
    tmp2 = tmp0 >= tmp1
    tmp3 = -4 + x0
    tmp4 = tl.full([1], 1, tl.int64)
    tmp5 = tmp3 < tmp4
    tmp6 = tmp5 & tmp2
    tmp7 = tmp0 >= tmp4
    tmp8 = tmp0 < tmp1
    tmp9 = tmp7 & tmp8
    tmp10 = tmp9 & tmp6
    tmp11 = tl.load(in_ptr0 + (-1 + x0 + 4 * x1), tmp10 & xmask, other=0.0)
    tmp12 = float('nan')
    tmp13 = tl.where(tmp9, tmp11, tmp12)
    tmp14 = tl.full(tmp13.shape, 0.0, tmp13.dtype)
    tmp15 = tl.where(tmp6, tmp13, tmp14)
    tmp16 = tmp3 >= tmp4
    tmp17 = tmp3 < tmp1
    tmp18 = tmp16 & tmp17
    tmp19 = tmp18 & tmp2
    tmp20 = tl.load(in_ptr0 + (-5 + x0 + 4 * x1), tmp19 & xmask, other=0.0)
    tmp21 = tl.where(tmp18, tmp20, tmp12)
    tmp22 = tl.where(tmp5, tmp15, tmp21)
    tmp23 = tl.full(tmp22.shape, 0.0, tmp22.dtype)
    tmp24 = tl.where(tmp2, tmp22, tmp23)
    tmp25 = tmp0 < tmp4
    tmp26 = 4 + x0
    tmp27 = tmp26 >= tmp4
    tmp28 = tmp26 < tmp1
    tmp29 = tmp27 & tmp28
    tmp30 = tmp29 & tmp25
    tmp31 = tl.load(in_ptr0 + (3 + x0 + 4 * x1), tmp30 & xmask, other=0.0)
    tmp32 = tl.where(tmp29, tmp31, tmp12)
    tmp33 = tl.full(tmp32.shape, 0.0, tmp32.dtype)
    tmp34 = tl.where(tmp25, tmp32, tmp33)
    tmp35 = tl.load(in_ptr0 + (-1 + x0 + 4 * x1), tmp9 & xmask, other=0.0)
    tmp36 = tl.where(tmp9, tmp35, tmp12)
    tmp37 = tl.where(tmp25, tmp34, tmp36)
    tmp38 = tl.where(tmp2, tmp24, tmp37)
    tl.store(out_ptr0 + x2, tmp38, xmask)


@triton.jit
def triton_poi_fused_copy_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 384
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 6
    x1 = xindex // 6
    x2 = xindex
    tmp0 = x0
    tmp1 = tl.full([1], 5, tl.int64)
    tmp2 = tmp0 >= tmp1
    tmp3 = -4 + x0
    tmp4 = tl.full([1], 1, tl.int64)
    tmp5 = tmp3 < tmp4
    tmp6 = tmp5 & tmp2
    tmp7 = tmp0 >= tmp4
    tmp8 = tmp0 < tmp1
    tmp9 = tmp7 & tmp8
    tmp10 = tmp9 & tmp6
    tmp11 = tl.load(in_ptr0 + (-1 + x0 + 4 * x1), tmp10 & xmask, other=0.0)
    tmp12 = tl.full([1], 0, tl.int32)
    tmp13 = triton_helpers.maximum(tmp12, tmp11)
    tmp14 = tl.full(tmp13.shape, 0.0, tmp13.dtype)
    tmp15 = tl.where(tmp10, tmp13, tmp14)
    tmp16 = float('nan')
    tmp17 = tl.where(tmp9, tmp15, tmp16)
    tmp18 = tl.full(tmp17.shape, 0.0, tmp17.dtype)
    tmp19 = tl.where(tmp6, tmp17, tmp18)
    tmp20 = tmp3 >= tmp4
    tmp21 = tmp3 < tmp1
    tmp22 = tmp20 & tmp21
    tmp23 = tmp22 & tmp2
    tmp24 = tl.load(in_ptr0 + (-5 + x0 + 4 * x1), tmp23 & xmask, other=0.0)
    tmp25 = triton_helpers.maximum(tmp12, tmp24)
    tmp26 = tl.full(tmp25.shape, 0.0, tmp25.dtype)
    tmp27 = tl.where(tmp23, tmp25, tmp26)
    tmp28 = tl.where(tmp22, tmp27, tmp16)
    tmp29 = tl.where(tmp5, tmp19, tmp28)
    tmp30 = tl.full(tmp29.shape, 0.0, tmp29.dtype)
    tmp31 = tl.where(tmp2, tmp29, tmp30)
    tmp32 = tmp0 < tmp4
    tmp33 = 4 + x0
    tmp34 = tmp33 >= tmp4
    tmp35 = tmp33 < tmp1
    tmp36 = tmp34 & tmp35
    tmp37 = tmp36 & tmp32
    tmp38 = tl.load(in_ptr0 + (3 + x0 + 4 * x1), tmp37 & xmask, other=0.0)
    tmp39 = triton_helpers.maximum(tmp12, tmp38)
    tmp40 = tl.full(tmp39.shape, 0.0, tmp39.dtype)
    tmp41 = tl.where(tmp37, tmp39, tmp40)
    tmp42 = tl.where(tmp36, tmp41, tmp16)
    tmp43 = tl.full(tmp42.shape, 0.0, tmp42.dtype)
    tmp44 = tl.where(tmp32, tmp42, tmp43)
    tmp45 = tl.load(in_ptr0 + (-1 + x0 + 4 * x1), tmp9 & xmask, other=0.0)
    tmp46 = triton_helpers.maximum(tmp12, tmp45)
    tmp47 = tl.full(tmp46.shape, 0.0, tmp46.dtype)
    tmp48 = tl.where(tmp9, tmp46, tmp47)
    tmp49 = tl.where(tmp9, tmp48, tmp16)
    tmp50 = tl.where(tmp32, tmp44, tmp49)
    tmp51 = tl.where(tmp2, tmp31, tmp50)
    tl.store(out_ptr0 + x2, tmp51, xmask)


@triton.jit
def triton_poi_fused_add_relu_threshold_backward_2(in_out_ptr0, in_ptr0,
    out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = tl.load(in_out_ptr0 + x0, xmask)
    tmp1 = tl.load(in_ptr0 + x0, xmask)
    tmp2 = tmp0 + tmp1
    tmp3 = tl.full([1], 0, tl.int32)
    tmp4 = triton_helpers.maximum(tmp3, tmp2)
    tmp5 = 0.0
    tmp6 = tmp4 <= tmp5
    tl.store(in_out_ptr0 + x0, tmp4, xmask)
    tl.store(out_ptr0 + x0, tmp6, xmask)


@triton.jit
def triton_poi_fused_relu_threshold_backward_3(in_ptr0, out_ptr0, xnumel,
    XBLOCK: tl.constexpr):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + x0, xmask)
    tmp1 = tl.full([1], 0, tl.int32)
    tmp2 = triton_helpers.maximum(tmp1, tmp0)
    tmp3 = 0.0
    tmp4 = tmp2 <= tmp3
    tl.store(out_ptr0 + x0, tmp4, 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, 3, 3), (36, 9, 3, 1))
    assert_size_stride(primals_3, (4, 4, 3, 3), (36, 9, 3, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf1 = empty_strided_cuda((4, 4, 4, 6), (96, 24, 6, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_copy_0[grid(384)](primals_1, buf1, 384, XBLOCK=128,
            num_warps=4, num_stages=1)
        buf2 = extern_kernels.convolution(buf1, primals_2, stride=(1, 1),
            padding=(1, 0), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf2, (4, 4, 4, 4), (64, 16, 4, 1))
        buf4 = empty_strided_cuda((4, 4, 4, 6), (96, 24, 6, 1), torch.float32)
        triton_poi_fused_copy_1[grid(384)](buf2, buf4, 384, XBLOCK=128,
            num_warps=4, num_stages=1)
        buf5 = extern_kernels.convolution(buf4, primals_3, stride=(1, 1),
            padding=(1, 0), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf5, (4, 4, 4, 4), (64, 16, 4, 1))
        buf6 = buf5
        del buf5
        buf7 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.bool)
        triton_poi_fused_add_relu_threshold_backward_2[grid(256)](buf6,
            primals_1, buf7, 256, XBLOCK=256, num_warps=4, num_stages=1)
        del primals_1
        buf8 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.bool)
        triton_poi_fused_relu_threshold_backward_3[grid(256)](buf2, buf8, 
            256, XBLOCK=256, num_warps=4, num_stages=1)
        del buf2
    return buf6, primals_2, primals_3, buf1, buf4, buf7, buf8


def conv3x3(in_planes, out_planes, stride=1, groups=1, dilation=1, padding=0):
    """3x3 convolution with padding"""
    return torch.nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=
        stride, padding=padding, groups=groups, bias=False, dilation=dilation)


class BasicBlockNew(torch.nn.Module):
    expansion = 1

    def __init__(self, inplanes, planes, stride=1, downsample=None, groups=
        1, base_width=64, dilation=1, activation_fct='relu', norm_layer=None):
        super(BasicBlockNew, self).__init__()
        if groups != 1 or base_width != 64:
            raise ValueError(
                'BasicBlock only supports groups=1 and base_width=64')
        if dilation > 1:
            raise NotImplementedError(
                'Dilation > 1 not supported in BasicBlock')
        self.conv1 = conv3x3(in_planes=inplanes, out_planes=planes, stride=
            stride, padding=(1, 0))
        self.activation = torch.nn.ReLU(inplace=True
            ) if activation_fct == 'relu' else torch.nn.Tanh()
        self.conv2 = conv3x3(in_planes=planes, out_planes=planes, padding=(
            1, 0))
        self.downsample = downsample
        self.stride = stride

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


Measured latency_ms: 0.068216