Repository navigation
Clamp quantized_add_out results before integer conversion - #25794
professor-moody wants to merge 1 commit into
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/25794
Note: Links to docs will display an error until the docs builds have been completed. ✅ You can merge normally! (1 Unrelated Failure)As of commit 926f6be with merge base 48afcd7 ( BROKEN TRUNK - The following job failed but were present on the merge base:👉 Rebase onto the `viable/strict` branch to avoid these failures
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
@claude review effort=max |
|
Claude finished @JakeStevens's task in 31m 55s —— View job Request Changes (no blockers): widening the a/b zero points breaks the documented Dq → add → Q equivalence with Detailed reviewPR Review: #25794SummaryThis PR changes
Nothing blocks merging. The problems are:
I reviewed both changed files by reading the code and working the arithmetic by hand. I did not build or run the tests in this job. API Design
Code Quality
Testing
Specific Comments
RecommendationRequest Changes. Nothing blocks merging, but two things should be settled first:
The test fixes are small.
|
Summary
quantize_valinkernels/quantized/cpu/op_add.cppconvertszero_point + std::nearbyint(inv_scale * value)directly toint64_tbefore clamping to the quantized range and divides bystatic_cast<float>(scale)without guarding zero scales. Becausequantized_add_outdoes not constrain scales, a zero, float-subnormal, NaN, or very smallout_scale(or large input scales) makesinv_scaleinfinite or NaN or pushes the scaled sum past theint64_trange, causing float-to-integer conversion undefined behavior, float divide-by-zero undefined behavior, and silent output corruption in release builds when reached through.pteexecution ofquantized_decomposed::add.out. UBSan reports "op_add.cpp:33:33: runtime error: inf is outside the range of representable values of type 'long'" and, under float-divide-by-zero checks, "op_add.cpp:32:26: runtime error: division by zero".Three related boundary issues sit on the same path in
kernels/quantized/cpu/op_add.cpp. First,quantized_add_outchecksquant_minandquant_maxagainst [0, 255] for every integer dtype, which allowsquant_maxup to 255 forScalarType::Char(int8_t, max 127) and causes float-to-int8_tconversion overflow or wraparound. Second,quantized_add_outtruncatesa_zero_point_l,b_zero_point_l, andout_zero_point_lfromint64_ttoint32_tbefore callingadd_tensors, flipping the sign of out-of-range zero points before clamping. Third,dequantize_valcomputesvalue - zero_pointin signedint64_tforScalarType::Long, triggering signed integer overflow on extreme inputs.#22811 defined non-finite quantization semantics in kernels/quantized/cpu/op_quantize.cpp: NaN maps to the clamped zero point, and values are clamped in
doublebefore integer conversion. This applies those semantics toquantize_valinkernels/quantized/cpu/op_add.cppwhile avoiding division by zero on zero scales (preserving the sign of zero viastd::copysign), converting the clampeddoubleviaint64_t, cappingquant_maxto 127 forScalarType::Char, preservingint64_tzero points intoadd_tensors, and performing the subtraction indequantize_valindouble. Finite in-range results are unchanged.Test plan
Added four tests to
kernels/quantized/test/op_add_test.cpp:OutScaleOverflowSaturatesBeforeIntegerConversion,OutZeroPointClampedOnNaN,CharDtypeSaturatesWithinSignedRangeAndRejectsOutOfRangeQuantMax, andLongDtypeDequantizeExtremeValuesDoNotOverflow.cmake -S . -B build-san \ -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ \ -DEXECUTORCH_BUILD_TESTS=ON \ -DEXECUTORCH_BUILD_KERNELS_OPTIMIZED=ON \ -DEXECUTORCH_BUILD_KERNELS_QUANTIZED=ON \ -DEXECUTORCH_USE_SANITIZER=ON \ -DEXECUTORCH_ENABLE_LOGGING=OFF cmake --build build-san -j --target quantized_kernels_test ./build-san/kernels/test/quantized_kernels_testBuilt and ran quantized_kernels_test unfiltered on Linux x86_64 with Clang 21.1.8 across three configurations: EXECUTORCH_USE_CPP_CODE_COVERAGE, EXECUTORCH_USE_SANITIZER (ASan+UBSan), and ASan+UBSan with float-divide-by-zero checks. All 98 tests pass in all three configurations with zero sanitizer diagnostics, and coverage confirms every touched line and branch in
quantize_val,dequantize_val, andquantized_add_outexecutes. Without thekernels/quantized/cpu/op_add.cppchanges, all four new tests fail and UBSan reports the overflow and divide-by-zero errors above.Also verified end-to-end
.pteexecution of quantized_decomposed::add.out through both executor_runner and the C++ Module API on Linux x86_64 in ASan+UBSan with float-divide-by-zero checks and stock Release (-O2 -DNDEBUG), using InternalConsistency program verification in the sanitizer Module build (where program verification is enabled) and default Minimal verification in executor_runner and stock Release (where program verification is off by default and Module falls back to Minimal). Across a benign finite control and ten non-finite/boundary.pteprograms covering zero, negative-zero, float-underflow, subnormal, NaN, infinite, out-of-int32 zero-point, signed-Char, and extreme-Long inputs, the benign control produces identical outputs before and after the fix, unpatched builds reproduce the UBSan failures (and silent output corruption orint8_twraparound in Release), and patched builds produce the expected saturated outputs and rejectScalarType::Charquant_maxabove 127 with zero sanitizer diagnostics.AI assistance was used in drafting and validating this change; the patch and tests were reviewed and verified by the author.
cc @nil-is-all