-
Notifications
You must be signed in to change notification settings - Fork 362
[main][quantization] Adapt to the new format of ds w4a8 weight #2392
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
+184
−71
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5ffde17
[main][quantization] Adapt to the new format of ds w4a8 quantization …
kunpengW-code 13d6fe0
add e2e
kunpengW-code 20747d4
Merge branch 'main' of https://github.com/vllm-project/vllm-ascend in…
kunpengW-code 07c819e
Merge branch 'main' of https://github.com/vllm-project/vllm-ascend in…
kunpengW-code cf1081e
Merge branch 'main' of https://github.com/vllm-project/vllm-ascend in…
kunpengW-code 547075a
trigger ci
kunpengW-code 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,3 +1,4 @@ | ||||||||||
import copy | ||||||||||
from unittest.mock import Mock, patch | ||||||||||
|
||||||||||
import torch | ||||||||||
|
@@ -31,79 +32,139 @@ def test_get_pergroup_param(self): | |||||||||
|
||||||||||
|
||||||||||
class TestAscendW4A8DynamicFusedMoEMethod(TestBase): | ||||||||||
experts = 8 | ||||||||||
input_size = 16 | ||||||||||
output_size = 56 | ||||||||||
group_size = 2 | ||||||||||
|
||||||||||
@patch('vllm_ascend.quantization.w4a8_dynamic.get_current_vllm_config') | ||||||||||
@patch('vllm_ascend.quantization.w4a8_dynamic.get_ep_group') | ||||||||||
@patch("vllm_ascend.ascend_config.get_ascend_config") | ||||||||||
@patch('vllm_ascend.quantization.w4a8_dynamic.get_mc2_group') | ||||||||||
@patch('torch.distributed.get_rank', return_value=0) | ||||||||||
def setUp(self, mock_get_rank, mock_get_mc2_group, mock_get_ascend_config, | ||||||||||
mock_get_ep_group): | ||||||||||
mock_get_ep_group, get_current_vllm_config): | ||||||||||
mock_ascend_config = Mock() | ||||||||||
mock_ascend_config.torchair_graph_config = Mock(enabled=False) | ||||||||||
mock_get_ascend_config.return_value = mock_ascend_config | ||||||||||
mock_vllm_config = Mock() | ||||||||||
mock_vllm_config.quant_config = Mock(quant_description={ | ||||||||||
"group_size": self.group_size, | ||||||||||
"version": "0.0.0" | ||||||||||
}) | ||||||||||
mock_vllm_config.parallel_config = Mock(enable_expert_parallel=True) | ||||||||||
get_current_vllm_config.return_value = mock_vllm_config | ||||||||||
self.quant_method = AscendW4A8DynamicFusedMoEMethod() | ||||||||||
|
||||||||||
def test_get_weight(self): | ||||||||||
param_dict = self.quant_method.get_weight(8, 4, 14, torch.bfloat16) | ||||||||||
# old quant version w4a8 weight | ||||||||||
param_dict = self.quant_method.get_weight(self.experts, | ||||||||||
self.input_size, | ||||||||||
self.output_size, | ||||||||||
torch.bfloat16) | ||||||||||
self.assertEqual(param_dict["w13_weight"].dtype, torch.int8) | ||||||||||
self.assertEqual(param_dict["w13_weight"].shape, | ||||||||||
(self.experts, 2 * self.input_size, self.output_size)) | ||||||||||
# new quant version weight | ||||||||||
self.quant_method.new_quant_version = True | ||||||||||
param_dict = self.quant_method.get_weight(self.experts, | ||||||||||
self.input_size, | ||||||||||
self.output_size, | ||||||||||
torch.bfloat16) | ||||||||||
self.assertEqual(param_dict["w13_weight"].dtype, torch.int8) | ||||||||||
self.assertEqual(param_dict["w13_weight"].shape, (8, 8, 14)) | ||||||||||
self.assertEqual(param_dict["w13_weight"].shape, | ||||||||||
(self.experts, self.input_size, self.output_size)) | ||||||||||
|
||||||||||
@patch('vllm_ascend.quantization.w4a8_dynamic.get_current_vllm_config') | ||||||||||
def test_get_dynamic_quant_param(self, mock_get_current_vllm_config): | ||||||||||
mock_vllm_config = Mock() | ||||||||||
mock_vllm_config.quant_config = Mock( | ||||||||||
quant_description={"group_size": 2}) | ||||||||||
mock_get_current_vllm_config.return_value = mock_vllm_config | ||||||||||
def test_get_dynamic_quant_param(self): | ||||||||||
# old quant version weight | ||||||||||
param_dict = self.quant_method.get_dynamic_quant_param( | ||||||||||
8, 4, 14, torch.bfloat16) | ||||||||||
self.experts, self.input_size, self.output_size, torch.bfloat16) | ||||||||||
self.assertEqual(param_dict["w13_weight_scale"].dtype, torch.bfloat16) | ||||||||||
self.assertEqual(param_dict["w13_weight_scale"].shape, (8, 8, 1)) | ||||||||||
self.assertEqual(param_dict["w13_weight_scale"].shape, | ||||||||||
(self.experts, 2 * self.input_size, 1)) | ||||||||||
self.assertEqual(param_dict["w13_weight_scale_second"].dtype, | ||||||||||
torch.bfloat16) | ||||||||||
self.assertEqual(param_dict["w13_weight_scale_second"].shape, | ||||||||||
(8, 8, 7)) | ||||||||||
(self.experts, 2 * self.input_size, | ||||||||||
self.output_size // self.group_size)) | ||||||||||
self.assertEqual(param_dict["w2_weight_scale"].dtype, torch.bfloat16) | ||||||||||
self.assertEqual(param_dict["w2_weight_scale"].shape, (8, 14, 1)) | ||||||||||
self.assertEqual(param_dict["w2_weight_scale"].shape, | ||||||||||
(self.experts, self.output_size, 1)) | ||||||||||
self.assertEqual(param_dict["w2_weight_scale_second"].dtype, | ||||||||||
torch.bfloat16) | ||||||||||
self.assertEqual(param_dict["w2_weight_scale_second"].shape, | ||||||||||
(8, 14, 2)) | ||||||||||
(self.experts, self.output_size, | ||||||||||
self.input_size // self.group_size)) | ||||||||||
# new quant version weight | ||||||||||
self.quant_method.new_quant_version = True | ||||||||||
param_dict = self.quant_method.get_dynamic_quant_param( | ||||||||||
self.experts, self.input_size, self.output_size, torch.bfloat16) | ||||||||||
self.assertEqual(param_dict["w2_scale_bias"].dtype, torch.float32) | ||||||||||
self.assertEqual( | ||||||||||
param_dict["w2_scale_bias"].shape, | ||||||||||
(self.experts, self.output_size, 16 // self.quant_method.tp_size)) | ||||||||||
|
||||||||||
@patch('torch_npu.npu_quantize') | ||||||||||
@patch('torch.Tensor.npu') | ||||||||||
def test_process_weights_after_loading(self, mock_npu, mock_npu_quantize): | ||||||||||
# old quant version weight | ||||||||||
layer = torch.nn.Module() | ||||||||||
layer.w13_weight = torch.nn.Parameter(torch.zeros((8, 8, 14), | ||||||||||
dtype=torch.int8), | ||||||||||
layer.w13_weight = torch.nn.Parameter(torch.zeros( | ||||||||||
(self.experts, 2 * self.input_size, self.output_size), | ||||||||||
dtype=torch.int8), | ||||||||||
requires_grad=False) | ||||||||||
layer.w2_weight = torch.nn.Parameter(torch.zeros((8, 14, 4), | ||||||||||
dtype=torch.int8), | ||||||||||
layer.w2_weight = torch.nn.Parameter(torch.zeros( | ||||||||||
(self.experts, self.output_size, self.input_size), | ||||||||||
dtype=torch.int8), | ||||||||||
requires_grad=False) | ||||||||||
layer.w13_weight_scale = torch.nn.Parameter(torch.ones( | ||||||||||
(8, 8, 1), dtype=torch.bfloat16), | ||||||||||
(self.experts, 2 * self.input_size, 1), dtype=torch.bfloat16), | ||||||||||
requires_grad=False) | ||||||||||
layer.w13_weight_offset = torch.nn.Parameter(torch.zeros( | ||||||||||
(8, 8, 1), dtype=torch.bfloat16), | ||||||||||
requires_grad=False) | ||||||||||
layer.w13_weight_scale_second = torch.nn.Parameter(torch.ones( | ||||||||||
(8, 8, 7), dtype=torch.bfloat16), | ||||||||||
(self.experts, 2 * self.input_size, | ||||||||||
self.output_size // self.group_size), | ||||||||||
dtype=torch.bfloat16), | ||||||||||
requires_grad=False) | ||||||||||
layer.w2_weight_scale = torch.nn.Parameter(torch.ones( | ||||||||||
(8, 14, 1), dtype=torch.bfloat16), | ||||||||||
(self.experts, self.output_size, 1), dtype=torch.bfloat16), | ||||||||||
requires_grad=False) | ||||||||||
layer.w2_weight_offset = torch.nn.Parameter(torch.zeros( | ||||||||||
(8, 14, 1), dtype=torch.bfloat16), | ||||||||||
requires_grad=False) | ||||||||||
layer.w2_weight_scale_second = torch.nn.Parameter(torch.ones( | ||||||||||
(8, 14, 2), dtype=torch.bfloat16), | ||||||||||
(self.experts, self.output_size, | ||||||||||
self.input_size // self.group_size), | ||||||||||
dtype=torch.bfloat16), | ||||||||||
requires_grad=False) | ||||||||||
new_layer = copy.deepcopy(layer) | ||||||||||
|
||||||||||
mock_npu.return_value = torch.Tensor() | ||||||||||
mock_npu_quantize.return_value = torch.Tensor() | ||||||||||
self.quant_method.process_weights_after_loading(layer) | ||||||||||
self.assertTrue(hasattr(layer, "w13_scale_bias")) | ||||||||||
self.assertEqual(layer.w13_scale_bias.data.shape, (8, 8)) | ||||||||||
self.assertEqual(layer.w13_scale_bias.data.shape, | ||||||||||
(self.experts, 2 * self.input_size)) | ||||||||||
self.assertEqual(layer.w13_scale_bias.data.dtype, torch.float32) | ||||||||||
self.assertTrue(hasattr(layer, "w2_scale_bias")) | ||||||||||
self.assertEqual(layer.w2_scale_bias.data.shape, (8, 14)) | ||||||||||
self.assertEqual(layer.w2_scale_bias.data.shape, | ||||||||||
(self.experts, self.output_size)) | ||||||||||
self.assertEqual(layer.w2_scale_bias.data.dtype, torch.float32) | ||||||||||
# new quant version weight | ||||||||||
self.quant_method.new_quant_version = True | ||||||||||
new_layer.w13_weight.data = torch.zeros( | ||||||||||
(self.experts, self.input_size, self.output_size), | ||||||||||
dtype=torch.int8) | ||||||||||
new_layer.w2_weight.data = torch.zeros( | ||||||||||
(self.experts, self.output_size // 2, self.input_size), | ||||||||||
dtype=torch.int8) | ||||||||||
w13_scale_bias = torch.zeros((self.experts, 2 * self.input_size, 1), | ||||||||||
dtype=torch.float32) | ||||||||||
new_layer.w13_scale_bias = torch.nn.Parameter(w13_scale_bias, | ||||||||||
requires_grad=False) | ||||||||||
w2_scale_bias = torch.zeros( | ||||||||||
(self.experts, self.output_size, 16 // self.quant_method.tp_size), | ||||||||||
dtype=torch.float32) | ||||||||||
new_layer.w2_scale_bias = torch.nn.Parameter(w2_scale_bias, | ||||||||||
requires_grad=False) | ||||||||||
self.quant_method.process_weights_after_loading(new_layer) | ||||||||||
self.assertEqual(new_layer.w13_scale_bias.data.shape, | ||||||||||
(self.experts, 2 * self.input_size)) | ||||||||||
Comment on lines
+167
to
+168
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. This assertion checks for a shape that is inconsistent with the corresponding weight's shape. Following the correction in the
Suggested change
|
||||||||||
self.assertEqual(new_layer.w2_scale_bias.data.shape, | ||||||||||
(self.experts, self.output_size)) |
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.
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.
The setup for
w13_scale_bias
seems to be based on an incorrect shape definition inget_dynamic_quant_param
. The dimension2 * self.input_size
is inconsistent with the correspondingw13_weight
's dimension for the new quantization version. This should beself.input_size
to match the weight.