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 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__softmax_add_div_mul_rsub_0(in_ptr0, in_ptr1, in_ptr2,
    out_ptr0, out_ptr1, 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 + x0, xmask, eviction_policy='evict_last')
    tmp5 = tl.load(in_ptr2 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp10 = tl.load(in_ptr0 + (4 + x0 + 16 * x1), xmask)
    tmp13 = tl.load(in_ptr2 + (1 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp18 = tl.load(in_ptr0 + (8 + x0 + 16 * x1), xmask)
    tmp21 = tl.load(in_ptr2 + (2 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp26 = tl.load(in_ptr0 + (12 + x0 + 16 * x1), xmask)
    tmp29 = tl.load(in_ptr2 + (3 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp2 = tmp0 + tmp1
    tmp3 = 1.0
    tmp4 = tmp2 * tmp3
    tmp6 = tmp3 - tmp5
    tmp7 = -10000.0
    tmp8 = tmp6 * tmp7
    tmp9 = tmp4 + tmp8
    tmp11 = tmp10 + tmp1
    tmp12 = tmp11 * tmp3
    tmp14 = tmp3 - tmp13
    tmp15 = tmp14 * tmp7
    tmp16 = tmp12 + tmp15
    tmp17 = triton_helpers.maximum(tmp9, tmp16)
    tmp19 = tmp18 + tmp1
    tmp20 = tmp19 * tmp3
    tmp22 = tmp3 - tmp21
    tmp23 = tmp22 * tmp7
    tmp24 = tmp20 + tmp23
    tmp25 = triton_helpers.maximum(tmp17, tmp24)
    tmp27 = tmp26 + tmp1
    tmp28 = tmp27 * tmp3
    tmp30 = tmp3 - tmp29
    tmp31 = tmp30 * tmp7
    tmp32 = tmp28 + tmp31
    tmp33 = triton_helpers.maximum(tmp25, tmp32)
    tmp34 = tmp9 - tmp33
    tmp35 = tl_math.exp(tmp34)
    tmp36 = tmp16 - tmp33
    tmp37 = tl_math.exp(tmp36)
    tmp38 = tmp35 + tmp37
    tmp39 = tmp24 - tmp33
    tmp40 = tl_math.exp(tmp39)
    tmp41 = tmp38 + tmp40
    tmp42 = tmp32 - tmp33
    tmp43 = tl_math.exp(tmp42)
    tmp44 = tmp41 + tmp43
    tl.store(out_ptr0 + x2, tmp33, xmask)
    tl.store(out_ptr1 + x2, tmp44, xmask)


@triton.jit
def triton_poi_fused__softmax_add_div_mul_rsub_1(in_ptr0, in_ptr1, in_ptr2,
    in_ptr3, in_ptr4, 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')
    tmp1 = tl.load(in_ptr1 + y0, ymask, eviction_policy='evict_last')
    tmp5 = tl.load(in_ptr2 + (x2 + 4 * y1), xmask & ymask, eviction_policy=
        'evict_last')
    tmp10 = tl.load(in_ptr3 + y3, ymask, eviction_policy='evict_last')
    tmp13 = tl.load(in_ptr4 + y3, ymask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp3 = 1.0
    tmp4 = tmp2 * tmp3
    tmp6 = tmp3 - tmp5
    tmp7 = -10000.0
    tmp8 = tmp6 * tmp7
    tmp9 = tmp4 + tmp8
    tmp11 = tmp9 - tmp10
    tmp12 = tl_math.exp(tmp11)
    tmp14 = tmp12 / tmp13
    tl.store(out_ptr0 + (x2 + 4 * y3), tmp14, xmask & ymask)


@triton.jit
def triton_poi_fused_clone_2(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_mul_repeat_3(in_ptr0, in_ptr1, out_ptr0, out_ptr1,
    xnumel, XBLOCK: tl.constexpr):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 4
    x2 = xindex // 16
    x3 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 4 * x2), xmask, eviction_policy='evict_last'
        )
    tmp1 = tl.load(in_ptr1 + x3, xmask)
    tmp2 = tmp1 * tmp0
    tl.store(out_ptr0 + x3, tmp0, xmask)
    tl.store(out_ptr1 + x3, tmp2, xmask)


@triton.jit
def triton_poi_fused_mul_4(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
    x0 = xindex % 4
    x2 = xindex // 16
    x3 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 4 * x2), xmask, eviction_policy='evict_last'
        )
    tmp1 = tl.load(in_ptr1 + x3, xmask)
    tmp2 = tmp0 * tmp1
    tl.store(out_ptr0 + x3, tmp2, xmask)


@triton.jit
def triton_poi_fused_add_5(in_out_ptr0, in_ptr0, in_ptr1, 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_out_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr1 + x2, xmask)
    tmp2 = tmp0 + tmp1
    tmp4 = tmp2 + tmp3
    tl.store(in_out_ptr0 + x2, tmp4, 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, primals_12
        ) = args
    args.clear()
    assert_size_stride(primals_1, (4, 4), (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,), (1,))
    assert_size_stride(primals_5, (4, 4), (4, 1))
    assert_size_stride(primals_6, (4,), (1,))
    assert_size_stride(primals_7, (4, 4), (4, 1))
    assert_size_stride(primals_8, (4,), (1,))
    assert_size_stride(primals_9, (4, 4), (4, 1))
    assert_size_stride(primals_10, (4,), (1,))
    assert_size_stride(primals_11, (4, 4), (4, 1))
    assert_size_stride(primals_12, (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)
        del primals_3
        del primals_4
        buf1 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
        extern_kernels.addmm(primals_6, reinterpret_tensor(primals_2, (16, 
            4), (4, 1), 0), reinterpret_tensor(primals_5, (4, 4), (1, 4), 0
            ), alpha=1, beta=1, out=buf1)
        del primals_5
        del primals_6
        buf2 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
        extern_kernels.mm(buf0, reinterpret_tensor(primals_7, (4, 4), (1, 4
            ), 0), out=buf2)
        buf3 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32)
        buf4 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32)
        get_raw_stream(0)
        triton_poi_fused__softmax_add_div_mul_rsub_0[grid(16)](buf2,
            primals_8, primals_1, buf3, buf4, 16, XBLOCK=16, num_warps=1,
            num_stages=1)
        buf5 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        triton_poi_fused__softmax_add_div_mul_rsub_1[grid(16, 4)](buf2,
            primals_8, primals_1, buf3, buf4, buf5, 16, 4, XBLOCK=4, YBLOCK
            =16, num_warps=1, num_stages=1)
        del primals_8
        buf6 = reinterpret_tensor(buf2, (4, 4, 4, 1), (16, 4, 1, 1), 0)
        del buf2
        triton_poi_fused_clone_2[grid(16, 4)](buf0, buf6, 16, 4, XBLOCK=4,
            YBLOCK=16, num_warps=1, num_stages=1)
        buf7 = reinterpret_tensor(buf4, (16, 1, 1), (1, 1, 1), 0)
        del buf4
        extern_kernels.bmm(reinterpret_tensor(buf5, (16, 1, 4), (4, 4, 1), 
            0), reinterpret_tensor(buf6, (16, 4, 1), (4, 1, 0), 0), out=buf7)
        buf8 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        buf9 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        triton_poi_fused_mul_repeat_3[grid(64)](buf7, buf1, buf8, buf9, 64,
            XBLOCK=64, num_warps=1, num_stages=1)
        buf10 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(buf9, (16, 4), (4, 1), 0),
            reinterpret_tensor(primals_9, (4, 4), (1, 4), 0), out=buf10)
        buf11 = reinterpret_tensor(buf7, (4, 4, 1), (4, 1, 16), 0)
        del buf7
        buf12 = buf3
        del buf3
        triton_poi_fused__softmax_add_div_mul_rsub_0[grid(16)](buf10,
            primals_10, primals_1, buf11, buf12, 16, XBLOCK=16, num_warps=1,
            num_stages=1)
        buf13 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        triton_poi_fused__softmax_add_div_mul_rsub_1[grid(16, 4)](buf10,
            primals_10, primals_1, buf11, buf12, buf13, 16, 4, XBLOCK=4,
            YBLOCK=16, num_warps=1, num_stages=1)
        del buf11
        del primals_1
        del primals_10
        buf14 = reinterpret_tensor(buf10, (4, 4, 4, 1), (16, 4, 1, 1), 0)
        del buf10
        triton_poi_fused_clone_2[grid(16, 4)](buf9, buf14, 16, 4, XBLOCK=4,
            YBLOCK=16, num_warps=1, num_stages=1)
        buf15 = reinterpret_tensor(buf12, (16, 1, 1), (1, 1, 1), 0)
        del buf12
        extern_kernels.bmm(reinterpret_tensor(buf13, (16, 1, 4), (4, 4, 1),
            0), reinterpret_tensor(buf14, (16, 4, 1), (4, 1, 0), 0), out=buf15)
        buf16 = empty_strided_cuda((4, 4, 4, 1), (16, 1, 4, 16), torch.float32)
        triton_poi_fused_mul_4[grid(64)](buf15, buf0, buf16, 64, XBLOCK=64,
            num_warps=1, num_stages=1)
        buf17 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(buf16, (16, 4), (4, 1), 0),
            reinterpret_tensor(primals_11, (4, 4), (1, 4), 0), out=buf17)
        buf18 = reinterpret_tensor(buf17, (4, 4, 4), (16, 4, 1), 0)
        del buf17
        triton_poi_fused_add_5[grid(64)](buf18, primals_12, buf0, 64,
            XBLOCK=64, num_warps=1, num_stages=1)
        del primals_12
    return buf18, reinterpret_tensor(primals_2, (16, 4), (4, 1), 0
        ), buf0, buf1, buf5, buf8, reinterpret_tensor(buf9, (16, 4), (4, 1), 0
        ), buf13, buf15, reinterpret_tensor(buf16, (16, 4), (4, 1), 0
        ), primals_11, reinterpret_tensor(buf14, (16, 1, 4), (4, 1, 1), 0
        ), primals_9, reinterpret_tensor(buf6, (16, 1, 4), (4, 1, 1), 0
        ), primals_7


class FastAttentionNew(nn.Module):
    """ wuch15's Fastformer Attention module (Official) """

    def __init__(self, dim, dim_head, heads, dropout=0.1, initializer_range
        =0.02):
        super(FastAttentionNew, self).__init__()
        self.initializer_range = initializer_range
        if dim % dim_head != 0:
            raise ValueError(
                'The hidden size (%d) is not a multiple of the number of attention heads (%d)'
                 % (dim, dim_head))
        self.attention_head_size = int(dim / dim_head)
        self.num_attention_heads = dim_head
        self.all_head_size = (self.num_attention_heads * self.
            attention_head_size)
        self.input_dim = dim
        self.query = nn.Linear(self.input_dim, self.all_head_size)
        self.to_q_attn_logits = nn.Linear(self.all_head_size, self.
            num_attention_heads)
        self.key = nn.Linear(self.input_dim, self.all_head_size)
        self.to_k_attn_logits = nn.Linear(self.all_head_size, self.
            num_attention_heads)
        self.transform = nn.Linear(self.all_head_size, self.all_head_size)
        self.softmax = nn.Softmax(dim=-1)
        self.apply(self.init_weights)
        self.dropout = nn.Dropout(dropout)

    def init_weights(self, module):
        if isinstance(module, nn.Linear):
            module.weight.data.normal_(mean=0.0, std=self.initializer_range)
        if isinstance(module, nn.Linear) and module.bias is not None:
            module.bias.data.zero_()

    def transpose_for_scores(self, x):
        new_x_shape = x.size()[:-1] + (self.num_attention_heads, self.
            attention_head_size)
        x = x.view(*new_x_shape)
        return x.permute(0, 2, 1, 3)

    def forward(self, input_0, input_1):
        primals_1 = self.query.weight
        primals_4 = self.query.bias
        primals_3 = self.to_q_attn_logits.weight
        primals_6 = self.to_q_attn_logits.bias
        primals_5 = self.key.weight
        primals_8 = self.key.bias
        primals_7 = self.to_k_attn_logits.weight
        primals_10 = self.to_k_attn_logits.bias
        primals_9 = self.transform.weight
        primals_12 = self.transform.bias
        primals_2 = input_0
        primals_11 = 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, primals_12])
        return output[0]


Measured latency_ms: 0.129698