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
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):
    ynumel = 16
    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
    y3 = yindex
    y0 = yindex % 4
    y1 = yindex // 4
    tmp0 = tl.load(in_ptr0 + (x2 + 16 * y3), xmask & ymask)
    tl.store(out_ptr0 + (y0 + 4 * x2 + 64 * y1), tmp0, xmask & ymask)


@triton.jit
def triton_poi_fused_1(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl.
    constexpr, XBLOCK: tl.constexpr):
    ynumel = 6400
    xnumel = 9
    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 % 64
    y1 = yindex // 64
    tmp0 = tl.load(in_ptr0 + (x2 + 9 * y3), xmask & ymask, eviction_policy=
        'evict_last')
    tl.store(out_ptr0 + (y0 + 64 * x2 + 576 * y1), tmp0, xmask & ymask)


@triton.jit
def triton_poi_fused_convolution_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 % 64
    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_per_fused__softmax_3(in_ptr0, in_ptr1, out_ptr0, out_ptr1,
    xnumel, rnumel, XBLOCK: tl.constexpr):
    xnumel = 256
    rnumel = 25
    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
    r3 = rindex
    x0 = xindex % 8
    x1 = xindex // 8 % 8
    x2 = xindex // 64
    x4 = xindex
    tmp0 = tl.load(in_ptr0 + (2 * (x1 % 2) + 4 * r3 + 100 * (x0 // 2) + 400 *
        (x1 // 2) + 1600 * x2 + x0 % 2), rmask & xmask, other=0.0)
    tmp1 = tl.load(in_ptr1 + (2 * (x1 % 2) + 4 * r3 + x0 % 2), rmask &
        xmask, eviction_policy='evict_last', other=0.0)
    tmp2 = tmp0 + tmp1
    tmp3 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK])
    tmp5 = tl.where(rmask & xmask, tmp3, float('-inf'))
    tmp6 = triton_helpers.max2(tmp5, 1)[:, None]
    tmp7 = tmp2 - 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]
    tl.store(out_ptr0 + x4, tmp6, xmask)
    tl.store(out_ptr1 + x4, tmp12, xmask)


@triton.jit
def triton_poi_fused__softmax_4(in_ptr0, in_ptr1, in_ptr2, in_ptr3,
    out_ptr0, ynumel, xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.constexpr):
    ynumel = 100
    xnumel = 64
    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 % 8
    x3 = xindex // 8
    y0 = yindex % 25
    y1 = yindex // 25
    x4 = xindex
    tmp0 = tl.load(in_ptr0 + (2 * (x3 % 2) + 4 * y0 + 100 * (x2 // 2) + 400 *
        (x3 // 2) + 1600 * y1 + x2 % 2), xmask & ymask, eviction_policy=
        'evict_last')
    tmp1 = tl.load(in_ptr1 + (2 * (x3 % 2) + 4 * y0 + x2 % 2), xmask &
        ymask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr2 + (x4 + 64 * y1), xmask & ymask, eviction_policy
        ='evict_last')
    tmp6 = tl.load(in_ptr3 + (x4 + 64 * y1), xmask & ymask, eviction_policy
        ='evict_last')
    tmp2 = tmp0 + tmp1
    tmp4 = tmp2 - tmp3
    tmp5 = tl_math.exp(tmp4)
    tmp7 = tmp5 / tmp6
    tl.store(out_ptr0 + (y0 + 25 * x4 + 1600 * y1), tmp7, xmask & ymask)


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


@triton.jit
def triton_poi_fused_clone_6(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl.
    constexpr, XBLOCK: tl.constexpr):
    ynumel = 1600
    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
    x3 = xindex
    y0 = yindex % 25
    y1 = yindex // 25 % 4
    y2 = yindex // 100
    y4 = yindex
    tmp0 = tl.load(in_ptr0 + (y0 + 25 * (x3 % 2) + 50 * y1 + 200 * (x3 // 2
        ) + 400 * y2), xmask & ymask, eviction_policy='evict_last')
    tl.store(out_ptr0 + (x3 + 4 * y4), tmp0, xmask & ymask)


@triton.jit
def triton_poi_fused_pixel_shuffle_7(in_ptr0, out_ptr0, ynumel, xnumel,
    YBLOCK: tl.constexpr, XBLOCK: tl.constexpr):
    ynumel = 16
    xnumel = 64
    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 % 8
    x3 = xindex // 8
    y0 = yindex % 4
    y1 = yindex // 4
    x4 = xindex
    tmp0 = tl.load(in_ptr0 + (2 * (x3 % 2) + 4 * y0 + 16 * (x2 // 2) + 64 *
        (x3 // 2) + 256 * y1 + x2 % 2), xmask & ymask, eviction_policy=
        'evict_last')
    tl.store(out_ptr0 + (y0 + 4 * x4 + 256 * y1), tmp0, xmask & ymask)


@triton.jit
def triton_poi_fused_convolution_8(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl
    .constexpr):
    xnumel = 1024
    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,
        primals_7) = args
    args.clear()
    assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_2, (64, 4, 1, 1), (4, 1, 1, 1))
    assert_size_stride(primals_3, (64,), (1,))
    assert_size_stride(primals_4, (100, 64, 3, 3), (576, 9, 3, 1))
    assert_size_stride(primals_5, (100,), (1,))
    assert_size_stride(primals_6, (4, 4, 1, 1), (4, 1, 1, 1))
    assert_size_stride(primals_7, (4,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 1, 16, 4), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_0[grid(16, 16)](primals_1, buf0, 16, 16, XBLOCK=16,
            YBLOCK=16, num_warps=4, num_stages=1)
        del primals_1
        buf1 = empty_strided_cuda((100, 64, 3, 3), (576, 1, 192, 64), torch
            .float32)
        triton_poi_fused_1[grid(6400, 9)](primals_4, buf1, 6400, 9, XBLOCK=
            16, YBLOCK=64, num_warps=4, num_stages=1)
        del primals_4
        buf2 = 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(buf2, (4, 64, 4, 4), (1024, 1, 256, 64))
        buf3 = buf2
        del buf2
        triton_poi_fused_convolution_2[grid(4096)](buf3, primals_3, 4096,
            XBLOCK=128, num_warps=4, num_stages=1)
        del primals_3
        buf4 = extern_kernels.convolution(buf3, buf1, stride=(1, 1),
            padding=(1, 1), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf4, (4, 100, 4, 4), (1600, 1, 400, 100))
        buf5 = empty_strided_cuda((4, 1, 8, 8), (64, 256, 8, 1), torch.float32)
        buf6 = empty_strided_cuda((4, 1, 8, 8), (64, 256, 8, 1), torch.float32)
        triton_per_fused__softmax_3[grid(256)](buf4, primals_5, buf5, buf6,
            256, 25, XBLOCK=8, num_warps=2, num_stages=1)
        buf7 = empty_strided_cuda((4, 25, 8, 8), (1600, 1, 200, 25), torch.
            float32)
        triton_poi_fused__softmax_4[grid(100, 64)](buf4, primals_5, buf5,
            buf6, buf7, 100, 64, XBLOCK=2, YBLOCK=128, num_warps=4,
            num_stages=1)
        del buf5
        del buf6
        del primals_5
        buf8 = reinterpret_tensor(buf4, (4, 4, 4, 4, 25), (1600, 400, 100, 
            25, 1), 0)
        del buf4
        triton_poi_fused_clone_5[grid(256, 25)](buf0, buf8, 256, 25, XBLOCK
            =32, YBLOCK=32, num_warps=4, num_stages=1)
        buf9 = empty_strided_cuda((4, 4, 4, 25, 4), (1600, 400, 100, 4, 1),
            torch.float32)
        triton_poi_fused_clone_6[grid(1600, 4)](buf7, buf9, 1600, 4, XBLOCK
            =4, YBLOCK=256, num_warps=4, num_stages=1)
        buf10 = empty_strided_cuda((64, 4, 4), (16, 4, 1), torch.float32)
        extern_kernels.bmm(reinterpret_tensor(buf8, (64, 4, 25), (100, 25, 
            1), 0), reinterpret_tensor(buf9, (64, 25, 4), (100, 4, 1), 0),
            out=buf10)
        del buf9
        buf11 = empty_strided_cuda((4, 4, 8, 8), (256, 1, 32, 4), torch.float32
            )
        triton_poi_fused_pixel_shuffle_7[grid(16, 64)](buf10, buf11, 16, 64,
            XBLOCK=64, YBLOCK=16, num_warps=4, num_stages=1)
        del buf10
        buf12 = extern_kernels.convolution(buf11, primals_6, stride=(1, 1),
            padding=(0, 0), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf12, (4, 4, 8, 8), (256, 1, 32, 4))
        buf13 = buf12
        del buf12
        triton_poi_fused_convolution_8[grid(1024)](buf13, primals_7, 1024,
            XBLOCK=256, num_warps=4, num_stages=1)
        del primals_7
    return (buf13, buf0, primals_2, buf1, primals_6, buf3, buf7, buf11,
        reinterpret_tensor(buf8, (64, 25, 4), (100, 1, 25), 0))


class CARAFENew(nn.Module):

    def __init__(self, inC, outC, Kencoder=3, delta=2, Kup=5, Cm=64):
        super(CARAFENew, self).__init__()
        self.Kencoder = Kencoder
        self.delta = delta
        self.Kup = Kup
        self.down = nn.Conv2d(in_channels=inC, out_channels=Cm, kernel_size=1)
        self.encoder = nn.Conv2d(64, self.delta ** 2 * self.Kup ** 2, self.
            Kencoder, 1, self.Kencoder // 2)
        self.out = nn.Conv2d(inC, outC, 1)

    def forward(self, input_0):
        primals_2 = self.down.weight
        primals_3 = self.down.bias
        primals_4 = self.encoder.weight
        primals_5 = self.encoder.bias
        primals_6 = self.out.weight
        primals_7 = self.out.bias
        primals_1 = input_0
        output = call([primals_1, primals_2, primals_3, primals_4,
            primals_5, primals_6, primals_7])
        return output[0]


Measured latency_ms: 0.179075