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

Function set_input_callback

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

Source from the content-addressed store, hash-verified

303}
304
305bool set_input_callback(const Args& args) {
306 std::string network_path = args.model_path;
307 std::string input_path = args.input_path;
308 Config config;
309 config.options.var_sanity_check_first_run = false;
310
311 //! create and load the network
312 std::shared_ptr<Network> network = std::make_shared<Network>(config);
313
314 network->load_model(network_path);
315
316 //! set input data to input tensor
317 std::shared_ptr<Tensor> input_tensor = network->get_input_tensor(0);
318 //! copy or forward data to network
319 size_t length = input_tensor->get_tensor_total_size_in_byte();
320 void* dst_ptr = input_tensor->get_memory_ptr();
321 auto src_tensor = parse_npy(input_path);
322 void* src = src_tensor->get_memory_ptr();
323 memcpy(dst_ptr, src, length);
324
325 //! set input callback
326 volatile bool finished = false;
327 network->set_start_callback(
328 [&finished](const std::unordered_map<
329 std::string, std::pair<IO, std::shared_ptr<Tensor>>>& inputs) {
330#if !__DEPLOY_ON_XP_SP2__
331 std::cout << "worker thread_id:" << std::this_thread::get_id()
332 << std::endl;
333#endif
334 for (auto&& item : inputs) {
335 std::cout << "input name: " << item.first
336 << "input dim: " << item.second.second->get_layout().ndim
337 << std::endl;
338 }
339 finished = true;
340 });
341
342#if !__DEPLOY_ON_XP_SP2__
343 std::cout << "out thread_id:" << std::this_thread::get_id() << std::endl;
344#endif
345
346 //! forward
347 network->forward();
348 size_t count = 0;
349 while (finished == false) {
350 count++;
351 }
352 printf("Forward finish, count is %zu\n", count);
353
354 //! get the output data or read tensor set in network_in
355 std::shared_ptr<Tensor> output_tensor = network->get_output_tensor(0);
356 void* out_data = output_tensor->get_memory_ptr();
357 size_t out_length = output_tensor->get_tensor_total_size_in_byte() /
358 output_tensor->get_layout().get_elem_size();
359 printf("length=%zu\n", length);
360 float max = -1.0f;
361 float sum = 0.0f;
362 for (size_t i = 0; i < out_length; i++) {

Callers

nothing calls this directly

Calls 9

get_elem_sizeMethod · 0.80
load_modelMethod · 0.45
get_input_tensorMethod · 0.45
get_memory_ptrMethod · 0.45
set_start_callbackMethod · 0.45
get_layoutMethod · 0.45
forwardMethod · 0.45
get_output_tensorMethod · 0.45

Tested by

no test coverage detected