| 9 | |
| 10 | namespace { |
| 11 | bool 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 | |
| 66 | bool device_input_output(const Args& args) { |
| 67 | std::string network_path = args.model_path; |
nothing calls this directly
no test coverage detected