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 math as tl_math
import numpy as np
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_per_fused__to_copy_abs_ge_le_mul_sub_sum_0(in_ptr0, in_ptr1,
    out_ptr0, out_ptr1, out_ptr2, xnumel, rnumel, XBLOCK: tl.constexpr):
    RBLOCK: tl.constexpr = 4
    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 + 16 * r0, None, eviction_policy='evict_last')
    tmp7 = tl.load(in_ptr1 + r0, None)
    tmp1 = 1.0
    tmp2 = tmp0 <= tmp1
    tmp3 = 0.0
    tmp4 = tmp0 >= tmp3
    tmp5 = tmp2 & tmp4
    tmp6 = tmp5.to(tl.float32)
    tmp8 = tmp7 - tmp0
    tmp9 = tmp8 * tmp6
    tmp10 = tl_math.abs(tmp9)
    tmp11 = tl.broadcast_to(tmp10, [XBLOCK, RBLOCK])
    tmp13 = tl.sum(tmp11, 1)[:, None]
    tmp14 = tl.broadcast_to(tmp6, [XBLOCK, RBLOCK])
    tmp16 = tl.sum(tmp14, 1)[:, None]
    tl.store(out_ptr0 + tl.broadcast_to(r0, [XBLOCK, RBLOCK]), tmp6, None)
    tl.store(out_ptr1 + tl.full([XBLOCK, 1], 0, tl.int32), tmp13, None)
    tl.store(out_ptr2 + tl.full([XBLOCK, 1], 0, tl.int32), tmp16, None)


@triton.jit
def triton_per_fused__to_copy_abs_add_clamp_div_eq_gt_mul_sgn_sub_sum_1(
    in_out_ptr0, in_out_ptr1, in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4,
    in_ptr5, in_ptr6, out_ptr2, out_ptr3, out_ptr4, xnumel, rnumel, XBLOCK:
    tl.constexpr):
    RBLOCK: tl.constexpr = 4
    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)
    tmp3 = tl.load(in_ptr1 + r0, None)
    tmp7 = tl.load(in_out_ptr0 + r0, None)
    tmp9 = tl.load(in_ptr2 + r0, None)
    tmp10 = tl.load(in_ptr3 + r0, None)
    tmp13 = tl.load(in_ptr4 + r0, None)
    tmp14 = tl.load(in_ptr5 + r0, None)
    tmp38 = tl.load(in_out_ptr1 + 0)
    tmp39 = tl.broadcast_to(tmp38, [XBLOCK, 1])
    tmp40 = tl.load(in_ptr6 + 0)
    tmp41 = tl.broadcast_to(tmp40, [XBLOCK, 1])
    tmp1 = 0.0
    tmp2 = tmp0 == tmp1
    tmp4 = tmp3 == tmp1
    tmp5 = tmp2 & tmp4
    tmp6 = tmp5.to(tl.float32)
    tmp8 = tmp6 * tmp7
    tmp11 = tmp9 - tmp10
    tmp12 = tl_math.abs(tmp11)
    tmp15 = tmp13 - tmp14
    tmp16 = tl_math.abs(tmp15)
    tmp17 = tmp12 + tmp16
    tmp18 = tmp17 * tmp8
    tmp19 = tl.broadcast_to(tmp18, [XBLOCK, RBLOCK])
    tmp21 = tl.sum(tmp19, 1)[:, None]
    tmp22 = tl.broadcast_to(tmp8, [XBLOCK, RBLOCK])
    tmp24 = tl.sum(tmp22, 1)[:, None]
    tmp25 = tl.full([1, 1], 0, tl.int32)
    tmp26 = tmp25 < tmp15
    tmp27 = tmp26.to(tl.int8)
    tmp28 = tmp15 < tmp25
    tmp29 = tmp28.to(tl.int8)
    tmp30 = tmp27 - tmp29
    tmp31 = tmp30.to(tmp15.dtype)
    tmp32 = tmp25 < tmp11
    tmp33 = tmp32.to(tl.int8)
    tmp34 = tmp11 < tmp25
    tmp35 = tmp34.to(tl.int8)
    tmp36 = tmp33 - tmp35
    tmp37 = tmp36.to(tmp11.dtype)
    tmp42 = 1.0
    tmp43 = triton_helpers.maximum(tmp41, tmp42)
    tmp44 = tmp39 / tmp43
    tmp45 = triton_helpers.maximum(tmp24, tmp42)
    tmp46 = tmp21 / tmp45
    tmp47 = tmp44 + tmp46
    tmp48 = 10.0
    tmp49 = tmp47 > tmp48
    tl.store(in_out_ptr0 + tl.broadcast_to(r0, [XBLOCK, RBLOCK]), tmp8, None)
    tl.store(out_ptr2 + tl.broadcast_to(r0, [XBLOCK, RBLOCK]), tmp31, None)
    tl.store(out_ptr3 + tl.broadcast_to(r0, [XBLOCK, RBLOCK]), tmp37, None)
    tl.debug_barrier()
    tl.store(in_out_ptr1 + tl.full([XBLOCK, 1], 0, tl.int32), tmp47, None)
    tl.store(out_ptr4 + tl.full([XBLOCK, 1], 0, tl.int32), tmp49, None)


def call(args):
    primals_1, primals_2, primals_3, primals_4 = args
    args.clear()
    assert_size_stride(primals_1, (4, 1, 1, 1), (1, 1, 1, 1))
    assert_size_stride(primals_2, (4, 1, 4, 4), (16, 16, 4, 1))
    assert_size_stride(primals_3, (1, 1, 3, 3), (9, 9, 3, 1))
    assert_size_stride(primals_4, (1, 1, 3, 3), (9, 9, 3, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 1, 1, 1), (1, 1, 1, 1), torch.float32)
        buf1 = empty_strided_cuda((), (), torch.float32)
        buf2 = empty_strided_cuda((), (), torch.float32)
        get_raw_stream(0)
        triton_per_fused__to_copy_abs_ge_le_mul_sub_sum_0[grid(1)](primals_2,
            primals_1, buf0, buf1, buf2, 1, 4, XBLOCK=1, num_warps=2,
            num_stages=1)
        buf3 = extern_kernels.convolution(primals_1, primals_3, 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, 1, 1), (1, 1, 1, 1))
        buf4 = extern_kernels.convolution(primals_1, primals_4, stride=(1, 
            1), padding=(1, 1), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf4, (4, 1, 1, 1), (1, 1, 1, 1))
        buf5 = extern_kernels.convolution(reinterpret_tensor(primals_2, (4,
            1, 1, 1), (16, 16, 4, 1), 0), primals_3, stride=(1, 1), padding
            =(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 
            0), groups=1, bias=None)
        assert_size_stride(buf5, (4, 1, 1, 1), (1, 1, 1, 1))
        buf6 = extern_kernels.convolution(reinterpret_tensor(primals_2, (4,
            1, 1, 1), (16, 16, 4, 1), 0), primals_4, stride=(1, 1), padding
            =(1, 1), dilation=(1, 1), transposed=False, output_padding=(0, 
            0), groups=1, bias=None)
        assert_size_stride(buf6, (4, 1, 1, 1), (1, 1, 1, 1))
        buf7 = extern_kernels.convolution(buf0, primals_3, stride=(1, 1),
            padding=(1, 1), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf7, (4, 1, 1, 1), (1, 1, 1, 1))
        buf8 = extern_kernels.convolution(buf0, primals_4, stride=(1, 1),
            padding=(1, 1), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf8, (4, 1, 1, 1), (1, 1, 1, 1))
        buf9 = buf0
        del buf0
        buf13 = empty_strided_cuda((4, 1, 1, 1), (1, 1, 1, 1), torch.float32)
        buf14 = empty_strided_cuda((4, 1, 1, 1), (1, 1, 1, 1), torch.float32)
        buf12 = buf1
        del buf1
        buf15 = empty_strided_cuda((), (), torch.bool)
        triton_per_fused__to_copy_abs_add_clamp_div_eq_gt_mul_sgn_sub_sum_1[
            grid(1)](buf9, buf12, buf7, buf8, buf3, buf5, buf4, buf6, buf2,
            buf13, buf14, buf15, 1, 4, XBLOCK=1, num_warps=2, num_stages=1)
        del buf2
        del buf3
        del buf4
        del buf5
        del buf6
        del buf7
        del buf8
    return buf12, buf15, primals_1, primals_3, primals_4, reinterpret_tensor(
        primals_2, (4, 1, 1, 1), (16, 16, 4, 1), 0), buf9, buf13, buf14


class DepthGTLossNew(torch.nn.Module):
    """
    A simple L1 loss, but restricted to the cropped center of the image.
    It also does not count pixels outside of a given range of values (in target).
    Additionally, there is also an L1 loss on the gradient.
    """

    def __init__(self, crop_fraction=0.25, vmin=0, vmax=1, limit=10):
        """
        The input should be (batch x channels x height x width).
        We L1-penalize the inner portion of the image,
        with crop_fraction cut off from all borders.

        Keyword arguments:
            crop_fraction -- fraction to cut off from all sides (defaults to 0.25)
            vmin -- minimal (GT!) value to supervise
            vmax -- maximal (GT!) value to supervise
            limit -- anything higher than this is wrong, and should be ignored
        """
        super().__init__()
        self.crop_fraction = crop_fraction
        """Cut-off fraction"""
        self.vmin = vmin
        """Lower bound for valid target pixels"""
        self.vmax = vmax
        """Upper bound for valid target pixels"""
        self.sobel_x = torch.nn.Conv2d(1, 1, kernel_size=3, stride=1,
            padding=1, bias=False)
        self.sobel_x.weight = torch.nn.Parameter(torch.from_numpy(np.array(
            [[1, 0, -1], [2, 0, -2], [1, 0, -1]]) / 8.0).float().unsqueeze(
            0).unsqueeze(0))
        self.sobel_y = torch.nn.Conv2d(1, 1, kernel_size=3, stride=1,
            padding=1, bias=False)
        self.sobel_y.weight = torch.nn.Parameter(torch.from_numpy(np.array(
            [[1, 2, 1], [0, 0, 0], [-1, -2, -1]]) / 8.0).float().unsqueeze(
            0).unsqueeze(0))
        torch.device('cuda')
        self.sobel_x = self.sobel_x
        self.sobel_y = self.sobel_y
        self.limit = limit

    def forward(self, input_0, input_1):
        primals_3 = self.sobel_x.weight
        primals_4 = self.sobel_y.weight
        primals_1 = input_0
        primals_2 = input_1
        output = call([primals_1, primals_2, primals_3, primals_4])
        return output[0]


Measured latency_ms: 0.100373