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 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_per_fused_div_exp_mean_pow_sub_sum_0(in_ptr0, in_ptr1, in_ptr2,
    out_ptr0, 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)
    r2 = rindex // 16
    r3 = rindex % 16
    r0 = rindex % 4
    tmp0 = tl.load(in_ptr0 + (r3 + 64 * r2), None)
    tmp1 = tl.load(in_ptr1 + r0, None, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr2 + r0, None, eviction_policy='evict_last')
    tmp7 = tl.load(in_ptr0 + (16 + r3 + 64 * r2), None)
    tmp12 = tl.load(in_ptr0 + (32 + r3 + 64 * r2), None)
    tmp17 = tl.load(in_ptr0 + (48 + r3 + 64 * r2), None)
    tmp2 = tmp0 - tmp1
    tmp3 = tmp2 * tmp2
    tmp5 = tl_math.exp(tmp4)
    tmp6 = tmp3 / tmp5
    tmp8 = tmp7 - tmp1
    tmp9 = tmp8 * tmp8
    tmp10 = tmp9 / tmp5
    tmp11 = tmp6 + tmp10
    tmp13 = tmp12 - tmp1
    tmp14 = tmp13 * tmp13
    tmp15 = tmp14 / tmp5
    tmp16 = tmp11 + tmp15
    tmp18 = tmp17 - tmp1
    tmp19 = tmp18 * tmp18
    tmp20 = tmp19 / tmp5
    tmp21 = tmp16 + tmp20
    tmp22 = tl.broadcast_to(tmp21, [XBLOCK, RBLOCK])
    tmp24 = tl.sum(tmp22, 1)[:, None]
    tl.store(out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp24, None)


@triton.jit
def triton_per_fused_add_div_exp_mean_mul_pow_sub_sum_1(in_out_ptr0,
    in_ptr0, 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)
    tmp4 = tl.load(in_out_ptr0 + 0)
    tmp5 = tl.broadcast_to(tmp4, [XBLOCK, 1])
    tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
    tmp3 = tl.sum(tmp1, 1)[:, None]
    tmp6 = 64.0
    tmp7 = tmp5 / tmp6
    tmp8 = 0.5
    tmp9 = tmp7 * tmp8
    tmp10 = 3.6757541328186907
    tmp11 = tmp9 + tmp10
    tmp12 = 1.0
    tmp13 = tmp3 / tmp12
    tmp14 = tmp13 * tmp8
    tmp15 = tmp11 + tmp14
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp15, None)


def call(args):
    primals_1, primals_2, primals_3 = args
    args.clear()
    assert_size_stride(primals_1, (1, 4), (4, 1))
    assert_size_stride(primals_2, (1, 4), (4, 1))
    assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((), (), torch.float32)
        get_raw_stream(0)
        triton_per_fused_div_exp_mean_pow_sub_sum_0[grid(1)](primals_3,
            primals_1, primals_2, buf0, 1, 64, XBLOCK=1, num_warps=2,
            num_stages=1)
        buf2 = buf0
        del buf0
        triton_per_fused_add_div_exp_mean_mul_pow_sub_sum_1[grid(1)](buf2,
            primals_2, 1, 4, XBLOCK=1, num_warps=2, num_stages=1)
    return buf2, primals_1, primals_2, primals_3


def compute_negative_ln_prob(Y, mu, ln_var, pdf):
    var = ln_var.exp()
    if pdf == 'gauss':
        negative_ln_prob = 0.5 * ((Y - mu) ** 2 / var).sum(1).mean(
            ) + 0.5 * Y.size(1) * math.log(2 * math.pi) + 0.5 * ln_var.sum(1
            ).mean()
    elif pdf == 'logistic':
        whitened = (Y - mu) / var
        adjust = torch.logsumexp(torch.stack([torch.zeros(Y.size()), -
            whitened]), 0)
        negative_ln_prob = whitened.sum(1).mean() + 2 * adjust.sum(1).mean(
            ) + ln_var.sum(1).mean()
    else:
        raise ValueError('Unknown PDF: %s' % pdf)
    return negative_ln_prob


class PDFNew(nn.Module):

    def __init__(self, dim, pdf):
        super(PDFNew, self).__init__()
        assert pdf in {'gauss', 'logistic'}
        self.dim = dim
        self.pdf = pdf
        self.mu = nn.Embedding(1, self.dim)
        self.ln_var = nn.Embedding(1, self.dim)

    def forward(self, input_0):
        primals_1 = self.mu.weight
        primals_2 = self.ln_var.weight
        primals_3 = input_0
        output = call([primals_1, primals_2, primals_3])
        return output[0]


Measured latency_ms: 0.027065