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
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_per_fused_mean_0(in_out_ptr0, in_ptr0, out_ptr0, xnumel, rnumel,
    XBLOCK: tl.constexpr):
    xnumel = 384
    RBLOCK: tl.constexpr = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    rindex = tl.arange(0, RBLOCK)[None, :]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    r2 = rindex
    x3 = xindex
    x0 = xindex % 96
    x1 = xindex // 96
    tmp0 = tl.load(in_ptr0 + (r2 + 16 * x3), xmask, other=0.0)
    tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
    tmp3 = tl.where(xmask, tmp1, 0)
    tmp4 = tl.sum(tmp3, 1)[:, None]
    tmp5 = 16.0
    tmp6 = tmp4 / tmp5
    tl.store(out_ptr0 + (x0 + 96 * r2 + 1536 * x1), tmp0, xmask)
    tl.debug_barrier()
    tl.store(in_out_ptr0 + x3, tmp6, xmask)


@triton.jit
def triton_poi_fused_relu_1(in_out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 24
    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.full([1], 0, tl.int32)
    tmp2 = triton_helpers.maximum(tmp1, tmp0)
    tl.store(in_out_ptr0 + x0, tmp2, xmask)


@triton.jit
def triton_poi_fused_adaptive_max_pool2d_2(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 % 96
    x1 = xindex // 96
    x2 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 1536 * x1), xmask)
    tmp1 = tl.load(in_ptr0 + (96 + x0 + 1536 * x1), xmask)
    tmp3 = tl.load(in_ptr0 + (192 + x0 + 1536 * x1), xmask)
    tmp5 = tl.load(in_ptr0 + (288 + x0 + 1536 * x1), xmask)
    tmp7 = tl.load(in_ptr0 + (384 + x0 + 1536 * x1), xmask)
    tmp9 = tl.load(in_ptr0 + (480 + x0 + 1536 * x1), xmask)
    tmp11 = tl.load(in_ptr0 + (576 + x0 + 1536 * x1), xmask)
    tmp13 = tl.load(in_ptr0 + (672 + x0 + 1536 * x1), xmask)
    tmp15 = tl.load(in_ptr0 + (768 + x0 + 1536 * x1), xmask)
    tmp17 = tl.load(in_ptr0 + (864 + x0 + 1536 * x1), xmask)
    tmp19 = tl.load(in_ptr0 + (960 + x0 + 1536 * x1), xmask)
    tmp21 = tl.load(in_ptr0 + (1056 + x0 + 1536 * x1), xmask)
    tmp23 = tl.load(in_ptr0 + (1152 + x0 + 1536 * x1), xmask)
    tmp25 = tl.load(in_ptr0 + (1248 + x0 + 1536 * x1), xmask)
    tmp27 = tl.load(in_ptr0 + (1344 + x0 + 1536 * x1), xmask)
    tmp29 = tl.load(in_ptr0 + (1440 + x0 + 1536 * x1), xmask)
    tmp2 = triton_helpers.maximum(tmp1, tmp0)
    tmp4 = triton_helpers.maximum(tmp3, tmp2)
    tmp6 = triton_helpers.maximum(tmp5, tmp4)
    tmp8 = triton_helpers.maximum(tmp7, tmp6)
    tmp10 = triton_helpers.maximum(tmp9, tmp8)
    tmp12 = triton_helpers.maximum(tmp11, tmp10)
    tmp14 = triton_helpers.maximum(tmp13, tmp12)
    tmp16 = triton_helpers.maximum(tmp15, tmp14)
    tmp18 = triton_helpers.maximum(tmp17, tmp16)
    tmp20 = triton_helpers.maximum(tmp19, tmp18)
    tmp22 = triton_helpers.maximum(tmp21, tmp20)
    tmp24 = triton_helpers.maximum(tmp23, tmp22)
    tmp26 = triton_helpers.maximum(tmp25, tmp24)
    tmp28 = triton_helpers.maximum(tmp27, tmp26)
    tmp30 = triton_helpers.maximum(tmp29, tmp28)
    tl.store(out_ptr0 + x2, tmp30, xmask)


@triton.jit
def triton_poi_fused_add_mul_sigmoid_3(in_ptr0, in_ptr1, in_ptr2, out_ptr0,
    ynumel, xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.constexpr):
    ynumel = 384
    xnumel = 16
    yoffset = tl.program_id(1) * YBLOCK
    yindex = yoffset + tl.arange(0, YBLOCK)[None, :]
    ymask = yindex < ynumel
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    x2 = xindex
    y0 = yindex % 96
    y1 = yindex // 96
    y3 = yindex
    tmp0 = tl.load(in_ptr0 + (y0 + 96 * x2 + 1536 * y1), xmask & ymask,
        eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr1 + y3, ymask, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr2 + y3, ymask, eviction_policy='evict_last')
    tmp3 = tmp1 + tmp2
    tmp4 = tl.sigmoid(tmp3)
    tmp5 = tmp0 * tmp4
    tl.store(out_ptr0 + (x2 + 16 * y3), tmp5, xmask & ymask)


@triton.jit
def triton_poi_fused_add_sigmoid_sigmoid_backward_4(in_out_ptr0, in_ptr0,
    xnumel, XBLOCK: tl.constexpr):
    xnumel = 384
    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.sigmoid(tmp2)
    tmp4 = 1.0
    tmp5 = tmp4 - tmp3
    tmp6 = tmp3 * tmp5
    tl.store(in_out_ptr0 + x0, tmp6, xmask)


def call(args):
    primals_1, primals_2, primals_3 = args
    args.clear()
    assert_size_stride(primals_1, (4, 96, 4, 4), (1536, 16, 4, 1))
    assert_size_stride(primals_2, (6, 96, 1, 1), (96, 1, 1, 1))
    assert_size_stride(primals_3, (96, 6, 1, 1), (6, 1, 1, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 96, 4, 4), (1536, 1, 384, 96), torch.
            float32)
        buf1 = empty_strided_cuda((4, 96, 1, 1), (96, 1, 384, 384), torch.
            float32)
        buf2 = reinterpret_tensor(buf1, (4, 96, 1, 1), (96, 1, 96, 96), 0)
        del buf1
        get_raw_stream(0)
        triton_per_fused_mean_0[grid(384)](buf2, primals_1, buf0, 384, 16,
            XBLOCK=32, num_warps=4, num_stages=1)
        del primals_1
        buf3 = extern_kernels.convolution(buf2, primals_2, stride=(1, 1),
            padding=(0, 0), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf3, (4, 6, 1, 1), (6, 1, 6, 6))
        buf4 = buf3
        del buf3
        triton_poi_fused_relu_1[grid(24)](buf4, 24, XBLOCK=32, num_warps=1,
            num_stages=1)
        buf5 = extern_kernels.convolution(buf4, primals_3, stride=(1, 1),
            padding=(0, 0), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf5, (4, 96, 1, 1), (96, 1, 96, 96))
        buf6 = empty_strided_cuda((4, 96, 1, 1), (96, 1, 96, 96), torch.float32
            )
        triton_poi_fused_adaptive_max_pool2d_2[grid(384)](buf0, buf6, 384,
            XBLOCK=128, num_warps=4, num_stages=1)
        buf7 = extern_kernels.convolution(buf6, primals_2, stride=(1, 1),
            padding=(0, 0), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf7, (4, 6, 1, 1), (6, 1, 6, 6))
        buf8 = buf7
        del buf7
        triton_poi_fused_relu_1[grid(24)](buf8, 24, XBLOCK=32, num_warps=1,
            num_stages=1)
        buf9 = extern_kernels.convolution(buf8, primals_3, stride=(1, 1),
            padding=(0, 0), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf9, (4, 96, 1, 1), (96, 1, 96, 96))
        buf10 = empty_strided_cuda((4, 96, 4, 4), (1536, 16, 4, 1), torch.
            float32)
        triton_poi_fused_add_mul_sigmoid_3[grid(384, 16)](buf0, buf5, buf9,
            buf10, 384, 16, XBLOCK=1, YBLOCK=256, num_warps=4, num_stages=1)
        buf11 = buf5
        del buf5
        triton_poi_fused_add_sigmoid_sigmoid_backward_4[grid(384)](buf11,
            buf9, 384, XBLOCK=256, num_warps=4, num_stages=1)
        del buf9
    return buf10, buf0, primals_2, primals_3, buf2, buf4, buf6, buf8, buf11


class ChannelAttentionNew(nn.Module):

    def __init__(self, in_planes=96, ratio=16):
        super(ChannelAttentionNew, self).__init__()
        self.avg_pool = nn.AdaptiveAvgPool2d(1)
        self.max_pool = nn.AdaptiveMaxPool2d(1)
        self.fc1 = nn.Conv2d(in_planes, in_planes // ratio, 1, bias=False)
        self.relu1 = nn.ReLU()
        self.fc2 = nn.Conv2d(in_planes // ratio, in_planes, 1, bias=False)
        self.sigmoid = nn.Sigmoid()

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


Measured latency_ms: 0.11685