From 36c05d36ab4340637f54311f7dc48aac879a7a87 Mon Sep 17 00:00:00 2001 From: Yibing Liu Date: Fri, 19 Apr 2019 18:25:24 +0800 Subject: [PATCH] Check some shapes only in runtime (#16919) * Check some shapes only in runtime test=develop * Follow review comments test=develop * Update API spec --- paddle/fluid/API.spec | 2 +- .../operators/bilinear_tensor_product_op.cc | 8 ++++--- paddle/fluid/operators/crf_decoding_op.cc | 21 +++++++++++-------- paddle/fluid/operators/log_loss_op.cc | 14 ++++++++----- paddle/fluid/operators/nce_op.cc | 7 +++++-- paddle/fluid/operators/one_hot_op.cc | 7 ++++--- paddle/fluid/operators/random_crop_op.cc | 4 +++- python/paddle/fluid/layers/nn.py | 2 +- 8 files changed, 40 insertions(+), 25 deletions(-) diff --git a/paddle/fluid/API.spec b/paddle/fluid/API.spec index 6a2431d46..7e3c8e645 100644 --- a/paddle/fluid/API.spec +++ b/paddle/fluid/API.spec @@ -145,7 +145,7 @@ paddle.fluid.layers.group_norm (ArgSpec(args=['input', 'groups', 'epsilon', 'par paddle.fluid.layers.spectral_norm (ArgSpec(args=['weight', 'dim', 'power_iters', 'eps', 'name'], varargs=None, keywords=None, defaults=(0, 1, 1e-12, None)), ('document', '3f536aafba30d793287b52d231baff1b')) paddle.fluid.layers.softmax_with_cross_entropy (ArgSpec(args=['logits', 'label', 'soft_label', 'ignore_index', 'numeric_stable_mode', 'return_softmax'], varargs=None, keywords=None, defaults=(False, -100, True, False)), ('document', 'bce1b75e3d95b75cacd1099655cbb3c3')) paddle.fluid.layers.smooth_l1 (ArgSpec(args=['x', 'y', 'inside_weight', 'outside_weight', 'sigma'], varargs=None, keywords=None, defaults=(None, None, None)), ('document', 'c6b175d253c55baf4b9c0eca9b1dda88')) -paddle.fluid.layers.one_hot (ArgSpec(args=['input', 'depth'], varargs=None, keywords=None, defaults=None), ('document', '6148b6a555cbfb62fdcd030d8982c18c')) +paddle.fluid.layers.one_hot (ArgSpec(args=['input', 'depth'], varargs=None, keywords=None, defaults=None), ('document', 'c87f620c15573442be7c84b50223b567')) paddle.fluid.layers.autoincreased_step_counter (ArgSpec(args=['counter_name', 'begin', 'step'], varargs=None, keywords=None, defaults=(None, 1, 1)), ('document', '3f6c828594720c9b2da89c464be94478')) paddle.fluid.layers.reshape (ArgSpec(args=['x', 'shape', 'actual_shape', 'act', 'inplace', 'name'], varargs=None, keywords=None, defaults=(None, None, False, None)), ('document', '323c019f257e55ddea4a824a362de62f')) paddle.fluid.layers.squeeze (ArgSpec(args=['input', 'axes', 'name'], varargs=None, keywords=None, defaults=(None,)), ('document', '3229d06517f794e86ca3da14c38b1465')) diff --git a/paddle/fluid/operators/bilinear_tensor_product_op.cc b/paddle/fluid/operators/bilinear_tensor_product_op.cc index bd69f422e..f2c30cd7e 100644 --- a/paddle/fluid/operators/bilinear_tensor_product_op.cc +++ b/paddle/fluid/operators/bilinear_tensor_product_op.cc @@ -41,9 +41,11 @@ class BilinearTensorProductOp : public framework::OperatorWithKernel { PADDLE_ENFORCE_EQ(y_dims.size(), 2UL, "The input(Y) must be a 2D Tensor."); PADDLE_ENFORCE_EQ(weight_dims.size(), 3UL, "The input(Weight) must be a 3D tensor."); - PADDLE_ENFORCE_EQ(x_dims[0], y_dims[0], - "The first dimension(batch_size) of input(X) must be " - "equal to the first dimension of the input(Y)."); + if (ctx->IsRuntime() || (x_dims[0] > 0 && y_dims[0] > 0)) { + PADDLE_ENFORCE_EQ(x_dims[0], y_dims[0], + "The first dimension(batch_size) of input(X) must be " + "equal to the first dimension of the input(Y)."); + } PADDLE_ENFORCE_EQ(x_dims[1], weight_dims[1], "The second dimension of input(X) must be equal to " "the second dimension of the input(Weight)."); diff --git a/paddle/fluid/operators/crf_decoding_op.cc b/paddle/fluid/operators/crf_decoding_op.cc index e053ae577..c701e895a 100644 --- a/paddle/fluid/operators/crf_decoding_op.cc +++ b/paddle/fluid/operators/crf_decoding_op.cc @@ -95,20 +95,23 @@ class CRFDecodingOp : public framework::OperatorWithKernel { transition_dims[0] - 2, transition_dims[1], "An invalid dimension for the Input(Transition), which should " "be a 2-D tensor with shape [(D + 2) x D]."); - 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."); - + if (ctx->IsRuntime() || (emission_dims[1] > 0 && transition_dims[1] > 0)) { + 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."); + } if (ctx->HasInput("Label")) { auto label_dims = ctx->GetInputDim("Label"); PADDLE_ENFORCE(label_dims.size() == 2UL && label_dims[1] == 1UL, "The Input(Label) should be a 2-D tensor with the 2nd " "dimensions fixed to 1."); - PADDLE_ENFORCE_EQ( - emission_dims[0], label_dims[0], - "The height of Input(Emission) and the height of Input(Label) " - "should be the same."); + if (ctx->IsRuntime() || (emission_dims[0] > 0 && label_dims[0] > 0)) { + 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->ShareLoD("Emission", /*->*/ "ViterbiPath"); diff --git a/paddle/fluid/operators/log_loss_op.cc b/paddle/fluid/operators/log_loss_op.cc index e8850a1e5..0048c75cc 100644 --- a/paddle/fluid/operators/log_loss_op.cc +++ b/paddle/fluid/operators/log_loss_op.cc @@ -31,14 +31,18 @@ class LogLossOp : public framework::OperatorWithKernel { auto pred_dims = ctx->GetInputDim("Predicted"); auto label_dims = ctx->GetInputDim("Labels"); - PADDLE_ENFORCE_EQ(pred_dims, label_dims); + if (ctx->IsRuntime() || (framework::product(pred_dims) > 0 && + framework::product(label_dims) > 0)) { + PADDLE_ENFORCE_EQ(pred_dims, label_dims); + } PADDLE_ENFORCE_EQ(pred_dims.size(), 2, "The rank of Input(Predicted) must be 2 and the shape is " "[batch_size, 1]."); - PADDLE_ENFORCE_EQ(pred_dims[1], 1, - "Each row of Input(Predicted) contains a real value, " - "so the 2nd dimension of Input(X) must be 1."); - + if (ctx->IsRuntime()) { + PADDLE_ENFORCE_EQ(pred_dims[1], 1, + "Each row of Input(Predicted) contains a real value, " + "so the 2nd dimension of Input(X) must be 1."); + } ctx->SetOutputDim("Loss", {pred_dims[0], 1}); ctx->ShareLoD("Predicted", "Loss"); } diff --git a/paddle/fluid/operators/nce_op.cc b/paddle/fluid/operators/nce_op.cc index 358e4f37b..0ccc5d30b 100644 --- a/paddle/fluid/operators/nce_op.cc +++ b/paddle/fluid/operators/nce_op.cc @@ -36,7 +36,9 @@ class NCEOp : public framework::OperatorWithKernel { auto x_dims = ctx->GetInputDim("Input"); auto label_dims = ctx->GetInputDim("Label"); - PADDLE_ENFORCE_EQ(x_dims[0], label_dims[0]); + if (ctx->IsRuntime() || (x_dims[0] > 0 && label_dims[0] > 0)) { + PADDLE_ENFORCE_EQ(x_dims[0], label_dims[0]); + } int num_true_classes = label_dims.size() == 2 ? label_dims[1] : 1; if (ctx->HasInput("Bias")) { PADDLE_ENFORCE_EQ(ctx->GetInputDim("Weight")[0], @@ -60,7 +62,8 @@ class NCEOp : public framework::OperatorWithKernel { // set dims of output(SampleOut) std::vector sample_out_dims; sample_out_dims.push_back(x_dims[0]); - sample_out_dims.push_back(num_neg_samples + num_true_classes); + sample_out_dims.push_back( + (num_true_classes == -1) ? -1 : (num_neg_samples + num_true_classes)); ctx->SetOutputDim("SampleLogits", framework::make_ddim(sample_out_dims)); ctx->SetOutputDim("SampleLabels", framework::make_ddim(sample_out_dims)); } diff --git a/paddle/fluid/operators/one_hot_op.cc b/paddle/fluid/operators/one_hot_op.cc index 4fcb1d699..626895f49 100644 --- a/paddle/fluid/operators/one_hot_op.cc +++ b/paddle/fluid/operators/one_hot_op.cc @@ -30,9 +30,10 @@ class OneHotOp : public framework::OperatorWithKernel { auto x_dims = ctx->GetInputDim("X"); PADDLE_ENFORCE_GE(x_dims.size(), 2, "Rank of Input(X) should be at least 2."); - PADDLE_ENFORCE_GE(x_dims[x_dims.size() - 1], 1U, - "Last dimension of Input(X) should be 1."); - + if (ctx->IsRuntime() || x_dims[x_dims.size() - 1] > 0) { + PADDLE_ENFORCE_GE(x_dims[x_dims.size() - 1], 1U, + "Last dimension of Input(X) should be 1."); + } int depth = ctx->Attrs().Get("depth"); PADDLE_ENFORCE_GT(depth, 0, "Should provide a positive depth (%d).", depth); diff --git a/paddle/fluid/operators/random_crop_op.cc b/paddle/fluid/operators/random_crop_op.cc index cd3bd32ad..dad46ec66 100644 --- a/paddle/fluid/operators/random_crop_op.cc +++ b/paddle/fluid/operators/random_crop_op.cc @@ -60,7 +60,9 @@ class RandomCropOpInferShape : public framework::InferShapeBase { for (size_t i = 1; i <= shape.size(); ++i) { size_t x_i = x_dim.size() - i; size_t shape_i = shape.size() - i; - PADDLE_ENFORCE_GE(x_dim[x_i], shape[shape_i]); + if (ctx->IsRuntime() || (x_dim[x_i] > 0 && shape[shape_i] > 0)) { + PADDLE_ENFORCE_GE(x_dim[x_i], shape[shape_i]); + } out_dim[x_i] = shape[shape_i]; } ctx->SetOutputDim("Out", framework::make_ddim(out_dim)); diff --git a/python/paddle/fluid/layers/nn.py b/python/paddle/fluid/layers/nn.py index 2bac9dd9a..6bf437b4d 100644 --- a/python/paddle/fluid/layers/nn.py +++ b/python/paddle/fluid/layers/nn.py @@ -6394,7 +6394,7 @@ def one_hot(input, depth): Examples: .. code-block:: python - label = layers.data(name="label", shape=[1], dtype="float32") + label = layers.data(name="label", shape=[1], dtype="int64") one_hot_label = layers.one_hot(input=label, depth=10) """ helper = LayerHelper("one_hot", **locals()) -- GitLab