Repository navigation
Fix nested ForwardAccumulator results when intermediates are reused - #128029
copybara-service[bot] merged 2 commits into
Conversation
There was a problem hiding this comment.
Code Review
This pull request improves cache reuse for concrete functions under forward propagation by normalizing tensor IDs of inputs and tangents to create a stable cache key, and adds comprehensive tests to verify this behavior. The feedback suggests using itertools.chain instead of sequence concatenation when processing args and input_tangents.tangents to prevent potential TypeErrors and avoid unnecessary memory allocation.
| forwardprop_input_aliases = tuple( | ||
| tensor_indices.setdefault(ops.tensor_id(t), len(tensor_indices)) | ||
| for t in args + input_tangents.tangents) |
There was a problem hiding this comment.
Concatenating args and input_tangents.tangents using the + operator can raise a TypeError if one is a list and the other is a tuple (e.g., TypeError: can only concatenate list (not "tuple") to list).\n\nUsing itertools.chain is more robust as it handles any combination of iterable types, and it is also more efficient because it avoids allocating a temporary concatenated list in memory.
forwardprop_input_aliases = tuple(\n tensor_indices.setdefault(ops.tensor_id(t), len(tensor_indices))\n for t in itertools.chain(args, input_tangents.tangents))References
- Ensure robustness by flagging fragile logic (such as potential TypeErrors from sequence concatenation) and optimizing performance by avoiding unnecessary memory allocations. (link)
dmiltr3
left a comment
There was a problem hiding this comment.
The internal presubmit tests have compiled cleanly and passed successfully. Code looks good to me! Approving.
Summary
Fixes #127753.
Include the aliasing pattern of primal inputs and packed tangents in the
ConcreteFunctionforward/backward function cache key.JVP wrappers capture external tensors by identity. When several inputs refer to
the same tensor, the generated graph's capture mapping reflects that sharing.
Previously, the cache key included tangent indices but not tensor sharing, so a
later call with distinct tangents could reuse a graph built for shared tangents.
This produces incorrect derivatives, including the reported
2.4instead of4.2for the nested forward derivative of(m * m) * matx = 0.7.The new key records normalized alias indices, not actual tensor IDs, so fresh
tensors with the same sharing pattern still reuse the generated functions. The
alias key is only computed when input tangents are present.
Regression coverage includes:
an active reverse-mode tape, and checking cache reuse with fresh tensors.
in float32/float64, with eager and
tf.functionexecution.Validation
Local validation used Python 3.13 on macOS arm64 with
tf-nightly==2.22.0.dev20260923. The source test modules were loaded against theinstalled runtime, with the exact edited
ConcreteFunction._select_forward_and_backward_functionsmethod compiled fromthe checkout and installed on the runtime class. This is not a full source build.
forwardprop_test.py: 93 passed, 4 skipped, 2 baseline failures outof 99. Both remaining failures occur with and without this patch:
testNonTensorInputsandtestShapeMismatchRaisesError. The Nightly wheellacks the newer input-validation behavior expected by those source tests.
polymorphic_function/gradients_test.py: 42 passed, 2 skipped.git diff --check: passed.Bazel and GPU validation were not run locally. The relevant source-build targets
for CI are:
bazel test //tensorflow/python/eager:forwardprop_test \ //tensorflow/python/eager/polymorphic_function:gradients_test