import torch
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 import triton_helpers
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
import torch.sparse
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
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor


@triton.jit
def triton_poi_fused_tanh_0(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr
    ):
    xnumel = 9600
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex
    x0 = xindex % 600
    tmp0 = tl.load(in_out_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp3 = libdevice.tanh(tmp2)
    tl.store(in_out_ptr0 + x2, tmp3, xmask)


@triton.jit
def triton_poi_fused__softmax_tanh_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.
    constexpr):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex
    x1 = xindex // 4
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tmp2 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp7 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp10 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp1 = libdevice.tanh(tmp0)
    tmp3 = libdevice.tanh(tmp2)
    tmp5 = libdevice.tanh(tmp4)
    tmp6 = triton_helpers.maximum(tmp3, tmp5)
    tmp8 = libdevice.tanh(tmp7)
    tmp9 = triton_helpers.maximum(tmp6, tmp8)
    tmp11 = libdevice.tanh(tmp10)
    tmp12 = triton_helpers.maximum(tmp9, tmp11)
    tmp13 = tmp1 - tmp12
    tmp14 = tl_math.exp(tmp13)
    tl.store(out_ptr0 + x2, tmp14, xmask)


@triton.jit
def triton_poi_fused__softmax_mul_2(in_ptr0, in_ptr1, out_ptr0, xnumel,
    XBLOCK: tl.constexpr):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x3 = xindex
    x4 = xindex // 4
    x0 = xindex % 4
    x2 = xindex // 16
    tmp0 = tl.load(in_ptr0 + x3, xmask)
    tmp1 = tl.load(in_ptr0 + 4 * x4, xmask, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr0 + (1 + 4 * x4), xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr0 + (2 + 4 * x4), xmask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr0 + (3 + 4 * x4), xmask, eviction_policy='evict_last')
    tmp9 = tl.load(in_ptr1 + (4 * x0 + 16 * x2), xmask, eviction_policy=
        'evict_last')
    tmp13 = tl.load(in_ptr1 + (1 + 4 * x0 + 16 * x2), xmask,
        eviction_policy='evict_last')
    tmp17 = tl.load(in_ptr1 + (2 + 4 * x0 + 16 * x2), xmask,
        eviction_policy='evict_last')
    tmp21 = tl.load(in_ptr1 + (3 + 4 * x0 + 16 * x2), xmask,
        eviction_policy='evict_last')
    tmp3 = tmp1 + tmp2
    tmp5 = tmp3 + tmp4
    tmp7 = tmp5 + tmp6
    tmp8 = tmp0 / tmp7
    tmp10 = 0.0
    tmp11 = tmp9 != tmp10
    tmp12 = tmp11.to(tl.int64)
    tmp14 = tmp13 != tmp10
    tmp15 = tmp14.to(tl.int64)
    tmp16 = tmp12 + tmp15
    tmp18 = tmp17 != tmp10
    tmp19 = tmp18.to(tl.int64)
    tmp20 = tmp16 + tmp19
    tmp22 = tmp21 != tmp10
    tmp23 = tmp22.to(tl.int64)
    tmp24 = tmp20 + tmp23
    tmp25 = tmp24.to(tl.float32)
    tmp26 = tmp8 * tmp25
    tl.store(out_ptr0 + x3, tmp26, xmask)


def call(args):
    (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6,
        primals_7, primals_8, primals_9, primals_10, primals_11, primals_12,
        primals_13, primals_14, primals_15) = args
    args.clear()
    assert_size_stride(primals_1, (4, 4, 4), (16, 4, 1))
    assert_size_stride(primals_2, (600, 4), (4, 1))
    assert_size_stride(primals_3, (600,), (1,))
    assert_size_stride(primals_4, (600, 600), (600, 1))
    assert_size_stride(primals_5, (600,), (1,))
    assert_size_stride(primals_6, (200, 600), (600, 1))
    assert_size_stride(primals_7, (200,), (1,))
    assert_size_stride(primals_8, (200, 600), (600, 1))
    assert_size_stride(primals_9, (200,), (1,))
    assert_size_stride(primals_10, (600, 200), (200, 1))
    assert_size_stride(primals_11, (600,), (1,))
    assert_size_stride(primals_12, (600, 600), (600, 1))
    assert_size_stride(primals_13, (600,), (1,))
    assert_size_stride(primals_14, (4, 600), (600, 1))
    assert_size_stride(primals_15, (4,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((16, 600), (600, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(primals_1, (16, 4), (4, 1), 0),
            reinterpret_tensor(primals_2, (4, 600), (1, 4), 0), out=buf0)
        del primals_2
        buf1 = reinterpret_tensor(buf0, (4, 4, 600), (2400, 600, 1), 0)
        del buf0
        get_raw_stream(0)
        triton_poi_fused_tanh_0[grid(9600)](buf1, primals_3, 9600, XBLOCK=
            128, num_warps=4, num_stages=1)
        del primals_3
        buf2 = empty_strided_cuda((16, 600), (600, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(buf1, (16, 600), (600, 1), 0),
            reinterpret_tensor(primals_4, (600, 600), (1, 600), 0), out=buf2)
        buf3 = reinterpret_tensor(buf2, (4, 4, 600), (2400, 600, 1), 0)
        del buf2
        triton_poi_fused_tanh_0[grid(9600)](buf3, primals_5, 9600, XBLOCK=
            128, num_warps=4, num_stages=1)
        del primals_5
        buf4 = empty_strided_cuda((16, 200), (200, 1), torch.float32)
        extern_kernels.addmm(primals_7, reinterpret_tensor(buf3, (16, 600),
            (600, 1), 0), reinterpret_tensor(primals_6, (600, 200), (1, 600
            ), 0), alpha=1, beta=1, out=buf4)
        del primals_7
        buf5 = empty_strided_cuda((16, 200), (200, 1), torch.float32)
        extern_kernels.addmm(primals_9, reinterpret_tensor(buf3, (16, 600),
            (600, 1), 0), reinterpret_tensor(primals_8, (600, 200), (1, 600
            ), 0), alpha=1, beta=1, out=buf5)
        del primals_9
        buf6 = empty_strided_cuda((16, 600), (600, 1), torch.float32)
        extern_kernels.mm(buf4, reinterpret_tensor(primals_10, (200, 600),
            (1, 200), 0), out=buf6)
        buf7 = reinterpret_tensor(buf6, (4, 4, 600), (2400, 600, 1), 0)
        del buf6
        triton_poi_fused_tanh_0[grid(9600)](buf7, primals_11, 9600, XBLOCK=
            128, num_warps=4, num_stages=1)
        del primals_11
        buf8 = empty_strided_cuda((16, 600), (600, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(buf7, (16, 600), (600, 1), 0),
            reinterpret_tensor(primals_12, (600, 600), (1, 600), 0), out=buf8)
        buf9 = reinterpret_tensor(buf8, (4, 4, 600), (2400, 600, 1), 0)
        del buf8
        triton_poi_fused_tanh_0[grid(9600)](buf9, primals_13, 9600, XBLOCK=
            128, num_warps=4, num_stages=1)
        del primals_13
        buf10 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
        extern_kernels.addmm(primals_15, reinterpret_tensor(buf9, (16, 600),
            (600, 1), 0), reinterpret_tensor(primals_14, (600, 4), (1, 600),
            0), alpha=1, beta=1, out=buf10)
        del primals_15
        buf11 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        triton_poi_fused__softmax_tanh_1[grid(64)](buf10, buf11, 64, XBLOCK
            =64, num_warps=1, num_stages=1)
        buf12 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        triton_poi_fused__softmax_mul_2[grid(64)](buf11, primals_1, buf12, 
            64, XBLOCK=64, num_warps=1, num_stages=1)
        del buf11
    return (buf12, reinterpret_tensor(buf4, (4, 4, 200), (800, 200, 1), 0),
        reinterpret_tensor(buf5, (4, 4, 200), (800, 200, 1), 0), primals_1,
        buf1, buf3, buf4, buf7, buf9, buf10, primals_14, primals_12,
        primals_10, primals_8, primals_6, primals_4)


class MultVAE_encoder(nn.Module):

    def __init__(self, item_dim: 'int', hidden_dim=600, latent_dim=200,
        n_hidden_layers=1, dropout=0.5, nonlinearity=nn.Tanh):
        super(MultVAE_encoder, self).__init__()
        self.item_dim = item_dim
        self.latent_dim = latent_dim
        self.nonlinearity = nn.Tanh()
        self.layers = nn.Sequential()
        self.layers.add_module('input_dropout', nn.Dropout(dropout))
        self.layers.add_module('linear_enc_1', nn.Linear(in_features=
            item_dim, out_features=hidden_dim))
        self.layers.add_module('Tanh_enc_1', self.nonlinearity)
        if n_hidden_layers > 0:
            for i in range(n_hidden_layers):
                self.layers.add_module('hidden_enc_{}'.format(i + 1), nn.
                    Linear(in_features=hidden_dim, out_features=hidden_dim))
                self.layers.add_module('Tanh_enc_{}'.format(i + 2), self.
                    nonlinearity)
        self.mu = nn.Linear(in_features=hidden_dim, out_features=latent_dim)
        self.logvar = nn.Linear(in_features=hidden_dim, out_features=latent_dim
            )

    def forward(self, x):
        output = self.layers(x)
        mu = self.mu(output)
        logvar = self.logvar(output)
        return mu, logvar


class MultVAE_decoder(nn.Module):

    def __init__(self, item_dim, hidden_dim=600, latent_dim=200,
        n_hidden_layers=1, nonlinearity=nn.Tanh):
        super(MultVAE_decoder, self).__init__()
        self.item_dim = item_dim
        self.latent_dim = latent_dim
        self.nonlinearity = nonlinearity()
        self.layers = nn.Sequential()
        self.layers.add_module('linear_dec_1', nn.Linear(in_features=
            latent_dim, out_features=hidden_dim))
        self.layers.add_module('Tanh_dec_1', self.nonlinearity)
        if n_hidden_layers > 0:
            for i in range(n_hidden_layers):
                self.layers.add_module('hidden_dec_{}'.format(i + 1), nn.
                    Linear(in_features=hidden_dim, out_features=hidden_dim))
                self.layers.add_module('Tanh_dec_{}'.format(i + 2), self.
                    nonlinearity)
        self.item_layer = nn.Linear(in_features=hidden_dim, out_features=
            item_dim)

    def forward(self, x):
        output = self.layers(x)
        items = self.item_layer(output)
        items = self.nonlinearity(items)
        return items


class MultVaeNew(nn.Module):

    def __init__(self, item_dim, hidden_dim=600, latent_dim=200,
        n_enc_hidden_layers=1, n_dec_hidden_layers=1, dropout=0.5):
        super(MultVaeNew, self).__init__()
        self.item_dim = item_dim
        self.encoder = MultVAE_encoder(item_dim=item_dim, hidden_dim=
            hidden_dim, latent_dim=latent_dim, n_hidden_layers=
            n_enc_hidden_layers, dropout=0.5, nonlinearity=nn.Tanh)
        self.decoder = MultVAE_decoder(item_dim=item_dim, hidden_dim=
            hidden_dim, latent_dim=latent_dim, n_hidden_layers=
            n_dec_hidden_layers, nonlinearity=nn.Tanh)
        self.softmax = nn.Softmax(dim=2)

    def reparameterize(self, mu, logvar):
        if self.training:
            std = torch.exp(0.5 * logvar)
            eps = torch.randn_like(std)
            return eps.mul(std).add_(mu)
        else:
            return mu

    def forward(self, input_0):
        primals_2 = self.encoder.layers.linear_enc_1.weight
        primals_3 = self.encoder.layers.linear_enc_1.bias
        primals_4 = self.encoder.layers.hidden_enc_1.weight
        primals_5 = self.encoder.layers.hidden_enc_1.bias
        primals_6 = self.encoder.mu.weight
        primals_7 = self.encoder.mu.bias
        primals_8 = self.encoder.logvar.weight
        primals_9 = self.encoder.logvar.bias
        primals_10 = self.decoder.layers.linear_dec_1.weight
        primals_11 = self.decoder.layers.linear_dec_1.bias
        primals_12 = self.decoder.layers.hidden_dec_1.weight
        primals_13 = self.decoder.layers.hidden_dec_1.bias
        primals_14 = self.decoder.item_layer.weight
        primals_15 = self.decoder.item_layer.bias
        primals_1 = input_0
        output = call([primals_1, primals_2, primals_3, primals_4,
            primals_5, primals_6, primals_7, primals_8, primals_9,
            primals_10, primals_11, primals_12, primals_13, primals_14,
            primals_15])
        return output[0], output[1], output[2]


Measured latency_ms: 0.064472