| 188 | } |
| 189 | |
| 190 | bool basic_load_from_memory(const Args& args) { |
| 191 | std::string network_path = args.model_path; |
| 192 | std::string input_path = args.input_path; |
| 193 | |
| 194 | //! create and load the network |
| 195 | std::shared_ptr<Network> network = std::make_shared<Network>(); |
| 196 | |
| 197 | FILE* fin = fopen(network_path.c_str(), "rb"); |
| 198 | if (!fin) { |
| 199 | printf("failed to open %s.", network_path.c_str()); |
| 200 | } |
| 201 | |
| 202 | fseek(fin, 0, SEEK_END); |
| 203 | size_t size = ftell(fin); |
| 204 | fseek(fin, 0, SEEK_SET); |
| 205 | void* ptr = malloc(size); |
| 206 | std::shared_ptr<void> buf{ptr, ::free}; |
| 207 | auto len = fread(buf.get(), 1, size, fin); |
| 208 | if (len < 1) { |
| 209 | printf("read file failed.\n"); |
| 210 | } |
| 211 | fclose(fin); |
| 212 | |
| 213 | network->load_model(buf.get(), size); |
| 214 | |
| 215 | //! set input data to input tensor |
| 216 | std::shared_ptr<Tensor> input_tensor = network->get_input_tensor(0); |
| 217 | //! copy or forward data to network |
| 218 | size_t length = input_tensor->get_tensor_total_size_in_byte(); |
| 219 | void* dst_ptr = input_tensor->get_memory_ptr(); |
| 220 | auto src_tensor = parse_npy(input_path); |
| 221 | void* src = src_tensor->get_memory_ptr(); |
| 222 | memcpy(dst_ptr, src, length); |
| 223 | |
| 224 | //! forward |
| 225 | network->forward(); |
| 226 | network->wait(); |
| 227 | |
| 228 | //! get the output data or read tensor set in network_in |
| 229 | std::shared_ptr<Tensor> output_tensor = network->get_output_tensor(0); |
| 230 | void* out_data = output_tensor->get_memory_ptr(); |
| 231 | size_t out_length = output_tensor->get_tensor_total_size_in_byte() / |
| 232 | output_tensor->get_layout().get_elem_size(); |
| 233 | printf("length=%zu\n", length); |
| 234 | float max = -1.0f; |
| 235 | float sum = 0.0f; |
| 236 | for (size_t i = 0; i < out_length; i++) { |
| 237 | float data = static_cast<float*>(out_data)[i]; |
| 238 | sum += data; |
| 239 | if (max < data) |
| 240 | max = data; |
| 241 | } |
| 242 | printf("max=%e, sum=%e\n", max, sum); |
| 243 | return true; |
| 244 | } |
| 245 | |
| 246 | bool async_forward(const Args& args) { |
| 247 | std::string network_path = args.model_path; |
nothing calls this directly
no test coverage detected