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
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


@triton.jit
def triton_poi_fused__to_copy_add_arange_mul_0(out_ptr0, xnumel, XBLOCK: tl
    .constexpr):
    xnumel = 124
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = x0
    tmp1 = tmp0.to(tl.float32)
    tmp2 = 0.5
    tmp3 = tmp1 * tmp2
    tmp4 = tmp3.to(tl.int32)
    tl.store(out_ptr0 + x0, tmp4, xmask)


@triton.jit
def triton_poi_fused__to_copy_add_arange_mul_1(out_ptr0, xnumel, XBLOCK: tl
    .constexpr):
    xnumel = 248
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = x0
    tmp1 = tmp0.to(tl.float32)
    tmp2 = 0.5
    tmp3 = tmp1 * tmp2
    tmp4 = tmp3.to(tl.int32)
    tl.store(out_ptr0 + x0, tmp4, xmask)


@triton.jit
def triton_poi_fused__unsafe_index_convolution_2(in_ptr0, in_ptr1, in_ptr2,
    in_ptr3, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    tl.full([XBLOCK], True, tl.int1)
    x1 = xindex // 248 % 248
    x0 = xindex % 248
    x5 = xindex // 61504
    x2 = xindex // 61504 % 16
    x6 = xindex
    tmp0 = tl.load(in_ptr0 + x1, None, eviction_policy='evict_last')
    tmp5 = tl.load(in_ptr0 + x0, None, eviction_policy='evict_last')
    tmp19 = tl.load(in_ptr3 + x2, None, eviction_policy='evict_last')
    tmp1 = tl.full([XBLOCK], 124, tl.int32)
    tmp2 = tmp0 + tmp1
    tmp3 = tmp0 < 0
    tmp4 = tl.where(tmp3, tmp2, tmp0)
    tmp6 = tmp5 + tmp1
    tmp7 = tmp5 < 0
    tmp8 = tl.where(tmp7, tmp6, tmp5)
    tmp9 = tl.load(in_ptr1 + tmp4, None, eviction_policy='evict_last')
    tmp10 = tl.full([XBLOCK], 62, tl.int32)
    tmp11 = tmp9 + tmp10
    tmp12 = tmp9 < 0
    tmp13 = tl.where(tmp12, tmp11, tmp9)
    tmp14 = tl.load(in_ptr1 + tmp8, None, eviction_policy='evict_last')
    tmp15 = tmp14 + tmp10
    tmp16 = tmp14 < 0
    tmp17 = tl.where(tmp16, tmp15, tmp14)
    tmp18 = tl.load(in_ptr2 + (tmp17 + 62 * tmp13 + 3844 * x5), None,
        eviction_policy='evict_last')
    tmp20 = tmp18 + tmp19
    tl.store(out_ptr0 + x6, tmp20, None)


def call(args):
    primals_1, primals_2, primals_3 = args
    args.clear()
    assert_size_stride(primals_1, (16, 10, 3, 3), (90, 9, 3, 1))
    assert_size_stride(primals_2, (16,), (1,))
    assert_size_stride(primals_3, (4, 10, 64, 64), (40960, 4096, 64, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = extern_kernels.convolution(primals_3, primals_1, stride=(1, 
            1), padding=(0, 0), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf0, (4, 16, 62, 62), (61504, 3844, 62, 1))
        buf1 = empty_strided_cuda((124,), (1,), torch.int64)
        get_raw_stream(0)
        triton_poi_fused__to_copy_add_arange_mul_0[grid(124)](buf1, 124,
            XBLOCK=128, num_warps=4, num_stages=1)
        buf2 = empty_strided_cuda((248,), (1,), torch.int64)
        triton_poi_fused__to_copy_add_arange_mul_1[grid(248)](buf2, 248,
            XBLOCK=128, num_warps=4, num_stages=1)
        buf3 = empty_strided_cuda((4, 16, 248, 248), (984064, 61504, 248, 1
            ), torch.float32)
        triton_poi_fused__unsafe_index_convolution_2[grid(3936256)](buf2,
            buf1, buf0, primals_2, buf3, 3936256, XBLOCK=1024, num_warps=4,
            num_stages=1)
        del buf0
        del primals_2
    return buf3, primals_1, primals_3, buf1, buf2


class TestUpsampleNearest2dNew(nn.Module):
    """Module for UpsampleNearest2d conversion testing
    """

    def __init__(self, inp=10, out=16, kernel_size=3, bias=True):
        super(TestUpsampleNearest2dNew, self).__init__()
        self.conv2d = nn.Conv2d(inp, out, kernel_size=kernel_size, bias=bias)
        self.up = nn.UpsamplingNearest2d(scale_factor=2)

    def forward(self, input_0):
        primals_1 = self.conv2d.weight
        primals_2 = self.conv2d.bias
        primals_3 = input_0
        output = call([primals_1, primals_2, primals_3])
        return output[0]


Measured latency_ms: 0.052724