-
Notifications
You must be signed in to change notification settings - Fork 625
[Feature]: Support speculative decoding #3945
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
RunningLeon
wants to merge
15
commits into
InternLM:main
Choose a base branch
from
RunningLeon:spec-decode
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 11 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
465b533
support spec with tp and cudagraph
RunningLeon 2c18726
fa3 cudagraph
RunningLeon 693998a
only use fa3 decode for spec decoding model
RunningLeon 68d4705
update req
RunningLeon af5efb1
add simple doc
RunningLeon 0aeb651
fix conficts with main
RunningLeon f3f3634
resolve comments
RunningLeon 24020f3
use spec decode config inside pt
RunningLeon 63fcaa3
Merge remote-tracking branch 'upstream/main' into spec-decode
RunningLeon 726c5fe
Merge remote-tracking branch 'upstream/main' into spec-decode
RunningLeon 37518a4
Merge remote-tracking branch 'upstream/main' into spec-decode
RunningLeon 6a403c4
resolve comments
RunningLeon f020a38
Merge remote-tracking branch 'upstream/main' into spec-decode
RunningLeon 571a0a2
resolve comment
RunningLeon 6e6b890
fix doc
RunningLeon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| # Speculative Decoding | ||
|
|
||
| Speculative decoding is an optimization technique that introcude a lightweight draft model to propose multiple next tokens and then, the main model verify and choose the longest matched tokens in a forward pass. Compared with standard auto-regressive decoding, this methold lets the system generate multiple tokens at once. | ||
|
|
||
| > \[!NOTE\] | ||
| > This is an experimental feature in lmdeploy. | ||
|
|
||
| ## Examples | ||
|
|
||
| Here are some examples. | ||
|
|
||
| ### Eagle 3 | ||
|
|
||
| #### Prepare | ||
|
|
||
| Install [flash-atten3 ](https://github.com/Dao-AILab/flash-attention?tab=readme-ov-file#flashattention-3-beta-release) | ||
|
|
||
| ```shell | ||
| git clone --depth=1 https://github.com/Dao-AILab/flash-attention.git | ||
| cd flash-attention/hopper | ||
| python setup.py install | ||
| ``` | ||
|
|
||
| #### pipeline | ||
|
|
||
| ```python | ||
| from lmdeploy import pipeline, PytorchEngineConfig | ||
| from lmdeploy.messages import SpeculativeConfig | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
|
|
||
| model_path = 'meta-llama/Llama-3.1-8B-Instruct' | ||
| spec_cfg = SpeculativeConfig(method='eagle3', | ||
| num_speculative_tokens=3, | ||
| model='yuhuili/EAGLE3-LLaMA3.1-Instruct-8B', | ||
| ) | ||
RunningLeon marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| pipe = pipeline(model_path, | ||
| backend_config=PytorchEngineConfig(max_batch_size=128), | ||
| speculative_config=spec_cfg) | ||
| response = pipe(['Hi, pls intro yourself', 'Shanghai is']) | ||
| print(response) | ||
| ``` | ||
|
|
||
| ### serving | ||
RunningLeon marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```shell | ||
| lmdeploy serve api_server \ | ||
| meta-llama/Llama-3.1-8B-Instruct \ | ||
| --backend pytorch \ | ||
| --server-port 24545 \ | ||
| --speculative-draft-model yuhuili/EAGLE3-LLaMA3.1-Instruct-8B \ | ||
| --speculative-algorithm eagle3 \ | ||
| --speculative-num-draft-tokens 3 \ | ||
| --max-batch-size 128 \ | ||
| --enable-metrics | ||
| ``` | ||
|
|
||
| ### Deepseek MTP | ||
|
|
||
| #### Prepare | ||
|
|
||
| Install [FlashMLA](https://github.com/deepseek-ai/FlashMLA?tab=readme-ov-file#installation) | ||
|
|
||
| ```shell | ||
| git clone https://github.com/deepseek-ai/FlashMLA.git flash-mla | ||
| cd flash-mla | ||
| git submodule update --init --recursive | ||
| pip install -v . | ||
| ``` | ||
|
|
||
| #### pipeline | ||
|
|
||
| ```python | ||
| from lmdeploy import pipeline, PytorchEngineConfig | ||
| from lmdeploy.messages import SpeculativeConfig | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
|
|
||
| model_path = 'deepseek-ai/DeepSeek-V3' | ||
| spec_cfg = SpeculativeConfig(method='deepseek_mtp', | ||
| num_speculative_tokens=3, | ||
| ) | ||
RunningLeon marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| pipe = pipeline(model_path, | ||
| backend_config=PytorchEngineConfig(tp=16, max_batch_size=128), | ||
| speculative_config=spec_cfg) | ||
| response = pipe(['Hi, pls intro yourself', 'Shanghai is']) | ||
| print(response) | ||
| ``` | ||
|
|
||
| ### serving | ||
RunningLeon marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```shell | ||
| lmdeploy serve api_server \ | ||
| deepseek-ai/DeepSeek-V3 \ | ||
| --backend pytorch \ | ||
| --server-port 24545 \ | ||
| --tp 16 \ | ||
| --speculative-algorithm deepseek_mtp \ | ||
| --speculative-num-draft-tokens 3 \ | ||
| --max-batch-size 128 \ | ||
| --enable-metrics | ||
| ``` | ||
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| # Speculative Decoding | ||
|
|
||
| 推测解码是一种优化技术,它通过引入轻量级草稿模型来预测多个后续token,再由主模型在前向推理过程中验证并选择匹配度最高的长token序列。与标准的自回归解码相比,这种方法可使系统一次性生成多个token。 | ||
RunningLeon marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| > \[!NOTE\] | ||
| > 请注意,这是lmdeploy中的实验性功能。 | ||
|
|
||
| ## 示例 | ||
|
|
||
| 请参考如下使用示例。 | ||
|
|
||
| ### Eagle 3 | ||
|
|
||
| #### 安装依赖 | ||
|
|
||
| 安装 [flash-atten3 ](https://github.com/Dao-AILab/flash-attention?tab=readme-ov-file#flashattention-3-beta-release) | ||
|
|
||
| ```shell | ||
| git clone --depth=1 https://github.com/Dao-AILab/flash-attention.git | ||
| cd flash-attention/hopper | ||
| python setup.py install | ||
| ``` | ||
|
|
||
| #### pipeline | ||
|
|
||
| ```python | ||
| from lmdeploy import pipeline, PytorchEngineConfig | ||
| from lmdeploy.messages import SpeculativeConfig | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
|
|
||
| model_path = 'meta-llama/Llama-3.1-8B-Instruct' | ||
| spec_cfg = SpeculativeConfig(method='eagle3', | ||
| num_speculative_tokens=3, | ||
| model='yuhuili/EAGLE3-LLaMA3.1-Instruct-8B', | ||
| ) | ||
| pipe = pipeline(model_path, | ||
| backend_config=PytorchEngineConfig(max_batch_size=128), | ||
| speculative_config=spec_cfg) | ||
| response = pipe(['Hi, pls intro yourself', 'Shanghai is']) | ||
| print(response) | ||
| ``` | ||
|
|
||
| ### serving | ||
|
|
||
| ```shell | ||
| lmdeploy serve api_server \ | ||
| meta-llama/Llama-3.1-8B-Instruct \ | ||
| --backend pytorch \ | ||
| --server-port 24545 \ | ||
| --speculative-draft-model yuhuili/EAGLE3-LLaMA3.1-Instruct-8B \ | ||
| --speculative-algorithm eagle3 \ | ||
| --speculative-num-draft-tokens 3 \ | ||
| --max-batch-size 128 \ | ||
| --enable-metrics | ||
| ``` | ||
|
|
||
| ### Deepseek MTP | ||
|
|
||
| #### 安装依赖 | ||
|
|
||
| Install [FlashMLA](https://github.com/deepseek-ai/FlashMLA?tab=readme-ov-file#installation) | ||
|
|
||
| ```shell | ||
| git clone https://github.com/deepseek-ai/FlashMLA.git flash-mla | ||
| cd flash-mla | ||
| git submodule update --init --recursive | ||
| pip install -v . | ||
| ``` | ||
|
|
||
| #### pipeline | ||
|
|
||
| ```python | ||
| from lmdeploy import pipeline, PytorchEngineConfig | ||
| from lmdeploy.messages import SpeculativeConfig | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
|
|
||
| model_path = 'deepseek-ai/DeepSeek-V3' | ||
| spec_cfg = SpeculativeConfig(method='deepseek_mtp', | ||
| num_speculative_tokens=3, | ||
| ) | ||
| pipe = pipeline(model_path, | ||
| backend_config=PytorchEngineConfig(tp=16, max_batch_size=128), | ||
| speculative_config=spec_cfg) | ||
| response = pipe(['Hi, pls intro yourself', 'Shanghai is']) | ||
| print(response) | ||
| ``` | ||
|
|
||
| ### serving | ||
|
|
||
| ```shell | ||
| lmdeploy serve api_server \ | ||
| deepseek-ai/DeepSeek-V3 \ | ||
| --backend pytorch \ | ||
| --server-port 24545 \ | ||
| --tp 16 \ | ||
| --speculative-algorithm deepseek_mtp \ | ||
| --speculative-num-draft-tokens 3 \ | ||
| --max-batch-size 128 \ | ||
| --enable-metrics | ||
| ``` | ||
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
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is flash-attn3 a must?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes.