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 #include "tensorflow/lite/testing/tf_driver.h"
16
17 #include <algorithm>
18 #include <string>
19
20 #include <gmock/gmock.h>
21 #include <gtest/gtest.h>
22 #include "absl/strings/escaping.h"
23 #include "tensorflow/lite/string_util.h"
24
25 namespace tflite {
26 namespace testing {
27 namespace {
28
29 class TestDriver : public TfDriver {
30 public:
31 // No need for a full TfDriver. We just want to test the read/write methods.
TestDriver()32 TestDriver() : TfDriver({}, {}, {}, {}) {}
WriteAndReadBack(tensorflow::DataType type,const std::vector<int64_t> & shape,const string & values)33 string WriteAndReadBack(tensorflow::DataType type,
34 const std::vector<int64_t>& shape,
35 const string& values) {
36 tensorflow::Tensor t = {
37 type,
38 tensorflow::TensorShape{tensorflow::gtl::ArraySlice<int64_t>{
39 reinterpret_cast<const int64_t*>(shape.data()), shape.size()}}};
40 SetInput(values, &t);
41 return ReadOutput(t);
42 }
43 };
44
TEST(TfDriverTest,ReadingAndWritingValues)45 TEST(TfDriverTest, ReadingAndWritingValues) {
46 TestDriver driver;
47 ASSERT_EQ(driver.WriteAndReadBack(tensorflow::DT_FLOAT, {1, 2, 2},
48 "0.10,0.20,0.30,0.40"),
49 "0.100000001,0.200000003,0.300000012,0.400000006");
50 ASSERT_EQ(driver.WriteAndReadBack(tensorflow::DT_INT32, {1, 2, 2},
51 "10,40,100,-100"),
52 "10,40,100,-100");
53 ASSERT_EQ(driver.WriteAndReadBack(tensorflow::DT_UINT8, {1, 2, 2},
54 "48,49,121, 122"),
55 "0,1,y,z");
56 }
57
TEST(TfDriverTest,ReadingAndWritingValuesStrings)58 TEST(TfDriverTest, ReadingAndWritingValuesStrings) {
59 TestDriver driver;
60
61 auto set_buffer = [](const std::vector<string>& values, string* buffer) {
62 DynamicBuffer dynamic_buffer;
63 for (const string& s : values) {
64 dynamic_buffer.AddString(s.data(), s.size());
65 }
66
67 char* char_b = nullptr;
68 int size = dynamic_buffer.WriteToBuffer(&char_b);
69 *buffer = absl::BytesToHexString(absl::string_view(char_b, size));
70 free(char_b);
71 };
72
73 string buffer;
74
75 set_buffer({"", "", "", ""}, &buffer);
76 ASSERT_EQ(driver.WriteAndReadBack(tensorflow::DT_STRING, {1, 2, 2}, buffer),
77 buffer);
78
79 // Note that if we pass the empty string we get the "empty" buffer (where all
80 // the strings are empty).
81 ASSERT_EQ(driver.WriteAndReadBack(tensorflow::DT_STRING, {1, 2, 2}, ""),
82 buffer);
83
84 set_buffer({"AB", "ABC", "X", "YZ"}, &buffer);
85
86 ASSERT_EQ(driver.WriteAndReadBack(tensorflow::DT_STRING, {1, 2, 2}, buffer),
87 buffer);
88 }
89
TEST(TfDriverTest,SimpleTest)90 TEST(TfDriverTest, SimpleTest) {
91 std::unique_ptr<TfDriver> runner(
92 new TfDriver({"a", "b", "c", "d"}, {"float", "float", "float", "float"},
93 {"1,8,8,3", "1,8,8,3", "1,8,8,3", "1,8,8,3"}, {"x", "y"}));
94
95 runner->LoadModel("tensorflow/lite/testdata/multi_add.pb");
96 EXPECT_TRUE(runner->IsValid()) << runner->GetErrorMessage();
97
98 for (const auto& i : {"a", "b", "c", "d"}) {
99 runner->ReshapeTensor(i, "1,2,2,1");
100 }
101 ASSERT_TRUE(runner->IsValid());
102 runner->ResetTensor("c");
103 runner->Invoke({{"a", "0.1,0.2,0.3,0.4"},
104 {"b", "0.001,0.002,0.003,0.004"},
105 {"d", "0.01,0.02,0.03,0.04"}});
106
107 ASSERT_EQ(runner->ReadOutput("x"),
108 "0.101000004,0.202000007,0.303000003,0.404000014");
109 ASSERT_EQ(runner->ReadOutput("y"),
110 "0.0109999999,0.0219999999,0.0329999998,0.0439999998");
111 }
112
113 } // namespace
114 } // namespace testing
115 } // namespace tflite
116