• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2021 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 #include <limits>
16 
17 #include "tensorflow/lite/c/builtin_op_data.h"
18 #include "tensorflow/lite/c/common.h"
19 #include "tensorflow/lite/micro/all_ops_resolver.h"
20 #include "tensorflow/lite/micro/kernels/kernel_runner.h"
21 #include "tensorflow/lite/micro/test_helpers.h"
22 #include "tensorflow/lite/micro/testing/micro_test.h"
23 
24 namespace tflite {
25 namespace testing {
26 namespace {
27 
TestExp(const int * input_dims_data,const float * input_data,const float * expected_output_data,float * output_data)28 void TestExp(const int* input_dims_data, const float* input_data,
29              const float* expected_output_data, float* output_data) {
30   TfLiteIntArray* input_dims = IntArrayFromInts(input_dims_data);
31   TfLiteIntArray* output_dims = IntArrayFromInts(input_dims_data);
32   const int output_dims_count = ElementCount(*output_dims);
33   constexpr int inputs_size = 1;
34   constexpr int outputs_size = 1;
35   constexpr int tensors_size = inputs_size + outputs_size;
36   TfLiteTensor tensors[tensors_size] = {
37       CreateTensor(input_data, input_dims),
38       CreateTensor(output_data, output_dims),
39   };
40 
41   int inputs_array_data[] = {1, 0};
42   TfLiteIntArray* inputs_array = IntArrayFromInts(inputs_array_data);
43   int outputs_array_data[] = {1, 1};
44   TfLiteIntArray* outputs_array = IntArrayFromInts(outputs_array_data);
45 
46   const TfLiteRegistration registration = Register_EXP();
47   micro::KernelRunner runner(registration, tensors, tensors_size, inputs_array,
48                              outputs_array,
49                              /*builtin_data=*/nullptr);
50 
51   TF_LITE_MICRO_EXPECT_EQ(kTfLiteOk, runner.InitAndPrepare());
52   TF_LITE_MICRO_EXPECT_EQ(kTfLiteOk, runner.Invoke());
53   for (int i = 0; i < output_dims_count; ++i) {
54     TF_LITE_MICRO_EXPECT_NEAR(expected_output_data[i], output_data[i], 1e-5f);
55   }
56 }
57 }  // namespace
58 }  // namespace testing
59 }  // namespace tflite
60 
61 TF_LITE_MICRO_TESTS_BEGIN
62 
TF_LITE_MICRO_TEST(SingleDim)63 TF_LITE_MICRO_TEST(SingleDim) {
64   constexpr int kInputSize = 7;
65   float output_data[kInputSize];
66   const int input_dims[] = {2, 1, kInputSize};
67   const float input_values[kInputSize] = {0.0f,    1.0f,  -1.0f, 100.0f,
68                                           -100.0f, 0.01f, -0.01f};
69   float golden[kInputSize];
70   for (int i = 0; i < kInputSize; ++i) {
71     golden[i] = std::exp(input_values[i]);
72   }
73 
74   tflite::testing::TestExp(input_dims, input_values, golden, output_data);
75 }
76 
77 TF_LITE_MICRO_TESTS_END
78