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

Function device_input

lite/example/cpp_example/mge/device_io.cpp:11–64  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

9
10namespace {
11bool device_input(const Args& args) {
12 std::string network_path = args.model_path;
13 std::string input_path = args.input_path;
14
15 //! config the network running in CUDA device
16 lite::Config config{LiteDeviceType::LITE_CUDA};
17
18 //! set NetworkIO
19 NetworkIO network_io;
20 std::string input_name = "data";
21 bool is_host = false;
22 IO device_input{input_name, is_host};
23 network_io.inputs.push_back(device_input);
24
25 //! create and load the network
26 std::shared_ptr<Network> network = std::make_shared<Network>(config, network_io);
27 network->load_model(network_path);
28
29 std::shared_ptr<Tensor> input_tensor = network->get_input_tensor(0);
30 Layout input_layout = input_tensor->get_layout();
31
32 //! read data from numpy data file
33 auto src_tensor = parse_npy(input_path);
34
35 //! malloc the device memory
36 auto tensor_device = Tensor(LiteDeviceType::LITE_CUDA, input_layout);
37
38 //! copy to the device memory
39 tensor_device.copy_from(*src_tensor);
40
41 //! Now the device memory if filled with user input data, set it to the
42 //! input tensor
43 input_tensor->reset(tensor_device.get_memory_ptr(), input_layout);
44
45 //! forward
46 network->forward();
47 network->wait();
48
49 //! get the output data or read tensor set in network_in
50 std::shared_ptr<Tensor> output_tensor = network->get_output_tensor(0);
51 void* out_data = output_tensor->get_memory_ptr();
52 size_t out_length = output_tensor->get_tensor_total_size_in_byte() /
53 output_tensor->get_layout().get_elem_size();
54 float max = -1.0f;
55 float sum = 0.0f;
56 for (size_t i = 0; i < out_length; i++) {
57 float data = static_cast<float*>(out_data)[i];
58 sum += data;
59 if (max < data)
60 max = data;
61 }
62 printf("max=%e, sum=%e\n", max, sum);
63 return true;
64}
65
66bool device_input_output(const Args& args) {
67 std::string network_path = args.model_path;

Callers

nothing calls this directly

Calls 13

get_elem_sizeMethod · 0.80
TensorClass · 0.50
push_backMethod · 0.45
load_modelMethod · 0.45
get_input_tensorMethod · 0.45
get_layoutMethod · 0.45
copy_fromMethod · 0.45
resetMethod · 0.45
get_memory_ptrMethod · 0.45
forwardMethod · 0.45
waitMethod · 0.45

Tested by

no test coverage detected