Repository navigation
Conversation
Several op kernels read `context->input(0).dim_size(0)` (and, in `TfqInnerProductGrad`, `context->input(1).dim_size(0)`) to size the output tensor before the input parsers had checked the ranks of those tensors. For a scalar input, TensorFlow's `dim_size(0)` skips its bounds check and reads an uninitialized dimension slot, so the output was allocated with an arbitrary size before the op later failed. Add explicit `OP_REQUIRES` rank checks for `programs` (and for `symbol_names` in `TfqInnerProductGrad`) before any `dim_size()` call in the expectation, sampled-expectation, adjoint-gradient, inner-product, MPS 1D, and noisy expectation ops. Also correct the `symbol_values` error message in `TfqAdjointGradient`, which reported the rank of `pauli_sums` instead of `symbol_values`. Add regression tests that pass a scalar `programs` tensor to each affected op, a scalar `symbol_names` tensor to `TfqInnerProductGrad`, and check the corrected `symbol_values` error message.
mhucka
marked this pull request as ready for review
November 20, 2026 01:32
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.
This is a fix for b/510434434, "Missing Rank and Shape Validation in Multiple C++ Operations".
Several op kernels read
context->input(0).dim_size(0)(and, inTfqInnerProductGrad,context->input(1).dim_size(0)) and use that to set the size of the output tensor. However, they did this before the input parsers had checked the ranks of those tensors. For a scalar input, TensorFlow'sdim_size(0)skips its bounds check and reads an uninitialized dimension slot, so the output was allocated with an arbitrary size before the op later failed.This PR adds explicit
OP_REQUIRESrank checks forprograms(and forsymbol_namesinTfqInnerProductGrad) before anydim_size()call in the expectation, sampled-expectation, adjoint-gradient, inner-product, MPS 1D, and noisy expectation ops. Also correct thesymbol_valueserror message inTfqAdjointGradient, which reported the rank ofpauli_sumsinstead ofsymbol_values. Along with these changes, this also adds some regression tests.