Skip to content

Commit aa78ad1

Browse files
Merge branch 'main' into export-D78323399
2 parents e6b28df + a8070ec commit aa78ad1

File tree

3 files changed

+63
-15
lines changed

3 files changed

+63
-15
lines changed

runtime/executor/method_meta.cpp

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ Result<Tag> get_tag(
5252
}
5353
}
5454

55-
size_t calculate_nbytes(
55+
Result<size_t> calculate_nbytes(
5656
Span<const int32_t> sizes,
5757
executorch::aten::ScalarType scalar_type) {
5858
size_t n = 1;
@@ -61,7 +61,13 @@ size_t calculate_nbytes(
6161
prev_n = n;
6262
n *= sizes[i];
6363
// Check for overflow
64-
ET_CHECK(sizes[i] == 0 || n / sizes[i] == prev_n);
64+
ET_CHECK_OR_RETURN_ERROR(
65+
sizes[i] == 0 || n / sizes[i] == prev_n,
66+
InvalidArgument,
67+
"Invalid size[%zu]: %d. Potentially overflowed, expect to be 0 or prev_n: %zu",
68+
i,
69+
sizes[i],
70+
prev_n);
6571
}
6672

6773
size_t elem_size = executorch::runtime::elementSize(scalar_type);
@@ -70,25 +76,47 @@ size_t calculate_nbytes(
7076
n = n * elem_size;
7177

7278
// Check for overflow
73-
ET_CHECK(elem_size == 0 || n / elem_size == prev_n);
79+
ET_CHECK_OR_RETURN_ERROR(
80+
elem_size == 0 || n / elem_size == prev_n,
81+
InvalidArgument,
82+
"Invalid elem_size: %zu. Potentially overflowed, expect to be 0 or prev_n: %zu",
83+
elem_size,
84+
prev_n);
7485

7586
return n;
7687
}
7788

7889
} // namespace
7990

91+
/*static*/ Result<TensorInfo> TensorInfo::create(
92+
Span<const int32_t> sizes,
93+
Span<const uint8_t> dim_order,
94+
executorch::aten::ScalarType scalar_type,
95+
const bool is_memory_planned,
96+
std::string_view name) {
97+
auto nbytes = calculate_nbytes(sizes, scalar_type);
98+
ET_CHECK_OR_RETURN_ERROR(
99+
nbytes.ok(),
100+
InvalidArgument,
101+
"Failed to calculate nbytes for TensorInfo");
102+
103+
return TensorInfo(
104+
sizes, dim_order, scalar_type, is_memory_planned, name, nbytes.get());
105+
}
106+
80107
TensorInfo::TensorInfo(
81108
Span<const int32_t> sizes,
82109
Span<const uint8_t> dim_order,
83110
executorch::aten::ScalarType scalar_type,
84111
const bool is_memory_planned,
85-
std::string_view name)
112+
std::string_view name,
113+
size_t nbytes)
86114
: sizes_(sizes),
87115
dim_order_(dim_order),
88116
name_(name),
89117
scalar_type_(scalar_type),
90118
is_memory_planned_(is_memory_planned),
91-
nbytes_(calculate_nbytes(sizes_, scalar_type_)) {}
119+
nbytes_(nbytes) {}
92120

93121
Span<const int32_t> TensorInfo::sizes() const {
94122
return sizes_;
@@ -160,7 +188,7 @@ Result<TensorInfo> MethodMeta::input_tensor_meta(size_t index) const {
160188
auto input_index = s_plan_->inputs()->Get(index);
161189
// input_index was already validated by input_tag().
162190
auto tensor_value = s_plan_->values()->Get(input_index)->val_as_Tensor();
163-
return TensorInfo(
191+
return TensorInfo::create(
164192
Span<const int32_t>(
165193
tensor_value->sizes()->data(), tensor_value->sizes()->size()),
166194
Span<const uint8_t>(
@@ -212,7 +240,7 @@ Result<TensorInfo> MethodMeta::output_tensor_meta(size_t index) const {
212240
// output_index was already validated by output_tag().
213241
auto tensor_value = s_plan_->values()->Get(output_index)->val_as_Tensor();
214242

215-
return TensorInfo(
243+
return TensorInfo::create(
216244
Span<const int32_t>(
217245
tensor_value->sizes()->data(), tensor_value->sizes()->size()),
218246
Span<const uint8_t>(
@@ -255,7 +283,7 @@ Result<TensorInfo> MethodMeta::attribute_tensor_meta(size_t index) const {
255283
auto t_name =
256284
tensor_value->extra_tensor_info()->fully_qualified_name();
257285
// Count constant returns as memory planned
258-
return TensorInfo(
286+
return TensorInfo::create(
259287
Span<const int32_t>(
260288
tensor_value->sizes()->data(), tensor_value->sizes()->size()),
261289
Span<const uint8_t>(

runtime/executor/method_meta.h

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,32 @@ class TensorInfo final {
7777
friend class MethodMeta;
7878
friend class testing::TensorInfoTestFriend;
7979

80-
TensorInfo(
80+
/**
81+
* Create a TensorInfo instance.
82+
*
83+
* @param[in] sizes The sizes of the tensor.
84+
* @param[in] dim_order The dim order of the tensor.
85+
* @param[in] scalar_type The scalar type of the tensor.
86+
* @param[in] is_memory_planned Whether the tensor's memory was planned.
87+
* @param[in] name The fully qualified name of the tensor.
88+
* @returns A Result containing the TensorInfo on success, or an error on
89+
* failure.
90+
*/
91+
static Result<TensorInfo> create(
8192
Span<const int32_t> sizes,
8293
Span<const uint8_t> dim_order,
8394
executorch::aten::ScalarType scalar_type,
8495
const bool is_memory_planned,
8596
std::string_view name);
8697

98+
TensorInfo(
99+
Span<const int32_t> sizes,
100+
Span<const uint8_t> dim_order,
101+
executorch::aten::ScalarType scalar_type,
102+
const bool is_memory_planned,
103+
std::string_view name,
104+
size_t nbytes);
105+
87106
/**
88107
* The sizes of the tensor.
89108
*

runtime/executor/test/method_meta_test.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,13 @@ class TensorInfoTestFriend final {
3939
executorch::aten::ScalarType scalar_type,
4040
const bool is_memory_planned,
4141
executorch::aten::string_view name) {
42-
return TensorInfo(
43-
Span<const int32_t>(sizes.data(), sizes.size()),
44-
Span<const uint8_t>(dim_order.data(), dim_order.size()),
45-
scalar_type,
46-
is_memory_planned,
47-
name);
42+
return TensorInfo::create(
43+
Span<const int32_t>(sizes.data(), sizes.size()),
44+
Span<const uint8_t>(dim_order.data(), dim_order.size()),
45+
scalar_type,
46+
is_memory_planned,
47+
name)
48+
.get();
4849
}
4950
};
5051
} // namespace testing

0 commit comments

Comments
 (0)