-
Notifications
You must be signed in to change notification settings - Fork 744
【Hackathon 9th No.31】add test_gptq_marlin_repack [cf] #7707
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
Open
ghost
wants to merge
1
commit into
PaddlePaddle:develop
Choose a base branch
from
CloudForge-Solutions:task/031-gptq-marlin-repack-unit-test-v3
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| # Copyright (c) 2025 PaddlePaddle Authors. 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 unittest | ||
|
|
||
| import numpy as np | ||
| import paddle | ||
|
|
||
| from fastdeploy.model_executor.ops.gpu import gptq_marlin_repack | ||
|
|
||
| paddle.seed(42) | ||
| np.random.seed(42) | ||
|
|
||
|
|
||
| def _unpack_int32(packed_np, num_bits): | ||
| """Unpack int32 array into individual quantized values (sorted).""" | ||
| mask = np.uint32((1 << num_bits) - 1) | ||
| pack_factor = 32 // num_bits | ||
| flat = packed_np.flatten().astype(np.uint32) | ||
| values = [] | ||
| for shift in range(pack_factor): | ||
| values.append((flat >> np.uint32(shift * num_bits)) & mask) | ||
| return np.sort(np.concatenate(values)) | ||
|
|
||
|
|
||
| def _make_random_packed_weights(size_k, size_n, num_bits): | ||
| """Create random int32-packed quantized weight tensor on GPU.""" | ||
| pack_factor = 32 // num_bits | ||
| data = np.random.randint(0, 2**32, size=(size_k // pack_factor, size_n), dtype=np.uint32).view(np.int32) | ||
|
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. ❓ 疑问
data = np.random.randint(
np.iinfo(np.int32).min, np.iinfo(np.int32).max + 1,
size=(size_k // pack_factor, size_n),
dtype=np.int32
)或使用新式 Generator API: rng = np.random.default_rng(42)
data = rng.integers(0, 2**32, size=(size_k // pack_factor, size_n), dtype=np.uint32).view(np.int32) |
||
| return paddle.to_tensor(data, place=paddle.CUDAPlace(0)) | ||
|
|
||
|
|
||
| def _make_perm(size_k, act_order=False): | ||
| """Create perm tensor (random permutation if act_order, else empty).""" | ||
| if act_order: | ||
| return paddle.to_tensor(np.random.permutation(size_k).astype(np.int32), place=paddle.CUDAPlace(0)) | ||
| return paddle.to_tensor(np.zeros([0], dtype=np.int32), place=paddle.CUDAPlace(0)) | ||
|
|
||
|
|
||
| class TestGptqMarlinRepack(unittest.TestCase): | ||
| """Tests for gptq_marlin_repack — value conservation across repacking.""" | ||
|
|
||
| def setUp(self): | ||
| paddle.set_device("gpu") | ||
|
|
||
| def _check_conservation(self, size_k, size_n, num_bits, act_order=False): | ||
| """Verify unpacked value multisets are identical before and after repack.""" | ||
| b_q_weight = _make_random_packed_weights(size_k, size_n, num_bits) | ||
| perm = _make_perm(size_k, act_order=act_order) | ||
| out = gptq_marlin_repack(b_q_weight, perm, size_k, size_n, num_bits) | ||
|
|
||
| expected_shape = [size_k // 16, size_n * 16 // (32 // num_bits)] | ||
| self.assertEqual(list(out.shape), expected_shape) | ||
| self.assertEqual(out.dtype, paddle.int32) | ||
|
|
||
| np.testing.assert_array_equal( | ||
| _unpack_int32(b_q_weight.numpy(), num_bits), | ||
| _unpack_int32(out.numpy(), num_bits), | ||
| ) | ||
|
|
||
| def test_4bit_no_perm(self): | ||
| """4-bit repacking without act_order, multiple sizes.""" | ||
| for size_k, size_n in [(16, 64), (64, 128), (128, 256)]: | ||
| with self.subTest(size_k=size_k, size_n=size_n): | ||
| self._check_conservation(size_k, size_n, 4, act_order=False) | ||
|
|
||
| def test_8bit_no_perm(self): | ||
| """8-bit repacking without act_order, multiple sizes.""" | ||
| for size_k, size_n in [(16, 64), (64, 128), (128, 256)]: | ||
| with self.subTest(size_k=size_k, size_n=size_n): | ||
| self._check_conservation(size_k, size_n, 8, act_order=False) | ||
|
|
||
| def test_4bit_with_perm(self): | ||
| """4-bit repacking with act_order permutation.""" | ||
| for size_k, size_n in [(64, 128), (128, 256)]: | ||
| with self.subTest(size_k=size_k, size_n=size_n): | ||
| self._check_conservation(size_k, size_n, 4, act_order=True) | ||
|
|
||
| def test_8bit_with_perm(self): | ||
| """8-bit repacking with act_order permutation.""" | ||
| for size_k, size_n in [(64, 128), (128, 256)]: | ||
| with self.subTest(size_k=size_k, size_n=size_n): | ||
| self._check_conservation(size_k, size_n, 8, act_order=True) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
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.
🟡 建议 模块级别全局 seed 影响测试隔离性
paddle.seed(42)和np.random.seed(42)在模块 import 时即执行,会影响同进程内其他测试的随机状态。建议将 seed 设置移入setUp()方法中,限制影响范围: