diff options
author | Asim Shankar <ashankar@google.com> | 2016-11-09 08:21:50 -0800 |
---|---|---|
committer | TensorFlower Gardener <gardener@tensorflow.org> | 2016-11-09 08:41:53 -0800 |
commit | e580e721cc1a205cb4b7afe64bc6af4d775a4851 (patch) | |
tree | 04338afa9d4f905c632d084dd964261eae9b8668 /tensorflow | |
parent | 988fec702bb0c23613cfd3ba76d04bf41c1a7c59 (diff) |
C API: Rename TF_SessionWithGraph to TF_Session.
This makes the preferred API for graph construction and
execution have the simpler, more sensible names (TF_Session,
TF_NewSession etc.) and the older one which we hope to
eventually remove have the more awkward ones (TF_DeprecatedSession,
TF_NewDeprecatedSession etc.)
Change: 138641615
Diffstat (limited to 'tensorflow')
-rw-r--r-- | tensorflow/c/c_api.cc | 30 | ||||
-rw-r--r-- | tensorflow/c/c_api.h | 75 | ||||
-rw-r--r-- | tensorflow/c/c_api_test.cc | 22 | ||||
-rw-r--r-- | tensorflow/g3doc/how_tos/language_bindings/index.md | 6 | ||||
-rw-r--r-- | tensorflow/go/session.go | 8 |
5 files changed, 64 insertions, 77 deletions
diff --git a/tensorflow/c/c_api.cc b/tensorflow/c/c_api.cc index f3eb98e431..ced231396a 100644 --- a/tensorflow/c/c_api.cc +++ b/tensorflow/c/c_api.cc @@ -683,8 +683,8 @@ struct TF_Graph { // TF_Graph may only / must be deleted when // num_sessions == 0 && delete_requested == true - // num_sessions incremented by TF_NewSessionWithGraph, and decremented by - // TF_DeleteSessionWithGraph. + // num_sessions incremented by TF_NewSession, and decremented by + // TF_DeleteSession. int num_sessions GUARDED_BY(mu); bool delete_requested GUARDED_BY(mu); // set true by TF_DeleteGraph }; @@ -703,8 +703,8 @@ struct TF_Operation { Node node; }; -struct TF_SessionWithGraph { - TF_SessionWithGraph(Session* s, TF_Graph* g) +struct TF_Session { + TF_Session(Session* s, TF_Graph* g) : session(s), graph(g), last_num_graph_nodes(0) {} Session* session; TF_Graph* graph; @@ -1610,11 +1610,10 @@ void TF_GraphImportGraphDef(TF_Graph* graph, const TF_Buffer* graph_def, } } -// TF_SessionWithGraph functions ---------------------------------------------- +// TF_Session functions ---------------------------------------------- -TF_SessionWithGraph* TF_NewSessionWithGraph(TF_Graph* graph, - const TF_SessionOptions* opt, - TF_Status* status) { +TF_Session* TF_NewSession(TF_Graph* graph, const TF_SessionOptions* opt, + TF_Status* status) { Session* session; status->status = NewSession(opt->options, &session); if (status->status.ok()) { @@ -1622,18 +1621,18 @@ TF_SessionWithGraph* TF_NewSessionWithGraph(TF_Graph* graph, mutex_lock l(graph->mu); graph->num_sessions += 1; } - return new TF_SessionWithGraph(session, graph); + return new TF_Session(session, graph); } else { DCHECK_EQ(nullptr, session); return NULL; } } -void TF_CloseSessionWithGraph(TF_SessionWithGraph* s, TF_Status* status) { +void TF_CloseSession(TF_Session* s, TF_Status* status) { status->status = s->session->Close(); } -void TF_DeleteSessionWithGraph(TF_SessionWithGraph* s, TF_Status* status) { +void TF_DeleteSession(TF_Session* s, TF_Status* status) { status->status = Status::OK(); TF_Graph* const graph = s->graph; if (graph != nullptr) { @@ -1650,8 +1649,7 @@ void TF_DeleteSessionWithGraph(TF_SessionWithGraph* s, TF_Status* status) { // TODO(josh11b,mrry): Change Session to be able to use a Graph* // directly, instead of requiring us to serialize to a GraphDef and // call Session::Extend(). -static bool ExtendSessionGraphHelper(TF_SessionWithGraph* session, - TF_Status* status) { +static bool ExtendSessionGraphHelper(TF_Session* session, TF_Status* status) { if (session->graph != nullptr) { mutex_lock session_lock(session->mu); session->graph->mu.lock(); @@ -1687,7 +1685,7 @@ static bool ExtendSessionGraphHelper(TF_SessionWithGraph* session, return true; } -void TF_SessionRun(TF_SessionWithGraph* session, const TF_Buffer* run_options, +void TF_SessionRun(TF_Session* session, const TF_Buffer* run_options, const TF_Port* inputs, TF_Tensor* const* input_values, int ninputs, const TF_Port* outputs, TF_Tensor** output_values, int noutputs, @@ -1730,7 +1728,7 @@ void TF_SessionRun(TF_SessionWithGraph* session, const TF_Buffer* run_options, status); } -void TF_SessionPRunSetup(TF_SessionWithGraph* session, const TF_Port* inputs, +void TF_SessionPRunSetup(TF_Session* session, const TF_Port* inputs, int ninputs, const TF_Port* outputs, int noutputs, const TF_Operation* const* target_opers, int ntargets, const char** handle, TF_Status* status) { @@ -1763,7 +1761,7 @@ void TF_SessionPRunSetup(TF_SessionWithGraph* session, const TF_Port* inputs, } } -void TF_SessionPRun(TF_SessionWithGraph* session, const char* handle, +void TF_SessionPRun(TF_Session* session, const char* handle, const TF_Port* inputs, TF_Tensor* const* input_values, int ninputs, const TF_Port* outputs, TF_Tensor** output_values, int noutputs, diff --git a/tensorflow/c/c_api.h b/tensorflow/c/c_api.h index 5eaff52fdf..544ff5a2ee 100644 --- a/tensorflow/c/c_api.h +++ b/tensorflow/c/c_api.h @@ -264,7 +264,7 @@ typedef struct TF_Graph TF_Graph; extern TF_Graph* TF_NewGraph(); // Destroy an options object. Graph will be deleted once no more -// TFSessionWithGraph's are referencing it. +// TFSession's are referencing it. extern void TF_DeleteGraph(TF_Graph*); // Operation being built. The underlying graph must outlive this. @@ -781,32 +781,31 @@ extern void TF_OperationToNodeDef(TF_Operation* oper, // TODO(yuanbyu): Add while loop to graph. // -------------------------------------------------------------------------- -// The new session API that uses TF_Graph*. The intent is this will -// replace the TF_ExtendGraph() API. - -// TODO(ashankar,josh11b): Rename this to TF_Session before v1.0. -typedef struct TF_SessionWithGraph TF_SessionWithGraph; - -// Return a new execution session with the associated graph, or NULL -// on error. *graph must be a valid graph (not deleted or nullptr). -// This function will prevent the graph from being deleted until -// TF_DeleteSessionWithGraph() is called. Does not take ownership of opts. -// TODO(josh11b): Rename this TF_NewSession() once we delete the old API. -extern TF_SessionWithGraph* TF_NewSessionWithGraph( - TF_Graph* graph, const TF_SessionOptions* opts, TF_Status* status); - -// Close a session. This contacts any other processes associated with this -// session, if applicable. This may not be called after -// TF_DeleteSessionWithGraph(). -// TODO(josh11b): Rename this TF_CloseSession() once we delete the old API. -extern void TF_CloseSessionWithGraph(TF_SessionWithGraph*, TF_Status* status); - -// Destroy a session object. Even if error information is recorded in -// *status, this call discards all local resources associated with the -// session. The session may not be used during or after this call -// (and the session drops its reference to the corresponding graph). -// TODO(josh11b): Rename this TF_DeleteSession() once we delete the old API. -extern void TF_DeleteSessionWithGraph(TF_SessionWithGraph*, TF_Status* status); +// API for driving Graph execution. + +typedef struct TF_Session TF_Session; + +// Return a new execution session with the associated graph, or NULL on error. +// +// *graph must be a valid graph (not deleted or nullptr). This function will +// prevent the graph from being deleted until TF_DeleteSession() is called. +// Does not take ownership of opts. +extern TF_Session* TF_NewSession(TF_Graph* graph, const TF_SessionOptions* opts, + TF_Status* status); + +// Close a session. +// +// Contacts any other processes associated with the session, if applicable. +// May not be called after TF_DeleteSession(). +extern void TF_CloseSession(TF_Session*, TF_Status* status); + +// Destroy a session object. +// +// Even if error information is recorded in *status, this call discards all +// local resources associated with the session. The session may not be used +// during or after this call (and the session drops its reference to the +// corresponding graph). +extern void TF_DeleteSession(TF_Session*, TF_Status* status); // Run the graph associated with the session starting with the supplied inputs // (inputs[0,ninputs-1] with corresponding values in input_values[0,ninputs-1])/ @@ -832,7 +831,7 @@ extern void TF_DeleteSessionWithGraph(TF_SessionWithGraph*, TF_Status* status); // to the caller, which must eventually call TF_DeleteTensor on them. // // On failure, output_values[] contains NULLs. -extern void TF_SessionRun(TF_SessionWithGraph* session, +extern void TF_SessionRun(TF_Session* session, // RunOptions const TF_Buffer* run_options, // Input tensors @@ -856,7 +855,7 @@ extern void TF_SessionRun(TF_SessionWithGraph* session, // On failure, out_status contains a tensorflow::Status with an error // message. // NOTE: This is EXPERIMENTAL and subject to change. -extern void TF_SessionPRunSetup(TF_SessionWithGraph*, +extern void TF_SessionPRunSetup(TF_Session*, // Input names const TF_Port* inputs, int ninputs, // Output names @@ -872,7 +871,7 @@ extern void TF_SessionPRunSetup(TF_SessionWithGraph*, // Continue to run the graph with additional feeds and fetches. The // execution state is uniquely identified by the handle. // NOTE: This is EXPERIMENTAL and subject to change. -extern void TF_SessionPRun(TF_SessionWithGraph*, const char* handle, +extern void TF_SessionPRun(TF_Session*, const char* handle, // Input tensors const TF_Port* inputs, TF_Tensor* const* input_values, int ninputs, @@ -887,30 +886,22 @@ extern void TF_SessionPRun(TF_SessionWithGraph*, const char* handle, // -------------------------------------------------------------------------- // The deprecated session API. Please switch to the above instead of -// TF_ExtendGraph(). TF_DeprecatedSession manages a single graph and execution. +// TF_ExtendGraph(). This deprecated API can be removed at any time without +// notice. typedef struct TF_DeprecatedSession TF_DeprecatedSession; -// Return a new execution session, or NULL on error. extern TF_DeprecatedSession* TF_NewDeprecatedSession(const TF_SessionOptions*, TF_Status* status); - -// Close a session. extern void TF_CloseDeprecatedSession(TF_DeprecatedSession*, TF_Status* status); - -// Destroy a session. Even if error information is recorded in *status, -// this call discards all resources associated with the session. extern void TF_DeleteDeprecatedSession(TF_DeprecatedSession*, TF_Status* status); - -// Closes all existing sessions connected to the `target` specified in the -// `SessionOptions`, and frees shared resources in `containers` on `target'. -// If no containers are provided, all containers are cleared. extern void TF_Reset(const TF_SessionOptions* opt, const char** containers, int ncontainers, TF_Status* status); - // Treat the bytes proto[0,proto_len-1] as a serialized GraphDef and // add the nodes in that GraphDef to the graph for the session. +// +// Prefer use of TF_Session and TF_GraphImportGraphDef over this. extern void TF_ExtendGraph(TF_DeprecatedSession*, const void* proto, size_t proto_len, TF_Status*); diff --git a/tensorflow/c/c_api_test.cc b/tensorflow/c/c_api_test.cc index dec891fdc6..27a5a0991b 100644 --- a/tensorflow/c/c_api_test.cc +++ b/tensorflow/c/c_api_test.cc @@ -154,7 +154,7 @@ TEST(CAPI, SessionOptions) { TF_DeleteSessionOptions(opt); } -TEST(CAPI, SessionWithRunMetadata) { +TEST(CAPI, DeprecatedSession) { TF_Status* s = TF_NewStatus(); TF_SessionOptions* opt = TF_NewSessionOptions(); TF_DeprecatedSession* session = TF_NewDeprecatedSession(opt, s); @@ -687,15 +687,15 @@ TEST(CAPI, ImportGraphDef) { TF_DeleteStatus(s); } -class CSessionWithGraph { +class CSession { public: - CSessionWithGraph(TF_Graph* graph, TF_Status* s) { + CSession(TF_Graph* graph, TF_Status* s) { TF_SessionOptions* opts = TF_NewSessionOptions(); - session_ = TF_NewSessionWithGraph(graph, opts, s); + session_ = TF_NewSession(graph, opts, s); TF_DeleteSessionOptions(opts); } - ~CSessionWithGraph() { + ~CSession() { TF_Status* s = TF_NewStatus(); CloseAndDelete(s); EXPECT_EQ(TF_OK, TF_GetCode(s)) << TF_Message(s); @@ -758,9 +758,9 @@ class CSessionWithGraph { DeleteInputValues(); ResetOutputValues(); if (session_ != nullptr) { - TF_CloseSessionWithGraph(session_, s); + TF_CloseSession(session_, s); EXPECT_EQ(TF_OK, TF_GetCode(s)) << TF_Message(s); - TF_DeleteSessionWithGraph(session_, s); + TF_DeleteSession(session_, s); session_ = nullptr; } } @@ -782,7 +782,7 @@ class CSessionWithGraph { output_values_.clear(); } - TF_SessionWithGraph* session_; + TF_Session* session_; std::vector<TF_Port> inputs_; std::vector<TF_Tensor*> input_values_; std::vector<TF_Port> outputs_; @@ -790,7 +790,7 @@ class CSessionWithGraph { std::vector<TF_Operation*> targets_; }; -TEST(CAPI, SessionWithGraph) { +TEST(CAPI, Session) { TF_Status* s = TF_NewStatus(); TF_Graph* graph = TF_NewGraph(); @@ -807,7 +807,7 @@ TEST(CAPI, SessionWithGraph) { ASSERT_EQ(TF_OK, TF_GetCode(s)) << TF_Message(s); // Create a session for this graph. - CSessionWithGraph csession(graph, s); + CSession csession(graph, s); ASSERT_EQ(TF_OK, TF_GetCode(s)) << TF_Message(s); // Run the graph. @@ -1346,6 +1346,6 @@ TEST_F(CApiAttributesTest, Errors) { // * TF_SetDevice(desc, "/job:worker"); // * control inputs / outputs // * targets -// * TF_DeleteGraph() before TF_DeleteSessionWithGraph() +// * TF_DeleteGraph() before TF_DeleteSession() } // namespace diff --git a/tensorflow/g3doc/how_tos/language_bindings/index.md b/tensorflow/g3doc/how_tos/language_bindings/index.md index 5c8290a493..5b8a8c8146 100644 --- a/tensorflow/g3doc/how_tos/language_bindings/index.md +++ b/tensorflow/g3doc/how_tos/language_bindings/index.md @@ -56,7 +56,7 @@ more functionality in the [C API] is an ongoing project. Feature | Python | C :--------------------------------------------- | :---------------------------------------------------------- | :-- -Run a predefined Graph | `tf.import_graph_def`, `tf.Session` | `TF_GraphImportGraphDef`, `TF_NewSessionWithGraph` +Run a predefined Graph | `tf.import_graph_def`, `tf.Session` | `TF_GraphImportGraphDef`, `TF_NewSession` Graph construction with generated op functions | Yes | Yes (The C API supports client languages that do this) Gradients | `tf.gradients` | Functions | `tf.python.framework.function.Defun` | @@ -88,9 +88,7 @@ A language binding is expected to define the following classes: - `Session`: Represents a client to a particular instance of the TensorFlow runtime. Its main job is to be constructed with a `Graph` and some options and then field calls to `Run()` the graph. Corresponds to a - `TF_SessionWithGraph` in the C API. (Note: `TF_SessionWithGraph` will - eventually be renamed `TF_Session` once the deprecated `TF_Session` is - removed.) + `TF_Session` in the C API. - `Tensor`: Represents an N-dimensional (rectangular) array with elements all the same `DataType`. Gets data into and out of a `Session`'s `Run()` call. Corresponds to a `TF_Tensor` in the C API. diff --git a/tensorflow/go/session.go b/tensorflow/go/session.go index 83dbbd8c04..430f69a8a4 100644 --- a/tensorflow/go/session.go +++ b/tensorflow/go/session.go @@ -34,7 +34,7 @@ import ( // perform the computation and potentially fetch outputs as Tensors. // A Session allows concurrent calls to Run(). type Session struct { - c *C.TF_SessionWithGraph + c *C.TF_Session // For ensuring that: // - Close() blocks on all Run() calls to complete. @@ -48,7 +48,7 @@ type Session struct { func NewSession(graph *Graph, options *SessionOptions) (*Session, error) { status := newStatus() cOpt := options.c() - cSess := C.TF_NewSessionWithGraph(graph.c, cOpt, status.c) + cSess := C.TF_NewSession(graph.c, cOpt, status.c) C.TF_DeleteSessionOptions(cOpt) if err := status.Err(); err != nil { return nil, err @@ -139,11 +139,11 @@ func (s *Session) Close() error { return nil } status := newStatus() - C.TF_CloseSessionWithGraph(s.c, status.c) + C.TF_CloseSession(s.c, status.c) if err := status.Err(); err != nil { return err } - C.TF_DeleteSessionWithGraph(s.c, status.c) + C.TF_DeleteSession(s.c, status.c) s.c = nil return status.Err() } |