• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef TENSORFLOW_FRAMEWORK_TENSOR_REFERENCE_H_
17 #define TENSORFLOW_FRAMEWORK_TENSOR_REFERENCE_H_
18 
19 #include "tensorflow/core/framework/tensor.h"
20 #include "tensorflow/core/lib/gtl/inlined_vector.h"
21 
22 namespace tensorflow {
23 
24 // An opaque class that holds a reference to an underlying TensorBuffer.
25 // Unlike Tensor, it does not have any shape or type information, so
26 // it is cheaper to construct/move, but the only thing you can really do
27 // with it is Unref it, which releases one of the references to the underlying
28 // TensorBuffer.
29 // IMPORTANT: If you do not call Unref(), you will likely leak tensor memory.
30 class TensorReference {
31  public:
32   // Take the reference of the root buffer so the size will be more accurate
33   explicit TensorReference(const Tensor& tensor);
34 
~TensorReference()35   ~TensorReference() {}
36 
Unref()37   void Unref() const {
38     if (buf_) buf_->Unref();
39   }
40 
41   // Return an estimate of the total bytes being kept alive by this reference.
TotalBytes()42   size_t TotalBytes() const {
43     // We add 128 as a baseline to account for per-Tensor metadata
44     return 128 + (buf_ ? buf_->size() : 0);
45   }
46 
FillDescription(AllocationDescription * description)47   void FillDescription(AllocationDescription* description) const {
48     if (buf_) buf_->FillAllocationDescription(description);
49   }
50 
51   // Convenience function for de-duplicating tensor references.
SharesBufferWith(const TensorReference & t)52   bool SharesBufferWith(const TensorReference& t) const {
53     return buf_ == t.buf_;
54   }
55 
56   // Convenience function for de-duplicating tensor references.
SharesBufferWith(const Tensor & t)57   bool SharesBufferWith(const Tensor& t) const {
58     return buf_ == (t.buf_ ? t.buf_->root_buffer() : nullptr);
59   }
60 
61   // Convenience function for de-duplicating tensor references.
BufferHash()62   size_t BufferHash() const { return std::hash<TensorBuffer*>()(buf_); }
63 
64   // A constructor used only for tests
TensorReference(TensorBuffer * test_buffer)65   explicit TensorReference(TensorBuffer* test_buffer) : buf_(test_buffer) {
66     if (buf_) buf_->Ref();
67   }
68 
69  private:
70   TensorBuffer* buf_;
71 };
72 
73 typedef gtl::InlinedVector<TensorReference, 4> TensorReferenceVector;
74 
75 }  // namespace tensorflow
76 
77 #endif  // TENSORFLOW_FRAMEWORK_TENSOR_REFERENCE_H_
78