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
11 times, most recently
from
August 10, 2026 06:42
f6e6f27 to
bbc7b58
Compare
voltjia
force-pushed
the
fix/torch-backend-stream
branch
from
August 10, 2026 08:03
bbc7b58 to
ce71ffc
Compare
voltjia
force-pushed
the
fix/torch-backend-stream
branch
from
August 10, 2026 08:11
ce71ffc to
5d4f0dc
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.C10<kDev>::GetStream, so generated wrappers, linked adapters, and the hand-written FlashAttention wrapper always construct the platform's native PyTorch stream guard.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_HYGON).WITH_METAX).WITH_CAMBRICON).WITH_MOORE).WITH_ASCEND).WITH_TORCH).Smoke Test Result
Test Results on Supported Platforms
StreamGuardand does not introduce a separate alias type.moe_wna16_marlin_gemmlinked adapter to the shared C10 implementation.--no-undefinedin the CI base image.c10::hip::getCurrentHIPStreamandc10::hip::getStreamFromExternalfrom an isolated source file. Hygon hardware runtime testing was not available, and unrelated current InfiniRT/DTK build issues prevent a full smoke result.torch_mlustream API.torch_musastream API from the explicitly linkedlibmusa_python.torch_npulacks external-stream support, so this validates the compatibility path that preserves the selected current PyTorch stream.Validation details.
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 translate 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 construct a native platform
StreamGuardunconditionally fromC10<kDev>::GetStream. A null handle is translated to the target device's selected current PyTorch stream, while a non-null handle is translated through the vendor's external-stream API. This leaves native C10 guard behavior unchanged and removes repeated null checks from call sites.The Ascend external-stream API was introduced together with
NPUStreamUtils.h, so that header is used as the compile-time capability signal. Oldertorch_npureleases keep the selected current PyTorch stream for both null and non-null handles; inheriting a non-null InfiniRT stream requires a release that providesc10_npu::getStreamFromExternal.Hygon is not a no-op. Its PyTorch fork exports HIP C10 stream symbols, but directly combining the HIP headers with InfiniRT's DTK CUDA compatibility headers causes runtime type redefinitions.
src/torch/hygon/c10.cctherefore performs only the HIP stream conversion behind a genericc10::Streamdeclaration, and the normalc10::StreamGuardremains visible in the platform specialization.