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]


Measured latency_ms: 0.049926