[performance] InferEngine: drastically improve initialization speed#481
Merged
Merged
Conversation
- Replace sync `set_zeros` with async `set_zeros_device_async` to eliminate CPU buffer allocation and H2D overhead. - Decouple RankWorker creation from initialization wait to allow parallel startup. - Add explicit synchronization after KV cache allocation to ensure readiness.
Collaborator
|
/test |
Collaborator
|
需要解conflict以及用scripts/format.py格式化 |
97ae62a to
b480569
Compare
b480569 to
2bc048c
Compare
PanZezhong1725
approved these changes
Jul 8, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
set_zeroswith asyncset_zeros_device_asyncto eliminate CPU buffer allocation and H2D overhead.提升InferEngine初始化时间,将rank_worker并行化
InferEngine初始化非常慢,尤其是在多tp场景下,更加恶化。
主要两个原因,1、显存分配多卡是串行的 2、kv-cache清零操作的时候额外写了内存,再从内存搬迁到GPU显存
以下几个图说明,显存分配的串行过程




然后在csrc/engine/infer_engine.cpp的InferEngine构造函数中增加如下时间记录打印

优化对比测试:
沐曦环境 InferEngine初始化时间能从157.34秒缩短到28.54秒
沐曦环境优化前


沐曦环境优化后
摩尔环境初始化时间能从11.93秒缩短到4.47秒


摩尔环境优化前
python examples/test_infer.py --device moore --model /home/rubik/models/Mistral-Large-Instruct-2411 --tp 8 --enable-paged-attn --prompt 'introduce yourself'
摩尔环境优化后
优化后显存分配是多卡并行的,参考以下几个图:




不影响模型的正常推理。

摩尔环境验证了Mistral-Large-Instruct-2411
沐曦环境也验证了Mistral-Large-Instruct-2411

N卡验证了所支持的大部分模型。





