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
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda


@triton.jit
def triton_per_fused_sort_0(in_ptr0, out_ptr0, xnumel, rnumel, XBLOCK: tl.
    constexpr):
    xnumel = 64
    RBLOCK: tl.constexpr = 4
    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
    x0 = xindex % 16
    x1 = xindex // 16
    tmp0 = tl.load(in_ptr0 + (x0 + 16 * r2 + 64 * x1), xmask, other=0.0)
    tmp1 = r2
    tmp2 = tmp1.to(tl.int16)
    tmp3 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
    tmp4 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK])
    _tmp5, tmp6 = triton_helpers.sort_with_index(tmp3, tmp4, None, 1,
        stable=False, descending=False)
    tl.store(out_ptr0 + (x0 + 16 * r2 + 64 * x1), tmp6, xmask)


@triton.jit
def triton_poi_fused_sort_1(in_ptr0, 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
    tmp0 = tl.load(in_ptr0 + x0, xmask)
    tmp1 = tmp0.to(tl.int64)
    tl.store(out_ptr0 + x0, tmp1, xmask)


@triton.jit
def triton_per_fused_sort_2(in_ptr0, out_ptr0, xnumel, rnumel, XBLOCK: tl.
    constexpr):
    xnumel = 64
    RBLOCK: tl.constexpr = 4
    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
    x0 = xindex % 16
    x1 = xindex // 16
    tmp0 = tl.load(in_ptr0 + (x0 + 16 * r2 + 64 * x1), xmask, other=0.0)
    tmp1 = r2
    tmp2 = tmp1.to(tl.int16)
    tmp3 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
    tmp4 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK])
    _tmp5, tmp6 = triton_helpers.sort_with_index(tmp3, tmp4, None, 1,
        stable=False, descending=False)
    tl.store(out_ptr0 + (x0 + 16 * r2 + 64 * x1), tmp6, xmask)


@triton.jit
def triton_per_fused_div_exp_mul_neg_sort_sum_3(in_ptr0, in_ptr1, out_ptr0,
    xnumel, rnumel):
    XBLOCK: tl.constexpr = 1
    RBLOCK: tl.constexpr = 256
    xoffset = tl.program_id(0) * XBLOCK
    tl.full([1], xoffset, tl.int32)
    tl.full([RBLOCK], True, tl.int1)
    rindex = tl.arange(0, RBLOCK)[:]
    tl.full([RBLOCK], True, tl.int1)
    r0 = rindex
    tmp0 = tl.load(in_ptr0 + r0, None)
    tmp7 = tl.load(in_ptr1 + r0, None)
    tmp1 = tmp0.to(tl.int64)
    tmp2 = -tmp1
    tmp3 = tmp2.to(tl.float32)
    tmp4 = 0.25
    tmp5 = tmp3 * tmp4
    tmp6 = tl_math.exp(tmp5)
    tmp8 = tmp6 * tmp7
    tmp9 = tl.broadcast_to(tmp8, [RBLOCK])
    tmp11 = triton_helpers.promote_to_tensor(tl.sum(tmp9, 0))
    tl.store(out_ptr0 + tl.full([1], 0, tl.int32), tmp11, None)


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((4, 4, 4, 4), (64, 16, 4, 1), torch.int16)
        get_raw_stream(0)
        triton_per_fused_sort_0[grid(64)](arg0_1, buf1, 64, 4, XBLOCK=1,
            num_warps=2, num_stages=1)
        buf2 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.int64)
        triton_poi_fused_sort_1[grid(256)](buf1, buf2, 256, XBLOCK=256,
            num_warps=4, num_stages=1)
        buf4 = buf1
        del buf1
        triton_per_fused_sort_2[grid(64)](buf2, buf4, 64, 4, XBLOCK=32,
            num_warps=2, num_stages=1)
        buf5 = empty_strided_cuda((), (), torch.float32)
        triton_per_fused_div_exp_mul_neg_sort_sum_3[grid(1)](buf4, arg0_1,
            buf5, 1, 256, num_warps=2, num_stages=1)
        del arg0_1
        del buf4
    return buf5, buf2


class NeuralGasEnergyNew(torch.nn.Module):

    def __init__(self, lm):
        super().__init__()
        self.lm = lm

    def extra_repr(self):
        return f'lambda: {self.lm}'

    @staticmethod
    def _nghood_fn(rankings, lm):
        return torch.exp(-rankings / lm)

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


Measured latency_ms: 0.050746