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 torch.nn as nn
import torch.nn.parallel
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_im2col_0(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 % 4
    x1 = xindex // 4
    x2 = xindex
    tmp0 = x0 + x1
    tl.store(out_ptr0 + x2, tmp0, xmask)


@triton.jit
def triton_poi_fused_avg_pool2d_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.
    constexpr):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + x0, xmask)
    tmp1 = 1.0
    tmp2 = tmp0 * tmp1
    tl.store(out_ptr0 + x0, tmp2, xmask)


@triton.jit
def triton_per_fused__softmax_mul_2(in_ptr0, in_ptr1, out_ptr2, xnumel,
    rnumel, XBLOCK: tl.constexpr):
    xnumel = 2304
    rnumel = 9
    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, :]
    rmask = rindex < rnumel
    r2 = rindex
    x7 = xindex
    x0 = xindex % 36
    x3 = xindex % 9
    x4 = xindex // 9 % 4
    x5 = xindex // 36 % 16
    x6 = xindex // 576
    tmp0 = tl.load(in_ptr0 + (r2 + 9 * x7), rmask & xmask, other=0.0)
    tmp1 = tl.load(in_ptr1 + (r2 + 9 * x0), rmask & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp2 = tmp0 + tmp1
    tmp3 = 1.0
    tmp4 = tmp2 * tmp3
    tmp5 = tl.broadcast_to(tmp4, [XBLOCK, RBLOCK])
    tmp7 = tl.where(rmask & xmask, tmp5, float('-inf'))
    tmp8 = triton_helpers.max2(tmp7, 1)[:, None]
    tmp9 = tmp4 - tmp8
    tmp10 = tl_math.exp(tmp9)
    tmp11 = tl.broadcast_to(tmp10, [XBLOCK, RBLOCK])
    tmp13 = tl.where(rmask & xmask, tmp11, 0)
    tmp14 = tl.sum(tmp13, 1)[:, None]
    tmp15 = tmp10 / tmp14
    tl.store(out_ptr2 + (r2 + 9 * x3 + 81 * x5 + 1312 * x4 + 5248 * x6),
        tmp15, rmask & xmask)


@triton.jit
def triton_poi_fused_clone_3(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl
    .constexpr):
    xnumel = 2304
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 9
    x1 = xindex // 9 % 16
    x2 = xindex // 144 % 4
    x3 = xindex // 576
    x5 = xindex
    tmp0 = tl.load(in_ptr0 + (4 * (x0 // 3) + x1 // 4), xmask,
        eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr0 + (4 * (x0 % 3) + x1 % 4), xmask,
        eviction_policy='evict_last')
    tmp1 = tl.full([XBLOCK], 6, tl.int32)
    tmp2 = tmp0 + tmp1
    tmp3 = tmp0 < 0
    tmp4 = tl.where(tmp3, tmp2, tmp0)
    tl.device_assert((0 <= tmp4) & (tmp4 < 6) | ~xmask,
        'index out of bounds: 0 <= tmp4 < 6')
    tmp7 = tmp6 + tmp1
    tmp8 = tmp6 < 0
    tmp9 = tl.where(tmp8, tmp7, tmp6)
    tl.device_assert((0 <= tmp9) & (tmp9 < 6) | ~xmask,
        'index out of bounds: 0 <= tmp9 < 6')
    tmp11 = -1 + tmp4
    tmp12 = tl.full([1], 0, tl.int64)
    tmp13 = tmp11 >= tmp12
    tmp14 = tl.full([1], 4, tl.int64)
    tmp15 = tmp11 < tmp14
    tmp16 = -1 + tmp9
    tmp17 = tmp16 >= tmp12
    tmp18 = tmp16 < tmp14
    tmp19 = tmp13 & tmp15
    tmp20 = tmp19 & tmp17
    tmp21 = tmp20 & tmp18
    tmp22 = tl.load(in_ptr1 + (-20 + x2 + 4 * tmp9 + 16 * tmp4 + 64 * x3), 
        tmp21 & xmask, eviction_policy='evict_last', other=0.0)
    tl.store(out_ptr0 + x5, tmp22, xmask)


@triton.jit
def triton_poi_fused_bmm_4(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 20736
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 81
    x1 = xindex // 81
    x2 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 81 * (x1 % 16) + 1312 * (x1 // 16)), xmask)
    tl.store(out_ptr0 + x2, tmp0, xmask)


@triton.jit
def triton_poi_fused_col2im_5(out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 576
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = 0.0
    tl.store(out_ptr0 + x0, tmp0, xmask)


@triton.jit
def triton_poi_fused_col2im_6(in_ptr0, in_ptr1, out_ptr0, ynumel, xnumel,
    YBLOCK: tl.constexpr, XBLOCK: tl.constexpr):
    ynumel = 576
    xnumel = 4
    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
    y5 = yindex // 3 % 12
    x4 = xindex
    y0 = yindex % 3
    y1 = yindex // 3 % 4
    y2 = yindex // 12 % 3
    y3 = yindex // 36
    tmp0 = tl.load(in_ptr0 + y5, ymask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr0 + (x4 + 4 * y0), xmask & ymask, eviction_policy=
        'evict_last')
    tmp11 = tl.load(in_ptr1 + (y0 + 3 * y2 + 9 * x4 + 36 * y1 + 144 * y3 + 
        144 * ((y0 + 3 * y2) // 9)), xmask & ymask, eviction_policy=
        'evict_last')
    tmp1 = tl.full([XBLOCK, YBLOCK], 6, tl.int32)
    tmp2 = tmp0 + tmp1
    tmp3 = tmp0 < 0
    tmp4 = tl.where(tmp3, tmp2, tmp0)
    tl.device_assert((0 <= tmp4) & (tmp4 < 6) | ~ymask,
        'index out of bounds: 0 <= tmp4 < 6')
    tmp7 = tmp6 + tmp1
    tmp8 = tmp6 < 0
    tmp9 = tl.where(tmp8, tmp7, tmp6)
    tl.device_assert((0 <= tmp9) & (tmp9 < 6) | ~(xmask & ymask),
        'index out of bounds: 0 <= tmp9 < 6')
    tl.atomic_add(out_ptr0 + tl.broadcast_to(tmp9 + 6 * tmp4 + 36 * y3, [
        XBLOCK, YBLOCK]), tmp11, xmask & ymask, sem='relaxed')


@triton.jit
def triton_poi_fused_clone_7(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl.
    constexpr, XBLOCK: tl.constexpr):
    ynumel = 64
    xnumel = 4
    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
    y1 = yindex // 4 % 4
    y0 = yindex % 4
    x3 = xindex
    y2 = yindex // 16
    y5 = yindex
    tmp0 = 1 + y1
    tmp1 = tl.full([1, 1], 0, tl.int64)
    tmp2 = tmp0 >= tmp1
    tmp3 = tl.full([1, 1], 6, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp5 = 1 + y0
    tmp6 = tmp5 >= tmp1
    tmp7 = tmp5 < tmp3
    tmp8 = tmp2 & tmp4
    tmp9 = tmp8 & tmp6
    tmp10 = tmp9 & tmp7
    tmp11 = tl.load(in_ptr0 + (7 + y0 + 6 * y1 + 36 * x3 + 144 * y2), tmp10 &
        xmask & ymask, eviction_policy='evict_last', other=0.0)
    tl.store(out_ptr0 + (x3 + 4 * y5), tmp11, xmask & ymask)


@triton.jit
def triton_poi_fused_add_8(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex
    x0 = xindex % 4
    tmp0 = tl.load(in_out_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tl.store(in_out_ptr0 + x2, tmp2, xmask)


def call(args):
    primals_1, primals_2, primals_3, primals_4, primals_5, primals_6 = args
    args.clear()
    assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_2, (4, 4), (4, 1))
    assert_size_stride(primals_3, (324, 4), (4, 1))
    assert_size_stride(primals_4, (324,), (1,))
    assert_size_stride(primals_5, (4, 4), (4, 1))
    assert_size_stride(primals_6, (4,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(primals_1, (64, 4), (4, 1), 0),
            reinterpret_tensor(primals_2, (4, 4), (1, 4), 0), out=buf0)
        del primals_2
        buf1 = empty_strided_cuda((3, 4), (4, 1), torch.int64)
        get_raw_stream(0)
        triton_poi_fused_im2col_0[grid(12)](buf1, 12, XBLOCK=16, num_warps=
            1, num_stages=1)
        buf2 = empty_strided_cuda((4, 4, 4, 4), (64, 1, 16, 4), torch.float32)
        triton_poi_fused_avg_pool2d_1[grid(256)](primals_1, buf2, 256,
            XBLOCK=256, num_warps=4, num_stages=1)
        buf3 = empty_strided_cuda((64, 324), (324, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(buf2, (64, 4), (4, 1), 0),
            reinterpret_tensor(primals_3, (4, 324), (1, 4), 0), out=buf3)
        del primals_3
        buf6 = empty_strided_cuda((4, 4, 16, 9, 9), (5248, 1312, 81, 9, 1),
            torch.float32)
        triton_per_fused__softmax_mul_2[grid(2304)](buf3, primals_4, buf6, 
            2304, 9, XBLOCK=8, num_warps=2, num_stages=1)
        del primals_4
        buf7 = empty_strided_cuda((4, 4, 16, 9, 1), (576, 144, 9, 1, 1),
            torch.float32)
        triton_poi_fused_clone_3[grid(2304)](buf1, buf0, buf7, 2304, XBLOCK
            =256, num_warps=4, num_stages=1)
        buf8 = reinterpret_tensor(buf3, (256, 9, 9), (81, 9, 1), 0)
        del buf3
        triton_poi_fused_bmm_4[grid(20736)](buf6, buf8, 20736, XBLOCK=256,
            num_warps=4, num_stages=1)
        buf9 = empty_strided_cuda((256, 9, 1), (9, 1, 1), torch.float32)
        extern_kernels.bmm(buf8, reinterpret_tensor(buf7, (256, 9, 1), (9, 
            1, 0), 0), out=buf9)
        del buf8
        buf10 = empty_strided_cuda((4, 4, 6, 6), (144, 36, 6, 1), torch.float32
            )
        triton_poi_fused_col2im_5[grid(576)](buf10, 576, XBLOCK=256,
            num_warps=4, num_stages=1)
        buf11 = empty_strided_cuda((4, 4, 6, 6), (144, 36, 6, 1), torch.float32
            )
        triton_poi_fused_col2im_5[grid(576)](buf11, 576, XBLOCK=256,
            num_warps=4, num_stages=1)
        triton_poi_fused_col2im_6[grid(576, 4)](buf1, buf9, buf11, 576, 4,
            XBLOCK=1, YBLOCK=256, num_warps=4, num_stages=1)
        del buf9
        buf13 = reinterpret_tensor(buf0, (4, 4, 4, 4), (64, 16, 4, 1), 0)
        del buf0
        triton_poi_fused_clone_7[grid(64, 4)](buf11, buf13, 64, 4, XBLOCK=4,
            YBLOCK=32, num_warps=4, num_stages=1)
        del buf11
        buf14 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(buf13, (64, 4), (4, 1), 0),
            reinterpret_tensor(primals_5, (4, 4), (1, 4), 0), out=buf14)
        buf15 = reinterpret_tensor(buf14, (4, 4, 4, 4), (64, 16, 4, 1), 0)
        del buf14
        triton_poi_fused_add_8[grid(256)](buf15, primals_6, 256, XBLOCK=256,
            num_warps=4, num_stages=1)
        del primals_6
    return buf15, reinterpret_tensor(primals_1, (64, 4), (4, 1), 0
        ), buf1, reinterpret_tensor(buf2, (64, 4), (4, 1), 0
        ), buf6, buf10, reinterpret_tensor(buf13, (64, 4), (4, 1), 0
        ), primals_5, reinterpret_tensor(buf7, (256, 1, 9), (9, 1, 1), 0)


class OutlookAttentionNew(nn.Module):
    """
    Implementation of outlook attention
    --dim: hidden dim
    --num_heads: number of heads
    --kernel_size: kernel size in each window for outlook attention
    return: token features after outlook attention
    """

    def __init__(self, dim, num_heads, kernel_size=3, padding=1, stride=1,
        qkv_bias=False, qk_scale=None, attn_drop=0.0, proj_drop=0.0):
        super().__init__()
        head_dim = dim // num_heads
        self.num_heads = num_heads
        self.kernel_size = kernel_size
        self.padding = padding
        self.stride = stride
        self.scale = qk_scale or head_dim ** -0.5
        self.v = nn.Linear(dim, dim, bias=qkv_bias)
        self.attn = nn.Linear(dim, kernel_size ** 4 * num_heads)
        self.attn_drop = nn.Dropout(attn_drop)
        self.proj = nn.Linear(dim, dim)
        self.proj_drop = nn.Dropout(proj_drop)
        self.unfold = nn.Unfold(kernel_size=kernel_size, padding=padding,
            stride=stride)
        self.pool = nn.AvgPool2d(kernel_size=stride, stride=stride,
            ceil_mode=True)

    def forward(self, input_0):
        primals_2 = self.v.weight
        primals_3 = self.attn.weight
        primals_4 = self.attn.bias
        primals_5 = self.proj.weight
        primals_6 = self.proj.bias
        primals_1 = input_0
        output = call([primals_1, primals_2, primals_3, primals_4,
            primals_5, primals_6])
        return output[0]


Measured latency_ms: 0.071143