Co-authored-by: huangyunlong2022<huangyunlong4@h-partners.com> # message auto-generated for no-merge-commit merge: !45706 merge 2.13head into master Using complete header files instead of stub Created-by: huangyunlong2022 Commit-by: huangyunlong2022 Merged-by: ascend-robot Description: <!-- PR描述模板更新日期:20260203 --> # 【合入来源】 > <font color="red">**如有社区issue,请关联issue链接**</font>\ > <font color="red">**请勿携带内部流程信息(需求链接、问题单、内部issue等)**</font> - [ ] 需求 - [x] 问题单 - [x] issue/工单 - [ ] 重构优化 - [ ] 资料更新 # 【修改方案】 > 请描述修改内容的具体实现,涉及哪些组件之间进行交互,可以用1、2、3、...进行罗列\ > 如果是需求或者重构类的PR,需要补充详细设计文档(说明上下游组件关系、时序图、类图、DFX能力等内容) 将打包third_party目录下的stub替换为真实的头文件,防止该路径放在-I的前面时由于`#pragma once`被截断导致的报错 其中`#include_next <acl/acl_rt.h>`没有被使用,是因为这种情况下如果-`I third_party`目录后没有真实头文件也会报错 另外该方法会暴露用户使用`#include "third_party/acl/inc/acl/acl_rt.h"`时的错误,这种场景就是torch_npu之前修复的cann和torch_npu中acl头文件混合使用的报错,这种深度路径只能找到torch_npu目录下的acl头文件,但是acl头文件里由于存在`#include "acl/acl_base_rt.h"`这种写法导致只能在-I目录中寻找,可能找到cann中的头文件,如果和torch_npu版本不配套就会报错,这种情况需要用户改脚本成`#include <acl/acl_rt.h>`这种规范写法,torch_npu中这种写法已修复 同时添加测试用例,模拟用户#include cann和torch_npu真实头文件以及配置-I 中torch_npu,third_party,cann include路径的组合情况,目前只测试编译情况,真实的ABI兼容性暂未进行测试 其中用户`#include "third_party/acl/inc/acl/acl_rt.h"`这种使用没有被测试,因为这种是我们认为不合理写法,是会报错的, 另外该测试用例使用环境上真实的torch_npu和cann路径,只能覆盖CI上的真实情况,实际还可以使用不同的cann和torch_npu版本进行兼容性配套测试 # 【资料变更】 > 请确认是否涉及资料变更。如涉及,需要在PR中体现,并简要说明修改内容。如不涉及,需填写“不涉及” # 【接口变更】 > 请确认是否涉及跨代码仓或者客户面可见的接口变更。如涉及,需要详细说明接口以及对应的变更内容,同时需要在资料中体现。如不涉及,需填写“不涉及” # 【功能验证】 > 说明测试场景,测试方法。如果本次测试方式与常规单元测试不同,请详细说明您的测试步骤\ > 新增/变更内容是否已新增/适配UT测试用例看护,并补充测试自验证截图 # 【CheckList】 > PR提交人对以下CheckList自检项进行全量自检,自检通过或不涉及,均修改 [ ] 为 [x] - [x] 代码注释完备,正确记录错误日志 - [x] 代码实现进行了返回值、空指针等校验 - [x] PR标题正确使用类型标签,如:feat、fix、refactor、docs、test等 - [x] PR持续集成流水线(CI)执行通过,代码检查无异常 See merge request: Ascend/pytorch!45706
317 lines
11 KiB
Python
317 lines
11 KiB
Python
import multiprocessing
|
|
import glob
|
|
import os
|
|
import re
|
|
import shutil
|
|
import subprocess
|
|
import sys
|
|
import traceback
|
|
import platform
|
|
from pathlib import Path
|
|
|
|
from sysconfig import get_paths
|
|
from distutils.version import LooseVersion
|
|
from distutils import file_util
|
|
|
|
# Disable autoloading before running 'import torch' to avoid circular dependencies
|
|
os.environ["TORCH_DEVICE_BACKEND_AUTOLOAD"] = "0"
|
|
|
|
|
|
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
|
from tools.setup_helpers.version import get_version
|
|
VERSION = get_version()
|
|
|
|
|
|
def which(thefile):
|
|
path = os.environ.get("PATH", os.defpath).split(os.pathsep)
|
|
for d in path:
|
|
fname = os.path.join(d, thefile)
|
|
fnames = [fname]
|
|
if sys.platform == 'win32':
|
|
exts = os.environ.get('PATHEXT', '').split(os.pathsep)
|
|
fnames += [fname + ext for ext in exts]
|
|
for name in fnames:
|
|
if os.access(name, os.F_OK | os.X_OK) and not os.path.isdir(name):
|
|
return name
|
|
return None
|
|
|
|
|
|
def get_cmake_command():
|
|
def _get_version(cmd):
|
|
for line in subprocess.check_output([cmd, '--version']).decode('utf-8').split('\n'):
|
|
if 'version' in line:
|
|
return LooseVersion(line.strip().split(' ')[2])
|
|
raise RuntimeError('no version found')
|
|
"Returns cmake command."
|
|
cmake_command = 'cmake'
|
|
if platform.system() == 'Windows':
|
|
return cmake_command
|
|
cmake3 = which('cmake3')
|
|
cmake = which('cmake')
|
|
if cmake3 is not None and _get_version(cmake3) >= LooseVersion("3.18.0"):
|
|
cmake_command = 'cmake3'
|
|
return cmake_command
|
|
elif cmake is not None and _get_version(cmake) >= LooseVersion("3.18.0"):
|
|
return cmake_command
|
|
else:
|
|
raise RuntimeError('no cmake or cmake3 with version >= 3.18.0 found')
|
|
|
|
|
|
def get_build_type():
|
|
build_type = 'Release'
|
|
if os.getenv('DEBUG', default='0').upper() in ['ON', '1', 'YES', 'TRUE', 'Y']:
|
|
build_type = 'Debug'
|
|
|
|
if os.getenv('REL_WITH_DEB_INFO', default='0').upper() in ['ON', '1', 'YES', 'TRUE', 'Y']:
|
|
build_type = 'RelWithDebInfo'
|
|
|
|
return build_type
|
|
|
|
|
|
def get_pytorch_dir():
|
|
try:
|
|
import torch
|
|
return os.path.dirname(os.path.abspath(torch.__file__))
|
|
except Exception:
|
|
_, _, exc_traceback = sys.exc_info()
|
|
frame_summary = traceback.extract_tb(exc_traceback)[-1]
|
|
return os.path.dirname(frame_summary.filename)
|
|
|
|
|
|
def generate_bindings_code(base_dir):
|
|
python_execute = sys.executable
|
|
generate_code_cmd = ["bash", os.path.join(base_dir, 'generate_code.sh'), python_execute, VERSION]
|
|
if subprocess.call(generate_code_cmd) != 0:
|
|
print(
|
|
'Failed to generate ATEN bindings: {}'.format(generate_code_cmd),
|
|
file=sys.stderr)
|
|
sys.exit(1)
|
|
|
|
|
|
def build_stub(base_dir):
|
|
build_stub_cmd = ["sh", os.path.join(base_dir, 'third_party/acl/libs/build_stub.sh')]
|
|
if subprocess.call(build_stub_cmd) != 0:
|
|
print(
|
|
'Failed to build stub: {}'.format(build_stub_cmd),
|
|
file=sys.stderr)
|
|
sys.exit(1)
|
|
|
|
|
|
def clean_generated_files():
|
|
f_ignore = open(os.path.join(BASE_DIR, '.gitignore'), 'r')
|
|
ignores = f_ignore.read()
|
|
pat = re.compile(r'^#( BEGIN NOT-CLEAN-FILES )?')
|
|
for wildcard in filter(None, ignores.split('\n')):
|
|
match = pat.match(wildcard)
|
|
if match:
|
|
if match.group(1):
|
|
# Marker is found and stop reading .gitignore.
|
|
break
|
|
# Ignore lines which begin with '#'.
|
|
else:
|
|
for filename in glob.glob(wildcard):
|
|
shutil.rmtree(filename, ignore_errors=True)
|
|
f_ignore.close()
|
|
clean_files = ['torch_npu/csrc/aten/RegisterCPU.cpp', 'torch_npu/csrc/aten/RegisterNPU.cpp',
|
|
'torch_npu/csrc/aten/RegisterAutogradNPU.cpp', 'torch_npu/csrc/aten/NPUNativeFunctions.h']
|
|
for file in clean_files:
|
|
if os.path.exists(os.path.join(BASE_DIR, file)):
|
|
os.remove(os.path.join(BASE_DIR, file))
|
|
if os.path.exists(os.path.join(BASE_DIR, "libtorch")):
|
|
shutil.rmtree(os.path.join(BASE_DIR, "libtorch"))
|
|
|
|
|
|
def check_gtest_valid(base_dir):
|
|
# validation of GoogleTest path.
|
|
gtest_path = os.path.join(base_dir, 'third_party/googletest/CMakeLists.txt')
|
|
return os.path.exists(gtest_path)
|
|
|
|
|
|
def generate_dbg_files_and_strip():
|
|
library_dir = Path(BASE_DIR).joinpath("build/packages/torch_npu")
|
|
dbg_dir = Path(BASE_DIR).joinpath("build/dbg_libtorch")
|
|
os.makedirs(dbg_dir, exist_ok=True)
|
|
library_files = [Path(i) for i in library_dir.rglob('*.so')]
|
|
for library_file in library_files:
|
|
subprocess.check_call(["eu-strip", library_file, "-f",
|
|
str(dbg_dir.joinpath(library_file.name)) + ".debug"], cwd=BASE_DIR) # Compliant
|
|
|
|
|
|
def run_cmake():
|
|
cmake = get_cmake_command()
|
|
|
|
if cmake is None:
|
|
raise RuntimeError(
|
|
"CMake must be installed to build the following extensions: ")
|
|
|
|
build_dir = os.path.join(BASE_DIR, "build")
|
|
build_type_dir = os.path.join(build_dir)
|
|
output_lib_path = os.path.join(build_type_dir, "packages/torch_npu/lib")
|
|
os.makedirs(build_type_dir, exist_ok=True)
|
|
os.makedirs(output_lib_path, exist_ok=True)
|
|
cmake_args = [
|
|
'-DCMAKE_BUILD_TYPE=' + get_build_type(),
|
|
'-DCMAKE_INSTALL_PREFIX=' + os.path.abspath(output_lib_path),
|
|
'-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=' + os.path.abspath(output_lib_path),
|
|
'-DCMAKE_ARCHIVE_OUTPUT_DIRECTORY=' + os.path.abspath(output_lib_path),
|
|
'-DTORCHNPU_INSTALL_LIBDIR=' + os.path.abspath(output_lib_path),
|
|
'-DPYTHON_INCLUDE_DIR=' + get_paths().get('include'),
|
|
'-DPYTORCH_INSTALL_DIR=' + get_pytorch_dir(),
|
|
'-DTORCH_VERSION=' + VERSION,
|
|
'-DBUILD_LIBTORCH=' + "ON",
|
|
'-DUSE_NPU=' + 'ON']
|
|
|
|
if check_opplugin_valid(BASE_DIR):
|
|
cmake_args.append('-DBUILD_OPPLUGIN=on')
|
|
cmake_args.append('-DBUILD_NEW_HEADER=on')
|
|
|
|
if check_tensorpipe_valid(BASE_DIR):
|
|
cmake_args.append('-DBUILD_TENSORPIPE=on')
|
|
|
|
if os.getenv('_GLIBCXX_USE_CXX11_ABI') is not None:
|
|
cmake_args.append('-DGLIBCXX_USE_CXX11_ABI=' + os.getenv('_GLIBCXX_USE_CXX11_ABI'))
|
|
|
|
if os.getenv('_ABI_VERSION') is not None:
|
|
cmake_args.append('-DABI_VERSION=' + os.getenv('_ABI_VERSION'))
|
|
|
|
use_ninja = os.environ["CMAKE_GENERATOR"].lower() == "ninja" if "CMAKE_GENERATOR" in os.environ else which("ninja") is not None
|
|
if use_ninja:
|
|
cmake_args.append("-GNinja")
|
|
|
|
max_jobs = os.getenv("MAX_JOBS")
|
|
if max_jobs is not None or not use_ninja:
|
|
max_jobs = max_jobs or str(multiprocessing.cpu_count())
|
|
jobs_args = ["-j", max_jobs]
|
|
else:
|
|
jobs_args = []
|
|
|
|
subprocess.check_call([cmake, BASE_DIR] + cmake_args, cwd=build_type_dir, env=os.environ)
|
|
|
|
build_cmd = [cmake, "--build", "."]
|
|
if jobs_args:
|
|
build_cmd += ["--"] + jobs_args
|
|
subprocess.check_call(build_cmd, cwd=build_type_dir, env=os.environ)
|
|
if which('eu-strip') is not None:
|
|
generate_dbg_files_and_strip()
|
|
|
|
|
|
def check_opplugin_valid(base_dir):
|
|
# build with submodule of op_plugin, if path of op-plugin is valid
|
|
op_plugin_path = os.path.join(base_dir, 'third_party/op-plugin/op_plugin')
|
|
return os.path.exists(op_plugin_path)
|
|
|
|
|
|
def check_tensorpipe_valid(base_dir):
|
|
tensorpipe_path = os.path.join(base_dir, 'third_party/Tensorpipe/tensorpipe')
|
|
return os.path.exists(tensorpipe_path)
|
|
|
|
|
|
def copy_file(infile, outfile, preserve_mode=1, preserve_times=1, link=None, level=1):
|
|
"""Copy a file respecting verbose, dry-run and force flags. (The
|
|
former two default to whatever is in the Distribution object, and
|
|
the latter defaults to false for commands that don't define it.)
|
|
"""
|
|
return file_util.copy_file(infile, outfile, preserve_mode, preserve_times, True, link)
|
|
|
|
|
|
def move_special_hpp(ret):
|
|
hpp_name = "torch_npu.h"
|
|
for _, dst in ret:
|
|
if dst.endswith(hpp_name):
|
|
dir_name = os.path.join(os.path.dirname(dst), "../../" + hpp_name)
|
|
copy_file(dst, dir_name)
|
|
return
|
|
|
|
|
|
def copy_hpp():
|
|
def get_src_py_and_dst():
|
|
ret = []
|
|
|
|
header_files = [
|
|
"torch_npu/csrc/aten/*.h",
|
|
"torch_npu/csrc/aten/*/*.h",
|
|
"torch_npu/csrc/aten/*/*/*.h",
|
|
"torch_npu/csrc/core/*.h",
|
|
"torch_npu/csrc/core/*/*.h",
|
|
"torch_npu/csrc/core/*/*/*.h",
|
|
"torch_npu/csrc/framework/*.h",
|
|
"torch_npu/csrc/framework/*/*.h",
|
|
"torch_npu/csrc/framework/*/*/*.h",
|
|
"torch_npu/csrc/libs/*.h",
|
|
"torch_npu/csrc/logging/*.h",
|
|
"torch_npu/csrc/inductor/**/*.h",
|
|
"torch_npu/csrc/distributed/*.h",
|
|
"torch_npu/csrc/distributed/*.hpp",
|
|
"third_party/acl/inc/*/*.h",
|
|
"third_party/acl/inc/*/*/*.h",
|
|
"third_party/hccl/inc/*/*.h",
|
|
]
|
|
glob_header_files = []
|
|
for regex_pattern in header_files:
|
|
glob_header_files += glob.glob(os.path.join(BASE_DIR, regex_pattern), recursive=True)
|
|
|
|
for src in glob_header_files:
|
|
dst = os.path.join(
|
|
os.path.join(BASE_DIR, "libtorch_npu/include/torch_npu"),
|
|
os.path.relpath(src, os.path.join(BASE_DIR, "torch_npu")))
|
|
os.makedirs(os.path.dirname(dst), exist_ok=True)
|
|
ret.append((src, dst))
|
|
|
|
acl_include_root = os.path.join(BASE_DIR, "third_party", "acl", "inc")
|
|
acl_header_files = glob.glob(
|
|
os.path.join(acl_include_root, "**", "*.h"),
|
|
recursive=True,
|
|
)
|
|
for src in acl_header_files:
|
|
relative_header = os.path.relpath(src, acl_include_root)
|
|
dst = os.path.join(
|
|
BASE_DIR,
|
|
"libtorch_npu/include",
|
|
relative_header,
|
|
)
|
|
os.makedirs(os.path.dirname(dst), exist_ok=True)
|
|
ret.append((src, dst))
|
|
|
|
return ret
|
|
ret = get_src_py_and_dst()
|
|
for src, dst in ret:
|
|
copy_file(src, dst)
|
|
move_special_hpp(ret)
|
|
|
|
|
|
def copy_lib():
|
|
lib_file = ["build/packages/torch_npu/lib/*.*"]
|
|
glob_lib_files = []
|
|
|
|
for regex_pattern in lib_file:
|
|
glob_lib_files += glob.glob(os.path.join(BASE_DIR, regex_pattern), recursive=True)
|
|
dst_path = os.path.join(BASE_DIR, "libtorch_npu/lib")
|
|
os.makedirs(dst_path, exist_ok=True)
|
|
|
|
for src in glob_lib_files:
|
|
_, src_file_name = os.path.split(src)
|
|
copy_file(src, os.path.join(dst_path, src_file_name))
|
|
|
|
|
|
def copy_cmake():
|
|
cmake_file = os.path.join(BASE_DIR, "build/Torch_npuConfig.cmake")
|
|
dst_path = os.path.join(BASE_DIR, "libtorch_npu/share/cmake/Torch_npu")
|
|
os.makedirs(dst_path, exist_ok=True)
|
|
copy_file(cmake_file, dst_path)
|
|
|
|
|
|
def build_libtorch_npu():
|
|
clean_generated_files()
|
|
generate_bindings_code(BASE_DIR)
|
|
if Path(BASE_DIR).joinpath("third_party/Tensorpipe/third_party/acl/libs").exists():
|
|
build_stub(Path(BASE_DIR).joinpath("third_party/Tensorpipe"))
|
|
build_stub(BASE_DIR)
|
|
run_cmake()
|
|
copy_hpp()
|
|
copy_lib()
|
|
copy_cmake()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
build_libtorch_npu()
|