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 logging
import numpy as np
from torch.nn import functional as F
import torch.multiprocessing
from torch import 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_relu_0(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr
    ):
    xnumel = 32000
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex
    x0 = xindex % 500
    tmp0 = tl.load(in_out_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp3 = tl.full([1], 0, tl.int32)
    tmp4 = triton_helpers.maximum(tmp3, tmp2)
    tl.store(in_out_ptr0 + x2, tmp4, xmask)


@triton.jit
def triton_poi_fused_relu_threshold_backward_1(in_out_ptr0, in_ptr0,
    out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 32000
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex
    x0 = xindex % 500
    tmp0 = tl.load(in_out_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp3 = tl.full([1], 0, tl.int32)
    tmp4 = triton_helpers.maximum(tmp3, tmp2)
    tmp5 = 0.0
    tmp6 = tmp4 <= tmp5
    tl.store(in_out_ptr0 + x2, tmp4, xmask)
    tl.store(out_ptr0 + x2, tmp6, xmask)


@triton.jit
def triton_poi_fused_tanh_2(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr
    ):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex
    x0 = xindex % 4
    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_add_tanh_3(in_ptr0, in_ptr1, 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 + x0, xmask)
    tmp2 = tl.load(in_ptr1 + x0, xmask)
    tmp1 = libdevice.tanh(tmp0)
    tmp3 = tmp1 + tmp2
    tl.store(out_ptr0 + x0, tmp3, xmask)


@triton.jit
def triton_poi_fused__softmax_4(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
    ):
    xnumel = 256
    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)
    tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp3 = triton_helpers.maximum(tmp1, tmp2)
    tmp5 = triton_helpers.maximum(tmp3, tmp4)
    tmp7 = triton_helpers.maximum(tmp5, tmp6)
    tmp8 = tmp0 - tmp7
    tmp9 = tl_math.exp(tmp8)
    tl.store(out_ptr0 + x2, tmp9, xmask)


@triton.jit
def triton_poi_fused__softmax_5(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
    ):
    xnumel = 256
    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)
    tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp3 = tmp1 + tmp2
    tmp5 = tmp3 + tmp4
    tmp7 = tmp5 + tmp6
    tmp8 = tmp0 / tmp7
    tl.store(out_ptr0 + x2, tmp8, 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, primals_16, primals_17,
        primals_18, primals_19, primals_20) = args
    args.clear()
    assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_2, (500, 4), (4, 1))
    assert_size_stride(primals_3, (500,), (1,))
    assert_size_stride(primals_4, (500, 500), (500, 1))
    assert_size_stride(primals_5, (500,), (1,))
    assert_size_stride(primals_6, (500, 4), (4, 1))
    assert_size_stride(primals_7, (4, 500), (500, 1))
    assert_size_stride(primals_8, (4,), (1,))
    assert_size_stride(primals_9, (4, 500), (500, 1))
    assert_size_stride(primals_10, (4,), (1,))
    assert_size_stride(primals_11, (4, 4), (4, 1))
    assert_size_stride(primals_12, (4,), (1,))
    assert_size_stride(primals_13, (4, 4), (4, 1))
    assert_size_stride(primals_14, (4,), (1,))
    assert_size_stride(primals_15, (4, 4), (4, 1))
    assert_size_stride(primals_16, (4,), (1,))
    assert_size_stride(primals_17, (4, 4), (4, 1))
    assert_size_stride(primals_18, (4,), (1,))
    assert_size_stride(primals_19, (4, 4), (4, 1))
    assert_size_stride(primals_20, (4,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((64, 500), (500, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(primals_1, (64, 4), (4, 1), 0),
            reinterpret_tensor(primals_2, (4, 500), (1, 4), 0), out=buf0)
        del primals_2
        buf1 = buf0
        del buf0
        get_raw_stream(0)
        triton_poi_fused_relu_0[grid(32000)](buf1, primals_3, 32000, XBLOCK
            =256, num_warps=4, num_stages=1)
        del primals_3
        buf2 = empty_strided_cuda((64, 500), (500, 1), torch.float32)
        extern_kernels.mm(buf1, reinterpret_tensor(primals_4, (500, 500), (
            1, 500), 0), out=buf2)
        buf3 = buf2
        del buf2
        buf18 = empty_strided_cuda((64, 500), (500, 1), torch.bool)
        triton_poi_fused_relu_threshold_backward_1[grid(32000)](buf3,
            primals_5, buf18, 32000, XBLOCK=256, num_warps=4, num_stages=1)
        del primals_5
        buf4 = empty_strided_cuda((64, 500), (500, 1), torch.float32)
        extern_kernels.addmm(buf3, reinterpret_tensor(primals_1, (64, 4), (
            4, 1), 0), reinterpret_tensor(primals_6, (4, 500), (1, 4), 0),
            alpha=1, beta=1, out=buf4)
        del buf3
        del primals_6
        buf5 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
        extern_kernels.addmm(primals_8, buf4, reinterpret_tensor(primals_7,
            (500, 4), (1, 500), 0), alpha=1, beta=1, out=buf5)
        del primals_8
        buf6 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
        extern_kernels.addmm(primals_10, buf4, reinterpret_tensor(primals_9,
            (500, 4), (1, 500), 0), alpha=1, beta=1, out=buf6)
        del primals_10
        buf7 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
        extern_kernels.mm(buf5, reinterpret_tensor(primals_11, (4, 4), (1, 
            4), 0), out=buf7)
        buf8 = buf7
        del buf7
        triton_poi_fused_tanh_2[grid(256)](buf8, primals_12, 256, XBLOCK=
            128, num_warps=4, num_stages=1)
        del primals_12
        buf9 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
        extern_kernels.mm(buf8, reinterpret_tensor(primals_13, (4, 4), (1, 
            4), 0), out=buf9)
        buf10 = buf9
        del buf9
        triton_poi_fused_tanh_2[grid(256)](buf10, primals_14, 256, XBLOCK=
            128, num_warps=4, num_stages=1)
        del primals_14
        buf11 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
        extern_kernels.mm(buf10, reinterpret_tensor(primals_15, (4, 4), (1,
            4), 0), out=buf11)
        buf12 = buf11
        del buf11
        triton_poi_fused_tanh_2[grid(256)](buf12, primals_16, 256, XBLOCK=
            128, num_warps=4, num_stages=1)
        del primals_16
        buf13 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
        extern_kernels.addmm(primals_18, buf12, reinterpret_tensor(
            primals_17, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf13)
        del primals_18
        buf14 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
        triton_poi_fused_add_tanh_3[grid(256)](buf13, buf5, buf14, 256,
            XBLOCK=256, num_warps=4, num_stages=1)
        buf15 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
        extern_kernels.addmm(primals_20, buf14, reinterpret_tensor(
            primals_19, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf15)
        del primals_20
        buf16 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
        triton_poi_fused__softmax_4[grid(256)](buf15, buf16, 256, XBLOCK=
            256, num_warps=4, num_stages=1)
        buf17 = buf15
        del buf15
        triton_poi_fused__softmax_5[grid(256)](buf16, buf17, 256, XBLOCK=
            256, num_warps=4, num_stages=1)
        del buf16
    return (buf5, buf14, buf17, buf6, reinterpret_tensor(primals_1, (64, 4),
        (4, 1), 0), buf1, buf4, buf5, buf8, buf10, buf12, buf13, buf14,
        buf17, primals_19, primals_17, primals_15, primals_13, primals_11,
        primals_9, primals_7, buf18, primals_4)


class NTMNew(nn.Module):

    def __init__(self, opt, hidden_dim=500, l1_strength=0.001):
        super(NTMNew, self).__init__()
        self.input_dim = opt.bow_vocab_size
        self.topic_num = opt.topic_num
        topic_num = opt.topic_num
        self.fc11 = nn.Linear(self.input_dim, hidden_dim)
        self.fc12 = nn.Linear(hidden_dim, hidden_dim)
        self.fc21 = nn.Linear(hidden_dim, topic_num)
        self.fc22 = nn.Linear(hidden_dim, topic_num)
        self.fcs = nn.Linear(self.input_dim, hidden_dim, bias=False)
        self.fcg1 = nn.Linear(topic_num, topic_num)
        self.fcg2 = nn.Linear(topic_num, topic_num)
        self.fcg3 = nn.Linear(topic_num, topic_num)
        self.fcg4 = nn.Linear(topic_num, topic_num)
        self.fcd1 = nn.Linear(topic_num, self.input_dim)
        self.l1_strength = torch.FloatTensor([l1_strength])

    def encode(self, x):
        e1 = F.relu(self.fc11(x))
        e1 = F.relu(self.fc12(e1))
        e1 = e1.add(self.fcs(x))
        return self.fc21(e1), self.fc22(e1)

    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 generate(self, h):
        g1 = torch.tanh(self.fcg1(h))
        g1 = torch.tanh(self.fcg2(g1))
        g1 = torch.tanh(self.fcg3(g1))
        g1 = torch.tanh(self.fcg4(g1))
        g1 = g1.add(h)
        return g1

    def decode(self, z):
        d1 = F.softmax(self.fcd1(z), dim=1)
        return d1

    def print_topic_words(self, vocab_dic, fn, n_top_words=10):
        beta_exp = self.fcd1.weight.data.cpu().numpy().T
        logging.info('Writing to %s' % fn)
        fw = open(fn, 'w')
        for k, beta_k in enumerate(beta_exp):
            topic_words = [vocab_dic[w_id] for w_id in np.argsort(beta_k)[:
                -n_top_words - 1:-1]]
            None
            fw.write('{}\n'.format(' '.join(topic_words)))
        fw.close()

    def get_topic_words(self):
        return self.fcd1.weight.T

    def forward(self, input_0):
        primals_2 = self.fc11.weight
        primals_3 = self.fc11.bias
        primals_4 = self.fc12.weight
        primals_5 = self.fc12.bias
        primals_7 = self.fc21.weight
        primals_8 = self.fc21.bias
        primals_9 = self.fc22.weight
        primals_10 = self.fc22.bias
        primals_6 = self.fcs.weight
        primals_11 = self.fcg1.weight
        primals_12 = self.fcg1.bias
        primals_13 = self.fcg2.weight
        primals_14 = self.fcg2.bias
        primals_15 = self.fcg3.weight
        primals_16 = self.fcg3.bias
        primals_17 = self.fcg4.weight
        primals_18 = self.fcg4.bias
        primals_19 = self.fcd1.weight
        primals_20 = self.fcd1.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, primals_16, primals_17, primals_18, primals_19,
            primals_20])
        return output[0], output[1], output[2], output[3], output[4]


Measured latency_ms: 0.036815