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 libdevice, math as tl_math
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_poi_fused_add_clamp_mul_sqrt_sub_0(in_out_ptr0, in_ptr0, xnumel,
    XBLOCK: tl.constexpr):
    xnumel = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x1 = xindex // 4
    x0 = xindex % 4
    x2 = xindex
    tmp0 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp5 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp11 = tl.load(in_ptr0 + 4 * x0, xmask, eviction_policy='evict_last')
    tmp13 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp16 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp19 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp23 = tl.load(in_out_ptr0 + x2, xmask)
    tmp1 = tmp0 * tmp0
    tmp3 = tmp2 * tmp2
    tmp4 = tmp1 + tmp3
    tmp6 = tmp5 * tmp5
    tmp7 = tmp4 + tmp6
    tmp9 = tmp8 * tmp8
    tmp10 = tmp7 + tmp9
    tmp12 = tmp11 * tmp11
    tmp14 = tmp13 * tmp13
    tmp15 = tmp12 + tmp14
    tmp17 = tmp16 * tmp16
    tmp18 = tmp15 + tmp17
    tmp20 = tmp19 * tmp19
    tmp21 = tmp18 + tmp20
    tmp22 = tmp10 + tmp21
    tmp24 = 2.0
    tmp25 = tmp23 * tmp24
    tmp26 = tmp22 - tmp25
    tmp27 = 1e-12
    tmp28 = triton_helpers.maximum(tmp26, tmp27)
    tmp29 = libdevice.sqrt(tmp28)
    tl.store(in_out_ptr0 + x2, tmp29, xmask)


@triton.jit
def triton_poi_fused_clamp_index_put_lift_fresh_sqrt_1(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 = tl.full([1], 2, tl.int64)
    tmp2 = tmp0 < tmp1
    tmp3 = tl.full([1], 1, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp5 = tl.full([1], 0, tl.int64)
    tmp6 = tl.where(tmp4, tmp5, tmp3)
    tmp7 = tl.full([1], 3, tl.int64)
    tmp8 = tmp0 < tmp7
    tmp9 = tl.where(tmp8, tmp1, tmp7)
    tmp10 = tl.where(tmp2, tmp6, tmp9)
    tmp11 = 0.0
    tl.store(out_ptr0 + tl.broadcast_to(5 * tmp10, [XBLOCK]), tmp11, xmask)


@triton.jit
def triton_per_fused_mul_pow_sort_2(in_ptr0, out_ptr0, xnumel, rnumel,
    XBLOCK: tl.constexpr):
    xnumel = 4
    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)
    r1 = rindex
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + (r1 + 4 * x0), xmask, other=0.0)
    tmp1 = tmp0 * tmp0
    tmp2 = tmp1 * tmp0
    tmp3 = -3.0
    tmp4 = tmp2 * tmp3
    tmp5 = r1
    tmp6 = tmp5.to(tl.int16)
    tmp7 = tl.broadcast_to(tmp4, [XBLOCK, RBLOCK])
    tmp8 = tl.broadcast_to(tmp6, [XBLOCK, RBLOCK])
    _tmp9, tmp10 = triton_helpers.sort_with_index(tmp7, tmp8, None, 1,
        stable=False, descending=True)
    tl.store(out_ptr0 + (r1 + 4 * x0), tmp10, xmask)


@triton.jit
def triton_poi_fused_gather_logsumexp_mul_pow_3(in_ptr0, in_ptr1, out_ptr0,
    out_ptr1, xnumel, XBLOCK: tl.constexpr):
    xnumel = 4
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last')
    tmp12 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp23 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp1 = tmp0.to(tl.int64)
    tmp2 = tl.full([XBLOCK], 4, tl.int32)
    tmp3 = tmp1 + tmp2
    tmp4 = tmp1 < 0
    tmp5 = tl.where(tmp4, tmp3, tmp1)
    tl.device_assert((0 <= tmp5) & (tmp5 < 4) | ~xmask,
        'index out of bounds: 0 <= tmp5 < 4')
    tmp7 = tl.load(in_ptr1 + (tmp5 + 4 * x0), xmask, eviction_policy=
        'evict_last')
    tmp8 = tmp7 * tmp7
    tmp9 = tmp8 * tmp7
    tmp10 = -3.0
    tmp11 = tmp9 * tmp10
    tmp13 = tmp12.to(tl.int64)
    tmp14 = tmp13 + tmp2
    tmp15 = tmp13 < 0
    tmp16 = tl.where(tmp15, tmp14, tmp13)
    tl.device_assert((0 <= tmp16) & (tmp16 < 4) | ~xmask,
        'index out of bounds: 0 <= tmp16 < 4')
    tmp18 = tl.load(in_ptr1 + (tmp16 + 4 * x0), xmask, eviction_policy=
        'evict_last')
    tmp19 = tmp18 * tmp18
    tmp20 = tmp19 * tmp18
    tmp21 = tmp20 * tmp10
    tmp22 = triton_helpers.maximum(tmp11, tmp21)
    tmp24 = tmp23.to(tl.int64)
    tmp25 = tmp24 + tmp2
    tmp26 = tmp24 < 0
    tmp27 = tl.where(tmp26, tmp25, tmp24)
    tl.device_assert((0 <= tmp27) & (tmp27 < 4) | ~xmask,
        'index out of bounds: 0 <= tmp27 < 4')
    tmp29 = tl.load(in_ptr1 + (tmp27 + 4 * x0), xmask, eviction_policy=
        'evict_last')
    tmp30 = tmp29 * tmp29
    tmp31 = tmp30 * tmp29
    tmp32 = tmp31 * tmp10
    tmp33 = triton_helpers.maximum(tmp22, tmp32)
    tmp34 = tl_math.abs(tmp33)
    tmp35 = float('inf')
    tmp36 = tmp34 == tmp35
    tmp37 = 0.0
    tmp38 = tl.where(tmp36, tmp37, tmp33)
    tmp39 = tmp11 - tmp38
    tmp40 = tl_math.exp(tmp39)
    tmp41 = tmp21 - tmp38
    tmp42 = tl_math.exp(tmp41)
    tmp43 = tmp40 + tmp42
    tmp44 = tmp32 - tmp38
    tmp45 = tl_math.exp(tmp44)
    tmp46 = tmp43 + tmp45
    tl.store(out_ptr0 + x0, tmp33, xmask)
    tl.store(out_ptr1 + x0, tmp46, xmask)


@triton.jit
def triton_poi_fused_stack_4(in_ptr0, in_ptr1, in_ptr2, in_ptr3, out_ptr0,
    xnumel, XBLOCK: tl.constexpr):
    xnumel = 12
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 3
    x1 = xindex // 3
    x2 = xindex
    tmp0 = x0
    tl.full([1], 0, tl.int64)
    tmp3 = tl.full([1], 1, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp5 = tl.load(in_ptr0 + x1, tmp4 & xmask, eviction_policy='evict_last',
        other=0.0)
    tmp6 = tl_math.log(tmp5)
    tmp7 = tl.load(in_ptr1 + x1, tmp4 & xmask, eviction_policy='evict_last',
        other=0.0)
    tmp8 = tl_math.abs(tmp7)
    tmp9 = float('inf')
    tmp10 = tmp8 == tmp9
    tmp11 = 0.0
    tmp12 = tl.where(tmp10, tmp11, tmp7)
    tmp13 = tmp6 + tmp12
    tmp14 = tl.full(tmp13.shape, 0.0, tmp13.dtype)
    tmp15 = tl.where(tmp4, tmp13, tmp14)
    tmp16 = tmp0 >= tmp3
    tmp17 = tl.full([1], 2, tl.int64)
    tmp18 = tmp0 < tmp17
    tmp19 = tmp16 & tmp18
    tmp20 = tl.load(in_ptr2 + (2 + 4 * x1), tmp19 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp21 = tmp20.to(tl.int64)
    tmp22 = tl.full([XBLOCK], 4, tl.int32)
    tmp23 = tmp21 + tmp22
    tmp24 = tmp21 < 0
    tmp25 = tl.where(tmp24, tmp23, tmp21)
    tl.device_assert((0 <= tl.broadcast_to(tmp25, [XBLOCK])) & (tl.
        broadcast_to(tmp25, [XBLOCK]) < 4) | ~(tmp19 & xmask),
        'index out of bounds: 0 <= tl.broadcast_to(tmp25, [XBLOCK]) < 4')
    tmp27 = tl.load(in_ptr3 + (tmp25 + 4 * x1), tmp19 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp28 = tmp27 * tmp27
    tmp29 = tmp28 * tmp27
    tmp30 = -3.0
    tmp31 = tmp29 * tmp30
    tmp32 = tl.load(in_ptr2 + (3 + 4 * x1), tmp19 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp33 = tmp32.to(tl.int64)
    tmp34 = tmp33 + tmp22
    tmp35 = tmp33 < 0
    tmp36 = tl.where(tmp35, tmp34, tmp33)
    tl.device_assert((0 <= tl.broadcast_to(tmp36, [XBLOCK])) & (tl.
        broadcast_to(tmp36, [XBLOCK]) < 4) | ~(tmp19 & xmask),
        'index out of bounds: 0 <= tl.broadcast_to(tmp36, [XBLOCK]) < 4')
    tmp38 = tl.load(in_ptr3 + (tmp36 + 4 * x1), tmp19 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp39 = tmp38 * tmp38
    tmp40 = tmp39 * tmp38
    tmp41 = tmp40 * tmp30
    tmp42 = triton_helpers.maximum(tmp31, tmp41)
    tmp43 = tl_math.abs(tmp42)
    tmp44 = tmp43 == tmp9
    tmp45 = tl.where(tmp44, tmp11, tmp42)
    tmp46 = tmp31 - tmp45
    tmp47 = tl_math.exp(tmp46)
    tmp48 = tmp41 - tmp45
    tmp49 = tl_math.exp(tmp48)
    tmp50 = tmp47 + tmp49
    tmp51 = tl_math.log(tmp50)
    tmp52 = tmp51 + tmp45
    tmp53 = tl.full(tmp52.shape, 0.0, tmp52.dtype)
    tmp54 = tl.where(tmp19, tmp52, tmp53)
    tmp55 = tmp0 >= tmp17
    tl.full([1], 3, tl.int64)
    tmp58 = tl.load(in_ptr2 + (3 + 4 * x1), tmp55 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp59 = tmp58.to(tl.int64)
    tmp60 = tmp59 + tmp22
    tmp61 = tmp59 < 0
    tmp62 = tl.where(tmp61, tmp60, tmp59)
    tl.device_assert((0 <= tl.broadcast_to(tmp62, [XBLOCK])) & (tl.
        broadcast_to(tmp62, [XBLOCK]) < 4) | ~(tmp55 & xmask),
        'index out of bounds: 0 <= tl.broadcast_to(tmp62, [XBLOCK]) < 4')
    tmp64 = tl.load(in_ptr3 + (tmp62 + 4 * x1), tmp55 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp65 = tmp64 * tmp64
    tmp66 = tmp65 * tmp64
    tmp67 = tmp66 * tmp30
    tmp68 = tl_math.abs(tmp67)
    tmp69 = tmp68 == tmp9
    tmp70 = tl.where(tmp69, tmp11, tmp67)
    tmp71 = tmp67 - tmp70
    tmp72 = tl_math.exp(tmp71)
    tmp73 = tl_math.log(tmp72)
    tmp74 = tmp73 + tmp70
    tmp75 = tl.full(tmp74.shape, 0.0, tmp74.dtype)
    tmp76 = tl.where(tmp55, tmp74, tmp75)
    tmp77 = tl.where(tmp19, tmp54, tmp76)
    tmp78 = tl.where(tmp4, tmp15, tmp77)
    tl.store(out_ptr0 + x2, tmp78, xmask)


@triton.jit
def triton_per_fused_gather_mean_mul_pow_sub_sum_5(in_out_ptr0, in_ptr0,
    in_ptr1, in_ptr2, xnumel, rnumel, XBLOCK: tl.constexpr):
    RBLOCK: tl.constexpr = 4
    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 + (1 + 4 * r0), None, eviction_policy='evict_last')
    tmp12 = tl.load(in_ptr2 + 3 * r0, None, eviction_policy='evict_last')
    tmp14 = tl.load(in_ptr0 + (2 + 4 * r0), None, eviction_policy='evict_last')
    tmp24 = tl.load(in_ptr2 + (1 + 3 * r0), None, eviction_policy='evict_last')
    tmp27 = tl.load(in_ptr0 + (3 + 4 * r0), None, eviction_policy='evict_last')
    tmp37 = tl.load(in_ptr2 + (2 + 3 * r0), None, eviction_policy='evict_last')
    tmp1 = tmp0.to(tl.int64)
    tmp2 = tl.full([XBLOCK, RBLOCK], 4, tl.int32)
    tmp3 = tmp1 + tmp2
    tmp4 = tmp1 < 0
    tmp5 = tl.where(tmp4, tmp3, tmp1)
    tl.device_assert((0 <= tmp5) & (tmp5 < 4),
        'index out of bounds: 0 <= tmp5 < 4')
    tmp7 = tl.load(in_ptr1 + (tmp5 + 4 * r0), None, eviction_policy=
        'evict_last')
    tmp8 = tmp7 * tmp7
    tmp9 = tmp8 * tmp7
    tmp10 = -3.0
    tmp11 = tmp9 * tmp10
    tmp13 = tmp11 - tmp12
    tmp15 = tmp14.to(tl.int64)
    tmp16 = tmp15 + tmp2
    tmp17 = tmp15 < 0
    tmp18 = tl.where(tmp17, tmp16, tmp15)
    tl.device_assert((0 <= tmp18) & (tmp18 < 4),
        'index out of bounds: 0 <= tmp18 < 4')
    tmp20 = tl.load(in_ptr1 + (tmp18 + 4 * r0), None, eviction_policy=
        'evict_last')
    tmp21 = tmp20 * tmp20
    tmp22 = tmp21 * tmp20
    tmp23 = tmp22 * tmp10
    tmp25 = tmp23 - tmp24
    tmp26 = tmp13 + tmp25
    tmp28 = tmp27.to(tl.int64)
    tmp29 = tmp28 + tmp2
    tmp30 = tmp28 < 0
    tmp31 = tl.where(tmp30, tmp29, tmp28)
    tl.device_assert((0 <= tmp31) & (tmp31 < 4),
        'index out of bounds: 0 <= tmp31 < 4')
    tmp33 = tl.load(in_ptr1 + (tmp31 + 4 * r0), None, eviction_policy=
        'evict_last')
    tmp34 = tmp33 * tmp33
    tmp35 = tmp34 * tmp33
    tmp36 = tmp35 * tmp10
    tmp38 = tmp36 - tmp37
    tmp39 = tmp26 + tmp38
    tmp40 = -1.0
    tmp41 = tmp39 * tmp40
    tmp42 = tl.broadcast_to(tmp41, [XBLOCK, RBLOCK])
    tmp44 = tl.sum(tmp42, 1)[:, None]
    tmp45 = 4.0
    tmp46 = tmp44 / tmp45
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp46, None)


def call(args):
    arg0_1, arg1_1 = args
    args.clear()
    assert_size_stride(arg0_1, (4, 4), (4, 1))
    assert_size_stride(arg1_1, (4, 4), (4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        extern_kernels.mm(arg0_1, reinterpret_tensor(arg0_1, (4, 4), (1, 4),
            0), out=buf0)
        buf1 = buf0
        del buf0
        buf2 = buf1
        del buf1
        get_raw_stream(0)
        triton_poi_fused_add_clamp_mul_sqrt_sub_0[grid(16)](buf2, arg0_1, 
            16, XBLOCK=16, num_warps=1, num_stages=1)
        del arg0_1
        triton_poi_fused_clamp_index_put_lift_fresh_sqrt_1[grid(4)](buf2, 4,
            XBLOCK=4, num_warps=1, num_stages=1)
        buf5 = empty_strided_cuda((4, 4), (4, 1), torch.int16)
        triton_per_fused_mul_pow_sort_2[grid(4)](buf2, buf5, 4, 4, XBLOCK=1,
            num_warps=2, num_stages=1)
        buf6 = buf2
        del buf2
        extern_kernels.mm(arg1_1, reinterpret_tensor(arg1_1, (4, 4), (1, 4),
            0), out=buf6)
        buf7 = buf6
        del buf6
        buf8 = buf7
        del buf7
        triton_poi_fused_add_clamp_mul_sqrt_sub_0[grid(16)](buf8, arg1_1, 
            16, XBLOCK=16, num_warps=1, num_stages=1)
        del arg1_1
        triton_poi_fused_clamp_index_put_lift_fresh_sqrt_1[grid(4)](buf8, 4,
            XBLOCK=4, num_warps=1, num_stages=1)
        buf10 = empty_strided_cuda((4, 1), (1, 4), torch.float32)
        buf11 = empty_strided_cuda((4,), (1,), torch.float32)
        triton_poi_fused_gather_logsumexp_mul_pow_3[grid(4)](buf5, buf8,
            buf10, buf11, 4, XBLOCK=4, num_warps=1, num_stages=1)
        buf12 = empty_strided_cuda((4, 3), (3, 1), torch.float32)
        triton_poi_fused_stack_4[grid(12)](buf11, buf10, buf5, buf8, buf12,
            12, XBLOCK=16, num_warps=1, num_stages=1)
        del buf10
        del buf11
        buf14 = empty_strided_cuda((), (), torch.float32)
        buf15 = buf14
        del buf14
        triton_per_fused_gather_mean_mul_pow_sub_sum_5[grid(1)](buf15, buf5,
            buf8, buf12, 1, 4, XBLOCK=1, num_warps=2, num_stages=1)
        del buf12
        del buf5
        del buf8
    return buf15,


def pdist(e, squared=False, eps=1e-12):
    e_square = e.pow(2).sum(dim=1)
    prod = e @ e.t()
    res = (e_square.unsqueeze(1) + e_square.unsqueeze(0) - 2 * prod).clamp(min
        =eps)
    if not squared:
        res = res.sqrt()
    res = res.clone()
    res[range(len(e)), range(len(e))] = 0
    return res


class HardDarkRankNew(nn.Module):

    def __init__(self, alpha=3, beta=3, permute_len=4):
        super().__init__()
        self.alpha = alpha
        self.beta = beta
        self.permute_len = permute_len

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


Measured latency_ms: 0.027371