Skip to content

Commit e57ee9c

Browse files
AxelNounclaude
andauthored
Enforce per-dim upper bounds for DYNAMIC_BOUND inputs in set_input (#24203)
### Summary In the portable runtime, `Method::set_input` checks a `DYNAMIC_BOUND` input only against the total element count of its serialized bound shape: `numel_bound_` is the product of those sizes, and the resize compares the incoming numel against it. A model bounded at (3, 4) therefore accepts (2, 6). `program.fbs` already states the per-dimension contract ("each dimension of the tensor at runtime should never exceed the corresponding dimension of the upper bound shape"). This PR enforces it in `set_input`, as @Gasoonjia suggested in #20546, rather than in `resize_tensor`, which is used throughout the runtime. The existing `input_tensor_meta()` lookup moves above the resize and its `Result` is now checked instead of being dereferenced directly. `DYNAMIC_UNBOUND` is skipped, since its sizes carry no bound, and rank mismatches are left to `resize_tensor`. Minimum sizes and divisibility are not serialized, so they are not checked here: a dimension that was static at export is only bounded from above. The cross-input check in `set_inputs()` discussed in the issue is not part of this PR either. From reading the code, ATen mode changes more than the portable runtime: its `resize_tensor_impl` (`tensor_util_aten.cpp`) checks only the rank, so this is the first shape bound `set_input` applies there. Part of #20546 I asked @JacobSzwejbka for a go-ahead on 4 Sep and have not heard back. I am opening this so the diff can be reviewed together with the questions below, and I will rework or close it if you would rather handle this differently. Three questions from that comment are still open: 1. Bound violations now return `InvalidArgument`. In the portable runtime, a shape that also exceeds the numel bound used to get `NotSupported` from `resize_tensor` and now gets `InvalidArgument` from the new check first (`SetInputRejectsOverflowingSizes` still passes; the second commit updates its comment accordingly). Should that case keep `NotSupported`? 2. Should there be an opt-out for callers that rely on the current lenient behaviour? On 4 Aug I said the first PR would include one, after @benITo47 and @Gasoonjia preferred the strict check on by default. This PR is on by default and has no opt-out yet, pending your answer. 3. Does turning a silently accepted input into an error need a release note? I suggested one on 4 Aug. ### Test plan Five new tests in `runtime/executor/test/method_test.cpp`: `SetInputAcceptsShapesWithinPerDimBound`, `SetInputRejectsPerDimOverflowWithinNumelBound`, `SetInputRejectsPerDimOverflowBeyondNumelBound`, `SetInputRejectsRankMismatchOnBoundedInput` and `SetInputStaticShapeBehaviorUnchanged`. Built with CMake on Ubuntu 24.04 (Release, `EXECUTORCH_BUILD_TESTS=ON`, `EXECUTORCH_ENABLE_LOGGING=ON`) on top of main at a519331: ``` ctest --test-dir cmake-out -R '^method_test$' -V ``` All 22 tests pass, at both commits and again after merging main at d6abe23. With `method.cpp` reverted to main and the new tests kept, `SetInputRejectsPerDimOverflowWithinNumelBound` and `SetInputRejectsPerDimOverflowBeyondNumelBound` fail and the other 20 pass. `lintrunner --skip MYPY` reports no issues on the two files. I have not run the ATen-mode or Buck builds, and I have not measured the binary size on this base. I used Claude Code to write this PR. cc @larryliu0820 @JacobSzwejbka @lucylq @nil-is-all --------- Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
1 parent cc2c9f1 commit e57ee9c

2 files changed

Lines changed: 191 additions & 2 deletions

File tree

‎runtime/executor/method.cpp‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1344,13 +1344,44 @@ Method::set_input(const EValue& input_evalue, size_t input_idx) {
13441344
input_idx,
13451345
numel,
13461346
executorch::runtime::elementSize(t_src.scalar_type()));
1347+
auto tensor_meta = this->method_meta().input_tensor_meta(input_idx);
1348+
ET_CHECK_OK_OR_RETURN_ERROR(
1349+
tensor_meta.error(),
1350+
"Error getting tensor metadata at input %" ET_PRIsize_t,
1351+
input_idx);
1352+
1353+
// For DYNAMIC_BOUND the serialized sizes are the per-dimension upper
1354+
// bound, but numel_bound_ is initialized to their product, so the resize
1355+
// below only rejects shapes whose total numel exceeds it. A shape that
1356+
// trades size between dimensions stays under that product and would
1357+
// otherwise be accepted. Rank mismatches are left to resize_tensor.
1358+
const auto* s_tensor = serialization_plan_->values()
1359+
->Get(get_input_index(input_idx))
1360+
->val_as_Tensor();
1361+
if (s_tensor->shape_dynamism() ==
1362+
executorch_flatbuffer::TensorShapeDynamism::DYNAMIC_BOUND) {
1363+
const Span<const int32_t> bound_sizes = tensor_meta->sizes();
1364+
if (static_cast<size_t>(t_src.dim()) == bound_sizes.size()) {
1365+
for (size_t i = 0; i < bound_sizes.size(); ++i) {
1366+
ET_CHECK_OR_RETURN_ERROR(
1367+
t_src.size(i) <= bound_sizes[i],
1368+
InvalidArgument,
1369+
"Input %" ET_PRIsize_t " dimension %" ET_PRIsize_t
1370+
" is %" ET_PRIssize_t " but its upper bound is %" PRId32 ".",
1371+
input_idx,
1372+
i,
1373+
static_cast<ssize_t>(t_src.size(i)),
1374+
bound_sizes[i]);
1375+
}
1376+
}
1377+
}
1378+
13471379
// Reset the shape for the Method's input as the size of forwarded input
13481380
// tensor for shape dynamism. Also is a safety check if need memcpy.
13491381
ET_CHECK_OK_OR_RETURN_ERROR(
13501382
resize_tensor(t_dst, t_src.sizes()),
13511383
"Error resizing tensor at input %" ET_PRIsize_t,
13521384
input_idx);
1353-
auto tensor_meta = this->method_meta().input_tensor_meta(input_idx);
13541385
if (tensor_meta->is_memory_planned()) {
13551386
ET_CHECK_OK_OR_RETURN_ERROR(
13561387
internal::copy_tensor_data(t_dst, t_src),

‎runtime/executor/test/method_test.cpp‎

Lines changed: 159 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,8 @@ TEST_F(MethodTest, SetInputRejectsOverflowingSizes) {
325325
ASSERT_EQ(method.error(), Error::Ok);
326326

327327
// Create a 2D tensor with enormous sizes. On 32-bit platforms the numel
328-
// multiplication overflows; on 64-bit the resize bounds check rejects it.
328+
// multiplication overflows; on 64-bit the per-dimension bound check in
329+
// set_input rejects it.
329330
int32_t sizes[2] = {2000000000, 2000000000};
330331
uint8_t dim_order[2] = {0, 1};
331332
int32_t strides[2] = {1, 1};
@@ -342,6 +343,163 @@ TEST_F(MethodTest, SetInputRejectsOverflowingSizes) {
342343
EXPECT_NE(input_err, Error::Ok);
343344
}
344345

346+
// The "cat" model (ModuleDynamicCatUnallocatedIO) is DYNAMIC_BOUND with a
347+
// serialized upper-bound shape of (3, 4), so numel_bound_ is 12.
348+
TEST_F(MethodTest, SetInputAcceptsShapesWithinPerDimBound) {
349+
float data[12] = {};
350+
ManagedMemoryManager mmm(kDefaultNonConstMemBytes, kDefaultRuntimeMemBytes);
351+
Result<Method> method = programs_["cat"]->load_method("forward", &mmm.get());
352+
ASSERT_EQ(method.error(), Error::Ok);
353+
354+
// (3, 4) is the bound itself, and must still be accepted.
355+
const int32_t accepted[][2] = {{3, 4}, {2, 4}, {1, 1}};
356+
for (const auto& shape : accepted) {
357+
SCOPED_TRACE(
358+
testing::Message() << "shape (" << shape[0] << ", " << shape[1] << ")");
359+
int32_t sizes[2] = {shape[0], shape[1]};
360+
uint8_t dim_order[2] = {0, 1};
361+
int32_t strides[2] = {shape[1], 1};
362+
executorch::aten::TensorImpl impl(
363+
executorch::aten::ScalarType::Float,
364+
2,
365+
sizes,
366+
data,
367+
dim_order,
368+
strides);
369+
EXPECT_EQ(
370+
method->set_input(EValue(executorch::aten::Tensor(&impl)), 0),
371+
Error::Ok);
372+
}
373+
}
374+
375+
TEST_F(MethodTest, SetInputRejectsPerDimOverflowWithinNumelBound) {
376+
float data[12] = {};
377+
ManagedMemoryManager mmm(kDefaultNonConstMemBytes, kDefaultRuntimeMemBytes);
378+
Result<Method> method = programs_["cat"]->load_method("forward", &mmm.get());
379+
ASSERT_EQ(method.error(), Error::Ok);
380+
381+
// Both shapes have numel 12, so they satisfy the numel-only bound while
382+
// exceeding the upper bound of 4 on dim 1.
383+
const int32_t rejected[][2] = {{2, 6}, {1, 12}};
384+
for (const auto& shape : rejected) {
385+
SCOPED_TRACE(
386+
testing::Message() << "shape (" << shape[0] << ", " << shape[1] << ")");
387+
int32_t sizes[2] = {shape[0], shape[1]};
388+
uint8_t dim_order[2] = {0, 1};
389+
int32_t strides[2] = {shape[1], 1};
390+
executorch::aten::TensorImpl impl(
391+
executorch::aten::ScalarType::Float,
392+
2,
393+
sizes,
394+
data,
395+
dim_order,
396+
strides);
397+
EXPECT_EQ(
398+
method->set_input(EValue(executorch::aten::Tensor(&impl)), 0),
399+
Error::InvalidArgument);
400+
}
401+
}
402+
403+
TEST_F(MethodTest, SetInputRejectsPerDimOverflowBeyondNumelBound) {
404+
float data[16] = {};
405+
ManagedMemoryManager mmm(kDefaultNonConstMemBytes, kDefaultRuntimeMemBytes);
406+
Result<Method> method = programs_["cat"]->load_method("forward", &mmm.get());
407+
ASSERT_EQ(method.error(), Error::Ok);
408+
409+
// (4, 4) exceeds both dim 0 and numel_bound_. It was already rejected, but
410+
// as NotSupported from resize_tensor; the per-dim check now reports it
411+
// first, as InvalidArgument.
412+
int32_t sizes[2] = {4, 4};
413+
uint8_t dim_order[2] = {0, 1};
414+
int32_t strides[2] = {4, 1};
415+
executorch::aten::TensorImpl impl(
416+
executorch::aten::ScalarType::Float, 2, sizes, data, dim_order, strides);
417+
418+
EXPECT_EQ(
419+
method->set_input(EValue(executorch::aten::Tensor(&impl)), 0),
420+
Error::InvalidArgument);
421+
}
422+
423+
TEST_F(MethodTest, SetInputRejectsRankMismatchOnBoundedInput) {
424+
float data[12] = {};
425+
ManagedMemoryManager mmm(kDefaultNonConstMemBytes, kDefaultRuntimeMemBytes);
426+
Result<Method> method = programs_["cat"]->load_method("forward", &mmm.get());
427+
ASSERT_EQ(method.error(), Error::Ok);
428+
429+
// The per-dim check skips these, so resize_tensor still reports the rank
430+
// change. Covered here because the check indexes the bound shape by the
431+
// input's rank.
432+
{
433+
int32_t sizes[1] = {12};
434+
uint8_t dim_order[1] = {0};
435+
int32_t strides[1] = {1};
436+
executorch::aten::TensorImpl impl(
437+
executorch::aten::ScalarType::Float,
438+
1,
439+
sizes,
440+
data,
441+
dim_order,
442+
strides);
443+
EXPECT_EQ(
444+
method->set_input(EValue(executorch::aten::Tensor(&impl)), 0),
445+
Error::NotSupported);
446+
}
447+
{
448+
int32_t sizes[3] = {1, 3, 4};
449+
uint8_t dim_order[3] = {0, 1, 2};
450+
int32_t strides[3] = {12, 4, 1};
451+
executorch::aten::TensorImpl impl(
452+
executorch::aten::ScalarType::Float,
453+
3,
454+
sizes,
455+
data,
456+
dim_order,
457+
strides);
458+
EXPECT_EQ(
459+
method->set_input(EValue(executorch::aten::Tensor(&impl)), 0),
460+
Error::NotSupported);
461+
}
462+
}
463+
464+
TEST_F(MethodTest, SetInputStaticShapeBehaviorUnchanged) {
465+
float data[4] = {};
466+
ManagedMemoryManager mmm(kDefaultNonConstMemBytes, kDefaultRuntimeMemBytes);
467+
Result<Method> method = programs_["add"]->load_method("forward", &mmm.get());
468+
ASSERT_EQ(method.error(), Error::Ok);
469+
470+
// "add" has STATIC (2, 2) inputs, which the per-dim check leaves alone.
471+
{
472+
int32_t sizes[2] = {2, 2};
473+
uint8_t dim_order[2] = {0, 1};
474+
int32_t strides[2] = {2, 1};
475+
executorch::aten::TensorImpl impl(
476+
executorch::aten::ScalarType::Float,
477+
2,
478+
sizes,
479+
data,
480+
dim_order,
481+
strides);
482+
EXPECT_EQ(
483+
method->set_input(EValue(executorch::aten::Tensor(&impl)), 0),
484+
Error::Ok);
485+
}
486+
{
487+
int32_t sizes[2] = {4, 1};
488+
uint8_t dim_order[2] = {0, 1};
489+
int32_t strides[2] = {1, 1};
490+
executorch::aten::TensorImpl impl(
491+
executorch::aten::ScalarType::Float,
492+
2,
493+
sizes,
494+
data,
495+
dim_order,
496+
strides);
497+
EXPECT_EQ(
498+
method->set_input(EValue(executorch::aten::Tensor(&impl)), 0),
499+
Error::NotSupported);
500+
}
501+
}
502+
345503
TEST_F(MethodTest, ConstantSegmentTest) {
346504
// Execute model with constants stored in segment.
347505
ManagedMemoryManager mmm(kDefaultNonConstMemBytes, kDefaultRuntimeMemBytes);

0 commit comments

Comments
 (0)