Skip to content

Add scale and zero-point dimension validation to quantization primitives#4539

Open
GiGiKoneti wants to merge 2 commits into
pytorch:mainfrom
GiGiKoneti:scale-zp-validation
Open

Add scale and zero-point dimension validation to quantization primitives#4539
GiGiKoneti wants to merge 2 commits into
pytorch:mainfrom
GiGiKoneti:scale-zp-validation

Conversation

@GiGiKoneti

Copy link
Copy Markdown
Contributor

Description

This PR implements upfront shape and size validation for the scale and zero_point tensors in low-level affine quantization and dequantization primitives under torchao/quantization/quant_primitives.py.

It resolves several # TODO: validate scale/zero_point dimensions are compatible with block_size comments in the codebase.

By checking these dimensions before entering .view() calls, we prevent generic PyTorch RuntimeError: shape [...] is invalid for input of size ... and instead raise clear, actionable ValueError and AssertionError messages when dimensions or number of elements are incompatible.

Closes #4538

Test Plan

Added unit tests in test/quantization/test_quant_primitives.py (test_validate_scale_zero_point) and updated test_raises to expect the new ValueError instead of the old RuntimeError.

To run the tests:

python3 -m pytest test/quantization/test_quant_primitives.py -k "test_raises or test_validate_scale_zero_point"

Output:

============================= test session starts ==============================
platform darwin -- Python 3.11.9, pytest-9.0.3, pluggy-1.6.0
rootdir: /Users/mynimbus/ao
configfile: pyproject.toml
plugins: zarr-3.1.6, requests-mock-1.12.1, anyio-4.13.0
collected 27 items / 25 deselected / 2 selected

test/quantization/test_quant_primitives.py ..                            [100%]

======================= 2 passed, 25 deselected in 2.05s =======================
@pytorch-bot

pytorch-bot Bot commented Jun 27, 2026

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/ao/4539

Note: Links to docs will display an error until the docs builds have been completed.

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jun 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

1 participant