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 math as tl_math
import torch.nn as nn
import torch.nn.parallel
import torch.onnx
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_avg_pool2d_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.
    constexpr):
    xnumel = 15376
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 31
    x1 = xindex // 31 % 31
    x4 = xindex // 961
    x3 = xindex // 3844
    x5 = xindex % 3844
    tmp0 = tl.load(in_ptr0 + (2 * x0 + 128 * x1 + 4096 * x4), xmask,
        eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr0 + (1 + 2 * x0 + 128 * x1 + 4096 * x4), xmask,
        eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr0 + (2 + 2 * x0 + 128 * x1 + 4096 * x4), xmask,
        eviction_policy='evict_last')
    tmp5 = tl.load(in_ptr0 + (64 + 2 * x0 + 128 * x1 + 4096 * x4), xmask,
        eviction_policy='evict_last')
    tmp7 = tl.load(in_ptr0 + (65 + 2 * x0 + 128 * x1 + 4096 * x4), xmask,
        eviction_policy='evict_last')
    tmp9 = tl.load(in_ptr0 + (66 + 2 * x0 + 128 * x1 + 4096 * x4), xmask,
        eviction_policy='evict_last')
    tmp11 = tl.load(in_ptr0 + (128 + 2 * x0 + 128 * x1 + 4096 * x4), xmask,
        eviction_policy='evict_last')
    tmp13 = tl.load(in_ptr0 + (129 + 2 * x0 + 128 * x1 + 4096 * x4), xmask,
        eviction_policy='evict_last')
    tmp15 = tl.load(in_ptr0 + (130 + 2 * x0 + 128 * x1 + 4096 * x4), xmask,
        eviction_policy='evict_last')
    tmp2 = tmp1 + tmp0
    tmp4 = tmp3 + tmp2
    tmp6 = tmp5 + tmp4
    tmp8 = tmp7 + tmp6
    tmp10 = tmp9 + tmp8
    tmp12 = tmp11 + tmp10
    tmp14 = tmp13 + tmp12
    tmp16 = tmp15 + tmp14
    tmp17 = 0.1111111111111111
    tmp18 = tmp16 * tmp17
    tl.store(out_ptr0 + (x5 + 3872 * x3), tmp18, xmask)


@triton.jit
def triton_poi_fused_max_pool2d_with_indices_1(in_ptr0, out_ptr0, xnumel,
    XBLOCK: tl.constexpr):
    xnumel = 3600
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 15
    x1 = xindex // 15 % 15
    x2 = xindex // 225 % 4
    x3 = xindex // 900
    x4 = xindex
    tmp0 = tl.load(in_ptr0 + (2 * x0 + 62 * x1 + 961 * x2 + 3872 * x3),
        xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr0 + (1 + 2 * x0 + 62 * x1 + 961 * x2 + 3872 * x3),
        xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr0 + (2 + 2 * x0 + 62 * x1 + 961 * x2 + 3872 * x3),
        xmask, eviction_policy='evict_last')
    tmp5 = tl.load(in_ptr0 + (31 + 2 * x0 + 62 * x1 + 961 * x2 + 3872 * x3),
        xmask, eviction_policy='evict_last')
    tmp7 = tl.load(in_ptr0 + (32 + 2 * x0 + 62 * x1 + 961 * x2 + 3872 * x3),
        xmask, eviction_policy='evict_last')
    tmp9 = tl.load(in_ptr0 + (33 + 2 * x0 + 62 * x1 + 961 * x2 + 3872 * x3),
        xmask, eviction_policy='evict_last')
    tmp11 = tl.load(in_ptr0 + (62 + 2 * x0 + 62 * x1 + 961 * x2 + 3872 * x3
        ), xmask, eviction_policy='evict_last')
    tmp13 = tl.load(in_ptr0 + (63 + 2 * x0 + 62 * x1 + 961 * x2 + 3872 * x3
        ), xmask, eviction_policy='evict_last')
    tmp15 = tl.load(in_ptr0 + (64 + 2 * x0 + 62 * x1 + 961 * x2 + 3872 * x3
        ), xmask, eviction_policy='evict_last')
    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)
    tl.store(out_ptr0 + x4, tmp16, xmask)


@triton.jit
def triton_poi_fused__softmax_mul_2(in_ptr0, in_ptr1, out_ptr0, out_ptr1,
    ynumel, xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.constexpr):
    ynumel = 60
    xnumel = 15
    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 % 15
    y1 = yindex // 15
    y3 = yindex
    tmp0 = tl.load(in_ptr0 + (y0 + 15 * x2 + 900 * y1), xmask & ymask,
        eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr1 + (x2 + 15 * y0 + 900 * y1), xmask & ymask,
        eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr0 + (225 + y0 + 15 * x2 + 900 * y1), xmask & ymask,
        eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr1 + (225 + x2 + 15 * y0 + 900 * y1), xmask & ymask,
        eviction_policy='evict_last')
    tmp7 = tl.load(in_ptr0 + (450 + y0 + 15 * x2 + 900 * y1), xmask & ymask,
        eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr1 + (450 + x2 + 15 * y0 + 900 * y1), xmask & ymask,
        eviction_policy='evict_last')
    tmp11 = tl.load(in_ptr0 + (675 + y0 + 15 * x2 + 900 * y1), xmask &
        ymask, eviction_policy='evict_last')
    tmp12 = tl.load(in_ptr1 + (675 + x2 + 15 * y0 + 900 * y1), xmask &
        ymask, eviction_policy='evict_last')
    tmp2 = tmp0 * tmp1
    tmp5 = tmp3 * tmp4
    tmp6 = triton_helpers.maximum(tmp2, tmp5)
    tmp9 = tmp7 * tmp8
    tmp10 = triton_helpers.maximum(tmp6, tmp9)
    tmp13 = tmp11 * tmp12
    tmp14 = triton_helpers.maximum(tmp10, tmp13)
    tmp15 = tmp2 - tmp14
    tmp16 = tl_math.exp(tmp15)
    tmp17 = tmp5 - tmp14
    tmp18 = tl_math.exp(tmp17)
    tmp19 = tmp16 + tmp18
    tmp20 = tmp9 - tmp14
    tmp21 = tl_math.exp(tmp20)
    tmp22 = tmp19 + tmp21
    tmp23 = tmp13 - tmp14
    tmp24 = tl_math.exp(tmp23)
    tmp25 = tmp22 + tmp24
    tl.store(out_ptr0 + (x2 + 15 * y3), tmp14, xmask & ymask)
    tl.store(out_ptr1 + (x2 + 15 * y3), tmp25, xmask & ymask)


@triton.jit
def triton_poi_fused__softmax_mul_3(in_ptr0, in_ptr1, in_ptr2, in_ptr3,
    in_ptr4, out_ptr0, ynumel, xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.
    constexpr):
    ynumel = 240
    xnumel = 15
    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
    x3 = xindex
    y4 = yindex
    y0 = yindex % 15
    y5 = yindex // 15
    y2 = yindex // 60
    tmp0 = tl.load(in_ptr0 + (x3 + 15 * y4), xmask & ymask, eviction_policy
        ='evict_last')
    tmp1 = tl.load(in_ptr1 + (y0 + 15 * x3 + 225 * y5), xmask & ymask,
        eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr2 + (x3 + 15 * y4), xmask & ymask, eviction_policy
        ='evict_last')
    tmp4 = tl.load(in_ptr3 + (x3 + 15 * y0 + 225 * y2), xmask & ymask,
        eviction_policy='evict_last')
    tmp7 = tl.load(in_ptr4 + (x3 + 15 * y0 + 225 * y2), xmask & ymask,
        eviction_policy='evict_last')
    tmp3 = tmp1 * tmp2
    tmp5 = tmp3 - tmp4
    tmp6 = tl_math.exp(tmp5)
    tmp8 = tmp6 / tmp7
    tmp9 = tmp0 * tmp8
    tl.store(out_ptr0 + (x3 + 15 * y4), tmp9, xmask & ymask)


@triton.jit
def triton_poi_fused__to_copy_4(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.5
    tmp3 = tmp1 + tmp2
    tmp4 = 3.75
    tmp5 = tmp3 * tmp4
    tmp6 = tmp5 - tmp2
    tmp7 = 0.0
    tmp8 = triton_helpers.maximum(tmp6, tmp7)
    tmp9 = tmp8.to(tl.int32)
    tl.store(out_ptr0 + x0, tmp9, xmask)


@triton.jit
def triton_poi_fused_add_clamp_5(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.5
    tmp3 = tmp1 + tmp2
    tmp4 = 3.75
    tmp5 = tmp3 * tmp4
    tmp6 = tmp5 - tmp2
    tmp7 = 0.0
    tmp8 = triton_helpers.maximum(tmp6, tmp7)
    tmp9 = tmp8.to(tl.int32)
    tmp10 = tl.full([1], 1, tl.int64)
    tmp11 = tmp9 + tmp10
    tmp12 = tl.full([1], 14, tl.int64)
    tmp13 = triton_helpers.minimum(tmp11, tmp12)
    tl.store(out_ptr0 + x0, tmp13, xmask)


@triton.jit
def triton_poi_fused__to_copy_add_arange_clamp_mul_sub_6(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.5
    tmp3 = tmp1 + tmp2
    tmp4 = 3.75
    tmp5 = tmp3 * tmp4
    tmp6 = tmp5 - tmp2
    tmp7 = 0.0
    tmp8 = triton_helpers.maximum(tmp6, tmp7)
    tmp9 = tmp8.to(tl.int32)
    tmp10 = tmp9.to(tl.float32)
    tmp11 = tmp8 - tmp10
    tmp12 = triton_helpers.maximum(tmp11, tmp7)
    tmp13 = 1.0
    tmp14 = triton_helpers.minimum(tmp12, tmp13)
    tl.store(out_ptr0 + x0, tmp14, xmask)


@triton.jit
def triton_poi_fused__unsafe_index_add_mul_sub_7(in_out_ptr0, in_ptr0,
    in_ptr1, in_ptr2, in_ptr3, in_ptr4, in_ptr5, in_ptr6, xnumel, XBLOCK:
    tl.constexpr):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x1 = xindex // 4 % 4
    x0 = xindex % 4
    x2 = xindex // 16
    x4 = xindex
    tmp0 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last')
    tmp5 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last')
    tmp10 = tl.load(in_ptr3 + x0, xmask, eviction_policy='evict_last')
    tmp16 = tl.load(in_ptr4 + x0, xmask, eviction_policy='evict_last')
    tmp19 = tl.load(in_ptr5 + x1, xmask, eviction_policy='evict_last')
    tmp29 = tl.load(in_ptr6 + x1, xmask, eviction_policy='evict_last')
    tmp1 = tl.full([XBLOCK], 15, tl.int32)
    tmp2 = tmp0 + tmp1
    tmp3 = tmp0 < 0
    tmp4 = tl.where(tmp3, tmp2, tmp0)
    tmp6 = tmp5 + tmp1
    tmp7 = tmp5 < 0
    tmp8 = tl.where(tmp7, tmp6, tmp5)
    tmp9 = tl.load(in_ptr2 + (tmp8 + 15 * tmp4 + 225 * x2), xmask,
        eviction_policy='evict_last')
    tmp11 = tmp10 + tmp1
    tmp12 = tmp10 < 0
    tmp13 = tl.where(tmp12, tmp11, tmp10)
    tmp14 = tl.load(in_ptr2 + (tmp13 + 15 * tmp4 + 225 * x2), xmask,
        eviction_policy='evict_last')
    tmp15 = tmp14 - tmp9
    tmp17 = tmp15 * tmp16
    tmp18 = tmp9 + tmp17
    tmp20 = tmp19 + tmp1
    tmp21 = tmp19 < 0
    tmp22 = tl.where(tmp21, tmp20, tmp19)
    tmp23 = tl.load(in_ptr2 + (tmp8 + 15 * tmp22 + 225 * x2), xmask,
        eviction_policy='evict_last')
    tmp24 = tl.load(in_ptr2 + (tmp13 + 15 * tmp22 + 225 * x2), xmask,
        eviction_policy='evict_last')
    tmp25 = tmp24 - tmp23
    tmp26 = tmp25 * tmp16
    tmp27 = tmp23 + tmp26
    tmp28 = tmp27 - tmp18
    tmp30 = tmp28 * tmp29
    tmp31 = tmp18 + tmp30
    tl.store(in_out_ptr0 + x4, tmp31, xmask)


def call(args):
    primals_1, primals_2, primals_3, primals_4 = 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, 4, 1, 1), (4, 1, 1, 1))
    assert_size_stride(primals_4, (4, 4, 1, 1), (4, 1, 1, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 4, 31, 31), (3872, 961, 31, 1), torch
            .float32)
        get_raw_stream(0)
        triton_poi_fused_avg_pool2d_0[grid(15376)](primals_1, buf0, 15376,
            XBLOCK=128, num_warps=4, num_stages=1)
        del primals_1
        buf1 = empty_strided_cuda((4, 4, 15, 15), (900, 225, 15, 1), torch.
            float32)
        triton_poi_fused_max_pool2d_with_indices_1[grid(3600)](buf0, buf1, 
            3600, XBLOCK=256, num_warps=4, num_stages=1)
        del buf0
        buf2 = extern_kernels.convolution(buf1, primals_2, stride=(1, 1),
            padding=(0, 0), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf2, (4, 4, 15, 15), (900, 225, 15, 1))
        buf3 = extern_kernels.convolution(buf1, primals_3, stride=(1, 1),
            padding=(0, 0), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf3, (4, 4, 15, 15), (900, 225, 15, 1))
        buf4 = extern_kernels.convolution(buf1, primals_4, stride=(1, 1),
            padding=(0, 0), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf4, (4, 4, 15, 15), (900, 225, 15, 1))
        buf5 = empty_strided_cuda((4, 1, 15, 15), (225, 900, 15, 1), torch.
            float32)
        buf6 = empty_strided_cuda((4, 1, 15, 15), (225, 900, 15, 1), torch.
            float32)
        triton_poi_fused__softmax_mul_2[grid(60, 15)](buf2, buf3, buf5,
            buf6, 60, 15, XBLOCK=4, YBLOCK=64, num_warps=4, num_stages=1)
        buf7 = empty_strided_cuda((4, 4, 15, 15), (900, 225, 15, 1), torch.
            float32)
        triton_poi_fused__softmax_mul_3[grid(240, 15)](buf4, buf2, buf3,
            buf5, buf6, buf7, 240, 15, XBLOCK=16, YBLOCK=16, num_warps=4,
            num_stages=1)
        del buf5
        del buf6
        buf8 = empty_strided_cuda((4, 1), (1, 1), torch.int64)
        triton_poi_fused__to_copy_4[grid(4)](buf8, 4, XBLOCK=4, num_warps=1,
            num_stages=1)
        buf9 = empty_strided_cuda((4, 1), (1, 1), torch.int64)
        triton_poi_fused_add_clamp_5[grid(4)](buf9, 4, XBLOCK=4, num_warps=
            1, num_stages=1)
        buf10 = empty_strided_cuda((4,), (1,), torch.int64)
        triton_poi_fused__to_copy_4[grid(4)](buf10, 4, XBLOCK=4, num_warps=
            1, num_stages=1)
        buf11 = empty_strided_cuda((4,), (1,), torch.int64)
        triton_poi_fused_add_clamp_5[grid(4)](buf11, 4, XBLOCK=4, num_warps
            =1, num_stages=1)
        buf12 = empty_strided_cuda((4,), (1,), torch.float32)
        triton_poi_fused__to_copy_add_arange_clamp_mul_sub_6[grid(4)](buf12,
            4, XBLOCK=4, num_warps=1, num_stages=1)
        buf14 = empty_strided_cuda((4, 1), (1, 1), torch.float32)
        triton_poi_fused__to_copy_add_arange_clamp_mul_sub_6[grid(4)](buf14,
            4, XBLOCK=4, num_warps=1, num_stages=1)
        buf13 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        buf15 = buf13
        del buf13
        triton_poi_fused__unsafe_index_add_mul_sub_7[grid(256)](buf15, buf8,
            buf10, buf7, buf11, buf12, buf9, buf14, 256, XBLOCK=256,
            num_warps=4, num_stages=1)
        del buf7
    return (buf15, primals_2, primals_3, primals_4, buf1, buf2, buf3, buf4,
        buf8, buf9, buf10, buf11, buf12, buf14)


def conv1x1(in_planes, out_planes, bias=False):
    """1x1 convolution with padding"""
    return nn.Conv2d(in_planes, out_planes, kernel_size=1, stride=1,
        padding=0, bias=bias)


class SpatialAttentionModuleNew(nn.Module):

    def __init__(self, input_size, input_channels):
        super(SpatialAttentionModuleNew, self).__init__()
        self.avgPool = nn.AvgPool2d(kernel_size=3, stride=2)
        self.maxPool = nn.MaxPool2d(kernel_size=3, stride=2)
        self.hidden_size = input_size // 2 - 1
        self.upsampling = nn.Upsample(size=(input_size, input_size), mode=
            'bilinear')
        self.conv_f = conv1x1(input_channels, input_channels)
        self.conv_g = conv1x1(input_channels, input_channels)
        self.conv_h = conv1x1(input_channels, input_channels)
        self.softmax = torch.nn.Softmax()

    def forward(self, input_0):
        primals_2 = self.conv_f.weight
        primals_3 = self.conv_g.weight
        primals_4 = self.conv_h.weight
        primals_1 = input_0
        output = call([primals_1, primals_2, primals_3, primals_4])
        return output[0]


Measured latency_ms: 0.180938