Pull Request已成功合入, 合并人@CANN-robot
(感谢 liangtongxue 的贡献)变更摘要
此 PR 对 InitTensorSliceState 函数引入维度合并(coalesce)逻辑,使其切分行为与 PyTorch 对齐。核心改动在于:先将原始 shape 和 strides 提取到临时数组,然后依次剔除大小为 1 的维度、合并连续相邻维度,最终将合并后的结果写入 TensorSliceState,从而减少冗余维度并简化后续的 tensor 分块计算。
主要改动
- 引入维度合并预处理: 新增
rawShape/rawStrides临时数组存储原始维度信息,再通过coalescedShape/coalescedStrides进行合并,解耦了原始 shape 读取与state写入,便于插入合并步骤。 - 剔除大小为 1 的维度: 遍历
rawShape时跳过rawShape[dim] == 1的维度,不将其计入coalescedShape,使合并后的state.ndim减少,避免无效维度参与后续分块运算。 - 合并连续维度: 从高维向低维遍历,当
coalescedShape[dim] * coalescedStrides[dim] == coalescedStrides[dim - 1]时,将两个连续维度合并为一个,并压缩数组,进一步减少coalescedNdim,对齐 PyTorch 将连续维度折叠为单维度的行为。 - 全 1 维度保护: 当所有维度均为 1 导致
coalescedNdim == 0时,设置coalescedShape[0] = 1、coalescedStrides[0] = 1、coalescedNdim = 1,确保标量或全 1 张量场景下状态合法。


代码审查
基于对 random/random_common/op_host/arch35/random_tiling_arch35.cpp 的详细审查,我已逐行分析了 InitTensorSliceState 中新增的 coalesce 逻辑,并追踪了其对 TensorSliceState 的全部消费者(GetMaxOffsetBytes、Is32bitIndexable、GetDimToSplit、ReduceDimExtent、PartitionDim、CalcSplitBlocks)的影响。
审查结论:本 DIFF 没有引入可报告的缺陷。
具体来说:
-
size-1 维度移除(第 111-118 行):正确地从 rawShape/rawStrides 中过滤掉大小为 1 的维度,保留其余维度的形状和步长关系。
-
全 1 维度边界处理(第 119-123 行):当所有维度都是 size-1(
coalescedNdim == 0)时,正确降级为 shape=[1], strides=[1], ndim=1 的一维张量。 -
相邻维度合并(第 124-134 行):合并条件
coalescedShape[dim] * coalescedStrides[dim] == coalescedStrides[dim - 1]正确检测行主序连续维度;合并后的 shape 乘积、stride 继承和元素移位逻辑均正确。循环从最内层向外层推进,合并后的维度能在下一轮迭代中被继续检测,不会遗漏可合并维度对。 -
与消费者的契约一致性:
GetMaxOffsetBytes:coalesce 前后对同一个连续张量返回相同字节偏移(已验证 shape [2,3,4] → coalesced [24])。GetDimToSplit:coalesce 后ndim减少,但最大跨度的维度选择结果一致。PartitionDim/ReduceDimExtent:使用 coalesced 后的 shape/strides 计算gmOffset与numel,产生与原始维度表示相同的gmOffset和numel。CalcSplitBlocks:仅依赖numel和gmOffset填充分块信息,coalesce 不会改变这些关键值。
-
数组越界:所有数组访问均受
MAX_TENSOR_DIMS(=8)或coalescedNdim约束,移位循环的边界coalescedNdim - 1在递减前求值,安全。 -
无死代码、无资源泄漏、无签名不匹配。
审查总结:
- 审查文件数:1
random/random_common/op_host/arch35/random_tiling_arch35.cpp:无问题
- 各优先级发现数:P0: 0, P1: 0, P2: 0, P3: 0
- 整体风险评估:此变更逻辑正确,与现有调用链契约一致,风险低。
⚠️ 已识别出整体风险,但无法提取行内评论,请参考整体评估。


Thanks for your pull-request.
The full list of commands accepted by me can be found at here。
You can get sig-info at here
PR Approval Progress
✅ Congratulations! All modules have met the lgtm and approve requirements.
Module Approval Details
| module | lgtm status | approve status |
|---|---|---|
| ** | ✅ gcw_kUomxQ2l, wangrui_, 宋恺 (3/2) | ✅ gcw_kUomxQ2l (1/1) |
💡 Tip:
- Committer can comment
/approveor/lgtm- Commenting
/approveimplies both code review (lgtm) and intent to merge (approve)


compile


流水线任务触发成功
任务链接 [f05818077bd241448acf07a8df3e286f][流水线指导]
| 任务名称 | 状态 | 日志 | 下载链接 |
|---|---|---|---|
| pre_comment | ✅ SUCCESS | >>>>> | |
| Compile_harmony-infer | ✅ SUCCESS | >>>>> | |
| Compile_X86_monitor_910b | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_X86_monitor_910c | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_X86_monitor_950 | ✅ SUCCESS | >>>>> | >>>>> |
| UT_Test | ✅ SUCCESS | ||
| UT_Test_experimental | ✅ SUCCESS | ||
| UT_Test_kernel | ✅ SUCCESS | ||
| PreSmoke_A900 | ✅ SUCCESS | >>>>> | |
| PreSmoke_ATK_Test_A2 | ✅ SUCCESS | >>>>> | |
| Compile_harmony-infer-chs-math | ✅ SUCCESS | >>>>> |
[2026-07-10 12:53:13] CI执行结束


流水线任务触发成功
任务链接 [d774b4b59f2a4c16a72a4ad15eb53eea][流水线指导]
| 任务名称 | 状态 | 日志 | 下载链接 |
|---|---|---|---|
| codecheck | ✅ SUCCESS | >>>>> | |
| SCA | ✅ SUCCESS | >>>>> | |
| antipoison | ✅ SUCCESS | >>>>> | |
| codecheck_Pr | ✅ SUCCESS | ||
| StaticCheck_codespell_check | ✅ SUCCESS | ||
| StaticCheck_link_validity_check | ✅ SUCCESS | ||
| StaticCheck_resource_existence_check | ✅ SUCCESS | ||
| StaticCheck_tag_closed_check | ✅ SUCCESS | ||
| StaticCheck_markdownlint | ✅ SUCCESS | ||
| codecheck_codestyle | ✅ SUCCESS | >>>>> | |
| codecheck_precommit | ✅ SUCCESS | >>>>> | >>>>> |
[2026-07-10 12:48:12] CI执行结束


/check-pr


The following labels are not ready.
lgtm: Please wait for reviewers to review the code.
approved: Please wait for committers to review the code.


/lgtm
/approve


/approve


描述
InitTensorSliceState增加coalesce对齐PyTorch切分
关联的Issue
测试
文档更新
类型标签