MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / set_output_callback

Function set_output_callback

lite/example/cpp_example/mge/basic.cpp:372–438  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

370}
371
372bool set_output_callback(const Args& args) {
373 std::string network_path = args.model_path;
374 std::string input_path = args.input_path;
375 Config config;
376 config.options.var_sanity_check_first_run = false;
377
378 //! create and load the network
379 std::shared_ptr<Network> network = std::make_shared<Network>(config);
380
381 network->load_model(network_path);
382
383 //! set input data to input tensor
384 std::shared_ptr<Tensor> input_tensor = network->get_output_tensor(0);
385 //! copy or forward data to network
386 size_t length = input_tensor->get_tensor_total_size_in_byte();
387 void* dst_ptr = input_tensor->get_memory_ptr();
388 auto src_tensor = parse_npy(input_path);
389 void* src = src_tensor->get_memory_ptr();
390 memcpy(dst_ptr, src, length);
391
392 //! set output callback
393 volatile bool finished = false;
394 network->set_finish_callback(
395 [&finished](const std::unordered_map<
396 std::string, std::pair<IO, std::shared_ptr<Tensor>>>& outputs) {
397#if !__DEPLOY_ON_XP_SP2__
398 std::cout << "worker thread_id:" << std::this_thread::get_id()
399 << std::endl;
400#endif
401 for (auto&& item : outputs) {
402 std::cout << "output name: " << item.first
403 << "output dim: " << item.second.second->get_layout().ndim
404 << std::endl;
405 }
406 finished = true;
407 });
408
409#if !__DEPLOY_ON_XP_SP2__
410 std::cout << "out thread_id:" << std::this_thread::get_id() << std::endl;
411#endif
412
413 //! forward
414 network->forward();
415 network->wait();
416 size_t count = 0;
417 while (finished == false) {
418 count++;
419 }
420 printf("Forward finish, count is %zu\n", count);
421
422 //! get the output data or read tensor set in network_in
423 std::shared_ptr<Tensor> output_tensor = network->get_output_tensor(0);
424 void* out_data = output_tensor->get_memory_ptr();
425 size_t out_length = output_tensor->get_tensor_total_size_in_byte() /
426 output_tensor->get_layout().get_elem_size();
427 printf("length=%zu\n", length);
428 float max = -1.0f;
429 float sum = 0.0f;

Callers

nothing calls this directly

Calls 9

get_elem_sizeMethod · 0.80
load_modelMethod · 0.45
get_output_tensorMethod · 0.45
get_memory_ptrMethod · 0.45
set_finish_callbackMethod · 0.45
get_layoutMethod · 0.45
forwardMethod · 0.45
waitMethod · 0.45

Tested by

no test coverage detected