pytorch  1.8.2
About: PyTorch provides Tensor computation (like NumPy) with strong GPU acceleration and Deep Neural Networks (in Python) built on a tape-based autograd system. LTS (Long Term Support) release.
  Fossies Dox: pytorch-1.8.2.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

blob.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstddef>
4#include <sstream>
5#include <type_traits>
6#include <typeinfo>
7#include <vector>
8
10#include <c10/util/typeid.h>
11#include <c10/macros/Macros.h>
12
13namespace caffe2 {
14
15class Tensor;
16
17/**
18 * @brief Blob is a general container that hosts a typed pointer.
19 *
20 * A Blob hosts a pointer as well as its type, and takes charge of deleting it
21 * properly when the blob is deallocated or re-allocated with a new type. A blob
22 * could contain anything, although the most common case is to contain a Tensor.
23 */
25 public:
26 /**
27 * Initializes an empty Blob.
28 */
29 Blob() noexcept : meta_(), pointer_(nullptr), has_ownership_(false) {}
31 Reset();
32 }
33
34 Blob(Blob&& other) noexcept : Blob() {
35 swap(other);
36 }
37
38 Blob& operator=(Blob&& other) noexcept {
39 Blob(std::move(other)).swap(*this);
40 return *this;
41 }
42
43 /**
44 * Checks if the content stored in the blob is of type T.
45 */
46 template <class T>
47 bool IsType() const noexcept {
48 return meta_.Match<T>();
49 }
50
51 /**
52 * Returns the meta info of the blob.
53 */
54 const TypeMeta meta() const noexcept {
55 return meta_;
56 }
57
58 /**
59 * Returns a printable typename of the blob.
60 */
62 return meta_.name();
63 }
64
65 /**
66 * @brief Gets the const reference of the stored object. The code checks if
67 * the stored object is of the desired type.
68 */
69 // TODO(jerryzh): add a Get(DeviceType) function?
70 template <class T>
71 const T& Get() const {
73 IsType<T>(),
74 "wrong type for the Blob instance. Blob contains ",
75 meta_.name(),
76 " while caller expects ",
77 TypeMeta::TypeName<T>());
78 // TODO: after we add Get<Tensor>(DeviceType)
79 // and changed all the callsites, we can add
80 // a static assert here to enforce T != Tensor
81 return *static_cast<const T*>(pointer_);
82 }
83
84 const void* GetRaw() const noexcept {
85 return pointer_;
86 }
87 void* GetRaw() noexcept {
88 return pointer_;
89 }
90
91 /**
92 * @brief Gets a mutable pointer to the stored object.
93 *
94 * If the current object is not of the right type, a new object is created
95 * and the old object is freed. Note that type T should have a default
96 * constructor. Otherwise, create the object yourself first, and use
97 * Reset().
98 */
99 template <class T>
101 static_assert(
103 "GetMutable can't be called with non-default-constructible types. "
104 "Try using specialized methods");
105 if (IsType<T>()) {
106 return static_cast<T*>(pointer_);
107 } else {
108 // TODO Re-enable logging
109 // VLOG(1) << "Create new mutable object " << TypeMeta::TypeName<T>();
110 return Reset<T>(new T());
111 }
112 }
113
114 template <class T>
116 if (IsType<T>()) {
117 return static_cast<T*>(pointer_);
118 } else {
119 return nullptr;
120 }
121 }
122
123 /**
124 * Sets the underlying object to the allocated one. The Blob then takes over
125 * the ownership of the passed in pointer. If there is already an object in
126 * the Blob, the old object is freed.
127 *
128 * This is used when the underlying class T does not have a default ctor, or
129 * complex initializations needs to be done outside the blob.
130 */
131 template <class T>
133 free_();
134 meta_ = TypeMeta::Make<T>();
135 pointer_ = static_cast<void*>(allocated);
136 has_ownership_ = true;
137 return allocated;
138 }
139
140 /**
141 * Sets the underlying object to the allocated one, but does not take over
142 * the ownership of the passed in pointer. If there is already an object in
143 * the Blob, the old object is freed.
144 *
145 * Unlike Reset, this does not take over the ownership of the pointer and the
146 * caller is responsible for making sure that the lifetime of the allocated
147 * blob outlasts the lifetime of any access to this blob, until another Reset
148 * call is made or the blob is destructed.
149 */
150 template <class T>
153 return static_cast<T*>(ShareExternal(
154 static_cast<void*>(allocated),
156 }
157
158 void* ShareExternal(void* allocated, const TypeMeta meta) {
159 free_();
160 meta_ = meta;
161 pointer_ = allocated;
162 has_ownership_ = false;
163 return allocated;
164 }
165
166 /**
167 * Resets the Blob to an empty one.
168 */
169 void Reset() {
170 free_();
171 pointer_ = nullptr;
172 meta_ = TypeMeta();
173 has_ownership_ = false;
174 }
175
176 /**
177 * @brief Swaps the underlying storage of two blobs.
178 */
179 void swap(Blob& rhs) {
180 using std::swap;
181 swap(meta_, rhs.meta_);
182 swap(pointer_, rhs.pointer_);
183 swap(has_ownership_, rhs.has_ownership_);
184 }
185
186 private:
187 void free_() {
188 if (has_ownership_ && pointer_ != nullptr) {
189 (*meta_.deleteFn())(pointer_);
190 }
191 }
192
194 void* pointer_;
196
198};
199
200inline void swap(Blob& lhs, Blob& rhs) {
201 lhs.swap(rhs);
202}
203
204inline std::ostream& operator<<(std::ostream& out, const Blob& v) {
205 return out << "Blob[" << v.TypeName() << "]";
206}
207
208} // namespace caffe2
bool allocated
#define TORCH_INTERNAL_ASSERT(cond,...)
Definition: Exception.h:290
#define TORCH_API
Definition: Export.h:98
VkAccessFlags value
Definition: Tensor.cpp:1156
Reimplementation of std::string_view for C++11.
Definition: string_view.h:25
intrusive_ptr<T> is an alternative to shared_ptr<T> that has better performance because it does the r...
Definition: intrusive_ptr.h:52
Blob is a general container that hosts a typed pointer.
Definition: blob.h:24
void free_()
Definition: blob.h:187
Blob & operator=(Blob &&other) noexcept
Definition: blob.h:38
const void * GetRaw() const noexcept
Definition: blob.h:84
void * ShareExternal(void *allocated, const TypeMeta meta)
Definition: blob.h:158
T * Reset(T *allocated)
Sets the underlying object to the allocated one.
Definition: blob.h:132
Blob(Blob &&other) noexcept
Definition: blob.h:34
const TypeMeta meta() const noexcept
Returns the meta info of the blob.
Definition: blob.h:54
std::remove_const< T >::type * ShareExternal(typename std::remove_const< T >::type *allocated)
Sets the underlying object to the allocated one, but does not take over the ownership of the passed i...
Definition: blob.h:151
T * GetMutableOrNull()
Definition: blob.h:115
c10::string_view TypeName() const noexcept
Returns a printable typename of the blob.
Definition: blob.h:61
bool IsType() const noexcept
Checks if the content stored in the blob is of type T.
Definition: blob.h:47
void swap(Blob &rhs)
Swaps the underlying storage of two blobs.
Definition: blob.h:179
void * pointer_
Definition: blob.h:194
const T & Get() const
Gets the const reference of the stored object.
Definition: blob.h:71
Blob() noexcept
Initializes an empty Blob.
Definition: blob.h:29
bool has_ownership_
Definition: blob.h:195
void Reset()
Resets the Blob to an empty one.
Definition: blob.h:169
T * GetMutable()
Gets a mutable pointer to the stored object.
Definition: blob.h:100
C10_DISABLE_COPY_AND_ASSIGN(Blob)
void * GetRaw() noexcept
Definition: blob.h:87
~Blob()
Definition: blob.h:30
TypeMeta meta_
Definition: blob.h:193
TypeMeta is a thin class that allows us to store the type of a container such as a blob,...
Definition: typeid.h:325
static TypeMeta Make()
Returns a TypeMeta object that corresponds to the typename T.
Definition: typeid.h:454
const Tensor & other
Definition: BinaryOps.cpp:17
constexpr remove_reference_t< T > && move(T &&t) noexcept
Definition: variant.h:418
Copyright (c) 2016-present, Facebook, Inc.
Definition: blob.h:13
float T
Definition: cc_bmm_bg_op.h:11
std::ostream & operator<<(std::ostream &out, const Blob &v)
Definition: blob.h:204
void swap(Blob &lhs, Blob &rhs)
Definition: blob.h:200
void swap(c10::SmallVectorImpl< T > &LHS, c10::SmallVectorImpl< T > &RHS)
Implement std::swap in terms of SmallVector swap.
Definition: SmallVector.h:1074
int32_t type