import torch
import torch.nn as nn


class RankingLoss(nn.Module):

    def __init__(self):
        super().__init__()
        self.bce = nn.BCELoss()

    def forward(self, pred_loss, target_loss):
        target = (target_loss - target_loss.flip(0))[:target_loss.size(0) // 2]
        target = target.detach()
        ones = torch.sign(torch.clamp(target, min=0))
        pred_loss = (pred_loss - pred_loss.flip(0))[:pred_loss.size(0) // 2]
        pred_loss = torch.sigmoid(pred_loss)
        return self.bce(pred_loss, ones)


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 import triton_helpers
from torch._inductor.runtime.triton_helpers import libdevice, 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_per_fused_binary_cross_entropy_clamp_sigmoid_sign_0(in_out_ptr0,
    in_ptr0, in_ptr1, xnumel, rnumel, XBLOCK: tl.constexpr):
    RBLOCK: tl.constexpr = 128
    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)
    r2 = rindex
    r0 = rindex % 64
    r1 = rindex // 64
    tmp0 = tl.load(in_ptr0 + r2, None)
    tmp1 = tl.load(in_ptr0 + (192 + r0 + -64 * r1), None)
    tmp14 = tl.load(in_ptr1 + r2, None)
    tmp15 = tl.load(in_ptr1 + (192 + r0 + -64 * r1), None)
    tmp2 = tmp0 - tmp1
    tmp3 = 0.0
    tmp4 = triton_helpers.maximum(tmp2, tmp3)
    tmp5 = tl.full([1, 1], 0, tl.int32)
    tmp6 = tmp5 < tmp4
    tmp7 = tmp6.to(tl.int8)
    tmp8 = tmp4 < tmp5
    tmp9 = tmp8.to(tl.int8)
    tmp10 = tmp7 - tmp9
    tmp11 = tmp10.to(tmp4.dtype)
    tmp12 = 1.0
    tmp13 = tmp11 - tmp12
    tmp16 = tmp14 - tmp15
    tmp17 = tl.sigmoid(tmp16)
    tmp18 = -tmp17
    tmp19 = libdevice.log1p(tmp18)
    tmp20 = -100.0
    tmp21 = triton_helpers.maximum(tmp19, tmp20)
    tmp22 = tmp13 * tmp21
    tmp23 = tl_math.log(tmp17)
    tmp24 = triton_helpers.maximum(tmp23, tmp20)
    tmp25 = tmp11 * tmp24
    tmp26 = tmp22 - tmp25
    tmp27 = tl.broadcast_to(tmp26, [XBLOCK, RBLOCK])
    tmp29 = tl.sum(tmp27, 1)[:, None]
    tmp30 = 128.0
    tmp31 = tmp29 / tmp30
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp31, 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)
        buf1 = buf0
        del buf0
        get_raw_stream(0)
        triton_per_fused_binary_cross_entropy_clamp_sigmoid_sign_0[grid(1)](
            buf1, arg0_1, arg1_1, 1, 128, XBLOCK=1, num_warps=2, num_stages=1)
        del arg0_1
        del arg1_1
    return buf1,


class RankingLossNew(nn.Module):

    def __init__(self):
        super().__init__()
        self.bce = nn.BCELoss()

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