已合并
fix: 统一管理 Python 解释器生命周期 #3631
duhua创建于 6月22日
fix: 统一管理 Python 解释器生命周期 #3631
已合并
duhua创建于 6月22日
duhua
duhua成员
6月22日

Pull Request

描述

当前 TBE、Python Pass 等多处依赖 Python 解释器,本 PR 引入 GePythonRuntimeManager,在 GE 各初始化入口统一探测、加载 libpython 并管理 Python 解释器生命周期,为 Python Fusion Pass 等能力提供一致的运行时基础。

主要变更:

1. 新增 GePythonRuntimeManager(base/common/python_runtime/)

  • ge_python_runtime_manager.h:管理器类定义,提供 EnsureReady() / ShutdownProcess() 接口
  • ge_python_runtime_manager.cc:管理器实现,包含 EnsureReadyLocked()、FinalizeOwnedInterpreterLocked() 等核心逻辑
  • ge_python_runtime_manager_helper.h:辅助头文件,包含 Python C API 类型定义、符号常量、探测脚本及 inline 工具函数(ResolvePythonCApi、EnsureLibpythonLoaded、ProbePythonRuntimeFromCommand 等)
  • 自动探测 python3/python 并解析 libpython 路径,通过 dlopen 加载 Python C API
  • 若进程内已有解释器则 attach,否则初始化 owned interpreter 并在 init 线程释放 GIL

2. 接入 GE 初始化链路

  • GEInitializeV2 / GEFinalizeV2(api/session/client/ge_api_v2.cc)
  • GeGenerator::Initialize(compiler/api/generator/ge_generator.cc)
  • aclgrphBuildInitializeImpl / aclgrphBuildFinalize(compiler/api/aclgrph/ge_ir_build.cc)
  • ATC 离线编译路径(api/atc/main_impl.cc):GenerateModel、GenerateSingleOp 等入口在 GELib::Initialize 前调用 EnsureReady(),失败/退出路径补充 ShutdownProcess()

3. 测试更新

  • 新增 ge_python_runtime_unittest.cc,覆盖 attach 已有解释器、owned interpreter 初始化与 shutdown 场景
  • 更新 fusion_pass_executor_unittest.cc:添加 python_pass_loaded_ 状态追踪,SetUp/TearDown 仅在加载过 Python pass 时调用 UnloadPythonPasses()
  • 更新 ge_generator_unittest.cc、ge_api_v2_unittest.cc、ge_ir_build_unittest.cc 等,在 teardown 中调用 ShutdownProcess() 避免用例间状态污染
  • 更新 ST 测试入口(test_main.cc、rt2_test_main.cc、dump_test_fixture.h)添加 ShutdownProcess()
  • 更新 dflow 测试(test.cc、init_ge.h、dflow_api_unittest.cc、ge_api_dflow_unittest.cc)添加 GEFinalize() 调用
  • 新增 scoped_unset_ld_preload.h 辅助类,用于 ge_running_env_test 中临时清除 LD_PRELOAD 环境变量

变更类型

关联的Issue

无

如何测试

  1. 验证在线、离线、atc 场景编译入口在加载 Python pass 前能成功初始化解释器(日志含 [GePythonRuntime] Loaded libpython 或 Initialize owned interpreter)
  2. 运行 ge_python_runtime_unittest 验证 attach/owned interpreter 生命周期管理
  3. 运行 fusion_pass_executor_unittest 验证 Python pass 注册与卸载逻辑

核对清单

其他信息

无

likedislike
Pull Request已成功合入, 合并人@CANN-robot
(感谢 duhua 的贡献)
duhuaduhua成员
6月22日 添加了label:enhancement
duhuaduhua成员
6月22日 创建了 pull request,commit 99210bed
CANN-robotCANN-robot成员
6月22日 添加了label:stat/needs-squash
CANN-robotCANN-robot成员
6月22日 添加了label:cann-cla/yes
CANN-robot
CANN-robot成员
6月22日 评论:

Thanks for your pull-request.
The full list of commands accepted by me can be found at here。
You can get sig-info at here


PR Approval Progress

✅ Congratulations! All modules have met the lgtm and approve requirements.

Module Approval Details

module lgtm status approve status
repo-cann/ge ✅ yangyongqiang0606, kobemini, stevenaw0 (3/2) ✅ yangyongqiang0606 (1/1)

💡 Tip:

  • Committer can comment /approve or /lgtm
  • Commenting /approve implies both code review (lgtm) and intent to merge (approve)

CLA Signature Pass

du-hua1024, thanks for your pull request. All authors of the commits have signed the CLA. 👍

likedislike
此处折叠了502条消息 查看更多
CANN-robotCANN-robot成员
6月26日 添加了label:lgtm
yangyongqiang
yangyongqiang成员
6月26日 评论:

/lgtm

likedislike
yangyongqiang
yangyongqiang成员
6月26日 评论:

/approve

likedislike
CANN-robotCANN-robot成员
6月26日 添加了label:approved
CANN-robotCANN-robot成员
6月26日 合入了pull request