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_support/custom_ops/kernel/sentencepiece/optimized_decoder.h"
17
18 #include <fstream>
19
20 #include <gmock/gmock.h>
21 #include <gtest/gtest.h>
22 #include "src/sentencepiece.pb.h"
23 #include "src/sentencepiece_processor.h"
24 #include "tensorflow/core/platform/env.h"
25 #include "tensorflow_lite_support/custom_ops/kernel/sentencepiece/model_converter.h"
26
27 namespace tflite {
28 namespace ops {
29 namespace custom {
30 namespace sentencepiece {
31
32 namespace internal {
33
TFReadFileToString(const std::string & filepath,std::string * data)34 tensorflow::Status TFReadFileToString(const std::string& filepath,
35 std::string* data) {
36 return tensorflow::ReadFileToString(tensorflow::Env::Default(),
37 /*test_path*/ filepath, data);
38 }
39
StdReadFileToString(const std::string & filepath,std::string * data)40 absl::Status StdReadFileToString(const std::string& filepath,
41 std::string* data) {
42 std::ifstream infile(filepath);
43 if (!infile.is_open()) {
44 return absl::NotFoundError(
45 absl::StrFormat("Error when opening %s", filepath));
46 }
47 std::string contents((std::istreambuf_iterator<char>(infile)),
48 (std::istreambuf_iterator<char>()));
49 data->append(contents);
50 infile.close();
51 return absl::OkStatus();
52 }
53
54 } // namespace internal
55
56 namespace {
57 static char kConfigFilePath[] =
58 "tensorflow_lite_support/custom_ops/kernel/"
59 "sentencepiece/testdata/sentencepiece.model";
60
TEST(OptimizedEncoder,ConfigConverter)61 TEST(OptimizedEncoder, ConfigConverter) {
62 std::string config;
63 auto status = internal::StdReadFileToString(kConfigFilePath, &config);
64
65 ASSERT_TRUE(status.ok());
66
67 ::sentencepiece::SentencePieceProcessor processor;
68 ASSERT_OK(processor.LoadFromSerializedProto(config));
69 const auto converted_model = ConvertSentencepieceModelForDecoder(config);
70 const std::string test_string("Hello world!\\xF0\\x9F\\x8D\\x95");
71 ::sentencepiece::SentencePieceText reference_encoded;
72 CHECK_OK(processor.Encode(test_string, &reference_encoded));
73
74 std::vector<int> encoded_vector;
75 encoded_vector.reserve(reference_encoded.pieces_size());
76 for (const auto& piece : reference_encoded.pieces()) {
77 encoded_vector.push_back(piece.id());
78 }
79 std::string ref_decoded;
80 ASSERT_OK(processor.Decode(encoded_vector, &ref_decoded));
81 const auto decoded = DecodeString(encoded_vector, converted_model.data());
82 ASSERT_EQ(decoded.type, DecoderResultType::SUCCESS);
83 ASSERT_EQ(ref_decoded, decoded.decoded);
84 }
85 } // namespace
86
87 } // namespace sentencepiece
88 } // namespace custom
89 } // namespace ops
90 } // namespace tflite
91