Skip to content

Preserve Python scalar semantics in Vulkan and XNNPACK quantizers - #22804

Open
JakeStevens wants to merge 1 commit into
pytorch:mainfrom
JakeStevens:fix/quantizer-scalar-promotion
Open

JakeStevens wants to merge 1 commit into
pytorch:mainfrom
JakeStevens:fix/quantizer-scalar-promotion

Conversation

@JakeStevens

@JakeStevens JakeStevens commented Sep 14, 2026

Copy link
Copy Markdown
Contributor

Summary

Preparing a model with the Vulkan or XNNPACK quantizer can change its results when a Python scalar is materialized using the operation's output dtype. For example, torch.tensor([1e-4], dtype=torch.float16) * 100000.0 produces approximately 10 in PyTorch, but casting the scalar to FP16 first makes the result infinite. Smaller scalars can underflow, and other dtypes have their own promotion and rounding rules.

Keep the original Python scalar operands for add and multiply operations whose output is not FP32. FP32 operations retain scalar lifting for quantization. Integer index arithmetic covered by #22372 and #22065 continues to work.

Test plan

All 10 focused tests and 208 scalar-promotion subtests pass across dtypes, add/multiply, scalar/vector inputs, and configured/unconfigured quantizers:

OMP_NUM_THREADS=1 MKL_NUM_THREADS=1 PYTHONPATH=src python -m pytest -o addopts= -q \
  backends/vulkan/test/quantizer/test_vulkan_quantizer.py \
  backends/xnnpack/test/quantizer/test_xnnpack_quantizer.py \
  -k 'scalar or int64 or add_mul_long or mul_float32_max or add_and_inplace_add or mul_and_inplace_mul'

Existing FP32 scalar quantization, tensor add/multiply, large-scalar handling, and integer-index regressions are included. lintrunner --revision main and git diff --check pass.

Authored with OpenAI Codex.

cc @SS-JIA @manuelcandales @digantdesai @cbilgin

Converting Python scalars to the output tensor dtype can round or overflow
them before arithmetic. For example, multiplying an FP16 tensor containing
1e-4 by 100000.0 should produce approximately 10, but materializing the
scalar as FP16 produces infinity instead.

Limit scalar lifting to FP32 add and multiply operations, preserving the
original scalar operands for other dtypes. Cover scalar promotion and
rounding across low-precision floats, doubles, integers, bools, and complex
values, with and without quantization configuration.

Authored with OpenAI Codex.
@JakeStevens JakeStevens added the release notes: quantization Changes to quantization label Sep 14, 2026
@pytorch-bot

pytorch-bot Bot commented Sep 14, 2026

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/22804

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

❌ 1 New Failure, 1 Pending

As of commit 0a26d9e with merge base 89fcfe7 (image):

NEW FAILURE - The following job has failed:

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

@pytorch-bot pytorch-bot Bot added the module: vulkan Issues related to the Vulkan delegate and code under backends/vulkan/ label Sep 14, 2026
@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 Sep 14, 2026
@JakeStevens
JakeStevens marked this pull request as ready for review September 14, 2026 16:14
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. module: vulkan Issues related to the Vulkan delegate and code under backends/vulkan/ release notes: quantization Changes to quantization

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant