Repository navigation
fix: preserve CLIP hidden states after first skipped forward - #9806
Open
wunianze666-netizen wants to merge 1 commit into
Open
wunianze666-netizen wants to merge 1 commit into
wunianze666-netizen wants to merge 1 commit into
Conversation
Initialize capture hooks on the complete CLIP encoder before temporary layer removal. Cover fresh, warmed, repeated and exceptional contexts against same-weight reference encoders.
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
A fresh CLIP encoder first used with positive CLIP skip can keep returning incomplete hidden states after its skipped layers are restored. Subsequent SDXL conditioning can therefore select the wrong penultimate layer even though the final encoder output is unchanged.
Initialize Transformers' existing capture hooks on the complete, actual CLIP capture owner before temporarily removing layers. For
CLIPTextModelWithProjection, this is the innertext_model, not the projection wrapper. The existing helper is idempotent; skip-zero handling and the context manager's layer restoration are unchanged.Related Issues / Discussions
Closes #9722.
The dependency/layout update in #9723 is already in this branch's baseline; this fixes the separate first-forward capture lifetime problem reported by @lstein.
QA Instructions
Repository-relative command for the application-context checks:
Windows, Python 3.12.14, Torch 2.13.0+cpu, Transformers 5.10.4:
aa46003581e2c482e03e62116f68ef3c3a6f0233, replaying the exact unmodifiedapply_clip_skipmethod against the 26 new cases gives 16 failed / 10 passed. This is an isolated owning-method replay with real Torch/Transformers, not a full-application baseline run.b47066e8c6candidate was also validated with the new file alone in the application context: 26 passed. Earlier isolated compatibility checks on Transformers 5.5.4 gave 16 failed / 10 passed before and 26 passed after; these do not claim full-application backward compatibility.git diff --checkpass. Only the production patcher and the new regression file are submitted.The new tests use small, real, random CPU float32 encoders and independent weight-matched references. They compare every hidden state, final and pooled/projected outputs, layer identity/order, fresh/warmed/repeated skip contexts, and recovery from both an error inside the forward and an error after it. No model download is required.
Not run: accelerator/compiled inference, full SDXL generation, concurrent generation, complete repository test/type/build checks. No speedup or full-platform claim is made.
Review
Initializing the outer projection wrapper after an earlier forward can double-record hidden states. The warmed-model numerical controls exposed this during development; the final implementation initializes only the inner capture owner. Exceptions retain the existing
finallyrestoration behavior.The alternative of warming the model first requires an extra inference. This patch instead reuses the pinned dependency's capture installation helper without changing output selection or temporarily replacing the forward implementation.
Implementation, diagnosis and regression design were AI-assisted. The reported commands and results were executed; internal checks are not maintainer approval.
Compatibility / Rollout
No API, serialized model or database migration. The helper is an internal Transformers API, validated for the currently pinned
>=5.10.4,<5.11range and separately checked at 5.5.4. Future dependency upgrades require revalidation.The fix prevents corruption on a fresh encoder's first skipped forward; it does not repair an encoder already poisoned in a running process. Such instances must be reloaded/restarted.
Checklist
What's Newcopy (if doing a release after this PR) (not a release PR)Need help on this PR? Tag
@codesmith-botwith what you need. Autofix is disabled.