import tensorflow as tf

    geq = tf.cast(tgt_dif > 0, tf.bool)
    tgt_posi_dif = tf.where(geq, tgt_dif, -tgt_dif)
    pred_posi_dif = tf.where(geq, pred_dif, -pred_dif)
    loss = tf.maximum(0., tgt_posi_dif - pred_posi_dif)
    cstr_pct = tf.math.count_nonzero(loss, dtype=tf.float32) / tf.cast(tf.reduce_prod(tf.shape(loss)), tf.float32)

    unorm_w = tf.exp((tgt_flat1 + tgt_flat2)/temp)
    loss = unorm_w * loss / (tf.reduce_sum(unorm_w))

    a = tf.print(tf.reduce_sum(unorm_w))
    with tf.control_dependencies([a]):
        final_loss = tf.reduce_sum(loss)
    return final_loss, cstr_pct

def contra_traj_lossV8(pred, tgt, horizon=12):
    horizon_pred, horizon_tgt = horizon_sumV1(pred, horizon), horizon_sumV1(tgt, horizon)
    # horizon_pred, horizon_tgt = horizon_sumV2(pred, tgt, horizon)
    horizon_pred1, horizon_pred2 = tf.split(horizon_pred, 2, axis=0)
    horizon_tgt1, horizon_tgt2 = tf.split(horizon_tgt, 2, axis=0)
    pred_flat1, pred_flat2 = tf.reshape(horizon_pred1, [-1, 1]), tf.reshape(horizon_pred2, [1, -1])
