Co-authored-by: Dring<17737727613@163.com> # message auto-generated for no-merge-commit merge: !44990 merge master into master [master][bugfix]cann and pta header mixing bulid bugfix Created-by: Dring Commit-by: Dring Merged-by: ascend-robot Description: <!-- PR描述模板更新日期:20260203 --> # 【合入来源】 > <font color="red">**如有社区issue,请关联issue链接**</font>\ > <font color="red">**请勿携带内部流程信息(需求链接、问题单、内部issue等)**</font> https://gitcode.com/Ascend/pytorch/issues/3991 - [✕ ] 需求 - [ ✓] 问题单 - [ ✓] issue/工单 - [ ✓] 重构优化 - [✕ ] 资料更新 # 【修改方案】 > 请描述修改内容的具体实现,涉及哪些组件之间进行交互,可以用1、2、3、...进行罗列\ > 如果是需求或者重构类的PR,需要补充详细设计文档(说明上下游组件关系、时序图、类图、DFX能力等内容) 问题现象: PTA 26.1.0 仓库中的 third_party/acl 头文件来自 CANN 9.1.0,而编译环境安装的是 CANN 9.0.0。当同一个编译单元同时包含两套版本的 ACL 头文件时,会出现类似以下错误:aclmdlRITask 未声明、未定义或类型不匹配 aclmdlRITask 是 CANN 9.1.0 头文件所依赖的定义,但 CANN 9.0.0 对应头文件中不存在该定义或定义不兼容。 根因分析: 1 当前头文件查找路径不统一:PTA 源码中大量使用以下写法 #include "third_party/acl/inc/acl/acl_mdl.h" 因为编译命令中包含 PTA 项目根目录,这种写法会直接命中 PTA 26.1.0 仓库内由 CANN 9.1.0 导入的头文件:torch_npu/third_party/acl/inc/acl/acl_mdl.h 但该头文件内部使用的是标准 SDK 相对路径:#include "acl/acl_base.h" 编译器无法相对 acl_mdl.h 所在目录找到 acl/acl_base.h,因此转而按照全部 -I 目录依次搜索。如果 CANN 9.0.0 的 include 路径排在 PTA 内置 ACL 路径前面,就会命中:CANN-9.0.0/include/acl/acl_base.h 同一个翻译单元最终形成: acl_mdl.h -> PTA third_party 中的 CANN 9.1.0 版本 acl_base.h -> 环境 CANN 9.0.0 版本 这是一种“首层头文件由源码路径固定版本、传递头文件由 -I 顺序选择版本”的混合查找模式。 2 问题不局限于 acl_mdl.h 同样风险存在于所有能够继续 include 其他 CANN 头文件的入口,包括: acl/... aml/... profiling/... graph/... ge/... op_proto/... 因此不能只修复出现错误的某一个头文件,也不能只替换 acl_mdl.h。必须统一整个 CANN 头文件族的查找规则。 修改目标: 修改后需要满足 源码不再包含 third_party/acl/inc/... 这种仓库物理路径。 ACL、AML、profiling 等头文件全部通过统一 include root 查找。 一个编译 target 对 CANN 头文件只选择一套版本。 CANN 9.0.0 环境构建时,首层和传递头文件必须全部命中 CANN 9.0.0。 使用 PTA 内置头文件构建时,首层和传递头文件必须全部命中 PTA 内置的同一套版本。 wheel、libtorch_npu、C++ Extension 和 Inductor/AOT 使用同一目录契约 修改方案: 1、代码中使用 PTA 内置 ACL 头文件的地方,删除 third_party/acl/inc/ 物理路径前缀,统一改成从 include root 查找 2、打包或安装时,把 third_party/acl/inc 下需要公开的目录按原相对结构复制到安装产物的公共 include 根目录 # 【资料变更】 > 请确认是否涉及资料变更。如涉及,需要在PR中体现,并简要说明修改内容。如不涉及,需填写“不涉及” 不涉及 # 【接口变更】 > 请确认是否涉及跨代码仓或者客户面可见的接口变更。如涉及,需要详细说明接口以及对应的变更内容,同时需要在资料中体现。如不涉及,需填写“不涉及” 不涉及 # 【功能验证】 > 说明测试场景,测试方法。如果本次测试方式与常规单元测试不同,请详细说明您的测试步骤\ > 新增/变更内容是否已新增/适配UT测试用例看护,并补充测试自验证截图 cann 9.0.0 + pta 26.1.0的vllm-ascend安装验证成功  # 【CheckList】 > PR提交人对以下CheckList自检项进行全量自检,自检通过或不涉及,均修改 [ ] 为 [x] - [✓ ] 代码注释完备,正确记录错误日志 - [✕ ] 代码实现进行了返回值、空指针等校验 - [✓ ] PR标题正确使用类型标签,如:feat、fix、refactor、docs、test等 - [✓ ] PR持续集成流水线(CI)执行通过,代码检查无异常 See merge request: Ascend/pytorch!44990
110 lines
4.5 KiB
Python
110 lines
4.5 KiB
Python
import os
|
|
import shutil
|
|
import subprocess
|
|
import ctypes
|
|
import torch
|
|
import torch.utils.cpp_extension
|
|
|
|
import torch_npu
|
|
from torch_npu.testing.testcase import TestCase, run_tests
|
|
|
|
|
|
PYTORCH_INSTALL_PATH = os.path.dirname(os.path.realpath(torch.__file__))
|
|
PYTORCH_NPU_INSTALL_PATH = os.path.dirname(os.path.realpath(torch_npu.__file__))
|
|
|
|
|
|
def create_build_path(build_directory):
|
|
if os.path.exists(build_directory):
|
|
shutil.rmtree(build_directory, ignore_errors=True)
|
|
os.makedirs(build_directory, exist_ok=True)
|
|
|
|
|
|
def build_stub(base_dir):
|
|
build_stub_cmd = [
|
|
"sh",
|
|
os.path.join(base_dir, 'third_party/acl/libs/build_stub.sh'),
|
|
# Build the stub and Extension against the same installed header root.
|
|
os.path.join(PYTORCH_NPU_INSTALL_PATH, 'include'),
|
|
]
|
|
if subprocess.call(build_stub_cmd) != 0:
|
|
raise RuntimeError('Failed to build stub: {}'.format(build_stub_cmd))
|
|
|
|
|
|
class TestPluggableAllocator(TestCase):
|
|
module = None
|
|
new_alloc = None
|
|
build_directory = "allocator/build"
|
|
|
|
@classmethod
|
|
def setUpClass(cls):
|
|
# Build Extension
|
|
TEST_FILE_DIR = os.path.dirname(os.path.abspath(__file__))
|
|
TEST_DIR = os.path.dirname(TEST_FILE_DIR)
|
|
BASE_DIR = os.path.dirname(TEST_DIR)
|
|
build_stub(BASE_DIR)
|
|
create_build_path(cls.build_directory)
|
|
CANN_LIB_PATH = os.path.join(BASE_DIR, 'third_party/acl/libs')
|
|
extra_ldflags = []
|
|
extra_ldflags.append("-lascendcl")
|
|
extra_ldflags.append(f"-L{CANN_LIB_PATH}")
|
|
extra_ldflags.append("-lc10")
|
|
extra_ldflags.append(f"-L{PYTORCH_INSTALL_PATH}")
|
|
extra_include_paths = [os.path.join(TEST_DIR, "cpp_extensions")]
|
|
extra_include_paths.append(os.path.join(PYTORCH_NPU_INSTALL_PATH, 'include'))
|
|
|
|
cls.module = torch.utils.cpp_extension.load(
|
|
name="pluggable_allocator_extensions",
|
|
sources=[
|
|
os.path.join(TEST_DIR, "cpp_extensions", "pluggable_allocator_extensions.cpp")
|
|
],
|
|
extra_include_paths=extra_include_paths,
|
|
extra_cflags=["-g"],
|
|
extra_ldflags=extra_ldflags,
|
|
build_directory=cls.build_directory,
|
|
verbose=True,
|
|
)
|
|
|
|
def test_pluggable_allocator(self):
|
|
os_path = os.path.join(TestPluggableAllocator.build_directory, 'pluggable_allocator_extensions.so')
|
|
# Load the allocator
|
|
TestPluggableAllocator.new_alloc = torch_npu.npu.memory.NPUPluggableAllocator(os_path, 'my_malloc', 'my_free')
|
|
# Swap the current allocator
|
|
torch_npu.npu.memory.change_current_allocator(TestPluggableAllocator.new_alloc)
|
|
# This will allocate memory in the device using the new allocator
|
|
self.assertFalse(self.module.check_custom_allocator_used())
|
|
npu_tensor = torch.zeros(10, device='npu')
|
|
cpu_tensor = torch.zeros(10)
|
|
self.assertRtolEqual(npu_tensor.cpu().numpy(), cpu_tensor.numpy())
|
|
self.assertTrue(self.module.check_custom_allocator_used())
|
|
|
|
def test_set_get_device_stats_fn(self):
|
|
os_path = os.path.join(TestPluggableAllocator.build_directory, 'pluggable_allocator_extensions.so')
|
|
myallocator = ctypes.CDLL(os_path)
|
|
get_device_stats_fn = ctypes.cast(myallocator.my_get_device_stats, ctypes.c_void_p).value
|
|
|
|
TestPluggableAllocator.new_alloc.allocator().set_get_device_stats_fn(get_device_stats_fn)
|
|
self.assertEqual(torch.npu.memory_stats_as_nested_dict()["num_alloc_retries"], 0)
|
|
|
|
def test_set_reset_peak_status_fn(self):
|
|
os_path = os.path.join(TestPluggableAllocator.build_directory, 'pluggable_allocator_extensions.so')
|
|
myallocator = ctypes.CDLL(os_path)
|
|
reset_peak_status_fn = ctypes.cast(myallocator.my_reset_peak_status, ctypes.c_void_p).value
|
|
|
|
TestPluggableAllocator.new_alloc.allocator().set_reset_peak_status_fn(reset_peak_status_fn)
|
|
torch.npu.reset_peak_memory_stats()
|
|
self.assertEqual(torch.npu.max_memory_allocated(), 0)
|
|
|
|
def test_pluggable_allocator_after_init(self):
|
|
os_path = os.path.join(TestPluggableAllocator.build_directory, 'pluggable_allocator_extensions.so')
|
|
# Do an initial memory allocator
|
|
ori_tensor = torch.zeros(10, device='npu')
|
|
# Load the allocator
|
|
new_alloc = torch_npu.npu.memory.NPUPluggableAllocator(os_path, 'my_malloc', 'my_free')
|
|
msg = "Can't swap an already initialized allocator"
|
|
with self.assertRaisesRegex(RuntimeError, msg):
|
|
torch_npu.npu.memory.change_current_allocator(new_alloc)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
run_tests()
|