blob: d1892c78581a51c898c74bc1be3631f15f7f862f [file] [log] [blame]
Tony Mak6c4cc672018-09-17 11:48:50 +01001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <fstream>
18#include <string>
19#include <vector>
20
21#include "utils/tflite/text_encoder.h"
22#include "gtest/gtest.h"
23#include "flatbuffers/flexbuffers.h"
24#include "tensorflow/contrib/lite/interpreter.h"
25#include "tensorflow/contrib/lite/kernels/register.h"
26#include "tensorflow/contrib/lite/kernels/test_util.h"
27#include "tensorflow/contrib/lite/model.h"
28#include "tensorflow/contrib/lite/string_util.h"
29
30namespace libtextclassifier3 {
31namespace {
32
33std::string GetTestConfigPath() {
34 return "";
35}
36
37class TextEncoderOpModel : public tflite::SingleOpModel {
38 public:
39 TextEncoderOpModel(std::initializer_list<int> input_strings_shape,
40 std::initializer_list<int> attribute_shape);
41 void SetInputText(const std::initializer_list<string>& strings) {
42 PopulateStringTensor(input_string_, strings);
43 PopulateTensor(input_length_, {static_cast<int32_t>(strings.size())});
44 }
45 void SetMaxOutputLength(int length) {
46 PopulateTensor(input_output_maxlength_, {length});
47 }
48 void SetInt32Attribute(const std::initializer_list<int>& attribute) {
49 PopulateTensor(input_attributes_int32_, attribute);
50 }
51 void SetFloatAttribute(const std::initializer_list<float>& attribute) {
52 PopulateTensor(input_attributes_float_, attribute);
53 }
54
55 std::vector<int> GetOutputEncoding() {
56 return ExtractVector<int>(output_encoding_);
57 }
58 std::vector<int> GetOutputAttributeInt32() {
59 return ExtractVector<int>(output_attributes_int32_);
60 }
61 std::vector<float> GetOutputAttributeFloat() {
62 return ExtractVector<float>(output_attributes_float_);
63 }
64 int GetEncodedLength() { return ExtractVector<int>(output_length_)[0]; }
65
66 private:
67 int input_string_;
68 int input_length_;
69 int input_output_maxlength_;
70 int input_attributes_int32_;
71 int input_attributes_float_;
72
73 int output_encoding_;
74 int output_length_;
75 int output_attributes_int32_;
76 int output_attributes_float_;
77};
78
79TextEncoderOpModel::TextEncoderOpModel(
80 std::initializer_list<int> input_strings_shape,
81 std::initializer_list<int> attribute_shape) {
82 input_string_ = AddInput(tflite::TensorType_STRING);
83 input_length_ = AddInput(tflite::TensorType_INT32);
84 input_output_maxlength_ = AddInput(tflite::TensorType_INT32);
85 input_attributes_int32_ = AddInput(tflite::TensorType_INT32);
86 input_attributes_float_ = AddInput(tflite::TensorType_FLOAT32);
87
88 output_encoding_ = AddOutput(tflite::TensorType_INT32);
89 output_length_ = AddOutput(tflite::TensorType_INT32);
90 output_attributes_int32_ = AddOutput(tflite::TensorType_INT32);
91 output_attributes_float_ = AddOutput(tflite::TensorType_FLOAT32);
92
93 std::ifstream test_config_stream(GetTestConfigPath());
94 std::string config((std::istreambuf_iterator<char>(test_config_stream)),
95 (std::istreambuf_iterator<char>()));
96 flexbuffers::Builder builder;
97 builder.Map([&]() { builder.String("text_encoder_config", config); });
98 builder.Finish();
99 SetCustomOp("TextEncoder", builder.GetBuffer(),
100 tflite::ops::custom::Register_TEXT_ENCODER);
101 BuildInterpreter(
102 {input_strings_shape, {1}, {1}, attribute_shape, attribute_shape});
103}
104
105// Tests
106TEST(TextEncoderTest, SimpleEncoder) {
107 TextEncoderOpModel m({1, 1}, {1, 1});
108 m.SetInputText({"Hello"});
109 m.SetMaxOutputLength(10);
110 m.SetInt32Attribute({7});
111 m.SetFloatAttribute({3.f});
112 m.Invoke();
113 EXPECT_EQ(m.GetEncodedLength(), 5);
114 EXPECT_THAT(m.GetOutputEncoding(),
115 testing::ElementsAre(1, 90, 547, 58, 2, 2, 2, 2, 2, 2));
116 EXPECT_THAT(m.GetOutputAttributeInt32(),
117 testing::ElementsAre(7, 7, 7, 7, 7, 7, 7, 7, 7, 7));
118 EXPECT_THAT(
119 m.GetOutputAttributeFloat(),
120 testing::ElementsAre(3.f, 3.f, 3.f, 3.f, 3.f, 3.f, 3.f, 3.f, 3.f, 3.f));
121}
122
123TEST(TextEncoderTest, ManyStrings) {
124 TextEncoderOpModel m({1, 3}, {1, 3});
125 m.SetInt32Attribute({1, 2, 3});
126 m.SetFloatAttribute({5.f, 4.f, 3.f});
127 m.SetInputText({"Hello", "Hi", "Bye"});
128 m.SetMaxOutputLength(10);
129 m.Invoke();
130 EXPECT_EQ(m.GetEncodedLength(), 10);
131 EXPECT_THAT(m.GetOutputEncoding(),
132 testing::ElementsAre(547, 58, 2, 1, 862, 2, 1, 1919, 19, 2));
133 EXPECT_THAT(m.GetOutputAttributeInt32(),
134 testing::ElementsAre(1, 1, 1, 2, 2, 2, 3, 3, 3, 3));
135 EXPECT_THAT(
136 m.GetOutputAttributeFloat(),
137 testing::ElementsAre(5.f, 5.f, 5.f, 4.f, 4.f, 4.f, 3.f, 3.f, 3.f, 3.f));
138}
139
140TEST(TextEncoderTest, LongStrings) {
141 TextEncoderOpModel m({1, 4}, {1, 4});
142 m.SetInt32Attribute({1, 2, 3, 4});
143 m.SetFloatAttribute({5.f, 4.f, 3.f, 2.f});
144 m.SetInputText({"Hello", "Hi", "Bye", "Hi"});
145 m.SetMaxOutputLength(9);
146 m.Invoke();
147 EXPECT_EQ(m.GetEncodedLength(), 9);
148 EXPECT_THAT(m.GetOutputEncoding(),
149 testing::ElementsAre(862, 2, 1, 1919, 19, 2, 1, 862, 2));
150 EXPECT_THAT(m.GetOutputAttributeInt32(),
151 testing::ElementsAre(2, 2, 3, 3, 3, 3, 4, 4, 4));
152 EXPECT_THAT(
153 m.GetOutputAttributeFloat(),
154 testing::ElementsAre(4.f, 4.f, 3.f, 3.f, 3.f, 3.f, 2.f, 2.f, 2.f));
155}
156
157} // namespace
158} // namespace libtextclassifier3