# 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/xo/cxopkxo47z5lmvyq2gx2n4exgeyhweclzftd73pco236dokn54dm.py
# Topologically Sorted Source Nodes: [image_dx, abs_1, mean, neg, weights_x], Original ATen: [aten.sub, aten.abs, aten.mean, aten.neg, aten.exp]
# Source node to ATen node mapping:
#   abs_1 => abs_1
#   image_dx => sub_2
#   mean => mean
#   neg => neg
#   weights_x => exp
# Graph fragment:
#   %sub_2 : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (%slice_20, %slice_24), kwargs = {})
#   %abs_1 : [num_users=1] = call_function[target=torch.ops.aten.abs.default](args = (%sub_2,), kwargs = {})
#   %mean : [num_users=1] = call_function[target=torch.ops.aten.mean.dim](args = (%abs_1, [1], True), kwargs = {})
#   %neg : [num_users=1] = call_function[target=torch.ops.aten.neg.default](args = (%mean,), kwargs = {})
#   %exp : [num_users=1] = call_function[target=torch.ops.aten.exp.default](args = (%neg,), kwargs = {})
triton_poi_fused_abs_exp_mean_neg_sub_0 = async_compile.triton('triton_poi_fused_abs_exp_mean_neg_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=[64], 
    filename=__file__,
    triton_meta={'signature': {0: '*fp32', 1: '*fp32', 2: '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), equal_to_1=())]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_poi_fused_abs_exp_mean_neg_sub_0', 'mutated_arg_names': [], 'no_x_dim': False, 'num_load': 8, '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_exp_mean_neg_sub_0(in_ptr0, out_ptr0, xnumel, XBLOCK : tl.constexpr):
    xnumel = 48
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 3
    x1 = (xindex // 3) % 4
    x2 = (xindex // 12)
    x3 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + (4*x1) + (64*x2)), xmask)
    tmp1 = tl.load(in_ptr0 + (1 + x0 + (4*x1) + (64*x2)), xmask)
    tmp4 = tl.load(in_ptr0 + (16 + x0 + (4*x1) + (64*x2)), xmask)
    tmp5 = tl.load(in_ptr0 + (17 + x0 + (4*x1) + (64*x2)), xmask)
    tmp9 = tl.load(in_ptr0 + (32 + x0 + (4*x1) + (64*x2)), xmask)
    tmp10 = tl.load(in_ptr0 + (33 + x0 + (4*x1) + (64*x2)), xmask)
    tmp14 = tl.load(in_ptr0 + (48 + x0 + (4*x1) + (64*x2)), xmask)
    tmp15 = tl.load(in_ptr0 + (49 + x0 + (4*x1) + (64*x2)), xmask)
    tmp2 = tmp0 - tmp1
    tmp3 = tl_math.abs(tmp2)
    tmp6 = tmp4 - tmp5
    tmp7 = tl_math.abs(tmp6)
    tmp8 = tmp3 + tmp7
    tmp11 = tmp9 - tmp10
    tmp12 = tl_math.abs(tmp11)
    tmp13 = tmp8 + tmp12
    tmp16 = tmp14 - tmp15
    tmp17 = tl_math.abs(tmp16)
    tmp18 = tmp13 + tmp17
    tmp19 = 4.0
    tmp20 = tmp18 / tmp19
    tmp21 = -tmp20
    tmp22 = tl_math.exp(tmp21)
    tl.store(out_ptr0 + (x3), tmp22, xmask)
''', device_str='cuda')


# kernel path: runs/run_shard_4/inductor_cache/wl/cwl6yd7l3sndnb22swumccpruxx46plry27po4wlv5ktz7r5sl5b.py
# Topologically Sorted Source Nodes: [image_dy, abs_2, mean_1, neg_1, weights_y], Original ATen: [aten.sub, aten.abs, aten.mean, aten.neg, aten.exp]
# Source node to ATen node mapping:
#   abs_2 => abs_2
#   image_dy => sub_3
#   mean_1 => mean_1
#   neg_1 => neg_1
#   weights_y => exp_1
# Graph fragment:
#   %sub_3 : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (%slice_27, %slice_31), kwargs = {})
#   %abs_2 : [num_users=1] = call_function[target=torch.ops.aten.abs.default](args = (%sub_3,), kwargs = {})
#   %mean_1 : [num_users=1] = call_function[target=torch.ops.aten.mean.dim](args = (%abs_2, [1], True), kwargs = {})
#   %neg_1 : [num_users=1] = call_function[target=torch.ops.aten.neg.default](args = (%mean_1,), kwargs = {})
#   %exp_1 : [num_users=1] = call_function[target=torch.ops.aten.exp.default](args = (%neg_1,), kwargs = {})
triton_poi_fused_abs_exp_mean_neg_sub_1 = async_compile.triton('triton_poi_fused_abs_exp_mean_neg_sub_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.pointwise(
    size_hints=[64], 
    filename=__file__,
    triton_meta={'signature': {0: '*fp32', 1: '*fp32', 2: '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), equal_to_1=())]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_poi_fused_abs_exp_mean_neg_sub_1', 'mutated_arg_names': [], 'no_x_dim': False, 'num_load': 8, '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_exp_mean_neg_sub_1(in_ptr0, out_ptr0, xnumel, XBLOCK : tl.constexpr):
    xnumel = 48
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 12
    x1 = (xindex // 12)
    x2 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + (64*x1)), xmask)
    tmp1 = tl.load(in_ptr0 + (4 + x0 + (64*x1)), xmask)
    tmp4 = tl.load(in_ptr0 + (16 + x0 + (64*x1)), xmask)
    tmp5 = tl.load(in_ptr0 + (20 + x0 + (64*x1)), xmask)
    tmp9 = tl.load(in_ptr0 + (32 + x0 + (64*x1)), xmask)
    tmp10 = tl.load(in_ptr0 + (36 + x0 + (64*x1)), xmask)
    tmp14 = tl.load(in_ptr0 + (48 + x0 + (64*x1)), xmask)
    tmp15 = tl.load(in_ptr0 + (52 + x0 + (64*x1)), xmask)
    tmp2 = tmp0 - tmp1
    tmp3 = tl_math.abs(tmp2)
    tmp6 = tmp4 - tmp5
    tmp7 = tl_math.abs(tmp6)
    tmp8 = tmp3 + tmp7
    tmp11 = tmp9 - tmp10
    tmp12 = tl_math.abs(tmp11)
    tmp13 = tmp8 + tmp12
    tmp16 = tmp14 - tmp15
    tmp17 = tl_math.abs(tmp16)
    tmp18 = tmp13 + tmp17
    tmp19 = 4.0
    tmp20 = tmp18 / tmp19
    tmp21 = -tmp20
    tmp22 = tl_math.exp(tmp21)
    tl.store(out_ptr0 + (x2), tmp22, xmask)
''', device_str='cuda')


# kernel path: runs/run_shard_4/inductor_cache/o7/co77hkmtzy5ubvbrmxqqwu5n4mggkr3ljkpuheg7zy4q2bdaspun.py
# Topologically Sorted Source Nodes: [idepth_dx, image_dx, abs_1, mean, neg, weights_x, mul, smoothness_x, mean_2, idepth_dy, image_dy, abs_2, mean_1, neg_1, weights_y, mul_1, smoothness_y, mean_3, add], Original ATen: [aten.sub, aten.abs, aten.mean, aten.neg, aten.exp, aten.mul, aten.add]
# Source node to ATen node mapping:
#   abs_1 => abs_1
#   abs_2 => abs_2
#   add => add
#   idepth_dx => sub
#   idepth_dy => sub_1
#   image_dx => sub_2
#   image_dy => sub_3
#   mean => mean
#   mean_1 => mean_1
#   mean_2 => mean_2
#   mean_3 => mean_3
#   mul => mul
#   mul_1 => mul_1
#   neg => neg
#   neg_1 => neg_1
#   smoothness_x => abs_3
#   smoothness_y => abs_4
#   weights_x => exp
#   weights_y => exp_1
# Graph fragment:
#   %sub : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (%slice_4, %slice_8), kwargs = {})
#   %sub_2 : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (%slice_20, %slice_24), kwargs = {})
#   %abs_1 : [num_users=1] = call_function[target=torch.ops.aten.abs.default](args = (%sub_2,), kwargs = {})
#   %mean : [num_users=1] = call_function[target=torch.ops.aten.mean.dim](args = (%abs_1, [1], True), kwargs = {})
#   %neg : [num_users=1] = call_function[target=torch.ops.aten.neg.default](args = (%mean,), kwargs = {})
#   %exp : [num_users=1] = call_function[target=torch.ops.aten.exp.default](args = (%neg,), kwargs = {})
#   %mul : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%sub, %exp), kwargs = {})
#   %abs_3 : [num_users=1] = call_function[target=torch.ops.aten.abs.default](args = (%mul,), kwargs = {})
#   %mean_2 : [num_users=1] = call_function[target=torch.ops.aten.mean.default](args = (%abs_3,), kwargs = {})
#   %sub_1 : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (%slice_11, %slice_15), kwargs = {})
#   %sub_3 : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (%slice_27, %slice_31), kwargs = {})
#   %abs_2 : [num_users=1] = call_function[target=torch.ops.aten.abs.default](args = (%sub_3,), kwargs = {})
#   %mean_1 : [num_users=1] = call_function[target=torch.ops.aten.mean.dim](args = (%abs_2, [1], True), kwargs = {})
#   %neg_1 : [num_users=1] = call_function[target=torch.ops.aten.neg.default](args = (%mean_1,), kwargs = {})
#   %exp_1 : [num_users=1] = call_function[target=torch.ops.aten.exp.default](args = (%neg_1,), kwargs = {})
#   %mul_1 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%sub_1, %exp_1), kwargs = {})
#   %abs_4 : [num_users=1] = call_function[target=torch.ops.aten.abs.default](args = (%mul_1,), kwargs = {})
#   %mean_3 : [num_users=1] = call_function[target=torch.ops.aten.mean.default](args = (%abs_4,), kwargs = {})
#   %add : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%mean_2, %mean_3), kwargs = {})
triton_per_fused_abs_add_exp_mean_mul_neg_sub_2 = async_compile.triton('triton_per_fused_abs_add_exp_mean_mul_neg_sub_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: 'i32', 5: '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': {4: 1}, 'configs': [AttrsDescriptor(divisible_by_16=(0, 1, 2, 3, 5), equal_to_1=(4,))]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_per_fused_abs_add_exp_mean_mul_neg_sub_2', 'mutated_arg_names': ['in_out_ptr0'], 'no_x_dim': False, 'num_load': 6, 'num_reduction': 2, '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_abs_add_exp_mean_mul_neg_sub_2(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, 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 % 3
    r5 = (rindex // 3)
    r3 = (rindex // 48)
    r4 = rindex % 12
    r6 = (rindex // 12)
    tmp0 = tl.load(in_ptr0 + (r0 + (4*r5)), rmask, other=0.0)
    tmp1 = tl.load(in_ptr0 + (1 + r0 + (4*r5)), rmask, other=0.0)
    tmp3 = tl.load(in_ptr1 + (r4 + (12*r3)), rmask, eviction_policy='evict_last', other=0.0)
    tmp10 = tl.load(in_ptr0 + (r4 + (16*r6)), rmask, other=0.0)
    tmp11 = tl.load(in_ptr0 + (4 + r4 + (16*r6)), rmask, other=0.0)
    tmp13 = tl.load(in_ptr2 + (r4 + (12*r3)), rmask, eviction_policy='evict_last', other=0.0)
    tmp2 = tmp0 - tmp1
    tmp4 = tmp2 * tmp3
    tmp5 = tl_math.abs(tmp4)
    tmp6 = tl.broadcast_to(tmp5, [XBLOCK, RBLOCK])
    tmp8 = tl.where(rmask, tmp6, 0)
    tmp9 = tl.sum(tmp8, 1)[:, None]
    tmp12 = tmp10 - tmp11
    tmp14 = tmp12 * tmp13
    tmp15 = tl_math.abs(tmp14)
    tmp16 = tl.broadcast_to(tmp15, [XBLOCK, RBLOCK])
    tmp18 = tl.where(rmask, tmp16, 0)
    tmp19 = tl.sum(tmp18, 1)[:, None]
    tmp20 = 192.0
    tmp21 = tmp9 / tmp20
    tmp22 = tmp19 / tmp20
    tmp23 = tmp21 + tmp22
    tl.debug_barrier()
    tl.store(in_out_ptr0 + (tl.full([XBLOCK, 1], 0, tl.int32)), tmp23, 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, 1, 4, 3), (12, 48, 3, 1), torch.float32)
        # Topologically Sorted Source Nodes: [image_dx, abs_1, mean, neg, weights_x], Original ATen: [aten.sub, aten.abs, aten.mean, aten.neg, aten.exp]
        stream0 = get_raw_stream(0)
        triton_poi_fused_abs_exp_mean_neg_sub_0.run(arg1_1, buf0, 48, grid=grid(48), stream=stream0)
        buf2 = empty_strided_cuda((4, 1, 3, 4), (12, 48, 4, 1), torch.float32)
        # Topologically Sorted Source Nodes: [image_dy, abs_2, mean_1, neg_1, weights_y], Original ATen: [aten.sub, aten.abs, aten.mean, aten.neg, aten.exp]
        triton_poi_fused_abs_exp_mean_neg_sub_1.run(arg1_1, buf2, 48, grid=grid(48), stream=stream0)
        del arg1_1
        buf1 = empty_strided_cuda((), (), torch.float32)
        buf4 = buf1; del buf1  # reuse
        # Topologically Sorted Source Nodes: [idepth_dx, image_dx, abs_1, mean, neg, weights_x, mul, smoothness_x, mean_2, idepth_dy, image_dy, abs_2, mean_1, neg_1, weights_y, mul_1, smoothness_y, mean_3, add], Original ATen: [aten.sub, aten.abs, aten.mean, aten.neg, aten.exp, aten.mul, aten.add]
        triton_per_fused_abs_add_exp_mean_mul_neg_sub_2.run(buf4, arg0_1, buf0, buf2, 1, 192, grid=grid(1), stream=stream0)
        del arg0_1
        del buf0
        del buf2
    return (buf4, )


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


def _gradient_x(img: 'torch.Tensor') ->torch.Tensor:
    assert len(img.shape) == 4, img.shape
    return img[:, :, :, :-1] - img[:, :, :, 1:]


def _gradient_y(img: 'torch.Tensor') ->torch.Tensor:
    assert len(img.shape) == 4, img.shape
    return img[:, :, :-1, :] - img[:, :, 1:, :]


def inverse_depth_smoothness_loss(idepth: 'torch.Tensor', image: 'torch.Tensor'
    ) ->torch.Tensor:
    """Computes image-aware inverse depth smoothness loss.

    See :class:`~kornia.losses.InvDepthSmoothnessLoss` for details.
    """
    if not torch.is_tensor(idepth):
        raise TypeError('Input idepth type is not a torch.Tensor. Got {}'.
            format(type(idepth)))
    if not torch.is_tensor(image):
        raise TypeError('Input image type is not a torch.Tensor. Got {}'.
            format(type(image)))
    if not len(idepth.shape) == 4:
        raise ValueError('Invalid idepth shape, we expect BxCxHxW. Got: {}'
            .format(idepth.shape))
    if not len(image.shape) == 4:
        raise ValueError('Invalid image shape, we expect BxCxHxW. Got: {}'.
            format(image.shape))
    if not idepth.shape[-2:] == image.shape[-2:]:
        raise ValueError('idepth and image shapes must be the same. Got: {}'
            .format(idepth.shape))
    if not idepth.device == image.device:
        raise ValueError('idepth and image must be in the same device. Got: {}'
            .format(idepth.device))
    if not idepth.dtype == image.dtype:
        raise ValueError('idepth and image must be in the same dtype. Got: {}'
            .format(idepth.dtype))
    idepth_dx: 'torch.Tensor' = _gradient_x(idepth)
    idepth_dy: 'torch.Tensor' = _gradient_y(idepth)
    image_dx: 'torch.Tensor' = _gradient_x(image)
    image_dy: 'torch.Tensor' = _gradient_y(image)
    weights_x: 'torch.Tensor' = torch.exp(-torch.mean(torch.abs(image_dx),
        dim=1, keepdim=True))
    weights_y: 'torch.Tensor' = torch.exp(-torch.mean(torch.abs(image_dy),
        dim=1, keepdim=True))
    smoothness_x: 'torch.Tensor' = torch.abs(idepth_dx * weights_x)
    smoothness_y: 'torch.Tensor' = torch.abs(idepth_dy * weights_y)
    return torch.mean(smoothness_x) + torch.mean(smoothness_y)


class InverseDepthSmoothnessLoss(nn.Module):
    """Criterion that computes image-aware inverse depth smoothness loss.

    .. math::

        \\text{loss} = \\left | \\partial_x d_{ij} \\right | e^{-\\left \\|
        \\partial_x I_{ij} \\right \\|} + \\left |
        \\partial_y d_{ij} \\right | e^{-\\left \\| \\partial_y I_{ij} \\right \\|}


    Shape:
        - Inverse Depth: :math:`(N, 1, H, W)`
        - Image: :math:`(N, 3, H, W)`
        - Output: scalar

    Examples::

        >>> idepth = torch.rand(1, 1, 4, 5)
        >>> image = torch.rand(1, 3, 4, 5)
        >>> smooth = kornia.losses.DepthSmoothnessLoss()
        >>> loss = smooth(idepth, image)
    """

    def __init__(self) ->None:
        super(InverseDepthSmoothnessLoss, self).__init__()

    def forward(self, idepth: 'torch.Tensor', image: 'torch.Tensor'
        ) ->torch.Tensor:
        return inverse_depth_smoothness_loss(idepth, image)


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.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_poi_fused_abs_exp_mean_neg_sub_0(in_ptr0, out_ptr0, xnumel,
    XBLOCK: tl.constexpr):
    xnumel = 48
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 3
    x1 = xindex // 3 % 4
    x2 = xindex // 12
    x3 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 4 * x1 + 64 * x2), xmask)
    tmp1 = tl.load(in_ptr0 + (1 + x0 + 4 * x1 + 64 * x2), xmask)
    tmp4 = tl.load(in_ptr0 + (16 + x0 + 4 * x1 + 64 * x2), xmask)
    tmp5 = tl.load(in_ptr0 + (17 + x0 + 4 * x1 + 64 * x2), xmask)
    tmp9 = tl.load(in_ptr0 + (32 + x0 + 4 * x1 + 64 * x2), xmask)
    tmp10 = tl.load(in_ptr0 + (33 + x0 + 4 * x1 + 64 * x2), xmask)
    tmp14 = tl.load(in_ptr0 + (48 + x0 + 4 * x1 + 64 * x2), xmask)
    tmp15 = tl.load(in_ptr0 + (49 + x0 + 4 * x1 + 64 * x2), xmask)
    tmp2 = tmp0 - tmp1
    tmp3 = tl_math.abs(tmp2)
    tmp6 = tmp4 - tmp5
    tmp7 = tl_math.abs(tmp6)
    tmp8 = tmp3 + tmp7
    tmp11 = tmp9 - tmp10
    tmp12 = tl_math.abs(tmp11)
    tmp13 = tmp8 + tmp12
    tmp16 = tmp14 - tmp15
    tmp17 = tl_math.abs(tmp16)
    tmp18 = tmp13 + tmp17
    tmp19 = 4.0
    tmp20 = tmp18 / tmp19
    tmp21 = -tmp20
    tmp22 = tl_math.exp(tmp21)
    tl.store(out_ptr0 + x3, tmp22, xmask)


@triton.jit
def triton_poi_fused_abs_exp_mean_neg_sub_1(in_ptr0, out_ptr0, xnumel,
    XBLOCK: tl.constexpr):
    xnumel = 48
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 12
    x1 = xindex // 12
    x2 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 64 * x1), xmask)
    tmp1 = tl.load(in_ptr0 + (4 + x0 + 64 * x1), xmask)
    tmp4 = tl.load(in_ptr0 + (16 + x0 + 64 * x1), xmask)
    tmp5 = tl.load(in_ptr0 + (20 + x0 + 64 * x1), xmask)
    tmp9 = tl.load(in_ptr0 + (32 + x0 + 64 * x1), xmask)
    tmp10 = tl.load(in_ptr0 + (36 + x0 + 64 * x1), xmask)
    tmp14 = tl.load(in_ptr0 + (48 + x0 + 64 * x1), xmask)
    tmp15 = tl.load(in_ptr0 + (52 + x0 + 64 * x1), xmask)
    tmp2 = tmp0 - tmp1
    tmp3 = tl_math.abs(tmp2)
    tmp6 = tmp4 - tmp5
    tmp7 = tl_math.abs(tmp6)
    tmp8 = tmp3 + tmp7
    tmp11 = tmp9 - tmp10
    tmp12 = tl_math.abs(tmp11)
    tmp13 = tmp8 + tmp12
    tmp16 = tmp14 - tmp15
    tmp17 = tl_math.abs(tmp16)
    tmp18 = tmp13 + tmp17
    tmp19 = 4.0
    tmp20 = tmp18 / tmp19
    tmp21 = -tmp20
    tmp22 = tl_math.exp(tmp21)
    tl.store(out_ptr0 + x2, tmp22, xmask)


@triton.jit
def triton_per_fused_abs_add_exp_mean_mul_neg_sub_2(in_out_ptr0, in_ptr0,
    in_ptr1, in_ptr2, 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 % 3
    r5 = rindex // 3
    r3 = rindex // 48
    r4 = rindex % 12
    r6 = rindex // 12
    tmp0 = tl.load(in_ptr0 + (r0 + 4 * r5), rmask, other=0.0)
    tmp1 = tl.load(in_ptr0 + (1 + r0 + 4 * r5), rmask, other=0.0)
    tmp3 = tl.load(in_ptr1 + (r4 + 12 * r3), rmask, eviction_policy=
        'evict_last', other=0.0)
    tmp10 = tl.load(in_ptr0 + (r4 + 16 * r6), rmask, other=0.0)
    tmp11 = tl.load(in_ptr0 + (4 + r4 + 16 * r6), rmask, other=0.0)
    tmp13 = tl.load(in_ptr2 + (r4 + 12 * r3), rmask, eviction_policy=
        'evict_last', other=0.0)
    tmp2 = tmp0 - tmp1
    tmp4 = tmp2 * tmp3
    tmp5 = tl_math.abs(tmp4)
    tmp6 = tl.broadcast_to(tmp5, [XBLOCK, RBLOCK])
    tmp8 = tl.where(rmask, tmp6, 0)
    tmp9 = tl.sum(tmp8, 1)[:, None]
    tmp12 = tmp10 - tmp11
    tmp14 = tmp12 * tmp13
    tmp15 = tl_math.abs(tmp14)
    tmp16 = tl.broadcast_to(tmp15, [XBLOCK, RBLOCK])
    tmp18 = tl.where(rmask, tmp16, 0)
    tmp19 = tl.sum(tmp18, 1)[:, None]
    tmp20 = 192.0
    tmp21 = tmp9 / tmp20
    tmp22 = tmp19 / tmp20
    tmp23 = tmp21 + tmp22
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp23, 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, 1, 4, 3), (12, 48, 3, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_abs_exp_mean_neg_sub_0[grid(48)](arg1_1, buf0, 48,
            XBLOCK=64, num_warps=1, num_stages=1)
        buf2 = empty_strided_cuda((4, 1, 3, 4), (12, 48, 4, 1), torch.float32)
        triton_poi_fused_abs_exp_mean_neg_sub_1[grid(48)](arg1_1, buf2, 48,
            XBLOCK=64, num_warps=1, num_stages=1)
        del arg1_1
        buf1 = empty_strided_cuda((), (), torch.float32)
        buf4 = buf1
        del buf1
        triton_per_fused_abs_add_exp_mean_mul_neg_sub_2[grid(1)](buf4,
            arg0_1, buf0, buf2, 1, 192, XBLOCK=1, num_warps=2, num_stages=1)
        del arg0_1
        del buf0
        del buf2
    return buf4,


def _gradient_x(img: 'torch.Tensor') ->torch.Tensor:
    assert len(img.shape) == 4, img.shape
    return img[:, :, :, :-1] - img[:, :, :, 1:]


def _gradient_y(img: 'torch.Tensor') ->torch.Tensor:
    assert len(img.shape) == 4, img.shape
    return img[:, :, :-1, :] - img[:, :, 1:, :]


def inverse_depth_smoothness_loss(idepth: 'torch.Tensor', image: 'torch.Tensor'
    ) ->torch.Tensor:
    """Computes image-aware inverse depth smoothness loss.

    See :class:`~kornia.losses.InvDepthSmoothnessLoss` for details.
    """
    if not torch.is_tensor(idepth):
        raise TypeError('Input idepth type is not a torch.Tensor. Got {}'.
            format(type(idepth)))
    if not torch.is_tensor(image):
        raise TypeError('Input image type is not a torch.Tensor. Got {}'.
            format(type(image)))
    if not len(idepth.shape) == 4:
        raise ValueError('Invalid idepth shape, we expect BxCxHxW. Got: {}'
            .format(idepth.shape))
    if not len(image.shape) == 4:
        raise ValueError('Invalid image shape, we expect BxCxHxW. Got: {}'.
            format(image.shape))
    if not idepth.shape[-2:] == image.shape[-2:]:
        raise ValueError('idepth and image shapes must be the same. Got: {}'
            .format(idepth.shape))
    if not idepth.device == image.device:
        raise ValueError('idepth and image must be in the same device. Got: {}'
            .format(idepth.device))
    if not idepth.dtype == image.dtype:
        raise ValueError('idepth and image must be in the same dtype. Got: {}'
            .format(idepth.dtype))
    idepth_dx: 'torch.Tensor' = _gradient_x(idepth)
    idepth_dy: 'torch.Tensor' = _gradient_y(idepth)
    image_dx: 'torch.Tensor' = _gradient_x(image)
    image_dy: 'torch.Tensor' = _gradient_y(image)
    weights_x: 'torch.Tensor' = torch.exp(-torch.mean(torch.abs(image_dx),
        dim=1, keepdim=True))
    weights_y: 'torch.Tensor' = torch.exp(-torch.mean(torch.abs(image_dy),
        dim=1, keepdim=True))
    smoothness_x: 'torch.Tensor' = torch.abs(idepth_dx * weights_x)
    smoothness_y: 'torch.Tensor' = torch.abs(idepth_dy * weights_y)
    return torch.mean(smoothness_x) + torch.mean(smoothness_y)


class InverseDepthSmoothnessLossNew(nn.Module):
    """Criterion that computes image-aware inverse depth smoothness loss.

    .. math::

        \\text{loss} = \\left | \\partial_x d_{ij} \\right | e^{-\\left \\|
        \\partial_x I_{ij} \\right \\|} + \\left |
        \\partial_y d_{ij} \\right | e^{-\\left \\| \\partial_y I_{ij} \\right \\|}


    Shape:
        - Inverse Depth: :math:`(N, 1, H, W)`
        - Image: :math:`(N, 3, H, W)`
        - Output: scalar

    Examples::

        >>> idepth = torch.rand(1, 1, 4, 5)
        >>> image = torch.rand(1, 3, 4, 5)
        >>> smooth = kornia.losses.DepthSmoothnessLoss()
        >>> loss = smooth(idepth, image)
    """

    def __init__(self) ->None:
        super(InverseDepthSmoothnessLossNew, self).__init__()

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