已合并
fix: MallocOutputTensor支持零长度输出跳过内存申请 #5134
duhua创建于 21 天前
fix: MallocOutputTensor支持零长度输出跳过内存申请 #5134
已合并
Pull Request已成功合入, 合并人@CANN-robot
(感谢 duhua 的贡献)21 天前 添加了label:enhancement
atomgit-bot
21 天前 评论:
21 天前 评论:
变更摘要
本 PR 修复了 HostCPU 输出申请接口 HostCpuOpExecutionContext::MallocOutputTensor 在逻辑大小为 0(storage shape 含 0 维的空 tensor,如 {2,0,4})时仍调用分配器、要求返回非空地址与大于 0 的大小而导致申请失败返回 nullptr 的问题,该问题会造成 AICPU 常量折叠等产生空输出的算子执行失败。修复方式是在 logical_size == 0 时跳过内存分配,清空输出地址与数据大小并保留已设置的 shape/format/dtype,直接返回有效的输出 Tensor;同时新增 UT 用例覆盖零长度输出及复用历史 buffer 后的零长度场景。
主要改动
- 零长度输出短路处理: 在
HostCpuOpExecutionContext::MallocOutputTensor中新增logical_size == 0分支,通过MutableTensorData()调用SetAddr(nullptr, nullptr)、SetSize(0U)与SetPlacement(kOnHost),跳过分配器并直接返回输出 Tensor。 - 保留张量元信息: 该分支仅清空地址与数据大小,保留已设置的 shape/format/dtype,使零长度输出仍返回非空且元信息完整的 Tensor。
- 新增零长度输出 UT: 在
host_cpu_op_execution_context_unittest.cc新增MallocOutputTensorZeroLengthOk用例,验证零长度输出返回非空 Tensor 且GetAddr()为nullptr、GetSize()为 0、placement 为kOnHost。 - 新增复用历史 buffer 后的零长度 UT: 新增
MallocOutputTensorZeroLengthAfterMallocOk用例,验证先申请非零长度 buffer 后再以零长度 shape 申请时,地址与大小被正确重置。


不准确?
atomgit-bot
21 天前 评论:
21 天前 评论:
21 天前 添加了label:cann-cla/yes
CANN-robot
21 天前 评论:
21 天前 评论:
CANN-robot
21 天前 评论:
21 天前 评论:
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.
You can self-configure the PR merge rules for this repository. For more details, please refer to Here.
For more, you also can visit HICANN.
PR Approval Progress
✅ Congratulations! All modules have met the lgtm and approve requirements.
Module Approval Details
| module | lgtm status | approve status |
|---|---|---|
| repo-cann/ge | ✅ yangyongqiang0606, kobemini (2/2) | ✅ yangyongqiang0606 (1/1) |
💡 Tip:
- Committer can comment
/approveor/lgtm- Commenting
/approveimplies both code review (lgtm) and intent to merge (approve)


21 天前 将yangyongqiang0606,kobemini,ClarkXie,tangqunzhang,xchu42,hugo111,lining23666,tang-haojie,depeng1994,wqtshg_wt,peiyang,shengnan666,zhangfan_hanq,zhanj,zhujingjing,Mexyy,wangxiaotian995,stevenaw0,yuht9设为评审人
21 天前 将yangyongqiang0606,kobemini,ClarkXie,tangqunzhang,xchu42,hugo111,lining23666,tang-haojie,depeng1994,wqtshg_wt,peiyang,shengnan666,zhangfan_hanq,zhanj,zhujingjing,Mexyy,wangxiaotian995,stevenaw0,yuht9设为评审人
21 天前 将yangyongqiang0606,kobemini,ClarkXie,tangqunzhang,xchu42,hugo111,lining23666,tang-haojie,depeng1994,wqtshg_wt,peiyang,shengnan666,zhangfan_hanq,zhanj,zhujingjing,Mexyy,wangxiaotian995,stevenaw0,yuht9设为审查人
21 天前 将yangyongqiang0606,kobemini,ClarkXie,tangqunzhang,xchu42,hugo111,lining23666,tang-haojie,depeng1994,wqtshg_wt,peiyang,shengnan666,zhangfan_hanq,zhanj,zhujingjing,Mexyy,wangxiaotian995,stevenaw0,yuht9设为审查人
duhua
21 天前 评论:
21 天前 评论:
compile


21 天前 预合并成功(commit_id: 9bdc0f09dec99ac484972286054e41df50bfea9e)
21 天前 修改标题为 “fix: MallocOutputTensor支持零长度输出跳过内存申请”,原标题为“【PR】: 简要描述”
CANN-robot
21 天前 评论:
21 天前 评论:
| 🚀 CI 流水线已启动 |
|---|
| 📋 执行详情: 点击查看流水线 |


21 天前 添加了label:ci-pipeline-running
21 天前 修改了pull request 的描述
21 天前 删除了label:ci-pipeline-running
21 天前 添加了label:ci-pipeline-passed
21 天前 修改了pull request 的描述
21 天前 修改了pull request 的描述
GengChao
20 天前 评论:
20 天前 评论:
/lgtm


yangyongqiang
20 天前 评论:
20 天前 评论:
/approve


20 天前 添加了label:lgtmapproved
20 天前 合入了pull request
Pull Request
描述
HostCPU输出申请接口HostCpuOpExecutionContext::MallocOutputTensor在逻辑大小为0(storage shape含0维的空tensor,如{2,0,4})时仍调用分配器,导致申请失败返回nullptr,常量折叠等产生空输出的算子执行失败。
修复:logical_size为0时清空输出地址与数据大小、保留已设置的shape/format/dtype,直接返回有效输出Tensor,跳过内存分配;同时新增UT用例覆盖零长度输出及复用历史buffer后的零长度场景。
变更类型
请选择本次引入的变更类型:
关联的Issue
如何测试
描述测试此变更的步骤和前提条件:
核对清单
其他信息
无