Refactor utility functions * Make clear separation between buffer allocation model API headers. * Make clear device API in a header potentially support different HAL drivers. * Remove extern declaration and use proper declarations in the header file. Change-Id: I49ef2f3547dc39e19c3da6b8783a16e6dcffb704
diff --git a/samples/device/CMakeLists.txt b/samples/device/CMakeLists.txt index f26e234..460b738 100644 --- a/samples/device/CMakeLists.txt +++ b/samples/device/CMakeLists.txt
@@ -1,6 +1,8 @@ iree_cc_library( NAME device_embedded_sync + HDRS + "device.h" SRCS "device_embedded_sync.c" DEPS
diff --git a/samples/device/device.h b/samples/device/device.h new file mode 100644 index 0000000..df7fe29 --- /dev/null +++ b/samples/device/device.h
@@ -0,0 +1,12 @@ +#ifndef SAMPLES_DEVICE_H +#define SAMPLES_DEVICE_H + +#include "iree/base/api.h" +#include "iree/hal/api.h" + +// Create the HAL device from the different backend targets. +// The HAL device is returned based on the implementation, and it must be +// released by the caller. +iree_status_t create_sample_device(iree_hal_device_t **device); + +#endif // SAMPLES_DEVICE_H
diff --git a/samples/device/device_embedded_sync.c b/samples/device/device_embedded_sync.c index 2f10e6b..dd47484 100644 --- a/samples/device/device_embedded_sync.c +++ b/samples/device/device_embedded_sync.c
@@ -2,11 +2,10 @@ #include <stddef.h> -#include "iree/base/api.h" -#include "iree/hal/api.h" #include "iree/hal/local/executable_loader.h" #include "iree/hal/local/loaders/embedded_library_loader.h" #include "iree/hal/local/sync_device.h" +#include "samples/device/device.h" iree_status_t create_sample_device(iree_hal_device_t **device) { // Set parameters for the device created in the next step.
diff --git a/samples/util/CMakeLists.txt b/samples/util/CMakeLists.txt index f32025a..6023a18 100644 --- a/samples/util/CMakeLists.txt +++ b/samples/util/CMakeLists.txt
@@ -6,6 +6,7 @@ SRCS "util.c" DEPS + ::alloc iree::base iree::hal iree::hal::local @@ -15,5 +16,15 @@ iree::vm iree::vm::bytecode_module samples::device::device_embedded_sync - PUBLIC +) + +iree_cc_library( + NAME + alloc + HDRS + "alloc.h" + SRCS + "alloc.c" + DEPS + iree::base )
diff --git a/samples/util/alloc.c b/samples/util/alloc.c new file mode 100644 index 0000000..08cf339 --- /dev/null +++ b/samples/util/alloc.c
@@ -0,0 +1,18 @@ +#include "samples/util/alloc.h" + +iree_status_t alloc_input_buffer(const MlModel *model, + void **buffer) { + iree_status_t result = iree_ok_status(); + for (int i = 0; i < model->num_input; ++i) { + if (iree_status_is_ok(result)) { + buffer[i] = + iree_aligned_alloc( + sizeof(uint32_t), + model->input_size_bytes[i] * model->input_length[i]); + if (buffer[i] == NULL) { + result = iree_make_status(IREE_STATUS_RESOURCE_EXHAUSTED); + } + } + } + return result; +}
diff --git a/samples/util/alloc.h b/samples/util/alloc.h new file mode 100644 index 0000000..0d7f07f --- /dev/null +++ b/samples/util/alloc.h
@@ -0,0 +1,12 @@ +#ifndef SAMPLES_UTIL_ALLOC_H +#define SAMPLES_UTIL_ALLOC_H + +#include "iree/base/api.h" +#include "samples/util/model_api.h" + +// Allocate the input buffer w.r.t the model config. +// The buffer must be released by the external caller. +iree_status_t alloc_input_buffer(const MlModel *model, + void **buffer); + +#endif // SAMPLES_UTIL_ALLOC_H
diff --git a/samples/util/model_api.h b/samples/util/model_api.h new file mode 100644 index 0000000..a409019 --- /dev/null +++ b/samples/util/model_api.h
@@ -0,0 +1,45 @@ +#ifndef SW_VEC_IREE_SAMPLES_UTIL_MODEL_API_H_ +#define SW_VEC_IREE_SAMPLES_UTIL_MODEL_API_H_ + +// Define ML model configuration and model-specific utility APIs. + +#include "iree/base/api.h" +#include "iree/hal/api.h" +#include "iree/modules/hal/module.h" +#include "iree/vm/api.h" +#include "iree/vm/bytecode_module.h" + +#define MAX_MODEL_INPUT_NUM 2 +#define MAX_MODEL_INPUT_DIM 4 +#define MAX_MODEL_OUTPUTS 12 +#define MAX_ENTRY_FUNC_NAME 20 + +typedef struct { + int num_input; + int num_input_dim[MAX_MODEL_INPUT_NUM]; + iree_hal_dim_t input_shape[MAX_MODEL_INPUT_NUM][MAX_MODEL_INPUT_DIM]; + int input_length[MAX_MODEL_INPUT_NUM]; + int input_size_bytes[MAX_MODEL_INPUT_NUM]; + int num_output; + int output_length[MAX_MODEL_OUTPUTS]; + int output_size_bytes; + enum iree_hal_element_types_t hal_element_type; + char entry_func[MAX_ENTRY_FUNC_NAME]; + char model_name[]; +} MlModel; + +// Load the VM bytecode module from the embedded c library into memory. +const iree_const_byte_span_t load_bytecode_module_data(); + +// For each ML workload, based on the model configuration, allocate the buffer +// and prepare the data. It can be loaded from a embedded image binary, a +// randomly generated stream, or a pointer from the sensor/ISP output. +iree_status_t load_input_data(const MlModel *model, void **buffer); + +// Check the ML execution output, and prepare the final data to be sent to the +// host with post processing. The final format is model dependent. +iree_status_t check_output_data(const MlModel *model, + iree_hal_buffer_mapping_t *mapped_memory, + int index_output); + +#endif // SW_VEC_IREE_SAMPLES_UTIL_MODEL_API_H_
diff --git a/samples/util/util.c b/samples/util/util.c index ed0a98c..fd9ac00 100644 --- a/samples/util/util.c +++ b/samples/util/util.c
@@ -10,19 +10,7 @@ #include "iree/modules/hal/module.h" #include "iree/vm/api.h" #include "iree/vm/bytecode_module.h" - -// A function to create the HAL device from the different backend targets. -// The HAL device is returned based on the implementation, and it must be -// released by the caller -extern iree_status_t create_sample_device(iree_hal_device_t **device); - -extern const iree_const_byte_span_t load_bytecode_module_data(); - -extern iree_status_t load_input_data(const MlModel *model, void **buffer); - -extern iree_status_t check_output_data(const MlModel *model, - iree_hal_buffer_mapping_t *mapped_memory, - int index_output); +#include "samples/device/device.h" extern const MlModel kModel;
diff --git a/samples/util/util.h b/samples/util/util.h index cae9682..68881d1 100644 --- a/samples/util/util.h +++ b/samples/util/util.h
@@ -1,48 +1,9 @@ #ifndef SW_VEC_IREE_SAMPLES_UTIL_UTIL_H_ #define SW_VEC_IREE_SAMPLES_UTIL_UTIL_H_ -#include "iree/base/api.h" -#include "iree/hal/api.h" -#include "iree/modules/hal/module.h" -#include "iree/vm/api.h" -#include "iree/vm/bytecode_module.h" +// A top-level header collection for ML executable utility library. -#define MAX_MODEL_INPUT_NUM 2 -#define MAX_MODEL_INPUT_DIM 4 -#define MAX_MODEL_OUTPUTS 12 -#define MAX_ENTRY_FUNC_NAME 20 - -typedef struct { - int num_input; - int num_input_dim[MAX_MODEL_INPUT_NUM]; - iree_hal_dim_t input_shape[MAX_MODEL_INPUT_NUM][MAX_MODEL_INPUT_DIM]; - int input_length[MAX_MODEL_INPUT_NUM]; - int input_size_bytes[MAX_MODEL_INPUT_NUM]; - int num_output; - int output_length[MAX_MODEL_OUTPUTS]; - int output_size_bytes; - enum iree_hal_element_types_t hal_element_type; - char entry_func[MAX_ENTRY_FUNC_NAME]; - char model_name[]; -} MlModel; - -// Allocate the input buffer w.r.t the model config. -// The buffer must be released by the external caller. -static inline iree_status_t alloc_input_buffer(const MlModel *model, - void **buffer) { - iree_status_t result = iree_ok_status(); - for (int i = 0; i < model->num_input; ++i) { - if (iree_status_is_ok(result)) { - buffer[i] = - iree_aligned_alloc( - sizeof(uint32_t), - model->input_size_bytes[i] * model->input_length[i]); - if (buffer[i] == NULL) { - result = iree_make_status(IREE_STATUS_RESOURCE_EXHAUSTED); - } - } - } - return result; -} +#include "samples/util/alloc.h" +#include "samples/util/model_api.h" #endif // SW_VEC_IREE_SAMPLES_UTIL_UTIL_H_