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

Function device_input_output

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

Source from the content-addressed store, hash-verified

64}
65
66bool device_input_output(const Args& args) {
67 std::string network_path = args.model_path;
68 std::string input_path = args.input_path;
69
70 //! config the network running in CUDA device
71 lite::Config config{LiteDeviceType::LITE_CUDA};
72
73 //! set NetworkIO include input and output
74 NetworkIO network_io;
75 std::string input_name = "data";
76 std::string output_name = "TRUE_DIV(EXP[12065],reduce0[12067])[12077]";
77 bool is_host = false;
78 IO device_input{input_name, is_host};
79 IO device_output{output_name, is_host};
80 network_io.inputs.push_back(device_input);
81 network_io.outputs.push_back(device_output);
82
83 //! create and load the network
84 std::shared_ptr<Network> network = std::make_shared<Network>(config, network_io);
85 network->load_model(network_path);
86
87 std::shared_ptr<Tensor> input_tensor_device = network->get_input_tensor(0);
88 Layout input_layout = input_tensor_device->get_layout();
89
90 //! read data from numpy data file
91 auto src_tensor = parse_npy(input_path);
92
93 //! malloc the device memory
94 auto tensor_device = Tensor(LiteDeviceType::LITE_CUDA, input_layout);
95
96 //! copy to the device memory
97 tensor_device.copy_from(*src_tensor);
98
99 //! Now the device memory is filled with user input data, set it to the
100 //! input tensor
101 input_tensor_device->reset(tensor_device.get_memory_ptr(), input_layout);
102
103 //! forward
104 network->forward();
105 network->wait();
106
107 //! output is in device, should copy it to host
108 std::shared_ptr<Tensor> output_tensor_device = network->get_io_tensor(output_name);
109
110 auto output_tensor = std::make_shared<Tensor>();
111 output_tensor->copy_from(*output_tensor_device);
112
113 //! get the output data or read tensor set in network_in
114 void* out_data = output_tensor->get_memory_ptr();
115 size_t out_length = output_tensor->get_tensor_total_size_in_byte() /
116 output_tensor->get_layout().get_elem_size();
117 float max = -1.0f;
118 float sum = 0.0f;
119 for (size_t i = 0; i < out_length; i++) {
120 float data = static_cast<float*>(out_data)[i];
121 sum += data;
122 if (max < data)
123 max = data;

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