1 // Copyright (C) 2018 Intel Corporation
3 // SPDX-License-Identifier: Apache-2.0
14 #include "ie_plugin.hpp"
15 #include "cpp/ie_cnn_network.h"
16 #include "debug_options.h"
17 #include "inference_engine.hpp"
18 #include <CPP/network.hpp>
19 #include <CPP/memory.hpp>
20 #include <CPP/primitive.hpp>
21 #include <CPP/topology.hpp>
22 #include <CPP/pooling.hpp>
23 #include <CPP/eltwise.hpp>
24 #include <CPP/concatenation.hpp>
25 #include <CPP/detection_output.hpp>
26 #include <CPP/softmax.hpp>
27 #include <cpp_interfaces/impl/ie_executable_network_thread_safe_default.hpp>
28 #include <CPP/upsampling.hpp>
29 #include "cldnn_custom_layer.h"
31 namespace CLDNNPlugin {
34 std::shared_ptr<const cldnn::engine> engine;
35 std::shared_ptr<cldnn::network> network;
36 std::map<std::string, cldnn::primitive_id> primitiveIDs;
37 std::map<std::string, std::vector<cldnn::primitive_id>> prevPrimitiveIDs;
38 std::map<std::string, InferenceEngine::InferenceEngineProfileInfo> perfMap;
39 std::set<cldnn::primitive_id> profilingIDs;
41 DebugOptions debugOptions;
43 std::map<std::string, InferenceEngine::SizeVector> outputDims;
44 std::map<std::string, cldnn::layout> inputLayouts;
45 std::map<cldnn::primitive_id, cldnn::memory> constBlobs;
47 std::vector<std::shared_ptr<cldnn::network>> batchNetworks;
52 class CLDNNGraph : public InferenceEngine::ExecutableNetworkThreadSafeDefault {
54 typedef std::shared_ptr<CLDNNGraph> Ptr;
56 Config() : useProfiling(false), dumpCustomKernels(false), exclusiveAsyncRequests(false),
57 memory_pool_on(false),
58 enableDynamicBatch(false),
59 queuePriority(cldnn::priority_mode_types::disabled),
60 queueThrottle(cldnn::throttle_mode_types::disabled) {}
62 void LoadFromMap(const std::map<std::string, std::string>& configMap);
64 bool enableDynamicBatch;
66 bool dumpCustomKernels;
67 bool exclusiveAsyncRequests;
69 cldnn::priority_mode_types queuePriority;
70 cldnn::throttle_mode_types queueThrottle;
71 CLDNNCustomLayerMap customLayers;
72 cldnn::tuning_config_options tuningConfig;
73 std::string graph_dumps_dir;
74 std::string sources_dumps_dir;
76 explicit CLDNNGraph(InferenceEngine::ICNNNetwork &network, const Config& config = {}, int max_batch = -1);
78 InferenceEngine::InferRequestInternal::Ptr
79 CreateInferRequestImpl(InferenceEngine::InputsDataMap networkInputs, InferenceEngine::OutputsDataMap networkOutputs) override;
81 static bool IsLayerSupported(const std::string &type) {
82 return LayerTypeFromStr(type) != NO_TYPE;
87 std::shared_ptr<cldnn::topology> m_topology;
91 InferenceEngine::InputsDataMap* p_currentInputs;
92 InferenceEngine::OutputsDataMap* p_currentOutputs;
94 static const cldnn::primitive_id m_preProcessTag;
95 static const cldnn::primitive_id m_weightsTag;
96 static const cldnn::primitive_id m_biasesTag;
97 static const cldnn::primitive_id m_meanValuesTag;
98 static const cldnn::primitive_id m_postProcessTag;
99 static const cldnn::primitive_id m_scalesTag;
100 static const cldnn::primitive_id m_workaroundTag;
101 static const cldnn::primitive_id m_preCustomLayerTag;
102 static const cldnn::primitive_id m_postCustomLayerTag;
149 enum WeightRearrangeType {
155 cldnn::format m_defaultFormat;
156 cldnn::data_types m_networkPrecision;
157 void InitFormat(InferenceEngine::ICNNNetwork &network);
159 static cldnn::data_types DataTypeFromPrecision(InferenceEngine::Precision p);
160 static cldnn::format FormatFromLayout(InferenceEngine::Layout l);
161 static cldnn::upsampling_sample_type UpsamplingTypeFromString(const std::string& str);
163 void Load(InferenceEngine::ICNNNetwork &network);
164 static LayerType LayerTypeFromStr(const std::string& str);
165 static cldnn::pooling_mode PoolingModeFromIEPooling(InferenceEngine::PoolingLayer::PoolType pt, bool excludePadding = false);
166 static cldnn::eltwise_mode EltwiseModeFromIEEltwise(InferenceEngine::EltwiseLayer::eOperation op);
167 static cldnn::concatenation::concatenation_axis ConcatAxisFromIEAxis(unsigned axis);
168 static cldnn::prior_box_code_type PriorBoxCodeFromString(const std::string& str);
169 static cldnn::softmax::dimension_t SoftmaxDimensionFromIEAxis(const InferenceEngine::SoftMaxLayer* softmaxLayer, bool isPrevFC = false);
170 void CreatePrimitiveFromBlob(cldnn::primitive_id primID,
171 const InferenceEngine::Blob::Ptr pBlob,
172 cldnn::layout blobLayout,
173 size_t blobByteOffset = 0,
174 WeightRearrangeType rearrange = NO_REARRANGE);
175 void CreateWeightAndBiasPrimitives(const InferenceEngine::CNNLayerPtr& layer,
176 std::vector<cldnn::primitive_id>& weightsPrimID,
177 std::vector<cldnn::primitive_id>& biasesPrimID);
178 void CreateScaleWeightsAndBiasesFromBN(const InferenceEngine::BatchNormalizationLayer* bnLayer,
179 cldnn::primitive_id weightsPrimID,
180 cldnn::primitive_id biasesPrimID);
181 void AddPreProcessPrimitive(InferenceEngine::InputInfo::Ptr inputInfo);
182 void AddInputPrimitive(InferenceEngine::InputInfo::Ptr inputInfo);
183 void AddOutputPrimitive(std::string outputName, const InferenceEngine::DataPtr outputData,
184 InferenceEngine::Precision outputPrecision = InferenceEngine::Precision::UNSPECIFIED);
185 void CreateSingleLayerPrimitive(InferenceEngine::CNNLayerPtr& layer);
186 bool IsValidSplitConvMerge(const InferenceEngine::SplitLayer* splitLayer) const;
187 bool CanProcessDynBatch(InferenceEngine::ICNNNetwork &network) const;
188 static std::vector<InferenceEngine::CNNLayerPtr> GetNextLayers(const InferenceEngine::DataPtr data);
189 static std::vector<InferenceEngine::CNNLayerPtr> GetNextLayers(const InferenceEngine::CNNLayerPtr layer);
190 static InferenceEngine::CNNLayerPtr GetNextSingleLayer(const InferenceEngine::DataPtr data);
191 static InferenceEngine::CNNLayerPtr GetNextSingleLayer(const InferenceEngine::CNNLayerPtr layer);
192 std::vector<cldnn::primitive_id> GetPrevLayersPrimitives(const InferenceEngine::CNNLayerPtr layer) const;
193 void AddSingleValuePrimitive(cldnn::primitive_id valPrimID, cldnn::data_types dataType, float value);
195 void CreateGenericLayerBlobPrimitives(const InferenceEngine::GenericLayer* layer);
196 static void ValidateGenericLayerBlobs(const InferenceEngine::GenericLayer* layer, const std::vector<std::string>& blobNames);
197 static cldnn::tensor CldnnTensorFromIEDims(const InferenceEngine::SizeVector& dims);
198 static bool HasParam(const std::map<std::string, std::string>& layerParams, std::string paramName) {
199 auto p = layerParams.find(paramName);
200 return p != layerParams.end();
203 void InitProfileInfo(const std::string& layerName,
204 const std::string& layerType,
205 const std::string& execType,
206 InferenceEngine::InferenceEngineProfileInfo::LayerStatus status);
207 void changeInputBatch(size_t batch);
208 void CompileNetwork();
210 // Layer Primitive Creators
211 void CreatePReLUPrimitive(InferenceEngine::CNNLayerPtr &layer);
212 void CreateBatchNormalizationPrimitive(InferenceEngine::CNNLayerPtr & layer);
213 void CreateFlattenPrimitive(InferenceEngine::CNNLayerPtr &layer);
214 void CreatePermutePrimitive(InferenceEngine::CNNLayerPtr &layer);
215 void CreateReshapePrimitive(InferenceEngine::CNNLayerPtr &layer);
216 void CreateNormalizePrimitive(InferenceEngine::CNNLayerPtr &layer);
217 void CreateDetectionOutputPrimitive(InferenceEngine::CNNLayerPtr &layer);
218 void CreatePriorBoxPrimitive(InferenceEngine::CNNLayerPtr &layer);
219 void CreateDeconvolutionPrimitive(InferenceEngine::CNNLayerPtr &layer);
220 void CreateCropPrimitive(InferenceEngine::CNNLayerPtr &layer);
221 void CreateROIPoolingPrimitive(InferenceEngine::CNNLayerPtr &layer);
222 void CreateSimplerNMSPrimitive(InferenceEngine::CNNLayerPtr &layer);
223 void CreateEltwisePrimitive(InferenceEngine::CNNLayerPtr &layer);
224 void CreateConcatenatePrimitive(InferenceEngine::CNNLayerPtr &layer);
225 void CreateSplitPrimitive(InferenceEngine::CNNLayerPtr &layer);
226 void CreateFusedSplitConvMergePrimitive(InferenceEngine::CNNLayerPtr &layer);
227 void CreatePowerPrimitive(InferenceEngine::CNNLayerPtr &layer);
228 void CreateSoftMaxPrimitive(InferenceEngine::CNNLayerPtr &layer);
229 void CreateFullyConnectedPrimitive(InferenceEngine::CNNLayerPtr &layer);
230 void CreatePoolingPrimitive(InferenceEngine::CNNLayerPtr &layer);
231 void CreateLRNPrimitive(InferenceEngine::CNNLayerPtr &layer);
232 void CreateActivationPrimitive(InferenceEngine::CNNLayerPtr &layer, const LayerType type);
233 void CreateConvolutionPrimitive(InferenceEngine::CNNLayerPtr &layer);
234 void CreateScaleShiftPrimitive(InferenceEngine::CNNLayerPtr &layer);
235 void CreateProposalPrimitive(InferenceEngine::CNNLayerPtr &layer);
236 void CreatePSROIPoolingPrimitive(InferenceEngine::CNNLayerPtr &layer);
237 void CreateCopyPrimitive(InferenceEngine::CNNLayerPtr &layer);
238 void CreateUpsamplingPrimitive(InferenceEngine::CNNLayerPtr &layer);
239 void CreateResamplePrimitive(InferenceEngine::CNNLayerPtr &layer);
240 void CreateYOLO2RegionPrimitive(InferenceEngine::CNNLayerPtr &layer);
241 void CreateYOLO2ReorgPrimitive(InferenceEngine::CNNLayerPtr &layer);
242 void CreateArgMaxPrimitive(InferenceEngine::CNNLayerPtr &layer);
243 void CreateMaxUnpoolingPrimitive(InferenceEngine::CNNLayerPtr &layer);
244 void CreateMVNPrimitive(InferenceEngine::CNNLayerPtr &layer);
245 void AddConstantBlobInput(InferenceEngine::CNNLayerPtr &layer);
246 void CreateCustomLayerPrimitive(InferenceEngine::CNNLayerPtr &layer, CLDNNCustomLayerPtr customLayer);
249 }; // namespace CLDNNPlugin