Skip to content

Commit 8728a45

Browse files
committed
Address remaining Copilot feedback and lint
- Remove BufferCleanup UB: replace with plain Error return since buffers are arena-allocated (not malloc), so free() would be undefined behavior - Add Corstone-300 board-specific pool size overrides (768KB each) to fit within 2 MiB ISRAM budget - Fix README module path to use in-repo invocation - Apply lintrunner formatting to mv2_input.h
1 parent 46092d6 commit 8728a45

4 files changed

Lines changed: 12568 additions & 9440 deletions

File tree

zephyr/samples/mv2-ethosu/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ ImageNet classes and prints the top-5 predictions.
1717
Export a quantized INT8 MobileNetV2 model with Ethos-U delegation:
1818

1919
```bash
20-
python -m executorch.backends.arm.scripts.aot_arm_compiler \
20+
python -m modules.lib.executorch.backends.arm.scripts.aot_arm_compiler \
2121
--model_name=mv2_untrained \
2222
--quantize \
2323
--delegate \

zephyr/samples/mv2-ethosu/boards/mps3_corstone300_fvp.conf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,8 @@
55
# Enable the Zephyr Ethos-U driver so executorch_delegate_ethos_u can reserve
66
# and use the hardware instance exposed by the board DTS.
77
CONFIG_ETHOS_U=y
8+
9+
# Corstone-300 has 2 MiB ISRAM. Reduce pool sizes to fit within budget
10+
# alongside stack, heap, model data, and runtime buffers.
11+
CONFIG_EXECUTORCH_METHOD_ALLOCATOR_POOL_SIZE=786432
12+
CONFIG_EXECUTORCH_TEMP_ALLOCATOR_POOL_SIZE=786432

zephyr/samples/mv2-ethosu/src/main.cpp

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
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;
3433
using executorch::aten::ScalarType;
3534
using executorch::aten::Tensor;
3635
using executorch::aten::TensorImpl;
37-
using executorch::extension::BufferCleanup;
3836
using executorch::extension::BufferDataLoader;
3937
using executorch::runtime::Error;
4038
using executorch::runtime::EValue;
@@ -85,41 +83,35 @@ unsigned char* ethosu_fast_scratch = dedicated_sram;
8583

8684
namespace {
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

185173
void 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

Comments
 (0)