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