/* Copyright (c) 2017 PaddlePaddle Authors. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #pragma once #include #include #include #include #include #include #include "paddle/fluid/framework/data_layout_transform.h" #include "paddle/fluid/framework/operator.h" #include "paddle/fluid/operators/pool_op.h" #include "paddle/fluid/platform/mkldnn_helper.h" #include "paddle/fluid/platform/place.h" #include "paddle/phi/backends/onednn/onednn_reuse.h" namespace paddle { namespace platform { using framework::DataLayout; using framework::Tensor; using user_function = std::function(const float*)>; using memory = dnnl::memory; template using MKLDNNHandlerNoCachingT = phi::funcs:: MKLDNNHandlerNoCachingT; template class MKLDNNHandlerT { public: MKLDNNHandlerT(const MKLDNNDeviceContext& dev_ctx, dnnl::engine engine, platform::Place cpu_place, const std::string& base_key) : dev_ctx_(dev_ctx), engine_(engine), place_(cpu_place), key_common_(base_key), key_(platform::ExtendKeyWithThreadInfoIfNeeded(dev_ctx, base_key)), fwd_pd_(nullptr), bwd_pd_(nullptr) { platform::MKLDNNDeviceContext::tls().log_lib_version(); } std::shared_ptr AcquireForwardPrimitive() { const std::string key_p = key_ + "@fwd_p"; auto forward_p = std::static_pointer_cast(dev_ctx_.GetBlob(key_p)); if (forward_p == nullptr) { forward_p = std::make_shared(*fwd_pd_); dev_ctx_.SetBlob(key_p, forward_p); } return forward_p; } std::shared_ptr AcquireBackwardPrimitive() { const std::string key_p = key_ + "@bwd_p"; auto backward_p = std::static_pointer_cast(dev_ctx_.GetBlob(key_p)); if (backward_p == nullptr) { backward_p = std::make_shared(*bwd_pd_); dev_ctx_.SetBlob(key_p, backward_p); } return backward_p; } std::shared_ptr AcquireBackwardWeightsPrimitive() { const std::string key_p = key_ + "@bwd_w_p"; auto backward_p = std::static_pointer_cast(dev_ctx_.GetBlob(key_p)); if (backward_p == nullptr) { PADDLE_ENFORCE_NOT_NULL( bwd_w_pd_, platform::errors::Unavailable("BWD_PD should be set when " "getting BWD prim witk key: %s .", key_p)); backward_p = std::make_shared(*bwd_w_pd_); dev_ctx_.SetBlob(key_p, backward_p); } return backward_p; } std::shared_ptr AcquireSrcMemory( const framework::Tensor* input) { const T* input_data = input->data(); return this->AcquireMemoryFromPrimitive( fwd_pd_->src_desc(), to_void_cast(input_data), "@src_mem_p"); } template std::shared_ptr AcquireDstMemory(framework::Tensor* output) { T_out* ptr = output->mutable_data(place_, fwd_pd_->dst_desc().get_size()); return this->AcquireMemoryFromPrimitive( fwd_pd_->dst_desc(), ptr, "@dst_mem_p"); } template std::shared_ptr AcquireDstMemory(void) { return this->AcquireMemoryFromPrimitive(fwd_pd_->dst_desc(), "@dstt_mem_p"); } template std::shared_ptr AcquireDstMemory( const framework::Tensor* output) { const T_out* output_data = output->data(); return this->AcquireMemoryFromPrimitive(bwd_pd_->dst_desc(), to_void_cast(output_data), "@bwd-dst_mem_p"); } std::shared_ptr AcquireDiffDstMemory( const framework::Tensor* diffdst) { const T* ptr = diffdst->data(); return this->AcquireMemoryFromPrimitive( bwd_pd_->diff_dst_desc(), to_void_cast(ptr), "@diff_dst_mem_p"); } std::shared_ptr AcquireDiffSrcMemory( framework::Tensor* diffsrc) { T* ptr = diffsrc->mutable_data(place_, bwd_pd_->diff_src_desc().get_size()); return this->AcquireMemoryFromPrimitive( bwd_pd_->diff_src_desc(), ptr, "@diff_src_mem_p"); } // Buffer of given Tensor is used for oneDNN computation std::shared_ptr AcquireDiffWeightsMemory( framework::Tensor* diff_weights) { PADDLE_ENFORCE_NOT_NULL( bwd_w_pd_, platform::errors::Unavailable( "BWD_W_PD should be set when getting BWD grad of weights.")); T* ptr = diff_weights->mutable_data( place_, bwd_w_pd_->diff_weights_desc().get_size()); return this->AcquireMemoryFromPrimitive( bwd_w_pd_->diff_weights_desc(), ptr, "@diff_wei_mem_p"); } // Buffer is allocated by oneDNN to store computation results std::shared_ptr AcquireDiffWeightsMemory(void) { PADDLE_ENFORCE_NOT_NULL( bwd_w_pd_, platform::errors::Unavailable( "BWD_W_PD should be set when getting BWD grad of weights.")); return this->AcquireMemoryFromPrimitive(bwd_w_pd_->diff_weights_desc(), "@diff_wei_mem_p"); } protected: bool isCached() { const std::string key_pd = key_ + "@fwd_pd"; fwd_pd_ = std::static_pointer_cast( dev_ctx_.GetBlob(key_pd)); return (fwd_pd_ != nullptr); } bool isBwdCached() { const std::string key_pd = key_ + "@bwd_pd"; bwd_pd_ = std::static_pointer_cast( dev_ctx_.GetBlob(key_pd)); if (bwd_pd_ == nullptr) { return false; } else { if (std::is_same::value == false) { const std::string key_bw_w_pd = key_ + "@bwd_w_pd"; bwd_w_pd_ = std::static_pointer_cast( dev_ctx_.GetBlob(key_bw_w_pd)); } // When BWD is cached then still we need to Get FWD PD const std::string key_fpd = key_ + "@fwd_pd"; fwd_pd_ = std::static_pointer_cast( dev_ctx_.GetBlob(key_fpd)); PADDLE_ENFORCE_NOT_NULL( fwd_pd_, platform::errors::Unavailable( "Error: FWD PD should be set when BWD PD is cached.")); return true; } } // If your primitive descriptor requires attributes, pass them as a // first argument and paramters to descriptor constructor in the following // arguments. Otherwise, all arguments will be forwarded to descriptor // constructor, including the first one. template void AcquireForwardPrimitiveDescriptor(Arg&& first_arg, Args&&... args) { // This is used when we can recreate FWD PD in BWD so // we do not need to pass FWD to BWD const std::string key_pd = key_ + "@fwd_pd"; fwd_pd_ = std::static_pointer_cast( dev_ctx_.GetBlob(key_pd)); if (fwd_pd_ == nullptr) { CreateForwardPrimitiveDescriptor(first_arg, std::forward(args)...); dev_ctx_.SetBlob(key_pd, fwd_pd_); } } // Using sfinae to specialise variadic function. Workaround for not having // if constexpr in C++ 11. template typename std::enable_if::type, dnnl::primitive_attr>::value>::type CreateForwardPrimitiveDescriptor(First&& first, Args&&... args) { auto fwd_desc = typename TForward::desc(std::forward(args)...); fwd_pd_ = std::make_shared( fwd_desc, first, engine_); } template typename std::enable_if::type, dnnl::primitive_attr>::value>::type CreateForwardPrimitiveDescriptor(First&& first, Args&&... args) { auto fwd_desc = typename TForward::desc(std::forward(first), std::forward(args)...); fwd_pd_ = std::make_shared(fwd_desc, engine_); } template void AcquireBackwardPrimitiveDescriptor(Args&&... args) { // fwd_pd_ is set during grad by calling // AcquireForwardPrimitiveDescriptor PADDLE_ENFORCE_NOT_NULL( fwd_pd_, platform::errors::Unavailable("Get MKLDNN Forward primitive %s failed.", key_ + "@fwd_pd")); const std::string key_pd = key_ + "@bwd_pd"; bwd_pd_ = std::static_pointer_cast( dev_ctx_.GetBlob(key_pd)); if (bwd_pd_ == nullptr) { auto bwd_desc = typename TBackward::desc(std::forward(args)...); bwd_pd_ = std::make_shared( bwd_desc, engine_, *fwd_pd_); dev_ctx_.SetBlob(key_pd, bwd_pd_); } } template void AcquireBackwardWeightsPrimitiveDescriptor(Args&&... args) { // fwd_pd_ is set during grad by calling // AcquireForwardPrimitiveDescriptor PADDLE_ENFORCE_NOT_NULL( fwd_pd_, platform::errors::Unavailable("Get MKLDNN Forward primitive %s failed.", key_ + "@fwd_pd")); const std::string key_pd = key_ + "@bwd_w_pd"; bwd_w_pd_ = std::static_pointer_cast( dev_ctx_.GetBlob(key_pd)); if (bwd_w_pd_ == nullptr) { auto bwd_desc = typename TBackward_params::desc(std::forward(args)...); bwd_w_pd_ = std::make_shared( bwd_desc, engine_, *fwd_pd_); dev_ctx_.SetBlob(key_pd, bwd_w_pd_); } } std::shared_ptr AcquireMemoryFromPrimitive( const std::string& suffix) { return std::static_pointer_cast( dev_ctx_.GetBlob(key_ + suffix)); } std::shared_ptr AcquireMemoryFromPrimitive( dnnl::memory::desc md, void* ptr, const std::string& suffix) { const auto local_key = key_ + suffix; auto mem_p = std::static_pointer_cast(dev_ctx_.GetBlob(local_key)); if (mem_p == nullptr) { mem_p = std::make_shared(md, engine_, ptr); dev_ctx_.SetBlob(local_key, mem_p); } else { mem_p->set_data_handle(ptr); } return mem_p; } std::shared_ptr AcquireMemoryFromPrimitive( dnnl::memory::desc md, const std::string& suffix) { const auto local_key = key_ + suffix; auto mem_p = std::static_pointer_cast(dev_ctx_.GetBlob(local_key)); if (mem_p == nullptr) { mem_p = std::make_shared(md, engine_); dev_ctx_.SetBlob(local_key, mem_p); } return mem_p; } void AcquireReorder(const std::shared_ptr& user_memory_p, const std::shared_ptr& target_memory_p) { auto reorder_p = std::make_shared(*user_memory_p, *target_memory_p); auto& astream = platform::MKLDNNDeviceContext::tls().get_stream(); platform::RecordEvent record_reorder("int_reorder", platform::TracerEventType::UserDefined, 2, platform::EventRole::kUniqueOp); reorder_p->execute( astream, {{DNNL_ARG_FROM, *user_memory_p}, {DNNL_ARG_TO, *target_memory_p}}); astream.wait(); } template std::shared_ptr AcquireMemoryWithReorder( const dnnl::memory::desc& user_md, const dnnl::memory::desc& target_md, void* ptr, const std::string& suffix, bool is_persistent = false, std::function(const F*)> custom_reorder_func = {}, const std::vector& scale_data = {1.0f}, int mask = 0) { const auto target_key = key_ + suffix + "_target"; const auto key_reorder_p = key_ + suffix + "reorder_p"; const auto user_key = key_ + suffix + "_user"; auto target_memory_p = std::static_pointer_cast(dev_ctx_.GetBlob(target_key)); if (target_memory_p == nullptr) { if (custom_reorder_func) { auto reordered_data = custom_reorder_func(reinterpret_cast(ptr)); dev_ctx_.SetBlob(key_reorder_p + "-custom_reorder", reordered_data); ptr = reinterpret_cast(reordered_data.get()); } auto user_memory_p = std::make_shared(user_md, engine_, ptr); if (user_md != target_md) { target_memory_p = std::make_shared(target_md, engine_); dnnl::reorder::primitive_desc reorder_pdesc; if (is_int8()) { dnnl::primitive_attr attr; attr.set_output_scales(mask, scale_data); reorder_pdesc = dnnl::reorder::primitive_desc( *user_memory_p, *target_memory_p, attr); } else { reorder_pdesc = dnnl::reorder::primitive_desc(*user_memory_p, *target_memory_p); } auto reorder_p = std::make_shared(reorder_pdesc); dev_ctx_.SetBlob(key_reorder_p, reorder_p); auto& astream = platform::MKLDNNDeviceContext::tls().get_stream(); platform::RecordEvent record_reorder( "int_reorder", platform::TracerEventType::UserDefined, 2, platform::EventRole::kUniqueOp); reorder_p->execute( astream, {{DNNL_ARG_FROM, *user_memory_p}, {DNNL_ARG_TO, *target_memory_p}}); astream.wait(); } else { target_memory_p = user_memory_p; } dev_ctx_.SetBlob(user_key, user_memory_p); dev_ctx_.SetBlob(target_key, target_memory_p); } else if (!is_persistent) { auto& astream = platform::MKLDNNDeviceContext::tls().get_stream(); auto user_memory_p = std::static_pointer_cast(dev_ctx_.GetBlob(user_key)); user_memory_p->set_data_handle(ptr); // TODO(jczaja): Here we detect if reorder is cached it means it is needed // need to change this to get rid of keys auto reorder_p = std::static_pointer_cast( dev_ctx_.GetBlob(key_reorder_p)); if (reorder_p != nullptr) { platform::RecordEvent record_reorder( "int_reorder", platform::TracerEventType::UserDefined, 2, platform::EventRole::kUniqueOp); reorder_p->execute( astream, {{DNNL_ARG_FROM, *user_memory_p}, {DNNL_ARG_TO, *target_memory_p}}); astream.wait(); } } return target_memory_p; } std::shared_ptr AcquireMemory(const std::string& suffix) { const auto local_key = key_ + suffix; return std::static_pointer_cast(dev_ctx_.GetBlob(local_key)); } const MKLDNNDeviceContext& dev_ctx_; dnnl::engine engine_; platform::Place place_; std::string key_common_; std::string key_; std::shared_ptr fwd_pd_; std::shared_ptr bwd_pd_; std::shared_ptr bwd_w_pd_; }; template class BinaryMKLDNNHandler : public platform::MKLDNNHandlerNoCachingT { public: BinaryMKLDNNHandler(const dnnl::algorithm algo, const int axis, const dnnl::engine engine, platform::Place cpu_place, const Tensor* x, const Tensor* y, Tensor* out, float scale_x, float scale_y, float scale_out, const dnnl::post_ops& post_ops = dnnl::post_ops{}) : platform::MKLDNNHandlerNoCachingT(engine, cpu_place) { const auto src_x_tz = phi::vectorize(x->dims()); const auto src_y_tz = phi::vectorize(y->dims()); // if output tensor(z) is nullptr then we are computing into oneDNN // managed buffer auto rankdiff = x->dims().size() - y->dims().size(); const auto dst_tz = (out == nullptr) ? (rankdiff > 0 ? src_x_tz : src_y_tz) : phi::vectorize(out->dims()); auto src0_md = x->mem_desc(); auto src1_md = y->mem_desc(); if (rankdiff > 0) { // Second input is of smaller rank than first std::vector dims1_ex(rankdiff, 1); dims1_ex.insert(next(dims1_ex.begin(), (axis == -1 ? rankdiff : axis)), src_y_tz.begin(), src_y_tz.end()); // For broadcasting for NHWC we need rotate extended shape if (MKLDNNDeviceContext::tls().get_cur_paddle_data_layout() == framework::DataLayout::kNHWC) { std::rotate(dims1_ex.begin() + 1, dims1_ex.end() - 1, dims1_ex.end()); } src1_md = src1_md.reshape(dims1_ex); } else if (rankdiff < 0) { // First input is of smaller than second std::vector dims0_ex(-rankdiff, 1); dims0_ex.insert(next(dims0_ex.begin(), (axis == -1 ? -rankdiff : axis)), src_x_tz.begin(), src_x_tz.end()); // For broadcasting for NHWC we need rotate extended shape if (MKLDNNDeviceContext::tls().get_cur_paddle_data_layout() == framework::DataLayout::kNHWC) { std::rotate(dims0_ex.begin() + 1, dims0_ex.end() - 1, dims0_ex.end()); } src0_md = src0_md.reshape(dims0_ex); } const auto dst_md = memory::desc( dst_tz, platform::MKLDNNGetDataType(), MKLDNNMemoryFormat::any); auto attributes = CreateAttributes(algo, scale_x, scale_y, scale_out, post_ops); if (x->numel() < y->numel()) { this->AcquireForwardPrimitiveDescriptor( attributes, algo, src1_md, src0_md, dst_md); } else { this->AcquireForwardPrimitiveDescriptor( attributes, algo, src0_md, src1_md, dst_md); } } std::shared_ptr AcquireSecondSrcMemory( const framework::Tensor* input) { const T* input_data = input->data(); return this->AcquireMemoryFromPrimitive(this->fwd_pd_->src1_desc(), to_void_cast(input_data)); } private: static inline dnnl::primitive_attr CreateAttributes( dnnl::algorithm op, float scale_x, float scale_y, float scale_out, dnnl::post_ops post_ops = dnnl::post_ops{}) { // Scales set in attributes for inputs contibute to the output equation // in the following way (assuming no broadcasting takes place): // output_i = scale_0 * x_i <+ or *> scale_1 * y_i; // Hence we have to create scales that will: // 1. Dequantize both values, by multiplying with (1.0 / scale_x_or_y) // 2. Quantize their result to output scale range, by multiplying with // (scale_z) // If we combine these two, we end up with following equation // output = scale_out * (1/scale_x * x <* or +> 1/scale_y * y) // Hence, to mimic such behaviour using provided interface, // For add operation the equation is equal to: // output = (scale_out / scale_x) * x + (scale_out / scale_y) * y // // For mul operation on the other hand // output = (scale_out / scale_x) * x * (1.0 / scale_y) * y // float scale_0 = scale_out / scale_x; float scale_1 = op == dnnl::algorithm::binary_add ? scale_out / scale_y : 1.0 / scale_y; dnnl::primitive_attr attributes; attributes.set_scales( /* input_x_id = */ DNNL_ARG_SRC_0, /* mask = */ 0, {scale_0}); attributes.set_scales( /* input_y_id = */ DNNL_ARG_SRC_1, /* mask = */ 0, {scale_1}); if (post_ops.len() > 0) attributes.set_post_ops(post_ops); return attributes; } }; template class BroadcastDataMKLDNNHandler : public platform::MKLDNNHandlerNoCachingT { public: BroadcastDataMKLDNNHandler(const dnnl::algorithm algo, const dnnl::engine engine, platform::Place cpu_place, const Tensor* x, Tensor* out, float scale_x, float scale_y, const std::vector& extended_x_dims) : platform::MKLDNNHandlerNoCachingT(engine, cpu_place) { const auto src0_tz = phi::vectorize(out->dims()); const auto src0_md = dnnl::memory::desc(src0_tz, platform::MKLDNNGetDataType(), platform::GetPlainMKLDNNFormat(src0_tz.size())); const auto src1_md = x->mem_desc().reshape(extended_x_dims); dnnl::primitive_attr attributes; attributes.set_scales(DNNL_ARG_SRC_0, 0, {scale_x}); attributes.set_scales(DNNL_ARG_SRC_1, 0, {scale_y}); this->AcquireForwardPrimitiveDescriptor( attributes, algo, src0_md, src1_md, src0_md); } template std::shared_ptr AcquireZeroedDstMemory(framework::Tensor* out) { T_out* ptr = out->mutable_data(this->place_, this->fwd_pd_->dst_desc().get_size()); memset(ptr, 0, this->fwd_pd_->dst_desc().get_size()); return this->AcquireMemoryFromPrimitive(this->fwd_pd_->dst_desc(), ptr); } }; static void AppendActivation(const framework::ExecutionContext& ctx, dnnl::post_ops& post_ops, // NOLINT float activation_scale = 1.0f) { const auto invalid_attribute = ctx.HasAttr("fuse_activation") ? ctx.Attr("fuse_activation").empty() : true; if (invalid_attribute) return; const auto fuse_activation = ctx.Attr("fuse_activation"); const auto fuse_alpha = ctx.HasAttr("fuse_alpha") ? ctx.Attr("fuse_alpha") : 0.0f; const auto fuse_beta = ctx.HasAttr("fuse_beta") ? ctx.Attr("fuse_beta") : 0.0f; if (fuse_activation == "hard_sigmoid") { post_ops.append_eltwise(activation_scale, dnnl::algorithm::eltwise_linear, fuse_alpha, fuse_beta); post_ops.append_eltwise( activation_scale, dnnl::algorithm::eltwise_clip, 0.0f, 1.0f); } else { const std::unordered_map activation_map = { {"abs", dnnl::algorithm::eltwise_abs}, {"clip", dnnl::algorithm::eltwise_clip}, {"gelu", dnnl::algorithm::eltwise_gelu_erf}, {"gelu_erf", dnnl::algorithm::eltwise_gelu_erf}, {"gelu_tanh", dnnl::algorithm::eltwise_gelu_tanh}, {"hard_swish", dnnl::algorithm::eltwise_hardswish}, {"leaky_relu", dnnl::algorithm::eltwise_relu}, {"mish", dnnl::algorithm::eltwise_mish}, {"relu", dnnl::algorithm::eltwise_relu}, {"relu6", dnnl::algorithm::eltwise_bounded_relu}, {"sigmoid", dnnl::algorithm::eltwise_logistic}, {"sqrt", dnnl::algorithm::eltwise_sqrt}, {"swish", dnnl::algorithm::eltwise_swish}, {"tanh", dnnl::algorithm::eltwise_tanh}}; const auto& activation_type = activation_map.find(fuse_activation); PADDLE_ENFORCE_NE( activation_type, activation_map.end(), platform::errors::InvalidArgument( "Activation '%s' not found in oneDNN algorithms mapper", fuse_activation)); post_ops.append_eltwise( activation_scale, activation_type->second, fuse_alpha, fuse_beta); } } template class ReductionMKLDNNHandler : public platform::MKLDNNHandlerNoCachingT { public: ReductionMKLDNNHandler(const dnnl::algorithm algo, const float p, const float eps, const dnnl::engine engine, platform::Place cpu_place, const Tensor* x, const Tensor* out, std::vector out_tz, const dnnl::primitive_attr& attrs = NULL) : platform::MKLDNNHandlerNoCachingT(engine, cpu_place) { const auto out_md = memory::desc(out_tz, platform::MKLDNNGetDataType(), dnnl::memory::format_tag::any); if (attrs) this->AcquireForwardPrimitiveDescriptor( attrs, algo, x->mem_desc(), out_md, p, eps); else this->AcquireForwardPrimitiveDescriptor( algo, x->mem_desc(), out_md, p, eps); } }; template constexpr bool IsInt8() { return std::is_same::value || std::is_same::value; } template constexpr bool IsBfloat16() { return std::is_same::value; } template class MatMulV2MKLDNNHandler : public paddle::platform::MKLDNNHandlerNoCachingT { public: MatMulV2MKLDNNHandler(const framework::ExecutionContext& ctx, const dnnl::engine engine, paddle::platform::Place cpu_place, const std::vector& x_org_dims, bool trans_x, const std::vector& y_org_dims, bool trans_y, bool is_output_fused, const std::vector& x_strides_override, const std::vector& y_strides_override) : paddle::platform::MKLDNNHandlerNoCachingT(engine, cpu_place) { // M X K * K X N std::vector x_dims(x_org_dims); std::vector y_dims(y_org_dims); const int MB_idx = x_dims.size() - 3; const int H_idx = x_dims.size() - 2; const int W_idx = x_dims.size() - 1; if (trans_x) std::swap(x_dims[H_idx], x_dims[W_idx]); if (trans_y) std::swap(y_dims[H_idx], y_dims[W_idx]); const memory::dim M = x_dims[H_idx]; const memory::dim K = x_dims[W_idx]; const memory::dim N = y_dims[W_idx]; std::vector x_strides(x_dims.size() - 3, 1); std::vector y_strides(x_dims.size() - 3, 1); std::vector out_strides(x_dims.size() - 3, 1); std::vector out_ddims(x_dims.size() - 3, 1); x_strides.reserve(x_dims.size()); y_strides.reserve(x_dims.size()); out_strides.reserve(x_dims.size()); if (!x_strides_override.empty()) { x_strides = x_strides_override; } else { if (!trans_x) { x_strides.insert(x_strides.end(), {M * K, K, 1}); } else { x_strides.insert(x_strides.end(), {M * K, 1, M}); } } if (!y_strides_override.empty()) { y_strides = y_strides_override; } else { if (!trans_y) { y_strides.insert(y_strides.end(), {N * K, N, 1}); } else { y_strides.insert(y_strides.end(), {N * K, 1, K}); } } out_strides.insert(out_strides.end(), {M * N, N, 1}); out_ddims.insert(out_ddims.end(), {std::max(x_dims[MB_idx], y_dims[MB_idx]), M, N}); for (int i = x_dims.size() - 4; i >= 0; --i) { out_ddims[i] = std::max(x_dims[i], y_dims[i]); if (x_strides_override.empty()) { x_strides[i] = x_dims[i + 1] * x_strides[i + 1]; } if (y_strides_override.empty()) { y_strides[i] = y_dims[i + 1] * y_strides[i + 1]; } out_strides[i] = out_ddims[i + 1] * out_strides[i + 1]; } if (!IsInt8() && !IsBfloat16() && is_output_fused) { out_strides = FakeTransposeStrides(out_ddims); } auto x_md = memory::desc(x_dims, MKLDNNGetDataType(), x_strides); auto y_md = memory::desc(y_dims, MKLDNNGetDataType(), y_strides); auto out_md = memory::desc(out_ddims, MKLDNNGetDataType(), out_strides); const dnnl::primitive_attr matmul_attrs = CreateMatmulAttrs(ctx); this->AcquireForwardPrimitiveDescriptor(matmul_attrs, x_md, y_md, out_md); } float ComputeOutputScale(const framework::ExecutionContext& ctx) { float alpha = ctx.HasAttr("alpha") ? ctx.Attr("alpha") : 1.0f; if (ctx.HasAttr("Scale_x") && ctx.HasAttr("Scale_y") && ctx.HasAttr("Scale_out")) { float scale_x = ctx.Attr("Scale_x"); float scale_y = ctx.Attr("Scale_y"); bool force_fp32_out = ctx.HasAttr("force_fp32_output") ? ctx.Attr("force_fp32_output") : false; float scale_out = force_fp32_out ? 1.f : ctx.Attr("Scale_out"); alpha *= scale_out / (scale_x * scale_y); } return alpha; } dnnl::primitive_attr CreateMatmulAttrs( const framework::ExecutionContext& ctx) { dnnl::primitive_attr matmul_attrs; dnnl::post_ops post_operations; float scale_out = ComputeOutputScale(ctx); if (scale_out != 1.0f) { matmul_attrs.set_output_scales(0, {scale_out}); } if (ctx.HasInput("ResidualData")) { auto* residual_data = ctx.Input("ResidualData"); auto residual_data_tz = phi::vectorize(residual_data->dims()); auto residual_data_md = memory::desc(residual_data_tz, MKLDNNGetDataType(), dnnl::memory::format_tag::any); post_operations.append_binary(dnnl::algorithm::binary_add, residual_data_md); if (ctx.HasAttr("Scale_in_eltwise")) { float sum_scale = scale_out / ctx.Attr("Scale_in_eltwise"); post_operations.append_sum(sum_scale); } } AppendActivation(ctx, post_operations); matmul_attrs.set_post_ops(post_operations); return matmul_attrs; } std::vector FakeTransposeStrides( const std::vector& matmul_out_dims) const { // fuse matmul_v2 + transpose + reshape guarantees that output is 4D and // transpose axis are: {0, 2, 1, 3} std::vector transpose_axis = {0, 2, 1, 3}; std::vector fake_strides(transpose_axis.size()); int ndims = static_cast(transpose_axis.size()); int total_stride = 1; for (int i = ndims - 1; i >= 0; --i) { fake_strides[transpose_axis[i]] = total_stride; total_stride *= matmul_out_dims[transpose_axis[i]]; } return fake_strides; } std::shared_ptr AcquireWeightsMemory(const Tensor* input) { const YT* input_data = input->data(); return this->AcquireMemoryFromPrimitive(this->fwd_pd_->weights_desc(), to_void_cast(input_data)); } std::shared_ptr AcquireDstMemory( paddle::framework::Tensor* output) { // We cannot use base AcquireDstMemory as it makes an allocation request // base on DST memory primitive size. This is fine in general, but in MatMul // we have primitive that covers only one batch of Data and then shift // pointer for every new batch. Hence Tensor size is bigger that dst memory // primitive size. So would we request less memory that is there and it // triggers an // assertion. So as there is no 'any' format here we can leave default size // of Tensor as computed in ComputeInferShape OT* ptr = output->mutable_data(this->place_); return this->AcquireMemoryFromPrimitive(this->fwd_pd_->dst_desc(), ptr); } }; template class ActivationMKLDNNHandler : public MKLDNNHandlerNoCachingT { public: ActivationMKLDNNHandler(dnnl::algorithm algorithm, const framework::ExecutionContext& ctx, const dnnl::engine engine, Place cpu_place, const framework::Tensor* x) : platform::MKLDNNHandlerNoCachingT(engine, cpu_place) { float alpha = ctx.HasAttr("alpha") ? ctx.Attr("alpha") : 0; float beta = ctx.HasAttr("beta") ? ctx.Attr("beta") : 0; if (ctx.Type() == "scale") { bool bias_after_scale = ctx.Attr("bias_after_scale"); auto* scale_tensor = ctx.Input("ScaleTensor"); alpha = (scale_tensor == nullptr) ? ctx.Attr("scale") : static_cast(*(scale_tensor->data())); beta = ctx.Attr("bias"); // if bias_after_scale == true // out = scale*X + bias // else // out = scale*(X + bias) = scale*X + scale*bias if (!bias_after_scale) { beta *= alpha; } } else if (ctx.Type() == "clip") { alpha = ctx.HasInput("Min") ? ctx.Input("Min")->data()[0] : ctx.Attr("min"); beta = ctx.HasInput("Max") ? ctx.Input("Max")->data()[0] : ctx.Attr("max"); } else { // paddle uses beta but mkldnn uses alpha for swish if (algorithm == dnnl::algorithm::eltwise_swish) { std::swap(alpha, beta); } else if (algorithm == dnnl::algorithm::eltwise_bounded_relu) { alpha = ctx.Attr("threshold"); } } this->AcquireForwardPrimitiveDescriptor(dnnl::prop_kind::forward_training, algorithm, x->mem_desc(), alpha, beta); } ActivationMKLDNNHandler(dnnl::algorithm algorithm, const framework::ExecutionContext& ctx, const dnnl::engine engine, Place cpu_place, const framework::Tensor* x, const Tensor* dout) : platform::MKLDNNHandlerNoCachingT(engine, cpu_place) { float alpha = ctx.HasAttr("alpha") ? ctx.Attr("alpha") : 0; float beta = ctx.HasAttr("beta") ? ctx.Attr("beta") : 0; // paddle uses beta but mkldnn uses alpha for swish if (algorithm == dnnl::algorithm::eltwise_swish) { std::swap(alpha, beta); } else if (algorithm == dnnl::algorithm::eltwise_bounded_relu) { alpha = ctx.Attr("threshold"); } if (ctx.Type() == "clip_grad") { alpha = ctx.HasInput("Min") ? ctx.Input("Min")->data()[0] : ctx.Attr("min"); beta = ctx.HasInput("Max") ? ctx.Input("Max")->data()[0] : ctx.Attr("max"); } this->AcquireForwardPrimitiveDescriptor(dnnl::prop_kind::forward_training, algorithm, x->mem_desc(), alpha, beta); this->AcquireBackwardPrimitiveDescriptor( algorithm, dout->mem_desc(), x->mem_desc(), alpha, beta); } std::shared_ptr AcquireBackwardSrcMemory( const framework::Tensor* input) { const T* input_data = input->data(); return this->AcquireMemoryFromPrimitive(this->bwd_pd_->src_desc(), to_void_cast(input_data)); } }; static std::unordered_map GetAttributeMap( std::string act_type) { std::unordered_map attr_map; if (act_type == "swish") { attr_map.emplace("beta", "fuse_alpha"); } else if (act_type == "relu6") { attr_map.emplace("threshold", "fuse_alpha"); } else if (act_type == "hard_sigmoid") { attr_map.emplace("slope", "fuse_alpha"); attr_map.emplace("offset", "fuse_beta"); } else if (act_type == "clip") { attr_map.emplace("min", "fuse_alpha"); attr_map.emplace("max", "fuse_beta"); } else { attr_map.emplace("alpha", "fuse_alpha"); attr_map.emplace("beta", "fuse_beta"); } return attr_map; } static std::vector GetSupportedActivations() { return std::vector{"abs", "clip", "gelu", "hard_sigmoid", "hard_swish", "leaky_relu", "mish", "relu", "relu6", "sigmoid", "sqrt", "swish", "tanh"}; } class ReorderMKLDNNHandler { public: ReorderMKLDNNHandler(std::vector& dims, // NOLINT framework::proto::VarType::Type vtype, dnnl::memory::data_type dtype, dnnl::engine engine) : dims_(dims), vtype_(vtype), vtype_dst_(vtype), dtype_(dtype), dtype_dst_(dtype), engine_(engine) {} ReorderMKLDNNHandler(std::vector& dims, // NOLINT framework::proto::VarType::Type vtype, dnnl::memory::data_type dtype, framework::proto::VarType::Type vtype_dst, dnnl::memory::data_type dtype_dst, dnnl::engine engine) : dims_(dims), vtype_(vtype), vtype_dst_(vtype_dst), dtype_(dtype), dtype_dst_(dtype_dst), engine_(engine) {} std::shared_ptr AcquireSrcMemory(const dnnl::memory::desc& md, void* ptr) { return std::make_shared(md, engine_, ptr); } std::shared_ptr AcquireSrcMemory(const MKLDNNMemoryFormat& fmt, void* ptr) { auto md = dnnl::memory::desc(dims_, dtype_, fmt); return std::make_shared(md, engine_, ptr); } std::shared_ptr AcquireSubmemory( const std::vector& dims, const std::vector& offset, const std::shared_ptr& mem_p) { auto sub_md = mem_p->get_desc().submemory_desc(dims, {offset}); auto sub_mem_p = std::make_shared( sub_md, engine_, mem_p->get_data_handle()); return sub_mem_p; } std::shared_ptr AcquireDstMemory(framework::Tensor* output, const MKLDNNMemoryFormat& fmt, platform::Place place) { auto dst_md = platform::MKLDNNMemDesc(dims_, dtype_dst_, fmt); auto dst_data = output->mutable_data( place, framework::TransToPhiDataType(vtype_dst_), dst_md.get_size()); return std::make_shared(dst_md, engine_, dst_data); } std::shared_ptr AcquireDstMemory( framework::Tensor* output, const dnnl::memory::desc& src_md, platform::Place place) { if (vtype_dst_ == vtype_) { auto dst_data = output->mutable_data( place, framework::TransToPhiDataType(vtype_dst_), src_md.get_size()); return std::make_shared(src_md, engine_, dst_data); } else { auto dst_md = src_md; dst_md.data.data_type = static_cast(dtype_dst_); auto dst_data = output->mutable_data( place, framework::TransToPhiDataType(vtype_dst_), dst_md.get_size()); return std::make_shared(dst_md, engine_, dst_data); } } std::shared_ptr AcquireDstMemory( framework::Tensor* output, const std::vector& dims, const MKLDNNMemoryFormat& fmt, platform::Place place) { auto dst_md = platform::MKLDNNMemDesc(dims, dtype_dst_, fmt); auto dst_data = output->mutable_data( place, framework::TransToPhiDataType(vtype_dst_), dst_md.get_size()); return std::make_shared(dst_md, engine_, dst_data); } std::shared_ptr AcquireReorder( std::shared_ptr dst_memory_p, std::shared_ptr src_memory_p) { return std::make_shared(*(src_memory_p), *(dst_memory_p)); } std::shared_ptr AcquireReorder( std::shared_ptr dst_memory_p, std::shared_ptr src_memory_p, const dnnl::primitive_attr& attrs) { return std::make_shared( *(src_memory_p), *(dst_memory_p), attrs); } private: std::vector dims_; framework::proto::VarType::Type vtype_, vtype_dst_; dnnl::memory::data_type dtype_, dtype_dst_; dnnl::engine engine_; }; template static void SetDstMemoryQuantized( const framework::ExecutionContext& ctx, framework::Tensor* output, std::vector dst_tz, const dnnl::engine& engine, std::shared_ptr& dst_md, // NOLINT std::shared_ptr& dst_memory, // NOLINT MKLDNNMemoryFormat output_format) { T* output_data = output->mutable_data(ctx.GetPlace()); const size_t dst_dims = dst_tz.size(); MKLDNNMemoryFormat dst_fmt; PADDLE_ENFORCE_LE(dst_dims, 5, platform::errors::InvalidArgument( "Dst memory for quantization can not have " "dims > 5. But received dst_dims is %d.", dst_dims)); dst_fmt = platform::MKLDNNFormatForSize(dst_dims, output_format); auto tmp_dst_md = platform::MKLDNNMemDesc({dst_tz}, paddle::framework::ToMKLDNNDataType( framework::DataTypeTrait::DataType()), dst_fmt); dst_md.reset(new dnnl::memory::desc(tmp_dst_md)); dst_memory.reset( new dnnl::memory(*dst_md, engine, to_void_cast(output_data))); } } // namespace platform } // namespace paddle