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 libdevice, 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_per_fused_native_layer_norm_0(in_out_ptr0, in_ptr0, in_ptr1,
    in_ptr2, out_ptr0, out_ptr1, xnumel, rnumel):
    XBLOCK: tl.constexpr = 1
    RBLOCK: tl.constexpr = 512
    xoffset = tl.program_id(0) * XBLOCK
    xindex = tl.full([1], xoffset, tl.int32)
    tl.full([RBLOCK], True, tl.int1)
    rindex = tl.arange(0, RBLOCK)[:]
    tl.full([RBLOCK], True, tl.int1)
    r1 = rindex
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + (r1 + 512 * x0), None)
    tmp21 = tl.load(in_ptr1 + r1, None, eviction_policy='evict_last')
    tmp23 = tl.load(in_ptr2 + r1, None, eviction_policy='evict_last')
    tmp1 = tl.broadcast_to(tmp0, [RBLOCK])
    tmp3 = tl.broadcast_to(tmp1, [RBLOCK])
    tmp5 = triton_helpers.promote_to_tensor(tl.sum(tmp3, 0))
    tmp6 = tl.full([1], 512, tl.int32)
    tmp7 = tmp6.to(tl.float32)
    tmp8 = tmp5 / tmp7
    tmp9 = tmp1 - tmp8
    tmp10 = tmp9 * tmp9
    tmp11 = tl.broadcast_to(tmp10, [RBLOCK])
    tmp13 = triton_helpers.promote_to_tensor(tl.sum(tmp11, 0))
    tmp14 = 512.0
    tmp15 = tmp13 / tmp14
    tmp16 = 1e-05
    tmp17 = tmp15 + tmp16
    tmp18 = libdevice.rsqrt(tmp17)
    tmp19 = tmp0 - tmp8
    tmp20 = tmp19 * tmp18
    tmp22 = tmp20 * tmp21
    tmp24 = tmp22 + tmp23
    tl.debug_barrier()
    tl.store(in_out_ptr0 + x0, tmp18, None)
    tl.store(out_ptr1 + (r1 + 512 * x0), tmp24, None)
    tl.store(out_ptr0 + x0, tmp8, None)


@triton.jit
def triton_poi_fused_clone_1(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 % 64
    x1 = xindex // 64 % 4
    x2 = xindex // 256 % 8
    x3 = xindex // 2048
    x4 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 64 * x2 + 512 * x1 + 2048 * x3), None)
    tl.store(out_ptr0 + x4, tmp0, None)


@triton.jit
def triton_poi_fused_clone_2(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl.
    constexpr, XBLOCK: tl.constexpr):
    xnumel = 4
    yoffset = tl.program_id(1) * YBLOCK
    yindex = yoffset + tl.arange(0, YBLOCK)[None, :]
    tl.full([XBLOCK, YBLOCK], True, tl.int1)
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    x2 = xindex
    y0 = yindex % 512
    y1 = yindex // 512
    y3 = yindex
    tmp0 = tl.load(in_ptr0 + (y0 + 512 * x2 + 2048 * y1), xmask,
        eviction_policy='evict_last')
    tl.store(out_ptr0 + (x2 + 4 * y3), tmp0, 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)
    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.125
    tmp16 = tmp14 * tmp15
    tmp17 = tl_math.exp(tmp16)
    tl.store(out_ptr0 + x2, tmp17, xmask)


@triton.jit
def triton_poi_fused__softmax_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
    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_5(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 % 64
    x1 = xindex // 64 % 8
    x2 = xindex // 512 % 4
    x3 = xindex // 2048
    x4 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 64 * x2 + 256 * x1 + 2048 * x3), None)
    tl.store(out_ptr0 + x4, tmp0, None)


@triton.jit
def triton_per_fused_add_native_layer_norm_6(in_out_ptr0, in_ptr0, in_ptr1,
    in_ptr2, in_ptr3, out_ptr0, out_ptr1, xnumel, rnumel):
    XBLOCK: tl.constexpr = 1
    RBLOCK: tl.constexpr = 512
    xoffset = tl.program_id(0) * XBLOCK
    xindex = tl.full([1], xoffset, tl.int32)
    tl.full([RBLOCK], True, tl.int1)
    rindex = tl.arange(0, RBLOCK)[:]
    tl.full([RBLOCK], True, tl.int1)
    r1 = rindex
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + (r1 + 512 * x0), None)
    tmp1 = tl.load(in_ptr1 + (r1 + 512 * x0), None)
    tmp23 = tl.load(in_ptr2 + r1, None, eviction_policy='evict_last')
    tmp25 = tl.load(in_ptr3 + r1, None, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp3 = tl.broadcast_to(tmp2, [RBLOCK])
    tmp5 = tl.broadcast_to(tmp3, [RBLOCK])
    tmp7 = triton_helpers.promote_to_tensor(tl.sum(tmp5, 0))
    tmp8 = tl.full([1], 512, tl.int32)
    tmp9 = tmp8.to(tl.float32)
    tmp10 = tmp7 / tmp9
    tmp11 = tmp3 - tmp10
    tmp12 = tmp11 * tmp11
    tmp13 = tl.broadcast_to(tmp12, [RBLOCK])
    tmp15 = triton_helpers.promote_to_tensor(tl.sum(tmp13, 0))
    tmp16 = 512.0
    tmp17 = tmp15 / tmp16
    tmp18 = 1e-05
    tmp19 = tmp17 + tmp18
    tmp20 = libdevice.rsqrt(tmp19)
    tmp21 = tmp2 - tmp10
    tmp22 = tmp21 * tmp20
    tmp24 = tmp22 * tmp23
    tmp26 = tmp24 + tmp25
    tl.debug_barrier()
    tl.store(in_out_ptr0 + x0, tmp20, None)
    tl.store(out_ptr1 + (r1 + 512 * x0), tmp26, None)
    tl.store(out_ptr0 + x0, tmp10, None)


@triton.jit
def triton_poi_fused_gelu_7(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
    tmp0 = tl.load(in_ptr0 + x0, None)
    tmp1 = 0.5
    tmp2 = tmp0 * tmp1
    tmp3 = 0.7071067811865476
    tmp4 = tmp0 * tmp3
    tmp5 = libdevice.erf(tmp4)
    tmp6 = 1.0
    tmp7 = tmp5 + tmp6
    tmp8 = tmp2 * tmp7
    tl.store(out_ptr0 + x0, tmp8, None)


@triton.jit
def triton_poi_fused_add_8(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, xnumel,
    XBLOCK: tl.constexpr):
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    tl.full([XBLOCK], True, tl.int1)
    x2 = xindex
    x0 = xindex % 512
    tmp0 = tl.load(in_ptr0 + x2, None)
    tmp1 = tl.load(in_ptr1 + x2, None)
    tmp3 = tl.load(in_out_ptr0 + x2, None)
    tmp4 = tl.load(in_ptr2 + x0, None, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp5 = tmp3 + tmp4
    tmp6 = tmp2 + tmp5
    tl.store(in_out_ptr0 + x2, tmp6, None)


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) = args
    args.clear()
    assert_size_stride(primals_1, (4, 4, 512), (2048, 512, 1))
    assert_size_stride(primals_2, (512,), (1,))
    assert_size_stride(primals_3, (512,), (1,))
    assert_size_stride(primals_4, (512, 512), (512, 1))
    assert_size_stride(primals_5, (512, 512), (512, 1))
    assert_size_stride(primals_6, (512, 512), (512, 1))
    assert_size_stride(primals_7, (512, 512), (512, 1))
    assert_size_stride(primals_8, (512,), (1,))
    assert_size_stride(primals_9, (512,), (1,))
    assert_size_stride(primals_10, (256, 512), (512, 1))
    assert_size_stride(primals_11, (256,), (1,))
    assert_size_stride(primals_12, (512, 256), (256, 1))
    assert_size_stride(primals_13, (512,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 4, 1), (4, 1, 1), torch.float32)
        buf1 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32)
        buf3 = reinterpret_tensor(buf1, (4, 4, 1), (4, 1, 1), 0)
        del buf1
        buf4 = empty_strided_cuda((4, 4, 512), (2048, 512, 1), torch.float32)
        get_raw_stream(0)
        triton_per_fused_native_layer_norm_0[grid(16)](buf3, primals_1,
            primals_2, primals_3, buf0, buf4, 16, 512, num_warps=4,
            num_stages=1)
        del primals_2
        del primals_3
        buf5 = empty_strided_cuda((16, 512), (512, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(buf4, (16, 512), (512, 1), 0),
            reinterpret_tensor(primals_4, (512, 512), (1, 512), 0), out=buf5)
        buf6 = empty_strided_cuda((16, 512), (512, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(buf4, (16, 512), (512, 1), 0),
            reinterpret_tensor(primals_5, (512, 512), (1, 512), 0), out=buf6)
        buf7 = empty_strided_cuda((16, 512), (512, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(buf4, (16, 512), (512, 1), 0),
            reinterpret_tensor(primals_6, (512, 512), (1, 512), 0), out=buf7)
        buf8 = empty_strided_cuda((4, 8, 4, 64), (2048, 256, 64, 1), torch.
            float32)
        triton_poi_fused_clone_1[grid(8192)](buf5, buf8, 8192, XBLOCK=256,
            num_warps=4, num_stages=1)
        buf9 = reinterpret_tensor(buf5, (4, 8, 64, 4), (2048, 256, 4, 1), 0)
        del buf5
        triton_poi_fused_clone_2[grid(2048, 4)](buf6, buf9, 2048, 4, XBLOCK
            =4, YBLOCK=256, num_warps=4, num_stages=1)
        buf10 = empty_strided_cuda((32, 4, 4), (16, 4, 1), torch.float32)
        extern_kernels.bmm(reinterpret_tensor(buf8, (32, 4, 64), (256, 64, 
            1), 0), reinterpret_tensor(buf9, (32, 64, 4), (256, 4, 1), 0),
            out=buf10)
        buf11 = empty_strided_cuda((4, 8, 4, 4), (128, 16, 4, 1), torch.float32
            )
        triton_poi_fused__softmax_3[grid(512)](buf10, buf11, 512, XBLOCK=
            256, num_warps=4, num_stages=1)
        buf12 = reinterpret_tensor(buf10, (4, 8, 4, 4), (128, 16, 4, 1), 0)
        del buf10
        triton_poi_fused__softmax_4[grid(512)](buf11, buf12, 512, XBLOCK=
            128, num_warps=4, num_stages=1)
        del buf11
        buf13 = reinterpret_tensor(buf6, (4, 8, 4, 64), (2048, 256, 64, 1), 0)
        del buf6
        triton_poi_fused_clone_1[grid(8192)](buf7, buf13, 8192, XBLOCK=256,
            num_warps=4, num_stages=1)
        buf14 = reinterpret_tensor(buf7, (32, 4, 64), (256, 64, 1), 0)
        del buf7
        extern_kernels.bmm(reinterpret_tensor(buf12, (32, 4, 4), (16, 4, 1),
            0), reinterpret_tensor(buf13, (32, 4, 64), (256, 64, 1), 0),
            out=buf14)
        buf15 = empty_strided_cuda((4, 4, 8, 64), (2048, 512, 64, 1), torch
            .float32)
        triton_poi_fused_clone_5[grid(8192)](buf14, buf15, 8192, XBLOCK=256,
            num_warps=4, num_stages=1)
        buf16 = reinterpret_tensor(buf14, (16, 512), (512, 1), 0)
        del buf14
        extern_kernels.mm(reinterpret_tensor(buf15, (16, 512), (512, 1), 0),
            reinterpret_tensor(primals_7, (512, 512), (1, 512), 0), out=buf16)
        buf17 = empty_strided_cuda((4, 4, 1), (4, 1, 1), torch.float32)
        buf18 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32)
        buf20 = reinterpret_tensor(buf18, (4, 4, 1), (4, 1, 1), 0)
        del buf18
        buf21 = empty_strided_cuda((4, 4, 512), (2048, 512, 1), torch.float32)
        triton_per_fused_add_native_layer_norm_6[grid(16)](buf20, primals_1,
            buf16, primals_8, primals_9, buf17, buf21, 16, 512, num_warps=4,
            num_stages=1)
        del primals_9
        buf22 = empty_strided_cuda((16, 256), (256, 1), torch.float32)
        extern_kernels.addmm(primals_11, reinterpret_tensor(buf21, (16, 512
            ), (512, 1), 0), reinterpret_tensor(primals_10, (512, 256), (1,
            512), 0), alpha=1, beta=1, out=buf22)
        del primals_11
        buf23 = empty_strided_cuda((4, 4, 256), (1024, 256, 1), torch.float32)
        triton_poi_fused_gelu_7[grid(4096)](buf22, buf23, 4096, XBLOCK=256,
            num_warps=4, num_stages=1)
        buf24 = empty_strided_cuda((16, 512), (512, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(buf23, (16, 256), (256, 1), 0),
            reinterpret_tensor(primals_12, (256, 512), (1, 256), 0), out=buf24)
        buf25 = reinterpret_tensor(buf24, (4, 4, 512), (2048, 512, 1), 0)
        del buf24
        triton_poi_fused_add_8[grid(8192)](buf25, primals_1, buf16,
            primals_13, 8192, XBLOCK=256, num_warps=4, num_stages=1)
        del primals_13
    return buf25, primals_1, primals_8, buf0, buf3, reinterpret_tensor(buf4,
        (16, 512), (512, 1), 0), buf12, reinterpret_tensor(buf15, (16, 512),
        (512, 1), 0), buf16, buf17, buf20, reinterpret_tensor(buf21, (16, 
        512), (512, 1), 0), buf22, reinterpret_tensor(buf23, (16, 256), (
        256, 1), 0), primals_12, primals_10, primals_7, reinterpret_tensor(
        buf13, (32, 64, 4), (256, 1, 64), 0), reinterpret_tensor(buf8, (32,
        64, 4), (256, 1, 64), 0), reinterpret_tensor(buf9, (32, 4, 64), (
        256, 1, 4), 0), primals_6, primals_5, primals_4


class MultiHeadAttention(nn.Module):
    """Multi-Head Attention module."""

    def __init__(self, n_head=8, d_model=512, d_k=64, d_v=64, dropout=0.1,
        qkv_bias=False, mask_value=0):
        super().__init__()
        self.mask_value = mask_value
        self.n_head = n_head
        self.d_k = d_k
        self.d_v = d_v
        self.scale = d_k ** -0.5
        self.dim_k = n_head * d_k
        self.dim_v = n_head * d_v
        self.linear_q = nn.Linear(self.dim_k, self.dim_k, bias=qkv_bias)
        self.linear_k = nn.Linear(self.dim_k, self.dim_k, bias=qkv_bias)
        self.linear_v = nn.Linear(self.dim_v, self.dim_v, bias=qkv_bias)
        self.fc = nn.Linear(self.dim_v, d_model, bias=qkv_bias)
        self.attn_drop = nn.Dropout(dropout)
        self.proj_drop = nn.Dropout(dropout)

    def forward(self, q, k, v, mask=None):
        batch_size, len_q, _ = q.size()
        _, len_k, _ = k.size()
        q = self.linear_q(q).view(batch_size, len_q, self.n_head, self.d_k)
        k = self.linear_k(k).view(batch_size, len_k, self.n_head, self.d_k)
        v = self.linear_v(v).view(batch_size, len_k, self.n_head, self.d_v)
        q = q.permute(0, 2, 1, 3)
        k = k.permute(0, 2, 3, 1)
        v = v.permute(0, 2, 1, 3)
        logits = torch.matmul(q, k) * self.scale
        if mask is not None:
            if mask.dim() == 3:
                mask = mask.unsqueeze(1)
            elif mask.dim() == 2:
                mask = mask.unsqueeze(1).unsqueeze(1)
            logits = logits.masked_fill(mask == self.mask_value, float('-inf'))
        weights = logits.softmax(dim=-1)
        weights = self.attn_drop(weights)
        attn_out = torch.matmul(weights, v).transpose(1, 2)
        attn_out = attn_out.reshape(batch_size, len_q, self.dim_v)
        attn_out = self.fc(attn_out)
        attn_out = self.proj_drop(attn_out)
        return attn_out


class PositionwiseFeedForward(nn.Module):
    """A two-feed-forward-layer module."""

    def __init__(self, d_in, d_hid, dropout=0.1, act_layer=nn.GELU):
        super().__init__()
        self.w_1 = nn.Linear(d_in, d_hid)
        self.w_2 = nn.Linear(d_hid, d_in)
        self.act = act_layer()
        self.dropout = nn.Dropout(dropout)

    def forward(self, x):
        x = self.w_1(x)
        x = self.act(x)
        x = self.dropout(x)
        x = self.w_2(x)
        x = self.dropout(x)
        return x


class TransformerEncoderLayerNew(nn.Module):
    """"""

    def __init__(self, d_model=512, d_inner=256, n_head=8, d_k=64, d_v=64,
        dropout=0.1, qkv_bias=False, mask_value=0, act_layer=nn.GELU):
        super().__init__()
        self.norm1 = nn.LayerNorm(d_model)
        self.attn = MultiHeadAttention(n_head, d_model, d_k, d_v, qkv_bias=
            qkv_bias, dropout=dropout, mask_value=mask_value)
        self.norm2 = nn.LayerNorm(d_model)
        self.mlp = PositionwiseFeedForward(d_model, d_inner, dropout=
            dropout, act_layer=act_layer)

    def forward(self, input_0):
        primals_2 = self.norm1.weight
        primals_3 = self.norm1.bias
        primals_4 = self.attn.linear_q.weight
        primals_5 = self.attn.linear_k.weight
        primals_6 = self.attn.linear_v.weight
        primals_7 = self.attn.fc.weight
        primals_8 = self.norm2.weight
        primals_9 = self.norm2.bias
        primals_10 = self.mlp.w_1.weight
        primals_11 = self.mlp.w_1.bias
        primals_12 = self.mlp.w_2.weight
        primals_13 = self.mlp.w_2.bias
        primals_1 = 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])
        return output[0]


Measured latency_ms: 0.038282