@@ -289,42 +289,6 @@ Result<bool> parse_cond_value(const EValue& cond_value) {
289289
290290} // namespace
291291
292- Result<size_t > Method::get_num_external_constants () {
293- auto flatbuffer_values = serialization_plan_->values ();
294- size_t n_value = flatbuffer_values->size ();
295-
296- size_t num_external_constants = 0 ;
297- for (size_t i = 0 ; i < n_value; ++i) {
298- auto serialization_value = flatbuffer_values->Get (i);
299- // Ensure that the `val_as_X()` calls will return non-null pointers.
300- ET_CHECK_OR_RETURN_ERROR (
301- serialization_value != nullptr &&
302- (serialization_value->val_type () ==
303- executorch_flatbuffer::KernelTypes::Null ||
304- serialization_value->val () != nullptr ),
305- InvalidProgram,
306- " Null value at index %" ET_PRIsize_t,
307- i);
308- // Ignore non-tensor types.
309- if (serialization_value->val_type () !=
310- executorch_flatbuffer::KernelTypes::Tensor) {
311- continue ;
312- }
313- const auto s_tensor = static_cast <const executorch_flatbuffer::Tensor*>(
314- serialization_value->val ());
315-
316- // An external constant is tagged with EXTERNAL and has no
317- // allocation_info.
318- if (s_tensor->extra_tensor_info () != nullptr &&
319- s_tensor->extra_tensor_info ()->location () ==
320- executorch_flatbuffer::TensorDataLocation::EXTERNAL &&
321- s_tensor->allocation_info () == nullptr ) {
322- num_external_constants++;
323- }
324- }
325- return num_external_constants;
326- }
327-
328292bool key_exists (const char * key, NamedData* external_constants, int num_keys) {
329293 for (int i = 0 ; i < num_keys; i++) {
330294 if (strcmp (key, external_constants[i].key ) == 0 ) {
@@ -342,6 +306,15 @@ Error Method::parse_external_constants(const NamedDataMap* named_data_map) {
342306 int index = 0 ;
343307 for (size_t i = 0 ; i < n_value; ++i) {
344308 auto serialization_value = flatbuffer_values->Get (i);
309+ // Ensure that the `val_as_X()` calls will return non-null pointers.
310+ ET_CHECK_OR_RETURN_ERROR (
311+ serialization_value != nullptr &&
312+ (serialization_value->val_type () ==
313+ executorch_flatbuffer::KernelTypes::Null ||
314+ serialization_value->val () != nullptr ),
315+ InvalidProgram,
316+ " Null value at index %" ET_PRIsize_t,
317+ i);
345318 // Ignore non-tensor types.
346319 if (serialization_value->val_type () !=
347320 executorch_flatbuffer::KernelTypes::Tensor) {
@@ -412,11 +385,7 @@ Error Method::parse_values(const NamedDataMap* named_data_map) {
412385 }
413386
414387 // Check if there are any external constants.
415- Result<size_t > num_external_constants = get_num_external_constants ();
416- if (!num_external_constants.ok ()) {
417- return num_external_constants.error ();
418- }
419- num_external_constants_ = *num_external_constants;
388+ num_external_constants_ = serialization_plan_->num_external_constants ();
420389 if (num_external_constants_ > 0 ) {
421390 // Allocate space for external tensors.
422391 external_constants_ =
0 commit comments