-
Notifications
You must be signed in to change notification settings - Fork 741
Added HiFi optimizations for add sub mul and div operators #5483
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 5 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
4ed8bd6
Main backup (#12)
dijopaul 71d78a1
Merge pull request #7 from dijopaul/main
cad-audio 6ad490a
Adding sigmoid optimizations
dijopaul c0b1005
Adding tanh optimizations
dijopaul 41d7533
Merge pull request #8 from dijopaul/main
cad-audio a8c4f66
Fixing review comments in 5483
dijopaul 99a772c
Adding cflags to prevent compilation halts
dijopaul 8064895
Merge pull request #9 from dijopaul/main
cad-audio 9543622
Adding cflags to prevent compilation halts
dijopaul d45e25d
Changing name space of optimized ops; Remove unused ops from file
dijopaul a3581f1
Merge branch 'main' of https://github.com/dijopaul/executorch
dijopaul fd955cf
Merge pull request #11 from dijopaul/main
cad-audio 5851fca
Merge pull request #10 from dijopaul/main_cflags
cad-audio 605f374
Merge branch 'main' into main
cad-audio 4e89e2a
fixed lint issues.
cad-audio e9d9f6c
Merge branch 'main' into main
cad-audio 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
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,123 @@ | ||
| /* | ||
| * 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/kernels/portable/cpu/scalar_utils.h> | ||
| #include <executorch/kernels/portable/cpu/util/broadcast_util.h> | ||
| #include <executorch/kernels/portable/cpu/util/functional_util.h> | ||
| #include <executorch/runtime/kernel/kernel_includes.h> | ||
| #include <executorch/runtime/platform/assert.h> | ||
| #include "kernels.h" | ||
|
|
||
| namespace torch { | ||
| namespace executor { | ||
| namespace native { | ||
|
|
||
| #define NNLIB_MAX_DIM 4 /* Add fallback if broadcast and dim > 4 */ | ||
|
|
||
| Tensor& add_out( | ||
| RuntimeContext& ctx, | ||
| const Tensor& a, | ||
| const Tensor& b, | ||
| const Scalar& alpha, | ||
| Tensor& out) { | ||
| (void)ctx; | ||
|
|
||
| ScalarType a_type = a.scalar_type(); | ||
| ScalarType b_type = b.scalar_type(); | ||
| ScalarType common_type = promoteTypes(a_type, b_type); | ||
| ScalarType out_type = out.scalar_type(); | ||
|
|
||
| ET_CHECK_MSG(a_type == ScalarType::Float, "Input tensor not a float.\n"); | ||
| ET_CHECK_MSG(b_type == ScalarType::Float, "Input tensor not a float.\n"); | ||
| ET_CHECK_MSG(out_type == ScalarType::Float, "Output tensor not a float.\n"); | ||
mcremon-meta marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ET_CHECK(canCast(common_type, out_type)); | ||
|
|
||
| using CTYPE_A = float; | ||
| using CTYPE_B = float; | ||
| using CTYPE_IN = float; | ||
| using CTYPE_OUT = float; | ||
| CTYPE_IN alpha_val; | ||
| ET_EXTRACT_SCALAR(alpha, alpha_val); | ||
|
|
||
| int a_dim = a.dim(), b_dim = b.dim(), out_dim = out.dim(); | ||
| int fall_back = 0; | ||
| /*find broadcast*/ | ||
| const int a_is_broadcasted = !out.sizes().equals(a.sizes()); | ||
| const int b_is_broadcasted = !out.sizes().equals(b.sizes()); | ||
| const int 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( (out_type != ScalarType::Float) || (alpha_val != 1.0)) | ||
| fall_back = 1; | ||
|
|
||
| if( (a_dim == 0) || (b_dim == 0) ) | ||
| fall_back = 1; | ||
|
|
||
| if((broadcast == 1) && (max_dim > NNLIB_MAX_DIM)) | ||
| fall_back = 1; | ||
|
|
||
|
|
||
| if (!fall_back) | ||
| { | ||
| const float* const a_data = a.const_data_ptr<float>(); | ||
| const float* const b_data = b.const_data_ptr<float>(); | ||
| float* const out_data = out.mutable_data_ptr<float>(); | ||
| if(broadcast == 1) | ||
| { | ||
| int out_shape[NNLIB_MAX_DIM]; | ||
| int inp1_shape[NNLIB_MAX_DIM]; | ||
| int inp2_shape[NNLIB_MAX_DIM]; | ||
|
|
||
| for(int i = 0; i < NNLIB_MAX_DIM; i++) | ||
| { | ||
| out_shape[i] = 1; | ||
| inp1_shape[i] = 1; | ||
| inp2_shape[i] = 1; | ||
| } | ||
|
|
||
| int off_o = NNLIB_MAX_DIM - out.dim(); | ||
| int off_a = NNLIB_MAX_DIM - a.dim(); | ||
| int off_b = NNLIB_MAX_DIM - 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_add_broadcast_4D_f32xf32_f32(out_data, out_shape, a_data, inp1_shape, | ||
| b_data, inp2_shape); | ||
| } | ||
| else | ||
| xa_nn_elm_add_f32xf32_f32(out_data, a_data, b_data, out.numel()); | ||
|
|
||
| } | ||
| else | ||
| { | ||
| apply_binary_elementwise_fn<CTYPE_A, CTYPE_B, CTYPE_OUT>( | ||
| [alpha_val](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 = a_casted + alpha_val * b_casted; | ||
|
|
||
| return static_cast<CTYPE_OUT>(value); | ||
| }, | ||
| a, | ||
| b, | ||
| out); | ||
| } | ||
|
|
||
| return out; | ||
| } | ||
|
|
||
| } // namespace native | ||
| } // namespace executor | ||
| } // namespace torch | ||
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.
Uh oh!
There was an error while loading. Please reload this page.