Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions op_builder/npu/quantizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@ class NPUQuantizer:
Wire format, per group:
scale = 2**num_bits / (2 * max(|x|)) (1.0 when max == 0)
params = 1 / scale (fp32, the value that is sent)
q = trunc(x * scale), clamped to [-2**(b-1), 2**(b-1)-1]
q = round(x * scale), clamped to [-2**(b-1), 2**(b-1)-1]

Truncation is toward zero: the reference implementation casts to an
integer type, so a round-to-nearest "simplification" would silently
change every quantized value. tests/unit/ops/quantizer/test_npu_quantizer.py
pins this against an independent per-group reference.
Rounding is half to even, like the reference kernel's __float2int_rn.
tests/unit/ops/quantizer/test_npu_quantizer.py pins this against the
torch.round reference that the CUDA quantizer test uses.

Known deviation: fp32/fp64 division on this accelerator is approximate
(1 ulp off on ~5% of values), so params can differ from a correctly
Expand All @@ -32,10 +31,9 @@ class NPUQuantizer:
five orders below the quantization error (~1e-2), and the wire format is
transient (checkpoints store unquantized weights).

Native operators: torch_npu's npu_quantize family rounds half-to-even
and exposes no truncation mode, so no native op can honor the wire
format today. If CANN later adds one, _quantize_groups is the single
swap point.
Native operators: torch_npu's npu_quantize family also rounds
half-to-even, so _quantize_groups is the single swap point if a native
op is adopted later.

The dequantize_*_experimental methods are unrelated to that wire format:
they serve the inference weight-quantization path, which is asymmetric
Expand Down Expand Up @@ -80,11 +78,11 @@ def _quantize_groups(xg, num_bits):
# The stored/sent value is the reciprocal of the scale; dequantize
# multiplies it back, so the round trip is q * (1/scale) == q * scale.
stored = scale.reciprocal().view(-1)
# Truncate toward zero, then clamp into the representable range.
# Round half to even, then clamp into the representable range.
# Chained in-place form: xg is always a fresh float() copy here.
q_min = -(1 << (num_bits - 1))
q_max = (1 << (num_bits - 1)) - 1
xg.mul_(scale.view(-1, 1)).trunc_().clamp_(q_min, q_max)
xg.mul_(scale.view(-1, 1)).round_().clamp_(q_min, q_max)
return xg, stored

@staticmethod
Expand Down
37 changes: 37 additions & 0 deletions tests/unit/ops/quantizer/test_npu_quantizer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# SPDX-License-Identifier: Apache-2.0
# DeepSpeed Team

import pytest
import torch

from deepspeed.ops.op_builder.npu.quantizer import NPUQuantizer


def reference_quantize(x, groups, num_bits):
# The reference from run_float_quantize in test_quantize.py: torch.round is half to even,
# like the CUDA kernel's __float2int_rn.
x = x.reshape(groups, -1).float()
absmax = x.abs().amax(dim=-1, keepdim=True)
scale = torch.where(absmax == 0, torch.ones_like(absmax), 2**num_bits / (2 * absmax))
q = torch.round(x * scale).clamp(-2**(num_bits - 1), 2**(num_bits - 1) - 1)
return q, 1.0 / scale


@pytest.mark.parametrize("num_bits", [4, 8])
def test_npu_quantizer_rounds_to_nearest(num_bits):
# NPUQuantizer is pure torch, so this runs on CPU. Truncating instead of rounding
# changes about half the codes and doubles the quantization error.
torch.manual_seed(0)
groups = 16
x = torch.randn(groups * 256, dtype=torch.float16)
q, scales = reference_quantize(x, groups, num_bits)

if num_bits == 8:
data, params = NPUQuantizer.quantize(x, groups, num_bits, NPUQuantizer.Symmetric)
else:
# the qgZ int4 path; one node with one device keeps the groups in order
data, params = NPUQuantizer.swizzle_quant(x, groups, num_bits, NPUQuantizer.Symmetric, 1, 1, 1)
out = NPUQuantizer.dequantize(data, params, groups, num_bits, NPUQuantizer.Symmetric)

assert torch.equal(params, scales)
assert torch.equal(out.view(groups, -1), (q * scales).half())
Loading