| /* |
| * Copyright 2022 Google LLC |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| #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/hal/local/executable_library.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; |
| |
| typedef struct { |
| void* result; |
| uint32_t len; |
| } MlOutput; |
| |
| // Load the statically embedded library |
| const iree_hal_executable_library_header_t **library_query( |
| iree_hal_executable_library_version_t max_version, void *reserved); |
| |
| // Function to create the bytecode or C module. |
| iree_status_t create_module(iree_vm_module_t **module); |
| |
| // 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, |
| iree_byte_span_t **byte_span); |
| |
| // Process the ML execution output into the final data to be sent to the |
| // host. The final format is model dependent, so the address and size |
| // are returned via `output.` |
| iree_status_t process_output(const MlModel *model, |
| iree_hal_buffer_mapping_t *buffers, |
| MlOutput *output); |
| |
| #endif // SW_VEC_IREE_SAMPLES_UTIL_MODEL_API_H_ |