0
tests/ut/_310p/quantization/__init__.py
Normal file
0
tests/ut/_310p/quantization/__init__.py
Normal file
138
tests/ut/_310p/quantization/test_modelslim_config_310.py
Normal file
138
tests/ut/_310p/quantization/test_modelslim_config_310.py
Normal file
@@ -0,0 +1,138 @@
|
||||
#
|
||||
# Copyright (c) 2026 Huawei Technologies Co., Ltd. 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.
|
||||
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from vllm.model_executor.layers.fused_moe.config import FusedMoEConfig, FusedMoEParallelConfig
|
||||
from vllm.model_executor.layers.linear import LinearBase
|
||||
|
||||
from tests.ut.base import TestBase
|
||||
from vllm_ascend._310p.fused_moe.fused_moe import AscendUnquantizedFusedMoEMethod310
|
||||
from vllm_ascend._310p.quantization.modelslim_config import AscendModelSlimConfig310
|
||||
from vllm_ascend.ops.linear import AscendUnquantizedLinearMethod
|
||||
from vllm_ascend.utils import vllm_version_is
|
||||
|
||||
if vllm_version_is("0.23.0"):
|
||||
from vllm.model_executor.layers.fused_moe import FusedMoE
|
||||
else:
|
||||
from vllm.model_executor.layers.fused_moe import RoutedExperts
|
||||
|
||||
|
||||
class TestAscendModelSlimConfig310(TestBase):
|
||||
def setUp(self):
|
||||
self.sample_config = {
|
||||
"weight": "INT8",
|
||||
"layer1.weight": "INT8",
|
||||
"layer2.weight": "FLOAT",
|
||||
"fused_layer.weight": "FLOAT",
|
||||
"fused_layer.shard1.weight": "FLOAT",
|
||||
"fused_layer.shard2.weight": "FLOAT",
|
||||
"shard1.weight": "FLOAT",
|
||||
"shard2.weight": "FLOAT",
|
||||
}
|
||||
self.ascend_config = AscendModelSlimConfig310(self.sample_config)
|
||||
self.ascend_config.packed_modules_mapping = None
|
||||
|
||||
def test_get_quant_method_for_linear_310(self):
|
||||
mock_config = MagicMock()
|
||||
mock_config.model_config.hf_config.model_type = None
|
||||
linear_layer = MagicMock(spec=LinearBase)
|
||||
# Test skipped layer
|
||||
with (
|
||||
patch("vllm_ascend._310p.quantization.modelslim_config.get_current_vllm_config", return_value=mock_config),
|
||||
patch.object(self.ascend_config, "is_layer_skipped_ascend", return_value=True),
|
||||
):
|
||||
method = self.ascend_config.get_quant_method(linear_layer, ".attn")
|
||||
self.assertIsInstance(method, AscendUnquantizedLinearMethod)
|
||||
|
||||
# Test quantized layer
|
||||
mock_scheme = MagicMock()
|
||||
with (
|
||||
patch.object(self.ascend_config, "is_layer_skipped_ascend", return_value=False),
|
||||
patch("vllm_ascend._310p.quantization.modelslim_config.get_current_vllm_config", return_value=mock_config),
|
||||
patch("vllm_ascend._310p.quantization.modelslim_config.create_scheme_for_layer", return_value=mock_scheme),
|
||||
patch(
|
||||
"vllm_ascend._310p.quantization.modelslim_config.AscendLinearMethod", return_value=MagicMock()
|
||||
) as mock_ascend_linear,
|
||||
):
|
||||
method = self.ascend_config.get_quant_method(linear_layer, ".attn")
|
||||
self.assertIs(method, mock_ascend_linear.return_value)
|
||||
mock_ascend_linear.assert_called_once_with(mock_scheme)
|
||||
|
||||
def test_get_quant_method_maps_lm_head_prefix_310(self):
|
||||
config = AscendModelSlimConfig310({"language_model.lm_head.weight": "INT8"})
|
||||
linear_layer = MagicMock(spec=LinearBase)
|
||||
mock_config = MagicMock()
|
||||
mock_config.model_config.hf_config.model_type = "qwen3_5_moe"
|
||||
mock_scheme = MagicMock()
|
||||
|
||||
with (
|
||||
patch("vllm_ascend._310p.quantization.modelslim_config.get_current_vllm_config", return_value=mock_config),
|
||||
patch(
|
||||
"vllm_ascend._310p.quantization.modelslim_config.create_scheme_for_layer",
|
||||
return_value=mock_scheme,
|
||||
) as mock_create_scheme,
|
||||
patch("vllm_ascend._310p.quantization.modelslim_config.AscendLinearMethod", return_value=MagicMock()),
|
||||
):
|
||||
config.get_quant_method(linear_layer, "lm_head")
|
||||
|
||||
mock_create_scheme.assert_called_once_with(
|
||||
quant_description=config.quant_description,
|
||||
prefix="language_model.lm_head",
|
||||
layer_type="linear",
|
||||
packed_modules_mapping=config.packed_modules_mapping,
|
||||
)
|
||||
|
||||
def test_get_quant_method_for_fused_moe_310(self):
|
||||
if vllm_version_is("0.23.0"):
|
||||
fused_moe_cls = FusedMoE
|
||||
else:
|
||||
fused_moe_cls = RoutedExperts
|
||||
fused_moe_layer = MagicMock(spec=fused_moe_cls)
|
||||
fused_moe_layer.moe = MagicMock(spec=FusedMoEConfig)
|
||||
fused_moe_layer.moe_config = MagicMock(spec=FusedMoEConfig)
|
||||
fused_moe_layer.moe_config.moe_backend = "auto"
|
||||
fused_moe_layer.moe_config.moe_parallel_config = MagicMock(spec=FusedMoEParallelConfig)
|
||||
fused_moe_layer.moe_config.moe_parallel_config.use_ep = True
|
||||
fused_moe_layer.moe_config.moe_parallel_config.dp_size = 1
|
||||
mock_config = MagicMock()
|
||||
mock_config.model_config.hf_config.model_type = None
|
||||
mock_config.compilation_config.custom_ops = ["all"]
|
||||
mock_scheme = MagicMock()
|
||||
# Test skipped layer
|
||||
with (
|
||||
patch("vllm.config.vllm.get_current_vllm_config", return_value=mock_config),
|
||||
patch("vllm_ascend._310p.quantization.modelslim_config.get_current_vllm_config", return_value=mock_config),
|
||||
patch("vllm_ascend.quantization.modelslim_config.get_current_vllm_config", return_value=mock_config),
|
||||
patch.object(self.ascend_config, "is_layer_skipped_ascend", return_value=True),
|
||||
):
|
||||
method = self.ascend_config.get_quant_method(fused_moe_layer, ".moe")
|
||||
self.assertIsInstance(method, AscendUnquantizedFusedMoEMethod310)
|
||||
|
||||
# Test quantized layer
|
||||
mock_scheme = MagicMock()
|
||||
with (
|
||||
patch.object(self.ascend_config, "is_layer_skipped_ascend", return_value=False),
|
||||
patch("vllm.config.vllm.get_current_vllm_config", return_value=mock_config),
|
||||
patch("vllm_ascend._310p.quantization.modelslim_config.get_current_vllm_config", return_value=mock_config),
|
||||
patch("vllm_ascend.quantization.modelslim_config.get_current_vllm_config", return_value=mock_config),
|
||||
patch("vllm_ascend._310p.quantization.modelslim_config.create_scheme_for_layer", return_value=mock_scheme),
|
||||
patch(
|
||||
"vllm_ascend._310p.quantization.modelslim_config.AscendFusedMoEMethod", return_value=MagicMock()
|
||||
) as fused_moe_method,
|
||||
):
|
||||
method = self.ascend_config.get_quant_method(fused_moe_layer, ".moe")
|
||||
self.assertIs(method, fused_moe_method.return_value)
|
||||
fused_moe_method.assert_called_once_with(mock_scheme, fused_moe_layer.moe_config)
|
||||
145
tests/ut/_310p/quantization/test_w8a8_dynamic_310.py
Normal file
145
tests/ut/_310p/quantization/test_w8a8_dynamic_310.py
Normal file
@@ -0,0 +1,145 @@
|
||||
#
|
||||
# Copyright (c) 2026 Huawei Technologies Co., Ltd. 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.
|
||||
|
||||
from unittest.mock import MagicMock, Mock, patch
|
||||
|
||||
import torch
|
||||
|
||||
from tests.ut.base import TestBase
|
||||
from vllm_ascend._310p.quantization.methods.w8a8_dynamic import (
|
||||
AscendW8A8DynamicFusedMoEMethod310,
|
||||
AscendW8A8DynamicLinearMethod310,
|
||||
)
|
||||
|
||||
|
||||
class TestAscendW8A8FusedMoEMethod310(TestBase):
|
||||
num_experts = 8
|
||||
hidden_size = 128
|
||||
intermediate_size = 128
|
||||
|
||||
@patch("vllm_ascend._310p.quantization.methods.w8a8_dynamic.get_ep_group")
|
||||
def setUp(self, mock_get_ep_group):
|
||||
with patch(
|
||||
"vllm_ascend._310p.quantization.methods.w8a8_dynamic.get_current_vllm_config"
|
||||
) as mock_get_current_vllm_config:
|
||||
mock_vllm_config = Mock()
|
||||
mock_vllm_config.quant_config = Mock(quant_description={"group_size": 0})
|
||||
mock_vllm_config.scheduler_config = Mock(
|
||||
max_num_batched_tokens=2048, max_model_len=2048, enable_chunked_prefill=False
|
||||
)
|
||||
mock_get_current_vllm_config.return_value = mock_vllm_config
|
||||
mock_ep_group = Mock()
|
||||
mock_get_ep_group.return_value = mock_ep_group
|
||||
mock_ascend_config = Mock()
|
||||
|
||||
mock_ascend_config.enable_chunked_prefill = False
|
||||
|
||||
self.quant_method = AscendW8A8DynamicFusedMoEMethod310()
|
||||
|
||||
def test_get_weight_310(self):
|
||||
param_dict = self.quant_method.get_weight(
|
||||
self.num_experts, self.intermediate_size, self.hidden_size, torch.float16
|
||||
)
|
||||
self.assertEqual(param_dict["w13_weight"].dtype, torch.int8)
|
||||
self.assertEqual(
|
||||
param_dict["w13_weight"].shape, (self.num_experts, 2 * self.intermediate_size, self.hidden_size)
|
||||
)
|
||||
self.assertEqual(param_dict["w2_weight"].dtype, torch.int8)
|
||||
self.assertEqual(param_dict["w2_weight"].shape, (self.num_experts, self.hidden_size, self.intermediate_size))
|
||||
|
||||
def test_get_dynamic_quant_param_310(self):
|
||||
param_dict = self.quant_method.get_dynamic_quant_param(
|
||||
self.num_experts, self.intermediate_size, self.hidden_size, torch.float16
|
||||
)
|
||||
self.assertEqual(param_dict["w13_weight_scale"].dtype, torch.float32)
|
||||
self.assertEqual(param_dict["w13_weight_scale"].shape, (self.num_experts, 2 * self.intermediate_size, 1))
|
||||
self.assertEqual(param_dict["w2_weight_scale"].dtype, torch.float32)
|
||||
self.assertEqual(param_dict["w2_weight_scale"].shape, (self.num_experts, self.hidden_size, 1))
|
||||
|
||||
|
||||
class TestAscendW8A8DynamicLinearMethod310(TestBase):
|
||||
def setUp(self):
|
||||
self.method = AscendW8A8DynamicLinearMethod310()
|
||||
|
||||
def test_get_weight_310(self):
|
||||
weight = self.method.get_weight(10, 20)
|
||||
self.assertEqual(weight["weight"].dtype, torch.int8)
|
||||
self.assertEqual(weight["weight"].shape, (20, 10))
|
||||
|
||||
def test_get_perchannel_param_310(self):
|
||||
params = self.method.get_perchannel_param(10, torch.float32)
|
||||
|
||||
self.assertEqual(params["weight_scale"].dtype, torch.float32)
|
||||
self.assertEqual(params["weight_offset"].dtype, torch.float32)
|
||||
|
||||
self.assertEqual(params["weight_scale"].shape, (10, 1))
|
||||
self.assertEqual(params["weight_offset"].shape, (10, 1))
|
||||
|
||||
@patch("torch_npu.npu_dynamic_quant", create=True)
|
||||
@patch("torch_npu.npu_quant_matmul")
|
||||
def test_apply_310(self, mock_npu_quant_matmul, mock_npu_dynamic_quantize):
|
||||
layer = MagicMock()
|
||||
layer.weight = torch.randn(128, 256, dtype=torch.float16)
|
||||
layer.weight_scale = torch.randn(128, dtype=torch.float32)
|
||||
layer.params_dtype = torch.float16
|
||||
|
||||
x = torch.randn(32, 128, dtype=torch.float16)
|
||||
expect_x_output = torch.randint(-128, 127, x.shape, dtype=torch.int8)
|
||||
expect_pertoken_scale_output = torch.randn(x.shape[0], dtype=torch.float32)
|
||||
mock_npu_dynamic_quantize.return_value = expect_x_output, expect_pertoken_scale_output
|
||||
|
||||
expected_y_output = torch.randn(32, 256)
|
||||
mock_npu_quant_matmul.return_value = expected_y_output
|
||||
|
||||
output = self.method.apply(layer, x, tp_rank=0)
|
||||
|
||||
mock_npu_dynamic_quantize.assert_called_with(x)
|
||||
mock_npu_quant_matmul.assert_called_once()
|
||||
(args, kwargs) = mock_npu_quant_matmul.call_args
|
||||
|
||||
# positional args
|
||||
self.assertTrue(torch.equal(args[0], expect_x_output))
|
||||
self.assertTrue(torch.equal(args[1], layer.weight.data))
|
||||
self.assertTrue(torch.equal(args[2], layer.weight_scale))
|
||||
|
||||
# kwargs
|
||||
self.assertTrue(torch.equal(kwargs["pertoken_scale"], expect_pertoken_scale_output))
|
||||
self.assertTrue(kwargs["bias"] is None)
|
||||
self.assertEqual(kwargs["output_dtype"], layer.params_dtype)
|
||||
|
||||
self.assertTrue(torch.equal(output, expected_y_output))
|
||||
|
||||
@patch("vllm_ascend.utils.is_310p", return_value=True)
|
||||
@patch("torch_npu.npu_format_cast")
|
||||
def test_process_weights_after_loading_calls_nz_format_cast_310p(self, mock_npu_format_cast, _mock_is_310p):
|
||||
mock_npu_format_cast.side_effect = lambda x, fmt: x
|
||||
|
||||
layer = MagicMock()
|
||||
|
||||
# Attributes used by process_weights_after_loading()
|
||||
layer.weight = MagicMock()
|
||||
layer.weight_scale = MagicMock()
|
||||
layer.weight_offset = MagicMock()
|
||||
|
||||
layer.weight.data = torch.randint(-127, 128, (128, 256), dtype=torch.int8)
|
||||
|
||||
layer.weight_scale.data = torch.randn(128, 1, dtype=torch.bfloat16)
|
||||
layer.weight_offset.data = torch.randn(128, 1, dtype=torch.bfloat16)
|
||||
# w2_weight_offset is reshaped to (N, -1); any (N, 1) is fine
|
||||
layer.w2_weight_offset.data = torch.randn(128, 1, dtype=torch.bfloat16)
|
||||
|
||||
self.method.process_weights_after_loading(layer)
|
||||
|
||||
mock_npu_format_cast.assert_called_once()
|
||||
151
tests/ut/_310p/quantization/test_w8a8_static_310.py
Normal file
151
tests/ut/_310p/quantization/test_w8a8_static_310.py
Normal file
@@ -0,0 +1,151 @@
|
||||
#
|
||||
# Copyright (c) 2026 Huawei Technologies Co., Ltd. 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.
|
||||
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import torch
|
||||
|
||||
from tests.ut.base import TestBase
|
||||
from vllm_ascend._310p.quantization.methods.w8a8_static import AscendW8A8LinearMethod310
|
||||
|
||||
|
||||
class TestAscendW8A8LinearMethod310(TestBase):
|
||||
def setUp(self):
|
||||
self.method = AscendW8A8LinearMethod310()
|
||||
|
||||
def test_get_weight_310(self):
|
||||
weight = self.method.get_weight(10, 20)
|
||||
self.assertEqual(weight["weight"].dtype, torch.int8)
|
||||
self.assertEqual(weight["weight"].shape, (20, 10))
|
||||
|
||||
def test_get_pertensor_param_310(self):
|
||||
params = self.method.get_pertensor_param(torch.float16)
|
||||
self.assertEqual(params["input_scale"].dtype, torch.float16)
|
||||
self.assertEqual(params["input_offset"].dtype, torch.int8)
|
||||
self.assertEqual(params["input_scale"].shape, (1,))
|
||||
self.assertEqual(params["input_offset"].shape, (1,))
|
||||
|
||||
def test_get_perchannel_param_310(self):
|
||||
params = self.method.get_perchannel_param(10, torch.float16)
|
||||
|
||||
self.assertEqual(params["quant_bias"].dtype, torch.int32)
|
||||
self.assertEqual(params["deq_scale"].dtype, torch.int64)
|
||||
self.assertEqual(params["weight_scale"].dtype, torch.float16)
|
||||
self.assertEqual(params["weight_offset"].dtype, torch.float16)
|
||||
|
||||
self.assertEqual(params["quant_bias"].shape, (10,))
|
||||
self.assertEqual(params["deq_scale"].shape, (10,))
|
||||
self.assertEqual(params["weight_scale"].shape, (10, 1))
|
||||
self.assertEqual(params["weight_offset"].shape, (10, 1))
|
||||
|
||||
@patch("torch.ops.vllm.quantize")
|
||||
@patch("torch_npu.npu_quant_matmul")
|
||||
def test_apply_with_x_not_int8_310(self, mock_npu_quant_matmul, mock_quantize):
|
||||
layer = MagicMock()
|
||||
layer.aclnn_input_scale = torch.randn(256)
|
||||
layer.aclnn_input_scale_reciprocal = 1.0 / layer.aclnn_input_scale
|
||||
layer.aclnn_input_offset = torch.randint(-128, 127, (256,), dtype=torch.int8)
|
||||
layer.weight = torch.randn(128, 256)
|
||||
layer.deq_scale = torch.randn(128)
|
||||
layer.quant_bias = torch.randint(-128, 127, (256,))
|
||||
layer.params_dtype = torch.float16
|
||||
|
||||
x = torch.randn(32, 128)
|
||||
expect_x_output = torch.randint(-128, 127, x.shape, dtype=torch.int8)
|
||||
mock_quantize.return_value = expect_x_output
|
||||
|
||||
expected_y_output = torch.randn(32, 256)
|
||||
mock_npu_quant_matmul.return_value = expected_y_output
|
||||
|
||||
output = self.method.apply(layer, x, tp_rank=0)
|
||||
|
||||
mock_quantize.assert_called_with(
|
||||
x,
|
||||
layer.aclnn_input_scale,
|
||||
layer.aclnn_input_scale_reciprocal,
|
||||
layer.aclnn_input_offset,
|
||||
)
|
||||
mock_npu_quant_matmul.assert_called_once()
|
||||
(args, kwargs) = mock_npu_quant_matmul.call_args
|
||||
|
||||
# positional args
|
||||
self.assertTrue(torch.equal(args[0], expect_x_output))
|
||||
self.assertTrue(torch.equal(args[1], layer.weight.data))
|
||||
self.assertTrue(torch.equal(args[2], layer.deq_scale))
|
||||
|
||||
# kwargs
|
||||
self.assertTrue(torch.equal(kwargs["bias"], layer.quant_bias))
|
||||
self.assertEqual(kwargs["output_dtype"], layer.params_dtype)
|
||||
|
||||
self.assertTrue(torch.equal(output, expected_y_output))
|
||||
|
||||
@patch("torch.ops.vllm.quantize")
|
||||
@patch("torch_npu.npu_quant_matmul")
|
||||
def test_apply_with_x_is_int8_310(self, mock_npu_quant_matmul, mock_quantize):
|
||||
layer = MagicMock()
|
||||
layer.aclnn_input_scale = torch.randn(256)
|
||||
layer.aclnn_input_offset = torch.randint(-128, 127, (256,), dtype=torch.int8)
|
||||
layer.weight = torch.randn(128, 256)
|
||||
layer.deq_scale = torch.randn(128)
|
||||
layer.quant_bias = torch.randint(-128, 127, (256,))
|
||||
layer.params_dtype = torch.float16
|
||||
|
||||
x = torch.randint(-128, 127, (32, 128), dtype=torch.int8)
|
||||
|
||||
expected_y_output = torch.randn(32, 256)
|
||||
mock_npu_quant_matmul.return_value = expected_y_output
|
||||
|
||||
output = self.method.apply(layer, x, tp_rank=0)
|
||||
|
||||
mock_quantize.assert_not_called()
|
||||
mock_npu_quant_matmul.assert_called_once()
|
||||
(args, kwargs) = mock_npu_quant_matmul.call_args
|
||||
|
||||
self.assertTrue(torch.equal(args[0], x))
|
||||
self.assertTrue(torch.equal(args[1], layer.weight.data))
|
||||
self.assertTrue(torch.equal(args[2], layer.deq_scale))
|
||||
|
||||
self.assertTrue(torch.equal(kwargs["bias"], layer.quant_bias))
|
||||
self.assertEqual(kwargs["output_dtype"], layer.params_dtype)
|
||||
|
||||
self.assertTrue(torch.equal(output, expected_y_output))
|
||||
|
||||
@patch("vllm_ascend.utils.is_310p", return_value=True)
|
||||
@patch("torch_npu.npu_format_cast")
|
||||
def test_process_weights_after_loading_calls_nz_format_cast_310p(self, mock_npu_format_cast, _mock_is_310p):
|
||||
mock_npu_format_cast.side_effect = lambda x, fmt: x
|
||||
|
||||
layer = MagicMock()
|
||||
|
||||
# Attributes used by process_weights_after_loading()
|
||||
layer.weight = MagicMock()
|
||||
layer.input_scale = MagicMock()
|
||||
layer.input_offset = MagicMock()
|
||||
layer.weight_scale = MagicMock()
|
||||
layer.weight_offset = MagicMock()
|
||||
layer.w2_weight_offset = MagicMock()
|
||||
|
||||
layer.weight.data = torch.randint(-127, 128, (128, 256), dtype=torch.int8)
|
||||
layer.input_scale.data = torch.tensor([0.1], dtype=torch.float16)
|
||||
layer.input_offset.data = torch.tensor([0], dtype=torch.int8)
|
||||
|
||||
layer.weight_scale.data = torch.randn(128, 1, dtype=torch.bfloat16)
|
||||
layer.weight_offset.data = torch.randn(128, 1, dtype=torch.bfloat16)
|
||||
# w2_weight_offset is reshaped to (N, -1); any (N, 1) is fine
|
||||
layer.w2_weight_offset.data = torch.randn(128, 1, dtype=torch.bfloat16)
|
||||
|
||||
self.method.process_weights_after_loading(layer)
|
||||
|
||||
mock_npu_format_cast.assert_called_once()
|
||||
93
tests/ut/_310p/quantization/test_w8a8s_310.py
Normal file
93
tests/ut/_310p/quantization/test_w8a8s_310.py
Normal file
@@ -0,0 +1,93 @@
|
||||
#
|
||||
# Copyright (c) 2026 Huawei Technologies Co., Ltd. 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.
|
||||
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import torch
|
||||
|
||||
from tests.ut.base import TestBase
|
||||
from vllm_ascend._310p.quantization.methods.w8a8s import AscendW8A8SLinearMethod310
|
||||
|
||||
|
||||
class TestAscendW8A8SLinearMethod310(TestBase):
|
||||
def setUp(self):
|
||||
self.method = AscendW8A8SLinearMethod310()
|
||||
|
||||
def test_get_weight_310(self):
|
||||
weight = self.method.get_weight(10, 20)
|
||||
self.assertEqual(weight["weight"].dtype, torch.int8)
|
||||
self.assertEqual(weight["weight"].shape, (20, 10))
|
||||
|
||||
def test_get_pertensor_param_310(self):
|
||||
params = self.method.get_pertensor_param(torch.float16)
|
||||
self.assertEqual(params["input_scale"].dtype, torch.float16)
|
||||
self.assertEqual(params["input_offset"].dtype, torch.int8)
|
||||
self.assertEqual(params["input_scale"].shape, (1,))
|
||||
self.assertEqual(params["input_offset"].shape, (1,))
|
||||
|
||||
def test_get_perchannel_param_310(self):
|
||||
params = self.method.get_perchannel_param(10, torch.float16)
|
||||
|
||||
self.assertEqual(params["quant_bias"].dtype, torch.int32)
|
||||
self.assertEqual(params["deq_scale"].dtype, torch.int64)
|
||||
self.assertEqual(params["quant_bias"].shape, (10,))
|
||||
self.assertEqual(params["deq_scale"].shape, (10,))
|
||||
|
||||
@patch("torch.ops.vllm.quantize")
|
||||
@patch("torch_npu.npu_quant_matmul")
|
||||
def test_apply_with_x_not_int8_310(self, mock_npu_quant_matmul, mock_quantize):
|
||||
layer = MagicMock()
|
||||
layer.aclnn_input_scale = torch.randn(256)
|
||||
layer.aclnn_input_scale_reciprocal = 1.0 / layer.aclnn_input_scale
|
||||
layer.aclnn_input_offset = torch.randint(-128, 127, (256,), dtype=torch.int8)
|
||||
layer.weight = torch.randn(128, 256)
|
||||
layer.deq_scale = torch.randn(128)
|
||||
layer.quant_bias = torch.randint(-128, 127, (256,))
|
||||
layer.params_dtype = torch.float16
|
||||
|
||||
x = torch.randn(32, 128)
|
||||
expect_x_output = torch.randint(-128, 127, x.shape, dtype=torch.int8)
|
||||
mock_quantize.return_value = expect_x_output
|
||||
|
||||
expected_y_output = torch.randn(32, 256)
|
||||
mock_npu_quant_matmul.return_value = expected_y_output
|
||||
|
||||
output = self.method.apply(layer, x, tp_rank=0)
|
||||
|
||||
mock_quantize.assert_called_with(
|
||||
x, layer.aclnn_input_scale, layer.aclnn_input_scale_reciprocal, layer.aclnn_input_offset
|
||||
)
|
||||
self.assertTrue(torch.equal(output, expected_y_output))
|
||||
|
||||
@patch("torch.ops.vllm.quantize")
|
||||
@patch("torch_npu.npu_quant_matmul")
|
||||
def test_apply_with_x_is_int8_310(self, mock_npu_quant_matmul, mock_quantize):
|
||||
layer = MagicMock()
|
||||
layer.aclnn_input_scale = torch.randn(256)
|
||||
layer.aclnn_input_offset = torch.randint(-128, 127, (256,), dtype=torch.int8)
|
||||
layer.weight = torch.randn(128, 256)
|
||||
layer.deq_scale = torch.randn(128)
|
||||
layer.quant_bias = torch.randint(-128, 127, (256,))
|
||||
layer.params_dtype = torch.float16
|
||||
|
||||
x = torch.randint(-128, 127, (32, 128), dtype=torch.int8)
|
||||
|
||||
expected_y_output = torch.randn(32, 256)
|
||||
mock_npu_quant_matmul.return_value = expected_y_output
|
||||
|
||||
output = self.method.apply(layer, x, tp_rank=0)
|
||||
|
||||
mock_quantize.assert_not_called()
|
||||
self.assertTrue(torch.equal(output, expected_y_output))
|
||||
108
tests/ut/_310p/quantization/test_w8a8sc_310.py
Normal file
108
tests/ut/_310p/quantization/test_w8a8sc_310.py
Normal file
@@ -0,0 +1,108 @@
|
||||
#
|
||||
# Copyright (c) 2026 Huawei Technologies Co., Ltd. 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 math
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
import torch
|
||||
|
||||
from tests.ut.base import TestBase
|
||||
from vllm_ascend._310p.quantization.methods.w8a8sc import AscendW8A8SCLinearMethod310
|
||||
|
||||
|
||||
class TestAscendW8A8SCLinearMethod310(TestBase):
|
||||
def setUp(self):
|
||||
self.method = AscendW8A8SCLinearMethod310()
|
||||
|
||||
def test_get_weight_310(self):
|
||||
weight = self.method.get_weight(10, 20)
|
||||
self.assertEqual(weight["weight"].dtype, torch.int8)
|
||||
self.assertEqual(weight["weight"].shape, (10 * 20,))
|
||||
self.assertEqual(weight["index"].dtype, torch.int8)
|
||||
index_len = math.ceil(10 / 256) * math.ceil(20 / 128) * 8
|
||||
self.assertEqual(weight["index"].shape, (index_len,))
|
||||
self.assertEqual(weight["info"].dtype, torch.int64)
|
||||
self.assertEqual(weight["info"].shape, (5,))
|
||||
|
||||
def test_get_pertensor_param_310(self):
|
||||
params = self.method.get_pertensor_param(torch.float16)
|
||||
self.assertEqual(params["input_scale"].dtype, torch.float16)
|
||||
self.assertEqual(params["input_offset"].dtype, torch.int8)
|
||||
self.assertEqual(params["input_scale"].shape, (1,))
|
||||
self.assertEqual(params["input_offset"].shape, (1,))
|
||||
|
||||
def test_get_perchannel_param_310(self):
|
||||
params = self.method.get_perchannel_param(10, torch.float16)
|
||||
|
||||
self.assertEqual(params["quant_bias"].dtype, torch.int32)
|
||||
self.assertEqual(params["deq_scale"].dtype, torch.int64)
|
||||
self.assertEqual(params["quant_bias"].shape, (10,))
|
||||
self.assertEqual(params["deq_scale"].shape, (10,))
|
||||
|
||||
@pytest.mark.skip("Skip as npu_matmul_compress_dequant will be supported in PTA 26.0.0.")
|
||||
@patch("torch.ops.vllm.quantize")
|
||||
@patch("torch_npu.npu_matmul_compress_dequant")
|
||||
def test_apply_with_x_not_int8_310(self, mock_matmul_compress_dequant, mock_quantize):
|
||||
layer = MagicMock()
|
||||
layer.aclnn_input_scale = torch.randn(256)
|
||||
layer.aclnn_input_scale_reciprocal = 1.0 / layer.aclnn_input_scale
|
||||
layer.aclnn_input_offset = torch.randint(-128, 127, (256,), dtype=torch.int8)
|
||||
layer.weight = torch.randint(-128, 127, (256 * 128,), dtype=torch.int8)
|
||||
layer.index = torch.randint(-128, 127, (8,), dtype=torch.int8)
|
||||
layer.deq_scale = torch.randn(128)
|
||||
layer.quant_bias = torch.randint(-128, 127, (256,))
|
||||
layer.params_dtype = torch.float16
|
||||
|
||||
x = torch.randn(32, 128)
|
||||
expect_x_output = torch.randint(-128, 127, x.shape, dtype=torch.int8)
|
||||
mock_quantize.return_value = expect_x_output
|
||||
|
||||
expected_y_output = torch.randn(32, 256)
|
||||
mock_matmul_compress_dequant.return_value = expected_y_output
|
||||
|
||||
output = self.method.apply(layer, x, tp_rank=0)
|
||||
|
||||
mock_quantize.assert_called_with(
|
||||
x, layer.aclnn_input_scale, layer.aclnn_input_scale_reciprocal, layer.aclnn_input_offset
|
||||
)
|
||||
mock_matmul_compress_dequant.assert_called_with(
|
||||
expect_x_output, layer.weight, layer.index, layer.quant_bias, layer.deq_scale
|
||||
)
|
||||
self.assertTrue(torch.equal(output, expected_y_output))
|
||||
|
||||
@pytest.mark.skip("Skip as npu_matmul_compress_dequant will be supported in PTA 26.0.0.")
|
||||
@patch("torch.ops.vllm.quantize")
|
||||
@patch("torch_npu.npu_matmul_compress_dequant")
|
||||
def test_apply_with_x_is_int8_310(self, mock_matmul_compress_dequant, mock_quantize):
|
||||
layer = MagicMock()
|
||||
layer.aclnn_input_scale = torch.randn(256)
|
||||
layer.aclnn_input_offset = torch.randint(-128, 127, (256,), dtype=torch.int8)
|
||||
layer.weight = torch.randint(-128, 127, (256 * 128,), dtype=torch.int8)
|
||||
layer.index = torch.randint(-128, 127, (8,), dtype=torch.int8)
|
||||
layer.deq_scale = torch.randn(128)
|
||||
layer.quant_bias = torch.randint(-128, 127, (256,))
|
||||
layer.params_dtype = torch.float16
|
||||
|
||||
x = torch.randint(-128, 127, (32, 128), dtype=torch.int8)
|
||||
|
||||
expected_y_output = torch.randn(32, 256)
|
||||
mock_matmul_compress_dequant.return_value = expected_y_output
|
||||
|
||||
output = self.method.apply(layer, x, tp_rank=0)
|
||||
|
||||
mock_quantize.assert_not_called()
|
||||
mock_matmul_compress_dequant.assert_called_with(x, layer.weight, layer.index, layer.quant_bias, layer.deq_scale)
|
||||
self.assertTrue(torch.equal(output, expected_y_output))
|
||||
Reference in New Issue
Block a user