已合并
Revert "fix: fix aclnn npu_format_cast to respect allow_internal_format=False" #39259
wangzili121创建于 6月25日
Revert "fix: fix aclnn npu_format_cast to respect allow_internal_format=False" #39259
已合并
wangzili121创建于 6月25日
2 个文件变更+9-26
Mtest/npu/test_c10d.py+9-13
@@ -171,19 +171,15 @@ class ProcessGroupHCCLTest(TestCase):
171 171 
172class ComputeBucketAssignmentTest(TestCase):172class ComputeBucketAssignmentTest(TestCase):
173 def test_single_limit_single_dtype(self):173 def test_single_limit_single_dtype(self):
174- torch_npu.npu.config.allow_internal_format = True174+ tensors = [
175- try:175+ torch_npu.npu_format_cast(torch.empty([100, 1], dtype=torch.float).npu(), Format.NZ),
176- tensors = [176+ torch.empty([200], dtype=torch.float).npu(),
177- torch_npu.npu_format_cast(torch.empty([100, 1], dtype=torch.float).npu(), Format.NZ),177+ torch.empty([100], dtype=torch.float).npu(),
178- torch.empty([200], dtype=torch.float).npu(),178+ torch.empty([50], dtype=torch.float).npu(),
179- torch.empty([100], dtype=torch.float).npu(),179+ ]
180- torch.empty([50], dtype=torch.float).npu(),180+ result = dist._compute_bucket_assignment_by_size(tensors, [1792 * 4 + 1])
181- ]181+ expec_result = ([[0, 1, 2, 3]], [7169])
182- result = dist._compute_bucket_assignment_by_size(tensors, [1792 * 4 + 1])182+ self.assertEqual(expec_result, result)
183- expec_result = ([[0, 1, 2, 3]], [7169])
184- self.assertEqual(expec_result, result)
185- finally:
186- torch_npu.npu.config.allow_internal_format = False
187 183 
188 def test_single_limit_multi_dtype(self):184 def test_single_limit_multi_dtype(self):
189 tensors = [185 tensors = [
Mtorch_npu/csrc/aten/common/FormatCastKernelNpu.cpp+0-13
@@ -8,7 +8,6 @@
8#include "torch_npu/csrc/core/NPUStorageImpl.h"8#include "torch_npu/csrc/core/NPUStorageImpl.h"
9#include "torch_npu/csrc/core/npu/NpuVariables.h"9#include "torch_npu/csrc/core/npu/NpuVariables.h"
10#include "torch_npu/csrc/core/npu/GetCANNInfo.h"10#include "torch_npu/csrc/core/npu/GetCANNInfo.h"
11-#include "torch_npu/csrc/framework/interface/EnvVariables.h"
12#include "torch_npu/csrc/aten/CustomFunctions.h"11#include "torch_npu/csrc/aten/CustomFunctions.h"
13#include "torch_npu/csrc/custom_dtype/Init.h"12#include "torch_npu/csrc/custom_dtype/Init.h"
14#include "third_party/op-plugin/op_plugin/utils/op_api_common.h"13#include "third_party/op-plugin/op_plugin/utils/op_api_common.h"
@@ -62,21 +61,9 @@ static bool ShouldFallbackNzToNd(const at::Tensor& self, int64_t acl_format)
62 return false;61 return false;
63}62}
64 63 
65-static int64_t MaybeDowngradeInternalFormat(int64_t acl_format)
66-{
67- if (env::CheckForbidInternalFormat() &&
68- !FormatHelper::IsBaseFormatType(static_cast<aclFormat>(acl_format))) {
69- TORCH_WARN_ONCE("Cannot create tensor with internal format while allow_internal_format=False, "
70- "tensor will be created with base format.");
71- return static_cast<int64_t>(FormatHelper::GetBaseFormat(static_cast<aclFormat>(acl_format)));
72- }
73- return acl_format;
74-}
75- 
76std::tuple<bool, int64_t, c10::SmallVector<int64_t, SIZE>> MaybeUseAclnnNpuFormatCast(const at::Tensor& src,64std::tuple<bool, int64_t, c10::SmallVector<int64_t, SIZE>> MaybeUseAclnnNpuFormatCast(const at::Tensor& src,
77 int64_t acl_format, c10::optional<int64_t> customize_dtype, c10::optional<int64_t> input_dtype)65 int64_t acl_format, c10::optional<int64_t> customize_dtype, c10::optional<int64_t> input_dtype)
atomgit-bot
atomgit-botatomgit-bot6月25日

🟠 High Priority

问题

本 PR 删除了 MaybeDowngradeInternalFormat 函数及其在 MaybeUseAclnnNpuFormatCast 中的调用。该函数是 ACLNN 格式转换路径中唯一 执行 allow_internal_format=False 检查的守卫。

证据链

  1. 删除的函数 (MaybeDowngradeInternalFormat) 在 allow_internal_format=False 时,将内部格式(如 NZ、NC1HWC0 等)降级为基础格式(ND),从而阻止内部格式张量的创建。

  2. ACLNN 路径绕过 TensorFactories 的检查:ACLNN 路径通过 format_cast_impl_out_npu_aclnncreate_tensor_with_format_and_shape 直接分配存储并设置格式描述符,完全绕过 TensorFactories::unsafe_empty_with_format 中的 CheckForbidInternalFormat() 检查(该检查仍在 line 421)。

  3. 旧 aclop 回退路径仍保留检查npu_format_cast_implApplyTensorWithFormatunsafe_empty_with_formatCheckForbidInternalFormat,但 ACLNN 路径不再受保护。

  4. ACLNN-only 设备上彻底失效:在 c10_npu::IsAclnnOnly() 的设备上,没有 aclop 回退路径,allow_internal_format=False 设置对格式转换完全无效。

影响

建议:恢复 MaybeDowngradeInternalFormat 函数及其在 MaybeUseAclnnNpuFormatCast 中的调用,或者作为替代方案,在 create_tensor_with_format_and_shape 中添加等价的 CheckForbidInternalFormat() 检查,确保 ACLNN 路径与 aclop 路径行为一致。推荐恢复原修复:在 MaybeUseAclnnNpuFormatCast 入口处对 acl_format 调用 MaybeDowngradeInternalFormat

likedislike
78{66{
79- acl_format = MaybeDowngradeInternalFormat(acl_format);
80 const static auto GetFormatFuncAddr = GetOpApiFuncAddr("aclnnNpuFormatCastCalculateSizeAndFormat");67 const static auto GetFormatFuncAddr = GetOpApiFuncAddr("aclnnNpuFormatCastCalculateSizeAndFormat");
81 const static auto FormatCastFuncAddr = GetOpApiFuncAddr("aclnnNpuFormatCast");68 const static auto FormatCastFuncAddr = GetOpApiFuncAddr("aclnnNpuFormatCast");
82 69