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_native_batch_norm_backward_0(
    in_ptr0, in_ptr1, in_ptr2, out_ptr2, out_ptr3, out_ptr4, xnumel, rnumel,
    XBLOCK: tl.constexpr):
    xnumel = 8
    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)
    r1 = rindex
    x0 = xindex
    x2 = xindex % 2
    x3 = xindex // 2
    tmp0 = tl.load(in_ptr0 + (r1 + 16 * (x0 % 2) + 64 * (x0 // 2)), xmask,
        other=0.0)
    tmp17 = tl.load(in_ptr0 + (r1 + 16 * x2 + 64 * x3), xmask, other=0.0)
    tmp25 = tl.load(in_ptr1 + x2, xmask, eviction_policy='evict_last')
    tmp27 = tl.load(in_ptr2 + x2, xmask, eviction_policy='evict_last')
    tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
    tl.where(xmask, tmp1, 0)
    tmp4 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK])
    tmp6 = tl.where(xmask, tmp4, 0)
    tmp7 = tl.sum(tmp6, 1)[:, None]
    tmp8 = tl.full([XBLOCK, 1], 16, tl.int32)
    tmp9 = tmp8.to(tl.float32)
    tmp10 = tmp7 / tmp9
    tmp11 = tmp1 - tmp10
    tmp12 = tmp11 * tmp11
    tmp13 = tl.broadcast_to(tmp12, [XBLOCK, RBLOCK])
    tmp15 = tl.where(xmask, tmp13, 0)
    tmp16 = tl.sum(tmp15, 1)[:, None]
    tmp18 = tmp17 - tmp10
    tmp19 = 16.0
    tmp20 = tmp16 / tmp19
    tmp21 = 1e-05
    tmp22 = tmp20 + tmp21
    tmp23 = libdevice.rsqrt(tmp22)
    tmp24 = tmp18 * tmp23
    tmp26 = tmp24 * tmp25
    tmp28 = tmp26 + tmp27
    tmp29 = tmp0 - tmp10
    tl.store(out_ptr2 + (r1 + 16 * x2 + 64 * x3), tmp28, xmask)
    tl.store(out_ptr3 + (r1 + 16 * x0), tmp29, xmask)
    tl.store(out_ptr4 + x0, tmp23, xmask)


@triton.jit
def triton_poi_fused_cat_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 128
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 32
    x1 = xindex // 32
    tmp0 = tl.load(in_ptr0 + (32 + x0 + 64 * x1), xmask)
    tl.store(out_ptr0 + (x0 + 64 * x1), tmp0, 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, (2,), (1,))
    assert_size_stride(primals_3, (2,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf6 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        buf4 = reinterpret_tensor(buf6, (4, 2, 4, 4), (64, 16, 4, 1), 0)
        buf7 = empty_strided_cuda((1, 8, 4, 4), (128, 16, 4, 1), torch.float32)
        buf3 = empty_strided_cuda((1, 8, 1, 1), (8, 1, 8, 8), torch.float32)
        get_raw_stream(0)
        triton_per_fused__native_batch_norm_legit_cat_native_batch_norm_backward_0[
            grid(8)](primals_1, primals_2, primals_3, buf4, buf7, buf3, 8, 
            16, XBLOCK=1, num_warps=2, num_stages=1)
        del primals_2
        del primals_3
        buf5 = reinterpret_tensor(buf6, (4, 2, 4, 4), (64, 16, 4, 1), 32)
        triton_poi_fused_cat_1[grid(128)](primals_1, buf5, 128, XBLOCK=128,
            num_warps=4, num_stages=1)
        del primals_1
    return buf6, reinterpret_tensor(buf3, (8,), (1,), 0), buf7


class HINetNew(nn.Module):

    def __init__(self, in_ch):
        super(HINetNew, self).__init__()
        self.instance_norm = nn.InstanceNorm2d(in_ch - in_ch // 2, affine=True)

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


Measured latency_ms: 0.032215