Skip to content

Commit 4567831

Browse files
Validate Method input dimension order (pytorch#23358)
## Summary `Method::set_input` now rejects supplied tensor layout metadata whose dimension order differs from the exported method input. Portable tensors without layout metadata remain accepted, while ATen tensor dimension order is derived from its strides. ## Test plan Built `method_test` with CMake and ran the complete test target through CTest. Authored with Codex.
1 parent e901324 commit 4567831

7 files changed

Lines changed: 267 additions & 10 deletions

File tree

‎extension/android/jni/jni_layer.cpp‎

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,11 @@ class TensorHybrid : public facebook::jni::HybridClass<TensorHybrid> {
104104
}
105105

106106
static TensorPtr newTensorFromJTensor(
107-
facebook::jni::alias_ref<TensorHybrid::javaobject> jtensor) {
107+
facebook::jni::alias_ref<TensorHybrid::javaobject> jtensor);
108+
109+
static TensorPtr newTensorFromJTensor(
110+
facebook::jni::alias_ref<TensorHybrid::javaobject> jtensor,
111+
std::vector<executorch::aten::DimOrderType> dim_order) {
108112
static auto cls = TensorHybrid::javaClassStatic();
109113
static const auto dtypeMethod = cls->getMethod<jint()>("dtypeJniCode");
110114
jint jdtype = dtypeMethod(jtensor);
@@ -163,14 +167,25 @@ class TensorHybrid : public facebook::jni::HybridClass<TensorHybrid> {
163167
static_cast<uint32_t>(Error::InvalidArgument), ss.str().c_str());
164168
return nullptr;
165169
}
166-
return from_blob(
170+
auto tensor = for_blob(
167171
jni->GetDirectBufferAddress(jbuffer.get()), shape_vec, scalar_type);
172+
if (!dim_order.empty() && dim_order.size() == shape_vec.size()) {
173+
return std::move(tensor)
174+
.dim_order(std::move(dim_order))
175+
.make_tensor_ptr();
176+
}
177+
return std::move(tensor).make_tensor_ptr();
168178
}
169179

170180
private:
171181
friend HybridBase;
172182
};
173183

184+
TensorPtr TensorHybrid::newTensorFromJTensor(
185+
facebook::jni::alias_ref<TensorHybrid::javaobject> jtensor) {
186+
return newTensorFromJTensor(jtensor, {});
187+
}
188+
174189
class JEValue : public facebook::jni::JavaClass<JEValue> {
175190
public:
176191
constexpr static const char* kJavaDescriptor =
@@ -227,7 +242,11 @@ class JEValue : public facebook::jni::JavaClass<JEValue> {
227242
}
228243

229244
static TensorPtr JEValueToTensorImpl(
230-
facebook::jni::alias_ref<JEValue> JEValue) {
245+
facebook::jni::alias_ref<JEValue> JEValue);
246+
247+
static TensorPtr JEValueToTensorImpl(
248+
facebook::jni::alias_ref<JEValue> JEValue,
249+
std::vector<executorch::aten::DimOrderType> dim_order) {
231250
static const auto typeCodeField =
232251
JEValue::javaClassStatic()->getField<jint>("mTypeCode");
233252
const auto typeCode = JEValue->getFieldValue(typeCodeField);
@@ -237,7 +256,7 @@ class JEValue : public facebook::jni::JavaClass<JEValue> {
237256
->getMethod<facebook::jni::alias_ref<TensorHybrid::javaobject>()>(
238257
"toTensor");
239258
auto jtensor = jMethodGetTensor(JEValue);
240-
return TensorHybrid::newTensorFromJTensor(jtensor);
259+
return TensorHybrid::newTensorFromJTensor(jtensor, std::move(dim_order));
241260
}
242261
std::stringstream ss;
243262
ss << "Unknown EValue typeCode: " << typeCode;
@@ -247,6 +266,11 @@ class JEValue : public facebook::jni::JavaClass<JEValue> {
247266
}
248267
};
249268

269+
TensorPtr JEValue::JEValueToTensorImpl(
270+
facebook::jni::alias_ref<JEValue> JEValue) {
271+
return JEValueToTensorImpl(JEValue, {});
272+
}
273+
250274
class ExecuTorchJni : public facebook::jni::HybridClass<ExecuTorchJni> {
251275
private:
252276
friend HybridBase;
@@ -458,14 +482,32 @@ class ExecuTorchJni : public facebook::jni::HybridClass<ExecuTorchJni> {
458482
std::vector<EValue> evalues;
459483
std::vector<TensorPtr> tensors;
460484

485+
auto method_meta = module_->method_meta(method);
486+
if (!method_meta.ok()) {
487+
executorch::jni_helper::throwExecutorchException(
488+
static_cast<uint32_t>(method_meta.error()),
489+
"Failed to get metadata for method: " + method);
490+
return {};
491+
}
492+
461493
static const auto typeCodeField =
462494
JEValue::javaClassStatic()->getField<jint>("mTypeCode");
463495

464496
for (int i = 0; i < jinputs->size(); i++) {
465497
auto jevalue = jinputs->getElement(i);
466498
const auto typeCode = jevalue->getFieldValue(typeCodeField);
467499
if (typeCode == JEValue::kTypeCodeTensor) {
468-
tensors.emplace_back(JEValue::JEValueToTensorImpl(jevalue));
500+
auto tensor_meta = method_meta->input_tensor_meta(i);
501+
if (!tensor_meta.ok()) {
502+
executorch::jni_helper::throwExecutorchException(
503+
static_cast<uint32_t>(tensor_meta.error()),
504+
"Failed to get tensor metadata for input " + std::to_string(i) +
505+
" of method: " + method);
506+
return {};
507+
}
508+
const auto expected_dim_order = tensor_meta->dim_order();
509+
tensors.emplace_back(JEValue::JEValueToTensorImpl(
510+
jevalue, {expected_dim_order.begin(), expected_dim_order.end()}));
469511
evalues.emplace_back(tensors.back());
470512
} else if (typeCode == JEValue::kTypeCodeInt) {
471513
static const auto toIntMethod =

‎runtime/core/exec_aten/util/tensor_util.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,6 +1254,7 @@ ET_DEPRECATED inline void resize(
12541254
* @param out_dim_order Pointing to an array of DimOrderType where we write
12551255
* dim order into it.
12561256
* @param out_dim_order_size Size of the DimOrderType array.
1257+
* @returns InvalidArgument if the tensor does not provide a dim order.
12571258
*/
12581259
ET_NODISCARD Error get_dim_order(
12591260
const executorch::aten::Tensor& tensor,

‎runtime/core/exec_aten/util/tensor_util_aten.cpp‎

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,47 @@
99
#include <executorch/runtime/core/exec_aten/util/tensor_util.h>
1010

1111
#include <ATen/Tensor.h> // @manual
12+
#include <c10/core/MemoryFormat.h>
1213
#include <c10/util/irange.h>
14+
#include <c10/util/strides.h>
1315
#include <executorch/runtime/platform/assert.h>
1416

1517
namespace executorch {
1618
namespace ET_RUNTIME_NAMESPACE {
19+
namespace {
20+
21+
Error get_dim_order_from_sizes_and_strides(
22+
at::IntArrayRef sizes,
23+
at::IntArrayRef strides,
24+
executorch::aten::DimOrderType* out_dim_order) {
25+
if (strides == c10::contiguous_strides(sizes)) {
26+
for (const auto i : c10::irange(sizes.size())) {
27+
out_dim_order[i] = static_cast<executorch::aten::DimOrderType>(i);
28+
}
29+
return Error::Ok;
30+
}
31+
if (sizes.size() == 4 &&
32+
strides == c10::get_channels_last_strides_2d(sizes)) {
33+
constexpr executorch::aten::DimOrderType channels_last[] = {0, 2, 3, 1};
34+
std::copy(
35+
std::begin(channels_last), std::end(channels_last), out_dim_order);
36+
return Error::Ok;
37+
}
38+
if (sizes.size() == 5 &&
39+
strides == c10::get_channels_last_strides_3d(sizes)) {
40+
constexpr executorch::aten::DimOrderType channels_last_3d[] = {
41+
0, 2, 3, 4, 1};
42+
std::copy(
43+
std::begin(channels_last_3d),
44+
std::end(channels_last_3d),
45+
out_dim_order);
46+
return Error::Ok;
47+
}
48+
return stride_to_dim_order(strides.data(), strides.size(), out_dim_order);
49+
}
50+
51+
} // namespace
52+
1753
/**
1854
* Implementation for ATen tensor util, should only be included in
1955
* `<target>_aten` target and only be used in ATen mode. Explicitly taking
@@ -30,8 +66,8 @@ Error get_dim_order(
3066
"out_dim_order_size needs to be equal to the number of dimensions of the tensor. out_dim_order_size %zu, tensor.dim() %" PRId64,
3167
out_dim_order_size,
3268
tensor.dim());
33-
return stride_to_dim_order(
34-
tensor.strides().data(), tensor.dim(), out_dim_order);
69+
return get_dim_order_from_sizes_and_strides(
70+
tensor.sizes(), tensor.strides(), out_dim_order);
3571
}
3672

3773
bool tensor_has_valid_dim_order(at::Tensor t) {
@@ -232,8 +268,24 @@ Error resize_tensor_impl(
232268
new_sizes.size());
233269
return torch::executor::Error::NotSupported;
234270
}
235-
// Will panic on failure.
236-
impl->set_sizes_contiguous(new_sizes);
271+
if (impl->sizes() == new_sizes) {
272+
return torch::executor::Error::Ok;
273+
}
274+
275+
std::array<executorch::aten::DimOrderType, kTensorDimensionLimit> dim_order;
276+
Error error = get_dim_order_from_sizes_and_strides(
277+
impl->sizes(), impl->strides(), dim_order.data());
278+
if (error != Error::Ok) {
279+
return error;
280+
}
281+
std::array<executorch::aten::StridesType, kTensorDimensionLimit> new_strides;
282+
error = dim_order_to_stride(
283+
new_sizes.data(), dim_order.data(), new_sizes.size(), new_strides.data());
284+
if (error != Error::Ok) {
285+
return error;
286+
}
287+
impl->set_sizes_and_strides(
288+
new_sizes, {new_strides.data(), new_sizes.size()});
237289
return torch::executor::Error::Ok;
238290
}
239291

‎runtime/core/exec_aten/util/tensor_util_portable.cpp‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ Error get_dim_order(
2727
const torch::executor::Tensor& tensor,
2828
executorch::aten::DimOrderType* out_dim_order,
2929
size_t out_dim_order_size) {
30+
if (!tensor.unsafeGetTensorImpl()->has_layout_metadata()) {
31+
return Error::InvalidArgument;
32+
}
3033
ET_CHECK_OR_RETURN_ERROR(
3134
out_dim_order_size == tensor.dim_order().size(),
3235
InvalidArgument,

‎runtime/core/exec_aten/util/test/tensor_util_test.cpp‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,38 @@ TEST_F(TensorUtilTest, ResizeZeroDimTensor) {
575575
EXPECT_EQ(a.dim(), 0);
576576
}
577577

578+
TEST_F(TensorUtilTest, GetDimOrderPreservesCanonicalChannelsLast) {
579+
Tensor tensor = tf_float_.full_channels_last({1, 1, 28, 28}, 1);
580+
executorch::aten::DimOrderType dim_order[4];
581+
582+
EXPECT_EQ(
583+
executorch::ET_RUNTIME_NAMESPACE::get_dim_order(tensor, dim_order, 4),
584+
executorch::runtime::Error::Ok);
585+
const executorch::aten::DimOrderType expected[4] = {0, 2, 3, 1};
586+
for (const auto i : c10::irange(4)) {
587+
EXPECT_EQ(dim_order[i], expected[i]);
588+
}
589+
}
590+
591+
#ifdef USE_ATEN_LIB
592+
TEST_F(TensorUtilTest, ResizePreservesChannelsLastDimOrder) {
593+
Tensor tensor = tf_float_.full_channels_last({1, 1, 28, 28}, 1);
594+
595+
EXPECT_EQ(
596+
executorch::ET_RUNTIME_NAMESPACE::resize_tensor(tensor, {1, 1, 14, 56}),
597+
executorch::runtime::Error::Ok);
598+
599+
executorch::aten::DimOrderType dim_order[4];
600+
ASSERT_EQ(
601+
executorch::ET_RUNTIME_NAMESPACE::get_dim_order(tensor, dim_order, 4),
602+
executorch::runtime::Error::Ok);
603+
const executorch::aten::DimOrderType expected[4] = {0, 2, 3, 1};
604+
for (const auto i : c10::irange(4)) {
605+
EXPECT_EQ(dim_order[i], expected[i]);
606+
}
607+
}
608+
#endif
609+
578610
TEST_F(TensorUtilTest, SameDimOrderContiguous) {
579611
using namespace torch::executor;
580612
// Three different tensors with the same shape and same dim order

‎runtime/executor/method.cpp‎

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1212,6 +1212,21 @@ Method::set_input(const EValue& input_evalue, size_t input_idx) {
12121212
input_idx,
12131213
executorch::runtime::toString(t_dst.scalar_type()),
12141214
executorch::runtime::toString(t_src.scalar_type()));
1215+
ET_CHECK_OR_RETURN_ERROR(
1216+
t_src.dim() == t_dst.dim(),
1217+
InvalidArgument,
1218+
"Input %" ET_PRIsize_t
1219+
" has unexpected rank: expected %zd but was %zd.",
1220+
input_idx,
1221+
t_dst.dim(),
1222+
t_src.dim());
1223+
ET_CHECK_OR_RETURN_ERROR(
1224+
static_cast<size_t>(t_src.dim()) <= kTensorDimensionLimit,
1225+
InvalidArgument,
1226+
"Input %" ET_PRIsize_t " rank %zd exceeds the maximum rank %zu.",
1227+
input_idx,
1228+
t_src.dim(),
1229+
kTensorDimensionLimit);
12151230

12161231
ssize_t numel = 1;
12171232
for (ssize_t i = 0; i < t_src.dim(); i++) {
@@ -1239,13 +1254,48 @@ Method::set_input(const EValue& input_evalue, size_t input_idx) {
12391254
input_idx,
12401255
numel,
12411256
executorch::runtime::elementSize(t_src.scalar_type()));
1257+
auto tensor_meta = this->method_meta().input_tensor_meta(input_idx);
1258+
std::array<executorch::aten::DimOrderType, kTensorDimensionLimit>
1259+
input_dim_order;
1260+
const Error input_dim_order_error =
1261+
get_dim_order(t_src, input_dim_order.data(), t_src.dim());
1262+
if (input_dim_order_error == Error::Ok) {
1263+
ET_CHECK_OR_RETURN_ERROR(
1264+
validate_dim_order(input_dim_order.data(), t_src.dim()),
1265+
InvalidArgument,
1266+
"Input %" ET_PRIsize_t " has an invalid dim order.",
1267+
input_idx);
1268+
const auto expected_dim_order = tensor_meta->dim_order();
1269+
size_t expected_index = 0;
1270+
bool dim_orders_match = true;
1271+
for (const auto input_index : c10::irange(t_src.dim())) {
1272+
const auto input_dim = input_dim_order[input_index];
1273+
if (t_src.size(input_dim) <= 1) {
1274+
continue;
1275+
}
1276+
while (expected_index < expected_dim_order.size() &&
1277+
t_src.size(expected_dim_order[expected_index]) <= 1) {
1278+
++expected_index;
1279+
}
1280+
if (expected_index == expected_dim_order.size() ||
1281+
input_dim != expected_dim_order[expected_index++]) {
1282+
dim_orders_match = false;
1283+
break;
1284+
}
1285+
}
1286+
ET_CHECK_OR_RETURN_ERROR(
1287+
dim_orders_match,
1288+
InvalidArgument,
1289+
"Input %" ET_PRIsize_t
1290+
" dim order does not match the method input dim order.",
1291+
input_idx);
1292+
}
12421293
// Reset the shape for the Method's input as the size of forwarded input
12431294
// tensor for shape dynamism. Also is a safety check if need memcpy.
12441295
ET_CHECK_OK_OR_RETURN_ERROR(
12451296
resize_tensor(t_dst, t_src.sizes()),
12461297
"Error resizing tensor at input %" ET_PRIsize_t,
12471298
input_idx);
1248-
auto tensor_meta = this->method_meta().input_tensor_meta(input_idx);
12491299
if (tensor_meta->is_memory_planned()) {
12501300
ET_CHECK_OK_OR_RETURN_ERROR(
12511301
internal::copy_tensor_data(t_dst, t_src),

0 commit comments

Comments
 (0)