blob: 0b6ff71b414c8b68e3ca6edcc34410a0ebcd42cf [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 }
Tony Mak51a9e542018-11-02 13:36:22 +000058 std::vector<int> GetOutputPositions() {
59 return ExtractVector<int>(output_positions_);
60 }
Tony Mak6c4cc672018-09-17 11:48:50 +010061 std::vector<int> GetOutputAttributeInt32() {
62 return ExtractVector<int>(output_attributes_int32_);
63 }
64 std::vector<float> GetOutputAttributeFloat() {
65 return ExtractVector<float>(output_attributes_float_);
66 }
67 int GetEncodedLength() { return ExtractVector<int>(output_length_)[0]; }
68
69 private:
70 int input_string_;
71 int input_length_;
72 int input_output_maxlength_;
73 int input_attributes_int32_;
74 int input_attributes_float_;
75
76 int output_encoding_;
Tony Mak51a9e542018-11-02 13:36:22 +000077 int output_positions_;
Tony Mak6c4cc672018-09-17 11:48:50 +010078 int output_length_;
79 int output_attributes_int32_;
80 int output_attributes_float_;
81};
82
83TextEncoderOpModel::TextEncoderOpModel(
84 std::initializer_list<int> input_strings_shape,
85 std::initializer_list<int> attribute_shape) {
86 input_string_ = AddInput(tflite::TensorType_STRING);
87 input_length_ = AddInput(tflite::TensorType_INT32);
88 input_output_maxlength_ = AddInput(tflite::TensorType_INT32);
89 input_attributes_int32_ = AddInput(tflite::TensorType_INT32);
90 input_attributes_float_ = AddInput(tflite::TensorType_FLOAT32);
91
92 output_encoding_ = AddOutput(tflite::TensorType_INT32);
Tony Mak51a9e542018-11-02 13:36:22 +000093 output_positions_ = AddOutput(tflite::TensorType_INT32);
Tony Mak6c4cc672018-09-17 11:48:50 +010094 output_length_ = AddOutput(tflite::TensorType_INT32);
95 output_attributes_int32_ = AddOutput(tflite::TensorType_INT32);
96 output_attributes_float_ = AddOutput(tflite::TensorType_FLOAT32);
97
98 std::ifstream test_config_stream(GetTestConfigPath());
99 std::string config((std::istreambuf_iterator<char>(test_config_stream)),
100 (std::istreambuf_iterator<char>()));
101 flexbuffers::Builder builder;
102 builder.Map([&]() { builder.String("text_encoder_config", config); });
103 builder.Finish();
104 SetCustomOp("TextEncoder", builder.GetBuffer(),
105 tflite::ops::custom::Register_TEXT_ENCODER);
106 BuildInterpreter(
107 {input_strings_shape, {1}, {1}, attribute_shape, attribute_shape});
108}
109
110// Tests
111TEST(TextEncoderTest, SimpleEncoder) {
112 TextEncoderOpModel m({1, 1}, {1, 1});
113 m.SetInputText({"Hello"});
114 m.SetMaxOutputLength(10);
115 m.SetInt32Attribute({7});
116 m.SetFloatAttribute({3.f});
117 m.Invoke();
118 EXPECT_EQ(m.GetEncodedLength(), 5);
119 EXPECT_THAT(m.GetOutputEncoding(),
120 testing::ElementsAre(1, 90, 547, 58, 2, 2, 2, 2, 2, 2));
Tony Mak51a9e542018-11-02 13:36:22 +0000121 EXPECT_THAT(m.GetOutputPositions(),
122 testing::ElementsAre(0, 1, 2, 3, 4, 10, 10, 10, 10, 10));
Tony Mak6c4cc672018-09-17 11:48:50 +0100123 EXPECT_THAT(m.GetOutputAttributeInt32(),
124 testing::ElementsAre(7, 7, 7, 7, 7, 7, 7, 7, 7, 7));
125 EXPECT_THAT(
126 m.GetOutputAttributeFloat(),
127 testing::ElementsAre(3.f, 3.f, 3.f, 3.f, 3.f, 3.f, 3.f, 3.f, 3.f, 3.f));
128}
129
130TEST(TextEncoderTest, ManyStrings) {
131 TextEncoderOpModel m({1, 3}, {1, 3});
132 m.SetInt32Attribute({1, 2, 3});
133 m.SetFloatAttribute({5.f, 4.f, 3.f});
134 m.SetInputText({"Hello", "Hi", "Bye"});
135 m.SetMaxOutputLength(10);
136 m.Invoke();
137 EXPECT_EQ(m.GetEncodedLength(), 10);
138 EXPECT_THAT(m.GetOutputEncoding(),
139 testing::ElementsAre(547, 58, 2, 1, 862, 2, 1, 1919, 19, 2));
Tony Mak51a9e542018-11-02 13:36:22 +0000140 EXPECT_THAT(m.GetOutputPositions(),
141 testing::ElementsAre(2, 3, 4, 0, 1, 2, 0, 1, 2, 3));
Tony Mak6c4cc672018-09-17 11:48:50 +0100142 EXPECT_THAT(m.GetOutputAttributeInt32(),
143 testing::ElementsAre(1, 1, 1, 2, 2, 2, 3, 3, 3, 3));
144 EXPECT_THAT(
145 m.GetOutputAttributeFloat(),
146 testing::ElementsAre(5.f, 5.f, 5.f, 4.f, 4.f, 4.f, 3.f, 3.f, 3.f, 3.f));
147}
148
149TEST(TextEncoderTest, LongStrings) {
150 TextEncoderOpModel m({1, 4}, {1, 4});
151 m.SetInt32Attribute({1, 2, 3, 4});
152 m.SetFloatAttribute({5.f, 4.f, 3.f, 2.f});
153 m.SetInputText({"Hello", "Hi", "Bye", "Hi"});
154 m.SetMaxOutputLength(9);
155 m.Invoke();
156 EXPECT_EQ(m.GetEncodedLength(), 9);
157 EXPECT_THAT(m.GetOutputEncoding(),
158 testing::ElementsAre(862, 2, 1, 1919, 19, 2, 1, 862, 2));
Tony Mak51a9e542018-11-02 13:36:22 +0000159 EXPECT_THAT(m.GetOutputPositions(),
160 testing::ElementsAre(1, 2, 0, 1, 2, 3, 0, 1, 2));
Tony Mak6c4cc672018-09-17 11:48:50 +0100161 EXPECT_THAT(m.GetOutputAttributeInt32(),
162 testing::ElementsAre(2, 2, 3, 3, 3, 3, 4, 4, 4));
163 EXPECT_THAT(
164 m.GetOutputAttributeFloat(),
165 testing::ElementsAre(4.f, 4.f, 3.f, 3.f, 3.f, 3.f, 2.f, 2.f, 2.f));
166}
167
168} // namespace
169} // namespace libtextclassifier3