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

Function basic_c_interface

lite/example/cpp_example/mge/lite_c_interface.cpp:18–74  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

16 } while (0)
17
18bool basic_c_interface(const lite::example::Args& args) {
19 std::string network_path = args.model_path;
20 std::string input_path = args.input_path;
21
22 //! read input data to lite::tensor
23 auto src_tensor = lite::example::parse_npy(input_path);
24 void* src_ptr = src_tensor->get_memory_ptr();
25
26 //! create and load the network
27 LiteNetwork c_network;
28 LITE_CAPI_CHECK(
29 LITE_make_network(&c_network, *default_config(), *default_network_io()));
30
31 LITE_CAPI_CHECK(LITE_load_model_from_path(c_network, network_path.c_str()));
32
33 //! set input data to input tensor
34 LiteTensor c_input_tensor;
35 LITE_CAPI_CHECK(LITE_get_io_tensor(c_network, "data", LITE_IO, &c_input_tensor));
36 void* dst_ptr;
37 size_t length_in_byte;
38 LITE_CAPI_CHECK(
39 LITE_get_tensor_total_size_in_byte(c_input_tensor, &length_in_byte));
40 LITE_CAPI_CHECK(LITE_get_tensor_memory(c_input_tensor, &dst_ptr));
41 //! copy or forward data to network
42 memcpy(dst_ptr, src_ptr, length_in_byte);
43
44 //! forward
45 LITE_CAPI_CHECK(LITE_forward(c_network));
46 LITE_CAPI_CHECK(LITE_wait(c_network));
47
48 //! get the output data or read tensor data
49 const char* output_name;
50 LiteTensor c_output_tensor;
51 //! get the first output tensor name
52 LITE_CAPI_CHECK(LITE_get_output_name(c_network, 0, &output_name));
53 LITE_CAPI_CHECK(
54 LITE_get_io_tensor(c_network, output_name, LITE_IO, &c_output_tensor));
55 void* output_ptr;
56 size_t length_output_in_byte;
57 LITE_CAPI_CHECK(LITE_get_tensor_memory(c_output_tensor, &output_ptr));
58 LITE_CAPI_CHECK(LITE_get_tensor_total_size_in_byte(
59 c_output_tensor, &length_output_in_byte));
60
61 size_t out_length = length_output_in_byte / sizeof(float);
62 printf("length=%zu\n", out_length);
63
64 float max = -1.0f;
65 float sum = 0.0f;
66 for (size_t i = 0; i < out_length; i++) {
67 float data = static_cast<float*>(output_ptr)[i];
68 sum += data;
69 if (max < data)
70 max = data;
71 }
72 printf("max=%e, sum=%e\n", max, sum);
73 return true;
74}
75

Callers

nothing calls this directly

Calls 11

LITE_make_networkFunction · 0.85
default_configFunction · 0.85
default_network_ioFunction · 0.85
LITE_get_io_tensorFunction · 0.85
LITE_get_tensor_memoryFunction · 0.85
LITE_forwardFunction · 0.85
LITE_waitFunction · 0.85
LITE_get_output_nameFunction · 0.85
get_memory_ptrMethod · 0.45

Tested by

no test coverage detected