Skip to content

QAT: fake quantize embeddings under FSDP1 and torch.func.functional_call - #15385

Merged
danielhanchen merged 3 commits into
mainfrom
dh/s7-rc-qatembview
Oct 27, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
dh/s7-rc-qatembview

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

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.Parameter weight 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 as module.weight:

  • FSDP1, which installs tensor views of its flat parameter during forward and backward (both use_orig_params=False and True). Unsloth still supports FSDP1 (accelerate launch with FSDP version 1, see fsdp2_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

  • Track the scope with a per-module flag (_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.
  • When weight lives in the instance dict, swap that entry. FSDP1 with use_orig_params=True keeps the Parameter registered in _parameters while its view in __dict__ shadows it, so swapping only the registered parameter left the unquantized view visible.
  • Ordinary, Accelerate-hooked, offloaded and torch.compile(fullgraph=True) paths are unchanged.

Tests

New in tests/test_qat_phone_deployment.py (CPU, gloo, single rank):

Test origin/main this branch
test_functional_call_plain_tensor_weight_is_fake_quantized FAIL (0 quantizer calls) PASS
test_fsdp1_embedding_is_fake_quantized[False] FAIL (0 quantizer calls) PASS
test_fsdp1_embedding_is_fake_quantized[True] FAIL (0 quantizer calls) PASS
test_nested_fake_quantized_weight_call_quantizes_once PASS PASS

The 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.

…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).
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-26T14:36:28.228369Z 661f4b3 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: 661f4b31ed

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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".

@danielhanchen
danielhanchen merged commit 4bd1155 into main Oct 27, 2026
@danielhanchen
danielhanchen deleted the dh/s7-rc-qatembview branch October 27, 2026 05:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant