Adam Jesionowski | 6e273a7 | 2022-04-14 12:20:20 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2022 Google LLC |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Lun Dong | c369f68 | 2021-09-02 11:33:37 -0700 | [diff] [blame] | 17 | // Semantic_lift quant model |
| 18 | // MlModel struct initialization to include model I/O info. |
| 19 | // Bytecode loading, input/output processes. |
| 20 | |
| 21 | #include <springbok.h> |
| 22 | |
| 23 | #include "iree/base/api.h" |
| 24 | #include "iree/hal/api.h" |
| 25 | #include "samples/util/util.h" |
| 26 | |
| 27 | // Compiled module embedded here to avoid file IO: |
Lun Dong | 53c187c | 2022-01-24 18:48:13 +0000 | [diff] [blame] | 28 | #if !defined(BUILD_EMITC) |
| 29 | #include "samples/quant_model/semantic_lift_bytecode_module_static.h" |
| 30 | #include "samples/quant_model/semantic_lift_bytecode_module_static_c.h" |
| 31 | #else |
| 32 | #include "samples/quant_model/semantic_lift_c_module_static_c.h" |
| 33 | #include "samples/quant_model/semantic_lift_c_module_static_emitc.h" |
| 34 | #endif |
Lun Dong | c369f68 | 2021-09-02 11:33:37 -0700 | [diff] [blame] | 35 | |
| 36 | const MlModel kModel = { |
Cindy Liu | e3240e2 | 2021-10-07 17:24:21 -0700 | [diff] [blame] | 37 | .num_input = 1, |
| 38 | .num_input_dim = {4}, |
| 39 | .input_shape = {{1, 128, 128, 3}}, |
| 40 | .input_length = {128 * 128 * 3}, |
| 41 | .input_size_bytes = {sizeof(uint8_t)}, |
Lun Dong | c369f68 | 2021-09-02 11:33:37 -0700 | [diff] [blame] | 42 | .num_output = 3, |
| 43 | .output_length = {2, 2, 2}, |
| 44 | .output_size_bytes = sizeof(uint8_t), |
| 45 | .hal_element_type = IREE_HAL_ELEMENT_TYPE_UINT_8, |
Lun Dong | b7990a2 | 2021-09-17 21:46:30 +0000 | [diff] [blame] | 46 | .entry_func = "module.main", |
Lun Dong | c369f68 | 2021-09-02 11:33:37 -0700 | [diff] [blame] | 47 | .model_name = "semantic_lift_quant", |
| 48 | }; |
| 49 | |
Lun Dong | 53c187c | 2022-01-24 18:48:13 +0000 | [diff] [blame] | 50 | iree_status_t create_module(iree_vm_module_t **module) { |
| 51 | #if !defined(BUILD_EMITC) |
Lun Dong | c369f68 | 2021-09-02 11:33:37 -0700 | [diff] [blame] | 52 | const struct iree_file_toc_t *module_file_toc = |
Lun Dong | 53c187c | 2022-01-24 18:48:13 +0000 | [diff] [blame] | 53 | samples_quant_model_semantic_lift_bytecode_module_static_create(); |
| 54 | return iree_vm_bytecode_module_create( |
| 55 | iree_make_const_byte_span(module_file_toc->data, module_file_toc->size), |
| 56 | iree_allocator_null(), iree_allocator_system(), module); |
| 57 | #else |
| 58 | return module_create(iree_allocator_system(), module); |
| 59 | #endif |
| 60 | } |
| 61 | |
| 62 | const iree_hal_executable_library_header_t **library_query( |
| 63 | iree_hal_executable_library_version_t max_version, void *reserved) { |
| 64 | #if !defined(BUILD_EMITC) |
| 65 | return semantic_lift_bytecode_module_static_linked_llvm_library_query( |
| 66 | max_version, |
| 67 | /*reserved=*/reserved); |
| 68 | #else |
| 69 | return semantic_lift_c_module_static_linked_llvm_library_query( |
| 70 | max_version, |
| 71 | /*reserved=*/reserved); |
| 72 | #endif |
Lun Dong | c369f68 | 2021-09-02 11:33:37 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Lun Dong | dbc0ab8 | 2022-01-07 18:18:10 +0000 | [diff] [blame] | 75 | iree_status_t load_input_data(const MlModel *model, void **buffer, |
| 76 | iree_byte_span_t **byte_span) { |
Cindy Liu | e3240e2 | 2021-10-07 17:24:21 -0700 | [diff] [blame] | 77 | iree_status_t result = alloc_input_buffer(model, buffer); |
Lun Dong | c369f68 | 2021-09-02 11:33:37 -0700 | [diff] [blame] | 78 | // Populate initial value |
| 79 | srand(66666666); |
Cindy Liu | e3240e2 | 2021-10-07 17:24:21 -0700 | [diff] [blame] | 80 | if (iree_status_is_ok(result)) { |
| 81 | for (int i = 0; i < model->input_length[0]; ++i) { |
| 82 | ((uint8_t *)*buffer)[i] = (uint8_t)rand(); |
| 83 | } |
Lun Dong | c369f68 | 2021-09-02 11:33:37 -0700 | [diff] [blame] | 84 | } |
Lun Dong | dbc0ab8 | 2022-01-07 18:18:10 +0000 | [diff] [blame] | 85 | byte_span[0] = malloc(sizeof(iree_byte_span_t)); |
| 86 | *byte_span[0] = iree_make_byte_span( |
| 87 | buffer[0], model->input_size_bytes[0] * model->input_length[0]); |
Cindy Liu | e3240e2 | 2021-10-07 17:24:21 -0700 | [diff] [blame] | 88 | return result; |
Lun Dong | c369f68 | 2021-09-02 11:33:37 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Adam Jesionowski | da88093 | 2021-12-20 11:24:07 -0800 | [diff] [blame] | 91 | iree_status_t process_output(const MlModel *model, |
Lun Dong | 53c187c | 2022-01-24 18:48:13 +0000 | [diff] [blame] | 92 | iree_hal_buffer_mapping_t *buffers, |
| 93 | MlOutput *output) { |
Adam Jesionowski | da88093 | 2021-12-20 11:24:07 -0800 | [diff] [blame] | 94 | return iree_ok_status(); |
Lun Dong | c369f68 | 2021-09-02 11:33:37 -0700 | [diff] [blame] | 95 | } |