Skip to content

Add tryTo evalue accessors#19039

Merged
lucylq merged 6 commits into
mainfrom
gh/lucylq/151/head
Apr 24, 2026
Merged

Add tryTo evalue accessors#19039
lucylq merged 6 commits into
mainfrom
gh/lucylq/151/head

Conversation

@lucylq
Copy link
Copy Markdown
Contributor

@lucylq lucylq commented Apr 22, 2026

Add tryTo accessors for each value. Previously, toTensor etc. abort with ET_CHECK_MSG when the type mismatches.

API additions:

  • Per-type: tryToInt, tryToDouble, tryToBool, tryToScalar, tryToString,
    tryToTensor (already present, kept), tryToIntList, tryToBoolList,
    tryToDoubleList, tryToTensorList, tryToListOptionalTensor,
    tryToScalarType, tryToMemoryFormat, tryToLayout, tryToDevice.
    Tag mismatch returns Error::InvalidType; null list/string payload
    returns Error::InvalidState.
  • Templated tryTo() dispatcher mirroring to(), via a new
    EVALUE_DEFINE_TRY_TO macro kept adjacent to EVALUE_DEFINE_TO so drift
    between the two surfaces is visible at review time.
  • tryToOptional() widened from Tensor-only to generic, delegating
    to tryTo() so it works for any supported payload type.

Tests cover success + mismatch paths for each new accessor, plus the
widened tryToOptional() path.

Authored-with: Claude

[ghstack-poisoned]
@lucylq
Copy link
Copy Markdown
Contributor Author

lucylq commented Apr 22, 2026

Copilot AI review requested due to automatic review settings April 22, 2026 00:15
@lucylq lucylq requested a review from JacobSzwejbka as a code owner April 22, 2026 00:15
@pytorch-bot
Copy link
Copy Markdown

pytorch-bot Bot commented Apr 22, 2026

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/19039

Note: Links to docs will display an error until the docs builds have been completed.

❗ 1 Active SEVs

There are 1 currently active SEVs. If your PR is affected, please view them below:

❌ 1 Cancelled Job, 1 Unrelated Failure

As of commit 5c4e010 with merge base 069a793 (image):

CANCELLED JOB - The following job was cancelled. Please retry:

BROKEN TRUNK - The following job failed but were present on the merge base:

👉 Rebase onto the `viable/strict` branch to avoid these failures

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Apr 22, 2026
@github-actions
Copy link
Copy Markdown

This PR needs a release notes: label

If your change should be included in the release notes (i.e. would users of this library care about this change?), please use a label starting with release notes:. This helps us keep track and include your important work in the next release notes.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "release notes: none"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a non-fatal, Result-returning “tryTo” API surface to executorch::runtime::EValue so callers can safely handle tag mismatches (and some invalid internal states) without ET_CHECK_MSG aborting—intended for paths that may process untrusted .pte inputs.

Changes:

  • Introduce tryToX() accessors for most EValue payloads (primitives, Scalar, Tensor, string, lists, and int-backed enums) plus a templated tryTo<T>() dispatcher.
  • Generalize tryToOptional<T>() to work for any supported T by delegating to tryTo<T>().
  • Add unit tests exercising a subset of the new APIs (mainly primitives/Tensor/Scalar/ScalarType and some mismatch paths).

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
runtime/core/evalue.h Adds Result-returning tryTo* accessors + templated dispatcher + generic tryToOptional<T>().
runtime/core/test/evalue_test.cpp Adds new tests for several tryTo* accessors and the generalized tryToOptional<T>().

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread runtime/core/test/evalue_test.cpp Outdated
Comment thread runtime/core/evalue.h Outdated
Comment thread runtime/core/test/evalue_test.cpp Outdated
Comment thread runtime/core/test/evalue_test.cpp Outdated
@lucylq
Copy link
Copy Markdown
Contributor Author

lucylq commented Apr 22, 2026

@claude review

@lucylq lucylq changed the title Add EValue::tryTo<T>() for all EValue payload types Add tryTo evalue accessors Apr 22, 2026
Github Executorch added 2 commits April 21, 2026 18:52
[ghstack-poisoned]
[ghstack-poisoned]
Copilot AI review requested due to automatic review settings April 22, 2026 02:23
@pytorch pytorch deleted a comment from claude Bot Apr 22, 2026
@lucylq
Copy link
Copy Markdown
Contributor Author

lucylq commented Apr 22, 2026

@claude review

@claude
Copy link
Copy Markdown

claude Bot commented Apr 22, 2026

Claude finished @lucylq's task in 1m 34s —— View job


Review of PR #19039: Add tryTo EValue accessors

  • Read changed files and understand the diff
  • Review evalue.h changes
  • Review evalue.cpp changes
  • Review test coverage in evalue_test.cpp
  • Post review feedback

Overall this is a well-structured addition. The approach of mirroring each toX() with a tryToX() returning Result<T> is clean, and having EVALUE_DEFINE_TRY_TO adjacent to EVALUE_DEFINE_TO makes drift visible. A few observations:

Design

BoxedEvalueList::tryGet() — good catch on element-level safety. The generic tryGet() template (evalue.h:841-855) correctly validates each element via tryTo<T>() instead of to<T>(), avoiding process aborts on malformed .pte data. The optional<Tensor> specialization in evalue.cpp:37-54 correctly treats null wrapped_vals_[i] as None (matching the existing get() semantics from parseListOptionalType), while the generic template treats null elements as Error::InvalidState.

tryToOptional<T>() generalization is correct. The widening from Tensor-only to generic tryToOptional<T>() (evalue.h:648-657) delegates properly to tryTo<T>(), and the EVALUE_DEFINE_TRY_TO specializations for std::optional<...> types route through tryToOptional<...> — all consistent.

Issues

  1. tryToScalar() returns by value, constructing a new Scalar — but toScalar() uses the aborting toDouble()/toInt()/toBool() internally. tryToScalar() (evalue.h:295-306) avoids this by reading the payload directly — good. However, there's a subtle inconsistency: toScalar() returns via implicit Scalar constructors from the primitive getters (which could theoretically route differently from explicit Scalar(payload...) construction). This isn't a real bug today, but worth noting the implementation divergence.

  2. tryToTensor() returns by copy, not by reference (evalue.h:360-365). The comment acknowledges this, and it's inherent to Result<T> wrapping by value. In lean mode this is free (TensorImpl* copy), but in ATen mode it's a refcount bump. Users migrating from toTensor() (which returns const& or &) to tryToTensor() need to be aware of this semantic difference. Consider documenting this in the method comment or in the PR description for downstream consumers.

  3. Minor: two whitespace fixes in EVALUE_DEFINE_TO got bundled in (evalue.h:709, evalue.h:723to<T>()&&to<T>() && and to<T>()&to<T>() &). These are correct formatting fixes but are unrelated to the tryTo feature. Not blocking, just noting they increase noise in the diff.

Test coverage gaps

The test file has solid coverage for primitives, Scalar, Tensor, and the templated tryTo<T>()/tryToOptional<T>() dispatchers. However:

  • No success-path tests for tryToString, tryToIntList, tryToBoolList, tryToDoubleList, tryToTensorList, tryToListOptionalTensor. The mismatch paths are covered for TensorList and ListOptionalTensor only. Adding at least one success test per list/string accessor would verify the end-to-end path through the null-pointer check and tryGet().
    Add tests →

  • No success-path tests for tryToMemoryFormat, tryToLayout, tryToDevice. Only mismatch tests exist. These int-backed enum conversions should have a basic success test to confirm the cast is wired correctly.
    Add tests →

  • No InvalidState coverage for pointer-backed tryTo* methods. The death tests cover the aborting toString()/toIntList() etc. on null pointers, but there are no tests verifying that tryToString()/tryToIntList() etc. return Error::InvalidState for the same null-pointer scenario. Since these are the defensive paths this PR is designed to enable, they should be tested.
    Add tests →

Summary

The core implementation is correct and well-motivated. The BoxedEvalueList::tryGet() addition properly addresses the element-level abort concern. The main gap is test coverage — adding success-path and InvalidState tests for the pointer-backed accessors would make this robust. The rest looks good to land.


Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread runtime/core/test/evalue_test.cpp Outdated
Comment thread runtime/core/evalue.h
Comment thread runtime/core/test/evalue_test.cpp Outdated
Github Executorch added 2 commits April 22, 2026 10:51
[ghstack-poisoned]
[ghstack-poisoned]
Copilot AI review requested due to automatic review settings April 22, 2026 18:01
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread runtime/core/test/evalue_test.cpp
Comment thread runtime/core/evalue.h
[ghstack-poisoned]
@lucylq lucylq requested a review from mergennachin April 23, 2026 22:44
@lucylq lucylq merged commit c3f3d12 into main Apr 24, 2026
174 of 176 checks passed
@lucylq lucylq deleted the gh/lucylq/151/head branch April 24, 2026 00:23
zeel2104 pushed a commit to zeel2104/executorch that referenced this pull request May 5, 2026
Add tryTo accessors for each value. Previously, `toTensor` etc. abort
with ET_CHECK_MSG when the type mismatches.

API additions:
- Per-type: tryToInt, tryToDouble, tryToBool, tryToScalar, tryToString,
  tryToTensor (already present, kept), tryToIntList, tryToBoolList,
  tryToDoubleList, tryToTensorList, tryToListOptionalTensor,
  tryToScalarType, tryToMemoryFormat, tryToLayout, tryToDevice.
  Tag mismatch returns Error::InvalidType; null list/string payload
  returns Error::InvalidState.
- Templated tryTo<T>() dispatcher mirroring to<T>(), via a new
  EVALUE_DEFINE_TRY_TO macro kept adjacent to EVALUE_DEFINE_TO so drift
  between the two surfaces is visible at review time.
- tryToOptional<T>() widened from Tensor-only to generic, delegating
  to tryTo<T>() so it works for any supported payload type.

Tests cover success + mismatch paths for each new accessor, plus the
widened tryToOptional<T>() path.

Authored-with: Claude

---------

Co-authored-by: Github Executorch <github_executorch@arm.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants