• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef TENSORFLOW_FRAMEWORK_UNIQUE_TENSOR_REFERENCES_H_
17 #define TENSORFLOW_FRAMEWORK_UNIQUE_TENSOR_REFERENCES_H_
18 
19 #include <unordered_set>
20 
21 #include "tensorflow/core/framework/tensor.h"
22 #include "tensorflow/core/framework/tensor_reference.h"
23 #include "tensorflow/core/lib/gtl/inlined_vector.h"
24 #include "tensorflow/core/platform/macros.h"
25 
26 namespace tensorflow {
27 
28 // Helper class to maintain a unique set of tensor references. In the
29 // common case there are not many references, so an inline vector is
30 // used for <= kInVector unique elements, defaulting to 4 since that
31 // is the inlined size of TensorReferenceVector. To avoid N^2
32 // operations when adding N items, any larger number of unique tensor
33 // references switches to using an unordered set.
34 class UniqueTensorReferences {
35  public:
UniqueTensorReferences()36   UniqueTensorReferences() : frozen_(false), referenced_tensors_set_(nullptr) {}
37 
38   ~UniqueTensorReferences();
39 
40   // Adds a reference to tensor if its buffer is not already referenced.
41   void Add(const Tensor& tensor);
42 
43   // No more references may be added after this is called. The unique
44   // references are returning in out_vector.
45   void FreezeAndReturnReferences(TensorReferenceVector* out_vector);
46 
47  private:
48   // Up to kInVector elements are stored in reference_tensors_vector_
49   // to avoid any allocations or hash computations in the common
50   // case. When more unique elements are added they move to
51   // referenced_tensors_set_ to avoid an N^2 algorithm on insert.
52   static const int kInVector = 4;  // Must be >= 1.
53 
54   struct TensorReferenceEqualFn {
operatorTensorReferenceEqualFn55     bool operator()(const TensorReference& t1,
56                     const TensorReference& t2) const {
57       return t1.SharesBufferWith(t2);
58     }
59   };
60 
61   struct TensorReferenceHashFn {
operatorTensorReferenceHashFn62     size_t operator()(const TensorReference& t) const { return t.BufferHash(); }
63   };
64 
65   bool frozen_;
66   TensorReferenceVector referenced_tensors_vector_;
67 
68   typedef std::unordered_set<TensorReference, TensorReferenceHashFn,
69                              TensorReferenceEqualFn>
70       ReferencedTensorsSet;
71   // Lazily allocated hash set for when the number of tensors becomes too large.
72   // If this is non-NULL, then we use the hash set, otherwise, we use the
73   // referenced_tensors_vector_ (and do O(N^2) work per insertion).
74   ReferencedTensorsSet* referenced_tensors_set_;
75 
76   TF_DISALLOW_COPY_AND_ASSIGN(UniqueTensorReferences);
77 };
78 
79 }  // end namespace tensorflow
80 
81 #endif  // TENSORFLOW_FRAMEWORK_UNIQUE_TENSOR_REFERENCES_H_
82