Skip to content

Commit 39add97

Browse files
Add a test to verify that the TF Lite C API headers can build, link,
and run successfully when compiled as C code. PiperOrigin-RevId: 321428801 Change-Id: I0cc6fc82006e27a22c73450b9a0cb86d5c50a3f6
1 parent 3111937 commit 39add97

3 files changed

Lines changed: 159 additions & 0 deletions

File tree

‎tensorflow/lite/c/BUILD‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,18 @@ cc_test(
158158
"@com_google_googletest//:gtest",
159159
],
160160
)
161+
162+
cc_test(
163+
name = "c_test",
164+
size = "small",
165+
srcs = ["c_test.c"],
166+
copts = tflite_copts(),
167+
data = [
168+
"//tensorflow/lite:testdata/add.bin",
169+
],
170+
deps = [
171+
":c_api",
172+
":c_api_experimental",
173+
":common",
174+
],
175+
)

‎tensorflow/lite/c/c_test.c‎

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
==============================================================================*/
15+
16+
#include "tensorflow/lite/c/c_api.h"
17+
#include "tensorflow/lite/c/c_api_experimental.h"
18+
#include "tensorflow/lite/c/common.h"
19+
#include "tensorflow/lite/c/builtin_op_data.h"
20+
21+
// This file exists just to verify that the above header files above can build,
22+
// link, and run as "C" code.
23+
24+
#ifdef __cplusplus
25+
#error "This file should be compiled as C code, not as C++."
26+
#endif
27+
28+
#include <stdio.h>
29+
#include <stdlib.h>
30+
#include <string.h>
31+
32+
static void CheckFailed(const char *expression, const char *filename,
33+
int line_number) {
34+
fprintf(stderr, "ERROR: CHECK failed: %s:%d: %s\n", filename, line_number,
35+
expression);
36+
fflush(stderr);
37+
abort();
38+
}
39+
40+
// We use an extra level of macro indirection here to ensure that the
41+
// macro arguments get evaluated, so that in a call to CHECK(foo),
42+
// the call to STRINGIZE(condition) in the definition of the CHECK
43+
// macro results in the string "foo" rather than the string "condition".
44+
#define STRINGIZE(expression) STRINGIZE2(expression)
45+
#define STRINGIZE2(expression) #expression
46+
47+
// Like assert(), but not dependent on NDEBUG.
48+
#define CHECK(condition) \
49+
((condition) ? (void)0 \
50+
: CheckFailed(STRINGIZE(condition), __FILE__, __LINE__))
51+
#define ASSERT_EQ(expected, actual) CHECK((expected) == (actual))
52+
#define ASSERT_NE(expected, actual) CHECK((expected) != (actual))
53+
#define ASSERT_STREQ(expected, actual) \
54+
ASSERT_EQ(0, strcmp((expected), (actual)))
55+
56+
// Test the TfLiteVersion function.
57+
static void TestVersion(void) {
58+
const char *version = TfLiteVersion();
59+
printf("Version = %s\n", version);
60+
CHECK(version[0] != '\0');
61+
}
62+
63+
static void TestSmokeTest(void) {
64+
TfLiteModel* model =
65+
TfLiteModelCreateFromFile("third_party/tensorflow/lite/testdata/add.bin");
66+
ASSERT_NE(model, NULL);
67+
68+
TfLiteInterpreterOptions* options = TfLiteInterpreterOptionsCreate();
69+
ASSERT_NE(options, NULL);
70+
TfLiteInterpreterOptionsSetNumThreads(options, 2);
71+
72+
TfLiteInterpreter* interpreter = TfLiteInterpreterCreate(model, options);
73+
ASSERT_NE(interpreter, NULL);
74+
75+
// The options/model can be deleted immediately after interpreter creation.
76+
TfLiteInterpreterOptionsDelete(options);
77+
TfLiteModelDelete(model);
78+
79+
ASSERT_EQ(TfLiteInterpreterAllocateTensors(interpreter), kTfLiteOk);
80+
ASSERT_EQ(TfLiteInterpreterGetInputTensorCount(interpreter), 1);
81+
ASSERT_EQ(TfLiteInterpreterGetOutputTensorCount(interpreter), 1);
82+
83+
int input_dims[1] = {2};
84+
ASSERT_EQ(TfLiteInterpreterResizeInputTensor(
85+
interpreter, 0, input_dims, 1),
86+
kTfLiteOk);
87+
ASSERT_EQ(TfLiteInterpreterAllocateTensors(interpreter), kTfLiteOk);
88+
89+
TfLiteTensor* input_tensor = TfLiteInterpreterGetInputTensor(interpreter, 0);
90+
ASSERT_NE(input_tensor, NULL);
91+
ASSERT_EQ(TfLiteTensorType(input_tensor), kTfLiteFloat32);
92+
ASSERT_EQ(TfLiteTensorNumDims(input_tensor), 1);
93+
ASSERT_EQ(TfLiteTensorDim(input_tensor, 0), 2);
94+
ASSERT_EQ(TfLiteTensorByteSize(input_tensor), sizeof(float) * 2);
95+
ASSERT_NE(TfLiteTensorData(input_tensor), NULL);
96+
ASSERT_STREQ(TfLiteTensorName(input_tensor), "input");
97+
98+
TfLiteQuantizationParams input_params =
99+
TfLiteTensorQuantizationParams(input_tensor);
100+
ASSERT_EQ(input_params.scale, 0.f);
101+
ASSERT_EQ(input_params.zero_point, 0);
102+
103+
float input[2] = {1.f, 3.f};
104+
ASSERT_EQ(TfLiteTensorCopyFromBuffer(input_tensor, input,
105+
2 * sizeof(float)),
106+
kTfLiteOk);
107+
108+
ASSERT_EQ(TfLiteInterpreterInvoke(interpreter), kTfLiteOk);
109+
110+
const TfLiteTensor* output_tensor =
111+
TfLiteInterpreterGetOutputTensor(interpreter, 0);
112+
ASSERT_NE(output_tensor, NULL);
113+
ASSERT_EQ(TfLiteTensorType(output_tensor), kTfLiteFloat32);
114+
ASSERT_EQ(TfLiteTensorNumDims(output_tensor), 1);
115+
ASSERT_EQ(TfLiteTensorDim(output_tensor, 0), 2);
116+
ASSERT_EQ(TfLiteTensorByteSize(output_tensor), sizeof(float) * 2);
117+
ASSERT_NE(TfLiteTensorData(output_tensor), NULL);
118+
ASSERT_STREQ(TfLiteTensorName(output_tensor), "output");
119+
120+
TfLiteQuantizationParams output_params =
121+
TfLiteTensorQuantizationParams(output_tensor);
122+
ASSERT_EQ(output_params.scale, 0.f);
123+
ASSERT_EQ(output_params.zero_point, 0);
124+
125+
float output[2];
126+
ASSERT_EQ(TfLiteTensorCopyToBuffer(output_tensor, output,
127+
2 * sizeof(float)),
128+
kTfLiteOk);
129+
ASSERT_EQ(output[0], 3.f);
130+
ASSERT_EQ(output[1], 9.f);
131+
132+
TfLiteInterpreterDelete(interpreter);
133+
}
134+
135+
static void RunTests(void) {
136+
TestVersion();
137+
TestSmokeTest();
138+
}
139+
140+
int main(void) {
141+
RunTests();
142+
return 0;
143+
}

‎tensorflow/lite/tools/make/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ endif
148148
CORE_CC_ALL_SRCS := $(sort $(CORE_CC_ALL_SRCS))
149149
CORE_CC_EXCLUDE_SRCS := \
150150
$(wildcard tensorflow/lite/*test.cc) \
151+
$(wildcard tensorflow/lite/*/*test.c) \
151152
$(wildcard tensorflow/lite/*/*test.cc) \
152153
$(wildcard tensorflow/lite/*/*/benchmark.cc) \
153154
$(wildcard tensorflow/lite/*/*/example*.cc) \

0 commit comments

Comments
 (0)