aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/core/common_runtime/session_state.cc
diff options
context:
space:
mode:
authorGravatar Yuan Yu <yuanbyu@google.com>2016-04-10 08:46:36 -0800
committerGravatar TensorFlower Gardener <gardener@tensorflow.org>2016-04-10 09:51:54 -0700
commit098f930de4ef044021f3ef1d3cdd6848c23eddb0 (patch)
tree107b20a63c2c1f4069a804af84489b38e9899478 /tensorflow/core/common_runtime/session_state.cc
parentcc9560e8f449060feeaa73f47eb41e4d77079573 (diff)
This is another step to make TensorFlow more interactive and flexible to users. It allows a tensor produced by a run call to stay "in-place" so that a future run call can use it in-place. To achieve this, a run call can now return a handle of a tensor to the client, which can then be fed to a subsequent run call. This feature is complimentary to partial run, though there are some overlaps.
Here are a few properties of the current implementation: 1. Tensors are stored in the state of a session. The tensors are garbage collected if the client doesn't have a reference to the tensor or the session is closed. 2. There is no change to the current session API. We introduced two ops to manage the conversions between tensors and its handles. (There is a third op to garbage collect a tensor.) See the example below. 3. It fits quite well into the current feed-fetch design/implementation. It tries to reuse the graph (and caches) as much as possible so to make things efficient. Below is a simple example. More examples can be found in sessopn_ops_test.py. # Return a handle. a = tf.constant(10) b = tf.constant(5) c = tf.mul(a, b) h = tf.get_session_handle(c).eval() # Feed a tensor handle. f, x = tf.get_session_tensor(dtypes.int32) y = tf.mul(x, 10) result = sess.run(y, feed_dict={f: h.handle}) # result == 500 Change: 119481352
Diffstat (limited to 'tensorflow/core/common_runtime/session_state.cc')
-rw-r--r--tensorflow/core/common_runtime/session_state.cc83
1 files changed, 83 insertions, 0 deletions
diff --git a/tensorflow/core/common_runtime/session_state.cc b/tensorflow/core/common_runtime/session_state.cc
new file mode 100644
index 0000000000..10e614cce5
--- /dev/null
+++ b/tensorflow/core/common_runtime/session_state.cc
@@ -0,0 +1,83 @@
+/* Copyright 2015 Google Inc. 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/core/framework/session_state.h"
+#include "tensorflow/core/graph/tensor_id.h"
+
+namespace tensorflow {
+
+Status SessionState::GetTensor(const string& handle, Tensor* tensor) {
+ mutex_lock l(state_lock_);
+ auto it = tensors_.find(handle);
+ if (it == tensors_.end()) {
+ return errors::InvalidArgument("The tensor with handle '", handle,
+ "' is not in the session store.");
+ }
+ *tensor = it->second;
+ return Status::OK();
+}
+
+Status SessionState::AddTensor(const string& handle, const Tensor& tensor) {
+ mutex_lock l(state_lock_);
+ if (!tensors_.insert({handle, tensor}).second) {
+ return errors::InvalidArgument("Failed to add a tensor with handle '",
+ handle, "' to the session store.");
+ }
+ return Status::OK();
+}
+
+Status SessionState::DeleteTensor(const string& handle) {
+ mutex_lock l(state_lock_);
+ if (tensors_.erase(handle) == 0) {
+ return errors::InvalidArgument("Failed to delete a tensor with handle '",
+ handle, "' in the session store.");
+ }
+ return Status::OK();
+}
+
+int64 SessionState::GetNewId() {
+ mutex_lock l(state_lock_);
+ return tensor_id_++;
+}
+
+Status TensorStore::AddTensor(const string& name, const TensorAndKey& tk) {
+ mutex_lock l(lock_);
+ if (!tensors_.insert({name, tk}).second) {
+ return errors::InvalidArgument("Failed to add a tensor with name '", name,
+ "' to the tensor store.");
+ }
+ return Status::OK();
+}
+
+Status TensorStore::SaveTensors(const std::vector<string>& output_names,
+ SessionState* session_state) {
+ mutex_lock l(lock_);
+ if (tensors_.size() != 0) {
+ // Save only the tensors in output_names in the session.
+ for (const string& name : output_names) {
+ TensorId id(ParseTensorName(name));
+ const string& op_name = id.first.ToString();
+ auto it = tensors_.find(op_name);
+ if (it != tensors_.end()) {
+ // Save the tensor to the session state.
+ string key = it->second.GetHandle(op_name);
+ TF_RETURN_IF_ERROR(session_state->AddTensor(key, it->second.tensor));
+ }
+ }
+ }
+ return Status::OK();
+}
+
+} // namespace tensorflow