# 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_4/inductor_cache/xm/cxm7ych4a3r3bcjodg6asqprbzzj4cw6ffxffmk7ejqwx5ah2ldp.py
# Topologically Sorted Source Nodes: [mul, mul_1, add, add_1, gm, truediv, mul_2, mul_3, add_2, add_3, pm, truediv_2, mul_4, truediv_1, truediv_3, mul_5, add_4, abs_1, sub, Lcos, mul_8, mul_7, sub_1, Lmag, mul_9, add_5], Original ATen: [aten.mul, aten.add, aten.sqrt, aten.div, aten.abs, aten.rsub, aten.sub, aten.clamp_min]
# Source node to ATen node mapping:
#   Lcos => mul_6
#   Lmag => clamp_min
#   abs_1 => abs_1
#   add => add
#   add_1 => add_1
#   add_2 => add_2
#   add_3 => add_3
#   add_4 => add_4
#   add_5 => add_5
#   gm => sqrt
#   mul => mul
#   mul_1 => mul_1
#   mul_2 => mul_2
#   mul_3 => mul_3
#   mul_4 => mul_4
#   mul_5 => mul_5
#   mul_7 => mul_7
#   mul_8 => mul_8
#   mul_9 => mul_9
#   pm => sqrt_1
#   sub => sub
#   sub_1 => sub_1
#   truediv => div
#   truediv_1 => div_1
#   truediv_2 => div_2
#   truediv_3 => div_3
# Graph fragment:
#   %mul : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%convolution, %convolution), kwargs = {})
#   %mul_1 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%convolution_1, %convolution_1), kwargs = {})
#   %add : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%mul, %mul_1), kwargs = {})
#   %add_1 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%add, 1e-06), kwargs = {})
#   %sqrt : [num_users=3] = call_function[target=torch.ops.aten.sqrt.default](args = (%add_1,), kwargs = {})
#   %div : [num_users=1] = call_function[target=torch.ops.aten.div.Tensor](args = (%convolution, %sqrt), kwargs = {})
#   %mul_2 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%convolution_2, %convolution_2), kwargs = {})
#   %mul_3 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%convolution_3, %convolution_3), kwargs = {})
#   %add_2 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%mul_2, %mul_3), kwargs = {})
#   %add_3 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%add_2, 1e-06), kwargs = {})
#   %sqrt_1 : [num_users=4] = call_function[target=torch.ops.aten.sqrt.default](args = (%add_3,), kwargs = {})
#   %div_2 : [num_users=1] = call_function[target=torch.ops.aten.div.Tensor](args = (%convolution_2, %sqrt_1), kwargs = {})
#   %mul_4 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%div, %div_2), kwargs = {})
#   %div_1 : [num_users=1] = call_function[target=torch.ops.aten.div.Tensor](args = (%convolution_1, %sqrt), kwargs = {})
#   %div_3 : [num_users=1] = call_function[target=torch.ops.aten.div.Tensor](args = (%convolution_3, %sqrt_1), kwargs = {})
#   %mul_5 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%div_1, %div_3), kwargs = {})
#   %add_4 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%mul_4, %mul_5), kwargs = {})
#   %abs_1 : [num_users=1] = call_function[target=torch.ops.aten.abs.default](args = (%add_4,), kwargs = {})
#   %sub : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (1, %abs_1), kwargs = {})
#   %mul_6 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%sub, %sqrt_1), kwargs = {})
#   %mul_8 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%mul_6, 0.5), kwargs = {})
#   %mul_7 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%sqrt, 1.5), kwargs = {})
#   %sub_1 : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (%mul_7, %sqrt_1), kwargs = {})
#   %clamp_min : [num_users=1] = call_function[target=torch.ops.aten.clamp_min.default](args = (%sub_1, 0), kwargs = {})
#   %mul_9 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%clamp_min, 0.5), kwargs = {})
#   %add_5 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%mul_8, %mul_9), kwargs = {})
triton_poi_fused_abs_add_clamp_min_div_mul_rsub_sqrt_sub_0 = async_compile.triton('triton_poi_fused_abs_add_clamp_min_div_mul_rsub_sqrt_sub_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.pointwise(
    size_hints=[16384], 
    filename=__file__,
    triton_meta={'signature': {0: '*fp32', 1: '*fp32', 2: '*fp32', 3: '*fp32', 4: 'i32'}, 'device': DeviceProperties(type='cuda', index=0, cc=80, major=8, regs_per_multiprocessor=65536, max_threads_per_multi_processor=2048, multi_processor_count=108), 'constants': {}, 'configs': [AttrsDescriptor(divisible_by_16=(0, 1, 2, 3, 4), equal_to_1=())]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_poi_fused_abs_add_clamp_min_div_mul_rsub_sqrt_sub_0', 'mutated_arg_names': ['in_out_ptr0'], 'no_x_dim': False, 'num_load': 4, 'num_reduction': 0, 'backend_hash': 'A9C866B4A14FD3277824029365D703C2427B2E685E54EC9B3EF4ADC8D1EEAC1D', '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},
    min_elem_per_thread=0
)
@triton.jit
def triton_poi_fused_abs_add_clamp_min_div_mul_rsub_sqrt_sub_0(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, xnumel, XBLOCK : tl.constexpr):
    xnumel = 16384
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = tl.full([XBLOCK], True, tl.int1)
    x0 = xindex
    tmp0 = tl.load(in_out_ptr0 + (x0), None)
    tmp2 = tl.load(in_ptr0 + (x0), None)
    tmp9 = tl.load(in_ptr1 + (x0), None)
    tmp11 = tl.load(in_ptr2 + (x0), None)
    tmp1 = tmp0 * tmp0
    tmp3 = tmp2 * tmp2
    tmp4 = tmp1 + tmp3
    tmp5 = 1e-06
    tmp6 = tmp4 + tmp5
    tmp7 = libdevice.sqrt(tmp6)
    tmp8 = tmp0 / tmp7
    tmp10 = tmp9 * tmp9
    tmp12 = tmp11 * tmp11
    tmp13 = tmp10 + tmp12
    tmp14 = tmp13 + tmp5
    tmp15 = libdevice.sqrt(tmp14)
    tmp16 = tmp9 / tmp15
    tmp17 = tmp8 * tmp16
    tmp18 = tmp2 / tmp7
    tmp19 = tmp11 / tmp15
    tmp20 = tmp18 * tmp19
    tmp21 = tmp17 + tmp20
    tmp22 = tl_math.abs(tmp21)
    tmp23 = 1.0
    tmp24 = tmp23 - tmp22
    tmp25 = tmp24 * tmp15
    tmp26 = 0.5
    tmp27 = tmp25 * tmp26
    tmp28 = 1.5
    tmp29 = tmp7 * tmp28
    tmp30 = tmp29 - tmp15
    tmp31 = 0.0
    tmp32 = triton_helpers.maximum(tmp30, tmp31)
    tmp33 = tmp32 * tmp26
    tmp34 = tmp27 + tmp33
    tl.store(in_out_ptr0 + (x0), tmp34, None)
''', device_str='cuda')


# kernel path: runs/run_shard_4/inductor_cache/rt/crtesf3tgdclv4qr5rutmf2tfa2nho6q4ns6plnvp4zyafxujw65.py
# Topologically Sorted Source Nodes: [Lrefine, Lrefine_1], Original ATen: [aten.mul, aten.mean]
# Source node to ATen node mapping:
#   Lrefine => mul_10
#   Lrefine_1 => mean
# Graph fragment:
#   %mul_10 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%add_5, %arg4_1), kwargs = {})
#   %mean : [num_users=1] = call_function[target=torch.ops.aten.mean.default](args = (%mul_10,), kwargs = {})
triton_red_fused_mean_mul_1 = async_compile.triton('triton_red_fused_mean_mul_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.reduction(
    size_hints=[8, 8192],
    reduction_hint=ReductionHint.INNER,
    filename=__file__,
    triton_meta={'signature': {0: '*fp32', 1: '*fp32', 2: '*fp32', 3: 'i32', 4: 'i32'}, 'device': DeviceProperties(type='cuda', index=0, cc=80, major=8, regs_per_multiprocessor=65536, max_threads_per_multi_processor=2048, multi_processor_count=108), 'constants': {}, 'configs': [AttrsDescriptor(divisible_by_16=(0, 1, 2, 4), equal_to_1=())]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_red_fused_mean_mul_1', 'mutated_arg_names': [], 'no_x_dim': False, 'num_load': 2, 'num_reduction': 1, 'backend_hash': 'A9C866B4A14FD3277824029365D703C2427B2E685E54EC9B3EF4ADC8D1EEAC1D', '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_red_fused_mean_mul_1(in_ptr0, in_ptr1, out_ptr0, xnumel, rnumel, XBLOCK : tl.constexpr, RBLOCK : tl.constexpr):
    xnumel = 8
    rnumel = 8192
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    rbase = tl.arange(0, RBLOCK)[None, :]
    x0 = xindex
    _tmp4 = tl.full([XBLOCK, RBLOCK], 0, tl.float32)
    for roffset in range(0, rnumel, RBLOCK):
        rindex = roffset + rbase
        rmask = rindex < rnumel
        r1 = rindex
        tmp0 = tl.load(in_ptr0 + ((4096*((r1 + (8192*x0)) // 16384)) + (r1 % 4096)), rmask & xmask, eviction_policy='evict_last', other=0.0)
        tmp1 = tl.load(in_ptr1 + (r1 + (8192*x0)), rmask & xmask, eviction_policy='evict_first', other=0.0)
        tmp2 = tmp0 * tmp1
        tmp3 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK])
        tmp5 = _tmp4 + tmp3
        _tmp4 = tl.where(rmask & xmask, tmp5, _tmp4)
    tmp4 = tl.sum(_tmp4, 1)[:, None]
    tl.store(out_ptr0 + (x0), tmp4, xmask)
''', device_str='cuda')


# kernel path: runs/run_shard_4/inductor_cache/sl/csl7zqsuuht4isdmcmdndxucluxnhhq774wue4va23y6v7bc3z2b.py
# Topologically Sorted Source Nodes: [Lrefine, Lrefine_1], Original ATen: [aten.mul, aten.mean]
# Source node to ATen node mapping:
#   Lrefine => mul_10
#   Lrefine_1 => mean
# Graph fragment:
#   %mul_10 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%add_5, %arg4_1), kwargs = {})
#   %mean : [num_users=1] = call_function[target=torch.ops.aten.mean.default](args = (%mul_10,), kwargs = {})
triton_per_fused_mean_mul_2 = async_compile.triton('triton_per_fused_mean_mul_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, 8],
    reduction_hint=ReductionHint.INNER,
    filename=__file__,
    triton_meta={'signature': {0: '*fp32', 1: '*fp32', 2: 'i32', 3: 'i32'}, 'device': DeviceProperties(type='cuda', index=0, cc=80, major=8, regs_per_multiprocessor=65536, max_threads_per_multi_processor=2048, multi_processor_count=108), 'constants': {2: 1}, 'configs': [AttrsDescriptor(divisible_by_16=(0, 1), equal_to_1=(2,))]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_per_fused_mean_mul_2', 'mutated_arg_names': ['in_out_ptr0'], 'no_x_dim': False, 'num_load': 1, 'num_reduction': 1, 'backend_hash': 'A9C866B4A14FD3277824029365D703C2427B2E685E54EC9B3EF4ADC8D1EEAC1D', '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_mean_mul_2(in_out_ptr0, in_ptr0, xnumel, rnumel, XBLOCK : tl.constexpr):
    xnumel = 1
    rnumel = 8
    RBLOCK: tl.constexpr = 8
    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 = tl.full([XBLOCK, RBLOCK], True, tl.int1)
    r0 = rindex
    tmp0 = tl.load(in_ptr0 + (r0), None)
    tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
    tmp3 = tl.sum(tmp1, 1)[:, None]
    tmp4 = 65536.0
    tmp5 = tmp3 / tmp4
    tl.debug_barrier()
    tl.store(in_out_ptr0 + (tl.full([XBLOCK, 1], 0, tl.int32)), tmp5, None)
''', device_str='cuda')


async_compile.wait(globals())
del async_compile

def call(args):
    arg0_1, arg1_1, arg2_1, arg3_1, arg4_1 = args
    args.clear()
    assert_size_stride(arg0_1, (1, 1, 3, 3), (9, 9, 3, 1))
    assert_size_stride(arg1_1, (4, 1, 64, 64), (4096, 4096, 64, 1))
    assert_size_stride(arg2_1, (1, 1, 3, 3), (9, 9, 3, 1))
    assert_size_stride(arg3_1, (4, 1, 64, 64), (4096, 4096, 64, 1))
    assert_size_stride(arg4_1, (4, 4, 64, 64), (16384, 4096, 64, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        # Topologically Sorted Source Nodes: [gx], Original ATen: [aten.convolution]
        buf0 = extern_kernels.convolution(arg1_1, arg0_1, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf0, (4, 1, 64, 64), (4096, 4096, 64, 1))
        # Topologically Sorted Source Nodes: [gy], Original ATen: [aten.convolution]
        buf1 = extern_kernels.convolution(arg1_1, arg2_1, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf1, (4, 1, 64, 64), (4096, 4096, 64, 1))
        del arg1_1
        # Topologically Sorted Source Nodes: [px], Original ATen: [aten.convolution]
        buf2 = extern_kernels.convolution(arg3_1, arg0_1, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf2, (4, 1, 64, 64), (4096, 4096, 64, 1))
        del arg0_1
        # Topologically Sorted Source Nodes: [py], Original ATen: [aten.convolution]
        buf3 = extern_kernels.convolution(arg3_1, arg2_1, stride=(1, 1), padding=(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf3, (4, 1, 64, 64), (4096, 4096, 64, 1))
        del arg2_1
        del arg3_1
        buf4 = reinterpret_tensor(buf0, (4, 1, 64, 64), (4096, 16384, 64, 1), 0); del buf0  # reuse
        # Topologically Sorted Source Nodes: [mul, mul_1, add, add_1, gm, truediv, mul_2, mul_3, add_2, add_3, pm, truediv_2, mul_4, truediv_1, truediv_3, mul_5, add_4, abs_1, sub, Lcos, mul_8, mul_7, sub_1, Lmag, mul_9, add_5], Original ATen: [aten.mul, aten.add, aten.sqrt, aten.div, aten.abs, aten.rsub, aten.sub, aten.clamp_min]
        stream0 = get_raw_stream(0)
        triton_poi_fused_abs_add_clamp_min_div_mul_rsub_sqrt_sub_0.run(buf4, buf1, buf2, buf3, 16384, grid=grid(16384), stream=stream0)
        del buf1
        del buf2
        del buf3
        buf5 = empty_strided_cuda((8, ), (1, ), torch.float32)
        # Topologically Sorted Source Nodes: [Lrefine, Lrefine_1], Original ATen: [aten.mul, aten.mean]
        triton_red_fused_mean_mul_1.run(buf4, arg4_1, buf5, 8, 8192, grid=grid(8), stream=stream0)
        del arg4_1
        del buf4
        buf6 = empty_strided_cuda((), (), torch.float32)
        buf7 = buf6; del buf6  # reuse
        # Topologically Sorted Source Nodes: [Lrefine, Lrefine_1], Original ATen: [aten.mul, aten.mean]
        triton_per_fused_mean_mul_2.run(buf7, buf5, 1, 8, grid=grid(1), stream=stream0)
        del buf5
    return (buf7, )


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((1, 1, 3, 3), (9, 9, 3, 1), device='cuda:0', dtype=torch.float32)
    arg1_1 = rand_strided((4, 1, 64, 64), (4096, 4096, 64, 1), device='cuda:0', dtype=torch.float32)
    arg2_1 = rand_strided((1, 1, 3, 3), (9, 9, 3, 1), device='cuda:0', dtype=torch.float32)
    arg3_1 = rand_strided((4, 1, 64, 64), (4096, 4096, 64, 1), device='cuda:0', dtype=torch.float32)
    arg4_1 = rand_strided((4, 4, 64, 64), (16384, 4096, 64, 1), device='cuda:0', dtype=torch.float32)
    fn = lambda: call([arg0_1, arg1_1, arg2_1, arg3_1, arg4_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 numpy as np
import torch.nn as nn


class RefineLoss(nn.Module):

    def __init__(self, alpha=1.5, alpha1=0.5, reduction='mean'):
        super(RefineLoss, self).__init__()
        self.alpha = alpha
        self.alpha1 = alpha1
        self.reduction = reduction
        self.fx = nn.Conv2d(1, 1, 3, padding=1, bias=False)
        self.fy = nn.Conv2d(1, 1, 3, padding=1, bias=False)
        ngx = np.array([[1, 0, -1], [2, 0, -2], [1, 0, -1]], dtype=np.float32)
        ngy = np.array([[1, 2, 1], [0, 0, 0], [-1, -2, -1]], dtype=np.float32)
        self.fx.weight.data.copy_(torch.from_numpy(ngx))
        self.fy.weight.data.copy_(torch.from_numpy(ngy))
        for param in self.fx.parameters():
            param.requires_grad = False
        for param in self.fy.parameters():
            param.requires_grad = False

    def forward(self, grayimg, pred, mask):
        """
        grayimg: gray scale input image
        pred: predicted mask
        mask: boundary mask. can be generate from ground truth foreground mask by  morphological transformation  
        """
        gx = self.fx(grayimg)
        gy = self.fy(grayimg)
        px = self.fx(pred)
        py = self.fy(pred)
        gm = torch.sqrt(gx * gx + gy * gy + 1e-06)
        pm = torch.sqrt(px * px + py * py + 1e-06)
        gv = gx / gm, gy / gm
        pv = px / pm, py / pm
        Lcos = (1 - torch.abs(gv[0] * pv[0] + gv[1] * pv[1])) * pm
        Lmag = torch.clamp_min(self.alpha * gm - pm, 0)
        Lrefine = (self.alpha1 * Lcos + (1 - self.alpha1) * Lmag) * mask
        if self.reduction == 'mean':
            Lrefine = Lrefine.mean()
        elif self.reduction == 'sum':
            Lrefine = Lrefine.sum()
        return Lrefine


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


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

import torch
from torch._inductor.select_algorithm import extern_kernels
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 libdevice, math as tl_math
import numpy as np
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_poi_fused_abs_add_clamp_min_div_mul_rsub_sqrt_sub_0(in_out_ptr0,
    in_ptr0, in_ptr1, in_ptr2, xnumel, XBLOCK: tl.constexpr):
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    tl.full([XBLOCK], True, tl.int1)
    x0 = xindex
    tmp0 = tl.load(in_out_ptr0 + x0, None)
    tmp2 = tl.load(in_ptr0 + x0, None)
    tmp9 = tl.load(in_ptr1 + x0, None)
    tmp11 = tl.load(in_ptr2 + x0, None)
    tmp1 = tmp0 * tmp0
    tmp3 = tmp2 * tmp2
    tmp4 = tmp1 + tmp3
    tmp5 = 1e-06
    tmp6 = tmp4 + tmp5
    tmp7 = libdevice.sqrt(tmp6)
    tmp8 = tmp0 / tmp7
    tmp10 = tmp9 * tmp9
    tmp12 = tmp11 * tmp11
    tmp13 = tmp10 + tmp12
    tmp14 = tmp13 + tmp5
    tmp15 = libdevice.sqrt(tmp14)
    tmp16 = tmp9 / tmp15
    tmp17 = tmp8 * tmp16
    tmp18 = tmp2 / tmp7
    tmp19 = tmp11 / tmp15
    tmp20 = tmp18 * tmp19
    tmp21 = tmp17 + tmp20
    tmp22 = tl_math.abs(tmp21)
    tmp23 = 1.0
    tmp24 = tmp23 - tmp22
    tmp25 = tmp24 * tmp15
    tmp26 = 0.5
    tmp27 = tmp25 * tmp26
    tmp28 = 1.5
    tmp29 = tmp7 * tmp28
    tmp30 = tmp29 - tmp15
    tmp31 = 0.0
    tmp32 = triton_helpers.maximum(tmp30, tmp31)
    tmp33 = tmp32 * tmp26
    tmp34 = tmp27 + tmp33
    tl.store(in_out_ptr0 + x0, tmp34, None)


@triton.jit
def triton_red_fused_mean_mul_1(in_ptr0, in_ptr1, out_ptr0, xnumel, rnumel,
    XBLOCK: tl.constexpr, RBLOCK: tl.constexpr):
    xnumel = 8
    rnumel = 8192
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    rbase = tl.arange(0, RBLOCK)[None, :]
    x0 = xindex
    _tmp4 = tl.full([XBLOCK, RBLOCK], 0, tl.float32)
    for roffset in range(0, rnumel, RBLOCK):
        rindex = roffset + rbase
        rmask = rindex < rnumel
        r1 = rindex
        tmp0 = tl.load(in_ptr0 + (4096 * ((r1 + 8192 * x0) // 16384) + r1 %
            4096), rmask & xmask, eviction_policy='evict_last', other=0.0)
        tmp1 = tl.load(in_ptr1 + (r1 + 8192 * x0), rmask & xmask,
            eviction_policy='evict_first', other=0.0)
        tmp2 = tmp0 * tmp1
        tmp3 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK])
        tmp5 = _tmp4 + tmp3
        _tmp4 = tl.where(rmask & xmask, tmp5, _tmp4)
    tmp4 = tl.sum(_tmp4, 1)[:, None]
    tl.store(out_ptr0 + x0, tmp4, xmask)


@triton.jit
def triton_per_fused_mean_mul_2(in_out_ptr0, in_ptr0, xnumel, rnumel,
    XBLOCK: tl.constexpr):
    RBLOCK: tl.constexpr = 8
    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, :]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    r0 = rindex
    tmp0 = tl.load(in_ptr0 + r0, None)
    tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
    tmp3 = tl.sum(tmp1, 1)[:, None]
    tmp4 = 65536.0
    tmp5 = tmp3 / tmp4
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp5, None)


def call(args):
    arg0_1, arg1_1, arg2_1, arg3_1, arg4_1 = args
    args.clear()
    assert_size_stride(arg0_1, (1, 1, 3, 3), (9, 9, 3, 1))
    assert_size_stride(arg1_1, (4, 1, 64, 64), (4096, 4096, 64, 1))
    assert_size_stride(arg2_1, (1, 1, 3, 3), (9, 9, 3, 1))
    assert_size_stride(arg3_1, (4, 1, 64, 64), (4096, 4096, 64, 1))
    assert_size_stride(arg4_1, (4, 4, 64, 64), (16384, 4096, 64, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = extern_kernels.convolution(arg1_1, arg0_1, stride=(1, 1),
            padding=(1, 1), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf0, (4, 1, 64, 64), (4096, 4096, 64, 1))
        buf1 = extern_kernels.convolution(arg1_1, arg2_1, stride=(1, 1),
            padding=(1, 1), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf1, (4, 1, 64, 64), (4096, 4096, 64, 1))
        del arg1_1
        buf2 = extern_kernels.convolution(arg3_1, arg0_1, stride=(1, 1),
            padding=(1, 1), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf2, (4, 1, 64, 64), (4096, 4096, 64, 1))
        del arg0_1
        buf3 = extern_kernels.convolution(arg3_1, arg2_1, stride=(1, 1),
            padding=(1, 1), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf3, (4, 1, 64, 64), (4096, 4096, 64, 1))
        del arg2_1
        del arg3_1
        buf4 = reinterpret_tensor(buf0, (4, 1, 64, 64), (4096, 16384, 64, 1), 0
            )
        del buf0
        get_raw_stream(0)
        triton_poi_fused_abs_add_clamp_min_div_mul_rsub_sqrt_sub_0[grid(16384)
            ](buf4, buf1, buf2, buf3, 16384, XBLOCK=256, num_warps=4,
            num_stages=1)
        del buf1
        del buf2
        del buf3
        buf5 = empty_strided_cuda((8,), (1,), torch.float32)
        triton_red_fused_mean_mul_1[grid(8)](buf4, arg4_1, buf5, 8, 8192,
            XBLOCK=1, RBLOCK=2048, num_warps=16, num_stages=1)
        del arg4_1
        del buf4
        buf6 = empty_strided_cuda((), (), torch.float32)
        buf7 = buf6
        del buf6
        triton_per_fused_mean_mul_2[grid(1)](buf7, buf5, 1, 8, XBLOCK=1,
            num_warps=2, num_stages=1)
        del buf5
    return buf7,


class RefineLossNew(nn.Module):

    def __init__(self, alpha=1.5, alpha1=0.5, reduction='mean'):
        super(RefineLossNew, self).__init__()
        self.alpha = alpha
        self.alpha1 = alpha1
        self.reduction = reduction
        self.fx = nn.Conv2d(1, 1, 3, padding=1, bias=False)
        self.fy = nn.Conv2d(1, 1, 3, padding=1, bias=False)
        ngx = np.array([[1, 0, -1], [2, 0, -2], [1, 0, -1]], dtype=np.float32)
        ngy = np.array([[1, 2, 1], [0, 0, 0], [-1, -2, -1]], dtype=np.float32)
        self.fx.weight.data.copy_(torch.from_numpy(ngx))
        self.fy.weight.data.copy_(torch.from_numpy(ngy))
        for param in self.fx.parameters():
            param.requires_grad = False
        for param in self.fy.parameters():
            param.requires_grad = False

    def forward(self, input_0, input_1, input_2):
        arg0_1 = self.fx.weight
        arg2_1 = self.fy.weight
        arg1_1 = input_0
        arg3_1 = input_1
        arg4_1 = input_2
        output = call([arg0_1, arg1_1, arg2_1, arg3_1, arg4_1])
        return output[0]
