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
import numpy as np
import torch.utils.data
import torch.nn as nn
import torch.nn.parallel
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_cat_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex // 16
    x0 = xindex % 4
    x1 = xindex // 4 % 4
    x3 = xindex
    tmp0 = x2
    tl.full([1], 0, tl.int64)
    tmp3 = tl.full([1], 4, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp5 = tl.load(in_ptr0 + (x0 + 16 * x1 + 64 * x2), tmp4 & xmask, other=0.0)
    tmp6 = tmp0 >= tmp3
    tmp7 = tl.full([1], 8, tl.int64)
    tmp8 = tmp0 < tmp7
    tmp9 = tmp6 & tmp8
    tmp10 = tl.load(in_ptr0 + (4 + x0 + 16 * x1 + 64 * (-4 + x2)), tmp9 &
        xmask, other=0.0)
    tmp11 = tmp0 >= tmp7
    tmp12 = tl.full([1], 12, tl.int64)
    tmp13 = tmp0 < tmp12
    tmp14 = tmp11 & tmp13
    tmp15 = tl.load(in_ptr0 + (8 + x0 + 16 * x1 + 64 * (-8 + x2)), tmp14 &
        xmask, other=0.0)
    tmp16 = tmp0 >= tmp12
    tl.full([1], 16, tl.int64)
    tmp19 = tl.load(in_ptr0 + (12 + x0 + 16 * x1 + 64 * (-12 + x2)), tmp16 &
        xmask, other=0.0)
    tmp20 = tl.where(tmp14, tmp15, tmp19)
    tmp21 = tl.where(tmp9, tmp10, tmp20)
    tmp22 = tl.where(tmp4, tmp5, tmp21)
    tl.store(out_ptr0 + x3, tmp22, xmask)


@triton.jit
def triton_poi_fused__softmax_sqrt_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.
    constexpr):
    xnumel = 256
    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)
    tmp8 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp10 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp13 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp16 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp1 = tl.full([1], 2.0, tl.float64)
    tmp2 = tl.full([1], 0.0, tl.float64)
    tmp3 = tmp1 >= tmp2
    tmp4 = 1.0
    tmp5 = -1.0
    tmp6 = tl.where(tmp3, tmp4, tmp5)
    tmp7 = tmp0 * tmp6
    tmp9 = tmp8 * tmp6
    tmp11 = tmp10 * tmp6
    tmp12 = triton_helpers.maximum(tmp9, tmp11)
    tmp14 = tmp13 * tmp6
    tmp15 = triton_helpers.maximum(tmp12, tmp14)
    tmp17 = tmp16 * tmp6
    tmp18 = triton_helpers.maximum(tmp15, tmp17)
    tmp19 = tmp7 - tmp18
    tmp20 = tmp6.to(tl.float64)
    tmp21 = tmp20 * tmp1
    tmp22 = tmp21.to(tl.float32)
    tmp23 = tmp19 / tmp22
    tmp24 = tl_math.exp(tmp23)
    tl.store(out_ptr0 + x2, tmp24, xmask)


@triton.jit
def triton_poi_fused__softmax_2(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
    ):
    xnumel = 256
    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_cat_3(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 16
    x1 = xindex // 16
    x2 = xindex
    tmp0 = x0
    tl.full([1], 0, tl.int64)
    tmp3 = tl.full([1], 4, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp5 = tl.load(in_ptr0 + (4 * x1 + x0), tmp4 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp6 = tmp0 >= tmp3
    tmp7 = tl.full([1], 8, tl.int64)
    tmp8 = tmp0 < tmp7
    tmp9 = tmp6 & tmp8
    tmp10 = tl.load(in_ptr0 + (64 + 4 * x1 + (-4 + x0)), tmp9 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp11 = tmp0 >= tmp7
    tmp12 = tl.full([1], 12, tl.int64)
    tmp13 = tmp0 < tmp12
    tmp14 = tmp11 & tmp13
    tmp15 = tl.load(in_ptr0 + (128 + 4 * x1 + (-8 + x0)), tmp14 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp16 = tmp0 >= tmp12
    tl.full([1], 16, tl.int64)
    tmp19 = tl.load(in_ptr0 + (192 + 4 * x1 + (-12 + x0)), tmp16 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp20 = tl.where(tmp14, tmp15, tmp19)
    tmp21 = tl.where(tmp9, tmp10, tmp20)
    tmp22 = tl.where(tmp4, tmp5, tmp21)
    tl.store(out_ptr0 + x2, tmp22, 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, (16, 4), (4, 1))
    assert_size_stride(primals_3, (16,), (1,))
    assert_size_stride(primals_4, (16, 4), (4, 1))
    assert_size_stride(primals_5, (16,), (1,))
    assert_size_stride(primals_6, (4, 4, 4), (16, 4, 1))
    assert_size_stride(primals_7, (16, 4), (4, 1))
    assert_size_stride(primals_8, (16,), (1,))
    assert_size_stride(primals_9, (4, 4, 4), (16, 4, 1))
    assert_size_stride(primals_10, (4, 16), (16, 1))
    assert_size_stride(primals_11, (4,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((16, 16), (16, 1), torch.float32)
        extern_kernels.addmm(primals_3, reinterpret_tensor(primals_1, (16, 
            4), (4, 1), 0), reinterpret_tensor(primals_2, (4, 16), (1, 4), 
            0), alpha=1, beta=1, out=buf0)
        del primals_2
        del primals_3
        buf1 = empty_strided_cuda((16, 4, 4), (16, 4, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_cat_0[grid(256)](buf0, buf1, 256, XBLOCK=256,
            num_warps=4, num_stages=1)
        buf2 = buf0
        del buf0
        extern_kernels.addmm(primals_5, reinterpret_tensor(primals_6, (16, 
            4), (4, 1), 0), reinterpret_tensor(primals_4, (4, 16), (1, 4), 
            0), alpha=1, beta=1, out=buf2)
        del primals_4
        del primals_5
        buf3 = empty_strided_cuda((16, 16), (16, 1), torch.float32)
        extern_kernels.addmm(primals_8, reinterpret_tensor(primals_9, (16, 
            4), (4, 1), 0), reinterpret_tensor(primals_7, (4, 16), (1, 4), 
            0), alpha=1, beta=1, out=buf3)
        del primals_7
        del primals_8
        buf4 = empty_strided_cuda((16, 4, 4), (16, 4, 1), torch.float32)
        triton_poi_fused_cat_0[grid(256)](buf3, buf4, 256, XBLOCK=256,
            num_warps=4, num_stages=1)
        buf5 = reinterpret_tensor(buf3, (16, 4, 4), (16, 4, 1), 0)
        del buf3
        triton_poi_fused_cat_0[grid(256)](buf2, buf5, 256, XBLOCK=256,
            num_warps=4, num_stages=1)
        buf6 = reinterpret_tensor(buf2, (16, 4, 4), (16, 4, 1), 0)
        del buf2
        extern_kernels.bmm(buf1, reinterpret_tensor(buf5, (16, 4, 4), (16, 
            1, 4), 0), out=buf6)
        buf7 = empty_strided_cuda((16, 4, 4), (16, 4, 1), torch.float32)
        triton_poi_fused__softmax_sqrt_1[grid(256)](buf6, buf7, 256, XBLOCK
            =256, num_warps=4, num_stages=1)
        buf8 = buf6
        del buf6
        triton_poi_fused__softmax_2[grid(256)](buf7, buf8, 256, XBLOCK=128,
            num_warps=4, num_stages=1)
        buf9 = buf7
        del buf7
        extern_kernels.bmm(buf8, buf4, out=buf9)
        buf10 = empty_strided_cuda((4, 4, 16), (64, 16, 1), torch.float32)
        triton_poi_fused_cat_3[grid(256)](buf9, buf10, 256, 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, 16),
            (16, 1), 0), reinterpret_tensor(primals_10, (16, 4), (1, 16), 0
            ), alpha=1, beta=1, out=buf11)
        del primals_11
    return reinterpret_tensor(buf11, (4, 4, 4), (16, 4, 1), 0
        ), buf8, reinterpret_tensor(primals_1, (16, 4), (4, 1), 0
        ), reinterpret_tensor(primals_6, (16, 4), (4, 1), 0
        ), reinterpret_tensor(primals_9, (16, 4), (4, 1), 0
        ), buf8, reinterpret_tensor(buf10, (16, 16), (16, 1), 0
        ), primals_10, reinterpret_tensor(buf4, (16, 4, 4), (16, 1, 4), 0
        ), reinterpret_tensor(buf1, (16, 4, 4), (16, 1, 4), 0), buf5


def generate_local_map_mask(chunk_size: 'int', attention_size: 'int',
    mask_future=False, device: 'torch.device'='cpu') ->torch.BoolTensor:
    """Compute attention mask as attention_size wide diagonal.

    Parameters
    ----------
    chunk_size:
        Time dimension size.
    attention_size:
        Number of backward elements to apply attention.
    device:
        torch device. Default is ``'cpu'``.

    Returns
    -------
        Mask as a boolean tensor.
    """
    local_map = np.empty((chunk_size, chunk_size))
    i, j = np.indices(local_map.shape)
    if mask_future:
        local_map[i, j] = (i - j > attention_size) ^ (j - i > 0)
    else:
        local_map[i, j] = np.abs(i - j) > attention_size
    return torch.BoolTensor(local_map)


class MultiHeadAttentionNew(nn.Module):
    """Multi Head Attention block from Attention is All You Need.

    Given 3 inputs of shape (batch_size, K, d_model), that will be used
    to compute query, keys and values, we output a self attention
    tensor of shape (batch_size, K, d_model).

    Parameters
    ----------
    d_model:
        Dimension of the input vector.
    q:
        Dimension of all query matrix.
    v:
        Dimension of all value matrix.
    h:
        Number of heads.
    attention_size:
        Number of backward elements to apply attention.
        Deactivated if ``None``. Default is ``None``.
    """

    def __init__(self, d_model: 'int', q: 'int', v: 'int', h: 'int',
        attention_size: 'int'=None):
        """Initialize the Multi Head Block."""
        super().__init__()
        self._h = h
        self._attention_size = attention_size
        self._W_q = nn.Linear(d_model, q * self._h)
        self._W_k = nn.Linear(d_model, q * self._h)
        self._W_v = nn.Linear(d_model, v * self._h)
        self._W_o = nn.Linear(self._h * v, d_model)
        self._scores = None

    @property
    def attention_map(self) ->torch.Tensor:
        """Attention map after a forward propagation,
        variable `score` in the original paper.
        """
        if self._scores is None:
            raise RuntimeError(
                'Evaluate the model once to generate attention map')
        return self._scores

    def forward(self, input_0, input_1, input_2):
        primals_2 = self._W_q.weight
        primals_3 = self._W_q.bias
        primals_4 = self._W_k.weight
        primals_5 = self._W_k.bias
        primals_7 = self._W_v.weight
        primals_8 = self._W_v.bias
        primals_10 = self._W_o.weight
        primals_11 = self._W_o.bias
        primals_1 = input_0
        primals_6 = input_1
        primals_9 = 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]


Measured latency_ms: 0.027536