Skip to content

fix(npu): sync PyTorch NPU stream for graph-structure queries - #13

Open
xuejiakn wants to merge 1 commit into
BUPT-GAMMA:masterfrom
xuejiakn:fix/npu-stream-sync
Open

fix(npu): sync PyTorch NPU stream for graph-structure queries#13
xuejiakn wants to merge 1 commit into
BUPT-GAMMA:masterfrom
xuejiakn:fix/npu-stream-sync

Conversation

@xuejiakn

@xuejiakn xuejiakn commented Jul 20, 2026

Copy link
Copy Markdown

Summary

Sync PyTorch NPU stream for graph-structure queries to fix data races.

DGL-Ascend runs C++ kernels on the default ACL stream, decoupled from PyTorch's NPU stream. This caused data races: graph-structure queries (in_degrees, out_degrees, edges, in_edges) returned corrupted values when called after PyTorch tensor ops on the same graph.

Changes

  • streams.py: to_dgl_stream_handle() now supports torch.npu.Stream
  • heterograph.py: DGLGraph.to() calls record_stream() with NPU stream; new _sync_npu() helper around structure queries
  • _sparse_ops.py / core.py: stream sync for segment_reduce

Restructure note

This PR has been restructured to consolidate the stream-sync commits that were duplicated across #14[3] and #15[6] (functionally identical supersets). The branch now contains a single clean commit representing the latest version.

Verified: test_cgcnn.py::test_cgcnn_regression passes stably on Ascend 910B3 (CANN 8.3.RC1, torch_npu 2.7.1).

…ment_reduce

- streams.py: to_dgl_stream_handle() now supports torch.npu.Stream (.npu_stream)
  in addition to torch.cuda.Stream (.cuda_stream)
- heterograph.py:
  - DGLGraph.to() calls record_stream() with current PyTorch NPU stream
  - New _sync_npu() helper calls torch.npu.synchronize() before/after every
    structure query (in_degrees, out_degrees, edges, in_edges) on NPU graphs
- _sparse_ops.py: add torch.npu.synchronize() before _CAPI_DGLKernelSegmentReduce
  to prevent data races between PyTorch NPU stream and DGL default ACL stream
- core.py: fix assert to use int(deg) for numpy type compatibility
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant