Skip to content

Fix OOB reads on empty batch in TfqInnerProductGrad - #1145

Open
mhucka wants to merge 2 commits into
tensorflow:masterfrom
mhucka:fix-ip-grad-empty-batch
Open

mhucka wants to merge 2 commits into
tensorflow:masterfrom
mhucka:fix-ip-grad-empty-batch

Conversation

@mhucka

@mhucka mhucka commented Nov 20, 2026

Copy link
Copy Markdown
Member

This is a fix for b/510434594, "Out-of-Bounds Vector Access on Empty Batch Inputs".

TfqInnerProductGradOp indexed maps[0] and downstream_grads[0] while validating input shapes, and ComputeSmall indexed other_fused_circuits[0]. All three vectors are empty when programs has batch size 0, so the op was reading out of bounds (a crash in practice, and an assertion failure under _GLIBCXX_ASSERTIONS).

This PR makes a number of changes to tfq_inner_product_grad.cc to do more checks. In addition, this expands the test cases to cover more situations.

TfqInnerProductGradOp indexed maps[0] and downstream_grads[0] while
validating input shapes, and ComputeSmall indexed
other_fused_circuits[0]. All three vectors are empty when `programs`
has batch size 0, so the op read out of bounds (a crash in practice,
and an assertion failure under _GLIBCXX_ASSERTIONS).

Skip the symbol-map size check when there are no maps, compare the
downstream_grads width using the tensor shape so that mismatches are
still rejected for empty batches, and return the zeroed output early
when there are no programs or no other_programs.
@mhucka
mhucka marked this pull request as ready for review November 20, 2026 05:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant