-
Notifications
You must be signed in to change notification settings - Fork 752
Add _clone_dim_order portable kernel #12974
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
Gasoonjia
merged 16 commits into
pytorch:main
from
keyprocedure:add-dim-order-clone-kernel
Aug 11, 2025
Merged
Changes from 4 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
1f60bb7
Add clone_dim_order kernel (portable + ATen) and layout conversion tests
keyprocedure beeeebd
Remove duplicate helper functions (moved to utils)
keyprocedure f8d18ec
Revert ATen _clone_dim_order implementation and refactoring
keyprocedure 62bced0
Register _clone_dim_order and add operator tests
keyprocedure b4e7f7a
Add broadcast_util dep for copy_ops_util.h
keyprocedure a4f98ac
Remove Python side registration and tests for _clone_dim_order
keyprocedure 9d45191
Add _clone_dim_order runtime test
keyprocedure de049ef
Merge branch 'main' into add-dim-order-clone-kernel
Gasoonjia e1865bf
Clarify dim_order format and remove unrelated comments
keyprocedure 72fcff3
Merge branch 'main' into add-dim-order-clone-kernel
Gasoonjia 19d14e1
Move broadcast_util to exported_deps for copy_ops_util
keyprocedure 0a4fe14
Merge branch 'main' into add-dim-order-clone-kernel
Gasoonjia 0100fd4
Add op__clone_dim_order to op_library
keyprocedure 06c38ad
Merge branch 'main' into add-dim-order-clone-kernel
Gasoonjia b7bc064
Merge branch 'main' into add-dim-order-clone-kernel
Gasoonjia e28b9b9
Merge branch 'main' into add-dim-order-clone-kernel
Gasoonjia 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| /* | ||
| * 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/copy_ops_util.h> | ||
| #include <executorch/runtime/kernel/kernel_includes.h> | ||
|
|
||
| namespace torch { | ||
| namespace executor { | ||
| namespace native { | ||
|
|
||
| using Tensor = executorch::aten::Tensor; | ||
|
|
||
| template <typename T> | ||
| using OptionalArrayRef = executorch::aten::OptionalArrayRef<T>; | ||
|
|
||
| /** | ||
| * _clone_dim_order.out(Tensor self, *, bool non_blocking=False, int[]? | ||
| * dim_order=None, Tensor(a!) out) -> Tensor(a!) | ||
| * | ||
| * Clones via element-wise copy while preserving dim_order. | ||
| */ | ||
| Tensor& _clone_dim_order_out( | ||
| KernelRuntimeContext& ctx, | ||
| const Tensor& self, | ||
| bool non_blocking, | ||
| OptionalArrayRef<int64_t> dim_order, | ||
| Tensor& out) { | ||
| (void)ctx; | ||
|
|
||
| // Ensure input and output dtype match. | ||
| ET_KERNEL_CHECK( | ||
| ctx, self.scalar_type() == out.scalar_type(), InvalidArgument, out); | ||
|
|
||
| // Ensure output has the same layout as input or matches dim_order. | ||
| ET_KERNEL_CHECK( | ||
| ctx, | ||
| check__to_dim_order_copy_args(self, non_blocking, dim_order, out), | ||
| InvalidArgument, | ||
| out); | ||
|
|
||
| // Ensure input and output shapes match, resizing if necessary. | ||
| ET_KERNEL_CHECK( | ||
| ctx, | ||
| resize_tensor(out, self.sizes()) == torch::executor::Error::Ok, | ||
| InvalidArgument, | ||
| out); | ||
|
|
||
| if (self.numel() == 0) { | ||
| return out; | ||
| } | ||
|
|
||
| // Select the correct input dtype and copy the tensors. | ||
| ET_SWITCH_REALHBBF16_TYPES( | ||
| self.scalar_type(), | ||
| ctx, | ||
| "dim_order_ops::_clone_dim_order.out", | ||
| CTYPE, | ||
| [&] { _to_dim_order_copy_impl<CTYPE, CTYPE>(self, out); }); | ||
|
|
||
| return out; | ||
| } | ||
|
|
||
| Tensor& _clone_dim_order_out( | ||
| const Tensor& self, | ||
| bool non_blocking, | ||
| OptionalArrayRef<int64_t> dim_order, | ||
| Tensor& out) { | ||
| executorch::ET_RUNTIME_NAMESPACE::KernelRuntimeContext context{}; | ||
| return _clone_dim_order_out(context, self, non_blocking, dim_order, out); | ||
| } | ||
|
|
||
| } // namespace native | ||
| } // namespace executor | ||
| } // namespace torch |
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
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.
It is ok to leave here since we are gonna need it in the future, but when we talk about adding portable kernels we mainly focus on the kernels in the runtime, specificly under
executorch/kernels/portable.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 makes sense, but I needed to register the operator here otherwise the tests I added fail since there is no Python side reference to
_clone_dim_orderThere 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, should all the tests for this PR have been on the kernel side and not in test_memory_format_ops_pass.py?
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.
This PR should only focus on our runtime changes, so no python side will refer to our new operator.
Yes absolutely correct! This PR is only for portable kernel and its tests. Sorry for any misleading!
Uh oh!
There was an error while loading. Please reload this page.
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.
I’ve added the runtime test and all tests passed locally. I couldn’t run the
DynamicShapeUnboundtest since it depends onSupportedFeaturesandsupported_features.hdoesn’t seem to be generated in OSS builds.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 disregard my previous comment about the missing
SupportedFeaturesdependency, the issue was with my local build setup. All tests pass now.