1 /* Copyright 2018 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/cc/saved_model/reader.h"
17
18 #include "tensorflow/cc/saved_model/constants.h"
19 #include "tensorflow/cc/saved_model/tag_constants.h"
20 #include "tensorflow/core/lib/core/status.h"
21 #include "tensorflow/core/lib/core/status_test_util.h"
22 #include "tensorflow/core/lib/io/path.h"
23 #include "tensorflow/core/lib/strings/str_util.h"
24 #include "tensorflow/core/platform/test.h"
25
26 namespace tensorflow {
27 namespace {
28
29 constexpr char kTestDataPbTxt[] =
30 "cc/saved_model/testdata/half_plus_two_pbtxt/00000123";
31 constexpr char kTestDataSharded[] =
32 "cc/saved_model/testdata/half_plus_two/00000123";
33
34 class ReaderTest : public ::testing::Test {
35 protected:
ReaderTest()36 ReaderTest() {}
37
CheckMetaGraphDef(const MetaGraphDef & meta_graph_def)38 void CheckMetaGraphDef(const MetaGraphDef& meta_graph_def) {
39 const auto& tags = meta_graph_def.meta_info_def().tags();
40 EXPECT_TRUE(std::find(tags.begin(), tags.end(), kSavedModelTagServe) !=
41 tags.end());
42 EXPECT_NE(meta_graph_def.meta_info_def().tensorflow_version(), "");
43 EXPECT_EQ(
44 meta_graph_def.signature_def().at("serving_default").method_name(),
45 "tensorflow/serving/predict");
46 }
47 };
48
TEST_F(ReaderTest,TagMatch)49 TEST_F(ReaderTest, TagMatch) {
50 MetaGraphDef meta_graph_def;
51
52 const string export_dir =
53 io::JoinPath(testing::TensorFlowSrcRoot(), kTestDataSharded);
54 TF_ASSERT_OK(ReadMetaGraphDefFromSavedModel(export_dir, {kSavedModelTagServe},
55 &meta_graph_def));
56 CheckMetaGraphDef(meta_graph_def);
57 }
58
TEST_F(ReaderTest,NoTagMatch)59 TEST_F(ReaderTest, NoTagMatch) {
60 MetaGraphDef meta_graph_def;
61
62 const string export_dir =
63 io::JoinPath(testing::TensorFlowSrcRoot(), kTestDataSharded);
64 Status st = ReadMetaGraphDefFromSavedModel(export_dir, {"missing-tag"},
65 &meta_graph_def);
66 EXPECT_FALSE(st.ok());
67 EXPECT_TRUE(str_util::StrContains(
68 st.error_message(),
69 "Could not find meta graph def matching supplied tags: { missing-tag }"))
70 << st.error_message();
71 }
72
TEST_F(ReaderTest,NoTagMatchMultiple)73 TEST_F(ReaderTest, NoTagMatchMultiple) {
74 MetaGraphDef meta_graph_def;
75
76 const string export_dir =
77 io::JoinPath(testing::TensorFlowSrcRoot(), kTestDataSharded);
78 Status st = ReadMetaGraphDefFromSavedModel(
79 export_dir, {kSavedModelTagServe, "missing-tag"}, &meta_graph_def);
80 EXPECT_FALSE(st.ok());
81 EXPECT_TRUE(str_util::StrContains(
82 st.error_message(),
83 "Could not find meta graph def matching supplied tags: "))
84 << st.error_message();
85 }
86
TEST_F(ReaderTest,PbtxtFormat)87 TEST_F(ReaderTest, PbtxtFormat) {
88 MetaGraphDef meta_graph_def;
89
90 const string export_dir =
91 io::JoinPath(testing::TensorFlowSrcRoot(), kTestDataPbTxt);
92 TF_ASSERT_OK(ReadMetaGraphDefFromSavedModel(export_dir, {kSavedModelTagServe},
93 &meta_graph_def));
94 CheckMetaGraphDef(meta_graph_def);
95 }
96
TEST_F(ReaderTest,InvalidExportPath)97 TEST_F(ReaderTest, InvalidExportPath) {
98 MetaGraphDef meta_graph_def;
99
100 const string export_dir =
101 io::JoinPath(testing::TensorFlowSrcRoot(), "missing-path");
102 Status st = ReadMetaGraphDefFromSavedModel(export_dir, {kSavedModelTagServe},
103 &meta_graph_def);
104 EXPECT_FALSE(st.ok());
105 }
106
107 } // namespace
108 } // namespace tensorflow
109