1010
1111#include < executorch/examples/arm/executor_runner/arm_memory_allocator.h>
1212#include < executorch/extension/data_loader/buffer_data_loader.h>
13- #include < executorch/runtime/core/memory_allocator.h>
1413#include < executorch/runtime/executor/program.h>
1514#include < executorch/runtime/platform/log.h>
1615#include < executorch/runtime/platform/platform.h>
@@ -34,7 +33,6 @@ static bool model_pte_runtime_initialized = false;
3433using executorch::aten::ScalarType;
3534using executorch::aten::Tensor;
3635using executorch::aten::TensorImpl;
37- using executorch::extension::BufferCleanup;
3836using executorch::extension::BufferDataLoader;
3937using executorch::runtime::Error;
4038using executorch::runtime::EValue;
@@ -85,41 +83,35 @@ unsigned char* ethosu_fast_scratch = dedicated_sram;
8583
8684namespace {
8785
88- Result<BufferCleanup> prepare_input_tensors (
86+ Error prepare_input_tensors (
8987 Method& method,
9088 MemoryAllocator& allocator,
9189 const uint8_t * input_data,
9290 size_t input_size) {
9391 MethodMeta method_meta = method.method_meta ();
9492 size_t num_inputs = method_meta.num_inputs ();
95- size_t num_allocated = 0 ;
96-
97- void ** inputs =
98- static_cast <void **>(allocator.allocate (num_inputs * sizeof (void *)));
99- ET_CHECK_OR_RETURN_ERROR (
100- inputs != nullptr ,
101- MemoryAllocationFailed,
102- " Could not allocate memory for pointers to input buffers." );
10393
10494 for (size_t i = 0 ; i < num_inputs; i++) {
10595 auto tag = method_meta.input_tag (i);
106- ET_CHECK_OK_OR_RETURN_ERROR (tag.error ());
96+ if (!tag.ok ()) {
97+ return tag.error ();
98+ }
10799
108100 if (tag.get () != Tag::Tensor) {
109101 ET_LOG (Debug, " Skipping non-tensor input %zu" , i);
110102 continue ;
111103 }
112104 Result<TensorInfo> tensor_meta = method_meta.input_tensor_meta (i);
113- ET_CHECK_OK_OR_RETURN_ERROR (tensor_meta.error ());
105+ if (!tensor_meta.ok ()) {
106+ return tensor_meta.error ();
107+ }
114108
115109 void * data_ptr = allocator.allocate (tensor_meta->nbytes ());
116- ET_CHECK_OR_RETURN_ERROR (
117- data_ptr != nullptr ,
118- MemoryAllocationFailed,
119- " Could not allocate memory for input buffers." );
120- inputs[num_allocated++] = data_ptr;
110+ if (data_ptr == nullptr ) {
111+ ET_LOG (Error, " Could not allocate memory for input buffer" );
112+ return Error::MemoryAllocationFailed;
113+ }
121114
122- Error err = Error::Ok;
123115 ScalarType scalar_type = tensor_meta->scalar_type ();
124116 size_t num_elements = 1 ;
125117 auto sizes = tensor_meta->sizes ();
@@ -156,7 +148,7 @@ Result<BufferCleanup> prepare_input_tensors(
156148 static_cast <unsigned long >(input_size),
157149 static_cast <unsigned long >(num_elements),
158150 static_cast <unsigned long >(tensor_meta->nbytes ()));
159- err = Error::InvalidArgument;
151+ return Error::InvalidArgument;
160152 }
161153
162154 TensorImpl impl = TensorImpl (
@@ -168,18 +160,14 @@ Result<BufferCleanup> prepare_input_tensors(
168160 tensor_meta.get ().dim_order ().data ()));
169161 Tensor t (&impl);
170162
171- if (err == Error::Ok) {
172- err = method.set_input (t, i);
173- }
174-
163+ Error err = method.set_input (t, i);
175164 if (err != Error::Ok) {
176165 ET_LOG (
177166 Error, " Failed to prepare input %zu: 0x%" PRIx32, i, (uint32_t )err);
178- BufferCleanup cleanup ({inputs, num_allocated});
179167 return err;
180168 }
181169 }
182- return BufferCleanup ({inputs, num_allocated}) ;
170+ return Error::Ok ;
183171}
184172
185173void print_top_k (const std::vector<EValue>& outputs) {
@@ -376,14 +364,14 @@ int main(void) {
376364 static_cast <unsigned long >(sizeof (mv2_input_data)));
377365
378366 {
379- static auto prepared_inputs = ::prepare_input_tensors (
367+ Error input_err = ::prepare_input_tensors (
380368 *method, method_allocator, mv2_input_data, sizeof (mv2_input_data));
381369
382- if (!prepared_inputs. ok () ) {
370+ if (input_err != Error::Ok ) {
383371 ET_LOG (
384372 Error,
385373 " Preparing input failed: 0x%" PRIx32,
386- static_cast <uint32_t >(prepared_inputs. error () ));
374+ static_cast <uint32_t >(input_err ));
387375 return 1 ;
388376 }
389377 }
0 commit comments