An opaque class that holds a reference to an underlying TensorBuffer. Unlike Tensor, it does not have any shape or type information, so it is cheaper to construct/move, but the only thing you can really do with it is Unref it, which releases one of the references to the underlying TensorBuffer. IMPORTANT: If you do not call Unref(), you will likely leak tensor memory.
| 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 | |
| 35 | ~TensorReference() {} |
| 36 | |
| 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. |
| 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 | |
| 47 | void FillDescription(AllocationDescription* description) const { |
| 48 | if (buf_) buf_->FillAllocationDescription(description); |
| 49 | } |
| 50 | |
| 51 | // Convenience function for de-duplicating tensor references. |
| 52 | bool SharesBufferWith(const TensorReference& t) const { |
| 53 | return buf_ == t.buf_; |
| 54 | } |
| 55 | |
| 56 | // Convenience function for de-duplicating tensor references. |
| 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. |
| 62 | size_t BufferHash() const { return std::hash<TensorBuffer*>()(buf_); } |
| 63 | |
| 64 | // A constructor used only for tests |
| 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 |