executorch icon indicating copy to clipboard operation
executorch copied to clipboard

Qualcomm: a quantized bias can flip sign on an x86-64 host

Open INHA-SW opened this issue 2 weeks ago • 0 comments

🐛 Describe the bug

The observer-based int32 bias specs in backends/qualcomm/quantizer/qconfig.py use quant_max=2**31 - 1, and quantize_per_tensor clamps in float32, where that bound rounds to 2**31. On an x86-64 host the cast to int32 then turns that into -2**31, so a positive bias at the bound changes sign. The cast runs on the host in convert_pt2e, so the device inherits the flipped value. On an arm64 host the cast saturates and the value is right.

The symmetric MinMax observer puts the largest-magnitude element of a bias at the bound, so this happens when that element is positive and larger than about 3.3 with the 16-bit configs or about 840 with the 8-bit configs (eps * 2**31). LayerNorm biases, and Conv and Linear biases under per-tensor configs, can reach it.

Repro, no model or data (x86-64 host):

import operator, torch
from torchao.quantization.pt2e.quantize_pt2e import prepare_pt2e, convert_pt2e
from executorch.backends.qualcomm.quantizer.quantizer import QnnQuantizer, QuantDtype
from executorch.backends.qualcomm.serialization.qc_schema import QcomChipset

m = torch.nn.LayerNorm(8).eval()
with torch.no_grad():
    m.bias.copy_(torch.tensor([0.5, -1.0, 8.25, 0.1, -0.3, 2.0, -4.0, 0.0]))
x = torch.randn(4, 8)
q = QnnQuantizer(soc_model=QcomChipset.SM8650)  # V75: 16a8w gives LayerNorm a 16-bit weight
q.set_default_quant_config(QuantDtype.use_16a8w)
p = prepare_pt2e(torch.export.export(m, (x,)).module(), q)
for _ in range(4):
    p(torch.randn(4, 8))
c = convert_pt2e(p, fold_quantize=False)
ln = next(n for n in c.graph.nodes if n.target == torch.ops.aten.layer_norm.default)
dq = ln.args[3]; qn = dq.args[0]
b = operator.attrgetter(qn.args[0].target)(c)
qb = torch.ops.quantized_decomposed.quantize_per_tensor.default(b, *qn.args[1:])
print(qb)
print(torch.ops.quantized_decomposed.dequantize_per_tensor.default(qb, *dq.args[1:]))

On main the third element is stored as -2**31 and comes back negative:

tensor([  130150528,  -260301056, -2147483648,    26030106,   -78090320,
          520602112, -1041204224,           0], dtype=torch.int32)
tensor([ 0.5000, -1.0000, -8.2500,  0.1000, -0.3000,  2.0000, -4.0000,  0.0000])

With the bounds in qconfig.py set to ±(2**31 - 2**8), which float32 represents exactly:

tensor([  130150512,  -260301024,  2147483392,    26030102,   -78090312,
          520602048, -1041204096,           0], dtype=torch.int32)
tensor([ 0.5000, -1.0000,  8.2500,  0.1000, -0.3000,  2.0000, -4.0000,  0.0000])

I hit this on RF-DETR Nano (16a8w, QCS6490), where 17 of the 45 DINOv2 LayerNorm biases flip. Person AP on 100 COCO val2017 images (fp32 0.580):

Host fake-quant Device
bound [-2**31, 2**31 - 1] 0.392 0.392
bound ±(2**31 - 2**8) 0.578 0.576

Only the bound differs between the rows.

Versions

ExecuTorch from source at main ec5e3cf (2026-11-18), torch 2.14.0+cpu, torchao 0.18.0+cpu, QAIRT 2.51.0.260929, Linux x86-64; the device numbers were measured on main 12dac4a, QCS6490.

collect_env output
Collecting environment information...
PyTorch version: 2.14.0+cpu
Is debug build: False
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N/A

OS: Ubuntu 24.04.5 LTS (x86_64)
GCC version: (Ubuntu 13.3.0-6ubuntu2~24.04.1) 13.3.0
Clang version: 18.1.3 (1ubuntu1)
CMake version: version 3.28.3
Libc version: glibc-2.39

Python version: 3.12.3 (main, Aug 31 2026, 10:18:26) [GCC 13.3.0] (64-bit runtime)
Python platform: Linux-6.17.0-35-generic-x86_64-with-glibc2.39
Is CUDA available: False
CUDA runtime version: No CUDA
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA
Is XPU available: False
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: False
Caching allocator config: N/A

CPU:
Architecture:                            x86_64
CPU op-mode(s):                          32-bit, 64-bit
Address sizes:                           52 bits physical, 57 bits virtual
Byte Order:                              Little Endian
CPU(s):                                  48
On-line CPU(s) list:                     0-47
Vendor ID:                               AuthenticAMD
Model name:                              AMD Ryzen Threadripper PRO 9965WX 24-Cores
CPU family:                              26
Model:                                   8
Thread(s) per core:                      2
Core(s) per socket:                      24
Socket(s):                               1
Stepping:                                1
Frequency boost:                         enabled
CPU(s) scaling MHz:                      41%
CPU max MHz:                             5489.7642
CPU min MHz:                             1223.6230
BogoMIPS:                                8386.95
Flags:                                   fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good amd_lbr_v2 nopl xtopology nonstop_tsc cpuid extd_apicid aperfmperf rapl pni pclmulqdq monitor ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs skinit wdt tce topoext perfctr_core perfctr_nb bpext perfctr_llc mwaitx cpuid_fault cpb cat_l3 cdp_l3 hw_pstate ssbd mba perfmon_v2 ibrs ibpb stibp ibrs_enhanced vmmcall fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid cqm rdt_a avx512f avx512dq adx smap avx512ifma clflushopt clwb avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local user_shstk avx_vnni avx512_bf16 clzero irperf xsaveerptr rdpru wbnoinvd amd_ppin cppc arat npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold avic v_vmsave_vmload vgif x2avic v_spec_ctrl vnmi avx512vbmi umip pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg avx512_vpopcntdq la57 rdpid bus_lock_detect movdiri movdir64b overflow_recov succor smca fsrm avx512_vp2intersect flush_l1d debug_swap amd_lbr_pmc_freeze
Virtualization:                          AMD-V
L1d cache:                               1.1 MiB (24 instances)
L1i cache:                               768 KiB (24 instances)
L2 cache:                                24 MiB (24 instances)
L3 cache:                                128 MiB (4 instances)
NUMA node(s):                            1
NUMA node0 CPU(s):                       0-47
Vulnerability Gather data sampling:      Not affected
Vulnerability Ghostwrite:                Not affected
Vulnerability Indirect target selection: Not affected
Vulnerability Itlb multihit:             Not affected
Vulnerability L1tf:                      Not affected
Vulnerability Mds:                       Not affected
Vulnerability Meltdown:                  Not affected
Vulnerability Mmio stale data:           Not affected
Vulnerability Old microcode:             Not affected
Vulnerability Reg file data sampling:    Not affected
Vulnerability Retbleed:                  Not affected
Vulnerability Spec rstack overflow:      Mitigation; Reduced Speculation
Vulnerability Spec store bypass:         Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:                Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:                Mitigation; Enhanced / Automatic IBRS; IBPB conditional; STIBP always-on; PBRSB-eIBRS Not affected; BHI Not affected
Vulnerability Srbds:                     Not affected
Vulnerability Tsa:                       Not affected
Vulnerability Tsx async abort:           Not affected
Vulnerability Vmscape:                   Mitigation; IBPB before exit to userspace

Versions of relevant libraries:
[pip3] numpy==1.26.4
[pip3] torch==2.14.0+cpu
[pip3] torchao==0.18.0+cpu
[pip3] torchvision==0.29.0+cpu
[conda] Could not collect

Prepared with the help of Claude Code; I reviewed it before posting.

cc @cccclai @winskuo-quic @shewu-quic @haowhsu-quic @DannyYuyang-quic @qti-horodnic @cbilgin @abhinaykukkadapu @psiddh

INHA-SW avatar Nov 18 '26 07:11 INHA-SW