forked from openvinotoolkit/openvino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blob_factory.hpp
151 lines (137 loc) · 4.86 KB
/
blob_factory.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright (C) 2018-2023 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
/**
* @brief A file with helper functions to uniformly create Blob objects
* @file blob_transform.hpp
*/
#pragma once
#include <memory>
#include <utility>
#include <vector>
#include "ie_blob.h"
#include "ie_data.h"
#include "ie_memcpy.h"
#include "ie_preprocess.hpp"
IE_SUPPRESS_DEPRECATED_START
/**
* @private
*/
template <InferenceEngine::Precision::ePrecision precision>
class BlobFactory {
public:
using BlobType = typename InferenceEngine::PrecisionTrait<precision>::value_type;
static InferenceEngine::Blob::Ptr make(const InferenceEngine::TensorDesc& desc) {
return InferenceEngine::make_shared_blob<BlobType>(desc);
}
static InferenceEngine::Blob::Ptr make(const InferenceEngine::TensorDesc& desc, void* ptr) {
return InferenceEngine::make_shared_blob<BlobType>(desc, reinterpret_cast<BlobType*>(ptr));
}
static InferenceEngine::Blob::Ptr make(const InferenceEngine::TensorDesc& desc, void* ptr, const std::size_t size) {
return InferenceEngine::make_shared_blob<BlobType>(desc, reinterpret_cast<BlobType*>(ptr), size);
}
static InferenceEngine::Blob::Ptr make(const InferenceEngine::TensorDesc& desc,
const std::shared_ptr<InferenceEngine::IAllocator>& alloc) {
return InferenceEngine::make_shared_blob<BlobType>(desc, alloc);
}
};
/**
* @private
*/
template <InferenceEngine::Precision::ePrecision precision, class... Args>
InferenceEngine::Blob::Ptr make_shared_blob2(Args&&... args) {
return BlobFactory<precision>::make(std::forward<Args>(args)...);
}
/**
* @brief Creates Blob::Ptr with precision.
* @ingroup ie_dev_api_memory
*
* @param[in] desc The TensorDesc object
* @return A Blob::Ptr pointer
*/
INFERENCE_ENGINE_API_CPP(InferenceEngine::Blob::Ptr)
make_blob_with_precision(const InferenceEngine::TensorDesc& desc);
/**
* @brief Makes a blob with precision.
* @ingroup ie_dev_api_memory
*
* @param[in] desc The TensorDesc object
* @param ptr The pointer to a raw memory
* @return A Blob::Ptr pointer
*/
INFERENCE_ENGINE_API_CPP(InferenceEngine::Blob::Ptr)
make_blob_with_precision(const InferenceEngine::TensorDesc& desc, void* ptr);
/**
* @brief Makes a blob with precision.
* @ingroup ie_dev_api_memory
*
* @param[in] desc The description
* @param[in] alloc The IAllocator object
* @return A Blob::Ptr pointer
*/
INFERENCE_ENGINE_API_CPP(InferenceEngine::Blob::Ptr)
make_blob_with_precision(const InferenceEngine::TensorDesc& desc,
const std::shared_ptr<InferenceEngine::IAllocator>& alloc);
/**
* @brief Creates a plain Blob::Ptr
* @ingroup ie_dev_api_memory
*
* @param[in] prec The Precision value
* @param[in] dims The dims
* @return A Blob::Ptr pointer
*/
INFERENCE_ENGINE_API_CPP(InferenceEngine::Blob::Ptr)
make_plain_blob(InferenceEngine::Precision prec, const InferenceEngine::SizeVector dims);
/**
* @brief Creates Blob::Ptr with precision
* @ingroup ie_dev_api_memory
*
* @param[in] precision The precision
* @param args The arguments
* @tparam Args Variadic template arguments
* @return A Blob::Ptr pointer
*/
template <class... Args>
InferenceEngine::Blob::Ptr make_blob_with_precision(InferenceEngine::Precision precision, Args&&... args) {
#define USE_FACTORY(precision) \
case InferenceEngine::Precision::precision: \
return make_shared_blob2<InferenceEngine::Precision::precision>(std::forward<Args>(args)...);
switch (precision) {
USE_FACTORY(FP32);
USE_FACTORY(FP64);
USE_FACTORY(FP16);
USE_FACTORY(Q78);
USE_FACTORY(I4);
USE_FACTORY(I8);
USE_FACTORY(I16);
USE_FACTORY(I32);
USE_FACTORY(I64);
USE_FACTORY(U4);
USE_FACTORY(U8);
USE_FACTORY(U16);
USE_FACTORY(U32);
USE_FACTORY(U64);
USE_FACTORY(BIN);
USE_FACTORY(BF16);
USE_FACTORY(BOOL);
default:
IE_THROW() << "cannot locate blob for precision: " << precision;
}
#undef USE_FACTORY
}
/**
* @brief Copy data from std::vector to Blob
* @ingroup ie_dev_api_memory
* @tparam T type of data in std::vector
* @param outputBlob An output blob to copy to
* @param inputVector An input std::vector to copy from
*/
template <typename T>
void CopyVectorToBlob(const InferenceEngine::Blob::Ptr outputBlob, const std::vector<T>& inputVector) {
if (outputBlob->size() != inputVector.size())
IE_THROW() << "Size mismatch between dims and vector";
if (outputBlob->element_size() != sizeof(T))
IE_THROW() << "Element size mismatch between blob and vector";
ie_memcpy(outputBlob->buffer().as<T*>(), outputBlob->byteSize(), &inputVector[0], inputVector.size() * sizeof(T));
}
IE_SUPPRESS_DEPRECATED_END