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_add_logsumexp_mean_mul_rsub_softplus_sub_0(in_out_ptr0,
    in_ptr0, in_ptr1, xnumel, rnumel, XBLOCK: tl.constexpr):
    RBLOCK: tl.constexpr = 64
    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 + 4 * r0, None, eviction_policy='evict_last')
    tmp5 = tl.load(in_ptr1 + 4 * r0, None, eviction_policy='evict_last')
    tmp13 = tl.load(in_ptr0 + (1 + 4 * r0), None, eviction_policy='evict_last')
    tmp16 = tl.load(in_ptr1 + (1 + 4 * r0), None, eviction_policy='evict_last')
    tmp23 = tl.load(in_ptr0 + (2 + 4 * r0), None, eviction_policy='evict_last')
    tmp26 = tl.load(in_ptr1 + (2 + 4 * r0), None, eviction_policy='evict_last')
    tmp33 = tl.load(in_ptr0 + (3 + 4 * r0), None, eviction_policy='evict_last')
    tmp36 = tl.load(in_ptr1 + (3 + 4 * r0), None, eviction_policy='evict_last')
    tmp1 = 2.0
    tmp2 = tmp0 * tmp1
    tmp3 = 1.0
    tmp4 = tmp3 - tmp2
    tmp6 = tmp4 * tmp5
    tmp7 = 1000000000000.0
    tmp8 = tmp0 * tmp7
    tmp9 = tmp6 - tmp8
    tmp10 = 4.0
    tmp11 = tmp9 - tmp10
    tmp12 = tmp11 * tmp10
    tmp14 = tmp13 * tmp1
    tmp15 = tmp3 - tmp14
    tmp17 = tmp15 * tmp16
    tmp18 = tmp13 * tmp7
    tmp19 = tmp17 - tmp18
    tmp20 = tmp19 - tmp10
    tmp21 = tmp20 * tmp10
    tmp22 = triton_helpers.maximum(tmp12, tmp21)
    tmp24 = tmp23 * tmp1
    tmp25 = tmp3 - tmp24
    tmp27 = tmp25 * tmp26
    tmp28 = tmp23 * tmp7
    tmp29 = tmp27 - tmp28
    tmp30 = tmp29 - tmp10
    tmp31 = tmp30 * tmp10
    tmp32 = triton_helpers.maximum(tmp22, tmp31)
    tmp34 = tmp33 * tmp1
    tmp35 = tmp3 - tmp34
    tmp37 = tmp35 * tmp36
    tmp38 = tmp33 * tmp7
    tmp39 = tmp37 - tmp38
    tmp40 = tmp39 - tmp10
    tmp41 = tmp40 * tmp10
    tmp42 = triton_helpers.maximum(tmp32, tmp41)
    tmp43 = tl_math.abs(tmp42)
    tmp44 = float('inf')
    tmp45 = tmp43 == tmp44
    tmp46 = 0.0
    tmp47 = tl.where(tmp45, tmp46, tmp42)
    tmp48 = tmp12 - tmp47
    tmp49 = tl_math.exp(tmp48)
    tmp50 = tmp21 - tmp47
    tmp51 = tl_math.exp(tmp50)
    tmp52 = tmp49 + tmp51
    tmp53 = tmp31 - tmp47
    tmp54 = tl_math.exp(tmp53)
    tmp55 = tmp52 + tmp54
    tmp56 = tmp41 - tmp47
    tmp57 = tl_math.exp(tmp56)
    tmp58 = tmp55 + tmp57
    tmp59 = tmp3 - tmp0
    tmp60 = tmp59 * tmp7
    tmp61 = tmp6 - tmp60
    tmp62 = tmp61 * tmp10
    tmp63 = tmp3 - tmp13
    tmp64 = tmp63 * tmp7
    tmp65 = tmp17 - tmp64
    tmp66 = tmp65 * tmp10
    tmp67 = triton_helpers.maximum(tmp62, tmp66)
    tmp68 = tmp3 - tmp23
    tmp69 = tmp68 * tmp7
    tmp70 = tmp27 - tmp69
    tmp71 = tmp70 * tmp10
    tmp72 = triton_helpers.maximum(tmp67, tmp71)
    tmp73 = tmp3 - tmp33
    tmp74 = tmp73 * tmp7
    tmp75 = tmp37 - tmp74
    tmp76 = tmp75 * tmp10
    tmp77 = triton_helpers.maximum(tmp72, tmp76)
    tmp78 = tl_math.abs(tmp77)
    tmp79 = tmp78 == tmp44
    tmp80 = tl.where(tmp79, tmp46, tmp77)
    tmp81 = tmp62 - tmp80
    tmp82 = tl_math.exp(tmp81)
    tmp83 = tmp66 - tmp80
    tmp84 = tl_math.exp(tmp83)
    tmp85 = tmp82 + tmp84
    tmp86 = tmp71 - tmp80
    tmp87 = tl_math.exp(tmp86)
    tmp88 = tmp85 + tmp87
    tmp89 = tmp76 - tmp80
    tmp90 = tl_math.exp(tmp89)
    tmp91 = tmp88 + tmp90
    tmp92 = tl_math.log(tmp58)
    tmp93 = tmp92 + tmp47
    tmp94 = tl_math.log(tmp91)
    tmp95 = tmp94 + tmp80
    tmp96 = tmp93 + tmp95
    tmp97 = tmp96 * tmp3
    tmp98 = 20.0
    tmp99 = tmp97 > tmp98
    tmp100 = tl_math.exp(tmp97)
    tmp101 = libdevice.log1p(tmp100)
    tmp102 = tmp101 * tmp3
    tmp103 = tl.where(tmp99, tmp96, tmp102)
    tmp104 = tl.broadcast_to(tmp103, [XBLOCK, RBLOCK])
    tmp106 = tl.sum(tmp104, 1)[:, None]
    tmp107 = 64.0
    tmp108 = tmp106 / tmp107
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp108, 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)
        buf4 = empty_strided_cuda((), (), torch.float32)
        buf5 = buf4
        del buf4
        get_raw_stream(0)
        triton_per_fused_add_logsumexp_mean_mul_rsub_softplus_sub_0[grid(1)](
            buf5, arg0_1, arg1_1, 1, 64, XBLOCK=1, num_warps=2, num_stages=1)
        del arg0_1
        del arg1_1
    return buf5,


class RankingLossNew(nn.Module):
    """
    ref: https://arxiv.org/abs/2002.10857
    """

    def __init__(self, m: 'float', gamma: 'float') ->None:
        super(RankingLossNew, self).__init__()
        self.m = m
        self.gamma = gamma
        self.soft_plus = nn.Softplus()

    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.015482