Repository navigation
Round to nearest in NPUQuantizer, matching the CUDA quantizer - #8764
vineethsaivs wants to merge 1 commit into
Conversation
|
Thanks for the fix — this is correct. The truncation was my misreading in #8627: the CUDA kernel goes through Verified on Ascend 910B4 (CANN 8.5.1, torch_npu 2.10.0.post7):
One small suggestion on the test: 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 |
|
Thanks for checking it on the 910B4. I added your numbers to the description. |
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]>
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]>
|
Hi @vineethsaivs — I see this PR is still in draft. We're preparing a broader LoRA + ZeRO-3 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! |
|
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]>
fec115b to
eb0b894
Compare
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_groupstruncatesx * scale. The CUDA kernel rounds half to even (__float2int_rn, before the(int8_t)cast), andtest_quantize.pychecks it againsttorch.round.Fix:
trunc_()toround_(), plus the docstring. The new CPU test, at the path the docstring already cites, checks int8quantizeand int4swizzle_quantagainst that reference. It fails on master and passes here.Before to after, on CPU:
test_float_quantize(int8 symmetric) onNPUQuantizer: 30 of 60 fail, then all passVerified on Ascend 910B4 by @VenusTZZ: qgZ int4 mean reconstruction error 0.1198 to 0.0506.