Merged
fix(build): Fix some build error. #1137
AlizarinAdd on 4 days ago
fix(build): Fix some build error. #1137
Merged
AlizarinAdd on 4 days ago
Total: 6 files changed+65-10
@@ -71,9 +71,15 @@ function(enable_pypto_aicpu_kernel op_file)
71 message(FATAL_ERROR "enable_pypto_aicpu_kernel: ARM cross compiler not found: ${GYM_AICPU_ARM_CXX}")71 message(FATAL_ERROR "enable_pypto_aicpu_kernel: ARM cross compiler not found: ${GYM_AICPU_ARM_CXX}")
72 endif()72 endif()
73 73 
74+ # 定位 CLI 仅需解析 pypto_pro 包路径; 与 pypto_codegen.py / pypto_aicpu_codegen_cli.py 的
75+ # build-only stub 对齐: 构建期 codegen 不依赖真 torch 运行时(绑定源码走 runtime JIT),
76+ # stub 掉 pypto.cost_model 顶层未受保护的 import torch.
74 execute_process(77 execute_process(
75 COMMAND ${Python3_EXECUTABLE} -c78 COMMAND ${Python3_EXECUTABLE} -c
76- "import os, pypto_pro.runtime.opc as opc; print(os.path.join(os.path.dirname(opc.__file__), 'pypto_aicpu_codegen_cli.py'))"79+ "import sys; from unittest.mock import MagicMock;
80+sys.modules['torch'] = MagicMock(name='torch');
81+sys.modules['torch_npu'] = MagicMock(name='torch_npu');
82+import os, pypto_pro.runtime.opc as opc; print(os.path.join(os.path.dirname(opc.__file__), 'pypto_aicpu_codegen_cli.py'))"
77 OUTPUT_VARIABLE _gym_codegen_py83 OUTPUT_VARIABLE _gym_codegen_py
78 OUTPUT_STRIP_TRAILING_WHITESPACE84 OUTPUT_STRIP_TRAILING_WHITESPACE
79 RESULT_VARIABLE _gym_codegen_rc85 RESULT_VARIABLE _gym_codegen_rc
@@ -371,12 +371,15 @@ endforeach()
371 371 
372# 二次构建机制: 主 configure 期先跑 prepare.sh(PREPARE_BUILD=ON 的第二次 cmake + make prepare_build),372# 二次构建机制: 主 configure 期先跑 prepare.sh(PREPARE_BUILD=ON 的第二次 cmake + make prepare_build),
373# 产出 autogen(ops-info.ini/aclnn 源) 与 impl(adapt py), 供主构建图消费373# 产出 autogen(ops-info.ini/aclnn 源) 与 impl(adapt py), 供主构建图消费
374+# 透传 Python3_EXECUTABLE, 保证二次构建与主构建(含 build_ci.py 入口)使用同一解释器,
375+# 避免 find_package(Python3) 在多版本环境(如 /usr/local/bin/python3.x)中漂移.
374if (NOT PREPARE_BUILD AND ENABLE_OPS_KERNEL)376if (NOT PREPARE_BUILD AND ENABLE_OPS_KERNEL)
375 string(REPLACE ";" "::" EP_ASCEND_OP_NAME "${ASCEND_OP_NAME}")377 string(REPLACE ";" "::" EP_ASCEND_OP_NAME "${ASCEND_OP_NAME}")
376 execute_process(COMMAND bash ${PYPTO_GYM_SRC_DIR}/cmake/scripts/prepare.sh378 execute_process(COMMAND bash ${PYPTO_GYM_SRC_DIR}/cmake/scripts/prepare.sh
377 -s ${CMAKE_CURRENT_SOURCE_DIR}379 -s ${CMAKE_CURRENT_SOURCE_DIR}
378 -b ${CMAKE_BINARY_DIR}/prepare_build380 -b ${CMAKE_BINARY_DIR}/prepare_build
379 -p ${ASCEND_CANN_PACKAGE_PATH}381 -p ${ASCEND_CANN_PACKAGE_PATH}
382+ --python3 ${Python3_EXECUTABLE}
380 --autogen-dir ${ASCEND_AUTOGEN_DIR}383 --autogen-dir ${ASCEND_AUTOGEN_DIR}
381 --binary-out-dir ${ASCEND_BINARY_OUT_DIR}384 --binary-out-dir ${ASCEND_BINARY_OUT_DIR}
382 --impl-out-dir ${ASCEND_IMPL_OUT_DIR}385 --impl-out-dir ${ASCEND_IMPL_OUT_DIR}
@@ -28,6 +28,7 @@ while [[ $# -gt 0 ]]; do
28 --ascend-cmake-dir) ASCEND_CMAKE_DIR="$2"; shift 2 ;;28 --ascend-cmake-dir) ASCEND_CMAKE_DIR="$2"; shift 2 ;;
29 --ascend-compute_unit) ASCEND_COMPUTE_UNIT="$2"; shift 2 ;;29 --ascend-compute_unit) ASCEND_COMPUTE_UNIT="$2"; shift 2 ;;
30 --ascend-op_name) ASCEND_OP_NAME="$2"; shift 2 ;;30 --ascend-op_name) ASCEND_OP_NAME="$2"; shift 2 ;;
31+ --python3) PYTHON3_EXECUTABLE="$2"; shift 2 ;;
31 *) echo "unknown arg: $1"; exit 1 ;;32 *) echo "unknown arg: $1"; exit 1 ;;
32 esac33 esac
33done34done
@@ -37,6 +38,13 @@ convert_string() { echo "$1" | sed 's/::/;/g'; }
37CONVERT_ASCEND_COMPUTE_UNIT="$(convert_string ${ASCEND_COMPUTE_UNIT})"38CONVERT_ASCEND_COMPUTE_UNIT="$(convert_string ${ASCEND_COMPUTE_UNIT})"
38CONVERT_ASCEND_OP_NAME="$(convert_string ${ASCEND_OP_NAME})"39CONVERT_ASCEND_OP_NAME="$(convert_string ${ASCEND_OP_NAME})"
39 40 
41+# 透传主 configure 解析的 Python3, 保证二次构建与入口(build_ci.py)使用同一解释器;
42+# 未提供时保持原有 find_package(Python3) 自发现行为(独立手工调用场景).
43+PYTHON3_DEFINE=""
44+if [[ -n "${PYTHON3_EXECUTABLE}" ]]; then
45+ PYTHON3_DEFINE="-DPython3_EXECUTABLE=${PYTHON3_EXECUTABLE}"
46+fi
47+ 
40rm -rf "${PATH_TO_BUILD}"48rm -rf "${PATH_TO_BUILD}"
41mkdir -p "${PATH_TO_BUILD}"49mkdir -p "${PATH_TO_BUILD}"
42cd "${PATH_TO_BUILD}"50cd "${PATH_TO_BUILD}"
@@ -53,5 +61,6 @@ cmake ${PATH_TO_SOURCE} \
53 -DASCEND_CMAKE_DIR=${ASCEND_CMAKE_DIR} \61 -DASCEND_CMAKE_DIR=${ASCEND_CMAKE_DIR} \
54 -DASCEND_COMPUTE_UNIT=${CONVERT_ASCEND_COMPUTE_UNIT} \62 -DASCEND_COMPUTE_UNIT=${CONVERT_ASCEND_COMPUTE_UNIT} \
55 -DASCEND_OP_NAME=${CONVERT_ASCEND_OP_NAME} \63 -DASCEND_OP_NAME=${CONVERT_ASCEND_OP_NAME} \
64+ ${PYTHON3_DEFINE}
56 65 
57make ${JOB_NUM} prepare_build66make ${JOB_NUM} prepare_build
@@ -79,9 +79,23 @@ def _prepare_binary_headers(py_file: Path, arch: str) -> Path:
79def _build_native_aicpu(py_file: Path, out_dir: Path, op_file: str, arch: str):79def _build_native_aicpu(py_file: Path, out_dir: Path, op_file: str, arch: str):
80 """Compile the beta AICPU launcher and emit its C ABI declaration."""80 """Compile the beta AICPU launcher and emit its C ABI declaration."""
81 from pypto_pro import ir81 from pypto_pro import ir
82- from pypto_pro.runtime.aicpu_codegen import generate_aicpu_bare_body, generate_aicpu_sources82+ try: # newer pypto_pro: codegen helpers split into aicpu_codegen
83+ from pypto_pro.runtime.aicpu_codegen import generate_aicpu_bare_body, generate_aicpu_sources
84+ except ImportError: # pypto_pro 9.2.1: helpers live in aicpu_compile
85+ from pypto_pro.runtime.aicpu_compile import generate_aicpu_bare_body, generate_aicpu_sources
83 from pypto_pro.runtime.aicpu_compile import build_aicpu_jit_so86 from pypto_pro.runtime.aicpu_compile import build_aicpu_jit_so
84- from pypto_pro.runtime.opc import pypto_aicpu_codegen as offline87+ try:
88+ from pypto_pro.runtime.opc import pypto_aicpu_codegen as offline
89+ except ImportError: # pypto_pro 9.2.1: symbols live in runtime.{jit,platform}
90+ from types import SimpleNamespace
91+ 
92+ from pypto_pro.runtime.jit import _extract_param_specs, _setup_arch_env
93+ from pypto_pro.runtime.platform import normalize_arch
94+ offline = SimpleNamespace(
95+ _setup_arch_env=_setup_arch_env,
96+ normalize_arch=normalize_arch,
97+ _extract_param_specs=_extract_param_specs,
98+ )
85 99 
86 spec = importlib.util.spec_from_file_location(py_file.stem, py_file)100 spec = importlib.util.spec_from_file_location(py_file.stem, py_file)
87 module = importlib.util.module_from_spec(spec)101 module = importlib.util.module_from_spec(spec)
@@ -95,12 +109,12 @@ def _build_native_aicpu(py_file: Path, out_dir: Path, op_file: str, arch: str):
95 body = generate_aicpu_bare_body(program, arch, kernel.__name__)109 body = generate_aicpu_bare_body(program, arch, kernel.__name__)
96 sources = generate_aicpu_sources(body, params, kernel.__name__)110 sources = generate_aicpu_sources(body, params, kernel.__name__)
97 111 
98- args_header = out_dir / f"{op_file}_aicpu_args.h"
99 kernel_source = out_dir / f"{op_file}_aicpu.cpp"112 kernel_source = out_dir / f"{op_file}_aicpu.cpp"
100 host_source = out_dir / f"{op_file}_aicpu_host.cpp"113 host_source = out_dir / f"{op_file}_aicpu_host.cpp"
101- args_header.write_text(sources["args_header"], encoding="utf-8")
102 kernel_source.write_text(sources["kernel_source"], encoding="utf-8")114 kernel_source.write_text(sources["kernel_source"], encoding="utf-8")
103 host_source.write_text(sources["host_source"], encoding="utf-8")115 host_source.write_text(sources["host_source"], encoding="utf-8")
116+ if "args_header" in sources: # newer pypto_pro only: 9.2.1 embeds it in the host TU
117+ (out_dir / f"{op_file}_aicpu_args.h").write_text(sources["args_header"], encoding="utf-8")
104 declaration = re.search(r'extern "C" uint32_t \w+Run\([^{}]+\)', sources["host_source"])118 declaration = re.search(r'extern "C" uint32_t \w+Run\([^{}]+\)', sources["host_source"])
105 if declaration is None:119 if declaration is None:
106 raise RuntimeError("native AICPU codegen did not emit its Run ABI")120 raise RuntimeError("native AICPU codegen did not emit its Run ABI")
@@ -137,11 +151,13 @@ def main():
137 out_dir = Path(args.out_dir)151 out_dir = Path(args.out_dir)
138 out_dir.mkdir(parents=True, exist_ok=True)152 out_dir.mkdir(parents=True, exist_ok=True)
139 153 
154+ # 两条 codegen 路径(aicpu-native / binary headers)均为 build-only, 均不依赖真 torch,
155+ # stub 需在 aicpu-native 分支 import pypto_pro 之前安装.
156+ _install_build_only_torch_stub()
140 if args.aicpu_native:157 if args.aicpu_native:
141 _build_native_aicpu(py_file, out_dir.resolve(), args.op_file, arch)158 _build_native_aicpu(py_file, out_dir.resolve(), args.op_file, arch)
142 return159 return
143 160 
144- _install_build_only_torch_stub()
145 binary_dir = _prepare_binary_headers(py_file, arch)161 binary_dir = _prepare_binary_headers(py_file, arch)
146 shutil.copytree(binary_dir, out_dir, dirs_exist_ok=True)162 shutil.copytree(binary_dir, out_dir, dirs_exist_ok=True)
147 copied = sorted(163 copied = sorted(
@@ -57,6 +57,7 @@ dev = [
57 "pip",57 "pip",
58 "build",58 "build",
59 "packaging",59 "packaging",
60+ "numpy",
60 "pytest>=7",61 "pytest>=7",
61 "pytest-forked",62 "pytest-forked",
62 "pytest-xdist",63 "pytest-xdist",
@@ -1,13 +1,33 @@
1# Runtime dependencies for pypto-gym1# Runtime dependencies for pypto-gym
2# 注:torch / torch_npu 需要用户按照昇腾环境手册单独安装,不从 PyPI 自动拉取。2# 注:torch / torch_npu 需要用户按照昇腾环境手册单独安装,不从 PyPI 自动拉取。
3+# 注:pypto / pypto_pro 由 CANN 环境提供(source set_env.sh 后经 PYTHONPATH 可达,
4+# 其 LD_LIBRARY_PATH 亦提供 libprofapi.so 等运行库),不从 PyPI 自动拉取,
5+# 故本文件不将其列为可安装项(约定开发/使用前必先 source CANN 环境变量)。
6+# 依赖以 pyproject.toml 为准([build-system].requires / [project].dependencies /
7+# [project].optional-dependencies.{dev,tests}),本文件面向源码仓开发场景,
8+# 修改依赖时两处保持同步。
3 9 
4-# [project] dependencies10+# 构建期依赖(python3 build_ci.py 全链路所需;
5-pypto>=0.3.011+# 对应 [build-system].requires 与 [project].optional-dependencies.dev)
12+# setuptools: python -m build --no-isolation 强制校验 >=70.0.3;
13+# >=70.1 内置 bdist_wheel,无需单独安装 wheel 包
14+# build: build_ci.py 打包阶段预检(>=1.0.3)
15+# packaging: build_ci.py 模块级 import(pip 依赖预检解析用)
16+# PyYAML: cmake configure 期 select_run_ops.py 解析 build_ops.yaml 的硬依赖
17+# (tests 套件亦依赖,与 tests extra 共用)
18+# numpy: CANN pypto 顶层 import 硬依赖(pypto/op/math.py),codegen 期需要
19+setuptools>=70.3.0
20+pip
21+build>=1.0.3
22+packaging
23+PyYAML>=6.0
24+numpy
25+ 
26+# [project] dependencies(运行时)
27+# pypto>=0.3.0:不从 PyPI 拉取,由 CANN 环境提供,见顶部注释
6ninja>=1.1128ninja>=1.11
7 29 
8# [project.optional-dependencies] tests30# [project.optional-dependencies] tests
9-PyYAML>=6.0
10pytest>=731pytest>=7
11pytest-forked32pytest-forked
12pytest-xdist33pytest-xdist
13-numpy