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 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_per_fused_div_sum_0(in_ptr0, out_ptr1, xnumel, rnumel, XBLOCK:
    tl.constexpr):
    RBLOCK: tl.constexpr = 16
    xoffset = tl.program_id(0) * XBLOCK
    xoffset + tl.arange(0, XBLOCK)[:, None]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    rindex = tl.arange(0, RBLOCK)[None, :]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    r0 = rindex
    tmp0 = tl.load(in_ptr0 + r0, None)
    tmp1 = tl.load(in_ptr0 + (16 + r0), None)
    tmp3 = tl.load(in_ptr0 + (32 + r0), None)
    tmp5 = tl.load(in_ptr0 + (48 + r0), None)
    tmp2 = tmp0 + tmp1
    tmp4 = tmp2 + tmp3
    tmp6 = tmp4 + tmp5
    tmp7 = 4.0
    tmp8 = tmp6 / tmp7
    tmp9 = tl.broadcast_to(tmp8, [XBLOCK, RBLOCK])
    tmp11 = tl.sum(tmp9, 1)[:, None]
    tmp12 = tmp8 / tmp11
    tl.store(out_ptr1 + tl.broadcast_to(r0, [XBLOCK, RBLOCK]), tmp12, None)


@triton.jit
def triton_poi_fused_div_1(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.
    constexpr):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x1 = xindex // 16
    x0 = xindex % 16
    x2 = xindex
    tmp3 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr1 + (x0 + 64 * x1), xmask)
    tmp5 = tl.load(in_ptr1 + (16 + x0 + 64 * x1), xmask)
    tmp7 = tl.load(in_ptr1 + (32 + x0 + 64 * x1), xmask)
    tmp9 = tl.load(in_ptr1 + (48 + x0 + 64 * x1), xmask)
    tmp0 = x1
    tmp1 = tl.full([1], 0, tl.int32)
    tmp2 = tmp0 == tmp1
    tmp6 = tmp4 + tmp5
    tmp8 = tmp6 + tmp7
    tmp10 = tmp8 + tmp9
    tmp11 = 4.0
    tmp12 = tmp10 / tmp11
    tmp13 = tl.where(tmp2, tmp3, tmp12)
    tl.store(out_ptr0 + x2, tmp13, xmask)


@triton.jit
def triton_per_fused_div_sum_2(in_ptr0, out_ptr0, out_ptr2, xnumel, rnumel,
    XBLOCK: tl.constexpr):
    RBLOCK: tl.constexpr = 16
    xoffset = tl.program_id(0) * XBLOCK
    xoffset + tl.arange(0, XBLOCK)[:, None]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    rindex = tl.arange(0, RBLOCK)[None, :]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    r0 = rindex
    tmp3 = tl.load(in_ptr0 + (r0 + 4 * (r0 % 4 // 4)), None)
    tmp4 = tl.load(in_ptr0 + (16 + r0 + 4 * (r0 % 4 // 4)), None)
    tmp12 = tl.load(in_ptr0 + (r0 + 8 * (r0 % 4 // 4)), None)
    tmp13 = tl.load(in_ptr0 + (16 + r0 + 8 * (r0 % 4 // 4)), None)
    tmp18 = tl.load(in_ptr0 + (32 + r0 + 8 * (r0 % 4 // 4)), None)
    tmp0 = tl.full([1, 1], 1, tl.int32)
    tmp1 = tl.full([1, 1], 0, tl.int32)
    tmp2 = tmp0 == tmp1
    tmp5 = tl.where(tmp2, tmp3, tmp4)
    tmp6 = tl.broadcast_to(tmp5, [XBLOCK, RBLOCK])
    tmp8 = tl.sum(tmp6, 1)[:, None]
    tmp9 = tl.full([1, 1], 2, tl.int32)
    tmp10 = tmp9 == tmp0
    tmp11 = tmp0 == tmp0
    tmp14 = tl.where(tmp2, tmp12, tmp13)
    tmp15 = tmp14 / tmp8
    tmp16 = tl.where(tmp11, tmp15, tmp14)
    tmp17 = tmp9 == tmp1
    tmp19 = tl.where(tmp17, tmp12, tmp18)
    tmp20 = tl.where(tmp10, tmp15, tmp19)
    tmp21 = tl.where(tmp10, tmp16, tmp20)
    tmp22 = tl.broadcast_to(tmp21, [XBLOCK, RBLOCK])
    tmp24 = tl.sum(tmp22, 1)[:, None]
    tmp25 = tmp21 / tmp24
    tl.store(out_ptr2 + tl.broadcast_to(r0, [XBLOCK, RBLOCK]), tmp25, None)
    tl.store(out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp8, None)


@triton.jit
def triton_poi_fused_div_3(in_ptr0, in_ptr1, in_ptr2, out_ptr0, xnumel,
    XBLOCK: tl.constexpr):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x1 = xindex // 16
    x0 = xindex % 16
    x2 = xindex
    tmp3 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
    tmp9 = tl.load(in_ptr1 + (x0 + 8 * (x0 % 4 // 4)), xmask,
        eviction_policy='evict_last')
    tmp10 = tl.load(in_ptr1 + (16 + x0 + 8 * (x0 % 4 // 4)), xmask,
        eviction_policy='evict_last')
    tmp12 = tl.load(in_ptr2 + 0)
    tmp13 = tl.broadcast_to(tmp12, [XBLOCK])
    tmp17 = tl.load(in_ptr1 + (x0 + 8 * (x0 % 4 // 4) + 16 * x1), xmask)
    tmp0 = x1
    tmp1 = tl.full([1], 2, tl.int32)
    tmp2 = tmp0 == tmp1
    tmp4 = tl.full([1], 1, tl.int32)
    tmp5 = tmp0 == tmp4
    tmp6 = tmp4 == tmp4
    tmp7 = tl.full([1], 0, tl.int32)
    tmp8 = tmp4 == tmp7
    tmp11 = tl.where(tmp8, tmp9, tmp10)
    tmp14 = tmp11 / tmp13
    tmp15 = tl.where(tmp6, tmp14, tmp11)
    tmp16 = tmp0 == tmp7
    tmp18 = tl.where(tmp16, tmp9, tmp17)
    tmp19 = tl.where(tmp5, tmp14, tmp18)
    tmp20 = tl.where(tmp5, tmp15, tmp19)
    tmp21 = tl.where(tmp2, tmp3, tmp20)
    tl.store(out_ptr0 + x2, tmp21, xmask)


@triton.jit
def triton_per_fused_sum_4(in_ptr0, out_ptr0, xnumel, rnumel, XBLOCK: tl.
    constexpr):
    RBLOCK: tl.constexpr = 16
    xoffset = tl.program_id(0) * XBLOCK
    xoffset + tl.arange(0, XBLOCK)[:, None]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    rindex = tl.arange(0, RBLOCK)[None, :]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    r0 = rindex
    tmp3 = tl.load(in_ptr0 + (32 + r0 + 4 * (r0 % 4 // 4)), None)
    tmp4 = tl.load(in_ptr0 + (48 + r0 + 4 * (r0 % 4 // 4)), None)
    tmp0 = tl.full([1, 1], 3, tl.int32)
    tmp1 = tl.full([1, 1], 2, tl.int32)
    tmp2 = tmp0 == tmp1
    tmp5 = tl.where(tmp2, tmp3, tmp4)
    tmp6 = tl.broadcast_to(tmp5, [XBLOCK, RBLOCK])
    tmp8 = tl.sum(tmp6, 1)[:, None]
    tl.store(out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp8, None)


@triton.jit
def triton_poi_fused_5(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.constexpr
    ):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x1 = xindex // 16
    x0 = xindex % 16
    x2 = xindex
    tmp6 = tl.load(in_ptr0 + (32 + x0 + 8 * (x0 % 4 // 4)), xmask,
        eviction_policy='evict_last')
    tmp7 = tl.load(in_ptr0 + (48 + x0 + 8 * (x0 % 4 // 4)), xmask,
        eviction_policy='evict_last')
    tmp9 = tl.load(in_ptr1 + 0)
    tmp10 = tl.broadcast_to(tmp9, [XBLOCK])
    tmp14 = tl.load(in_ptr0 + (x0 + 8 * (x0 % 4 // 4) + 16 * x1), xmask)
    tmp0 = x1
    tmp1 = tl.full([1], 3, tl.int32)
    tmp2 = tmp0 == tmp1
    tmp3 = tmp1 == tmp1
    tmp4 = tl.full([1], 2, tl.int32)
    tmp5 = tmp1 == tmp4
    tmp8 = tl.where(tmp5, tmp6, tmp7)
    tmp11 = tmp8 / tmp10
    tmp12 = tl.where(tmp3, tmp11, tmp8)
    tmp13 = tmp0 == tmp4
    tmp15 = tl.where(tmp13, tmp6, tmp14)
    tmp16 = tl.where(tmp2, tmp11, tmp15)
    tmp17 = tl.where(tmp2, tmp12, tmp16)
    tl.store(out_ptr0 + x2, tmp17, xmask)


def call(args):
    arg0_1, = args
    args.clear()
    assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf1 = empty_strided_cuda((16,), (1,), torch.float32)
        get_raw_stream(0)
        triton_per_fused_div_sum_0[grid(1)](arg0_1, buf1, 1, 16, XBLOCK=1,
            num_warps=2, num_stages=1)
        buf2 = empty_strided_cuda((4, 16), (16, 1), torch.float32)
        triton_poi_fused_div_1[grid(64)](buf1, arg0_1, buf2, 64, XBLOCK=64,
            num_warps=1, num_stages=1)
        del arg0_1
        buf3 = empty_strided_cuda((), (), torch.float32)
        buf5 = buf1
        del buf1
        triton_per_fused_div_sum_2[grid(1)](buf2, buf3, buf5, 1, 16, XBLOCK
            =1, num_warps=2, num_stages=1)
        buf6 = empty_strided_cuda((4, 16), (16, 1), torch.float32)
        triton_poi_fused_div_3[grid(64)](buf5, buf2, buf3, buf6, 64, XBLOCK
            =64, num_warps=1, num_stages=1)
        del buf5
        buf7 = buf3
        del buf3
        triton_per_fused_sum_4[grid(1)](buf6, buf7, 1, 16, XBLOCK=1,
            num_warps=2, num_stages=1)
        buf8 = buf2
        del buf2
        triton_poi_fused_5[grid(64)](buf6, buf7, buf8, 64, XBLOCK=64,
            num_warps=1, num_stages=1)
        del buf6
        del buf7
    return reinterpret_tensor(buf8, (4, 1, 4, 4), (16, 16, 4, 1), 0),


class SpatialAttnNew(nn.Module):
    """Spatial Attention Layer"""

    def __init__(self):
        super(SpatialAttnNew, self).__init__()

    def forward(self, input_0):
        arg0_1 = input_0
        output = call([arg0_1])
        return output[0]


Measured latency_ms: 0.078292