| 127 | } |
| 128 | |
| 129 | bool pinned_host_input(const Args& args) { |
| 130 | std::string network_path = args.model_path; |
| 131 | std::string input_path = args.input_path; |
| 132 | |
| 133 | //! config the network running in CUDA device |
| 134 | lite::Config config{LiteDeviceType::LITE_CUDA}; |
| 135 | |
| 136 | //! create and load the network |
| 137 | std::shared_ptr<Network> network = std::make_shared<Network>(config); |
| 138 | network->load_model(network_path); |
| 139 | |
| 140 | std::shared_ptr<Tensor> input_tensor = network->get_input_tensor(0); |
| 141 | Layout input_layout = input_tensor->get_layout(); |
| 142 | |
| 143 | //! read data from numpy data file |
| 144 | auto src_tensor = parse_npy(input_path); |
| 145 | //! malloc the pinned host memory |
| 146 | bool is_pinned_host = true; |
| 147 | auto tensor_pinned_input = |
| 148 | Tensor(LiteDeviceType::LITE_CUDA, input_layout, is_pinned_host); |
| 149 | //! copy to the pinned memory |
| 150 | tensor_pinned_input.copy_from(*src_tensor); |
| 151 | //! set the pinned host memory to the network as input |
| 152 | input_tensor->reset(tensor_pinned_input.get_memory_ptr(), input_layout); |
| 153 | |
| 154 | //! forward |
| 155 | network->forward(); |
| 156 | network->wait(); |
| 157 | |
| 158 | //! get the output data or read tensor set in network_in |
| 159 | std::shared_ptr<Tensor> output_tensor = network->get_output_tensor(0); |
| 160 | void* out_data = output_tensor->get_memory_ptr(); |
| 161 | size_t out_length = output_tensor->get_tensor_total_size_in_byte() / |
| 162 | output_tensor->get_layout().get_elem_size(); |
| 163 | float max = -1.0f; |
| 164 | float sum = 0.0f; |
| 165 | for (size_t i = 0; i < out_length; i++) { |
| 166 | float data = static_cast<float*>(out_data)[i]; |
| 167 | sum += data; |
| 168 | if (max < data) |
| 169 | max = data; |
| 170 | } |
| 171 | printf("max=%e, sum=%e\n", max, sum); |
| 172 | return true; |
| 173 | } |
| 174 | } // namespace |
| 175 | |
| 176 | REGIST_EXAMPLE("device_input", device_input); |
nothing calls this directly
no test coverage detected