From a92488b55c599280bc84dcdef4fb52c0dff90499 Mon Sep 17 00:00:00 2001 From: Brian Zhao Date: Mon, 20 Apr 2020 09:57:01 -0700 Subject: [PATCH] Initial checkin of stub implementation for TF C/C++ SavedModel API. See RFC https://github.com/tensorflow/community/pull/207. This commit contains: 1. the C API headers (under tf/c/experimental/saved_model/public), 2. the C -> C++ implementation glue code (under tf/c/experimental/saved_model/internal) 3. the C++ implementation (under tf/c/experimental/saved_model/core) The C++ implementation (3) is currently a stub. Please note: The C++ header-only API wrapping the C API will be added in a subsequent change. Certain aspects of this C API are expected to change heavily (mainly regarding ConcreteFunction execution). PiperOrigin-RevId: 307419836 Change-Id: I6755fca97394d47c09799c534236ac17a30410d1 --- tensorflow/c/BUILD | 6 + tensorflow/c/c_api_macros.h | 33 ++++ .../c/experimental/saved_model/README.md | 66 ++++++++ .../c/experimental/saved_model/core/BUILD | 46 +++++ .../saved_model/core/concrete_function.cc | 32 ++++ .../saved_model/core/concrete_function.h | 55 ++++++ .../saved_model/core/function_metadata.h | 27 +++ .../saved_model/core/saved_model_api.h | 55 ++++++ .../c/experimental/saved_model/internal/BUILD | 157 ++++++++++++++++++ .../saved_model/internal/concrete_function.cc | 40 +++++ .../internal/concrete_function_list.cc | 33 ++++ .../internal/concrete_function_list_type.h | 36 ++++ .../internal/concrete_function_type.h | 36 ++++ .../saved_model/internal/conversion_macros.h | 28 ++++ .../saved_model/internal/function_metadata.cc | 20 +++ .../internal/function_metadata_type.h | 30 ++++ .../saved_model/internal/saved_model_api.cc | 67 ++++++++ .../internal/saved_model_api_type.h | 30 ++++ .../c/experimental/saved_model/public/BUILD | 63 +++++++ .../saved_model/public/c_saved_model_api.h | 26 +++ .../saved_model/public/concrete_function.h | 53 ++++++ .../public/concrete_function_list.h | 35 ++++ .../saved_model/public/function_metadata.h | 35 ++++ .../saved_model/public/saved_model_api.h | 96 +++++++++++ 24 files changed, 1105 insertions(+) create mode 100644 tensorflow/c/c_api_macros.h create mode 100644 tensorflow/c/experimental/saved_model/README.md create mode 100644 tensorflow/c/experimental/saved_model/core/BUILD create mode 100644 tensorflow/c/experimental/saved_model/core/concrete_function.cc create mode 100644 tensorflow/c/experimental/saved_model/core/concrete_function.h create mode 100644 tensorflow/c/experimental/saved_model/core/function_metadata.h create mode 100644 tensorflow/c/experimental/saved_model/core/saved_model_api.h create mode 100644 tensorflow/c/experimental/saved_model/internal/BUILD create mode 100644 tensorflow/c/experimental/saved_model/internal/concrete_function.cc create mode 100644 tensorflow/c/experimental/saved_model/internal/concrete_function_list.cc create mode 100644 tensorflow/c/experimental/saved_model/internal/concrete_function_list_type.h create mode 100644 tensorflow/c/experimental/saved_model/internal/concrete_function_type.h create mode 100644 tensorflow/c/experimental/saved_model/internal/conversion_macros.h create mode 100644 tensorflow/c/experimental/saved_model/internal/function_metadata.cc create mode 100644 tensorflow/c/experimental/saved_model/internal/function_metadata_type.h create mode 100644 tensorflow/c/experimental/saved_model/internal/saved_model_api.cc create mode 100644 tensorflow/c/experimental/saved_model/internal/saved_model_api_type.h create mode 100644 tensorflow/c/experimental/saved_model/public/BUILD create mode 100644 tensorflow/c/experimental/saved_model/public/c_saved_model_api.h create mode 100644 tensorflow/c/experimental/saved_model/public/concrete_function.h create mode 100644 tensorflow/c/experimental/saved_model/public/concrete_function_list.h create mode 100644 tensorflow/c/experimental/saved_model/public/function_metadata.h create mode 100644 tensorflow/c/experimental/saved_model/public/saved_model_api.h diff --git a/tensorflow/c/BUILD b/tensorflow/c/BUILD index 9bc96ff5242..c7ed2b62b50 100644 --- a/tensorflow/c/BUILD +++ b/tensorflow/c/BUILD @@ -118,6 +118,12 @@ cc_library( visibility = ["//visibility:public"], ) +cc_library( + name = "c_api_macros", + hdrs = ["c_api_macros.h"], + visibility = ["//visibility:public"], +) + tf_cuda_library( name = "c_api", hdrs = [ diff --git a/tensorflow/c/c_api_macros.h b/tensorflow/c/c_api_macros.h new file mode 100644 index 00000000000..85c9507db87 --- /dev/null +++ b/tensorflow/c/c_api_macros.h @@ -0,0 +1,33 @@ +/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +==============================================================================*/ + +#ifndef TENSORFLOW_C_C_API_MACROS_H_ +#define TENSORFLOW_C_C_API_MACROS_H_ + +#ifdef SWIG +#define TF_CAPI_EXPORT +#else +#if defined(_WIN32) +#ifdef TF_COMPILE_LIBRARY +#define TF_CAPI_EXPORT __declspec(dllexport) +#else +#define TF_CAPI_EXPORT __declspec(dllimport) +#endif // TF_COMPILE_LIBRARY +#else +#define TF_CAPI_EXPORT __attribute__((visibility("default"))) +#endif // _WIN32 +#endif // SWIG + +#endif // TENSORFLOW_C_C_API_MACROS_H_ diff --git a/tensorflow/c/experimental/saved_model/README.md b/tensorflow/c/experimental/saved_model/README.md new file mode 100644 index 00000000000..2fdb8137598 --- /dev/null +++ b/tensorflow/c/experimental/saved_model/README.md @@ -0,0 +1,66 @@ +# Tensorflow C SavedModel API + +## Overview + +These are the new experimental C SavedModel APIs for loading and running +SavedModels in a TF2-idiomatic fashion. See +[RFC 207](https://github.com/tensorflow/community/pull/207) for additional +context. + +The directory structure is as follows: + +```none +saved_model/ + + public/ + + internal/ + + core/ + +``` + +## saved_model/public + +`saved_model/public` is intended to house *only the public headers* of the +SavedModel C API. + +These headers: + +1. declare opaque C types (like `TF_SavedModel`), + +2. declare the functions that operate on these types (like `TF_LoadSavedModel`). + +Once they leave experimental, these APIs should be considered stable for use +by external clients. + +These headers are in a separate directory to make it obvious to clients which +headers they should depend on, and which headers are implementation details. +Separating these public headers by directory also allow future programmatic +checks to ensure that TF public headers only `#include` other public TF headers. + +## saved_model/internal + +`saved_model/internal` is the "glue" between the C API and the internal C++ +implementation. + +Its role is to: + +1. implement the C API functions declared in `saved_model/public` + +2. define the C API types declared in `saved_model/public` + +The files fulfilling 1. are named `*.cc` (eg: `concrete_function.cc`), while +the files fulfilling 2. are `*type.h` (eg: `concrete_function_type.h`). + +The headers exposing the internal implementation of the opaque C types are only +visible to other implementors of the C API. This is similar to how other +TF C API implementations use `tf_status_internal.h` (to extract the underlying +`tensorflow::Status`). All other targets in this directory are private. + +## saved_model/core + +`saved_model/core` contains pure C++ "Classes" underlying the C API types +in `saved_model/public/`. These are implementation +details subject to change, and have limited visibility to implementors only. +This is the bottom-most layer of the `C++ -> C -> C++` sandwich. diff --git a/tensorflow/c/experimental/saved_model/core/BUILD b/tensorflow/c/experimental/saved_model/core/BUILD new file mode 100644 index 00000000000..68e46a42ab4 --- /dev/null +++ b/tensorflow/c/experimental/saved_model/core/BUILD @@ -0,0 +1,46 @@ +# Experimental SavedModel C APIs for TensorFlow. See RFC +# https://github.com/tensorflow/community/pull/207 +# Targets in this directory are pure C++ "Classes" underlying the C API types +# under tf/c/experimental/saved_model/public/. They are subject to change and +# have visibility limited to Tensorflow's implementation only. + +package( + default_visibility = [ + "//tensorflow/c/experimental/saved_model/internal:__pkg__", + ], + licenses = ["notice"], # Apache 2.0 +) + +cc_library( + name = "concrete_function", + srcs = [ + "concrete_function.cc", + ], + hdrs = [ + "concrete_function.h", + ], + deps = [ + ":function_metadata", + "//tensorflow/c/eager:operation_interface", + "//tensorflow/c/eager:tensor_handle_interface", + "//tensorflow/core:protos_all_cc", + ], +) + +cc_library( + name = "function_metadata", + hdrs = [ + "function_metadata.h", + ], +) + +cc_library( + name = "saved_model_api", + hdrs = [ + "saved_model_api.h", + ], + deps = [ + ":concrete_function", + "//tensorflow/core:lib", + ], +) diff --git a/tensorflow/c/experimental/saved_model/core/concrete_function.cc b/tensorflow/c/experimental/saved_model/core/concrete_function.cc new file mode 100644 index 00000000000..e6e7a5118d7 --- /dev/null +++ b/tensorflow/c/experimental/saved_model/core/concrete_function.cc @@ -0,0 +1,32 @@ +/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +==============================================================================*/ + +#include "tensorflow/c/experimental/saved_model/core/concrete_function.h" + +#include "tensorflow/c/eager/tensor_handle_interface.h" +#include "tensorflow/c/experimental/saved_model/core/function_metadata.h" + +namespace tensorflow { + +const std::vector& +ConcreteFunction::Captures() const { + return captures_; +} + +const FunctionMetadata& ConcreteFunction::GetFunctionMetadata() const { + return metadata_; +} + +} // namespace tensorflow diff --git a/tensorflow/c/experimental/saved_model/core/concrete_function.h b/tensorflow/c/experimental/saved_model/core/concrete_function.h new file mode 100644 index 00000000000..e4b8cf6c975 --- /dev/null +++ b/tensorflow/c/experimental/saved_model/core/concrete_function.h @@ -0,0 +1,55 @@ +/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +==============================================================================*/ + +#ifndef TENSORFLOW_C_EXPERIMENTAL_SAVED_MODEL_CORE_CONCRETE_FUNCTION_H_ +#define TENSORFLOW_C_EXPERIMENTAL_SAVED_MODEL_CORE_CONCRETE_FUNCTION_H_ + +#include + +#include "tensorflow/c/eager/operation_interface.h" +#include "tensorflow/c/eager/tensor_handle_interface.h" +#include "tensorflow/c/experimental/saved_model/core/function_metadata.h" +#include "tensorflow/core/framework/function.pb.h" + +namespace tensorflow { + +// Note that ConcreteFunctions's lifetimes are effectively bound +// to the SavedModel they are loaded from, since they retain pointers +// to the TensorHandles owned by the SavedModel, and the FunctionDef +// of the SavedModel. +// Note(bmzhao): This class is only TEMPORARILY virtual, as a way to unblock +// TFRT integration with TF Serving. Do not add more virtual implementations of +// this class. Eventually we want to remove this virtual base class indirection +// and have only a single implementation. +class ConcreteFunction { + public: + virtual ~ConcreteFunction() = 0; + + // This method returns the "Call" Op used to execute the function. + virtual AbstractOperationInterface* GetFunctionOp() = 0; + + const std::vector& Captures() + const; + const FunctionMetadata& GetFunctionMetadata() const; + + private: + FunctionMetadata metadata_; + std::vector captures_; + FunctionDef* function_; +}; + +} // namespace tensorflow + +#endif // TENSORFLOW_C_EXPERIMENTAL_SAVED_MODEL_CORE_CONCRETE_FUNCTION_H_ diff --git a/tensorflow/c/experimental/saved_model/core/function_metadata.h b/tensorflow/c/experimental/saved_model/core/function_metadata.h new file mode 100644 index 00000000000..8499288f032 --- /dev/null +++ b/tensorflow/c/experimental/saved_model/core/function_metadata.h @@ -0,0 +1,27 @@ +/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +==============================================================================*/ + +#ifndef TENSORFLOW_C_EXPERIMENTAL_SAVED_MODEL_CORE_FUNCTION_METADATA_H_ +#define TENSORFLOW_C_EXPERIMENTAL_SAVED_MODEL_CORE_FUNCTION_METADATA_H_ + +namespace tensorflow { + +class FunctionMetadata { + // TODO(bmzhao): Fill in with fields as necessary +}; + +} // namespace tensorflow + +#endif // TENSORFLOW_C_EXPERIMENTAL_SAVED_MODEL_CORE_FUNCTION_METADATA_H_ diff --git a/tensorflow/c/experimental/saved_model/core/saved_model_api.h b/tensorflow/c/experimental/saved_model/core/saved_model_api.h new file mode 100644 index 00000000000..993ae9340e9 --- /dev/null +++ b/tensorflow/c/experimental/saved_model/core/saved_model_api.h @@ -0,0 +1,55 @@ +/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +==============================================================================*/ + +#ifndef TENSORFLOW_C_EXPERIMENTAL_SAVED_MODEL_CORE_SAVED_MODEL_API_H_ +#define TENSORFLOW_C_EXPERIMENTAL_SAVED_MODEL_CORE_SAVED_MODEL_API_H_ + +#include +#include +#include +#include + +#include "tensorflow/c/experimental/saved_model/core/concrete_function.h" +#include "tensorflow/core/platform/status.h" + +namespace tensorflow { + +// Note(bmzhao): This class is only TEMPORARILY virtual, as a way to unblock +// TFRT integration with TF Serving. Do not add more virtual implementations of +// this class. Eventually we want to remove this virtual base class indirection +// and have only a single implementation. +class SavedModelAPI { + public: + // Retrieve a function from the TF2 SavedModel, using the "path" to a function + // in a TF2 savedmodel. + // Note: `function` is a double pointer, so that implementations are + // able to return a pointer to an internal member. + virtual Status GetFunction(const std::string& function_path, + ConcreteFunction** function) = 0; + + // Retrieve a function from a SavedModel, using the key of the + // SignatureDef map: + // https://github.com/tensorflow/tensorflow/blob/69b08900b1e991d84bce31f3b404f5ed768f339f/tensorflow/core/protobuf/meta_graph.proto#L89 + virtual Status GetSignatureDefFunction(const std::string& signature_def_key, + ConcreteFunction** function) = 0; + + virtual const std::vector& ListFunctions() = 0; + + virtual ~SavedModelAPI() = default; +}; + +} // namespace tensorflow + +#endif // TENSORFLOW_C_EXPERIMENTAL_SAVED_MODEL_CORE_SAVED_MODEL_API_H_ diff --git a/tensorflow/c/experimental/saved_model/internal/BUILD b/tensorflow/c/experimental/saved_model/internal/BUILD new file mode 100644 index 00000000000..10798041f62 --- /dev/null +++ b/tensorflow/c/experimental/saved_model/internal/BUILD @@ -0,0 +1,157 @@ +# Experimental Implementation of SavedModel C APIs for TensorFlow. See RFC +# https://github.com/tensorflow/community/pull/207 +# External clients should not worry about this directory; all contents are implementation details. +# Code in this directory is intended to form the glue between the C API and the internal C++ +# implementation by +# 1. mapping C API calls onto correponding methods of C++ objects +# 2. mapping opaque C types onto C++ classes + +# Note(bmzhao): The *.cc files in this directory form the direct implementation of the +# C API functions exposed in tf/c/experimental/saved_model/public/. + +# Note(bmzhao): All *type.h files in this directory are the internal definitions of +# the opaque C types. These headers should only be visible to internal tensorflow +# implementors. + +package( + licenses = ["notice"], # Apache 2.0 +) + +cc_library( + name = "conversion_macros", + hdrs = [ + "conversion_macros.h", + ], +) + +cc_library( + name = "concrete_function", + srcs = [ + "concrete_function.cc", + ], + hdrs = [ + "//tensorflow/c/experimental/saved_model/public:concrete_function.h", + ], + # TODO(bmzhao): Remove this as we refactor C API to granular targets, + # so that we can depend on c/eager/c_api_unified_experimental.h. + features = ["-layering_check"], + visibility = [ + "//tensorflow/c/experimental/saved_model/public:__pkg__", + ], + deps = [ + ":concrete_function_type", + ":function_metadata", + ":function_metadata_type", + "//tensorflow/c:c_api_macros", + "//tensorflow/c/eager:c_api", + "//tensorflow/c/eager:c_api_internal", + "//tensorflow/c/experimental/saved_model/core:concrete_function", + "//tensorflow/c/experimental/saved_model/core:function_metadata", + ], +) + +cc_library( + name = "concrete_function_list", + srcs = [ + "concrete_function_list.cc", + ], + hdrs = [ + "//tensorflow/c/experimental/saved_model/public:concrete_function_list.h", + ], + visibility = [ + "//tensorflow/c/experimental/saved_model/public:__pkg__", + ], + deps = [ + ":concrete_function", + ":concrete_function_list_type", + ":concrete_function_type", + "//tensorflow/c:c_api_macros", + "//tensorflow/c/experimental/saved_model/core:concrete_function", + ], +) + +cc_library( + name = "concrete_function_list_type", + hdrs = [ + "concrete_function_list_type.h", + ], + deps = [ + ":conversion_macros", + "//tensorflow/c/experimental/saved_model/core:concrete_function", + ], +) + +cc_library( + name = "concrete_function_type", + hdrs = [ + "concrete_function_type.h", + ], + deps = [ + ":conversion_macros", + "//tensorflow/c/experimental/saved_model/core:concrete_function", + ], +) + +cc_library( + name = "function_metadata", + srcs = [ + "function_metadata.cc", + ], + hdrs = [ + "//tensorflow/c/experimental/saved_model/public:function_metadata.h", + ], + visibility = [ + "//tensorflow/c/experimental/saved_model/public:__pkg__", + ], + deps = [ + ":function_metadata_type", + "//tensorflow/c:c_api_macros", + "//tensorflow/c/experimental/saved_model/core:function_metadata", + ], +) + +cc_library( + name = "function_metadata_type", + hdrs = [ + "function_metadata_type.h", + ], + deps = [ + ":conversion_macros", + "//tensorflow/c/experimental/saved_model/core:function_metadata", + ], +) + +cc_library( + name = "saved_model_api", + srcs = [ + "saved_model_api.cc", + ], + hdrs = [ + "//tensorflow/c/experimental/saved_model/public:saved_model_api.h", + ], + visibility = [ + "//tensorflow/c/experimental/saved_model/public:__pkg__", + ], + deps = [ + ":concrete_function", + ":concrete_function_list", + ":concrete_function_list_type", + ":concrete_function_type", + ":saved_model_api_type", + "//tensorflow/c:c_api_macros", + "//tensorflow/c:tf_status", + "//tensorflow/c:tf_status_internal", + "//tensorflow/c/experimental/saved_model/core:saved_model_api", + "//tensorflow/core:lib", + ], +) + +cc_library( + name = "saved_model_api_type", + hdrs = [ + "saved_model_api_type.h", + ], + deps = [ + "//tensorflow/c/experimental/saved_model/core:saved_model_api", + ], +) diff --git a/tensorflow/c/experimental/saved_model/internal/concrete_function.cc b/tensorflow/c/experimental/saved_model/internal/concrete_function.cc new file mode 100644 index 00000000000..69f9c2cc609 --- /dev/null +++ b/tensorflow/c/experimental/saved_model/internal/concrete_function.cc @@ -0,0 +1,40 @@ +/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +==============================================================================*/ + +#include "tensorflow/c/experimental/saved_model/public/concrete_function.h" + +#include "tensorflow/c/eager/c_api_unified_experimental.h" +#include "tensorflow/c/experimental/saved_model/core/concrete_function.h" +#include "tensorflow/c/experimental/saved_model/core/function_metadata.h" +#include "tensorflow/c/experimental/saved_model/internal/concrete_function_type.h" +#include "tensorflow/c/experimental/saved_model/internal/function_metadata_type.h" + +extern "C" { + +TF_FunctionMetadata* TF_ConcreteFunctionGetMetadata(TF_ConcreteFunction* func) { + return tensorflow::wrap(&tensorflow::unwrap(func)->GetFunctionMetadata()); +} + +TF_OutputList* TF_ConcreteFunctionGetCaptures(TF_ConcreteFunction* func) { + // TODO(bmzhao): Refactor TF_OutputList struct definition into a separate + // internal header, and implement this function. + return nullptr; +} + +TFE_Op* TF_ConcreteFunctionGetOperation(TF_ConcreteFunction* func) { + return new TFE_Op{tensorflow::unwrap(func)->GetFunctionOp()}; +} + +} // end extern "C" diff --git a/tensorflow/c/experimental/saved_model/internal/concrete_function_list.cc b/tensorflow/c/experimental/saved_model/internal/concrete_function_list.cc new file mode 100644 index 00000000000..00ba3149168 --- /dev/null +++ b/tensorflow/c/experimental/saved_model/internal/concrete_function_list.cc @@ -0,0 +1,33 @@ +/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +==============================================================================*/ + +#include + +#include "tensorflow/c/experimental/saved_model/core/concrete_function.h" +#include "tensorflow/c/experimental/saved_model/internal/concrete_function_list_type.h" +#include "tensorflow/c/experimental/saved_model/internal/concrete_function_type.h" + +extern "C" { + +size_t TF_ConcreteFunctionListNumOutputs(TF_ConcreteFunctionList* list) { + return tensorflow::unwrap(list)->size(); +} + +TF_ConcreteFunction* TF_ConcreteFunctionListGet(TF_ConcreteFunctionList* list, + int i) { + return tensorflow::wrap((*tensorflow::unwrap(list))[i]); +} + +} // end extern "C" diff --git a/tensorflow/c/experimental/saved_model/internal/concrete_function_list_type.h b/tensorflow/c/experimental/saved_model/internal/concrete_function_list_type.h new file mode 100644 index 00000000000..5a89d7a16d7 --- /dev/null +++ b/tensorflow/c/experimental/saved_model/internal/concrete_function_list_type.h @@ -0,0 +1,36 @@ +/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +==============================================================================*/ + +#ifndef TENSORFLOW_C_EXPERIMENTAL_SAVED_MODEL_INTERNAL_CONCRETE_FUNCTION_LIST_TYPE_H_ +#define TENSORFLOW_C_EXPERIMENTAL_SAVED_MODEL_INTERNAL_CONCRETE_FUNCTION_LIST_TYPE_H_ + +#include + +#include "tensorflow/c/experimental/saved_model/core/concrete_function.h" +#include "tensorflow/c/experimental/saved_model/internal/conversion_macros.h" + +// Internal structures used by the SavedModel C API. These are likely to change +// and should not be depended on. + +typedef struct TF_ConcreteFunctionList TF_ConcreteFunctionList; + +namespace tensorflow { + +DEFINE_CONVERSION_FUNCTIONS(std::vector, + TF_ConcreteFunctionList) + +} // namespace tensorflow + +#endif // TENSORFLOW_C_EXPERIMENTAL_SAVED_MODEL_INTERNAL_CONCRETE_FUNCTION_LIST_TYPE_H_ diff --git a/tensorflow/c/experimental/saved_model/internal/concrete_function_type.h b/tensorflow/c/experimental/saved_model/internal/concrete_function_type.h new file mode 100644 index 00000000000..37973373191 --- /dev/null +++ b/tensorflow/c/experimental/saved_model/internal/concrete_function_type.h @@ -0,0 +1,36 @@ +/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +==============================================================================*/ + +#ifndef TENSORFLOW_C_EXPERIMENTAL_SAVED_MODEL_INTERNAL_CONCRETE_FUNCTION_TYPE_H_ +#define TENSORFLOW_C_EXPERIMENTAL_SAVED_MODEL_INTERNAL_CONCRETE_FUNCTION_TYPE_H_ + +#include "tensorflow/c/experimental/saved_model/core/concrete_function.h" +#include "tensorflow/c/experimental/saved_model/internal/conversion_macros.h" + +// Internal structures used by the SavedModel C API. These are likely to change +// and should not be depended on. + +// It doesn't make sense to wrap tensorflow::ConcreteFunction* in a separate +// struct, since the lifetime of the struct and the raw pointer it wraps would +// be different. Therefore TF_ConcreteFunction* = tensorflow::ConcreteFunction*. +typedef struct TF_ConcreteFunction TF_ConcreteFunction; + +namespace tensorflow { + +DEFINE_CONVERSION_FUNCTIONS(tensorflow::ConcreteFunction, TF_ConcreteFunction) + +} // namespace tensorflow + +#endif // TENSORFLOW_C_EXPERIMENTAL_SAVED_MODEL_INTERNAL_CONCRETE_FUNCTION_TYPE_H_ diff --git a/tensorflow/c/experimental/saved_model/internal/conversion_macros.h b/tensorflow/c/experimental/saved_model/internal/conversion_macros.h new file mode 100644 index 00000000000..73875f02441 --- /dev/null +++ b/tensorflow/c/experimental/saved_model/internal/conversion_macros.h @@ -0,0 +1,28 @@ +/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +==============================================================================*/ + +#ifndef TENSORFLOW_C_EXPERIMENTAL_SAVED_MODEL_INTERNAL_CONVERSION_MACROS_H_ +#define TENSORFLOW_C_EXPERIMENTAL_SAVED_MODEL_INTERNAL_CONVERSION_MACROS_H_ + +#define DEFINE_CONVERSION_FUNCTIONS(cpp_impl, wrapper) \ + inline cpp_impl *unwrap(wrapper *w) { \ + return reinterpret_cast(w); \ + } \ + \ + inline wrapper *wrap(const cpp_impl *i) { \ + return reinterpret_cast(const_cast(i)); \ + } + +#endif // TENSORFLOW_C_EXPERIMENTAL_SAVED_MODEL_INTERNAL_CONVERSION_MACROS_H_ diff --git a/tensorflow/c/experimental/saved_model/internal/function_metadata.cc b/tensorflow/c/experimental/saved_model/internal/function_metadata.cc new file mode 100644 index 00000000000..4cf31e1abe1 --- /dev/null +++ b/tensorflow/c/experimental/saved_model/internal/function_metadata.cc @@ -0,0 +1,20 @@ +/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +==============================================================================*/ + +#include "tensorflow/c/experimental/saved_model/public/function_metadata.h" + +#include "tensorflow/c/experimental/saved_model/internal/function_metadata_type.h" + +// TODO(bmzhao): Add getter functions here as necessary. diff --git a/tensorflow/c/experimental/saved_model/internal/function_metadata_type.h b/tensorflow/c/experimental/saved_model/internal/function_metadata_type.h new file mode 100644 index 00000000000..ab89cf2d7be --- /dev/null +++ b/tensorflow/c/experimental/saved_model/internal/function_metadata_type.h @@ -0,0 +1,30 @@ +/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +==============================================================================*/ + +#ifndef TENSORFLOW_C_EXPERIMENTAL_SAVED_MODEL_INTERNAL_FUNCTION_METADATA_TYPE_H_ +#define TENSORFLOW_C_EXPERIMENTAL_SAVED_MODEL_INTERNAL_FUNCTION_METADATA_TYPE_H_ + +#include "tensorflow/c/experimental/saved_model/core/function_metadata.h" +#include "tensorflow/c/experimental/saved_model/internal/conversion_macros.h" + +typedef struct TF_FunctionMetadata TF_FunctionMetadata; + +namespace tensorflow { + +DEFINE_CONVERSION_FUNCTIONS(tensorflow::FunctionMetadata, TF_FunctionMetadata) + +} // namespace tensorflow + +#endif // TENSORFLOW_C_EXPERIMENTAL_SAVED_MODEL_INTERNAL_FUNCTION_METADATA_TYPE_H_ diff --git a/tensorflow/c/experimental/saved_model/internal/saved_model_api.cc b/tensorflow/c/experimental/saved_model/internal/saved_model_api.cc new file mode 100644 index 00000000000..711454a6a3b --- /dev/null +++ b/tensorflow/c/experimental/saved_model/internal/saved_model_api.cc @@ -0,0 +1,67 @@ +/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +==============================================================================*/ + +#include "tensorflow/c/experimental/saved_model/public/saved_model_api.h" + +#include "tensorflow/c/experimental/saved_model/core/saved_model_api.h" +#include "tensorflow/c/experimental/saved_model/internal/concrete_function_list_type.h" +#include "tensorflow/c/experimental/saved_model/internal/concrete_function_type.h" +#include "tensorflow/c/experimental/saved_model/internal/saved_model_api_type.h" +#include "tensorflow/c/tf_status.h" +#include "tensorflow/c/tf_status_internal.h" +#include "tensorflow/core/platform/status.h" + +extern "C" { + +TF_SavedModel* TF_LoadSavedModel(const char* dirname, TFE_Context* ctx, + const char* const* tags, int tags_len, + TF_Status* status) { + // TODO(bmzhao): Add a virtual "LoadSavedModel" method to + // AbstractContextInterface, and call it here. + return nullptr; +} + +void TF_DeleteSavedModel(TF_SavedModel* model) { delete model; } + +TF_ConcreteFunction* TF_GetSavedModelFunction(TF_SavedModel* model, + char* function_path, + TF_Status* status) { + tensorflow::ConcreteFunction* result = nullptr; + tensorflow::Status get_function_status = + model->saved_model->GetFunction(function_path, &result); + status->status.Update(get_function_status); + if (!get_function_status.ok()) { + return nullptr; + } + return tensorflow::wrap(result); +} + +TF_CAPI_EXPORT extern TF_ConcreteFunction* TF_GetSavedModelSignatureDefFunction( + TF_SavedModel* model, char* signature_def_key, TF_Status* status) { + tensorflow::ConcreteFunction* result = nullptr; + tensorflow::Status get_function_status = + model->saved_model->GetSignatureDefFunction(signature_def_key, &result); + status->status.Update(get_function_status); + if (!get_function_status.ok()) { + return nullptr; + } + return tensorflow::wrap(result); +} + +TF_ConcreteFunctionList* TF_ListSavedModelFunctions(TF_SavedModel* model) { + return tensorflow::wrap(&model->saved_model->ListFunctions()); +} + +} // end extern "C" diff --git a/tensorflow/c/experimental/saved_model/internal/saved_model_api_type.h b/tensorflow/c/experimental/saved_model/internal/saved_model_api_type.h new file mode 100644 index 00000000000..9e2d1117463 --- /dev/null +++ b/tensorflow/c/experimental/saved_model/internal/saved_model_api_type.h @@ -0,0 +1,30 @@ +/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +==============================================================================*/ + +#ifndef TENSORFLOW_C_EXPERIMENTAL_SAVED_MODEL_INTERNAL_SAVED_MODEL_API_TYPE_H_ +#define TENSORFLOW_C_EXPERIMENTAL_SAVED_MODEL_INTERNAL_SAVED_MODEL_API_TYPE_H_ + +#include + +#include "tensorflow/c/experimental/saved_model/core/saved_model_api.h" + +// Internal structures used by the SavedModel C API. These are likely to change +// and should not be depended on. + +struct TF_SavedModel { + std::unique_ptr saved_model; +}; + +#endif // TENSORFLOW_C_EXPERIMENTAL_SAVED_MODEL_INTERNAL_SAVED_MODEL_API_TYPE_H_ diff --git a/tensorflow/c/experimental/saved_model/public/BUILD b/tensorflow/c/experimental/saved_model/public/BUILD new file mode 100644 index 00000000000..af65e05e7f6 --- /dev/null +++ b/tensorflow/c/experimental/saved_model/public/BUILD @@ -0,0 +1,63 @@ +# Experimental SavedModel C APIs for TensorFlow. +# See RFC https://github.com/tensorflow/community/pull/207 +# All headers are on the public surface of Tensorflow's C API. +# Once moved out of experimental, these will be stable. +# The idea behind a separate public/ directory is to make apparent +# which headers are part of TF's public interface (and which headers) +# are implementation details. This structure allows us to also perform future +# programmatic checks that all "public" headers only include other "public" +# headers. + +package( + # This is intentionally public + default_visibility = [ + "//visibility:public", + ], + licenses = ["notice"], # Apache 2.0 +) + +# TODO(bmzhao): Remove these exports_files and rules, swap with cc_public_library instead. +# cc_public_library would allows us to separate the header dep graph from header+srcs dep graph. +exports_files( + [ + "concrete_function.h", + "concrete_function_list.h", + "function_metadata.h", + "saved_model_api.h", + ], + visibility = ["//tensorflow/c/experimental/saved_model/internal:__pkg__"], +) + +# The purpose of this header is to provide insulation against +# future changes where we rename/move a public header, without +# forcing all clients to change their "#includes". +cc_library( + name = "c_saved_model_api", + hdrs = ["c_saved_model_api.h"], + deps = [ + ":concrete_function", + ":concrete_function_list", + ":function_metadata", + ":saved_model_api", + ], +) + +alias( + name = "concrete_function", + actual = "//tensorflow/c/experimental/saved_model/internal:concrete_function", +) + +alias( + name = "concrete_function_list", + actual = "//tensorflow/c/experimental/saved_model/internal:concrete_function_list", +) + +alias( + name = "function_metadata", + actual = "//tensorflow/c/experimental/saved_model/internal:function_metadata", +) + +alias( + name = "saved_model_api", + actual = "//tensorflow/c/experimental/saved_model/internal:saved_model_api", +) diff --git a/tensorflow/c/experimental/saved_model/public/c_saved_model_api.h b/tensorflow/c/experimental/saved_model/public/c_saved_model_api.h new file mode 100644 index 00000000000..30f533f140a --- /dev/null +++ b/tensorflow/c/experimental/saved_model/public/c_saved_model_api.h @@ -0,0 +1,26 @@ +/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +==============================================================================*/ + +#ifndef TENSORFLOW_C_EXPERIMENTAL_SAVED_MODEL_PUBLIC_C_SAVED_MODEL_API_H_ +#define TENSORFLOW_C_EXPERIMENTAL_SAVED_MODEL_PUBLIC_C_SAVED_MODEL_API_H_ + +// IWYU pragma: begin_exports +#include "tensorflow/c/experimental/saved_model/public/concrete_function.h" +#include "tensorflow/c/experimental/saved_model/public/concrete_function_list.h" +#include "tensorflow/c/experimental/saved_model/public/function_metadata.h" +#include "tensorflow/c/experimental/saved_model/public/saved_model_api.h" +// IWYU pragma: end_exports + +#endif // TENSORFLOW_C_EXPERIMENTAL_SAVED_MODEL_PUBLIC_C_SAVED_MODEL_API_H_ diff --git a/tensorflow/c/experimental/saved_model/public/concrete_function.h b/tensorflow/c/experimental/saved_model/public/concrete_function.h new file mode 100644 index 00000000000..b4cd63c7a67 --- /dev/null +++ b/tensorflow/c/experimental/saved_model/public/concrete_function.h @@ -0,0 +1,53 @@ +/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +==============================================================================*/ + +#ifndef TENSORFLOW_C_EXPERIMENTAL_SAVED_MODEL_PUBLIC_CONCRETE_FUNCTION_H_ +#define TENSORFLOW_C_EXPERIMENTAL_SAVED_MODEL_PUBLIC_CONCRETE_FUNCTION_H_ + +#include "tensorflow/c/c_api_macros.h" +#include "tensorflow/c/eager/c_api_internal.h" +#include "tensorflow/c/eager/c_api_unified_experimental.h" +#include "tensorflow/c/experimental/saved_model/public/function_metadata.h" + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +// An opaque type that corresponds to a Function loaded from a SavedModel. +// TODO(bmzhao): Work together w/srbs@ to make sure this composes w/the +// C++ Unified Eager/Graph API's AbstractFunction +typedef struct TF_ConcreteFunction TF_ConcreteFunction; + +// Returns FunctionMetadata associated with `func`. Metadata's lifetime is +// bound to `func`, which is bound to the TF_SavedModel it was loaded from. +TF_CAPI_EXPORT extern TF_FunctionMetadata* TF_ConcreteFunctionGetMetadata( + TF_ConcreteFunction* func); + +// Returns a list of TensorHandles implicitly captured by this function. +TF_CAPI_EXPORT extern TF_OutputList* TF_ConcreteFunctionGetCaptures( + TF_ConcreteFunction* func); + +// Returns a TFE_Op suitable for executing this function. +TF_CAPI_EXPORT extern TFE_Op* TF_ConcreteFunctionGetOperation( + TF_ConcreteFunction* func); + +// Deletes `func`. +TF_CAPI_EXPORT extern void TF_DeleteConcreteFunction(TF_ConcreteFunction* func); + +#ifdef __cplusplus +} // end extern "C" +#endif // __cplusplus + +#endif // TENSORFLOW_C_EXPERIMENTAL_SAVED_MODEL_PUBLIC_CONCRETE_FUNCTION_H_ diff --git a/tensorflow/c/experimental/saved_model/public/concrete_function_list.h b/tensorflow/c/experimental/saved_model/public/concrete_function_list.h new file mode 100644 index 00000000000..ab5ed35d85a --- /dev/null +++ b/tensorflow/c/experimental/saved_model/public/concrete_function_list.h @@ -0,0 +1,35 @@ +/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +==============================================================================*/ + +#ifndef TENSORFLOW_C_EXPERIMENTAL_SAVED_MODEL_PUBLIC_CONCRETE_FUNCTION_LIST_H_ +#define TENSORFLOW_C_EXPERIMENTAL_SAVED_MODEL_PUBLIC_CONCRETE_FUNCTION_LIST_H_ + +#include + +#include "tensorflow/c/c_api_macros.h" +#include "tensorflow/c/experimental/saved_model/public/concrete_function.h" + +// An opaque type that is acts like a list of TF_ConcreteFunction pointers. +typedef struct TF_ConcreteFunctionList TF_ConcreteFunctionList; + +// Returns the size of `list`. +TF_CAPI_EXPORT size_t +TF_ConcreteFunctionListSize(TF_ConcreteFunctionList* list); + +// Returns the `i`th TF_ConcreteFunction in the list. +TF_CAPI_EXPORT TF_ConcreteFunction* TF_ConcreteFunctionListGet( + TF_ConcreteFunctionList* list, int i); + +#endif // TENSORFLOW_C_EXPERIMENTAL_SAVED_MODEL_PUBLIC_CONCRETE_FUNCTION_LIST_H_ diff --git a/tensorflow/c/experimental/saved_model/public/function_metadata.h b/tensorflow/c/experimental/saved_model/public/function_metadata.h new file mode 100644 index 00000000000..83ca3c73523 --- /dev/null +++ b/tensorflow/c/experimental/saved_model/public/function_metadata.h @@ -0,0 +1,35 @@ +/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +==============================================================================*/ + +#ifndef TENSORFLOW_C_EXPERIMENTAL_SAVED_MODEL_PUBLIC_FUNCTION_METADATA_H_ +#define TENSORFLOW_C_EXPERIMENTAL_SAVED_MODEL_PUBLIC_FUNCTION_METADATA_H_ + +#include "tensorflow/c/c_api_macros.h" + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +// An opaque type used to store any metadata associated with a function. +typedef struct TF_FunctionMetadata TF_FunctionMetadata; + +// TODO(bmzhao): Add getters for fields as we determine what metadata +// we want to expose. + +#ifdef __cplusplus +} // end extern "C" +#endif // __cplusplus + +#endif // TENSORFLOW_C_EXPERIMENTAL_SAVED_MODEL_PUBLIC_FUNCTION_METADATA_H_ diff --git a/tensorflow/c/experimental/saved_model/public/saved_model_api.h b/tensorflow/c/experimental/saved_model/public/saved_model_api.h new file mode 100644 index 00000000000..0610f8d24a9 --- /dev/null +++ b/tensorflow/c/experimental/saved_model/public/saved_model_api.h @@ -0,0 +1,96 @@ +/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +==============================================================================*/ + +#ifndef TENSORFLOW_C_EXPERIMENTAL_SAVED_MODEL_PUBLIC_SAVED_MODEL_API_H_ +#define TENSORFLOW_C_EXPERIMENTAL_SAVED_MODEL_PUBLIC_SAVED_MODEL_API_H_ + +#include "tensorflow/c/c_api_macros.h" +#include "tensorflow/c/experimental/saved_model/public/concrete_function.h" +#include "tensorflow/c/experimental/saved_model/public/concrete_function_list.h" +#include "tensorflow/c/tf_status.h" + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +// An opaque type representing a Tensorflow "SavedModel" +// (https://www.tensorflow.org/guide/saved_model) that we always pass by pointer +// to achieve ABI stability. +typedef struct TF_SavedModel TF_SavedModel; + +// Load a SavedModel from `dirname`. +// +// Params: +// dirname - A directory filepath that the SavedModel is at. +// ctx - A TFE_Context containing optional load/TF runtime options. +// `ctx` must outlive the returned TF_SavedModel pointer. +// tags - Pointer to char* array of SavedModel tags. Optional if the SavedModel +// contains a single Metagraph, as for those exported from +// `tf.saved_model.save`. +// tags_len - number of elements in the `tags` array. +// status - Set to OK on success and an appropriate error on failure. +// Returns: +// If status is not OK, returns nullptr. Otherwise, returns a newly created +// TF_SavedModel instance. It must be deleted by calling TF_DeleteSavedModel. +TF_CAPI_EXPORT extern TF_SavedModel* TF_LoadSavedModel(const char* dirname, + TFE_Context* ctx, + const char* const* tags, + int tags_len, + TF_Status* status); + +// Deletes a TF_SavedModel, and frees any resources owned by it. +TF_CAPI_EXPORT extern void TF_DeleteSavedModel(TF_SavedModel* model); + +// Retrieve a function from the TF2 SavedModel via function path. +// +// Params: +// model - The TF2 SavedModel to load a function from. +// function_path - A string containing the path from the root saved python +// object to a tf.function method. +// TODO(bmzhao): Add a detailed example of this with a +// python tf.module before moving this out of experimental. +// status - Set to OK on success and an appropriate error on failure. +// Returns: +// If status is not OK, returns nullptr. Otherwise, returns a +// TF_ConcreteFunction instance. The lifetime of this instance is +// "conceptually" bound to `model`. Once `model` is deleted, all +// `TF_ConcreteFunctions` retrieved from it are invalid, and have been deleted. +TF_CAPI_EXPORT extern TF_ConcreteFunction* TF_GetSavedModelFunction( + TF_SavedModel* model, char* function_path, TF_Status* status); + +// Retrieve a function from the TF SavedModel via a SignatureDef key. +// +// Params: +// model - The SavedModel to load a function from. +// signature_def_key - The string key of the SignatureDef map of a SavedModel: +// https://github.com/tensorflow/tensorflow/blob/69b08900b1e991d84bce31f3b404f5ed768f339f/tensorflow/core/protobuf/meta_graph.proto#L89 +// status - Set to OK on success and an appropriate error on failure. +// Returns: +// If status is not OK, returns nullptr. Otherwise, returns a +// TF_ConcreteFunction instance. Once `model` is deleted, all +// `TF_ConcreteFunctions` retrieved from it are invalid, and have been deleted. +TF_CAPI_EXPORT extern TF_ConcreteFunction* TF_GetSavedModelSignatureDefFunction( + TF_SavedModel* model, char* signature_def_key, TF_Status* status); + +// Returns a list of all ConcreteFunctions stored in this SavedModel. +// The lifetime of the returned list is bound to `model`. +TF_CAPI_EXPORT extern TF_ConcreteFunctionList* TF_ListSavedModelFunctions( + TF_SavedModel* model); + +#ifdef __cplusplus +} // end extern "C" +#endif // __cplusplus + +#endif // TENSORFLOW_C_EXPERIMENTAL_SAVED_MODEL_PUBLIC_SAVED_MODEL_API_H_