import torch
import torch.nn.functional as F
from torch.nn.modules.loss import _Loss
from torch.optim.lr_scheduler import *


def stable_kl(logit, target, epsilon=1e-06, reduce=True):
    logit = logit.view(-1, logit.size(-1)).float()
    target = target.view(-1, target.size(-1)).float()
    bs = logit.size(0)
    p = F.log_softmax(logit, 1).exp()
    y = F.log_softmax(target, 1).exp()
    rp = -(1.0 / (p + epsilon) - 1 + epsilon).detach().log()
    ry = -(1.0 / (y + epsilon) - 1 + epsilon).detach().log()
    if reduce:
        return (p * (rp - ry) * 2).sum() / bs
    else:
        return (p * (rp - ry) * 2).sum()


class Criterion(_Loss):

    def __init__(self, alpha=1.0, name='criterion'):
        super().__init__()
        """Alpha is used to weight each loss term
        """
        self.alpha = alpha
        self.name = name

    def forward(self, input, target, weight=None, ignore_index=-1):
        """weight: sample weight
        """
        return


class NsSymKlCriterion(Criterion):

    def __init__(self, alpha=1.0, name='KL Div Criterion'):
        super().__init__()
        self.alpha = alpha
        self.name = name

    def forward(self, input, target, weight=None, ignore_index=-1):
        """input/target: logits
        """
        input = input.float()
        target = target.float()
        loss = stable_kl(input, target.detach()) + stable_kl(target, input.
            detach())
        loss = loss * self.alpha
        return loss


def get_inputs():
    return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4])]


def get_init_inputs():
    return [[], {}]

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.functional as F
from torch.nn.modules.loss import _Loss
from torch.optim.lr_scheduler import *
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_0(in_ptr0, out_ptr0, out_ptr1, xnumel,
    XBLOCK: tl.constexpr):
    xnumel = 256
    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)
    tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp3 = triton_helpers.maximum(tmp1, tmp2)
    tmp5 = triton_helpers.maximum(tmp3, tmp4)
    tmp7 = triton_helpers.maximum(tmp5, tmp6)
    tmp8 = tmp0 - tmp7
    tl.store(out_ptr0 + x2, tmp8, xmask)
    tl.store(out_ptr1 + x2, tmp8, xmask)


@triton.jit
def triton_red_fused__log_softmax_add_div_exp_log_mul_neg_reciprocal_sub_sum_1(
    in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, in_ptr3, xnumel, rnumel, XBLOCK:
    tl.constexpr, RBLOCK: tl.constexpr):
    rnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xoffset + tl.arange(0, XBLOCK)[:, None]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    rbase = tl.arange(0, RBLOCK)[None, :]
    _tmp52 = tl.full([XBLOCK, RBLOCK], 0, tl.float32)
    _tmp102 = tl.full([XBLOCK, RBLOCK], 0, tl.float32)
    for roffset in range(0, rnumel, RBLOCK):
        rindex = roffset + rbase
        rmask = rindex < rnumel
        r2 = rindex
        r1 = rindex // 4
        tmp0 = tl.load(in_ptr0 + r2, rmask, eviction_policy='evict_first',
            other=0.0)
        tmp1 = tl.load(in_ptr0 + 4 * r1, rmask, eviction_policy=
            'evict_last', other=0.0)
        tmp3 = tl.load(in_ptr0 + (1 + 4 * r1), rmask, eviction_policy=
            'evict_last', other=0.0)
        tmp6 = tl.load(in_ptr0 + (2 + 4 * r1), rmask, eviction_policy=
            'evict_last', other=0.0)
        tmp9 = tl.load(in_ptr0 + (3 + 4 * r1), rmask, eviction_policy=
            'evict_last', other=0.0)
        tmp25 = tl.load(in_ptr1 + r2, rmask, eviction_policy='evict_first',
            other=0.0)
        tmp26 = tl.load(in_ptr1 + 4 * r1, rmask, eviction_policy=
            'evict_last', other=0.0)
        tmp28 = tl.load(in_ptr1 + (1 + 4 * r1), rmask, eviction_policy=
            'evict_last', other=0.0)
        tmp31 = tl.load(in_ptr1 + (2 + 4 * r1), rmask, eviction_policy=
            'evict_last', other=0.0)
        tmp34 = tl.load(in_ptr1 + (3 + 4 * r1), rmask, eviction_policy=
            'evict_last', other=0.0)
        tmp54 = tl.load(in_ptr2 + r2, rmask, eviction_policy='evict_first',
            other=0.0)
        tmp55 = tl.load(in_ptr2 + 4 * r1, rmask, eviction_policy=
            'evict_last', other=0.0)
        tmp57 = tl.load(in_ptr2 + (1 + 4 * r1), rmask, eviction_policy=
            'evict_last', other=0.0)
        tmp60 = tl.load(in_ptr2 + (2 + 4 * r1), rmask, eviction_policy=
            'evict_last', other=0.0)
        tmp63 = tl.load(in_ptr2 + (3 + 4 * r1), rmask, eviction_policy=
            'evict_last', other=0.0)
        tmp76 = tl.load(in_ptr3 + r2, rmask, eviction_policy='evict_first',
            other=0.0)
        tmp77 = tl.load(in_ptr3 + 4 * r1, rmask, eviction_policy=
            'evict_last', other=0.0)
        tmp79 = tl.load(in_ptr3 + (1 + 4 * r1), rmask, eviction_policy=
            'evict_last', other=0.0)
        tmp82 = tl.load(in_ptr3 + (2 + 4 * r1), rmask, eviction_policy=
            'evict_last', other=0.0)
        tmp85 = tl.load(in_ptr3 + (3 + 4 * r1), rmask, eviction_policy=
            'evict_last', other=0.0)
        tmp2 = tl_math.exp(tmp1)
        tmp4 = tl_math.exp(tmp3)
        tmp5 = tmp2 + tmp4
        tmp7 = tl_math.exp(tmp6)
        tmp8 = tmp5 + tmp7
        tmp10 = tl_math.exp(tmp9)
        tmp11 = tmp8 + tmp10
        tmp12 = tl_math.log(tmp11)
        tmp13 = tmp0 - tmp12
        tmp14 = tl_math.exp(tmp13)
        tmp15 = 1e-06
        tmp16 = tmp14 + tmp15
        tmp17 = tl.full([1, 1], 1, tl.int32)
        tmp18 = tmp17 / tmp16
        tmp19 = 1.0
        tmp20 = tmp18 * tmp19
        tmp21 = tmp20 - tmp19
        tmp22 = tmp21 + tmp15
        tmp23 = tl_math.log(tmp22)
        tmp24 = -tmp23
        tmp27 = tl_math.exp(tmp26)
        tmp29 = tl_math.exp(tmp28)
        tmp30 = tmp27 + tmp29
        tmp32 = tl_math.exp(tmp31)
        tmp33 = tmp30 + tmp32
        tmp35 = tl_math.exp(tmp34)
        tmp36 = tmp33 + tmp35
        tmp37 = tl_math.log(tmp36)
        tmp38 = tmp25 - tmp37
        tmp39 = tl_math.exp(tmp38)
        tmp40 = tmp39 + tmp15
        tmp41 = tmp17 / tmp40
        tmp42 = tmp41 * tmp19
        tmp43 = tmp42 - tmp19
        tmp44 = tmp43 + tmp15
        tmp45 = tl_math.log(tmp44)
        tmp46 = -tmp45
        tmp47 = tmp24 - tmp46
        tmp48 = tmp14 * tmp47
        tmp49 = 2.0
        tmp50 = tmp48 * tmp49
        tmp51 = tl.broadcast_to(tmp50, [XBLOCK, RBLOCK])
        tmp53 = _tmp52 + tmp51
        _tmp52 = tl.where(rmask, tmp53, _tmp52)
        tmp56 = tl_math.exp(tmp55)
        tmp58 = tl_math.exp(tmp57)
        tmp59 = tmp56 + tmp58
        tmp61 = tl_math.exp(tmp60)
        tmp62 = tmp59 + tmp61
        tmp64 = tl_math.exp(tmp63)
        tmp65 = tmp62 + tmp64
        tmp66 = tl_math.log(tmp65)
        tmp67 = tmp54 - tmp66
        tmp68 = tl_math.exp(tmp67)
        tmp69 = tmp68 + tmp15
        tmp70 = tmp17 / tmp69
        tmp71 = tmp70 * tmp19
        tmp72 = tmp71 - tmp19
        tmp73 = tmp72 + tmp15
        tmp74 = tl_math.log(tmp73)
        tmp75 = -tmp74
        tmp78 = tl_math.exp(tmp77)
        tmp80 = tl_math.exp(tmp79)
        tmp81 = tmp78 + tmp80
        tmp83 = tl_math.exp(tmp82)
        tmp84 = tmp81 + tmp83
        tmp86 = tl_math.exp(tmp85)
        tmp87 = tmp84 + tmp86
        tmp88 = tl_math.log(tmp87)
        tmp89 = tmp76 - tmp88
        tmp90 = tl_math.exp(tmp89)
        tmp91 = tmp90 + tmp15
        tmp92 = tmp17 / tmp91
        tmp93 = tmp92 * tmp19
        tmp94 = tmp93 - tmp19
        tmp95 = tmp94 + tmp15
        tmp96 = tl_math.log(tmp95)
        tmp97 = -tmp96
        tmp98 = tmp75 - tmp97
        tmp99 = tmp68 * tmp98
        tmp100 = tmp99 * tmp49
        tmp101 = tl.broadcast_to(tmp100, [XBLOCK, RBLOCK])
        tmp103 = _tmp102 + tmp101
        _tmp102 = tl.where(rmask, tmp103, _tmp102)
    tmp52 = tl.sum(_tmp52, 1)[:, None]
    tmp102 = tl.sum(_tmp102, 1)[:, None]
    tmp104 = 0.015625
    tmp105 = tmp52 * tmp104
    tmp106 = tmp102 * tmp104
    tmp107 = tmp105 + tmp106
    tmp108 = 1.0
    tmp109 = tmp107 * tmp108
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp109, 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 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
        buf7 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused__log_softmax_0[grid(256)](arg0_1, buf0, buf7, 256,
            XBLOCK=128, num_warps=4, num_stages=1)
        del arg0_1
        buf2 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
        buf5 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
        triton_poi_fused__log_softmax_0[grid(256)](arg1_1, buf2, buf5, 256,
            XBLOCK=128, num_warps=4, num_stages=1)
        del arg1_1
        buf4 = empty_strided_cuda((), (), torch.float32)
        buf10 = buf4
        del buf4
        triton_red_fused__log_softmax_add_div_exp_log_mul_neg_reciprocal_sub_sum_1[
            grid(1)](buf10, buf0, buf2, buf5, buf7, 1, 256, XBLOCK=1,
            RBLOCK=256, num_warps=8, num_stages=1)
        del buf0
        del buf2
        del buf5
        del buf7
    return buf10,


def stable_kl(logit, target, epsilon=1e-06, reduce=True):
    logit = logit.view(-1, logit.size(-1)).float()
    target = target.view(-1, target.size(-1)).float()
    bs = logit.size(0)
    p = F.log_softmax(logit, 1).exp()
    y = F.log_softmax(target, 1).exp()
    rp = -(1.0 / (p + epsilon) - 1 + epsilon).detach().log()
    ry = -(1.0 / (y + epsilon) - 1 + epsilon).detach().log()
    if reduce:
        return (p * (rp - ry) * 2).sum() / bs
    else:
        return (p * (rp - ry) * 2).sum()


class Criterion(_Loss):

    def __init__(self, alpha=1.0, name='criterion'):
        super().__init__()
        """Alpha is used to weight each loss term
        """
        self.alpha = alpha
        self.name = name

    def forward(self, input, target, weight=None, ignore_index=-1):
        """weight: sample weight
        """
        return


class NsSymKlCriterionNew(Criterion):

    def __init__(self, alpha=1.0, name='KL Div Criterion'):
        super().__init__()
        self.alpha = alpha
        self.name = name

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