import tensorflow as tf

    # with tf.control_dependencies([p]):
    #     avg_loss = 1. * avg_loss
    # print(avg_loss)
    # exit()
    return avg_loss


def compute_contra_loss(pred1, pred2, tgt1, tgt2, hard_ratio=1.0):
    geq = tf.cast((tgt1 - tgt2) > 0, tf.bool)
    tgt_larg = tf.where(geq, tgt1, tgt2)
    tgt_small = tf.where(geq, tgt2, tgt1)
    pred_larg = tf.where(geq, pred1, pred2)
    pred_small = tf.where(geq, pred2, pred1)
    loss = tf.maximum(0., (tgt_larg - tgt_small) - (pred_larg - pred_small))
    if hard_ratio < 1.0:
        hard_num = tf.cast(tools.shape(pred1)[0] * hard_ratio, tf.int32)
        loss = tf.reshape(loss, [-1])
        hard_loss, _ = tf.math.top_k(loss, k=hard_num)
        return hard_loss
    return loss


