Repository navigation
Handle negative axes in the pfor converter for Roll - #129841
Merged
copybara-service[bot] merged 1 commit intoNov 3, 2026
Merged
copybara-service[bot] merged 1 commit into
copybara-service[bot] merged 1 commit into
Conversation
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.
Contributor
There was a problem hiding this comment.
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.
dmiltr3
approved these changes
Nov 3, 2026
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.
Fixes #129840
Problem
The pfor converter for
Rolladds 1 toaxisto account for the loop dimension at the front. Negative axes count from the end and shouldn't change, but-1 + 1becomes0, the loop dimension. Sotf.roll(x, 1, axis=-1)undertf.vectorized_mapmoves values between batch elements:The gradient of
Rollis aRollalong the same axis, andGradientTape.jacobianandbatch_jacobianuse pfor by default, so they also silently return wrong results for any computation usingtf.rollwith a negative axis:Change
axis += math_ops.cast(axis >= 0, axis.dtype)idiom as the neighboring converters.axisto computeaxis_segment_idsrelative to the unstacked dimensions, so normalize negative axes first by addingnum_unstacked_axes.Tests added to
NNTestincontrol_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.pyis identical to master's, with this PR's files copied in:control_flow_ops_test.py: 211 tests, OK (71 skipped).pfor.py, all three new tests fail in both graph and eager mode.pylint --rcfile=tensorflow/tools/ci_build/pylintrcon the changed files: no messages on the changed lines.I did not run
gradients_test.pyor test on GPU. I added aRELEASE.mdentry because outputs change for these inputs.