PyPTO 配置查询系列:`pypto.get_host_options()` 获取 Host 端编译配置全解析
PyPTO 配置查询系列pypto.get_host_options()获取 Host 端编译配置全解析【免费下载链接】pyptoPyPTO发音: pai p-t-oParallel Tensor/Tile Operation编程范式。项目地址: https://gitcode.com/cann/pyptopypto.get_host_options()是 CANN PyPTOParallel 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_stageint00 ~ 5控制编译执行的阶段compile_monitor_enableint10 ~ 2编译阶段监控模式0 关闭监控1 开启监控但关闭 Pass 明细2 开启监控和 Pass 明细compile_timeoutint6000 ~ 2147483647控制整个编译过程的超时时间秒compile_timeout_stageint00 ~ 2147483647控制编译过程中某一阶段的超时时间秒compile_monitor_print_intervalint600 ~ 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枚举一一对应值枚举成员阶段含义0CompStage.ALL_COMPLETE全流程编译完成1CompStage.TENSOR_GRAPH张量图构建2CompStage.TILE_GRAPHTile 图构建3CompStage.EXECUTE_GRAPH执行图构建4CompStage.CODEGEN_INSTRUCTION指令生成5CompStage.CODEGEN_BINARY二进制生成与 set_host_options 配套使用get_host_options通常与set_host_options成对出现。set_host_options的可设置项与get_host_options的返回值一一对应python/pypto/config.pypypto.set_host_options( compile_stagepypto.CompStage.EXECUTE_GRAPH, # 控制编译阶段可传 CompStage 枚举或 0~5 整数 compile_monitor_enable2, # 0 关闭监控1 开启监控(无 Pass 明细)2 开启监控和 Pass 明细默认 1 compile_timeout1000, # 整个编译流程超时时间(秒)默认 600 compile_timeout_stage50, # 单个阶段编译超时时间(秒)默认 0 表示不限制 compile_monitor_print_interval120, # 编译进度打印间隔(秒)默认 60 )随后即可通过get_host_options校验配置是否生效import pypto pypto.set_host_options(compile_stagepypto.CompStage.EXECUTE_GRAPH) options pypto.get_host_options() print(options[compile_stage]) # 输出 3CompStage.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.pydef 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_stageget_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等常量并通过GetConfigAllTypeT(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_stagepypto.CompStage.EXECUTE_GRAPH, compile_monitor_enable0, # 关闭编译监控 compile_timeout1000, # 整个编译流程 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-oParallel Tensor/Tile Operation编程范式。项目地址: https://gitcode.com/cann/pypto创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考