TensorFlow Serving C++ API Documentation
util.h
1 /* Copyright 2017 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_SERVABLES_TENSORFLOW_UTIL_H_
17 #define TENSORFLOW_SERVING_SERVABLES_TENSORFLOW_UTIL_H_
18 
19 #include "absl/types/optional.h"
20 #include "tensorflow/core/framework/tensor.h"
21 #include "tensorflow/core/lib/core/status.h"
22 #include "tensorflow/core/lib/monitoring/counter.h"
23 #include "tensorflow/core/lib/monitoring/sampler.h"
24 #include "tensorflow/core/platform/threadpool_options.h"
25 #include "tensorflow/core/public/session.h"
26 #include "tensorflow_serving/apis/input.pb.h"
27 #include "tensorflow_serving/apis/model.pb.h"
28 #include "tensorflow_serving/resources/resources.pb.h"
29 #include "tensorflow_serving/util/file_probing_env.h"
30 
31 namespace tensorflow {
32 namespace serving {
33 
34 // Implementation details mainly used for testing; please don't depend on it.
35 namespace internal {
36 
37 monitoring::Sampler<1>* GetExampleCounts();
38 
39 monitoring::Counter<1>* GetExampleCountTotal();
40 
41 } // namespace internal
42 
43 // Metrics by model
44 void RecordModelRequestCount(const string& model_name, const Status& status);
45 
46 // Enable/disable `method_name` checks on `SignatureDef` for predict, classify,
47 // regress APIs. Native TF2 models use fixed `method_name` for all APIs, and
48 // the check needs to be disabled to support both TF1 and (native) TF2 models.
49 //
50 // Disabling the check (typically done at process startup) should be OK and
51 // safe for most API users. By default the checks are enabled.
52 void SetSignatureMethodNameCheckFeature(bool v);
53 
54 // Get current state of `method_name` check (see above for details).
55 bool GetSignatureMethodNameCheckFeature();
56 
57 // Records the example count of this request with the metric tracking the
58 // histogram of number of examples per request.
59 void RecordRequestExampleCount(const string& model_name, size_t count);
60 
61 // InputToSerializedExampleTensor populates a string Tensor of serialized
62 // Examples.
63 // If input has n Examples returns a string Tensor with shape {n}.
64 //
65 // Cases:
66 // - Input kind unset: Tensor of shape {0}
67 // - Input::example_list: Serializes each example.
68 // - Input::example_list_with_context: Serializes each example merged with the
69 // context.
70 // - Other: non-OK Status.
71 //
72 // Note: does not perform any structural validation (e.g., if an example list is
73 // empty it will return a Tensor of shape {0}).
74 Status InputToSerializedExampleTensor(const Input& input, Tensor* examples);
75 
76 // Issues a single Session::Run() call with 'input' to produce 'outputs'.
77 // Equivalent to InputToSerializedExampleTensor() followed by Session::Run().
78 Status PerformOneShotTensorComputation(
79  const RunOptions& run_options, const Input& input,
80  const string& input_tensor_name,
81  const std::vector<string>& output_tensor_names, Session* session,
82  std::vector<Tensor>* outputs, int* num_input_examples,
83  const thread::ThreadPoolOptions& thread_pool_options =
84  thread::ThreadPoolOptions(),
85  int64_t* runtime_latency = nullptr);
86 
87 // Same as PerformOneShotTensorComputation() above, except allows for multiple
88 // input tensor names (each tensor is fed the *same* `input`).
89 Status PerformOneShotTensorComputation(
90  const RunOptions& run_options, const Input& input,
91  const std::set<string>& input_tensor_names,
92  const std::vector<string>& output_tensor_names, Session* session,
93  std::vector<Tensor>* outputs, int* num_input_examples,
94  const thread::ThreadPoolOptions& thread_pool_options =
95  thread::ThreadPoolOptions());
96 
97 // Populates given model_spec based on the model name and optional
98 // signature/version information.
99 // If signature_name has a value and is empty, model_spec's signature_name is
100 // set to tensorflow::kDefaultServingSignatureDefKey.
101 void MakeModelSpec(const string& model_name,
102  const absl::optional<string>& signature_name,
103  const absl::optional<int64_t>& version,
104  ModelSpec* model_spec);
105 
106 // Gets the disk size of the model in the given path.
107 Status GetModelDiskSize(const string& path, FileProbingEnv* env,
108  uint64_t* total_file_size);
109 
110 // Estimates the resources a session bundle or saved model bundle will use once
111 // loaded, from its export or saved model path. Directly uses disk state for
112 // estimation.
113 Status EstimateResourceFromPathUsingDiskState(const string& path,
114  FileProbingEnv* env,
115  ResourceAllocation* estimate);
116 
117 // Update metrics for runtime latency.
118 void RecordRuntimeLatency(const string& model_name, const string& api,
119  const string& runtime, int64_t latency_usec);
120 
121 // Update metrics for request latency.
122 void RecordRequestLatency(const string& model_name, const string& api,
123  const string& entrypoint, int64_t latency_usec);
124 
125 // Get string keys of a map.
126 template <typename T>
127 std::set<string> GetMapKeys(const T& map) {
128  std::set<string> keys;
129  for (const auto& it : map) {
130  keys.insert(it.first);
131  }
132  return keys;
133 }
134 
135 // Returns a \ b, i.e. all items that are in `set_a` but not in `set_b`.
136 std::set<string> SetDifference(std::set<string> set_a, std::set<string> set_b);
137 
138 // Returns true if errors should be exported to logging service.
139 bool IsTfrtErrorLoggingEnabled();
140 
141 } // namespace serving
142 } // namespace tensorflow
143 
144 #endif // TENSORFLOW_SERVING_SERVABLES_TENSORFLOW_UTIL_H_