Skip to content

Commit 2287a8b

Browse files
Reject non-default dim order in unfold_copy and select_scatter (pytorch#21915)
### Summary Follow-up to pytorch#21865. Two more portable kernels index with `getLeadingDims` and `getTrailingDims`, which describe a contiguous run only in the default dim order. `unfold_copy` carries no dim order check at all. Checked against the same logical data in two memory formats: | kernel | contiguous input | channels-last input | |---|---|---| | `aten.unfold_copy` | matches | wrong, max abs diff 14.0 | It reads `self` as a contiguous run and writes `out` as a flat run, so neither side survives a non-default layout. `unfold` appears in shipped example models including the Qualcomm llama vision encoder and the gemma4 speech transform, where a channels-last input is ordinary. `select_scatter` has the same arithmetic and the same defect, but it is not reachable from export today. It carries `tensors_have_same_dim_order(in, src, out)`, which is not sufficient on its own, because `is_channels_last_dim_order` accepts 4 and 5 dims, so a rank-5 input and its rank-4 `src` can both be channels-last and pass together. At rank 4 the `src` is rank 3 and the check does reject. Reaching the rank-5 case needs a channels-last dim order that `exir/dim_order_utils.py` refuses to generate, since it raises for any rank other than 4. So the kernel is protected by a gap in the export layer rather than by its own checks, and the guard closes that before 5D channels-last support makes it reachable. Both get the guard `op_addmm`, `op_bmm` and the kernels in pytorch#21865 use: ```cpp ET_KERNEL_CHECK(ctx, tensor_is_default_dim_order(in), InvalidArgument, out); ``` `unfold_copy` needs it on `out` as well, since `out` is one rank above `self` and is written as a flat run. Checking the pair with `tensors_have_same_dim_order` would not work there, for the same rank reason as above. ### Test plan A `NonDefaultDimOrderDies` per kernel, plus a second `unfold_copy` case covering the `out` guard. Reverting the two kernel sources while keeping the tests fails all three. The `select_scatter` test has to be rank 5. A rank-4 case is rejected by the pre-existing same dim order check on current main, so it would pass without reaching the new guard. It builds its tensors with `make_with_dimorder`, because `channels_last_like` asserts `sizes.size() == 4` and cannot express the failing case. All three tests skip under `is_aten`, since the ATen kernels handle a non-default dim order and do not throw. This also registers `op_unfold_copy_test.cpp` in `kernels/test/CMakeLists.txt`, without which neither the existing tests in that file nor the new ones run in a CMake build.
1 parent 5d22fd9 commit 2287a8b

5 files changed

Lines changed: 61 additions & 0 deletions

File tree

‎kernels/portable/cpu/op_select_scatter.cpp‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ Tensor& select_scatter_out(
3737
ET_KERNEL_CHECK(
3838
ctx, tensors_have_same_dim_order(in, src, out), InvalidArgument, out);
3939

40+
ET_KERNEL_CHECK(ctx, tensor_is_default_dim_order(in), InvalidArgument, out);
41+
4042
// Account for negative indices
4143
if (dim < 0) {
4244
dim += in.dim();

‎kernels/portable/cpu/op_unfold_copy.cpp‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ Tensor& unfold_copy_out(
3939
InvalidArgument,
4040
out);
4141

42+
ET_KERNEL_CHECK(ctx, tensor_is_default_dim_order(self), InvalidArgument, out);
43+
44+
ET_KERNEL_CHECK(ctx, tensor_is_default_dim_order(out), InvalidArgument, out);
45+
4246
// Copy data
4347
const size_t leading_dims = getLeadingDims(self, dim);
4448
const size_t trailing_dims = getTrailingDims(self, dim);

‎kernels/test/CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ set(all_test_sources
306306
"op_tril_test.cpp"
307307
"op_trunc_test.cpp"
308308
"op_unbind_copy_test.cpp"
309+
"op_unfold_copy_test.cpp"
309310
"op_unsqueeze_copy_test.cpp"
310311
"op_upsample_bilinear2d_test.cpp"
311312
"op_upsample_bilinear2d_aa_test.cpp"

‎kernels/test/op_select_scatter_test.cpp‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,3 +665,25 @@ TEST_F(OpSelectScatterOutTest, DynamicShapeUnbound) {
665665
test_dynamic_shape(
666666
{1, 1, 1}, torch::executor::TensorShapeDynamism::DYNAMIC_UNBOUND);
667667
}
668+
669+
TEST_F(OpSelectScatterOutTest, NonDefaultDimOrderDies) {
670+
TensorFactory<ScalarType::Float> tf;
671+
672+
// src is one rank below x, so at rank 4 the same dim order check above
673+
// rejects first. Only rank 5 reaches the default dim order check, because
674+
// is_channels_last_dim_order accepts both 4 and 5 dims.
675+
Tensor x = tf.make_with_dimorder(
676+
{2, 2, 2, 2, 2}, std::vector<float>(32, 1), {0, 2, 3, 4, 1});
677+
Tensor src = tf.make_with_dimorder(
678+
{2, 2, 2, 2}, std::vector<float>(16, 2), {0, 2, 3, 1});
679+
680+
Tensor out = tf.make_with_dimorder(
681+
{2, 2, 2, 2, 2}, std::vector<float>(32), {0, 2, 3, 4, 1});
682+
683+
ET_SKIP_IF(
684+
torch::executor::testing::SupportedFeatures::get()->is_aten,
685+
"ATen kernel can handle non-default dim order");
686+
687+
ET_EXPECT_KERNEL_FAILURE(
688+
context_, op_select_scatter_out(x, src, /*dim=*/0, /*index=*/0, out));
689+
}

‎kernels/test/op_unfold_copy_test.cpp‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#include <executorch/runtime/core/exec_aten/util/tensor_util.h>
1414

1515
#include <executorch/kernels/test/TestUtil.h>
16+
#include <executorch/kernels/test/supported_features.h>
17+
#include <executorch/kernels/test/supported_features_skip.h>
1618

1719
#include <gtest/gtest.h>
1820

@@ -127,3 +129,33 @@ TEST_F(OpUnfoldTest, InvalidDimAndSizeTooLargeDies) {
127129
context_,
128130
op_unfold_copy_out(input, /*dim=*/1, /*size=*/10, /*step=*/1, output));
129131
}
132+
133+
TEST_F(OpUnfoldTest, NonDefaultDimOrderSelfDies) {
134+
TensorFactory<ScalarType::Float> tf;
135+
const auto input =
136+
tf.channels_last_like(tf.make({1, 2, 2, 2}, {1, 2, 3, 4, 5, 6, 7, 8}));
137+
auto output = tf.zeros({1, 2, 2, 2, 1});
138+
139+
ET_SKIP_IF(
140+
torch::executor::testing::SupportedFeatures::get()->is_aten,
141+
"ATen kernel can handle non-default dim order");
142+
143+
ET_EXPECT_KERNEL_FAILURE(
144+
context_,
145+
op_unfold_copy_out(input, /*dim=*/3, /*size=*/1, /*step=*/1, output));
146+
}
147+
148+
TEST_F(OpUnfoldTest, NonDefaultDimOrderOutDies) {
149+
TensorFactory<ScalarType::Float> tf;
150+
const auto input = tf.make({1, 2, 2, 2}, {1, 2, 3, 4, 5, 6, 7, 8});
151+
auto output = tf.make_with_dimorder(
152+
{1, 2, 2, 2, 1}, std::vector<float>(8), {0, 2, 3, 4, 1});
153+
154+
ET_SKIP_IF(
155+
torch::executor::testing::SupportedFeatures::get()->is_aten,
156+
"ATen kernel can handle non-default dim order");
157+
158+
ET_EXPECT_KERNEL_FAILURE(
159+
context_,
160+
op_unfold_copy_out(input, /*dim=*/3, /*size=*/1, /*step=*/1, output));
161+
}

0 commit comments

Comments
 (0)