import math
import torch
import torch.nn as nn
import torch.utils.data.dataloader
import torch.nn


class QuadriLinearScore(nn.Module):
    """
    Outer product version of quadrilinear function for sequence labeling.
    """

    def __init__(self, wemb_size, tagset_size, temb_size=20, rank=396, std=
        0.1545, window_size=1, normalization=True, **kwargs):
        """
        Args:
            wemb_size: word embedding hidden size
            tagset_size: tag set size
            temb_size: tag embedding size
            rank: rank of the weight tensor
            std: standard deviation of the tensor
        """
        super(QuadriLinearScore, self).__init__()
        self.wemb_size = wemb_size
        self.tagset_size = tagset_size
        self.temb_size = temb_size
        self.rank = rank
        self.std = std
        self.window_size = window_size
        self.normalization = normalization
        self.tag_emd = nn.Parameter(torch.Tensor(self.tagset_size, self.
            temb_size))
        self.T = nn.Parameter(torch.Tensor(self.wemb_size, self.rank))
        self.U = nn.Parameter(torch.Tensor(self.wemb_size, self.rank))
        self.V = nn.Parameter(torch.Tensor(self.temb_size, self.rank))
        self.W = nn.Parameter(torch.Tensor(self.temb_size, self.rank))
        self.rand_init()
        self

    def rand_init(self):
        """random initialization
        """
        nn.init.uniform_(self.tag_emd, a=math.sqrt(6 / self.temb_size), b=
            math.sqrt(6 / self.temb_size))
        nn.init.normal_(self.T, std=self.std)
        nn.init.normal_(self.U, std=self.std)
        nn.init.normal_(self.V, std=self.std)
        nn.init.normal_(self.W, std=self.std)

    def forward(self, word_emb):
        """
        Args:
            word_emb: [batch, sent_length, wemb_size]
        Returns: Tensor
            [batch, sent_length-window_size, tagset_size, tagset_size]
        """
        assert word_emb.size(2
            ) == self.wemb_size, 'batch sizes of encoder and decoder are requires to be equal.'
        g0 = torch.matmul(word_emb[:, :-self.window_size], self.U)
        g1 = torch.matmul(word_emb[:, self.window_size:], self.T)
        g2 = torch.matmul(self.tag_emd, self.V)
        g3 = torch.matmul(self.tag_emd, self.W)
        temp01 = g0 * g1
        temp012 = torch.einsum('nak,bk->nabk', [temp01, g2])
        score = torch.einsum('nabk,ck->nabc', [temp012, g3])
        if self.normalization:
            score = score / math.sqrt(self.rank)
        return score


def get_inputs():
    return [torch.rand([4, 4, 4])]


def get_init_inputs():
    return [[], {'wemb_size': 4, 'tagset_size': 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 math
import torch.nn as nn
import torch.utils.data.dataloader
import torch.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__unsafe_view_clone_0(in_ptr0, out_ptr0, xnumel, XBLOCK:
    tl.constexpr):
    xnumel = 48
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 4
    x1 = xindex // 4
    x2 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 4 * (x1 % 3) + 16 * (x1 // 3)), xmask)
    tl.store(out_ptr0 + x2, tmp0, xmask)


@triton.jit
def triton_poi_fused__unsafe_view_clone_1(in_ptr0, out_ptr0, xnumel, XBLOCK:
    tl.constexpr):
    xnumel = 48
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 4
    x1 = xindex // 4
    x2 = xindex
    tmp0 = tl.load(in_ptr0 + (4 + x0 + 4 * (x1 % 3) + 16 * (x1 // 3)), xmask)
    tl.store(out_ptr0 + x2, tmp0, xmask)


@triton.jit
def triton_poi_fused_mul_2(in_ptr0, in_ptr1, in_ptr2, out_ptr0, xnumel,
    XBLOCK: tl.constexpr):
    xnumel = 19008
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 396
    x2 = xindex // 1584
    x3 = xindex % 1584
    tmp0 = tl.load(in_ptr0 + (x0 + 396 * x2), xmask, eviction_policy=
        'evict_last')
    tmp1 = tl.load(in_ptr1 + (x0 + 396 * x2), xmask, eviction_policy=
        'evict_last')
    tmp3 = tl.load(in_ptr2 + x3, xmask, eviction_policy='evict_last')
    tmp2 = tmp0 * tmp1
    tmp4 = tmp2 * tmp3
    tl.store(out_ptr0 + (x3 + 1600 * x2), tmp4, xmask)


@triton.jit
def triton_poi_fused_bmm_transpose_3(in_ptr0, out_ptr0, out_ptr1, xnumel,
    XBLOCK: tl.constexpr):
    xnumel = 19008
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 396
    x1 = xindex // 396
    x2 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 396 * (x1 % 4) + 1600 * (x1 // 4)), xmask)
    tl.store(out_ptr0 + x2, tmp0, xmask)
    tl.store(out_ptr1 + x2, tmp0, xmask)


@triton.jit
def triton_poi_fused_div_4(in_out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 192
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = tl.load(in_out_ptr0 + x0, xmask)
    tmp1 = 0.050251890762960605
    tmp2 = tmp0 * tmp1
    tl.store(in_out_ptr0 + x0, tmp2, 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), (16, 4, 1))
    assert_size_stride(primals_2, (4, 396), (396, 1))
    assert_size_stride(primals_3, (4, 396), (396, 1))
    assert_size_stride(primals_4, (4, 20), (20, 1))
    assert_size_stride(primals_5, (20, 396), (396, 1))
    assert_size_stride(primals_6, (20, 396), (396, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((12, 4), (4, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused__unsafe_view_clone_0[grid(48)](primals_1, buf0, 48,
            XBLOCK=64, num_warps=1, num_stages=1)
        buf1 = empty_strided_cuda((12, 396), (396, 1), torch.float32)
        extern_kernels.mm(buf0, primals_2, out=buf1)
        del primals_2
        buf2 = empty_strided_cuda((12, 4), (4, 1), torch.float32)
        triton_poi_fused__unsafe_view_clone_1[grid(48)](primals_1, buf2, 48,
            XBLOCK=64, num_warps=1, num_stages=1)
        del primals_1
        buf3 = empty_strided_cuda((12, 396), (396, 1), torch.float32)
        extern_kernels.mm(buf2, primals_3, out=buf3)
        del primals_3
        buf4 = empty_strided_cuda((4, 396), (396, 1), torch.float32)
        extern_kernels.mm(primals_4, primals_5, out=buf4)
        buf5 = empty_strided_cuda((4, 396), (396, 1), torch.float32)
        extern_kernels.mm(primals_4, primals_6, out=buf5)
        buf6 = empty_strided_cuda((4, 3, 4, 396), (4800, 1600, 396, 1),
            torch.float32)
        triton_poi_fused_mul_2[grid(19008)](buf1, buf3, buf4, buf6, 19008,
            XBLOCK=256, num_warps=4, num_stages=1)
        buf7 = empty_strided_cuda((1, 48, 396), (19008, 396, 1), torch.float32)
        buf10 = empty_strided_cuda((1, 396, 48), (19008, 1, 396), torch.float32
            )
        triton_poi_fused_bmm_transpose_3[grid(19008)](buf6, buf7, buf10, 
            19008, XBLOCK=256, num_warps=4, num_stages=1)
        del buf6
        buf8 = empty_strided_cuda((1, 48, 4), (192, 4, 1), torch.float32)
        extern_kernels.bmm(buf7, reinterpret_tensor(buf5, (1, 396, 4), (0, 
            1, 396), 0), out=buf8)
        del buf7
        buf9 = reinterpret_tensor(buf8, (4, 3, 4, 4), (48, 16, 4, 1), 0)
        del buf8
        triton_poi_fused_div_4[grid(192)](buf9, 192, XBLOCK=256, num_warps=
            4, num_stages=1)
    return buf9, buf1, buf3, buf4, buf10, reinterpret_tensor(buf5, (1, 4, 
        396), (396, 396, 1), 0), reinterpret_tensor(primals_4, (20, 4), (1,
        20), 0), reinterpret_tensor(primals_6, (396, 20), (1, 396), 0
        ), reinterpret_tensor(primals_5, (396, 20), (1, 396), 0
        ), reinterpret_tensor(buf2, (4, 12), (1, 4), 0), reinterpret_tensor(
        buf0, (4, 12), (1, 4), 0)


class QuadriLinearScoreNew(nn.Module):
    """
    Outer product version of quadrilinear function for sequence labeling.
    """

    def __init__(self, wemb_size, tagset_size, temb_size=20, rank=396, std=
        0.1545, window_size=1, normalization=True, **kwargs):
        """
        Args:
            wemb_size: word embedding hidden size
            tagset_size: tag set size
            temb_size: tag embedding size
            rank: rank of the weight tensor
            std: standard deviation of the tensor
        """
        super(QuadriLinearScoreNew, self).__init__()
        self.wemb_size = wemb_size
        self.tagset_size = tagset_size
        self.temb_size = temb_size
        self.rank = rank
        self.std = std
        self.window_size = window_size
        self.normalization = normalization
        self.tag_emd = nn.Parameter(torch.Tensor(self.tagset_size, self.
            temb_size))
        self.T = nn.Parameter(torch.Tensor(self.wemb_size, self.rank))
        self.U = nn.Parameter(torch.Tensor(self.wemb_size, self.rank))
        self.V = nn.Parameter(torch.Tensor(self.temb_size, self.rank))
        self.W = nn.Parameter(torch.Tensor(self.temb_size, self.rank))
        self.rand_init()
        self

    def rand_init(self):
        """random initialization
        """
        nn.init.uniform_(self.tag_emd, a=math.sqrt(6 / self.temb_size), b=
            math.sqrt(6 / self.temb_size))
        nn.init.normal_(self.T, std=self.std)
        nn.init.normal_(self.U, std=self.std)
        nn.init.normal_(self.V, std=self.std)
        nn.init.normal_(self.W, std=self.std)

    def forward(self, input_0):
        primals_4 = self.tag_emd
        primals_2 = self.T
        primals_3 = self.U
        primals_5 = self.V
        primals_6 = self.W
        primals_1 = input_0
        output = call([primals_1, primals_2, primals_3, primals_4,
            primals_5, primals_6])
        return output[0]
