Closed
[Task|任务跟踪]: CodeGen编译时长优化 #683
HujunAdd on  Mar 14Closed at  Mar 16
Hujun
HujunCollaborators
Mar 14 Create

任务描述

python3 python/tests/st/operator/pg/test_pg_lightning_indexer_prolog_quant_hif8.py
执行环境:
A5 Host 96核 Intel(R) Xeon(R) Gold 6248R CPU @ 3.00GHz
Top Func数量14个,开启VF融合,kernel文件 总计192个

编译耗时如下
image.png

存在优化空间

任务目标

通过增加详细时长统计日志:
https://gitcode.com/cann/pypto/pull/1540

分析发现:
[ERROR] PYPTO(3833510):2026-03-13 07:31:55.416 [codegen.cpp:29][CODEGEN]:hjhj CodeGen TopFunc TENSOR_TENSOR_lightning_indexer_prolog_quant_loop_Unroll1_PATH0_hiddenfunc0_5, hash 6938669248580862220, cost: 2743.708849 ms
[ERROR] PYPTO(3833510):2026-03-13 07:31:57.361 [codegen.cpp:29][CODEGEN]:hjhj CodeGen TopFunc TENSOR_TENSOR_lightning_indexer_prolog_quant_loop_Unroll1_PATH0_hiddenfunc0_5, hash 6938669248580862220, cost: 1945.517552 ms
[ERROR] PYPTO(3833510):2026-03-13 07:32:04.885 [codegen.cpp:29][CODEGEN]:hjhj CodeGen TopFunc TENSOR_IndexerPrologQuantQuantLoop_LoopUnroll32_Unroll1_PATH0_hiddenfunc0_8, hash 896416327755869673, cost: 7523.869860 ms
[ERROR] PYPTO(3833510):2026-03-13 07:32:12.379 [codegen.cpp:29][CODEGEN]:hjhj CodeGen TopFunc TENSOR_IndexerPrologQuantQuantLoop_LoopUnroll32_Unroll1_PATH0_hiddenfunc0_8, hash 896416327755869673, cost: 7494.114814 ms
[ERROR] PYPTO(3833510):2026-03-13 07:32:18.047 [codegen.cpp:29][CODEGEN]:hjhj CodeGen TopFunc TENSOR_IndexerPrologQuantQuantLoop_LoopUnroll16_Unroll1_PATH0_hiddenfunc0_12, hash 13504058370752046350, cost: 5667.534479 ms
[ERROR] PYPTO(3833510):2026-03-13 07:32:23.482 [codegen.cpp:29][CODEGEN]:hjhj CodeGen TopFunc TENSOR_IndexerPrologQuantQuantLoop_LoopUnroll16_Unroll1_PATH0_hiddenfunc0_12, hash 13504058370752046350, cost: 5435.199985 ms
[ERROR] PYPTO(3833510):2026-03-13 07:32:29.007 [codegen.cpp:29][CODEGEN]:hjhj CodeGen TopFunc TENSOR_IndexerPrologQuantQuantLoop_LoopUnroll8_Unroll1_PATH0_hiddenfunc0_16, hash 9665263376435430323, cost: 5524.180422 ms
[ERROR] PYPTO(3833510):2026-03-13 07:32:34.307 [codegen.cpp:29][CODEGEN]:hjhj CodeGen TopFunc TENSOR_IndexerPrologQuantQuantLoop_LoopUnroll8_Unroll1_PATH0_hiddenfunc0_16, hash 9665263376435430323, cost: 5300.138056 ms
[ERROR] PYPTO(3833510):2026-03-13 07:32:39.852 [codegen.cpp:29][CODEGEN]:hjhj CodeGen TopFunc TENSOR_IndexerPrologQuantQuantLoop_LoopUnroll4_Unroll1_PATH0_hiddenfunc0_20, hash 10029177497931812616, cost: 5544.696010 ms
[ERROR] PYPTO(3833510):2026-03-13 07:32:45.262 [codegen.cpp:29][CODEGEN]:hjhj CodeGen TopFunc TENSOR_IndexerPrologQuantQuantLoop_LoopUnroll4_Unroll1_PATH0_hiddenfunc0_20, hash 10029177497931812616, cost: 5409.937455 ms
[ERROR] PYPTO(3833510):2026-03-13 07:32:50.685 [codegen.cpp:29][CODEGEN]:hjhj CodeGen TopFunc TENSOR_IndexerPrologQuantQuantLoop_LoopUnroll2_Unroll1_PATH0_hiddenfunc0_24, hash 1928974339839487415, cost: 5423.002562 ms
[ERROR] PYPTO(3833510):2026-03-13 07:32:56.082 [codegen.cpp:29][CODEGEN]:hjhj CodeGen TopFunc TENSOR_IndexerPrologQuantQuantLoop_LoopUnroll2_Unroll1_PATH0_hiddenfunc0_24, hash 1928974339839487415, cost: 5397.179105 ms
[ERROR] PYPTO(3833510):2026-03-13 07:33:01.428 [codegen.cpp:29][CODEGEN]:hjhj CodeGen TopFunc TENSOR_IndexerPrologQuantQuantLoop_LoopUnroll1_Unroll1_PATH0_hiddenfunc0_28, hash 7269035677932632888, cost: 5346.106077 ms
[ERROR] PYPTO(3833510):2026-03-13 07:33:06.681 [codegen.cpp:29][CODEGEN]:hjhj CodeGen TopFunc TENSOR_IndexerPrologQuantQuantLoop_LoopUnroll1_Unroll1_PATH0_hiddenfunc0_28, hash 7269035677932632888, cost: 5252.524583 ms

虽然CodeGen内部对子图进行了并行编译,但是Machine在调用每个顶层root function 的CodeGen接口时,仍然为串行调用,导致整体编译时长随顶层root Function个数增加而线性叠加。

提取优化点如下:

  1. Machine 对顶层root function并行编译(优化效果占比最大)
  2. 经过分析日志发现CodeGen的print code和dump file时间为ms级,远小于bisheng编译时间,故拆分编译阶段:
  • 阶段1: 通过多线程方式对单个root function内的所有子图进行print code & dump kernel file 并生成makefile文件
  • 阶段2: 通过make方式调用bisheng编译器并行编译root function内的所有子图kernel代码文件
  1. 将并行编译的线程数从默认的8改为128

日志分析见附件
96c6aac6b7734d9db66e287ef49ef785.zip

likedislike
HujunHujunCollaborators
Mar 14 Modify description of issue
10 event messages are collapsed here。 See more
Jie YinJie YinCollaborators
Mar 15 changed custom priority from 次要 to 主要
Jie Yin
Jie YinCollaborators
Mar 15 Comments:

machine 和 codegen 领域跟踪

likedislike
Jie YinJie YinCollaborators
Mar 15 Assign jason_yuan_ye as responsible person
HujunHujunCollaborators
Mar 16 changed custom state from 进行中 to 已完成
HujunHujunCollaborators
Mar 16 Close issue
HujunHujunCollaborators
Mar 16 changed custom state from 已完成 to 进行中
HujunHujunCollaborators
Mar 16 Reopen issue
Hujun
HujunCollaborators
Mar 16 Comments:

关联pr !1570 已合入,效果复合预期

测试用例:
python3 ./python/tests/st/operator/pg/test_pg_lightning_indexer_prolog_quant_mxfp8.py

Host 96核 Intel(R) Xeon(R) Gold 6248R CPU @ 3.00GHz

优化前:
image.png

优化后
image.png

likedislike
HujunHujunCollaborators
Mar 16 changed custom state from 进行中 to 已完成
HujunHujunCollaborators
Mar 16 Close issue
杨旭杨旭Collaborators
Mar 17 associated Kanban: PyPTO
杨旭杨旭Collaborators
Mar 19 Associated milestone: 0.2.0
CANN-robotCANN-robotCollaborators
Mar 20 Added label: resolved