TensorFlow Serving C++ API Documentation
saved_model_config_test.cc
1 /* Copyright 2023 Google Inc. 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_serving/session_bundle/saved_model_config.h"
17 
18 #include <string>
19 
20 #include <gmock/gmock.h>
21 #include <gtest/gtest.h>
22 #include "absl/strings/escaping.h"
23 #include "absl/strings/substitute.h"
24 #include "tensorflow/cc/saved_model/constants.h"
25 #include "xla/tsl/lib/core/status_test_util.h"
26 #include "tensorflow/core/grappler/optimizers/inference/batch_op_rewriter.pb.h"
27 #include "tensorflow/core/protobuf/config.pb.h"
28 #include "tensorflow/core/protobuf/rewriter_config.pb.h"
29 #include "tensorflow/core/public/session_options.h"
30 #include "tsl/platform/env.h"
31 #include "tsl/platform/path.h"
32 #include "tsl/platform/status.h"
33 #include "tensorflow_serving/servables/tensorflow/remote_op_config_rewriter.pb.h"
34 #include "tensorflow_serving/servables/tensorflow/saved_model_config.pb.h"
35 #include "tensorflow_serving/servables/tensorflow/saved_model_config_util.h"
36 #include "tensorflow_serving/test_util/test_util.h"
37 
38 namespace tensorflow {
39 namespace serving {
40 namespace session_bundle {
41 namespace {
42 
43 const char kTestSavedModelWithSavedModelConfigPath[] =
44  "servables/tensorflow/testdata/"
45  "saved_model_half_plus_two_cpu_with_saved_model_config/00000123";
46 
47 const char kTestSavedModelWithEmptySavedModelConfigPath[] =
48  "servables/tensorflow/testdata/"
49  "saved_model_half_plus_two_cpu_with_empty_saved_model_config/00000123";
50 
51 using test_util::EqualsProto;
52 
53 TEST(SavedModelConfigTest, EmptySavedModelConfig) {
54  const std::string export_dir =
55  test_util::TestSrcDirPath(kTestSavedModelWithEmptySavedModelConfigPath);
56 
57  SessionOptions session_options;
58 
59  TF_ASSERT_OK(MaybeLoadSavedModelConfig(export_dir, &session_options));
60 
61  auto& custom_optimizers = session_options.config.graph_options()
62  .rewrite_options()
63  .custom_optimizers();
64  EXPECT_EQ(custom_optimizers.size(), 0);
65 }
66 
67 TEST(SavedModelConfigTest, SavedModelConfig) {
68  const std::string export_dir =
69  test_util::TestSrcDirPath(kTestSavedModelWithSavedModelConfigPath);
70 
71  SessionOptions session_options;
72 
73  SavedModelConfig saved_model_config;
74  {
75  std::string content;
76  TF_ASSERT_OK(tsl::ReadFileToString(
77  tsl::Env::Default(),
78  test_util::TestSrcDirPath(tsl::io::JoinPath(
79  kTestSavedModelWithSavedModelConfigPath,
80  kSavedModelAssetsExtraDirectory, kSavedModelConfigPath)),
81  &content));
82 
83  EXPECT_TRUE(saved_model_config.ParseFromString(content));
84  }
85 
86  TF_ASSERT_OK(MaybeLoadSavedModelConfig(export_dir, &session_options));
87 
88  auto& custom_optimizers = session_options.config.graph_options()
89  .rewrite_options()
90  .custom_optimizers();
91  EXPECT_EQ(custom_optimizers.size(), 2);
92 
93  EXPECT_THAT(custom_optimizers,
94  ::testing::UnorderedElementsAre(
95  EqualsProto(absl::Substitute(
96  R"pb(
97  name: "$0"
98  parameter_map {
99  key: "$1"
100  value { s: "$2" }
101  })pb",
102  kRemoteOpConfigRewriter, kRemoteOpRewriteConfigParamKey,
103  absl::Base64Escape(saved_model_config.session_overrides()
104  .remote_op_remap_config()
105  .SerializeAsString()))),
106  EqualsProto(absl::Substitute(
107  R"pb(
108  name: "$0"
109  parameter_map {
110  key: "$1"
111  value { s: "$2" }
112  })pb",
113  kBatchOpRewriter, kBatchOpRewriteConfigParamKey,
114  absl::Base64Escape(saved_model_config.session_overrides()
115  .batch_op_rewriter_config()
116  .SerializeAsString())))));
117 }
118 
119 } // namespace
120 } // namespace session_bundle
121 } // namespace serving
122 } // namespace tensorflow