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.utils.data
import torch
from torch import nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda


@triton.jit
def triton_per_fused__to_copy_add_clamp_div_eq_index_put_lift_fresh_mul_ne_pow_rsub_sum_0(
    in_out_ptr0, in_ptr0, in_ptr1, out_ptr0, 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)
    r0 = rindex
    tmp0 = tl.load(in_ptr0 + r0, None)
    tmp8 = tl.load(in_ptr1 + r0, None)
    tmp1 = 255.0
    tmp2 = tmp0 == tmp1
    tmp3 = 1.0
    tmp4 = tl.where(tmp2, tmp3, tmp0)
    tmp5 = tmp4 + tmp3
    tmp6 = 0.5
    tmp7 = tmp5 * tmp6
    tmp9 = tmp4 != tmp1
    tmp10 = tmp9.to(tl.float32)
    tmp11 = tmp8 * tmp10
    tmp12 = tmp11 * tmp11
    tmp13 = tmp7 * tmp12
    tmp14 = tl.broadcast_to(tmp13, [RBLOCK])
    tmp16 = triton_helpers.promote_to_tensor(tl.sum(tmp14, 0))
    tmp17 = tmp3 - tmp4
    tmp18 = tmp17 * tmp6
    tmp19 = tmp18 * tmp10
    tmp20 = 2.0
    tmp21 = tmp20 - tmp11
    tmp22 = 0.0
    tmp23 = triton_helpers.maximum(tmp21, tmp22)
    tmp24 = tmp23 * tmp23
    tmp25 = tmp19 * tmp24
    tmp26 = tl.broadcast_to(tmp25, [RBLOCK])
    tmp28 = triton_helpers.promote_to_tensor(tl.sum(tmp26, 0))
    tmp29 = tmp4 == tmp3
    tmp30 = tmp29.to(tl.float32)
    tmp31 = tl.broadcast_to(tmp30, [RBLOCK])
    tmp33 = triton_helpers.promote_to_tensor(tl.sum(tmp31, 0))
    tmp34 = -1.0
    tmp35 = tmp4 == tmp34
    tmp36 = tmp35.to(tl.float32)
    tmp37 = tl.broadcast_to(tmp36, [RBLOCK])
    tmp39 = triton_helpers.promote_to_tensor(tl.sum(tmp37, 0))
    tmp40 = 0.0001
    tmp41 = tmp33 + tmp40
    tmp42 = tmp16 / tmp41
    tmp43 = tmp39 + tmp40
    tmp44 = tmp28 / tmp43
    tmp45 = tmp42 + tmp44
    tl.store(out_ptr0 + tl.broadcast_to(r0, [RBLOCK]), tmp4, None)
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp45, 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)
        buf2 = empty_strided_cuda((), (), torch.float32)
        buf6 = buf2
        del buf2
        get_raw_stream(0)
        triton_per_fused__to_copy_add_clamp_div_eq_index_put_lift_fresh_mul_ne_pow_rsub_sum_0[
            grid(1)](buf6, arg0_1, arg1_1, arg0_1, 1, 256, num_warps=2,
            num_stages=1)
        del arg0_1
        del arg1_1
    return buf6,


class BCLNew(nn.Module):
    """
    batch-balanced contrastive loss
    no-change，1
    change，-1
    """

    def __init__(self, margin=2.0):
        super(BCLNew, self).__init__()
        self.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]


Measured latency_ms: 0.015971