diff --git a/paddle/fluid/framework/expect.h b/paddle/fluid/framework/expect.h index 146f4de9382a687686d5f7fdd6f4fa2300cb043b..686a14fca15c19205b9242e5ad925f25520f133a 100644 --- a/paddle/fluid/framework/expect.h +++ b/paddle/fluid/framework/expect.h @@ -19,14 +19,18 @@ #define _LINUX #endif -#ifdef _LINUX #ifndef likely -#define likely(x) __builtin_expect((x), 1) +#ifdef _LINUX +#define likely(expr) (__builtin_expect(!!(expr), 1)) +#else +#define likely(expr) (expr) #endif #endif -#ifdef _LINUX #ifndef unlikely -#define unlikely(x) __builtin_expect((x), 0) +#ifdef _LINUX +#define unlikely(expr) (__builtin_expect(!!(expr), 0)) +#else +#define unlikely(expr) (expr) #endif #endif diff --git a/paddle/fluid/operators/fused/mkldnn/fusion_gru_mkldnn_op.cc b/paddle/fluid/operators/fused/mkldnn/fusion_gru_mkldnn_op.cc index 6ef49e2cf3db7318f2eb8f0f55ffccd0e3bbad15..f1deab3e65299b5188fbbbb8583705a4560a9ad4 100644 --- a/paddle/fluid/operators/fused/mkldnn/fusion_gru_mkldnn_op.cc +++ b/paddle/fluid/operators/fused/mkldnn/fusion_gru_mkldnn_op.cc @@ -12,6 +12,7 @@ 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. */ +#include "paddle/fluid/framework/expect.h" #include "paddle/fluid/operators/fused/fusion_gru_op.h" #include "paddle/fluid/operators/fused/mkldnn/fusion_rnn_mkldnn.h" @@ -41,7 +42,7 @@ class GRUMKLDNNHandler : public RNNMKLDNNHandler { ctx.InputName("X") + ctx.InputName("WeightH")) { const bool is_INT8 = std::is_same::value; - if (!this->isCached()) { + if (unlikely(!this->isCached())) { // oneDNN kernel has hardcoded activation functions PADDLE_ENFORCE_EQ( ctx.Attr("gate_activation"), "sigmoid", diff --git a/paddle/fluid/operators/fused/mkldnn/fusion_lstm_mkldnn_op.cc b/paddle/fluid/operators/fused/mkldnn/fusion_lstm_mkldnn_op.cc index 385e4ad8808a51a207ef8779c4544da60f0a6a3d..dfd88248ede3452bab7a23ea8f3e349e23430349 100644 --- a/paddle/fluid/operators/fused/mkldnn/fusion_lstm_mkldnn_op.cc +++ b/paddle/fluid/operators/fused/mkldnn/fusion_lstm_mkldnn_op.cc @@ -12,6 +12,7 @@ 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. */ +#include "paddle/fluid/framework/expect.h" #include "paddle/fluid/operators/fused/fusion_lstm_op.h" #include "paddle/fluid/operators/fused/mkldnn/fusion_rnn_mkldnn.h" @@ -40,7 +41,7 @@ class LSTMMKLDNNHandler ctx, dev_ctx, mkldnn_engine, ctx.GetPlace(), input, weight_h, h0, is_reverse, N, Ti, IC, OC, 4, ctx.InputName("X") + ctx.InputName("WeightH")) { - if (!this->isCached()) { + if (unlikely(!this->isCached())) { const bool is_INT8 = std::is_same::value; const bool use_peepholes = ctx.Attr("use_peepholes"); // oneDNN kernel has hardcoded activation functions diff --git a/paddle/fluid/operators/mkldnn/conv_mkldnn_op.cc b/paddle/fluid/operators/mkldnn/conv_mkldnn_op.cc index 68e2a7c8a91bb232fb479942d307679137b6172a..eef38bf99b1366a46b80e7e0088e838110787c39 100644 --- a/paddle/fluid/operators/mkldnn/conv_mkldnn_op.cc +++ b/paddle/fluid/operators/mkldnn/conv_mkldnn_op.cc @@ -14,6 +14,7 @@ #include +#include "paddle/fluid/framework/expect.h" #include "paddle/fluid/operators/conv_op.h" #include "paddle/fluid/platform/cpu_info.h" #include "paddle/fluid/platform/mkldnn_helper.h" @@ -79,7 +80,7 @@ class ConvMKLDNNHandlerT dev_ctx, mkldnn_engine, cpu_place, platform::CreateKey(dev_ctx, framework::vectorize(input->dims()), unique_name)) { - if (!this->isCached()) { + if (unlikely(!this->isCached())) { PADDLE_ENFORCE_EQ( input->layout(), framework::DataLayout::kMKLDNN, platform::errors::InvalidArgument( @@ -264,7 +265,7 @@ class ConvMKLDNNHandlerT dev_ctx, dev_ctx.GetEngine(), cpu_place, platform::CreateKey(dev_ctx, framework::vectorize(in->dims()), unique_name)) { - if (!this->isBwdCached()) { + if (unlikely(!this->isBwdCached())) { PADDLE_ENFORCE_EQ( in->layout(), framework::DataLayout::kMKLDNN, platform::errors::InvalidArgument( diff --git a/paddle/fluid/operators/mkldnn/prelu_mkldnn_op.cc b/paddle/fluid/operators/mkldnn/prelu_mkldnn_op.cc index 8c7113d963bd5214d74b4289dc569e9c33359e57..7119d68d583f0a224860da51793ccc79ecb5b8c4 100644 --- a/paddle/fluid/operators/mkldnn/prelu_mkldnn_op.cc +++ b/paddle/fluid/operators/mkldnn/prelu_mkldnn_op.cc @@ -12,6 +12,7 @@ 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. */ +#include "paddle/fluid/framework/expect.h" #include "paddle/fluid/platform/mkldnn_reuse.h" namespace paddle { @@ -39,7 +40,7 @@ class PReluMKLDNNHandler dev_ctx, engine, cpu_place, platform::CreateKey(dev_ctx, framework::vectorize(x->dims()), uniq_name)) { - if (!this->isCached()) { + if (unlikely(!this->isCached())) { auto x_md = memory::desc(framework::vectorize(x->dims()), MKLDNNGetDataType(), x->format()); diff --git a/paddle/fluid/platform/device_context.cc b/paddle/fluid/platform/device_context.cc index 27b900198bc82e5629cab29e8d325e31ff69d26d..6ffeaf101feca795f8a330b72206dffa2d68904c 100644 --- a/paddle/fluid/platform/device_context.cc +++ b/paddle/fluid/platform/device_context.cc @@ -25,6 +25,7 @@ limitations under the License. */ #include "paddle/fluid/platform/ipu/ipu_backend.h" #endif #include "glog/logging.h" +#include "paddle/fluid/framework/expect.h" #include "paddle/fluid/platform/profiler.h" namespace paddle { @@ -841,15 +842,6 @@ unsigned int MKLDNNDeviceContext::GetCachedObjectsNumber(void) const { return num_entries; } -// TODO(jczaja): Replace with C++20 equivalents when applicable -#ifdef _WIN32 -#define likely(expr) (expr) -#define unlikely(expr) (expr) -#else -#define likely(expr) (__builtin_expect(!!(expr), 1)) -#define unlikely(expr) (__builtin_expect(!!(expr), 0)) -#endif - MKLDNNDeviceContext::BlobPtr_t MKLDNNDeviceContext::GetBlob( const std::string& name) const { BlobMap* pMap = p_blobmap_.get();