已合并
feat(coo_ops): adapt 5 COO operators for Ascend NPU via CSR composition #22
feat(coo_ops): adapt 5 COO operators for Ascend NPU via CSR composition #22
已合并
lhp_lhp创建于 8月27日
lhp_lhp成员
8月27日

Description

为 Ascend 910B3 (dav-2201) 适配 5 个 COO 算子的 NPU 原生路径,替换原有的 CPU 回退(D2H→CPU→H2D)和 LOG(FATAL) 崩溃。

适配策略

通过组合已有 Ascend 原生 CSR 算子实现:COO → COOToCSR → CSR op → CSRToCOO(与 csr_transpose.cc 同范式),无需编写新的 AscendC kernel。

适配算子

算子 实现方式 说明
COOSort_ COOToCSR(排序) + CSRToCOO row-only sort 走 NPU 原生;column sort 走 CPU 回退(CSRSort_ 无 Ascend kernel)
COOGetRowNNZ (scalar+array) COOToCSR + CSRGetRowNNZ 支持 int32/int64
COOGetRowDataAndIndices COOToCSR + CSRGetRowColumnIndices + CSRGetRowData EnsureCOOData 保留原始边 ID
COOSliceRows (start/end + rows) COOToCSR + CSRSliceRows + CSRToCOO 两个重载均已适配
COOSliceMatrix COOToCSR + CSRSliceMatrix + CSRToCOO 行列双向切片

支持的数据类型

COO 算子为整数索引操作(无浮点特征),只涉及 IdType,不涉及 DType:

IdType 状态
int32 ✅ 全部 5 个算子
int64 ✅ 全部 5 个算子

COO 算子操作的是图的边索引(row/col/data),均为整数类型,不涉及特征张量的浮点 DType(float/half/bfloat16/double)。

Checklist

Changes

Test Results

  • 126/126 passed, 0 failed, 0 xfailed
  • 精度口径:整数索引操作 bit-exact(torch.equal)

Excluded (Follow-up)

  • COOIsSorted, COOIsNonZero (scalar+array): 将在后续 PR 中用专用 AIV kernel 适配,避免大 D2H 开销
likedislike
Pull Request已成功合入, 合并人@knightgd
(感谢 lhp_lhp 的贡献)
Llhp_lhp成员
8月27日 强制推送  1 个提交:d674b6e1-feat(coo_ops): adapt 7 COO operators for Ascend NPU via CSR composition
Llhp_lhp成员
8月27日 修改了pull request 的描述
Llhp_lhp成员
8月27日 将 knightgd 设为合并人
Llhp_lhp成员
8月27日 移除了合并人knightgd
Llhp_lhp成员
8月27日 将 knightgd 设为合并人
Llhp_lhp成员
9月1日 关联了issue:COO系列算子适配NPU
knightgd成员9月2日进行代码检视3
src/array/ascend/coo_ops.cc
已过期
@@ -0,0 +238,20 @@
238+template <typename IdType>
239+std::pair<bool, bool> COOIsSorted_Ascend(COOMatrix coo) {
240+ // Use metadata flags. If both flags say sorted, trust them.
241+ if (coo.row_sorted && coo.col_sorted) {
242+ return {true, true};
243+ }
244+ // When the metadata does not guarantee both sorted (row_sorted false or
245+ // col_sorted false), we cannot trust the flags alone: the data may still be
246+ // genuinely sorted but simply not flagged (e.g. is_sorted_srcdst always
247+ // constructs a COOMatrix with row_sorted=false). Fall back to a CPU scan
248+ // (D2H of the small index arrays) to report the true sorted state.
249+ //
250+ // COOIsSorted is called infrequently and returns only a bool pair, so the
251+ // CPU fallback is the pragmatic choice (mirrors the row_sorted-only branch).
252+ DGLContext cpu_ctx{kDGLCPU, 0};
253+ aclrtSynchronizeDevice();
254+ COOMatrix coo_cpu{
255+ coo.num_rows, coo.num_cols,
256+ coo.row.CopyTo(cpu_ctx), coo.col.CopyTo(cpu_ctx),
257+ coo.data.CopyTo(cpu_ctx), coo.row_sorted, coo.col_sorted};
258+ return COOIsSorted<kDGLCPU, IdType>(coo_cpu);
knightgd9月2日评论:

此条代码评论区间+238至+258

没有实现AscendC kernel,cpu fallback了,影响性能

likedislike
System
系统消息系统
9月4日 评论:

changed this line on c981ac7a view diff detail

lhp_lhp成员
9月4日 评论:

已移除,重新开PR单独跟踪解决

knightgd成员9月2日进行代码检视3
src/array/ascend/coo_ops.cc
已过期
@@ -0,0 +275,19 @@
275+template <typename IdType>
276+bool COOIsNonZero_Ascend(COOMatrix coo, int64_t row, int64_t col) {
277+ CSRMatrix csr = COOToCSR(coo);
278+ // Construct the single-element query arrays on CPU (host-accessible) and copy
279+ // to the NPU. Directly dereferencing the raw device pointer returned by
280+ // NDArray::data (e.g. `static_cast<IdType*>(row_arr->data)[0] = row`) is host
281+ // pointer aliasing of device memory -- undefined behaviour under the ACL
282+ // unified-memory model. Building on CPU + CopyTo mirrors the established
283+ // pattern in this file (EnsureCOODataForCSR) and coo2csr.cc.
284+ DGLContext cpu_ctx{kDGLCPU, 0};
285+ NDArray row_arr = NDArray::Empty({1}, coo.row->dtype, cpu_ctx);
286+ NDArray col_arr = NDArray::Empty({1}, coo.col->dtype, cpu_ctx);
287+ static_cast<IdType*>(row_arr->data)[0] = static_cast<IdType>(row);
288+ static_cast<IdType*>(col_arr->data)[0] = static_cast<IdType>(col);
289+ row_arr = row_arr.CopyTo(coo.row->ctx);
290+ col_arr = col_arr.CopyTo(coo.col->ctx);
291+ NDArray data = CSRGetData<kDGLAscend, IdType>(csr, row_arr, col_arr);
292+ IdType val = static_cast<IdType*>(data.CopyTo(cpu_ctx)->data)[0];
293+ return val != -1;
294+}
knightgd9月2日评论:

此条代码评论区间+275至+294

部分NPU实现+部分CPU实现,存在CPU到NPU的数据搬运,对性能有影响

likedislike
System
系统消息系统
9月4日 评论:

changed this line on c981ac7a view diff detail

lhp_lhp成员
9月4日 评论:

已移除,重新开PR单独解决

Llhp_lhp成员
9月4日 强制推送  1 个提交:c981ac7a-feat(coo_ops): adapt 5 COO operators for Ascend NPU via CSR composition
Llhp_lhp成员
9月4日 修改了pull request 的描述
Llhp_lhp成员
9月4日 修改了pull request 的描述
Kknightgd成员
9月4日 合入了pull request,合并节点 SHA:e4e189a7a80ac1393b7e41253a2d458eaf3db6f9
Llhp_lhp成员
27 天前 修改标题为 “feat(coo_ops): adapt 5 COO operators for Ascend NPU via CSR composition”,原标题为“feat(coo_ops): adapt 7 COO operators for Ascend NPU via CSR composition”