import torch
import torch.distributions as dist
import torch.nn as nn


class Decoder_h(nn.Module):

    def __init__(self, B, H_dim):
        super().__init__()
        self.B = B
        self.H_dim = H_dim
        self.make_parameters()

    def make_parameters(self):
        self.mu = nn.Linear(self.H_dim, self.B, bias=False)
        self.sigma = nn.Linear(self.H_dim, self.B, bias=False)
        torch.nn.init.uniform_(self.sigma.weight, a=1.0, b=2.0)

    def _log_likelihood(self, h):
        """
        h: shape=(BS,N,H_dim)
        """
        BS, S, H_dim = h.shape
        return dist.Normal(self.mu.weight.view(1, 1, self.B, H_dim), self.
            sigma.weight.view(1, 1, self.B, self.H_dim)).log_prob(h.view(BS,
            S, 1, H_dim))

    def forward(self, z):
        """
        z: shape = (BS,N) or (BS,) or (1,)
        """
        h_dist = dist.Normal(self.mu.weight[z], self.sigma.weight[z])
        return h_dist.rsample()


def get_inputs():
    return [torch.ones([4], dtype=torch.int64)]


def get_init_inputs():
    return [[], {'B': 4, 'H_dim': 4}]

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
import torch.distributions as dist
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_add_index_mul_0(in_ptr0, in_ptr1, in_ptr2, in_ptr3,
    out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x1 = xindex // 4
    x0 = xindex % 4
    x2 = xindex
    tmp0 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last')
    tmp7 = tl.load(in_ptr2 + x2, xmask)
    tmp1 = tl.full([XBLOCK], 4, tl.int32)
    tmp2 = tmp0 + tmp1
    tmp3 = tmp0 < 0
    tmp4 = tl.where(tmp3, tmp2, tmp0)
    tl.device_assert((0 <= tmp4) & (tmp4 < 4) | ~xmask,
        'index out of bounds: 0 <= tmp4 < 4')
    tmp6 = tl.load(in_ptr1 + (x0 + 4 * tmp4), xmask)
    tmp8 = tl.load(in_ptr3 + (x0 + 4 * tmp4), xmask)
    tmp9 = tmp7 * tmp8
    tmp10 = tmp6 + tmp9
    tl.store(out_ptr0 + x2, tmp10, xmask)


def call(args):
    primals_1, primals_2, primals_3 = args
    args.clear()
    assert_size_stride(primals_1, (4, 4), (4, 1))
    assert_size_stride(primals_2, (4,), (1,))
    assert_size_stride(primals_3, (4, 4), (4, 1))
    buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf1 = torch.ops.aten.normal_functional.default(buf0)
        buf2 = buf1
        del buf1
        buf3 = buf0
        del buf0
        get_raw_stream(0)
        triton_poi_fused_add_index_mul_0[grid(16)](primals_2, primals_1,
            buf2, primals_3, buf3, 16, XBLOCK=16, num_warps=1, num_stages=1)
        del primals_1
        del primals_3
    return buf3, primals_2, buf2


class Decoder_hNew(nn.Module):

    def __init__(self, B, H_dim):
        super().__init__()
        self.B = B
        self.H_dim = H_dim
        self.make_parameters()

    def make_parameters(self):
        self.mu = nn.Linear(self.H_dim, self.B, bias=False)
        self.sigma = nn.Linear(self.H_dim, self.B, bias=False)
        torch.nn.init.uniform_(self.sigma.weight, a=1.0, b=2.0)

    def _log_likelihood(self, h):
        """
        h: shape=(BS,N,H_dim)
        """
        BS, S, H_dim = h.shape
        return dist.Normal(self.mu.weight.view(1, 1, self.B, H_dim), self.
            sigma.weight.view(1, 1, self.B, self.H_dim)).log_prob(h.view(BS,
            S, 1, H_dim))

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