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 #include "tensorflow/c/experimental/saved_model/public/saved_model_api.h"
17
18 #include <memory>
19 #include <string>
20 #include <unordered_set>
21
22 #include "absl/types/optional.h"
23 #include "tensorflow/c/eager/tfe_context_internal.h"
24 #include "tensorflow/c/experimental/saved_model/core/saved_model_api.h"
25 #include "tensorflow/c/experimental/saved_model/core/tf_saved_model_api.h"
26 #include "tensorflow/c/experimental/saved_model/internal/concrete_function_list_type.h"
27 #include "tensorflow/c/experimental/saved_model/internal/concrete_function_type.h"
28 #include "tensorflow/c/experimental/saved_model/internal/saved_model_api_type.h"
29 #include "tensorflow/c/experimental/saved_model/internal/signature_def_function_type.h"
30 #include "tensorflow/c/tf_status.h"
31 #include "tensorflow/c/tf_status_internal.h"
32 #include "tensorflow/core/common_runtime/eager/context.h"
33 #include "tensorflow/core/platform/casts.h"
34 #include "tensorflow/core/platform/errors.h"
35 #include "tensorflow/core/platform/status.h"
36
37 extern "C" {
38
TF_LoadSavedModel(const char * dirname,TFE_Context * ctx,TF_Status * status)39 TF_SavedModel* TF_LoadSavedModel(const char* dirname, TFE_Context* ctx,
40 TF_Status* status) {
41 std::string saved_model_dir(dirname);
42 std::unique_ptr<tensorflow::SavedModelAPI> result;
43
44 if (tensorflow::unwrap(ctx)->UsesTFRT()) {
45 status->status = tensorflow::errors::Unimplemented(
46 "TFRT SavedModel implementation will be added in the future");
47 } else {
48 std::unique_ptr<tensorflow::TFSavedModelAPI> saved_model;
49 status->status = tensorflow::TFSavedModelAPI::Load(
50 dirname, absl::nullopt,
51 tensorflow::down_cast<tensorflow::EagerContext*>(
52 tensorflow::unwrap(ctx)),
53 &saved_model);
54 result = std::move(saved_model);
55 }
56
57 if (!status->status.ok()) {
58 return nullptr;
59 }
60 return tensorflow::wrap(result.release());
61 }
62
TF_LoadSavedModelWithTags(const char * dirname,TFE_Context * ctx,const char * const * tags,int tags_len,TF_Status * status)63 TF_SavedModel* TF_LoadSavedModelWithTags(const char* dirname, TFE_Context* ctx,
64 const char* const* tags, int tags_len,
65 TF_Status* status) {
66 std::string saved_model_dir(dirname);
67
68 std::unordered_set<std::string> tagset;
69 for (int i = 0; i < tags_len; ++i) {
70 tagset.insert(std::string(tags[i]));
71 }
72
73 std::unique_ptr<tensorflow::SavedModelAPI> result;
74 if (tensorflow::unwrap(ctx)->UsesTFRT()) {
75 status->status = tensorflow::errors::Unimplemented(
76 "TFRT SavedModel implementation will be added in the future");
77 } else {
78 std::unique_ptr<tensorflow::TFSavedModelAPI> saved_model;
79 status->status = tensorflow::TFSavedModelAPI::Load(
80 dirname, tagset,
81 tensorflow::down_cast<tensorflow::EagerContext*>(
82 tensorflow::unwrap(ctx)),
83 &saved_model);
84 result = std::move(saved_model);
85 }
86
87 if (!status->status.ok()) {
88 return nullptr;
89 }
90 return tensorflow::wrap(result.release());
91 }
92
TF_DeleteSavedModel(TF_SavedModel * model)93 void TF_DeleteSavedModel(TF_SavedModel* model) {
94 delete tensorflow::unwrap(model);
95 }
96
TF_GetSavedModelConcreteFunction(TF_SavedModel * model,const char * function_path,TF_Status * status)97 TF_ConcreteFunction* TF_GetSavedModelConcreteFunction(TF_SavedModel* model,
98 const char* function_path,
99 TF_Status* status) {
100 tensorflow::ConcreteFunction* result = nullptr;
101 tensorflow::Status get_function_status =
102 tensorflow::unwrap(model)->GetFunction(function_path, &result);
103 status->status.Update(get_function_status);
104 if (!get_function_status.ok()) {
105 return nullptr;
106 }
107 return tensorflow::wrap(result);
108 }
109
110 TF_CAPI_EXPORT extern TF_SignatureDefFunction*
TF_GetSavedModelSignatureDefFunction(TF_SavedModel * model,const char * signature_def_key,TF_Status * status)111 TF_GetSavedModelSignatureDefFunction(TF_SavedModel* model,
112 const char* signature_def_key,
113 TF_Status* status) {
114 tensorflow::SignatureDefFunction* result = nullptr;
115 tensorflow::Status get_function_status =
116 tensorflow::unwrap(model)->GetSignatureDefFunction(signature_def_key,
117 &result);
118 status->status.Update(get_function_status);
119 if (!get_function_status.ok()) {
120 return nullptr;
121 }
122 return tensorflow::wrap(result);
123 }
124
125 } // end extern "C"
126