Repository navigation
[Bug] Scale the gathered contrastive loss by world size before adding the RTD loss - #30
Open
gss10282023 wants to merge 1 commit into
Open
gss10282023 wants to merge 1 commit into
gss10282023 wants to merge 1 commit into
Conversation
gss10282023
marked this pull request as ready for review
October 1, 2026 05:54
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
With DDP, the gathered contrastive loss only backpropagates through local embeddings, so after DDP's averaging its gradient is
1/world_sizeof the single-GPU gradient, while the RTD loss is not affected. On 2 GPUs,lambda_weight=0.005acts like0.01.This PR multiplies the contrastive loss by
dist.get_world_size()during distributed training, beforelambda_weight * RTDis added. Only the contrastive term is multiplied, not the combined loss. Single-GPU training is unchanged.The PR also reads five training metrics from
model.modulewhenCLTrainerreceives a DDP wrapper. The original reads raiseAttributeErrorbefore the first distributed update, so this small prerequisite lets the corrected loss path run through the native Trainer. On a single GPU the reads still usemodel.Issue links
Fixes #29
Validation
At
33b29a38, one AdamW step with a global batch of 64 on 1 vs. 2 GPUs,lambda_weight=0.005, the same number of RTD tokens per GPU: before the change the full gradient differs from the 1-GPU run by about0.49999; after the change the difference is1.563e-7in FP32 and1.989e-16in FP64, and the update and AdamW state match as well. Reproduced on a second machine.The recorded experiments used the equivalent five trainer-read adaptation now included in this PR. They also sized
electra_headto the small test model and ran without--batchnorm; those two experimental controls are not part of this PR. This exact combined PR diff has not had a fresh full Trainer run.Checks on this branch
.modulein a small check.Prepared with AI assistance; opened as a draft for code review.