-
Notifications
You must be signed in to change notification settings - Fork 594
open source quantized_fully_connected #8164
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
Merged
Merged
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
267 changes: 267 additions & 0 deletions
267
backends/cadence/hifi/operators/op_quantized_fully_connected_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,267 @@ | ||
// (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary. | ||
#include <executorch/backends/cadence/hifi/kernels/kernels.h> | ||
#include <executorch/runtime/kernel/kernel_includes.h> | ||
|
||
#include <algorithm> | ||
#include <cmath> | ||
|
||
namespace cadence { | ||
namespace impl { | ||
namespace HiFi { | ||
namespace native { | ||
|
||
using ::executorch::aten::ArrayRef; | ||
using ::executorch::aten::IntArrayRef; | ||
using ::executorch::aten::optional; | ||
using ::executorch::aten::Scalar; | ||
using ::executorch::aten::ScalarType; | ||
using ::executorch::aten::SizesType; | ||
using ::executorch::aten::Tensor; | ||
using ::executorch::runtime::KernelRuntimeContext; | ||
|
||
void inline _quantized_fully_connected_asym8u( | ||
const Tensor& in, | ||
const Tensor& weight, | ||
const Tensor& bias, | ||
int64_t in_zero_point, | ||
const Tensor& weight_zero_point, | ||
const Tensor& out_multiplier, | ||
const Tensor& out_shift, | ||
int64_t out_zero_point, | ||
__ET_UNUSED const optional<Tensor>& offset, | ||
Tensor& out) { | ||
// input comes in shape [leading_dims, in_dim] | ||
// weight comes in shape [out_dim, in_dim] | ||
// output comes in empty with shape [leading_dims, out_dim] | ||
// Perform matrix multiply (M x N) x (N x P)' => M x P | ||
int64_t leading_dims = 1; | ||
int64_t out_dim = weight.size(0); // = out_dim | ||
int64_t in_dim = weight.size(1); // = in_dim | ||
|
||
const uint8_t* __restrict__ in_data = in.const_data_ptr<uint8_t>(); | ||
const uint8_t* __restrict__ weight_data = weight.const_data_ptr<uint8_t>(); | ||
const int32_t* __restrict__ bias_data = bias.const_data_ptr<int32_t>(); | ||
uint8_t* __restrict__ out_data = out.mutable_data_ptr<uint8_t>(); | ||
|
||
int32_t ret = xa_nn_fully_connected_asym8uxasym8u_asym8u( | ||
out_data, | ||
weight_data, | ||
in_data, | ||
bias_data, | ||
in_dim, // weight_depth, number of columns in weight | ||
out_dim, // out_depth, number of rows in weight | ||
-in_zero_point, | ||
-weight_zero_point.const_data_ptr<int32_t>()[0], | ||
out_multiplier.const_data_ptr<int32_t>()[0], | ||
out_shift.const_data_ptr<int32_t>()[0], | ||
out_zero_point); | ||
ET_DCHECK_MSG(ret == 0, "HiFi quantized::fully_connected failed"); | ||
} | ||
|
||
void inline _quantized_fully_connected_asym8s( | ||
const Tensor& in, | ||
const Tensor& weight, | ||
const Tensor& bias, | ||
int64_t in_zero_point, | ||
const Tensor& weight_zero_point, | ||
const Tensor& out_multiplier, | ||
const Tensor& out_shift, | ||
int64_t out_zero_point, | ||
__ET_UNUSED const optional<Tensor>& offset, | ||
Tensor& out) { | ||
// input comes in shape [leading_dims, in_dim] | ||
// weight comes in shape [out_dim, in_dim] | ||
// output comes in empty with shape [leading_dims, out_dim] | ||
// Perform matrix multiply (M x N) x (N x P)' => M x P | ||
int64_t leading_dims = 1; | ||
int64_t out_dim = weight.size(0); // = out_dim | ||
int64_t in_dim = weight.size(1); // = in_dim | ||
|
||
const int8_t* __restrict__ in_data = in.const_data_ptr<int8_t>(); | ||
const int8_t* __restrict__ weight_data = weight.const_data_ptr<int8_t>(); | ||
const int32_t* __restrict__ bias_data = bias.const_data_ptr<int32_t>(); | ||
int8_t* __restrict__ out_data = out.mutable_data_ptr<int8_t>(); | ||
|
||
int32_t ret = xa_nn_fully_connected_asym8sxasym8s_asym8s( | ||
out_data, | ||
weight_data, | ||
in_data, | ||
bias_data, | ||
in_dim, // weight_depth, number of columns in weight | ||
out_dim, // out_depth, number of rows in weight | ||
-in_zero_point, | ||
-weight_zero_point.const_data_ptr<int32_t>()[0], | ||
out_multiplier.const_data_ptr<int32_t>()[0], | ||
out_shift.const_data_ptr<int32_t>()[0], | ||
out_zero_point); | ||
ET_DCHECK_MSG(ret == 0, "HiFi quantized::fully_connected failed"); | ||
} | ||
|
||
void quantized_fully_connected_out( | ||
__ET_UNUSED KernelRuntimeContext& ctx, | ||
const Tensor& in, | ||
const Tensor& weight, | ||
const Tensor& bias, | ||
int64_t in_zero_point, | ||
const Tensor& weight_zero_point, | ||
const Tensor& out_multiplier, | ||
const Tensor& out_shift, | ||
int64_t out_zero_point, | ||
__ET_UNUSED const optional<Tensor>& offset, | ||
Tensor& out) { | ||
if (out.scalar_type() == ScalarType::Byte) { | ||
_quantized_fully_connected_asym8u( | ||
in, | ||
weight, | ||
bias, | ||
in_zero_point, | ||
weight_zero_point, | ||
out_multiplier, | ||
out_shift, | ||
out_zero_point, | ||
offset, | ||
out); | ||
} else if (out.scalar_type() == ScalarType::Char) { | ||
_quantized_fully_connected_asym8s( | ||
in, | ||
weight, | ||
bias, | ||
in_zero_point, | ||
weight_zero_point, | ||
out_multiplier, | ||
out_shift, | ||
out_zero_point, | ||
offset, | ||
out); | ||
} else { | ||
ET_CHECK_MSG( | ||
false, | ||
"quantized fully connected only supported for uint8 and int8 dtypes"); | ||
} | ||
} | ||
|
||
void inline _quantized_fully_connected_per_tensor_asym8u( | ||
const Tensor& in, | ||
const Tensor& weight, | ||
const Tensor& bias, | ||
int64_t in_zero_point, | ||
int64_t weight_zero_point, | ||
int64_t out_multiplier, | ||
int64_t out_shift, | ||
int64_t out_zero_point, | ||
__ET_UNUSED const optional<Tensor>& offset, | ||
Tensor& out) { | ||
// input comes in shape [leading_dims, in_dim] | ||
// weight comes in shape [out_dim, in_dim] | ||
// output comes in empty with shape [leading_dims, out_dim] | ||
// Perform matrix multiply (M x N) x (N x P)' => M x P | ||
int64_t leading_dims = 1; | ||
int64_t out_dim = weight.size(0); // = out_dim | ||
int64_t in_dim = weight.size(1); // = in_dim | ||
|
||
const uint8_t* __restrict__ in_data = in.const_data_ptr<uint8_t>(); | ||
const uint8_t* __restrict__ weight_data = weight.const_data_ptr<uint8_t>(); | ||
const int32_t* __restrict__ bias_data = bias.const_data_ptr<int32_t>(); | ||
uint8_t* __restrict__ out_data = out.mutable_data_ptr<uint8_t>(); | ||
|
||
int32_t ret = xa_nn_fully_connected_asym8uxasym8u_asym8u( | ||
out_data, | ||
weight_data, | ||
in_data, | ||
bias_data, | ||
in_dim, // weight_depth, number of columns in weight | ||
out_dim, // out_depth, number of rows in weight | ||
-in_zero_point, | ||
-static_cast<int32_t>(weight_zero_point), | ||
static_cast<int32_t>(out_multiplier), | ||
static_cast<int32_t>(out_shift), | ||
out_zero_point); | ||
ET_DCHECK_MSG(ret == 0, "HiFi quantized::fully_connected failed"); | ||
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. Use XT_ macros please |
||
} | ||
|
||
void inline _quantized_fully_connected_per_tensor_asym8s( | ||
const Tensor& in, | ||
const Tensor& weight, | ||
const Tensor& bias, | ||
int64_t in_zero_point, | ||
int64_t weight_zero_point, | ||
int64_t out_multiplier, | ||
int64_t out_shift, | ||
int64_t out_zero_point, | ||
__ET_UNUSED const optional<Tensor>& offset, | ||
Tensor& out) { | ||
// input comes in shape [leading_dims, in_dim] | ||
// weight comes in shape [out_dim, in_dim] | ||
// output comes in empty with shape [leading_dims, out_dim] | ||
// Perform matrix multiply (M x N) x (N x P)' => M x P | ||
int64_t leading_dims = 1; | ||
int64_t out_dim = weight.size(0); // = out_dim | ||
int64_t in_dim = weight.size(1); // = in_dim | ||
|
||
const int8_t* __restrict__ in_data = in.const_data_ptr<int8_t>(); | ||
const int8_t* __restrict__ weight_data = weight.const_data_ptr<int8_t>(); | ||
const int32_t* __restrict__ bias_data = bias.const_data_ptr<int32_t>(); | ||
int8_t* __restrict__ out_data = out.mutable_data_ptr<int8_t>(); | ||
|
||
int32_t ret = xa_nn_fully_connected_asym8sxasym8s_asym8s( | ||
out_data, | ||
weight_data, | ||
in_data, | ||
bias_data, | ||
in_dim, // weight_depth, number of columns in weight | ||
out_dim, // out_depth, number of rows in weight | ||
-in_zero_point, | ||
-static_cast<int32_t>(weight_zero_point), | ||
static_cast<int32_t>(out_multiplier), | ||
static_cast<int32_t>(out_shift), | ||
out_zero_point); | ||
ET_DCHECK_MSG(ret == 0, "HiFi quantized::fully_connected failed"); | ||
} | ||
|
||
void quantized_fully_connected_per_tensor_out( | ||
__ET_UNUSED KernelRuntimeContext& ctx, | ||
const Tensor& in, | ||
const Tensor& weight, | ||
const Tensor& bias, | ||
int64_t in_zero_point, | ||
int64_t weight_zero_point, | ||
int64_t out_multiplier, | ||
int64_t out_shift, | ||
int64_t out_zero_point, | ||
__ET_UNUSED const optional<Tensor>& offset, | ||
Tensor& out) { | ||
if (out.scalar_type() == ScalarType::Byte) { | ||
_quantized_fully_connected_per_tensor_asym8u( | ||
in, | ||
weight, | ||
bias, | ||
in_zero_point, | ||
weight_zero_point, | ||
out_multiplier, | ||
out_shift, | ||
out_zero_point, | ||
offset, | ||
out); | ||
} else if (out.scalar_type() == ScalarType::Char) { | ||
_quantized_fully_connected_per_tensor_asym8s( | ||
in, | ||
weight, | ||
bias, | ||
in_zero_point, | ||
weight_zero_point, | ||
out_multiplier, | ||
out_shift, | ||
out_zero_point, | ||
offset, | ||
out); | ||
} else { | ||
ET_CHECK_MSG( | ||
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. Replace this with ET_KERNEL_CHECK please |
||
false, | ||
"quantized fully connected only supported for uint8 and int8 dtypes"); | ||
} | ||
} | ||
|
||
} // namespace native | ||
} // namespace HiFi | ||
} // namespace impl | ||
} // namespace cadence |
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
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.
Please use ET_KERNEL_CHECK instead