Home | History | Annotate | Download | only in common_runtime
      1 /* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
      2 
      3 Licensed under the Apache License, Version 2.0 (the "License");
      4 you may not use this file except in compliance with the License.
      5 You may obtain a copy of the License at
      6 
      7     http://www.apache.org/licenses/LICENSE-2.0
      8 
      9 Unless required by applicable law or agreed to in writing, software
     10 distributed under the License is distributed on an "AS IS" BASIS,
     11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 See the License for the specific language governing permissions and
     13 limitations under the License.
     14 ==============================================================================*/
     15 
     16 #include "tensorflow/core/framework/session_state.h"
     17 #include "tensorflow/core/graph/tensor_id.h"
     18 
     19 namespace tensorflow {
     20 
     21 const char* SessionState::kTensorHandleResourceTypeName = "TensorHandle";
     22 
     23 Status SessionState::GetTensor(const string& handle, Tensor* tensor) {
     24   mutex_lock l(state_lock_);
     25   auto it = tensors_.find(handle);
     26   if (it == tensors_.end()) {
     27     return errors::InvalidArgument("The tensor with handle '", handle,
     28                                    "' is not in the session store.");
     29   }
     30   *tensor = it->second;
     31   return Status::OK();
     32 }
     33 
     34 Status SessionState::AddTensor(const string& handle, const Tensor& tensor) {
     35   mutex_lock l(state_lock_);
     36   if (!tensors_.insert({handle, tensor}).second) {
     37     return errors::InvalidArgument("Failed to add a tensor with handle '",
     38                                    handle, "' to the session store.");
     39   }
     40   return Status::OK();
     41 }
     42 
     43 Status SessionState::DeleteTensor(const string& handle) {
     44   mutex_lock l(state_lock_);
     45   if (tensors_.erase(handle) == 0) {
     46     return errors::InvalidArgument("Failed to delete a tensor with handle '",
     47                                    handle, "' in the session store.");
     48   }
     49   return Status::OK();
     50 }
     51 
     52 int64 SessionState::GetNewId() {
     53   mutex_lock l(state_lock_);
     54   return tensor_id_++;
     55 }
     56 
     57 Status TensorStore::AddTensor(const string& name, const TensorAndKey& tk) {
     58   mutex_lock l(lock_);
     59   if (!tensors_.insert({name, tk}).second) {
     60     return errors::InvalidArgument("Failed to add a tensor with name '", name,
     61                                    "' to the tensor store.");
     62   }
     63   return Status::OK();
     64 }
     65 
     66 Status TensorStore::SaveTensors(const std::vector<string>& output_names,
     67                                 SessionState* session_state) {
     68   mutex_lock l(lock_);
     69   if (!tensors_.empty()) {
     70     // Save only the tensors in output_names in the session.
     71     for (const string& name : output_names) {
     72       TensorId id(ParseTensorName(name));
     73       const string& op_name = id.first.ToString();
     74       auto it = tensors_.find(op_name);
     75       if (it != tensors_.end()) {
     76         // Save the tensor to the session state.
     77         string key = it->second.GetHandle(op_name);
     78         TF_RETURN_IF_ERROR(session_state->AddTensor(key, it->second.tensor));
     79       }
     80     }
     81   }
     82   return Status::OK();
     83 }
     84 
     85 }  // namespace tensorflow
     86