1 /* Copyright 2017 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 // Unit test for TFLite LOG_SOFTMAX op.
16
17 #include <initializer_list>
18 #include <memory>
19 #include <vector>
20
21 #include <gmock/gmock.h>
22 #include <gtest/gtest.h>
23 #include "flatbuffers/flatbuffers.h" // from @flatbuffers
24 #include "tensorflow/lite/kernels/internal/reference/reference_ops.h"
25 #include "tensorflow/lite/kernels/internal/types.h"
26 #include "tensorflow/lite/kernels/test_util.h"
27 #include "tensorflow/lite/schema/schema_generated.h"
28
29 namespace tflite {
30 namespace {
31
32 class LogSoftmaxOpModel : public SingleOpModel {
33 public:
LogSoftmaxOpModel(int batches,int size)34 LogSoftmaxOpModel(int batches, int size)
35 : batches_(batches), input_size_(size) {
36 input_ = AddInput(TensorType_FLOAT32);
37 output_ = AddOutput(TensorType_FLOAT32);
38 SetBuiltinOp(BuiltinOperator_LOG_SOFTMAX, BuiltinOptions_LogSoftmaxOptions,
39 CreateLogSoftmaxOptions(builder_).Union());
40 BuildInterpreter({{batches_, input_size_}});
41 }
42
SetInput(std::initializer_list<float> data)43 void SetInput(std::initializer_list<float> data) {
44 PopulateTensor(input_, data);
45 }
46
SetInput(int offset,float * begin,float * end)47 void SetInput(int offset, float* begin, float* end) {
48 PopulateTensor(input_, offset, begin, end);
49 }
50
GetOutput()51 std::vector<float> GetOutput() { return ExtractVector<float>(output_); }
52
53 private:
54 int input_;
55 int output_;
56
57 int batches_;
58 int input_size_;
59 };
60
TEST(LogSoftmaxOpTest,SimpleTest)61 TEST(LogSoftmaxOpTest, SimpleTest) {
62 LogSoftmaxOpModel m(/*batches=*/2, /*size=*/5);
63 m.SetInput({
64 1.0, 2.0, 3.0, 4.0, 5.0, // b = 0
65 -1.0, -2.0, -3.0, -4.0, -5.0, // b = 1
66 });
67
68 m.Invoke();
69
70 EXPECT_THAT(
71 m.GetOutput(),
72 ElementsAreArray(ArrayFloatNear(
73 {-4.45191431, -3.45191431, -2.45191431, -1.45191443, -0.4519144,
74 -0.4519144, -1.45191443, -2.45191431, -3.45191431, -4.45191431},
75 1e-6)));
76 }
77
TEST(LogSoftmaxOpTest,CompareWithTFmini)78 TEST(LogSoftmaxOpTest, CompareWithTFmini) {
79 const int batch_size = 2;
80 const int input_size = 5;
81 static float input_buffer[] = {
82 1.0, 2.0, 3.0, 4.0, 5.0, // b = 0
83 -1.0, -2.0, -3.0, -4.0, -5.0, // b = 1
84 };
85
86 LogSoftmaxOpModel m(batch_size, input_size);
87
88 m.SetInput(0, input_buffer, input_buffer + input_size * batch_size);
89
90 m.Invoke();
91
92 std::unique_ptr<float[]> output_buffer(new float[input_size * batch_size]);
93 auto input_shape = RuntimeShape({batch_size, 1, 1, input_size});
94 SoftmaxParams params;
95 tflite::reference_ops::LogSoftmax(params, input_shape, input_buffer,
96 input_shape, output_buffer.get());
97
98 std::vector<float> expected;
99 expected.insert(expected.end(), output_buffer.get(),
100 output_buffer.get() + input_size * batch_size);
101
102 EXPECT_THAT(m.GetOutput(), ElementsAreArray(ArrayFloatNear(expected, 1e-6)));
103 }
104
105 } // namespace
106 } // namespace tflite
107