-
Notifications
You must be signed in to change notification settings - Fork 634
【Hackathon 9th No.71】add test_weight_only.py #4109
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
base: develop
Are you sure you want to change the base?
Conversation
Thanks for your contribution! |
def test_apply_numerical_precision(self): | ||
"""Test numerical precision of quantized output""" | ||
x = paddle.to_tensor(np.random.randn(2, self.in_features).astype("float16")) | ||
|
||
# Reference FP32 output | ||
ref_out = paddle.matmul( | ||
x.astype("float32"), | ||
(self.layer.weight.astype("float32") * self.layer.weight_scale.astype("float32")).transpose([1, 0]), | ||
) | ||
if self.layer.bias is not None: | ||
ref_out += self.layer.bias.astype("float32") | ||
|
||
# Manual quantized output | ||
weight_f32 = self.layer.weight.astype("float32") | ||
x_f32 = x.astype("float32") | ||
quant_out = paddle.matmul(x_f32, weight_f32 * self.layer.weight_scale.astype("float32"), transpose_y=True) | ||
if self.layer.bias is not None: | ||
quant_out += self.layer.bias.astype("float32") | ||
quant_out = quant_out.astype("float16") | ||
|
||
np.testing.assert_allclose(ref_out.numpy(), quant_out.numpy(), rtol=1e-2, atol=1e-2) |
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.
这里好像并没有检查self.method.apply的结果
from fastdeploy.model_executor.layers.quantization.weight_only import ( | ||
GPUWeightOnlyLinearMethod, |
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.
weight_only.py 除了 GPUWeightOnlyLinearMethod,还有 MacheteWeightOnlyLinearMethod,使用的kernel实现方式不同,如果是H卡的环境也可以加一下MacheteWeightOnlyLinearMethod的case
self.fd_config.load_config = type("load_config", (), {"load_choices": "default_v1"})() | ||
|
||
# float32 weights | ||
weight_fp32 = np.random.randn(*self.weight_shape).astype("float32") |
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.
权重一般是fp16或bf16类型
# Per-channel scale | ||
max_abs = np.max(np.abs(weight_fp32), axis=1, keepdims=True) + 1e-6 | ||
scale = (max_abs / 127.0).astype("float32") | ||
|
||
# Int8 quantization | ||
weight_int8 = np.clip(np.round(weight_fp32 / scale), -128, 127).astype("int8") |
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.
这里建议调用GPUWeightOnlyLinearMethod里的process_loaded_weights进行权重的量化
return paddle.create_parameter(shape=shape, dtype=dtype, default_initializer=default_initializer) | ||
|
||
|
||
class TestGPUWeightOnlyLinearMethod(unittest.TestCase): |
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.
除了对apply方法的检查,也希望增加对process_prequanted_weights、process_loaded_weights方法的检查
add test_weight_only.py