def _ComputeMetrics(self,
                      logits,
                      target_labels,
                      target_weights,
                      target_probs=None):
    """Compute loss and misc metrics.

    Args:
      logits: Tensor of shape [batch, time, num_classes].
      target_labels: Tensor of shape [batch, time].
      target_weights: Tensor of shape [batch, time].
      target_probs: Tensor of shape [batch, time, num_classes].
    Returns:
      A (metrics, per_sequence_loss) pair.
    """
    p = self.params
    target_weights_sum = tf.reduce_sum(target_weights)
    # add 0.000001 to avoid divide-by-zero.
    target_weights_sum_eps = target_weights_sum + 0.000001
    # apply Qin patch (see: https://github.com/yaq007/lingvo/commit/414e035b2c60372de732c9d67db14d1003be6dd6)
    # line 1,2
    target_weights_batch = tf.reduce_sum(target_weights, 1)
    target_weights_batch_eps = target_weights_batch + 0.000001
    correct_preds = tf.cast(
        tf.equal(tf.argmax(logits, 2, output_type=tf.int32), target_labels),
        py_utils.FPropDtype(p))
    correct_next_preds = tf.reduce_sum(correct_preds * target_weights)
    accuracy = tf.identity(
        correct_next_preds / target_weights_sum_eps,
        name='fraction_of_correct_next_step_preds')
    # Pad zeros so that we can stack them.
    per_example_loss = py_utils.SoftmaxCrossEntropyFocalLoss(
        logits=logits,
        label_ids=target_labels,
        label_probs=target_probs,
        alpha=p.focal_loss_alpha,
        gamma=p.focal_loss_gamma)

    per_sequence_loss = tf.reduce_sum(per_example_loss * target_weights, 1)
    # apply Qin patch (see: https://github.com/yaq007/lingvo/commit/414e035b2c60372de732c9d67db14d1003be6dd6)
    # line 3
    per_example_batch = per_sequence_loss/target_weights_batch_eps
    per_token_avg_loss = (
        tf.reduce_sum(per_sequence_loss) / target_weights_sum_eps)
    if p.token_normalized_per_seq_loss:
      per_seq_length = tf.reduce_sum(target_weights, 1)
      # +0.001 to avoid possible divide by 0.
      per_sequence_loss /= (per_seq_length + 0.001)
    if p.per_token_avg_loss:
      loss = per_token_avg_loss
      loss_weight = target_weights_sum
    else:  # per-sequence average loss
      loss = tf.reduce_mean(per_sequence_loss)
      loss_weight = tf.shape(per_sequence_loss)[0]
    # apply Qin patch (see: https://github.com/yaq007/lingvo/commit/414e035b2c60372de732c9d67db14d1003be6dd6)
    # line 4
    tf.add_to_collection('per_loss', per_example_batch)
    metrics = {
        'loss': (loss, loss_weight),
        # add log_pplx for compatibility with the mt/decoder.py
        'log_pplx': (per_token_avg_loss, target_weights_sum),
        'token_normed_prob': (tf.exp(-per_token_avg_loss), target_weights_sum),
    }
    metrics['fraction_of_correct_next_step_preds'] = (accuracy,
                                                      target_weights_sum)
    return metrics, per_sequence_loss
Compute loss and misc metrics.

    Args:
      logits: Tensor of shape [batch, time, num_classes].
      target_labels: Tensor of shape [batch, time].
      target_weights: Tensor of shape [batch, time].
      target_probs: Tensor of shape [batch, time, num_classes].
    Returns:
      A (metrics, per_sequence_loss) pair.
    
contrib/lingvo-patched-decoder.py