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
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_0(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl.
    constexpr, XBLOCK: tl.constexpr):
    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]
    tl.full([XBLOCK, YBLOCK], True, tl.int1)
    x2 = xindex
    y3 = yindex
    y0 = yindex % 512
    y1 = yindex // 512
    tmp0 = tl.load(in_ptr0 + (x2 + 4096 * y3), None, eviction_policy=
        'evict_last')
    tl.store(out_ptr0 + (y0 + 512 * x2 + 2097152 * y1), tmp0, None)


@triton.jit
def triton_red_fused__softmax_1(in_ptr0, in_ptr1, out_ptr2, xnumel, rnumel,
    XBLOCK: tl.constexpr, RBLOCK: tl.constexpr):
    xnumel = 4
    rnumel = 4096
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    rbase = tl.arange(0, RBLOCK)[None, :]
    x0 = xindex
    tmp1 = tl.load(in_ptr1 + 0)
    tmp2 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK])
    _tmp5 = tl.full([XBLOCK, RBLOCK], float('-inf'), tl.float32)
    for roffset in range(0, rnumel, RBLOCK):
        rindex = roffset + rbase
        rmask = rindex < rnumel
        r1 = rindex
        tmp0 = tl.load(in_ptr0 + (r1 + 4096 * x0), rmask & xmask,
            eviction_policy='evict_last', other=0.0)
        tmp3 = tmp0 + tmp2
        tmp4 = tl.broadcast_to(tmp3, [XBLOCK, RBLOCK])
        tmp6 = triton_helpers.maximum(_tmp5, tmp4)
        _tmp5 = tl.where(rmask & xmask, tmp6, _tmp5)
    tmp5 = triton_helpers.max2(_tmp5, 1)[:, None]
    tmp8 = tl.load(in_ptr1 + 0)
    tmp9 = tl.broadcast_to(tmp8, [XBLOCK, RBLOCK])
    _tmp14 = tl.full([XBLOCK, RBLOCK], 0, tl.float32)
    for roffset in range(0, rnumel, RBLOCK):
        rindex = roffset + rbase
        rmask = rindex < rnumel
        r1 = rindex
        tmp7 = tl.load(in_ptr0 + (r1 + 4096 * x0), rmask & xmask,
            eviction_policy='evict_last', other=0.0)
        tmp10 = tmp7 + tmp9
        tmp11 = tmp10 - tmp5
        tmp12 = tl_math.exp(tmp11)
        tmp13 = tl.broadcast_to(tmp12, [XBLOCK, RBLOCK])
        tmp15 = _tmp14 + tmp13
        _tmp14 = tl.where(rmask & xmask, tmp15, _tmp14)
    tmp14 = tl.sum(_tmp14, 1)[:, None]
    tmp17 = tl.load(in_ptr1 + 0)
    tmp18 = tl.broadcast_to(tmp17, [XBLOCK, RBLOCK])
    for roffset in range(0, rnumel, RBLOCK):
        rindex = roffset + rbase
        rmask = rindex < rnumel
        r1 = rindex
        tmp16 = tl.load(in_ptr0 + (r1 + 4096 * x0), rmask & xmask,
            eviction_policy='evict_first', other=0.0)
        tmp19 = tmp16 + tmp18
        tmp20 = tmp19 - tmp5
        tmp21 = tl_math.exp(tmp20)
        tmp22 = tmp21 / tmp14
        tl.store(out_ptr2 + (r1 + 4096 * x0), tmp22, rmask & xmask)


@triton.jit
def triton_poi_fused_convolution_2(in_ptr0, in_ptr1, out_ptr0, ynumel,
    xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.constexpr):
    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]
    tl.full([XBLOCK, YBLOCK], True, tl.int1)
    x2 = xindex
    y0 = yindex % 256
    y1 = yindex // 256
    y3 = yindex
    tmp0 = tl.load(in_ptr0 + (y0 + 256 * x2 + 1048576 * y1), None,
        eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr1 + y0, None, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tl.store(out_ptr0 + (x2 + 4096 * y3), tmp2, None)


@triton.jit
def triton_per_fused_convolution_native_layer_norm_sigmoid_3(in_out_ptr0,
    in_out_ptr1, 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_out_ptr0 + (r1 + 512 * x0), None)
    tmp1 = tl.load(in_ptr0 + r1, None, eviction_policy='evict_last')
    tmp23 = tl.load(in_ptr1 + r1, None, eviction_policy='evict_last')
    tmp25 = tl.load(in_ptr2 + 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
    tmp27 = tl.sigmoid(tmp26)
    tl.store(in_out_ptr0 + (r1 + 512 * x0), tmp2, None)
    tl.debug_barrier()
    tl.store(in_out_ptr1 + x0, tmp20, None)
    tl.store(out_ptr1 + (r1 + 512 * x0), tmp27, None)
    tl.store(out_ptr0 + x0, tmp10, None)


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


@triton.jit
def triton_red_fused_convolution_mean_5(in_ptr0, in_ptr1, out_ptr0, 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, :]
    x0 = xindex % 256
    x1 = xindex // 256
    tmp1 = tl.load(in_ptr1 + x0, None, eviction_policy='evict_last')
    _tmp4 = tl.full([XBLOCK, RBLOCK], 0, tl.float32)
    x3 = xindex
    for roffset in range(0, rnumel, RBLOCK):
        rindex = roffset + rbase
        rmask = rindex < rnumel
        r2 = rindex
        tmp0 = tl.load(in_ptr0 + (x0 + 256 * r2 + 32768 * x1), rmask,
            eviction_policy='evict_first', other=0.0)
        tmp2 = tmp0 + tmp1
        tmp3 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK])
        tmp5 = _tmp4 + tmp3
        _tmp4 = tl.where(rmask, tmp5, _tmp4)
    tmp4 = tl.sum(_tmp4, 1)[:, None]
    tl.store(out_ptr0 + x3, tmp4, None)


@triton.jit
def triton_per_fused_convolution_mean_6(in_ptr0, out_ptr0, xnumel, rnumel,
    XBLOCK: tl.constexpr):
    xnumel = 1024
    RBLOCK: tl.constexpr = 32
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    rindex = tl.arange(0, RBLOCK)[None, :]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    r2 = rindex
    x0 = xindex % 256
    x1 = xindex // 256
    x3 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 256 * r2 + 8192 * x1), xmask, other=0.0)
    tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
    tmp3 = tl.where(xmask, tmp1, 0)
    tmp4 = tl.sum(tmp3, 1)[:, None]
    tl.store(out_ptr0 + x3, tmp4, xmask)


@triton.jit
def triton_per_fused__softmax_7(in_ptr0, out_ptr2, xnumel, rnumel):
    XBLOCK: tl.constexpr = 1
    RBLOCK: tl.constexpr = 256
    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 + 256 * x0), None)
    tmp1 = 4096.0
    tmp2 = tmp0 / tmp1
    tmp3 = tl.broadcast_to(tmp2, [RBLOCK])
    tmp5 = triton_helpers.promote_to_tensor(triton_helpers.max2(tmp3, 0))
    tmp6 = tmp2 - tmp5
    tmp7 = tl_math.exp(tmp6)
    tmp8 = tl.broadcast_to(tmp7, [RBLOCK])
    tmp10 = triton_helpers.promote_to_tensor(tl.sum(tmp8, 0))
    tmp11 = tmp7 / tmp10
    tl.store(out_ptr2 + (r1 + 256 * x0), tmp11, None)


@triton.jit
def triton_poi_fused_mul_sigmoid_8(in_ptr0, in_ptr1, out_ptr0, ynumel,
    xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.constexpr):
    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]
    tl.full([XBLOCK, YBLOCK], True, tl.int1)
    x2 = xindex
    y1 = yindex // 512
    y0 = yindex % 512
    y3 = yindex
    tmp0 = tl.load(in_ptr0 + (x2 + 4096 * y1), None, eviction_policy=
        'evict_last')
    tmp2 = tl.load(in_ptr1 + (y0 + 512 * x2 + 2097152 * y1), None,
        eviction_policy='evict_last')
    tmp1 = tl.sigmoid(tmp0)
    tmp3 = tmp1 * tmp2
    tl.store(out_ptr0 + (x2 + 4096 * y3), tmp3, 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, 512, 64, 64), (2097152, 4096, 64, 1))
    assert_size_stride(primals_2, (256, 512, 1, 1), (512, 1, 1, 1))
    assert_size_stride(primals_3, (256,), (1,))
    assert_size_stride(primals_4, (1, 512, 1, 1), (512, 1, 1, 1))
    assert_size_stride(primals_5, (1,), (1,))
    assert_size_stride(primals_6, (512, 256, 1, 1), (256, 1, 1, 1))
    assert_size_stride(primals_7, (512,), (1,))
    assert_size_stride(primals_8, (512,), (1,))
    assert_size_stride(primals_9, (512,), (1,))
    assert_size_stride(primals_10, (256, 512, 1, 1), (512, 1, 1, 1))
    assert_size_stride(primals_11, (256,), (1,))
    assert_size_stride(primals_12, (256, 512, 1, 1), (512, 1, 1, 1))
    assert_size_stride(primals_13, (256,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 512, 64, 64), (2097152, 1, 32768, 512
            ), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_0[grid(2048, 4096)](primals_1, buf0, 2048, 4096,
            XBLOCK=32, YBLOCK=32, num_warps=4, num_stages=1)
        del primals_1
        buf1 = extern_kernels.convolution(buf0, primals_2, stride=(1, 1),
            padding=(0, 0), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf1, (4, 256, 64, 64), (1048576, 1, 16384, 256))
        buf2 = extern_kernels.convolution(buf0, primals_4, stride=(1, 1),
            padding=(0, 0), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf2, (4, 1, 64, 64), (4096, 1, 64, 1))
        buf5 = empty_strided_cuda((4, 4096, 1), (4096, 1, 1), torch.float32)
        triton_red_fused__softmax_1[grid(4)](buf2, primals_5, buf5, 4, 4096,
            XBLOCK=1, RBLOCK=2048, num_warps=16, num_stages=1)
        del primals_5
        buf6 = empty_strided_cuda((4, 256, 64, 64), (1048576, 4096, 64, 1),
            torch.float32)
        triton_poi_fused_convolution_2[grid(1024, 4096)](buf1, primals_3,
            buf6, 1024, 4096, XBLOCK=16, YBLOCK=256, num_warps=8, num_stages=1)
        del buf1
        del primals_3
        buf7 = empty_strided_cuda((4, 256, 1), (256, 1, 1), torch.float32)
        extern_kernels.bmm(reinterpret_tensor(buf6, (4, 256, 4096), (
            1048576, 4096, 1), 0), buf5, out=buf7)
        buf8 = extern_kernels.convolution(reinterpret_tensor(buf7, (4, 256,
            1, 1), (256, 1, 1, 1), 0), primals_6, stride=(1, 1), padding=(0,
            0), dilation=(1, 1), transposed=False, output_padding=(0, 0),
            groups=1, bias=None)
        assert_size_stride(buf8, (4, 512, 1, 1), (512, 1, 1, 1))
        buf9 = reinterpret_tensor(buf8, (4, 512, 1, 1), (512, 1, 512, 512), 0)
        del buf8
        buf10 = empty_strided_cuda((4, 1, 1), (1, 1, 1), torch.float32)
        buf11 = empty_strided_cuda((4, 1, 1), (1, 4, 4), torch.float32)
        buf13 = reinterpret_tensor(buf11, (4, 1, 1), (1, 1, 1), 0)
        del buf11
        buf14 = empty_strided_cuda((4, 1, 512), (512, 2048, 1), torch.float32)
        triton_per_fused_convolution_native_layer_norm_sigmoid_3[grid(4)](buf9,
            buf13, primals_7, primals_8, primals_9, buf10, buf14, 4, 512,
            num_warps=4, num_stages=1)
        del primals_7
        buf15 = empty_strided_cuda((4, 512, 64, 64), (2097152, 1, 32768, 
            512), torch.float32)
        triton_poi_fused_mul_4[grid(8388608)](buf14, buf0, buf15, 8388608,
            XBLOCK=1024, num_warps=4, num_stages=1)
        del buf14
        buf16 = extern_kernels.convolution(buf15, primals_10, stride=(1, 1),
            padding=(0, 0), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf16, (4, 256, 64, 64), (1048576, 1, 16384, 256))
        buf17 = extern_kernels.convolution(buf15, primals_12, stride=(1, 1),
            padding=(0, 0), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf17, (4, 256, 64, 64), (1048576, 1, 16384, 256))
        buf18 = empty_strided_cuda((4, 256, 1, 1, 32), (8192, 1, 32768, 
            32768, 256), torch.float32)
        triton_red_fused_convolution_mean_5[grid(32768)](buf17, primals_13,
            buf18, 32768, 128, XBLOCK=64, RBLOCK=8, num_warps=4, num_stages=1)
        del primals_13
        buf19 = empty_strided_cuda((4, 256, 1, 1), (256, 1, 1024, 1024),
            torch.float32)
        triton_per_fused_convolution_mean_6[grid(1024)](buf18, buf19, 1024,
            32, XBLOCK=128, num_warps=8, num_stages=1)
        del buf18
        buf22 = empty_strided_cuda((4, 1, 256), (256, 256, 1), torch.float32)
        triton_per_fused__softmax_7[grid(4)](buf19, buf22, 4, 256,
            num_warps=2, num_stages=1)
        del buf19
        buf23 = reinterpret_tensor(buf17, (4, 256, 64, 64), (1048576, 4096,
            64, 1), 0)
        del buf17
        triton_poi_fused_convolution_2[grid(1024, 4096)](buf16, primals_11,
            buf23, 1024, 4096, XBLOCK=16, YBLOCK=256, num_warps=8, num_stages=1
            )
        del buf16
        del primals_11
        buf24 = reinterpret_tensor(buf2, (4, 1, 4096), (4096, 4096, 1), 0)
        del buf2
        extern_kernels.bmm(buf22, reinterpret_tensor(buf23, (4, 256, 4096),
            (1048576, 4096, 1), 0), out=buf24)
        buf25 = empty_strided_cuda((4, 512, 64, 64), (2097152, 4096, 64, 1),
            torch.float32)
        triton_poi_fused_mul_sigmoid_8[grid(2048, 4096)](buf24, buf15,
            buf25, 2048, 4096, XBLOCK=32, YBLOCK=32, num_warps=4, num_stages=1)
    return (buf25, buf0, primals_2, primals_4, primals_6, primals_8,
        primals_9, primals_10, primals_12, buf5, reinterpret_tensor(buf7, (
        4, 256, 1, 1), (256, 1, 1, 1), 0), buf9, buf10, buf13, buf15, buf22,
        buf24, reinterpret_tensor(buf23, (4, 4096, 256), (1048576, 1, 4096),
        0), reinterpret_tensor(buf6, (4, 4096, 256), (1048576, 1, 4096), 0))


class SequentialPolarizedSelfAttentionNew(nn.Module):

    def __init__(self, channel=512):
        super().__init__()
        self.ch_wv = nn.Conv2d(channel, channel // 2, kernel_size=(1, 1))
        self.ch_wq = nn.Conv2d(channel, 1, kernel_size=(1, 1))
        self.softmax_channel = nn.Softmax(1)
        self.softmax_spatial = nn.Softmax(-1)
        self.ch_wz = nn.Conv2d(channel // 2, channel, kernel_size=(1, 1))
        self.ln = nn.LayerNorm(channel)
        self.sigmoid = nn.Sigmoid()
        self.sp_wv = nn.Conv2d(channel, channel // 2, kernel_size=(1, 1))
        self.sp_wq = nn.Conv2d(channel, channel // 2, kernel_size=(1, 1))
        self.agp = nn.AdaptiveAvgPool2d((1, 1))

    def forward(self, input_0):
        primals_2 = self.ch_wv.weight
        primals_3 = self.ch_wv.bias
        primals_4 = self.ch_wq.weight
        primals_5 = self.ch_wq.bias
        primals_6 = self.ch_wz.weight
        primals_7 = self.ch_wz.bias
        primals_8 = self.ln.weight
        primals_9 = self.ln.bias
        primals_10 = self.sp_wv.weight
        primals_11 = self.sp_wv.bias
        primals_12 = self.sp_wq.weight
        primals_13 = self.sp_wq.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.722846