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.triton_helpers import libdevice
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_per_fused__native_batch_norm_legit_cat_leaky_relu_leaky_relu_backward_0(
    in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, out_ptr0, out_ptr1, out_ptr3,
    out_ptr4, xnumel, rnumel, XBLOCK: tl.constexpr):
    xnumel = 32
    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, :]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    x0 = xindex % 8
    r2 = rindex
    x1 = xindex // 8
    x3 = xindex
    tmp37 = tl.load(in_ptr1 + x3 % 8, xmask, eviction_policy='evict_last')
    tmp39 = tl.load(in_ptr2 + x3 % 8, xmask, eviction_policy='evict_last')
    tmp0 = x0
    tl.full([1, 1], 0, tl.int64)
    tmp3 = tl.full([1, 1], 4, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp5 = tl.load(in_ptr0 + (r2 + 16 * x0 + 64 * x1), tmp4 & xmask, other=0.0)
    tmp6 = tmp0 >= tmp3
    tl.full([1, 1], 8, tl.int64)
    tmp9 = tl.load(in_ptr0 + (r2 + 16 * (-4 + x0) + 64 * x1), tmp6 & xmask,
        other=0.0)
    tmp10 = -tmp9
    tmp11 = tl.full(tmp10.shape, 0.0, tmp10.dtype)
    tmp12 = tl.where(tmp6, tmp10, tmp11)
    tmp13 = tl.where(tmp4, tmp5, tmp12)
    tmp14 = tl.broadcast_to(tmp13, [XBLOCK, RBLOCK])
    tl.where(xmask, tmp14, 0)
    tmp17 = tl.broadcast_to(tmp14, [XBLOCK, RBLOCK])
    tmp19 = tl.where(xmask, tmp17, 0)
    tmp20 = tl.sum(tmp19, 1)[:, None]
    tmp21 = tl.full([XBLOCK, 1], 16, tl.int32)
    tmp22 = tmp21.to(tl.float32)
    tmp23 = tmp20 / tmp22
    tmp24 = tmp14 - tmp23
    tmp25 = tmp24 * tmp24
    tmp26 = tl.broadcast_to(tmp25, [XBLOCK, RBLOCK])
    tmp28 = tl.where(xmask, tmp26, 0)
    tmp29 = tl.sum(tmp28, 1)[:, None]
    tmp30 = tmp13 - tmp23
    tmp31 = 16.0
    tmp32 = tmp29 / tmp31
    tmp33 = 1e-05
    tmp34 = tmp32 + tmp33
    tmp35 = libdevice.rsqrt(tmp34)
    tmp36 = tmp30 * tmp35
    tmp38 = tmp36 * tmp37
    tmp40 = tmp38 + tmp39
    tmp41 = 0.0
    tmp42 = tmp40 > tmp41
    tmp43 = 0.01
    tmp44 = tmp40 * tmp43
    tmp45 = tl.where(tmp42, tmp40, tmp44)
    tmp46 = tmp45 > tmp41
    tl.store(out_ptr0 + (r2 + 16 * x3), tmp13, xmask)
    tl.store(in_out_ptr0 + (r2 + 16 * x3), tmp45, xmask)
    tl.store(out_ptr3 + (r2 + 16 * x3), tmp46, xmask)
    tl.store(out_ptr4 + x3, tmp35, xmask)
    tl.store(out_ptr1 + x3, tmp23, xmask)


def call(args):
    primals_1, primals_2, primals_3 = args
    args.clear()
    assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_2, (8,), (1,))
    assert_size_stride(primals_3, (8,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 8, 4, 4), (128, 16, 4, 1), torch.float32)
        buf1 = empty_strided_cuda((1, 32, 1, 1), (32, 1, 32, 32), torch.float32
            )
        buf5 = empty_strided_cuda((1, 32, 4, 4), (512, 16, 4, 1), torch.float32
            )
        buf6 = reinterpret_tensor(buf5, (4, 8, 4, 4), (128, 16, 4, 1), 0)
        del buf5
        buf7 = empty_strided_cuda((4, 8, 4, 4), (128, 16, 4, 1), torch.bool)
        buf4 = empty_strided_cuda((1, 32, 1, 1), (32, 1, 32, 32), torch.float32
            )
        get_raw_stream(0)
        triton_per_fused__native_batch_norm_legit_cat_leaky_relu_leaky_relu_backward_0[
            grid(32)](buf6, primals_1, primals_2, primals_3, buf0, buf1,
            buf7, buf4, 32, 16, XBLOCK=32, num_warps=4, num_stages=1)
        del primals_1
        del primals_2
        del primals_3
    return buf6, buf0, reinterpret_tensor(buf4, (32,), (1,), 0
        ), buf7, reinterpret_tensor(buf1, (1, 32, 1, 1), (32, 1, 1, 1), 0)


class CReLU_INNew(nn.Module):

    def __init__(self, channels):
        super(CReLU_INNew, self).__init__()
        self.bn = nn.InstanceNorm2d(channels * 2, eps=1e-05, momentum=0.1,
            affine=True)

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


Measured latency_ms: 0.024223