From 8b59ac3ad076cc67dad09977c4bde6fb771ba0d4 Mon Sep 17 00:00:00 2001 From: Chen Weihang Date: Mon, 28 Oct 2019 12:45:45 +0800 Subject: [PATCH] delete paddle infershape enforce marco (#20832) --- paddle/fluid/operators/linear_chain_crf_op.cc | 36 ++++++++-------- paddle/fluid/operators/metrics/accuracy_op.cc | 27 ++++++------ paddle/fluid/operators/metrics/auc_op.cc | 14 ++++--- paddle/fluid/operators/smooth_l1_loss_op.cc | 12 +++--- .../fluid/operators/squared_l2_distance_op.cc | 14 ++++--- paddle/fluid/platform/enforce.h | 41 ------------------- 6 files changed, 58 insertions(+), 86 deletions(-) diff --git a/paddle/fluid/operators/linear_chain_crf_op.cc b/paddle/fluid/operators/linear_chain_crf_op.cc index b6758c8975b..32d9f09e3a2 100644 --- a/paddle/fluid/operators/linear_chain_crf_op.cc +++ b/paddle/fluid/operators/linear_chain_crf_op.cc @@ -184,30 +184,34 @@ class LinearChainCRFOp : public framework::OperatorWithKernel { true, "The Input(Label) should be a 3-D tensor with last " "dimension fixed to 1 or a 2-D tensor in padding mode."); - PADDLE_INFERSHAPE_ENFORCE_EQ( - ctx, emission_dims[0], label_dims[0], - "The batch size of Input(Emission) and Input(Label) " - "should be the same."); - PADDLE_INFERSHAPE_ENFORCE_EQ( - ctx, emission_dims[1], label_dims[1], - "The max length of Input(Emission) and Input(Label) " - "should be the same."); + if (ctx->IsRuntime()) { + PADDLE_ENFORCE_EQ(emission_dims[0], label_dims[0], + "The batch size of Input(Emission) and Input(Label) " + "should be the same."); + PADDLE_ENFORCE_EQ(emission_dims[1], label_dims[1], + "The max length of Input(Emission) and Input(Label) " + "should be the same."); + } } else { PADDLE_ENFORCE_EQ(emission_dims.size(), 2, "The Input(Emission) should be a 2-D tensor."); - PADDLE_INFERSHAPE_ENFORCE_EQ( - ctx, emission_dims[1], transition_dims[1], - "The 2nd dimension of the Input(Emission) and the Input(Transition) " - "should be equal to the tag number."); + if (ctx->IsRuntime()) { + PADDLE_ENFORCE_EQ(emission_dims[1], transition_dims[1], + "The 2nd dimension of the Input(Emission) and the " + "Input(Transition) " + "should be equal to the tag number."); + } auto label_dims = ctx->GetInputDim("Label"); PADDLE_ENFORCE_EQ(label_dims.size(), 2, "The Input(Label) should be a 2-D tensor with the 2nd " "dimensions fixed to 1."); - PADDLE_INFERSHAPE_ENFORCE_EQ( - ctx, emission_dims[0], label_dims[0], - "The height of Input(Emission) and the height of Input(Label) " - "should be the same."); + if (ctx->IsRuntime()) { + PADDLE_ENFORCE_EQ( + emission_dims[0], label_dims[0], + "The height of Input(Emission) and the height of Input(Label) " + "should be the same."); + } } ctx->SetOutputDim("Alpha", emission_dims); ctx->SetOutputDim("EmissionExps", emission_dims); diff --git a/paddle/fluid/operators/metrics/accuracy_op.cc b/paddle/fluid/operators/metrics/accuracy_op.cc index bedcfc6a387..937b987b88c 100644 --- a/paddle/fluid/operators/metrics/accuracy_op.cc +++ b/paddle/fluid/operators/metrics/accuracy_op.cc @@ -45,19 +45,20 @@ class AccuracyOp : public framework::OperatorWithKernel { "ShapeError: label's dimensions of AccuracyOp must be 2. " "But received label's dimensions = %d, label's shape = [%s]", label_dim.size(), label_dim); - PADDLE_INFERSHAPE_ENFORCE_EQ( - ctx, label_dim[1], 1, - "ShapeError: label's second dimension of " - "AccuracyOp must be 1. But received label's " - "second dimension is = %d, label's shape = [%s]", - label_dim[1], label_dim); - PADDLE_INFERSHAPE_ENFORCE_EQ( - ctx, inference_dim[0], label_dim[0], - "ShapeError: the output's num_rows of AccuracyOp must be" - " the same as label's num_rows. But received output's " - "shape = [%s], label's shape = [%s], output's num_rows = %d, label's " - "num_rows = %d", - inference_dim, label_dim, inference_dim[0], label_dim[0]); + if (ctx->IsRuntime()) { + PADDLE_ENFORCE_EQ(label_dim[1], 1, + "ShapeError: label's second dimension of " + "AccuracyOp must be 1. But received label's " + "second dimension is = %d, label's shape = [%s]", + label_dim[1], label_dim); + PADDLE_ENFORCE_EQ( + inference_dim[0], label_dim[0], + "ShapeError: the output's num_rows of AccuracyOp must be" + " the same as label's num_rows. But received output's " + "shape = [%s], label's shape = [%s], output's num_rows = %d, label's " + "num_rows = %d", + inference_dim, label_dim, inference_dim[0], label_dim[0]); + } ctx->SetOutputDim("Accuracy", {1}); ctx->SetOutputDim("Correct", {1}); diff --git a/paddle/fluid/operators/metrics/auc_op.cc b/paddle/fluid/operators/metrics/auc_op.cc index 3543a334935..90734307acc 100644 --- a/paddle/fluid/operators/metrics/auc_op.cc +++ b/paddle/fluid/operators/metrics/auc_op.cc @@ -28,14 +28,18 @@ class AucOp : public framework::OperatorWithKernel { PADDLE_ENFORCE(ctx->HasInput("Label"), "Input of Label should not be null."); auto predict_width = ctx->GetInputDim("Predict")[1]; - PADDLE_INFERSHAPE_ENFORCE_LE(ctx, predict_width, 2, - "Only support binary classification," - "prediction dims[1] should be 1 or 2"); + if (ctx->IsRuntime()) { + PADDLE_ENFORCE_LE(predict_width, 2, + "Only support binary classification," + "prediction dims[1] should be 1 or 2"); + } auto predict_height = ctx->GetInputDim("Predict")[0]; auto label_height = ctx->GetInputDim("Label")[0]; - PADDLE_INFERSHAPE_ENFORCE_EQ(ctx, predict_height, label_height, - "Out and Label should have same height."); + if (ctx->IsRuntime()) { + PADDLE_ENFORCE_EQ(predict_height, label_height, + "Out and Label should have same height."); + } int num_pred_buckets = ctx->Attrs().Get("num_thresholds") + 1; int slide_steps = ctx->Attrs().Get("slide_steps"); diff --git a/paddle/fluid/operators/smooth_l1_loss_op.cc b/paddle/fluid/operators/smooth_l1_loss_op.cc index 22b621248d6..fbfe3331188 100644 --- a/paddle/fluid/operators/smooth_l1_loss_op.cc +++ b/paddle/fluid/operators/smooth_l1_loss_op.cc @@ -135,11 +135,13 @@ class SmoothL1LossGradOp : public framework::OperatorWithKernel { PADDLE_ENFORCE_GE(out_dims.size(), 2, "The tensor rank of Input(Out@Grad) should be 2."); - PADDLE_INFERSHAPE_ENFORCE_EQ(ctx, out_dims[0], in_dims[0], - "The 1st dimension of Input(Out@Grad) must be " - "same as input."); - PADDLE_INFERSHAPE_ENFORCE_EQ( - ctx, out_dims[1], 1, "The 2nd dimension of Input(Out@Grad) must be 1."); + if (ctx->IsRuntime()) { + PADDLE_ENFORCE_EQ(out_dims[0], in_dims[0], + "The 1st dimension of Input(Out@Grad) must be " + "same as input."); + PADDLE_ENFORCE_EQ(out_dims[1], 1, + "The 2nd dimension of Input(Out@Grad) must be 1."); + } auto x_grad_name = framework::GradVarName("X"); auto y_grad_name = framework::GradVarName("Y"); diff --git a/paddle/fluid/operators/squared_l2_distance_op.cc b/paddle/fluid/operators/squared_l2_distance_op.cc index 17538c98fe5..6c0b195decc 100644 --- a/paddle/fluid/operators/squared_l2_distance_op.cc +++ b/paddle/fluid/operators/squared_l2_distance_op.cc @@ -137,12 +137,14 @@ class SquaredL2DistanceGradOp : public framework::OperatorWithKernel { auto out_dims = ctx->GetInputDim(framework::GradVarName("Out")); auto x_dims = ctx->GetInputDim("X"); auto y_dims = ctx->GetInputDim("Y"); - PADDLE_INFERSHAPE_ENFORCE_EQ(ctx, out_dims[0], x_dims[0], - "First dimension of output gradient and " - "input value must be equal."); - PADDLE_INFERSHAPE_ENFORCE_EQ(ctx, out_dims[1], 1, - "Second dimension of output gradient " - "must be 1."); + if (ctx->IsRuntime()) { + PADDLE_ENFORCE_EQ(out_dims[0], x_dims[0], + "First dimension of output gradient and " + "input value must be equal."); + PADDLE_ENFORCE_EQ(out_dims[1], 1, + "Second dimension of output gradient " + "must be 1."); + } auto x_grad_name = framework::GradVarName("X"); auto y_grad_name = framework::GradVarName("Y"); if (ctx->HasOutput(x_grad_name)) ctx->SetOutputDim(x_grad_name, x_dims); diff --git a/paddle/fluid/platform/enforce.h b/paddle/fluid/platform/enforce.h index a1dc1718327..9263ab401bf 100644 --- a/paddle/fluid/platform/enforce.h +++ b/paddle/fluid/platform/enforce.h @@ -484,46 +484,5 @@ struct BinaryCompareMessageConverter { #define PADDLE_ENFORCE_LE(__VAL0, __VAL1, ...) \ __PADDLE_BINARY_COMPARE(__VAL0, __VAL1, <=, >, __VA_ARGS__) -#define __PADDLE_INFERSHAPE_BINARY_COMPARE(__CTX, __VAL1, __VAL2, __CMP, \ - __INV_CMP, ...) \ - do { \ - auto __val1 = (__VAL1); \ - auto __val2 = (__VAL2); \ - if (!__CTX->IsRuntime()) { \ - if (__val1 == -1 || __val2 == -1) { \ - break; \ - } \ - } \ - using __TYPE1__ = decltype(__val1); \ - using __TYPE2__ = decltype(__val2); \ - using __COMMON_TYPE1__ = \ - ::paddle::platform::details::CommonType1<__TYPE1__, __TYPE2__>; \ - using __COMMON_TYPE2__ = \ - ::paddle::platform::details::CommonType2<__TYPE1__, __TYPE2__>; \ - bool __is_not_error = (static_cast<__COMMON_TYPE1__>(__val1))__CMP( \ - static_cast<__COMMON_TYPE2__>(__val2)); \ - if (UNLIKELY(!__is_not_error)) { \ - PADDLE_THROW("Expected %s " #__CMP " %s, but received %s:%s " #__INV_CMP \ - " %s:%s.\n%s", \ - #__VAL1, #__VAL2, #__VAL1, \ - ::paddle::string::to_string(__val1), #__VAL2, \ - ::paddle::string::to_string(__val2), \ - ::paddle::string::Sprintf(__VA_ARGS__)); \ - } \ - } while (0) - -#define PADDLE_INFERSHAPE_ENFORCE_EQ(__CTX, __VAL0, __VAL1, ...) \ - __PADDLE_INFERSHAPE_BINARY_COMPARE(__CTX, __VAL0, __VAL1, ==, !=, __VA_ARGS__) -#define PADDLE_INFERSHAPE_ENFORCE_NE(__CTX, __VAL0, __VAL1, ...) \ - __PADDLE_INFERSHAPE_BINARY_COMPARE(__CTX, __VAL0, __VAL1, !=, ==, __VA_ARGS__) -#define PADDLE_INFERSHAPE_ENFORCE_GT(__CTX, __VAL0, __VAL1, ...) \ - __PADDLE_INFERSHAPE_BINARY_COMPARE(__CTX, __VAL0, __VAL1, >, <=, __VA_ARGS__) -#define PADDLE_INFERSHAPE_ENFORCE_GE(__CTX, __VAL0, __VAL1, ...) \ - __PADDLE_INFERSHAPE_BINARY_COMPARE(__CTX, __VAL0, __VAL1, >=, <, __VA_ARGS__) -#define PADDLE_INFERSHAPE_ENFORCE_LT(__CTX, __VAL0, __VAL1, ...) \ - __PADDLE_INFERSHAPE_BINARY_COMPARE(__CTX, __VAL0, __VAL1, <, >=, __VA_ARGS__) -#define PADDLE_INFERSHAPE_ENFORCE_LE(__CTX, __VAL0, __VAL1, ...) \ - __PADDLE_INFERSHAPE_BINARY_COMPARE(__CTX, __VAL0, __VAL1, <=, >, __VA_ARGS__) - } // namespace platform } // namespace paddle -- GitLab