TensorFlow Serving C++ API Documentation
graph_rewriter.h
1 /* Copyright 2019 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 #ifndef TENSORFLOW_SERVING_SESSION_BUNDLE_GRAPH_REWRITER_H_
17 #define TENSORFLOW_SERVING_SESSION_BUNDLE_GRAPH_REWRITER_H_
18 
19 #include <functional>
20 #include <utility>
21 
22 #include "absl/base/thread_annotations.h"
23 #include "absl/synchronization/mutex.h"
24 #include "tensorflow/core/platform/errors.h"
25 #include "tensorflow/core/platform/status.h"
26 #include "tensorflow/core/protobuf/meta_graph.pb.h"
27 
28 namespace tensorflow {
29 namespace serving {
30 
31 // Class for registering a global graph rewrite function.
33  public:
34  static GraphRewriter& GetGlobal() {
35  static auto* const singleton = new GraphRewriter();
36  return *singleton;
37  }
38 
39  Status Set(std::function<Status(tensorflow::MetaGraphDef*)>&& rewriter) {
40  absl::MutexLock l(&m_);
41  if (rewriter_ != nullptr)
42  return errors::AlreadyExists("Graph rewriter already set.");
43 
44  rewriter_ = std::move(rewriter);
45 
46  return Status();
47  }
48 
49  // For testing only. Resets the rewriter to nullptr
50  Status ResetForTesting() {
51  absl::MutexLock l(&m_);
52  rewriter_ = nullptr;
53  return Status();
54  }
55 
56  std::function<Status(tensorflow::MetaGraphDef*)>& Get() {
57  absl::MutexLock l(&m_);
58  return rewriter_;
59  }
60 
61  bool IsRegistered() {
62  absl::MutexLock l(&m_);
63  return rewriter_ != nullptr;
64  }
65 
66  private:
67  GraphRewriter() = default;
68 
69  absl::Mutex m_;
70  std::function<Status(tensorflow::MetaGraphDef*)> rewriter_
71  ABSL_GUARDED_BY(m_);
72 };
73 
74 // EXPERIMENTAL. THE 2 METHODS BELOW MAY CHANGE OR GO AWAY. USE WITH CAUTION.
75 // Sets a global graph rewrite function that is called on all saved models
76 // immediately after metagraph load, but before session creation. This function
77 // can only be called once.
78 inline Status SetGraphRewriter(
79  std::function<Status(tensorflow::MetaGraphDef*)>&& rewriter) {
80  return GraphRewriter::GetGlobal().Set(std::move(rewriter));
81 }
82 
83 // For testing only. Resets the experimental graph rewriter above.
84 inline Status ResetGraphRewriterForTesting() {
85  return GraphRewriter::GetGlobal().ResetForTesting();
86 }
87 
88 } // namespace serving
89 } // namespace tensorflow
90 
91 #endif // TENSORFLOW_SERVING_SESSION_BUNDLE_GRAPH_REWRITER_H_