import torch
from torch import device
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
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_index_sort_0(in_ptr0, in_ptr1, 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)
    x1 = xindex // 4 % 4
    r3 = rindex
    x0 = xindex % 4
    x2 = xindex // 16
    x4 = xindex
    tmp0 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last')
    tmp1 = tl.full([XBLOCK, RBLOCK], 4, tl.int32)
    tmp2 = tmp0 + tmp1
    tmp3 = tmp0 < 0
    tmp4 = tl.where(tmp3, tmp2, tmp0)
    tl.device_assert((0 <= tmp4) & (tmp4 < 4) | ~xmask,
        'index out of bounds: 0 <= tmp4 < 4')
    tmp6 = tl.load(in_ptr1 + (r3 + 4 * x0 + 16 * tmp4 + 64 * x2), xmask,
        other=0.0)
    tmp7 = r3
    tmp8 = tmp7.to(tl.int16)
    tmp9 = tl.broadcast_to(tmp6, [XBLOCK, RBLOCK])
    tmp10 = tl.broadcast_to(tmp8, [XBLOCK, RBLOCK])
    _tmp11, tmp12 = triton_helpers.sort_with_index(tmp9, tmp10, None, 1,
        stable=False, descending=True)
    tl.store(out_ptr0 + (r3 + 4 * x4), tmp12, xmask)


@triton.jit
def _triton_helper_fn_add0(arg0_0, arg1_0):
    tmp0 = arg0_0 + arg1_0
    return tmp0


@triton.jit
def triton_per_fused_cumsum_exp_flip_gather_index_max_sort_sub_1(in_ptr0,
    in_ptr1, in_ptr2, 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)
    x0 = xindex % 16
    x1 = xindex // 16
    x3 = xindex
    r2 = rindex
    tmp0 = tl.load(in_ptr0 + (x0 + 64 * x1), xmask, eviction_policy=
        'evict_last')
    tmp13 = tl.load(in_ptr0 + (16 + x0 + 64 * x1), xmask, eviction_policy=
        'evict_last')
    tmp26 = tl.load(in_ptr0 + (32 + x0 + 64 * x1), xmask, eviction_policy=
        'evict_last')
    tmp39 = tl.load(in_ptr0 + (48 + x0 + 64 * x1), xmask, eviction_policy=
        'evict_last')
    tmp52 = tl.load(in_ptr0 + (48 + x0 + -16 * r2 + 64 * x1), xmask, other=0.0)
    tmp1 = tmp0.to(tl.int64)
    tmp2 = tl.full([XBLOCK, 1], 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, xmask, eviction_policy='evict_last')
    tmp8 = tmp7 + tmp2
    tmp9 = tmp7 < 0
    tmp10 = tl.where(tmp9, tmp8, tmp7)
    tl.device_assert((0 <= tmp10) & (tmp10 < 4) | ~xmask,
        'index out of bounds: 0 <= tmp10 < 4')
    tmp12 = tl.load(in_ptr2 + (x0 + 16 * tmp10 + 64 * x1), xmask)
    tmp14 = tmp13.to(tl.int64)
    tmp15 = tmp14 + tmp2
    tmp16 = tmp14 < 0
    tmp17 = tl.where(tmp16, tmp15, tmp14)
    tl.device_assert((0 <= tmp17) & (tmp17 < 4) | ~xmask,
        'index out of bounds: 0 <= tmp17 < 4')
    tmp19 = tl.load(in_ptr1 + tmp17, xmask, eviction_policy='evict_last')
    tmp20 = tmp19 + tmp2
    tmp21 = tmp19 < 0
    tmp22 = tl.where(tmp21, tmp20, tmp19)
    tl.device_assert((0 <= tmp22) & (tmp22 < 4) | ~xmask,
        'index out of bounds: 0 <= tmp22 < 4')
    tmp24 = tl.load(in_ptr2 + (x0 + 16 * tmp22 + 64 * x1), xmask)
    tmp25 = triton_helpers.maximum(tmp12, tmp24)
    tmp27 = tmp26.to(tl.int64)
    tmp28 = tmp27 + tmp2
    tmp29 = tmp27 < 0
    tmp30 = tl.where(tmp29, tmp28, tmp27)
    tl.device_assert((0 <= tmp30) & (tmp30 < 4) | ~xmask,
        'index out of bounds: 0 <= tmp30 < 4')
    tmp32 = tl.load(in_ptr1 + tmp30, xmask, eviction_policy='evict_last')
    tmp33 = tmp32 + tmp2
    tmp34 = tmp32 < 0
    tmp35 = tl.where(tmp34, tmp33, tmp32)
    tl.device_assert((0 <= tmp35) & (tmp35 < 4) | ~xmask,
        'index out of bounds: 0 <= tmp35 < 4')
    tmp37 = tl.load(in_ptr2 + (x0 + 16 * tmp35 + 64 * x1), xmask)
    tmp38 = triton_helpers.maximum(tmp25, tmp37)
    tmp40 = tmp39.to(tl.int64)
    tmp41 = tmp40 + tmp2
    tmp42 = tmp40 < 0
    tmp43 = tl.where(tmp42, tmp41, tmp40)
    tl.device_assert((0 <= tmp43) & (tmp43 < 4) | ~xmask,
        'index out of bounds: 0 <= tmp43 < 4')
    tmp45 = tl.load(in_ptr1 + tmp43, xmask, eviction_policy='evict_last')
    tmp46 = tmp45 + tmp2
    tmp47 = tmp45 < 0
    tmp48 = tl.where(tmp47, tmp46, tmp45)
    tl.device_assert((0 <= tmp48) & (tmp48 < 4) | ~xmask,
        'index out of bounds: 0 <= tmp48 < 4')
    tmp50 = tl.load(in_ptr2 + (x0 + 16 * tmp48 + 64 * x1), xmask)
    tmp51 = triton_helpers.maximum(tmp38, tmp50)
    tmp53 = tmp52.to(tl.int64)
    tmp54 = tl.full([XBLOCK, RBLOCK], 4, tl.int32)
    tmp55 = tmp53 + tmp54
    tmp56 = tmp53 < 0
    tmp57 = tl.where(tmp56, tmp55, tmp53)
    tl.device_assert((0 <= tmp57) & (tmp57 < 4) | ~xmask,
        'index out of bounds: 0 <= tmp57 < 4')
    tmp59 = tl.load(in_ptr1 + tmp57, xmask, eviction_policy='evict_last')
    tmp60 = tmp59 + tmp54
    tmp61 = tmp59 < 0
    tmp62 = tl.where(tmp61, tmp60, tmp59)
    tl.device_assert((0 <= tmp62) & (tmp62 < 4) | ~xmask,
        'index out of bounds: 0 <= tmp62 < 4')
    tmp64 = tl.load(in_ptr2 + (x0 + 16 * tmp62 + 64 * x1), xmask)
    tmp65 = tmp64 - tmp51
    tmp66 = tl_math.exp(tmp65)
    tmp67 = tmp66.to(tl.float32)
    tmp68 = tl.broadcast_to(tmp67, [XBLOCK, RBLOCK])
    tmp69, = tl.associative_scan((tmp68,), 1, _triton_helper_fn_add0)
    tl.store(out_ptr0 + x3, tmp51, xmask)
    tl.store(out_ptr1 + (x0 + 16 * r2 + 64 * x1), tmp69, xmask)


@triton.jit
def triton_per_fused_add_flip_gather_index_log_mean_sort_sub_sum_2(in_out_ptr0,
    in_out_ptr1, in_ptr0, in_ptr1, in_ptr2, in_ptr3, xnumel, rnumel, XBLOCK:
    tl.constexpr):
    RBLOCK: tl.constexpr = 64
    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 % 16
    r1 = rindex // 16
    r2 = rindex
    tmp0 = tl.load(in_ptr0 + (48 + r0 + 64 * r1), None)
    tmp4 = tl.load(in_ptr1 + (r0 + 64 * r1), None)
    tmp17 = tl.load(in_out_ptr0 + r2, None)
    tmp20 = tl.load(in_ptr0 + (32 + r0 + 64 * r1), None)
    tmp23 = tl.load(in_ptr1 + (16 + r0 + 64 * r1), None)
    tmp38 = tl.load(in_ptr0 + (16 + r0 + 64 * r1), None)
    tmp41 = tl.load(in_ptr1 + (32 + r0 + 64 * r1), None)
    tmp56 = tl.load(in_ptr0 + (r0 + 64 * r1), None)
    tmp59 = tl.load(in_ptr1 + (48 + r0 + 64 * r1), None)
    tmp1 = 1e-05
    tmp2 = tmp0 + tmp1
    tmp3 = tl_math.log(tmp2)
    tmp5 = tmp4.to(tl.int64)
    tmp6 = tl.full([XBLOCK, RBLOCK], 4, tl.int32)
    tmp7 = tmp5 + tmp6
    tmp8 = tmp5 < 0
    tmp9 = tl.where(tmp8, tmp7, tmp5)
    tl.device_assert((0 <= tmp9) & (tmp9 < 4),
        'index out of bounds: 0 <= tmp9 < 4')
    tmp11 = tl.load(in_ptr2 + tmp9, None, eviction_policy='evict_last')
    tmp12 = tmp11 + tmp6
    tmp13 = tmp11 < 0
    tmp14 = tl.where(tmp13, tmp12, tmp11)
    tl.device_assert((0 <= tmp14) & (tmp14 < 4),
        'index out of bounds: 0 <= tmp14 < 4')
    tmp16 = tl.load(in_ptr3 + (r0 + 16 * tmp14 + 64 * r1), None)
    tmp18 = tmp16 - tmp17
    tmp19 = tmp3 - tmp18
    tmp21 = tmp20 + tmp1
    tmp22 = tl_math.log(tmp21)
    tmp24 = tmp23.to(tl.int64)
    tmp25 = tmp24 + tmp6
    tmp26 = tmp24 < 0
    tmp27 = tl.where(tmp26, tmp25, tmp24)
    tl.device_assert((0 <= tmp27) & (tmp27 < 4),
        'index out of bounds: 0 <= tmp27 < 4')
    tmp29 = tl.load(in_ptr2 + tmp27, None, eviction_policy='evict_last')
    tmp30 = tmp29 + tmp6
    tmp31 = tmp29 < 0
    tmp32 = tl.where(tmp31, tmp30, tmp29)
    tl.device_assert((0 <= tmp32) & (tmp32 < 4),
        'index out of bounds: 0 <= tmp32 < 4')
    tmp34 = tl.load(in_ptr3 + (r0 + 16 * tmp32 + 64 * r1), None)
    tmp35 = tmp34 - tmp17
    tmp36 = tmp22 - tmp35
    tmp37 = tmp19 + tmp36
    tmp39 = tmp38 + tmp1
    tmp40 = tl_math.log(tmp39)
    tmp42 = tmp41.to(tl.int64)
    tmp43 = tmp42 + tmp6
    tmp44 = tmp42 < 0
    tmp45 = tl.where(tmp44, tmp43, tmp42)
    tl.device_assert((0 <= tmp45) & (tmp45 < 4),
        'index out of bounds: 0 <= tmp45 < 4')
    tmp47 = tl.load(in_ptr2 + tmp45, None, eviction_policy='evict_last')
    tmp48 = tmp47 + tmp6
    tmp49 = tmp47 < 0
    tmp50 = tl.where(tmp49, tmp48, tmp47)
    tl.device_assert((0 <= tmp50) & (tmp50 < 4),
        'index out of bounds: 0 <= tmp50 < 4')
    tmp52 = tl.load(in_ptr3 + (r0 + 16 * tmp50 + 64 * r1), None)
    tmp53 = tmp52 - tmp17
    tmp54 = tmp40 - tmp53
    tmp55 = tmp37 + tmp54
    tmp57 = tmp56 + tmp1
    tmp58 = tl_math.log(tmp57)
    tmp60 = tmp59.to(tl.int64)
    tmp61 = tmp60 + tmp6
    tmp62 = tmp60 < 0
    tmp63 = tl.where(tmp62, tmp61, tmp60)
    tl.device_assert((0 <= tmp63) & (tmp63 < 4),
        'index out of bounds: 0 <= tmp63 < 4')
    tmp65 = tl.load(in_ptr2 + tmp63, None, eviction_policy='evict_last')
    tmp66 = tmp65 + tmp6
    tmp67 = tmp65 < 0
    tmp68 = tl.where(tmp67, tmp66, tmp65)
    tl.device_assert((0 <= tmp68) & (tmp68 < 4),
        'index out of bounds: 0 <= tmp68 < 4')
    tmp70 = tl.load(in_ptr3 + (r0 + 16 * tmp68 + 64 * r1), None)
    tmp71 = tmp70 - tmp17
    tmp72 = tmp58 - tmp71
    tmp73 = tmp55 + tmp72
    tmp74 = tl.broadcast_to(tmp73, [XBLOCK, RBLOCK])
    tmp76 = tl.sum(tmp74, 1)[:, None]
    tmp77 = 64.0
    tmp78 = tmp76 / tmp77
    tl.debug_barrier()
    tl.store(in_out_ptr1 + tl.full([XBLOCK, 1], 0, tl.int32), tmp78, None)


def call(args):
    arg0_1, arg1_1 = args
    args.clear()
    assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = torch.ops.aten.randperm.default(4, device=device(type='cuda',
            index=0), pin_memory=False)
        buf1 = buf0
        del buf0
        buf3 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.int16)
        get_raw_stream(0)
        triton_per_fused_index_sort_0[grid(64)](buf1, arg1_1, buf3, 64, 4,
            XBLOCK=8, num_warps=2, num_stages=1)
        del arg1_1
        buf4 = empty_strided_cuda((4, 1, 4, 4), (16, 64, 4, 1), torch.float32)
        buf5 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        triton_per_fused_cumsum_exp_flip_gather_index_max_sort_sub_1[grid(64)](
            buf3, buf1, arg0_1, buf4, buf5, 64, 4, XBLOCK=8, num_warps=2,
            num_stages=1)
        buf6 = reinterpret_tensor(buf4, (4, 4, 4), (16, 4, 1), 0)
        del buf4
        buf7 = empty_strided_cuda((), (), torch.float32)
        buf8 = buf7
        del buf7
        triton_per_fused_add_flip_gather_index_log_mean_sort_sub_sum_2[grid(1)
            ](buf6, buf8, buf5, buf3, buf1, arg0_1, 1, 64, XBLOCK=1,
            num_warps=2, num_stages=1)
        del arg0_1
        del buf1
        del buf3
        del buf5
        del buf6
    return buf8,


class ListMLELossNew(nn.Module):

    def __init__(self):
        super(ListMLELossNew, self).__init__()
        return

    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.05458