Skip to content

Commit c3f3d12

Browse files
lucylqGithub Executorch
andauthored
Add tryTo evalue accessors (#19039)
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>
1 parent 75b31bb commit c3f3d12

3 files changed

Lines changed: 444 additions & 0 deletions

File tree

runtime/core/evalue.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010

1111
namespace executorch {
1212
namespace runtime {
13+
14+
// Specialize for list of optional tensors, as nullptr is a valid std::nullopt.
15+
// For non-optional types, nullptr is invalid.
16+
1317
template <>
1418
executorch::aten::ArrayRef<std::optional<executorch::aten::Tensor>>
1519
BoxedEvalueList<std::optional<executorch::aten::Tensor>>::get() const {
@@ -27,5 +31,26 @@ BoxedEvalueList<std::optional<executorch::aten::Tensor>>::get() const {
2731
return executorch::aten::ArrayRef<std::optional<executorch::aten::Tensor>>{
2832
unwrapped_vals_, wrapped_vals_.size()};
2933
}
34+
35+
template <>
36+
Result<executorch::aten::ArrayRef<std::optional<executorch::aten::Tensor>>>
37+
BoxedEvalueList<std::optional<executorch::aten::Tensor>>::tryGet() const {
38+
for (typename executorch::aten::ArrayRef<
39+
std::optional<executorch::aten::Tensor>>::size_type i = 0;
40+
i < wrapped_vals_.size();
41+
i++) {
42+
if (wrapped_vals_[i] == nullptr) {
43+
unwrapped_vals_[i] = std::nullopt;
44+
continue;
45+
}
46+
auto r = wrapped_vals_[i]->tryToOptional<executorch::aten::Tensor>();
47+
if (!r.ok()) {
48+
return r.error();
49+
}
50+
unwrapped_vals_[i] = std::move(r.get());
51+
}
52+
return executorch::aten::ArrayRef<std::optional<executorch::aten::Tensor>>{
53+
unwrapped_vals_, wrapped_vals_.size()};
54+
}
3055
} // namespace runtime
3156
} // namespace executorch

0 commit comments

Comments
 (0)