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
import torch.nn.functional as F
import torch.nn.init as init
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, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    tl.full([XBLOCK], True, tl.int1)
    x0 = xindex % 16
    x1 = xindex // 16 % 4
    x2 = xindex // 64 % 8
    x3 = xindex // 512
    x4 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 16 * x2 + 128 * x1 + 512 * x3), None)
    tl.store(out_ptr0 + x4, tmp0, None)


@triton.jit
def triton_poi_fused__softmax_1(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.24999993750001562
    tmp16 = tmp14 * tmp15
    tmp17 = tl_math.exp(tmp16)
    tl.store(out_ptr0 + x2, tmp17, xmask)


@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)
    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_view_3(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    tl.full([XBLOCK], True, tl.int1)
    x0 = xindex % 128
    x1 = xindex // 128
    x2 = xindex
    tmp0 = tl.load(in_ptr0 + (16 * (x1 % 4) + 64 * (x0 // 16) + 512 * (x1 //
        4) + x0 % 16), None)
    tl.store(out_ptr0 + x2, tmp0, None)


@triton.jit
def triton_poi_fused_add_4(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
    tl.store(in_out_ptr0 + x0, tmp2, xmask)


@triton.jit
def triton_poi_fused_relu_threshold_backward_5(in_out_ptr0, in_ptr0,
    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_out_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp3 = tl.full([1], 0, tl.int32)
    tmp4 = triton_helpers.maximum(tmp3, tmp2)
    tmp5 = 0.0
    tmp6 = tmp4 <= tmp5
    tl.store(in_out_ptr0 + x2, tmp4, xmask)
    tl.store(out_ptr0 + x2, tmp6, xmask)


@triton.jit
def triton_poi_fused_add_6(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) = 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, (4, 128), (128, 1))
    assert_size_stride(primals_5, (4, 128), (128, 1))
    assert_size_stride(primals_6, (4, 128), (128, 1))
    assert_size_stride(primals_7, (128, 4), (4, 1))
    assert_size_stride(primals_8, (4, 4), (4, 1))
    assert_size_stride(primals_9, (4,), (1,))
    assert_size_stride(primals_10, (4, 4), (4, 1))
    assert_size_stride(primals_11, (4,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((16, 128), (128, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(primals_1, (16, 4), (4, 1), 0),
            primals_4, out=buf0)
        del primals_4
        buf1 = empty_strided_cuda((16, 128), (128, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(primals_2, (16, 4), (4, 1), 0),
            primals_5, out=buf1)
        del primals_5
        buf2 = empty_strided_cuda((16, 128), (128, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(primals_3, (16, 4), (4, 1), 0),
            primals_6, out=buf2)
        del primals_6
        buf3 = empty_strided_cuda((4, 8, 4, 16), (512, 64, 16, 1), torch.
            float32)
        get_raw_stream(0)
        triton_poi_fused_clone_0[grid(2048)](buf0, buf3, 2048, XBLOCK=256,
            num_warps=4, num_stages=1)
        buf4 = reinterpret_tensor(buf0, (4, 8, 4, 16), (512, 64, 16, 1), 0)
        del buf0
        triton_poi_fused_clone_0[grid(2048)](buf1, buf4, 2048, XBLOCK=256,
            num_warps=4, num_stages=1)
        buf5 = empty_strided_cuda((32, 4, 4), (16, 4, 1), torch.float32)
        extern_kernels.bmm(reinterpret_tensor(buf3, (32, 4, 16), (64, 16, 1
            ), 0), reinterpret_tensor(buf4, (32, 16, 4), (64, 1, 16), 0),
            out=buf5)
        buf6 = empty_strided_cuda((32, 4, 4), (16, 4, 1), torch.float32)
        triton_poi_fused__softmax_1[grid(512)](buf5, buf6, 512, XBLOCK=128,
            num_warps=4, num_stages=1)
        buf7 = buf5
        del buf5
        triton_poi_fused__softmax_2[grid(512)](buf6, buf7, 512, XBLOCK=128,
            num_warps=4, num_stages=1)
        del buf6
        buf8 = reinterpret_tensor(buf1, (4, 8, 4, 16), (512, 64, 16, 1), 0)
        del buf1
        triton_poi_fused_clone_0[grid(2048)](buf2, buf8, 2048, XBLOCK=256,
            num_warps=4, num_stages=1)
        buf9 = reinterpret_tensor(buf2, (32, 4, 16), (64, 16, 1), 0)
        del buf2
        extern_kernels.bmm(buf7, reinterpret_tensor(buf8, (32, 4, 16), (64,
            16, 1), 0), out=buf9)
        buf10 = empty_strided_cuda((16, 128), (128, 1), torch.float32)
        triton_poi_fused_view_3[grid(2048)](buf9, buf10, 2048, XBLOCK=256,
            num_warps=4, num_stages=1)
        del buf9
        buf11 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
        extern_kernels.mm(buf10, primals_7, out=buf11)
        buf12 = reinterpret_tensor(buf11, (4, 4, 4), (16, 4, 1), 0)
        del buf11
        triton_poi_fused_add_4[grid(64)](buf12, primals_1, 64, XBLOCK=64,
            num_warps=1, num_stages=1)
        buf13 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(buf12, (16, 4), (4, 1), 0),
            reinterpret_tensor(primals_8, (4, 4), (1, 4), 0), out=buf13)
        buf14 = reinterpret_tensor(buf13, (4, 4, 4), (16, 4, 1), 0)
        del buf13
        buf17 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.bool)
        triton_poi_fused_relu_threshold_backward_5[grid(64)](buf14,
            primals_9, buf17, 64, XBLOCK=64, num_warps=1, num_stages=1)
        del primals_9
        buf15 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(buf14, (16, 4), (4, 1), 0),
            reinterpret_tensor(primals_10, (4, 4), (1, 4), 0), out=buf15)
        buf16 = reinterpret_tensor(buf15, (4, 4, 4), (16, 4, 1), 0)
        del buf15
        triton_poi_fused_add_6[grid(64)](buf16, primals_11, buf12, 64,
            XBLOCK=64, num_warps=1, num_stages=1)
        del primals_11
    return buf16, buf7, reinterpret_tensor(buf12, (16, 4), (4, 1), 0
        ), reinterpret_tensor(buf14, (16, 4), (4, 1), 0
        ), primals_10, buf17, primals_8, reinterpret_tensor(buf10, (128, 16
        ), (1, 128), 0), reinterpret_tensor(primals_7, (4, 128), (1, 4), 0
        ), reinterpret_tensor(buf8, (32, 16, 4), (64, 1, 16), 0
        ), reinterpret_tensor(buf3, (32, 16, 4), (64, 1, 16), 0
        ), reinterpret_tensor(buf4, (32, 4, 16), (64, 16, 1), 0
        ), reinterpret_tensor(primals_3, (4, 16), (1, 4), 0
        ), reinterpret_tensor(primals_2, (4, 16), (1, 4), 0
        ), reinterpret_tensor(primals_1, (4, 16), (1, 4), 0)


class TransformerBlockNew(nn.Module):

    def __init__(self, input_size, d_k=16, d_v=16, n_heads=8, is_layer_norm
        =False, attn_dropout=0.1):
        super(TransformerBlockNew, self).__init__()
        self.n_heads = n_heads
        self.d_k = d_k if d_k is not None else input_size
        self.d_v = d_v if d_v is not None else input_size
        self.is_layer_norm = is_layer_norm
        if is_layer_norm:
            self.layer_morm = nn.LayerNorm(normalized_shape=input_size)
        self.W_q = nn.Parameter(torch.Tensor(input_size, n_heads * d_k))
        self.W_k = nn.Parameter(torch.Tensor(input_size, n_heads * d_k))
        self.W_v = nn.Parameter(torch.Tensor(input_size, n_heads * d_v))
        self.W_o = nn.Parameter(torch.Tensor(d_v * n_heads, input_size))
        self.linear1 = nn.Linear(input_size, input_size)
        self.linear2 = nn.Linear(input_size, input_size)
        self.dropout = nn.Dropout(attn_dropout)
        self.__init_weights__()
        None

    def __init_weights__(self):
        init.xavier_normal_(self.W_q)
        init.xavier_normal_(self.W_k)
        init.xavier_normal_(self.W_v)
        init.xavier_normal_(self.W_o)
        init.xavier_normal_(self.linear1.weight)
        init.xavier_normal_(self.linear2.weight)

    def FFN(self, X):
        output = self.linear2(F.relu(self.linear1(X)))
        output = self.dropout(output)
        return output

    def scaled_dot_product_attention(self, Q, K, V, episilon=1e-06):
        """
        :param Q: (*, max_q_words, n_heads, input_size)
        :param K: (*, max_k_words, n_heads, input_size)
        :param V: (*, max_v_words, n_heads, input_size)
        :param episilon:
        :return:
        """
        temperature = self.d_k ** 0.5
        Q_K = torch.einsum('bqd,bkd->bqk', Q, K) / (temperature + episilon)
        Q_K_score = F.softmax(Q_K, dim=-1)
        Q_K_score = self.dropout(Q_K_score)
        V_att = Q_K_score.bmm(V)
        return V_att

    def multi_head_attention(self, Q, K, V):
        bsz, q_len, _ = Q.size()
        bsz, k_len, _ = K.size()
        bsz, v_len, _ = V.size()
        Q_ = Q.matmul(self.W_q).view(bsz, q_len, self.n_heads, self.d_k)
        K_ = K.matmul(self.W_k).view(bsz, k_len, self.n_heads, self.d_k)
        V_ = V.matmul(self.W_v).view(bsz, v_len, self.n_heads, self.d_v)
        Q_ = Q_.permute(0, 2, 1, 3).contiguous().view(bsz * self.n_heads,
            q_len, self.d_k)
        K_ = K_.permute(0, 2, 1, 3).contiguous().view(bsz * self.n_heads,
            q_len, self.d_k)
        V_ = V_.permute(0, 2, 1, 3).contiguous().view(bsz * self.n_heads,
            q_len, self.d_v)
        V_att = self.scaled_dot_product_attention(Q_, K_, V_)
        V_att = V_att.view(bsz, self.n_heads, q_len, self.d_v)
        V_att = V_att.permute(0, 2, 1, 3).contiguous().view(bsz, q_len, 
            self.n_heads * self.d_v)
        output = self.dropout(V_att.matmul(self.W_o))
        return output

    def forward(self, input_0, input_1, input_2):
        primals_4 = self.W_q
        primals_5 = self.W_k
        primals_6 = self.W_v
        primals_7 = self.W_o
        primals_8 = self.linear1.weight
        primals_9 = self.linear1.bias
        primals_10 = self.linear2.weight
        primals_11 = self.linear2.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]


Measured latency_ms: 0.177819