xref: /aosp_15_r20/external/tensorflow/tensorflow/c/experimental/saved_model/core/tf_saved_model_api.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2020 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 #ifndef TENSORFLOW_C_EXPERIMENTAL_SAVED_MODEL_CORE_TF_SAVED_MODEL_IMPL_H_
17 #define TENSORFLOW_C_EXPERIMENTAL_SAVED_MODEL_CORE_TF_SAVED_MODEL_IMPL_H_
18 
19 #include <memory>
20 #include <string>
21 #include <unordered_map>
22 #include <unordered_set>
23 #include <vector>
24 
25 #include "absl/container/flat_hash_map.h"
26 #include "absl/types/optional.h"
27 #include "tensorflow/c/eager/immediate_execution_context.h"
28 #include "tensorflow/c/experimental/saved_model/core/concrete_function.h"
29 #include "tensorflow/c/experimental/saved_model/core/revived_types/revived_objects.h"
30 #include "tensorflow/c/experimental/saved_model/core/revived_types/tensorhandle_convertible.h"
31 #include "tensorflow/c/experimental/saved_model/core/revived_types/tf_concrete_function.h"
32 #include "tensorflow/c/experimental/saved_model/core/revived_types/variable.h"
33 #include "tensorflow/c/experimental/saved_model/core/saved_model_api.h"
34 #include "tensorflow/c/experimental/saved_model/core/signature_def_function.h"
35 #include "tensorflow/cc/saved_model/bundle_v2.h"
36 #include "tensorflow/core/platform/status.h"
37 
38 namespace tensorflow {
39 
40 // An implementation of the SavedModelAPI using the TF Eager runtime. See
41 // https://github.com/tensorflow/community/blob/master/rfcs/20200218-tf-c-saved-model.md
42 // Conceptually, there are many differences between a tf.function and
43 // a FunctionDef is executed by the C API.
44 // 1. A tf.function is polymorphic, meaning it can correspond to multiple
45 // ConcreteFunctions (of differing shapes, python arguments, etc). A
46 // FunctionDef corresponds to a single ConcreteFunction.
47 // 2. A tf.function can take arbitrary python inputs, whereas the FunctionDef
48 // only accepts tensors.
49 // 3. A tf.function is a closure that can contain captured inputs, whereas
50 // FunctionDefs loaded from SavedModels are "functional" (all inputs are
51 // explicitly passed as arguments).
52 // The SavedModelAPI only supports loading tf.functions annotated with input
53 // signatures so that we ensure that there is a 1:1 mapping between tf.function
54 // -> FunctionDef, and have a guarantee that all inputs are tensors.
55 // (https://github.com/tensorflow/tensorflow/blob/2b96f3662bd776e277f86997659e61046b56c315/tensorflow/python/eager/def_function.py#L1167-L1171),
56 class TFSavedModelAPI : public SavedModelAPI {
57  public:
58   Status GetFunction(const std::string& function_path,
59                      ConcreteFunction** function) override;
60 
61   Status GetFunctions(
62       int node_id,
63       absl::flat_hash_map<std::string, ConcreteFunction*>* functions) override;
64 
65   Status GetSignatureDefFunction(const std::string& signature_def_key,
66                                  SignatureDefFunction** function) override;
67 
68   static Status Load(
69       const std::string& directory,
70       const absl::optional<std::unordered_set<std::string>>& tags,
71       ImmediateExecutionContext* context,
72       std::unique_ptr<TFSavedModelAPI>* out);
73 
74   ~TFSavedModelAPI() override = default;
75 
76   Status GetVariable(const std::string& variable_path, Variable** variable);
77 
78   SavedModelV2Bundle* GetBundle() override;
79 
80  private:
81   TFSavedModelAPI(const std::string& directory, SavedModelV2Bundle bundle,
82                   RevivedObjects revived_objects);
83 
84   std::string directory_;
85   SavedModelV2Bundle bundle_;
86   RevivedObjects revived_objects_;
87 };
88 
89 }  // namespace tensorflow
90 
91 #endif  // TENSORFLOW_C_EXPERIMENTAL_SAVED_MODEL_CORE_TF_SAVED_MODEL_IMPL_H_
92