1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
15 #include <cpp/ie_cnn_network.h>
16 #include <cpp_interfaces/exception2status.hpp>
18 #include <ie_plugin.hpp>
19 #include <inference_engine.hpp>
21 #include "debug_options.h"
22 #include "cldnn_custom_layer.h"
23 #include "cldnn_config.h"
25 #include <CPP/engine.hpp>
26 #include <CPP/memory.hpp>
27 #include <CPP/topology.hpp>
28 #include <CPP/primitive.hpp>
29 #include <CPP/softmax.hpp>
30 #include <CPP/upsampling.hpp>
31 #include <CPP/pooling.hpp>
32 #include <CPP/eltwise.hpp>
33 #include <CPP/concatenation.hpp>
34 #include <CPP/detection_output.hpp>
40 #define THROW_CLDNN_EXCEPTION(desc)\
42 InferenceEngineException ex(__FILE__, __LINE__);\
43 std::cout << desc << "\n---\nException detected at " << __FILE__ << ":" << \
44 __LINE__ << " (" << __FUNCTION__ << ")\n---\n" << std::endl; THROW_IE_EXCEPTION << desc; } while (0);
46 #define THROW_CLDNN_EXCEPTION(desc) THROW_IE_EXCEPTION << desc;
48 #define TensorValue(val) static_cast<cldnn::tensor::value_type>(val)
50 namespace CLDNNPlugin {
51 template<typename LayerTypePtr>
52 LayerTypePtr tryAs(const InferenceEngine::CNNLayerPtr& in_ptr) {
53 return dynamic_cast<LayerTypePtr>(in_ptr.get());
56 template<typename LayerTypePtr>
57 LayerTypePtr as(const InferenceEngine::CNNLayerPtr& in_ptr) {
58 auto result_ptr = dynamic_cast<LayerTypePtr> (in_ptr.get());
59 if (nullptr == result_ptr) {
60 THROW_IE_EXCEPTION << "CNNLayerPtr is not suitable for casting to requested layer type";
65 inline std::string layer_type_lower(const InferenceEngine::CNNLayer* layer) {
66 std::string layerType = layer->type;
67 std::transform(layerType.begin(), layerType.end(), layerType.begin(),
68 [](unsigned char c) -> unsigned char { return std::tolower(c); });
72 inline std::string layer_type_name_ID(const InferenceEngine::CNNLayer* layer) {
73 return layer_type_lower(layer) + ":" + layer->name;
76 inline std::string layer_type_lower(InferenceEngine::CNNLayerPtr layer) {
77 return layer_type_lower(layer.get());
80 inline std::string layer_type_name_ID(InferenceEngine::CNNLayerPtr layer) {
81 return layer_type_name_ID(layer.get());
85 InferenceEngine::InferenceEngineProfileInfo::LayerStatus status;
87 uint64_t realTime_uSec;
90 std::string layerType;
93 PerfCounter() : realTime_uSec(0), cpu_uSec(0), num(0),
94 status(InferenceEngine::InferenceEngineProfileInfo::NOT_RUN), isCPU(false) {}
96 long long realTime_avg() const { return (num == 0) ? 0 : realTime_uSec / num; }
97 long long cpu_avg() const { return (num == 0) ? 0 : cpu_uSec / num; }
102 Program(InferenceEngine::ICNNNetwork &network, std::shared_ptr<const cldnn::engine> engine, const Config& config);
103 std::shared_ptr<cldnn::program> getCompiledProgram(int program_id = 0);
105 std::map<std::string, cldnn::primitive_id> primitiveIDs;
106 std::map<cldnn::primitive_id, std::vector<std::string>> primitivesToIRLayersMap;
107 std::map<std::string, std::vector<cldnn::primitive_id>> prevPrimitiveIDs;
108 std::map<cldnn::primitive_id, std::pair<std::string, PerfCounter>> perfMap;
110 std::vector<cldnn::primitive_id> profilingIDs;
112 std::map<std::string, InferenceEngine::SizeVector> outputDims;
113 std::map<std::string, cldnn::layout> inputLayouts;
114 std::map<const char *, cldnn::primitive_id> blobMemCache;
119 InferenceEngine::OutputsDataMap p_currentOutputs;
121 std::vector<cldnn::primitive_id> GetPrevLayersPrimitives(const InferenceEngine::CNNLayerPtr layer) const;
122 const std::map<std::string, cldnn::layout>& getInputLayouts() const { return inputLayouts; }
123 int GetMaxBatchSizeForSingleProgram();
129 DeformableConvolution,
211 using GenericBlobMap = std::map<cldnn::primitive_id, cldnn::primitive_id>;
213 static LayerType LayerTypeFromStr(const std::string& str);
216 std::vector<std::shared_ptr<cldnn::program>> m_programs;
217 std::shared_ptr<const cldnn::engine> m_engine;
220 std::shared_ptr<cldnn::program> BuildProgram(InferenceEngine::ICNNNetwork &network);
222 void InitProfileInfo(const std::string& layerName,
223 const std::string& layerType,
225 InferenceEngine::InferenceEngineProfileInfo::LayerStatus status
226 = InferenceEngine::InferenceEngineProfileInfo::EXECUTED);
228 static const cldnn::primitive_id m_preProcessTag;
229 static const cldnn::primitive_id m_weightsTag;
230 static const cldnn::primitive_id m_biasesTag;
231 static const cldnn::primitive_id m_meanValuesTag;
232 static const cldnn::primitive_id m_postProcessTag;
233 static const cldnn::primitive_id m_scalesTag;
234 static const cldnn::primitive_id m_workaroundTag;
235 static const cldnn::primitive_id m_preCustomLayerTag;
236 static const cldnn::primitive_id m_postCustomLayerTag;
239 enum WeightRearrangeType {
245 cldnn::format m_defaultFormat;
246 void InitFormat(InferenceEngine::ICNNNetwork &network);
248 static cldnn::data_types DataTypeFromPrecision(InferenceEngine::Precision p);
249 static cldnn::format FormatFromLayout(InferenceEngine::Layout l);
250 static cldnn::upsampling_sample_type UpsamplingTypeFromString(const std::string& str);
252 void Load(InferenceEngine::ICNNNetwork &network);
253 static cldnn::pooling_mode PoolingModeFromIEPooling(InferenceEngine::PoolingLayer::PoolType pt, bool excludePadding = false);
254 static cldnn::eltwise_mode EltwiseModeFromIEEltwise(InferenceEngine::EltwiseLayer::eOperation op);
255 static cldnn::prior_box_code_type PriorBoxCodeFromString(const std::string& str);
256 static cldnn::softmax::dimension_t SoftmaxDimensionFromIEAxis(const InferenceEngine::SoftMaxLayer* softmaxLayer);
257 cldnn::primitive_id CreatePrimitiveFromBlob(cldnn::topology& topology,
258 cldnn::primitive_id primID,
259 const InferenceEngine::Blob::Ptr pBlob,
260 const cldnn::layout& blobLayout,
261 size_t blobByteOffset = 0,
262 WeightRearrangeType rearrange = NO_REARRANGE);
263 void CreateWeightAndBiasPrimitives(cldnn::topology& topology,
264 const InferenceEngine::CNNLayerPtr& layer,
265 std::vector<cldnn::primitive_id>& weightsPrimID,
266 std::vector<cldnn::primitive_id>& biasesPrimID);
267 void CreateBinaryWeightAndBiasPrimitives(cldnn::topology& topology,
268 const InferenceEngine::CNNLayerPtr& layer,
269 std::vector<cldnn::primitive_id>& weightsPrimID,
270 std::vector<cldnn::primitive_id>& biasesPrimID);
271 void CreateScaleWeightsAndBiasesFromBN(cldnn::topology& topology,
272 const InferenceEngine::BatchNormalizationLayer* bnLayer,
273 cldnn::primitive_id& weightsPrimID,
274 cldnn::primitive_id& biasesPrimID);
275 void CreateQuantizationPrimitives(cldnn::topology& topology,
276 const InferenceEngine::CNNLayerPtr& layer,
277 std::vector<cldnn::primitive_id>& weightsQuantizationPrimID,
278 bool supportsDequantization,
280 void AddPreProcessPrimitive(InferenceEngine::InputInfo::Ptr inputInfo);
281 void AddInputPrimitive(cldnn::topology& topology,
282 InferenceEngine::InputInfo::Ptr inputInfo, InferenceEngine::Precision inputPrecision, const std::string inputName);
283 void AddOutputPrimitive(cldnn::topology& topology,
284 std::string outputName, const InferenceEngine::DataPtr outputData,
285 InferenceEngine::Precision outputPrecision = InferenceEngine::Precision::UNSPECIFIED);
286 void CreateSingleLayerPrimitive(cldnn::topology& topology, InferenceEngine::CNNLayerPtr& layer);
287 bool IsValidSplitConvMerge(const InferenceEngine::SplitLayer* splitLayer) const;
288 bool CanProcessDynBatch(InferenceEngine::ICNNNetwork &network) const;
289 static std::vector<InferenceEngine::CNNLayerPtr> GetNextLayers(const InferenceEngine::DataPtr data);
290 static std::vector<InferenceEngine::CNNLayerPtr> GetNextLayers(const InferenceEngine::CNNLayerPtr layer);
291 static InferenceEngine::CNNLayerPtr GetNextSingleLayer(const InferenceEngine::DataPtr data);
292 static InferenceEngine::CNNLayerPtr GetNextSingleLayer(const InferenceEngine::CNNLayerPtr layer);
293 void AddSingleValuePrimitive(cldnn::topology& topology, cldnn::primitive_id valPrimID, cldnn::data_types dataType, float value);
295 GenericBlobMap CreateGenericLayerBlobPrimitives(cldnn::topology& topology, const InferenceEngine::GenericLayer* layer);
296 static void ValidateGenericLayerBlobs(const InferenceEngine::GenericLayer* layer, const std::vector<std::string>& blobNames);
297 static bool HasParam(const std::map<std::string, std::string>& layerParams, std::string paramName) {
298 auto p = layerParams.find(paramName);
299 return p != layerParams.end();
302 void changeInputBatch(int batch);
304 // Layer Primitive Creators
305 void CreatePReLUPrimitive(cldnn::topology& topology, InferenceEngine::CNNLayerPtr &layer);
306 void CreateBatchNormalizationPrimitive(cldnn::topology& topology, InferenceEngine::CNNLayerPtr & layer);
307 void CreateFlattenPrimitive(cldnn::topology& topology, InferenceEngine::CNNLayerPtr &layer);
308 void CreatePermutePrimitive(cldnn::topology& topology, InferenceEngine::CNNLayerPtr &layer);
309 void CreateReshapePrimitive(cldnn::topology& topology, InferenceEngine::CNNLayerPtr &layer);
310 void CreateNormalizePrimitive(cldnn::topology& topology, InferenceEngine::CNNLayerPtr &layer);
311 void CreateDetectionOutputPrimitive(cldnn::topology& topology, InferenceEngine::CNNLayerPtr &layer);
312 void CreatePriorBoxPrimitive(cldnn::topology& topology, InferenceEngine::CNNLayerPtr &layer);
313 void CreateDeconvolutionPrimitive(cldnn::topology& topology, InferenceEngine::CNNLayerPtr &layer);
314 void CreateCropPrimitive(cldnn::topology& topology, InferenceEngine::CNNLayerPtr &layer);
315 void CreateROIPoolingPrimitive(cldnn::topology& topology, InferenceEngine::CNNLayerPtr &layer);
316 void CreateSimplerNMSPrimitive(cldnn::topology& topology, InferenceEngine::CNNLayerPtr &layer);
317 void CreateEltwisePrimitive(cldnn::topology& topology, InferenceEngine::CNNLayerPtr &layer);
318 void CreateConcatenatePrimitive(cldnn::topology& topology, InferenceEngine::CNNLayerPtr &layer);
319 void CreateSplitPrimitive(cldnn::topology& topology, InferenceEngine::CNNLayerPtr &layer);
320 void CreateFusedSplitConvMergePrimitive(cldnn::topology& topology, InferenceEngine::CNNLayerPtr &layer);
321 void CreatePowerPrimitive(cldnn::topology& topology, InferenceEngine::CNNLayerPtr &layer);
322 void CreateSoftMaxPrimitive(cldnn::topology& topology, InferenceEngine::CNNLayerPtr &layer);
323 void CreateFullyConnectedPrimitive(cldnn::topology& topology, InferenceEngine::CNNLayerPtr &layer);
324 void CreatePoolingPrimitive(cldnn::topology& topology, InferenceEngine::CNNLayerPtr &layer);
325 void CreateLRNPrimitive(cldnn::topology& topology, InferenceEngine::CNNLayerPtr &layer);
326 void CreateActivationPrimitive(cldnn::topology& topology, InferenceEngine::CNNLayerPtr &layer, const LayerType type);
327 void CreateConvolutionPrimitive(cldnn::topology& topology, InferenceEngine::CNNLayerPtr &layer);
328 void CreateDeformableConvolutionPrimitive(cldnn::topology& topology, InferenceEngine::CNNLayerPtr &layer);
329 void CreateScaleShiftPrimitive(cldnn::topology& topology, InferenceEngine::CNNLayerPtr &layer);
330 void CreateProposalPrimitive(cldnn::topology& topology, InferenceEngine::CNNLayerPtr &layer);
331 void CreatePSROIPoolingPrimitive(cldnn::topology& topology, InferenceEngine::CNNLayerPtr &layer);
332 void CreateCopyPrimitive(cldnn::topology& topology, InferenceEngine::CNNLayerPtr &layer);
333 void CreateUpsamplingPrimitive(cldnn::topology& topology, InferenceEngine::CNNLayerPtr &layer);
334 void CreateResamplePrimitive(cldnn::topology& topology, InferenceEngine::CNNLayerPtr &layer);
335 void CreateYOLO2RegionPrimitive(cldnn::topology& topology, InferenceEngine::CNNLayerPtr &layer);
336 void CreateYOLO2ReorgPrimitive(cldnn::topology& topology, InferenceEngine::CNNLayerPtr &layer);
337 void CreateArgMaxMinPrimitive(cldnn::topology& topology, InferenceEngine::CNNLayerPtr &layer, const LayerType type);
338 void CreateTopKPrimitive(cldnn::topology& topology, InferenceEngine::CNNLayerPtr &layer);
339 void CreateMaxUnpoolingPrimitive(cldnn::topology& topology, InferenceEngine::CNNLayerPtr &layer);
340 void CreateMVNPrimitive(cldnn::topology& topology, InferenceEngine::CNNLayerPtr &layer);
341 void CreateTilePrimitive(cldnn::topology& topology, InferenceEngine::CNNLayerPtr &layer);
342 void CreatePadPrimitive(cldnn::topology& topology, InferenceEngine::CNNLayerPtr &layer);
343 void CreateRegularLSTM(cldnn::topology& topology, InferenceEngine::CNNLayerPtr &layer);
344 void CreateDynamicLSTM(cldnn::topology& topology, InferenceEngine::CNNLayerPtr &layer);
345 void CreateRNNPrimitive(cldnn::topology& topology, InferenceEngine::CNNLayerPtr &layer);
346 void CreateLSTMCellPrimitive(cldnn::topology& topology, InferenceEngine::CNNLayerPtr &layer);
347 void AddConstantBlobInput(cldnn::topology& topology, InferenceEngine::CNNLayerPtr &layer);
348 void CreateCustomLayerPrimitive(cldnn::topology& topology, InferenceEngine::CNNLayerPtr &layer, CLDNNCustomLayerPtr customLayer);
349 void CreateGatherPrimitive(cldnn::topology& topology, InferenceEngine::CNNLayerPtr &layer);
350 void CreateDepthToSpacePrimitive(cldnn::topology& topology, InferenceEngine::CNNLayerPtr &layer);
351 void CreateShuffleChannelsPrimitive(cldnn::topology& topology, InferenceEngine::CNNLayerPtr &layer);
352 void CreateStridedSlicePrimitive(cldnn::topology& topology, InferenceEngine::CNNLayerPtr &layer);
353 void CreateBroadcastPrimitive(cldnn::topology &topology, InferenceEngine::CNNLayerPtr &layer);
354 void CreateReverseSequencePrimitive(cldnn::topology& topology, InferenceEngine::CNNLayerPtr &layer);
355 void CreateBinaryConvolutionPrimitive(cldnn::topology& topology, InferenceEngine::CNNLayerPtr &layer);
356 void CreateQuantizePrimitive(cldnn::topology& topology, InferenceEngine::CNNLayerPtr &layer);
357 void CreateGemmPrimitive(cldnn::topology& topology, InferenceEngine::CNNLayerPtr &layer);
358 void CreateReducePrimitive(cldnn::topology& topology, InferenceEngine::CNNLayerPtr &layer);
359 void CreateOneHotPrimitive(cldnn::topology& topology, InferenceEngine::CNNLayerPtr &layer);
362 } // namespace CLDNNPlugin