[Bug] Scale the cross-device contrastive loss by world size in the kl_div branch - #1604
Draft
gss10282025 wants to merge 1 commit into
Draft
gss10282025 wants to merge 1 commit into
gss10282025 wants to merge 1 commit into
Conversation
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
In the cross-device
kl_divbranch, the contrastive loss is computed on gathered embeddings that only carry gradients for the local slice, so after DDP's averaging its gradient is1/world_sizeof the single-GPU gradient. The local KD loss is not affected, so distillation gets relatively more weight as GPUs are added.This PR multiplies the contrastive term by
self.world_sizebefore it is added to the KD loss, the same compensation Tevatron applies after its identical gather. The KD term, them3_kd_lossbranch and runs without distillation are unchanged. Theencoder_only/base_same_dataset.shanddecoder_only/icl_same_dataset.shexamples go through this branch.Issue links
Fixes #1603
Validation
At
fd1a2bdf,BiEncoderOnlyEmbedderModelwith cross-device negatives andkl_divKD, eight queries split over 1 or 2 GPUs with the same number of KD targets per GPU, one AdamW step with clipping, FP32: compared with a reference computed on the whole global batch, the 2-GPU gradient differs by0.406442(relative L2) before the change and1.50857e-6after; the clipped gradient, Adam moments and update match as well. KD alone already agrees on 1 and 2 GPUs. Reproduced on a second machine. I tested plain DDP, not DeepSpeed.Checks on this branch
fd1a2bdfand the file passes a Python syntax check.Prepared with AI assistance; opened as a draft for code review.