blob: 474f0227b00a131d813d6c6efa793adbfc631d3d [file] [log] [blame]
Adam Jesionowski6e273a72022-04-14 12:20:20 -07001/*
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 Dongc369f682021-09-02 11:33:37 -070017// 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 Dong53c187c2022-01-24 18:48:13 +000028#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 Dongc369f682021-09-02 11:33:37 -070035
36const MlModel kModel = {
Cindy Liue3240e22021-10-07 17:24:21 -070037 .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 Dongc369f682021-09-02 11:33:37 -070042 .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 Dongb7990a22021-09-17 21:46:30 +000046 .entry_func = "module.main",
Lun Dongc369f682021-09-02 11:33:37 -070047 .model_name = "semantic_lift_quant",
48};
49
Lun Dong53c187c2022-01-24 18:48:13 +000050iree_status_t create_module(iree_vm_module_t **module) {
51#if !defined(BUILD_EMITC)
Lun Dongc369f682021-09-02 11:33:37 -070052 const struct iree_file_toc_t *module_file_toc =
Lun Dong53c187c2022-01-24 18:48:13 +000053 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
62const 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 Dongc369f682021-09-02 11:33:37 -070073}
74
Lun Dongdbc0ab82022-01-07 18:18:10 +000075iree_status_t load_input_data(const MlModel *model, void **buffer,
76 iree_byte_span_t **byte_span) {
Cindy Liue3240e22021-10-07 17:24:21 -070077 iree_status_t result = alloc_input_buffer(model, buffer);
Lun Dongc369f682021-09-02 11:33:37 -070078 // Populate initial value
79 srand(66666666);
Cindy Liue3240e22021-10-07 17:24:21 -070080 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 Dongc369f682021-09-02 11:33:37 -070084 }
Lun Dongdbc0ab82022-01-07 18:18:10 +000085 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 Liue3240e22021-10-07 17:24:21 -070088 return result;
Lun Dongc369f682021-09-02 11:33:37 -070089}
90
Adam Jesionowskida880932021-12-20 11:24:07 -080091iree_status_t process_output(const MlModel *model,
Lun Dong53c187c2022-01-24 18:48:13 +000092 iree_hal_buffer_mapping_t *buffers,
93 MlOutput *output) {
Adam Jesionowskida880932021-12-20 11:24:07 -080094 return iree_ok_status();
Lun Dongc369f682021-09-02 11:33:37 -070095}