-
Notifications
You must be signed in to change notification settings - Fork 4.5k
[inference]fix import bug and delete down useless init #4830
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
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
| @@ -1,10 +1,67 @@ | ||
| """ | ||
| Utils for model inference | ||
| """ | ||
| import os | ||
|
|
||
| import torch | ||
|
|
||
| from colossalai.kernel.triton.copy_kv_cache_dest import copy_kv_cache_to_dest | ||
|
|
||
|
|
||
| def copy_kv_to_mem_cache(layer_id, key_buffer, value_buffer, context_mem_index, mem_manager): | ||
| """ | ||
| This function copies the key and value cache to the memory cache | ||
| Args: | ||
| layer_id : id of current layer | ||
| key_buffer : key cache | ||
| value_buffer : value cache | ||
| context_mem_index : index of memory cache in kv cache manager | ||
| mem_manager : cache manager | ||
| """ | ||
| copy_kv_cache_to_dest(key_buffer, context_mem_index, mem_manager.key_buffer[layer_id]) | ||
| copy_kv_cache_to_dest(value_buffer, context_mem_index, mem_manager.value_buffer[layer_id]) | ||
| return | ||
|
|
||
|
|
||
| def init_to_get_rotary(self, base=10000, use_elem=False): | ||
| """ | ||
| This function initializes the rotary positional embedding, it is compatible for all models and is called in ShardFormer | ||
| Args: | ||
| self : Model that holds the rotary positional embedding | ||
| base : calculation arg | ||
| use_elem : activated when using chatglm-based models | ||
| """ | ||
| 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) | ||
|
|
||
| # NTK ref: https://www.reddit.com/r/LocalLLaMA/comments/14lz7j5/ntkaware_scaled_rope_allows_llama_models_to_have/ | ||
| ntk_alpha = float(os.environ.get("INFER_NTK_ALPHA", None)) | ||
|
|
||
| if ntk_alpha is not None: | ||
| ntk_alpha = float(ntk_alpha) | ||
| assert ntk_alpha >= 1, "NTK alpha must be greater than or equal to 1" | ||
| if ntk_alpha > 1: | ||
| print(f"Note: NTK enabled, alpha set to {ntk_alpha}") | ||
| max_seq_len *= ntk_alpha | ||
| base = base * (ntk_alpha ** (self.head_dim_ / (self.head_dim_ - 2))) # Base change formula | ||
|
|
||
| n_elem = self.config.head_dim_ | ||
| if use_elem: | ||
| n_elem //= 2 | ||
|
|
||
| inv_freq = 1.0 / (base ** (torch.arange(0, n_elem, 2, device="cpu", dtype=torch.float32) / n_elem)) | ||
| 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() | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.