import torch
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 math
import torch.optim
import torch.utils.data
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_stack_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 1024
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex // 16 % 16
    x3 = xindex // 256
    x4 = xindex % 16
    x0 = xindex % 4
    x1 = xindex // 4 % 4
    x5 = xindex
    tmp0 = x2
    tl.full([1], 0, tl.int64)
    tmp3 = tl.full([1], 4, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp5 = tl.load(in_ptr0 + (x4 + 16 * x2 + 64 * x3), tmp4 & xmask, other=0.0)
    tmp6 = tmp0 >= tmp3
    tmp7 = tl.full([1], 8, tl.int64)
    tmp8 = tmp0 < tmp7
    tmp9 = tmp6 & tmp8
    tmp10 = tl.load(in_ptr0 + (4 * x1 + 16 * (-4 + x2) + 64 * x3 + (3 + x0) %
        4), tmp9 & xmask, eviction_policy='evict_last', other=0.0)
    tmp11 = tmp0 >= tmp7
    tmp12 = tl.full([1], 12, tl.int64)
    tmp13 = tmp0 < tmp12
    tmp14 = tmp11 & tmp13
    tmp15 = tl.load(in_ptr0 + (4 * x1 + 16 * (-8 + x2) + 64 * x3 + (2 + x0) %
        4), tmp14 & xmask, other=0.0)
    tmp16 = tmp0 >= tmp12
    tl.full([1], 16, tl.int64)
    tmp19 = tl.load(in_ptr0 + (4 * x1 + 16 * (-12 + x2) + 64 * x3 + (1 + x0
        ) % 4), tmp16 & xmask, other=0.0)
    tmp20 = tl.where(tmp14, tmp15, tmp19)
    tmp21 = tl.where(tmp9, tmp10, tmp20)
    tmp22 = tl.where(tmp4, tmp5, tmp21)
    tl.store(out_ptr0 + x5, tmp22, xmask)


@triton.jit
def triton_per_fused_mean_1(in_out_ptr0, in_ptr0, xnumel, rnumel, XBLOCK:
    tl.constexpr):
    xnumel = 16
    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)
    r1 = rindex
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + (r1 + 16 * x0), 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.debug_barrier()
    tl.store(in_out_ptr0 + x0, tmp6, xmask)


@triton.jit
def triton_poi_fused_max_2(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + 16 * x0, xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr0 + (4 + 16 * x0), xmask, eviction_policy='evict_last'
        )
    tmp3 = tl.load(in_ptr0 + (8 + 16 * x0), xmask, eviction_policy='evict_last'
        )
    tmp5 = tl.load(in_ptr0 + (12 + 16 * x0), xmask, eviction_policy=
        'evict_last')
    tmp7 = tl.load(in_ptr0 + (1 + 16 * x0), xmask, eviction_policy='evict_last'
        )
    tmp8 = tl.load(in_ptr0 + (5 + 16 * x0), xmask, eviction_policy='evict_last'
        )
    tmp10 = tl.load(in_ptr0 + (9 + 16 * x0), xmask, eviction_policy=
        'evict_last')
    tmp12 = tl.load(in_ptr0 + (13 + 16 * x0), xmask, eviction_policy=
        'evict_last')
    tmp15 = tl.load(in_ptr0 + (2 + 16 * x0), xmask, eviction_policy=
        'evict_last')
    tmp16 = tl.load(in_ptr0 + (6 + 16 * x0), xmask, eviction_policy=
        'evict_last')
    tmp18 = tl.load(in_ptr0 + (10 + 16 * x0), xmask, eviction_policy=
        'evict_last')
    tmp20 = tl.load(in_ptr0 + (14 + 16 * x0), xmask, eviction_policy=
        'evict_last')
    tmp23 = tl.load(in_ptr0 + (3 + 16 * x0), xmask, eviction_policy=
        'evict_last')
    tmp24 = tl.load(in_ptr0 + (7 + 16 * x0), xmask, eviction_policy=
        'evict_last')
    tmp26 = tl.load(in_ptr0 + (11 + 16 * x0), xmask, eviction_policy=
        'evict_last')
    tmp28 = tl.load(in_ptr0 + (15 + 16 * x0), xmask, eviction_policy=
        'evict_last')
    tmp2 = triton_helpers.maximum(tmp0, tmp1)
    tmp4 = triton_helpers.maximum(tmp2, tmp3)
    tmp6 = triton_helpers.maximum(tmp4, tmp5)
    tmp9 = triton_helpers.maximum(tmp7, tmp8)
    tmp11 = triton_helpers.maximum(tmp9, tmp10)
    tmp13 = triton_helpers.maximum(tmp11, tmp12)
    tmp14 = triton_helpers.maximum(tmp6, tmp13)
    tmp17 = triton_helpers.maximum(tmp15, tmp16)
    tmp19 = triton_helpers.maximum(tmp17, tmp18)
    tmp21 = triton_helpers.maximum(tmp19, tmp20)
    tmp22 = triton_helpers.maximum(tmp14, tmp21)
    tmp25 = triton_helpers.maximum(tmp23, tmp24)
    tmp27 = triton_helpers.maximum(tmp25, tmp26)
    tmp29 = triton_helpers.maximum(tmp27, tmp28)
    tmp30 = triton_helpers.maximum(tmp22, tmp29)
    tl.store(out_ptr0 + x0, tmp30, xmask)


@triton.jit
def triton_poi_fused_mul_relu_sum_3(in_ptr0, in_ptr1, in_ptr2, out_ptr0,
    out_ptr1, xnumel, XBLOCK: tl.constexpr):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 4
    x1 = xindex // 4
    x2 = xindex
    tmp0 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr1 + (x0 + 16 * x1), xmask)
    tmp3 = tl.load(in_ptr0 + (4 + x0), xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr1 + (4 + x0 + 16 * x1), xmask)
    tmp7 = tl.load(in_ptr0 + (8 + x0), xmask, eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr1 + (8 + x0 + 16 * x1), xmask)
    tmp11 = tl.load(in_ptr0 + (12 + x0), xmask, eviction_policy='evict_last')
    tmp12 = tl.load(in_ptr1 + (12 + x0 + 16 * x1), xmask)
    tmp17 = tl.load(in_ptr2 + x0, xmask, eviction_policy='evict_last')
    tmp19 = tl.load(in_ptr2 + (4 + x0), xmask, eviction_policy='evict_last')
    tmp22 = tl.load(in_ptr2 + (8 + x0), xmask, eviction_policy='evict_last')
    tmp25 = tl.load(in_ptr2 + (12 + x0), xmask, eviction_policy='evict_last')
    tmp2 = tmp0 * tmp1
    tmp5 = tmp3 * tmp4
    tmp6 = tmp2 + tmp5
    tmp9 = tmp7 * tmp8
    tmp10 = tmp6 + tmp9
    tmp13 = tmp11 * tmp12
    tmp14 = tmp10 + tmp13
    tmp15 = tl.full([1], 0, tl.int32)
    tmp16 = triton_helpers.maximum(tmp15, tmp14)
    tmp18 = tmp17 * tmp1
    tmp20 = tmp19 * tmp4
    tmp21 = tmp18 + tmp20
    tmp23 = tmp22 * tmp8
    tmp24 = tmp21 + tmp23
    tmp26 = tmp25 * tmp12
    tmp27 = tmp24 + tmp26
    tmp28 = triton_helpers.maximum(tmp15, tmp27)
    tl.store(out_ptr0 + x2, tmp16, xmask)
    tl.store(out_ptr1 + x2, tmp28, xmask)


@triton.jit
def triton_poi_fused_add_mul_sigmoid_sigmoid_backward_sum_4(in_out_ptr0,
    in_ptr0, in_ptr1, in_ptr2, 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 % 4
    x2 = xindex // 16
    x4 = xindex // 4
    x3 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 16 * x2), xmask, eviction_policy=
        'evict_last')
    tmp1 = tl.load(in_ptr1 + (x0 + 16 * x4), xmask)
    tmp3 = tl.load(in_ptr0 + (4 + x0 + 16 * x2), xmask, eviction_policy=
        'evict_last')
    tmp4 = tl.load(in_ptr1 + (4 + x0 + 16 * x4), xmask)
    tmp7 = tl.load(in_ptr0 + (8 + x0 + 16 * x2), xmask, eviction_policy=
        'evict_last')
    tmp8 = tl.load(in_ptr1 + (8 + x0 + 16 * x4), xmask)
    tmp11 = tl.load(in_ptr0 + (12 + x0 + 16 * x2), xmask, eviction_policy=
        'evict_last')
    tmp12 = tl.load(in_ptr1 + (12 + x0 + 16 * x4), xmask)
    tmp15 = tl.load(in_ptr2 + (x0 + 16 * x2), xmask, eviction_policy=
        'evict_last')
    tmp17 = tl.load(in_ptr2 + (4 + x0 + 16 * x2), xmask, eviction_policy=
        'evict_last')
    tmp20 = tl.load(in_ptr2 + (8 + x0 + 16 * x2), xmask, eviction_policy=
        'evict_last')
    tmp23 = tl.load(in_ptr2 + (12 + x0 + 16 * x2), xmask, eviction_policy=
        'evict_last')
    tmp2 = tmp0 * tmp1
    tmp5 = tmp3 * tmp4
    tmp6 = tmp2 + tmp5
    tmp9 = tmp7 * tmp8
    tmp10 = tmp6 + tmp9
    tmp13 = tmp11 * tmp12
    tmp14 = tmp10 + tmp13
    tmp16 = tmp15 * tmp1
    tmp18 = tmp17 * tmp4
    tmp19 = tmp16 + tmp18
    tmp21 = tmp20 * tmp8
    tmp22 = tmp19 + tmp21
    tmp24 = tmp23 * tmp12
    tmp25 = tmp22 + tmp24
    tmp26 = tmp14 + tmp25
    tmp27 = tl.sigmoid(tmp26)
    tmp28 = 1.0
    tmp29 = tmp28 - tmp27
    tmp30 = tmp27 * tmp29
    tl.store(in_out_ptr0 + x3, tmp27, xmask)
    tl.store(out_ptr0 + x3, tmp30, 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, 4, 4, 4), (64, 16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 16, 4, 4), (256, 16, 4, 1), torch.float32
            )
        get_raw_stream(0)
        triton_poi_fused_stack_0[grid(1024)](primals_1, buf0, 1024, XBLOCK=
            128, num_warps=4, num_stages=1)
        del primals_1
        buf1 = empty_strided_cuda((4, 16, 4, 4), (256, 16, 4, 1), torch.float32
            )
        triton_poi_fused_stack_0[grid(1024)](primals_2, buf1, 1024, XBLOCK=
            128, num_warps=4, num_stages=1)
        del primals_2
        buf2 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf4 = buf2
        del buf2
        triton_per_fused_mean_1[grid(16)](buf4, primals_3, 16, 16, XBLOCK=8,
            num_warps=2, num_stages=1)
        buf3 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        triton_poi_fused_max_2[grid(16)](primals_3, buf3, 16, XBLOCK=16,
            num_warps=1, num_stages=1)
        del primals_3
        buf5 = empty_strided_cuda((1, 4, 4, 4, 4, 1), (256, 64, 16, 4, 1, 1
            ), torch.float32)
        buf6 = empty_strided_cuda((1, 4, 4, 4, 4, 1), (256, 64, 16, 4, 1, 1
            ), torch.float32)
        triton_poi_fused_mul_relu_sum_3[grid(256)](buf4, buf0, buf3, buf5,
            buf6, 256, XBLOCK=256, num_warps=4, num_stages=1)
        del buf0
        buf7 = empty_strided_cuda((1, 4, 4, 4, 4, 1), (256, 64, 16, 4, 1, 
            256), torch.float32)
        buf8 = reinterpret_tensor(buf7, (1, 4, 4, 4, 4, 1), (256, 64, 16, 4,
            1, 1), 0)
        del buf7
        buf9 = empty_strided_cuda((1, 4, 4, 4, 4, 1), (256, 64, 16, 4, 1, 1
            ), torch.float32)
        triton_poi_fused_add_mul_sigmoid_sigmoid_backward_sum_4[grid(256)](buf8
            , buf5, buf1, buf6, buf9, 256, XBLOCK=256, num_warps=4,
            num_stages=1)
    return reinterpret_tensor(buf8, (4, 4, 4, 1, 4, 1, 1), (64, 16, 4, 4, 1,
        1, 1), 0), buf1, reinterpret_tensor(buf4, (1, 4, 4, 1), (16, 4, 1, 
        1), 0), buf5, reinterpret_tensor(buf3, (1, 4, 4, 1), (16, 4, 1, 1), 0
        ), buf6, buf9


class ChannelAttention(torch.nn.Module):

    def __init__(self, N_out, N_in, ratio=1):
        super(ChannelAttention, self).__init__()
        self.linear = torch.nn.functional.linear
        self.avg_pool = torch.nn.AdaptiveAvgPool2d(1)
        self.max_pool = torch.nn.AdaptiveMaxPool2d(1)
        self.N_in = N_in
        self.N_out = N_out
        self.weight_fc1 = torch.nn.Parameter(torch.Tensor(self.N_out, self.
            N_in // ratio, self.N_in))
        self.weight_fc2 = torch.nn.Parameter(torch.Tensor(self.N_out, self.
            N_in, self.N_in // ratio))
        self.reset_parameters()

    def reset_parameters(self):
        torch.nn.init.kaiming_uniform_(self.weight_fc1, a=math.sqrt(5))
        torch.nn.init.kaiming_uniform_(self.weight_fc2, a=math.sqrt(5))

    def forward(self, input):
        input_mean = input.mean(dim=[-2, -1]).unsqueeze(-1)
        input_max = input.max(dim=-2)[0].max(dim=-1)[0].unsqueeze(-1)
        avg_out = self._linear(torch.relu(self._linear(input_mean, self.
            weight_fc1)), self.weight_fc2)
        max_out = self._linear(torch.relu(self._linear(input_max, self.
            weight_fc1)), self.weight_fc2)
        out = torch.sigmoid(avg_out + max_out)
        out = torch.reshape(out, [input.shape[0], self.N_out, input.shape[2
            ], self.N_in, 1, 1])
        return out

    def _linear(self, input, w):
        in_reshaped = input.unsqueeze(-3)
        w_reshaped = w.reshape(1, w.shape[0], 1, w.shape[1], w.shape[2], 1)
        output = (in_reshaped * w_reshaped).sum(-2)
        return output


class ChannelAttentionGGNew(ChannelAttention):

    def __init__(self, N_h, N_out, N_h_in, N_in, ratio=1, bias=False):
        super(ChannelAttentionGGNew, self).__init__(N_out, N_in, ratio=ratio)
        self.N_h_in = N_h_in
        self.N_h = N_h
        self.weight_fc1 = torch.nn.Parameter(torch.rand(self.N_out, self.
            N_in // ratio, self.N_in, self.N_h_in))
        self.weight_fc2 = torch.nn.Parameter(torch.rand(self.N_out, self.
            N_in, self.N_in // ratio, self.N_h_in))
        self.reset_parameters()

    def _linear(self, input, w):
        in_reshaped = input.unsqueeze(-4)
        w_reshaped = torch.reshape(w, [1, w.shape[0], w.shape[1], w.shape[2
            ], w.shape[3], w.shape[4], 1])
        output = (in_reshaped * w_reshaped).sum(-3)
        return output

    def _left_action_of_h_grid(self):
        fc1 = torch.stack([self.weight_fc1.roll(shifts=i, dims=-1) for i in
            range(self.N_h)], dim=1)
        fc2 = torch.stack([self.weight_fc2.roll(shifts=i, dims=-1) for i in
            range(self.N_h)], dim=1)
        return fc1, fc2

    def forward(self, input_0):
        primals_1 = self.weight_fc1
        primals_2 = self.weight_fc2
        primals_3 = input_0
        output = call([primals_1, primals_2, primals_3])
        return output[0]


Measured latency_ms: 0.064996