Merged
fix: JIT编译include路径CANN优先于torch_npu修复新dtype枚举不可见报错 #13155
huangbolinAdd on Sep 28
fix: JIT编译include路径CANN优先于torch_npu修复新dtype枚举不可见报错 #13155
Merged
Total: 2 files changed+14-7
| @@ -19,6 +19,9 @@ | |||
| 19 | // 必须先于任何 torch_npu 头文件引入 acl 基础头文件:torch_npu 自带 third_party/acl 版本较旧, | 19 | // 必须先于任何 torch_npu 头文件引入 acl 基础头文件:torch_npu 自带 third_party/acl 版本较旧, |
| 20 | // 缺少 ACL_FLOAT8_E5M2/ACL_FLOAT4_E2M1 等新 dtype 常量。若在其之后 include,旧头文件已设置 | 20 | // 缺少 ACL_FLOAT8_E5M2/ACL_FLOAT4_E2M1 等新 dtype 常量。若在其之后 include,旧头文件已设置 |
| 21 | // 同名 include guard(INC_EXTERNAL_ACL_ACL_BASE_H_),新版 CANN 头文件会被跳过导致编译失败。 | 21 | // 同名 include guard(INC_EXTERNAL_ACL_ACL_BASE_H_),新版 CANN 头文件会被跳过导致编译失败。 |
| 22 | +// 注意:本文件的 include 顺序只是必要条件之一;<acl/acl_base.h> 能否解析到环境上实际安装的 | ||
| 23 | +// CANN 头文件,还取决于编译期 -I 搜索顺序。OpBuilder.include_paths() 已将 CANN 路径排在 | ||
| 24 | +// torch_npu 路径之前,两者缺一不可——仅调整 include 顺序而 -I 顺序未变时,仍会命中旧副本。 | ||
| 22 | #include <acl/acl_base.h> | 25 | #include <acl/acl_base.h> |
| 23 | #include <torch_npu/csrc/framework/utils/OpAdapter.h> | 26 | #include <torch_npu/csrc/framework/utils/OpAdapter.h> |
| 24 | #include <dlfcn.h> | 27 | #include <dlfcn.h> |
| @@ -14,9 +14,10 @@ import os | |||
| 14 | import shutil | 14 | import shutil |
| 15 | from abc import ABC, abstractmethod | 15 | from abc import ABC, abstractmethod |
| 16 | from typing import List, Union | 16 | from typing import List, Union |
| 17 | + | ||
| 17 | import torch | 18 | import torch |
| 18 | -from torch.utils.cpp_extension import load | ||
| 19 | from torch.library import Library | 19 | from torch.library import Library |
| 20 | +from torch.utils.cpp_extension import load | ||
| 20 | 21 | ||
| 21 | ASCEND_HOME_PATH = "ASCEND_HOME_PATH" | 22 | ASCEND_HOME_PATH = "ASCEND_HOME_PATH" |
| 22 | _as_library = None | 23 | _as_library = None |
| @@ -59,10 +60,9 @@ class OpBuilder(ABC): | |||
| 59 | self._ensure_initialized() | 60 | self._ensure_initialized() |
| 60 | 61 | ||
| 61 | def get_cann_path(self): | 62 | def get_cann_path(self): |
| 62 | - if ASCEND_HOME_PATH in os.environ and os.path.exists( | 63 | + cann_path = os.environ.get(ASCEND_HOME_PATH) |
| 63 | - os.environ[ASCEND_HOME_PATH] | 64 | + if cann_path and os.path.exists(cann_path): |
| 64 | - ): | 65 | + return cann_path |
| 65 | - return os.environ[ASCEND_HOME_PATH] | ||
| 66 | return os.path.dirname(os.path.dirname(self._torch_npu_path)) | 66 | return os.path.dirname(os.path.dirname(self._torch_npu_path)) |
| 67 | 67 | ||
| 68 | @property | 68 | @property |
| @@ -124,14 +124,18 @@ class OpBuilder(ABC): | |||
| 124 | inc = os.path.join(vendor_dir, "op_api", "include") | 124 | inc = os.path.join(vendor_dir, "op_api", "include") |
| 125 | if os.path.isdir(inc): | 125 | if os.path.isdir(inc): |
| 126 | paths.append(inc) | 126 | paths.append(inc) |
| 127 | + # CANN 路径必须先于 torch_npu 路径:torch_npu 自带的 third_party/acl 版本较旧, | ||
| 128 | + # 且与新版 CANN 头文件共用同名 include guard,若排在前面会因 -I 搜索顺序先命中旧头, | ||
| 129 | + # 环境上实际安装的 CANN 头文件(含 ACL_FLOAT8_E5M2 等新枚举)被 guard 抑制导致编译报错。 | ||
| 130 | + # vendor op_api/include 保持最优先,run 包的增强算子 aclnn 头文件需覆盖 CANN 同名头。 | ||
| 127 | paths.extend( | 131 | paths.extend( |
| 128 | [ | 132 | [ |
| 133 | + os.path.join(self._cann_path, "include"), | ||
| 134 | + os.path.join(self._cann_path, "include/aclnnop"), | ||
| 129 | os.path.join(self._torch_npu_path, "include"), | 135 | os.path.join(self._torch_npu_path, "include"), |
| 130 | os.path.join(self._torch_npu_path, "include/third_party/hccl/inc"), | 136 | os.path.join(self._torch_npu_path, "include/third_party/hccl/inc"), |
| 131 | os.path.join(self._torch_npu_path, "include/third_party/acl/inc"), | 137 | os.path.join(self._torch_npu_path, "include/third_party/acl/inc"), |
| 132 | os.path.join(self._torch_npu_path, "include/third_party/op-plugin"), | 138 | os.path.join(self._torch_npu_path, "include/third_party/op-plugin"), |
| 133 | - os.path.join(self._cann_path, "include"), | ||
| 134 | - os.path.join(self._cann_path, "include/aclnnop"), | ||
| 135 | os.path.join(self._package_path, "common"), | 139 | os.path.join(self._package_path, "common"), |
| 136 | ] | 140 | ] |
| 137 | ) | 141 | ) |