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
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
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor


@triton.jit
def triton_per_fused_diag_embed_mul_sort_sub_0(in_ptr0, out_ptr0, out_ptr1,
    xnumel, rnumel, XBLOCK: tl.constexpr):
    xnumel = 4
    RBLOCK: tl.constexpr = 4
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    rindex = tl.arange(0, RBLOCK)[None, :]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    r1 = rindex
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + (r1 + 4 * x0), xmask, other=0.0)
    tmp4 = tl.load(in_ptr0 + 5 * r1, None, eviction_policy='evict_last')
    tmp1 = r1
    tmp2 = x0
    tmp3 = tmp1 == tmp2
    tmp5 = 0.0
    tmp6 = tl.where(tmp3, tmp4, tmp5)
    tmp7 = 2.0
    tmp8 = tmp6 * tmp7
    tmp9 = tmp0 - tmp8
    tmp10 = tmp1.to(tl.int16)
    tmp11 = tl.broadcast_to(tmp9, [XBLOCK, RBLOCK])
    tmp12 = tl.broadcast_to(tmp10, [XBLOCK, RBLOCK])
    tmp13, _tmp14 = triton_helpers.sort_with_index(tmp11, tmp12, None, 1,
        stable=False, descending=True)
    tl.store(out_ptr0 + (r1 + 4 * x0), tmp9, xmask)
    tl.store(out_ptr1 + (r1 + 4 * x0), tmp13, xmask)


@triton.jit
def triton_per_fused_sort_1(in_ptr0, out_ptr0, xnumel, rnumel, XBLOCK: tl.
    constexpr):
    xnumel = 4
    RBLOCK: tl.constexpr = 4
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    rindex = tl.arange(0, RBLOCK)[None, :]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    r1 = rindex
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 4 * r1), xmask, other=0.0)
    tmp1 = r1
    tmp2 = tmp1.to(tl.int16)
    tmp3 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
    tmp4 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK])
    tmp5, _tmp6 = triton_helpers.sort_with_index(tmp3, tmp4, None, 1,
        stable=False, descending=True)
    tl.store(out_ptr0 + (x0 + 4 * r1), tmp5, xmask)


@triton.jit
def triton_per_fused_add_clamp_sum_2(in_out_ptr0, in_ptr0, in_ptr1, in_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 + r0, None)
    tmp1 = tl.load(in_ptr1 + 5 * r0, None, eviction_policy='evict_last')
    tmp10 = tl.load(in_ptr2 + 4 * r0, None, eviction_policy='evict_last')
    tmp2 = 0.2
    tmp3 = tmp2 - tmp1
    tmp4 = tmp0 + tmp3
    tmp5 = 0.0
    tmp6 = triton_helpers.maximum(tmp4, tmp5)
    tmp7 = tl.broadcast_to(tmp6, [XBLOCK, RBLOCK])
    tmp9 = tl.sum(tmp7, 1)[:, None]
    tmp11 = tmp10 + tmp3
    tmp12 = triton_helpers.maximum(tmp11, tmp5)
    tmp13 = tl.broadcast_to(tmp12, [XBLOCK, RBLOCK])
    tmp15 = tl.sum(tmp13, 1)[:, None]
    tmp16 = tmp9 + tmp15
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp16, None)


def call(args):
    arg0_1, arg1_1 = args
    args.clear()
    assert_size_stride(arg0_1, (4, 4), (4, 1))
    assert_size_stride(arg1_1, (4, 4), (4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        extern_kernels.mm(arg1_1, reinterpret_tensor(arg0_1, (4, 4), (1, 4),
            0), out=buf0)
        del arg0_1
        del arg1_1
        buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf4 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        get_raw_stream(0)
        triton_per_fused_diag_embed_mul_sort_sub_0[grid(4)](buf0, buf1,
            buf4, 4, 4, XBLOCK=1, num_warps=2, num_stages=1)
        buf2 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        triton_per_fused_sort_1[grid(4)](buf1, buf2, 4, 4, XBLOCK=1,
            num_warps=2, num_stages=1)
        del buf1
        buf6 = empty_strided_cuda((), (), torch.float32)
        buf8 = buf6
        del buf6
        triton_per_fused_add_clamp_sum_2[grid(1)](buf8, buf2, buf0, buf4, 1,
            4, XBLOCK=1, num_warps=2, num_stages=1)
        del buf0
        del buf2
        del buf4
    return buf8,


class HardNegativeContrastiveLossNew(nn.Module):

    def __init__(self, nmax=1, margin=0.2):
        super(HardNegativeContrastiveLossNew, self).__init__()
        self.margin = margin
        self.nmax = nmax

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