Skip to content

Commit 2099618

Browse files
Sand3r-luotao1
authored andcommitted
MKL-DNN] Added mkl-dnn cache clearing when creating Executor instance (#20241) (#20693)
test=release/1.6 * - Flushing mkl-dnn cache test=develop - Disabled clearing cache for LoadModel - Added clearing of mkl-dnn cache when Executor is created test=develop - Do not clear for GPU places test=develop - compilation fix test=develop * - Moved clearing of mkl-dnn cache in destructor of executor test=develop * - Compilation fix test=develop - Reverted conditional clearing of mkl-dnn cache in Executors's destructor test=develop - compilation fix
1 parent a77d75c commit 2099618

13 files changed

+39
-40
lines changed

paddle/fluid/framework/executor.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,20 @@ ExecutorPrepareContext::~ExecutorPrepareContext() {
9393

9494
Executor::Executor(const platform::Place& place) : place_(place) {}
9595

96+
Executor::~Executor() {
97+
#ifdef PADDLE_WITH_MKLDNN
98+
// Clear mkl-dnn cache, unless explicitly
99+
// (as set in constructor) marked not to do so
100+
// this is needed to have mkl-dnn unit tests working
101+
if (platform::is_cpu_place(place_)) {
102+
platform::DeviceContextPool& pool = platform::DeviceContextPool::Instance();
103+
platform::MKLDNNDeviceContext* dev_ctx =
104+
(platform::MKLDNNDeviceContext*)pool.Get(place_);
105+
dev_ctx->ResetBlobMap();
106+
}
107+
#endif
108+
}
109+
96110
void Executor::Close() {
97111
#ifdef PADDLE_WITH_DISTRIBUTE
98112
// TODO(typhoonzero): complete message will need to use real trainer_id,

paddle/fluid/framework/executor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class Executor {
5858

5959
explicit Executor(const platform::Place& place);
6060

61+
~Executor();
6162
/*
6263
* Close this Executor.
6364
* Calling this method will send complete messages to all pserver instances.

paddle/fluid/operators/elementwise/mkldnn/elementwise_add_mkldnn_op.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,8 @@ class EltwiseAddMKLDNNKernel : public framework::OpKernel<T> {
136136
std::vector<memory::primitive_desc> srcs_pd;
137137
std::vector<float> scales = {1.0f, 1.0f};
138138

139-
const std::string key = platform::CreateKey(
140-
src_x_tz, ctx.op().Output("Out") + std::to_string(x->format()) +
141-
std::to_string(y->format()));
139+
const std::string key =
140+
platform::CreateKey(src_x_tz, ctx.op().Output("Out"));
142141

143142
platform::SumMKLDNNHandler handler(dev_ctx, mkldnn_engine, key);
144143

paddle/fluid/operators/mkldnn/batch_norm_mkldnn_op.cc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ class BatchNormMKLDNNHandler
4040
: platform::MKLDNNHandlerT<T, mkldnn::batch_normalization_forward,
4141
mkldnn::batch_normalization_backward>(
4242
dev_ctx, dev_ctx.GetEngine(), cpu_place,
43-
platform::CreateKey(dims, epsilon, flags, global_stats, fmt,
44-
uniq_name)) {
43+
platform::CreateKey(dims, uniq_name)) {
4544
auto md = mkldnn::memory::desc(dims, platform::MKLDNNGetDataType<T>(), fmt);
4645

4746
this->AcquireForwardPrimitiveDescriptor(
@@ -59,8 +58,7 @@ class BatchNormMKLDNNHandler
5958
: platform::MKLDNNHandlerT<T, mkldnn::batch_normalization_forward,
6059
mkldnn::batch_normalization_backward>(
6160
dev_ctx, dev_ctx.GetEngine(), cpu_place,
62-
platform::CreateKey(dims, epsilon, flags, false, src_fmt,
63-
uniq_name)) {
61+
platform::CreateKey(dims, uniq_name)) {
6462
auto diff_dst_md =
6563
mkldnn::memory::desc(dims, platform::MKLDNNGetDataType<T>(), diff_fmt);
6664
auto src_md =

paddle/fluid/operators/mkldnn/concat_mkldnn_op.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,8 @@ class ConcatMKLDNNOpKernel : public paddle::framework::OpKernel<T> {
150150

151151
ConcatPrimitiveFactory<T> prim_creator;
152152
std::string key = platform::CreateKey(
153-
paddle::framework::vectorize<int>(multi_input[0]->dims()), concat_axis,
154-
ctx.op().Output("Out"), dt, multi_input[0]->format(),
155-
platform::ThreadIDasStr());
153+
paddle::framework::vectorize<int>(multi_input[0]->dims()),
154+
ctx.op().Output("Out"), dt, platform::ThreadIDasStr());
156155
const std::string key_prim = key + "@concat_p";
157156
const std::string key_concat_pd = key + "@concat_pd";
158157
const std::string key_srcs = key + "@concat_srcs";

paddle/fluid/operators/mkldnn/conv_mkldnn_op.cc

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,7 @@ class ConvMKLDNNOpKernel : public paddle::framework::OpKernel<T> {
220220

221221
// Get unique name for storing MKLDNN primitives
222222
const std::string key = platform::CreateKey(
223-
src_tz, weights_tz, fuse_activation, strides, paddings, dilations,
224-
groups, ctx.op().Input("Input") + ctx.op().Input("Filter"));
223+
src_tz, ctx.op().Input("Input") + ctx.op().Input("Filter"));
225224

226225
std::vector<primitive> pipeline;
227226

@@ -450,9 +449,7 @@ class ConvMKLDNNOpKernel : public paddle::framework::OpKernel<T> {
450449
paddle::framework::ToMKLDNNDataType(input->type());
451450

452451
std::string key = platform::CreateKey(
453-
src_tz, weights_tz, strides, paddings, dilations, groups, src_dt,
454-
input->format(), fuse_activation, fuse_residual_conn,
455-
ctx.op().Input("Input") + ctx.op().Input("Filter"));
452+
src_tz, src_dt, ctx.op().Input("Input") + ctx.op().Input("Filter"));
456453

457454
std::shared_ptr<mkldnn::convolution_forward> conv_p;
458455
std::shared_ptr<mkldnn::memory> src_memory_p;
@@ -662,8 +659,7 @@ class ConvMKLDNNGradOpKernel : public paddle::framework::OpKernel<T> {
662659
// as well as attributes of primitive to be created
663660
// This name will be used as key when saving info into device context
664661
const std::string key = platform::CreateKey(
665-
src_tz, weights_tz, "", strides, paddings, dilations, groups,
666-
ctx.op().Input("Input") + ctx.op().Input("Filter"));
662+
src_tz, ctx.op().Input("Input") + ctx.op().Input("Filter"));
667663

668664
const std::string key_conv_pd = key + "@conv_pd";
669665
std::vector<primitive> pipeline;

paddle/fluid/operators/mkldnn/conv_transpose_mkldnn_op.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,7 @@ class ConvTransposeMKLDNNOpKernel : public paddle::framework::OpKernel<T> {
128128

129129
// Get unique name for storing MKLDNN primitives
130130
const std::string key =
131-
platform::CreateKey(src_tz, weights_tz, strides, paddings, dilations,
132-
groups, ctx.op().Output("Output"));
131+
platform::CreateKey(src_tz, ctx.op().Output("Output"));
133132

134133
std::vector<mkldnn::primitive> pipeline;
135134

paddle/fluid/operators/mkldnn/dequantize_mkldnn_op.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ class DeQuantOpKernel : public framework::OpKernel<T> {
5252
mkldnn::memory::data_type src_dt =
5353
paddle::framework::ToMKLDNNDataType(input->type());
5454
MKLDNNMemoryFormat src_fmt = input->format();
55-
std::string key = platform::CreateKey(src_dt, src_tz, reorder_scale[0],
56-
ctx.op().Output("Output"));
55+
std::string key =
56+
platform::CreateKey(src_dt, src_tz, ctx.op().Output("Output"));
5757
const std::string key_prim = key + "@reorder_p";
5858
const std::string key_src_mem = key + "@src_mem";
5959
const std::string key_dst_mem = key + "@dst_mem";

paddle/fluid/operators/mkldnn/fc_mkldnn_op.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,7 @@ std::shared_ptr<FCPrimitiveFactory<T>> GetPrimitiveFactory(
228228
const Tensor* input, const Tensor* weights,
229229
const mkldnn::engine& mkldnn_engine) {
230230
const std::string key = platform::CreateKey(
231-
input->format(), framework::vectorize<int>(weights->dims()),
232-
ctx.op().Output("Out"));
231+
framework::vectorize<int>(weights->dims()), ctx.op().Output("Out"));
233232

234233
auto prim_creator =
235234
std::static_pointer_cast<FCPrimitiveFactory<T>>(dev_ctx.GetBlob(key));

paddle/fluid/operators/mkldnn/mul_mkldnn_op.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,8 +340,7 @@ std::shared_ptr<MulPrimitiveFactory<XT, YT, OT>> GetPrimitiveFactory(
340340
const Tensor *input_x, const Tensor *input_y,
341341
const mkldnn::engine &mkldnn_engine, bool enable_quant) {
342342
const std::string key = platform::CreateKey(
343-
input_x->format(), input_x->type(),
344-
framework::vectorize<int>(input_x->dims()), input_y->format(),
343+
input_x->type(), framework::vectorize<int>(input_x->dims()),
345344
input_y->type(), framework::vectorize<int>(input_y->dims()),
346345
ctx.op().Output("Out"));
347346

0 commit comments

Comments
 (0)