-
Notifications
You must be signed in to change notification settings - Fork 734
Enable 16-bit activations and 8 bit weigths in Cadence Quantizer for Matmul #15929
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
RahulC7
wants to merge
1
commit into
pytorch:main
Choose a base branch
from
RahulC7:export-D86644079
base: main
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.
+203
−3
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
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
145 changes: 145 additions & 0 deletions
145
backends/cadence/hifi/operators/tests/test_op_quantized_matmul_out.cpp
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,145 @@ | ||
| /* | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * All rights reserved. | ||
| * | ||
| * This source code is licensed under the BSD-style license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| #include <gtest/gtest.h> | ||
| #include <sys/times.h> | ||
|
|
||
| #include <executorch/kernels/test/TestUtil.h> | ||
| #include <executorch/runtime/core/error.h> | ||
| #include <executorch/runtime/core/exec_aten/exec_aten.h> | ||
| #include <executorch/runtime/core/exec_aten/testing_util/tensor_factory.h> | ||
| #include <executorch/runtime/core/exec_aten/testing_util/tensor_util.h> | ||
| #include <executorch/runtime/platform/runtime.h> | ||
|
|
||
| #include <executorch/backends/cadence/hifi/operators/operators.h> | ||
|
|
||
| namespace impl { | ||
| namespace HiFi { | ||
| namespace native { | ||
| namespace { | ||
|
|
||
| using ::executorch::aten::Scalar; | ||
| using ::executorch::aten::ScalarType; | ||
| using ::executorch::aten::Tensor; | ||
| using ::executorch::aten::TensorImpl; | ||
| using ::executorch::runtime::Error; | ||
| using ::executorch::runtime::KernelRuntimeContext; | ||
| using ::executorch::runtime::runtime_init; | ||
| using ::executorch::runtime::testing::TensorFactory; | ||
|
|
||
| class HiFiQuantizedMatmulTest : public OperatorTest { | ||
| public: | ||
| protected: | ||
| void quantized_matmul_out( | ||
| const Tensor& X, | ||
| int64_t X_zero_point, | ||
| const Tensor& Y, | ||
| int64_t Y_zero_point, | ||
| const std::optional<Tensor>& bias, | ||
| int64_t out_multiplier, | ||
| int64_t out_shift, | ||
| int64_t out_zero_point, | ||
| bool transposed, | ||
| Tensor& output) { | ||
| return ::impl::HiFi::native::quantized_matmul_out( | ||
| context_, | ||
| X, | ||
| X_zero_point, | ||
| Y, | ||
| Y_zero_point, | ||
| bias, | ||
| out_multiplier, | ||
| out_shift, | ||
| out_zero_point, | ||
| transposed, | ||
| output); | ||
| } | ||
| }; | ||
|
|
||
| // Test quantized_matmul_out with int16 activations and int8 weights | ||
| TEST_F(HiFiQuantizedMatmulTest, QuantizedMatmulInt16Test) { | ||
| TensorFactory<ScalarType::Short> tf_int16; | ||
| TensorFactory<ScalarType::Int> tf_int32; | ||
| TensorFactory<ScalarType::Char> tf_int8; | ||
|
|
||
| // Simple 2D case: X [64, 33] x Y [33, 128] = output [64, 128] | ||
| // Using simple values for testing | ||
| Tensor X = tf_int16.ones({64, 33}); | ||
| Tensor Y = tf_int8.ones({33, 128}); | ||
| // Bias not used | ||
| Tensor bias = tf_int32.full({128}, -30); | ||
| Tensor output = tf_int16.zeros({64, 128}); | ||
|
|
||
| int64_t X_zero_point = 0; | ||
| int64_t Y_zero_point = 0; | ||
| int64_t out_multiplier = 1073741824; // 0.5 * 2^31 | ||
| int64_t out_shift = 0; | ||
| int64_t out_zero_point = 0; | ||
|
|
||
| quantized_matmul_out( | ||
| X, | ||
| X_zero_point, | ||
| Y, | ||
| Y_zero_point, | ||
| bias, // pass bias tensor | ||
| out_multiplier, | ||
| out_shift, | ||
| out_zero_point, | ||
| false, // transposed | ||
| output); | ||
|
|
||
| // Verify the output is correct | ||
| // With all ones input and weights, inner dimension is 33 | ||
| // Matmul result: 33, with out_multiplier = 0.5 * 2^31 (scales by 0.5) | ||
| // Expected value: 33 * 0.5 = 16.5 ≈ 16 | ||
| EXPECT_EQ(output.const_data_ptr<int16_t>()[0], 16); | ||
| } | ||
|
|
||
| // Test quantized_matmul_out with transposed Y (int16 activations and int8 | ||
| // weights) | ||
| TEST_F(HiFiQuantizedMatmulTest, QuantizedMatmulInt16TransposedTest) { | ||
| TensorFactory<ScalarType::Short> tf_int16; | ||
| TensorFactory<ScalarType::Int> tf_int32; | ||
| TensorFactory<ScalarType::Char> tf_int8; | ||
|
|
||
| // Transposed case: X [64, 33] x Y^T [128, 33] = output [64, 128] | ||
| Tensor X = tf_int16.ones({64, 33}); | ||
| Tensor Y = tf_int8.ones({128, 33}); // Transposed | ||
| // Bias not used | ||
| Tensor bias = tf_int32.full({128}, -30); | ||
| Tensor output = tf_int16.zeros({64, 128}); | ||
|
|
||
| int64_t X_zero_point = 0; | ||
| int64_t Y_zero_point = 0; | ||
| int64_t out_multiplier = 1073741824; // 0.5 * 2^31 | ||
| int64_t out_shift = 0; | ||
| int64_t out_zero_point = 0; | ||
|
|
||
| quantized_matmul_out( | ||
| X, | ||
| X_zero_point, | ||
| Y, | ||
| Y_zero_point, | ||
| bias, // pass bias tensor | ||
| out_multiplier, | ||
| out_shift, | ||
| out_zero_point, | ||
| true, // transposed | ||
| output); | ||
|
|
||
| // Verify the output is correct | ||
| // With all ones input and weights, inner dimension is 33 | ||
| // Matmul result: 33, with out_multiplier = 0.5 * 2^31 (scales by 0.5) | ||
| // Expected value: 33 * 0.5 = 16.5 ≈ 16 | ||
| EXPECT_EQ(output.const_data_ptr<int16_t>()[0], 16); | ||
| } | ||
|
|
||
| } // namespace | ||
| } // namespace native | ||
| } // namespace HiFi | ||
| } // namespace impl |
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.
The deps list has a trailing comma after the closing bracket which is unusual formatting. While this may not cause a syntax error in Starlark, it's inconsistent with standard formatting practices. Consider reformatting as: