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.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_mean_tanh_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.
    constexpr):
    xnumel = 16
    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_ptr0 + (16 + x0), xmask)
    tmp5 = tl.load(in_ptr0 + (32 + x0), xmask)
    tmp8 = tl.load(in_ptr0 + (48 + x0), xmask)
    tmp1 = libdevice.tanh(tmp0)
    tmp3 = libdevice.tanh(tmp2)
    tmp4 = tmp1 + tmp3
    tmp6 = libdevice.tanh(tmp5)
    tmp7 = tmp4 + tmp6
    tmp9 = libdevice.tanh(tmp8)
    tmp10 = tmp7 + tmp9
    tmp11 = 4.0
    tmp12 = tmp10 / tmp11
    tl.store(out_ptr0 + x0, tmp12, xmask)


@triton.jit
def triton_per_fused__softmax_1(in_ptr0, out_ptr0, out_ptr1, xnumel, rnumel,
    XBLOCK: tl.constexpr):
    RBLOCK: tl.constexpr = 16
    xoffset = tl.program_id(0) * XBLOCK
    xoffset + tl.arange(0, XBLOCK)[:, None]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    rindex = tl.arange(0, RBLOCK)[None, :]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    r0 = rindex
    tmp0 = tl.load(in_ptr0 + r0, None)
    tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
    tmp3 = triton_helpers.max2(tmp1, 1)[:, None]
    tmp4 = tmp0 - tmp3
    tmp5 = tl_math.exp(tmp4)
    tmp6 = tl.broadcast_to(tmp5, [XBLOCK, RBLOCK])
    tmp8 = tl.sum(tmp6, 1)[:, None]
    tl.store(out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp3, None)
    tl.store(out_ptr1 + tl.full([XBLOCK, 1], 0, tl.int32), tmp8, None)


@triton.jit
def triton_poi_fused_add_mul_2(in_ptr0, in_ptr1, in_ptr2, in_ptr3, out_ptr0,
    xnumel, XBLOCK: tl.constexpr):
    xnumel = 64
    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 + 0)
    tmp2 = tl.broadcast_to(tmp1, [XBLOCK])
    tmp3 = tl.load(in_ptr2 + 0)
    tmp4 = tl.broadcast_to(tmp3, [XBLOCK])
    tmp7 = tl.load(in_ptr3 + 0)
    tmp8 = tl.broadcast_to(tmp7, [XBLOCK])
    tmp13 = tl.load(in_ptr0 + (64 + x0), xmask)
    tmp14 = tl.load(in_ptr1 + 1)
    tmp15 = tl.broadcast_to(tmp14, [XBLOCK])
    tmp21 = tl.load(in_ptr0 + (128 + x0), xmask)
    tmp22 = tl.load(in_ptr1 + 2)
    tmp23 = tl.broadcast_to(tmp22, [XBLOCK])
    tmp29 = tl.load(in_ptr0 + (192 + x0), xmask)
    tmp30 = tl.load(in_ptr1 + 3)
    tmp31 = tl.broadcast_to(tmp30, [XBLOCK])
    tmp5 = tmp2 - tmp4
    tmp6 = tl_math.exp(tmp5)
    tmp9 = tmp6 / tmp8
    tmp10 = tmp0 * tmp9
    tmp11 = 0.0
    tmp12 = tmp10 + tmp11
    tmp16 = tmp15 - tmp4
    tmp17 = tl_math.exp(tmp16)
    tmp18 = tmp17 / tmp8
    tmp19 = tmp13 * tmp18
    tmp20 = tmp12 + tmp19
    tmp24 = tmp23 - tmp4
    tmp25 = tl_math.exp(tmp24)
    tmp26 = tmp25 / tmp8
    tmp27 = tmp21 * tmp26
    tmp28 = tmp20 + tmp27
    tmp32 = tmp31 - tmp4
    tmp33 = tl_math.exp(tmp32)
    tmp34 = tmp33 / tmp8
    tmp35 = tmp29 * tmp34
    tmp36 = tmp28 + tmp35
    tl.store(out_ptr0 + x0, tmp36, xmask)


def call(args):
    primals_1, primals_2, primals_3, primals_4 = args
    args.clear()
    assert_size_stride(primals_1, (1, 4), (4, 1))
    assert_size_stride(primals_2, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_3, (4, 4), (4, 1))
    assert_size_stride(primals_4, (4,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
        extern_kernels.addmm(primals_4, reinterpret_tensor(primals_2, (16, 
            4), (4, 1), 0), reinterpret_tensor(primals_3, (4, 4), (1, 4), 0
            ), alpha=1, beta=1, out=buf0)
        buf1 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
        extern_kernels.addmm(primals_4, reinterpret_tensor(primals_2, (16, 
            4), (4, 1), 64), reinterpret_tensor(primals_3, (4, 4), (1, 4), 
            0), alpha=1, beta=1, out=buf1)
        buf2 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
        extern_kernels.addmm(primals_4, reinterpret_tensor(primals_2, (16, 
            4), (4, 1), 128), reinterpret_tensor(primals_3, (4, 4), (1, 4),
            0), alpha=1, beta=1, out=buf2)
        buf3 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
        extern_kernels.addmm(primals_4, reinterpret_tensor(primals_2, (16, 
            4), (4, 1), 192), reinterpret_tensor(primals_3, (4, 4), (1, 4),
            0), alpha=1, beta=1, out=buf3)
        del primals_3
        del primals_4
        buf4 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_mean_tanh_0[grid(16)](buf0, buf4, 16, XBLOCK=16,
            num_warps=1, num_stages=1)
        buf12 = empty_strided_cuda((1, 16), (16, 1), torch.float32)
        buf5 = reinterpret_tensor(buf12, (1, 4), (16, 1), 0)
        extern_kernels.mm(primals_1, reinterpret_tensor(buf4, (4, 4), (1, 4
            ), 0), out=buf5)
        buf6 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        triton_poi_fused_mean_tanh_0[grid(16)](buf1, buf6, 16, XBLOCK=16,
            num_warps=1, num_stages=1)
        buf7 = reinterpret_tensor(buf12, (1, 4), (16, 1), 4)
        extern_kernels.mm(primals_1, reinterpret_tensor(buf6, (4, 4), (1, 4
            ), 0), out=buf7)
        buf8 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        triton_poi_fused_mean_tanh_0[grid(16)](buf2, buf8, 16, XBLOCK=16,
            num_warps=1, num_stages=1)
        buf9 = reinterpret_tensor(buf12, (1, 4), (16, 1), 8)
        extern_kernels.mm(primals_1, reinterpret_tensor(buf8, (4, 4), (1, 4
            ), 0), out=buf9)
        buf10 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        triton_poi_fused_mean_tanh_0[grid(16)](buf3, buf10, 16, XBLOCK=16,
            num_warps=1, num_stages=1)
        buf11 = reinterpret_tensor(buf12, (1, 4), (16, 1), 12)
        extern_kernels.mm(primals_1, reinterpret_tensor(buf10, (4, 4), (1, 
            4), 0), out=buf11)
        buf13 = empty_strided_cuda((1,), (1,), torch.float32)
        buf14 = empty_strided_cuda((1,), (1,), torch.float32)
        triton_per_fused__softmax_1[grid(1)](buf12, buf13, buf14, 1, 16,
            XBLOCK=1, num_warps=2, num_stages=1)
        buf15 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        triton_poi_fused_add_mul_2[grid(64)](primals_2, buf12, buf13, buf14,
            buf15, 64, XBLOCK=64, num_warps=1, num_stages=1)
    return buf15, primals_1, reinterpret_tensor(primals_2, (4, 4, 4), (16, 
        4, 1), 0), reinterpret_tensor(primals_2, (4, 4, 4), (16, 4, 1), 64
        ), reinterpret_tensor(primals_2, (4, 4, 4), (16, 4, 1), 128
        ), reinterpret_tensor(primals_2, (4, 4, 4), (16, 4, 1), 192
        ), buf0, buf1, buf2, buf3, buf12, buf13, buf14, buf10, buf8, buf6, buf4


def init_drop(dropout):
    if dropout > 0:
        return nn.Dropout(dropout)
    else:
        return lambda x: x


class SelfAttentionNew(nn.Module):

    def __init__(self, hidden_dim, attn_drop, txt):
        """
        Description
        -----------
        This part is used to calculate type-level attention and semantic-level attention, and utilize them to generate :math:`z^{sc}` and :math:`z^{mp}`.

        .. math::
           w_{n}&=\\frac{1}{|V|}\\sum\\limits_{i\\in V} \\textbf{a}^\\top \\cdot \\tanh\\left(\\textbf{W}h_i^{n}+\\textbf{b}\\right) \\\\
           \\beta_{n}&=\\frac{\\exp\\left(w_{n}\\right)}{\\sum_{i=1}^M\\exp\\left(w_{i}\\right)} \\\\
           z &= \\sum_{n=1}^M \\beta_{n}\\cdot h^{n}

        Parameters
        ----------
        txt : str
            A str to identify view, MP or SC

        Returns
        -------
        z : matrix
            The fused embedding matrix

        """
        super(SelfAttentionNew, self).__init__()
        self.fc = nn.Linear(hidden_dim, hidden_dim, bias=True)
        nn.init.xavier_normal_(self.fc.weight, gain=1.414)
        self.tanh = nn.Tanh()
        self.att = nn.Parameter(torch.empty(size=(1, hidden_dim)),
            requires_grad=True)
        nn.init.xavier_normal_(self.att.data, gain=1.414)
        self.softmax = nn.Softmax(dim=0)
        self.attn_drop = init_drop(attn_drop)
        self.txt = txt

    def forward(self, input_0):
        primals_1 = self.att
        primals_3 = self.fc.weight
        primals_4 = self.fc.bias
        primals_2 = input_0
        output = call([primals_1, primals_2, primals_3, primals_4])
        return output[0]


Measured latency_ms: 0.147531