-
Notifications
You must be signed in to change notification settings - Fork 693
[V1 Loader] support weight_only #3413
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
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
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 |
|---|---|---|
|
|
@@ -38,6 +38,8 @@ | |
| moe_expert_reduce, | ||
| ) | ||
|
|
||
| from fastdeploy.model_executor.utils import TensorTracker, free_tensor, set_weight_attrs | ||
|
|
||
|
|
||
| # used for deepseek_v3 | ||
| def get_moe_scores( | ||
|
|
@@ -93,8 +95,8 @@ def compute_ffn( | |
| return fastdeploy.model_executor.ops.iluvatar.moe_expert_ffn( | ||
| permute_input, | ||
| token_nums_per_expert, | ||
| layer.up_gate_proj_weight, | ||
| layer.down_proj_weight, | ||
| getattr(layer, self.added_weight_attrs[0]), | ||
| getattr(layer, self.added_weight_attrs[1]), | ||
| None, | ||
| (layer.up_gate_proj_weight_scale if hasattr(layer, "up_gate_proj_weight_scale") else None), | ||
| (layer.down_proj_weight_scale if hasattr(layer, "down_proj_weight_scale") else None), | ||
|
|
@@ -106,8 +108,8 @@ def compute_ffn( | |
| return fastdeploy.model_executor.ops.gpu.moe_expert_ffn( | ||
| permute_input, | ||
| token_nums_per_expert, | ||
| layer.up_gate_proj_weight, | ||
| layer.down_proj_weight, | ||
| getattr(layer, self.added_weight_attrs[0]), | ||
| getattr(layer, self.added_weight_attrs[1]), | ||
| None, | ||
| (layer.up_gate_proj_weight_scale if hasattr(layer, "up_gate_proj_weight_scale") else None), | ||
| (layer.down_proj_weight_scale if hasattr(layer, "down_proj_weight_scale") else None), | ||
|
|
@@ -392,12 +394,12 @@ def create_weights(self, layer: nn.Layer, **extra_weight_attrs): | |
| Paddle cutlass create weight process. | ||
| """ | ||
| self.weight_dtype = "int8" | ||
| self.ffn1_weight_shape = [ | ||
| self.up_gate_proj_weight_shape = [ | ||
| layer.num_local_experts, | ||
| layer.hidden_size // 2, | ||
| layer.moe_intermediate_size * 2, | ||
| ] | ||
| self.ffn2_weight_shape = [ | ||
| self.down_proj_weight_shape = [ | ||
| layer.num_local_experts, | ||
| layer.moe_intermediate_size // 2, | ||
| layer.hidden_size, | ||
|
|
@@ -406,7 +408,7 @@ def create_weights(self, layer: nn.Layer, **extra_weight_attrs): | |
| layer, | ||
| self.added_weight_attrs[0], | ||
| layer.create_parameter( | ||
| shape=self.ffn1_weight_shape, | ||
| shape=self.up_gate_proj_weight_shape, | ||
| dtype=self.weight_dtype, | ||
| default_initializer=paddle.nn.initializer.Constant(0), | ||
| ), | ||
|
|
@@ -415,7 +417,7 @@ def create_weights(self, layer: nn.Layer, **extra_weight_attrs): | |
| layer, | ||
| self.added_weight_attrs[1], | ||
| layer.create_parameter( | ||
| shape=self.ffn2_weight_shape, | ||
| shape=self.down_proj_weight_shape, | ||
| dtype=self.weight_dtype, | ||
| default_initializer=paddle.nn.initializer.Constant(0), | ||
| ), | ||
|
|
@@ -625,71 +627,177 @@ def create_weights(self, layer: nn.Layer, **extra_weight_attrs): | |
| Paddle cutlass create weight process. | ||
| """ | ||
| self.default_dtype = layer._helper.get_default_dtype() | ||
| self.weight_dtype = "int8" | ||
|
|
||
| up_gate_proj_weight_name = self.added_weight_attrs[0] | ||
| down_proj_weight_name = self.added_weight_attrs[1] | ||
| if self.moe_quant_type == "weight_only_int4": | ||
| self.ffn1_weight_shape = [ | ||
| self.up_gate_proj_weight_shape = [ | ||
| layer.num_local_experts, | ||
| layer.moe_intermediate_size, | ||
| layer.hidden_size, | ||
| ] | ||
| else: | ||
| self.ffn1_weight_shape = [ | ||
| self.up_gate_proj_weight_shape = [ | ||
| layer.num_local_experts, | ||
| layer.moe_intermediate_size * 2, | ||
| layer.hidden_size, | ||
| ] | ||
| if self.moe_quant_type == "weight_only_int4": | ||
| self.ffn2_weight_shape = [ | ||
| self.down_proj_weight_shape = [ | ||
| layer.num_local_experts, | ||
| layer.hidden_size // 2, | ||
| layer.moe_intermediate_size, | ||
| ] | ||
| else: | ||
| self.ffn2_weight_shape = [ | ||
| self.down_proj_weight_shape = [ | ||
| layer.num_local_experts, | ||
| layer.hidden_size, | ||
| layer.moe_intermediate_size, | ||
| ] | ||
| setattr( | ||
| layer, | ||
| up_gate_proj_weight_name, | ||
| layer.create_parameter( | ||
| shape=self.ffn1_weight_shape, | ||
| dtype=self.weight_dtype, | ||
| self.up_gate_proj_scale_shape = [layer.num_local_experts, layer.moe_intermediate_size * 2] | ||
| self.down_proj_scale_shape = [layer.num_local_experts, layer.hidden_size] | ||
|
|
||
| if layer.fd_config.load_config.load_choices == "default_v1": | ||
| layer.up_gate_proj_weight = layer.create_parameter( | ||
| shape=[layer.num_experts, layer.hidden_size, layer.moe_intermediate_size * 2], | ||
| dtype=layer.weight_dtype, | ||
| default_initializer=paddle.nn.initializer.Constant(0), | ||
| ), | ||
| ) | ||
| setattr( | ||
| layer, | ||
| down_proj_weight_name, | ||
| layer.create_parameter( | ||
| shape=self.ffn2_weight_shape, | ||
| dtype=self.weight_dtype, | ||
| ) | ||
|
|
||
| layer.down_proj_weight = layer.create_parameter( | ||
| shape=[layer.num_experts, layer.moe_intermediate_size, layer.hidden_size], | ||
| dtype=layer.weight_dtype, | ||
| default_initializer=paddle.nn.initializer.Constant(0), | ||
| ), | ||
| ) | ||
| # weight_scale | ||
| ) | ||
|
|
||
| set_weight_attrs( | ||
| layer.up_gate_proj_weight, | ||
| { | ||
| **extra_weight_attrs, | ||
| "tensor_track": TensorTracker(shape=layer.up_gate_proj_weight.shape, output_dim=True), | ||
| }, | ||
| ) | ||
| set_weight_attrs( | ||
| layer.down_proj_weight, | ||
| { | ||
| **extra_weight_attrs, | ||
| "tensor_track": TensorTracker(shape=layer.down_proj_weight.shape, output_dim=False), | ||
| }, | ||
| ) | ||
|
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. output_dim 这些标记,TP 并行、EP 并行都支持吗?
Collaborator
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. ep磁盘权重用不到 output_dim这个属性,tp支持 |
||
| else: | ||
| self.weight_dtype = "int8" | ||
|
|
||
| up_gate_proj_weight_name = self.added_weight_attrs[0] | ||
| down_proj_weight_name = self.added_weight_attrs[1] | ||
| up_gate_proj_scale_name = self.added_scale_attrs[0] | ||
| down_proj_scale_name = self.added_scale_attrs[1] | ||
|
|
||
| setattr( | ||
| layer, | ||
| up_gate_proj_weight_name, | ||
| layer.create_parameter( | ||
| shape=self.up_gate_proj_weight_shape, | ||
| dtype=self.weight_dtype, | ||
| default_initializer=paddle.nn.initializer.Constant(0), | ||
| ), | ||
| ) | ||
| setattr( | ||
| layer, | ||
| down_proj_weight_name, | ||
| layer.create_parameter( | ||
| shape=self.down_proj_weight_shape, | ||
| dtype=self.weight_dtype, | ||
| default_initializer=paddle.nn.initializer.Constant(0), | ||
| ), | ||
| ) | ||
| # weight_scale | ||
| setattr( | ||
| layer, | ||
| up_gate_proj_scale_name, | ||
| layer.create_parameter( | ||
| shape=self.up_gate_proj_scale_shape, | ||
| dtype=self.default_dtype, | ||
| default_initializer=paddle.nn.initializer.Constant(0), | ||
| ), | ||
| ) | ||
| setattr( | ||
| layer, | ||
| down_proj_scale_name, | ||
| layer.create_parameter( | ||
| shape=self.down_proj_scale_shape, | ||
| dtype=self.default_dtype, | ||
| default_initializer=paddle.nn.initializer.Constant(0), | ||
| ), | ||
| ) | ||
|
|
||
| moe_extra_weight_attrs = {**extra_weight_attrs, "SHARD_ID_TO_SHARDED_DIM": {"gate": 0, "down": 1, "up": 0}} | ||
| set_weight_attrs(layer.up_gate_proj_weight, moe_extra_weight_attrs) | ||
| set_weight_attrs(layer.down_proj_weight, moe_extra_weight_attrs) | ||
| scale_extra_weight_attrs = { | ||
| **extra_weight_attrs, | ||
| "SHARD_ID_TO_SHARDED_DIM": {"gate": 0, "up": 0, "down": None}, | ||
| } | ||
| set_weight_attrs(layer.up_gate_proj_weight_scale, scale_extra_weight_attrs) | ||
| set_weight_attrs(layer.down_proj_weight_scale, scale_extra_weight_attrs) | ||
|
|
||
| def process_weights_after_loading(self, layer): | ||
| """ """ | ||
| if not layer.fd_config.load_config.load_choices == "default_v1": | ||
| return | ||
| weight_id_map = {"gate_up": 0, "down": 1} | ||
| if ( | ||
| hasattr(layer.up_gate_proj_weight, "tensor_track") | ||
| and layer.up_gate_proj_weight.tensor_track is not None | ||
| and layer.up_gate_proj_weight.tensor_track.is_fully_copied() | ||
| ): | ||
| weight_type = "gate_up" | ||
| else: | ||
| weight_type = "down" | ||
|
|
||
| # 1.init shape and type | ||
| # weight | ||
| weight_name = self.added_weight_attrs[weight_id_map[weight_type]] | ||
| unquantized_weight_name = weight_name.replace("quant_weight", "weight") | ||
| weight_shape = self.up_gate_proj_weight_shape if weight_type == "gate_up" else self.down_proj_weight_shape | ||
| weight_dtype = "int8" | ||
| # scale | ||
| scale_name = self.added_scale_attrs[weight_id_map[weight_type]] | ||
| scale_shape = self.up_gate_proj_scale_shape if weight_type == "gate_up" else self.down_proj_scale_shape | ||
| scale_dtype = self.default_dtype | ||
|
|
||
| # 2.crate tmp tensor | ||
|
|
||
| weight = paddle.empty(weight_shape, dtype=weight_dtype) | ||
| scale = paddle.empty(scale_shape, dtype=scale_dtype) | ||
|
|
||
| # 3.quantize weight | ||
|
|
||
| for expert_id in range(layer.num_experts): | ||
| weight[expert_id], scale[expert_id] = weight_quantize( | ||
| getattr(layer, unquantized_weight_name)[expert_id], algo=self.moe_quant_type | ||
| ) | ||
|
|
||
| free_tensor(getattr(layer, unquantized_weight_name)) | ||
|
|
||
| # create weight | ||
| setattr( | ||
| layer, | ||
| self.added_scale_attrs[0], | ||
| weight_name, | ||
| layer.create_parameter( | ||
| shape=[layer.num_local_experts, layer.moe_intermediate_size * 2], | ||
| dtype=self.default_dtype, | ||
| shape=weight_shape, | ||
| dtype=weight_dtype, | ||
| default_initializer=paddle.nn.initializer.Constant(0), | ||
| ), | ||
| ) | ||
| # create scale | ||
| setattr( | ||
| layer, | ||
| self.added_scale_attrs[1], | ||
| scale_name, | ||
| layer.create_parameter( | ||
| shape=[layer.num_local_experts, layer.hidden_size], | ||
| dtype=self.default_dtype, | ||
| shape=scale_shape, | ||
| dtype=scale_dtype, | ||
| default_initializer=paddle.nn.initializer.Constant(0), | ||
| ), | ||
| ) | ||
| getattr(layer, weight_name).copy_(weight, False) | ||
| getattr(layer, scale_name).copy_(scale, False) | ||
|
|
||
| def process_loaded_weights(self, layer: nn.Layer, state_dict): | ||
| """ | ||
|
|
||
Oops, something went wrong.
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.
这里希望保持原样,更清晰
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.
但是量化前后名字不同
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.
有可能会不一样因为 w4a8也继承这个方法 w4a8目前还没确定咋弄