Skip to content

PR #128149: Validate constant num_bits in XLA QuantizeAndDequantizeV3 - #129137

Draft
copybara-service[bot] wants to merge 1 commit into
masterfrom
exported_pr_999856372
Draft

copybara-service[bot] wants to merge 1 commit into
masterfrom
exported_pr_999856372

Conversation

@copybara-service

Copy link
Copy Markdown

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_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

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant