Skip to content

Round to nearest in NPUQuantizer, matching the CUDA quantizer - #8764

Draft
vineethsaivs wants to merge 1 commit into
deepspeedai:masterfrom
vineethsaivs:fix/npu-quantizer-round
Draft

vineethsaivs wants to merge 1 commit into
deepspeedai:masterfrom
vineethsaivs:fix/npu-quantizer-round

Conversation

@vineethsaivs

@vineethsaivs vineethsaivs commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

On Ascend NPUs, ZeRO++ quantized weights and gradients carry twice the quantization error of CUDA, biased toward zero, and int4 qgZ gradients come back about a third too small.

NPUQuantizer._quantize_groups truncates x * scale. The CUDA kernel rounds half to even (__float2int_rn, before the (int8_t) cast), and test_quantize.py checks it against torch.round.

Fix: trunc_() to round_(), plus the docstring. The new CPU test, at the path the docstring already cites, checks int8 quantize and int4 swizzle_quant against that reference. It fails on master and passes here.

Before to after, on CPU:

  • int8: L1 error 1.99x to 1.00x the reference, mean error -0.50 to 0.00 steps
  • int4 qgZ sum (2 nodes x 2 devices) as a fraction of the exact sum: 0.67 to 1.00
  • upstream test_float_quantize (int8 symmetric) on NPUQuantizer: 30 of 60 fail, then all pass

Verified on Ascend 910B4 by @VenusTZZ: qgZ int4 mean reconstruction error 0.1198 to 0.0506.

@VenusTZZ

VenusTZZ commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Thanks for the fix — this is correct. The truncation was my misreading in #8627: the CUDA kernel goes through conversion::to<int32_t> (__float2int_rn, round half to even), not a C-style cast, so this now matches both the kernel and the torch.round reference in test_quantize.py.

Verified on Ascend 910B4 (CANN 8.5.1, torch_npu 2.10.0.post7):

Check trunc (before) round (this PR)
qgZ int4 reconstruction, mean err (on device) 0.1198 0.0506
qgZ int4 reconstruction, max err (on device) 0.3511 0.2349
2-node × 2-device qgZ sum / exact sum (CPU sim) 0.845 0.995
This test (int4 + int8) — 2/2 pass
Per-group numpy-oracle round-trip suite (on device) — all pass

One small suggestion on the test: test_quantize.py already covers this contract across both hardware backends — QuantizerBuilder().load() resolves to NPUQuantizer on NPU (the path your 60/60 run exercised) and to the CUDA kernel on CUDA, both validated against the same torch.round reference.

It might be cleaner to reuse it directly instead of adding a separate NPU-specific file, so the reference logic stays consolidated in one place. The int4 swizzle_quant coverage is great new coverage and worth keeping — and since it’s pure torch as well, it can fit into the existing test file without needing an NPU-specific variant.

@vineethsaivs

Copy link
Copy Markdown
Contributor Author

Thanks for checking it on the 910B4. I added your numbers to the description.

VenusTZZ added a commit to VenusTZZ/DeepSpeed that referenced this pull request Oct 30, 2026
The CUDA kernel quantizes through __float2int_rn (round half to even),
not a C-style int32 cast; truncating instead biases int4 qgZ gradients
systematically low and roughly doubles the quantization error of both
the hpZ int8 and qgZ int4 paths.

Rename the docstring's "truncation" rationale accordingly and add the
CPU reference test for the 4-bit and 8-bit paths, pinned against
torch.round like test_quantize.py does for the CUDA kernel.

Adapted from deepspeedai#8764 (author: vineethsaivs).

Signed-off-by: Zizhao Tang <[email protected]>
VenusTZZ added a commit to VenusTZZ/DeepSpeed that referenced this pull request Nov 2, 2026
The CUDA kernel quantizes through __float2int_rn (round half to even),
not a C-style int32 cast; truncating instead biases int4 qgZ gradients
systematically low and roughly doubles the quantization error of both
the hpZ int8 and qgZ int4 paths.

Rename the docstring's "truncation" rationale accordingly and add the
CPU reference test for the 4-bit and 8-bit paths, pinned against
torch.round like test_quantize.py does for the CUDA kernel.

Adapted from deepspeedai#8764 (author: vineethsaivs).

Signed-off-by: Zizhao Tang <[email protected]>
@VenusTZZ

VenusTZZ commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

Hi @vineethsaivs — I see this PR is still in draft. We're preparing a broader LoRA + ZeRO-3 zero_quantized_nontrainable_weights fix PR, and it also carries the same NPUQuantizer trunc→round change. Following the suggestion in this thread, I moved the regression test into tests/unit/ops/quantizer/test_quantize.py via QuantizerBuilder and made it device-agnostic, with a small scale-ULP tolerance. Happy to share that commit.

Would you mind if we include this fix in our PR, with credit to you and this PR? Or would you prefer to update this PR yourself? If you'd rather keep it here, we'll drop it from ours and simply reference #8764. Thanks!

@vineethsaivs

Copy link
Copy Markdown
Contributor Author

Thanks for checking. I'd keep the fix here so it lands on its own. A device-agnostic test in test_quantize.py is better than my NPU-only file, so please share that commit and I'll fold it in.

NPUQuantizer truncated x * scale toward zero, but the CUDA kernel it
stands in for rounds half to even (__float2int_rn), which is what the
CUDA quantizer test's torch.round reference checks. Truncation doubled
the ZeRO++ quantization error on Ascend and biased it toward zero. Use
round_() and add a CPU test against the torch.round reference.

Signed-off-by: Vineeth Sai <[email protected]>
@vineethsaivs
vineethsaivs force-pushed the fix/npu-quantizer-round branch from fec115b to eb0b894 Compare November 11, 2026 23:27
@auto-pr-labeler-with-jev auto-pr-labeler-with-jev Bot added the skip-gpu-test Requires positive evidence that every changed behavior can be validated without a GPU. label Nov 14, 2026

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

skip-gpu-test Requires positive evidence that every changed behavior can be validated without a GPU.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants