# 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_4, sum_4, sum_5, mul_8, sum_7, sum_8, mul_12, sum_10, sum_11], Original ATen: [aten.mul, aten.sum]
# Source node to ATen node mapping:
#   mul => mul
#   mul_12 => mul_12
#   mul_4 => mul_4
#   mul_8 => mul_8
#   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_4 : [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_4, [2, 3]), kwargs = {})
#   %sum_5 : [num_users=1] = call_function[target=torch.ops.aten.sum.dim_IntList](args = (%arg0_1, [2, 3]), kwargs = {})
#   %mul_8 : [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_8, [2, 3]), kwargs = {})
#   %sum_8 : [num_users=1] = call_function[target=torch.ops.aten.sum.dim_IntList](args = (%arg0_1, [2, 3]), kwargs = {})
#   %mul_12 : [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_12, [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/g2/cg2t7i23iyzkf5rcqvd7pnpwsm465s5isw6ejryzkmmukuy3sq4n.py
# Topologically Sorted Source Nodes: [region_level, mul_1, mul_2, region_level_loss, sum_3, level_set_loss, region_level_1, mul_5, mul_6, region_level_loss_1, sum_6, level_set_loss_1, region_level_2, mul_9, mul_10, region_level_loss_2, sum_9, level_set_loss_2, region_level_3, mul_13, mul_14, region_level_loss_3, sum_12, level_set_loss_3], Original ATen: [aten.sub, aten.mul, aten.sum, aten.add]
# Source node to ATen node mapping:
#   level_set_loss => add
#   level_set_loss_1 => add_1
#   level_set_loss_2 => add_2
#   level_set_loss_3 => add_3
#   mul_1 => mul_1
#   mul_10 => mul_10
#   mul_13 => mul_13
#   mul_14 => mul_14
#   mul_2 => mul_2
#   mul_5 => mul_5
#   mul_6 => mul_6
#   mul_9 => mul_9
#   region_level => sub
#   region_level_1 => sub_1
#   region_level_2 => sub_2
#   region_level_3 => sub_3
#   region_level_loss => mul_3
#   region_level_loss_1 => mul_7
#   region_level_loss_2 => mul_11
#   region_level_loss_3 => mul_15
#   sum_12 => sum_12
#   sum_3 => sum_3
#   sum_6 => sum_6
#   sum_9 => sum_9
# Graph fragment:
#   %sub : [num_users=2] = 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 = (%arg2_1, %sub), kwargs = {})
#   %mul_2 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%mul_1, %sub), kwargs = {})
#   %mul_3 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%mul_2, %arg0_1), kwargs = {})
#   %sum_3 : [num_users=1] = call_function[target=torch.ops.aten.sum.default](args = (%mul_3,), kwargs = {})
#   %add : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%sum_3, 0.0), kwargs = {})
#   %sub_1 : [num_users=2] = call_function[target=torch.ops.aten.sub.Tensor](args = (%expand_2, %expand_3), kwargs = {})
#   %mul_5 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%arg2_1, %sub_1), kwargs = {})
#   %mul_6 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%mul_5, %sub_1), kwargs = {})
#   %mul_7 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%mul_6, %arg0_1), kwargs = {})
#   %sum_6 : [num_users=1] = call_function[target=torch.ops.aten.sum.default](args = (%mul_7,), kwargs = {})
#   %add_1 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%add, %sum_6), kwargs = {})
#   %sub_2 : [num_users=2] = call_function[target=torch.ops.aten.sub.Tensor](args = (%expand_4, %expand_5), kwargs = {})
#   %mul_9 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%arg2_1, %sub_2), kwargs = {})
#   %mul_10 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%mul_9, %sub_2), kwargs = {})
#   %mul_11 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%mul_10, %arg0_1), kwargs = {})
#   %sum_9 : [num_users=1] = call_function[target=torch.ops.aten.sum.default](args = (%mul_11,), kwargs = {})
#   %add_2 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%add_1, %sum_9), kwargs = {})
#   %sub_3 : [num_users=2] = call_function[target=torch.ops.aten.sub.Tensor](args = (%expand_6, %expand_7), kwargs = {})
#   %mul_13 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%arg2_1, %sub_3), kwargs = {})
#   %mul_14 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%mul_13, %sub_3), kwargs = {})
#   %mul_15 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%mul_14, %arg0_1), kwargs = {})
#   %sum_12 : [num_users=1] = call_function[target=torch.ops.aten.sum.default](args = (%mul_15,), kwargs = {})
#   %add_3 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%add_2, %sum_12), kwargs = {})
triton_per_fused_add_mul_sub_sum_1 = async_compile.triton('triton_per_fused_add_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: 'i32', 13: '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': {12: 1}, 'configs': [AttrsDescriptor(divisible_by_16=(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13), equal_to_1=(12,))]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_per_fused_add_mul_sub_sum_1', 'mutated_arg_names': ['in_out_ptr0'], 'no_x_dim': True, 'num_load': 14, '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_add_mul_sub_sum_1(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, in_ptr5, in_ptr6, in_ptr7, in_ptr8, in_ptr9, in_ptr10, 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)
    r3 = rindex
    r0 = rindex % 16
    r2 = (rindex // 64)
    r4 = (rindex // 16)
    tmp0 = tl.load(in_ptr0 + (r3), None)
    tmp1 = tl.load(in_ptr1 + (r0 + (64*r2)), None, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr2 + (r4), None, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr3 + (r4), None, eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr4 + (r3), None)
    tmp13 = tl.load(in_ptr1 + (16 + r0 + (64*r2)), None, eviction_policy='evict_last')
    tmp14 = tl.load(in_ptr5 + (r4), None, eviction_policy='evict_last')
    tmp15 = tl.load(in_ptr6 + (r4), None, eviction_policy='evict_last')
    tmp24 = tl.load(in_ptr1 + (32 + r0 + (64*r2)), None, eviction_policy='evict_last')
    tmp25 = tl.load(in_ptr7 + (r4), None, eviction_policy='evict_last')
    tmp26 = tl.load(in_ptr8 + (r4), None, eviction_policy='evict_last')
    tmp35 = tl.load(in_ptr1 + (48 + r0 + (64*r2)), None, eviction_policy='evict_last')
    tmp36 = tl.load(in_ptr9 + (r4), None, eviction_policy='evict_last')
    tmp37 = tl.load(in_ptr10 + (r4), None, eviction_policy='evict_last')
    tmp4 = tmp2 / tmp3
    tmp5 = tmp1 - tmp4
    tmp6 = tmp0 * tmp5
    tmp7 = tmp6 * tmp5
    tmp9 = tmp7 * tmp8
    tmp10 = tl.broadcast_to(tmp9, [RBLOCK])
    tmp12 = triton_helpers.promote_to_tensor(tl.sum(tmp10, 0))
    tmp16 = tmp14 / tmp15
    tmp17 = tmp13 - tmp16
    tmp18 = tmp0 * tmp17
    tmp19 = tmp18 * tmp17
    tmp20 = tmp19 * tmp8
    tmp21 = tl.broadcast_to(tmp20, [RBLOCK])
    tmp23 = triton_helpers.promote_to_tensor(tl.sum(tmp21, 0))
    tmp27 = tmp25 / tmp26
    tmp28 = tmp24 - tmp27
    tmp29 = tmp0 * tmp28
    tmp30 = tmp29 * tmp28
    tmp31 = tmp30 * tmp8
    tmp32 = tl.broadcast_to(tmp31, [RBLOCK])
    tmp34 = triton_helpers.promote_to_tensor(tl.sum(tmp32, 0))
    tmp38 = tmp36 / tmp37
    tmp39 = tmp35 - tmp38
    tmp40 = tmp0 * tmp39
    tmp41 = tmp40 * tmp39
    tmp42 = tmp41 * tmp8
    tmp43 = tl.broadcast_to(tmp42, [RBLOCK])
    tmp45 = triton_helpers.promote_to_tensor(tl.sum(tmp43, 0))
    tmp46 = 0.0
    tmp47 = tmp12 + tmp46
    tmp48 = tmp47 + tmp23
    tmp49 = tmp48 + tmp34
    tmp50 = tmp49 + tmp45
    tl.debug_barrier()
    tl.store(in_out_ptr0 + (tl.full([1], 0, tl.int32)), tmp50, None)
''', device_str='cuda')


async_compile.wait(globals())
del async_compile

def call(args):
    arg0_1, arg1_1, arg2_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))
    assert_size_stride(arg2_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_4, sum_4, sum_5, mul_8, sum_7, sum_8, mul_12, 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)
        buf11 = empty_strided_cuda((), (), torch.float32)
        buf12 = buf11; del buf11  # reuse
        # Topologically Sorted Source Nodes: [region_level, mul_1, mul_2, region_level_loss, sum_3, level_set_loss, region_level_1, mul_5, mul_6, region_level_loss_1, sum_6, level_set_loss_1, region_level_2, mul_9, mul_10, region_level_loss_2, sum_9, level_set_loss_2, region_level_3, mul_13, mul_14, region_level_loss_3, sum_12, level_set_loss_3], Original ATen: [aten.sub, aten.mul, aten.sum, aten.add]
        triton_per_fused_add_mul_sub_sum_1.run(buf12, arg2_1, arg1_1, buf0, buf1, arg0_1, buf3, buf4, buf6, buf7, buf9, buf10, 1, 256, grid=grid(1), stream=stream0)
        del arg0_1
        del arg1_1
        del arg2_1
        del buf0
        del buf1
        del buf10
        del buf3
        del buf4
        del buf6
        del buf7
        del buf9
    return (buf12, )


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)
    arg2_1 = rand_strided((4, 4, 4, 4), (64, 16, 4, 1), device='cuda:0', dtype=torch.float32)
    fn = lambda: call([arg0_1, arg1_1, arg2_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 region_levelset(nn.Module):
    """
    the mian of leveset function
    """

    def __init__(self):
        super(region_levelset, self).__init__()

    def forward(self, mask_score, norm_img, class_weight):
        """
        mask_score: predcited mask scores tensor:(N,C,W,H)
        norm_img: normalizated images tensor:(N,C,W,H)
        class_weight: weight for different classes
        """
        mask_score_shape = mask_score.shape
        norm_img_shape = norm_img.shape
        level_set_loss = 0.0
        for i in range(norm_img_shape[1]):
            norm_img_ = torch.unsqueeze(norm_img[:, i], 1)
            norm_img_ = norm_img_.expand(norm_img_shape[0],
                mask_score_shape[1], norm_img_shape[2], norm_img_shape[3])
            ave_similarity = torch.sum(norm_img_ * mask_score, (2, 3)
                ) / torch.sum(mask_score, (2, 3))
            ave_similarity = ave_similarity.view(norm_img_shape[0],
                mask_score_shape[1], 1, 1)
            region_level = norm_img_ - ave_similarity.expand(norm_img_shape
                [0], mask_score_shape[1], norm_img_shape[2], norm_img_shape[3])
            region_level_loss = (class_weight * region_level * region_level *
                mask_score)
            level_set_loss += torch.sum(region_level_loss)
        return level_set_loss


def get_inputs():
    return [torch.rand([4, 4, 4, 4]), 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
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_add_mul_sub_sum_1(in_out_ptr0, in_ptr0, in_ptr1,
    in_ptr2, in_ptr3, in_ptr4, in_ptr5, in_ptr6, in_ptr7, in_ptr8, in_ptr9,
    in_ptr10, 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)
    r3 = rindex
    r0 = rindex % 16
    r2 = rindex // 64
    r4 = rindex // 16
    tmp0 = tl.load(in_ptr0 + r3, None)
    tmp1 = tl.load(in_ptr1 + (r0 + 64 * r2), None, eviction_policy='evict_last'
        )
    tmp2 = tl.load(in_ptr2 + r4, None, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr3 + r4, None, eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr4 + r3, None)
    tmp13 = tl.load(in_ptr1 + (16 + r0 + 64 * r2), None, eviction_policy=
        'evict_last')
    tmp14 = tl.load(in_ptr5 + r4, None, eviction_policy='evict_last')
    tmp15 = tl.load(in_ptr6 + r4, None, eviction_policy='evict_last')
    tmp24 = tl.load(in_ptr1 + (32 + r0 + 64 * r2), None, eviction_policy=
        'evict_last')
    tmp25 = tl.load(in_ptr7 + r4, None, eviction_policy='evict_last')
    tmp26 = tl.load(in_ptr8 + r4, None, eviction_policy='evict_last')
    tmp35 = tl.load(in_ptr1 + (48 + r0 + 64 * r2), None, eviction_policy=
        'evict_last')
    tmp36 = tl.load(in_ptr9 + r4, None, eviction_policy='evict_last')
    tmp37 = tl.load(in_ptr10 + r4, None, eviction_policy='evict_last')
    tmp4 = tmp2 / tmp3
    tmp5 = tmp1 - tmp4
    tmp6 = tmp0 * tmp5
    tmp7 = tmp6 * tmp5
    tmp9 = tmp7 * tmp8
    tmp10 = tl.broadcast_to(tmp9, [RBLOCK])
    tmp12 = triton_helpers.promote_to_tensor(tl.sum(tmp10, 0))
    tmp16 = tmp14 / tmp15
    tmp17 = tmp13 - tmp16
    tmp18 = tmp0 * tmp17
    tmp19 = tmp18 * tmp17
    tmp20 = tmp19 * tmp8
    tmp21 = tl.broadcast_to(tmp20, [RBLOCK])
    tmp23 = triton_helpers.promote_to_tensor(tl.sum(tmp21, 0))
    tmp27 = tmp25 / tmp26
    tmp28 = tmp24 - tmp27
    tmp29 = tmp0 * tmp28
    tmp30 = tmp29 * tmp28
    tmp31 = tmp30 * tmp8
    tmp32 = tl.broadcast_to(tmp31, [RBLOCK])
    tmp34 = triton_helpers.promote_to_tensor(tl.sum(tmp32, 0))
    tmp38 = tmp36 / tmp37
    tmp39 = tmp35 - tmp38
    tmp40 = tmp0 * tmp39
    tmp41 = tmp40 * tmp39
    tmp42 = tmp41 * tmp8
    tmp43 = tl.broadcast_to(tmp42, [RBLOCK])
    tmp45 = triton_helpers.promote_to_tensor(tl.sum(tmp43, 0))
    tmp46 = 0.0
    tmp47 = tmp12 + tmp46
    tmp48 = tmp47 + tmp23
    tmp49 = tmp48 + tmp34
    tmp50 = tmp49 + tmp45
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp50, None)


def call(args):
    arg0_1, arg1_1, arg2_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))
    assert_size_stride(arg2_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)
        buf11 = empty_strided_cuda((), (), torch.float32)
        buf12 = buf11
        del buf11
        triton_per_fused_add_mul_sub_sum_1[grid(1)](buf12, arg2_1, arg1_1,
            buf0, buf1, arg0_1, buf3, buf4, buf6, buf7, buf9, buf10, 1, 256,
            num_warps=2, num_stages=1)
        del arg0_1
        del arg1_1
        del arg2_1
        del buf0
        del buf1
        del buf10
        del buf3
        del buf4
        del buf6
        del buf7
        del buf9
    return buf12,


class region_levelsetNew(nn.Module):
    """
    the mian of leveset function
    """

    def __init__(self):
        super(region_levelsetNew, self).__init__()

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