Skip to content

Handle negative axes in the pfor converter for Roll - #129841

Merged
copybara-service[bot] merged 1 commit into
tensorflow:masterfrom
bodapatisaikrishna:fix/pfor-roll-negative-axis
Nov 3, 2026
Merged

copybara-service[bot] merged 1 commit into
tensorflow:masterfrom
bodapatisaikrishna:fix/pfor-roll-negative-axis

Conversation

@bodapatisaikrishna

Copy link
Copy Markdown
Contributor

Fixes #129840

Problem

The pfor converter for Roll adds 1 to axis to account for the loop dimension at the front. Negative axes count from the end and shouldn't change, but -1 + 1 becomes 0, the loop dimension. So tf.roll(x, 1, axis=-1) under tf.vectorized_map moves values between batch elements:

loop: [[3.0, 1.0, 2.0], [6.0, 4.0, 5.0]]
vmap: [[4.0, 5.0, 6.0], [1.0, 2.0, 3.0]]

The gradient of Roll is a Roll along the same axis, and GradientTape.jacobian and batch_jacobian use pfor by default, so they also silently return wrong results for any computation using tf.roll with a negative axis:

jacobian use_pfor=False: [[0.0, 0.0, 1.0], [10.0, 0.0, 0.0], [0.0, 100.0, 0.0]]
jacobian use_pfor=True:  [[0.0, 10.0, 0.0], [0.0, 0.0, 100.0], [1.0, 0.0, 0.0]]

Change

  • Fixed shift: only add 1 to non-negative axes, using the same axis += math_ops.cast(axis >= 0, axis.dtype) idiom as the neighboring converters.
  • Loop-variant shift: this path uses axis to compute axis_segment_ids relative to the unstacked dimensions, so normalize negative axes first by adding num_unstacked_axes.

Tests added to NNTest in control_flow_ops_test.py:

  • test_roll_negative_axis: scalar and list axes, including mixed signs and a repeated axis, compared with a while loop by _test_loop_fn.
  • test_loop_variant_roll_shift_negative_axis: negative axes with a loop-variant shift.
  • test_roll_negative_axis_jacobian: the Jacobian against its known value.

Testing

I don't have a Bazel build. I ran these with Python on the tf-nightly 2.23.0.dev20261028 macOS arm64 wheel, whose pfor.py is identical to master's, with this PR's files copied in:

  • Full control_flow_ops_test.py: 211 tests, OK (71 skipped).
  • With the old pfor.py, all three new tests fail in both graph and eager mode.
  • pylint --rcfile=tensorflow/tools/ci_build/pylintrc on the changed files: no messages on the changed lines.

I did not run gradients_test.py or test on GPU. I added a RELEASE.md entry because outputs change for these inputs.

The Roll converter adds 1 to `axis` to account for the loop dimension
pfor adds at the front. Negative axes count from the end and must stay
unchanged, but `-1 + 1` becomes 0, the loop dimension itself, so
`tf.roll(x, 1, axis=-1)` under `tf.vectorized_map` rolled values between
batch elements. When the shift is loop variant, a negative axis also
produced the wrong segment ids.

The gradient of Roll is a Roll along the same axis, and
`GradientTape.jacobian` and `batch_jacobian` use pfor by default, so the
same mistake silently gave them wrong results.

Only add 1 to non-negative axes, as the other pfor converters do, and
normalize negative axes in the loop-variant shift path. Add tests for
negative axes with fixed and loop-variant shifts and for the Jacobian.
@google-ml-butler google-ml-butler Bot added the size:S CL Change Size: Small label Nov 2, 2026
@google-ml-butler
google-ml-butler Bot requested a review from cantonios November 2, 2026 14:57
@google-ml-butler google-ml-butler Bot added the awaiting review Pull request awaiting review label Nov 2, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request fixes a bug in tf.vectorized_map, tf.GradientTape.jacobian, and batch_jacobian where tf.roll with a negative axis rolled along the wrong dimension. The fix adjusts how the loop dimension affects the axis index in _convert_roll inside pfor.py, ensuring negative axes are correctly handled when they are either unstacked or stacked. Additionally, comprehensive unit tests covering negative axes and jacobians have been added to control_flow_ops_test.py, and a release note has been added to RELEASE.md. I have no feedback to provide as the changes are well-implemented and include appropriate test coverage.

@nithyak0204 nithyak0204 self-assigned this Nov 3, 2026
@nithyak0204
nithyak0204 requested a review from a team November 3, 2026 04:29
@nithyak0204 nithyak0204 added comp:ops OPs related issues prtype:bugfix PR to fix a bug labels Nov 3, 2026
@github-project-automation github-project-automation Bot moved this to Assigned Reviewer in PR Queue Nov 3, 2026
@google-ml-butler google-ml-butler Bot added kokoro:force-run Tests on submitted change ready to pull PR ready for merge process labels Nov 3, 2026
@github-project-automation github-project-automation Bot moved this from Assigned Reviewer to Approved by Reviewer in PR Queue Nov 3, 2026
@nithyak0204 nithyak0204 removed the awaiting review Pull request awaiting review label Nov 3, 2026
@copybara-service
copybara-service Bot merged commit 05d8735 into tensorflow:master Nov 3, 2026
27 checks passed
@github-project-automation github-project-automation Bot moved this from Approved by Reviewer to Merged in PR Queue Nov 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp:ops OPs related issues kokoro:force-run Tests on submitted change prtype:bugfix PR to fix a bug ready to pull PR ready for merge process size:S CL Change Size: Small

Projects

Status: Merged

Development

Successfully merging this pull request may close these issues.

tf.roll with a negative axis gives wrong results under tf.vectorized_map and GradientTape.jacobian

3 participants