PyPTO 配置查询系列:pypto.get_host_options()获取 Host 端编译配置全解析
【免费下载链接】pyptoPyPTO(发音: pai p-t-o):Parallel Tensor/Tile Operation编程范式。项目地址: https://gitcode.com/cann/pypto
pypto.get_host_options()是 CANN PyPTO(Parallel Tensor/Tile Operation 编程范式)提供的 Host 端配置查询接口,用于读取当前配置作用域(ConfigScope)中全部以host.为前缀的编译配置项,涵盖编译阶段控制、编译监控模式、编译超时等参数。读完本文,你将掌握该 API 的函数原型、返回值的每一项含义与默认值、与set_host_options的读写配套关系,以及它在编译流水线中的底层实现与消费链路。
产品支持情况
pypto.get_host_options()在以下产品形态上均受支持:
- Ascend 950PR / Ascend 950DT:支持
- Atlas A3 训练系列产品 / Atlas A3 推理系列产品:支持
- Atlas A2 训练系列产品 / Atlas A2 推理系列产品:支持
即当前仓库文档确认支持 Ascend 950 系列以及 Atlas A2 / A3 训练与推理系列产品。
功能说明
pypto.get_host_options()的功能非常聚焦:获取 Host 配置。它返回当前配置作用域内 Host 端全部配置项的键值对字典,是pypto.set_host_options()(python/pypto/config.py)设置动作的读取侧对称接口,二者共同构成 Host 端编译配置的读写闭环。
函数原型与参数说明
get_host_options() -> Dict[str, Union[str, int, List[int], Dict[int, int]]]- 参数:无。该接口不需要任何入参,直接从当前配置作用域读取。
- 返回值:返回
dict,包含 Host 的所有配置项信息。从类型标注看,字典值可以是str、int、List[int]或Dict[int, int]中的任意一种,实际以int为主。
返回值详解:Host 配置项清单与默认值
调用pypto.get_host_options()返回的字典内容,与仓库中配置文件 framework/src/interface/configs/tile_fwk_config.json 的host段一一对应。当前仓库中的默认配置如下:
| 配置项 | 类型 | 默认值 | 取值范围 | 含义 |
|---|---|---|---|---|
compile_stage | int | 0 | 0 ~ 5 | 控制编译执行的阶段 |
compile_monitor_enable | int | 1 | 0 ~ 2 | 编译阶段监控模式,0 关闭监控,1 开启监控但关闭 Pass 明细,2 开启监控和 Pass 明细 |
compile_timeout | int | 600 | 0 ~ 2147483647 | 控制整个编译过程的超时时间(秒) |
compile_timeout_stage | int | 0 | 0 ~ 2147483647 | 控制编译过程中某一阶段的超时时间(秒) |
compile_monitor_print_interval | int | 60 | 0 ~ 2147483647 | 控制某个阶段编译过程进度打印的频率(秒) |
上述取值范围、默认值与中英文说明在配置 Schema framework/src/interface/configs/tile_fwk_config_schema.json 中有明确定义(host段为 "主机端相关配置 / host-related configuration")。因此,查询返回的字典中一般可预期包含compile_stage、compile_monitor_enable、compile_timeout、compile_timeout_stage、compile_monitor_print_interval这五个键。
compile_stage 的阶段枚举
compile_stage的值与 python/pypto/config.py 中定义的CompStage枚举一一对应:
| 值 | 枚举成员 | 阶段含义 |
|---|---|---|
| 0 | CompStage.ALL_COMPLETE | 全流程编译完成 |
| 1 | CompStage.TENSOR_GRAPH | 张量图构建 |
| 2 | CompStage.TILE_GRAPH | Tile 图构建 |
| 3 | CompStage.EXECUTE_GRAPH | 执行图构建 |
| 4 | CompStage.CODEGEN_INSTRUCTION | 指令生成 |
| 5 | CompStage.CODEGEN_BINARY | 二进制生成 |
与 set_host_options 配套使用
get_host_options通常与set_host_options成对出现。set_host_options的可设置项与get_host_options的返回值一一对应(python/pypto/config.py):
pypto.set_host_options( compile_stage=pypto.CompStage.EXECUTE_GRAPH, # 控制编译阶段,可传 CompStage 枚举或 0~5 整数 compile_monitor_enable=2, # 0 关闭监控;1 开启监控(无 Pass 明细);2 开启监控和 Pass 明细,默认 1 compile_timeout=1000, # 整个编译流程超时时间(秒),默认 600 compile_timeout_stage=50, # 单个阶段编译超时时间(秒),默认 0 表示不限制 compile_monitor_print_interval=120, # 编译进度打印间隔(秒),默认 60 )随后即可通过get_host_options校验配置是否生效:
import pypto pypto.set_host_options(compile_stage=pypto.CompStage.EXECUTE_GRAPH) options = pypto.get_host_options() print(options["compile_stage"]) # 输出 3(CompStage.EXECUTE_GRAPH.value) print(options["compile_monitor_enable"]) # 1(默认开启监控)注意:set_host_options中传CompStage枚举成员时,内部会通过v.value if isinstance(v, CompStage) else v将其转换为整数值后再写入(python/pypto/config.py),因此get_host_options返回的compile_stage始终是整数,与枚举成员比较时应使用.value。
恢复默认配置
若希望把 Host 配置恢复为默认值,可调用pypto.reset_options()(python/pypto/config.py),它会清空所有类别(host/codegen/pass/runtime 等)的自定义配置:
pypto.reset_options() host_option = pypto.get_host_options() assert host_option["compile_stage"] == pypto.CompStage.ALL_COMPLETE.value # 恢复为 0底层实现原理
Python 侧:基于 ConfigScope 的前缀过滤
get_host_options的实现非常精简(python/pypto/config.py):
def get_host_options() -> Dict[str, Union[str, int, List[int], Dict[int, int]]]: scope = get_current_scope() return scope.get_host_options()其中get_current_scope()返回当前配置作用域(ConfigScope)。从 python/pypto/config.py 的ConfigScope实现可以看出,所有配置在内部以带前缀的扁平键存储(如host.compile_stage),get_host_options()实际执行的是前缀过滤:
def get_options(self, prefix): prefix = f"{prefix}." return {k[len(prefix):]: v for k, v in self._options.items() if k.startswith(prefix)} def get_host_options(self): return self.get_options("host")也就是说,返回值中的键名(如compile_stage)是去掉host.前缀之后的部分。ConfigScope的配置数据来源于 C++ 侧ConfigManagerNg的GetAllConfig(),若未显式配置则来自 tile_fwk_config.json 的默认值。
C++ 侧:host 前缀的配置存取
Host 配置在 C++ 侧由 framework/src/interface/configs/config_manager_ng.h 管理,其中定义了COMPILE_STAGE、COMPILE_MONITOR_ENABLE、TIMEOUT_SEC、TOTAL_TIMEOUT_SEC等常量,并通过GetConfigAllType<T>("host." + key)按host.前缀读取(同文件第 169-174 行、第 533 行附近),与 Python 侧的前缀过滤逻辑一致。
消费链路:Host 配置在哪里被使用
get_host_options返回的配置最终会被前端编译流水线消费。从 python/pypto/frontend/parser/entry.py 可以看到,编译器入口在构建编译选项时会主动查询这些值:
self._host_options["compile_stage"] = pypto.get_host_options().get(...) self._host_options["compile_monitor_enable"] = pypto.get_host_options().get(...) self._host_options["compile_timeout"] = pypto.get_host_options().get("compile_timeout", 600) self._host_options["compile_timeout_stage"] = pypto.get_host_options().get("compile_timeout_stage", 0) self._host_options["compile_monitor_print_interval"] = pypto.get_host_options().get(...)而在 C++ 绑定层 python/src/bindings/runtime.cpp 中,运行时同样会从_host_options字典中读取compile_stage、compile_monitor_enable、compile_monitor_print_interval、compile_timeout_stage、compile_timeout等字段,用于控制编译监控(Watchdog)与各阶段/整体超时。由此可以推断:Host 配置主要服务于编译流程的阶段控制、监控与超时保护,这也是它区别于 pass/codegen/runtime 配置的核心定位。
调用示例
最简单的调用方式即官方文档示例:
import pypto options = pypto.get_host_options() print(options) # 输出示例(默认配置): # { # "compile_stage": 0, # "compile_monitor_enable": 1, # "compile_timeout": 600, # "compile_timeout_stage": 0, # "compile_monitor_print_interval": 60, # }一个更完整的“设置-查询-校验”实战片段:
import pypto # 1. 设置 Host 配置 pypto.set_host_options( compile_stage=pypto.CompStage.EXECUTE_GRAPH, compile_monitor_enable=0, # 关闭编译监控 compile_timeout=1000, # 整个编译流程 1000 秒超时 ) # 2. 查询并校验 host_option = pypto.get_host_options() assert host_option["compile_stage"] == pypto.CompStage.EXECUTE_GRAPH.value assert host_option["compile_monitor_enable"] == 0 assert host_option["compile_timeout"] == 1000 # 3. 使用完毕恢复默认 pypto.reset_options() assert pypto.get_host_options()["compile_stage"] == pypto.CompStage.ALL_COMPLETE.value仓库单元测试 python/tests/ut/interface/test_config_options.py 中的test_host_option与test_reset_option正是按照上述“设置→查询断言→重置→恢复默认”的路径验证该接口行为的,可作为回归参考。
约束说明
get_host_options本身无参数、无额外约束,可在任意时刻调用;- 返回值反映的是当前配置作用域的 Host 配置快照。在
pypto.options()上下文管理器或装饰器(python/pypto/config.py)内部查询时,会看到该作用域内的临时配置;作用域退出后恢复外层配置; - 该接口只读,不会修改任何配置状态;如需修改请使用
set_host_options或pypto.options(host_options=...)作用域方式传入。
【免费下载链接】pyptoPyPTO(发音: pai p-t-o):Parallel Tensor/Tile Operation编程范式。项目地址: https://gitcode.com/cann/pypto
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考