flowchart TD
A[Kernel Process] --> B{activeDimCount == 0?}
B -->|Yes| C[CopyIdentity]
B -->|No| D{dimNum == 1?}
D -->|Yes| E[CopyFlattenRollBySource]
D -->|No| F{single active dim?}
F -->|Yes| G{activeDim == 0?}
G -->|Yes| H[CopyLeadingDimRollBySource]
G -->|No| I{innerSize == 1 and last dim?}
I -->|Yes| J[CopyLastDimRollByRows]
I -->|No| K[CopySingleDimRollByBlocks]
F -->|No| L{last active dim is final dim?}
L -->|Yes| M[CopyMultiDimLastDimRollByRows]
L -->|No| N[CopyMultiDimNonLastRollByBlocks]
N --> O[CopySegmentedRoll]
Thanks for sending an requirement! Please fill in the following template to help quickly solve your problem.
Roll 算子需求与设计说明
一、需求背景
Roll 算子用于沿指定维度对输入张量执行循环位移,常见于序列处理、窗口重排和特征对齐等场景。
本说明聚焦当前最终提交版本的真实能力,目标是明确:
二、目标与范围
2.1 功能目标
aclnnRollGetWorkspaceSize与aclnnRoll两阶段 ACLNN 接口。dims为空时,行为等价于先展平再执行一维 roll,最后恢复原始形状。2.2 支持范围
uint8、int8、bfloat16、float16、float32、int32、uint32NDshifts为滚动量,dims为滚动维度dims为空的 flatten roll2.3 非目标范围
当前版本不纳入以下支持范围:
boolint64ND数据格式三、输入约束与数学语义
3.1 输入约束
dtype必须一致。shape必须一致。rank不超过8。dims为空时,shifts长度必须为1。dims非空时,shifts与dims长度必须一致。dims的取值范围为[-rank, rank)。shift,需要先做归一化合并。shifts长度必须为1,且dims为空。3.2 数学语义
对任意参与滚动的维度,输出元素满足:
y[i] = x[(i - s) mod n]其中:
s为该维度归一化后的滚动量n为该维度长度多维滚动时,各维度上的归一化结果共同作用于输出索引映射。
四、问题分析
Roll 的定义简单,但高性能实现存在以下难点:
dims为空时需要退化为 flatten roll,不能仍按原始多维索引路径执行。因此,当前实现采取的主线是:
五、总体方案
5.1 方案概述
整体实现分为两部分:
flowchart TD A[aclnnRollGetWorkspaceSize] --> B[参数校验] B --> C[dtype format shape 约束校验] C --> D[dims shifts 归一化] D --> E[重复维度合并] E --> F[识别 activeDim 与 activeDimCount] F --> G[生成 Tiling 信息] G --> H[aclnnRoll] H --> I[Kernel Process] I --> J{路径选择} J --> K[Identity] J --> L[Flatten Roll] J --> M[Leading Dim Roll] J --> N[Last Dim Row Roll] J --> O[Single Dim Block Roll] J --> P[Multi-Dim Last-Dim Roll] J --> Q[Multi-Dim Non-Last Roll] J --> R[Segmented Fallback]5.2 Host 侧职责
Host 侧主要负责:
ND格式校验dims/shifts规则校验shift合并5.3 Kernel 侧职责
Kernel 侧不做复杂策略搜索,只消费 Host 给出的 tiling 信息并执行对应搬运路径。
核心原则:
六、详细设计
6.1 Host 侧设计
6.1.1 归一化逻辑
Host 侧将输入整理成更适合 Kernel 消费的结构:
dims为空时,退化为一维 flatten roll。dims非空时,将负维度转换为正维度。shift先累加,再按该维长度取模。activeDimCount。outerSizedimSizeinnerSizeactiveShift6.1.2 Tiling 数据
Host 侧向 Kernel 传递的核心信息包括:
totalNumdimNumshapes[]strides[]shifts[]activeDimactiveDimCountouterSizedimSizeinnerSizeactiveShiftusedCoreNumperCoreElementslastCoreElementsubElements6.1.3 切分策略
当前版本的 tiling 重点是稳定切分,而非复杂搜索:
perCoreElements切分,保证多核负载基本均衡。uint8小块场景,调整对齐与切分粒度。inner stride场景避免过粗切分。6.2 Kernel 侧设计
6.2.1 路径选择树
flowchart TD A[Kernel Process] --> B{activeDimCount == 0?} B -->|Yes| C[CopyIdentity] B -->|No| D{dimNum == 1?} D -->|Yes| E[CopyFlattenRollBySource] D -->|No| F{single active dim?} F -->|Yes| G{activeDim == 0?} G -->|Yes| H[CopyLeadingDimRollBySource] G -->|No| I{innerSize == 1 and last dim?} I -->|Yes| J[CopyLastDimRollByRows] I -->|No| K[CopySingleDimRollByBlocks] F -->|No| L{last active dim is final dim?} L -->|Yes| M[CopyMultiDimLastDimRollByRows] L -->|No| N[CopyMultiDimNonLastRollByBlocks] N --> O[CopySegmentedRoll]6.2.2 主要实现路径
CopyIdentityshift均为 0 时,直接拷贝。CopyFlattenRollBySourceCopyLeadingDimRollBySourceCopyLastDimRollByRowsinnerSize == 1时,按行处理。CopySingleDimRollByBlocksCopyMultiDimLastDimRollByRowsCopyMultiDimNonLastRollByBlocksCopySegmentedRoll6.2.3 核心辅助能力
Kernel 内部不是单一路径,而是按场景组合以下能力:
这些能力共同服务于不同的 roll 路径。
七、性能优化主线
当前保留下来的优化不是单点 shape 硬编码,而是四类可复用策略。
7.1 连续段优先
能转换为连续段搬运的场景,优先使用连续 copy 或源端对齐 copy,减少逐元素处理。
7.2 最后一维按行优化
最后一维滚动天然适合按行处理。当前版本围绕这条主线提供:
7.3 非最后维按 block 重排
中间维或多维组合滚动时,优先按 block 建模,而不是按元素重算索引:
7.4 小宽度定向增强
当前版本保留一部分收益明确、不过度特化的小宽度优化,主要包括:
uint8小块路径这些优化仍遵循同一原则:
八、测试与验收标准
8.1 功能验收
应满足以下条件:
shape与输入一致。dtype与输入一致。dims为空与dims显式指定两类模式均正确。8.2 性能验收
shape/dtype范围内,性能目标优于参考 TBE。8.3 建议测试覆盖项
建议持续保留以下覆盖:
shift/ 正shift/ 负shift/ 超维长度shift九、维护约束