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