Repository navigation
PR #128149: Validate constant num_bits in XLA QuantizeAndDequantizeV3 - #129137
Draft
copybara-service[bot] wants to merge 1 commit into
Draft
copybara-service[bot] wants to merge 1 commit into
copybara-service[bot] wants to merge 1 commit into
Conversation
Imported from GitHub PR #128149 Fixes #127914 ### Problem With `jit_compile=True`, `tf.quantization.quantize_and_dequantize_v3` accepts `num_bits` of 0 or a negative value and returns a result. Eager and plain graph execution raise `InvalidArgumentError: num_bits is out of range` for the same input. ### Cause In `tf2xla/kernels/quantize_and_dequantize_op.cc`, V3 is registered on the base `QuantizeAndDequantizeOp`, which takes `num_bits` from input 3 and feeds it straight into the `2^num_bits` bound computation. The CPU kernel checks `0 < num_bits < (signed_input ? 62 : 63)` first. The XLA lowering has no such check. ### Fix When input 3 folds to a constant, apply the same range check as the CPU kernel and fail with `InvalidArgument`. When `num_bits` is truly dynamic, behavior is unchanged, and the op registration is not modified. ### Test Added `UnaryOpsTest.testQuantizeAndDequantizeV3InvalidNumBits` in `compiler/tests/unary_ops_test.py`. It runs the op inside `def_function.function(jit_compile=True)` with constant `num_bits` of 0 and -1 and expects `InvalidArgumentError`. ``` bazel test //tensorflow/compiler/tests:unary_ops_test_cpu \ --test_sharding_strategy=disabled \ --test_filter='UnaryOpsTest.testQuantizeAndDequantize' --nocache_test_results ``` Without the kernel change, the new test fails with `InvalidArgumentError not raised`. With it, the new test and the existing `testQuantizeAndDequantize` both pass. Copybara import of the project: -- 8d11fb0 by Aditya Gaurav <[email protected]>: Validate constant num_bits in XLA QuantizeAndDequantizeV3 The tf2xla lowering of QuantizeAndDequantizeV3 reads num_bits from input 3 and uses it directly to compute the quantization bounds. The range check that the CPU kernel applies is missing, so with jit_compile=True a num_bits of 0 or a negative value returns a result instead of raising InvalidArgumentError. When input 3 folds to a constant, apply the same range check as the CPU kernel. Dynamic num_bits is left unchanged. Fixes #127914 -- ead4b3e by Aditya Gaurav <[email protected]>: Use self.evaluate in QuantizeAndDequantizeV3 num_bits test -- c0d2cc0 by Aditya Gaurav <[email protected]>: Test QuantizeAndDequantizeV3 num_bits upper bounds with an explicit session -- 765d6e4 by Aditya Gaurav <[email protected]>: Test valid QuantizeAndDequantizeV3 num_bits bounds and use device_scope -- 30c975e by Aditya Gaurav <[email protected]>: Rename QuantizeAndDequantizeV3 num_bits test to NumBitsBoundaries Merging this change closes #128149 FUTURE_COPYBARA_INTEGRATE_REVIEW=#128149 from adi-IL:bot/tensorflow-127914 30c975e PiperOrigin-RevId: 999856372
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR #128149: Validate constant num_bits in XLA QuantizeAndDequantizeV3
Imported from GitHub PR #128149
Fixes #127914
Problem
With
jit_compile=True,tf.quantization.quantize_and_dequantize_v3acceptsnum_bitsof 0 or a negative value and returns a result. Eager and plain graph execution raiseInvalidArgumentError: num_bits is out of rangefor the same input.Cause
In
tf2xla/kernels/quantize_and_dequantize_op.cc, V3 is registered on the baseQuantizeAndDequantizeOp, which takesnum_bitsfrom input 3 and feeds it straight into the2^num_bitsbound computation. The CPU kernel checks0 < num_bits < (signed_input ? 62 : 63)first. The XLA lowering has no such check.Fix
When input 3 folds to a constant, apply the same range check as the CPU kernel and fail with
InvalidArgument. Whennum_bitsis truly dynamic, behavior is unchanged, and the op registration is not modified.Test
Added
UnaryOpsTest.testQuantizeAndDequantizeV3InvalidNumBitsincompiler/tests/unary_ops_test.py. It runs the op insidedef_function.function(jit_compile=True)with constantnum_bitsof 0 and -1 and expectsInvalidArgumentError.Without the kernel change, the new test fails with
InvalidArgumentError not raised. With it, the new test and the existingtestQuantizeAndDequantizeboth pass.Copybara import of the project:
--
8d11fb0 by Aditya Gaurav [email protected]:
Validate constant num_bits in XLA QuantizeAndDequantizeV3
The tf2xla lowering of QuantizeAndDequantizeV3 reads num_bits from
input 3 and uses it directly to compute the quantization bounds. The
range check that the CPU kernel applies is missing, so with
jit_compile=True a num_bits of 0 or a negative value returns a result
instead of raising InvalidArgumentError.
When input 3 folds to a constant, apply the same range check as the
CPU kernel. Dynamic num_bits is left unchanged.
Fixes #127914
--
ead4b3e by Aditya Gaurav [email protected]:
Use self.evaluate in QuantizeAndDequantizeV3 num_bits test
--
c0d2cc0 by Aditya Gaurav [email protected]:
Test QuantizeAndDequantizeV3 num_bits upper bounds with an explicit session
--
765d6e4 by Aditya Gaurav [email protected]:
Test valid QuantizeAndDequantizeV3 num_bits bounds and use device_scope
--
30c975e by Aditya Gaurav [email protected]:
Rename QuantizeAndDequantizeV3 num_bits test to NumBitsBoundaries
Merging this change closes #128149
FUTURE_COPYBARA_INTEGRATE_REVIEW=#128149 from adi-IL:bot/tensorflow-127914 30c975e