feat: replace soon-deprecated torch quantization functions - #211
Conversation
Signed-off-by: Andrea Fasoli <andrea.fasoli@ibm.com>
Signed-off-by: Andrea Fasoli <andrea.fasoli@ibm.com>
Here's the feedback by claude. Looks ready for merge once CI is done. Please also double check Minor Findings 2.Review SummaryReplaces 13 call sites of Correctness — verified by hand-tracing the arithmetic:
Two minor findings:
Neither issue blocks merge — overall this is a solid, consistent conversion across all 13 sites. |
Description of the change
Replace deprecated
torch.quantize_per_tensor/torch.quantize_per_channelcalls with equivalent plain arithmetic to unblock the torch 2.13 upgrade (#209).PyTorch 2.13 emits a UserWarning from these ops (pytorch/pytorch#184982), which CI promotes to an error via
-W error::UserWarningin tox.ini, failingtest_save_aiu.py::test_large_outlier_bertandtest_lsq.py::test_lsq_single_sided.Converted 13 call sites across
quant/quantizers.py,quant_refactor/(torch_quantizer.py,per_tensor_ste.py,per_channel_ste.py,pactplussym_rc.py,quantizers_new.py),modules/bmm.py,modules/linear.py, andcustom_ext_kernels/utils.py.Also added shared tables to
quant_refactor/base_quant.py:_INT_REPR_DTYPES: maps each quantized dtype to the storage dtypeint_repr()used to return (qint8→int8, quint8→uint8, qint32→int32), so the arithmetic replacements cast to the right integer type instead of leaving a float._DTYPE_RANGES: gives each dtype's storage bounds so the replacements can reproduce the ops' saturation before the caller's narrower quant_min/quant_max clamp (which matters for int4-in-int8), and supplies the explicit bounds needed to clamp qint32 in float64, since 2**31-1 isn't representable in fp32 and wraps on cast.The replacements mirror the deprecated kernels bit-for-bit — reciprocal multiply in fp32 (not division, which is marginally more accurate but drifts 1 LSB on exact rounding ties), round half-to-even, and saturation to the storage dtype.
Note: one site (
modules/linear.py:439) is unchanged, it still callstorch.quantize_per_tensor. It's the only site that needs the quantized tensor object itself (passed toset_weight_biasfor FBGEMM), notint_repr(). Plain arithmetic can't produce that, and converting it means rewriting the FBGEMM interop. It's in an untested from_reference classmethod and doesn't trigger under-Werror today, but it will break when torch actually removes these ops.Related issues or PRs
Closes #210 , enables #209
Was the PR tested