Repository navigation
QAT: fake quantize embeddings under FSDP1 and torch.func.functional_call - #15385
Merged
Merged
Conversation
…functional_call) The re-entrancy guard in _call_with_fake_quantized_weight treated any non-Parameter weight as already fake quantized, so FSDP1 views and torch.func.functional_call weights silently skipped embedding fake quantization. Track the scope with a flag instead, and swap the instance-dict view FSDP1 installs (use_orig_params=True keeps the Parameter registered underneath it).
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Member
Author
|
@codex review |
|
Codex Review: Didn't find any major issues. More of your lovely PRs please. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
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
_call_with_fake_quantized_weight(added in #14702) decides whether it is already inside its own scope by checking the weight's type: a non-nn.Parameterweight was taken to mean "already fake quantized", and the original forward ran on it unchanged. Two legitimate callers also hand the module a plain tensor asmodule.weight:use_orig_params=FalseandTrue). Unsloth still supports FSDP1 (accelerate launchwith FSDP version 1, seefsdp2_launch.fsdp_version_from_env).torch.func.functional_call/torch.nn.utils.stateless.In both cases the QAT embedding silently trained and evaluated against the unquantized weight: zero quantizer calls per forward, finite gradients, no warning.
Fix
_unsloth_weight_fake_quantized) instead of the weight's type, so a nested call still quantizes exactly once and every other weight, Parameter or plain tensor, is fake quantized.weightlives in the instance dict, swap that entry. FSDP1 withuse_orig_params=Truekeeps theParameterregistered in_parameterswhile its view in__dict__shadows it, so swapping only the registered parameter left the unquantized view visible.torch.compile(fullgraph=True)paths are unchanged.Tests
New in
tests/test_qat_phone_deployment.py(CPU, gloo, single rank):test_functional_call_plain_tensor_weight_is_fake_quantizedtest_fsdp1_embedding_is_fake_quantized[False]test_fsdp1_embedding_is_fake_quantized[True]test_nested_fake_quantized_weight_call_quantizes_onceThe rest of the file, including the #14702 tests (
test_gemma3_embedding_forward_is_fake_quantized_once[upstream|direct_weight],test_accelerate_hooked_embedding_runs_fake_quantized_forward,test_fake_quantized_embedding_forward_compiles_fullgraph, the offload tests), passes on both: 54 passed on this branch, 51 passed and 3 failed on origin/main. The other QAT and FSDP test files (tests/utils/test_qat.py,tests/test_qat_*.py,tests/saving/test_qat_torchao_save.py,tests/test_fsdp2_native_qat_convert_generate_gloo.py, and others) give 335 passed, 17 skipped.Tested with torch 2.13, torchao 0.18, transformers 5.17. Not run: multi-rank FSDP1 on GPUs, or older torch versions.