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 math as tl_math
from torch import 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_clone_0(in_ptr0, in_ptr1, 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 % 4
    x1 = xindex // 4 % 4
    x2 = xindex // 16 % 8
    x3 = xindex // 128
    x4 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 4 * x2 + 32 * x1 + 128 * x3), xmask)
    tmp1 = tl.load(in_ptr1 + (x0 + 4 * x2), xmask, eviction_policy='evict_last'
        )
    tmp2 = tmp0 + tmp1
    tl.store(out_ptr0 + x4, tmp2, xmask)


@triton.jit
def triton_poi_fused_clone_1(in_ptr0, in_ptr1, out_ptr0, ynumel, xnumel,
    YBLOCK: tl.constexpr, XBLOCK: tl.constexpr):
    ynumel = 128
    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 % 32
    y1 = yindex // 32
    y3 = yindex
    tmp0 = tl.load(in_ptr0 + (y0 + 32 * x2 + 128 * y1), xmask & ymask,
        eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr1 + y0, ymask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tl.store(out_ptr0 + (x2 + 4 * y3), tmp2, xmask & ymask)


@triton.jit
def triton_poi_fused__softmax_2(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
    ):
    xnumel = 512
    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)
    tmp3 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp5 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp11 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp1 = 1.0
    tmp2 = tmp0 * tmp1
    tmp4 = tmp3 * tmp1
    tmp6 = tmp5 * tmp1
    tmp7 = triton_helpers.maximum(tmp4, tmp6)
    tmp9 = tmp8 * tmp1
    tmp10 = triton_helpers.maximum(tmp7, tmp9)
    tmp12 = tmp11 * tmp1
    tmp13 = triton_helpers.maximum(tmp10, tmp12)
    tmp14 = tmp2 - tmp13
    tmp15 = 0.5
    tmp16 = tmp14 * tmp15
    tmp17 = tl_math.exp(tmp16)
    tl.store(out_ptr0 + x2, tmp17, xmask)


@triton.jit
def triton_poi_fused__softmax_3(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
    ):
    xnumel = 512
    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_clone_4(in_ptr0, 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 % 4
    x1 = xindex // 4 % 8
    x2 = xindex // 32 % 4
    x3 = xindex // 128
    x4 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 4 * x2 + 16 * x1 + 128 * x3), xmask)
    tl.store(out_ptr0 + x4, tmp0, 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), (16, 4, 1))
    assert_size_stride(primals_4, (32, 4), (4, 1))
    assert_size_stride(primals_5, (32,), (1,))
    assert_size_stride(primals_6, (32, 4), (4, 1))
    assert_size_stride(primals_7, (32,), (1,))
    assert_size_stride(primals_8, (32, 4), (4, 1))
    assert_size_stride(primals_9, (32,), (1,))
    assert_size_stride(primals_10, (4, 32), (32, 1))
    assert_size_stride(primals_11, (4,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((16, 32), (32, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(primals_1, (16, 4), (4, 1), 0),
            reinterpret_tensor(primals_4, (4, 32), (1, 4), 0), out=buf0)
        del primals_4
        buf1 = empty_strided_cuda((16, 32), (32, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(primals_2, (16, 4), (4, 1), 0),
            reinterpret_tensor(primals_6, (4, 32), (1, 4), 0), out=buf1)
        del primals_6
        buf2 = empty_strided_cuda((16, 32), (32, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(primals_3, (16, 4), (4, 1), 0),
            reinterpret_tensor(primals_8, (4, 32), (1, 4), 0), out=buf2)
        del primals_8
        buf3 = empty_strided_cuda((4, 8, 4, 4), (128, 16, 4, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_clone_0[grid(512)](buf0, primals_5, buf3, 512,
            XBLOCK=128, num_warps=4, num_stages=1)
        del primals_5
        buf4 = reinterpret_tensor(buf0, (4, 8, 4, 4), (128, 16, 4, 1), 0)
        del buf0
        triton_poi_fused_clone_1[grid(128, 4)](buf1, primals_7, buf4, 128, 
            4, XBLOCK=4, YBLOCK=32, num_warps=4, num_stages=1)
        del primals_7
        buf5 = reinterpret_tensor(buf1, (32, 4, 4), (16, 4, 1), 0)
        del buf1
        extern_kernels.bmm(reinterpret_tensor(buf3, (32, 4, 4), (16, 4, 1),
            0), reinterpret_tensor(buf4, (32, 4, 4), (16, 4, 1), 0), out=buf5)
        buf6 = empty_strided_cuda((4, 8, 4, 4), (128, 16, 4, 1), torch.float32)
        triton_poi_fused__softmax_2[grid(512)](buf5, buf6, 512, XBLOCK=128,
            num_warps=4, num_stages=1)
        buf7 = reinterpret_tensor(buf5, (4, 8, 4, 4), (128, 16, 4, 1), 0)
        del buf5
        triton_poi_fused__softmax_3[grid(512)](buf6, buf7, 512, XBLOCK=256,
            num_warps=4, num_stages=1)
        buf8 = buf6
        del buf6
        triton_poi_fused_clone_0[grid(512)](buf2, primals_9, buf8, 512,
            XBLOCK=128, num_warps=4, num_stages=1)
        del primals_9
        buf9 = reinterpret_tensor(buf2, (32, 4, 4), (16, 4, 1), 0)
        del buf2
        extern_kernels.bmm(reinterpret_tensor(buf7, (32, 4, 4), (16, 4, 1),
            0), reinterpret_tensor(buf8, (32, 4, 4), (16, 4, 1), 0), out=buf9)
        buf10 = empty_strided_cuda((4, 4, 8, 4), (128, 32, 4, 1), torch.float32
            )
        triton_poi_fused_clone_4[grid(512)](buf9, buf10, 512, XBLOCK=256,
            num_warps=4, num_stages=1)
        del buf9
        buf11 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
        extern_kernels.addmm(primals_11, reinterpret_tensor(buf10, (16, 32),
            (32, 1), 0), reinterpret_tensor(primals_10, (32, 4), (1, 32), 0
            ), alpha=1, beta=1, out=buf11)
        del primals_11
    return reinterpret_tensor(buf11, (4, 4, 4), (16, 4, 1), 0
        ), buf7, reinterpret_tensor(primals_1, (16, 4), (4, 1), 0
        ), reinterpret_tensor(primals_2, (16, 4), (4, 1), 0
        ), reinterpret_tensor(primals_3, (16, 4), (4, 1), 0
        ), buf7, reinterpret_tensor(buf10, (16, 32), (32, 1), 0
        ), primals_10, reinterpret_tensor(buf8, (32, 4, 4), (16, 1, 4), 0
        ), reinterpret_tensor(buf3, (32, 4, 4), (16, 1, 4), 0
        ), reinterpret_tensor(buf4, (32, 4, 4), (16, 1, 4), 0)


class ScaledDotProductAttention(nn.Module):

    def __init__(self, temperature, dropout=0.1):
        super(ScaledDotProductAttention, self).__init__()
        self.temperature = temperature
        self.dropout = nn.Dropout(p=dropout)

    def forward(self, q, k, v, mask=None):
        attn = torch.matmul(q, k.transpose(2, 3)) / self.temperature
        if mask is not None:
            attn = attn.masked_fill(mask=mask, value=float('-inf'))
        attn = torch.softmax(attn, dim=-1)
        attn = self.dropout(attn)
        out = torch.matmul(attn, v)
        return out, attn


class MultiHeadAttentionNew(nn.Module):

    def __init__(self, in_channels, k_channels, v_channels, n_head=8,
        dropout=0.1):
        super(MultiHeadAttentionNew, self).__init__()
        self.in_channels = in_channels
        self.k_channels = k_channels
        self.v_channels = v_channels
        self.n_head = n_head
        self.q_linear = nn.Linear(in_channels, n_head * k_channels)
        self.k_linear = nn.Linear(in_channels, n_head * k_channels)
        self.v_linear = nn.Linear(in_channels, n_head * v_channels)
        self.attention = ScaledDotProductAttention(temperature=k_channels **
            0.5, dropout=dropout)
        self.out_linear = nn.Linear(n_head * v_channels, in_channels)
        self.dropout = nn.Dropout(p=dropout)

    def forward(self, input_0, input_1, input_2):
        primals_4 = self.q_linear.weight
        primals_5 = self.q_linear.bias
        primals_6 = self.k_linear.weight
        primals_7 = self.k_linear.bias
        primals_8 = self.v_linear.weight
        primals_9 = self.v_linear.bias
        primals_10 = self.out_linear.weight
        primals_11 = self.out_linear.bias
        primals_1 = input_0
        primals_2 = input_1
        primals_3 = input_2
        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.04005