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_tanh_0(in_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_out_ptr0 + x0, xmask)
    tmp1 = libdevice.tanh(tmp0)
    tl.store(in_out_ptr0 + x0, tmp1, xmask)


@triton.jit
def triton_poi_fused_clone_1(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl.
    constexpr, XBLOCK: tl.constexpr):
    ynumel = 16
    xnumel = 4
    yoffset = tl.program_id(1) * YBLOCK
    yindex = yoffset + tl.arange(0, YBLOCK)[None, :]
    ymask = yindex < ynumel
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    x2 = xindex
    y0 = yindex % 4
    y1 = yindex // 4
    y3 = yindex
    tmp0 = tl.load(in_ptr0 + (y0 + 4 * x2 + 16 * y1), xmask & ymask,
        eviction_policy='evict_last')
    tl.store(out_ptr0 + (x2 + 4 * y3), tmp0, xmask & ymask)


@triton.jit
def triton_poi_fused_add_tanh_2(in_out_ptr0, in_ptr0, in_ptr1, out_ptr0,
    xnumel, XBLOCK: tl.constexpr):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex
    x0 = xindex % 4
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_out_ptr0 + x2, xmask)
    tmp2 = tmp0 + tmp1
    tmp4 = tmp2 + tmp3
    tmp5 = libdevice.tanh(tmp4)
    tl.store(in_out_ptr0 + x2, tmp5, xmask)
    tl.store(out_ptr0 + x2, tmp2, xmask)


@triton.jit
def triton_poi_fused_add_tanh_3(in_out_ptr0, in_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_out_ptr0 + x0, xmask)
    tmp2 = tmp0 + tmp1
    tmp3 = libdevice.tanh(tmp2)
    tl.store(in_out_ptr0 + x0, tmp3, xmask)


@triton.jit
def triton_poi_fused__softmax_4(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
    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 = 16
    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)


@triton.jit
def triton_poi_fused_mul_sum_6(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
    x2 = xindex
    x1 = xindex // 4
    tmp0 = tl.load(in_ptr0 + 4 * x2, xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr1 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr0 + (1 + 4 * x2), xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr1 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp7 = tl.load(in_ptr0 + (2 + 4 * x2), xmask, eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr1 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp11 = tl.load(in_ptr0 + (3 + 4 * x2), xmask, eviction_policy='evict_last'
        )
    tmp12 = tl.load(in_ptr1 + (3 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp2 = tmp0 * tmp1
    tmp5 = tmp3 * tmp4
    tmp6 = tmp2 + tmp5
    tmp9 = tmp7 * tmp8
    tmp10 = tmp6 + tmp9
    tmp13 = tmp11 * tmp12
    tmp14 = tmp10 + tmp13
    tl.store(out_ptr0 + x2, tmp14, xmask)


@triton.jit
def triton_poi_fused_mul_sum_7(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
    x0 = xindex % 4
    x1 = xindex // 4
    x2 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 16 * x1), xmask)
    tmp1 = tl.load(in_ptr1 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr0 + (4 + x0 + 16 * x1), xmask)
    tmp4 = tl.load(in_ptr1 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp7 = tl.load(in_ptr0 + (8 + x0 + 16 * x1), xmask)
    tmp8 = tl.load(in_ptr1 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp11 = tl.load(in_ptr0 + (12 + x0 + 16 * x1), xmask)
    tmp12 = tl.load(in_ptr1 + (3 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp2 = tmp0 * tmp1
    tmp5 = tmp3 * tmp4
    tmp6 = tmp2 + tmp5
    tmp9 = tmp7 * tmp8
    tmp10 = tmp6 + tmp9
    tmp13 = tmp11 * tmp12
    tmp14 = tmp10 + tmp13
    tl.store(out_ptr0 + x2, tmp14, 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) = args
    args.clear()
    assert_size_stride(primals_1, (4, 4, 4), (16, 4, 1))
    assert_size_stride(primals_2, (4, 4, 4), (16, 4, 1))
    assert_size_stride(primals_3, (4, 4), (4, 1))
    assert_size_stride(primals_4, (4, 4), (4, 1))
    assert_size_stride(primals_5, (4,), (1,))
    assert_size_stride(primals_6, (4, 4), (4, 1))
    assert_size_stride(primals_7, (4,), (1,))
    assert_size_stride(primals_8, (1, 4), (4, 1))
    assert_size_stride(primals_9, (1,), (1,))
    assert_size_stride(primals_10, (1, 4), (4, 1))
    assert_size_stride(primals_11, (1,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(primals_1, (16, 4), (4, 1), 0),
            reinterpret_tensor(primals_3, (4, 4), (1, 4), 0), out=buf0)
        del primals_3
        buf1 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        extern_kernels.bmm(reinterpret_tensor(buf0, (4, 4, 4), (16, 4, 1), 
            0), primals_2, out=buf1)
        buf2 = buf1
        del buf1
        get_raw_stream(0)
        triton_poi_fused_tanh_0[grid(64)](buf2, 64, XBLOCK=64, num_warps=1,
            num_stages=1)
        buf3 = reinterpret_tensor(buf0, (4, 4, 4), (16, 4, 1), 0)
        del buf0
        triton_poi_fused_clone_1[grid(16, 4)](primals_2, buf3, 16, 4,
            XBLOCK=4, YBLOCK=16, num_warps=1, num_stages=1)
        buf4 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(buf3, (16, 4), (4, 1), 0),
            reinterpret_tensor(primals_4, (4, 4), (1, 4), 0), out=buf4)
        del primals_4
        buf5 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
        extern_kernels.addmm(primals_7, reinterpret_tensor(primals_1, (16, 
            4), (4, 1), 0), reinterpret_tensor(primals_6, (4, 4), (1, 4), 0
            ), alpha=1, beta=1, out=buf5)
        del primals_6
        del primals_7
        buf6 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        extern_kernels.bmm(reinterpret_tensor(buf2, (4, 4, 4), (16, 1, 4), 
            0), reinterpret_tensor(buf5, (4, 4, 4), (16, 4, 1), 0), out=buf6)
        buf7 = buf6
        del buf6
        buf8 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        triton_poi_fused_add_tanh_2[grid(64)](buf7, buf4, primals_5, buf8, 
            64, XBLOCK=64, num_warps=1, num_stages=1)
        del primals_5
        buf9 = reinterpret_tensor(buf4, (4, 4, 4), (16, 4, 1), 0)
        del buf4
        extern_kernels.bmm(buf2, buf8, out=buf9)
        buf10 = buf9
        del buf9
        triton_poi_fused_add_tanh_3[grid(64)](buf10, buf5, 64, XBLOCK=64,
            num_warps=1, num_stages=1)
        buf12 = empty_strided_cuda((16, 1), (1, 1), torch.float32)
        extern_kernels.addmm(primals_9, reinterpret_tensor(buf7, (16, 4), (
            4, 1), 0), reinterpret_tensor(primals_8, (4, 1), (1, 4), 0),
            alpha=1, beta=1, out=buf12)
        del primals_9
        buf14 = empty_strided_cuda((16, 1), (1, 1), torch.float32)
        extern_kernels.addmm(primals_11, reinterpret_tensor(buf10, (16, 4),
            (4, 1), 0), reinterpret_tensor(primals_10, (4, 1), (1, 4), 0),
            alpha=1, beta=1, out=buf14)
        del primals_11
        buf15 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        triton_poi_fused__softmax_4[grid(16)](buf12, buf15, 16, XBLOCK=16,
            num_warps=1, num_stages=1)
        buf16 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        triton_poi_fused__softmax_4[grid(16)](buf14, buf16, 16, XBLOCK=16,
            num_warps=1, num_stages=1)
        buf17 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        triton_poi_fused__softmax_5[grid(16)](buf15, buf17, 16, XBLOCK=16,
            num_warps=1, num_stages=1)
        buf18 = buf15
        del buf15
        triton_poi_fused_mul_sum_6[grid(16)](primals_2, buf17, buf18, 16,
            XBLOCK=16, num_warps=1, num_stages=1)
        buf19 = buf17
        del buf17
        triton_poi_fused__softmax_5[grid(16)](buf16, buf19, 16, XBLOCK=16,
            num_warps=1, num_stages=1)
        buf20 = buf16
        del buf16
        triton_poi_fused_mul_sum_7[grid(16)](primals_1, buf19, buf20, 16,
            XBLOCK=16, num_warps=1, num_stages=1)
        del buf19
    return buf18, buf20, primals_1, primals_2, buf2, reinterpret_tensor(buf3,
        (16, 4), (4, 1), 0
        ), buf7, buf10, buf12, buf14, primals_10, primals_8, reinterpret_tensor(
        buf8, (4, 4, 4), (16, 1, 4), 0), reinterpret_tensor(buf5, (4, 4, 4),
        (16, 1, 4), 0)


class ParallelAttentionNew(nn.Module):

    def __init__(self, embedding_size, hidden_size):
        super().__init__()
        self.hidden_size = hidden_size
        self.embedding_size = embedding_size
        self.ques_linear = nn.Linear(self.embedding_size, self.hidden_size)
        self.img_linear = nn.Linear(self.embedding_size, self.hidden_size)
        self.ques_linear_t = nn.Linear(self.hidden_size, 1)
        self.img_linear_t = nn.Linear(self.hidden_size, 1)
        self.affinity = nn.Linear(self.embedding_size, self.hidden_size,
            bias=False)
        self.activation = nn.Tanh()
        self.softmax = nn.Softmax(dim=-1)

    def forward(self, input_0, input_1):
        primals_3 = self.ques_linear.weight
        primals_5 = self.ques_linear.bias
        primals_4 = self.img_linear.weight
        primals_7 = self.img_linear.bias
        primals_8 = self.ques_linear_t.weight
        primals_9 = self.ques_linear_t.bias
        primals_10 = self.img_linear_t.weight
        primals_11 = self.img_linear_t.bias
        primals_6 = self.affinity.weight
        primals_1 = input_0
        primals_2 = input_1
        output = call([primals_1, primals_2, primals_3, primals_4,
            primals_5, primals_6, primals_7, primals_8, primals_9,
            primals_10, primals_11])
        return output[0], output[1]


Measured latency_ms: 0.023142