import torch
import torch.nn as nn
import torch.utils.data
from torch.nn.init import xavier_uniform_


class TorchGloVeModel(nn.Module):

    def __init__(self, n_words, embed_dim):
        super().__init__()
        self.n_words = n_words
        self.embed_dim = embed_dim
        self.W = self._init_weights(self.n_words, self.embed_dim)
        self.C = self._init_weights(self.n_words, self.embed_dim)
        self.bw = self._init_weights(self.n_words, 1)
        self.bc = self._init_weights(self.n_words, 1)

    def _init_weights(self, m, n):
        return nn.Parameter(xavier_uniform_(torch.empty(m, n)))

    def forward(self, X_log, idx):
        """
        Parameters
        ----------
        X_log : torch.FloatTensor, shape `(batch_size, n_vocab)`.

        idx : torch.LongTensor, shape `(batch_size, )`
            Indices of the vocab items in the current batch.

        Returns
        -------
        torch.FloatTensor, shape `(n_vocab, n_vocab)`.

        """
        preds = self.W[idx].matmul(self.C.T) + self.bw[idx] + self.bc.T
        diffs = preds - X_log
        return diffs


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


def get_init_inputs():
    return [[], {'n_words': 4, 'embed_dim': 4}]

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
import torch.nn as nn
import torch.utils.data
from torch.nn.init import xavier_uniform_
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_index_0(in_ptr0, in_ptr1, 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')
    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)
    tl.store(out_ptr0 + x2, tmp6, xmask)


@triton.jit
def triton_poi_fused_add_index_sub_1(in_ptr0, in_ptr1, in_ptr2, in_ptr3,
    in_ptr4, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x3 = xindex % 16
    x1 = xindex // 4 % 4
    x0 = xindex % 4
    x4 = xindex
    tmp0 = tl.load(in_ptr0 + x3, xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last')
    tmp9 = tl.load(in_ptr3 + x0, xmask, eviction_policy='evict_last')
    tmp11 = tl.load(in_ptr4 + x4, xmask)
    tmp2 = tl.full([XBLOCK], 4, tl.int32)
    tmp3 = tmp1 + tmp2
    tmp4 = tmp1 < 0
    tmp5 = tl.where(tmp4, tmp3, tmp1)
    tl.device_assert((0 <= tmp5) & (tmp5 < 4) | ~xmask,
        'index out of bounds: 0 <= tmp5 < 4')
    tmp7 = tl.load(in_ptr2 + tmp5, xmask, eviction_policy='evict_last')
    tmp8 = tmp0 + tmp7
    tmp10 = tmp8 + tmp9
    tmp12 = tmp10 - tmp11
    tl.store(out_ptr0 + x4, tmp12, xmask)


def call(args):
    primals_1, primals_2, primals_3, primals_4, primals_5, primals_6 = 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))
    assert_size_stride(primals_4, (4, 1), (1, 1))
    assert_size_stride(primals_5, (4, 1), (1, 1))
    assert_size_stride(primals_6, (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, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_index_0[grid(16)](primals_2, primals_1, buf0, 16,
            XBLOCK=16, num_warps=1, num_stages=1)
        del primals_1
        buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        extern_kernels.mm(buf0, reinterpret_tensor(primals_3, (4, 4), (1, 4
            ), 0), out=buf1)
        buf2 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        triton_poi_fused_add_index_sub_1[grid(256)](buf1, primals_2,
            primals_4, primals_5, primals_6, buf2, 256, XBLOCK=128,
            num_warps=4, num_stages=1)
        del buf1
        del primals_4
        del primals_5
        del primals_6
    return buf2, primals_2, buf0, primals_3


class TorchGloVeModelNew(nn.Module):

    def __init__(self, n_words, embed_dim):
        super().__init__()
        self.n_words = n_words
        self.embed_dim = embed_dim
        self.W = self._init_weights(self.n_words, self.embed_dim)
        self.C = self._init_weights(self.n_words, self.embed_dim)
        self.bw = self._init_weights(self.n_words, 1)
        self.bc = self._init_weights(self.n_words, 1)

    def _init_weights(self, m, n):
        return nn.Parameter(xavier_uniform_(torch.empty(m, n)))

    def forward(self, input_0, input_1):
        primals_1 = self.W
        primals_3 = self.C
        primals_4 = self.bw
        primals_5 = self.bc
        primals_6 = input_0
        primals_2 = input_1
        output = call([primals_1, primals_2, primals_3, primals_4,
            primals_5, primals_6])
        return output[0]
