已合并
feat:Support NZ_C0 format variants in tensor serialization #40289
feat:Support NZ_C0 format variants in tensor serialization #40289
已合并
wuyouqi1创建于 7月7日
wuyouqi1
wuyouqi1成员
7月7日
# 【合入来源】 > **如有社区issue,请关联issue链接** > **请勿携带内部流程信息(需求链接、问题单、内部issue等)** - [x] 需求 - [ ] 问题单 - [x] issue/工单 https://gitcode.com/Ascend/pytorch/issues/2629 - [ ] 重构优化 - [ ] 资料更新 # 【修改方案】 A5 (Ascend950+) 砍掉 GE TransData 路径,私有格式(FRACTAL_NZ 系列)仅走 `aclnnNpuFormatCast`。前期只支持 ND2NZ,NZ tensor 的 D2H / 打印等 debug 场景不可用。本 PR 适配 D2H 和打印场景: 1. op_api copy_ d2h 补 NZ→ND auto-cast(CopyKernelOpApi.cpp) 2. copy_ 标记 internal_format_opapi,去除 A5 gen 守卫(npu_native_functions.yaml) 3. Tensor.__repr__ patch:私有格式先 .cpu() 再打印(tensor_repr_patches.py) 4. NZ_C0 序列化兼容(NPUSerialization.cpp、FormatCastKernelNpu.cpp) # 【资料变更】 不涉及 # 【接口变更】 不涉及 # 【功能验证】 测试场景:A5 上 fp16/bf16/int8/int32 四种 dtype NZ tensor 的 D2H、打印、序列化 round-trip、C0 compat load。A2/A3上跳过这些用例,原用例PASS。 实测输出: - A2/A3: ``` # python test_serialization_format.py [W716 10:43:26.785109430 NPUCachingAllocator.cpp:201] Warning: The current CANN and Soc versions require processing for 32 padding size, with memory allocation. (function operator()) [W716 10:43:39.330520500 NPUCachingAllocator.cpp:201] Warning: The current CANN and Soc versions require processing for 32 padding size, with memory allocation. (function operator()) [W716 10:43:52.914299730 NPUCachingAllocator.cpp:201] Warning: The current CANN and Soc versions require processing for 32 padding size, with memory allocation. (function operator()) [W716 10:44:06.520910930 NPUCachingAllocator.cpp:201] Warning: The current CANN and Soc versions require processing for 32 padding size, with memory allocation. (function operator()) [W716 10:44:18.151774430 NPUCachingAllocator.cpp:201] Warning: The current CANN and Soc versions require processing for 32 padding size, with memory allocation. (function operator()) [W716 10:44:38.550773670 NPUCachingAllocator.cpp:201] Warning: The current CANN and Soc versions require processing for 32 padding size, with memory allocation. (function operator()) [W716 10:44:50.137489630 NPUCachingAllocator.cpp:201] Warning: The current CANN and Soc versions require processing for 32 padding size, with memory allocation. (function operator()) [W716 10:44:50.138126100 TensorFactories.cpp:348] Warning: Cannot create tensor with interal format while allow_internel_format=False, tensor will be created with base format. (function operator()) [W716 10:45:16.799575350 NPUCachingAllocator.cpp:201] Warning: The current CANN and Soc versions require processing for 32 padding size, with memory allocation. (function operator()) [W716 10:45:30.578267770 NPUCachingAllocator.cpp:201] Warning: The current CANN and Soc versions require processing for 32 padding size, with memory allocation. (function operator()) [W716 10:45:30.578893090 TensorFactories.cpp:348] Warning: Cannot create tensor with interal format while allow_internel_format=False, tensor will be created with base format. (function operator()) [W716 10:45:55.367333900 NPUCachingAllocator.cpp:201] Warning: The current CANN and Soc versions require processing for 32 padding size, with memory allocation. (function operator()) [W716 10:46:08.893237850 NPUCachingAllocator.cpp:201] Warning: The current CANN and Soc versions require processing for 32 padding size, with memory allocation. (function operator()) [W716 10:46:08.894251630 TensorFactories.cpp:348] Warning: Cannot create tensor with interal format while allow_internel_format=False, tensor will be created with base format. (function operator()) [W716 10:46:33.330123970 NPUCachingAllocator.cpp:201] Warning: The current CANN and Soc versions require processing for 32 padding size, with memory allocation. (function operator()) .sss ---------------------------------------------------------------------- Ran 4 tests in 200.025s OK (skipped=3) ``` - A5: ``` # python test_serialization_format.py s.Warning: The current version of the file storing weights is old, and it is relanded due to internal bug of torch and compatibility issue. We will deprecate the loading support for this type of file in the future, please use newer torch to re-store the weight file. .Warning: The current version of the file storing weights is old, and it is relanded due to internal bug of torch and compatibility issue. We will deprecate the loading support for this type of file in the future, please use newer torch to re-store the weight file. . ---------------------------------------------------------------------- Ran 4 tests in 26.036s OK (skipped=1) ``` # 【CheckList】 - [x] 代码注释完备,正确记录错误日志 - [x] 代码实现进行了返回值、空指针等校验 - [x] PR标题正确使用类型标签,如:feat、fix、refactor、docs、test等 - [x] PR持续集成流水线(CI)执行通过,代码检查无异常
likedislike
Pull Request已成功合入, 合并人@ascend-robot
(感谢 wuyouqi1 的贡献)
wuyouqi1wuyouqi1成员
7月7日 创建了 pull request,commit 08389979
atomgit-bot
atomgit-bot
7月7日 评论:

变更摘要

此 PR 主要扩展了 NPU 张量序列化对 FRACTAL_NZ_C0 系列格式变体的支持,同时修复了在 A5 aclnn-only 环境下内部格式(如 FRACTAL_NZ)张量在拷贝和打印时的兼容性问题。核心思路是在序列化映射表、格式转换内核及 OpApi 拷贝路径中补齐对 NZ_C0 变体的处理,并为 NPU 私有格式张量的 __repr__ 方法新增补丁以绕过 internal_format 守卫。

主要改动

  • 序列化映射表扩展:在 NPUSerialization.cppFORMAT_INFO 中新增 FRACTAL_NZ_C0_16FRACTAL_NZ_C0_32FRACTAL_NZ_C0_2FRACTAL_NZ_C0_4FRACTAL_NZ_C0_8 到对应 ACL_FORMAT_* 的映射,使这五种 NZ_C0 变体格式可被正常序列化与反序列化。

  • OpApi 拷贝路径的内部格式转换:在 CopyKernelOpApi.cppNPUNativeOpApiFunctions::copy_ 中,当源张量位于 NPU 且并非基格式时,先通过 FormatCastHelper::ApplyBaseFormatTensorBy 将源张量转为 ND 格式再执行 copy_d2h_baseformat_opapi,以兼容 A5 aclnn-only 环境下 aclnnInplaceCopy 不接受内部格式的限制。

  • copy_ 操作的 internal_format_opapi 标志启用:在 npu_native_functions.yaml 中为 copy_ 添加 internal_format_opapi: True,配合上述格式转换逻辑确保 copy 操作能正确处理内部格式张量。

  • 格式转换内核的额外数据类型修正:在 FormatCastKernelNpu.cppMaybeUseAclnnNpuFormatCast 中增加判断——当处于 IsAclnnOnly() 模式且未指定 customize_dtype 时,对元素大小 ≥4 字节的类型(如 int32/float32)强制使用 ACL_FLOAT16 作为 customizeAcltype,以适配 A5 平台要求。

  • NPU 私有格式张量的打印补丁:新增 tensor_repr_patches.py,通过 PatchManager 注册对 torch.Tensor.__repr__ 的补丁:当张量在 NPU 设备上且格式非 ND 时,自动执行 .cpu() 转换为基格式后再打印,避免 _tensor_str 中的 cat/stack 触发 internal_format 守卫。

likedislike
atomgit-bot
atomgit-bot
7月7日 评论:

代码审查

关闭总结

文件 审查结果
torch_npu/_init/patches/tensor_repr_patches.py 3 个问题 (1×P1, 2×P3)
torch_npu/csrc/aten/common/FormatCastKernelNpu.cpp 无问题
torch_npu/csrc/aten/npu_native_functions.yaml 无问题
torch_npu/csrc/aten/ops/op_api/CopyKernelOpApi.cpp 无问题
torch_npu/csrc/core/NPUSerialization.cpp 无问题
  • P1: 1 个 — _orig_repr 可能不接受 tensor_contents 关键字参数, 存在 __repr__ 崩溃风险
  • P2: 0 个
  • P3: 2 个 — 过宽异常捕获无日志 + __repr__ 热路径上的不必要 import

总体风险判断: 中低。核心 C++ 变更(序列化格式映射、格式转换、internal_format_opapi 白名单)逻辑正确且与现有代码风格一致。唯一的实质性风险来自新增的 Python tensor_repr_patches.pytensor_contents 参数兼容性问题, 需确认目标 PyTorch 版本是否支持。

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

likedislike
ascend-robotascend-robot成员
7月7日 添加了label:stat/needs-squash
此处折叠了359条消息 查看更多
ascend-robotascend-robot成员
9 天前 添加了label:lgtm
ascend-robotascend-robot成员
9 天前 合入了pull request
ascend-robot
ascend-robot成员
9 天前 评论:

Pull Request 已合并或已关闭。

If you want to solve this problem, you can click here to do it in the FAQs.

likedislike
ascend-robot
ascend-robot成员
9 天前 评论:

In response to this:

/sync v2.9.0 v2.10.0 v2.12.0 v2.11.0 master

@wuyouqi1

同步操作执行结果:

Branch Status Pull Request
v2.9.0 同步失败:请手动创建 PR 进行同步,我们会继续完善分支之间同步操作,尽量避免同步失败的情况
v2.10.0 同步失败:请手动创建 PR 进行同步,我们会继续完善分支之间同步操作,尽量避免同步失败的情况
v2.12.0 同步失败:请手动创建 PR 进行同步,我们会继续完善分支之间同步操作,尽量避免同步失败的情况
v2.11.0 同步失败:请手动创建 PR 进行同步,我们会继续完善分支之间同步操作,尽量避免同步失败的情况
master 同步失败:请手动创建 PR 进行同步,我们会继续完善分支之间同步操作,尽量避免同步失败的情况
likedislike
ascend-robot
ascend-robot成员
9 天前 评论:
流水线 pytorch_gitcode_PR_multiVersion#13837 [ commitID:425203e0 ] 运行失败
likedislike