fix: enter autocast context manager in T5/text conditioner#252
Open
rodriguescarson wants to merge 1 commit into
Open
fix: enter autocast context manager in T5/text conditioner#252rodriguescarson wants to merge 1 commit into
rodriguescarson wants to merge 1 commit into
Conversation
conditioners.py used `with autocast(...) and set_grad_enabled(...):`. Python evaluates the `and` expression first; the autocast object is truthy, so `and` returns only its right operand (set_grad_enabled). The `with` therefore enters just set_grad_enabled and never calls autocast.__enter__ — fp16 autocast is silently inactive for the model forward pass. Use a comma to enter both context managers, as intended. Fixes Stability-AI#238
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.
Fixes #238
Problem
In
stable_audio_tools/models/conditioners.py:Python evaluates the
andexpression before thewithbinds anything.torch.amp.autocast(...)returns a truthy object, soA and Bshort-circuits to its right operand,torch.set_grad_enabled(...). Thewithstatement therefore enters onlyset_grad_enabled—autocast.__enter__()is never called, so fp16 autocast is silently inactive for this conditioner's forward pass (it runs in the ambient dtype instead of float16).Fix
Separate the two context managers with a comma so both are entered, which is the intended behaviour:
One-token change, verifiable by inspection — this is the classic
with A and B:context-manager pitfall. It's the only occurrence of the pattern in the repo.🤖 Generated with Claude Code