已关闭
[Requirement|需求建议]: 910b的sort_with_index实现 #1765
zhaohujie创建于 6月2日关闭于 6月23日
zhaohujie
6月2日 评论:
6月2日 评论:
/assign @zhaohujie


6月2日 将 zhaohujie 设为负责人
6月2日 修改标题为 “[Requirement|需求建议]: 910b的sort_with_index实现”,原标题为“[Requirement|需求建议]: 910b的sortwithindex实现”
6月2日 修改标题为 “[Requirement|需求建议]: 910b的sort_with_index实现”,原标题为“[Requirement|需求建议]: 910b的sortwithindex实现”
6月2日 修改了issue 的描述
6月2日 关联了pull request:feat(sort_with_index): 910b的sort_with_index实现
6月23日 关闭了 issue
6月23日 添加了label:resolved
一、背景信息 (必填)
仓库已存在完整算子 math/sort_with_index,但其 AICore().AddConfig 仅注册 ascend950,kernel 全在 op_kernel/arch35/(ascend950 专用),在 Ascend910B 上无可用 kernel/二进制(系统 opp 中的 SortWithIndex 同样仅 arch35,且未导出 aclnnSortWithIndex)。本需求补齐 Ascend910B 原生 AscendC 开源实现,落到 experimental/math/sort_with_index/,以 math/sort_with_index 为接口真值源。
二、价值/作用 (必填)
SortWithIndex在 Atlas A2(910B)上可用,填补该芯片上的能力缺口。y)与跟随排序重排后的索引(sorted_index)。等价于「排序 + 携带一个调用方提供的索引张量同步搬移」,是 Top-K、argsort、检索/重排、稀疏索引跟随等场景的基础算子(index=0..N-1时sorted_index即torch.sort的 indices)。descending(降序)与stable(稳定排序)语义。三、设计方案 (必填)
3.1 使能方式(涉及哪些框架:如Aclnn直调、Pytorch训练等)
Aclnn 直调(aclnnSortWithIndex 两段式)、GE 图模式(proto,动态 shape/rank)、PyTorch(经 torch_npu)。
3.2 总体设计
基于 910B 可用的 Concat/Sort/Extract(大轴核内 MrgSort)重写排序核;索引跟随利用 AscendC Sort 的位置稳定性。
3.2.1 算子支持的数据类型
3.2.2 host侧设计
op_def:AddConfig("ascend910b", …),声明 4 组 dtype,属性axis(-1)/descending(false)/stable(false)。infershape:y.shape = x.shape、sorted_index.shape = index.shape;静态 shape 下强制校验x.shape == index.shape。tiling:按「非排序维(行)」多核切分、行内不跨核;UB 切分 + dtype 感知 padLen 对齐;TilingKey三维(VALUE_DT, INDEX_DT, SIZE_MODE),SIZE_MODE ∈ {单 tile, 大轴 MrgSort, 空 tensor};单行排序轴长超出核内上限时 tiling 返回失败,不崩溃。op_graph:proto +InferDataType(输出 dtype 跟随输入)。3.2.3 kernel侧设计
Concat+Sort+Extract(基于 8B proposal 记录),大轴用核内MrgSort多块归并(无 SyncAll)。Sort在分数相等时按输入位置(positional stable)定序,而非按索引通道值;故 int32 调用方索引可直接进入 Sort 索引通道并提取得到sorted_index,省去 position 通道 + 偏移 Muls + Gather(int64 与大轴 MrgSort 路径仍用 position 通道 + Gather)。Cast → float → Sort → Cast回原 dtype。Muls(-1)取反;±Inf 用 ±Inf 哨兵(升序 +Inf / 降序 -Inf,避免有效 ±Inf 被哨兵顶替)。PIPE_V,仅覆盖 V→MTE3)。3.3 支持硬件
Ascend910B(Atlas A2 系列,DAV_2201)
3.4 算子约束限制
axis须为-1或rank-1,否则报错;行间独立。|x| ≤ 2^24精确(经 float 路径排序)。torch.sort的末尾约定);位型比对按isnan(非按 bit)。±Inf:+Inf 升序末尾、-Inf 升序开头。x与index必须同 shape。💡 备注(选填)