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, out_ptr1, 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)
    tmp7 = tmp6.to(tl.int64)
    tmp8 = tl.broadcast_to(tmp7, [XBLOCK, RBLOCK])
    _tmp9, tmp10 = triton_helpers.sort_with_index(tmp8, tmp4, None, 1,
        stable=False, descending=False)
    tl.store(out_ptr0 + (x0 + 16 * r2 + 64 * x1), tmp6, xmask)
    tl.store(out_ptr1 + (x0 + 16 * r2 + 64 * x1), tmp10, xmask)


@triton.jit
def triton_poi_fused_div_exp_mul_neg_sort_1(in_ptr0, in_ptr1, 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)
    tmp8 = tl.load(in_ptr1 + x0, xmask)
    tmp1 = tmp0.to(tl.int64)
    tmp2 = -tmp1
    tmp3 = tmp2.to(tl.float32)
    tmp4 = 0.25
    tmp5 = tmp3 * tmp4
    tmp6 = tl_math.exp(tmp5)
    tmp7 = -tmp6
    tmp9 = tmp7 * tmp8
    tmp10 = tl_math.exp(tmp9)
    tl.store(out_ptr0 + x0, tmp10, 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((4, 4, 4, 4), (64, 16, 4, 1), torch.int16)
        buf3 = 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, buf3, 64, 4, XBLOCK
            =1, num_warps=2, num_stages=1)
        del buf1
        buf4 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        triton_poi_fused_div_exp_mul_neg_sort_1[grid(256)](buf3, arg0_1,
            buf4, 256, XBLOCK=256, num_warps=4, num_stages=1)
        del arg0_1
        del buf3
    return buf4,


def rank_scaled_gaussian(distances, lambd):
    order = torch.argsort(distances, dim=1)
    ranks = torch.argsort(order, dim=1)
    return torch.exp(-torch.exp(-ranks / lambd) * distances)


class RankScaledGaussianPriorNew(torch.nn.Module):

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

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


Measured latency_ms: 0.028842