-
Notifications
You must be signed in to change notification settings - Fork 26.3k
[quant] Support quantization of embedding lookup operators #44207
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
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
74007e0
[quant] Support quantization of embedding lookup operators
supriyar 635f9ef
Update on "[quant] Support quantization of embedding lookup operators"
supriyar be7d29b
Update on "[quant] Support quantization of embedding lookup operators"
supriyar 3e137ea
Update on "[quant] Support quantization of embedding lookup operators"
supriyar 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
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
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 |
|---|---|---|
|
|
@@ -22,6 +22,7 @@ | |
| from torch.testing._internal.common_quantization import skipIfNoFBGEMM | ||
| from torch.testing._internal.common_quantized import _quantize, _dequantize, _calculate_dynamic_qparams, \ | ||
| override_quantized_engine, supported_qengines, override_qengines | ||
| from torch.quantization import PerChannelMinMaxObserver | ||
|
|
||
| np_dtype = { | ||
| torch.quint8 : np.uint8, | ||
|
|
@@ -2716,7 +2717,7 @@ def test_qlinear_unpack(self, W, use_channelwise): | |
|
|
||
|
|
||
| @unittest.skipIf(sys.platform == "darwin", "Known test failure on Mac.") | ||
| class TestQuantizedEmbeddingBag(TestCase): | ||
| class TestQuantizedEmbeddingOps(TestCase): | ||
| def _test_embedding_bag_unpack_fn(self, pack_fn, unpack_fn, num_embeddings, embedding_dim, bit_rate): | ||
| weights = torch.from_numpy((np.random.random_sample(( | ||
| num_embeddings, embedding_dim)) + 1).astype(np.float32)) | ||
|
|
@@ -2727,7 +2728,6 @@ def _test_embedding_bag_unpack_fn(self, pack_fn, unpack_fn, num_embeddings, embe | |
| if bit_rate == 8: | ||
| # Check numerics of prepack function that accepts qtensor as input. | ||
| # We use min-max observer to mimic the quantization performed in the original function. | ||
| from torch.quantization import PerChannelMinMaxObserver | ||
| obs = PerChannelMinMaxObserver(dtype=torch.quint8, qscheme=torch.per_channel_affine_float_qparams, ch_axis=0) | ||
| obs(weights) | ||
| # Get the scale and zero point for the weight tensor | ||
|
|
@@ -2884,7 +2884,6 @@ def get_reference_result( | |
|
|
||
| if bit_rate == 8: | ||
| # Test operator that accepts TorchBind packed weights. | ||
| from torch.quantization import PerChannelMinMaxObserver | ||
| obs = PerChannelMinMaxObserver(dtype=torch.quint8, qscheme=torch.per_channel_affine_float_qparams, ch_axis=0) | ||
| obs(weights) | ||
| # Get the scale and zero point for the weight tensor | ||
|
|
@@ -2931,6 +2930,37 @@ def test_embedding_bag_4bit_rowwise_offsets(self, num_embeddings, | |
| include_last_offset, atol=0.1, | ||
| rtol=1e-2) | ||
|
|
||
| """ Tests the correctness of the quantized embedding lookup operator """ | ||
| @given(num_embeddings=st.integers(10, 100), | ||
| embedding_dim=st.integers(5, 50).filter(lambda x: x % 4 == 0)) | ||
| def test_embedding_byte(self, num_embeddings, embedding_dim): | ||
| quant_op = torch.ops.quantized.embedding_byte | ||
| prepack_op = torch.ops.quantized.embedding_bag_prepack | ||
|
|
||
| weights = torch.from_numpy((np.random.random_sample(( | ||
| num_embeddings, embedding_dim)) + 1).astype(np.float32)) | ||
|
|
||
| obs = PerChannelMinMaxObserver(dtype=torch.quint8, qscheme=torch.per_channel_affine_float_qparams, ch_axis=0) | ||
| obs(weights) | ||
| # Get the scale and zero point for the weight tensor | ||
| qparams = obs.calculate_qparams() | ||
|
|
||
| # Quantize the weights to 8bits | ||
| qweight = torch.quantize_per_channel(weights, qparams[0], qparams[1], axis=0, dtype=torch.quint8) | ||
| max_segments = 5 | ||
| max_segment_length = 20 | ||
| num_lengths = np.random.randint(1, max_segments + 1) | ||
| lengths = np.random.randint(1, max_segment_length + 1, | ||
| size=num_lengths).astype(np.int32) | ||
| num_indices = np.sum(lengths) | ||
| indices = torch.from_numpy(np.random.randint( | ||
| low=0, high=num_embeddings, size=num_indices, dtype=np.int64)) | ||
|
|
||
| packed_weight = prepack_op(qweight) | ||
| qresult = quant_op(packed_weight, indices, sparse=False) | ||
|
|
||
| ref = torch.embedding(weights, indices, padding_idx=-1, scale_grad_by_freq=False, sparse=False) | ||
| torch.testing.assert_allclose(ref, qresult, atol=0.005, rtol=1e-3) | ||
|
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. are the modified
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. Yes |
||
|
|
||
| class TestQuantizedConv(unittest.TestCase): | ||
| def _test_qconv_unpack_impl(self, qconv_prepack_fn, qconv_unpack_fn, inputs, | ||
|
|
||
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
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.
If you use qweight.dequantize(), this should be an exact match