Skip to content

Add opt-in NVFP4 weight-only loading for compressed-tensors - #51178

Open
eryk-roch wants to merge 6 commits into
huggingface:mainfrom
eryk-roch:nvfp4-weight-only
Open

eryk-roch wants to merge 6 commits into
huggingface:mainfrom
eryk-roch:nvfp4-weight-only

Conversation

@eryk-roch

@eryk-roch eryk-roch commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

🤗 Transformers CI

What does this PR do?

Adds CompressedTensorsConfig(weight_only=True) to load compressed-tensors NVFP4 checkpoints, such as RedHatAI/Qwen3-14B-NVFP4`, into torchao weights without an external adapter or requantization.

This is explicitly W4A16: activation quantization is disabled in favor of BF16, so outputs can differ from the original W4A4 deployment. That is why this uses a separate option rather than use_optimized_inference. Torchao dequantizes weights per forward call; this is not a fused FP4 kernel.

  • Preserves packed weights and block scales, passing the reciprocal of CT's global scale as torchao's multiplier.
  • Reuses existing loading hooks, including sharding and base-model prefixes; no core loader changes. Existing loading routes remain the default.
  • Requires compressed-tensors>=0.15.0, torchao>=0.16.0, explicit BF16 and ordinary dense Linear modules on one device. Unsupported combinations are rejected and documented.
  • Also makes is_qat_trainable a boolean property, as the trainer expects.

Related: #49779 and #49823 target the fine-grained machinery. This PR instead adds an opt-in torchao W4A16 path to the existing compressed-tensors quantizer.

Validation

6 tests passed on CPU and Intel XPU.
Coverage includes weight/output parity, generation, sharding, prefix handling, optional activation scales and malformed tensors. The CPU prefix/optional-scale case fails on unmodified main with missing activation scales because main ignores the new option and selects the existing CT route.

CUDA: 6/6 passed with each of torchao 0.16.0 and 0.19.0+git2b2fa0da on NVIDIA RTX PRO 4500 Blackwell (Windows, torch 2.15 dev, CUDA 13.0). Torchao 0.16 skipped incompatible C++ extensions; this W4A16 path passed
without them.

PYTHONPATH=src python -m pytest tests/quantization/compressed_tensors_integration/test_nvfp4.py -v

Changed-file lint/format and config/quantizer type checks passed. Extended checks still report two pre-existing FP8 type errors; the full style runner also hit unrelated comment findings without Git history on the DUT. Full repository CI remains unverified.

Who can review?

@SunMarc @IlyasMoutawwakil

Import compressed-tensors NVFP4 bytes and scales into torchao with
explicit BF16 activations, preserving packed weights without
requantization. Adapt reciprocal global scales, allow absent input
scales, and use scoped converters for base-model prefix handling.

Limit the path to ordinary dense Linear modules on one device, account
for packed storage during memory planning, and document unsupported
operations. Keep the existing loading routes opt-out by default.

Expose is_qat_trainable as a boolean property as expected by the
trainer, with a regression assertion for the existing compressed route.
Include compact CPU/accelerator loading and validation tests.
@eryk-roch
eryk-roch marked this pull request as draft November 24, 2026 12:21
Document the NVFP4-only scope and reuse on devices without native FP4
GEMMs. Remove the validation-specific revision from the usage example.

Import NVFP4Tensor directly for conversion and post-load validation,
while retaining the early dependency check and existing loader hooks.
Remove redundant deserializer checks covered by module preparation
and the single-device loader. Share NVFP4 scheme validation while
preserving distinct weight and activation requirements.

Retain checkpoint integrity and scope guards, shorten the documentation,
and add compact regression coverage for the shared validation.
Replace duplicate input validation with an assertion documenting the
config invariant established by post_init in the quantizer constructor.
@eryk-roch
eryk-roch marked this pull request as ready for review December 1, 2026 13:54
@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

[For maintainers] Suggested jobs to run (before merge)

run-slow: compressed_tensors_integration

@Rocketknight1

Copy link
Copy Markdown
Member

cc @SunMarc for quantization

This branch has not been deployed

No deployments
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.

2 participants