1 /** 2 * Copyright 2021 Huawei Technologies Co., Ltd 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 17 #include "coder/generator/component/const_blocks/load_input.h" 18 19 namespace mindspore::lite::micro { 20 const char load_input_h[] = R"RAW( 21 /** 22 * Copyright 2021 Huawei Technologies Co., Ltd 23 * 24 * Licensed under the Apache License, Version 2.0 (the "License"); 25 * you may not use this file except in compliance with the License. 26 * You may obtain a copy of the License at 27 * 28 * http://www.apache.org/licenses/LICENSE-2.0 29 * 30 * Unless required by applicable law or agreed to in writing, software 31 * distributed under the License is distributed on an "AS IS" BASIS, 32 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 33 * See the License for the specific language governing permissions and 34 * limitations under the License. 35 */ 36 37 #ifndef MICRO_EXAMPLE_LOAD_INPUT_LOAD_INPUT_H_ 38 #define MICRO_EXAMPLE_LOAD_INPUT_LOAD_INPUT_H_ 39 40 #ifdef __cplusplus 41 extern "C" { 42 #endif 43 44 void *ReadInputData(const char *real_input_path, int *size); 45 46 void SaveOutputData(char *final_name, unsigned char *output_data, unsigned int out_size); 47 48 int ReadInputsFile(char *path, void **buffers, const int *inputs_size, int inputs_num); 49 50 #ifdef __cplusplus 51 } 52 #endif 53 54 #endif // MICRO_EXAMPLE_LOAD_INPUT_LOAD_INPUT_H_ 55 56 )RAW"; 57 58 const char load_input_c[] = R"RAW( 59 /** 60 * Copyright 2021 Huawei Technologies Co., Ltd 61 * 62 * Licensed under the Apache License, Version 2.0 (the "License"); 63 * you may not use this file except in compliance with the License. 64 * You may obtain a copy of the License at 65 * 66 * http://www.apache.org/licenses/LICENSE-2.0 67 * 68 * Unless required by applicable law or agreed to in writing, software 69 * distributed under the License is distributed on an "AS IS" BASIS, 70 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 71 * See the License for the specific language governing permissions and 72 * limitations under the License. 73 */ 74 75 #include "load_input.h" 76 #include <stdlib.h> 77 #include <stdio.h> 78 #include <string.h> 79 80 void *ReadInputData(const char *real_input_path, int *size) { 81 if (real_input_path == NULL) { 82 return NULL; 83 } 84 if (strstr(real_input_path, ".bin") || strstr(real_input_path, ".net")) { 85 FILE *file; 86 file = fopen(real_input_path, "rb"); 87 if (!file) { 88 printf("Can't find %s\n", real_input_path); 89 return NULL; 90 } 91 int curr_file_posi = ftell(file); 92 fseek(file, 0, SEEK_END); 93 *size = ftell(file); 94 unsigned char *buf = malloc((*size)); 95 (void)memset(buf, 0, (*size)); 96 fseek(file, curr_file_posi, SEEK_SET); 97 int read_size = (int)(fread(buf, 1, *size, file)); 98 if (read_size != (*size)) { 99 printf("read file failed, total file size: %d, read_size: %d\n", (*size), read_size); 100 fclose(file); 101 free(buf); 102 return NULL; 103 } 104 fclose(file); 105 return (void *)buf; 106 } else { 107 printf("input data file should be .bin , .net"); 108 return NULL; 109 } 110 } 111 112 void SaveOutputData(char *final_name, unsigned char *output_data, unsigned int out_size) { 113 FILE *output_file; 114 output_file = fopen(final_name, "w"); 115 if (output_file == NULL) { 116 printf("fopen output file: %s failed\n", final_name); 117 return; 118 } 119 unsigned char str[out_size]; 120 for (unsigned int i = 0; i < out_size; ++i) { 121 str[i] = output_data[i]; 122 fprintf(output_file, "%d\t", str[i]); 123 } 124 fclose(output_file); 125 } 126 127 int ReadInputsFile(char *path, void **buffers, const int *inputs_size, int inputs_num) { 128 char *inputs_path[inputs_num]; 129 char *delim = ","; 130 char *token; 131 int i = 0; 132 while ((token = strtok_r(path, delim, &path))) { 133 if (i >= inputs_num) { 134 printf("inputs num is error, need: %d\n", inputs_num); 135 return -1; 136 } 137 inputs_path[i] = token; 138 printf("input %d: %s\n", i, inputs_path[i]); 139 i++; 140 } 141 142 for (i = 0; i < inputs_num; ++i) { 143 int size = 0; 144 buffers[i] = ReadInputData(inputs_path[i], &size); 145 if (size != inputs_size[i] || buffers[i] == NULL) { 146 printf("size mismatch, %s, input: %d, needed: %d\n", inputs_path[i], size, inputs_size[i]); 147 return -1; 148 } 149 } 150 return 0; 151 } 152 153 )RAW"; 154 } // namespace mindspore::lite::micro 155