feat(edge_softmax): add edge_softmax and CSRTranspose NPU support for Ascend - #15
Closed
xuejiakn wants to merge 10 commits into
Closed
feat(edge_softmax): add edge_softmax and CSRTranspose NPU support for Ascend#15xuejiakn wants to merge 10 commits into
xuejiakn wants to merge 10 commits into
Conversation
- Add sddmm_dot_kernel.cpp: Ascend C kernel for dot op (FP32/FP16, COO format) - Add sddmm_copy_lhs_kernel.cpp: NPU-native gather kernel for copy_lhs/copy_rhs (avoids CPU roundtrip that caused PyTorch NPU stream sync issues with autograd) - Add sddmm.cc: host dispatch supporting all ops (dot/add/sub/mul/div/copy_lhs/copy_rhs) - dot: NPU kernel (COO + CSR via CSRToCOO) - copy_lhs/copy_rhs: NPU gather kernel (no CPU roundtrip) - add/sub/mul/div: CPU fallback (FP32 only) - int64 index: cast to int32 then delegate to int32 path - Modify kernel.cc: SDDMM/CSRSDDMM/COOSDDMM dispatch macros to ATEN_XPU_SWITCH_CUDA_ASCEND
- Add spmm_kernel.cpp: Ascend C kernel for SpMM (copy_lhs + sum/max/min, FP32/FP16) - Multi-core: split by dst rows, dynamic core count - Double buffer feature queue (BUFFER_NUM=2) - FP16: cast to FP32 for accumulation, cast back - Tail core safety: load indptr by actualRows (not rowsPerCore+1) - Add spmm_tiling.h: tiling struct shared between kernel and host - Modify spmm.cc: - FP32: use new NPU kernel (SpMMCSRNpuF32) instead of CPU fallback - int64 index: cast CSR to int32 via CastCSRToInt32SpMM - Non-copy_lhs ops (mul/add/copy_rhs): CPU fallback - Use default stream (nullptr) instead of custom stream to align with PyTorch NPU - Remove use_pytorch_stream() env var dependency
…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
…stream - array.cc: add CPU fallback with aclrtSynchronizeDevice() for 5 COO operations that previously only dispatched to CPU/CUDA (not Ascend): - COOGetRowNNZ (scalar + array) - COOGetRowDataAndIndices - COOSliceRows(NDArray) - COOSort_ - Also add aclrtSynchronizeDevice() to existing COOGetDataAndIndices/COOGetData - segment_reduce.cc: - Use default stream (nullptr) instead of custom stream - Use synchronous aclrtMemcpy instead of aclrtMemcpyAsync - Add aclrtSynchronizeDevice() at entry to ensure PyTorch ops complete - unit_graph.cc: InEdges() prefers COO format on Ascend to avoid CSR kernel stream sync issues (COO InEdges uses CPU fallback which is more reliable)
- 51 test cases covering: - SDDMM: dot/add/mul/copy_lhs/copy_rhs × COO/CSR × FP32/FP16 × int32/int64 - SPMM: copy_lhs + sum/max/min × int32/int64, update_all, backward - UDF reduce (degree bucketing) + backward - Stream sync: in_edges/in_degrees after update_all/gsddmm - Repeated update_all (multi-step stability) - segment_reduce (mean_nodes) forward + backward - End-to-end: GCN forward/backward, multi-layer GCN, training loop - 4 skipped: sub/div CPU fallback (linker limitation) - 3 xfailed: FP16 SDDMM (intermittent NPU stream race, passes in isolation)
- sddmm.cc: use explicit aclrtSynchronizeDevice after each CopyTo in CPU fallback to ensure NPU data is fully transferred before CPU computation - _sparse_ops.py: add torch.npu.synchronize() before _CAPI_DGLKernelSDDMM (same pattern as segment_reduce) to prevent PyTorch NPU stream data races - test_sddmm_spmm.py: enable all SDDMM ops (add/sub/mul/div) with proper tolerance for div, add sync in test setup/teardown - All 58 tests pass: 0 failed, 0 skipped, 0 xfailed
… Ascend Add Ascend NPU adaptation for edge_softmax operator, enabling GAT/PAGTN graph attention network training on Ascend 910B NPU. New operators: - edge_softmax forward/backward: Ascend C kernel with FullLoad/RowSplit dual-mode, FP32/FP16 support, AR/ARA (num_heads=1/>1) branches - CSRTranspose: Ascend implementation via COOToSR(COOTranspose(CSRToCOO)) chain, reusing existing Ascend kernels Framework integration: - kernel.cc: dispatch edge_softmax by data tensor device (not graph context) to handle NPU tensors with CPU-resident graph topology; add CPU fallback for BackwardSegmentCmp and ScatterAdd (needed by GAT WeightedSumAndMax) - array.cc: add Ascend dispatch branch for CSRTranspose Host code (edge_softmax.cc): - ACL host with forward/backward template specializations (FP32/FP16 x int32/int64) - CSC edge ID remapping via CPU gather/scatter (NPU torch index ops unreliable on DGL blob tensors) - Backward interface adapted to DGL convention (sds = out * grad_out) End-to-end validation: - pytest test_gat.py: 2/3 passed, 1 regression needs 800 epochs (CPU also needs more epochs) - pytest test_pagtn.py: no operator crashes (precision is test-epoch limit) - test_edge_softmax_npu.py: 78 tests, 100% pass (FP32/FP16 x fwd/bwd x 6 graph types x 3 num_heads + edge cases + perf) Co-Authored-By: CANNBot <cannbot@ascend>
This was referenced Jul 28, 2026
Author
PR 已由重组后的新 PR 替代本 PR(10 commits)存在三个问题:① 巨型 commit 经核查,#15[1] 巨型 commit 的 65 个文件主体已在上游 master 合并,残留 6 文件均为旧版回退(README 回退为英文版、CMake/build.sh 含硬编码个人路径 重组后各 commit 去向:
|
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
Add Ascend NPU adaptation for
edge_softmaxoperator, enabling GAT/PAGTN graph attention network training on Ascend 910B NPU.Changes
New Operators
edge_softmax_kernel.cpp,edge_softmax_tiling.h,edge_softmax.cccsr_transpose.ccCOOToCSR(COOTranspose(CSRToCOO))chain, reusing existing Ascend kernels.Framework Integration
kernel.cc: Dispatchedge_softmaxby data tensor device (not graph context) to handle NPU tensors with CPU-resident graph topology. Add CPU fallback forBackwardSegmentCmpandScatterAdd(needed by GATWeightedSumAndMaxreadout).array.cc: Add Ascend dispatch branch forCSRTranspose.Host Code (
edge_softmax.cc)back_out = sds - out * sum(sds)Test Coverage
Unit Tests (
tests/ascend/test_edge_softmax_npu.py)78 tests, 100% pass rate:
End-to-End Validation
pytest test_gat.py::test_gat_classificationpytest test_gat.py::test_gat_reloadpytest test_gat.py::test_gat_regressionpytest test_pagtn.pyDependencies
feat-sddmm-spmm-ascend) which provides the Ascend build infrastructurefeat-edge-softmax-ascendis created fromfeat-sddmm-spmm-ascendCo-authored-by
CANNBot (Ascend C operator development tool)
operators/edge_softmax/