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.triton_helpers import libdevice, math as tl_math
import math
import torch.nn.functional as F
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_0(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 16
    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 = 1.0
    tmp4 = tmp2 * tmp3
    tl.store(in_out_ptr0 + x2, tmp4, xmask)


@triton.jit
def triton_poi_fused_1(in_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
    tmp0 = tl.load(in_out_ptr0 + x0, xmask)
    tmp1 = float('-inf')
    tmp2 = tmp0 == tmp1
    tmp3 = tmp2 == 0
    tmp4 = tmp3.to(tl.int64)
    tmp5 = tmp4 != 0
    tmp6 = tmp5 == 0
    tmp7 = tmp0 - tmp0
    tmp8 = tl_math.exp(tmp7)
    tmp9 = tmp8 / tmp8
    tmp10 = 0.0
    tmp11 = tl.where(tmp6, tmp10, tmp9)
    tl.store(in_out_ptr0 + x0, tmp11, xmask)


@triton.jit
def triton_poi_fused_add_mean_std_2(in_out_ptr0, 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 + 4 * x0, xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr1 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr0 + (1 + 4 * x0), 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 * x0), 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 * x0), 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
    tmp15 = 4.0
    tmp16 = tmp14 / tmp15
    tmp17 = tmp2 - tmp16
    tmp18 = tmp17 * tmp17
    tmp19 = tmp5 - tmp16
    tmp20 = tmp19 * tmp19
    tmp21 = tmp18 + tmp20
    tmp22 = tmp9 - tmp16
    tmp23 = tmp22 * tmp22
    tmp24 = tmp21 + tmp23
    tmp25 = tmp13 - tmp16
    tmp26 = tmp25 * tmp25
    tmp27 = tmp24 + tmp26
    tmp28 = 3.0
    tmp29 = tmp27 / tmp28
    tl.store(in_out_ptr0 + x2, tmp29, xmask)
    tl.store(out_ptr0 + x2, tmp16, xmask)


@triton.jit
def triton_poi_fused_add_div_mean_mul_std_sub_3(in_ptr0, in_ptr1, in_ptr2,
    in_ptr3, in_ptr4, in_ptr5, 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
    x3 = xindex % 16
    x2 = xindex // 16
    x4 = xindex // 4
    x5 = xindex
    tmp0 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr1 + x3, xmask, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr2 + (x0 + 4 * x2), xmask, eviction_policy='evict_last'
        )
    tmp4 = tl.load(in_ptr3 + x4, xmask, eviction_policy='evict_last')
    tmp7 = tl.load(in_ptr4 + x4, xmask, eviction_policy='evict_last')
    tmp12 = tl.load(in_ptr5 + x0, xmask, eviction_policy='evict_last')
    tmp3 = tmp1 + tmp2
    tmp5 = tmp3 - tmp4
    tmp6 = tmp0 * tmp5
    tmp8 = libdevice.sqrt(tmp7)
    tmp9 = 1e-06
    tmp10 = tmp8 + tmp9
    tmp11 = tmp6 / tmp10
    tmp13 = tmp11 + tmp12
    tl.store(out_ptr0 + x5, tmp13, xmask)


@triton.jit
def triton_poi_fused_add_div_erf_mul_4(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
    tmp0 = tl.load(in_ptr0 + x0, xmask)
    tmp1 = 0.5
    tmp2 = tmp0 * tmp1
    tmp3 = 0.7071067811865475
    tmp4 = tmp0 * tmp3
    tmp5 = libdevice.erf(tmp4)
    tmp6 = 1.0
    tmp7 = tmp5 + tmp6
    tmp8 = tmp2 * tmp7
    tl.store(out_ptr0 + x0, tmp8, 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_ptr0 + x2, xmask)
    tmp1 = tl.load(in_out_ptr0 + x2, xmask)
    tmp2 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last')
    tmp3 = tmp1 + tmp2
    tmp4 = tmp0 + tmp3
    tl.store(in_out_ptr0 + x2, tmp4, xmask)


@triton.jit
def triton_poi_fused_add_div_mean_mul_std_sub_6(in_ptr0, in_ptr1, in_ptr2,
    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
    x1 = xindex // 4
    tmp0 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr1 + x2, xmask)
    tmp2 = tl.load(in_ptr1 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr1 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp5 = tl.load(in_ptr1 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp7 = tl.load(in_ptr1 + (3 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp30 = tl.load(in_ptr2 + x0, xmask, eviction_policy='evict_last')
    tmp4 = tmp2 + tmp3
    tmp6 = tmp4 + tmp5
    tmp8 = tmp6 + tmp7
    tmp9 = 4.0
    tmp10 = tmp8 / tmp9
    tmp11 = tmp1 - tmp10
    tmp12 = tmp0 * tmp11
    tmp13 = tmp2 - tmp10
    tmp14 = tmp13 * tmp13
    tmp15 = tmp3 - tmp10
    tmp16 = tmp15 * tmp15
    tmp17 = tmp14 + tmp16
    tmp18 = tmp5 - tmp10
    tmp19 = tmp18 * tmp18
    tmp20 = tmp17 + tmp19
    tmp21 = tmp7 - tmp10
    tmp22 = tmp21 * tmp21
    tmp23 = tmp20 + tmp22
    tmp24 = 3.0
    tmp25 = tmp23 / tmp24
    tmp26 = libdevice.sqrt(tmp25)
    tmp27 = 1e-06
    tmp28 = tmp26 + tmp27
    tmp29 = tmp12 / tmp28
    tmp31 = tmp29 + tmp30
    tl.store(out_ptr0 + x2, tmp31, 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,
        primals_13, primals_14, primals_15, primals_16, primals_17) = args
    args.clear()
    assert_size_stride(primals_1, (4, 4), (4, 1))
    assert_size_stride(primals_2, (4, 4), (4, 1))
    assert_size_stride(primals_3, (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, (4, 4), (4, 1))
    assert_size_stride(primals_9, (4,), (1,))
    assert_size_stride(primals_10, (4,), (1,))
    assert_size_stride(primals_11, (4,), (1,))
    assert_size_stride(primals_12, (16, 4), (4, 1))
    assert_size_stride(primals_13, (16,), (1,))
    assert_size_stride(primals_14, (4, 16), (16, 1))
    assert_size_stride(primals_15, (4,), (1,))
    assert_size_stride(primals_16, (4,), (1,))
    assert_size_stride(primals_17, (4,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        extern_kernels.mm(primals_1, reinterpret_tensor(primals_2, (4, 4),
            (1, 4), 0), out=buf0)
        del primals_2
        buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        extern_kernels.mm(primals_1, reinterpret_tensor(primals_4, (4, 4),
            (1, 4), 0), out=buf1)
        del primals_4
        buf2 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        extern_kernels.addmm(primals_7, primals_1, reinterpret_tensor(
            primals_6, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf2)
        del primals_6
        del primals_7
        buf3 = reinterpret_tensor(buf0, (4, 4, 1, 1), (4, 1, 16, 16), 0)
        del buf0
        get_raw_stream(0)
        triton_poi_fused_0[grid(16)](buf3, primals_3, 16, XBLOCK=16,
            num_warps=1, num_stages=1)
        del primals_3
        buf4 = reinterpret_tensor(buf1, (4, 4, 1, 1), (4, 1, 16, 16), 0)
        del buf1
        triton_poi_fused_0[grid(16)](buf4, primals_5, 16, XBLOCK=16,
            num_warps=1, num_stages=1)
        del primals_5
        buf5 = empty_strided_cuda((16, 1, 1), (1, 1, 1), torch.float32)
        extern_kernels.bmm(reinterpret_tensor(buf3, (16, 1, 1), (1, 0, 0), 
            0), reinterpret_tensor(buf4, (16, 1, 1), (1, 0, 0), 0), out=buf5)
        buf6 = reinterpret_tensor(buf5, (4, 4, 1, 1), (4, 1, 1, 1), 0)
        del buf5
        triton_poi_fused_1[grid(16)](buf6, 16, XBLOCK=16, num_warps=1,
            num_stages=1)
        buf7 = empty_strided_cuda((16, 1, 1), (1, 1, 1), torch.float32)
        extern_kernels.bmm(reinterpret_tensor(buf6, (16, 1, 1), (1, 1, 1), 
            0), reinterpret_tensor(buf2, (16, 1, 1), (1, 1, 1), 0), out=buf7)
        buf8 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        extern_kernels.addmm(primals_9, reinterpret_tensor(buf7, (4, 4), (4,
            1), 0), reinterpret_tensor(primals_8, (4, 4), (1, 4), 0), alpha
            =1, beta=1, out=buf8)
        del primals_9
        buf9 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32)
        buf10 = buf9
        del buf9
        buf11 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32)
        triton_poi_fused_add_mean_std_2[grid(16)](buf10, primals_1, buf8,
            buf11, 16, XBLOCK=16, num_warps=1, num_stages=1)
        buf12 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        triton_poi_fused_add_div_mean_mul_std_sub_3[grid(64)](primals_10,
            primals_1, buf8, buf11, buf10, primals_11, buf12, 64, XBLOCK=64,
            num_warps=1, num_stages=1)
        del buf10
        del buf11
        del primals_11
        buf13 = empty_strided_cuda((16, 16), (16, 1), torch.float32)
        extern_kernels.addmm(primals_13, reinterpret_tensor(buf12, (16, 4),
            (4, 1), 0), reinterpret_tensor(primals_12, (4, 16), (1, 4), 0),
            alpha=1, beta=1, out=buf13)
        del primals_13
        buf14 = empty_strided_cuda((4, 4, 16), (64, 16, 1), torch.float32)
        triton_poi_fused_add_div_erf_mul_4[grid(256)](buf13, buf14, 256,
            XBLOCK=128, num_warps=4, num_stages=1)
        buf15 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(buf14, (16, 16), (16, 1), 0),
            reinterpret_tensor(primals_14, (16, 4), (1, 16), 0), out=buf15)
        buf16 = reinterpret_tensor(buf15, (4, 4, 4), (16, 4, 1), 0)
        del buf15
        triton_poi_fused_add_5[grid(64)](buf16, buf12, primals_15, 64,
            XBLOCK=64, num_warps=1, num_stages=1)
        del primals_15
        buf17 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        triton_poi_fused_add_div_mean_mul_std_sub_6[grid(64)](primals_16,
            buf16, primals_17, buf17, 64, XBLOCK=64, num_warps=1, num_stages=1)
        del primals_17
    return buf17, primals_1, primals_10, primals_16, buf6, reinterpret_tensor(
        buf2, (16, 1, 1), (1, 1, 4), 0), reinterpret_tensor(buf3, (16, 1, 1
        ), (1, 1, 4), 0), reinterpret_tensor(buf4, (16, 1, 1), (1, 4, 1), 0
        ), reinterpret_tensor(buf7, (4, 4), (4, 1), 0
        ), buf8, reinterpret_tensor(buf12, (16, 4), (4, 1), 0
        ), buf13, reinterpret_tensor(buf14, (16, 16), (16, 1), 0
        ), buf16, primals_14, primals_12, primals_8


def gelu(x):
    """
    GELU activation function.
    """
    return x * 0.5 * (1.0 + torch.erf(x / math.sqrt(2.0)))


class MultiHeadedAttention(torch.nn.Module):
    """
    Implement of multi-head attention.
    """

    def __init__(self, n_heads, hidden_size, drop_rate):
        super().__init__()
        assert hidden_size % n_heads == 0
        self.n_dk = hidden_size // n_heads
        self.n_heads = n_heads
        self.proj_query = torch.nn.Linear(hidden_size, hidden_size)
        self.proj_key = torch.nn.Linear(hidden_size, hidden_size)
        self.proj_value = torch.nn.Linear(hidden_size, hidden_size)
        self.dropout = torch.nn.Dropout(drop_rate)
        self.proj_output = torch.nn.Linear(hidden_size, hidden_size)

    def forward(self, input_, mask=None):
        """
        Input: embedding.
        """
        batch_size = input_.size(0)
        query = self.proj_query(input_)
        query = query.view(batch_size, -1, self.n_heads, self.n_dk).transpose(
            1, 2)
        key = self.proj_key(input_)
        key = key.view(batch_size, -1, self.n_heads, self.n_dk).transpose(1, 2)
        value = self.proj_value(input_)
        value = value.view(batch_size, -1, self.n_heads, self.n_dk).transpose(
            1, 2)
        scores = query @ key.transpose(-2, -1)
        scores = scores / math.sqrt(self.n_dk)
        if mask is not None:
            mask = mask[:, None, None, :]
            scores = scores.masked_fill(mask == 0, -1000000000.0)
        attn = F.softmax(scores, dim=-1)
        attn = self.dropout(attn)
        cv = attn @ value
        cv = cv.transpose(1, 2)
        cv = cv.contiguous().view(batch_size, -1, self.n_heads * self.n_dk)
        return self.proj_output(cv)


class LayerNormalization(torch.nn.Module):
    """
    Epsilon outsize the square root.
    """

    def __init__(self, size, eps=1e-06):
        super(LayerNormalization, self).__init__()
        self.gamma = torch.nn.Parameter(torch.ones(size))
        self.beta = torch.nn.Parameter(torch.zeros(size))
        self.eps = eps
        self.register_parameter('gamma', self.gamma)
        self.register_parameter('beta', self.beta)

    def forward(self, input_):
        mean = torch.mean(input_, -1, keepdim=True)
        std = torch.std(input_, -1, keepdim=True)
        return self.gamma * (input_ - mean) / (std + self.eps) + self.beta


class PositionwiseFeedForward(torch.nn.Module):
    """
    FeedForward Neural Networks for each position
    """

    def __init__(self, input_size, hidden_size, output_size, drop_rate):
        super(PositionwiseFeedForward, self).__init__()
        self.ff1 = torch.nn.Linear(input_size, hidden_size)
        self.ff2 = torch.nn.Linear(hidden_size, output_size)
        self.drop = torch.nn.Dropout(drop_rate)

    def forward(self, input_):
        """ 
        (B, S, D) -> (B, S, D_ff) -> (B, S, D)
        """
        return self.drop(self.ff2(gelu(self.ff1(input_))))


class TransformerBlockNew(torch.nn.Module):
    """
    Implementation of Transformer
    """

    def __init__(self, input_size, n_heads, drop_rate, device=torch.device(
        'cpu')):
        super().__init__()
        self.attentionMH = MultiHeadedAttention(n_heads, input_size, drop_rate)
        self.norm1 = LayerNormalization(input_size)
        self.norm2 = LayerNormalization(input_size)
        self.layer_ff = PositionwiseFeedForward(input_size, input_size * 4,
            input_size, drop_rate)
        self.drop = torch.nn.Dropout(drop_rate)

    def forward(self, input_0):
        primals_1 = self.attentionMH.proj_query.weight
        primals_3 = self.attentionMH.proj_query.bias
        primals_2 = self.attentionMH.proj_key.weight
        primals_5 = self.attentionMH.proj_key.bias
        primals_4 = self.attentionMH.proj_value.weight
        primals_7 = self.attentionMH.proj_value.bias
        primals_6 = self.attentionMH.proj_output.weight
        primals_9 = self.attentionMH.proj_output.bias
        primals_10 = self.norm1.gamma
        primals_11 = self.norm1.beta
        primals_15 = self.norm2.gamma
        primals_16 = self.norm2.beta
        primals_12 = self.layer_ff.ff1.weight
        primals_13 = self.layer_ff.ff1.bias
        primals_14 = self.layer_ff.ff2.weight
        primals_17 = self.layer_ff.ff2.bias
        primals_8 = input_0
        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, primals_13, primals_14,
            primals_15, primals_16, primals_17])
        return output[0]


Measured latency_ms: 0.142346