Repository navigation
Commit e57ee9c
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
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1344 | 1344 | | |
1345 | 1345 | | |
1346 | 1346 | | |
| 1347 | + | |
| 1348 | + | |
| 1349 | + | |
| 1350 | + | |
| 1351 | + | |
| 1352 | + | |
| 1353 | + | |
| 1354 | + | |
| 1355 | + | |
| 1356 | + | |
| 1357 | + | |
| 1358 | + | |
| 1359 | + | |
| 1360 | + | |
| 1361 | + | |
| 1362 | + | |
| 1363 | + | |
| 1364 | + | |
| 1365 | + | |
| 1366 | + | |
| 1367 | + | |
| 1368 | + | |
| 1369 | + | |
| 1370 | + | |
| 1371 | + | |
| 1372 | + | |
| 1373 | + | |
| 1374 | + | |
| 1375 | + | |
| 1376 | + | |
| 1377 | + | |
| 1378 | + | |
1347 | 1379 | | |
1348 | 1380 | | |
1349 | 1381 | | |
1350 | 1382 | | |
1351 | 1383 | | |
1352 | 1384 | | |
1353 | | - | |
1354 | 1385 | | |
1355 | 1386 | | |
1356 | 1387 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
325 | 325 | | |
326 | 326 | | |
327 | 327 | | |
328 | | - | |
| 328 | + | |
| 329 | + | |
329 | 330 | | |
330 | 331 | | |
331 | 332 | | |
| |||
342 | 343 | | |
343 | 344 | | |
344 | 345 | | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
345 | 503 | | |
346 | 504 | | |
347 | 505 | | |
| |||
0 commit comments