-
Notifications
You must be signed in to change notification settings - Fork 2.2k
[AutoParallel] Refactor llama3.1 model in intermediate api #3116
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
6 commits
Select commit
Hold shift + click to select a range
ef7b36e
refactor model in intermediate api mode
waliwali777 c57e5b1
update auto dist config
waliwali777 1c76af6
fix parallel_matmul
waliwali777 464668f
adapt workflow in auto parallel
waliwali777 d0e3854
rename run_single_model and remove redundant code
sevenan2 05b7388
fix conflict
sevenan2 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| # Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import paddle.distributed as dist | ||
|
|
||
|
|
||
| def get_dist_config(model, prefix=""): | ||
| """Generate distributed configuration for Llama model""" | ||
| if prefix != "": | ||
| assert prefix.endswith(".") | ||
|
|
||
| config = { | ||
| "mp_config": { | ||
| "parallelize_plan": { | ||
| f"{prefix}llama.embed_tokens": dist.ColWiseParallel(gather_output=True), | ||
| f"{prefix}llama.layers.*.self_attn.qkv_proj": dist.ColWiseParallel(), | ||
| f"{prefix}llama.layers.*.self_attn.q_proj": dist.ColWiseParallel(), | ||
| f"{prefix}llama.layers.*.self_attn.k_proj": dist.ColWiseParallel(), | ||
| f"{prefix}llama.layers.*.self_attn.v_proj": dist.ColWiseParallel(), | ||
| f"{prefix}llama.layers.*.self_attn.o_proj": dist.RowWiseParallel(), | ||
| f"{prefix}llama.layers.*.mlp.gate_proj": dist.ColWiseParallel(), | ||
| f"{prefix}llama.layers.*.mlp.up_proj": dist.ColWiseParallel(), | ||
| f"{prefix}llama.layers.*.mlp.gate_up_fused_proj": dist.ColWiseParallel(), | ||
| f"{prefix}llama.layers.*.mlp.down_proj": dist.RowWiseParallel(), | ||
| f"{prefix}lm_head.weight": dist.ColWiseParallel(), | ||
| } | ||
| }, | ||
| } | ||
| return config |
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 |
|---|---|---|
|
|
@@ -20,6 +20,11 @@ | |
| from paddle.distributed.fleet.utils import recompute | ||
| from paddle.distributed.fleet.utils.sequence_parallel_utils import ScatterOp | ||
|
|
||
| from paddleformers.transformers.conversion_utils import ( | ||
| StateDictNameMapping, | ||
| init_name_mappings, | ||
| ) | ||
|
|
||
| from ...nn.attention.interface import ALL_ATTENTION_FUNCTIONS | ||
| from ...nn.criterion.interface import CriterionLayer | ||
| from ...nn.embedding import Embedding as GeneralEmbedding | ||
|
|
@@ -34,6 +39,7 @@ | |
| from ..model_outputs import BaseModelOutputWithPast, CausalLMOutputWithPast | ||
| from ..model_utils import PretrainedModel, register_base_model | ||
| from ..modeling_rope_utils import ROPE_INIT_FUNCTIONS, dynamic_rope_update | ||
| from .auto_dist_config import get_dist_config | ||
| from .configuration import LlamaConfig | ||
|
|
||
|
|
||
|
|
@@ -160,9 +166,9 @@ def forward( | |
| q_shape = (batch_size, seq_len, self.num_heads, self.head_dim) | ||
| kv_shape = (batch_size, seq_len, self.num_key_value_heads, self.head_dim) | ||
|
|
||
| query_states = self.q_proj(hidden_states).view(q_shape).transpose(1, 2) | ||
| key_states = self.k_proj(hidden_states).view(kv_shape).transpose(1, 2) | ||
| value_states = self.v_proj(hidden_states).view(kv_shape).transpose(1, 2) | ||
| query_states = self.q_proj(hidden_states).reshape(q_shape).transpose(1, 2) | ||
| key_states = self.k_proj(hidden_states).reshape(kv_shape).transpose(1, 2) | ||
| value_states = self.v_proj(hidden_states).reshape(kv_shape).transpose(1, 2) | ||
|
|
||
| cos, sin = position_embeddings | ||
| query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin) | ||
|
|
@@ -324,6 +330,40 @@ class LlamaPretrainedModel(PretrainedModel): | |
| "down_proj", | ||
| ] | ||
|
|
||
| @classmethod | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个name_mapping的作用?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
这个确实没什么用,现在版本里一些其他的模型还留着 ,所以没有删除 |
||
| def _get_name_mappings(cls, config: LlamaConfig) -> list[StateDictNameMapping]: | ||
| mappings: list[StateDictNameMapping] = [] | ||
| model_mappings = [ | ||
| ["embed_tokens.weight"], | ||
| ["norm.weight"], | ||
| ] | ||
| for layer_index in range(config.num_hidden_layers): | ||
| layer_mappings = [ | ||
| [f"layers.{layer_index}.self_attn.q_proj.weight", None, "transpose"], | ||
| [f"layers.{layer_index}.self_attn.k_proj.weight", None, "transpose"], | ||
| [f"layers.{layer_index}.self_attn.v_proj.weight", None, "transpose"], | ||
| [f"layers.{layer_index}.self_attn.o_proj.weight", None, "transpose"], | ||
| [f"layers.{layer_index}.self_attn.rotary_emb.inv_freq"], | ||
| [f"layers.{layer_index}.mlp.gate_proj.weight", None, "transpose"], | ||
| [f"layers.{layer_index}.mlp.down_proj.weight", None, "transpose"], | ||
| [f"layers.{layer_index}.mlp.up_proj.weight", None, "transpose"], | ||
| [f"layers.{layer_index}.input_layernorm.weight"], | ||
| [f"layers.{layer_index}.post_attention_layernorm.weight"], | ||
| ] | ||
| model_mappings.extend(layer_mappings) | ||
|
|
||
| init_name_mappings(mappings=model_mappings) | ||
| # base-model prefix "LlamaModel" | ||
| if "LlamaModel" not in config.architectures: | ||
| for mapping in model_mappings: | ||
| mapping[0] = "model." + mapping[0] | ||
| mapping[1] = "llama." + mapping[1] | ||
| if not config.tie_word_embeddings: | ||
| model_mappings.append(["lm_head.weight", "lm_head.weight", "transpose"]) | ||
|
|
||
| mappings = [StateDictNameMapping(*mapping, index=index) for index, mapping in enumerate(model_mappings)] | ||
| return mappings | ||
|
|
||
| @classmethod | ||
| def _get_tensor_parallel_mappings(cls, config: LlamaConfig, is_split=True): | ||
| from ..conversion_utils import split_or_merge_func | ||
|
|
@@ -689,6 +729,10 @@ def forward( | |
| attentions=outputs.attentions, | ||
| ) | ||
|
|
||
| def auto_dist_config(self, prefix=""): | ||
| assert self.config.use_single_model_implementation, "Use `get_dist_config` only in single card mode." | ||
| return get_dist_config(self, prefix) | ||
|
|
||
|
|
||
| class LlamaForCausalLMPipe(GeneralModelForCausalLMPipe): | ||
| config_class = LlamaConfig | ||
|
|
||
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.
为什么要改为reshape?
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.
paddle transpose的问题,这里view之后虽然变成四维的,但是transpose还是认为该tensor是三维,即view之前的维度