import torch
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
from torch._inductor.runtime.triton_helpers import math as tl_math
import torch.onnx
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_abs_mean_sub_0(in_ptr0, in_ptr1, out_ptr0, xnumel, rnumel
    ):
    XBLOCK: tl.constexpr = 1
    RBLOCK: tl.constexpr = 256
    xoffset = tl.program_id(0) * XBLOCK
    tl.full([1], xoffset, tl.int32)
    tl.full([RBLOCK], True, tl.int1)
    rindex = tl.arange(0, RBLOCK)[:]
    tl.full([RBLOCK], True, tl.int1)
    r0 = rindex
    tmp0 = tl.load(in_ptr0 + r0, None)
    tmp1 = tl.load(in_ptr1 + r0, None)
    tmp2 = tmp0 - tmp1
    tmp3 = tl_math.abs(tmp2)
    tmp4 = tl.broadcast_to(tmp3, [RBLOCK])
    tmp6 = triton_helpers.promote_to_tensor(tl.sum(tmp4, 0))
    tl.store(out_ptr0 + tl.full([1], 0, tl.int32), tmp6, None)


@triton.jit
def triton_red_fused_abs_bitwise_and_gt_lt_mul_neg_relu_repeat_sign_sub_sum_1(
    in_ptr0, in_ptr1, out_ptr0, out_ptr1, 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
    _tmp25 = tl.full([XBLOCK, RBLOCK], 0, tl.float32)
    _tmp29 = tl.full([XBLOCK, RBLOCK], 0, tl.int64)
    for roffset in range(0, rnumel, RBLOCK):
        rindex = roffset + rbase
        rmask = rindex < rnumel
        r1 = rindex
        tmp0 = tl.load(in_ptr0 + r1 % 256, rmask, eviction_policy=
            'evict_last', other=0.0)
        tmp1 = tl.load(in_ptr0 + (32 * x0 + r1 // 256), rmask & xmask,
            eviction_policy='evict_last', other=0.0)
        tmp18 = tl.load(in_ptr1 + r1 % 256, rmask, eviction_policy=
            'evict_last', other=0.0)
        tmp19 = tl.load(in_ptr1 + (32 * x0 + r1 // 256), rmask & xmask,
            eviction_policy='evict_last', other=0.0)
        tmp2 = tmp0 - tmp1
        tmp3 = tl_math.abs(tmp2)
        tmp4 = 1.0
        tmp5 = tmp3 < tmp4
        tmp6 = 0.0
        tmp7 = tmp3 > tmp6
        tmp8 = tmp5 & tmp7
        tmp9 = tmp8.to(tl.float32)
        tmp10 = tl.full([1, 1], 0, tl.int32)
        tmp11 = tmp10 < tmp2
        tmp12 = tmp11.to(tl.int8)
        tmp13 = tmp2 < tmp10
        tmp14 = tmp13.to(tl.int8)
        tmp15 = tmp12 - tmp14
        tmp16 = tmp15.to(tmp2.dtype)
        tmp17 = -tmp16
        tmp20 = tmp18 - tmp19
        tmp21 = tmp17 * tmp20
        tmp22 = triton_helpers.maximum(tmp10, tmp21)
        tmp23 = tmp9 * tmp22
        tmp24 = tl.broadcast_to(tmp23, [XBLOCK, RBLOCK])
        tmp26 = _tmp25 + tmp24
        _tmp25 = tl.where(rmask & xmask, tmp26, _tmp25)
        tmp27 = tmp8.to(tl.int64)
        tmp28 = tl.broadcast_to(tmp27, [XBLOCK, RBLOCK])
        tmp30 = _tmp29 + tmp28
        _tmp29 = tl.where(rmask & xmask, tmp30, _tmp29)
    tmp25 = tl.sum(_tmp25, 1)[:, None]
    tl.store(out_ptr0 + x0, tmp25, xmask)
    tmp29 = tl.sum(_tmp29, 1)[:, None]
    tl.store(out_ptr1 + x0, tmp29, xmask)


@triton.jit
def triton_per_fused_abs_add_bitwise_and_div_gt_lt_mean_mul_neg_relu_repeat_sign_sub_sum_2(
    in_out_ptr0, in_ptr0, in_ptr1, 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)
    tmp4 = tl.load(in_ptr1 + r0, None)
    tmp8 = tl.load(in_out_ptr0 + 0)
    tmp9 = tl.broadcast_to(tmp8, [XBLOCK, 1])
    tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
    tmp3 = tl.sum(tmp1, 1)[:, None]
    tmp5 = tl.broadcast_to(tmp4, [XBLOCK, RBLOCK])
    tmp7 = tl.sum(tmp5, 1)[:, None]
    tmp10 = 256.0
    tmp11 = tmp9 / tmp10
    tmp12 = 1.0
    tmp13 = tmp11 * tmp12
    tmp14 = tmp7.to(tl.float32)
    tmp15 = 1e-08
    tmp16 = tmp14 + tmp15
    tmp17 = tmp3 / tmp16
    tmp18 = tmp17 * tmp12
    tmp19 = tmp13 + tmp18
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp19, 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((), (), torch.float32)
        get_raw_stream(0)
        triton_per_fused_abs_mean_sub_0[grid(1)](arg0_1, arg1_1, buf0, 1, 
            256, num_warps=2, num_stages=1)
        buf1 = empty_strided_cuda((8,), (1,), torch.float32)
        buf3 = empty_strided_cuda((8,), (1,), torch.int64)
        triton_red_fused_abs_bitwise_and_gt_lt_mul_neg_relu_repeat_sign_sub_sum_1[
            grid(8)](arg1_1, arg0_1, buf1, buf3, 8, 8192, XBLOCK=1, RBLOCK=
            2048, num_warps=16, num_stages=1)
        del arg0_1
        del arg1_1
        buf5 = buf0
        del buf0
        triton_per_fused_abs_add_bitwise_and_div_gt_lt_mean_mul_neg_relu_repeat_sign_sub_sum_2[
            grid(1)](buf5, buf1, buf3, 1, 8, XBLOCK=1, num_warps=2,
            num_stages=1)
        del buf1
        del buf3
    return buf5,


class L1RankLossNew(torch.nn.Module):
    """
    L1 loss + Rank loss
    """

    def __init__(self, **kwargs):
        super(L1RankLossNew, self).__init__()
        self.l1_w = kwargs.get('l1_w', 1)
        self.rank_w = kwargs.get('rank_w', 1)
        self.hard_thred = kwargs.get('hard_thred', 1)
        self.use_margin = kwargs.get('use_margin', False)

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


Measured latency_ms: 0.015126