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
from torch import 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_convolution_0(in_ptr0, out_ptr0, ynumel, xnumel,
    YBLOCK: tl.constexpr, XBLOCK: tl.constexpr):
    xnumel = 512
    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 + 262144 * y1), xmask,
        eviction_policy='evict_last')
    tl.store(out_ptr0 + (x2 + 512 * y3), tmp0, xmask)


@triton.jit
def triton_poi_fused_convolution_relu_1(in_out_ptr0, in_ptr0, xnumel,
    XBLOCK: tl.constexpr):
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    tl.full([XBLOCK], True, tl.int1)
    x3 = xindex
    x1 = xindex // 512 % 2048
    tmp0 = tl.load(in_out_ptr0 + x3, None)
    tmp1 = tl.load(in_ptr0 + x1, None, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp3 = tl.full([1], 0, tl.int32)
    tmp4 = triton_helpers.maximum(tmp3, tmp2)
    tl.store(in_out_ptr0 + x3, tmp4, None)


@triton.jit
def triton_poi_fused_convolution_2(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl
    .constexpr):
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    tl.full([XBLOCK], True, tl.int1)
    x3 = xindex
    x1 = xindex // 512 % 512
    tmp0 = tl.load(in_out_ptr0 + x3, None)
    tmp1 = tl.load(in_ptr0 + x1, None, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tl.store(in_out_ptr0 + x3, tmp2, None)


@triton.jit
def triton_red_fused_add_native_layer_norm_3(in_ptr0, in_ptr1, out_ptr0,
    out_ptr1, out_ptr2, xnumel, rnumel, XBLOCK: tl.constexpr, RBLOCK: tl.
    constexpr):
    rnumel = 128
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    rbase = tl.arange(0, RBLOCK)[None, :]
    x4 = xindex
    x0 = xindex % 4
    x1 = xindex // 4 % 512
    x2 = xindex // 2048
    tmp4_mean = tl.zeros([XBLOCK, RBLOCK], tl.float32)
    tmp4_m2 = tl.zeros([XBLOCK, RBLOCK], tl.float32)
    tmp4_weight = tl.zeros([XBLOCK, RBLOCK], tl.float32)
    for roffset in range(0, rnumel, RBLOCK):
        rindex = roffset + rbase
        rmask = rindex < rnumel
        r3 = rindex
        tmp0 = tl.load(in_ptr0 + (r3 + 128 * x4), rmask, eviction_policy=
            'evict_first', other=0.0)
        tmp1 = tl.load(in_ptr1 + (x1 + 512 * r3 + 65536 * x0 + 262144 * x2),
            rmask, eviction_policy='evict_last', other=0.0)
        tmp2 = tmp0 + tmp1
        tmp3 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK])
        tmp4_mean_next, tmp4_m2_next, tmp4_weight_next = (triton_helpers.
            welford_reduce(tmp3, tmp4_mean, tmp4_m2, tmp4_weight, roffset == 0)
            )
        tmp4_mean = tl.where(rmask, tmp4_mean_next, tmp4_mean)
        tmp4_m2 = tl.where(rmask, tmp4_m2_next, tmp4_m2)
        tmp4_weight = tl.where(rmask, tmp4_weight_next, tmp4_weight)
    tmp4_tmp, tmp5_tmp, tmp6_tmp = triton_helpers.welford(tmp4_mean,
        tmp4_m2, tmp4_weight, 1)
    tmp4 = tmp4_tmp[:, None]
    tmp5 = tmp5_tmp[:, None]
    tmp6 = tmp6_tmp[:, None]
    tl.store(out_ptr0 + x4, tmp4, None)
    tl.store(out_ptr1 + x4, tmp5, None)
    tl.store(out_ptr2 + x4, tmp6, None)


@triton.jit
def triton_per_fused_add_native_layer_norm_4(in_out_ptr0, in_ptr0, in_ptr1,
    in_ptr2, out_ptr0, xnumel, rnumel, XBLOCK: tl.constexpr):
    RBLOCK: tl.constexpr = 4
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    rindex = tl.arange(0, RBLOCK)[None, :]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    r1 = rindex
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + (r1 + 4 * x0), None)
    tmp1 = tl.load(in_ptr1 + (r1 + 4 * x0), None)
    tmp2 = tl.load(in_ptr2 + (r1 + 4 * x0), None)
    tmp3 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
    tmp4 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK])
    tmp5 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK])
    tmp7, tmp8, tmp9 = triton_helpers.welford(tmp3, tmp4, tmp5, 1)
    tmp10 = tmp7[:, None]
    tmp11 = tmp8[:, None]
    tmp9[:, None]
    tmp13 = 512.0
    tmp14 = tmp11 / tmp13
    tmp15 = 1e-05
    tmp16 = tmp14 + tmp15
    tmp17 = libdevice.rsqrt(tmp16)
    tl.debug_barrier()
    tl.store(in_out_ptr0 + x0, tmp17, None)
    tl.store(out_ptr0 + x0, tmp10, None)


@triton.jit
def triton_poi_fused_add_native_layer_norm_5(in_ptr0, in_ptr1, in_ptr2,
    in_ptr3, in_ptr4, in_ptr5, out_ptr0, ynumel, xnumel, YBLOCK: tl.
    constexpr, XBLOCK: tl.constexpr):
    xnumel = 512
    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
    y3 = yindex
    y0 = yindex % 512
    y1 = yindex // 512
    tmp0 = tl.load(in_ptr0 + (x2 + 512 * y3), xmask, eviction_policy=
        'evict_last')
    tmp1 = tl.load(in_ptr1 + (y0 + 512 * x2 + 262144 * y1), xmask,
        eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr2 + y3, None, eviction_policy='evict_last')
    tmp5 = tl.load(in_ptr3 + y3, None, eviction_policy='evict_last')
    tmp7 = tl.load(in_ptr4 + x2, xmask, eviction_policy='evict_last')
    tmp9 = tl.load(in_ptr5 + x2, xmask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp4 = tmp2 - tmp3
    tmp6 = tmp4 * tmp5
    tmp8 = tmp6 * tmp7
    tmp10 = tmp8 + tmp9
    tl.store(out_ptr0 + (x2 + 512 * y3), tmp10, xmask)


def call(args):
    (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6,
        primals_7) = args
    args.clear()
    assert_size_stride(primals_1, (4, 512, 512), (262144, 512, 1))
    assert_size_stride(primals_2, (2048, 512, 1), (512, 1, 1))
    assert_size_stride(primals_3, (2048,), (1,))
    assert_size_stride(primals_4, (512, 2048, 1), (2048, 1, 1))
    assert_size_stride(primals_5, (512,), (1,))
    assert_size_stride(primals_6, (512,), (1,))
    assert_size_stride(primals_7, (512,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 512, 512), (262144, 512, 1), torch.
            float32)
        get_raw_stream(0)
        triton_poi_fused_convolution_0[grid(2048, 512)](primals_1, buf0, 
            2048, 512, XBLOCK=32, YBLOCK=32, num_warps=4, num_stages=1)
        buf1 = extern_kernels.convolution(buf0, primals_2, stride=(1,),
            padding=(0,), dilation=(1,), transposed=False, output_padding=(
            0,), groups=1, bias=None)
        assert_size_stride(buf1, (4, 2048, 512), (1048576, 512, 1))
        buf2 = buf1
        del buf1
        triton_poi_fused_convolution_relu_1[grid(4194304)](buf2, primals_3,
            4194304, XBLOCK=1024, num_warps=4, num_stages=1)
        del primals_3
        buf3 = extern_kernels.convolution(buf2, primals_4, stride=(1,),
            padding=(0,), dilation=(1,), transposed=False, output_padding=(
            0,), groups=1, bias=None)
        assert_size_stride(buf3, (4, 512, 512), (262144, 512, 1))
        buf4 = buf3
        del buf3
        triton_poi_fused_convolution_2[grid(1048576)](buf4, primals_5, 
            1048576, XBLOCK=1024, num_warps=4, num_stages=1)
        del primals_5
        buf5 = empty_strided_cuda((4, 512, 1, 4), (2048, 4, 8192, 1), torch
            .float32)
        buf6 = empty_strided_cuda((4, 512, 1, 4), (2048, 4, 8192, 1), torch
            .float32)
        buf7 = empty_strided_cuda((4, 512, 1, 4), (2048, 4, 8192, 1), torch
            .float32)
        triton_red_fused_add_native_layer_norm_3[grid(8192)](primals_1,
            buf4, buf5, buf6, buf7, 8192, 128, XBLOCK=64, RBLOCK=8,
            num_warps=4, num_stages=1)
        buf8 = empty_strided_cuda((4, 512, 1), (512, 1, 1), torch.float32)
        buf9 = empty_strided_cuda((4, 512, 1), (512, 1, 2048), torch.float32)
        buf11 = reinterpret_tensor(buf9, (4, 512, 1), (512, 1, 1), 0)
        del buf9
        triton_per_fused_add_native_layer_norm_4[grid(2048)](buf11, buf5,
            buf6, buf7, buf8, 2048, 4, XBLOCK=128, num_warps=4, num_stages=1)
        del buf5
        del buf6
        del buf7
        buf12 = buf0
        del buf0
        triton_poi_fused_add_native_layer_norm_5[grid(2048, 512)](primals_1,
            buf4, buf8, buf11, primals_6, primals_7, buf12, 2048, 512,
            XBLOCK=64, YBLOCK=64, num_warps=8, num_stages=1)
        del primals_7
    return (buf12, primals_1, primals_2, primals_4, primals_6, buf2, buf4,
        buf8, buf11)


class PositionwiseFeedForwardNew(nn.Module):
    """ a　two layer feed forward"""

    def __init__(self, model_dim=512, ffn_dim=2048, dropout=0.1):
        super(PositionwiseFeedForwardNew, self).__init__()
        self.w1 = nn.Conv1d(model_dim, ffn_dim, 1)
        self.w2 = nn.Conv1d(ffn_dim, model_dim, 1)
        self.layer_norm = nn.LayerNorm(model_dim)
        self.dropout = nn.Dropout(dropout)

    def forward(self, input_0):
        primals_2 = self.w1.weight
        primals_3 = self.w1.bias
        primals_4 = self.w2.weight
        primals_5 = self.w2.bias
        primals_6 = self.layer_norm.weight
        primals_7 = self.layer_norm.bias
        primals_1 = input_0
        output = call([primals_1, primals_2, primals_3, primals_4,
            primals_5, primals_6, primals_7])
        return output[0]


Measured latency_ms: 0.037205