Repository navigation
Reuse the shard index tensor in the Neuron executor - #1047
zouchaoqun wants to merge 2 commits into
Conversation
ShardIndexInjection adds a vocab_shard_index input to a graph. The executor filled this input at each call with a new torch.tensor(shard_index, device=...). This is an 8-byte host-to-device write before each graph. The runtime submits an operation on a different execution unit only after the previous operation on the stream retires, and the write runs on a different unit than the graph. Thus the write stops the next graph from being submitted while the previous graph runs. Executor.__call__ now passes a cached transform for the shard index it injects. The cache holds one tensor for each (device index, current stream id, shard index), in an LRU of 16 entries. The executor only uses the tensor as a graph input. TRANSFORM_REGISTRY["shard_index"] still makes a new tensor at each call, so the CPU fallback and every other caller behave as before. The executor reuses its tensor only while the registry holds the original factory, and only for real execution on a Neuron device: a call under a dispatch mode, a pre-dispatch capture, native functionalization, a torch function mode, a functorch transform or Dynamo gets a new tensor. apply_input_mutations takes an override from transforms when the key is present.
Dynamo full-graph capture of the executor's shard index injection failed with Unsupported at torch._C._len_torch_dispatch_stack(). The check for torch.compiler.is_compiling() came after that call, so it did not prevent it. Dynamo cannot trace the call because it returns an int. The cached transform now checks is_compiling() first, after it honors a replacement registry factory. A compiled call gets a new tensor, as before this change. The trace-then-run test now includes Dynamo full-graph capture with an empty and a warm cache.
|
I ran the CI test nodes myself while the PR waits for Setup: trn2.48xlarge with the released Neuron SDK 2.33.0 stack (torch 2.14.0, torch-neuronx 2.14.3.0, neuronx-cc 2.28.4568.0, Python 3.14.6). The test tree is the head commit's (3bf0c73). Base uses the installed Two changes were necessary on my host. I used
Result: no test fails only with this change.
I did not run the C++ and MLIR LIT tests, because they need a Bazel build and this change is Python only. I did not run the op-accuracy sweep, because it tests eager ops, which this change does not touch. |
Description
Claude drafted the change, the benchmark scripts, and the blockquoted passages.
Related Issue
RFC: #1046
Motivation and Context
I noticed this issue while optimizing latency for my real-time model. The Trainium2 benchmark confirmed that reusing the shard-index tensor reduced the average time per queued graph call, which is why I’m proposing this change upstream.
Type of Change
Testing
bench_shard_index.py, same_outputs.py and dynamo_check.py
bench_shard_index.py:same_outputs.py:dynamo_check.py:Checklist