blob: 02f6cb6cf5a148fa94ceea23b72424d153fae386 [file] [log] [blame]
Lun Dongc369f682021-09-02 11:33:37 -07001// Semantic_lift quant model
2// MlModel struct initialization to include model I/O info.
3// Bytecode loading, input/output processes.
4
5#include <springbok.h>
6
7#include "iree/base/api.h"
8#include "iree/hal/api.h"
9#include "samples/util/util.h"
10
11// Compiled module embedded here to avoid file IO:
Lun Dong639870a2021-12-14 22:15:50 +000012#include "samples/quant_model/semantic_lift_bytecode_module_dylib_c.h"
Lun Dongc369f682021-09-02 11:33:37 -070013
14const MlModel kModel = {
Cindy Liue3240e22021-10-07 17:24:21 -070015 .num_input = 1,
16 .num_input_dim = {4},
17 .input_shape = {{1, 128, 128, 3}},
18 .input_length = {128 * 128 * 3},
19 .input_size_bytes = {sizeof(uint8_t)},
Lun Dongc369f682021-09-02 11:33:37 -070020 .num_output = 3,
21 .output_length = {2, 2, 2},
22 .output_size_bytes = sizeof(uint8_t),
23 .hal_element_type = IREE_HAL_ELEMENT_TYPE_UINT_8,
Lun Dongb7990a22021-09-17 21:46:30 +000024 .entry_func = "module.main",
Lun Dongc369f682021-09-02 11:33:37 -070025 .model_name = "semantic_lift_quant",
26};
27
28const iree_const_byte_span_t load_bytecode_module_data() {
29 const struct iree_file_toc_t *module_file_toc =
Lun Dong639870a2021-12-14 22:15:50 +000030 samples_quant_model_semantic_lift_bytecode_module_dylib_create();
Lun Dongc369f682021-09-02 11:33:37 -070031 return iree_make_const_byte_span(module_file_toc->data,
32 module_file_toc->size);
33}
34
35iree_status_t load_input_data(const MlModel *model, void **buffer) {
Cindy Liue3240e22021-10-07 17:24:21 -070036 iree_status_t result = alloc_input_buffer(model, buffer);
Lun Dongc369f682021-09-02 11:33:37 -070037 // Populate initial value
38 srand(66666666);
Cindy Liue3240e22021-10-07 17:24:21 -070039 if (iree_status_is_ok(result)) {
40 for (int i = 0; i < model->input_length[0]; ++i) {
41 ((uint8_t *)*buffer)[i] = (uint8_t)rand();
42 }
Lun Dongc369f682021-09-02 11:33:37 -070043 }
Cindy Liue3240e22021-10-07 17:24:21 -070044 return result;
Lun Dongc369f682021-09-02 11:33:37 -070045}
46
47iree_status_t check_output_data(const MlModel *model,
48 iree_hal_buffer_mapping_t *mapped_memory,
49 int index_output) {
50 iree_status_t result = iree_ok_status();
51 if (index_output > model->num_output ||
52 mapped_memory->contents.data_length / model->output_size_bytes !=
53 model->output_length[index_output]) {
54 result = iree_make_status(IREE_STATUS_UNKNOWN, "output length mismatches");
55 }
Cindy Liue3240e22021-10-07 17:24:21 -070056 LOG_INFO("Output #%d data length: %d", index_output,
Lun Dongc369f682021-09-02 11:33:37 -070057 mapped_memory->contents.data_length / model->output_size_bytes);
58 return result;
59}