# AOT ID: ['0_inference']
from ctypes import c_void_p, c_long, c_int
import torch
import math
import random
import os
import tempfile
from math import inf, nan
from torch._inductor.hooks import run_intermediate_hooks
from torch._inductor.utils import maybe_profile
from torch._inductor.codegen.memory_planning import _align as align
from torch import device, empty_strided
from torch._inductor.async_compile import AsyncCompile
from torch._inductor.select_algorithm import extern_kernels
from torch._inductor.codegen.multi_kernel import MultiKernelCall
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid, split_scan_grid, grid_combo_kernels, start_graph, end_graph
from torch._C import _cuda_getCurrentRawStream as get_raw_stream

aten = torch.ops.aten
inductor_ops = torch.ops.inductor
_quantized = torch.ops._quantized
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cpu = torch._C._dynamo.guards._empty_strided_cpu
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
empty_strided_xpu = torch._C._dynamo.guards._empty_strided_xpu
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
alloc_from_pool = torch.ops.inductor._alloc_from_pool
async_compile = AsyncCompile()


# kernel path: runs/run_shard_7/inductor_cache/nq/cnqxadd2nqiglmqaw4vqaddkj5bet76gl7wd4ucmiz7rls7hcdpu.py
# Topologically Sorted Source Nodes: [add, mul, dist], Original ATen: [aten.add, aten.mul, aten.sub]
# Source node to ATen node mapping:
#   add => add
#   dist => sub
#   mul => mul
# Graph fragment:
#   %add : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%expand, %expand_1), kwargs = {})
#   %mul : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%mm, 2), kwargs = {})
#   %sub : [num_users=2] = call_function[target=torch.ops.aten.sub.Tensor](args = (%add, %mul), kwargs = {})
triton_poi_fused_add_mul_sub_0 = async_compile.triton('triton_poi_fused_add_mul_sub_0', '''
import triton
import triton.language as tl
from triton.compiler.compiler import AttrsDescriptor

from torch._inductor.runtime import triton_helpers, triton_heuristics
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
from torch._inductor.runtime.hints import AutotuneHint, ReductionHint, TileHint, instance_descriptor, DeviceProperties

@triton_heuristics.pointwise(
    size_hints=[16], 
    filename=__file__,
    triton_meta={'signature': {0: '*fp32', 1: '*fp32', 2: 'i32'}, 'device': DeviceProperties(type='cuda', index=0, cc=80, major=8, regs_per_multiprocessor=65536, max_threads_per_multi_processor=2048, multi_processor_count=108), 'constants': {}, 'configs': [AttrsDescriptor(divisible_by_16=(0, 1, 2), equal_to_1=())]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_poi_fused_add_mul_sub_0', 'mutated_arg_names': ['in_out_ptr0'], 'no_x_dim': False, 'num_load': 9, 'num_reduction': 0, 'backend_hash': 'A9C866B4A14FD3277824029365D703C2427B2E685E54EC9B3EF4ADC8D1EEAC1D', 'are_deterministic_algorithms_enabled': False, 'assert_indirect_indexing': True, 'autotune_local_cache': True, 'autotune_pointwise': True, 'autotune_remote_cache': None, 'force_disable_caches': False, 'dynamic_scale_rblock': True, 'max_autotune': False, 'max_autotune_pointwise': False, 'min_split_scan_rblock': 256, 'spill_threshold': 16, 'store_cubin': False},
    min_elem_per_thread=0
)
@triton.jit
def triton_poi_fused_add_mul_sub_0(in_out_ptr0, in_ptr0, xnumel, XBLOCK : tl.constexpr):
    xnumel = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x1 = (xindex // 4)
    x0 = xindex % 4
    x2 = xindex
    tmp0 = tl.load(in_ptr0 + (4*x1), xmask, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr0 + (1 + (4*x1)), xmask, eviction_policy='evict_last')
    tmp5 = tl.load(in_ptr0 + (2 + (4*x1)), xmask, eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr0 + (3 + (4*x1)), xmask, eviction_policy='evict_last')
    tmp11 = tl.load(in_ptr0 + (4*x0), xmask, eviction_policy='evict_last')
    tmp13 = tl.load(in_ptr0 + (1 + (4*x0)), xmask, eviction_policy='evict_last')
    tmp16 = tl.load(in_ptr0 + (2 + (4*x0)), xmask, eviction_policy='evict_last')
    tmp19 = tl.load(in_ptr0 + (3 + (4*x0)), xmask, eviction_policy='evict_last')
    tmp23 = tl.load(in_out_ptr0 + (x2), xmask)
    tmp1 = tmp0 * tmp0
    tmp3 = tmp2 * tmp2
    tmp4 = tmp1 + tmp3
    tmp6 = tmp5 * tmp5
    tmp7 = tmp4 + tmp6
    tmp9 = tmp8 * tmp8
    tmp10 = tmp7 + tmp9
    tmp12 = tmp11 * tmp11
    tmp14 = tmp13 * tmp13
    tmp15 = tmp12 + tmp14
    tmp17 = tmp16 * tmp16
    tmp18 = tmp15 + tmp17
    tmp20 = tmp19 * tmp19
    tmp21 = tmp18 + tmp20
    tmp22 = tmp10 + tmp21
    tmp24 = 2.0
    tmp25 = tmp23 * tmp24
    tmp26 = tmp22 - tmp25
    tl.store(in_out_ptr0 + (x2), tmp26, xmask)
''', device_str='cuda')


# kernel path: runs/run_shard_7/inductor_cache/ka/ckaxq6y5mhx62yabsjzhpn6yjyeenx7523rr6eunxttnkjz43mmm.py
# Topologically Sorted Source Nodes: [is_pos, mul_1, max_1, is_neg, mul_2, mul_3, add_1, min_1, loss], Original ATen: [aten.eq, aten.mul, aten.max, aten.bitwise_not, aten.add, aten.min, aten.neg, aten.sub, aten.clamp_min, aten.mean]
# Source node to ATen node mapping:
#   add_1 => add_1
#   is_neg => bitwise_not
#   is_pos => eq
#   loss => add_2, clamp_min, full_default, mean, mul_4, sub_1
#   max_1 => max_1
#   min_1 => min_1
#   mul_1 => mul_1
#   mul_2 => mul_2
#   mul_3 => mul_3
# Graph fragment:
#   %eq : [num_users=3] = call_function[target=torch.ops.aten.eq.Tensor](args = (%unsqueeze, %unsqueeze_1), kwargs = {})
#   %mul_1 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%sub, %eq), kwargs = {})
#   %max_1 : [num_users=1] = call_function[target=torch.ops.aten.max.dim](args = (%mul_1, 1), kwargs = {})
#   %bitwise_not : [num_users=1] = call_function[target=torch.ops.aten.bitwise_not.default](args = (%eq,), kwargs = {})
#   %mul_2 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%sub, %bitwise_not), kwargs = {})
#   %mul_3 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%eq, 1000000000.0), kwargs = {})
#   %add_1 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%mul_2, %mul_3), kwargs = {})
#   %min_1 : [num_users=1] = call_function[target=torch.ops.aten.min.dim](args = (%add_1, 1), kwargs = {})
#   %full_default : [num_users=1] = call_function[target=torch.ops.aten.full.default](args = ([4, 4], -1.0), kwargs = {dtype: torch.float32, layout: torch.strided, device: cuda:0, pin_memory: False})
#   %sub_1 : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (%getitem_2, %getitem), kwargs = {})
#   %mul_4 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%full_default, %sub_1), kwargs = {})
#   %add_2 : [num_users=1] = call_function[target=torch.ops.aten.add.Scalar](args = (%mul_4, 0.3), kwargs = {})
#   %clamp_min : [num_users=1] = call_function[target=torch.ops.aten.clamp_min.default](args = (%add_2, 0), kwargs = {})
#   %mean : [num_users=1] = call_function[target=torch.ops.aten.mean.default](args = (%clamp_min,), kwargs = {})
triton_per_fused_add_bitwise_not_clamp_min_eq_max_mean_min_mul_neg_sub_1 = async_compile.triton('triton_per_fused_add_bitwise_not_clamp_min_eq_max_mean_min_mul_neg_sub_1', '''
import triton
import triton.language as tl
from triton.compiler.compiler import AttrsDescriptor

from torch._inductor.runtime import triton_helpers, triton_heuristics
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
from torch._inductor.runtime.hints import AutotuneHint, ReductionHint, TileHint, instance_descriptor, DeviceProperties

@triton_heuristics.persistent_reduction(
    size_hints=[1, 16],
    reduction_hint=ReductionHint.INNER,
    filename=__file__,
    triton_meta={'signature': {0: '*fp32', 1: '*fp32', 2: '*fp32', 3: 'i32', 4: 'i32'}, 'device': DeviceProperties(type='cuda', index=0, cc=80, major=8, regs_per_multiprocessor=65536, max_threads_per_multi_processor=2048, multi_processor_count=108), 'constants': {3: 1}, 'configs': [AttrsDescriptor(divisible_by_16=(0, 1, 2, 4), equal_to_1=(3,))]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_per_fused_add_bitwise_not_clamp_min_eq_max_mean_min_mul_neg_sub_1', 'mutated_arg_names': ['in_out_ptr0'], 'no_x_dim': False, 'num_load': 9, 'num_reduction': 1, 'backend_hash': 'A9C866B4A14FD3277824029365D703C2427B2E685E54EC9B3EF4ADC8D1EEAC1D', 'are_deterministic_algorithms_enabled': False, 'assert_indirect_indexing': True, 'autotune_local_cache': True, 'autotune_pointwise': True, 'autotune_remote_cache': None, 'force_disable_caches': False, 'dynamic_scale_rblock': True, 'max_autotune': False, 'max_autotune_pointwise': False, 'min_split_scan_rblock': 256, 'spill_threshold': 16, 'store_cubin': False}
)
@triton.jit
def triton_per_fused_add_bitwise_not_clamp_min_eq_max_mean_min_mul_neg_sub_1(in_out_ptr0, in_ptr0, in_ptr1, xnumel, rnumel, XBLOCK : tl.constexpr):
    xnumel = 1
    rnumel = 16
    RBLOCK: tl.constexpr = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = tl.full([XBLOCK, RBLOCK], True, tl.int1)
    rindex = tl.arange(0, RBLOCK)[None, :]
    roffset = 0
    rmask = tl.full([XBLOCK, RBLOCK], True, tl.int1)
    r0 = rindex % 4
    r2 = rindex
    tmp0 = tl.load(in_ptr0 + (r0), None, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr1 + (r0), None, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr1 + (r2), None)
    tmp6 = tl.load(in_ptr0 + (4 + r0), None, eviction_policy='evict_last')
    tmp7 = tl.load(in_ptr1 + (4 + r0), None, eviction_policy='evict_last')
    tmp12 = tl.load(in_ptr0 + (8 + r0), None, eviction_policy='evict_last')
    tmp13 = tl.load(in_ptr1 + (8 + r0), None, eviction_policy='evict_last')
    tmp18 = tl.load(in_ptr0 + (12 + r0), None, eviction_policy='evict_last')
    tmp19 = tl.load(in_ptr1 + (12 + r0), None, eviction_policy='evict_last')
    tmp3 = tmp1 == tmp2
    tmp4 = tmp3.to(tl.float32)
    tmp5 = tmp0 * tmp4
    tmp8 = tmp7 == tmp2
    tmp9 = tmp8.to(tl.float32)
    tmp10 = tmp6 * tmp9
    tmp11 = triton_helpers.maximum(tmp5, tmp10)
    tmp14 = tmp13 == tmp2
    tmp15 = tmp14.to(tl.float32)
    tmp16 = tmp12 * tmp15
    tmp17 = triton_helpers.maximum(tmp11, tmp16)
    tmp20 = tmp19 == tmp2
    tmp21 = tmp20.to(tl.float32)
    tmp22 = tmp18 * tmp21
    tmp23 = triton_helpers.maximum(tmp17, tmp22)
    tmp24 = tmp3 == 0
    tmp25 = tmp24.to(tl.float32)
    tmp26 = tmp0 * tmp25
    tmp27 = 1000000000.0
    tmp28 = tmp4 * tmp27
    tmp29 = tmp26 + tmp28
    tmp30 = tmp8 == 0
    tmp31 = tmp30.to(tl.float32)
    tmp32 = tmp6 * tmp31
    tmp33 = tmp9 * tmp27
    tmp34 = tmp32 + tmp33
    tmp35 = triton_helpers.minimum(tmp29, tmp34)
    tmp36 = tmp14 == 0
    tmp37 = tmp36.to(tl.float32)
    tmp38 = tmp12 * tmp37
    tmp39 = tmp15 * tmp27
    tmp40 = tmp38 + tmp39
    tmp41 = triton_helpers.minimum(tmp35, tmp40)
    tmp42 = tmp20 == 0
    tmp43 = tmp42.to(tl.float32)
    tmp44 = tmp18 * tmp43
    tmp45 = tmp21 * tmp27
    tmp46 = tmp44 + tmp45
    tmp47 = triton_helpers.minimum(tmp41, tmp46)
    tmp48 = tmp47 - tmp23
    tmp49 = -1.0
    tmp50 = tmp49 * tmp48
    tmp51 = 0.3
    tmp52 = tmp50 + tmp51
    tmp53 = 0.0
    tmp54 = triton_helpers.maximum(tmp52, tmp53)
    tmp55 = tl.broadcast_to(tmp54, [XBLOCK, RBLOCK])
    tmp57 = tl.sum(tmp55, 1)[:, None]
    tmp58 = 16.0
    tmp59 = tmp57 / tmp58
    tl.debug_barrier()
    tl.store(in_out_ptr0 + (tl.full([XBLOCK, 1], 0, tl.int32)), tmp59, None)
''', device_str='cuda')


async_compile.wait(globals())
del async_compile

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)
        # Topologically Sorted Source Nodes: [matmul], Original ATen: [aten.mm]
        extern_kernels.mm(arg0_1, reinterpret_tensor(arg0_1, (4, 4), (1, 4), 0), out=buf0)
        buf1 = buf0; del buf0  # reuse
        # Topologically Sorted Source Nodes: [add, mul, dist], Original ATen: [aten.add, aten.mul, aten.sub]
        stream0 = get_raw_stream(0)
        triton_poi_fused_add_mul_sub_0.run(buf1, arg0_1, 16, grid=grid(16), stream=stream0)
        del arg0_1
        buf4 = empty_strided_cuda((), (), torch.float32)
        buf5 = buf4; del buf4  # reuse
        # Topologically Sorted Source Nodes: [is_pos, mul_1, max_1, is_neg, mul_2, mul_3, add_1, min_1, loss], Original ATen: [aten.eq, aten.mul, aten.max, aten.bitwise_not, aten.add, aten.min, aten.neg, aten.sub, aten.clamp_min, aten.mean]
        triton_per_fused_add_bitwise_not_clamp_min_eq_max_mean_min_mul_neg_sub_1.run(buf5, buf1, arg1_1, 1, 16, grid=grid(1), stream=stream0)
        del arg1_1
        del buf1
    return (buf5, )


def benchmark_compiled_module(times=10, repeat=10):
    from torch._dynamo.testing import rand_strided
    from torch._inductor.utils import print_performance
    arg0_1 = rand_strided((4, 4), (4, 1), device='cuda:0', dtype=torch.float32)
    arg1_1 = rand_strided((4, 4), (4, 1), device='cuda:0', dtype=torch.float32)
    fn = lambda: call([arg0_1, arg1_1])
    return print_performance(fn, times=times, repeat=repeat)


if __name__ == "__main__":
    from torch._inductor.wrapper_benchmark import compiled_module_main
    compiled_module_main('None', benchmark_compiled_module)

import torch
from torch import Tensor
from torch import nn
from torch.nn import functional as F


def euclidean_dist(x: 'Tensor', y: 'Tensor') ->Tensor:
    xx, yy = torch.meshgrid((x ** 2).sum(1), (y ** 2).sum(1))
    return xx + yy - 2 * (x @ y.t())


class TripletLoss(nn.Module):
    """
    Modified from Tong Xiao's open-reid (https://github.com/Cysu/open-reid).
    Related Triplet Loss theory can be found in paper 'In Defense of the Triplet
    Loss for Person Re-Identification'
    """

    def __init__(self, margin: 'float'=0.3) ->None:
        super().__init__()
        self.margin = margin

    def forward(self, features: 'Tensor', targets: 'Tensor') ->Tensor:
        dist = euclidean_dist(features, features)
        is_pos = targets.unsqueeze(0) == targets.unsqueeze(1)
        is_neg = ~is_pos
        dist_ap, dist_an = self.hard_example_mining(dist, is_pos, is_neg)
        y = torch.ones_like(dist_an)
        if self.margin > 0:
            loss = F.margin_ranking_loss(dist_an, dist_ap, y, self.margin)
        else:
            loss = F.soft_margin_loss(dist_an - dist_ap, y)
            if loss == float('inf'):
                loss = F.margin_ranking_loss(dist_an, dist_ap, y, 0.3)
        return loss

    def hard_example_mining(self, dist, is_pos, is_neg):
        """For each anchor, find the hardest positive and negative sample.
        Args:
            dist_mat: pair wise distance between samples, shape [N, M]
            is_pos: positive index with shape [N, M]
            is_neg: negative index with shape [N, M]
        Returns:
            dist_ap: Tensor, distance(anchor, positive); shape [N]
            dist_an: Tensor, distance(anchor, negative); shape [N]
        NOTE: Only consider the case in which all labels have same num of samples,
        thus we can cope with all anchors in parallel.
        """
        assert len(dist.size()) == 2
        dist_ap = torch.max(dist * is_pos, dim=1)[0]
        dist_an = torch.min(dist * is_neg + is_pos * 1000000000.0, dim=1)[0]
        return dist_ap, dist_an

    def weighted_example_mining(self, dist, is_pos, is_neg):
        """For each anchor, find the weighted positive and negative sample.
        """
        assert len(dist.size()) == 2
        dist_ap = dist * is_pos
        dist_an = dist * is_neg
        weights_ap = self.softmax_weights(dist_ap, is_pos)
        weights_an = self.softmax_weights(-dist_an, is_neg)
        dist_ap = torch.sum(dist_ap * weights_ap, dim=1)
        dist_an = torch.sum(dist_an * weights_an, dim=1)
        return dist_ap, dist_an

    def softmax_weights(self, dist, mask):
        max_v = torch.max(dist * mask, dim=1, keepdim=True)[0]
        diff = dist - max_v
        Z = torch.sum(torch.exp(diff) * mask, dim=1, keepdim=True) + 1e-06
        W = torch.exp(diff) * mask / Z
        return W


def get_inputs():
    return [torch.rand([4, 4]), torch.rand([4, 4])]


def get_init_inputs():
    return [[], {}]

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 import Tensor
from torch import 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_poi_fused_add_mul_sub_0(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl
    .constexpr):
    xnumel = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x1 = xindex // 4
    x0 = xindex % 4
    x2 = xindex
    tmp0 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp5 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp11 = tl.load(in_ptr0 + 4 * x0, xmask, eviction_policy='evict_last')
    tmp13 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp16 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp19 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp23 = tl.load(in_out_ptr0 + x2, xmask)
    tmp1 = tmp0 * tmp0
    tmp3 = tmp2 * tmp2
    tmp4 = tmp1 + tmp3
    tmp6 = tmp5 * tmp5
    tmp7 = tmp4 + tmp6
    tmp9 = tmp8 * tmp8
    tmp10 = tmp7 + tmp9
    tmp12 = tmp11 * tmp11
    tmp14 = tmp13 * tmp13
    tmp15 = tmp12 + tmp14
    tmp17 = tmp16 * tmp16
    tmp18 = tmp15 + tmp17
    tmp20 = tmp19 * tmp19
    tmp21 = tmp18 + tmp20
    tmp22 = tmp10 + tmp21
    tmp24 = 2.0
    tmp25 = tmp23 * tmp24
    tmp26 = tmp22 - tmp25
    tl.store(in_out_ptr0 + x2, tmp26, xmask)


@triton.jit
def triton_per_fused_add_bitwise_not_clamp_min_eq_max_mean_min_mul_neg_sub_1(
    in_out_ptr0, in_ptr0, in_ptr1, xnumel, rnumel, XBLOCK: tl.constexpr):
    RBLOCK: tl.constexpr = 16
    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 % 4
    r2 = rindex
    tmp0 = tl.load(in_ptr0 + r0, None, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr1 + r0, None, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr1 + r2, None)
    tmp6 = tl.load(in_ptr0 + (4 + r0), None, eviction_policy='evict_last')
    tmp7 = tl.load(in_ptr1 + (4 + r0), None, eviction_policy='evict_last')
    tmp12 = tl.load(in_ptr0 + (8 + r0), None, eviction_policy='evict_last')
    tmp13 = tl.load(in_ptr1 + (8 + r0), None, eviction_policy='evict_last')
    tmp18 = tl.load(in_ptr0 + (12 + r0), None, eviction_policy='evict_last')
    tmp19 = tl.load(in_ptr1 + (12 + r0), None, eviction_policy='evict_last')
    tmp3 = tmp1 == tmp2
    tmp4 = tmp3.to(tl.float32)
    tmp5 = tmp0 * tmp4
    tmp8 = tmp7 == tmp2
    tmp9 = tmp8.to(tl.float32)
    tmp10 = tmp6 * tmp9
    tmp11 = triton_helpers.maximum(tmp5, tmp10)
    tmp14 = tmp13 == tmp2
    tmp15 = tmp14.to(tl.float32)
    tmp16 = tmp12 * tmp15
    tmp17 = triton_helpers.maximum(tmp11, tmp16)
    tmp20 = tmp19 == tmp2
    tmp21 = tmp20.to(tl.float32)
    tmp22 = tmp18 * tmp21
    tmp23 = triton_helpers.maximum(tmp17, tmp22)
    tmp24 = tmp3 == 0
    tmp25 = tmp24.to(tl.float32)
    tmp26 = tmp0 * tmp25
    tmp27 = 1000000000.0
    tmp28 = tmp4 * tmp27
    tmp29 = tmp26 + tmp28
    tmp30 = tmp8 == 0
    tmp31 = tmp30.to(tl.float32)
    tmp32 = tmp6 * tmp31
    tmp33 = tmp9 * tmp27
    tmp34 = tmp32 + tmp33
    tmp35 = triton_helpers.minimum(tmp29, tmp34)
    tmp36 = tmp14 == 0
    tmp37 = tmp36.to(tl.float32)
    tmp38 = tmp12 * tmp37
    tmp39 = tmp15 * tmp27
    tmp40 = tmp38 + tmp39
    tmp41 = triton_helpers.minimum(tmp35, tmp40)
    tmp42 = tmp20 == 0
    tmp43 = tmp42.to(tl.float32)
    tmp44 = tmp18 * tmp43
    tmp45 = tmp21 * tmp27
    tmp46 = tmp44 + tmp45
    tmp47 = triton_helpers.minimum(tmp41, tmp46)
    tmp48 = tmp47 - tmp23
    tmp49 = -1.0
    tmp50 = tmp49 * tmp48
    tmp51 = 0.3
    tmp52 = tmp50 + tmp51
    tmp53 = 0.0
    tmp54 = triton_helpers.maximum(tmp52, tmp53)
    tmp55 = tl.broadcast_to(tmp54, [XBLOCK, RBLOCK])
    tmp57 = tl.sum(tmp55, 1)[:, None]
    tmp58 = 16.0
    tmp59 = tmp57 / tmp58
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp59, 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
        get_raw_stream(0)
        triton_poi_fused_add_mul_sub_0[grid(16)](buf1, arg0_1, 16, XBLOCK=
            16, num_warps=1, num_stages=1)
        del arg0_1
        buf4 = empty_strided_cuda((), (), torch.float32)
        buf5 = buf4
        del buf4
        triton_per_fused_add_bitwise_not_clamp_min_eq_max_mean_min_mul_neg_sub_1[
            grid(1)](buf5, buf1, arg1_1, 1, 16, XBLOCK=1, num_warps=2,
            num_stages=1)
        del arg1_1
        del buf1
    return buf5,


def euclidean_dist(x: 'Tensor', y: 'Tensor') ->Tensor:
    xx, yy = torch.meshgrid((x ** 2).sum(1), (y ** 2).sum(1))
    return xx + yy - 2 * (x @ y.t())


class TripletLossNew(nn.Module):
    """
    Modified from Tong Xiao's open-reid (https://github.com/Cysu/open-reid).
    Related Triplet Loss theory can be found in paper 'In Defense of the Triplet
    Loss for Person Re-Identification'
    """

    def __init__(self, margin: 'float'=0.3) ->None:
        super().__init__()
        self.margin = margin

    def hard_example_mining(self, dist, is_pos, is_neg):
        """For each anchor, find the hardest positive and negative sample.
        Args:
            dist_mat: pair wise distance between samples, shape [N, M]
            is_pos: positive index with shape [N, M]
            is_neg: negative index with shape [N, M]
        Returns:
            dist_ap: Tensor, distance(anchor, positive); shape [N]
            dist_an: Tensor, distance(anchor, negative); shape [N]
        NOTE: Only consider the case in which all labels have same num of samples,
        thus we can cope with all anchors in parallel.
        """
        assert len(dist.size()) == 2
        dist_ap = torch.max(dist * is_pos, dim=1)[0]
        dist_an = torch.min(dist * is_neg + is_pos * 1000000000.0, dim=1)[0]
        return dist_ap, dist_an

    def weighted_example_mining(self, dist, is_pos, is_neg):
        """For each anchor, find the weighted positive and negative sample.
        """
        assert len(dist.size()) == 2
        dist_ap = dist * is_pos
        dist_an = dist * is_neg
        weights_ap = self.softmax_weights(dist_ap, is_pos)
        weights_an = self.softmax_weights(-dist_an, is_neg)
        dist_ap = torch.sum(dist_ap * weights_ap, dim=1)
        dist_an = torch.sum(dist_an * weights_an, dim=1)
        return dist_ap, dist_an

    def softmax_weights(self, dist, mask):
        max_v = torch.max(dist * mask, dim=1, keepdim=True)[0]
        diff = dist - max_v
        Z = torch.sum(torch.exp(diff) * mask, dim=1, keepdim=True) + 1e-06
        W = torch.exp(diff) * mask / Z
        return W

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