From d4413a54bc95e80d54403fd5c48261ca7313d125 Mon Sep 17 00:00:00 2001 From: Adam <38704900+grygielski@users.noreply.github.com> Date: Sat, 14 Sep 2019 03:58:05 +0200 Subject: [PATCH] Add common CreateKey for mkldnn handlers (#19767) test=develop --- .../fluid/framework/data_layout_transform.cc | 4 +- .../mkldnn/elementwise_add_mkldnn_op.cc | 4 +- .../operators/mkldnn/activation_mkldnn_op.cc | 14 -- .../operators/mkldnn/batch_norm_mkldnn_op.cc | 31 +--- .../operators/mkldnn/concat_mkldnn_op.cc | 26 +--- .../fluid/operators/mkldnn/conv_mkldnn_op.cc | 10 +- .../mkldnn/conv_transpose_mkldnn_op.cc | 6 +- .../operators/mkldnn/dequantize_mkldnn_op.cc | 15 +- paddle/fluid/operators/mkldnn/fc_mkldnn_op.cc | 17 +-- .../fluid/operators/mkldnn/lrn_mkldnn_op.cc | 4 +- .../fluid/operators/mkldnn/mul_mkldnn_op.cc | 26 +--- .../fluid/operators/mkldnn/pool_mkldnn_op.cc | 8 +- .../operators/mkldnn/quantize_mkldnn_op.cc | 15 +- .../operators/mkldnn/softmax_mkldnn_op.cc | 5 +- .../operators/mkldnn/transpose_mkldnn_op.cc | 8 +- paddle/fluid/platform/mkldnn_helper.h | 31 ++-- paddle/fluid/platform/mkldnn_reuse.h | 132 +----------------- 17 files changed, 70 insertions(+), 286 deletions(-) diff --git a/paddle/fluid/framework/data_layout_transform.cc b/paddle/fluid/framework/data_layout_transform.cc index dc7ef19f136..fe53c6f99d8 100644 --- a/paddle/fluid/framework/data_layout_transform.cc +++ b/paddle/fluid/framework/data_layout_transform.cc @@ -163,8 +163,8 @@ void innerTransDataLayoutFromMKLDNN(DataLayout in_layout, DataLayout out_layout, if (in_format != out_format) { void* in_data = GetDataFromTensor(in, in_type); - const std::string key = platform::ReorderMKLDNNHandler::GetHash( - in_tz, in_format, out_format, std::to_string(in_type)); + const std::string key = platform::CreateKey(in_tz, in_format, out_format, + std::to_string(in_type)); platform::ReorderMKLDNNHandler handler(in_tz, in.type(), in_type, *dev_ctx, cpu_engine, key); diff --git a/paddle/fluid/operators/elementwise/mkldnn/elementwise_add_mkldnn_op.cc b/paddle/fluid/operators/elementwise/mkldnn/elementwise_add_mkldnn_op.cc index e93baa2dfa0..97b1f3831c0 100644 --- a/paddle/fluid/operators/elementwise/mkldnn/elementwise_add_mkldnn_op.cc +++ b/paddle/fluid/operators/elementwise/mkldnn/elementwise_add_mkldnn_op.cc @@ -70,7 +70,7 @@ class EltwiseAddMKLDNNKernel : public framework::OpKernel { auto out_format = platform::MKLDNNFormatForSize( x_dims.size(), MKLDNNMemoryFormat::nchw); - const std::string key = platform::ReorderMKLDNNHandler::GetHash( + const std::string key = platform::CreateKey( src_x_tz, x->format(), out_format, std::to_string(in_type)); platform::ReorderMKLDNNHandler handler(src_x_tz, x->type(), in_type, @@ -136,7 +136,7 @@ class EltwiseAddMKLDNNKernel : public framework::OpKernel { std::vector srcs_pd; std::vector scales = {1.0f, 1.0f}; - const std::string key = platform::GetHash( + const std::string key = platform::CreateKey( src_x_tz, ctx.op().Output("Out") + std::to_string(x->format()) + std::to_string(y->format())); diff --git a/paddle/fluid/operators/mkldnn/activation_mkldnn_op.cc b/paddle/fluid/operators/mkldnn/activation_mkldnn_op.cc index 264ca81def2..b706982c3b5 100644 --- a/paddle/fluid/operators/mkldnn/activation_mkldnn_op.cc +++ b/paddle/fluid/operators/mkldnn/activation_mkldnn_op.cc @@ -27,20 +27,6 @@ using platform::GetMKLDNNFormat; using platform::MKLDNNDeviceContext; using platform::to_void_cast; -namespace { -std::string gethash(const mkldnn::memory::dims &operand_dims, - const mkldnn::algorithm algorithm) { - auto dim2str = [](const mkldnn::memory::dims &operand_dims) { - std::string dstr = ""; - for (size_t i = 0; i < operand_dims.size(); ++i) { - dstr += std::to_string(operand_dims[i]) + "-"; - } - return dstr; - }; - return dim2str(operand_dims) + std::to_string(algorithm); -} -} // namespace - template class MKLDNNActivationKernel : public framework::OpKernel { diff --git a/paddle/fluid/operators/mkldnn/batch_norm_mkldnn_op.cc b/paddle/fluid/operators/mkldnn/batch_norm_mkldnn_op.cc index d2a27d81ba9..f3209151b35 100644 --- a/paddle/fluid/operators/mkldnn/batch_norm_mkldnn_op.cc +++ b/paddle/fluid/operators/mkldnn/batch_norm_mkldnn_op.cc @@ -120,22 +120,6 @@ class BatchNormMKLDNNHandler : public platform::MKLDNNHandler { return batch_norm_p; } - static std::string GetHash(const memory::dims &input_dims, float epsilon, - unsigned flag, bool is_test, - MKLDNNMemoryFormat format, - const std::string &suffix = "") { - auto dims2str = [](const memory::dims &operand_dims) { - std::string dstr = ""; - for (size_t i = 0; i < operand_dims.size(); ++i) { - dstr += std::to_string(operand_dims[i]) + "-"; - } - return dstr; - }; - return dims2str(input_dims) + std::to_string(epsilon) + - std::to_string(flag) + std::to_string(is_test) + - std::to_string(format) + suffix; - } - private: std::shared_ptr batch_norm_pd_; }; @@ -236,9 +220,9 @@ class BatchNormMKLDNNOpKernel : public paddle::framework::OpKernel { platform::MKLDNNFormatForSize(src_tz.size(), x->format()); // keys for backward pass - const std::string key = BatchNormMKLDNNHandler::GetHash( - src_tz, epsilon, flags, global_stats, input_format, - ctx.op().Output("SavedMean")); + const std::string key = + platform::CreateKey(src_tz, epsilon, flags, global_stats, input_format, + ctx.op().Output("SavedMean")); BatchNormMKLDNNHandler handler(dev_ctx, mkldnn_engine, key); auto user_src_md = platform::MKLDNNMemDesc( @@ -369,15 +353,14 @@ class BatchNormMKLDNNGradOpKernel : public paddle::framework::OpKernel { unsigned flags = mkldnn::use_scale_shift; // keys from forward pass - const std::string key = BatchNormMKLDNNHandler::GetHash( - src_tz, epsilon, flags, false, input_format, - ctx.op().Input("SavedMean")); + const std::string key = + platform::CreateKey(src_tz, epsilon, flags, false, input_format, + ctx.op().Input("SavedMean")); const std::string key_batch_norm_fwd_pd = key + "@bn_fwd_pd"; // keys for primitives reuse const std::string key_with_hash = - key + BatchNormMKLDNNHandler::GetHash(src_tz, epsilon, flags, false, - input_format); + key + platform::CreateKey(src_tz, epsilon, flags, false, input_format); const std::string key_batch_norm_bwd_p = key_with_hash + "@batch_norm_bwd_p"; const std::string key_batch_norm_src_mem_p = diff --git a/paddle/fluid/operators/mkldnn/concat_mkldnn_op.cc b/paddle/fluid/operators/mkldnn/concat_mkldnn_op.cc index c9a8231f14e..8823e086555 100644 --- a/paddle/fluid/operators/mkldnn/concat_mkldnn_op.cc +++ b/paddle/fluid/operators/mkldnn/concat_mkldnn_op.cc @@ -66,27 +66,6 @@ static const mkldnn::engine& GetMKLDNNEngine( return dev_ctx.GetEngine(); } -std::string CreateKey(const paddle::framework::ExecutionContext& ctx, - const std::vector multi_input, - const int64_t& concat_axis, const memory::data_type& dt) { - std::string key; - key.reserve(platform::MKLDNNHandler::MaxKeyLength); - for (size_t i = 0; i < multi_input.size(); i++) { - platform::AppendKeyDims( - &key, paddle::framework::vectorize(multi_input[i]->dims())); - } - platform::AppendKey(&key, std::to_string(concat_axis)); - platform::AppendKey(&key, ctx.op().Output("Out")); - platform::AppendKey(&key, std::to_string(dt)); - platform::AppendKey(&key, std::to_string(multi_input[0]->format())); - if (platform::get_cur_mkldnn_session_id() == - platform::kMKLDNNSessionID_Default) { - platform::AppendKey(&key, "-t:"); - platform::AppendKey(&key, platform::ThreadIDasStr()); - } - return key; -} - template class ConcatPrimitiveFactory { public: @@ -175,7 +154,10 @@ class ConcatMKLDNNOpKernel : public paddle::framework::OpKernel { paddle::framework::ToMKLDNNDataType(multi_input[0]->type()); ConcatPrimitiveFactory prim_creator; - std::string key = CreateKey(ctx, multi_input, concat_axis, dt); + std::string key = platform::CreateKey( + paddle::framework::vectorize(multi_input[0]->dims()), concat_axis, + ctx.op().Output("Out"), dt, multi_input[0]->format(), + platform::ThreadIDasStr()); const std::string key_prim = key + "@concat_p"; const std::string key_concat_pd = key + "@concat_pd"; const std::string key_srcs = key + "@concat_srcs"; diff --git a/paddle/fluid/operators/mkldnn/conv_mkldnn_op.cc b/paddle/fluid/operators/mkldnn/conv_mkldnn_op.cc index 7989532fb3f..de8520e9b14 100644 --- a/paddle/fluid/operators/mkldnn/conv_mkldnn_op.cc +++ b/paddle/fluid/operators/mkldnn/conv_mkldnn_op.cc @@ -190,7 +190,7 @@ class ConvMKLDNNOpKernel : public paddle::framework::OpKernel { auto dst_tz = paddle::framework::vectorize(output->dims()); // Get unique name for storing MKLDNN primitives - const std::string key = platform::ConvMKLDNNHandler::GetHash( + const std::string key = platform::CreateKey( src_tz, weights_tz, fuse_activation, strides, paddings, dilations, groups, ctx.op().Input("Input") + ctx.op().Input("Filter")); @@ -415,10 +415,8 @@ class ConvMKLDNNOpKernel : public paddle::framework::OpKernel { paddle::framework::ToMKLDNNDataType(input->type()); // Get unique name for storing MKLDNN primitives - std::string key; - key.reserve(MaxKeyLength); - platform::ConvMKLDNNHandler::CreateKey( - &key, src_tz, weights_tz, strides, paddings, dilations, groups, src_dt, + const std::string key = platform::CreateKey( + src_tz, weights_tz, strides, paddings, dilations, groups, src_dt, input->format(), fuse_activation, fuse_residual_conn, ctx.op().Input("Input") + ctx.op().Input("Filter")); @@ -715,7 +713,7 @@ class ConvMKLDNNGradOpKernel : public paddle::framework::OpKernel { // Get an unique name from "argument" name of "input" and "Filter" variable // as well as attributes of primitive to be created // This name will be used as key when saving info into device context - const std::string key = platform::ConvMKLDNNHandler::GetHash( + const std::string key = platform::CreateKey( src_tz, weights_tz, "", strides, paddings, dilations, groups, ctx.op().Input("Input") + ctx.op().Input("Filter")); diff --git a/paddle/fluid/operators/mkldnn/conv_transpose_mkldnn_op.cc b/paddle/fluid/operators/mkldnn/conv_transpose_mkldnn_op.cc index 4efa4afc6dc..84240d30fe1 100644 --- a/paddle/fluid/operators/mkldnn/conv_transpose_mkldnn_op.cc +++ b/paddle/fluid/operators/mkldnn/conv_transpose_mkldnn_op.cc @@ -127,9 +127,9 @@ class ConvTransposeMKLDNNOpKernel : public paddle::framework::OpKernel { auto dst_tz = paddle::framework::vectorize(output->dims()); // Get unique name for storing MKLDNN primitives - const std::string key = platform::ConvTransposeMKLDNNHandler::GetHash( - src_tz, weights_tz, strides, paddings, dilations, groups, - ctx.op().Output("Output")); + const std::string key = + platform::CreateKey(src_tz, weights_tz, strides, paddings, dilations, + groups, ctx.op().Output("Output")); std::vector pipeline; diff --git a/paddle/fluid/operators/mkldnn/dequantize_mkldnn_op.cc b/paddle/fluid/operators/mkldnn/dequantize_mkldnn_op.cc index d1aee1a9154..b74e7127ea3 100644 --- a/paddle/fluid/operators/mkldnn/dequantize_mkldnn_op.cc +++ b/paddle/fluid/operators/mkldnn/dequantize_mkldnn_op.cc @@ -31,18 +31,6 @@ using framework::DataLayout; using mkldnn::stream; using platform::GetMKLDNNFormat; -std::string CreateKey(const paddle::framework::ExecutionContext& ctx, - const mkldnn::memory::data_type& src_dt, - const std::vector& src_tz, const float scale_data) { - std::string key; - key.reserve(platform::MKLDNNHandler::MaxKeyLength); - platform::AppendKey(&key, std::to_string(src_dt)); - platform::AppendKeyDims(&key, src_tz); - platform::AppendKey(&key, std::to_string(scale_data)); - platform::AppendKey(&key, ctx.op().Output("Output")); - return key; -} - template class DeQuantOpKernel : public framework::OpKernel { public: @@ -64,7 +52,8 @@ class DeQuantOpKernel : public framework::OpKernel { mkldnn::memory::data_type src_dt = paddle::framework::ToMKLDNNDataType(input->type()); MKLDNNMemoryFormat src_fmt = input->format(); - std::string key = CreateKey(ctx, src_dt, src_tz, reorder_scale[0]); + std::string key = platform::CreateKey(src_dt, src_tz, reorder_scale[0], + ctx.op().Output("Output")); const std::string key_prim = key + "@reorder_p"; const std::string key_src_mem = key + "@src_mem"; const std::string key_dst_mem = key + "@dst_mem"; diff --git a/paddle/fluid/operators/mkldnn/fc_mkldnn_op.cc b/paddle/fluid/operators/mkldnn/fc_mkldnn_op.cc index 30790425cb3..01837cfe36b 100644 --- a/paddle/fluid/operators/mkldnn/fc_mkldnn_op.cc +++ b/paddle/fluid/operators/mkldnn/fc_mkldnn_op.cc @@ -221,25 +221,14 @@ class FCPrimitiveFactory { boost::optional fc_; }; -static std::string GetHash(const Tensor* input, const Tensor* weights, - const std::string& suffix) { - auto dim2str = [](const DDim& operand_dims) { - std::string str = ""; - for (size_t i = 0; i < operand_dims.size(); ++i) { - str += std::to_string(operand_dims[i]) + "-"; - } - return str; - }; - return std::to_string((unsigned)input->format()) + dim2str(weights->dims()) + - suffix; -} - template std::shared_ptr> GetPrimitiveFactory( const MKLDNNDeviceContext& dev_ctx, const ExecutionContext& ctx, const Tensor* input, const Tensor* weights, const mkldnn::engine& mkldnn_engine) { - const std::string key = GetHash(input, weights, ctx.op().Output("Out")); + const std::string key = platform::CreateKey( + input->format(), framework::vectorize(weights->dims()), + ctx.op().Output("Out")); auto prim_creator = std::static_pointer_cast>(dev_ctx.GetBlob(key)); diff --git a/paddle/fluid/operators/mkldnn/lrn_mkldnn_op.cc b/paddle/fluid/operators/mkldnn/lrn_mkldnn_op.cc index 263a3393d20..d073822fc54 100644 --- a/paddle/fluid/operators/mkldnn/lrn_mkldnn_op.cc +++ b/paddle/fluid/operators/mkldnn/lrn_mkldnn_op.cc @@ -62,7 +62,7 @@ class LRNMKLDNNOpKernel : public paddle::framework::OpKernel { auto md = paddle::platform::MKLDNNMemDesc( dims, platform::MKLDNNGetDataType(), x->format()); - const std::string key = platform::LRNMKLDNNHandler::GetHash( + const std::string key = platform::CreateKey( dims, n, alpha, beta, k, x->format(), ctx.op().Output("Out")); platform::LRNMKLDNNHandler handler(ctx.Attr("is_test"), dev_ctx, @@ -121,7 +121,7 @@ class LRNMKLDNNGradOpKernel : public paddle::framework::OpKernel { auto dims = paddle::framework::vectorize(x->dims()); - const std::string key = platform::LRNMKLDNNHandler::GetHash( + const std::string key = platform::CreateKey( dims, n, alpha, beta, k, x->format(), ctx.op().Input("Out")); platform::LRNMKLDNNHandler handler(false, dev_ctx, mkldnn_engine, key); diff --git a/paddle/fluid/operators/mkldnn/mul_mkldnn_op.cc b/paddle/fluid/operators/mkldnn/mul_mkldnn_op.cc index f1936b01f2d..5c635e58ecb 100644 --- a/paddle/fluid/operators/mkldnn/mul_mkldnn_op.cc +++ b/paddle/fluid/operators/mkldnn/mul_mkldnn_op.cc @@ -332,33 +332,17 @@ class QuantMulPrimitiveFactory : public MulPrimitiveFactory { } }; -static std::string GetHash(const Tensor *input_x, const Tensor *input_y, - const std::string &suffix) { - auto dim2str = [](const DDim &operand_dims) { - std::string str = ""; - for (int i = 0; i < operand_dims.size(); ++i) { - str += std::to_string(operand_dims[i]) + "-"; - } - return str; - }; - - std::string hash = std::to_string((unsigned)input_x->format()) + - std::to_string((unsigned)input_x->type()) + - dim2str(input_x->dims()) + - std::to_string((unsigned)input_y->format()) + - std::to_string((unsigned)input_y->type()) + - dim2str(input_y->dims()) + suffix; - - return hash; -} - /* OT: output data type */ template std::shared_ptr> GetPrimitiveFactory( const MKLDNNDeviceContext &dev_ctx, const ExecutionContext &ctx, const Tensor *input_x, const Tensor *input_y, const mkldnn::engine &mkldnn_engine, bool enable_quant) { - const std::string key = GetHash(input_x, input_y, ctx.op().Output("Out")); + const std::string key = platform::CreateKey( + input_x->format(), input_x->type(), + framework::vectorize(input_x->dims()), input_y->format(), + input_y->type(), framework::vectorize(input_y->dims()), + ctx.op().Output("Out")); auto prim_creator = std::static_pointer_cast>( dev_ctx.GetBlob(key)); diff --git a/paddle/fluid/operators/mkldnn/pool_mkldnn_op.cc b/paddle/fluid/operators/mkldnn/pool_mkldnn_op.cc index 58a59a88eed..4862b029d6e 100644 --- a/paddle/fluid/operators/mkldnn/pool_mkldnn_op.cc +++ b/paddle/fluid/operators/mkldnn/pool_mkldnn_op.cc @@ -79,9 +79,9 @@ class PoolMKLDNNOpKernel : public paddle::framework::OpKernel { paddle::framework::ToMKLDNNDataType(input->type()); auto fmt = input->format(); - const std::string key = platform::PoolingMKLDNNHandler::GetHash( - src_tz, pooling_type, ksize, strides, paddings, dt, fmt, - ctx.op().Output("Out")); + const std::string key = + platform::CreateKey(src_tz, pooling_type, ksize, strides, paddings, dt, + fmt, ctx.op().Output("Out")); platform::PoolingMKLDNNHandler handler(pooling_type, dt, ctx.Attr("is_test"), dev_ctx, @@ -171,7 +171,7 @@ class PoolMKLDNNGradOpKernel : public paddle::framework::OpKernel { // Get an unique name from "argument" name of "Out" variable // This name will be used as key when referring info from device context - const std::string key = platform::PoolingMKLDNNHandler::GetHash( + const std::string key = platform::CreateKey( diff_src_tz, pooling_type, ksize, strides, paddings, memory::data_type::f32, in_x->format(), ctx.op().Input("Out")); diff --git a/paddle/fluid/operators/mkldnn/quantize_mkldnn_op.cc b/paddle/fluid/operators/mkldnn/quantize_mkldnn_op.cc index b268b14bc8e..788e3f27931 100644 --- a/paddle/fluid/operators/mkldnn/quantize_mkldnn_op.cc +++ b/paddle/fluid/operators/mkldnn/quantize_mkldnn_op.cc @@ -30,18 +30,6 @@ using framework::DataLayout; using mkldnn::stream; using platform::GetMKLDNNFormat; -std::string CreateKey(const paddle::framework::ExecutionContext& ctx, - const std::vector& src_tz, const float scale_data, - const bool is_negative) { - std::string key; - key.reserve(platform::MKLDNNHandler::MaxKeyLength); - platform::AppendKeyDims(&key, src_tz); - platform::AppendKey(&key, std::to_string(scale_data)); - platform::AppendKey(&key, std::to_string(is_negative)); - platform::AppendKey(&key, ctx.op().Output("Output")); - return key; -} - template class QuantOpKernel : public framework::OpKernel { public: @@ -60,7 +48,8 @@ class QuantOpKernel : public framework::OpKernel { const T* input_data = input->data(); bool is_negative = ctx.Attr("is_negative_input"); - std::string key = CreateKey(ctx, src_tz, scale_data, is_negative); + std::string key = platform::CreateKey(src_tz, scale_data, is_negative, + ctx.op().Output("Output")); const std::string key_prim = key + "@reorder_p"; const std::string key_src_mem = key + "@src_mem"; const std::string key_dst_mem = key + "@dst_mem"; diff --git a/paddle/fluid/operators/mkldnn/softmax_mkldnn_op.cc b/paddle/fluid/operators/mkldnn/softmax_mkldnn_op.cc index f1e711249d5..1135d672f7a 100644 --- a/paddle/fluid/operators/mkldnn/softmax_mkldnn_op.cc +++ b/paddle/fluid/operators/mkldnn/softmax_mkldnn_op.cc @@ -40,7 +40,7 @@ class SoftmaxMKLDNNHandler : public platform::MKLDNNHandler { const platform::MKLDNNDeviceContext& dev_ctx, platform::Place cpu_place, const std::string& uniq_name) : platform::MKLDNNHandler(dev_ctx, dev_ctx.GetEngine(), - platform::GetHash(dims, uniq_name)), + platform::CreateKey(dims, uniq_name)), place_(cpu_place), fwd_pd_(nullptr), bwd_pd_(nullptr) { @@ -53,7 +53,7 @@ class SoftmaxMKLDNNHandler : public platform::MKLDNNHandler { const platform::MKLDNNDeviceContext& dev_ctx, platform::Place cpu_place, const std::string& uniq_name) : platform::MKLDNNHandler(dev_ctx, dev_ctx.GetEngine(), - platform::GetHash(dims, uniq_name)), + platform::CreateKey(dims, uniq_name)), place_(cpu_place), fwd_pd_(nullptr), bwd_pd_(nullptr) { @@ -218,6 +218,7 @@ class SoftmaxMKLDNNKernel : public paddle::framework::OpKernel { auto dst_tz = src_tz; // Same memory descriptor to be used for input and output memory::dims softmax_tz = {src_tz[0], src_tz[1]}; + SoftmaxMKLDNNHandler handler(softmax_tz, MKLDNNMemoryFormat::nc, dev_ctx, ctx.GetPlace(), ctx.op().Output("Out")); // Currently only NC data format is supported diff --git a/paddle/fluid/operators/mkldnn/transpose_mkldnn_op.cc b/paddle/fluid/operators/mkldnn/transpose_mkldnn_op.cc index 4a79d590c25..bcf919fadcf 100644 --- a/paddle/fluid/operators/mkldnn/transpose_mkldnn_op.cc +++ b/paddle/fluid/operators/mkldnn/transpose_mkldnn_op.cc @@ -45,9 +45,9 @@ class TransposeMKLDNNOpKernel : public paddle::framework::OpKernel { auto nchw_tz = paddle::framework::vectorize(input->dims()); - const std::string key = platform::TransposeMKLDNNHandler::GetHash( - nchw_tz, axis, - ctx.op().Output("Out") + std::to_string(input->format())); + const std::string key = + platform::CreateKey(nchw_tz, axis, ctx.op().Output("Out") + + std::to_string(input->format())); platform::TransposeMKLDNNHandler handler(nchw_tz, axis, dev_ctx, mkldnn_engine, key); @@ -99,7 +99,7 @@ class TransposeMKLDNNGradOpKernel : public paddle::framework::OpKernel { auto nchw_tz = paddle::framework::vectorize(out_grad->dims()); - const std::string key = platform::TransposeMKLDNNHandler::GetHash( + const std::string key = platform::CreateKey( nchw_tz, axis, ctx.op().Output(framework::GradVarName("X"))); platform::TransposeMKLDNNHandler handler(nchw_tz, reversed_axis, dev_ctx, diff --git a/paddle/fluid/platform/mkldnn_helper.h b/paddle/fluid/platform/mkldnn_helper.h index 9583dde6231..7131266f3fa 100644 --- a/paddle/fluid/platform/mkldnn_helper.h +++ b/paddle/fluid/platform/mkldnn_helper.h @@ -184,28 +184,31 @@ inline std::string ThreadIDasStr(void) { std::hash()(std::this_thread::get_id())); } -inline std::string dims2str(const mkldnn::memory::dims& operand_dims) { - std::string dstr = ""; - for (size_t i = 0; i < operand_dims.size(); ++i) { - dstr += std::to_string(operand_dims[i]) + "-"; - } - return dstr; +template +inline void AppendKey(std::string* key, const T& num) { + key->append(std::to_string(num)); } -inline void AppendKey(std::string* key, const std::string& s) { - key->append(s); +inline void AppendKey(std::string* key, const std::string& str) { + key->append(str); } -inline std::string GetHash(const mkldnn::memory::dims& operand_dims, - const std::string& suffix) { - return dims2str(operand_dims) + suffix; -} +inline void AppendKey(std::string* key, const char* str) { key->append(str); } -inline void AppendKeyDims(std::string* key, const mkldnn::memory::dims& dims) { - for (unsigned int i = 0; i < dims.size(); i++) { +inline void AppendKey(std::string* key, const std::vector& dims) { + for (size_t i = 0; i < dims.size(); i++) { AppendKey(key, std::to_string(dims[i])); } } +template +inline std::string CreateKey(ArgTypes&&... args) { + std::string key; + key.reserve(256); + using expand_type = int[]; + expand_type{0, (AppendKey(&key, args), 0)...}; + return key; +} + } // namespace platform } // namespace paddle diff --git a/paddle/fluid/platform/mkldnn_reuse.h b/paddle/fluid/platform/mkldnn_reuse.h index d474b234a9a..911dffff422 100644 --- a/paddle/fluid/platform/mkldnn_reuse.h +++ b/paddle/fluid/platform/mkldnn_reuse.h @@ -198,9 +198,6 @@ class MKLDNNHandler { mkldnn::engine engine_; std::string key_; std::string key_common_; - - public: - static constexpr int MaxKeyLength = 256; }; class SumMKLDNNHandler : public MKLDNNHandler { @@ -267,10 +264,9 @@ class ActivationMKLDNNHandler : public MKLDNNHandler { platform::Place cpu_place, const std::string& unique_name) - : platform::MKLDNNHandler( - dev_ctx, dev_ctx.GetEngine(), - platform::ActivationMKLDNNHandler::GetHash( - dims, algorithm, fmt, alpha, beta, unique_name)), + : platform::MKLDNNHandler(dev_ctx, dev_ctx.GetEngine(), + platform::CreateKey(dims, algorithm, fmt, alpha, + beta, unique_name)), place_(cpu_place), fwd_pd_(nullptr), bwd_pd_(nullptr) { @@ -288,10 +284,9 @@ class ActivationMKLDNNHandler : public MKLDNNHandler { platform::Place cpu_place, const std::string& unique_name) - : platform::MKLDNNHandler( - dev_ctx, dev_ctx.GetEngine(), - platform::ActivationMKLDNNHandler::GetHash( - dims, algorithm, fmt, alpha, beta, unique_name)), + : platform::MKLDNNHandler(dev_ctx, dev_ctx.GetEngine(), + platform::CreateKey(dims, algorithm, fmt, alpha, + beta, unique_name)), place_(cpu_place), fwd_pd_(nullptr), bwd_pd_(nullptr) { @@ -383,21 +378,6 @@ class ActivationMKLDNNHandler : public MKLDNNHandler { return eltwise_bwd_p; } - static std::string GetHash(const memory::dims& input_dims, - const mkldnn::algorithm algorithm, - const MKLDNNMemoryFormat fmt, const float alpha, - const float beta, const std::string& suffix) { - std::string key; - key.reserve(platform::MKLDNNHandler::MaxKeyLength); - platform::AppendKeyDims(&key, input_dims); - platform::AppendKey(&key, std::to_string(algorithm)); - platform::AppendKey(&key, std::to_string(fmt)); - platform::AppendKey(&key, std::to_string(alpha)); - platform::AppendKey(&key, std::to_string(beta)); - platform::AppendKey(&key, suffix); - return key; - } - protected: void AcquireActivationPrimitiveDescriptor(mkldnn::prop_kind prop_kind, mkldnn::algorithm algorithm, @@ -597,22 +577,6 @@ class LRNMKLDNNHandler : public MKLDNNHandler { return lrn_bwd_p; } - static std::string GetHash(const memory::dims& input_dims, const int n, - const float alpha, const float beta, const float k, - const MKLDNNMemoryFormat& fmt, - const std::string& suffix) { - std::string key; - key.reserve(platform::MKLDNNHandler::MaxKeyLength); - platform::AppendKeyDims(&key, input_dims); - platform::AppendKey(&key, std::to_string(n)); - platform::AppendKey(&key, std::to_string(alpha)); - platform::AppendKey(&key, std::to_string(beta)); - platform::AppendKey(&key, std::to_string(k)); - platform::AppendKey(&key, std::to_string(fmt)); - platform::AppendKey(&key, suffix); - return key; - } - private: bool is_test_; std::shared_ptr fwd_pd_; @@ -790,24 +754,6 @@ class PoolingMKLDNNHandler : public MKLDNNHandler { return pooling_bwd_p; } - static std::string GetHash( - const memory::dims& input_dims, const std::string& pooling_type, - const std::vector& ksize, const std::vector& strides, - const std::vector& paddings, const memory::data_type& dt, - const MKLDNNMemoryFormat& fmt, const std::string& suffix) { - std::string key; - key.reserve(platform::MKLDNNHandler::MaxKeyLength); - platform::AppendKeyDims(&key, input_dims); - platform::AppendKey(&key, pooling_type); - platform::AppendKeyDims(&key, ksize); - platform::AppendKeyDims(&key, strides); - platform::AppendKeyDims(&key, paddings); - platform::AppendKey(&key, std::to_string(dt)); - platform::AppendKey(&key, std::to_string(fmt)); - platform::AppendKey(&key, suffix); - return key; - } - private: static inline int ComputeCeiledOutput(int input_size, int kernel_size, int padding, int stride) { @@ -905,12 +851,6 @@ class TransposeMKLDNNHandler : public MKLDNNHandler { return transpose_p; } - static std::string GetHash(std::vector& shape, // NOLINT - std::vector& axis, // NOLINT - const std::string& suffix) { - return dims2str(shape) + dims2str(axis) + suffix; - } - protected: mkldnn_memory_desc_t Axis2MemoryDesc(std::vector& nchw_tz, // NOLINT std::vector& axis // NOLINT @@ -999,14 +939,6 @@ class ReorderMKLDNNHandler : public MKLDNNHandler { return reorder_p; } - static std::string GetHash(std::vector& shape, // NOLINT - MKLDNNMemoryFormat in_fmt, - MKLDNNMemoryFormat out_fmt, - const std::string& suffix) { - return dims2str(shape) + std::to_string(in_fmt) + "->" + - std::to_string(out_fmt) + "#" + suffix; - } - private: std::vector dims_; framework::proto::VarType::Type vtype_; @@ -1346,58 +1278,6 @@ class ConvMKLDNNTemplateHandler : public MKLDNNHandler { return conv_bwd_data_p; } - // Generate keys for storing/retriving primitives for this operator - // TODO(jczaja): Make hashing function more optimial - static std::string GetHash(mkldnn::memory::dims& input_dims, // NOLINT - mkldnn::memory::dims& weights_dims, // NOLINT - const std::string& fuse_activation, // NOLINT - std::vector& strides, // NOLINT - std::vector& paddings, // NOLINT - std::vector& dilations, // NOLINT - int groups, const std::string& suffix) { - return dims2str(input_dims) + dims2str(weights_dims) + fuse_activation + - dims2str(strides) + dims2str(paddings) + dims2str(dilations) + - std::to_string(groups) + suffix; - } - - // Generate keys for storing/retriving primitives for this operator - // TODO(jczaja): Make hashing function more optimial - static std::string GetHash(mkldnn::memory::dims& input_dims, // NOLINT - mkldnn::memory::dims& weights_dims, // NOLINT - std::vector& strides, // NOLINT - std::vector& paddings, // NOLINT - std::vector& dilations, // NOLINT - int groups, const std::string& suffix) { - return dims2str(input_dims) + dims2str(weights_dims) + dims2str(strides) + - dims2str(paddings) + dims2str(dilations) + std::to_string(groups) + - suffix; - } - - static void CreateKey( - std::string* key, const mkldnn::memory::dims& input_dims, - const mkldnn::memory::dims& weights_dims, const std::vector& strides, - const std::vector& paddings, const std::vector& dilations, - const int& groups, const mkldnn::memory::data_type& srcdt, - const MKLDNNMemoryFormat& format, const std::string& fuse_activation, - const bool& residual, const std::string& suffix) { - AppendKeyDims(key, input_dims); - - AppendKeyDims(key, weights_dims); - - AppendKeyDims(key, strides); - - AppendKeyDims(key, paddings); - - AppendKeyDims(key, dilations); - - AppendKey(key, std::to_string(groups)); - AppendKey(key, std::to_string(srcdt)); - AppendKey(key, std::to_string(format)); - AppendKey(key, fuse_activation); - AppendKey(key, std::to_string(residual)); - AppendKey(key, suffix); - } - private: std::shared_ptr conv_pd_; std::shared_ptr -- GitLab