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


@triton.jit
def triton_red_fused_abs_add_div_mul_pow_reciprocal_sub_sum_xlogy_0(in_out_ptr0
    , in_ptr0, in_ptr1, in_ptr2, out_ptr0, out_ptr1, xnumel, rnumel, XBLOCK:
    tl.constexpr, RBLOCK: tl.constexpr):
    rnumel = 1600
    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, :]
    _tmp44 = tl.full([XBLOCK, RBLOCK], 0, tl.float32)
    for roffset in range(0, rnumel, RBLOCK):
        rindex = roffset + rbase
        rmask = rindex < rnumel
        r2 = rindex // 400
        r4 = rindex % 400
        r1 = rindex // 4 % 100
        r3 = rindex
        tmp0 = tl.load(in_ptr0 + (r4 + 1600 * r2), rmask, eviction_policy=
            'evict_first', other=0.0)
        tmp1 = tl.load(in_ptr1 + r1, rmask, eviction_policy='evict_last',
            other=0.0)
        tmp13 = tl.load(in_ptr0 + (400 + r4 + 1600 * r2), rmask,
            eviction_policy='evict_first', other=0.0)
        tmp23 = tl.load(in_ptr0 + (800 + r4 + 1600 * r2), rmask,
            eviction_policy='evict_first', other=0.0)
        tmp33 = tl.load(in_ptr0 + (1200 + r4 + 1600 * r2), rmask,
            eviction_policy='evict_first', other=0.0)
        tmp2 = tmp0 - tmp1
        tmp3 = 10.0
        tmp4 = tmp2 * tmp3
        tmp5 = tl_math.abs(tmp4)
        tmp6 = tmp5 * tmp5
        tmp7 = tmp6 * tmp6
        tmp8 = 1.0
        tmp9 = tmp7 + tmp8
        tmp10 = tl.full([1, 1], 1, tl.int32)
        tmp11 = tmp10 / tmp9
        tmp12 = tmp11 * tmp8
        tmp14 = tmp13 - tmp1
        tmp15 = tmp14 * tmp3
        tmp16 = tl_math.abs(tmp15)
        tmp17 = tmp16 * tmp16
        tmp18 = tmp17 * tmp17
        tmp19 = tmp18 + tmp8
        tmp20 = tmp10 / tmp19
        tmp21 = tmp20 * tmp8
        tmp22 = tmp12 + tmp21
        tmp24 = tmp23 - tmp1
        tmp25 = tmp24 * tmp3
        tmp26 = tl_math.abs(tmp25)
        tmp27 = tmp26 * tmp26
        tmp28 = tmp27 * tmp27
        tmp29 = tmp28 + tmp8
        tmp30 = tmp10 / tmp29
        tmp31 = tmp30 * tmp8
        tmp32 = tmp22 + tmp31
        tmp34 = tmp33 - tmp1
        tmp35 = tmp34 * tmp3
        tmp36 = tl_math.abs(tmp35)
        tmp37 = tmp36 * tmp36
        tmp38 = tmp37 * tmp37
        tmp39 = tmp38 + tmp8
        tmp40 = tmp10 / tmp39
        tmp41 = tmp40 * tmp8
        tmp42 = tmp32 + tmp41
        tmp43 = tl.broadcast_to(tmp42, [XBLOCK, RBLOCK])
        tmp45 = _tmp44 + tmp43
        _tmp44 = tl.where(rmask, tmp45, _tmp44)
        tl.store(out_ptr0 + tl.broadcast_to(r3, [XBLOCK, RBLOCK]), tmp42, rmask
            )
    tmp44 = tl.sum(_tmp44, 1)[:, None]
    tl.store(out_ptr1 + tl.full([XBLOCK, 1], 0, tl.int32), tmp44, None)
    _tmp61 = tl.full([XBLOCK, RBLOCK], 0, tl.float32)
    for roffset in range(0, rnumel, RBLOCK):
        rindex = roffset + rbase
        rmask = rindex < rnumel
        r1 = rindex // 4 % 100
        r3 = rindex
        tmp46 = tl.load(in_ptr2 + r1, rmask, eviction_policy='evict_last',
            other=0.0)
        tmp55 = tl.load(out_ptr0 + r3, rmask, eviction_policy='evict_first',
            other=0.0)
        tmp47 = libdevice.isnan(tmp46).to(tl.int1)
        tmp48 = 0.0
        tmp49 = tmp46 == tmp48
        tmp50 = tl_math.log(tmp46)
        tmp51 = tmp46 * tmp50
        tmp52 = tl.where(tmp49, tmp48, tmp51)
        tmp53 = float('nan')
        tmp54 = tl.where(tmp47, tmp53, tmp52)
        tmp56 = tmp55 / tmp44
        tmp57 = tl_math.log(tmp56)
        tmp58 = tmp46 * tmp57
        tmp59 = tmp54 - tmp58
        tmp60 = tl.broadcast_to(tmp59, [XBLOCK, RBLOCK])
        tmp62 = _tmp61 + tmp60
        _tmp61 = tl.where(rmask, tmp62, _tmp61)
    tmp61 = tl.sum(_tmp61, 1)[:, None]
    tmp63 = 0.25
    tmp64 = tmp61 * tmp63
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp64, None)


def call(args):
    primals_1, primals_2, primals_3 = args
    args.clear()
    assert_size_stride(primals_1, (4, 4, 100, 4), (1600, 400, 4, 1))
    assert_size_stride(primals_2, (100, 1), (1, 1))
    assert_size_stride(primals_3, (100, 1), (1, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 100, 4), (400, 4, 1), torch.float32)
        buf1 = empty_strided_cuda((), (), torch.float32)
        buf2 = empty_strided_cuda((), (), torch.float32)
        buf3 = buf2
        del buf2
        get_raw_stream(0)
        triton_red_fused_abs_add_div_mul_pow_reciprocal_sub_sum_xlogy_0[grid(1)
            ](buf3, primals_1, primals_2, primals_3, buf0, buf1, 1, 1600,
            XBLOCK=1, RBLOCK=2048, num_warps=16, num_stages=1)
        del buf0
    return buf3, primals_1, primals_2, primals_3, buf1


def fuzzyDist(x, a=0.1, b=2):
    return 1 / (1 + (x / a).abs().pow(2 * b))


class SoftHist(nn.Module):

    def __init__(self, bins, dist):
        super(SoftHist, self).__init__()
        bins[1] - bins[0]
        self.bins = nn.Parameter(bins.unsqueeze(1))
        self.dist = dist
        self.pdf = lambda h: h / h.sum()

    def forward(self, x):
        diffs = x.squeeze() - self.bins
        distances = self.dist(diffs)
        hist = distances.sum(1)
        hist_norm = self.pdf(hist)
        return hist_norm


class NormalDivLossNew(nn.Module):

    def __init__(self, dist=fuzzyDist):
        super(NormalDivLossNew, self).__init__()
        bins = torch.arange(-10, 10, 0.2)
        binwidth = bins[1] - bins[0]
        self.hist = SoftHist(bins, dist)
        self.kl = nn.KLDivLoss(reduction='batchmean')
        self.target = nn.Parameter(binwidth * torch.distributions.normal.
            Normal(0, 0.3).log_prob(bins).exp().unsqueeze(1))

    def forward(self, input_0):
        primals_2 = self.target
        primals_3 = self.hist.bins
        primals_1 = input_0
        output = call([primals_1, primals_2, primals_3])
        return output[0]


Measured latency_ms: 0.019124