# 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_6/inductor_cache/sf/csfowo4kuknw7sz5gvsrkrrm77qgppotuaecd2lgx7kf5jgyejoa.py
# Topologically Sorted Source Nodes: [mask], Original ATen: [aten.eq]
# Source node to ATen node mapping:
#   mask => eq
# Graph fragment:
#   %eq : [num_users=2] = call_function[target=torch.ops.aten.eq.Tensor](args = (%unsqueeze_2, %unsqueeze_3), kwargs = {})
triton_poi_fused_eq_0 = async_compile.triton('triton_poi_fused_eq_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=[1024], 
    filename=__file__,
    triton_meta={'signature': {0: '*fp32', 1: '*i1', 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_eq_0', 'mutated_arg_names': [], 'no_x_dim': False, 'num_load': 2, '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_eq_0(in_ptr0, out_ptr0, xnumel, XBLOCK : tl.constexpr):
    xnumel = 1024
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x3 = xindex % 256
    x0 = xindex % 64
    x2 = (xindex // 256)
    x4 = xindex
    tmp0 = tl.load(in_ptr0 + (x3), xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr0 + (x0 + (64*x2)), xmask, eviction_policy='evict_last')
    tmp2 = tmp0 == tmp1
    tl.store(out_ptr0 + (x4), tmp2, xmask)
''', device_str='cuda')


# kernel path: runs/run_shard_6/inductor_cache/i4/ci457pnjg4fsfqqsyoibupqa7vgipgqjf6zs5wlpi5n37vmwyohl.py
# Topologically Sorted Source Nodes: [fill_diagonal_], Original ATen: [aten.fill]
# Source node to ATen node mapping:
#   fill_diagonal_ => full_default
# Graph fragment:
#   %full_default : [num_users=1] = call_function[target=torch.ops.aten.full.default](args = ([4], False), kwargs = {dtype: torch.bool, layout: torch.strided, device: cuda:0, pin_memory: False})
#   %copy__default : [num_users=0] = call_function[target=torch.ops.aten.copy_.default](args = (%as_strided_default, %full_default), kwargs = {})
triton_poi_fused_fill_1 = async_compile.triton('triton_poi_fused_fill_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.pointwise(
    size_hints=[4], 
    filename=__file__,
    triton_meta={'signature': {0: '*i1', 1: '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,), equal_to_1=())]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_poi_fused_fill_1', 'mutated_arg_names': ['out_ptr0'], 'no_x_dim': False, 'num_load': 0, '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_fill_1(out_ptr0, xnumel, XBLOCK : tl.constexpr):
    xnumel = 4
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = tl.full([1], False, tl.int1)
    tl.store(out_ptr0 + (341*x0), tmp0, xmask)
''', device_str='cuda')


# kernel path: runs/run_shard_6/inductor_cache/66/c66uth72bjugbr4ng5m4qh4gz5hmg6x2mbdc7zf7lcwr5jmgh3dh.py
# Topologically Sorted Source Nodes: [float_1, anchor_positive_dist, max_1, max_2, labels_equal, mask_1, float_2, sub, mul_1, anchor_negative_dist, min_1, sub_1, add_1, loss, mean], Original ATen: [aten._to_copy, aten.mul, aten.max, aten.eq, aten.bitwise_not, aten.rsub, aten.add, aten.min, aten.sub, aten.relu, aten.mean]
# Source node to ATen node mapping:
#   add_1 => add_1
#   anchor_negative_dist => add
#   anchor_positive_dist => mul
#   float_1 => convert_element_type
#   float_2 => convert_element_type_1
#   labels_equal => eq_1
#   loss => relu
#   mask_1 => bitwise_not
#   max_1 => max_1
#   max_2 => max_2
#   mean => mean
#   min_1 => min_1
#   mul_1 => mul_1
#   sub => sub
#   sub_1 => sub_1
# Graph fragment:
#   %convert_element_type : [num_users=1] = call_function[target=torch.ops.prims.convert_element_type.default](args = (%eq, torch.float32), kwargs = {})
#   %mul : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%convert_element_type, %squeeze), kwargs = {})
#   %max_1 : [num_users=1] = call_function[target=torch.ops.aten.max.dim](args = (%mul, 1, True), kwargs = {})
#   %max_2 : [num_users=1] = call_function[target=torch.ops.aten.max.dim](args = (%squeeze, 1, True), kwargs = {})
#   %eq_1 : [num_users=1] = call_function[target=torch.ops.aten.eq.Tensor](args = (%unsqueeze_4, %unsqueeze_5), kwargs = {})
#   %bitwise_not : [num_users=1] = call_function[target=torch.ops.aten.bitwise_not.default](args = (%eq_1,), kwargs = {})
#   %convert_element_type_1 : [num_users=1] = call_function[target=torch.ops.prims.convert_element_type.default](args = (%bitwise_not, torch.float32), kwargs = {})
#   %sub : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (1.0, %convert_element_type_1), kwargs = {})
#   %mul_1 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%getitem_2, %sub), kwargs = {})
#   %add : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%squeeze, %mul_1), kwargs = {})
#   %min_1 : [num_users=1] = call_function[target=torch.ops.aten.min.dim](args = (%add, 1, True), kwargs = {})
#   %sub_1 : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (%getitem, %getitem_4), kwargs = {})
#   %add_1 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%sub_1, 1.0), kwargs = {})
#   %relu : [num_users=1] = call_function[target=torch.ops.aten.relu.default](args = (%add_1,), kwargs = {})
#   %mean : [num_users=1] = call_function[target=torch.ops.aten.mean.default](args = (%relu,), kwargs = {})
triton_per_fused__to_copy_add_bitwise_not_eq_max_mean_min_mul_relu_rsub_sub_2 = async_compile.triton('triton_per_fused__to_copy_add_bitwise_not_eq_max_mean_min_mul_relu_rsub_sub_2', '''
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, 256],
    reduction_hint=ReductionHint.INNER,
    filename=__file__,
    triton_meta={'signature': {0: '*fp32', 1: '*fp32', 2: '*fp32', 3: '*i1', 4: 'i32', 5: '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': {4: 1}, 'configs': [AttrsDescriptor(divisible_by_16=(0, 1, 2, 3, 5), equal_to_1=(4,))]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_per_fused__to_copy_add_bitwise_not_eq_max_mean_min_mul_relu_rsub_sub_2', 'mutated_arg_names': ['in_out_ptr1'], 'no_x_dim': True, 'num_load': 29, '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__to_copy_add_bitwise_not_eq_max_mean_min_mul_relu_rsub_sub_2(in_out_ptr1, in_ptr0, in_ptr1, in_ptr2, xnumel, rnumel):
    xnumel = 1
    XBLOCK: tl.constexpr = 1
    rnumel = 256
    RBLOCK: tl.constexpr = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = tl.full([1], xoffset, tl.int32)
    xmask = tl.full([RBLOCK], True, tl.int1)
    rindex = tl.arange(0, RBLOCK)[:]
    roffset = 0
    rmask = tl.full([RBLOCK], True, tl.int1)
    r3 = rindex % 64
    r0 = rindex % 16
    r4 = rindex
    r2 = (rindex // 64)
    tmp0 = tl.load(in_ptr0 + (r3), None, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr0 + (r0), None, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr0 + (16 + r0), None, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr0 + (32 + r0), None, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr0 + (48 + r0), None, eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr1 + (r3), None, eviction_policy='evict_last')
    tmp9 = tl.load(in_ptr1 + (r4), None)
    tmp17 = tl.load(in_ptr0 + (64 + r3), None, eviction_policy='evict_last')
    tmp18 = tl.load(in_ptr0 + (64 + r0), None, eviction_policy='evict_last')
    tmp19 = tl.load(in_ptr0 + (80 + r0), None, eviction_policy='evict_last')
    tmp21 = tl.load(in_ptr0 + (96 + r0), None, eviction_policy='evict_last')
    tmp23 = tl.load(in_ptr0 + (112 + r0), None, eviction_policy='evict_last')
    tmp25 = tl.load(in_ptr1 + (64 + r3), None, eviction_policy='evict_last')
    tmp33 = tl.load(in_ptr0 + (128 + r3), None, eviction_policy='evict_last')
    tmp34 = tl.load(in_ptr0 + (128 + r0), None, eviction_policy='evict_last')
    tmp35 = tl.load(in_ptr0 + (144 + r0), None, eviction_policy='evict_last')
    tmp37 = tl.load(in_ptr0 + (160 + r0), None, eviction_policy='evict_last')
    tmp39 = tl.load(in_ptr0 + (176 + r0), None, eviction_policy='evict_last')
    tmp41 = tl.load(in_ptr1 + (128 + r3), None, eviction_policy='evict_last')
    tmp49 = tl.load(in_ptr0 + (192 + r3), None, eviction_policy='evict_last')
    tmp50 = tl.load(in_ptr0 + (192 + r0), None, eviction_policy='evict_last')
    tmp51 = tl.load(in_ptr0 + (208 + r0), None, eviction_policy='evict_last')
    tmp53 = tl.load(in_ptr0 + (224 + r0), None, eviction_policy='evict_last')
    tmp55 = tl.load(in_ptr0 + (240 + r0), None, eviction_policy='evict_last')
    tmp57 = tl.load(in_ptr1 + (192 + r3), None, eviction_policy='evict_last')
    tmp65 = tl.load(in_ptr2 + (r3 + (256*r2)), None).to(tl.int1)
    tmp68 = tl.load(in_ptr2 + (64 + r3 + (256*r2)), None).to(tl.int1)
    tmp72 = tl.load(in_ptr2 + (128 + r3 + (256*r2)), None).to(tl.int1)
    tmp76 = tl.load(in_ptr2 + (192 + r3 + (256*r2)), None).to(tl.int1)
    tmp3 = triton_helpers.maximum(tmp1, tmp2)
    tmp5 = triton_helpers.maximum(tmp3, tmp4)
    tmp7 = triton_helpers.maximum(tmp5, tmp6)
    tmp10 = tmp8 == tmp9
    tmp11 = tmp10 == 0
    tmp12 = tmp11.to(tl.float32)
    tmp13 = 1.0
    tmp14 = tmp13 - tmp12
    tmp15 = tmp7 * tmp14
    tmp16 = tmp0 + tmp15
    tmp20 = triton_helpers.maximum(tmp18, tmp19)
    tmp22 = triton_helpers.maximum(tmp20, tmp21)
    tmp24 = triton_helpers.maximum(tmp22, tmp23)
    tmp26 = tmp25 == tmp9
    tmp27 = tmp26 == 0
    tmp28 = tmp27.to(tl.float32)
    tmp29 = tmp13 - tmp28
    tmp30 = tmp24 * tmp29
    tmp31 = tmp17 + tmp30
    tmp32 = triton_helpers.minimum(tmp16, tmp31)
    tmp36 = triton_helpers.maximum(tmp34, tmp35)
    tmp38 = triton_helpers.maximum(tmp36, tmp37)
    tmp40 = triton_helpers.maximum(tmp38, tmp39)
    tmp42 = tmp41 == tmp9
    tmp43 = tmp42 == 0
    tmp44 = tmp43.to(tl.float32)
    tmp45 = tmp13 - tmp44
    tmp46 = tmp40 * tmp45
    tmp47 = tmp33 + tmp46
    tmp48 = triton_helpers.minimum(tmp32, tmp47)
    tmp52 = triton_helpers.maximum(tmp50, tmp51)
    tmp54 = triton_helpers.maximum(tmp52, tmp53)
    tmp56 = triton_helpers.maximum(tmp54, tmp55)
    tmp58 = tmp57 == tmp9
    tmp59 = tmp58 == 0
    tmp60 = tmp59.to(tl.float32)
    tmp61 = tmp13 - tmp60
    tmp62 = tmp56 * tmp61
    tmp63 = tmp49 + tmp62
    tmp64 = triton_helpers.minimum(tmp48, tmp63)
    tmp66 = tmp65.to(tl.float32)
    tmp67 = tmp66 * tmp0
    tmp69 = tmp68.to(tl.float32)
    tmp70 = tmp69 * tmp17
    tmp71 = triton_helpers.maximum(tmp67, tmp70)
    tmp73 = tmp72.to(tl.float32)
    tmp74 = tmp73 * tmp33
    tmp75 = triton_helpers.maximum(tmp71, tmp74)
    tmp77 = tmp76.to(tl.float32)
    tmp78 = tmp77 * tmp49
    tmp79 = triton_helpers.maximum(tmp75, tmp78)
    tmp80 = tmp79 - tmp64
    tmp81 = tmp80 + tmp13
    tmp82 = tl.full([1], 0, tl.int32)
    tmp83 = triton_helpers.maximum(tmp82, tmp81)
    tmp84 = tl.broadcast_to(tmp83, [RBLOCK])
    tmp86 = triton_helpers.promote_to_tensor(tl.sum(tmp84, 0))
    tmp87 = 256.0
    tmp88 = tmp86 / tmp87
    tl.debug_barrier()
    tl.store(in_out_ptr1 + (tl.full([1], 0, tl.int32)), tmp88, 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, 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)
        buf0 = empty_strided_cuda((4, 4, 4, 4, 4), (256, 64, 16, 4, 1), torch.bool)
        # Topologically Sorted Source Nodes: [mask], Original ATen: [aten.eq]
        stream0 = get_raw_stream(0)
        triton_poi_fused_eq_0.run(arg1_1, buf0, 1024, grid=grid(1024), stream=stream0)
        # Topologically Sorted Source Nodes: [fill_diagonal_], Original ATen: [aten.fill]
        triton_poi_fused_fill_1.run(buf0, 4, grid=grid(4), stream=stream0)
        # Topologically Sorted Source Nodes: [cdist], Original ATen: [aten._cdist_forward]
        buf2 = torch.ops.aten._cdist_forward.default(reinterpret_tensor(arg0_1, (1, 4, 4, 4, 4), (256, 64, 16, 4, 1), 0), reinterpret_tensor(arg0_1, (1, 4, 4, 4, 4), (256, 64, 16, 4, 1), 0), 2.0, None)
        del arg0_1
        buf3 = buf2
        del buf2
        buf6 = empty_strided_cuda((), (), torch.float32)
        buf7 = buf6; del buf6  # reuse
        # Topologically Sorted Source Nodes: [float_1, anchor_positive_dist, max_1, max_2, labels_equal, mask_1, float_2, sub, mul_1, anchor_negative_dist, min_1, sub_1, add_1, loss, mean], Original ATen: [aten._to_copy, aten.mul, aten.max, aten.eq, aten.bitwise_not, aten.rsub, aten.add, aten.min, aten.sub, aten.relu, aten.mean]
        triton_per_fused__to_copy_add_bitwise_not_eq_max_mean_min_mul_relu_rsub_sub_2.run(buf7, buf3, arg1_1, buf0, 1, 256, grid=grid(1), stream=stream0)
        del arg1_1
        del buf0
        del buf3
    return (buf7, )


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, 4), (64, 16, 4, 1), device='cuda:0', dtype=torch.float32)
    arg1_1 = rand_strided((4, 4, 4, 4), (64, 16, 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
import torch.nn as nn
import torch.nn.parallel
import torch.optim
from torch.nn import functional as F


class BatchHardTripletLoss(nn.Module):

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

    @staticmethod
    def get_anchor_positive_triplet_mask(target):
        mask = torch.eq(target.unsqueeze(0), target.unsqueeze(1))
        mask.fill_diagonal_(False)
        return mask

    @staticmethod
    def get_anchor_negative_triplet_mask(target):
        labels_equal = torch.eq(target.unsqueeze(0), target.unsqueeze(1))
        mask = ~labels_equal
        return mask

    def forward(self, x, target):
        pairwise_dist = torch.cdist(x.unsqueeze(0), x.unsqueeze(0)).squeeze(0)
        mask_anchor_positive = self.get_anchor_positive_triplet_mask(target)
        anchor_positive_dist = mask_anchor_positive.float() * pairwise_dist
        hardest_positive_dist = anchor_positive_dist.max(1, True)[0]
        mask_anchor_negative = self.get_anchor_negative_triplet_mask(target)
        max_anchor_negative_dist = pairwise_dist.max(1, True)[0]
        anchor_negative_dist = pairwise_dist + max_anchor_negative_dist * (
            1.0 - mask_anchor_negative.float())
        hardest_negative_dist = anchor_negative_dist.min(1, True)[0]
        loss = F.relu(hardest_positive_dist - hardest_negative_dist + self.
            margin)
        return loss.mean()


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


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

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
import torch.nn as nn
import torch.nn.parallel
import torch.optim
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_eq_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 1024
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x3 = xindex % 256
    x0 = xindex % 64
    x2 = xindex // 256
    x4 = xindex
    tmp0 = tl.load(in_ptr0 + x3, xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr0 + (x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp2 = tmp0 == tmp1
    tl.store(out_ptr0 + x4, tmp2, xmask)


@triton.jit
def triton_poi_fused_fill_1(out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 4
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = tl.full([1], False, tl.int1)
    tl.store(out_ptr0 + 341 * x0, tmp0, xmask)


@triton.jit
def triton_per_fused__to_copy_add_bitwise_not_eq_max_mean_min_mul_relu_rsub_sub_2(
    in_out_ptr1, in_ptr0, in_ptr1, in_ptr2, xnumel, rnumel):
    XBLOCK: tl.constexpr = 1
    RBLOCK: tl.constexpr = 256
    xoffset = tl.program_id(0) * XBLOCK
    tl.full([1], xoffset, tl.int32)
    tl.full([RBLOCK], True, tl.int1)
    rindex = tl.arange(0, RBLOCK)[:]
    tl.full([RBLOCK], True, tl.int1)
    r3 = rindex % 64
    r0 = rindex % 16
    r4 = rindex
    r2 = rindex // 64
    tmp0 = tl.load(in_ptr0 + r3, None, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr0 + r0, None, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr0 + (16 + r0), None, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr0 + (32 + r0), None, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr0 + (48 + r0), None, eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr1 + r3, None, eviction_policy='evict_last')
    tmp9 = tl.load(in_ptr1 + r4, None)
    tmp17 = tl.load(in_ptr0 + (64 + r3), None, eviction_policy='evict_last')
    tmp18 = tl.load(in_ptr0 + (64 + r0), None, eviction_policy='evict_last')
    tmp19 = tl.load(in_ptr0 + (80 + r0), None, eviction_policy='evict_last')
    tmp21 = tl.load(in_ptr0 + (96 + r0), None, eviction_policy='evict_last')
    tmp23 = tl.load(in_ptr0 + (112 + r0), None, eviction_policy='evict_last')
    tmp25 = tl.load(in_ptr1 + (64 + r3), None, eviction_policy='evict_last')
    tmp33 = tl.load(in_ptr0 + (128 + r3), None, eviction_policy='evict_last')
    tmp34 = tl.load(in_ptr0 + (128 + r0), None, eviction_policy='evict_last')
    tmp35 = tl.load(in_ptr0 + (144 + r0), None, eviction_policy='evict_last')
    tmp37 = tl.load(in_ptr0 + (160 + r0), None, eviction_policy='evict_last')
    tmp39 = tl.load(in_ptr0 + (176 + r0), None, eviction_policy='evict_last')
    tmp41 = tl.load(in_ptr1 + (128 + r3), None, eviction_policy='evict_last')
    tmp49 = tl.load(in_ptr0 + (192 + r3), None, eviction_policy='evict_last')
    tmp50 = tl.load(in_ptr0 + (192 + r0), None, eviction_policy='evict_last')
    tmp51 = tl.load(in_ptr0 + (208 + r0), None, eviction_policy='evict_last')
    tmp53 = tl.load(in_ptr0 + (224 + r0), None, eviction_policy='evict_last')
    tmp55 = tl.load(in_ptr0 + (240 + r0), None, eviction_policy='evict_last')
    tmp57 = tl.load(in_ptr1 + (192 + r3), None, eviction_policy='evict_last')
    tmp65 = tl.load(in_ptr2 + (r3 + 256 * r2), None).to(tl.int1)
    tmp68 = tl.load(in_ptr2 + (64 + r3 + 256 * r2), None).to(tl.int1)
    tmp72 = tl.load(in_ptr2 + (128 + r3 + 256 * r2), None).to(tl.int1)
    tmp76 = tl.load(in_ptr2 + (192 + r3 + 256 * r2), None).to(tl.int1)
    tmp3 = triton_helpers.maximum(tmp1, tmp2)
    tmp5 = triton_helpers.maximum(tmp3, tmp4)
    tmp7 = triton_helpers.maximum(tmp5, tmp6)
    tmp10 = tmp8 == tmp9
    tmp11 = tmp10 == 0
    tmp12 = tmp11.to(tl.float32)
    tmp13 = 1.0
    tmp14 = tmp13 - tmp12
    tmp15 = tmp7 * tmp14
    tmp16 = tmp0 + tmp15
    tmp20 = triton_helpers.maximum(tmp18, tmp19)
    tmp22 = triton_helpers.maximum(tmp20, tmp21)
    tmp24 = triton_helpers.maximum(tmp22, tmp23)
    tmp26 = tmp25 == tmp9
    tmp27 = tmp26 == 0
    tmp28 = tmp27.to(tl.float32)
    tmp29 = tmp13 - tmp28
    tmp30 = tmp24 * tmp29
    tmp31 = tmp17 + tmp30
    tmp32 = triton_helpers.minimum(tmp16, tmp31)
    tmp36 = triton_helpers.maximum(tmp34, tmp35)
    tmp38 = triton_helpers.maximum(tmp36, tmp37)
    tmp40 = triton_helpers.maximum(tmp38, tmp39)
    tmp42 = tmp41 == tmp9
    tmp43 = tmp42 == 0
    tmp44 = tmp43.to(tl.float32)
    tmp45 = tmp13 - tmp44
    tmp46 = tmp40 * tmp45
    tmp47 = tmp33 + tmp46
    tmp48 = triton_helpers.minimum(tmp32, tmp47)
    tmp52 = triton_helpers.maximum(tmp50, tmp51)
    tmp54 = triton_helpers.maximum(tmp52, tmp53)
    tmp56 = triton_helpers.maximum(tmp54, tmp55)
    tmp58 = tmp57 == tmp9
    tmp59 = tmp58 == 0
    tmp60 = tmp59.to(tl.float32)
    tmp61 = tmp13 - tmp60
    tmp62 = tmp56 * tmp61
    tmp63 = tmp49 + tmp62
    tmp64 = triton_helpers.minimum(tmp48, tmp63)
    tmp66 = tmp65.to(tl.float32)
    tmp67 = tmp66 * tmp0
    tmp69 = tmp68.to(tl.float32)
    tmp70 = tmp69 * tmp17
    tmp71 = triton_helpers.maximum(tmp67, tmp70)
    tmp73 = tmp72.to(tl.float32)
    tmp74 = tmp73 * tmp33
    tmp75 = triton_helpers.maximum(tmp71, tmp74)
    tmp77 = tmp76.to(tl.float32)
    tmp78 = tmp77 * tmp49
    tmp79 = triton_helpers.maximum(tmp75, tmp78)
    tmp80 = tmp79 - tmp64
    tmp81 = tmp80 + tmp13
    tmp82 = tl.full([1], 0, tl.int32)
    tmp83 = triton_helpers.maximum(tmp82, tmp81)
    tmp84 = tl.broadcast_to(tmp83, [RBLOCK])
    tmp86 = triton_helpers.promote_to_tensor(tl.sum(tmp84, 0))
    tmp87 = 256.0
    tmp88 = tmp86 / tmp87
    tl.debug_barrier()
    tl.store(in_out_ptr1 + tl.full([1], 0, tl.int32), tmp88, 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)
        buf0 = empty_strided_cuda((4, 4, 4, 4, 4), (256, 64, 16, 4, 1),
            torch.bool)
        get_raw_stream(0)
        triton_poi_fused_eq_0[grid(1024)](arg1_1, buf0, 1024, XBLOCK=128,
            num_warps=4, num_stages=1)
        triton_poi_fused_fill_1[grid(4)](buf0, 4, XBLOCK=4, num_warps=1,
            num_stages=1)
        buf2 = torch.ops.aten._cdist_forward.default(reinterpret_tensor(
            arg0_1, (1, 4, 4, 4, 4), (256, 64, 16, 4, 1), 0),
            reinterpret_tensor(arg0_1, (1, 4, 4, 4, 4), (256, 64, 16, 4, 1),
            0), 2.0, None)
        del arg0_1
        buf3 = buf2
        del buf2
        buf6 = empty_strided_cuda((), (), torch.float32)
        buf7 = buf6
        del buf6
        triton_per_fused__to_copy_add_bitwise_not_eq_max_mean_min_mul_relu_rsub_sub_2[
            grid(1)](buf7, buf3, arg1_1, buf0, 1, 256, num_warps=2,
            num_stages=1)
        del arg1_1
        del buf0
        del buf3
    return buf7,


class BatchHardTripletLossNew(nn.Module):

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

    @staticmethod
    def get_anchor_positive_triplet_mask(target):
        mask = torch.eq(target.unsqueeze(0), target.unsqueeze(1))
        mask.fill_diagonal_(False)
        return mask

    @staticmethod
    def get_anchor_negative_triplet_mask(target):
        labels_equal = torch.eq(target.unsqueeze(0), target.unsqueeze(1))
        mask = ~labels_equal
        return mask

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