Skip to content

Commit ec48fcb

Browse files
CPU plugin selective build (#3360)
1 parent 78d09d9 commit ec48fcb

File tree

18 files changed

+356
-279
lines changed

18 files changed

+356
-279
lines changed

inference-engine/src/mkldnn_plugin/CMakeLists.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ set_ie_threading_interface_for(${TARGET_NAME})
169169
target_compile_definitions(${TARGET_NAME} PUBLIC -DMKLDNN_THR=${MKLDNN_THR})
170170

171171
target_link_libraries(${TARGET_NAME} PRIVATE mkldnn inference_engine inference_engine_legacy
172-
inference_engine_transformations inference_engine_lp_transformations)
172+
inference_engine_transformations inference_engine_lp_transformations openvino::conditional_compilation)
173173

174174
# Cross compiled function
175175
# TODO: The same for proposal, proposalONNX, topk
@@ -198,6 +198,7 @@ target_include_directories(${TARGET_NAME}_obj PRIVATE $<TARGET_PROPERTY:inferenc
198198
$<TARGET_PROPERTY:inference_engine_legacy,INTERFACE_INCLUDE_DIRECTORIES>
199199
$<TARGET_PROPERTY:inference_engine_transformations,INTERFACE_INCLUDE_DIRECTORIES>
200200
$<TARGET_PROPERTY:openvino::itt,INTERFACE_INCLUDE_DIRECTORIES>
201+
$<TARGET_PROPERTY:openvino::conditional_compilation,INTERFACE_INCLUDE_DIRECTORIES>
201202
$<TARGET_PROPERTY:inference_engine_lp_transformations,INTERFACE_INCLUDE_DIRECTORIES>)
202203

203204
set_ie_threading_interface_for(${TARGET_NAME}_obj)

inference-engine/src/mkldnn_plugin/mkldnn_node.cpp

+9-21
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ Type TypeFromName(const std::string type) {
152152

153153
} // namespace MKLDNNPlugin
154154

155-
MKLDNNNode::Factory & MKLDNNNode::factory() {
156-
static Factory factoryInstance;
155+
MKLDNNNode::NodesFactory & MKLDNNNode::factory() {
156+
static NodesFactory factoryInstance;
157157
return factoryInstance;
158158
}
159159

@@ -1121,28 +1121,20 @@ void MKLDNNNode::appendPostOps(mkldnn::post_ops& ops) {
11211121
THROW_IE_EXCEPTION << "Fusing of " << this->getType() << " operation is not implemented";
11221122
}
11231123

1124-
MKLDNNNode* MKLDNNNode::Factory::create(const InferenceEngine::CNNLayerPtr& layer, const mkldnn::engine& eng,
1125-
const MKLDNNExtensionManager::Ptr& extMgr, MKLDNNWeightsSharing::Ptr &w_cache) {
1124+
MKLDNNNode* MKLDNNNode::NodesFactory::create(const InferenceEngine::CNNLayerPtr& layer, const mkldnn::engine& eng,
1125+
const MKLDNNExtensionManager::Ptr& extMgr, MKLDNNWeightsSharing::Ptr &w_cache) {
11261126
MKLDNNNode *newNode = nullptr;
11271127

1128-
auto builder = builders.find(Generic);
1128+
std::unique_ptr<MKLDNNNode> ol(createNodeIfRegistered(MKLDNNPlugin, Generic, layer, eng, w_cache));
1129+
if (ol != nullptr && ol->created(extMgr))
1130+
newNode = ol.release();
11291131

1130-
if (builder != builders.end()) {
1131-
std::unique_ptr<MKLDNNNode> ol(builder->second(layer, eng, w_cache));
1132+
if (newNode == nullptr) {
1133+
std::unique_ptr<MKLDNNNode> ol(createNodeIfRegistered(MKLDNNPlugin, TypeFromName(layer->type), layer, eng, w_cache));
11321134
if (ol != nullptr && ol->created(extMgr))
11331135
newNode = ol.release();
11341136
}
11351137

1136-
if (newNode == nullptr) {
1137-
builder = builders.find(TypeFromName(layer->type));
1138-
1139-
if (builder != builders.end()) {
1140-
std::unique_ptr<MKLDNNNode> ol(builder->second(layer, eng, w_cache));
1141-
if (ol != nullptr && ol->created(extMgr))
1142-
newNode = ol.release();
1143-
}
1144-
}
1145-
11461138
// WA-start : TI node requires all attributes to construct internal subgpath
11471139
// including extManager, socket and mkldnn::eng.
11481140
#if defined (COMPILED_CPU_MKLDNN_TENSORITERATOR_NODE)
@@ -1157,7 +1149,3 @@ MKLDNNNode* MKLDNNNode::Factory::create(const InferenceEngine::CNNLayerPtr& laye
11571149

11581150
return newNode;
11591151
}
1160-
1161-
void MKLDNNNode::Factory::registerNode(Type type, builder_t builder) {
1162-
builders[type] = builder;
1163-
}

inference-engine/src/mkldnn_plugin/mkldnn_node.h

+24-30
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "mkldnn_memory.h"
1717
#include "mkldnn_edge.h"
1818
#include "mkldnn_descriptor.h"
19+
#include "mkldnn_selective_build.h"
1920
#include "mkldnn/iml_type_mapper.h"
2021
#include "mkldnn_extension_mngr.h"
2122
#include "mkldnn_primitive.h"
@@ -257,10 +258,6 @@ class PrimitiveDescInfo {
257258

258259
class MKLDNNNode : public InferenceEngine::details::no_copy {
259260
public:
260-
class Factory;
261-
template<typename To>
262-
class Registrar;
263-
264261
template<typename T, int N>
265262
struct Tag {};
266263

@@ -294,7 +291,8 @@ class MKLDNNNode : public InferenceEngine::details::no_copy {
294291
openvino::itt::handle_t initOptimalPrimitiveDescriptor;
295292
};
296293

297-
static Factory & factory();
294+
class NodesFactory;
295+
static NodesFactory & factory();
298296

299297
~MKLDNNNode() override = default;
300298

@@ -628,40 +626,36 @@ class MKLDNNNode : public InferenceEngine::details::no_copy {
628626
ConstantType checkConstant(LOOK look, std::vector<MKLDNNNodePtr>& checkNodes);
629627
};
630628

631-
class MKLDNNNode::Factory : InferenceEngine::details::no_copy {
629+
class MKLDNNNode::NodesFactory : public openvino::cc::Factory<Type,
630+
MKLDNNNode*(const InferenceEngine::CNNLayerPtr&,
631+
const mkldnn::engine &,
632+
MKLDNNWeightsSharing::Ptr &)> {
632633
public:
633-
using builder_t = std::function<MKLDNNNode *(const InferenceEngine::CNNLayerPtr& layer,
634-
const mkldnn::engine& eng, MKLDNNWeightsSharing::Ptr &w_cache)>;
634+
NodesFactory()
635+
: Factory("NodesFactory") {}
635636

636637
MKLDNNNode* create(const InferenceEngine::CNNLayerPtr& layer, const mkldnn::engine& eng,
637638
const MKLDNNExtensionManager::Ptr& extMgr, MKLDNNWeightsSharing::Ptr &w_cache);
638-
639-
void registerNode(Type type, builder_t builder);
640-
641-
private:
642-
using map_t = std::unordered_map<Type, builder_t,
643-
std::hash<std::underlying_type<MKLDNNPlugin::Type>::type>>;
644-
map_t builders;
645639
};
646640

647-
template<typename To>
648-
class MKLDNNNode::Registrar {
649-
public:
650-
explicit Registrar(Type type) {
651-
MKLDNNNode::factory().registerNode(type,
652-
[type](const InferenceEngine::CNNLayerPtr& layer, const mkldnn::engine& eng,
653-
MKLDNNWeightsSharing::Ptr &w_cache) -> MKLDNNNode* {
654-
MKLDNNNode *node = new To(layer, eng, w_cache);
655-
node->perfCounters().buildClassCounters<To>(NameFromType(type));
656-
return node;
657-
});
641+
template<typename MKLDNNNodeType>
642+
struct MKLDNNNodeImpl : public MKLDNNNodeType {
643+
MKLDNNNodeImpl(const InferenceEngine::CNNLayerPtr& layer, const mkldnn::engine& eng, MKLDNNWeightsSharing::Ptr &cache)
644+
: MKLDNNNodeType(layer, eng, cache) {
645+
MKLDNNNodeType::perfCounters().template buildClassCounters<MKLDNNNodeType>(NameFromType(MKLDNNNodeType::getType()));
658646
}
659647
};
660648

661-
#define REG_MKLDNN_CONCAT2(X, Y) X ## Y
662-
#define REG_MKLDNN_CONCAT(X, Y) REG_MKLDNN_CONCAT2(X, Y)
663-
#define REG_MKLDNN_PRIM_FOR(__prim, __type) \
664-
static MKLDNNNode::Registrar<__prim> REG_MKLDNN_CONCAT(_reg_, __LINE__)(__type)
649+
#define REG_MKLDNN_CONCAT3_(X, Y, Z) X ## Y ## Z
650+
#define REG_MKLDNN_CONCAT3(X, Y, Z) REG_MKLDNN_CONCAT3_(X, Y, Z)
651+
652+
#define REG_MKLDNN_PRIM_FOR(__prim, __type) \
653+
static struct REG_MKLDNN_CONCAT3(Registrar4, __prim, __LINE__) { \
654+
REG_MKLDNN_CONCAT3(Registrar4, __prim, __LINE__)() { \
655+
MKLDNNNode::factory() \
656+
.registerNodeIfRequired(MKLDNNPlugin, __prim, __type, MKLDNNNodeImpl<__prim>); \
657+
} \
658+
} REG_MKLDNN_CONCAT3(_reg_, __prim, __LINE__);
665659

666660
template <typename T, typename U>
667661
inline T div_up(const T a, const U b) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Copyright (C) 2020 Intel Corporation
2+
// SPDX-License-Identifier: Apache-2.0
3+
//
4+
5+
#pragma once
6+
#include <openvino/cc/factory.h>
7+
8+
namespace MKLDNNPlugin {
9+
OV_CC_DOMAINS(MKLDNNPlugin)
10+
}

inference-engine/src/mkldnn_plugin/nodes/base.hpp

+2-9
Original file line numberDiff line numberDiff line change
@@ -169,17 +169,10 @@ class ImplFactory : public ILayerImplFactory {
169169
InferenceEngine::CNNLayerPtr cnnLayer;
170170
};
171171

172-
template <typename __prim>
173-
inline void extRegister(MKLDNNExtensions * extInstance, const char * __type) {
174-
extInstance->AddExt(__type,
175-
[](const CNNLayer* layer) -> InferenceEngine::ILayerImplFactory* {
176-
return new __prim(layer);
177-
});
178-
}
179-
180172
#define REG_FACTORY_FOR(__prim, __type) \
181173
void __prim ## __type(MKLDNNExtensions * extInstance) { \
182-
extRegister<ImplFactory<__prim>>(extInstance, #__type); \
174+
using namespace MKLDNNPlugin; \
175+
extInstance->layersFactory.registerNodeIfRequired(MKLDNNPlugin, __type, OV_CC_TOSTRING(__type), ImplFactory<__prim>); \
183176
}
184177

185178
} // namespace Cpu

inference-engine/src/mkldnn_plugin/nodes/common/cpu_convert.cpp

+74-70
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@
55
#include "cpu_convert.h"
66
#include "cpu_memcpy.h"
77
#include "utils/bfloat16.hpp"
8+
#include <mkldnn_selective_build.h>
89
#include <type_traits>
10+
#include <tuple>
911
#include <ie_parallel.hpp>
1012

1113
using namespace InferenceEngine;
1214

15+
namespace {
16+
1317
template<typename srcType, typename dstType>
1418
void convert(const void *srcPtr, void *dstPtr, const size_t size) {
1519
if (std::is_same<srcType, dstType>::value) {
@@ -24,45 +28,41 @@ void convert(const void *srcPtr, void *dstPtr, const size_t size) {
2428
}
2529
}
2630

27-
template <typename srcType>
28-
void convertFrom(const void *srcPtr, void *dstPtr, Precision dstPrc, const size_t size) {
29-
switch (dstPrc) {
30-
case Precision::U8:
31-
convert<srcType, PrecisionTrait<Precision::U8>::value_type>(srcPtr, dstPtr, size);
32-
break;
33-
case Precision::I8:
34-
convert<srcType, PrecisionTrait<Precision::I8>::value_type>(srcPtr, dstPtr, size);
35-
break;
36-
case Precision::U16:
37-
convert<srcType, PrecisionTrait<Precision::U16>::value_type>(srcPtr, dstPtr, size);
38-
break;
39-
case Precision::I16:
40-
convert<srcType, PrecisionTrait<Precision::I16>::value_type>(srcPtr, dstPtr, size);
41-
break;
42-
case Precision::I32:
43-
convert<srcType, PrecisionTrait<Precision::I32>::value_type>(srcPtr, dstPtr, size);
44-
break;
45-
case Precision::U64:
46-
convert<srcType, PrecisionTrait<Precision::U64>::value_type>(srcPtr, dstPtr, size);
47-
break;
48-
case Precision::I64:
49-
convert<srcType, PrecisionTrait<Precision::I64>::value_type>(srcPtr, dstPtr, size);
50-
break;
51-
case Precision::FP32:
52-
convert<srcType, PrecisionTrait<Precision::FP32>::value_type>(srcPtr, dstPtr, size);
53-
break;
54-
case Precision::BF16:
55-
convert<srcType, MKLDNNPlugin::bfloat16_t>(srcPtr, dstPtr, size);
56-
break;
57-
case Precision::BOOL:
58-
convert<srcType, PrecisionTrait<Precision::BOOL>::value_type>(srcPtr, dstPtr, size);
59-
break;
60-
default:
61-
THROW_IE_EXCEPTION << "cpu_convert can't convert to: " << dstPrc << " precision";
31+
template <Precision::ePrecision p>
32+
struct PrecisionInfo {
33+
using value_type = typename PrecisionTrait<p>::value_type;
34+
};
35+
36+
template <>
37+
struct PrecisionInfo<Precision::BF16> {
38+
using value_type = MKLDNNPlugin::bfloat16_t;
39+
};
40+
41+
struct ConvertContext {
42+
const void *srcPtr;
43+
void *dstPtr;
44+
size_t size;
45+
bool converted;
46+
};
47+
48+
template<typename T>
49+
struct ConvertPrecision {
50+
using src_t = typename std::tuple_element<0, T>::type;
51+
using dst_t = typename std::tuple_element<1, T>::type;
52+
53+
void operator()(ConvertContext & ctx) {
54+
convert<src_t, dst_t>(ctx.srcPtr, ctx.dstPtr, ctx.size);
55+
ctx.converted = true;
6256
}
63-
}
57+
};
58+
59+
} // namespace
60+
61+
#define MKLDNN_CVT(ST, DT) OV_CASE2(Precision::ST, Precision::DT, PrecisionInfo<Precision::ST>::value_type, PrecisionInfo<Precision::DT>::value_type)
6462

6563
void cpu_convert(const void *srcPtr, void *dstPtr, Precision srcPrc, Precision dstPrc, const size_t size) {
64+
using namespace MKLDNNPlugin;
65+
6666
if (srcPtr == nullptr || dstPtr == nullptr)
6767
THROW_IE_EXCEPTION << "cpu_convert has null data pointer";
6868

@@ -71,38 +71,42 @@ void cpu_convert(const void *srcPtr, void *dstPtr, Precision srcPrc, Precision d
7171
return;
7272
}
7373

74-
switch (srcPrc) {
75-
case Precision::U8:
76-
convertFrom<PrecisionTrait<Precision::U8>::value_type>(srcPtr, dstPtr, dstPrc, size);
77-
break;
78-
case Precision::I8:
79-
convertFrom<PrecisionTrait<Precision::I8>::value_type>(srcPtr, dstPtr, dstPrc, size);
80-
break;
81-
case Precision::U16:
82-
convertFrom<PrecisionTrait<Precision::U16>::value_type>(srcPtr, dstPtr, dstPrc, size);
83-
break;
84-
case Precision::I16:
85-
convertFrom<PrecisionTrait<Precision::I16>::value_type>(srcPtr, dstPtr, dstPrc, size);
86-
break;
87-
case Precision::I32:
88-
convertFrom<PrecisionTrait<Precision::I32>::value_type>(srcPtr, dstPtr, dstPrc, size);
89-
break;
90-
case Precision::U64:
91-
convertFrom<PrecisionTrait<Precision::U64>::value_type>(srcPtr, dstPtr, dstPrc, size);
92-
break;
93-
case Precision::I64:
94-
convertFrom<PrecisionTrait<Precision::I64>::value_type>(srcPtr, dstPtr, dstPrc, size);
95-
break;
96-
case Precision::FP32:
97-
convertFrom<PrecisionTrait<Precision::FP32>::value_type>(srcPtr, dstPtr, dstPrc, size);
98-
break;
99-
case Precision::BF16:
100-
convertFrom<MKLDNNPlugin::bfloat16_t>(srcPtr, dstPtr, dstPrc, size);
101-
break;
102-
case Precision::BOOL:
103-
convertFrom<PrecisionTrait<Precision::BOOL>::value_type>(srcPtr, dstPtr, dstPrc, size);
104-
break;
105-
default:
106-
THROW_IE_EXCEPTION << "cpu_convert can't convert from: " << srcPrc << " precision";
107-
}
74+
ConvertContext ctx = { srcPtr, dstPtr, size, false };
75+
76+
OV_SWITCH(MKLDNNPlugin, ConvertPrecision, ctx, std::tie(srcPrc, dstPrc),
77+
MKLDNN_CVT(U8, I8), MKLDNN_CVT(U8, U16), MKLDNN_CVT(U8, I16),
78+
MKLDNN_CVT(U8, I32), MKLDNN_CVT(U8, U64), MKLDNN_CVT(U8, I64),
79+
MKLDNN_CVT(U8, FP32), MKLDNN_CVT(U8, BF16), MKLDNN_CVT(U8, BOOL),
80+
MKLDNN_CVT(I8, U8), MKLDNN_CVT(I8, U16), MKLDNN_CVT(I8, I16),
81+
MKLDNN_CVT(I8, I32), MKLDNN_CVT(I8, U64), MKLDNN_CVT(I8, I64),
82+
MKLDNN_CVT(I8, FP32), MKLDNN_CVT(I8, BF16), MKLDNN_CVT(I8, BOOL),
83+
MKLDNN_CVT(U16, U8), MKLDNN_CVT(U16, I8), MKLDNN_CVT(U16, I16),
84+
MKLDNN_CVT(U16, I32), MKLDNN_CVT(U16, U64), MKLDNN_CVT(U16, I64),
85+
MKLDNN_CVT(U16, FP32), MKLDNN_CVT(U16, BF16), MKLDNN_CVT(U16, BOOL),
86+
MKLDNN_CVT(I16, U8), MKLDNN_CVT(I16, I8), MKLDNN_CVT(I16, U16),
87+
MKLDNN_CVT(I16, I32), MKLDNN_CVT(I16, U64), MKLDNN_CVT(I16, I64),
88+
MKLDNN_CVT(I16, FP32), MKLDNN_CVT(I16, BF16), MKLDNN_CVT(I16, BOOL),
89+
MKLDNN_CVT(I32, U8), MKLDNN_CVT(I32, I8), MKLDNN_CVT(I32, U16),
90+
MKLDNN_CVT(I32, I16), MKLDNN_CVT(I32, U64), MKLDNN_CVT(I32, I64),
91+
MKLDNN_CVT(I32, FP32), MKLDNN_CVT(I32, BF16), MKLDNN_CVT(I32, BOOL),
92+
MKLDNN_CVT(U64, U8), MKLDNN_CVT(U64, I8), MKLDNN_CVT(U64, U16),
93+
MKLDNN_CVT(U64, I16), MKLDNN_CVT(U64, I32), MKLDNN_CVT(U64, I64),
94+
MKLDNN_CVT(U64, FP32), MKLDNN_CVT(U64, BF16), MKLDNN_CVT(U64, BOOL),
95+
MKLDNN_CVT(I64, U8), MKLDNN_CVT(I64, I8), MKLDNN_CVT(I64, U16),
96+
MKLDNN_CVT(I64, I16), MKLDNN_CVT(I64, I32), MKLDNN_CVT(I64, U64),
97+
MKLDNN_CVT(I64, FP32), MKLDNN_CVT(I64, BF16), MKLDNN_CVT(I64, BOOL),
98+
MKLDNN_CVT(FP32, U8), MKLDNN_CVT(FP32, I8), MKLDNN_CVT(FP32, U16),
99+
MKLDNN_CVT(FP32, I16), MKLDNN_CVT(FP32, I32), MKLDNN_CVT(FP32, U64),
100+
MKLDNN_CVT(FP32, I64), MKLDNN_CVT(FP32, BF16), MKLDNN_CVT(FP32, BOOL),
101+
MKLDNN_CVT(BF16, U8), MKLDNN_CVT(BF16, I8), MKLDNN_CVT(BF16, U16),
102+
MKLDNN_CVT(BF16, I16), MKLDNN_CVT(BF16, I32), MKLDNN_CVT(BF16, U64),
103+
MKLDNN_CVT(BF16, I64), MKLDNN_CVT(BF16, FP32), MKLDNN_CVT(BF16, BOOL),
104+
MKLDNN_CVT(BOOL, U8), MKLDNN_CVT(BOOL, I8), MKLDNN_CVT(BOOL, U16),
105+
MKLDNN_CVT(BOOL, I16), MKLDNN_CVT(BOOL, I32), MKLDNN_CVT(BOOL, U64),
106+
MKLDNN_CVT(BOOL, I64), MKLDNN_CVT(BOOL, FP32), MKLDNN_CVT(BOOL, BF16));
107+
108+
if (!ctx.converted)
109+
THROW_IE_EXCEPTION << "cpu_convert can't convert from: " << srcPrc << " precision to: " << dstPrc;
108110
}
111+
112+
#undef MKLDNN_CVT

inference-engine/src/mkldnn_plugin/nodes/list.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ namespace Cpu {
1818
# include "list_tbl.hpp"
1919
#undef MKLDNN_EXTENSION_NODE
2020

21-
MKLDNNExtensions::MKLDNNExtensions() {
21+
MKLDNNExtensions::MKLDNNExtensions()
22+
: layersFactory("LayersFactory") {
2223
#define MKLDNN_EXTENSION_NODE(__prim, __type) FACTORY_CALL(__prim, __type)
2324
# include "list_tbl.hpp"
2425
#undef MKLDNN_EXTENSION_NODE

0 commit comments

Comments
 (0)