fix(torch): honor handle streams in generated operators - #880
Open
voltjia wants to merge 2 commits into
Open
Conversation
voltjia
force-pushed
the
fix/torch-backend-stream
branch
4 times, most recently
from
August 5, 2026 10:00
8f6a607 to
f457ca2
Compare
voltjia
force-pushed
the
fix/torch-backend-stream
branch
from
August 5, 2026 10:15
f457ca2 to
f99e11e
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
Runtime<kDev>::Streamaliases to bridge explicit handle streams into generated PyTorch operators on NVIDIA, MetaX, and supported Ascendtorch_npuversions.Handlecarries no stream and restores the previous device and stream after an explicit 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. The bridge must also remain cross-platform and must not convert a missing handle stream into a vendor default stream.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
torch_npulacks the required APIs.Full validation output.
Benchmark / Performance Impact
N/A. This is a stream-ordering correctness fix, and no performance benchmark was run.
Notes for Reviewers
InfiniRT remains the source of truth for each backend's native stream type. Vendor PyTorch APIs are still required to install that native stream into PyTorch's thread-local current-stream state.
The guard is constructed inside every generated C++ ATen operator wrapper, so the fix applies to C++ execution and is not a Python-only adaptation. A null handle stream leaves the framework's existing current stream unchanged.
NVIDIA and MetaX use
c10::cuda::getStreamFromExternal. Ascend usesc10_npu::getStreamFromExternalonly when the installed headers includeNPUStreamUtils.h, which was added with the official external-stream implementation in Ascend/pytorch commit 03027fe. Oldertorch_npureleases retain the current NPU stream because they cannot safely encode an arbitraryaclrtStream.Validation used InfiniRT
95c70080f9551e61241110497d163dfcdf9dc7e7for NVIDIA and MetaX. The Ascend environment used PyTorch2.9.0+cpuandtorch_npu 2.9.0.post1+gitee7ba04; it validates the compatibility fallback but not the newer native external-stream path.