import torch
from torch import device
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
import torch.nn as nn
import torch.nn.parallel
import torch.optim
import torch.utils.data
import torch.utils.data.distributed
from math import sqrt as sqrt
from itertools import product as product
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_clamp_min_mean_mul_neg_sign_sub_0(in_out_ptr0,
    in_ptr0, in_ptr1, in_ptr2, xnumel, rnumel, XBLOCK: tl.constexpr):
    RBLOCK: tl.constexpr = 2
    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)
    tmp7 = tl.load(in_ptr0 + (2 + r0), None)
    tmp22 = tl.load(in_ptr2 + r0, None)
    tmp23 = tl.load(in_ptr2 + (2 + r0), None)
    tmp1 = tl.full([XBLOCK, RBLOCK], 4, tl.int32)
    tmp2 = tmp0 + tmp1
    tmp3 = tmp0 < 0
    tmp4 = tl.where(tmp3, tmp2, tmp0)
    tl.device_assert((0 <= tmp4) & (tmp4 < 4),
        'index out of bounds: 0 <= tmp4 < 4')
    tmp6 = tl.load(in_ptr1 + tmp4, None, eviction_policy='evict_last')
    tmp8 = tmp7 + tmp1
    tmp9 = tmp7 < 0
    tmp10 = tl.where(tmp9, tmp8, tmp7)
    tl.device_assert((0 <= tmp10) & (tmp10 < 4),
        'index out of bounds: 0 <= tmp10 < 4')
    tmp12 = tl.load(in_ptr1 + tmp10, None, eviction_policy='evict_last')
    tmp13 = tmp6 - tmp12
    tmp14 = tl.full([1, 1], 0, tl.int32)
    tmp15 = tmp14 < tmp13
    tmp16 = tmp15.to(tl.int8)
    tmp17 = tmp13 < tmp14
    tmp18 = tmp17.to(tl.int8)
    tmp19 = tmp16 - tmp18
    tmp20 = tmp19.to(tmp13.dtype)
    tmp21 = -tmp20
    tmp24 = tmp22 - tmp23
    tmp25 = tmp21 * tmp24
    tmp26 = 1.0
    tmp27 = tmp25 + tmp26
    tmp28 = 0.0
    tmp29 = triton_helpers.maximum(tmp27, tmp28)
    tmp30 = tl.broadcast_to(tmp29, [XBLOCK, RBLOCK])
    tmp32 = tl.sum(tmp30, 1)[:, None]
    tmp33 = 2.0
    tmp34 = tmp32 / tmp33
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp34, None)


def call(args):
    arg0_1, arg1_1 = args
    args.clear()
    assert_size_stride(arg0_1, (4, 1), (1, 1))
    assert_size_stride(arg1_1, (4, 1), (1, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = torch.ops.aten.randperm.default(4, device=device(type='cuda',
            index=0), pin_memory=False)
        buf1 = buf0
        del buf0
        buf2 = empty_strided_cuda((), (), torch.float32)
        buf3 = buf2
        del buf2
        get_raw_stream(0)
        triton_per_fused_add_clamp_min_mean_mul_neg_sign_sub_0[grid(1)](buf3,
            buf1, arg1_1, arg0_1, 1, 2, XBLOCK=1, num_warps=2, num_stages=1)
        del arg0_1
        del arg1_1
        del buf1
    return buf3,


class MarginRankingLossNew(nn.Module):

    def __init__(self, margin=1.0):
        super(MarginRankingLossNew, self).__init__()
        self.margin = margin

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