-
Notifications
You must be signed in to change notification settings - Fork 4.9k
add quant unit test #2315
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
add quant unit test #2315
Changes from 6 commits
48bd182
7f56db6
e696ed3
b5a5508
0f5dc1b
dfd3ee7
5cd45c8
325b86a
caa716f
9410fcd
1ebd819
9f6fa41
798c2f0
6bc80bb
2cf46f1
4f0b71e
c5d2173
2a5b3ad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| * @jeffra @samyam @tjruwase @ShadenSmith @conglongli @awan-10 @cli99 @eltonzheng @minjiaz @RezaYazdaniAminabadi @duli2012 @mrwyattii @yaozhewei @arashb @xiaoxiawu-microsoft @samadejacobs @cmikeh2 | ||
| * @jeffra @samyam @tjruwase @ShadenSmith @conglongli @awan-10 @cli99 @eltonzheng @minjiaz @RezaYazdaniAminabadi @duli2012 @mrwyattii @yaozhewei @arashb @xiaoxiawu-microsoft @samadejacobs @cmikeh2 @GuanhuaWang |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import torch | ||
| import pytest | ||
| from deepspeed.ops import op_builder | ||
|
|
||
| quantizer_cuda_module = op_builder.QuantizerBuilder().load() | ||
|
|
||
|
|
||
| def allclose(x, y): | ||
| assert x.dtype == y.dtype | ||
| rtol, atol = {torch.float32: (1e-2, 1e-3), torch.float16: (1e-2, 1e-3)}[x.dtype] | ||
| return torch.allclose(x, y, rtol=rtol, atol=atol) | ||
|
|
||
|
|
||
| def quantize_dequantize_ref(inputs, bit, num_groups=1): | ||
| # quantize | ||
| q_range = 2**bit | ||
| input_flat = inputs.float().reshape(num_groups, -1).contiguous() | ||
| input_flat = torch.nan_to_num(input_flat, nan=0.0) | ||
| input_min = input_flat.amin(-1, keepdim=True) | ||
| input_max = input_flat.amax(-1, keepdim=True) | ||
|
|
||
| scale = q_range / (2 * torch.max(input_min.abs(), input_max.abs())) | ||
| input_flat = (input_flat * scale).round().clamp(-q_range // 2, q_range // 2 - 1) | ||
| # dequantize | ||
| return input_flat.reshape(inputs.shape).to(torch.int8) / scale.view(-1).to( | ||
| torch.float16) | ||
|
|
||
|
|
||
| @pytest.mark.inference | ||
| @pytest.mark.parametrize("input_tensor", torch.rand(8, 8, dtype=torch.float16).cuda()) | ||
|
Contributor
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. I think we could benefit from some more test cases here. Looking at the kernels, we should test that it partitions in the correct number of groups correctly and things like that as well as larger tensor/higher dimensioned Tensors.
Contributor
Author
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. @cmikeh2 added more tests and also explicitly mentioned the group size boundary in comment. |
||
| def test_quant_dequant(input_tensor): | ||
| ref_input = input_tensor.clone().detach() | ||
| ref_out = quantize_dequantize_ref(ref_input, 8) | ||
|
|
||
| # ds_quantize will do quantize then dequantize and return the dequantized value. | ||
| ds_out = quantizer_cuda_module.ds_quantize_fp16(input_tensor, 1, 8) | ||
|
|
||
| assert (allclose(ds_out, ref_out)) | ||
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.
I believe this is causing the unrelated tests to fail. Let's refactor this to use the loading style used either here.
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.
@GuanhuaWang -- yes, please see the code above. We set the module to none at global level and only load it inside the function that needs it.
Uh oh!
There was an error while loading. Please reload this page.
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.
@cmikeh2 @awan-10 move the test file to
tests/unit/ops/quantizer, also used the loading style here.