# AOT ID: ['0_forward']
from ctypes import c_void_p, c_long, c_int
import torch
import math
import random
import os
import tempfile
from math import inf, nan
from torch._inductor.hooks import run_intermediate_hooks
from torch._inductor.utils import maybe_profile
from torch._inductor.codegen.memory_planning import _align as align
from torch import device, empty_strided
from torch._inductor.async_compile import AsyncCompile
from torch._inductor.select_algorithm import extern_kernels
from torch._inductor.codegen.multi_kernel import MultiKernelCall
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid, split_scan_grid, grid_combo_kernels, start_graph, end_graph
from torch._C import _cuda_getCurrentRawStream as get_raw_stream

aten = torch.ops.aten
inductor_ops = torch.ops.inductor
_quantized = torch.ops._quantized
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cpu = torch._C._dynamo.guards._empty_strided_cpu
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
empty_strided_xpu = torch._C._dynamo.guards._empty_strided_xpu
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
alloc_from_pool = torch.ops.inductor._alloc_from_pool
async_compile = AsyncCompile()


# kernel path: runs/run_shard_1/inductor_cache/eo/ceoz2kphbqrqev46qe2nwklfye5ugifeuhwm6mzhnum2ftts7qhx.py
# Topologically Sorted Source Nodes: [diffs, truediv, abs_1, pow_1, add, distances, hist, sum_2, kl_div], Original ATen: [aten.sub, aten.div, aten.abs, aten.pow, aten.add, aten.reciprocal, aten.mul, aten.sum, aten.xlogy]
# Source node to ATen node mapping:
#   abs_1 => abs_1
#   add => add
#   diffs => sub
#   distances => mul, reciprocal
#   hist => sum_1
#   kl_div => div_2, eq, full_default, full_default_1, isnan, log_1, mul_1, mul_2, sub_1, sum_3, where, where_1
#   pow_1 => pow_1
#   sum_2 => sum_2
#   truediv => div
# Graph fragment:
#   %sub : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (%squeeze, %primals_2), kwargs = {})
#   %div : [num_users=1] = call_function[target=torch.ops.aten.div.Tensor](args = (%sub, 0.1), kwargs = {})
#   %abs_1 : [num_users=1] = call_function[target=torch.ops.aten.abs.default](args = (%div,), kwargs = {})
#   %pow_1 : [num_users=1] = call_function[target=torch.ops.aten.pow.Tensor_Scalar](args = (%abs_1, 4), kwargs = {})
#   %add : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%pow_1, 1), kwargs = {})
#   %reciprocal : [num_users=1] = call_function[target=torch.ops.aten.reciprocal.default](args = (%add,), kwargs = {})
#   %mul : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%reciprocal, 1), kwargs = {})
#   %sum_1 : [num_users=2] = call_function[target=torch.ops.aten.sum.dim_IntList](args = (%mul, [1]), kwargs = {})
#   %sum_2 : [num_users=2] = call_function[target=torch.ops.aten.sum.default](args = (%sum_1,), kwargs = {})
#   %mul_1 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%primals_3, %unsqueeze), kwargs = {})
#   %eq : [num_users=1] = call_function[target=torch.ops.aten.eq.Scalar](args = (%primals_3, 0), kwargs = {})
#   %log_1 : [num_users=1] = call_function[target=torch.ops.aten.log.default](args = (%primals_3,), kwargs = {})
#   %mul_2 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%primals_3, %log_1), kwargs = {})
#   %full_default : [num_users=1] = call_function[target=torch.ops.aten.full.default](args = ([], 0.0), kwargs = {dtype: torch.float32, layout: torch.strided, device: cuda:0, pin_memory: False})
#   %where : [num_users=1] = call_function[target=torch.ops.aten.where.self](args = (%eq, %full_default, %mul_2), kwargs = {})
#   %isnan : [num_users=1] = call_function[target=torch.ops.aten.isnan.default](args = (%primals_3,), kwargs = {})
#   %full_default_1 : [num_users=1] = call_function[target=torch.ops.aten.full.default](args = ([], nan), kwargs = {dtype: torch.float32, layout: torch.strided, device: cuda:0, pin_memory: False})
#   %where_1 : [num_users=1] = call_function[target=torch.ops.aten.where.self](args = (%isnan, %full_default_1, %where), kwargs = {})
#   %sub_1 : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (%where_1, %mul_1), kwargs = {})
#   %sum_3 : [num_users=1] = call_function[target=torch.ops.aten.sum.default](args = (%sub_1,), kwargs = {})
#   %div_2 : [num_users=1] = call_function[target=torch.ops.aten.div.Tensor](args = (%sum_3, 4), kwargs = {})
triton_red_fused_abs_add_div_mul_pow_reciprocal_sub_sum_xlogy_0 = async_compile.triton('triton_red_fused_abs_add_div_mul_pow_reciprocal_sub_sum_xlogy_0', '''
import triton
import triton.language as tl
from triton.compiler.compiler import AttrsDescriptor

from torch._inductor.runtime import triton_helpers, triton_heuristics
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
from torch._inductor.runtime.hints import AutotuneHint, ReductionHint, TileHint, instance_descriptor, DeviceProperties

@triton_heuristics.reduction(
    size_hints=[1, 2048],
    reduction_hint=ReductionHint.INNER,
    filename=__file__,
    triton_meta={'signature': {0: '*fp32', 1: '*fp32', 2: '*fp32', 3: '*fp32', 4: '*fp32', 5: '*fp32', 6: 'i32', 7: 'i32'}, 'device': DeviceProperties(type='cuda', index=0, cc=90, major=9, regs_per_multiprocessor=65536, max_threads_per_multi_processor=2048, multi_processor_count=132), 'constants': {6: 1}, 'configs': [AttrsDescriptor(divisible_by_16=(0, 1, 2, 3, 4, 5, 7), equal_to_1=(6,))]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_red_fused_abs_add_div_mul_pow_reciprocal_sub_sum_xlogy_0', 'mutated_arg_names': ['in_out_ptr0'], 'no_x_dim': False, 'num_load': 7, 'num_reduction': 2, 'backend_hash': 'B098E03CDA7B8ADC90DAFFDF24A2956451D1B13F297756A5DCC209498AA53705', 'are_deterministic_algorithms_enabled': False, 'assert_indirect_indexing': True, 'autotune_local_cache': True, 'autotune_pointwise': True, 'autotune_remote_cache': None, 'force_disable_caches': False, 'dynamic_scale_rblock': True, 'max_autotune': False, 'max_autotune_pointwise': False, 'min_split_scan_rblock': 256, 'spill_threshold': 16, 'store_cubin': False}
)
@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):
    xnumel = 1
    rnumel = 1600
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = 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)
''', device_str='cuda')


async_compile.wait(globals())
del async_compile

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  # reuse
        # Topologically Sorted Source Nodes: [diffs, truediv, abs_1, pow_1, add, distances, hist, sum_2, kl_div], Original ATen: [aten.sub, aten.div, aten.abs, aten.pow, aten.add, aten.reciprocal, aten.mul, aten.sum, aten.xlogy]
        stream0 = get_raw_stream(0)
        triton_red_fused_abs_add_div_mul_pow_reciprocal_sub_sum_xlogy_0.run(buf3, primals_1, primals_2, primals_3, buf0, buf1, 1, 1600, grid=grid(1), stream=stream0)
        del buf0
    return (buf3, primals_1, primals_2, primals_3, buf1, )


def benchmark_compiled_module(times=10, repeat=10):
    from torch._dynamo.testing import rand_strided
    from torch._inductor.utils import print_performance
    primals_1 = rand_strided((4, 4, 100, 4), (1600, 400, 4, 1), device='cuda:0', dtype=torch.float32)
    primals_2 = rand_strided((100, 1), (1, 1), device='cuda:0', dtype=torch.float32)
    primals_3 = rand_strided((100, 1), (1, 1), device='cuda:0', dtype=torch.float32)
    fn = lambda: call([primals_1, primals_2, primals_3])
    return print_performance(fn, times=times, repeat=repeat)


if __name__ == "__main__":
    from torch._inductor.wrapper_benchmark import compiled_module_main
    compiled_module_main('None', benchmark_compiled_module)

import torch
import torch.nn as nn


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 NormalDivLoss(nn.Module):

    def __init__(self, dist=fuzzyDist):
        super(NormalDivLoss, 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, x):
        hist = self.hist(x)
        hist_log = torch.log(hist).unsqueeze(1)
        return self.kl(hist_log, self.target)


def get_inputs():
    return [torch.rand([4, 4, 100, 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.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]
