ie_blob.h
Go to the documentation of this file.
1 // Copyright (C) 2018 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  * @deprecated Please use TensorDesc for Blob initialization.
308  */
309  explicit TBlob(Precision p, Layout l) : Blob(p, l) {}
310 
311  /**
312  * @deprecated Please use TensorDesc for Blob initialization.
313  * @brief Creates a TBlob object with the specified dimensions but does not allocate the memory. Please use the allocate() method to allocate memory.
314  * @param p Precision
315  * @param l Layout
316  * @param dims Tensor dimensions
317  */
318  TBlob(Precision p, Layout l, const SizeVector& dims)
319  : Blob(p, l, dims) {
320  }
321 
322  /**
323  * @deprecated Please use TensorDesc for Blob initialization.
324  * @brief The constructor creates a TBlob object with the specified dimensions on the pre-allocated memory. Therefore, the allocate() call is not required.
325  * @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.
326  * Also certain operations might fail:
327  * After the blob is constructed with this pointer its size is stored in the TBlob instance.
328  * If the resize() operation happens which requires more memory, then the call to allocate() fails.
329  * @param p Precision
330  * @param dims Tensor dimensions
331  * @param ptr Pointer to the pre-allocated memory
332  * @param data_size Length of the pre-allocated array. If not set, size is assumed equal to dot product of dims.
333  */
334  TBlob(Precision p, Layout l, const SizeVector& dims, T* ptr, size_t data_size = 0) : Blob(p, l, dims) {
335  if (data_size == 0) {
336  data_size = size();
337  }
338  if (data_size != 0 && ptr == nullptr) {
339  THROW_IE_EXCEPTION << "Using Blob on external nullptr memory";
340  }
341  _allocator = details::make_pre_allocator(ptr, data_size);
342  // blob on attached memory is always allocated, so we are not forcing user to call allocate
343  allocate();
344  }
345 
346  /**
347  * @deprecated Please use TensorDesc for Blob initialization.
348  * @brief Constructor. Creates a TBlob object with the specified precision, layout, dimensions and custom memory allocator.
349  * @param p Precision
350  * @param l Layout
351  * @param dims Tensor dimensions
352  * @param alloc Allocator to be used
353  */
354  TBlob(Precision p, Layout l, const SizeVector &dims, std::shared_ptr<IAllocator> alloc)
355  : Blob(p, l, dims), _allocator(alloc) {
356  }
357 
358  /**
359  * @brief The copy constructor data is reallocated and copied from the source to the target blob.
360  * @param blob Source blob
361  */
362  TBlob(const TBlob<T> &blob) : Blob(blob.getTensorDesc()) {
363  copyFrom(blob);
364  }
365 
366  /**
367  * @brief A move constructor.
368  * @param blob rvalue to make a move from
369  */
370  TBlob(TBlob<T> &&blob) : Blob(blob.getTensorDesc()) {
371  moveFrom(blob);
372  }
373 
374  /**
375  * @brief Copy operator for the TBlob object.
376  * @param blob object reference to copy from
377  * @return Newly copied object
378  */
379  TBlob &operator=(const TBlob &blob) {
380  copyFrom(blob);
381  return *this;
382  }
383 
384  /**
385  *@brief Virtual destructor.
386  */
387  virtual ~TBlob() {
388  free();
389  }
390 
391  /**
392  * @brief Gets the size of the given type.
393  * @return Size of the type
394  */
395  size_t element_size() const noexcept override {
396  return sizeof(T);
397  }
398 
399  /**
400  * @brief Creates an new empty rvalue LockedMemory object.
401  * @return rvalue for the empty locked object of type T
402  */
403  virtual LockedMemory<T> data() noexcept {
404  return std::move(lockme<T>());
405  }
406 
407  /**
408  * @brief Creates a new empty rvalue read-only LockedMemory object.
409  * @return rvalue for the empty locked const object of type T.
410  */
411  virtual LockedMemory<const T> readOnly() const noexcept {
412  return std::move(lockme<const T>());
413  }
414 
415  /**
416  * @deprecated Deprecated to avoid memcpy() calls.
417  * @brief Copies data from the given vector to the blob.
418  * @param that Vector of values to copy to the blob
419  */
420  void set(const std::vector<T> &that) {
421  if (tensorDesc.getDims().size() != 0 && that.size() != product(tensorDesc.getDims()))
422  THROW_IE_EXCEPTION << "Size mismatch between dims and vector";
423  if (tensorDesc.getDims().size() == 0) {
424  tensorDesc.setDims({static_cast<unsigned int>(that.size())});
425  }
426  // minimisation of reallocations
427  if (_handle == nullptr) {
428  allocate();
429  }
430  auto memptr = data();
431  memcpy(memptr, that.data(), product(tensorDesc.getDims()) * sizeof(T));
432  }
433 
434  /**
435  * @brief Allocates or reallocates memory
436  */
437  void allocate() noexcept override {
438  if (_handle != nullptr) {
439  getAllocator()->free(_handle);
440  }
441  _handle = getAllocator()->alloc(TBlob<T>::product(tensorDesc.getDims()) * sizeof(T));
442  }
443 
444  /**
445  * @brief Frees all allocated data
446  */
447  bool deallocate() noexcept override {
448  return free();
449  }
450 
451  /**
452  * @brief Creates a new LockedMemory instance holding void pointer.
453  * @return LockedMemory instance holding void pointer
454  */
455  LockedMemory<void> buffer() noexcept override {
456  return std::move(lockme<void>());
457  }
458 
459  /**
460  * @brief Creates a new LockedMemory instance holding constant void pointer.
461  * @return LockedMemory instance holding constant void pointer
462  */
463  LockedMemory<const void> cbuffer() const noexcept override {
464  return std::move(lockme<const void>());
465  }
466 
467  /**
468  * @brief Gets BlobIterator for the data.
469  * Enables a ranged loop support for the TBlob object.
470  * @return BlobIterator object of type T
471  */
472  details::BlobIterator<T> begin() {
473  return details::BlobIterator<T>(data());
474  }
475 
476  /**
477  * @brief Gets BlobIterator for the end of data.
478  * Enables a ranged loop support for the TBlob object.
479  * @return BlobIterator object of type T representing end of the data
480  */
481  details::BlobIterator<T> end() {
482  return details::BlobIterator<T>(data(), size());
483  }
484 
485  /**
486  * @brief Gets a const BlobIterator for the read-only data.
487  * Enables a ranged loop support for the TBlob object.
488  * @return BlobIterator object of type const T
489  */
490  details::BlobIterator<const T> begin() const {
491  return details::BlobIterator<const T>(readOnly());
492  }
493 
494  /**
495  * @brief Gets a const BlobIterator for the end of read-only data.
496  * Enables a ranged loop support for the TBlob object.
497  * @return BlobIterator object of type const T representing end of data
498  */
499  details::BlobIterator<const T> end() const {
500  return details::BlobIterator<const T>(readOnly(), size());
501  }
502 
503 
504 protected:
505  /**
506  * @brief Local instance of IAllocator to manipulate memory.
507  */
508  mutable std::shared_ptr<IAllocator> _allocator;
509 
510  /**
511  * @brief A handle for the stored memory returned from _allocator.alloc().
512  */
513  void *_handle = nullptr;
514 
515  /**
516  * @brief Copies dimensions and data from the TBlob object.
517  * @param blob object reference to copy from
518  */
519  void copyFrom(const TBlob<T> &blob) {
520  tensorDesc = blob.tensorDesc;
521  this->allocate();
522  auto memptr = data();
523  memcpy(memptr, blob.readOnly(), product(tensorDesc.getDims()) * sizeof(T));
524  }
525 
526  /**
527  * @brief Swaps memory handlers between the current blob and the given one.
528  * @tparam U Type of the blob to move from
529  * @param blob TBlob instance to move from
530  */
531  template<class U>
532  void moveFrom(TBlob<U> &blob) {
533  tensorDesc = blob.tensorDesc;
534  this->_allocator = std::move(blob._allocator);
535  std::swap(this->_handle, blob._handle);
536  }
537 
538  /**
539  * @brief Frees handler and cleans up the stored data.
540  */
541  virtual bool free() {
542  bool bCanRelease = true;
543  if (_handle == nullptr) return bCanRelease;
544 
545  bCanRelease = getAllocator()->free(_handle);
546  _handle = nullptr;
547  return bCanRelease;
548  }
549 
550  /**
551  * @brief Creates a LockedMemory instance.
552  * @tparam S Type of the LockedMemory to be created
553  * @return A created instance of LockedMemory
554  */
555  template<class S>
556  LockedMemory<S> lockme() const {
557  return LockedMemory<S>(_allocator.get(), _handle, 0);
558  }
559 
560  /**
561  * @brief Gets an allocator or creates a default one.
562  * @return IAllocator instance
563  */
564  const std::shared_ptr<IAllocator> &getAllocator() const noexcept override {
565  // in case when constructor without allocator was used
566  if (!_allocator) {
567  _allocator = shared_from_irelease(CreateDefaultAllocator());
568  }
569 
570  return _allocator;
571  }
572 
573  /**
574  * @brief Returns handle to the stored data.
575  */
576  void *getHandle() const noexcept override {
577  return _handle;
578  }
579 };
580 
581 /**
582  * @deprecated Use TensorDesc to create Blob::Ptr.
583  * @brief Creates a blob with given precision and dimensions.
584  * @tparam Type Type of the shared pointer to be created
585  * @param p Given precision
586  * @param dims Given dimensions
587  * @return A shared pointer to the created blob
588  */
589 template<class Type>
591  IE_ASSERT(p.hasStorageType<Type>());
592  return std::make_shared<TBlob<Type>>(p, l, dims);
593 }
594 
595 /**
596  * @deprecated Use TensorDesc to create Blob::Ptr
597  * @brief Creates a blob with the NCHW layout, given precision, and given dimensions.
598  * @tparam Type Type of the shared pointer to be created
599  * @param p Given precision
600  * @param dims Given dimensions
601  * @return A shared pointer to the created blob
602  */
603 template<class Type>
605  IE_ASSERT(p.hasStorageType<Type>());
606  return make_shared_blob<Type>(p, TensorDesc::getLayoutByDims(dims), dims);
607 }
608 
609 /**
610  * @deprecated Use TensorDesc to create Blob::Ptr
611  * @brief Creates a blob with the given precision.
612  * @tparam Type Type of the shared pointer to be created
613  * @param p Given precision
614  * @param arg Shared pointer to IAllocator to use in the blob
615  * @return A shared pointer to the blob created
616  */
617 template<typename Type, class TArg>
618 inline typename InferenceEngine::TBlob<Type>::Ptr make_shared_blob(Precision p, Layout l, const TArg &arg) {
619  IE_ASSERT(p.hasStorageType<Type>());
620  return std::make_shared<InferenceEngine::TBlob<Type>>(p, l, arg);
621 }
622 
623 /**
624  * @deprecated Use TensorDesc in order to create Blob::Ptr
625  * @brief Creates a blob with the NCHW layout and given tensor precision.
626  * @tparam Type Type of the shared pointer to be created
627  * @param p Given precision
628  * @param arg Shared pointer to IAllocator to use in the blob
629  * @return A shared pointer to the blob created
630  */
631 template<typename Type, class TArg>
632 inline typename InferenceEngine::TBlob<Type>::Ptr make_shared_blob(Precision p, const TArg &arg) {
633  IE_ASSERT(p.hasStorageType<Type>());
634  return make_shared_blob<Type, TArg>(p, TensorDesc::getLayoutByDims(arg), arg);
635 }
636 
637 /**
638  * @brief Creates a blob with the given tensor descriptor.
639  * @tparam Type Type of the shared pointer to be created
640  * @param tensorDesc Tensor descriptor for Blob creation
641  * @return A shared pointer to the newly created blob of the given type
642  */
643 template<typename Type>
645  IE_ASSERT(tensorDesc.getPrecision().hasStorageType<Type>());
646  return std::make_shared<InferenceEngine::TBlob<Type>>(tensorDesc);
647 }
648 
649 /**
650  * @brief Creates a blob with the given tensor descriptor from the pointer to the pre-allocated memory.
651  * @tparam Type Type of the shared pointer to be created
652  * @param tensorDesc TensorDesc for Blob creation
653  * @param ptr Pointer to the pre-allocated memory
654  * @param size Length of the pre-allocated array
655  * @return A shared pointer to the newly created blob of the given type
656  */
657 template<typename Type>
658 inline typename InferenceEngine::TBlob<Type>::Ptr make_shared_blob(const TensorDesc& tensorDesc, Type * ptr, size_t size = 0) {
659  IE_ASSERT(tensorDesc.getPrecision().hasStorageType<Type>());
660  return std::make_shared<InferenceEngine::TBlob<Type>>(tensorDesc, ptr, size);
661 }
662 
663 /**
664  * @deprecated Use TensorDesc in order to create Blob::Ptr.
665  * @brief Gets a shared pointer for the new TBlob instance.
666  * The created instance is based on move semantics from the given TBlob instance.
667  * @tparam TypeTo Type of the shared pointer to be created
668  * @param arg rvalue for the blob to move from
669  * @return A shared pointer to the newly created blob of the given type
670  */
671 template<typename TypeTo>
673  return std::make_shared<InferenceEngine::TBlob<TypeTo>>(std::move(arg));
674 }
675 
676 /**
677  * @brief Creates a copy of given TBlob instance.
678  * @tparam TypeTo Type of the shared pointer to be created
679  * @param arg given pointer to blob
680  * @return A shared pointer to the newly created blob of the given type
681  */
682 template<typename TypeTo>
684  return std::make_shared<InferenceEngine::TBlob<TypeTo>>(arg);
685 }
686 
687 /**
688  * @deprecated Use TensorDesc in order to create Blob::Ptr.
689  * @brief Creates a blob with the given precision.
690  * @tparam TypeTo Type of the shared pointer to be created
691  * @param p Given precision
692  * @return A shared pointer to the blob created
693  */
694 template<typename TypeTo>
696  IE_ASSERT(p.hasStorageType<TypeTo>());
697  return std::make_shared<TBlob<TypeTo>>(p, l);
698 }
699 
700 /**
701  * @deprecated Use TensorDesc in order to create Blob::Ptr.
702  * @brief Creates a blob with the given precision, layout and dimensions from the vector of values.
703  * @tparam TypeTo Type of the shared pointer to be created
704  * @param p Given precision
705  * @param l Given Layout
706  * @param dims Given dimensions
707  * @param arg Vector of values
708  * @return A shared pointer to the blob created
709  */
710 template<typename TypeTo>
711 inline typename TBlob<TypeTo>::Ptr make_shared_blob(Precision p, Layout l, SizeVector dims, const std::vector<TypeTo> &arg) {
712  IE_ASSERT(p.hasStorageType<TypeTo>());
713  auto blob = std::make_shared<TBlob<TypeTo>>(p, l, dims);
714  blob->set(arg);
715  return blob;
716 }
717 
718 /**
719  * @deprecated Use TensorDesc in order to create Blob::Ptr.
720  * @brief Creates a blob with the given precision from the vector of values.
721  * @tparam TypeTo Type of the shared pointer to be created
722  * @param p Given precision
723  * @param l Layout
724  * @param arg Vector of values
725  * @return A shared pointer to the blob created
726  */
727 template<typename TypeTo>
728 inline typename TBlob<TypeTo>::Ptr make_shared_blob(Precision p, Layout l, const std::vector<TypeTo> &arg) {
729  IE_ASSERT(p.hasStorageType<TypeTo>());
730  auto blob = std::make_shared<TBlob<TypeTo>>(p, l);
731  blob->set(arg);
732  return blob;
733 }
734 
735 /**
736  * @deprecated Use TensorDesc in order to create Blob::Ptr.
737  * @brief Creates a blob with the NCHW layout and the given precision from the vector of values.
738  * @tparam TypeTo Type of the shared pointer to be created
739  * @param p Given precision
740  * @param arg Vector of values
741  * @return A shared pointer to the blob created
742  */
743 template<typename TypeTo>
744 inline typename TBlob<TypeTo>::Ptr make_shared_blob(Precision p, const std::vector<TypeTo> &arg) {
745  IE_ASSERT(p.hasStorageType<TypeTo>());
746  return make_shared_blob<TypeTo>(p, TensorDesc::getLayoutByDims(arg), arg);
747 }
748 
749 /**
750  * @deprecated Use TensorDesc in order to create Blob::Ptr.
751  * @brief Creates a blob with the given precision from the pointer to the pre-allocated memory.
752  * @param p Given precision
753  * @param l Layout
754  * @param dims Given dimensions
755  * @param ptr Pointer to the pre-allocated memory
756  * @param size Length of the pre-allocated array
757  * @return A shared pointer to the blob created
758  */
759 template <typename TypeTo>
760 inline typename TBlob<TypeTo>::Ptr make_shared_blob(Precision p, Layout l, const SizeVector &dims, TypeTo * ptr, size_t size = 0) {
761  IE_ASSERT(p.hasStorageType<TypeTo>());
762  auto blob = std::make_shared<TBlob<TypeTo>>(p, l, dims, ptr, size);
763  return blob;
764 }
765 
766 /**
767  * @deprecated Use TensorDesc in order to create Blob::Ptr.
768  * @brief Creates a blob with the NCHW layout and the given precision from the pointer to the pre-allocated memory
769  * @param p Given precision
770  * @param dims Given dimensions
771  * @param ptr Pointer to the pre-allocated memory
772  * @param size Length of the pre-allocated array
773  * @return A shared pointer to the blob created
774  */
775 template <typename TypeTo>
776 inline typename TBlob<TypeTo>::Ptr make_shared_blob(Precision p, const SizeVector &dims, TypeTo * ptr, size_t size = 0) {
777  IE_ASSERT(p.hasStorageType<TypeTo>());
778  return make_shared_blob<TypeTo>(p, TensorDesc::getLayoutByDims(dims), dims, ptr, size);
779 }
780 
781 /**
782  * @brief This structure describes ROI data.
783  */
784 struct ROI {
785  size_t id; // ID of a ROI
786  size_t posX; // W upper left coordinate of ROI
787  size_t posY; // H upper left coordinate of ROI
788  size_t sizeX; // W size of ROI
789  size_t sizeY; // H size of ROI
790 };
791 
792 /**
793  * @brief Creates a blob describing given ROI object based on the given blob with pre-allocated memory.
794  * @param inputBlob original blob with pre-allocated memory.
795  * @param roi A ROI object inside of the original blob.
796  * @return A shared pointer to the newly created blob.
797  */
798 INFERENCE_ENGINE_API_CPP(Blob::Ptr) make_shared_blob(const Blob::Ptr &inputBlob, const ROI &roi);
799 
800 } // 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:507
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:784
#define IE_ASSERT(EXPRESSION)
Uses assert() function if NDEBUG is not defined, InferenceEngine exception otherwise.
Definition: ie_exception.hpp:34
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:410
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:590
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:512
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:257
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 for store objects of current precision ...
Definition: ie_precision.hpp:84