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 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_poi_fused_div_exp_log_neg_pow_sub_0(in_ptr0, in_ptr1, in_ptr2,
    in_ptr3, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + 0)
    tmp1 = tl.broadcast_to(tmp0, [XBLOCK])
    tmp4 = tl.load(in_ptr1 + 0)
    tmp5 = tl.broadcast_to(tmp4, [XBLOCK])
    tmp9 = tl.load(in_ptr2 + x0, xmask)
    tmp10 = tl.load(in_ptr3 + 0)
    tmp11 = tl.broadcast_to(tmp10, [XBLOCK])
    tmp2 = tl_math.log(tmp1)
    tmp3 = -tmp2
    tmp6 = 0.5
    tmp7 = tmp5 * tmp6
    tmp8 = tmp3 - tmp7
    tmp12 = tmp9 - tmp11
    tmp13 = tmp12 / tmp1
    tmp14 = tmp13 * tmp13
    tmp15 = tmp14 * tmp6
    tmp16 = tmp8 - tmp15
    tmp17 = tl_math.exp(tmp16)
    tl.store(out_ptr0 + x0, tmp17, xmask)


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


class Normal_ModelNew(nn.Module):
    """
    Example of a module for modeling a probability distribution. This is set up with all pieces
    required for use with the rest of this package. (initial parameters; as well as implimented
    constrain, forward, and log_prob methods)
    """

    def __init__(self, init_mean: 'torch.Tensor'=torch.Tensor([0]),
        init_std: 'torch.Tensor'=torch.Tensor([1])):
        super(Normal_ModelNew, self).__init__()
        self.mean = nn.Parameter(init_mean, requires_grad=True)
        self.std = nn.Parameter(init_std, requires_grad=True)
        self.ln2p = nn.Parameter(torch.log(2 * torch.Tensor([torch.pi])),
            requires_grad=False)

    def constrain(self):
        """
        Method to run on "constrain" step of training. Easiest method for optimization under
        constraint is Projection Optimization by simply clamping parameters to bounds after each
        update. This is certainly not the most efficent way, but it gets the job done.
        """
        eps = 1e-06
        self.std.data = model.std.data.clamp(min=eps)

    def log_prob(self, x):
        """
        Returns the log probability of the items in tensor 'x' according to the probability
        distribution of the module.
        """
        return -torch.log(self.std.unsqueeze(-1)) - self.ln2p / 2 - ((x -
            self.mean.unsqueeze(-1)) / self.std.unsqueeze(-1)).pow(2) / 2

    def forward(self, input_0):
        primals_1 = self.mean
        primals_2 = self.std
        primals_3 = self.ln2p
        primals_4 = input_0
        output = call([primals_1, primals_2, primals_3, primals_4])
        return output[0]


Measured latency_ms: 0.016799