本功能为试验特性,后续版本可能存在变更,暂不支持应用于商用产品中。
PyTorch框架默认采用Eager模式,单算子下发后立即执行,每个算子都需要从Host侧Python API->Host侧C++层算子下发->Device侧算子kernel执行,在Device侧每次kernel执行之前都需要等待Host侧的下发逻辑完成。因此当单个算子计算量过小或者Host性能不佳时,很容易产生Device空闲时间,即每个kernel执行完后都需要一段时间来等待下一个kernel下发完成。
为了优化Host调度性能,CUDA提供了图模式方案,称为CUDA Graph,一种Device调度策略,即省略算子的Host调度过程,具体参见官网Accelerating PyTorch with CUDA Graphs。
类似地,NPU也提供了图模式方案,称为aclgraph,通过TorchAir提供的backend config配置Device调度模式。
该功能通过torchair.get_npu_backend中compiler_config配置,示例如下,参数介绍参见表1。
1 2 3 4 5 6 | import torch_npu, torchair config = torchair.CompilerConfig() # 设置图下沉执行模式 config.mode = "reduce-overhead" npu_backend = torchair.get_npu_backend(compiler_config=config) opt_model = torch.compile(model, backend=npu_backend) |
本功能端到端使能reduce-overhead执行模式的样例,具体参见gitee仓提供的DeepseekV3模型在NPU推理样例,单击Link获取详情。