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
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__log_softmax__to_copy_0(in_ptr0, out_ptr0, xnumel,
    XBLOCK: tl.constexpr):
    xnumel = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex
    x1 = xindex // 4
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tmp2 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp7 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp10 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp1 = tmp0.to(tl.float32)
    tmp3 = tmp2.to(tl.float32)
    tmp5 = tmp4.to(tl.float32)
    tmp6 = triton_helpers.maximum(tmp3, tmp5)
    tmp8 = tmp7.to(tl.float32)
    tmp9 = triton_helpers.maximum(tmp6, tmp8)
    tmp11 = tmp10.to(tl.float32)
    tmp12 = triton_helpers.maximum(tmp9, tmp11)
    tmp13 = tmp1 - tmp12
    tl.store(out_ptr0 + x2, tmp13, xmask)


@triton.jit
def triton_per_fused__log_softmax_div_eq_index_put_lift_fresh_mul_neg_scatter_sum_1(
    in_out_ptr1, in_ptr0, in_ptr1, 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 + r0, None)
    tmp5 = tl.load(in_ptr1 + 4 * r0, None, eviction_policy='evict_last')
    tmp7 = tl.load(in_ptr1 + (1 + 4 * r0), None, eviction_policy='evict_last')
    tmp10 = tl.load(in_ptr1 + (2 + 4 * r0), None, eviction_policy='evict_last')
    tmp13 = tl.load(in_ptr1 + (3 + 4 * r0), None, eviction_policy='evict_last')
    tmp1 = tl.full([1, 1], -100, tl.int64)
    tmp2 = tmp0 == tmp1
    tmp3 = tl.full([1, 1], 0, tl.int64)
    tmp4 = tl.where(tmp2, tmp3, tmp0)
    tmp6 = tl_math.exp(tmp5)
    tmp8 = tl_math.exp(tmp7)
    tmp9 = tmp6 + tmp8
    tmp11 = tl_math.exp(tmp10)
    tmp12 = tmp9 + tmp11
    tmp14 = tl_math.exp(tmp13)
    tmp15 = tmp12 + tmp14
    tmp16 = tl_math.log(tmp15)
    tmp17 = tmp5 - tmp16
    tmp18 = tmp4 == tmp3
    tmp19 = 0.9
    tmp20 = 0.025
    tmp21 = tl.where(tmp18, tmp19, tmp20)
    tmp22 = tmp17 * tmp21
    tmp23 = tmp7 - tmp16
    tmp24 = tl.full([1, 1], 1, tl.int64)
    tmp25 = tmp4 == tmp24
    tmp26 = tl.where(tmp25, tmp19, tmp20)
    tmp27 = tmp23 * tmp26
    tmp28 = tmp22 + tmp27
    tmp29 = tmp10 - tmp16
    tmp30 = tl.full([1, 1], 2, tl.int64)
    tmp31 = tmp4 == tmp30
    tmp32 = tl.where(tmp31, tmp19, tmp20)
    tmp33 = tmp29 * tmp32
    tmp34 = tmp28 + tmp33
    tmp35 = tmp13 - tmp16
    tmp36 = tl.full([1, 1], 3, tl.int64)
    tmp37 = tmp4 == tmp36
    tmp38 = tl.where(tmp37, tmp19, tmp20)
    tmp39 = tmp35 * tmp38
    tmp40 = tmp34 + tmp39
    tmp41 = -tmp40
    tmp42 = 0.0
    tmp43 = tl.where(tmp2, tmp42, tmp41)
    tmp44 = tmp2.to(tl.int64)
    tmp45 = tmp44 == tmp3
    tmp46 = tmp45.to(tl.int64)
    tmp47 = tl.broadcast_to(tmp46, [XBLOCK, RBLOCK])
    tmp49 = tl.sum(tmp47, 1)[:, None]
    tmp50 = tl.broadcast_to(tmp43, [XBLOCK, RBLOCK])
    tmp52 = tl.sum(tmp50, 1)[:, None]
    tmp53 = tmp49.to(tl.float32)
    tmp54 = tmp52 / tmp53
    tl.debug_barrier()
    tl.store(in_out_ptr1 + tl.full([XBLOCK, 1], 0, tl.int32), tmp54, 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,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused__log_softmax__to_copy_0[grid(16)](arg0_1, buf0, 16,
            XBLOCK=16, num_warps=1, num_stages=1)
        del arg0_1
        buf4 = empty_strided_cuda((), (), torch.float32)
        buf6 = buf4
        del buf4
        triton_per_fused__log_softmax_div_eq_index_put_lift_fresh_mul_neg_scatter_sum_1[
            grid(1)](buf6, arg1_1, buf0, 1, 4, XBLOCK=1, num_warps=2,
            num_stages=1)
        del arg1_1
        del buf0
    return buf6,


class LabelSmoothSoftmaxCEV1New(nn.Module):
    """
    This is the autograd version, you can also try the LabelSmoothSoftmaxCEV2 that uses derived gradients
    """

    def __init__(self, lb_smooth=0.1, reduction='mean', ignore_index=-100):
        super(LabelSmoothSoftmaxCEV1New, self).__init__()
        self.lb_smooth = lb_smooth
        self.reduction = reduction
        self.lb_ignore = ignore_index
        self.log_softmax = nn.LogSoftmax(dim=1)

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