-
Notifications
You must be signed in to change notification settings - Fork 713
[ExecuTorch] Add broadcast support for optimized add op #8205
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 9 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
27a79c4
[Executorch] Refactor op_mul's broadcasting utils
kimishpatel dbe3e8a
[ExecuTorch] Add broadcast support for optimized add op
kimishpatel bf761db
Update on "[ExecuTorch] Add broadcast support for optimized add op"
kimishpatel 0e1cfc7
Update base for Update on "[ExecuTorch] Add broadcast support for opt…
kimishpatel 0ce8fd7
Update on "[ExecuTorch] Add broadcast support for optimized add op"
kimishpatel 00e54b8
Update base for Update on "[ExecuTorch] Add broadcast support for opt…
kimishpatel 7ea55eb
Update on "[ExecuTorch] Add broadcast support for optimized add op"
kimishpatel ffb6903
Update base for Update on "[ExecuTorch] Add broadcast support for opt…
kimishpatel e9fe6af
Update on "[ExecuTorch] Add broadcast support for optimized add op"
kimishpatel e53eb97
Update base for Update on "[ExecuTorch] Add broadcast support for opt…
kimishpatel a91eef8
Update on "[ExecuTorch] Add broadcast support for optimized add op"
kimishpatel f565c3b
Update base for Update on "[ExecuTorch] Add broadcast support for opt…
kimishpatel 656873f
Update on "[ExecuTorch] Add broadcast support for optimized add op"
kimishpatel 8ecbd04
Update base for Update on "[ExecuTorch] Add broadcast support for opt…
kimishpatel 2804f70
Update on "[ExecuTorch] Add broadcast support for optimized add op"
kimishpatel f3406bf
Update base for Update on "[ExecuTorch] Add broadcast support for opt…
kimishpatel 132d2f5
Update on "[ExecuTorch] Add broadcast support for optimized add op"
kimishpatel 216c4be
Update base for Update on "[ExecuTorch] Add broadcast support for opt…
kimishpatel bde7998
Update on "[ExecuTorch] Add broadcast support for optimized add op"
kimishpatel 110a932
Update base for Update on "[ExecuTorch] Add broadcast support for opt…
kimishpatel 7ebd165
Update on "[ExecuTorch] Add broadcast support for optimized add op"
kimishpatel 5fb4107
Merge branch 'main' into gh/kimishpatel/154/head
kimishpatel 9e0855b
Update base for Update on "[ExecuTorch] Add broadcast support for opt…
kimishpatel 0d19ade
Update on "[ExecuTorch] Add broadcast support for optimized add op"
kimishpatel 8955d90
Update base for Update on "[ExecuTorch] Add broadcast support for opt…
kimishpatel 6f2f01a
Update on "[ExecuTorch] Add broadcast support for optimized add op"
kimishpatel 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -140,41 +140,32 @@ Tensor& opt_add_out( | |
| out.numel()); | ||
| }); | ||
| } else if (selected_optimized_path != ElementwiseOptimizedPath::kNone) { | ||
| const Tensor* lhs; | ||
| const Tensor* rhs; | ||
| static constexpr const char op_name[] = "add.out"; | ||
| if (selected_optimized_path == | ||
| ElementwiseOptimizedPath::kBroadcast2dBy1dReverseArguments) { | ||
| lhs = &b; | ||
| rhs = &a; | ||
| ElementwiseOptimizedPath::kBroadcast2dBy1dReverseArguments || | ||
| selected_optimized_path == | ||
| ElementwiseOptimizedPath::kBroadcastLastDimReverseArguments || | ||
| selected_optimized_path == | ||
| ElementwiseOptimizedPath::kBroadcastNdByNdReverseArguments) { | ||
| // Reason we swap out args here is because handle_broadcast_elementwise | ||
| // handles this selected_optimized_path option a bit differently. | ||
| // This should really be resolved in handle_broadcast_elementwise. | ||
| // However, the current blocker is that handle_broadcast_elementwise tries | ||
| // to be agnostic of op. This should be fixed, likely by moving lambda | ||
| // creation to handle_broadcast_elementwise and it be aware of which op is | ||
| // being executed. | ||
|
||
| auto add_lambda = [](auto x, auto y, auto alpha_val) { | ||
| return y + alpha_val * x; | ||
| }; | ||
| return torch::executor::handle_broadcast_elementwise<op_name>( | ||
| ctx, add_lambda, a, b, out, selected_optimized_path, alpha); | ||
| } else { | ||
| // Catch failure to update logic when adding new broadcasting possibility. | ||
| ET_DCHECK( | ||
| selected_optimized_path == | ||
| ElementwiseOptimizedPath::kBroadcast2dBy1d); | ||
| lhs = &a; | ||
| rhs = &b; | ||
| auto add_lambda = [](auto x, auto y, auto alpha_val) { | ||
| return x + alpha_val * y; | ||
| }; | ||
| return torch::executor::handle_broadcast_elementwise<op_name>( | ||
| ctx, add_lambda, a, b, out, selected_optimized_path, alpha); | ||
| } | ||
| auto error = resize_tensor(out, lhs->sizes()); | ||
| ET_KERNEL_CHECK_MSG( | ||
| ctx, | ||
| error == Error::Ok, | ||
| InvalidArgument, | ||
| out, | ||
| "Failed to resize output tensor."); | ||
| ET_SWITCH_REALB_TYPES(out_type, ctx, "add.out", CTYPE, [&]() { | ||
| CTYPE alpha_val; | ||
| ET_KERNEL_CHECK( | ||
| ctx, utils::extract_scalar(alpha, &alpha_val), InvalidArgument, ); | ||
|
|
||
| using Vec = executorch::vec::Vectorized<CTYPE>; | ||
| executorch::vec::broadcasting_map_2d_by_1d<CTYPE>( | ||
| [alpha_val](Vec x, Vec y) { return x + Vec(alpha_val) * y; }, | ||
| out.mutable_data_ptr<CTYPE>(), | ||
| lhs->const_data_ptr<CTYPE>(), | ||
| rhs->const_data_ptr<CTYPE>(), | ||
| lhs->sizes()[lhs->dim() - 2], | ||
| lhs->sizes()[lhs->dim() - 1]); | ||
| }); | ||
| } else { | ||
| ScalarType common_type = | ||
| promoteTypes(a_type, b_type, /*half_to_float*/ true); | ||
|
|
||
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.
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.
oh, I see, you're having problems with the lambda because of this part. you can solve this by factoring the code differently.
the end result at the callsite could look something like
disclaimer: this is off the top of my head and it may be possible to unify some of this stuff with dtype_util.h for further simplification, though dtype_util is mostly intended to cut size/build time of portable ops
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.
Good point. Let me see if I dont run into other issues to enable such refactor.
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.
Ok so I looked refactor required. I think it is doable at the cost of moving
ET_SWITCH_REALB_TYPESmacros to the callsite in respective ops. Downside here is that now if you enable new dtype for optimized path, you have to change all the callsites.So I am not fully convinced that it is better go down that route. But want to see whats your reasoning.
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.
that's just a matter of typing, right? if you plan to do it (I suppose optimizing Half/BFloat16 should be on our TODO list if the hardware supports the relevant instructions) and you really don't want to change 4-5 files later (you'll have to change them anyway for specifically Half/BFloat16 because there are opt-outs), you could always
#define ET_SWITCH_OPTIMIZED_ELEMENTWISE_BROADCAST_OP_TYPES ET_SWITCH_REALB_TYPEspre-emptively.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.
Ok thats fair. But is your reasoning for this change simpler code or you see perf impact.
I am not too stuck to it, so I will just go ahead and do it but wanted to understand your reasoning
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.
simpler less repetitive code
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.
ok will make the change but this will likely marginally increase size since now the whole
handle_broadcast_elementwisefunction is dtype specialized