import torch
from torch import nn


class DenoisingBlock(nn.Module):

    def __init__(self, in_channels, inner_channels, out_channels):
        super(DenoisingBlock, self).__init__()
        self.conv_0 = nn.Conv2d(in_channels, inner_channels, 3, padding=1)
        self.conv_1 = nn.Conv2d(in_channels + inner_channels,
            inner_channels, 3, padding=1)
        self.conv_2 = nn.Conv2d(in_channels + 2 * inner_channels,
            inner_channels, 3, padding=1)
        self.conv_3 = nn.Conv2d(in_channels + 3 * inner_channels,
            out_channels, 3, padding=1)
        self.actv_0 = nn.PReLU(inner_channels)
        self.actv_1 = nn.PReLU(inner_channels)
        self.actv_2 = nn.PReLU(inner_channels)
        self.actv_3 = nn.PReLU(out_channels)

    def forward(self, x):
        out_0 = self.actv_0(self.conv_0(x))
        out_0 = torch.cat([x, out_0], 1)
        out_1 = self.actv_1(self.conv_1(out_0))
        out_1 = torch.cat([out_0, out_1], 1)
        out_2 = self.actv_2(self.conv_2(out_1))
        out_2 = torch.cat([out_1, out_2], 1)
        out_3 = self.actv_3(self.conv_3(out_2))
        return out_3 + x


def get_inputs():
    return [torch.rand([4, 4, 4, 4])]


def get_init_inputs():
    return [[], {'in_channels': 4, 'inner_channels': 4, 'out_channels': 4}]

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 import 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_convolution_0(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
    x3 = xindex
    x1 = xindex // 16 % 4
    tmp0 = tl.load(in_out_ptr0 + x3, xmask)
    tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tl.store(in_out_ptr0 + x3, tmp2, xmask)


@triton.jit
def triton_poi_fused_cat_1(in_ptr0, in_ptr1, in_ptr2, out_ptr0, xnumel,
    XBLOCK: tl.constexpr):
    xnumel = 512
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x1 = xindex // 16 % 8
    x0 = xindex % 16
    x2 = xindex // 128
    x3 = xindex
    tmp0 = x1
    tl.full([1], 0, tl.int64)
    tmp3 = tl.full([1], 4, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp5 = tl.load(in_ptr0 + (x0 + 16 * x1 + 64 * x2), tmp4 & xmask, other=0.0)
    tmp6 = tmp0 >= tmp3
    tl.full([1], 8, tl.int64)
    tmp9 = tl.load(in_ptr1 + (x0 + 16 * (-4 + x1) + 64 * x2), tmp6 & xmask,
        other=0.0)
    tmp10 = 0.0
    tmp11 = tmp9 > tmp10
    tmp12 = tl.load(in_ptr2 + (-4 + x1), tmp6 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp13 = tmp12 * tmp9
    tmp14 = tl.where(tmp11, tmp9, tmp13)
    tmp15 = tl.full(tmp14.shape, 0.0, tmp14.dtype)
    tmp16 = tl.where(tmp6, tmp14, tmp15)
    tmp17 = tl.where(tmp4, tmp5, tmp16)
    tl.store(out_ptr0 + x3, tmp17, xmask)


@triton.jit
def triton_poi_fused_cat_2(in_ptr0, in_ptr1, in_ptr2, out_ptr0, xnumel,
    XBLOCK: tl.constexpr):
    xnumel = 768
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x1 = xindex // 16 % 12
    x0 = xindex % 16
    x2 = xindex // 192
    x3 = xindex
    tmp0 = x1
    tl.full([1], 0, tl.int64)
    tmp3 = tl.full([1], 8, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp5 = tl.load(in_ptr0 + (x0 + 16 * x1 + 128 * x2), tmp4 & xmask, other=0.0
        )
    tmp6 = tmp0 >= tmp3
    tl.full([1], 12, tl.int64)
    tmp9 = tl.load(in_ptr1 + (x0 + 16 * (-8 + x1) + 64 * x2), tmp6 & xmask,
        other=0.0)
    tmp10 = 0.0
    tmp11 = tmp9 > tmp10
    tmp12 = tl.load(in_ptr2 + (-8 + x1), tmp6 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp13 = tmp12 * tmp9
    tmp14 = tl.where(tmp11, tmp9, tmp13)
    tmp15 = tl.full(tmp14.shape, 0.0, tmp14.dtype)
    tmp16 = tl.where(tmp6, tmp14, tmp15)
    tmp17 = tl.where(tmp4, tmp5, tmp16)
    tl.store(out_ptr0 + x3, tmp17, xmask)


@triton.jit
def triton_poi_fused_cat_3(in_ptr0, in_ptr1, in_ptr2, out_ptr0, xnumel,
    XBLOCK: tl.constexpr):
    xnumel = 1024
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x1 = xindex // 16 % 16
    x0 = xindex % 16
    x2 = xindex // 256
    x3 = xindex
    tmp0 = x1
    tl.full([1], 0, tl.int64)
    tmp3 = tl.full([1], 12, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp5 = tl.load(in_ptr0 + (x0 + 16 * x1 + 192 * x2), tmp4 & xmask, other=0.0
        )
    tmp6 = tmp0 >= tmp3
    tl.full([1], 16, tl.int64)
    tmp9 = tl.load(in_ptr1 + (x0 + 16 * (-12 + x1) + 64 * x2), tmp6 & xmask,
        other=0.0)
    tmp10 = 0.0
    tmp11 = tmp9 > tmp10
    tmp12 = tl.load(in_ptr2 + (-12 + x1), tmp6 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp13 = tmp12 * tmp9
    tmp14 = tl.where(tmp11, tmp9, tmp13)
    tmp15 = tl.full(tmp14.shape, 0.0, tmp14.dtype)
    tmp16 = tl.where(tmp6, tmp14, tmp15)
    tmp17 = tl.where(tmp4, tmp5, tmp16)
    tl.store(out_ptr0 + x3, tmp17, xmask)


@triton.jit
def triton_poi_fused__prelu_kernel_add_convolution_4(in_out_ptr0, in_ptr0,
    in_ptr1, in_ptr2, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x3 = xindex
    x1 = xindex // 16 % 4
    tmp0 = tl.load(in_out_ptr0 + x3, xmask)
    tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last')
    tmp5 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr2 + x3, xmask)
    tmp2 = tmp0 + tmp1
    tmp3 = 0.0
    tmp4 = tmp2 > tmp3
    tmp6 = tmp5 * tmp2
    tmp7 = tl.where(tmp4, tmp2, tmp6)
    tmp9 = tmp7 + tmp8
    tl.store(in_out_ptr0 + x3, tmp2, xmask)
    tl.store(out_ptr0 + x3, tmp9, xmask)


def call(args):
    (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6,
        primals_7, primals_8, primals_9, primals_10, primals_11, primals_12,
        primals_13) = args
    args.clear()
    assert_size_stride(primals_1, (4, 4, 3, 3), (36, 9, 3, 1))
    assert_size_stride(primals_2, (4,), (1,))
    assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_4, (4,), (1,))
    assert_size_stride(primals_5, (4, 8, 3, 3), (72, 9, 3, 1))
    assert_size_stride(primals_6, (4,), (1,))
    assert_size_stride(primals_7, (4,), (1,))
    assert_size_stride(primals_8, (4, 12, 3, 3), (108, 9, 3, 1))
    assert_size_stride(primals_9, (4,), (1,))
    assert_size_stride(primals_10, (4,), (1,))
    assert_size_stride(primals_11, (4, 16, 3, 3), (144, 9, 3, 1))
    assert_size_stride(primals_12, (4,), (1,))
    assert_size_stride(primals_13, (4,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = extern_kernels.convolution(primals_3, primals_1, stride=(1, 
            1), padding=(1, 1), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf0, (4, 4, 4, 4), (64, 16, 4, 1))
        buf1 = buf0
        del buf0
        get_raw_stream(0)
        triton_poi_fused_convolution_0[grid(256)](buf1, primals_2, 256,
            XBLOCK=128, num_warps=4, num_stages=1)
        del primals_2
        buf2 = empty_strided_cuda((4, 8, 4, 4), (128, 16, 4, 1), torch.float32)
        triton_poi_fused_cat_1[grid(512)](primals_3, buf1, primals_4, buf2,
            512, XBLOCK=256, num_warps=4, num_stages=1)
        buf3 = extern_kernels.convolution(buf2, primals_5, stride=(1, 1),
            padding=(1, 1), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf3, (4, 4, 4, 4), (64, 16, 4, 1))
        buf4 = buf3
        del buf3
        triton_poi_fused_convolution_0[grid(256)](buf4, primals_6, 256,
            XBLOCK=128, num_warps=4, num_stages=1)
        del primals_6
        buf5 = empty_strided_cuda((4, 12, 4, 4), (192, 16, 4, 1), torch.float32
            )
        triton_poi_fused_cat_2[grid(768)](buf2, buf4, primals_7, buf5, 768,
            XBLOCK=128, num_warps=4, num_stages=1)
        buf6 = extern_kernels.convolution(buf5, primals_8, stride=(1, 1),
            padding=(1, 1), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf6, (4, 4, 4, 4), (64, 16, 4, 1))
        buf7 = buf6
        del buf6
        triton_poi_fused_convolution_0[grid(256)](buf7, primals_9, 256,
            XBLOCK=128, num_warps=4, num_stages=1)
        del primals_9
        buf8 = empty_strided_cuda((4, 16, 4, 4), (256, 16, 4, 1), torch.float32
            )
        triton_poi_fused_cat_3[grid(1024)](buf5, buf7, primals_10, buf8, 
            1024, XBLOCK=256, num_warps=4, num_stages=1)
        buf9 = extern_kernels.convolution(buf8, primals_11, stride=(1, 1),
            padding=(1, 1), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf9, (4, 4, 4, 4), (64, 16, 4, 1))
        buf10 = buf9
        del buf9
        buf11 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        triton_poi_fused__prelu_kernel_add_convolution_4[grid(256)](buf10,
            primals_12, primals_13, primals_3, buf11, 256, XBLOCK=128,
            num_warps=4, num_stages=1)
        del primals_12
    return (buf11, primals_1, primals_3, primals_4, primals_5, primals_7,
        primals_8, primals_10, primals_11, primals_13, buf1, buf2, buf4,
        buf5, buf7, buf8, buf10)


class DenoisingBlockNew(nn.Module):

    def __init__(self, in_channels, inner_channels, out_channels):
        super(DenoisingBlockNew, self).__init__()
        self.conv_0 = nn.Conv2d(in_channels, inner_channels, 3, padding=1)
        self.conv_1 = nn.Conv2d(in_channels + inner_channels,
            inner_channels, 3, padding=1)
        self.conv_2 = nn.Conv2d(in_channels + 2 * inner_channels,
            inner_channels, 3, padding=1)
        self.conv_3 = nn.Conv2d(in_channels + 3 * inner_channels,
            out_channels, 3, padding=1)
        self.actv_0 = nn.PReLU(inner_channels)
        self.actv_1 = nn.PReLU(inner_channels)
        self.actv_2 = nn.PReLU(inner_channels)
        self.actv_3 = nn.PReLU(out_channels)

    def forward(self, input_0):
        primals_1 = self.conv_0.weight
        primals_2 = self.conv_0.bias
        primals_5 = self.conv_1.weight
        primals_4 = self.conv_1.bias
        primals_8 = self.conv_2.weight
        primals_6 = self.conv_2.bias
        primals_11 = self.conv_3.weight
        primals_7 = self.conv_3.bias
        primals_9 = self.actv_0.weight
        primals_10 = self.actv_1.weight
        primals_12 = self.actv_2.weight
        primals_13 = self.actv_3.weight
        primals_3 = input_0
        output = call([primals_1, primals_2, primals_3, primals_4,
            primals_5, primals_6, primals_7, primals_8, primals_9,
            primals_10, primals_11, primals_12, primals_13])
        return output[0]
