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
from torch.nn import 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_cat_0(in_ptr0, in_ptr1, in_ptr2, in_ptr3, out_ptr0,
    xnumel, XBLOCK: tl.constexpr):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x1 = xindex // 4 % 4
    x0 = xindex % 4
    x2 = xindex // 16
    x3 = xindex
    tmp0 = x1
    tl.full([1], 0, tl.int64)
    tmp3 = tl.full([1], 1, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp5 = tl.load(in_ptr0 + (x0 + 4 * x2), tmp4 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp6 = tmp0 >= tmp3
    tmp7 = tl.full([1], 2, tl.int64)
    tmp8 = tmp0 < tmp7
    tmp9 = tmp6 & tmp8
    tmp10 = tl.load(in_ptr1 + (x0 + 4 * x2), tmp9 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp11 = tmp0 >= tmp7
    tmp12 = tl.full([1], 3, tl.int64)
    tmp13 = tmp0 < tmp12
    tmp14 = tmp11 & tmp13
    tmp15 = tl.load(in_ptr2 + (x0 + 4 * x2), tmp14 & xmask, eviction_policy
        ='evict_last', other=0.0)
    tmp16 = tmp0 >= tmp12
    tl.full([1], 4, tl.int64)
    tmp19 = tl.load(in_ptr3 + (x0 + 4 * x2), tmp16 & xmask, eviction_policy
        ='evict_last', other=0.0)
    tmp20 = tl.where(tmp14, tmp15, tmp19)
    tmp21 = tl.where(tmp9, tmp10, tmp20)
    tmp22 = tl.where(tmp4, tmp5, tmp21)
    tl.store(out_ptr0 + x3, tmp22, xmask)


@triton.jit
def triton_poi_fused__unsafe_index_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
    x1 = xindex // 4 % 4
    x0 = xindex % 4
    x2 = xindex // 16
    x4 = xindex
    tmp0 = x1
    tmp1 = tmp0.to(tl.float32)
    tmp2 = 0.5
    tmp3 = tmp1 * tmp2
    tmp4 = tmp3.to(tl.int32)
    tmp5 = x0
    tmp6 = tmp5.to(tl.float32)
    tmp7 = tmp6 * tmp2
    tmp8 = tmp7.to(tl.int32)
    tmp9 = tl.load(in_ptr0 + (tmp8 + 2 * tmp4 + 4 * x2), xmask,
        eviction_policy='evict_last')
    tl.store(out_ptr0 + x4, tmp9, xmask)


@triton.jit
def triton_poi_fused_cat_sub_2(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4,
    out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x1 = xindex // 16 % 4
    x0 = xindex % 16
    x2 = xindex // 64
    x3 = xindex
    tmp23 = tl.load(in_ptr4 + x3, xmask)
    tmp0 = x1
    tl.full([1], 0, tl.int64)
    tmp3 = tl.full([1], 1, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp5 = tl.load(in_ptr0 + (x0 + 16 * x2), tmp4 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp6 = tmp0 >= tmp3
    tmp7 = tl.full([1], 2, tl.int64)
    tmp8 = tmp0 < tmp7
    tmp9 = tmp6 & tmp8
    tmp10 = tl.load(in_ptr1 + (x0 + 16 * x2), tmp9 & xmask, eviction_policy
        ='evict_last', other=0.0)
    tmp11 = tmp0 >= tmp7
    tmp12 = tl.full([1], 3, tl.int64)
    tmp13 = tmp0 < tmp12
    tmp14 = tmp11 & tmp13
    tmp15 = tl.load(in_ptr2 + (x0 + 16 * x2), tmp14 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp16 = tmp0 >= tmp12
    tl.full([1], 4, tl.int64)
    tmp19 = tl.load(in_ptr3 + (x0 + 16 * x2), tmp16 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp20 = tl.where(tmp14, tmp15, tmp19)
    tmp21 = tl.where(tmp9, tmp10, tmp20)
    tmp22 = tl.where(tmp4, tmp5, tmp21)
    tmp24 = tmp23 - tmp22
    tl.store(out_ptr0 + x3, tmp22, xmask)
    tl.store(out_ptr1 + x3, tmp24, xmask)


def call(args):
    arg0_1, arg1_1, arg2_1 = args
    args.clear()
    assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(arg1_1, (1, 1, 5, 5), (25, 25, 5, 1))
    assert_size_stride(arg2_1, (1, 1, 5, 5), (25, 25, 5, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = extern_kernels.convolution(reinterpret_tensor(arg0_1, (4, 1,
            4, 4), (64, 16, 4, 1), 0), arg1_1, stride=(2, 2), padding=(2, 2
            ), dilation=(1, 1), transposed=False, output_padding=(0, 0),
            groups=1, bias=None)
        assert_size_stride(buf0, (4, 1, 2, 2), (4, 4, 2, 1))
        buf1 = extern_kernels.convolution(reinterpret_tensor(arg0_1, (4, 1,
            4, 4), (64, 16, 4, 1), 16), arg1_1, stride=(2, 2), padding=(2, 
            2), dilation=(1, 1), transposed=False, output_padding=(0, 0),
            groups=1, bias=None)
        assert_size_stride(buf1, (4, 1, 2, 2), (4, 4, 2, 1))
        buf2 = extern_kernels.convolution(reinterpret_tensor(arg0_1, (4, 1,
            4, 4), (64, 16, 4, 1), 32), arg1_1, stride=(2, 2), padding=(2, 
            2), dilation=(1, 1), transposed=False, output_padding=(0, 0),
            groups=1, bias=None)
        assert_size_stride(buf2, (4, 1, 2, 2), (4, 4, 2, 1))
        buf3 = extern_kernels.convolution(reinterpret_tensor(arg0_1, (4, 1,
            4, 4), (64, 16, 4, 1), 48), arg1_1, stride=(2, 2), padding=(2, 
            2), dilation=(1, 1), transposed=False, output_padding=(0, 0),
            groups=1, bias=None)
        assert_size_stride(buf3, (4, 1, 2, 2), (4, 4, 2, 1))
        del arg1_1
        buf4 = empty_strided_cuda((4, 4, 2, 2), (16, 4, 2, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_cat_0[grid(64)](buf0, buf1, buf2, buf3, buf4, 64,
            XBLOCK=64, num_warps=1, num_stages=1)
        del buf0
        del buf1
        del buf2
        del buf3
        buf5 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        triton_poi_fused__unsafe_index_1[grid(256)](buf4, buf5, 256, XBLOCK
            =128, num_warps=4, num_stages=1)
        buf6 = extern_kernels.convolution(reinterpret_tensor(buf5, (4, 1, 4,
            4), (64, 0, 4, 1), 0), arg2_1, stride=(1, 1), padding=(2, 2),
            dilation=(1, 1), transposed=False, output_padding=(0, 0),
            groups=1, bias=None)
        assert_size_stride(buf6, (4, 1, 4, 4), (16, 16, 4, 1))
        buf7 = extern_kernels.convolution(reinterpret_tensor(buf5, (4, 1, 4,
            4), (64, 16, 4, 1), 16), arg2_1, stride=(1, 1), padding=(2, 2),
            dilation=(1, 1), transposed=False, output_padding=(0, 0),
            groups=1, bias=None)
        assert_size_stride(buf7, (4, 1, 4, 4), (16, 16, 4, 1))
        buf8 = extern_kernels.convolution(reinterpret_tensor(buf5, (4, 1, 4,
            4), (64, 16, 4, 1), 32), arg2_1, stride=(1, 1), padding=(2, 2),
            dilation=(1, 1), transposed=False, output_padding=(0, 0),
            groups=1, bias=None)
        assert_size_stride(buf8, (4, 1, 4, 4), (16, 16, 4, 1))
        buf9 = extern_kernels.convolution(reinterpret_tensor(buf5, (4, 1, 4,
            4), (64, 16, 4, 1), 48), arg2_1, stride=(1, 1), padding=(2, 2),
            dilation=(1, 1), transposed=False, output_padding=(0, 0),
            groups=1, bias=None)
        assert_size_stride(buf9, (4, 1, 4, 4), (16, 16, 4, 1))
        del arg2_1
        buf10 = buf5
        del buf5
        buf11 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        triton_poi_fused_cat_sub_2[grid(256)](buf6, buf7, buf8, buf9,
            arg0_1, buf10, buf11, 256, XBLOCK=128, num_warps=4, num_stages=1)
        del arg0_1
        del buf6
        del buf7
        del buf8
        del buf9
    return buf11, buf4, buf10


class PyramidDown(nn.Module):

    def __init__(self) ->None:
        super(PyramidDown, self).__init__()
        self.filter = nn.Parameter(torch.tensor([[1, 4, 6, 4, 1], [4, 16, 
            24, 16, 4], [6, 24, 36, 24, 6], [4, 16, 24, 16, 4], [1, 4, 6, 4,
            1]], dtype=torch.float).reshape(1, 1, 5, 5) / 256,
            requires_grad=False)

    def forward(self, x: 'torch.Tensor') ->torch.Tensor:
        results = []
        for i in range(x.shape[1]):
            results.append(F.conv2d(x[:, i:i + 1, :, :], self.filter,
                padding=2, stride=2))
        return torch.cat(results, dim=1)


class PyramidUp(nn.Module):

    def __init__(self) ->None:
        super(PyramidUp, self).__init__()
        self.filter = nn.Parameter(torch.tensor([[1, 4, 6, 4, 1], [4, 16, 
            24, 16, 4], [6, 24, 36, 24, 6], [4, 16, 24, 16, 4], [1, 4, 6, 4,
            1]], dtype=torch.float).reshape(1, 1, 5, 5) / 256,
            requires_grad=False)

    def forward(self, x: 'torch.Tensor') ->torch.Tensor:
        upsample = F.interpolate(x, scale_factor=2)
        results = []
        for i in range(x.shape[1]):
            results.append(F.conv2d(upsample[:, i:i + 1, :, :], self.filter,
                padding=2))
        return torch.cat(results, dim=1)


class LaplacianPyramidLayerNew(nn.Module):

    def __init__(self) ->None:
        super(LaplacianPyramidLayerNew, self).__init__()
        self.pyramid_down = PyramidDown()
        self.pyramid_up = PyramidUp()

    def forward(self, input_0):
        arg1_1 = self.pyramid_down.filter
        arg2_1 = self.pyramid_up.filter
        arg0_1 = input_0
        output = call([arg0_1, arg1_1, arg2_1])
        return output[0], output[1], output[2]


Measured latency_ms: 0.065941