Skip to content

Commit 05bbe79

Browse files
Check dim order in the optimized layer_norm as the portable one does
1 parent 7dd3335 commit 05bbe79

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

kernels/optimized/cpu/op_native_layer_norm.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,17 @@ std::tuple<Tensor&, Tensor&, Tensor&> opt_native_layer_norm_out(
149149
InvalidArgument,
150150
ret_val);
151151

152+
// Only support default dim order for now.
153+
// TODO: Support other dim orders.
154+
ET_KERNEL_CHECK(
155+
ctx, tensor_is_default_dim_order(input), InvalidArgument, ret_val);
156+
157+
ET_KERNEL_CHECK(
158+
ctx,
159+
tensors_have_same_dim_order(input, out, mean_out, rstd_out),
160+
InvalidArgument,
161+
ret_val);
162+
152163
Tensor::SizesType mean_rstd_sizes[kTensorDimensionLimit];
153164
size_t mean_rstd_ndim = 0;
154165
get_layer_norm_out_target_size(

kernels/test/op_native_layer_norm_test.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,3 +452,29 @@ TEST_F(OpNativeLayerNormTest, DynamicShapeUnbound) {
452452
test_dynamic_shape(
453453
{1, 1}, torch::executor::TensorShapeDynamism::DYNAMIC_UNBOUND);
454454
}
455+
456+
TEST_F(OpNativeLayerNormTest, NonDefaultDimOrderDies) {
457+
TensorFactory<ScalarType::Float> tf;
458+
459+
// mean and rstd share the input's rank with the normalized dims set to 1.
460+
// All four are channels-last so the same-dim-order check passes and only the
461+
// default dim order check can reject.
462+
Tensor input = tf.channels_last_like(
463+
tf.make({1, 3, 2, 2}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}));
464+
Tensor out0 = tf.zeros_channels_last({1, 3, 2, 2});
465+
Tensor out1 = tf.zeros_channels_last({1, 3, 2, 1});
466+
Tensor out2 = tf.zeros_channels_last({1, 3, 2, 1});
467+
const std::vector<int64_t> normalized_shape = {2};
468+
469+
ET_EXPECT_KERNEL_FAILURE(
470+
context_,
471+
op_native_layer_norm_out(
472+
input,
473+
IntArrayRef(normalized_shape.data(), normalized_shape.size()),
474+
exec_aten::optional<Tensor>(),
475+
exec_aten::optional<Tensor>(),
476+
1e-5,
477+
out0,
478+
out1,
479+
out2));
480+
}

0 commit comments

Comments
 (0)