fix(ascend): synchronize PyTorch operators with NPU streams - #974
Merged
Conversation
voltjia
requested changes
Sep 4, 2026
Comment on lines
+20
to
+24
| if (stream == nullptr) { | ||
| return c10_npu::getDefaultNPUStream( | ||
| static_cast<c10::DeviceIndex>(device_index)); | ||
| } | ||
|
|
Collaborator
There was a problem hiding this comment.
这个感觉不是必要的,按理说应该都是相当于转发到 C10 的 getStreamFromExternal 就行,我看别的平台都没加 nullptr 的处理,可以看看昇腾是不是这样。
Contributor
Author
There was a problem hiding this comment.
确认过了,昇腾这里和其他平台的行为不同。当前验证的 torch_npu 2.10.0.post4 对应源码提交 5dd8ef3f9b375b5ae4a83538d5785754148c3302,其 c10_npu::getStreamFromExternal 会显式检查 stream != nullptr,否则直接报 External NPU stream does not support nullptr.:
InfiniOps 的 stream=0 会传入空指针来表示默认流,因此这里不能无条件转发;非空流仍然直接调用 getStreamFromExternal。我补充了英文注释说明这个平台差异,并重新验证了 Ascend 构建和 tests/test_abs.py --devices ascend,结果为 20 passed。
voltjia
approved these changes
Sep 4, 2026
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
src/torch/ascend/c10.husingc10_npu::NPUStreamGuardandc10_npu::getStreamFromExternal.torch_npuC++ headers and library for Ascend PyTorch builds, and add regression coverage for code generation and NPU stream ordering.Motivation
Ascend was not included in the generated PyTorch backend's C10 stream-guard path. Consequently, slot 8 operators could execute on a different NPU stream from the InfiniOps runtime stream, which risks incorrect ordering and results when surrounding work is asynchronous.
This change gives Ascend the same stream-guard behavior as the other PyTorch-backed accelerator platforms. It also fails at configure time with a clear diagnostic when the installed
torch_npudoes not provide the required external-stream API.Type of Change
feat— new feature / new operator / new platformfix— bug fixperf— performance improvement (no behavioral change)refactor— code restructuring without behavior changetest— adding or fixing tests onlydocs— documentation onlybuild/ci— build system or CI configurationchore— tooling, formatting, or other non-code changes!in the Conventional Commits prefix or aBREAKING CHANGE:footer)Platforms Affected
WITH_CPU)WITH_NVIDIA)WITH_ILUVATAR)WITH_METAX)WITH_CAMBRICON)WITH_MOORE)WITH_ASCEND)WITH_TORCH)Smoke Test Result
Ascend 910C, CANN 9.0.0, Python 3.11, PyTorch 2.10.0, and
torch_npu 2.10.0.post4:The CMake cache used
WITH_ASCEND=ON,WITH_CPU=ON,WITH_TORCH=ON,INFINI_RT_ROOT=/workspace/InfiniRT-6b256e0-prefix, and generatedabs,tril, andtriuPyTorch operators.A CPU-only regression build using
WITH_CPU=ON,WITH_ASCEND=OFF, andWITH_TORCH=ONalso completed successfully:Test Results on Supported Platforms
WITH_NVIDIAabs: 20 passed;tril/triu: 18 passedAdditional integration results
Benchmark / Performance Impact
N/A. This is a correctness and stream-ordering fix; no performance claim is made.
Notes for Reviewers
torch_npuintegration is enabled only when bothWITH_ASCENDand generated PyTorch sources are present. Non-Ascend builds do not discover or linktorch_npu.torch_npu 2.10.0.post4was validated. An older2.10.0.post2wheel does not exposec10_npu::getStreamFromExternal; CMake now reports this incompatibility explicitly.trilandtriuused implementation 8.trilortriuis included in this PR.