TensorFlow Serving C++ API Documentation
session_bundle_util.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_SESSION_BUNDLE_UTIL_H_
17 #define TENSORFLOW_SERVING_SESSION_BUNDLE_SESSION_BUNDLE_UTIL_H_
18 
19 #include "tensorflow/cc/saved_model/loader.h"
20 #include "tensorflow_serving/session_bundle/manifest_proto.h"
21 #include "tensorflow_serving/session_bundle/session_bundle.h"
22 
23 namespace tensorflow {
24 namespace serving {
25 
26 namespace session_bundle {
27 
28 // Interface from bundle_shim.h
29 
30 // Converts signatures in the MetaGraphDef into a SignatureDefs in the
31 // MetaGraphDef.
32 Status ConvertSignaturesToSignatureDefs(MetaGraphDef* meta_graph_def);
33 
34 // Converts a SessionBundle to a SavedModelBundle.
35 Status ConvertSessionBundleToSavedModelBundle(
36  SessionBundle& session_bundle, SavedModelBundle* saved_model_bundle);
37 
38 // Loads a SavedModel from either a session-bundle path or a SavedModel bundle
39 // path. If `is_session_bundle` is not a nullptr, sets it to `true` iff
40 // SavedModel was up-converted and loaded from a SessionBundle.
41 // `is_session_bundle` value should not be used if error is returned.
42 Status LoadSessionBundleOrSavedModelBundle(
43  const SessionOptions& session_options, const RunOptions& run_options,
44  const string& export_dir, const std::unordered_set<string>& tags,
45  SavedModelBundle* bundle, bool* is_session_bundle = nullptr);
46 
47 // Loads a SavedModel from either a session-bundle path or a SavedModel bundle
48 // path. If `is_session_bundle` is not a nullptr, sets it to `true` iff
49 // SavedModel was up-converted and loaded from a SessionBundle.
50 // `is_session_bundle` value should not be used if error is returned. If
51 // `maybe_load_saved_model_config` is set and
52 // {export_dir}/assets.extra/saved_model_config.pb exists, the configs defined
53 // in saved_model_config.pb will override associated values specified in
54 // `session_options`.
55 Status LoadSessionBundleOrSavedModelBundle(
56  const SessionOptions& session_options, const RunOptions& run_options,
57  const string& export_dir, const std::unordered_set<string>& tags,
58  bool maybe_load_saved_model_config, SavedModelBundle* bundle,
59  bool* is_session_bundle = nullptr);
60 
61 // Interface from session_bundle.h
62 
63 // Similar to the LoadSessionBundleFromPath(), but also allows the session run
64 // invocations for the restore and init ops to be configured with
65 // tensorflow::RunOptions.
66 //
67 // This method is EXPERIMENTAL and may change or be removed.
68 Status LoadSessionBundleFromPathUsingRunOptions(
69  const SessionOptions& session_options, const RunOptions& run_options,
70  const StringPiece export_dir, SessionBundle* bundle);
71 
72 // Interface from signature.h
73 
74 // (Re)set Signatures in a MetaGraphDef.
75 Status SetSignatures(const Signatures& signatures,
76  tensorflow::MetaGraphDef* meta_graph_def);
77 
78 // Gets a ClassificationSignature from a MetaGraphDef's default signature.
79 // Returns an error if the default signature is not a ClassificationSignature,
80 // or does not exist.
81 Status GetClassificationSignature(
82  const tensorflow::MetaGraphDef& meta_graph_def,
83  ClassificationSignature* signature);
84 
85 // Gets a RegressionSignature from a MetaGraphDef's default signature.
86 // Returns an error if the default signature is not a RegressionSignature,
87 // or does not exist.
88 Status GetRegressionSignature(const tensorflow::MetaGraphDef& meta_graph_def,
89  RegressionSignature* signature);
90 
91 // Runs a classification using the provided signature and initialized Session.
92 // input: input batch of items to classify
93 // classes: output batch of classes; may be null if not needed
94 // scores: output batch of scores; may be null if not needed
95 // Validates sizes of the inputs and outputs are consistent (e.g., input
96 // batch size equals output batch sizes).
97 // Does not do any type validation.
98 Status RunClassification(const ClassificationSignature& signature,
99  const Tensor& input, Session* session, Tensor* classes,
100  Tensor* scores);
101 
102 // Runs regression using the provided signature and initialized Session.
103 // input: input batch of items to run the regression model against
104 // output: output targets
105 // Validates sizes of the inputs and outputs are consistent (e.g., input
106 // batch size equals output batch sizes).
107 // Does not do any type validation.
108 Status RunRegression(const RegressionSignature& signature, const Tensor& input,
109  Session* session, Tensor* output);
110 
111 // Gets a named Signature from a MetaGraphDef.
112 // Returns an error if a Signature with the given name does not exist.
113 Status GetNamedSignature(const string& name,
114  const tensorflow::MetaGraphDef& meta_graph_def,
115  Signature* default_signature);
116 
117 } // namespace session_bundle
118 } // namespace serving
119 } // namespace tensorflow
120 
121 #endif // TENSORFLOW_SERVING_SESSION_BUNDLE_SESSION_BUNDLE_UTIL_H_