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_PUBLIC_SAVED_MODEL_API_H_
17 #define TENSORFLOW_C_EXPERIMENTAL_SAVED_MODEL_PUBLIC_SAVED_MODEL_API_H_
18 
19 #include "tensorflow/c/c_api_macros.h"
20 #include "tensorflow/c/experimental/saved_model/public/concrete_function.h"
21 #include "tensorflow/c/experimental/saved_model/public/concrete_function_list.h"
22 #include "tensorflow/c/experimental/saved_model/public/signature_def_function.h"
23 #include "tensorflow/c/tf_status.h"
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif  // __cplusplus
28 
29 // An opaque type representing a Tensorflow "SavedModel"
30 // (https://www.tensorflow.org/guide/saved_model) that we always pass by pointer
31 // to achieve ABI stability.
32 typedef struct TF_SavedModel TF_SavedModel;
33 
34 // Load a SavedModel from `dirname`. We expect the SavedModel to contain a
35 // single Metagraph (as for those exported from TF2's `tf.saved_model.save`).
36 //
37 // Params:
38 //  dirname - A directory filepath that the SavedModel is at.
39 //  ctx - A TFE_Context containing optional load/TF runtime options.
40 //        `ctx` must outlive the returned TF_SavedModel pointer.
41 //  status - Set to OK on success and an appropriate error on failure.
42 // Returns:
43 //  If status is not OK, returns nullptr. Otherwise, returns a newly created
44 //  TF_SavedModel instance. It must be deleted by calling TF_DeleteSavedModel.
45 TF_CAPI_EXPORT extern TF_SavedModel* TF_LoadSavedModel(const char* dirname,
46                                                        TFE_Context* ctx,
47                                                        TF_Status* status);
48 
49 // Load a SavedModel from `dirname`.
50 //
51 // Params:
52 //  dirname - A directory filepath that the SavedModel is at.
53 //  ctx - A TFE_Context containing optional load/TF runtime options.
54 //        `ctx` must outlive the returned TF_SavedModel pointer.
55 //  tags - char* array of SavedModel tags. We will load the metagraph matching
56 //         the tags.
57 //  tags_len - number of elements in the `tags` array.
58 //  status - Set to OK on success and an appropriate error on failure.
59 // Returns:
60 //  If status is not OK, returns nullptr. Otherwise, returns a newly created
61 //  TF_SavedModel instance. It must be deleted by calling TF_DeleteSavedModel.
62 TF_CAPI_EXPORT extern TF_SavedModel* TF_LoadSavedModelWithTags(
63     const char* dirname, TFE_Context* ctx, const char* const* tags,
64     int tags_len, TF_Status* status);
65 
66 // Deletes a TF_SavedModel, and frees any resources owned by it.
67 TF_CAPI_EXPORT extern void TF_DeleteSavedModel(TF_SavedModel* model);
68 
69 // Retrieve a function from the TF2 SavedModel via function path.
70 //
71 // Params:
72 //  model - The TF2 SavedModel to load a function from.
73 //  function_path - A string containing the path from the root saved python
74 //                  object to a tf.function method.
75 //                  TODO(bmzhao): Add a detailed example of this with a
76 //                  python tf.module before moving this out of experimental.
77 //  status - Set to OK on success and an appropriate error on failure.
78 // Returns:
79 //  If status is not OK, returns nullptr. Otherwise, returns a
80 //  TF_ConcreteFunction instance. The lifetime of this instance is
81 //  "conceptually" bound to `model`. Once `model` is deleted, all
82 //  `TF_ConcreteFunctions` retrieved from it are invalid, and have been deleted.
83 TF_CAPI_EXPORT extern TF_ConcreteFunction* TF_GetSavedModelConcreteFunction(
84     TF_SavedModel* model, const char* function_path, TF_Status* status);
85 
86 // Retrieve a function from the TF SavedModel via a SignatureDef key.
87 //
88 // Params:
89 //  model - The SavedModel to load a function from.
90 //  signature_def_key - The string key of the SignatureDef map of a SavedModel:
91 //                      https://github.com/tensorflow/tensorflow/blob/69b08900b1e991d84bce31f3b404f5ed768f339f/tensorflow/core/protobuf/meta_graph.proto#L89
92 //  status - Set to OK on success and an appropriate error on failure.
93 // Returns:
94 //  If status is not OK, returns nullptr. Otherwise, returns a
95 //  TF_SignatureDefFunction instance. Once `model` is deleted, all
96 //  `TF_SignatureDefFunctions` retrieved from it are invalid, and have been
97 //  deleted.
98 TF_CAPI_EXPORT extern TF_SignatureDefFunction*
99 TF_GetSavedModelSignatureDefFunction(TF_SavedModel* model,
100                                      const char* signature_def_key,
101                                      TF_Status* status);
102 
103 #ifdef __cplusplus
104 }  // end extern "C"
105 #endif  // __cplusplus
106 
107 #endif  // TENSORFLOW_C_EXPERIMENTAL_SAVED_MODEL_PUBLIC_SAVED_MODEL_API_H_
108