# AOT ID: ['0_inference']
from ctypes import c_void_p, c_long, c_int
import torch
import math
import random
import os
import tempfile
from math import inf, nan
from torch._inductor.hooks import run_intermediate_hooks
from torch._inductor.utils import maybe_profile
from torch._inductor.codegen.memory_planning import _align as align
from torch import device, empty_strided
from torch._inductor.async_compile import AsyncCompile
from torch._inductor.select_algorithm import extern_kernels
from torch._inductor.codegen.multi_kernel import MultiKernelCall
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid, split_scan_grid, grid_combo_kernels, start_graph, end_graph
from torch._C import _cuda_getCurrentRawStream as get_raw_stream

aten = torch.ops.aten
inductor_ops = torch.ops.inductor
_quantized = torch.ops._quantized
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cpu = torch._C._dynamo.guards._empty_strided_cpu
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
empty_strided_xpu = torch._C._dynamo.guards._empty_strided_xpu
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
alloc_from_pool = torch.ops.inductor._alloc_from_pool
async_compile = AsyncCompile()


# kernel path: runs/run_shard_1/inductor_cache/mv/cmvz47ch3dym7sclktj42hufe6c4fpig64zsi573kcvcgtkqiuye.py
# Topologically Sorted Source Nodes: [mul, sum_1, sum_2, mul_3, sum_4, sum_5, mul_6, sum_7, sum_8, mul_9, sum_10, sum_11], Original ATen: [aten.mul, aten.sum]
# Source node to ATen node mapping:
#   mul => mul
#   mul_3 => mul_3
#   mul_6 => mul_6
#   mul_9 => mul_9
#   sum_1 => sum_1
#   sum_10 => sum_10
#   sum_11 => sum_11
#   sum_2 => sum_2
#   sum_4 => sum_4
#   sum_5 => sum_5
#   sum_7 => sum_7
#   sum_8 => sum_8
# Graph fragment:
#   %mul : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%expand, %arg0_1), kwargs = {})
#   %sum_1 : [num_users=1] = call_function[target=torch.ops.aten.sum.dim_IntList](args = (%mul, [2, 3]), kwargs = {})
#   %sum_2 : [num_users=1] = call_function[target=torch.ops.aten.sum.dim_IntList](args = (%arg0_1, [2, 3]), kwargs = {})
#   %mul_3 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%expand_2, %arg0_1), kwargs = {})
#   %sum_4 : [num_users=1] = call_function[target=torch.ops.aten.sum.dim_IntList](args = (%mul_3, [2, 3]), kwargs = {})
#   %sum_5 : [num_users=1] = call_function[target=torch.ops.aten.sum.dim_IntList](args = (%arg0_1, [2, 3]), kwargs = {})
#   %mul_6 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%expand_4, %arg0_1), kwargs = {})
#   %sum_7 : [num_users=1] = call_function[target=torch.ops.aten.sum.dim_IntList](args = (%mul_6, [2, 3]), kwargs = {})
#   %sum_8 : [num_users=1] = call_function[target=torch.ops.aten.sum.dim_IntList](args = (%arg0_1, [2, 3]), kwargs = {})
#   %mul_9 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%expand_6, %arg0_1), kwargs = {})
#   %sum_10 : [num_users=1] = call_function[target=torch.ops.aten.sum.dim_IntList](args = (%mul_9, [2, 3]), kwargs = {})
#   %sum_11 : [num_users=1] = call_function[target=torch.ops.aten.sum.dim_IntList](args = (%arg0_1, [2, 3]), kwargs = {})
triton_per_fused_mul_sum_0 = async_compile.triton('triton_per_fused_mul_sum_0', '''
import triton
import triton.language as tl
from triton.compiler.compiler import AttrsDescriptor

from torch._inductor.runtime import triton_helpers, triton_heuristics
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
from torch._inductor.runtime.hints import AutotuneHint, ReductionHint, TileHint, instance_descriptor, DeviceProperties

@triton_heuristics.persistent_reduction(
    size_hints=[16, 16],
    reduction_hint=ReductionHint.DEFAULT,
    filename=__file__,
    triton_meta={'signature': {0: '*fp32', 1: '*fp32', 2: '*fp32', 3: '*fp32', 4: '*fp32', 5: '*fp32', 6: '*fp32', 7: '*fp32', 8: '*fp32', 9: '*fp32', 10: 'i32', 11: 'i32'}, 'device': DeviceProperties(type='cuda', index=0, cc=90, major=9, regs_per_multiprocessor=65536, max_threads_per_multi_processor=2048, multi_processor_count=132), 'constants': {}, 'configs': [AttrsDescriptor(divisible_by_16=(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11), equal_to_1=())]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_per_fused_mul_sum_0', 'mutated_arg_names': [], 'no_x_dim': False, 'num_load': 5, 'num_reduction': 8, 'backend_hash': 'B098E03CDA7B8ADC90DAFFDF24A2956451D1B13F297756A5DCC209498AA53705', 'are_deterministic_algorithms_enabled': False, 'assert_indirect_indexing': True, 'autotune_local_cache': True, 'autotune_pointwise': True, 'autotune_remote_cache': None, 'force_disable_caches': False, 'dynamic_scale_rblock': True, 'max_autotune': False, 'max_autotune_pointwise': False, 'min_split_scan_rblock': 256, 'spill_threshold': 16, 'store_cubin': False}
)
@triton.jit
def triton_per_fused_mul_sum_0(in_ptr0, in_ptr1, out_ptr0, out_ptr1, out_ptr2, out_ptr3, out_ptr4, out_ptr5, out_ptr6, out_ptr7, xnumel, rnumel, XBLOCK : tl.constexpr):
    xnumel = 16
    rnumel = 16
    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, :]
    roffset = 0
    rmask = tl.full([XBLOCK, RBLOCK], True, tl.int1)
    r2 = rindex
    x1 = (xindex // 4)
    x3 = xindex
    tmp0 = tl.load(in_ptr0 + (r2 + (64*x1)), xmask, eviction_policy='evict_last', other=0.0)
    tmp1 = tl.load(in_ptr1 + (r2 + (16*x3)), xmask, other=0.0)
    tmp11 = tl.load(in_ptr0 + (16 + r2 + (64*x1)), xmask, eviction_policy='evict_last', other=0.0)
    tmp17 = tl.load(in_ptr0 + (32 + r2 + (64*x1)), xmask, eviction_policy='evict_last', other=0.0)
    tmp23 = tl.load(in_ptr0 + (48 + r2 + (64*x1)), xmask, eviction_policy='evict_last', other=0.0)
    tmp2 = tmp0 * tmp1
    tmp3 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK])
    tmp5 = tl.where(xmask, tmp3, 0)
    tmp6 = tl.sum(tmp5, 1)[:, None]
    tmp7 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK])
    tmp9 = tl.where(xmask, tmp7, 0)
    tmp10 = tl.sum(tmp9, 1)[:, None]
    tmp12 = tmp11 * tmp1
    tmp13 = tl.broadcast_to(tmp12, [XBLOCK, RBLOCK])
    tmp15 = tl.where(xmask, tmp13, 0)
    tmp16 = tl.sum(tmp15, 1)[:, None]
    tmp18 = tmp17 * tmp1
    tmp19 = tl.broadcast_to(tmp18, [XBLOCK, RBLOCK])
    tmp21 = tl.where(xmask, tmp19, 0)
    tmp22 = tl.sum(tmp21, 1)[:, None]
    tmp24 = tmp23 * tmp1
    tmp25 = tl.broadcast_to(tmp24, [XBLOCK, RBLOCK])
    tmp27 = tl.where(xmask, tmp25, 0)
    tmp28 = tl.sum(tmp27, 1)[:, None]
    tl.store(out_ptr0 + (x3), tmp6, xmask)
    tl.store(out_ptr1 + (x3), tmp10, xmask)
    tl.store(out_ptr2 + (x3), tmp16, xmask)
    tl.store(out_ptr3 + (x3), tmp10, xmask)
    tl.store(out_ptr4 + (x3), tmp22, xmask)
    tl.store(out_ptr5 + (x3), tmp10, xmask)
    tl.store(out_ptr6 + (x3), tmp28, xmask)
    tl.store(out_ptr7 + (x3), tmp10, xmask)
''', device_str='cuda')


# kernel path: runs/run_shard_1/inductor_cache/h2/ch2d42btsiytszj7kjoxvre7y6lc4ymkjcoznqk2c25dn2x66ban.py
# Topologically Sorted Source Nodes: [plevel, mul_1, pLoss, sum_3, plevel_1, mul_4, pLoss_1, sum_6, plevel_2, mul_7, pLoss_2, sum_9, plevel_3, mul_10, pLoss_3, sum_12], Original ATen: [aten.sub, aten.mul, aten.sum]
# Source node to ATen node mapping:
#   mul_1 => mul_1
#   mul_10 => mul_10
#   mul_4 => mul_4
#   mul_7 => mul_7
#   pLoss => mul_2
#   pLoss_1 => mul_5
#   pLoss_2 => mul_8
#   pLoss_3 => mul_11
#   plevel => sub
#   plevel_1 => sub_1
#   plevel_2 => sub_2
#   plevel_3 => sub_3
#   sum_12 => sum_12
#   sum_3 => sum_3
#   sum_6 => sum_6
#   sum_9 => sum_9
# Graph fragment:
#   %sub : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (%expand, %expand_1), kwargs = {})
#   %mul_1 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%sub, %sub), kwargs = {})
#   %mul_2 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%mul_1, %arg0_1), kwargs = {})
#   %sum_3 : [num_users=1] = call_function[target=torch.ops.aten.sum.default](args = (%mul_2,), kwargs = {})
#   %sub_1 : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (%expand_2, %expand_3), kwargs = {})
#   %mul_4 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%sub_1, %sub_1), kwargs = {})
#   %mul_5 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%mul_4, %arg0_1), kwargs = {})
#   %sum_6 : [num_users=1] = call_function[target=torch.ops.aten.sum.default](args = (%mul_5,), kwargs = {})
#   %sub_2 : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (%expand_4, %expand_5), kwargs = {})
#   %mul_7 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%sub_2, %sub_2), kwargs = {})
#   %mul_8 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%mul_7, %arg0_1), kwargs = {})
#   %sum_9 : [num_users=1] = call_function[target=torch.ops.aten.sum.default](args = (%mul_8,), kwargs = {})
#   %sub_3 : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (%expand_6, %expand_7), kwargs = {})
#   %mul_10 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%sub_3, %sub_3), kwargs = {})
#   %mul_11 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%mul_10, %arg0_1), kwargs = {})
#   %sum_12 : [num_users=1] = call_function[target=torch.ops.aten.sum.default](args = (%mul_11,), kwargs = {})
triton_per_fused_mul_sub_sum_1 = async_compile.triton('triton_per_fused_mul_sub_sum_1', '''
import triton
import triton.language as tl
from triton.compiler.compiler import AttrsDescriptor

from torch._inductor.runtime import triton_helpers, triton_heuristics
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
from torch._inductor.runtime.hints import AutotuneHint, ReductionHint, TileHint, instance_descriptor, DeviceProperties

@triton_heuristics.persistent_reduction(
    size_hints=[1, 256],
    reduction_hint=ReductionHint.INNER,
    filename=__file__,
    triton_meta={'signature': {0: '*fp32', 1: '*fp32', 2: '*fp32', 3: '*fp32', 4: '*fp32', 5: '*fp32', 6: '*fp32', 7: '*fp32', 8: '*fp32', 9: '*fp32', 10: '*fp32', 11: '*fp32', 12: '*fp32', 13: '*fp32', 14: 'i32', 15: 'i32'}, 'device': DeviceProperties(type='cuda', index=0, cc=90, major=9, regs_per_multiprocessor=65536, max_threads_per_multi_processor=2048, multi_processor_count=132), 'constants': {14: 1}, 'configs': [AttrsDescriptor(divisible_by_16=(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15), equal_to_1=(14,))]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_per_fused_mul_sub_sum_1', 'mutated_arg_names': [], 'no_x_dim': True, 'num_load': 13, 'num_reduction': 4, 'backend_hash': 'B098E03CDA7B8ADC90DAFFDF24A2956451D1B13F297756A5DCC209498AA53705', 'are_deterministic_algorithms_enabled': False, 'assert_indirect_indexing': True, 'autotune_local_cache': True, 'autotune_pointwise': True, 'autotune_remote_cache': None, 'force_disable_caches': False, 'dynamic_scale_rblock': True, 'max_autotune': False, 'max_autotune_pointwise': False, 'min_split_scan_rblock': 256, 'spill_threshold': 16, 'store_cubin': False}
)
@triton.jit
def triton_per_fused_mul_sub_sum_1(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, in_ptr5, in_ptr6, in_ptr7, in_ptr8, in_ptr9, out_ptr0, out_ptr1, out_ptr2, out_ptr3, xnumel, rnumel):
    xnumel = 1
    XBLOCK: tl.constexpr = 1
    rnumel = 256
    RBLOCK: tl.constexpr = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = tl.full([1], xoffset, tl.int32)
    xmask = tl.full([RBLOCK], True, tl.int1)
    rindex = tl.arange(0, RBLOCK)[:]
    roffset = 0
    rmask = tl.full([RBLOCK], True, tl.int1)
    r0 = rindex % 16
    r2 = (rindex // 64)
    r3 = (rindex // 16)
    r4 = rindex
    tmp0 = tl.load(in_ptr0 + (r0 + (64*r2)), None, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr1 + (r3), None, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr2 + (r3), None, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr3 + (r4), None)
    tmp11 = tl.load(in_ptr0 + (16 + r0 + (64*r2)), None, eviction_policy='evict_last')
    tmp12 = tl.load(in_ptr4 + (r3), None, eviction_policy='evict_last')
    tmp13 = tl.load(in_ptr5 + (r3), None, eviction_policy='evict_last')
    tmp21 = tl.load(in_ptr0 + (32 + r0 + (64*r2)), None, eviction_policy='evict_last')
    tmp22 = tl.load(in_ptr6 + (r3), None, eviction_policy='evict_last')
    tmp23 = tl.load(in_ptr7 + (r3), None, eviction_policy='evict_last')
    tmp31 = tl.load(in_ptr0 + (48 + r0 + (64*r2)), None, eviction_policy='evict_last')
    tmp32 = tl.load(in_ptr8 + (r3), None, eviction_policy='evict_last')
    tmp33 = tl.load(in_ptr9 + (r3), None, eviction_policy='evict_last')
    tmp3 = tmp1 / tmp2
    tmp4 = tmp0 - tmp3
    tmp5 = tmp4 * tmp4
    tmp7 = tmp5 * tmp6
    tmp8 = tl.broadcast_to(tmp7, [RBLOCK])
    tmp10 = triton_helpers.promote_to_tensor(tl.sum(tmp8, 0))
    tmp14 = tmp12 / tmp13
    tmp15 = tmp11 - tmp14
    tmp16 = tmp15 * tmp15
    tmp17 = tmp16 * tmp6
    tmp18 = tl.broadcast_to(tmp17, [RBLOCK])
    tmp20 = triton_helpers.promote_to_tensor(tl.sum(tmp18, 0))
    tmp24 = tmp22 / tmp23
    tmp25 = tmp21 - tmp24
    tmp26 = tmp25 * tmp25
    tmp27 = tmp26 * tmp6
    tmp28 = tl.broadcast_to(tmp27, [RBLOCK])
    tmp30 = triton_helpers.promote_to_tensor(tl.sum(tmp28, 0))
    tmp34 = tmp32 / tmp33
    tmp35 = tmp31 - tmp34
    tmp36 = tmp35 * tmp35
    tmp37 = tmp36 * tmp6
    tmp38 = tl.broadcast_to(tmp37, [RBLOCK])
    tmp40 = triton_helpers.promote_to_tensor(tl.sum(tmp38, 0))
    tl.store(out_ptr0 + (tl.full([1], 0, tl.int32)), tmp10, None)
    tl.store(out_ptr1 + (tl.full([1], 0, tl.int32)), tmp20, None)
    tl.store(out_ptr2 + (tl.full([1], 0, tl.int32)), tmp30, None)
    tl.store(out_ptr3 + (tl.full([1], 0, tl.int32)), tmp40, None)
''', device_str='cuda')


# kernel path: runs/run_shard_1/inductor_cache/gp/cgpnpjcg7zpsdulkqz2it5s6dulyayunkfcfizni2bezjjfrofuy.py
# Topologically Sorted Source Nodes: [loss, loss_1, loss_2, loss_3, sub_4, dH, sum_13, sub_5, dW, sum_14, loss_4, add_2], Original ATen: [aten.add, aten.sub, aten.abs, aten.sum]
# Source node to ATen node mapping:
#   add_2 => add_5
#   dH => abs_1
#   dW => abs_2
#   loss => add
#   loss_1 => add_1
#   loss_2 => add_2
#   loss_3 => add_3
#   loss_4 => add_4
#   sub_4 => sub_4
#   sub_5 => sub_5
#   sum_13 => sum_13
#   sum_14 => sum_14
# Graph fragment:
#   %add : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%sum_3, 0.0), kwargs = {})
#   %add_1 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%add, %sum_6), kwargs = {})
#   %add_2 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%add_1, %sum_9), kwargs = {})
#   %add_3 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%add_2, %sum_12), kwargs = {})
#   %sub_4 : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (%slice_7, %slice_11), kwargs = {})
#   %abs_1 : [num_users=1] = call_function[target=torch.ops.aten.abs.default](args = (%sub_4,), kwargs = {})
#   %sum_13 : [num_users=1] = call_function[target=torch.ops.aten.sum.default](args = (%abs_1,), kwargs = {})
#   %sub_5 : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (%slice_16, %slice_20), kwargs = {})
#   %abs_2 : [num_users=1] = call_function[target=torch.ops.aten.abs.default](args = (%sub_5,), kwargs = {})
#   %sum_14 : [num_users=1] = call_function[target=torch.ops.aten.sum.default](args = (%abs_2,), kwargs = {})
#   %add_4 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%sum_13, %sum_14), kwargs = {})
#   %add_5 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%add_3, %add_4), kwargs = {})
triton_per_fused_abs_add_sub_sum_2 = async_compile.triton('triton_per_fused_abs_add_sub_sum_2', '''
import triton
import triton.language as tl
from triton.compiler.compiler import AttrsDescriptor

from torch._inductor.runtime import triton_helpers, triton_heuristics
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
from torch._inductor.runtime.hints import AutotuneHint, ReductionHint, TileHint, instance_descriptor, DeviceProperties

@triton_heuristics.persistent_reduction(
    size_hints=[1, 256],
    reduction_hint=ReductionHint.INNER,
    filename=__file__,
    triton_meta={'signature': {0: '*fp32', 1: '*fp32', 2: '*fp32', 3: '*fp32', 4: '*fp32', 5: 'i32', 6: 'i32'}, 'device': DeviceProperties(type='cuda', index=0, cc=90, major=9, regs_per_multiprocessor=65536, max_threads_per_multi_processor=2048, multi_processor_count=132), 'constants': {5: 1}, 'configs': [AttrsDescriptor(divisible_by_16=(0, 1, 2, 3, 4, 6), equal_to_1=(5,))]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_per_fused_abs_add_sub_sum_2', 'mutated_arg_names': ['in_out_ptr0'], 'no_x_dim': False, 'num_load': 8, 'num_reduction': 2, 'backend_hash': 'B098E03CDA7B8ADC90DAFFDF24A2956451D1B13F297756A5DCC209498AA53705', 'are_deterministic_algorithms_enabled': False, 'assert_indirect_indexing': True, 'autotune_local_cache': True, 'autotune_pointwise': True, 'autotune_remote_cache': None, 'force_disable_caches': False, 'dynamic_scale_rblock': True, 'max_autotune': False, 'max_autotune_pointwise': False, 'min_split_scan_rblock': 256, 'spill_threshold': 16, 'store_cubin': False}
)
@triton.jit
def triton_per_fused_abs_add_sub_sum_2(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, in_ptr3, xnumel, rnumel, XBLOCK : tl.constexpr):
    xnumel = 1
    rnumel = 192
    RBLOCK: tl.constexpr = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = tl.full([XBLOCK, RBLOCK], True, tl.int1)
    rindex = tl.arange(0, RBLOCK)[None, :]
    roffset = 0
    rmask = rindex < rnumel
    r0 = rindex % 12
    r1 = (rindex // 12)
    r2 = rindex % 3
    r3 = (rindex // 3)
    tmp0 = tl.load(in_ptr0 + (4 + r0 + (16*r1)), rmask, other=0.0)
    tmp1 = tl.load(in_ptr0 + (r0 + (16*r1)), rmask, other=0.0)
    tmp8 = tl.load(in_ptr0 + (1 + r2 + (4*r3)), rmask, other=0.0)
    tmp9 = tl.load(in_ptr0 + (r2 + (4*r3)), rmask, other=0.0)
    tmp16 = tl.load(in_ptr1 + (0))
    tmp17 = tl.broadcast_to(tmp16, [XBLOCK, 1])
    tmp20 = tl.load(in_ptr2 + (0))
    tmp21 = tl.broadcast_to(tmp20, [XBLOCK, 1])
    tmp23 = tl.load(in_ptr3 + (0))
    tmp24 = tl.broadcast_to(tmp23, [XBLOCK, 1])
    tmp26 = tl.load(in_out_ptr0 + (0))
    tmp27 = tl.broadcast_to(tmp26, [XBLOCK, 1])
    tmp2 = tmp0 - tmp1
    tmp3 = tl_math.abs(tmp2)
    tmp4 = tl.broadcast_to(tmp3, [XBLOCK, RBLOCK])
    tmp6 = tl.where(rmask, tmp4, 0)
    tmp7 = tl.sum(tmp6, 1)[:, None]
    tmp10 = tmp8 - tmp9
    tmp11 = tl_math.abs(tmp10)
    tmp12 = tl.broadcast_to(tmp11, [XBLOCK, RBLOCK])
    tmp14 = tl.where(rmask, tmp12, 0)
    tmp15 = tl.sum(tmp14, 1)[:, None]
    tmp18 = 0.0
    tmp19 = tmp17 + tmp18
    tmp22 = tmp19 + tmp21
    tmp25 = tmp22 + tmp24
    tmp28 = tmp25 + tmp27
    tmp29 = tmp7 + tmp15
    tmp30 = tmp28 + tmp29
    tl.debug_barrier()
    tl.store(in_out_ptr0 + (tl.full([XBLOCK, 1], 0, tl.int32)), tmp30, None)
''', device_str='cuda')


async_compile.wait(globals())
del async_compile

def call(args):
    arg0_1, arg1_1 = args
    args.clear()
    assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf3 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf4 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf6 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf7 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf9 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf10 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        # Topologically Sorted Source Nodes: [mul, sum_1, sum_2, mul_3, sum_4, sum_5, mul_6, sum_7, sum_8, mul_9, sum_10, sum_11], Original ATen: [aten.mul, aten.sum]
        stream0 = get_raw_stream(0)
        triton_per_fused_mul_sum_0.run(arg1_1, arg0_1, buf0, buf1, buf3, buf4, buf6, buf7, buf9, buf10, 16, 16, grid=grid(16), stream=stream0)
        buf2 = empty_strided_cuda((), (), torch.float32)
        buf5 = empty_strided_cuda((), (), torch.float32)
        buf8 = empty_strided_cuda((), (), torch.float32)
        buf11 = empty_strided_cuda((), (), torch.float32)
        # Topologically Sorted Source Nodes: [plevel, mul_1, pLoss, sum_3, plevel_1, mul_4, pLoss_1, sum_6, plevel_2, mul_7, pLoss_2, sum_9, plevel_3, mul_10, pLoss_3, sum_12], Original ATen: [aten.sub, aten.mul, aten.sum]
        triton_per_fused_mul_sub_sum_1.run(arg1_1, buf0, buf1, arg0_1, buf3, buf4, buf6, buf7, buf9, buf10, buf2, buf5, buf8, buf11, 1, 256, grid=grid(1), stream=stream0)
        del arg1_1
        del buf0
        del buf1
        del buf10
        del buf3
        del buf4
        del buf6
        del buf7
        del buf9
        buf14 = buf11; del buf11  # reuse
        # Topologically Sorted Source Nodes: [loss, loss_1, loss_2, loss_3, sub_4, dH, sum_13, sub_5, dW, sum_14, loss_4, add_2], Original ATen: [aten.add, aten.sub, aten.abs, aten.sum]
        triton_per_fused_abs_add_sub_sum_2.run(buf14, arg0_1, buf2, buf5, buf8, 1, 192, grid=grid(1), stream=stream0)
        del arg0_1
        del buf2
        del buf5
        del buf8
    return (buf14, )


def benchmark_compiled_module(times=10, repeat=10):
    from torch._dynamo.testing import rand_strided
    from torch._inductor.utils import print_performance
    arg0_1 = rand_strided((4, 4, 4, 4), (64, 16, 4, 1), device='cuda:0', dtype=torch.float32)
    arg1_1 = rand_strided((4, 4, 4, 4), (64, 16, 4, 1), device='cuda:0', dtype=torch.float32)
    fn = lambda: call([arg0_1, arg1_1])
    return print_performance(fn, times=times, repeat=repeat)


if __name__ == "__main__":
    from torch._inductor.wrapper_benchmark import compiled_module_main
    compiled_module_main('None', benchmark_compiled_module)

import torch
import torch.nn as nn


class MumfordShah_Loss(nn.Module):

    def levelsetLoss(self, output, target, penalty='l1'):
        outshape = output.shape
        tarshape = target.shape
        self.penalty = penalty
        loss = 0.0
        for ich in range(tarshape[1]):
            target_ = torch.unsqueeze(target[:, ich], 1)
            target_ = target_.expand(tarshape[0], outshape[1], tarshape[2],
                tarshape[3])
            pcentroid = torch.sum(target_ * output, (2, 3)) / torch.sum(output,
                (2, 3))
            pcentroid = pcentroid.view(tarshape[0], outshape[1], 1, 1)
            plevel = target_ - pcentroid.expand(tarshape[0], outshape[1],
                tarshape[2], tarshape[3])
            pLoss = plevel * plevel * output
            loss += torch.sum(pLoss)
        return loss

    def gradientLoss2d(self, input):
        dH = torch.abs(input[:, :, 1:, :] - input[:, :, :-1, :])
        dW = torch.abs(input[:, :, :, 1:] - input[:, :, :, :-1])
        if self.penalty == 'l2':
            dH = dH * dH
            dW = dW * dW
        loss = torch.sum(dH) + torch.sum(dW)
        return loss

    def forward(self, image, prediction):
        loss_level = self.levelsetLoss(image, prediction)
        loss_tv = self.gradientLoss2d(image)
        return loss_level + loss_tv


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


def get_init_inputs():
    return [[], {}]

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
from torch._inductor.runtime.triton_helpers import math as tl_math
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_per_fused_mul_sum_0(in_ptr0, in_ptr1, out_ptr0, out_ptr1,
    out_ptr2, out_ptr3, out_ptr4, out_ptr5, out_ptr6, out_ptr7, xnumel,
    rnumel, XBLOCK: tl.constexpr):
    xnumel = 16
    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)
    r2 = rindex
    x1 = xindex // 4
    x3 = xindex
    tmp0 = tl.load(in_ptr0 + (r2 + 64 * x1), xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp1 = tl.load(in_ptr1 + (r2 + 16 * x3), xmask, other=0.0)
    tmp11 = tl.load(in_ptr0 + (16 + r2 + 64 * x1), xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp17 = tl.load(in_ptr0 + (32 + r2 + 64 * x1), xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp23 = tl.load(in_ptr0 + (48 + r2 + 64 * x1), xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp2 = tmp0 * tmp1
    tmp3 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK])
    tmp5 = tl.where(xmask, tmp3, 0)
    tmp6 = tl.sum(tmp5, 1)[:, None]
    tmp7 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK])
    tmp9 = tl.where(xmask, tmp7, 0)
    tmp10 = tl.sum(tmp9, 1)[:, None]
    tmp12 = tmp11 * tmp1
    tmp13 = tl.broadcast_to(tmp12, [XBLOCK, RBLOCK])
    tmp15 = tl.where(xmask, tmp13, 0)
    tmp16 = tl.sum(tmp15, 1)[:, None]
    tmp18 = tmp17 * tmp1
    tmp19 = tl.broadcast_to(tmp18, [XBLOCK, RBLOCK])
    tmp21 = tl.where(xmask, tmp19, 0)
    tmp22 = tl.sum(tmp21, 1)[:, None]
    tmp24 = tmp23 * tmp1
    tmp25 = tl.broadcast_to(tmp24, [XBLOCK, RBLOCK])
    tmp27 = tl.where(xmask, tmp25, 0)
    tmp28 = tl.sum(tmp27, 1)[:, None]
    tl.store(out_ptr0 + x3, tmp6, xmask)
    tl.store(out_ptr1 + x3, tmp10, xmask)
    tl.store(out_ptr2 + x3, tmp16, xmask)
    tl.store(out_ptr3 + x3, tmp10, xmask)
    tl.store(out_ptr4 + x3, tmp22, xmask)
    tl.store(out_ptr5 + x3, tmp10, xmask)
    tl.store(out_ptr6 + x3, tmp28, xmask)
    tl.store(out_ptr7 + x3, tmp10, xmask)


@triton.jit
def triton_per_fused_mul_sub_sum_1(in_ptr0, in_ptr1, in_ptr2, in_ptr3,
    in_ptr4, in_ptr5, in_ptr6, in_ptr7, in_ptr8, in_ptr9, out_ptr0,
    out_ptr1, out_ptr2, out_ptr3, 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 % 16
    r2 = rindex // 64
    r3 = rindex // 16
    r4 = rindex
    tmp0 = tl.load(in_ptr0 + (r0 + 64 * r2), None, eviction_policy='evict_last'
        )
    tmp1 = tl.load(in_ptr1 + r3, None, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr2 + r3, None, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr3 + r4, None)
    tmp11 = tl.load(in_ptr0 + (16 + r0 + 64 * r2), None, eviction_policy=
        'evict_last')
    tmp12 = tl.load(in_ptr4 + r3, None, eviction_policy='evict_last')
    tmp13 = tl.load(in_ptr5 + r3, None, eviction_policy='evict_last')
    tmp21 = tl.load(in_ptr0 + (32 + r0 + 64 * r2), None, eviction_policy=
        'evict_last')
    tmp22 = tl.load(in_ptr6 + r3, None, eviction_policy='evict_last')
    tmp23 = tl.load(in_ptr7 + r3, None, eviction_policy='evict_last')
    tmp31 = tl.load(in_ptr0 + (48 + r0 + 64 * r2), None, eviction_policy=
        'evict_last')
    tmp32 = tl.load(in_ptr8 + r3, None, eviction_policy='evict_last')
    tmp33 = tl.load(in_ptr9 + r3, None, eviction_policy='evict_last')
    tmp3 = tmp1 / tmp2
    tmp4 = tmp0 - tmp3
    tmp5 = tmp4 * tmp4
    tmp7 = tmp5 * tmp6
    tmp8 = tl.broadcast_to(tmp7, [RBLOCK])
    tmp10 = triton_helpers.promote_to_tensor(tl.sum(tmp8, 0))
    tmp14 = tmp12 / tmp13
    tmp15 = tmp11 - tmp14
    tmp16 = tmp15 * tmp15
    tmp17 = tmp16 * tmp6
    tmp18 = tl.broadcast_to(tmp17, [RBLOCK])
    tmp20 = triton_helpers.promote_to_tensor(tl.sum(tmp18, 0))
    tmp24 = tmp22 / tmp23
    tmp25 = tmp21 - tmp24
    tmp26 = tmp25 * tmp25
    tmp27 = tmp26 * tmp6
    tmp28 = tl.broadcast_to(tmp27, [RBLOCK])
    tmp30 = triton_helpers.promote_to_tensor(tl.sum(tmp28, 0))
    tmp34 = tmp32 / tmp33
    tmp35 = tmp31 - tmp34
    tmp36 = tmp35 * tmp35
    tmp37 = tmp36 * tmp6
    tmp38 = tl.broadcast_to(tmp37, [RBLOCK])
    tmp40 = triton_helpers.promote_to_tensor(tl.sum(tmp38, 0))
    tl.store(out_ptr0 + tl.full([1], 0, tl.int32), tmp10, None)
    tl.store(out_ptr1 + tl.full([1], 0, tl.int32), tmp20, None)
    tl.store(out_ptr2 + tl.full([1], 0, tl.int32), tmp30, None)
    tl.store(out_ptr3 + tl.full([1], 0, tl.int32), tmp40, None)


@triton.jit
def triton_per_fused_abs_add_sub_sum_2(in_out_ptr0, in_ptr0, in_ptr1,
    in_ptr2, in_ptr3, xnumel, rnumel, XBLOCK: tl.constexpr):
    rnumel = 192
    RBLOCK: tl.constexpr = 256
    xoffset = tl.program_id(0) * XBLOCK
    xoffset + tl.arange(0, XBLOCK)[:, None]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    rindex = tl.arange(0, RBLOCK)[None, :]
    rmask = rindex < rnumel
    r0 = rindex % 12
    r1 = rindex // 12
    r2 = rindex % 3
    r3 = rindex // 3
    tmp0 = tl.load(in_ptr0 + (4 + r0 + 16 * r1), rmask, other=0.0)
    tmp1 = tl.load(in_ptr0 + (r0 + 16 * r1), rmask, other=0.0)
    tmp8 = tl.load(in_ptr0 + (1 + r2 + 4 * r3), rmask, other=0.0)
    tmp9 = tl.load(in_ptr0 + (r2 + 4 * r3), rmask, other=0.0)
    tmp16 = tl.load(in_ptr1 + 0)
    tmp17 = tl.broadcast_to(tmp16, [XBLOCK, 1])
    tmp20 = tl.load(in_ptr2 + 0)
    tmp21 = tl.broadcast_to(tmp20, [XBLOCK, 1])
    tmp23 = tl.load(in_ptr3 + 0)
    tmp24 = tl.broadcast_to(tmp23, [XBLOCK, 1])
    tmp26 = tl.load(in_out_ptr0 + 0)
    tmp27 = tl.broadcast_to(tmp26, [XBLOCK, 1])
    tmp2 = tmp0 - tmp1
    tmp3 = tl_math.abs(tmp2)
    tmp4 = tl.broadcast_to(tmp3, [XBLOCK, RBLOCK])
    tmp6 = tl.where(rmask, tmp4, 0)
    tmp7 = tl.sum(tmp6, 1)[:, None]
    tmp10 = tmp8 - tmp9
    tmp11 = tl_math.abs(tmp10)
    tmp12 = tl.broadcast_to(tmp11, [XBLOCK, RBLOCK])
    tmp14 = tl.where(rmask, tmp12, 0)
    tmp15 = tl.sum(tmp14, 1)[:, None]
    tmp18 = 0.0
    tmp19 = tmp17 + tmp18
    tmp22 = tmp19 + tmp21
    tmp25 = tmp22 + tmp24
    tmp28 = tmp25 + tmp27
    tmp29 = tmp7 + tmp15
    tmp30 = tmp28 + tmp29
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp30, None)


def call(args):
    arg0_1, arg1_1 = args
    args.clear()
    assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf3 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf4 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf6 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf7 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf9 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf10 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        get_raw_stream(0)
        triton_per_fused_mul_sum_0[grid(16)](arg1_1, arg0_1, buf0, buf1,
            buf3, buf4, buf6, buf7, buf9, buf10, 16, 16, XBLOCK=1,
            num_warps=2, num_stages=1)
        buf2 = empty_strided_cuda((), (), torch.float32)
        buf5 = empty_strided_cuda((), (), torch.float32)
        buf8 = empty_strided_cuda((), (), torch.float32)
        buf11 = empty_strided_cuda((), (), torch.float32)
        triton_per_fused_mul_sub_sum_1[grid(1)](arg1_1, buf0, buf1, arg0_1,
            buf3, buf4, buf6, buf7, buf9, buf10, buf2, buf5, buf8, buf11, 1,
            256, num_warps=2, num_stages=1)
        del arg1_1
        del buf0
        del buf1
        del buf10
        del buf3
        del buf4
        del buf6
        del buf7
        del buf9
        buf14 = buf11
        del buf11
        triton_per_fused_abs_add_sub_sum_2[grid(1)](buf14, arg0_1, buf2,
            buf5, buf8, 1, 192, XBLOCK=1, num_warps=2, num_stages=1)
        del arg0_1
        del buf2
        del buf5
        del buf8
    return buf14,


class MumfordShah_LossNew(nn.Module):

    def levelsetLoss(self, output, target, penalty='l1'):
        outshape = output.shape
        tarshape = target.shape
        self.penalty = penalty
        loss = 0.0
        for ich in range(tarshape[1]):
            target_ = torch.unsqueeze(target[:, ich], 1)
            target_ = target_.expand(tarshape[0], outshape[1], tarshape[2],
                tarshape[3])
            pcentroid = torch.sum(target_ * output, (2, 3)) / torch.sum(output,
                (2, 3))
            pcentroid = pcentroid.view(tarshape[0], outshape[1], 1, 1)
            plevel = target_ - pcentroid.expand(tarshape[0], outshape[1],
                tarshape[2], tarshape[3])
            pLoss = plevel * plevel * output
            loss += torch.sum(pLoss)
        return loss

    def gradientLoss2d(self, input):
        dH = torch.abs(input[:, :, 1:, :] - input[:, :, :-1, :])
        dW = torch.abs(input[:, :, :, 1:] - input[:, :, :, :-1])
        if self.penalty == 'l2':
            dH = dH * dH
            dW = dW * dW
        loss = torch.sum(dH) + torch.sum(dW)
        return loss

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