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 import triton_helpers
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_stack_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 512
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x3 = xindex // 64
    x1 = xindex // 4 % 4
    x0 = xindex % 4
    x2 = xindex // 16 % 4
    x4 = xindex // 4 % 16
    x5 = xindex
    tmp0 = x3
    tl.full([1], 0, tl.int64)
    tmp3 = tl.full([1], 4, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp5 = x1
    tmp7 = tl.full([1], 3, tl.int64)
    tmp8 = tmp5 < tmp7
    tmp9 = tmp8 & tmp4
    tmp10 = tl.load(in_ptr0 + (4 + x0 + 4 * x1 + 16 * x2 + 64 * x3), tmp9 &
        xmask, other=0.0)
    tmp11 = tmp5 >= tmp7
    tmp13 = tmp11 & tmp4
    tmp14 = tl.load(in_ptr0 + (x0 + 16 * x2 + 64 * x3), tmp13 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp15 = tl.where(tmp8, tmp10, tmp14)
    tmp16 = tl.full(tmp15.shape, 0.0, tmp15.dtype)
    tmp17 = tl.where(tmp4, tmp15, tmp16)
    tmp18 = tmp0 >= tmp3
    tl.full([1], 8, tl.int64)
    tmp21 = x0
    tmp23 = tmp21 < tmp7
    tmp24 = tmp23 & tmp18
    tmp25 = tl.load(in_ptr0 + (1 + 4 * x4 + 64 * (-4 + x3) + x0), tmp24 &
        xmask, eviction_policy='evict_last', other=0.0)
    tmp26 = tmp21 >= tmp7
    tmp28 = tmp26 & tmp18
    tmp29 = tl.load(in_ptr0 + (4 * x4 + 64 * (-4 + x3)), tmp28 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp30 = tl.where(tmp23, tmp25, tmp29)
    tmp31 = tl.full(tmp30.shape, 0.0, tmp30.dtype)
    tmp32 = tl.where(tmp18, tmp30, tmp31)
    tmp33 = tl.where(tmp4, tmp17, tmp32)
    tl.store(out_ptr0 + x5, tmp33, xmask)


@triton.jit
def triton_per_fused_mean_pow_sub_sum_1(in_out_ptr0, in_ptr0, in_ptr1,
    xnumel, rnumel):
    XBLOCK: tl.constexpr = 1
    RBLOCK: tl.constexpr = 256
    xoffset = tl.program_id(0) * XBLOCK
    tl.full([1], xoffset, tl.int32)
    tl.full([RBLOCK], True, tl.int1)
    rindex = tl.arange(0, RBLOCK)[:]
    tl.full([RBLOCK], True, tl.int1)
    r0 = rindex
    tmp0 = tl.load(in_ptr0 + r0, None)
    tmp1 = tl.load(in_ptr1 + r0, None)
    tmp4 = tl.load(in_ptr0 + (256 + r0), None)
    tmp2 = tmp0 - tmp1
    tmp3 = tmp2 * tmp2
    tmp5 = tmp4 - tmp1
    tmp6 = tmp5 * tmp5
    tmp7 = tmp3 + tmp6
    tmp8 = tl.broadcast_to(tmp7, [RBLOCK])
    tmp10 = triton_helpers.promote_to_tensor(tl.sum(tmp8, 0))
    tmp11 = 256.0
    tmp12 = tmp10 / tmp11
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp12, None)


def call(args):
    arg0_1, = args
    args.clear()
    assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((8, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_stack_0[grid(512)](arg0_1, buf0, 512, XBLOCK=128,
            num_warps=4, num_stages=1)
        buf1 = empty_strided_cuda((), (), torch.float32)
        buf2 = buf1
        del buf1
        triton_per_fused_mean_pow_sub_sum_1[grid(1)](buf2, buf0, arg0_1, 1,
            256, num_warps=2, num_stages=1)
        del arg0_1
        del buf0
    return buf2,


class TVLossNew(nn.Module):

    def __init__(self, norm=2):
        super().__init__()
        self.norm = norm

    def extra_repr(self):
        return f'norm={self.norm}'

    def forward(self, input_0):
        arg0_1 = input_0
        output = call([arg0_1])
        return output[0]


Measured latency_ms: 0.027554