From 0c25f4b5c83683f1981ff761ead6a826e07a9700 Mon Sep 17 00:00:00 2001 From: Jon Malmaud Date: Tue, 24 Oct 2017 01:23:55 -0400 Subject: [PATCH 1/3] Define 'close(::Session)' --- src/core.jl | 18 ++++++++++++++++-- src/run.jl | 13 ++++++++++++- test/core.jl | 9 +++++++++ 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/core.jl b/src/core.jl index 76e9b9a1..fc76699e 100644 --- a/src/core.jl +++ b/src/core.jl @@ -554,8 +554,7 @@ mutable struct Session this = new(ptr, graph) check_status(status) finalizer(this, self->begin - status = Status() - @tfcall(:TF_DeleteSession, Void, (Ptr{Void}, Ptr{Void}), self.ptr, status.ptr) + close(self) end) return this end @@ -571,6 +570,21 @@ mutable struct Session end end +""" + `close(sess::Session)` + +Closes the TensorFlow session, freeing the associated computational resources. +""" +function Base.close(sess::Session) + if sess.ptr != C_NULL + status = Status() + @tfcall(:TF_DeleteSession, Void, (Ptr{Void}, Ptr{Void}), sess.ptr, status.ptr) + check_status(status) + sess.ptr = C_NULL + end + return nothing +end + mutable struct Buffer ptr::Ptr{Void} diff --git a/src/run.jl b/src/run.jl index c82f56b3..233d8718 100644 --- a/src/run.jl +++ b/src/run.jl @@ -77,9 +77,18 @@ function build_input(tensor_map::Dict) input_tensors, input_values end +struct ClosedSessionError <: Exception +end + +function Base.show(io::IO, err::ClosedSessionError) + print(io, "Tried to call 'run' with a closed TensorFlow session.") +end + function run(sess::Session, inputs, input_values, outputs, targets) #Low level run, without size checking, and type conversion etc. - + if sess.ptr == C_NULL + throw(ClosedSessionError()) + end status = Status() output_values = fill(C_NULL, length(outputs)) input_tensors = [RawTensor(x) for x in input_values] @@ -184,6 +193,8 @@ end """ + `run(sess::Session, output, input_dict::Dict)` + Compute the result of one of more operations in the computation graph. """ function run(sess::Session, output, input_dict) diff --git a/test/core.jl b/test/core.jl index 8bf438b8..ce885c93 100644 --- a/test/core.jl +++ b/test/core.jl @@ -26,6 +26,15 @@ end end end +@testset "Session closing" begin + session = tf.Session(Graph()) + x = constant(1) + @test run(session, x) == 1 + close(session) + close(session) # Test that we can safely call `close` twice on the same session + @test_throws tf.ClosedSessionError run(session, x) +end + @testset "get_operations" begin let graph = Graph() From d33888b4fb71f2587a3e58d85dd3a4cfbb21ff56 Mon Sep 17 00:00:00 2001 From: Jon Malmaud Date: Tue, 24 Oct 2017 01:28:28 -0400 Subject: [PATCH 2/3] Fix docstrings --- src/core.jl | 2 +- src/run.jl | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/core.jl b/src/core.jl index fc76699e..8a1de9db 100644 --- a/src/core.jl +++ b/src/core.jl @@ -571,7 +571,7 @@ mutable struct Session end """ - `close(sess::Session)` + close(sess::Session) Closes the TensorFlow session, freeing the associated computational resources. """ diff --git a/src/run.jl b/src/run.jl index 233d8718..51a7efe5 100644 --- a/src/run.jl +++ b/src/run.jl @@ -193,7 +193,8 @@ end """ - `run(sess::Session, output, input_dict::Dict)` + run(sess::Session, output, input_dict::Dict) + Compute the result of one of more operations in the computation graph. """ From f25ea1ee1fb985ba967e226720c4d3a4ff578d43 Mon Sep 17 00:00:00 2001 From: Jon Malmaud Date: Tue, 24 Oct 2017 15:45:36 -0400 Subject: [PATCH 3/3] Revise error message --- src/run.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/run.jl b/src/run.jl index 51a7efe5..6d679367 100644 --- a/src/run.jl +++ b/src/run.jl @@ -81,7 +81,7 @@ struct ClosedSessionError <: Exception end function Base.show(io::IO, err::ClosedSessionError) - print(io, "Tried to call 'run' with a closed TensorFlow session.") + print(io, "An operation was attempted on a closed TensorFlow session.") end function run(sess::Session, inputs, input_values, outputs, targets)