mirror of
https://github.com/PaddlePaddle/FastDeploy.git
synced 2025-12-24 13:28:13 +08:00
[CI] Add unittest for activation, native_paddle_backend, w4a8, w4afp8, platforms/utils (#4812)
* add unnitest for activation, native_paddle_backend, w4a8, w4afp8, platforms/utils * Remove activation function retrieval tests Removed tests for valid and unsupported activation function retrieval. * move w4a8, w4afp8 to quantization * fix code style
This commit is contained in:
62
tests/quantization/test_w4a8.py
Normal file
62
tests/quantization/test_w4a8.py
Normal file
@@ -0,0 +1,62 @@
|
||||
import unittest
|
||||
from unittest import mock
|
||||
|
||||
from fastdeploy.model_executor.layers.quantization.w4a8 import W4A8Config
|
||||
from fastdeploy.platforms import current_platform
|
||||
|
||||
|
||||
class TestW4A8Config(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.config = W4A8Config(is_permuted=False, hadamard_block_size=128)
|
||||
|
||||
def test_name(self):
|
||||
"""Test name() method"""
|
||||
self.assertEqual(self.config.name(), "w4a8")
|
||||
|
||||
def test_from_config_defaults(self):
|
||||
"""Test from_config with empty dict uses defaults"""
|
||||
cfg = W4A8Config.from_config({})
|
||||
self.assertTrue(cfg.is_permuted)
|
||||
self.assertEqual(cfg.hadamard_block_size, 128)
|
||||
|
||||
def test_from_config_full(self):
|
||||
"""Test from_config with full dict"""
|
||||
cfg = W4A8Config.from_config({"is_permuted": False, "hadamard_block_size": 64})
|
||||
self.assertFalse(cfg.is_permuted)
|
||||
self.assertEqual(cfg.hadamard_block_size, 64)
|
||||
|
||||
def test_get_quant_method_cuda(self):
|
||||
"""Test get_quant_method returns CUDA method when on CUDA platform"""
|
||||
with (
|
||||
mock.patch.object(current_platform, "is_cuda", return_value=True),
|
||||
mock.patch(
|
||||
"fastdeploy.model_executor.layers.moe.fused_moe_cutlass_backend.CutlassW4A8MoEMethod"
|
||||
) as mock_cuda,
|
||||
):
|
||||
layer = mock.Mock()
|
||||
method = self.config.get_quant_method(layer)
|
||||
mock_cuda.assert_called_once_with(self.config)
|
||||
self.assertEqual(method, mock_cuda.return_value)
|
||||
|
||||
@unittest.skipIf(not hasattr(current_platform, "is_xpu") or not current_platform.is_xpu(), "No XPU, skip test")
|
||||
def test_get_quant_method_xpu(self):
|
||||
"""Test get_quant_method returns XPU method when on XPU platform"""
|
||||
with mock.patch("fastdeploy.model_executor.layers.backends.xpu.moe.fused_moe.XPUW4A8MoEMethod") as mock_xpu:
|
||||
layer = mock.Mock()
|
||||
method = self.config.get_quant_method(layer)
|
||||
mock_xpu.assert_called_once_with(self.config)
|
||||
self.assertEqual(method, mock_xpu.return_value)
|
||||
|
||||
def test_get_quant_method_unsupported(self):
|
||||
"""Test that unsupported platform raises ValueError"""
|
||||
with (
|
||||
mock.patch.object(current_platform, "is_cuda", return_value=False),
|
||||
mock.patch.object(current_platform, "is_xpu", return_value=False),
|
||||
):
|
||||
layer = mock.Mock()
|
||||
with self.assertRaises(ValueError):
|
||||
self.config.get_quant_method(layer)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
142
tests/quantization/test_w4afp8.py
Normal file
142
tests/quantization/test_w4afp8.py
Normal file
@@ -0,0 +1,142 @@
|
||||
import unittest
|
||||
from unittest import mock
|
||||
|
||||
from fastdeploy.model_executor.layers.moe import FusedMoE
|
||||
from fastdeploy.model_executor.layers.quantization.w4afp8 import (
|
||||
QUANT_SCALING_FACTOR,
|
||||
W4AFP8Config,
|
||||
W4AFP8LinearMethod,
|
||||
)
|
||||
|
||||
|
||||
class TestW4AFP8(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.config = W4AFP8Config(
|
||||
weight_scale_dict={"layer.weight_scale": 1.0},
|
||||
act_scale_dict={"layer.activation_scale": 1.0},
|
||||
is_permuted=False,
|
||||
hadamard_block_size=128,
|
||||
)
|
||||
self.method = W4AFP8LinearMethod(self.config)
|
||||
|
||||
# Mock layer
|
||||
self.layer = mock.Mock()
|
||||
self.layer.weight_shape = [8, 4]
|
||||
self.layer.create_parameter.return_value = "created_weight"
|
||||
self.layer.bias = "bias"
|
||||
self.layer.add_bias = True
|
||||
self.layer._dtype = "float16"
|
||||
self.layer.prefix = "layer"
|
||||
|
||||
def test_name(self):
|
||||
self.assertEqual(self.config.name(), "w4afp8")
|
||||
|
||||
def test_from_config_defaults(self):
|
||||
cfg = W4AFP8Config.from_config({})
|
||||
self.assertTrue(cfg.is_permuted)
|
||||
self.assertEqual(cfg.hadamard_block_size, 128)
|
||||
|
||||
def test_from_config_full(self):
|
||||
cfg = W4AFP8Config.from_config(
|
||||
{
|
||||
"weight_scale_dict": {"a": 1},
|
||||
"act_scale_dict": {"b": 2},
|
||||
"is_permuted": False,
|
||||
"hadamard_block_size": 64,
|
||||
}
|
||||
)
|
||||
self.assertEqual(cfg.weight_scale_dict["a"], 1)
|
||||
self.assertEqual(cfg.hadamard_block_size, 64)
|
||||
self.assertFalse(cfg.is_permuted)
|
||||
|
||||
def test_get_quant_method_linear(self):
|
||||
# Non-FusedMoE path
|
||||
method = self.config.get_quant_method(mock.Mock())
|
||||
self.assertIsInstance(method, W4AFP8LinearMethod)
|
||||
|
||||
@mock.patch("fastdeploy.model_executor.layers.moe.fused_moe_cutlass_backend.CutlassW4AFP8MoEMethod")
|
||||
def test_get_quant_method_moe(self, mock_cutlass):
|
||||
# Mock FusedMoE instance
|
||||
layer = mock.Mock(spec=FusedMoE)
|
||||
type(layer).return_value = None
|
||||
result = self.config.get_quant_method(layer)
|
||||
|
||||
mock_cutlass.assert_called_once_with(self.config)
|
||||
self.assertEqual(result, mock_cutlass.return_value)
|
||||
|
||||
def test_create_weights(self):
|
||||
original_shape = [8, 4]
|
||||
self.layer.weight_shape = original_shape.copy()
|
||||
|
||||
self.method.create_weights(self.layer)
|
||||
self.assertEqual(self.layer.weight_dtype, "int8")
|
||||
self.assertEqual(self.layer.weight, "created_weight")
|
||||
self.assertEqual(self.layer.weight_shape, [2, 8])
|
||||
|
||||
@mock.patch("fastdeploy.model_executor.ops.gpu.scaled_gemm_f8_i4_f16_weight_quantize")
|
||||
@mock.patch("paddle.view")
|
||||
@mock.patch("paddle.cast")
|
||||
def test_process_loaded_weights(self, mock_cast, mock_view, mock_quant):
|
||||
mock_cast.return_value.cpu.return_value = "cpu_tensor"
|
||||
mock_quant.return_value = ("quanted_weight", "weight_scale")
|
||||
mock_view.return_value = "reshaped_scale"
|
||||
|
||||
self.layer.weight = mock.Mock()
|
||||
self.layer.weight_scale = mock.Mock()
|
||||
|
||||
self.method.process_loaded_weights(self.layer, "weights")
|
||||
|
||||
mock_cast.assert_called_once_with("weights", "float32")
|
||||
mock_quant.assert_called_once()
|
||||
mock_view.assert_called_once_with("weight_scale", self.layer._dtype)
|
||||
self.layer.weight.set_value.assert_called_once_with("quanted_weight")
|
||||
self.layer.weight_scale.set_value.assert_called_once_with("reshaped_scale")
|
||||
|
||||
@mock.patch("fastdeploy.model_executor.ops.gpu.scaled_gemm_f8_i4_f16_weight_quantize")
|
||||
@mock.patch("paddle.view")
|
||||
@mock.patch("paddle.cast")
|
||||
def test_process_loaded_weights_with_error(self, mock_cast, mock_view, mock_quant):
|
||||
mock_cast.return_value.cpu.return_value = "cpu_tensor"
|
||||
mock_quant.return_value = (None, None)
|
||||
self.layer.weight = mock.Mock()
|
||||
self.layer.weight_scale = mock.Mock()
|
||||
|
||||
self.method.process_loaded_weights(self.layer, "weights")
|
||||
|
||||
@mock.patch("fastdeploy.model_executor.ops.gpu.scaled_gemm_f8_i4_f16")
|
||||
def test_apply_with_bias(self, mock_gemm):
|
||||
mock_gemm.return_value = "output"
|
||||
x = mock.Mock()
|
||||
self.layer.weight = "w"
|
||||
self.layer.weight_scale = "s"
|
||||
|
||||
result = self.method.apply(self.layer, x)
|
||||
mock_gemm.assert_called_once()
|
||||
self.assertEqual(result, "output")
|
||||
|
||||
# Verify out_scale value
|
||||
call_args = mock_gemm.call_args.kwargs
|
||||
expected_out_scale = 1.0 / (1.0 * QUANT_SCALING_FACTOR * QUANT_SCALING_FACTOR)
|
||||
self.assertAlmostEqual(call_args["out_scale"], expected_out_scale)
|
||||
|
||||
@mock.patch("fastdeploy.model_executor.ops.gpu.scaled_gemm_f8_i4_f16")
|
||||
def test_apply_without_bias(self, mock_gemm):
|
||||
self.layer.add_bias = False
|
||||
mock_gemm.return_value = "out"
|
||||
x = "x"
|
||||
|
||||
result = self.method.apply(self.layer, x)
|
||||
self.assertEqual(result, "out")
|
||||
args = mock_gemm.call_args.kwargs
|
||||
self.assertIsNone(args["bias"])
|
||||
|
||||
@mock.patch("fastdeploy.model_executor.ops.gpu.scaled_gemm_f8_i4_f16")
|
||||
def test_apply_prefix_missing_key(self, mock_gemm):
|
||||
self.layer.prefix = "unknown"
|
||||
x = mock.Mock()
|
||||
with self.assertRaises(TypeError):
|
||||
self.method.apply(self.layer, x)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user