Lun Dong | c369f68 | 2021-09-02 11:33:37 -0700 | [diff] [blame] | 1 | // 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 Dong | 639870a | 2021-12-14 22:15:50 +0000 | [diff] [blame] | 12 | #include "samples/quant_model/semantic_lift_bytecode_module_dylib_c.h" |
Lun Dong | c369f68 | 2021-09-02 11:33:37 -0700 | [diff] [blame] | 13 | |
| 14 | const MlModel kModel = { |
Cindy Liu | e3240e2 | 2021-10-07 17:24:21 -0700 | [diff] [blame] | 15 | .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 Dong | c369f68 | 2021-09-02 11:33:37 -0700 | [diff] [blame] | 20 | .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 Dong | b7990a2 | 2021-09-17 21:46:30 +0000 | [diff] [blame] | 24 | .entry_func = "module.main", |
Lun Dong | c369f68 | 2021-09-02 11:33:37 -0700 | [diff] [blame] | 25 | .model_name = "semantic_lift_quant", |
| 26 | }; |
| 27 | |
| 28 | const iree_const_byte_span_t load_bytecode_module_data() { |
| 29 | const struct iree_file_toc_t *module_file_toc = |
Lun Dong | 639870a | 2021-12-14 22:15:50 +0000 | [diff] [blame] | 30 | samples_quant_model_semantic_lift_bytecode_module_dylib_create(); |
Lun Dong | c369f68 | 2021-09-02 11:33:37 -0700 | [diff] [blame] | 31 | return iree_make_const_byte_span(module_file_toc->data, |
| 32 | module_file_toc->size); |
| 33 | } |
| 34 | |
| 35 | iree_status_t load_input_data(const MlModel *model, void **buffer) { |
Cindy Liu | e3240e2 | 2021-10-07 17:24:21 -0700 | [diff] [blame] | 36 | iree_status_t result = alloc_input_buffer(model, buffer); |
Lun Dong | c369f68 | 2021-09-02 11:33:37 -0700 | [diff] [blame] | 37 | // Populate initial value |
| 38 | srand(66666666); |
Cindy Liu | e3240e2 | 2021-10-07 17:24:21 -0700 | [diff] [blame] | 39 | 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 Dong | c369f68 | 2021-09-02 11:33:37 -0700 | [diff] [blame] | 43 | } |
Cindy Liu | e3240e2 | 2021-10-07 17:24:21 -0700 | [diff] [blame] | 44 | return result; |
Lun Dong | c369f68 | 2021-09-02 11:33:37 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | iree_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 Liu | e3240e2 | 2021-10-07 17:24:21 -0700 | [diff] [blame] | 56 | LOG_INFO("Output #%d data length: %d", index_output, |
Lun Dong | c369f68 | 2021-09-02 11:33:37 -0700 | [diff] [blame] | 57 | mapped_memory->contents.data_length / model->output_size_bytes); |
| 58 | return result; |
| 59 | } |