Added an experimental C API to dump TF_Graph in a human-readable format, for
authorMingsheng Hong <hongm@google.com>
Thu, 22 Mar 2018 00:38:04 +0000 (17:38 -0700)
committerTensorFlower Gardener <gardener@tensorflow.org>
Thu, 22 Mar 2018 00:41:09 +0000 (17:41 -0700)
debugging purposes.

PiperOrigin-RevId: 189997099

tensorflow/c/c_api_experimental.cc
tensorflow/c/c_api_experimental.h

index eb17e16..34b9dec 100644 (file)
@@ -483,3 +483,13 @@ void TF_ShutdownTPUExecution(TF_Session* session, TF_Output shutdown_node,
                 /*targets*/ &shutdown_node.oper, /*ntargets*/ 1,
                 /*run_metadata*/ nullptr, status);
 }
+
+TF_CAPI_EXPORT extern const char* TF_GraphDebugString(TF_Graph* graph,
+                                                      size_t* len) {
+  tensorflow::mutex_lock c(graph->mu);
+  const auto& debug_str = graph->graph.ToGraphDefDebug().DebugString();
+  *len = debug_str.size();
+  char* ret = static_cast<char*>(malloc(*len + 1));
+  memcpy(ret, debug_str.c_str(), *len + 1);
+  return ret;
+}
index 2bad278..b95cdfe 100644 (file)
@@ -94,6 +94,12 @@ TF_CAPI_EXPORT extern void TF_ShutdownTPUExecution(TF_Session* session,
                                                    TF_Output shutdown_node,
                                                    TF_Status* status);
 
+// Returns the graph content in a human-readable format, with length set in
+// `len`. The format is subject to change in the future.
+// The returned string is heap-allocated, and caller should call free() on it.
+TF_CAPI_EXPORT extern const char* TF_GraphDebugString(TF_Graph* graph,
+                                                      size_t* len);
+
 #ifdef __cplusplus
 } /* end extern "C" */
 #endif