1 /* Copyright 2016 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/unique_tensor_references.h" 17 18 namespace tensorflow { 19 ~UniqueTensorReferences()20UniqueTensorReferences::~UniqueTensorReferences() { 21 if (!frozen_) { 22 // The references were not retrieved so discard them to avoid 23 // leaking memory. 24 TensorReferenceVector refs; 25 FreezeAndReturnReferences(&refs); 26 for (auto& tensor : refs) { 27 tensor.Unref(); 28 } 29 } 30 delete referenced_tensors_set_; 31 } 32 Add(const Tensor & tensor)33void UniqueTensorReferences::Add(const Tensor& tensor) { 34 DCHECK(!frozen_); 35 // Do nothing if the tensor has a null buffer. 36 if (tensor.IsInitialized() && tensor.NumElements() > 0) { 37 if (referenced_tensors_set_ != nullptr) { 38 // There are enough tensors that we are using a hash set to 39 // de-duplicate. 40 const TensorReference tensor_ref(tensor); 41 if (!referenced_tensors_set_->insert(tensor_ref).second) { 42 // The tensor was a duplicate, so discard the reference. 43 tensor_ref.Unref(); 44 } 45 } else { 46 for (size_t i = 0; i < referenced_tensors_vector_.size(); ++i) { 47 if (referenced_tensors_vector_[i].SharesBufferWith(tensor)) { 48 // tensor is a duplicate, so nothing to do. 49 return; 50 } 51 } 52 referenced_tensors_vector_.push_back(TensorReference(tensor)); 53 if (kInVector == referenced_tensors_vector_.size()) { 54 // There are too many tensors to keep using the N^2 algorithm 55 // so start de-duplicating using a set. 56 // Transfer the refs from the vector to the set. 57 DCHECK(referenced_tensors_set_ == nullptr); 58 referenced_tensors_set_ = new ReferencedTensorsSet; 59 referenced_tensors_set_->reserve(kInVector); 60 referenced_tensors_set_->insert(referenced_tensors_vector_.begin(), 61 referenced_tensors_vector_.end()); 62 DCHECK_EQ(kInVector, referenced_tensors_set_->size()); 63 referenced_tensors_vector_.clear(); 64 } 65 } 66 } 67 } 68 FreezeAndReturnReferences(TensorReferenceVector * out_vector)69void UniqueTensorReferences::FreezeAndReturnReferences( 70 TensorReferenceVector* out_vector) { 71 // Prevent any further additions. 72 frozen_ = true; 73 if (referenced_tensors_set_ != nullptr) { 74 DCHECK(referenced_tensors_vector_.empty()); 75 out_vector->reserve(referenced_tensors_set_->size()); 76 for (const auto& ref : *referenced_tensors_set_) { 77 out_vector->push_back(ref); 78 } 79 referenced_tensors_set_->clear(); 80 delete referenced_tensors_set_; 81 referenced_tensors_set_ = nullptr; 82 } else { 83 out_vector->reserve(referenced_tensors_vector_.size()); 84 for (const auto& ref : referenced_tensors_vector_) { 85 out_vector->push_back(ref); 86 } 87 referenced_tensors_vector_.clear(); 88 } 89 } 90 91 } // namespace tensorflow 92