Files
TorchNPU/test/custom_ops
wuyouqi1 38c1f4290f fix: downgrade internal-format targets to base format on the aclnn path when ALLOW_INTERNAL_FORMAT=disable [master]
Co-authored-by: wuyouqi1<wuyouqi1@h-partners.com>



# message auto-generated for no-merge-commit merge:
!45500 merge fix/aclnn-format-cast-forbid-internal-format-master into master

fix: downgrade internal-format targets to base format on the aclnn path when ALLOW_INTERNAL_FORMAT=disable [master]

Created-by: wuyouqi1
Commit-by: wuyouqi1
Merged-by: ascend-robot
Description: # 【合入来源】
- [ ] 需求
- [ ] 问题单
- [x] issue/工单 https://gitcode.com/Ascend/pytorch/issues/4389
- [ ] 重构优化
- [ ] 资料更新

# 【修改方案】
allow_internal_format=False 时,npu_format_cast 走 aclnn 路径(7302fba05 引入)未检查该开关:目标为内部格式(FRACTAL_NZ/NC1HWC0/FRACTAL_Z)时直接创建私有格式 tensor,绕过了 aclop 分配层守卫(TensorFactories.cpp unsafe_empty_with_format),与 aclop 路径行为不一致,导致 test_allow_internal_format_false_with_npu_format_cast 类用例失败。

修复:在 MaybeUseAclnnNpuFormatCast 的 IsAclnnFormatCastSupported 分支入口增加降级守卫——allow_internal_format=False 且目标为非 base 格式时,将目标降级为 GetBaseFormat 并 TORCH_WARN_ONCE,与 aclop 路径行为对齐。

关键设计:cast_into_existing 豁免——目标格式等于 src tensor 自身格式时不降级(tensor 重载 npu_format_cast_(self, Tensor src) 中该值恒为 True,属预期:该重载语义为将 src 数据写入已存在 tensor,目标格式是既成事实;若降级会破坏 nz_dst.copy_(nd_src) 后 dst 保持 NZ 布局的既有行为)。int 重载经 same-format 早退后该值恒为 False,守卫正常生效。950 的 IsAclnnOnly 分支不经此守卫,不受影响。

注:此前 !39022 曾在 MaybeUseAclnnNpuFormatCast 入口无条件降级(无 cast_into_existing 豁免),因破坏既有行为被 !39259 回退。本 MR 为带豁免的修正版,同步自 v2.7.1 MR !45183。

# 【资料变更】
op-plugin 仓 MR !5771(allow_internal_format 影响范围说明)

# 【接口变更】
不涉及

# 【功能验证】
UT:test/custom_ops/test_npu_format_cast.py TestNpuFormatCastForbidInternalFormat(降级 4 组格式断言 + base 目标不受影响 + 开关开启对照 + copy_ 保持已存在 NZ tensor 格式)

See merge request: Ascend/pytorch!45500
2026-08-29 11:57:14 +08:00
..
2026-06-08 19:54:52 +08:00
2025-01-27 03:20:33 +00:00
2023-11-23 08:43:29 +00:00
2023-11-23 06:50:03 +00:00