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 math as tl_math
import numpy as np
from torch import 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_per_fused_add_div_log_mul_neg_pow_sub_sum_0(in_out_ptr0, in_ptr0,
    in_ptr1, in_ptr2, 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)
    r0 = rindex % 16
    r1 = rindex // 16
    tmp0 = tl.load(in_ptr0 + (r0 + 64 * r1), None)
    tmp1 = tl.load(in_ptr1 + (r0 + 64 * r1), None)
    tmp6 = tl.load(in_ptr2 + (r0 + 64 * r1), None)
    tmp9 = tl.load(in_ptr0 + (16 + r0 + 64 * r1), None)
    tmp10 = tl.load(in_ptr1 + (16 + r0 + 64 * r1), None)
    tmp14 = tl.load(in_ptr2 + (16 + r0 + 64 * r1), None)
    tmp18 = tl.load(in_ptr0 + (32 + r0 + 64 * r1), None)
    tmp19 = tl.load(in_ptr1 + (32 + r0 + 64 * r1), None)
    tmp23 = tl.load(in_ptr2 + (32 + r0 + 64 * r1), None)
    tmp27 = tl.load(in_ptr0 + (48 + r0 + 64 * r1), None)
    tmp28 = tl.load(in_ptr1 + (48 + r0 + 64 * r1), None)
    tmp32 = tl.load(in_ptr2 + (48 + r0 + 64 * r1), None)
    tmp2 = tmp0 - tmp1
    tmp3 = tmp2 * tmp2
    tmp4 = -0.5
    tmp5 = tmp3 * tmp4
    tmp7 = tmp6 * tmp6
    tmp8 = tmp5 * tmp7
    tmp11 = tmp9 - tmp10
    tmp12 = tmp11 * tmp11
    tmp13 = tmp12 * tmp4
    tmp15 = tmp14 * tmp14
    tmp16 = tmp13 * tmp15
    tmp17 = tmp8 + tmp16
    tmp20 = tmp18 - tmp19
    tmp21 = tmp20 * tmp20
    tmp22 = tmp21 * tmp4
    tmp24 = tmp23 * tmp23
    tmp25 = tmp22 * tmp24
    tmp26 = tmp17 + tmp25
    tmp29 = tmp27 - tmp28
    tmp30 = tmp29 * tmp29
    tmp31 = tmp30 * tmp4
    tmp33 = tmp32 * tmp32
    tmp34 = tmp31 * tmp33
    tmp35 = tmp26 + tmp34
    tmp36 = tl_math.log(tmp7)
    tmp37 = tl_math.log(tmp15)
    tmp38 = tmp36 + tmp37
    tmp39 = tl_math.log(tmp24)
    tmp40 = tmp38 + tmp39
    tmp41 = tl_math.log(tmp33)
    tmp42 = tmp40 + tmp41
    tmp43 = 0.5
    tmp44 = tmp42 * tmp43
    tmp45 = tmp44 + tmp35
    tmp46 = -3.6757541328186907
    tmp47 = tmp45 + tmp46
    tmp48 = -tmp47
    tmp49 = tl.broadcast_to(tmp48, [XBLOCK, RBLOCK])
    tmp51 = tl.sum(tmp49, 1)[:, None]
    tmp52 = 0.25
    tmp53 = tmp51 * tmp52
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp53, None)


def call(args):
    arg0_1, arg1_1, arg2_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))
    assert_size_stride(arg2_1, (4, 4, 4, 4), (64, 16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf1 = empty_strided_cuda((), (), torch.float32)
        buf2 = buf1
        del buf1
        get_raw_stream(0)
        triton_per_fused_add_div_log_mul_neg_pow_sub_sum_0[grid(1)](buf2,
            arg1_1, arg2_1, arg0_1, 1, 64, XBLOCK=1, num_warps=2, num_stages=1)
        del arg0_1
        del arg1_1
        del arg2_1
    return buf2,


class Nloss_GDNew(nn.Module):

    def __init__(self, dim):
        super(Nloss_GDNew, self).__init__()
        self.dim = dim
        torch.manual_seed(0)

    def get_likelihoods(self, X, Y, Beta, eps=1e-06):
        inv_det = Beta.prod(dim=1)
        if (inv_det < eps).any():
            inv_det += (inv_det < eps).type(torch.FloatTensor) * eps
        det = 1 / inv_det
        None
        norm_term = 1 / torch.sqrt((2 * np.pi) ** self.dim * torch.abs(det))
        inv_covars = Beta
        dist = (Y - X).pow(2)
        exponent = (-0.5 * dist * inv_covars).sum(dim=1)
        pk = norm_term * exponent.exp()
        return pk

    def get_log_likelihoods(self, X, Y, sq_Beta, eps=1e-06):
        Beta = sq_Beta ** 2
        log_det_term = 0.5 * Beta.log().sum(dim=1)
        norm_term = -0.5 * np.log(2 * np.pi) * self.dim
        inv_covars = Beta
        dist = (Y - X).pow(2)
        exponent = (-0.5 * dist * inv_covars).sum(dim=1)
        log_p = log_det_term + exponent + norm_term
        return log_p

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


Measured latency_ms: 0.015893