fix(torch): honor handle streams in generated operators - #880
Open
voltjia wants to merge 3 commits into
Open
Conversation
voltjia
force-pushed
the
fix/torch-backend-stream
branch
5 times, most recently
from
August 5, 2026 10:15
f457ca2 to
f99e11e
Compare
voltjia
force-pushed
the
fix/torch-backend-stream
branch
4 times, most recently
from
August 9, 2026 10:48
cc00c1d to
af8ae29
Compare
voltjia
force-pushed
the
fix/torch-backend-stream
branch
from
August 9, 2026 11:21
af8ae29 to
f6e6f27
Compare
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.
Summary
Handleas PyTorch's current stream before generated ATen and linked Torch calls, then restores the previous device and stream.C10<kDev>template insrc/torch/c10.hand keeps each backend specialization insrc/torch/<platform>/c10.h, following the existing platform-oriented C10 design without a separate stream bridge.Motivation
Generated ATen backends received the InfiniOps handle stream through
stream_, but they invoked PyTorch without installing that stream as the framework's current stream. PyTorch work could therefore run on a different stream from the caller-selected InfiniRT stream and violate the caller's ordering assumptions. Linked Torch adapters already modeled device-specific C10 behavior, so the shared implementation should retain that template structure and live under the common Torch backend rather than underlinked.No issue is linked.
Type of Change
feat- New feature, operator, or platform.fix- Bug fix.perf- Performance improvement without a behavioral change.refactor- Code restructuring without a behavioral change.test- Adding or fixing tests only.docs- Documentation only.build/ci- Build system or CI configuration.chore- Tooling, formatting, or other non-code changes.Platforms Affected
WITH_CPU).WITH_NVIDIA).WITH_ILUVATAR).WITH_METAX).WITH_CAMBRICON).WITH_MOORE).WITH_ASCEND).WITH_TORCH).Smoke Test Result
Test Results on Supported Platforms
abscurrent-stream and handle-stream cases passed, and generated plus linked FlashAttention handle-stream cases passed for implementation slots 8 and 16. The preceding SHA with identical executable code passed the NVIDIA main matrix. The final-SHA main and shadow jobs failed after their assigned runners went offline during execution, without producing failure logs.c10::cuda::getStreamFromExternal.c10::cuda::getStreamFromExternal.torch_mlu::getStreamFromExternal.c10::musa::getStreamFromExternalfrom the explicitly linkedlibmusa_python.c10_npu::getStreamFromExternalwhen the installed headers provideNPUStreamUtils.h, and otherwise retains the current stream.Local validation output.
Benchmark / Performance Impact
N/A. This is a stream-ordering correctness fix and an ownership refactor, and no performance benchmark was run.
Notes for Reviewers
InfiniRT remains the source of truth for each backend's native stream type through
Runtime<kDev>::Stream. Vendor PyTorch APIs are used only by the correspondingC10<kDev>specialization to install that native stream into PyTorch's thread-local current-stream state.src/torch/c10.hcontains only the template declaration. Each platform owns its specialization undersrc/torch/<platform>/c10.h, matching the original C10 organization. The oldsrc/linked/torch/c10.h, backend-local linked C10 headers, and the centralizedsrc/torch/stream_.hbridge have been removed.Generated and linked call sites hold an optional platform
StreamGuard. They callC10<kDev>::GetStreamFromExternalonly when InfiniRT supplies a non-null stream, so a null handle leaves PyTorch's selected current stream unchanged. The only platform-selection preprocessor conditions are the existing build guards needed to avoid including unavailable vendor headers; stream conversion itself is implemented by template specializations rather than macros.NVIDIA, Iluvatar, and MetaX use
c10::cuda::getStreamFromExternal. Moore usesc10::musa::getStreamFromExternal, Cambricon usestorch_mlu::getStreamFromExternal, and Ascend usesc10_npu::getStreamFromExternalwhen available.Hygon is not claimed by this change because a validated PyTorch external-stream bridge for that backend is not available in the current test matrix. Its C10 specialization explicitly retains the current PyTorch stream.