[Pytorch] Enable TE Sequential Op to consume extra_outputs from a previously run Op - #3320
[Pytorch] Enable TE Sequential Op to consume extra_outputs from a previously run Op#3320vthumbe1503 wants to merge 13 commits into
Conversation
Signed-off-by: Varun Thumbe <vthumbe@nvidia.com>
…h error handling tests Signed-off-by: Varun Thumbe <vthumbe@nvidia.com>
Signed-off-by: Varun Thumbe <vthumbe@nvidia.com>
Signed-off-by: Varun Thumbe <vthumbe@nvidia.com>
Signed-off-by: Varun Thumbe <vthumbe@nvidia.com>
for more information, see https://pre-commit.ci
Signed-off-by: Varun Thumbe <vthumbe@nvidia.com>
Signed-off-by: Varun Thumbe <vthumbe@nvidia.com>
for more information, see https://pre-commit.ci
Greptile SummaryThe PR adds named internal extra-tensor channels to PyTorch fusible operations, including forward routing, backward gradient accumulation, validation, documentation, and tests. The stale-routing fix now makes channel bindings immutable after fuser construction, but its lock also applies to temporary fusers used for standalone operation calls.
Confidence Score: 4/5The PR should not merge until standalone BasicOperation execution stops permanently preventing later channel configuration. A direct BasicOperation call creates and discards a temporary fuser, but construction irreversibly locks the operation, causing later public channel setters to fail even though no retained routing snapshot exists. Files Needing Attention: transformer_engine/pytorch/ops/fuser.py and transformer_engine/pytorch/ops/op.py Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart LR
A["BasicOperation.forward()"] --> B["Temporary OperationFuser"]
B --> C["Capture channel routing"]
C --> D["Permanently lock BasicOperation"]
D --> E["Run operation and discard fuser"]
E --> F["Later channel setter"]
F --> G["RuntimeError"]
Reviews (2): Last reviewed commit: "address review comment" | Re-trigger Greptile |
Signed-off-by: Varun Thumbe <vthumbe@nvidia.com>
| for op in self._basic_ops: | ||
| op._lock_extra_channels() |
There was a problem hiding this comment.
Transient fusers permanently lock channels
When a BasicOperation is executed directly before its channels are configured, BasicOperation.forward creates and discards a temporary OperationFuser, but this constructor permanently locks the operation. A later set_extra_input_channel or set_extra_output_channel call therefore raises RuntimeError even though no retained fuser depends on the captured routing.
Knowledge Base Used: PyTorch Fusible-Operation Framework
There was a problem hiding this comment.
Additional review comments from Codex:
1. [High] Internal channel outputs lose the signal that their gradient is required.
transformer_engine/pytorch/ops/fuser.py:170 only calls requires_grad_ for public outputs at line 194. A fresh tensor created by an internal producer inside
torch.autograd.Function.forward therefore arrives at its consumer with requires_grad=False. Existing operations such as transformer_engine/pytorch/ops/basic/
swiglu.py:468 and ScaledSReLU use that flag to decide whether to compute the extra-input gradient. They consequently return None, silently dropping the
gradient to a differentiable producer such as the documented router-probability dispatch. The new tests do not expose this because MakeExtraOutput returns the
original input tensor.
2. [Medium] Channel routing violates the declared iterable output contract.
transformer_engine/pytorch/ops/fuser.py:140 applies len() and indexing to a producer’s extra outputs, but transformer_engine/pytorch/ops/op.py:92 permits any
Iterable[Iterable[Tensor]]. A custom Dispatch returning a generator works under the old flattening logic but now fails when a later channel consumer executes.
3. [Medium] A standalone operation call permanently prevents later channel configuration.
transformer_engine/pytorch/ops/op.py:598 constructs a temporary OperationFuser, while transformer_engine/pytorch/ops/fuser.py:509 permanently locks every
attached operation. Calling an operation once through its normal forward, then placing it into a channel-connected Sequential, makes either setter raise even
though the temporary fuser no longer exists.
4. [Medium] The tests do not exercise two major routing branches.
tests/pytorch/test_fusible_ops.py:460 registers only a forward fusion, so backward remains unfused and never exercises the same-fusion skip at
transformer_engine/pytorch/ops/fuser.py:323. The multi-output test at tests/pytorch/test_fusible_ops.py:624 only checks duplicate-name rejection; its custom
operation never runs. Thus mixed bound/unbound slot ordering, filtered autograd returns, and the modified two-input GroupedLinear(scale_bias=True) behavior
remain unproved.
## Suggested repairs
- For finding 1: Preserve the gradient-requirement flag on every extra output before classifying it as public or internal, or carry equivalent explicit per-slot
metadata. Add a producer that creates a fresh tensor and verify gradient propagation through ScaledSwiGLU or ScaledSReLU.
- For finding 2: Materialize and validate each operation’s extra outputs as a tuple immediately after fuser_forward; store that tuple for later consumers and
lifetime tracking.
- For finding 3: Make transient fusers created by BasicOperation.forward non-locking, while persistent Sequential fusers retain immutable routing. Add a call-
then-bind regression test.
- For finding 4: Add a joint/backward fused residual operation and assert fusion selection plus input/parameter/channel gradients. Add a successful multi-input/
multi-output routing test and a GroupedLinear(scale_bias=True) channel case.
I would like you to also take a look at the tests - multiple of them duplicate each other (e.g. test_channel_fan_out_accumulates_grad is a stronger duplicate of test_internal_extra_tensor_channel_fanout).
| The operation fuser supports limited branching behavior. While the | ||
| operations must be in sequential order, basic operations may declare | ||
| extra tensor inputs and outputs. By default, an extra tensor slot has | ||
| no channel assigned and is part of the public ``Sequential`` interface: |
There was a problem hiding this comment.
There should be some explanation what a channel is before we say "no channel assigned".
It could be as simple as saying that the extra inputs/outputs may optionally specify
a channel (and then proceed with the rest of the text).
|
|
||
| # Construct MLP with residual connection | ||
| fc1 = te.ops.Sequential( | ||
| # Keep a residual connection inside one Sequential. |
There was a problem hiding this comment.
I would actually keep both of these examples here.
| and cycles are not supported. | ||
| - A channel has exactly one producer, but its output may fan out to | ||
| multiple consumers. | ||
| - Every named output channel must have at least one consumer, and the |
There was a problem hiding this comment.
This limitation that there has to be at least one consumer in the named channel seems
arbitrary to me. If we do not strictly need this behavior then we shouldn't have that
as it would introduce friction when somebody needs to refactor the code using those
named channels by splitting the sequential - now they also need to remove the channel
names. In fact, I would expect people to generally want to name their extra outputs and
inputs even if they would not be reused inside the sequential. That could also enable
us to accept and return the dictionary rather than a list (which would make it less
fragile).
Description
Please include a brief summary of the changes, relevant motivation and context.
Fixes # (issue)
Type of change
Changes
Please list the changes introduced in this PR:
Checklist: