serizba / cppflow

Run TensorFlow models in C++ without installation and without Bazel
https://serizba.github.io/cppflow/
MIT License
787 stars 178 forks source link

cppflow2: Discussions of SavedModel API in TF2 #50

Closed ljn917 closed 2 years ago

ljn917 commented 4 years ago

This issue serves as a shared note to track the status of SavedModel C API in TF 2.0, and also for discussions on possible implementations in cppflow2.

The original RFC proposal is here. The loading functions are defined here

However, as of this writing, the new SavedModel C API in TF 2.0 is still work-in-progress. It is not enabled in bazel. To enable the API, the following patch is needed. In addition, headers need to be copied manually. Although this patch enables the experimental APIs in the libtensorflow, calling these APIs will throw unimplemented errors. (at least in v2.3) I saw commits were submitted to support the new SavedModel C API in v2.4, so some of them may work now.

diff --git a/tensorflow/BUILD b/tensorflow/BUILD
index d00608ccc9..8bab3a6e3e 100644
--- a/tensorflow/BUILD
+++ b/tensorflow/BUILD
@@ -746,6 +746,7 @@ tf_cc_shared_object(
         "//tensorflow/c:version_script.lds",
         "//tensorflow/c/eager:c_api",
         "//tensorflow/c/eager:c_api_experimental",
+        "//tensorflow/c/experimental/saved_model/public:c_saved_model_api",
         "//tensorflow/core:distributed_tensorflow_dependencies",
         "//tensorflow/core:tensorflow",
     ],

There seems to be two ways to call the model, ConcreteFunction (TF_GetSavedModelConcreteFunction) and SignatureDefFunction (TF_GetSavedModelSignatureDefFunction) defined in this header, but both of them return TFE_Op* (TF_SignatureDefFunctionMakeCallOp and TF_ConcreteFunctionMakeCallOp). They should be compatible with eager execution in cppflow2.

Additional notes

Edit:

Edit2: A WIP for the TF v2 SavedModel API in cppflow: https://github.com/ljn917/cppflow/tree/cppflow2-savedmodel

Known limitations:

bnascimento commented 3 years ago

Thanks @ljn917 @serizba I stumbled into this thread, after failing alot to use c/cc tensorflow 2.x APIs. Your work here gave me lots of good insights. Please keep it up