[Bug] With negatives_cross_device and kd_loss_type=kl_div, the distillation weight grows with the number of GPUs

Author: gss10282025Created Sep 26, 2026Updated Sep 26, 2026

Summary

In _compute_cross_device_neg_loss, the kl_div branch adds two losses (AbsModeling.py#L219-L229):

  • the KD loss on this rank's queries (student_scores sliced to the local rank), and
  • the contrastive loss over the gathered global batch (cross_scores).

_dist_gather_tensor keeps gradients only for the local slice. After DDP averages across ranks, the contrastive gradient is 1/world_size times the single-GPU gradient, while the local KD loss gets the normal average and is not scaled. The gradient is

g_contrast / world_size + g_kd

instead of g_contrast + g_kd, so adding GPUs increases the weight of distillation relative to the contrastive loss, even with the same global batch, negatives and teacher scores.

For comparison, Tevatron uses the same _dist_gather_tensor and multiplies the contrastive loss by world_size to make up for this (encoder.py#L76-L77, "counter average weight reduction").

This applies with --negatives_cross_device, --kd_loss_type kl_div and --knowledge_distillation True. The fine-tuning examples encoder_only/base_same_dataset.sh and decoder_only/icl_same_dataset.sh enable all three on 2 GPUs, and the example retrieval data includes pos_scores/neg_scores, so they go through this branch. (encoder_only/base.sh turns distillation off.)

Environment

Commit fd1a2bdf, PyTorch 2.8.0+cu128, CUDA 12.8, FP32 with TF32 and autocast off, 2× RTX 5090.

Steps to reproduce

BiEncoderOnlyEmbedderModel with negatives_cross_device=True, kd_loss_type="kl_div", normalized embeddings and temperature 0.05. Eight fixed queries with their passage groups and teacher scores, split evenly over 1 or 2 GPUs so that each GPU has the same number of KD targets. Plain DDP, dropout off, one AdamW step (lr 2e-5, betas 0.9/0.999, eps 1e-8, weight decay 0.01) with gradient clipping at 1.0.

Splitting the gradient into its two parts shows that on 2 GPUs the contrastive part is half of the reference, while the KD part matches it. Compared with a reference gradient of the same loss computed on the whole global batch, the 2-GPU gradient differs by 0.406442 (relative L2). With the fix below it differs by 1.50857e-6, and the clipped gradient, Adam moments and parameter update match as well. With the KD loss alone, 1 and 2 GPUs already agree. Reproduced on a second machine.

Expected behavior

For the same global batch, the balance between the contrastive and KD losses should not depend on the number of GPUs.

Proposed fix

In the kl_div branch, scale only the contrastive loss by the world size before adding it to the local KD loss:

python
loss += self.world_size * self.compute_loss(cross_scores, cross_targets)

Scaling the total loss instead would not change the ratio. The m3_kd_loss branch computes KD over the gathered batch; I haven't analyzed or tested it.

I tested the model and loss with plain DDP and one optimizer step. The example scripts use DeepSpeed, which I didn't test. This assumes each GPU has the same number of KD targets.