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
from torch._inductor.runtime.triton_helpers import math as tl_math
from torch import Tensor
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__softmax_mul_sum_0(in_ptr0, out_ptr2, out_ptr3, 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, float('-inf'))
    tmp4 = triton_helpers.max2(tmp3, 1)[:, None]
    tmp5 = tmp0 - tmp4
    tmp6 = tl_math.exp(tmp5)
    tmp7 = tl.broadcast_to(tmp6, [XBLOCK, RBLOCK])
    tmp9 = tl.where(xmask, tmp7, 0)
    tmp10 = tl.sum(tmp9, 1)[:, None]
    tmp11 = tmp6 / tmp10
    tmp12 = r1 // 4
    tl.full([1, 1], 0, tl.int64)
    tmp15 = tl.full([1, 1], 4, tl.int64)
    tmp16 = tmp12 < tmp15
    tmp17 = tl.broadcast_to(r1 // 4, [XBLOCK, RBLOCK])
    tmp18 = tl.full(tmp17.shape, 0.0, tmp17.dtype)
    tmp19 = tl.where(tmp16, tmp17, tmp18)
    tmp20 = tmp12 >= tmp15
    tl.full([1, 1], 8, tl.int64)
    tmp23 = tl.broadcast_to(r1 % 4, [XBLOCK, RBLOCK])
    tmp24 = tl.full(tmp23.shape, 0.0, tmp23.dtype)
    tmp25 = tl.where(tmp20, tmp23, tmp24)
    tmp26 = tl.where(tmp16, tmp19, tmp25)
    tmp27 = tmp26.to(tl.float32)
    tmp28 = tmp11 * tmp27
    tmp29 = tl.broadcast_to(tmp28, [XBLOCK, RBLOCK])
    tmp31 = tl.where(xmask, tmp29, 0)
    tmp32 = tl.sum(tmp31, 1)[:, None]
    tmp33 = 4 + r1 // 4
    tmp35 = tmp33 < tmp15
    tmp36 = tl.broadcast_to(4 + r1 // 4, [XBLOCK, RBLOCK])
    tmp37 = tl.full(tmp36.shape, 0.0, tmp36.dtype)
    tmp38 = tl.where(tmp35, tmp36, tmp37)
    tmp39 = tmp33 >= tmp15
    tmp41 = tl.where(tmp39, tmp23, tmp24)
    tmp42 = tl.where(tmp35, tmp38, tmp41)
    tmp43 = tmp42.to(tl.float32)
    tmp44 = tmp11 * tmp43
    tmp45 = tl.broadcast_to(tmp44, [XBLOCK, RBLOCK])
    tmp47 = tl.where(xmask, tmp45, 0)
    tmp48 = tl.sum(tmp47, 1)[:, None]
    tl.store(out_ptr2 + 2 * x0, tmp32, xmask)
    tl.store(out_ptr3 + 2 * x0, tmp48, 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)
        buf4 = empty_strided_cuda((16, 2), (2, 1), torch.float32)
        buf2 = reinterpret_tensor(buf4, (16, 1), (2, 1), 0)
        buf3 = reinterpret_tensor(buf4, (16, 1), (2, 1), 1)
        get_raw_stream(0)
        triton_per_fused__softmax_mul_sum_0[grid(16)](arg0_1, buf2, buf3, 
            16, 16, XBLOCK=8, num_warps=2, num_stages=1)
        del arg0_1
    return reinterpret_tensor(buf4, (4, 8), (8, 1), 0),


class SpatialSoftArgmaxNew(nn.Module):
    """Spatial softmax as defined in `1`_.

    Concretely, the spatial softmax of each feature map is used to compute a
    weighted mean of the pixel locations, effectively performing a soft arg-max
    over the feature dimension.

    .. _1: https://arxiv.org/abs/1504.00702
    """

    def __init__(self, normalize: 'bool'=False):
        """Constructor.

        Args:
            normalize: Whether to use normalized image coordinates, i.e.
                coordinates in the range `[-1, 1]`.
        """
        super().__init__()
        self.normalize = normalize

    def _coord_grid(self, h: 'int', w: 'int', device: 'torch.device') ->Tensor:
        if self.normalize:
            return torch.stack(torch.meshgrid(torch.linspace(-1, 1, w,
                device=device), torch.linspace(-1, 1, h, device=device)))
        return torch.stack(torch.meshgrid(torch.arange(0, w, device=device),
            torch.arange(0, h, device=device)))

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


Measured latency_ms: 0.0171