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


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

    def __init__(self, wemb_size, tagset_size, temb_size=20, rank=396, std=
        0.1545, 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(HexaLinearScore, self).__init__()
        self.wemb_size = wemb_size
        self.tagset_size = tagset_size
        self.temb_size = temb_size
        self.rank = rank
        self.std = std
        self.normalization = normalization
        self.tag_emd = nn.Parameter(torch.Tensor(self.tagset_size, self.
            temb_size))
        self.W1 = nn.Parameter(torch.Tensor(self.wemb_size, self.rank))
        self.W2 = nn.Parameter(torch.Tensor(self.wemb_size, self.rank))
        self.W3 = nn.Parameter(torch.Tensor(self.wemb_size, self.rank))
        self.T1 = nn.Parameter(torch.Tensor(self.temb_size, self.rank))
        self.T2 = nn.Parameter(torch.Tensor(self.temb_size, self.rank))
        self.T3 = 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.T1, std=self.std)
        nn.init.normal_(self.T2, std=self.std)
        nn.init.normal_(self.T3, std=self.std)
        nn.init.normal_(self.W1, std=self.std)
        nn.init.normal_(self.W2, std=self.std)
        nn.init.normal_(self.W3, 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.'
        g1 = torch.matmul(word_emb[:, :-2], self.W1)
        g2 = torch.matmul(word_emb[:, 1:-1], self.W2)
        g3 = torch.matmul(word_emb[:, 2:], self.W3)
        g4 = torch.matmul(self.tag_emd, self.T1)
        g5 = torch.matmul(self.tag_emd, self.T2)
        g6 = torch.matmul(self.tag_emd, self.T3)
        temp01 = g1 * g2 * g3
        temp02 = torch.einsum('ak,bk,ck->abck', [g4, g5, g6])
        score = torch.einsum('nmk,abck->nmabc', [temp01, temp02])
        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 = 32
    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 % 2) + 16 * (x1 // 2)), 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 = 32
    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 % 2) + 16 * (x1 // 2)), xmask)
    tl.store(out_ptr0 + x2, tmp0, xmask)


@triton.jit
def triton_poi_fused__unsafe_view_clone_2(in_ptr0, out_ptr0, xnumel, XBLOCK:
    tl.constexpr):
    xnumel = 32
    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 + (8 + x0 + 4 * (x1 % 2) + 16 * (x1 // 2)), xmask)
    tl.store(out_ptr0 + x2, tmp0, xmask)


@triton.jit
def triton_poi_fused_mul_3(in_ptr0, in_ptr1, in_ptr2, out_ptr0, xnumel,
    XBLOCK: tl.constexpr):
    xnumel = 3168
    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)
    tmp3 = tl.load(in_ptr2 + x0, xmask)
    tmp2 = tmp0 * tmp1
    tmp4 = tmp2 * tmp3
    tl.store(out_ptr0 + x0, tmp4, xmask)


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


@triton.jit
def triton_poi_fused_bmm_transpose_5(in_ptr0, out_ptr0, out_ptr1, xnumel,
    XBLOCK: tl.constexpr):
    xnumel = 25344
    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_6(in_out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 512
    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,
        primals_7, primals_8) = 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, 396), (396, 1))
    assert_size_stride(primals_5, (4, 20), (20, 1))
    assert_size_stride(primals_6, (20, 396), (396, 1))
    assert_size_stride(primals_7, (20, 396), (396, 1))
    assert_size_stride(primals_8, (20, 396), (396, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((8, 4), (4, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused__unsafe_view_clone_0[grid(32)](primals_1, buf0, 32,
            XBLOCK=32, num_warps=1, num_stages=1)
        buf1 = empty_strided_cuda((8, 396), (396, 1), torch.float32)
        extern_kernels.mm(buf0, primals_2, out=buf1)
        del primals_2
        buf2 = empty_strided_cuda((8, 4), (4, 1), torch.float32)
        triton_poi_fused__unsafe_view_clone_1[grid(32)](primals_1, buf2, 32,
            XBLOCK=32, num_warps=1, num_stages=1)
        buf3 = empty_strided_cuda((8, 396), (396, 1), torch.float32)
        extern_kernels.mm(buf2, primals_3, out=buf3)
        del primals_3
        buf4 = empty_strided_cuda((8, 4), (4, 1), torch.float32)
        triton_poi_fused__unsafe_view_clone_2[grid(32)](primals_1, buf4, 32,
            XBLOCK=32, num_warps=1, num_stages=1)
        del primals_1
        buf5 = empty_strided_cuda((8, 396), (396, 1), torch.float32)
        extern_kernels.mm(buf4, primals_4, out=buf5)
        del primals_4
        buf6 = empty_strided_cuda((4, 396), (396, 1), torch.float32)
        extern_kernels.mm(primals_5, primals_6, out=buf6)
        buf7 = empty_strided_cuda((4, 396), (396, 1), torch.float32)
        extern_kernels.mm(primals_5, primals_7, out=buf7)
        buf8 = empty_strided_cuda((4, 396), (396, 1), torch.float32)
        extern_kernels.mm(primals_5, primals_8, out=buf8)
        buf9 = empty_strided_cuda((4, 2, 396), (792, 396, 1), torch.float32)
        triton_poi_fused_mul_3[grid(3168)](buf1, buf3, buf5, buf9, 3168,
            XBLOCK=128, num_warps=4, num_stages=1)
        buf10 = empty_strided_cuda((4, 4, 4, 396), (6400, 1600, 396, 1),
            torch.float32)
        triton_poi_fused_mul_4[grid(25344)](buf8, buf6, buf7, buf10, 25344,
            XBLOCK=128, num_warps=4, num_stages=1)
        buf11 = empty_strided_cuda((1, 396, 64), (25344, 1, 396), torch.float32
            )
        buf14 = empty_strided_cuda((1, 64, 396), (25344, 396, 1), torch.float32
            )
        triton_poi_fused_bmm_transpose_5[grid(25344)](buf10, buf11, buf14, 
            25344, XBLOCK=128, num_warps=4, num_stages=1)
        del buf10
        buf12 = empty_strided_cuda((1, 8, 64), (512, 64, 1), torch.float32)
        extern_kernels.bmm(reinterpret_tensor(buf9, (1, 8, 396), (0, 396, 1
            ), 0), buf11, out=buf12)
        del buf11
        buf13 = reinterpret_tensor(buf12, (4, 2, 4, 4, 4), (128, 64, 16, 4,
            1), 0)
        del buf12
        triton_poi_fused_div_6[grid(512)](buf13, 512, XBLOCK=256, num_warps
            =4, num_stages=1)
    return buf13, buf1, buf3, buf5, buf6, buf7, buf8, reinterpret_tensor(buf9,
        (1, 396, 8), (3168, 1, 396), 0), buf14, reinterpret_tensor(primals_5,
        (20, 4), (1, 20), 0), reinterpret_tensor(primals_8, (396, 20), (1, 
        396), 0), reinterpret_tensor(primals_7, (396, 20), (1, 396), 0
        ), reinterpret_tensor(primals_6, (396, 20), (1, 396), 0
        ), reinterpret_tensor(buf4, (4, 8), (1, 4), 0), reinterpret_tensor(buf2
        , (4, 8), (1, 4), 0), reinterpret_tensor(buf0, (4, 8), (1, 4), 0)


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

    def __init__(self, wemb_size, tagset_size, temb_size=20, rank=396, std=
        0.1545, 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(HexaLinearScoreNew, self).__init__()
        self.wemb_size = wemb_size
        self.tagset_size = tagset_size
        self.temb_size = temb_size
        self.rank = rank
        self.std = std
        self.normalization = normalization
        self.tag_emd = nn.Parameter(torch.Tensor(self.tagset_size, self.
            temb_size))
        self.W1 = nn.Parameter(torch.Tensor(self.wemb_size, self.rank))
        self.W2 = nn.Parameter(torch.Tensor(self.wemb_size, self.rank))
        self.W3 = nn.Parameter(torch.Tensor(self.wemb_size, self.rank))
        self.T1 = nn.Parameter(torch.Tensor(self.temb_size, self.rank))
        self.T2 = nn.Parameter(torch.Tensor(self.temb_size, self.rank))
        self.T3 = 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.T1, std=self.std)
        nn.init.normal_(self.T2, std=self.std)
        nn.init.normal_(self.T3, std=self.std)
        nn.init.normal_(self.W1, std=self.std)
        nn.init.normal_(self.W2, std=self.std)
        nn.init.normal_(self.W3, std=self.std)

    def forward(self, input_0):
        primals_5 = self.tag_emd
        primals_2 = self.W1
        primals_3 = self.W2
        primals_4 = self.W3
        primals_6 = self.T1
        primals_7 = self.T2
        primals_8 = self.T3
        primals_1 = input_0
        output = call([primals_1, primals_2, primals_3, primals_4,
            primals_5, primals_6, primals_7, primals_8])
        return output[0]
