已合并
fix(random): InitTensorSliceState增加coalesce对齐PyTorch切分 #3980
fix(random): InitTensorSliceState增加coalesce对齐PyTorch切分 #3980
已合并
liangtongxue创建于 7月10日
liangtongxue成员
7月10日

描述

InitTensorSliceState增加coalesce对齐PyTorch切分

关联的Issue

测试

文档更新

类型标签

  • Bug修复
  • 新特性
  • 性能优化
  • 文档更新
  • 其他,请描述:
likedislike
Pull Request已成功合入, 合并人@CANN-robot
(感谢 liangtongxue 的贡献)
Lliangtongxue成员
7月10日 创建了 pull request,commit 606d7fb2
atomgit-bot
atomgit-bot
7月10日 评论:

变更摘要

此 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] = 1coalescedStrides[0] = 1coalescedNdim = 1,确保标量或全 1 张量场景下状态合法。
likedislike
atomgit-bot
atomgit-bot
7月10日 评论:

代码审查

基于对 random/random_common/op_host/arch35/random_tiling_arch35.cpp 的详细审查,我已逐行分析了 InitTensorSliceState 中新增的 coalesce 逻辑,并追踪了其对 TensorSliceState 的全部消费者(GetMaxOffsetBytesIs32bitIndexableGetDimToSplitReduceDimExtentPartitionDimCalcSplitBlocks)的影响。

审查结论:本 DIFF 没有引入可报告的缺陷。

具体来说:

  1. size-1 维度移除(第 111-118 行):正确地从 rawShape/rawStrides 中过滤掉大小为 1 的维度,保留其余维度的形状和步长关系。

  2. 全 1 维度边界处理(第 119-123 行):当所有维度都是 size-1(coalescedNdim == 0)时,正确降级为 shape=[1], strides=[1], ndim=1 的一维张量。

  3. 相邻维度合并(第 124-134 行):合并条件 coalescedShape[dim] * coalescedStrides[dim] == coalescedStrides[dim - 1] 正确检测行主序连续维度;合并后的 shape 乘积、stride 继承和元素移位逻辑均正确。循环从最内层向外层推进,合并后的维度能在下一轮迭代中被继续检测,不会遗漏可合并维度对。

  4. 与消费者的契约一致性

    • GetMaxOffsetBytes:coalesce 前后对同一个连续张量返回相同字节偏移(已验证 shape [2,3,4] → coalesced [24])。
    • GetDimToSplit:coalesce 后 ndim 减少,但最大跨度的维度选择结果一致。
    • PartitionDim / ReduceDimExtent:使用 coalesced 后的 shape/strides 计算 gmOffsetnumel,产生与原始维度表示相同的 gmOffsetnumel
    • CalcSplitBlocks:仅依赖 numelgmOffset 填充分块信息,coalesce 不会改变这些关键值。
  5. 数组越界:所有数组访问均受 MAX_TENSOR_DIMS(=8)或 coalescedNdim 约束,移位循环的边界 coalescedNdim - 1 在递减前求值,安全。

  6. 无死代码、无资源泄漏、无签名不匹配


审查总结

  • 审查文件数:1
    • random/random_common/op_host/arch35/random_tiling_arch35.cpp:无问题
  • 各优先级发现数:P0: 0, P1: 0, P2: 0, P3: 0
  • 整体风险评估:此变更逻辑正确,与现有调用链契约一致,风险低。

⚠️ 已识别出整体风险,但无法提取行内评论,请参考整体评估。

likedislike
Lliangtongxue成员
7月10日 修改了pull request 的描述
CANN-robotCANN-robot成员
7月10日 添加了label:cann-cla/yes
CANN-robot
CANN-robot成员
7月10日 评论:

CLA Signature Pass

liangtongxue, thanks for your pull request. All authors of the commits have signed the CLA. 👍

likedislike
CANN-robot
CANN-robot成员
7月10日 评论:

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 /approve or /lgtm
  • Commenting /approve implies both code review (lgtm) and intent to merge (approve)
likedislike
CANN-robotCANN-robot成员
7月10日 将zhou-qilong,wangrui_,rxtfeng,loov1,zhangzijie,jia0liang,songkai111,gitcode-chenjiao,llimwang,gubaocheng,andong_hw,zhanw_coding,gcw_kUomxQ2l,Andy-lb设为评审人
CANN-robotCANN-robot成员
7月10日 将gitcode-chenjiao,gcw_kUomxQ2l,Andy-lb设为审查人
liangtongxue成员
7月10日 评论:

compile

likedislike
Lliangtongxue成员
7月10日 update merge request[project id: 7649531, iid: 3980, commit_id: 0bb18c79b6541849a4747238f6dabf0814a641b1] virtual merging success
CANN-robotCANN-robot成员
7月10日 添加了label:ci-pipeline-running
CANN-robot
CANN-robot成员
7月10日 评论:

流水线任务触发成功
任务链接 [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执行结束

likedislike
CANN-robot
CANN-robot成员
7月10日 评论:

流水线任务触发成功
任务链接 [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执行结束

likedislike
CANN-robotCANN-robot成员
7月10日 删除了label:ci-pipeline-running
CANN-robotCANN-robot成员
7月10日 添加了label:ci-pipeline-passed
RuiWang_成员
7月10日 评论:

/lgtm

likedislike
Lliangtongxue成员
7月10日 关联了issue:[Bug-Report|缺陷反馈]: 随机数 coalesce场景精度问题
liangtongxue成员
7月10日 评论:

/check-pr

likedislike
CANN-robot
CANN-robot成员
7月10日 评论:

The following labels are not ready.

lgtm: Please wait for reviewers to review the code.

approved: Please wait for committers to review the code.

likedislike
songkai111成员
7月10日 评论:

/lgtm
/approve

likedislike
CANN-robotCANN-robot成员
7月10日 添加了label:lgtm
liulun0308成员
7月10日 评论:

/approve

likedislike
CANN-robotCANN-robot成员
7月10日 添加了label:approved
CANN-robotCANN-robot成员
7月10日 关闭了关联的issue
CANN-robotCANN-robot成员
7月10日 合入了pull request