ie_blob.h
Go to the documentation of this file.
1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4 
5 /**
6  * @brief A header file for Blob and generic TBlob<>
7  * @file ie_blob.h
8  */
9 #pragma once
10 
11 #include <memory>
12 #include <vector>
13 #include <string>
14 #include <numeric>
15 #include <cstring>
16 #include <utility>
17 #include <functional>
18 #include <map>
19 
20 #include "ie_common.h"
21 #include "details/ie_exception.hpp"
23 #include "ie_allocator.hpp"
24 #include "ie_locked_memory.hpp"
25 #include "ie_precision.hpp"
26 #include "ie_layouts.h"
28 
29 namespace InferenceEngine {
30 /**
31  * @brief This class implements a container object that represents a tensor in memory (host and remote/accelerated)
32  */
33 class Blob {
34 public:
35  /**
36  * @brief A smart pointer containing Blob object
37  */
38  using Ptr = std::shared_ptr<Blob>;
39 
40  /**
41  * @brief A smart pointer to the const Blob object
42  */
43  using CPtr = std::shared_ptr<const Blob>;
44 
45  /**
46  * @deprecated Please use TensorDesc to get the precision
47  * @brief Returns the tensor precision of the current Blob object
48  */
49  Precision type() const noexcept {
50  return tensorDesc.getPrecision();
51  }
52 
53  /**
54  * @deprecated Please use TensorDesc to get the precision
55  * @brief Returns the tensor precision of the current Blob object
56  */
57  Precision precision() const noexcept {
58  return tensorDesc.getPrecision();
59  }
60 
61  /**
62  * @deprecated Please use TensorDesc to get the current layout
63  * @brief Returns the tensor layout of the current Blob object
64  */
65  Layout layout() const noexcept {
66  return tensorDesc.getLayout();
67  }
68 
69  /**
70  * @brief Creates a TBlob<> object from a Data node
71  * @param Data reference to a smart pointer of the Data node
72  * @return Smart pointer to TBlob<> with the relevant C type to the precision of the data node
73  */
74  static Ptr CreateFromData(const DataPtr &data);
75 
76  /**
77  * @brief Blob virtual destructor
78  */
79  virtual ~Blob() = default;
80 
81  /**
82  * @brief Constructor. Creates an empty Blob object with the specified precision.
83  * @param tensorDesc Defines the layout and dims of the blob
84  */
85  explicit Blob(TensorDesc tensorDesc): tensorDesc(tensorDesc) {}
86 
87  /**
88  * @deprecated Please use TensorDesc for Blob initialization
89  * @brief Constructor. Creates an empty Blob object with the specified precision.
90  * @param p Precision type
91  */
92  explicit Blob(Precision p) : Blob(p, NCHW) {}
93 
94  /**
95  * @deprecated Please use TensorDesc for Blob initialization
96  * @brief The constructor creates an empty Blob object with the specified precision and layout.
97  * @param p Precision type
98  * @param l Layout
99  */
100  Blob(Precision p, Layout l) : tensorDesc(p, l) {}
101 
102  /**
103  * @deprecated Please use TensorDesc for Blob initialization
104  * @brief The constructor creates an empty Blob object with the specified precision and dimensions.
105  * @param p Tensor precision type
106  * @param dims Tensor dimensions vector
107  */
109  : Blob(p, TensorDesc::getLayoutByDims(dims), dims) {}
110 
111  /**
112  * @deprecated Please use TensorDesc for Blob initialization
113  * @brief The constructor creates an empty Blob object with the specified precision, layout and dimensions.
114  * @param p tensor precision type
115  * @param l tensor layout
116  * @param dims Tensor dimensions vector with reversed order
117  */
119  : tensorDesc(p, SizeVector(dims.rbegin(), dims.rend()), l) {}
120 
121  /**
122  * @deprecated It works with reversed dimensions. Please create a new blob if you want to change a size.
123  * @brief Changes Tensor size to the specified dimensions. If it was allocated, the previous data is deallocated and lost.
124  * @param dims New dimensions to set
125  * @param layout New layout to set
126  * @return Total number of elements (a product of all the dimensions)
127  */
128  size_t Resize(const SizeVector &dims, Layout layout = Layout::ANY) noexcept {
129  bool bret = deallocate();
130 
131  if (layout != Layout::ANY) {
132  tensorDesc = TensorDesc(tensorDesc.getPrecision(), SizeVector(dims.rbegin(), dims.rend()), layout);
133  } else {
134  tensorDesc.setDims(SizeVector(dims.rbegin(), dims.rend()));
135  }
136  if (!bret) {
137  allocate();
138  }
139  return product(tensorDesc.getDims());
140  }
141 
142  /**
143  * @deprecated It works with reversed dimensions. Please use TensorDescriptor.reshape().
144  * @brief Changes tensor size to the specified dimensions without changing memory. The total size remains unchanged as well as the memory layout.
145  * @param dims New dimensions to set
146  * @param layout New layout to set
147  * @return The total number of elements (a product of all the dims)
148  */
149  size_t Reshape(const SizeVector &dims, Layout layout = Layout::ANY) noexcept {
150  if (product(tensorDesc.getDims()) != product(dims)) {
151  return 0;
152  }
153 
154  if (layout != Layout::ANY) {
155  tensorDesc = TensorDesc(tensorDesc.getPrecision(), SizeVector(dims.rbegin(), dims.rend()), layout);
156  } else {
157  tensorDesc.setDims(SizeVector(dims.rbegin(), dims.rend()));
158  }
159  return product(tensorDesc.getDims());
160  }
161 
162  /**
163  * @deprecated Please use TensorDesc for working with dimensions.
164  * @brief Returns the tensor dimensions vector with reversed order.
165  */
166  const SizeVector dims() const noexcept {
167  return SizeVector(tensorDesc.getDims().rbegin(), tensorDesc.getDims().rend());
168  }
169 
170  /**
171  * @brief Returns the tensor description
172  */
173  const TensorDesc &getTensorDesc() const noexcept {
174  return tensorDesc;
175  }
176 
177  /**
178  * @brief Returns the total number of elements (a product of all the dims)
179  */
180  size_t size() const noexcept {
181  return product(tensorDesc.getDims());
182  }
183 
184  /**
185  * @brief Returns the size of the current Blob in bytes.
186  */
187  size_t byteSize() const noexcept {
188  return product(tensorDesc.getDims()) * element_size();
189  }
190 
191  /**
192  * @brief Returns the number of bytes per element. The overall Blob capacity is size() * element_size().
193  * Abstract method.
194  */
195  virtual size_t element_size() const noexcept = 0;
196 
197  /**
198  * @brief Allocates memory to store the data.
199  * Abstract method.
200  */
201  virtual void allocate() noexcept = 0;
202 
203  /**
204  * @brief Releases previously allocated data.
205  * Abstract method.
206  */
207  virtual bool deallocate() noexcept = 0;
208 
209  /**
210  * @brief Gets access to the allocated memory.
211  * Abstract method.
212  * @return A LockedMemory object
213  */
214  virtual LockedMemory<void> buffer() noexcept = 0;
215 
216  /**
217  * @brief Gets read-only access to the allocated memory.
218  * Abstract method.
219  * @return A LockedMemory object
220  */
221  virtual LockedMemory<const void> cbuffer() const noexcept = 0;
222 
223 protected:
224  /**
225  * @brief The tensor descriptor of the given blob.
226  */
228 
229  /**
230  * @brief Multiplies the dimension vector's values.
231  * @param dims Reference to a vector with dimension values of type size_t
232  * @return Result of multiplication
233  */
234  static size_t product(const SizeVector &dims) noexcept {
235  if (dims.empty())
236  return 0;
237  return std::accumulate(std::begin(dims), std::end(dims), (size_t) 1, std::multiplies<size_t>());
238  }
239 
240  /**
241  * @brief Gets the allocator for allocator-based blobs.
242  * @return The allocator for allocator-based blobs or if there is none then a nullptr.
243  */
244  virtual const std::shared_ptr<IAllocator> &getAllocator() const noexcept = 0;
245 
246  /**
247  * @brief Gets the handle to allocated memory.
248  * @return The handle to allocated memory for allocator-based blobs or if there is none then a nullptr.
249  */
250  virtual void *getHandle() const noexcept = 0;
251 
252  template<typename> friend
253  class TBlobProxy;
254 };
255 
256 /**
257  * @brief This is a convenient type for working with a map containing pairs(string, pointer to a Blob instance).
258  */
259 using BlobMap = std::map<std::string, Blob::Ptr>;
260 
261 /**
262  * @brief Represents real host memory allocated for a Tensor/Blob per C type.
263  */
264 template<typename T,
265  typename = std::enable_if<std::is_pod<T>::value>>
266 class TBlob : public Blob {
267  template<typename, typename> friend
268  class TBlob;
269 
270 
271 public:
272  /**
273  * @brief Smart Pointer to this TBlob object.
274  */
275  using Ptr = std::shared_ptr<TBlob<T>>;
276 
277  /**
278  * @brief Creates a TBlob object with the specified dimensions and layout but does not allocate the memory.
279  * Please use the allocate() method to allocate memory.
280  * @param tensorDesc Tensor description
281  */
282  explicit TBlob(const TensorDesc& tensorDesc): Blob(tensorDesc) {}
283 
284  /**
285  * @brief The constructor creates a TBlob object with the specified dimensions and layout
286  * on the pre-allocated memory. The allocate() call is not required.
287  * @param tensorDesc Tensor description
288  * @param ptr Pointer to the pre-allocated memory
289  * @param data_size Length of the pre-allocated array. If not set, size is assumed equal
290  * to the dot product of dims.
291  */
292  TBlob(const TensorDesc& tensorDesc, T* ptr, size_t data_size = 0): Blob(tensorDesc) {
293  if (data_size == 0) {
294  data_size = size();
295  }
296 
297  if (data_size != 0 && ptr == nullptr) {
298  THROW_IE_EXCEPTION << "Using Blob on external nullptr memory";
299  }
300 
301  _allocator = details::make_pre_allocator(ptr, data_size);
302  // blob on attached memory is always allocated, so we are not forcing the user to call allocate()
303  allocate();
304  }
305 
306  /**
307  * @brief Creates a TBlob object with the specified dimensions, layout and custom memory allocator but does not allocate the memory.
308  * @param p Precision
309  * @param l Layout
310  * @param dims Tensor dimensions
311  * @param alloc Allocator to be used
312  */
313  TBlob(const TensorDesc& tensorDesc, const std::shared_ptr<IAllocator>& alloc)
314  : Blob(tensorDesc), _allocator(alloc) {
315  }
316 
317  /**
318  * @deprecated Please use TensorDesc for Blob initialization.
319  */
320  explicit TBlob(Precision p, Layout l) : Blob(p, l) {}
321 
322  /**
323  * @deprecated Please use TensorDesc for Blob initialization.
324  * @brief Creates a TBlob object with the specified dimensions but does not allocate the memory. Please use the allocate() method to allocate memory.
325  * @param p Precision
326  * @param l Layout
327  * @param dims Tensor dimensions
328  */
329  TBlob(Precision p, Layout l, const SizeVector& dims)
330  : Blob(p, l, dims) {
331  }
332 
333  /**
334  * @deprecated Please use TensorDesc for Blob initialization.
335  * @brief The constructor creates a TBlob object with the specified dimensions on the pre-allocated memory. Therefore, the allocate() call is not required.
336  * @details The TBlob object doesn't own memory that is pointed to by the ptr. Therefore, it doesn't free the memory after the TBlob object is destroyed.
337  * Also certain operations might fail:
338  * After the blob is constructed with this pointer its size is stored in the TBlob instance.
339  * If the resize() operation happens which requires more memory, then the call to allocate() fails.
340  * @param p Precision
341  * @param dims Tensor dimensions
342  * @param ptr Pointer to the pre-allocated memory
343  * @param data_size Length of the pre-allocated array. If not set, size is assumed equal to dot product of dims.
344  */
345  TBlob(Precision p, Layout l, const SizeVector& dims, T* ptr, size_t data_size = 0) : Blob(p, l, dims) {
346  if (data_size == 0) {
347  data_size = size();
348  }
349  if (data_size != 0 && ptr == nullptr) {
350  THROW_IE_EXCEPTION << "Using Blob on external nullptr memory";
351  }
352  _allocator = details::make_pre_allocator(ptr, data_size);
353  // blob on attached memory is always allocated, so we are not forcing user to call allocate
354  allocate();
355  }
356 
357  /**
358  * @deprecated Please use TensorDesc for Blob initialization.
359  * @brief Constructor. Creates a TBlob object with the specified precision, layout, dimensions and custom memory allocator.
360  * @param p Precision
361  * @param l Layout
362  * @param dims Tensor dimensions
363  * @param alloc Allocator to be used
364  */
365  TBlob(Precision p, Layout l, const SizeVector &dims, std::shared_ptr<IAllocator> alloc)
366  : Blob(p, l, dims), _allocator(alloc) {
367  }
368 
369  /**
370  * @brief The copy constructor data is reallocated and copied from the source to the target blob.
371  * @param blob Source blob
372  */
373  TBlob(const TBlob<T> &blob) : Blob(blob.getTensorDesc()) {
374  copyFrom(blob);
375  }
376 
377  /**
378  * @brief A move constructor.
379  * @param blob rvalue to make a move from
380  */
381  TBlob(TBlob<T> &&blob) : Blob(blob.getTensorDesc()) {
382  moveFrom(blob);
383  }
384 
385  /**
386  * @brief Copy operator for the TBlob object.
387  * @param blob object reference to copy from
388  * @return Newly copied object
389  */
390  TBlob &operator=(const TBlob &blob) {
391  copyFrom(blob);
392  return *this;
393  }
394 
395  /**
396  *@brief Virtual destructor.
397  */
398  virtual ~TBlob() {
399  free();
400  }
401 
402  /**
403  * @brief Gets the size of the given type.
404  * @return Size of the type
405  */
406  size_t element_size() const noexcept override {
407  return sizeof(T);
408  }
409 
410  /**
411  * @brief Creates an new empty rvalue LockedMemory object.
412  * @return rvalue for the empty locked object of type T
413  */
414  virtual LockedMemory<T> data() noexcept {
415  return std::move(lockme<T>());
416  }
417 
418  /**
419  * @brief Creates a new empty rvalue read-only LockedMemory object.
420  * @return rvalue for the empty locked const object of type T.
421  */
422  virtual LockedMemory<const T> readOnly() const noexcept {
423  return std::move(lockme<const T>());
424  }
425 
426  /**
427  * @deprecated Deprecated to avoid memcpy() calls.
428  * @brief Copies data from the given vector to the blob.
429  * @param that Vector of values to copy to the blob
430  */
431  void set(const std::vector<T> &that) {
432  if (tensorDesc.getDims().size() != 0 && that.size() != product(tensorDesc.getDims()))
433  THROW_IE_EXCEPTION << "Size mismatch between dims and vector";
434  if (tensorDesc.getDims().size() == 0) {
435  tensorDesc.setDims({static_cast<unsigned int>(that.size())});
436  }
437  // minimisation of reallocations
438  if (_handle == nullptr) {
439  allocate();
440  }
441  auto memptr = data();
442  memcpy(memptr, that.data(), product(tensorDesc.getDims()) * sizeof(T));
443  }
444 
445  /**
446  * @brief Allocates or reallocates memory
447  */
448  void allocate() noexcept override {
449  if (_handle != nullptr) {
450  getAllocator()->free(_handle);
451  }
452  _handle = getAllocator()->alloc(TBlob<T>::product(tensorDesc.getDims()) * sizeof(T));
453  }
454 
455  /**
456  * @brief Frees all allocated data
457  */
458  bool deallocate() noexcept override {
459  return free();
460  }
461 
462  /**
463  * @brief Creates a new LockedMemory instance holding void pointer.
464  * @return LockedMemory instance holding void pointer
465  */
466  LockedMemory<void> buffer() noexcept override {
467  return std::move(lockme<void>());
468  }
469 
470  /**
471  * @brief Creates a new LockedMemory instance holding constant void pointer.
472  * @return LockedMemory instance holding constant void pointer
473  */
474  LockedMemory<const void> cbuffer() const noexcept override {
475  return std::move(lockme<const void>());
476  }
477 
478  /**
479  * @brief Gets BlobIterator for the data.
480  * Enables a ranged loop support for the TBlob object.
481  * @return BlobIterator object of type T
482  */
483  details::BlobIterator<T> begin() {
484  return details::BlobIterator<T>(data());
485  }
486 
487  /**
488  * @brief Gets BlobIterator for the end of data.
489  * Enables a ranged loop support for the TBlob object.
490  * @return BlobIterator object of type T representing end of the data
491  */
492  details::BlobIterator<T> end() {
493  return details::BlobIterator<T>(data(), size());
494  }
495 
496  /**
497  * @brief Gets a const BlobIterator for the read-only data.
498  * Enables a ranged loop support for the TBlob object.
499  * @return BlobIterator object of type const T
500  */
501  details::BlobIterator<const T> begin() const {
502  return details::BlobIterator<const T>(readOnly());
503  }
504 
505  /**
506  * @brief Gets a const BlobIterator for the end of read-only data.
507  * Enables a ranged loop support for the TBlob object.
508  * @return BlobIterator object of type const T representing end of data
509  */
510  details::BlobIterator<const T> end() const {
511  return details::BlobIterator<const T>(readOnly(), size());
512  }
513 
514 
515 protected:
516  /**
517  * @brief Local instance of IAllocator to manipulate memory.
518  */
519  mutable std::shared_ptr<IAllocator> _allocator;
520 
521  /**
522  * @brief A handle for the stored memory returned from _allocator.alloc().
523  */
524  void *_handle = nullptr;
525 
526  /**
527  * @brief Copies dimensions and data from the TBlob object.
528  * @param blob object reference to copy from
529  */
530  void copyFrom(const TBlob<T> &blob) {
531  tensorDesc = blob.tensorDesc;
532  this->allocate();
533  auto memptr = data();
534  memcpy(memptr, blob.readOnly(), product(tensorDesc.getDims()) * sizeof(T));
535  }
536 
537  /**
538  * @brief Swaps memory handlers between the current blob and the given one.
539  * @tparam U Type of the blob to move from
540  * @param blob TBlob instance to move from
541  */
542  template<class U>
543  void moveFrom(TBlob<U> &blob) {
544  tensorDesc = blob.tensorDesc;
545  this->_allocator = std::move(blob._allocator);
546  std::swap(this->_handle, blob._handle);
547  }
548 
549  /**
550  * @brief Frees handler and cleans up the stored data.
551  */
552  virtual bool free() {
553  bool bCanRelease = true;
554  if (_handle == nullptr) return bCanRelease;
555 
556  bCanRelease = getAllocator()->free(_handle);
557  _handle = nullptr;
558  return bCanRelease;
559  }
560 
561  /**
562  * @brief Creates a LockedMemory instance.
563  * @tparam S Type of the LockedMemory to be created
564  * @return A created instance of LockedMemory
565  */
566  template<class S>
567  LockedMemory<S> lockme() const {
568  return LockedMemory<S>(_allocator.get(), _handle, 0);
569  }
570 
571  /**
572  * @brief Gets an allocator or creates a default one.
573  * @return IAllocator instance
574  */
575  const std::shared_ptr<IAllocator> &getAllocator() const noexcept override {
576  // in case when constructor without allocator was used
577  if (!_allocator) {
578  _allocator = shared_from_irelease(CreateDefaultAllocator());
579  }
580 
581  return _allocator;
582  }
583 
584  /**
585  * @brief Returns handle to the stored data.
586  */
587  void *getHandle() const noexcept override {
588  return _handle;
589  }
590 };
591 
592 /**
593  * @deprecated Use TensorDesc to create Blob::Ptr.
594  * @brief Creates a blob with given precision and dimensions.
595  * @tparam Type Type of the shared pointer to be created
596  * @param p Given precision
597  * @param dims Given dimensions
598  * @return A shared pointer to the created blob
599  */
600 template<class Type>
602  if (!p.hasStorageType<Type>())
603  THROW_IE_EXCEPTION << "Cannot make shared blob! "
604  << "The blob type cannot be used to store objects of current precision";
605  return std::make_shared<TBlob<Type>>(p, l, dims);
606 }
607 
608 /**
609  * @deprecated Use TensorDesc to create Blob::Ptr
610  * @brief Creates a blob with the NCHW layout, given precision, and given dimensions.
611  * @tparam Type Type of the shared pointer to be created
612  * @param p Given precision
613  * @param dims Given dimensions
614  * @return A shared pointer to the created blob
615  */
616 template<class Type>
618  if (!p.hasStorageType<Type>())
619  THROW_IE_EXCEPTION << "Cannot make shared blob! "
620  << "The blob type cannot be used to store objects of current precision";
621  return make_shared_blob<Type>(p, TensorDesc::getLayoutByDims(dims), dims);
622 }
623 
624 /**
625  * @deprecated Use TensorDesc to create Blob::Ptr
626  * @brief Creates a blob with the given precision.
627  * @tparam Type Type of the shared pointer to be created
628  * @param p Given precision
629  * @param arg Shared pointer to IAllocator to use in the blob
630  * @return A shared pointer to the blob created
631  */
632 template<typename Type, class TArg>
633 inline typename InferenceEngine::TBlob<Type>::Ptr make_shared_blob(Precision p, Layout l, const TArg &arg) {
634  if (!p.hasStorageType<Type>())
635  THROW_IE_EXCEPTION << "Cannot make shared blob! "
636  << "The blob type cannot be used to store objects of current precision";
637  return std::make_shared<InferenceEngine::TBlob<Type>>(p, l, arg);
638 }
639 
640 /**
641  * @deprecated Use TensorDesc in order to create Blob::Ptr
642  * @brief Creates a blob with the NCHW layout and given tensor precision.
643  * @tparam Type Type of the shared pointer to be created
644  * @param p Given precision
645  * @param arg Shared pointer to IAllocator to use in the blob
646  * @return A shared pointer to the blob created
647  */
648 template<typename Type, class TArg>
649 inline typename InferenceEngine::TBlob<Type>::Ptr make_shared_blob(Precision p, const TArg &arg) {
650  if (!p.hasStorageType<Type>())
651  THROW_IE_EXCEPTION << "Cannot make shared blob! "
652  << "The blob type cannot be used to store objects of current precision";
653  return make_shared_blob<Type, TArg>(p, TensorDesc::getLayoutByDims(arg), arg);
654 }
655 
656 /**
657  * @brief Creates a blob with the given tensor descriptor.
658  * @tparam Type Type of the shared pointer to be created
659  * @param tensorDesc Tensor descriptor for Blob creation
660  * @return A shared pointer to the newly created blob of the given type
661  */
662 template<typename Type>
664  if (!tensorDesc.getPrecision().hasStorageType<Type>())
665  THROW_IE_EXCEPTION << "Cannot make shared blob! "
666  << "The blob type cannot be used to store objects of current precision";
667  return std::make_shared<InferenceEngine::TBlob<Type>>(tensorDesc);
668 }
669 
670 /**
671  * @brief Creates a blob with the given tensor descriptor from the pointer to the pre-allocated memory.
672  * @tparam Type Type of the shared pointer to be created
673  * @param tensorDesc TensorDesc for Blob creation
674  * @param ptr Pointer to the pre-allocated memory
675  * @param size Length of the pre-allocated array
676  * @return A shared pointer to the newly created blob of the given type
677  */
678 template<typename Type>
679 inline typename InferenceEngine::TBlob<Type>::Ptr make_shared_blob(const TensorDesc& tensorDesc, Type * ptr, size_t size = 0) {
680  if (!tensorDesc.getPrecision().hasStorageType<Type>())
681  THROW_IE_EXCEPTION << "Cannot make shared blob! "
682  << "The blob type cannot be used to store objects of current precision";
683  return std::make_shared<InferenceEngine::TBlob<Type>>(tensorDesc, ptr, size);
684 }
685 
686 /**
687  * @brief Creates a blob with the given tensor descriptor and allocator.
688  * @tparam Type Type of the shared pointer to be created
689  * @param tensorDesc Tensor descriptor for Blob creation
690  * @param alloc Shared pointer to IAllocator to use in the blob
691  * @return A shared pointer to the newly created blob of the given type
692  */
693 template<typename Type>
694 inline typename InferenceEngine::TBlob<Type>::Ptr make_shared_blob(const TensorDesc& tensorDesc, const std::shared_ptr<InferenceEngine::IAllocator>& alloc) {
695  if (!tensorDesc.getPrecision().hasStorageType<Type>())
696  THROW_IE_EXCEPTION << "Cannot make shared blob! "
697  << "The blob type cannot be used to store objects of current precision";
698  return std::make_shared<InferenceEngine::TBlob<Type>>(tensorDesc, alloc);
699 }
700 
701 /**
702  * @deprecated Use TensorDesc in order to create Blob::Ptr.
703  * @brief Gets a shared pointer for the new TBlob instance.
704  * The created instance is based on move semantics from the given TBlob instance.
705  * @tparam TypeTo Type of the shared pointer to be created
706  * @param arg rvalue for the blob to move from
707  * @return A shared pointer to the newly created blob of the given type
708  */
709 template<typename TypeTo>
711  return std::make_shared<InferenceEngine::TBlob<TypeTo>>(std::move(arg));
712 }
713 
714 /**
715  * @brief Creates a copy of given TBlob instance.
716  * @tparam TypeTo Type of the shared pointer to be created
717  * @param arg given pointer to blob
718  * @return A shared pointer to the newly created blob of the given type
719  */
720 template<typename TypeTo>
722  return std::make_shared<InferenceEngine::TBlob<TypeTo>>(arg);
723 }
724 
725 /**
726  * @deprecated Use TensorDesc in order to create Blob::Ptr.
727  * @brief Creates a blob with the given precision.
728  * @tparam TypeTo Type of the shared pointer to be created
729  * @param p Given precision
730  * @return A shared pointer to the blob created
731  */
732 template<typename TypeTo>
734  if (!p.hasStorageType<TypeTo>())
735  THROW_IE_EXCEPTION << "Cannot make shared blob! "
736  << "The blob type cannot be used to store objects of current precision";
737  return std::make_shared<TBlob<TypeTo>>(p, l);
738 }
739 
740 /**
741  * @deprecated Use TensorDesc in order to create Blob::Ptr.
742  * @brief Creates a blob with the given precision, layout and dimensions from the vector of values.
743  * @tparam TypeTo Type of the shared pointer to be created
744  * @param p Given precision
745  * @param l Given Layout
746  * @param dims Given dimensions
747  * @param arg Vector of values
748  * @return A shared pointer to the blob created
749  */
750 template<typename TypeTo>
751 inline typename TBlob<TypeTo>::Ptr make_shared_blob(Precision p, Layout l, SizeVector dims, const std::vector<TypeTo> &arg) {
752  if (!p.hasStorageType<TypeTo>())
753  THROW_IE_EXCEPTION << "Cannot make shared blob! "
754  << "The blob type cannot be used to store objects of current precision";
755  auto blob = std::make_shared<TBlob<TypeTo>>(p, l, dims);
756  blob->set(arg);
757  return blob;
758 }
759 
760 /**
761  * @deprecated Use TensorDesc in order to create Blob::Ptr.
762  * @brief Creates a blob with the given precision from the vector of values.
763  * @tparam TypeTo Type of the shared pointer to be created
764  * @param p Given precision
765  * @param l Layout
766  * @param arg Vector of values
767  * @return A shared pointer to the blob created
768  */
769 template<typename TypeTo>
770 inline typename TBlob<TypeTo>::Ptr make_shared_blob(Precision p, Layout l, const std::vector<TypeTo> &arg) {
771  if (!p.hasStorageType<TypeTo>())
772  THROW_IE_EXCEPTION << "Cannot make shared blob! "
773  << "The blob type cannot be used to store objects of current precision";
774  auto blob = std::make_shared<TBlob<TypeTo>>(p, l);
775  blob->set(arg);
776  return blob;
777 }
778 
779 /**
780  * @deprecated Use TensorDesc in order to create Blob::Ptr.
781  * @brief Creates a blob with the NCHW layout and the given precision from the vector of values.
782  * @tparam TypeTo Type of the shared pointer to be created
783  * @param p Given precision
784  * @param arg Vector of values
785  * @return A shared pointer to the blob created
786  */
787 template<typename TypeTo>
788 inline typename TBlob<TypeTo>::Ptr make_shared_blob(Precision p, const std::vector<TypeTo> &arg) {
789  if (!p.hasStorageType<TypeTo>())
790  THROW_IE_EXCEPTION << "Cannot make shared blob! "
791  << "The blob type cannot be used to store objects of current precision";
792  return make_shared_blob<TypeTo>(p, TensorDesc::getLayoutByDims(arg), arg);
793 }
794 
795 /**
796  * @deprecated Use TensorDesc in order to create Blob::Ptr.
797  * @brief Creates a blob with the given precision from the pointer to the pre-allocated memory.
798  * @param p Given precision
799  * @param l Layout
800  * @param dims Given dimensions
801  * @param ptr Pointer to the pre-allocated memory
802  * @param size Length of the pre-allocated array
803  * @return A shared pointer to the blob created
804  */
805 template <typename TypeTo>
806 inline typename TBlob<TypeTo>::Ptr make_shared_blob(Precision p, Layout l, const SizeVector &dims, TypeTo * ptr, size_t size = 0) {
807  if (!p.hasStorageType<TypeTo>())
808  THROW_IE_EXCEPTION << "Cannot make shared blob! "
809  << "The blob type cannot be used to store objects of current precision";
810  auto blob = std::make_shared<TBlob<TypeTo>>(p, l, dims, ptr, size);
811  return blob;
812 }
813 
814 /**
815  * @deprecated Use TensorDesc in order to create Blob::Ptr.
816  * @brief Creates a blob with the NCHW layout and the given precision from the pointer to the pre-allocated memory
817  * @param p Given precision
818  * @param dims Given dimensions
819  * @param ptr Pointer to the pre-allocated memory
820  * @param size Length of the pre-allocated array
821  * @return A shared pointer to the blob created
822  */
823 template <typename TypeTo>
824 inline typename TBlob<TypeTo>::Ptr make_shared_blob(Precision p, const SizeVector &dims, TypeTo * ptr, size_t size = 0) {
825  if (!p.hasStorageType<TypeTo>())
826  THROW_IE_EXCEPTION << "Cannot make shared blob! "
827  << "The blob type cannot be used to store objects of current precision";
828  return make_shared_blob<TypeTo>(p, TensorDesc::getLayoutByDims(dims), dims, ptr, size);
829 }
830 
831 /**
832  * @brief This structure describes ROI data.
833  */
834 struct ROI {
835  size_t id; // ID of a ROI
836  size_t posX; // W upper left coordinate of ROI
837  size_t posY; // H upper left coordinate of ROI
838  size_t sizeX; // W size of ROI
839  size_t sizeY; // H size of ROI
840 };
841 
842 /**
843  * @brief Creates a blob describing given ROI object based on the given blob with pre-allocated memory.
844  * @param inputBlob original blob with pre-allocated memory.
845  * @param roi A ROI object inside of the original blob.
846  * @return A shared pointer to the newly created blob.
847  */
848 INFERENCE_ENGINE_API_CPP(Blob::Ptr) make_shared_blob(const Blob::Ptr &inputBlob, const ROI &roi);
849 
850 } // namespace InferenceEngine
#define THROW_IE_EXCEPTION
A macro used to throw the exception with a notable description.
Definition: ie_exception.hpp:22
std::shared_ptr< IAllocator > _allocator
Local instance of IAllocator to manipulate memory.
Definition: ie_blob.h:518
This class represents locked memory for read/write memory.
Definition: ie_locked_memory.hpp:107
const SizeVector dims() const noexcept
Returns the tensor dimensions vector with reversed order.
Definition: ie_blob.h:166
Precision type() const noexcept
Returns the tensor precision of the current Blob object.
Definition: ie_blob.h:49
Layout getLayout() const
Returns the memory layout.
Definition: ie_layouts.h:211
This structure describes ROI data.
Definition: ie_blob.h:834
std::vector< size_t > SizeVector
Represents tensor size. The order is opposite to the order in Caffe*: (w,h,n,b) where the most freque...
Definition: ie_common.h:26
TensorDesc tensorDesc
The tensor descriptor of the given blob.
Definition: ie_blob.h:227
Definition: ie_argmax_layer.hpp:11
size_t Resize(const SizeVector &dims, Layout layout=Layout::ANY) noexcept
Changes Tensor size to the specified dimensions. If it was allocated, the previous data is deallocate...
Definition: ie_blob.h:128
Layout
Layouts that the inference engine supports.
Definition: ie_common.h:76
void setDims(const SizeVector &dims)
Sets dimensions.
virtual bool deallocate() noexcept=0
Releases previously allocated data. Abstract method.
Blob(Precision p, Layout l, const SizeVector &dims)
The constructor creates an empty Blob object with the specified precision, layout and dimensions...
Definition: ie_blob.h:118
Blob(Precision p, const SizeVector &dims)
The constructor creates an empty Blob object with the specified precision and dimensions.
Definition: ie_blob.h:108
Blob(Precision p)
Constructor. Creates an empty Blob object with the specified precision.
Definition: ie_blob.h:92
static Ptr CreateFromData(const DataPtr &data)
Creates a TBlob<> object from a Data node.
This class is for <void*> data and allows casting to any pointers.
Definition: ie_locked_memory.hpp:204
size_t size() const noexcept
Returns the total number of elements (a product of all the dims)
Definition: ie_blob.h:180
This class is for read-only segments.
Definition: ie_locked_memory.hpp:295
Represents real host memory allocated for a Tensor/Blob per C type.
Definition: ie_blob.h:266
Blob(TensorDesc tensorDesc)
Constructor. Creates an empty Blob object with the specified precision.
Definition: ie_blob.h:85
static size_t product(const SizeVector &dims) noexcept
Multiplies the dimension vector&#39;s values.
Definition: ie_blob.h:234
virtual LockedMemory< const T > readOnly() const noexcept
Creates a new empty rvalue read-only LockedMemory object.
Definition: ie_blob.h:421
InferenceEngine::IAllocator * CreateDefaultAllocator() noexcept
Creates the default implementation of the Inference Engine allocator per plugin.
const TensorDesc & getTensorDesc() const noexcept
Returns the tensor description.
Definition: ie_blob.h:173
size_t Reshape(const SizeVector &dims, Layout layout=Layout::ANY) noexcept
Changes tensor size to the specified dimensions without changing memory. The total size remains uncha...
Definition: ie_blob.h:149
This class defines Tensor description.
Definition: ie_layouts.h:143
A header file that provides Allocator interface.
std::shared_ptr< const Blob > CPtr
A smart pointer to the const Blob object.
Definition: ie_blob.h:43
std::shared_ptr< TBlob< T >> Ptr
Smart Pointer to this TBlob object.
Definition: ie_blob.h:274
SizeVector & getDims()
Returns the vector of dimensions.
Definition: ie_layouts.h:191
A header file that provides class for describing precision of data.
std::shared_ptr< Blob > Ptr
A smart pointer containing Blob object.
Definition: ie_blob.h:38
virtual ~Blob()=default
Blob virtual destructor.
A header file for data layouts and conversion between them.
TBlob< Type >::Ptr make_shared_blob(Precision p, Layout l, const SizeVector &dims)
Creates a blob with given precision and dimensions.
Definition: ie_blob.h:601
size_t byteSize() const noexcept
Returns the size of the current Blob in bytes.
Definition: ie_blob.h:187
The header file defines utility PreAllocator class.
virtual void allocate() noexcept=0
Allocates memory to store the data. Abstract method.
A header file for generic LockedMemory<> and different variations of locks.
std::map< std::string, Blob::Ptr > BlobMap
This is a convenient type for working with a map containing pairs(string, pointer to a Blob instance)...
Definition: ie_blob.h:259
virtual LockedMemory< const void > cbuffer() const noexcept=0
Gets read-only access to the allocated memory. Abstract method.
This class implements a container object that represents a tensor in memory (host and remote/accelera...
Definition: ie_blob.h:33
virtual size_t element_size() const noexcept=0
Returns the number of bytes per element. The overall Blob capacity is size() * element_size(). Abstract method.
Precision precision() const noexcept
Returns the tensor precision of the current Blob object.
Definition: ie_blob.h:57
void * _handle
A handle for the stored memory returned from _allocator.alloc().
Definition: ie_blob.h:523
virtual LockedMemory< void > buffer() noexcept=0
Gets access to the allocated memory. Abstract method.
virtual const std::shared_ptr< IAllocator > & getAllocator() const noexcept=0
Gets the allocator for allocator-based blobs.
std::shared_ptr< Data > DataPtr
Smart pointer to Data.
Definition: ie_common.h:50
A header file for the BlobIterator class.
Blob(Precision p, Layout l)
The constructor creates an empty Blob object with the specified precision and layout.
Definition: ie_blob.h:100
virtual void * getHandle() const noexcept=0
Gets the handle to allocated memory.
Layout layout() const noexcept
Returns the tensor layout of the current Blob object.
Definition: ie_blob.h:65
const Precision & getPrecision() const
Returns the memory precision.
Definition: ie_layouts.h:260
static Layout getLayoutByDims(SizeVector dims)
Returns the standard layout for dimensions.
This is a header file with common inference engine definitions.
A header file for the main Inference Engine exception.
This class holds precision value and provides precision related operations.
Definition: ie_precision.hpp:19
bool hasStorageType(const char *typeName=nullptr) const noexcept
checks whether given storage class T can be used to store objects of current precision ...
Definition: ie_precision.hpp:85