-
Notifications
You must be signed in to change notification settings - Fork 4.5k
[infer]Add and optimize vllm Continous Batching and Pageattention #4755
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
Merged
isky-cd
merged 10 commits into
hpcaitech:feature/vllm-continupus-batching
from
isky-cd:vllm_continous_batching
Sep 22, 2023
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
611fdba
Add continous batching and pageattention
isky-cd a099a7b
fix bug when calling funtion
isky-cd 201cfff
adapted to vllm
isky-cd d60c662
resolve pr comments
isky-cd b0ace60
resolve pr comments
isky-cd c1102d3
handle vllm's import issues
isky-cd 07f2fca
fix ci bugs
isky-cd 86cd231
fix some bugs
isky-cd b67cc2c
fix some bugs
isky-cd 406ba07
remove code for skipping ci test
isky-cd 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
440 changes: 440 additions & 0 deletions
440
colossalai/inference/continous_batching/layers/attention.py
Large diffs are not rendered by default.
Oops, something went wrong.
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,52 @@ | ||
| import types | ||
| import warnings | ||
|
|
||
| import torch | ||
| try: | ||
| from vllm.model_executor.models.llama import LlamaAttention | ||
| VLLM_INSTALLED = True | ||
| except ImportError: | ||
| warnings.warn("vllm is not installed, PageAttention will not be replaced.") | ||
| VLLM_INSTALLED = False | ||
|
|
||
| def init_to_get_rotary(self, base=10000): | ||
| self.config.head_dim_ = self.config.hidden_size // self.config.num_attention_heads | ||
| if not hasattr(self.config, "rope_scaling"): | ||
| rope_scaling_factor = 1.0 | ||
| else: | ||
| rope_scaling_factor = self.config.rope_scaling.factor if self.config.rope_scaling is not None else 1.0 | ||
| if hasattr(self.config, "max_sequence_length"): | ||
| max_seq_len = self.config.max_sequence_length | ||
| elif hasattr(self.config, "max_position_embeddings"): | ||
| max_seq_len = self.config.max_position_embeddings * rope_scaling_factor | ||
| else: | ||
| max_seq_len = 2048 * rope_scaling_factor | ||
| base = float(base) | ||
| inv_freq = 1.0 / (base**(torch.arange(0, self.config.head_dim_, 2, device="cpu", dtype=torch.float32) / | ||
| self.config.head_dim_)) | ||
| t = torch.arange(max_seq_len + 1024 * 64, device="cpu", dtype=torch.float32) / rope_scaling_factor | ||
| freqs = torch.outer(t, inv_freq) | ||
|
|
||
| self._cos_cached = torch.cos(freqs).to(torch.float16).cuda() | ||
| self._sin_cached = torch.sin(freqs).to(torch.float16).cuda() | ||
| return | ||
|
|
||
|
|
||
| def replace_page_attention(model, kv_cache_stream): | ||
| if VLLM_INSTALLED: | ||
|
|
||
| from colossalai.inference.continous_batching.layers.attention import PagedAttentionWithRoPE | ||
|
|
||
| layers = model.model.layers | ||
| for i in range(len(layers)): | ||
| layer = layers[i] | ||
| if isinstance(layer.self_attn, LlamaAttention) is True: | ||
| attn = PagedAttentionWithRoPE(layer.self_attn.num_heads, | ||
| layer.self_attn.head_dim, | ||
| layer.self_attn.scaling, | ||
| rotary_dim=layer.self_attn.head_dim, | ||
| num_kv_heads=layer.self_attn.num_kv_heads, | ||
| kv_cache_stream=kv_cache_stream) | ||
| setattr(layer.self_attn, 'attn', attn) | ||
|
|
||
| return model |
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.
Uh oh!
There was an error while loading. Please reload this page.