import torch
from torch import device
from torch._inductor.select_algorithm import extern_kernels
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
import torch.utils.data
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor


@triton.jit
def triton_poi_fused_add_exp_mul_0(in_ptr0, in_ptr1, in_ptr2, out_ptr0,
    xnumel, XBLOCK: tl.constexpr):
    xnumel = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + x0, xmask)
    tmp1 = tl.load(in_ptr1 + x0, xmask)
    tmp2 = tl.load(in_ptr2 + x0, xmask)
    tmp3 = 0.5
    tmp4 = tmp2 * tmp3
    tmp5 = tl_math.exp(tmp4)
    tmp6 = tmp1 * tmp5
    tmp7 = tmp0 + tmp6
    tl.store(out_ptr0 + x0, tmp7, xmask)


def call(args):
    (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6,
        primals_7, primals_8, primals_9) = args
    args.clear()
    assert_size_stride(primals_1, (4, 784), (784, 1))
    assert_size_stride(primals_2, (4, 784), (784, 1))
    assert_size_stride(primals_3, (4,), (1,))
    assert_size_stride(primals_4, (4, 4), (4, 1))
    assert_size_stride(primals_5, (4,), (1,))
    assert_size_stride(primals_6, (4, 4), (4, 1))
    assert_size_stride(primals_7, (4,), (1,))
    assert_size_stride(primals_8, (784, 4), (4, 1))
    assert_size_stride(primals_9, (784,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        extern_kernels.addmm(primals_3, primals_1, reinterpret_tensor(
            primals_2, (784, 4), (1, 784), 0), alpha=1, beta=1, out=buf0)
        del primals_2
        del primals_3
        buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        extern_kernels.addmm(primals_5, buf0, reinterpret_tensor(primals_4,
            (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf1)
        del primals_5
        buf2 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        extern_kernels.addmm(primals_7, buf0, reinterpret_tensor(primals_6,
            (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf2)
        del primals_7
        buf3 = torch.ops.aten.randn.default([4, 4], dtype=torch.float32,
            device=device(type='cuda', index=0), pin_memory=False)
        buf4 = buf3
        del buf3
        buf5 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_add_exp_mul_0[grid(16)](buf1, buf4, buf2, buf5, 16,
            XBLOCK=16, num_warps=1, num_stages=1)
        buf6 = empty_strided_cuda((4, 784), (784, 1), torch.float32)
        extern_kernels.addmm(primals_9, buf5, reinterpret_tensor(primals_8,
            (4, 784), (1, 4), 0), alpha=1, beta=1, out=buf6)
        del primals_9
    return (buf6, buf1, buf2, primals_1, buf0, buf2, buf4, buf5, primals_8,
        primals_6, primals_4)


class Swish(nn.Module):

    def __init__(self):
        super(Swish, self).__init__()

    def forward(self, x):
        return x * torch.sigmoid(x)


class BetaVAENew(nn.Module):
    activations = {'relu': nn.ReLU, 'sigmoid': nn.Sigmoid, 'swish': Swish,
        'tanh': nn.Tanh, 'lrelu': nn.LeakyReLU, 'Elu': nn.ELU, 'PReLU': nn.
        PReLU}

    def __init__(self, layers, activation='relu'):
        super(BetaVAENew, self).__init__()
        encoders = []
        decoders = []
        layers_len = len(layers)
        self.fc1 = nn.Linear(784, layers[0])
        for i in range(1, layers_len // 2):
            encoders.append(nn.Sequential(nn.Linear(layers[i - 1], layers[i
                ]), self.activations[activation]()))
        self.encoder = nn.Sequential(*encoders)
        self.mu = nn.Linear(layers[layers_len // 2 - 1], layers[layers_len //
            2])
        self.sig = nn.Linear(layers[layers_len // 2 - 1], layers[layers_len //
            2])
        for i in range(layers_len // 2 + 1, layers_len):
            decoders.append(nn.Sequential(nn.Linear(layers[i - 1], layers[i
                ]), self.activations[activation]()))
        self.decoder = nn.Sequential(*decoders)
        self.fc_end = nn.Linear(layers[-1], 784)

    def encode(self, x):
        x = self.fc1(x)
        x = self.encoder(x)
        return self.mu(x), self.sig(x)

    def reparameterize(self, mu, log_var):
        std = torch.exp(0.5 * log_var)
        eps = torch.randn_like(std)
        return mu + eps * std

    def decode(self, z):
        x = self.decoder(z)
        x = self.fc_end(x)
        return x

    def forward(self, input_0):
        primals_1 = self.fc1.weight
        primals_3 = self.fc1.bias
        primals_4 = self.mu.weight
        primals_5 = self.mu.bias
        primals_6 = self.sig.weight
        primals_7 = self.sig.bias
        primals_8 = self.fc_end.weight
        primals_9 = self.fc_end.bias
        primals_2 = input_0
        output = call([primals_1, primals_2, primals_3, primals_4,
            primals_5, primals_6, primals_7, primals_8, primals_9])
        return output[0], output[1], output[2]


Measured latency_ms: 0.059073