| 144 | } // namespace |
| 145 | |
| 146 | bool async_c_interface(const lite::example::Args& args) { |
| 147 | std::string network_path = args.model_path; |
| 148 | std::string input_path = args.input_path; |
| 149 | |
| 150 | //! read input data to lite::tensor |
| 151 | auto src_tensor = lite::example::parse_npy(input_path); |
| 152 | void* src_ptr = src_tensor->get_memory_ptr(); |
| 153 | |
| 154 | LiteNetwork c_network; |
| 155 | LiteConfig config = *default_config(); |
| 156 | config.options.var_sanity_check_first_run = false; |
| 157 | LITE_CAPI_CHECK(LITE_make_network(&c_network, config, *default_network_io())); |
| 158 | LITE_CAPI_CHECK(LITE_load_model_from_path(c_network, network_path.c_str())); |
| 159 | |
| 160 | //! set input data to input tensor |
| 161 | LiteTensor c_input_tensor; |
| 162 | size_t length_tensor_in; |
| 163 | LITE_CAPI_CHECK(LITE_get_io_tensor(c_network, "data", LITE_IO, &c_input_tensor)); |
| 164 | LITE_CAPI_CHECK( |
| 165 | LITE_get_tensor_total_size_in_byte(c_input_tensor, &length_tensor_in)); |
| 166 | LITE_CAPI_CHECK( |
| 167 | LITE_reset_tensor_memory(c_input_tensor, src_ptr, length_tensor_in)); |
| 168 | |
| 169 | #if !__DEPLOY_ON_XP_SP2__ |
| 170 | std::cout << "user thread_id:" << std::this_thread::get_id() << std::endl; |
| 171 | #endif |
| 172 | |
| 173 | LITE_CAPI_CHECK(LITE_set_async_callback(c_network, async_callback)); |
| 174 | //! forward |
| 175 | LITE_CAPI_CHECK(LITE_forward(c_network)); |
| 176 | size_t count = 0; |
| 177 | while (finished == false) { |
| 178 | count++; |
| 179 | } |
| 180 | printf("The count is %zu\n", count); |
| 181 | finished = false; |
| 182 | |
| 183 | //! get the output data or read tensor data |
| 184 | const char* output_name; |
| 185 | LiteTensor c_output_tensor; |
| 186 | //! get the first output tensor name |
| 187 | LITE_CAPI_CHECK(LITE_get_output_name(c_network, 0, &output_name)); |
| 188 | LITE_CAPI_CHECK( |
| 189 | LITE_get_io_tensor(c_network, output_name, LITE_IO, &c_output_tensor)); |
| 190 | void* output_ptr; |
| 191 | size_t length_output_in_byte; |
| 192 | LITE_CAPI_CHECK(LITE_get_tensor_memory(c_output_tensor, &output_ptr)); |
| 193 | LITE_CAPI_CHECK(LITE_get_tensor_total_size_in_byte( |
| 194 | c_output_tensor, &length_output_in_byte)); |
| 195 | |
| 196 | size_t out_length = length_output_in_byte / sizeof(float); |
| 197 | printf("length=%zu\n", out_length); |
| 198 | |
| 199 | float max = -1.0f; |
| 200 | float sum = 0.0f; |
| 201 | for (size_t i = 0; i < out_length; i++) { |
| 202 | float data = static_cast<float*>(output_ptr)[i]; |
| 203 | sum += data; |
nothing calls this directly
no test coverage detected