import torch
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
import torch.nn.functional as F
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(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 144
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x1 = xindex // 6 % 6
    x0 = xindex % 6
    x2 = xindex // 36
    x5 = xindex
    tmp0 = -1 + x1
    tmp1 = tl.full([1], 0, tl.int64)
    tmp2 = tmp0 >= tmp1
    tmp3 = tl.full([1], 4, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp5 = -1 + x0
    tmp6 = tmp5 >= tmp1
    tmp7 = tmp5 < tmp3
    tmp8 = tmp2 & tmp4
    tmp9 = tmp8 & tmp6
    tmp10 = tmp9 & tmp7
    tmp11 = tmp0.to(tl.float32)
    tmp12 = 1.0
    tmp13 = tmp11 * tmp12
    tmp14 = tmp13.to(tl.int32)
    tmp15 = tl.full([XBLOCK], 4, tl.int32)
    tmp16 = tmp14 + tmp15
    tmp17 = tmp14 < 0
    tmp18 = tl.where(tmp17, tmp16, tmp14)
    tmp19 = tmp5.to(tl.float32)
    tmp20 = tmp19 * tmp12
    tmp21 = tmp20.to(tl.int32)
    tmp22 = tmp21 + tmp15
    tmp23 = tmp21 < 0
    tmp24 = tl.where(tmp23, tmp22, tmp21)
    tmp25 = tl.load(in_ptr0 + (tmp24 + 4 * tmp18 + 64 * x2), tmp10 & xmask,
        eviction_policy='evict_last', other=0.0)
    tl.store(out_ptr0 + x5, tmp25, xmask)


@triton.jit
def triton_poi_fused_mul_pow_sub_1(in_ptr0, in_ptr1, out_ptr0, ynumel,
    xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.constexpr):
    ynumel = 36
    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
    x2 = xindex % 4
    x3 = xindex // 4
    y0 = yindex % 9
    y1 = yindex // 9
    x4 = xindex
    y5 = yindex
    tmp0 = tl.load(in_ptr0 + (x2 + 6 * x3 + 6 * (y0 // 3) + 36 * y1 + y0 % 
        3), xmask & ymask, eviction_policy='evict_last')
    tmp1 = x3
    tmp2 = tmp1.to(tl.float32)
    tmp3 = 1.0
    tmp4 = tmp2 * tmp3
    tmp5 = tmp4.to(tl.int32)
    tmp6 = x2
    tmp7 = tmp6.to(tl.float32)
    tmp8 = tmp7 * tmp3
    tmp9 = tmp8.to(tl.int32)
    tmp10 = tl.load(in_ptr1 + (tmp9 + 4 * tmp5 + 64 * y1), xmask & ymask,
        eviction_policy='evict_last')
    tmp11 = tmp0 - tmp10
    tmp12 = tmp11 * tmp11
    tmp13 = -1.0
    tmp14 = tmp12 * tmp13
    tmp15 = tmp14 * tmp3
    tl.store(out_ptr0 + (x4 + 16 * y5), tmp15, xmask & ymask)


@triton.jit
def triton_poi_fused_im2col_2(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 144
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x1 = xindex // 6 % 6
    x0 = xindex % 6
    x2 = xindex // 36
    x5 = xindex
    tmp0 = -1 + x1
    tmp1 = tl.full([1], 0, tl.int64)
    tmp2 = tmp0 >= tmp1
    tmp3 = tl.full([1], 4, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp5 = -1 + x0
    tmp6 = tmp5 >= tmp1
    tmp7 = tmp5 < tmp3
    tmp8 = tmp2 & tmp4
    tmp9 = tmp8 & tmp6
    tmp10 = tmp9 & tmp7
    tmp11 = tmp0.to(tl.float32)
    tmp12 = 1.0
    tmp13 = tmp11 * tmp12
    tmp14 = tmp13.to(tl.int32)
    tmp15 = tl.full([XBLOCK], 4, tl.int32)
    tmp16 = tmp14 + tmp15
    tmp17 = tmp14 < 0
    tmp18 = tl.where(tmp17, tmp16, tmp14)
    tmp19 = tmp5.to(tl.float32)
    tmp20 = tmp19 * tmp12
    tmp21 = tmp20.to(tl.int32)
    tmp22 = tmp21 + tmp15
    tmp23 = tmp21 < 0
    tmp24 = tl.where(tmp23, tmp22, tmp21)
    tmp25 = tl.load(in_ptr0 + (16 + tmp24 + 4 * tmp18 + 64 * x2), tmp10 &
        xmask, eviction_policy='evict_last', other=0.0)
    tl.store(out_ptr0 + x5, tmp25, xmask)


@triton.jit
def triton_poi_fused_im2col_3(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 144
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x1 = xindex // 6 % 6
    x0 = xindex % 6
    x2 = xindex // 36
    x5 = xindex
    tmp0 = -1 + x1
    tmp1 = tl.full([1], 0, tl.int64)
    tmp2 = tmp0 >= tmp1
    tmp3 = tl.full([1], 4, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp5 = -1 + x0
    tmp6 = tmp5 >= tmp1
    tmp7 = tmp5 < tmp3
    tmp8 = tmp2 & tmp4
    tmp9 = tmp8 & tmp6
    tmp10 = tmp9 & tmp7
    tmp11 = tmp0.to(tl.float32)
    tmp12 = 1.0
    tmp13 = tmp11 * tmp12
    tmp14 = tmp13.to(tl.int32)
    tmp15 = tl.full([XBLOCK], 4, tl.int32)
    tmp16 = tmp14 + tmp15
    tmp17 = tmp14 < 0
    tmp18 = tl.where(tmp17, tmp16, tmp14)
    tmp19 = tmp5.to(tl.float32)
    tmp20 = tmp19 * tmp12
    tmp21 = tmp20.to(tl.int32)
    tmp22 = tmp21 + tmp15
    tmp23 = tmp21 < 0
    tmp24 = tl.where(tmp23, tmp22, tmp21)
    tmp25 = tl.load(in_ptr0 + (32 + tmp24 + 4 * tmp18 + 64 * x2), tmp10 &
        xmask, eviction_policy='evict_last', other=0.0)
    tl.store(out_ptr0 + x5, tmp25, xmask)


@triton.jit
def triton_per_fused__softmax_mul_sum_4(in_ptr0, in_ptr1, in_ptr2, out_ptr2,
    out_ptr3, xnumel, rnumel, XBLOCK: tl.constexpr):
    xnumel = 64
    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
    x0 = xindex % 16
    x1 = xindex // 16
    x3 = xindex % 4
    x4 = xindex // 4 % 4
    tmp0 = tl.load(in_ptr0 + (x0 + 16 * r2 + 144 * x1), rmask & xmask,
        other=0.0)
    tmp14 = tl.load(in_ptr1 + (x3 + 6 * x4 + 6 * (r2 // 3) + 36 * x1 + r2 %
        3), rmask & xmask, other=0.0)
    tmp20 = tl.load(in_ptr2 + (x3 + 6 * x4 + 6 * (r2 // 3) + 36 * x1 + r2 %
        3), 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 = 1.0
    tmp7 = tmp5 * tmp6
    tmp8 = tl_math.exp(tmp7)
    tmp9 = tl.broadcast_to(tmp8, [XBLOCK, RBLOCK])
    tmp11 = tl.where(rmask & xmask, tmp9, 0)
    tmp12 = tl.sum(tmp11, 1)[:, None]
    tmp13 = tmp8 / tmp12
    tmp15 = tmp13 * tmp14
    tmp16 = tl.broadcast_to(tmp15, [XBLOCK, RBLOCK])
    tmp18 = tl.where(rmask & xmask, tmp16, 0)
    tmp19 = tl.sum(tmp18, 1)[:, None]
    tmp21 = tmp13 * tmp20
    tmp22 = tl.broadcast_to(tmp21, [XBLOCK, RBLOCK])
    tmp24 = tl.where(rmask & xmask, tmp22, 0)
    tmp25 = tl.sum(tmp24, 1)[:, None]
    tl.store(out_ptr2 + (x0 + 32 * x1), tmp19, xmask)
    tl.store(out_ptr3 + (x0 + 32 * x1), tmp25, xmask)


def call(args):
    arg0_1, = args
    args.clear()
    assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 1, 6, 6), (36, 144, 6, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_im2col_0[grid(144)](arg0_1, buf0, 144, XBLOCK=256,
            num_warps=4, num_stages=1)
        buf1 = empty_strided_cuda((4, 9, 4, 4), (144, 16, 4, 1), torch.float32)
        triton_poi_fused_mul_pow_sub_1[grid(36, 16)](buf0, arg0_1, buf1, 36,
            16, XBLOCK=16, YBLOCK=64, num_warps=4, num_stages=1)
        buf4 = buf0
        del buf0
        triton_poi_fused_im2col_2[grid(144)](arg0_1, buf4, 144, XBLOCK=256,
            num_warps=4, num_stages=1)
        buf6 = empty_strided_cuda((4, 1, 6, 6), (36, 144, 6, 1), torch.float32)
        triton_poi_fused_im2col_3[grid(144)](arg0_1, buf6, 144, XBLOCK=128,
            num_warps=4, num_stages=1)
        del arg0_1
        buf8 = empty_strided_cuda((4, 2, 4, 4), (32, 16, 4, 1), torch.float32)
        buf5 = reinterpret_tensor(buf8, (4, 1, 4, 4), (32, 16, 4, 1), 0)
        buf7 = reinterpret_tensor(buf8, (4, 1, 4, 4), (32, 16, 4, 1), 16)
        triton_per_fused__softmax_mul_sum_4[grid(64)](buf1, buf4, buf6,
            buf5, buf7, 64, 9, XBLOCK=8, num_warps=2, num_stages=1)
        del buf1
        del buf4
        del buf6
    return buf8,


def find_local_patch(x, patch_size):
    N, _C, H, W = x.shape
    x_unfold = F.unfold(x, kernel_size=(patch_size, patch_size), padding=(
        patch_size // 2, patch_size // 2), stride=(1, 1))
    return x_unfold.view(N, x_unfold.shape[1], H, W)


class WeightedAverageNew(nn.Module):

    def __init__(self):
        super(WeightedAverageNew, self).__init__()

    def forward(self, input_0):
        arg0_1 = input_0
        output = call([arg0_1])
        return output[0]


Measured latency_ms: 0.068681