| 244 | } |
| 245 | |
| 246 | bool async_forward(const Args& args) { |
| 247 | std::string network_path = args.model_path; |
| 248 | std::string input_path = args.input_path; |
| 249 | Config config; |
| 250 | config.options.var_sanity_check_first_run = false; |
| 251 | |
| 252 | //! create and load the network |
| 253 | std::shared_ptr<Network> network = std::make_shared<Network>(config); |
| 254 | |
| 255 | network->load_model(network_path); |
| 256 | |
| 257 | //! set input data to input tensor |
| 258 | std::shared_ptr<Tensor> input_tensor = network->get_input_tensor(0); |
| 259 | //! copy or forward data to network |
| 260 | size_t length = input_tensor->get_tensor_total_size_in_byte(); |
| 261 | void* dst_ptr = input_tensor->get_memory_ptr(); |
| 262 | auto src_tensor = parse_npy(input_path); |
| 263 | void* src = src_tensor->get_memory_ptr(); |
| 264 | memcpy(dst_ptr, src, length); |
| 265 | |
| 266 | //! set async mode and callback |
| 267 | volatile bool finished = false; |
| 268 | network->set_async_callback([&finished]() { |
| 269 | #if !__DEPLOY_ON_XP_SP2__ |
| 270 | std::cout << "worker thread_id:" << std::this_thread::get_id() << std::endl; |
| 271 | #endif |
| 272 | finished = true; |
| 273 | }); |
| 274 | |
| 275 | #if !__DEPLOY_ON_XP_SP2__ |
| 276 | std::cout << "out thread_id:" << std::this_thread::get_id() << std::endl; |
| 277 | #endif |
| 278 | |
| 279 | //! forward |
| 280 | network->forward(); |
| 281 | size_t count = 0; |
| 282 | while (finished == false) { |
| 283 | count++; |
| 284 | } |
| 285 | printf("Forward finish, count is %zu\n", count); |
| 286 | |
| 287 | //! get the output data or read tensor set in network_in |
| 288 | std::shared_ptr<Tensor> output_tensor = network->get_output_tensor(0); |
| 289 | void* out_data = output_tensor->get_memory_ptr(); |
| 290 | size_t out_length = output_tensor->get_tensor_total_size_in_byte() / |
| 291 | output_tensor->get_layout().get_elem_size(); |
| 292 | printf("length=%zu\n", length); |
| 293 | float max = -1.0f; |
| 294 | float sum = 0.0f; |
| 295 | for (size_t i = 0; i < out_length; i++) { |
| 296 | float data = static_cast<float*>(out_data)[i]; |
| 297 | sum += data; |
| 298 | if (max < data) |
| 299 | max = data; |
| 300 | } |
| 301 | printf("max=%e, sum=%e\n", max, sum); |
| 302 | return true; |
| 303 | } |
nothing calls this directly
no test coverage detected