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
from torch._inductor.runtime.triton_helpers import libdevice
import torch.nn as nn
from torch.nn import *
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__to_copy_add_clamp_eq_mul_rsub_sort_sqrt_0(in_out_ptr0,
    in_ptr0, in_ptr1, 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_out_ptr0 + (r1 + 4 * x0), xmask, other=0.0)
    tmp1 = tl.load(in_ptr0 + 4 * x0, xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last')
    tmp9 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last')
    tmp12 = tl.load(in_ptr0 + 4 * r1, None, eviction_policy='evict_last')
    tmp14 = tl.load(in_ptr0 + (1 + 4 * r1), None, eviction_policy='evict_last')
    tmp17 = tl.load(in_ptr0 + (2 + 4 * r1), None, eviction_policy='evict_last')
    tmp20 = tl.load(in_ptr0 + (3 + 4 * r1), None, eviction_policy='evict_last')
    tmp28 = tl.load(in_ptr1 + (r1 + 4 * x0), xmask, other=0.0)
    tmp29 = tl.load(in_ptr1 + (x0 + 4 * r1), xmask, other=0.0)
    tmp2 = tmp1 * tmp1
    tmp4 = tmp3 * tmp3
    tmp5 = tmp2 + tmp4
    tmp7 = tmp6 * tmp6
    tmp8 = tmp5 + tmp7
    tmp10 = tmp9 * tmp9
    tmp11 = tmp8 + tmp10
    tmp13 = tmp12 * tmp12
    tmp15 = tmp14 * tmp14
    tmp16 = tmp13 + tmp15
    tmp18 = tmp17 * tmp17
    tmp19 = tmp16 + tmp18
    tmp21 = tmp20 * tmp20
    tmp22 = tmp19 + tmp21
    tmp23 = tmp11 + tmp22
    tmp24 = tmp0 + tmp23
    tmp25 = 1e-12
    tmp26 = triton_helpers.maximum(tmp24, tmp25)
    tmp27 = libdevice.sqrt(tmp26)
    tmp30 = tmp28 == tmp29
    tmp31 = tmp30.to(tl.float32)
    tmp32 = 1.0
    tmp33 = tmp32 - tmp31
    tmp34 = -9999999.0
    tmp35 = tmp33 * tmp34
    tmp36 = tmp27 + tmp35
    tmp37 = r1
    tmp38 = tmp37.to(tl.int16)
    tmp39 = tl.broadcast_to(tmp36, [XBLOCK, RBLOCK])
    tmp40 = tl.broadcast_to(tmp38, [XBLOCK, RBLOCK])
    tmp41, _tmp42 = triton_helpers.sort_with_index(tmp39, tmp40, None, 1,
        stable=False, descending=True)
    tmp43 = 9999999.0
    tmp44 = tmp31 * tmp43
    tmp45 = tmp27 + tmp44
    tmp46 = tl.broadcast_to(tmp45, [XBLOCK, RBLOCK])
    tmp47, _tmp48 = triton_helpers.sort_with_index(tmp46, tmp40, None, 1,
        stable=False, descending=False)
    tl.store(in_out_ptr0 + (r1 + 4 * x0), tmp24, xmask)
    tl.store(out_ptr0 + (r1 + 4 * x0), tmp41, xmask)
    tl.store(out_ptr1 + (r1 + 4 * x0), tmp47, xmask)


@triton.jit
def triton_per_fused_add_clamp_min_div_gt_mean_mul_neg_sub_sum_1(in_out_ptr0,
    in_ptr0, in_ptr1, out_ptr1, 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 + 4 * r0, None, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr1 + 4 * r0, None, eviction_policy='evict_last')
    tmp2 = tmp0 - tmp1
    tmp3 = -1.0
    tmp4 = tmp3 * tmp2
    tmp5 = 4.0
    tmp6 = tmp4 + tmp5
    tmp7 = 0.0
    tmp8 = triton_helpers.maximum(tmp6, tmp7)
    tmp9 = tl.broadcast_to(tmp8, [XBLOCK, RBLOCK])
    tmp11 = tl.sum(tmp9, 1)[:, None]
    tmp12 = tmp0 > tmp1
    tmp13 = tmp12.to(tl.int64)
    tmp14 = tl.broadcast_to(tmp13, [XBLOCK, RBLOCK])
    tmp16 = tl.sum(tmp14, 1)[:, None]
    tmp17 = tmp16.to(tl.float32)
    tmp18 = 1.0
    tmp19 = tmp17 * tmp18
    tmp20 = 0.25
    tmp21 = tmp19 * tmp20
    tmp22 = tmp11 / tmp5
    tl.store(out_ptr1 + tl.full([XBLOCK, 1], 0, tl.int32), tmp21, None)
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp22, 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(arg0_1, reinterpret_tensor(arg0_1, (4, 4), (1, 4),
            0), out=buf0)
        buf1 = buf0
        del buf0
        buf2 = 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__to_copy_add_clamp_eq_mul_rsub_sort_sqrt_0[grid(4)](
            buf1, arg0_1, arg1_1, buf2, buf4, 4, 4, XBLOCK=1, num_warps=2,
            num_stages=1)
        del arg0_1
        del arg1_1
        del buf1
        buf6 = empty_strided_cuda((), (), torch.float32)
        buf9 = empty_strided_cuda((), (), torch.float32)
        buf8 = buf6
        del buf6
        triton_per_fused_add_clamp_min_div_gt_mean_mul_neg_sub_sum_1[grid(1)](
            buf8, buf4, buf2, buf9, 1, 4, XBLOCK=1, num_warps=2, num_stages=1)
        del buf2
        del buf4
    return buf8, buf9


def _batch_hard(mat_distance, mat_similarity, indice=False):
    sorted_mat_distance, positive_indices = torch.sort(mat_distance + -
        9999999.0 * (1 - mat_similarity), dim=1, descending=True)
    hard_p = sorted_mat_distance[:, 0]
    hard_p_indice = positive_indices[:, 0]
    sorted_mat_distance, negative_indices = torch.sort(mat_distance + 
        9999999.0 * mat_similarity, dim=1, descending=False)
    hard_n = sorted_mat_distance[:, 0]
    hard_n_indice = negative_indices[:, 0]
    if indice:
        return hard_p, hard_n, hard_p_indice, hard_n_indice
    return hard_p, hard_n


def euclidean_dist(x, y):
    m, n = x.size(0), y.size(0)
    xx = torch.pow(x, 2).sum(1, keepdim=True).expand(m, n)
    yy = torch.pow(y, 2).sum(1, keepdim=True).expand(n, m).t()
    dist = xx + yy
    dist.addmm_(x, y.t(), beta=1, alpha=-2)
    dist = dist.clamp(min=1e-12).sqrt()
    return dist


class TripletLossNew(nn.Module):

    def __init__(self, margin, normalize_feature=False):
        super(TripletLossNew, self).__init__()
        self.margin = margin
        self.normalize_feature = normalize_feature
        self.margin_loss = nn.MarginRankingLoss(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], output[1]


Measured latency_ms: 0.039515