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


@triton.jit
def triton_poi_fused_fill_lift_fresh_0(out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 16
    x1 = xindex // 16
    tmp0 = 0.0
    tl.store(out_ptr0 + (x0 + 64 * x1), tmp0, xmask)


@triton.jit
def triton_poi_fused__log_softmax_add_fill_lift_fresh_mul_rsub_sub_zeros_like_1(
    in_ptr0, in_ptr1, out_ptr0, out_ptr1, out_ptr2, out_ptr3, xnumel,
    XBLOCK: tl.constexpr):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x3 = xindex
    x1 = xindex // 4 % 4
    tmp0 = tl.load(in_ptr0 + 4 * x3, xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr1 + 4 * x3, xmask, eviction_policy='evict_last')
    tmp13 = tl.load(in_ptr0 + (1 + 4 * x3), xmask, eviction_policy='evict_last'
        )
    tmp14 = tl.load(in_ptr1 + (1 + 4 * x3), xmask, eviction_policy='evict_last'
        )
    tmp20 = tl.load(in_ptr0 + (2 + 4 * x3), xmask, eviction_policy='evict_last'
        )
    tmp21 = tl.load(in_ptr1 + (2 + 4 * x3), xmask, eviction_policy='evict_last'
        )
    tmp27 = tl.load(in_ptr0 + (3 + 4 * x3), xmask, eviction_policy='evict_last'
        )
    tmp28 = tl.load(in_ptr1 + (3 + 4 * x3), xmask, eviction_policy='evict_last'
        )
    tmp2 = x1
    tmp3 = tl.full([1], 0, tl.int32)
    tmp4 = tmp2 == tmp3
    tmp5 = 1.0
    tmp6 = 0.0
    tmp7 = tl.where(tmp4, tmp5, tmp6)
    tmp8 = tmp1 + tmp7
    tmp9 = tmp5 - tmp8
    tmp10 = 1e+30
    tmp11 = tmp9 * tmp10
    tmp12 = tmp0 - tmp11
    tmp15 = tmp14 + tmp7
    tmp16 = tmp5 - tmp15
    tmp17 = tmp16 * tmp10
    tmp18 = tmp13 - tmp17
    tmp19 = triton_helpers.maximum(tmp12, tmp18)
    tmp22 = tmp21 + tmp7
    tmp23 = tmp5 - tmp22
    tmp24 = tmp23 * tmp10
    tmp25 = tmp20 - tmp24
    tmp26 = triton_helpers.maximum(tmp19, tmp25)
    tmp29 = tmp28 + tmp7
    tmp30 = tmp5 - tmp29
    tmp31 = tmp30 * tmp10
    tmp32 = tmp27 - tmp31
    tmp33 = triton_helpers.maximum(tmp26, tmp32)
    tmp34 = tmp12 - tmp33
    tmp35 = tl_math.exp(tmp34)
    tmp36 = tmp18 - tmp33
    tmp37 = tl_math.exp(tmp36)
    tmp38 = tmp35 + tmp37
    tmp39 = tmp25 - tmp33
    tmp40 = tl_math.exp(tmp39)
    tmp41 = tmp38 + tmp40
    tmp42 = tmp32 - tmp33
    tmp43 = tl_math.exp(tmp42)
    tmp44 = tmp41 + tmp43
    tmp45 = tmp5 - tmp1
    tmp46 = tmp5 - tmp45
    tmp47 = tmp46 * tmp10
    tmp48 = tmp0 - tmp47
    tmp49 = tmp5 - tmp14
    tmp50 = tmp5 - tmp49
    tmp51 = tmp50 * tmp10
    tmp52 = tmp13 - tmp51
    tmp53 = triton_helpers.maximum(tmp48, tmp52)
    tmp54 = tmp5 - tmp21
    tmp55 = tmp5 - tmp54
    tmp56 = tmp55 * tmp10
    tmp57 = tmp20 - tmp56
    tmp58 = triton_helpers.maximum(tmp53, tmp57)
    tmp59 = tmp5 - tmp28
    tmp60 = tmp5 - tmp59
    tmp61 = tmp60 * tmp10
    tmp62 = tmp27 - tmp61
    tmp63 = triton_helpers.maximum(tmp58, tmp62)
    tmp64 = tmp48 - tmp63
    tmp65 = tl_math.exp(tmp64)
    tmp66 = tmp52 - tmp63
    tmp67 = tl_math.exp(tmp66)
    tmp68 = tmp65 + tmp67
    tmp69 = tmp57 - tmp63
    tmp70 = tl_math.exp(tmp69)
    tmp71 = tmp68 + tmp70
    tmp72 = tmp62 - tmp63
    tmp73 = tl_math.exp(tmp72)
    tmp74 = tmp71 + tmp73
    tl.store(out_ptr0 + x3, tmp33, xmask)
    tl.store(out_ptr1 + x3, tmp44, xmask)
    tl.store(out_ptr2 + x3, tmp63, xmask)
    tl.store(out_ptr3 + x3, tmp74, xmask)


@triton.jit
def triton_per_fused__log_softmax_add_fill_lift_fresh_mean_mul_neg_rsub_sub_sum_zeros_like_2(
    in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, in_ptr5,
    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)
    r2 = rindex // 16
    r4 = rindex % 16
    r1 = rindex // 4 % 4
    tmp0 = tl.load(in_ptr0 + (r4 + 64 * r2), None)
    tmp1 = tl.load(in_ptr1 + (r4 + 64 * r2), None)
    tmp12 = tl.load(in_ptr2 + (r1 + 16 * r2), None, eviction_policy=
        'evict_last')
    tmp14 = tl.load(in_ptr3 + (r1 + 16 * r2), None, eviction_policy=
        'evict_last')
    tmp18 = tl.load(in_ptr0 + (16 + r4 + 64 * r2), None)
    tmp19 = tl.load(in_ptr1 + (16 + r4 + 64 * r2), None)
    tmp27 = tl.load(in_ptr2 + (4 + r1 + 16 * r2), None, eviction_policy=
        'evict_last')
    tmp29 = tl.load(in_ptr3 + (4 + r1 + 16 * r2), None, eviction_policy=
        'evict_last')
    tmp34 = tl.load(in_ptr0 + (32 + r4 + 64 * r2), None)
    tmp35 = tl.load(in_ptr1 + (32 + r4 + 64 * r2), None)
    tmp43 = tl.load(in_ptr2 + (8 + r1 + 16 * r2), None, eviction_policy=
        'evict_last')
    tmp45 = tl.load(in_ptr3 + (8 + r1 + 16 * r2), None, eviction_policy=
        'evict_last')
    tmp50 = tl.load(in_ptr0 + (48 + r4 + 64 * r2), None)
    tmp51 = tl.load(in_ptr1 + (48 + r4 + 64 * r2), None)
    tmp59 = tl.load(in_ptr2 + (12 + r1 + 16 * r2), None, eviction_policy=
        'evict_last')
    tmp61 = tl.load(in_ptr3 + (12 + r1 + 16 * r2), None, eviction_policy=
        'evict_last')
    tmp70 = tl.load(in_ptr4 + (r1 + 16 * r2), None, eviction_policy=
        'evict_last')
    tmp72 = tl.load(in_ptr5 + (r1 + 16 * r2), None, eviction_policy=
        'evict_last')
    tmp80 = tl.load(in_ptr4 + (4 + r1 + 16 * r2), None, eviction_policy=
        'evict_last')
    tmp82 = tl.load(in_ptr5 + (4 + r1 + 16 * r2), None, eviction_policy=
        'evict_last')
    tmp91 = tl.load(in_ptr4 + (8 + r1 + 16 * r2), None, eviction_policy=
        'evict_last')
    tmp93 = tl.load(in_ptr5 + (8 + r1 + 16 * r2), None, eviction_policy=
        'evict_last')
    tmp102 = tl.load(in_ptr4 + (12 + r1 + 16 * r2), None, eviction_policy=
        'evict_last')
    tmp104 = tl.load(in_ptr5 + (12 + r1 + 16 * r2), None, eviction_policy=
        'evict_last')
    tmp2 = tl.full([1, 1], 0, tl.int32)
    tmp3 = tmp2 == tmp2
    tmp4 = 1.0
    tmp5 = 0.0
    tmp6 = tl.where(tmp3, tmp4, tmp5)
    tmp7 = tmp1 + tmp6
    tmp8 = tmp4 - tmp7
    tmp9 = 1e+30
    tmp10 = tmp8 * tmp9
    tmp11 = tmp0 - tmp10
    tmp13 = tmp11 - tmp12
    tmp15 = tl_math.log(tmp14)
    tmp16 = tmp13 - tmp15
    tmp17 = tmp16 * tmp1
    tmp20 = tl.full([1, 1], 1, tl.int32)
    tmp21 = tmp20 == tmp2
    tmp22 = tl.where(tmp21, tmp4, tmp5)
    tmp23 = tmp19 + tmp22
    tmp24 = tmp4 - tmp23
    tmp25 = tmp24 * tmp9
    tmp26 = tmp18 - tmp25
    tmp28 = tmp26 - tmp27
    tmp30 = tl_math.log(tmp29)
    tmp31 = tmp28 - tmp30
    tmp32 = tmp31 * tmp19
    tmp33 = tmp17 + tmp32
    tmp36 = tl.full([1, 1], 2, tl.int32)
    tmp37 = tmp36 == tmp2
    tmp38 = tl.where(tmp37, tmp4, tmp5)
    tmp39 = tmp35 + tmp38
    tmp40 = tmp4 - tmp39
    tmp41 = tmp40 * tmp9
    tmp42 = tmp34 - tmp41
    tmp44 = tmp42 - tmp43
    tmp46 = tl_math.log(tmp45)
    tmp47 = tmp44 - tmp46
    tmp48 = tmp47 * tmp35
    tmp49 = tmp33 + tmp48
    tmp52 = tl.full([1, 1], 3, tl.int32)
    tmp53 = tmp52 == tmp2
    tmp54 = tl.where(tmp53, tmp4, tmp5)
    tmp55 = tmp51 + tmp54
    tmp56 = tmp4 - tmp55
    tmp57 = tmp56 * tmp9
    tmp58 = tmp50 - tmp57
    tmp60 = tmp58 - tmp59
    tmp62 = tl_math.log(tmp61)
    tmp63 = tmp60 - tmp62
    tmp64 = tmp63 * tmp51
    tmp65 = tmp49 + tmp64
    tmp66 = tmp4 - tmp1
    tmp67 = tmp4 - tmp66
    tmp68 = tmp67 * tmp9
    tmp69 = tmp0 - tmp68
    tmp71 = tmp69 - tmp70
    tmp73 = tl_math.log(tmp72)
    tmp74 = tmp71 - tmp73
    tmp75 = tmp74 * tmp6
    tmp76 = tmp4 - tmp19
    tmp77 = tmp4 - tmp76
    tmp78 = tmp77 * tmp9
    tmp79 = tmp18 - tmp78
    tmp81 = tmp79 - tmp80
    tmp83 = tl_math.log(tmp82)
    tmp84 = tmp81 - tmp83
    tmp85 = tmp84 * tmp22
    tmp86 = tmp75 + tmp85
    tmp87 = tmp4 - tmp35
    tmp88 = tmp4 - tmp87
    tmp89 = tmp88 * tmp9
    tmp90 = tmp34 - tmp89
    tmp92 = tmp90 - tmp91
    tmp94 = tl_math.log(tmp93)
    tmp95 = tmp92 - tmp94
    tmp96 = tmp95 * tmp38
    tmp97 = tmp86 + tmp96
    tmp98 = tmp4 - tmp51
    tmp99 = tmp4 - tmp98
    tmp100 = tmp99 * tmp9
    tmp101 = tmp50 - tmp100
    tmp103 = tmp101 - tmp102
    tmp105 = tl_math.log(tmp104)
    tmp106 = tmp103 - tmp105
    tmp107 = tmp106 * tmp54
    tmp108 = tmp97 + tmp107
    tmp109 = -tmp65
    tmp110 = -tmp108
    tmp111 = tmp109 + tmp110
    tmp112 = tl.broadcast_to(tmp111, [XBLOCK, RBLOCK])
    tmp114 = tl.sum(tmp112, 1)[:, None]
    tmp115 = 64.0
    tmp116 = tmp114 / tmp115
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp116, 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)
        get_raw_stream(0)
        triton_poi_fused_fill_lift_fresh_0[grid(64)](arg0_1, 64, XBLOCK=64,
            num_warps=1, num_stages=1)
        buf1 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 64), torch.float32)
        buf2 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 64), torch.float32)
        buf4 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 64), torch.float32)
        buf5 = empty_strided_cuda((4, 4, 4, 1), (16, 4, 1, 64), torch.float32)
        triton_poi_fused__log_softmax_add_fill_lift_fresh_mul_rsub_sub_zeros_like_1[
            grid(64)](arg1_1, arg0_1, buf1, buf2, buf4, buf5, 64, XBLOCK=64,
            num_warps=1, num_stages=1)
        buf7 = empty_strided_cuda((), (), torch.float32)
        buf8 = buf7
        del buf7
        triton_per_fused__log_softmax_add_fill_lift_fresh_mean_mul_neg_rsub_sub_sum_zeros_like_2[
            grid(1)](buf8, arg1_1, arg0_1, buf1, buf2, buf4, buf5, 1, 64,
            XBLOCK=1, num_warps=2, num_stages=1)
        del arg0_1
        del arg1_1
        del buf1
        del buf2
        del buf4
        del buf5
    return buf8,


class ATLossNew(nn.Module):

    def __init__(self):
        super().__init__()

    def get_label(self, logits: 'Tensor', num_labels: 'int'=-1, threshold:
        'float'=None) ->Tensor:
        """ Calculated the labels """
        if threshold:
            th_logit = torch.full((len(logits), 1), threshold)
        else:
            th_logit = logits[:, 0].unsqueeze(1)
        output = torch.zeros_like(logits)
        mask = logits > th_logit
        if num_labels > 0:
            top_v, _ = torch.topk(logits, num_labels, dim=1)
            top_v = top_v[:, -1]
            mask = (logits >= top_v.unsqueeze(1)) & mask
        output[mask] = 1.0
        output[:, 0] = output.sum(1) == 0.0
        return output

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