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 import 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_avg_pool2d_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.
    constexpr):
    xnumel = 3072
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 64
    x1 = xindex // 64 % 3
    x2 = xindex // 192
    x3 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 1280 * x1 + 4096 * x2), xmask)
    tmp1 = tl.load(in_ptr0 + (64 + x0 + 1280 * x1 + 4096 * x2), xmask)
    tmp3 = tl.load(in_ptr0 + (128 + x0 + 1280 * x1 + 4096 * x2), xmask)
    tmp5 = tl.load(in_ptr0 + (192 + x0 + 1280 * x1 + 4096 * x2), xmask)
    tmp7 = tl.load(in_ptr0 + (256 + x0 + 1280 * x1 + 4096 * x2), xmask)
    tmp9 = tl.load(in_ptr0 + (320 + x0 + 1280 * x1 + 4096 * x2), xmask)
    tmp11 = tl.load(in_ptr0 + (384 + x0 + 1280 * x1 + 4096 * x2), xmask)
    tmp13 = tl.load(in_ptr0 + (448 + x0 + 1280 * x1 + 4096 * x2), xmask)
    tmp15 = tl.load(in_ptr0 + (512 + x0 + 1280 * x1 + 4096 * x2), xmask)
    tmp17 = tl.load(in_ptr0 + (576 + x0 + 1280 * x1 + 4096 * x2), xmask)
    tmp19 = tl.load(in_ptr0 + (640 + x0 + 1280 * x1 + 4096 * x2), xmask)
    tmp21 = tl.load(in_ptr0 + (704 + x0 + 1280 * x1 + 4096 * x2), xmask)
    tmp23 = tl.load(in_ptr0 + (768 + x0 + 1280 * x1 + 4096 * x2), xmask)
    tmp25 = tl.load(in_ptr0 + (832 + x0 + 1280 * x1 + 4096 * x2), xmask)
    tmp27 = tl.load(in_ptr0 + (896 + x0 + 1280 * x1 + 4096 * x2), xmask)
    tmp29 = tl.load(in_ptr0 + (960 + x0 + 1280 * x1 + 4096 * x2), xmask)
    tmp31 = tl.load(in_ptr0 + (1024 + x0 + 1280 * x1 + 4096 * x2), xmask)
    tmp33 = tl.load(in_ptr0 + (1088 + x0 + 1280 * x1 + 4096 * x2), xmask)
    tmp35 = tl.load(in_ptr0 + (1152 + x0 + 1280 * x1 + 4096 * x2), xmask)
    tmp37 = tl.load(in_ptr0 + (1216 + x0 + 1280 * x1 + 4096 * x2), xmask)
    tmp2 = tmp1 + tmp0
    tmp4 = tmp3 + tmp2
    tmp6 = tmp5 + tmp4
    tmp8 = tmp7 + tmp6
    tmp10 = tmp9 + tmp8
    tmp12 = tmp11 + tmp10
    tmp14 = tmp13 + tmp12
    tmp16 = tmp15 + tmp14
    tmp18 = tmp17 + tmp16
    tmp20 = tmp19 + tmp18
    tmp22 = tmp21 + tmp20
    tmp24 = tmp23 + tmp22
    tmp26 = tmp25 + tmp24
    tmp28 = tmp27 + tmp26
    tmp30 = tmp29 + tmp28
    tmp32 = tmp31 + tmp30
    tmp34 = tmp33 + tmp32
    tmp36 = tmp35 + tmp34
    tmp38 = tmp37 + tmp36
    tmp39 = 0.05
    tmp40 = tmp38 * tmp39
    tl.store(out_ptr0 + x3, tmp40, xmask)


@triton.jit
def triton_poi_fused__to_copy_add_arange_mul_1(out_ptr0, xnumel, XBLOCK: tl
    .constexpr):
    xnumel = 4
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = x0
    tmp1 = tmp0.to(tl.float32)
    tmp2 = 0.75
    tmp3 = tmp1 * tmp2
    tmp4 = tmp3.to(tl.int32)
    tl.store(out_ptr0 + x0, tmp4, xmask)


@triton.jit
def triton_poi_fused__to_copy_add_arange_mul_2(out_ptr0, xnumel, XBLOCK: tl
    .constexpr):
    xoffset = tl.program_id(0) * XBLOCK
    xoffset + tl.arange(0, XBLOCK)[:]
    tl.full([XBLOCK], True, tl.int1)
    tmp0 = tl.full([1], 0, tl.int64)
    tl.store(out_ptr0 + tl.full([XBLOCK], 0, tl.int32), tmp0, None)


@triton.jit
def triton_poi_fused__unsafe_index_convolution_relu_3(in_ptr0, in_ptr1,
    in_ptr2, in_ptr3, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 4
    x3 = xindex // 4
    x1 = xindex // 4 % 4
    x4 = xindex
    tmp0 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
    tmp5 = tl.load(in_ptr1 + 0)
    tmp6 = tl.broadcast_to(tmp5, [XBLOCK])
    tmp12 = tl.load(in_ptr3 + x1, xmask, eviction_policy='evict_last')
    tmp1 = tl.full([XBLOCK], 3, tl.int32)
    tmp2 = tmp0 + tmp1
    tmp3 = tmp0 < 0
    tmp4 = tl.where(tmp3, tmp2, tmp0)
    tmp7 = tl.full([XBLOCK], 64, tl.int32)
    tmp8 = tmp6 + tmp7
    tmp9 = tmp6 < 0
    tmp10 = tl.where(tmp9, tmp8, tmp6)
    tmp11 = tl.load(in_ptr2 + (tmp10 + 64 * tmp4 + 192 * x3), xmask,
        eviction_policy='evict_last')
    tmp13 = tmp11 + tmp12
    tmp14 = tl.full([1], 0, tl.int32)
    tmp15 = triton_helpers.maximum(tmp14, tmp13)
    tl.store(out_ptr0 + x4, tmp15, xmask)


@triton.jit
def triton_poi_fused_convolution_relu_threshold_backward_4(in_ptr0, in_ptr1,
    out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 3072
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x3 = xindex
    x1 = xindex // 192 % 4
    tmp0 = tl.load(in_ptr0 + x3, xmask)
    tmp1 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp3 = tl.full([1], 0, tl.int32)
    tmp4 = triton_helpers.maximum(tmp3, tmp2)
    tmp5 = 0.0
    tmp6 = tmp4 <= tmp5
    tl.store(out_ptr0 + x3, tmp6, xmask)


def call(args):
    primals_1, primals_2, primals_3 = args
    args.clear()
    assert_size_stride(primals_1, (4, 4, 64, 64), (16384, 4096, 64, 1))
    assert_size_stride(primals_2, (4, 4, 1, 1), (4, 1, 1, 1))
    assert_size_stride(primals_3, (4,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 4, 3, 64), (768, 192, 64, 1), torch.
            float32)
        get_raw_stream(0)
        triton_poi_fused_avg_pool2d_0[grid(3072)](primals_1, buf0, 3072,
            XBLOCK=128, num_warps=4, num_stages=1)
        del primals_1
        buf1 = extern_kernels.convolution(buf0, primals_2, stride=(1, 1),
            padding=(0, 0), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf1, (4, 4, 3, 64), (768, 192, 64, 1))
        buf2 = empty_strided_cuda((4,), (1,), torch.int64)
        triton_poi_fused__to_copy_add_arange_mul_1[grid(4)](buf2, 4, XBLOCK
            =4, num_warps=1, num_stages=1)
        buf3 = empty_strided_cuda((1,), (1,), torch.int64)
        triton_poi_fused__to_copy_add_arange_mul_2[grid(1)](buf3, 1, XBLOCK
            =1, num_warps=1, num_stages=1)
        buf4 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 1), torch.float32)
        triton_poi_fused__unsafe_index_convolution_relu_3[grid(64)](buf2,
            buf3, buf1, primals_3, buf4, 64, XBLOCK=64, num_warps=1,
            num_stages=1)
        buf5 = empty_strided_cuda((4, 4, 3, 64), (768, 192, 64, 1), torch.bool)
        triton_poi_fused_convolution_relu_threshold_backward_4[grid(3072)](buf1
            , primals_3, buf5, 3072, XBLOCK=256, num_warps=4, num_stages=1)
        del buf1
        del primals_3
    return buf4, primals_2, buf0, reinterpret_tensor(buf2, (4, 1), (1, 1), 0
        ), buf3, buf5


class MS_BlockNew(nn.Module):

    def __init__(self, in_channel, out_channel, pool_level, txt_length):
        super(MS_BlockNew, self).__init__()
        self.txt_length = txt_length
        pool_kernel = 5 * pool_level, 1
        pool_stride = 5 * pool_level, 1
        self.pool = nn.AvgPool2d(kernel_size=pool_kernel, stride=pool_stride)
        self.conv = nn.Conv2d(in_channel, out_channel, kernel_size=(1, 1),
            stride=(1, 1))
        self.relu = nn.ReLU(inplace=True)
        self._init_weight()

    def _init_weight(self):
        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                nn.init.normal_(m.weight, 0, 0.01)
                if m.bias is not None:
                    nn.init.constant_(m.bias, 0)
            elif isinstance(m, nn.BatchNorm2d):
                nn.init.constant_(m.weight, 1)
                nn.init.constant_(m.bias, 0)
            elif isinstance(m, nn.BatchNorm1d):
                nn.init.constant_(m.weight, 1)
                nn.init.constant_(m.bias, 0)
            elif isinstance(m, nn.Linear):
                nn.init.normal_(m.weight, 0, 0.01)
                if m.bias is not None:
                    nn.init.constant_(m.bias, 0)

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


Measured latency_ms: 0.079058