Skip to content

Commit 1249f01

Browse files
Check dim order in the optimized mm and bmm as the portable ones do
1 parent 60cb889 commit 1249f01

4 files changed

Lines changed: 45 additions & 0 deletions

File tree

‎kernels/optimized/cpu/op_bmm.cpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,11 @@ Tensor& opt_bmm_out(
150150
ET_KERNEL_CHECK(
151151
ctx, check_bmm_out_args(self, mat2, out), InvalidArgument, out);
152152

153+
ET_KERNEL_CHECK(
154+
ctx, tensors_have_same_dim_order(self, mat2, out), InvalidArgument, out);
155+
156+
ET_KERNEL_CHECK(ctx, tensor_is_default_dim_order(self), InvalidArgument, out);
157+
153158
static constexpr auto name = "bmm.out";
154159

155160
auto self_type = self.scalar_type();

‎kernels/optimized/cpu/op_mm.cpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ Tensor& opt_mm_out(
3434
InvalidArgument,
3535
out);
3636

37+
ET_KERNEL_CHECK(
38+
ctx, tensors_have_same_dim_order(in, mat2, out), InvalidArgument, out);
39+
40+
ET_KERNEL_CHECK(ctx, tensor_is_default_dim_order(in), InvalidArgument, out);
41+
3742
if (out.numel() == 0) {
3843
return out;
3944
}

‎kernels/test/op_bmm_test.cpp‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,3 +471,22 @@ TEST_F(OpBmmOutTest, DISABLED_DynamicShapeUnbound) {
471471
Tensor ret = op_bmm_out(x, y, out);
472472
EXPECT_TENSOR_CLOSE(out, expected_result);
473473
}
474+
475+
TEST_F(OpBmmOutTest, NonDefaultDimOrderDies) {
476+
TensorFactory<ScalarType::Float> tf;
477+
478+
// All three tensors share the same non-default dim order, so the kernel's
479+
// same dim order check passes and only the default dim order check rejects.
480+
Tensor x =
481+
tf.make_with_dimorder({2, 3, 4}, std::vector<float>(24, 2), {0, 2, 1});
482+
Tensor y =
483+
tf.make_with_dimorder({2, 4, 5}, std::vector<float>(40, 3), {0, 2, 1});
484+
Tensor out =
485+
tf.make_with_dimorder({2, 3, 5}, std::vector<float>(30), {0, 2, 1});
486+
487+
ET_SKIP_IF(
488+
torch::executor::testing::SupportedFeatures::get()->is_aten,
489+
"ATen kernel can handle non-default dim order");
490+
491+
ET_EXPECT_KERNEL_FAILURE(context_, op_bmm_out(x, y, out));
492+
}

‎kernels/test/op_mm_test.cpp‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,3 +294,19 @@ TEST_F(OpMmOutTest, DISABLED_DynamicShapeUnbound) {
294294
Tensor ret = op_mm_out(x, y, out);
295295
EXPECT_TENSOR_CLOSE(out, expected_result);
296296
}
297+
298+
TEST_F(OpMmOutTest, NonDefaultDimOrderDies) {
299+
TensorFactory<ScalarType::Float> tf;
300+
301+
// All three tensors share the same non-default dim order, so the kernel's
302+
// same dim order check passes and only the default dim order check rejects.
303+
Tensor x = tf.make_with_dimorder({3, 4}, std::vector<float>(12, 2), {1, 0});
304+
Tensor y = tf.make_with_dimorder({4, 5}, std::vector<float>(20, 3), {1, 0});
305+
Tensor out = tf.make_with_dimorder({3, 5}, std::vector<float>(15), {1, 0});
306+
307+
ET_SKIP_IF(
308+
torch::executor::testing::SupportedFeatures::get()->is_aten,
309+
"ATen kernel can handle non-default dim order");
310+
311+
ET_EXPECT_KERNEL_FAILURE(context_, op_mm_out(x, y, out));
312+
}

0 commit comments

Comments
 (0)