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
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_per_fused_mean_0(in_ptr0, out_ptr0, xnumel, rnumel, XBLOCK: tl.
    constexpr):
    xnumel = 16
    RBLOCK: tl.constexpr = 16
    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)
    r1 = rindex
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + (r1 + 16 * x0), 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 + x0, tmp4, xmask)


@triton.jit
def triton_poi_fused_add_cat_1(in_ptr0, in_ptr1, in_ptr2, out_ptr0, xnumel,
    XBLOCK: tl.constexpr):
    xnumel = 272
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex // 16
    x3 = xindex % 16
    x0 = xindex % 4
    x4 = xindex
    tmp15 = tl.load(in_ptr2 + (x0 + 4 * x2), xmask, eviction_policy=
        'evict_last')
    tmp0 = x2
    tl.full([1], 0, tl.int64)
    tmp3 = tl.full([1], 1, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp5 = tl.load(in_ptr0 + x3, tmp4 & xmask, eviction_policy='evict_last',
        other=0.0)
    tmp6 = 16.0
    tmp7 = tmp5 / tmp6
    tmp8 = tl.full(tmp7.shape, 0.0, tmp7.dtype)
    tmp9 = tl.where(tmp4, tmp7, tmp8)
    tmp10 = tmp0 >= tmp3
    tl.full([1], 17, tl.int64)
    tmp13 = tl.load(in_ptr1 + (16 * x3 + (-1 + x2)), tmp10 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp14 = tl.where(tmp4, tmp9, tmp13)
    tmp16 = tmp14 + tmp15
    tl.store(out_ptr0 + x4, tmp16, xmask)


@triton.jit
def triton_poi_fused_cat_2(in_ptr0, in_ptr1, in_ptr2, out_ptr0, xnumel,
    XBLOCK: tl.constexpr):
    xnumel = 12
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = x0
    tl.full([1], 0, tl.int64)
    tmp3 = tl.full([1], 4, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp5 = tl.load(in_ptr0 + x0, tmp4 & xmask, eviction_policy='evict_last',
        other=0.0)
    tmp6 = tmp0 >= tmp3
    tmp7 = tl.full([1], 8, tl.int64)
    tmp8 = tmp0 < tmp7
    tmp9 = tmp6 & tmp8
    tmp10 = tl.load(in_ptr1 + (-4 + x0), tmp9 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp11 = tmp0 >= tmp7
    tl.full([1], 12, tl.int64)
    tmp14 = tl.load(in_ptr2 + (-8 + x0), tmp11 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp15 = tl.where(tmp9, tmp10, tmp14)
    tmp16 = tl.where(tmp4, tmp5, tmp15)
    tl.store(out_ptr0 + x0, tmp16, xmask)


@triton.jit
def triton_poi_fused_mul_transpose_3(in_ptr0, in_ptr1, in_ptr2, in_ptr3,
    out_ptr0, out_ptr1, ynumel, xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.
    constexpr):
    ynumel = 16
    xnumel = 17
    yoffset = tl.program_id(1) * YBLOCK
    yindex = yoffset + tl.arange(0, YBLOCK)[None, :]
    ymask = yindex < ynumel
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    x2 = xindex
    y3 = yindex
    y0 = yindex % 4
    tmp0 = tl.load(in_ptr0 + (y3 + 16 * x2), xmask & ymask, eviction_policy
        ='evict_last')
    tmp1 = y0
    tl.full([1, 1], 0, tl.int64)
    tmp4 = tl.full([1, 1], 4, tl.int64)
    tmp5 = tmp1 < tmp4
    tmp6 = tl.load(in_ptr1 + tl.broadcast_to(y0, [XBLOCK, YBLOCK]), tmp5 &
        xmask & ymask, eviction_policy='evict_last', other=0.0)
    tmp7 = tmp1 >= tmp4
    tmp8 = tl.full([1, 1], 8, tl.int64)
    tmp9 = tmp1 < tmp8
    tmp10 = tmp7 & tmp9
    tmp11 = tl.load(in_ptr2 + tl.broadcast_to(-4 + y0, [XBLOCK, YBLOCK]), 
        tmp10 & xmask & ymask, eviction_policy='evict_last', other=0.0)
    tmp12 = tmp1 >= tmp8
    tl.full([1, 1], 12, tl.int64)
    tmp15 = tl.load(in_ptr3 + tl.broadcast_to(-8 + y0, [XBLOCK, YBLOCK]), 
        tmp12 & xmask & ymask, eviction_policy='evict_last', other=0.0)
    tmp16 = tl.where(tmp10, tmp11, tmp15)
    tmp17 = tl.where(tmp5, tmp6, tmp16)
    tmp18 = tmp0 + tmp17
    tmp19 = 1.0
    tmp20 = tmp18 * tmp19
    tl.store(out_ptr0 + (x2 + 17 * y3), tmp20, xmask & ymask)
    tl.store(out_ptr1 + (y3 + 16 * x2), tmp20, xmask & ymask)


@triton.jit
def triton_poi_fused_mul_transpose_4(in_ptr0, in_ptr1, in_ptr2, in_ptr3,
    out_ptr0, out_ptr1, ynumel, xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.
    constexpr):
    ynumel = 16
    xnumel = 17
    yoffset = tl.program_id(1) * YBLOCK
    yindex = yoffset + tl.arange(0, YBLOCK)[None, :]
    ymask = yindex < ynumel
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    x2 = xindex
    y3 = yindex
    y0 = yindex % 4
    tmp0 = tl.load(in_ptr0 + (y3 + 16 * x2), xmask & ymask, eviction_policy
        ='evict_last')
    tmp1 = 4 + y0
    tl.full([1, 1], 0, tl.int64)
    tmp4 = tl.full([1, 1], 4, tl.int64)
    tmp5 = tmp1 < tmp4
    tmp6 = tl.load(in_ptr1 + tl.broadcast_to(4 + y0, [XBLOCK, YBLOCK]), 
        tmp5 & xmask & ymask, eviction_policy='evict_last', other=0.0)
    tmp7 = tmp1 >= tmp4
    tmp8 = tl.full([1, 1], 8, tl.int64)
    tmp9 = tmp1 < tmp8
    tmp10 = tmp7 & tmp9
    tmp11 = tl.load(in_ptr2 + tl.broadcast_to(y0, [XBLOCK, YBLOCK]), tmp10 &
        xmask & ymask, eviction_policy='evict_last', other=0.0)
    tmp12 = tmp1 >= tmp8
    tl.full([1, 1], 12, tl.int64)
    tmp15 = tl.load(in_ptr3 + tl.broadcast_to(-4 + y0, [XBLOCK, YBLOCK]), 
        tmp12 & xmask & ymask, eviction_policy='evict_last', other=0.0)
    tmp16 = tl.where(tmp10, tmp11, tmp15)
    tmp17 = tl.where(tmp5, tmp6, tmp16)
    tmp18 = tmp0 + tmp17
    tmp19 = 1.0
    tmp20 = tmp18 * tmp19
    tl.store(out_ptr0 + (x2 + 17 * y3), tmp20, xmask & ymask)
    tl.store(out_ptr1 + (y3 + 16 * x2), tmp20, xmask & ymask)


@triton.jit
def triton_per_fused__safe_softmax_5(in_ptr0, out_ptr3, xnumel, rnumel,
    XBLOCK: tl.constexpr):
    xnumel = 272
    rnumel = 17
    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, :]
    rmask = rindex < rnumel
    r1 = rindex
    x0 = xindex
    x2 = xindex % 68
    x3 = xindex // 68
    tmp0 = tl.load(in_ptr0 + (r1 + 17 * x0), rmask & xmask, other=0.0)
    tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
    tmp3 = tl.where(rmask & xmask, tmp1, float('-inf'))
    tmp4 = triton_helpers.max2(tmp3, 1)[:, None]
    tmp5 = tmp0 - tmp4
    tmp6 = tl_math.exp(tmp5)
    tmp7 = tl.broadcast_to(tmp6, [XBLOCK, RBLOCK])
    tmp9 = tl.where(rmask & xmask, tmp7, 0)
    tmp10 = tl.sum(tmp9, 1)[:, None]
    tmp11 = float('-inf')
    tmp12 = tmp0 == tmp11
    tmp13 = tmp12 == 0
    tmp14 = tmp13.to(tl.int64)
    tmp15 = tmp14 != 0
    tmp16 = tl.broadcast_to(tmp15, [XBLOCK, RBLOCK])
    tmp18 = tl.where(rmask & xmask, tmp16, 0)
    tmp19 = triton_helpers.any(tmp18, 1)[:, None]
    tmp20 = tmp19 == 0
    tmp21 = tmp6 / tmp10
    tmp22 = 0.0
    tmp23 = tl.where(tmp20, tmp22, tmp21)
    tl.store(out_ptr3 + (r1 + 17 * x2 + 1184 * x3), tmp23, rmask & xmask)


@triton.jit
def triton_poi_fused_bmm_6(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 4624
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 289
    x1 = xindex // 289
    x2 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 289 * (x1 % 4) + 1184 * (x1 // 4)), xmask)
    tl.store(out_ptr0 + x2, tmp0, xmask)


@triton.jit
def triton_poi_fused_clone_7(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl.
    constexpr, XBLOCK: tl.constexpr):
    ynumel = 17
    xnumel = 16
    yoffset = tl.program_id(1) * YBLOCK
    yindex = yoffset + tl.arange(0, YBLOCK)[None, :]
    ymask = yindex < ynumel
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    x1 = xindex
    y0 = yindex
    tmp0 = tl.load(in_ptr0 + (y0 + 17 * x1), xmask & ymask, eviction_policy
        ='evict_last')
    tl.store(out_ptr0 + (x1 + 16 * y0), tmp0, xmask & ymask)


def call(args):
    (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6,
        primals_7, primals_8, primals_9, primals_10) = args
    args.clear()
    assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_2, (17, 4), (4, 1))
    assert_size_stride(primals_3, (4, 4), (4, 1))
    assert_size_stride(primals_4, (4, 4), (4, 1))
    assert_size_stride(primals_5, (4, 4), (4, 1))
    assert_size_stride(primals_6, (4,), (1,))
    assert_size_stride(primals_7, (4,), (1,))
    assert_size_stride(primals_8, (4,), (1,))
    assert_size_stride(primals_9, (4, 4), (4, 1))
    assert_size_stride(primals_10, (4,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((1, 4, 4), (16, 4, 1), torch.float32)
        get_raw_stream(0)
        triton_per_fused_mean_0[grid(16)](primals_1, buf0, 16, 16, XBLOCK=8,
            num_warps=2, num_stages=1)
        buf1 = empty_strided_cuda((17, 4, 4), (16, 4, 1), torch.float32)
        triton_poi_fused_add_cat_1[grid(272)](buf0, primals_1, primals_2,
            buf1, 272, XBLOCK=128, num_warps=4, num_stages=1)
        del buf0
        del primals_1
        del primals_2
        buf2 = empty_strided_cuda((68, 4), (4, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(buf1, (68, 4), (4, 1), 0),
            reinterpret_tensor(primals_3, (4, 4), (1, 4), 0), out=buf2)
        buf3 = empty_strided_cuda((68, 4), (4, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(buf1, (68, 4), (4, 1), 0),
            reinterpret_tensor(primals_4, (4, 4), (1, 4), 0), out=buf3)
        buf4 = empty_strided_cuda((12,), (1,), torch.float32)
        triton_poi_fused_cat_2[grid(12)](primals_6, primals_7, primals_8,
            buf4, 12, XBLOCK=16, num_warps=1, num_stages=1)
        buf5 = empty_strided_cuda((68, 4), (4, 1), torch.float32)
        extern_kernels.addmm(reinterpret_tensor(buf4, (4,), (1,), 8),
            reinterpret_tensor(buf1, (68, 4), (4, 1), 0),
            reinterpret_tensor(primals_5, (4, 4), (1, 4), 0), alpha=1, beta
            =1, out=buf5)
        del buf4
        buf6 = empty_strided_cuda((4, 4, 17, 1), (68, 17, 1, 1), torch.float32)
        buf17 = empty_strided_cuda((16, 1, 17), (1, 1, 16), torch.float32)
        triton_poi_fused_mul_transpose_3[grid(16, 17)](buf2, primals_6,
            primals_7, primals_8, buf6, buf17, 16, 17, XBLOCK=32, YBLOCK=8,
            num_warps=4, num_stages=1)
        buf7 = reinterpret_tensor(buf2, (4, 4, 1, 17), (68, 17, 17, 1), 0)
        del buf2
        buf18 = empty_strided_cuda((16, 17, 1), (1, 16, 1), torch.float32)
        triton_poi_fused_mul_transpose_4[grid(16, 17)](buf3, primals_6,
            primals_7, primals_8, buf7, buf18, 16, 17, XBLOCK=32, YBLOCK=8,
            num_warps=4, num_stages=1)
        del buf3
        del primals_6
        del primals_7
        del primals_8
        buf8 = empty_strided_cuda((16, 17, 17), (289, 17, 1), torch.float32)
        extern_kernels.bmm(reinterpret_tensor(buf6, (16, 17, 1), (17, 1, 0),
            0), reinterpret_tensor(buf7, (16, 1, 17), (17, 0, 1), 0), out=buf8)
        buf12 = empty_strided_cuda((4, 4, 17, 17), (1184, 289, 17, 1),
            torch.float32)
        triton_per_fused__safe_softmax_5[grid(272)](buf8, buf12, 272, 17,
            XBLOCK=1, num_warps=2, num_stages=1)
        buf13 = buf8
        del buf8
        triton_poi_fused_bmm_6[grid(4624)](buf12, buf13, 4624, XBLOCK=128,
            num_warps=4, num_stages=1)
        buf14 = reinterpret_tensor(buf7, (16, 17, 1), (17, 1, 1), 0)
        del buf7
        extern_kernels.bmm(buf13, reinterpret_tensor(buf5, (16, 17, 1), (1,
            16, 0), 0), out=buf14)
        del buf13
        buf15 = reinterpret_tensor(buf6, (17, 4, 4, 1), (16, 4, 1, 1), 0)
        del buf6
        triton_poi_fused_clone_7[grid(17, 16)](buf14, buf15, 17, 16, XBLOCK
            =16, YBLOCK=32, num_warps=4, num_stages=1)
        buf16 = reinterpret_tensor(buf14, (68, 4), (4, 1), 0)
        del buf14
        extern_kernels.addmm(primals_10, reinterpret_tensor(buf15, (68, 4),
            (4, 1), 0), reinterpret_tensor(primals_9, (4, 4), (1, 4), 0),
            alpha=1, beta=1, out=buf16)
        del primals_10
    return reinterpret_tensor(buf16, (4, 4), (4, 1), 0), reinterpret_tensor(
        buf1, (68, 4), (4, 1), 0), buf12, reinterpret_tensor(buf15, (68, 4),
        (4, 1), 0), primals_9, reinterpret_tensor(buf5, (16, 1, 17), (1, 1,
        16), 0), buf17, buf18, primals_5, primals_4, primals_3


class AttentionPool2dNew(nn.Module):

    def __init__(self, spacial_dim: 'int', embed_dim: 'int', num_heads:
        'int', output_dim: 'int'=None):
        super().__init__()
        self.positional_embedding = nn.Parameter(torch.randn(spacial_dim **
            2 + 1, embed_dim) / embed_dim ** 0.5)
        self.k_proj = nn.Linear(embed_dim, embed_dim)
        self.q_proj = nn.Linear(embed_dim, embed_dim)
        self.v_proj = nn.Linear(embed_dim, embed_dim)
        self.c_proj = nn.Linear(embed_dim, output_dim or embed_dim)
        self.num_heads = num_heads

    def forward(self, input_0):
        primals_2 = self.positional_embedding
        primals_3 = self.k_proj.weight
        primals_6 = self.k_proj.bias
        primals_4 = self.q_proj.weight
        primals_7 = self.q_proj.bias
        primals_5 = self.v_proj.weight
        primals_8 = self.v_proj.bias
        primals_9 = self.c_proj.weight
        primals_10 = self.c_proj.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])
        return output[0]


Measured latency_ms: 0.0309