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 | |
Cindy Liu | 986ee24 | 2021-07-29 23:23:17 -0700 | [diff] [blame] | 17 | // Float simple_mul bytecode loading and input/output processes |
| 18 | |
Cindy Liu | 3c4d627 | 2022-08-04 18:54:12 -0700 | [diff] [blame] | 19 | #include "model_util/util.h" |
Cindy Liu | 986ee24 | 2021-07-29 23:23:17 -0700 | [diff] [blame] | 20 | |
| 21 | // Compiled module embedded here to avoid file IO: |
Lun Dong | 6243e9e | 2022-09-06 15:25:07 -0700 | [diff] [blame] | 22 | #if defined(BUILD_VMVX) |
| 23 | #if !defined(BUILD_EMITC) |
| 24 | #include "samples/simple_vec_mul/simple_float_mul_bytecode_module_vmvx_c.h" |
| 25 | #else |
| 26 | #include "samples/simple_vec_mul/simple_float_mul_c_module_vmvx_emitc.h" |
| 27 | #endif // !defined(BUILD_EMITC) |
| 28 | #else |
Lun Dong | 53c187c | 2022-01-24 18:48:13 +0000 | [diff] [blame] | 29 | #if !defined(BUILD_EMITC) |
| 30 | #include "samples/simple_vec_mul/simple_float_mul_bytecode_module_static.h" |
| 31 | #include "samples/simple_vec_mul/simple_float_mul_bytecode_module_static_c.h" |
Lun Dong | 72890d0 | 2021-12-03 18:51:03 -0800 | [diff] [blame] | 32 | #else |
| 33 | #include "samples/simple_vec_mul/simple_float_mul_c_module_static_c.h" |
| 34 | #include "samples/simple_vec_mul/simple_float_mul_c_module_static_emitc.h" |
Lun Dong | 6243e9e | 2022-09-06 15:25:07 -0700 | [diff] [blame] | 35 | #endif // #if !defined(BUILD_EMITC) |
| 36 | #endif // #if defined(BUILD_VMVX) |
Cindy Liu | 986ee24 | 2021-07-29 23:23:17 -0700 | [diff] [blame] | 37 | |
Cindy Liu | e3240e2 | 2021-10-07 17:24:21 -0700 | [diff] [blame] | 38 | const MlModel kModel = { |
| 39 | .num_input = 2, |
| 40 | .num_input_dim = {1, 1}, |
| 41 | .input_shape = {{1024}, {1024}}, |
| 42 | .input_length = {1024, 1024}, |
| 43 | .input_size_bytes = {sizeof(float), sizeof(float)}, |
| 44 | .num_output = 1, |
| 45 | .output_length = {1024}, |
| 46 | .output_size_bytes = sizeof(float), |
| 47 | .hal_element_type = IREE_HAL_ELEMENT_TYPE_FLOAT_32, |
| 48 | .entry_func = "module.simple_mul", |
| 49 | .model_name = "simple_float_vec_mul", |
| 50 | }; |
| 51 | |
Cindy Liu | caad995 | 2022-08-10 10:55:34 -0700 | [diff] [blame] | 52 | iree_status_t create_module(iree_vm_instance_t *instance, |
| 53 | iree_vm_module_t **module) { |
Lun Dong | 53c187c | 2022-01-24 18:48:13 +0000 | [diff] [blame] | 54 | #if !defined(BUILD_EMITC) |
Lun Dong | 6243e9e | 2022-09-06 15:25:07 -0700 | [diff] [blame] | 55 | #if defined(BUILD_VMVX) |
| 56 | const struct iree_file_toc_t *module_file_toc = |
| 57 | samples_simple_vec_mul_simple_float_mul_bytecode_module_vmvx_create(); |
| 58 | #else |
Cindy Liu | 986ee24 | 2021-07-29 23:23:17 -0700 | [diff] [blame] | 59 | const struct iree_file_toc_t *module_file_toc = |
Lun Dong | 53c187c | 2022-01-24 18:48:13 +0000 | [diff] [blame] | 60 | samples_simple_vec_mul_simple_float_mul_bytecode_module_static_create(); |
Lun Dong | 6243e9e | 2022-09-06 15:25:07 -0700 | [diff] [blame] | 61 | #endif // #if defined(BUILD_VMVX) |
Lun Dong | 53c187c | 2022-01-24 18:48:13 +0000 | [diff] [blame] | 62 | return iree_vm_bytecode_module_create( |
Cindy Liu | caad995 | 2022-08-10 10:55:34 -0700 | [diff] [blame] | 63 | instance, |
Lun Dong | 53c187c | 2022-01-24 18:48:13 +0000 | [diff] [blame] | 64 | iree_make_const_byte_span(module_file_toc->data, module_file_toc->size), |
| 65 | iree_allocator_null(), iree_allocator_system(), module); |
Lun Dong | 72890d0 | 2021-12-03 18:51:03 -0800 | [diff] [blame] | 66 | #else |
Cindy Liu | caad995 | 2022-08-10 10:55:34 -0700 | [diff] [blame] | 67 | return module_create(instance, iree_allocator_system(), module); |
Lun Dong | 6243e9e | 2022-09-06 15:25:07 -0700 | [diff] [blame] | 68 | #endif // #if !defined(BUILD_EMITC) |
Lun Dong | 72890d0 | 2021-12-03 18:51:03 -0800 | [diff] [blame] | 69 | } |
| 70 | |
Lun Dong | 6243e9e | 2022-09-06 15:25:07 -0700 | [diff] [blame] | 71 | #if !defined(BUILD_VMVX) |
Lun Dong | 96ec2e0 | 2022-03-08 23:30:36 +0000 | [diff] [blame] | 72 | iree_hal_executable_library_query_fn_t library_query(void) { |
| 73 | return &simple_mul_dispatch_0_library_query; |
Lun Dong | 72890d0 | 2021-12-03 18:51:03 -0800 | [diff] [blame] | 74 | } |
Lun Dong | 6243e9e | 2022-09-06 15:25:07 -0700 | [diff] [blame] | 75 | #endif |
Cindy Liu | 986ee24 | 2021-07-29 23:23:17 -0700 | [diff] [blame] | 76 | |
Lun Dong | dbc0ab8 | 2022-01-07 18:18:10 +0000 | [diff] [blame] | 77 | iree_status_t load_input_data(const MlModel *model, void **buffer, |
Lun Dong | 96ec2e0 | 2022-03-08 23:30:36 +0000 | [diff] [blame] | 78 | iree_const_byte_span_t **byte_span) { |
Cindy Liu | e3240e2 | 2021-10-07 17:24:21 -0700 | [diff] [blame] | 79 | iree_status_t result = alloc_input_buffer(model, buffer); |
Cindy Liu | 986ee24 | 2021-07-29 23:23:17 -0700 | [diff] [blame] | 80 | // Populate initial values |
| 81 | // arg0 = 0, 1/4, 1/2, 3/4... 1023/4 |
| 82 | // arg1 = 0, 1/2, 1, 3/2... 1023/2 |
Cindy Liu | 986ee24 | 2021-07-29 23:23:17 -0700 | [diff] [blame] | 83 | if (iree_status_is_ok(result)) { |
Cindy Liu | e3240e2 | 2021-10-07 17:24:21 -0700 | [diff] [blame] | 84 | for (int i = 0; i < model->input_length[0]; ++i) { |
| 85 | ((float *)buffer[0])[i] = i / 4.0f; |
| 86 | ((float *)buffer[1])[i] = i / 2.0f; |
| 87 | } |
Cindy Liu | 986ee24 | 2021-07-29 23:23:17 -0700 | [diff] [blame] | 88 | } |
Lun Dong | dbc0ab8 | 2022-01-07 18:18:10 +0000 | [diff] [blame] | 89 | for (int i = 0; i < model->num_input; ++i) { |
Lun Dong | 96ec2e0 | 2022-03-08 23:30:36 +0000 | [diff] [blame] | 90 | byte_span[i] = malloc(sizeof(iree_const_byte_span_t)); |
| 91 | *byte_span[i] = iree_make_const_byte_span( |
Lun Dong | dbc0ab8 | 2022-01-07 18:18:10 +0000 | [diff] [blame] | 92 | buffer[i], model->input_size_bytes[i] * model->input_length[i]); |
| 93 | } |
Cindy Liu | 986ee24 | 2021-07-29 23:23:17 -0700 | [diff] [blame] | 94 | return result; |
| 95 | } |
| 96 | |
Adam Jesionowski | da88093 | 2021-12-20 11:24:07 -0800 | [diff] [blame] | 97 | iree_status_t process_output(const MlModel *model, |
Lun Dong | 53c187c | 2022-01-24 18:48:13 +0000 | [diff] [blame] | 98 | iree_hal_buffer_mapping_t *buffers, |
Adam Jesionowski | 1a82e44 | 2022-09-16 10:28:59 -0700 | [diff] [blame] | 99 | uint32_t *output_length) { |
Cindy Liu | 986ee24 | 2021-07-29 23:23:17 -0700 | [diff] [blame] | 100 | iree_status_t result = iree_ok_status(); |
Lun Dong | 53c187c | 2022-01-24 18:48:13 +0000 | [diff] [blame] | 101 | for (int i = 0; i < buffers[0].contents.data_length / sizeof(float); ++i) { |
Adam Jesionowski | da88093 | 2021-12-20 11:24:07 -0800 | [diff] [blame] | 102 | if (((const float *)buffers[0].contents.data)[i] != i * i / 8.0f) { |
Cindy Liu | 986ee24 | 2021-07-29 23:23:17 -0700 | [diff] [blame] | 103 | result = iree_make_status(IREE_STATUS_UNKNOWN, "result mismatches"); |
| 104 | break; |
| 105 | } |
| 106 | } |
| 107 | return result; |
| 108 | } |