-
Notifications
You must be signed in to change notification settings - Fork 379
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
HiFi optimizations for mean, where, min, max, pow, rem and quantized_linear operators. #6867
Merged
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
216389c
Adding mean and where ops optimized on HiFi
dijopaul 3d849bb
Merge pull request #14 from dijopaul/main
cad-audio 9b71aed
Adding quantized linear optimized versions for int8 and uint8
dijopaul 07743ab
adding pow, remainder, minimum, maximum operators (#33)
nishpoonia edc1b3d
Fix for build issue faced in div_mod on old tools
dijopaul 222beee
Merge pull request #15 from dijopaul/main
cad-audio 6e074ec
Merge branch 'main' into main
cad-audio afca3db
Fix build failure due to merge issue
dijopaul 10a0ee0
Merge branch 'main' into main
mcremon-meta f1f0bb3
Fixing review comments on PR 6867
dijopaul 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 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 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 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 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 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,172 @@ | ||
/* | ||
* 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 <executorch/backends/cadence/hifi/kernels/kernels.h> | ||
#include <executorch/kernels/portable/cpu/scalar_utils.h> | ||
#include <executorch/kernels/portable/cpu/util/broadcast_util.h> | ||
#include <executorch/kernels/portable/cpu/util/math_util.h> | ||
#include <executorch/runtime/kernel/kernel_includes.h> | ||
|
||
using exec_aten::ScalarType; | ||
using exec_aten::Tensor; | ||
using executorch::aten::RuntimeContext; | ||
using executorch::runtime::can_cast; | ||
using executorch::runtime::canCast; | ||
using executorch::runtime::CppTypeToScalarType; | ||
using executorch::runtime::promoteTypes; | ||
using torch::executor::apply_binary_elementwise_fn; | ||
using torch::executor::Error; | ||
using torch::executor::resize_to_broadcast_target_size; | ||
|
||
namespace impl { | ||
namespace HiFi { | ||
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. Can the name spaces of all the ops build as
To align with other ops? HiFi example: https://github.com/pytorch/executorch/blob/main/backends/cadence/hifi/operators/op_add.cpp#L25 |
||
namespace native { | ||
namespace { | ||
|
||
template < | ||
bool can_cast, | ||
typename CTYPE_A, | ||
typename CTYPE_B, | ||
typename CTYPE_IN, | ||
typename CTYPE_OUT> | ||
struct MaximumInner; | ||
|
||
template < | ||
typename CTYPE_A, | ||
typename CTYPE_B, | ||
typename CTYPE_IN, | ||
typename CTYPE_OUT> | ||
struct MaximumInner<true, CTYPE_A, CTYPE_B, CTYPE_IN, CTYPE_OUT> { | ||
static void run(const Tensor& a, const Tensor& b, Tensor& out) { | ||
apply_binary_elementwise_fn<CTYPE_A, CTYPE_B, CTYPE_OUT>( | ||
// NOLINTNEXTLINE(facebook-hte-ConstantArgumentPassByValue) | ||
[](const CTYPE_A val_a, const CTYPE_B val_b) { | ||
CTYPE_IN a_casted = static_cast<CTYPE_IN>(val_a); | ||
CTYPE_IN b_casted = static_cast<CTYPE_IN>(val_b); | ||
CTYPE_IN value = | ||
torch::executor::native::utils::max_override(a_casted, b_casted); | ||
|
||
return static_cast<CTYPE_OUT>(value); | ||
}, | ||
a, | ||
b, | ||
out); | ||
} | ||
}; | ||
|
||
struct ReportCanCastBug { | ||
static void run(const Tensor&, const Tensor&, Tensor&) { | ||
ET_DCHECK_MSG(false, "BUG: canCast should have been checked above"); | ||
} | ||
}; | ||
|
||
template < | ||
typename CTYPE_A, | ||
typename CTYPE_B, | ||
typename CTYPE_IN, | ||
typename CTYPE_OUT> | ||
struct MaximumInner<false, CTYPE_A, CTYPE_B, CTYPE_IN, CTYPE_OUT> | ||
: public ReportCanCastBug {}; | ||
|
||
} // namespace | ||
|
||
Tensor& maximum_out( | ||
RuntimeContext& ctx, | ||
const Tensor& a, | ||
const Tensor& b, | ||
Tensor& out) { | ||
(void)ctx; | ||
|
||
ET_KERNEL_CHECK( | ||
ctx, | ||
resize_to_broadcast_target_size(a, b, out) == Error::Ok, | ||
InvalidArgument, | ||
out); | ||
|
||
constexpr int kNnlibMaxDim = 4; /*fallback if broadcast and dim > 4 */ | ||
|
||
ScalarType a_type = a.scalar_type(); | ||
ScalarType b_type = b.scalar_type(); | ||
ScalarType common_type = promoteTypes(a_type, b_type, /*half_to_float*/ true); | ||
ScalarType out_type = out.scalar_type(); | ||
|
||
ET_KERNEL_CHECK(ctx, canCast(common_type, out_type), InvalidArgument, out); | ||
|
||
bool optimized = true; | ||
/*find broadcast*/ | ||
bool a_is_broadcasted = !out.sizes().equals(a.sizes()); | ||
bool b_is_broadcasted = !out.sizes().equals(b.sizes()); | ||
bool broadcast = (a_is_broadcasted || b_is_broadcasted); | ||
|
||
int max_dim = a.dim() > b.dim() ? a.dim() : b.dim(); | ||
max_dim = out.dim() > max_dim ? out.dim() : max_dim; | ||
|
||
if ((a_type != ScalarType::Float) || (b_type != ScalarType::Float)) | ||
optimized = false; | ||
if ((broadcast == true) && (max_dim > kNnlibMaxDim)) | ||
optimized = false; | ||
|
||
if (optimized) { | ||
float* a_data = a.mutable_data_ptr<float>(); | ||
float* b_data = b.mutable_data_ptr<float>(); | ||
float* out_data = out.mutable_data_ptr<float>(); | ||
|
||
if (broadcast == true) { | ||
int out_shape[kNnlibMaxDim]; | ||
int inp1_shape[kNnlibMaxDim]; | ||
int inp2_shape[kNnlibMaxDim]; | ||
|
||
for (int i = 0; i < kNnlibMaxDim; i++) { | ||
out_shape[i] = 1; | ||
inp1_shape[i] = 1; | ||
inp2_shape[i] = 1; | ||
} | ||
|
||
int off_o = kNnlibMaxDim - out.dim(); | ||
int off_a = kNnlibMaxDim - a.dim(); | ||
int off_b = kNnlibMaxDim - b.dim(); | ||
|
||
for (int i = 0; i < out.dim(); i++) { | ||
out_shape[i + off_o] = out.size(i); | ||
} | ||
|
||
for (int i = 0; i < a.dim(); i++) | ||
inp1_shape[i + off_a] = a.size(i); | ||
|
||
for (int i = 0; i < b.dim(); i++) | ||
inp2_shape[i + off_b] = b.size(i); | ||
|
||
xa_nn_elm_maximum_broadcast_4D_f32xf32_f32( | ||
out_data, out_shape, a_data, inp1_shape, b_data, inp2_shape); | ||
} else { | ||
xa_nn_elm_maximum_f32xf32_f32(out_data, a_data, b_data, out.numel()); | ||
} | ||
return out; | ||
} | ||
ET_SWITCH_REALHB_TYPES(a_type, ctx, "maximum.out", CTYPE_A, [&]() { | ||
ET_SWITCH_REALHB_TYPES(b_type, ctx, "maximum.out", CTYPE_B, [&]() { | ||
using CTYPE_IN = typename torch::executor:: | ||
promote_types<CTYPE_A, CTYPE_B, /*half_to_float*/ true>::type; | ||
ET_DCHECK(CppTypeToScalarType<CTYPE_IN>::value == common_type); | ||
ET_SWITCH_REALHB_TYPES(out_type, ctx, "maximum.out", CTYPE_OUT, [&]() { | ||
MaximumInner< | ||
can_cast<CTYPE_IN, CTYPE_OUT>::value, | ||
CTYPE_A, | ||
CTYPE_B, | ||
CTYPE_IN, | ||
CTYPE_OUT>::run(a, b, out); | ||
}); | ||
}); | ||
}); | ||
|
||
return out; | ||
} | ||
|
||
} // namespace native | ||
} // namespace HiFi | ||
} // namespace impl |
Oops, something went wrong.
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 change all kernel_names here to match cadence::impl::HiFi::native::OP_NAME. For example: cadence::impl::HiFi::native::add_out