Fix loop variable shadowing in validation that corrupts sample iteration#811
Open
Mr-Neutr0n wants to merge 1 commit intozai-org:mainfrom
Open
Fix loop variable shadowing in validation that corrupts sample iteration#811Mr-Neutr0n wants to merge 1 commit intozai-org:mainfrom
Mr-Neutr0n wants to merge 1 commit intozai-org:mainfrom
Conversation
In the validate() method, the inner loop `for i, ... in enumerate(validation_artifacts)` shadows the outer loop variable `for i in range(num_validation_samples)`. After the inner loop completes, `i` holds the last enumerate index instead of the outer sample index. This causes subsequent validation samples to load incorrect prompts, images, and videos, and breaks DeepSpeed multi-GPU process distribution logic that depends on `i`. Renamed the inner variable to `artifact_idx` to prevent shadowing.
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
Trainer.validate()where the innerfor i, ... in enumerate(validation_artifacts)loop shadows the outerfor i in range(num_validation_samples)loop variable.iretains the last enumerate index rather than the outer sample index, which can cause incorrect validation behavior with multiple samples.itoartifact_idxto eliminate the shadowing.Details
In
finetune/trainer.py, thevalidate()method has a loop over validation samples:In Python,
forloop variables are scoped to the enclosing function, not the loop body. The innerfor ioverwrites the outeri. While the outerfor i in range()reassignsiat the top of each iteration, this shadowing is a correctness hazard that can produce subtle bugs if the code is modified, and violates the principle of least surprise.Test plan
git diff