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
2 times, most recently
from
August 10, 2026 08:11
ce71ffc to
5d4f0dc
Compare
voltjia
force-pushed
the
fix/torch-backend-stream
branch
from
August 11, 2026 05:41
5d4f0dc to
a074719
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
Handlehas no stream, and imports a non-null handle stream through the vendor's external-stream API before constructing the native PyTorch stream guard.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.StreamGuardoperations from each specialization, while keeping the null-versus-non-null selection explicit at generated, linked, and hand-written call sites.Motivation
Generated ATen backends received the InfiniOps handle stream through
stream_, but they invoked PyTorch without installing the selected stream through a native PyTorch guard. PyTorch work could therefore run on a different stream from the caller-selected InfiniRT stream and violate the caller's ordering assumptions.The previous revision also introduced
C10<kDev>::GetStream, which combined InfiniOps policy with vendor C10 integration and changed a null handle from the InfiniOps default stream to the current PyTorch stream. The specializations now expose separate operations corresponding to the vendor C10 APIs, and each consumer preserves the existing handle contract explicitly.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 exposes matching no-op default/external stream operations.accelerator-dev/nvidia:latest; its main and shadow CI jobs also passed.c10::hip::getDefaultHIPStreamandc10::hip::getStreamFromExternal.torch_mlu::getDefaultMLUStreamandtorch_mlu::getStreamFromExternal.torch_npureleases can use the default stream but reject explicit non-null external streams clearly.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 obtain the default stream or translate an external native stream into the vendor's C10 stream type.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 centralizedsrc/torch/stream_.hbridge have been removed.Generated, linked, and hand-written call sites select
GetDefaultStreamwhenstream_ == nullptrandGetStreamFromExternalotherwise, then construct the platform's nativeStreamGuard. This preserves the InfiniOps handle contract without hiding selection policy inside the C10 trait.The Ascend external-stream API was introduced together with
NPUStreamUtils.h, so that header remains the compile-time capability signal. Oldertorch_npureleases still support null handles throughgetDefaultNPUStream, while a non-null handle produces a clear error instead of silently running on another stream.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 genericc10::Streamdeclarations, and the normalc10::StreamGuardremains visible in the platform specialization.