diff --git a/paddle/fluid/operators/conv_mkldnn_op.cc b/paddle/fluid/operators/conv_mkldnn_op.cc index 83702ff61a65b7156e3af46682010fe094b51180..e143c8411efe43bca558c32cffca9ce6566b3388 100644 --- a/paddle/fluid/operators/conv_mkldnn_op.cc +++ b/paddle/fluid/operators/conv_mkldnn_op.cc @@ -293,102 +293,6 @@ class ConvMKLDNNHandler : public platform::MKLDNNHandler { conv_bwd_data_pd_; }; - struct key_desc{ - struct Hash{ - std::size_t operator()(const key_desc &key) const{ - int input_dim = 0; - int weights_dim = 0; - int stride_value = 0; - int padding_value = 0; - int dilation_value = 0; - for(size_t i=0; i hasher; - return hasher( (input_dim << 8) + - (weights_dim << 8 * 2) + - (stride_value << 8 * 3) + - (padding_value << 8) + - (dilation_value << 8 * 2) + - (key.groups << 8 * 3)); - } - }; - - std::vector input_tz; - std::vector weights_tz; - std::vector strides; - std::vector paddings; - std::vector dilations; - int groups; - const std::string suffix; - key_desc(std::vector input_tz, std::vector weights_tz, std::vector strides, std::vector paddings, std::vector dilations,int groups,const std::string suffix): input_tz(input_tz), weights_tz(weights_tz), strides(strides), paddings(paddings), dilations(dilations), groups(groups), suffix(suffix) {} - - bool operator==(const key_desc o) const{ - for(size_t i=0; i &key_map, key_desc key_dsr, std::string key){ - auto it = key_map.find(key_dsr); - if (it == key_map.end()) { - key_map[key_dsr] = key; // create new blob - } else { - (*it).second = key; // set data to existing blob - } - return; - } - - std::string GetKeyMap(std::unordered_map &key_map, key_desc key_dsr){ - auto it = key_map.find(key_dsr); - if (it != key_map.end()) { - return (*it).second; - } - return ""; - } -}; - template class ConvMKLDNNOpKernel : public paddle::framework::OpKernel { public: @@ -449,7 +353,7 @@ class ConvMKLDNNOpKernel : public paddle::framework::OpKernel { const float* filter_data = filter->data(); std::vector src_tz = paddle::framework::vectorize2int(input->dims()); - std::vector weights_tz = + std::vector weights_tz = paddle::framework::vectorize2int(filter->dims()); int g = std::max(groups, 1); if (g > 1) { @@ -467,28 +371,20 @@ class ConvMKLDNNOpKernel : public paddle::framework::OpKernel { std::vector dst_tz = paddle::framework::vectorize2int(output->dims()); // Get unique name for storing MKLDNN primitives - handle_key keyhandler; - key_desc key_dsr = {src_tz, weights_tz, strides, paddings, dilations, groups, ctx.op().Output("Output")}; - - static std::unordered_map key_map; - static std::shared_ptr> key_suffix_map(new std::unordered_map({})); - bool key_reuse = true; - std::string none_key = ""; - if(keyhandler.GetKeyMap(key_map, key_dsr) == none_key){ - key_reuse = false; - } - std::string key; - if(!key_reuse){ - key = ConvMKLDNNHandler::GetHash( - src_tz, weights_tz, strides, paddings, dilations, groups, - ctx.op().Output("Output")); - keyhandler.SetKeyMap(key_map, key_dsr, key); - } else{ - key = keyhandler.GetKeyMap(key_map, key_dsr); - } + const std::string key = ConvMKLDNNHandler::GetHash( + src_tz, weights_tz, strides, paddings, dilations, groups, + ctx.op().Output("Output")); const std::string key_conv_pd = key + "@conv_pd"; static std::unordered_map>> scale_map; + //scale_map.insert({key_conv_pd,{1.0f}}); + //scale_map[key_conv_pd]={0.1f}; bool scale_reuse = true; + //auto scale_in_key = key + "@scale_in"; + //auto scale_weights_key = key + "@scale_weights"; + //auto scale_out_key = key + "@scale_out"; + //auto output_shift_scale_key = key + "@output_shift_scale"; + //auto sum_scale_key = key + "@sum_scale"; + //auto scale_in_eltwise_key = key + "@scale_in_eltwise"; std::vector scale_in_data; std::vector scale_out_data; std::vector scale_weights_data; @@ -630,7 +526,6 @@ class ConvMKLDNNOpKernel : public paddle::framework::OpKernel { dev_ctx.SetBlob(key_conv_pd, conv_pd); ConvMKLDNNHandler handler(conv_pd, dev_ctx, mkldnn_engine, key); - handler.key_suffix_map_ = key_suffix_map; auto user_src_memory_p = handler.AcquireSrcMemory(*user_src_md, to_void_cast(input_data)); diff --git a/paddle/fluid/platform/mkldnn_helper.h b/paddle/fluid/platform/mkldnn_helper.h index ce03767f676b80d2f80d12f01d03144ba0039da5..327c0bbd903b1273e70f441b725e39b513c6ede2 100644 --- a/paddle/fluid/platform/mkldnn_helper.h +++ b/paddle/fluid/platform/mkldnn_helper.h @@ -115,28 +115,6 @@ class MKLDNNHandler { key_(base_key), is_reusing_(false) {} - struct key_suffix_desc{ - struct Hash{ - std::size_t operator()(const key_suffix_desc &dsr) const{ - int a = std::atoi(dsr.key.c_str()); - int b = std::atoi(dsr.suffix.c_str()); - std::hash hasher; - return hasher( (a << 8) + - (b << 8 * 2)); - } - }; - - std::string key; - std::string suffix; - - key_suffix_desc(std::string key, std::string suffix): key(key), suffix(suffix) {} - - bool operator==(const key_suffix_desc o) const{ - return(key == o.key && suffix == o.suffix); - } - bool operator!=(const key_suffix_desc& o) const { return !(*this == o); } - }; - std::shared_ptr AcquireSrcMemory( const mkldnn::memory::desc& md, void* ptr) { return this->AcquireMemory(md, ptr, "@user_src_mem_p"); @@ -170,20 +148,7 @@ class MKLDNNHandler { std::shared_ptr AcquireMemoryFromPrimitive( mkldnn::memory::primitive_desc mdp, void* ptr, const std::string& suffix) { - std::string local_key; - if(key_suffix_map_) { - key_suffix_desc dsr = {key_ , suffix}; - if(GetKeySuffixMap(key_suffix_map_, dsr) == ""){ -//std::cout<<"create key!!!!!!!"<(dev_ctx_.GetBlob(local_key)); PADDLE_ENFORCE((mem_p != nullptr) || (is_reusing_ == false), @@ -205,20 +170,7 @@ class MKLDNNHandler { void* ptr, const std::string& suffix) { /*Generate key*/ - std::string local_key; - if(key_suffix_map_){ - key_suffix_desc dsr = {key_ , suffix}; - if(GetKeySuffixMap(key_suffix_map_, dsr) == ""){ -//std::cout<<"create key!!!!!!!"<(dev_ctx_.GetBlob(local_key)); PADDLE_ENFORCE((mem_p != nullptr) || (is_reusing_ == false), @@ -241,21 +193,7 @@ class MKLDNNHandler { const std::shared_ptr& target_memory_p, const std::string& suffix, std::vector& pipeline) { // NOLINT - - std::string local_key; - if(key_suffix_map_){ - key_suffix_desc dsr = {key_ , suffix}; - if(GetKeySuffixMap(key_suffix_map_, dsr) == ""){ -//std::cout<<"create key!!!!!!!"<( @@ -284,20 +222,7 @@ class MKLDNNHandler { std::vector scale_data = {1.0f}, int mask = 0) { // create reorder primitive if the input format is not the preferred one - std::string local_key; - if(key_suffix_map_){ - key_suffix_desc dsr = {key_ , suffix}; - if(GetKeySuffixMap(key_suffix_map_, dsr) == ""){ -//std::cout<<"create key!!!!!!!"<> key_suffix_map, key_suffix_desc key_suffix_dsr, std::string key){ - auto it = (*key_suffix_map).find(key_suffix_dsr); - if (it == (*key_suffix_map).end()) { - (*key_suffix_map)[key_suffix_dsr] = key; // create new blob - } else { - (*it).second = key; // set data to existing blob - } - return; - } - - std::string GetKeySuffixMap(std::shared_ptr> key_suffix_map, key_suffix_desc key_suffix_dsr){ - auto it = (*key_suffix_map).find(key_suffix_dsr); - if (it != (*key_suffix_map).end()) { - return (*it).second; - } - return ""; - } - - std::shared_ptr> key_suffix_map_; - protected: static std::string dims2str(const mkldnn::memory::dims& operand_dims) { std::string dstr = "";