| 26 | } while (0) |
| 27 | |
| 28 | int basic_c_interface(const char* mode_path) { |
| 29 | //! create and load the network |
| 30 | LiteNetwork c_network; |
| 31 | LITE_CAPI_CHECK( |
| 32 | LITE_make_network(&c_network, *default_config(), *default_network_io())); |
| 33 | |
| 34 | LITE_CAPI_CHECK(LITE_load_model_from_path(c_network, mode_path)); |
| 35 | |
| 36 | //! set input data to input tensor |
| 37 | LiteTensor c_input_tensor; |
| 38 | LITE_CAPI_CHECK(LITE_get_io_tensor(c_network, "data", LITE_IO, &c_input_tensor)); |
| 39 | void* dst_ptr; |
| 40 | size_t length_in_byte; |
| 41 | LITE_CAPI_CHECK( |
| 42 | LITE_get_tensor_total_size_in_byte(c_input_tensor, &length_in_byte)); |
| 43 | LITE_CAPI_CHECK(LITE_get_tensor_memory(c_input_tensor, &dst_ptr)); |
| 44 | //! copy or forward data to network |
| 45 | memset(dst_ptr, 5, length_in_byte); |
| 46 | |
| 47 | //! forward |
| 48 | LITE_CAPI_CHECK(LITE_forward(c_network)); |
| 49 | LITE_CAPI_CHECK(LITE_wait(c_network)); |
| 50 | |
| 51 | //! get the output data or read tensor data |
| 52 | const char* output_name; |
| 53 | LiteTensor c_output_tensor; |
| 54 | //! get the first output tensor name |
| 55 | LITE_CAPI_CHECK(LITE_get_output_name(c_network, 0, &output_name)); |
| 56 | LITE_CAPI_CHECK( |
| 57 | LITE_get_io_tensor(c_network, output_name, LITE_IO, &c_output_tensor)); |
| 58 | void* output_ptr; |
| 59 | size_t length_output_in_byte; |
| 60 | LITE_CAPI_CHECK(LITE_get_tensor_memory(c_output_tensor, &output_ptr)); |
| 61 | LITE_CAPI_CHECK(LITE_get_tensor_total_size_in_byte( |
| 62 | c_output_tensor, &length_output_in_byte)); |
| 63 | |
| 64 | size_t out_length = length_output_in_byte / sizeof(float); |
| 65 | printf("length=%zu\n", out_length); |
| 66 | |
| 67 | float max = -1.0f; |
| 68 | float sum = 0.0f; |
| 69 | for (size_t i = 0; i < out_length; i++) { |
| 70 | float data = ((float*)(output_ptr))[i]; |
| 71 | sum += data; |
| 72 | if (max < data) |
| 73 | max = data; |
| 74 | } |
| 75 | printf("max=%e, sum=%e\n", max, sum); |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | int main(int argc, char** argv) { |
| 80 | if (argc < 2) { |
no test coverage detected