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 numpy as np
from torch import nn
from torch.nn import 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_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_poi_fused_1(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl.
    constexpr, XBLOCK: tl.constexpr):
    xnumel = 9
    yoffset = (tl.program_id(1) + tl.program_id(2) * tl.num_programs(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 + 9 * y3), xmask, eviction_policy='evict_last'
        )
    tl.store(out_ptr0 + (y0 + 512 * x2 + 4608 * y1), tmp0, xmask)


@triton.jit
def triton_poi_fused_clone_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)
    x2 = xindex
    x0 = xindex % 512
    tmp0 = tl.load(in_out_ptr0 + x2, None)
    tmp1 = tl.load(in_ptr0 + x0, None, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tl.store(in_out_ptr0 + x2, tmp2, None)


@triton.jit
def triton_red_fused__softmax_sqrt_3(in_ptr0, out_ptr2, xnumel, rnumel,
    XBLOCK: tl.constexpr, RBLOCK: tl.constexpr):
    rnumel = 4096
    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
    _tmp9 = 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, eviction_policy=
            'evict_last', other=0.0)
        tmp1 = tl.full([1, 1], 22.62741699796952, tl.float64)
        tmp2 = tl.full([1, 1], 0.0, tl.float64)
        tmp3 = tmp1 >= tmp2
        tmp4 = 1.0
        tmp5 = -1.0
        tmp6 = tl.where(tmp3, tmp4, tmp5)
        tmp7 = tmp0 * tmp6
        tmp8 = tl.broadcast_to(tmp7, [XBLOCK, RBLOCK])
        tmp10 = triton_helpers.maximum(_tmp9, tmp8)
        _tmp9 = tl.where(rmask, tmp10, _tmp9)
    tmp9 = triton_helpers.max2(_tmp9, 1)[:, None]
    _tmp26 = tl.full([XBLOCK, RBLOCK], 0, tl.float32)
    for roffset in range(0, rnumel, RBLOCK):
        rindex = roffset + rbase
        rmask = rindex < rnumel
        r1 = rindex
        tmp11 = tl.load(in_ptr0 + (r1 + 4096 * x0), rmask, eviction_policy=
            'evict_last', other=0.0)
        tmp12 = tl.full([1, 1], 22.62741699796952, tl.float64)
        tmp13 = tl.full([1, 1], 0.0, tl.float64)
        tmp14 = tmp12 >= tmp13
        tmp15 = 1.0
        tmp16 = -1.0
        tmp17 = tl.where(tmp14, tmp15, tmp16)
        tmp18 = tmp11 * tmp17
        tmp19 = tmp18 - tmp9
        tmp20 = tmp17.to(tl.float64)
        tmp21 = tmp20 * tmp12
        tmp22 = tmp21.to(tl.float32)
        tmp23 = tmp19 / tmp22
        tmp24 = tl_math.exp(tmp23)
        tmp25 = tl.broadcast_to(tmp24, [XBLOCK, RBLOCK])
        tmp27 = _tmp26 + tmp25
        _tmp26 = tl.where(rmask, tmp27, _tmp26)
    tmp26 = tl.sum(_tmp26, 1)[:, None]
    for roffset in range(0, rnumel, RBLOCK):
        rindex = roffset + rbase
        rmask = rindex < rnumel
        r1 = rindex
        tmp28 = tl.load(in_ptr0 + (r1 + 4096 * x0), rmask, eviction_policy=
            'evict_first', other=0.0)
        tmp29 = tl.full([1, 1], 22.62741699796952, tl.float64)
        tmp30 = tl.full([1, 1], 0.0, tl.float64)
        tmp31 = tmp29 >= tmp30
        tmp32 = 1.0
        tmp33 = -1.0
        tmp34 = tl.where(tmp31, tmp32, tmp33)
        tmp35 = tmp28 * tmp34
        tmp36 = tmp35 - tmp9
        tmp37 = tmp34.to(tl.float64)
        tmp38 = tmp37 * tmp29
        tmp39 = tmp38.to(tl.float32)
        tmp40 = tmp36 / tmp39
        tmp41 = tl_math.exp(tmp40)
        tmp42 = tmp41 / tmp26
        tl.store(out_ptr2 + (r1 + 4096 * x0), tmp42, rmask)


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, 512, 64, 64), (2097152, 4096, 64, 1))
    assert_size_stride(primals_2, (512, 512, 3, 3), (4608, 9, 3, 1))
    assert_size_stride(primals_3, (512,), (1,))
    assert_size_stride(primals_4, (512, 512), (512, 1))
    assert_size_stride(primals_5, (512,), (1,))
    assert_size_stride(primals_6, (512, 512), (512, 1))
    assert_size_stride(primals_7, (512,), (1,))
    assert_size_stride(primals_8, (512, 512), (512, 1))
    assert_size_stride(primals_9, (512,), (1,))
    assert_size_stride(primals_10, (512, 512), (512, 1))
    assert_size_stride(primals_11, (512,), (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 = empty_strided_cuda((512, 512, 3, 3), (4608, 1, 1536, 512),
            torch.float32)
        triton_poi_fused_1[grid(262144, 9)](primals_2, buf1, 262144, 9,
            XBLOCK=16, YBLOCK=64, num_warps=4, num_stages=1)
        del primals_2
        buf2 = extern_kernels.convolution(buf0, buf1, stride=(1, 1),
            padding=(1, 1), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf2, (4, 512, 64, 64), (2097152, 1, 32768, 512))
        buf3 = reinterpret_tensor(buf2, (4, 4096, 512), (2097152, 512, 1), 0)
        del buf2
        triton_poi_fused_clone_2[grid(8388608)](buf3, primals_3, 8388608,
            XBLOCK=1024, num_warps=4, num_stages=1)
        del primals_3
        buf4 = empty_strided_cuda((16384, 512), (512, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(buf3, (16384, 512), (512, 1), 
            0), reinterpret_tensor(primals_4, (512, 512), (1, 512), 0), out
            =buf4)
        buf5 = empty_strided_cuda((16384, 512), (512, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(buf3, (16384, 512), (512, 1), 
            0), reinterpret_tensor(primals_6, (512, 512), (1, 512), 0), out
            =buf5)
        buf6 = empty_strided_cuda((16384, 512), (512, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(buf3, (16384, 512), (512, 1), 
            0), reinterpret_tensor(primals_8, (512, 512), (1, 512), 0), out
            =buf6)
        buf7 = reinterpret_tensor(buf4, (4, 4096, 512), (2097152, 512, 1), 0)
        del buf4
        triton_poi_fused_clone_2[grid(8388608)](buf7, primals_5, 8388608,
            XBLOCK=1024, num_warps=4, num_stages=1)
        del primals_5
        buf8 = reinterpret_tensor(buf5, (4, 4096, 512), (2097152, 512, 1), 0)
        del buf5
        triton_poi_fused_clone_2[grid(8388608)](buf8, primals_7, 8388608,
            XBLOCK=1024, num_warps=4, num_stages=1)
        del primals_7
        buf9 = empty_strided_cuda((4, 4096, 4096), (16777216, 4096, 1),
            torch.float32)
        extern_kernels.bmm(buf7, reinterpret_tensor(buf8, (4, 512, 4096), (
            2097152, 1, 512), 0), out=buf9)
        buf12 = empty_strided_cuda((4, 1, 4096, 4096), (16777216, 1, 4096, 
            1), torch.float32)
        triton_red_fused__softmax_sqrt_3[grid(16384)](buf9, buf12, 16384, 
            4096, XBLOCK=1, RBLOCK=2048, num_warps=16, num_stages=1)
        del buf9
        buf13 = reinterpret_tensor(buf6, (4, 4096, 512), (2097152, 512, 1), 0)
        del buf6
        triton_poi_fused_clone_2[grid(8388608)](buf13, primals_9, 8388608,
            XBLOCK=1024, num_warps=4, num_stages=1)
        del primals_9
        buf14 = empty_strided_cuda((4, 4096, 512), (2097152, 512, 1), torch
            .float32)
        extern_kernels.bmm(reinterpret_tensor(buf12, (4, 4096, 4096), (
            16777216, 4096, 1), 0), buf13, out=buf14)
        buf15 = empty_strided_cuda((16384, 512), (512, 1), torch.float32)
        extern_kernels.addmm(primals_11, reinterpret_tensor(buf14, (16384, 
            512), (512, 1), 0), reinterpret_tensor(primals_10, (512, 512),
            (1, 512), 0), alpha=1, beta=1, out=buf15)
        del primals_11
    return reinterpret_tensor(buf15, (4, 4096, 512), (2097152, 512, 1), 0
        ), buf0, buf1, reinterpret_tensor(buf3, (16384, 512), (512, 1), 0
        ), buf12, reinterpret_tensor(buf14, (16384, 512), (512, 1), 0
        ), primals_10, reinterpret_tensor(buf13, (4, 512, 4096), (2097152, 
        1, 512), 0), reinterpret_tensor(buf7, (4, 512, 4096), (2097152, 1, 
        512), 0), buf8, primals_8, primals_6, primals_4


class ScaledDotProductAttention(nn.Module):
    """
    Scaled dot-product attention
    """

    def __init__(self, d_model, d_k, d_v, h, dropout=0.1):
        """
        :param d_model: Output dimensionality of the model
        :param d_k: Dimensionality of queries and keys
        :param d_v: Dimensionality of values
        :param h: Number of heads
        """
        super(ScaledDotProductAttention, self).__init__()
        self.fc_q = nn.Linear(d_model, h * d_k)
        self.fc_k = nn.Linear(d_model, h * d_k)
        self.fc_v = nn.Linear(d_model, h * d_v)
        self.fc_o = nn.Linear(h * d_v, d_model)
        self.dropout = nn.Dropout(dropout)
        self.d_model = d_model
        self.d_k = d_k
        self.d_v = d_v
        self.h = h
        self.init_weights()

    def init_weights(self):
        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                init.kaiming_normal_(m.weight, mode='fan_out')
                if m.bias is not None:
                    init.constant_(m.bias, 0)
            elif isinstance(m, nn.BatchNorm2d):
                init.constant_(m.weight, 1)
                init.constant_(m.bias, 0)
            elif isinstance(m, nn.Linear):
                init.normal_(m.weight, std=0.001)
                if m.bias is not None:
                    init.constant_(m.bias, 0)

    def forward(self, queries, keys, values, attention_mask=None,
        attention_weights=None):
        """
        Computes
        :param queries: Queries (b_s, nq, d_model)
        :param keys: Keys (b_s, nk, d_model)
        :param values: Values (b_s, nk, d_model)
        :param attention_mask: Mask over attention values (b_s, h, nq, nk). True indicates masking.
        :param attention_weights: Multiplicative weights for attention values (b_s, h, nq, nk).
        :return:
        """
        b_s, nq = queries.shape[:2]
        nk = keys.shape[1]
        q = self.fc_q(queries).view(b_s, nq, self.h, self.d_k).permute(0, 2,
            1, 3)
        k = self.fc_k(keys).view(b_s, nk, self.h, self.d_k).permute(0, 2, 3, 1)
        v = self.fc_v(values).view(b_s, nk, self.h, self.d_v).permute(0, 2,
            1, 3)
        att = torch.matmul(q, k) / np.sqrt(self.d_k)
        if attention_weights is not None:
            att = att * attention_weights
        if attention_mask is not None:
            att = att.masked_fill(attention_mask, -np.inf)
        att = torch.softmax(att, -1)
        att = self.dropout(att)
        out = torch.matmul(att, v).permute(0, 2, 1, 3).contiguous().view(b_s,
            nq, self.h * self.d_v)
        out = self.fc_o(out)
        return out


class PositionAttentionModuleNew(nn.Module):

    def __init__(self, d_model=512, kernel_size=3, H=7, W=7):
        super().__init__()
        self.cnn = nn.Conv2d(d_model, d_model, kernel_size=kernel_size,
            padding=(kernel_size - 1) // 2)
        self.pa = ScaledDotProductAttention(d_model, d_k=d_model, d_v=
            d_model, h=1)

    def forward(self, input_0):
        primals_2 = self.cnn.weight
        primals_3 = self.cnn.bias
        primals_4 = self.pa.fc_q.weight
        primals_5 = self.pa.fc_q.bias
        primals_6 = self.pa.fc_k.weight
        primals_7 = self.pa.fc_k.bias
        primals_8 = self.pa.fc_v.weight
        primals_9 = self.pa.fc_v.bias
        primals_10 = self.pa.fc_o.weight
        primals_11 = self.pa.fc_o.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])
        return output[0]


Measured latency_ms: 0.118041