Skip to content

Commit 64c1427

Browse files
author
Yibing Liu
authored
Cherry pick 16919 to 1.4 (#16957)
* Check some shapes only in runtime test=release/1.4 * Follow review comments test=release/1.4 * Update API spec
1 parent e06e8fd commit 64c1427

File tree

8 files changed

+40
-25
lines changed

8 files changed

+40
-25
lines changed

paddle/fluid/API.spec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ paddle.fluid.layers.group_norm (ArgSpec(args=['input', 'groups', 'epsilon', 'par
143143
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'))
144144
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'))
145145
paddle.fluid.layers.smooth_l1 (ArgSpec(args=['x', 'y', 'inside_weight', 'outside_weight', 'sigma'], varargs=None, keywords=None, defaults=(None, None, None)), ('document', 'c6b175d253c55baf4b9c0eca9b1dda88'))
146-
paddle.fluid.layers.one_hot (ArgSpec(args=['input', 'depth'], varargs=None, keywords=None, defaults=None), ('document', '6148b6a555cbfb62fdcd030d8982c18c'))
146+
paddle.fluid.layers.one_hot (ArgSpec(args=['input', 'depth'], varargs=None, keywords=None, defaults=None), ('document', 'c87f620c15573442be7c84b50223b567'))
147147
paddle.fluid.layers.autoincreased_step_counter (ArgSpec(args=['counter_name', 'begin', 'step'], varargs=None, keywords=None, defaults=(None, 1, 1)), ('document', '3f6c828594720c9b2da89c464be94478'))
148148
paddle.fluid.layers.reshape (ArgSpec(args=['x', 'shape', 'actual_shape', 'act', 'inplace', 'name'], varargs=None, keywords=None, defaults=(None, None, False, None)), ('document', '323c019f257e55ddea4a824a362de62f'))
149149
paddle.fluid.layers.squeeze (ArgSpec(args=['input', 'axes', 'name'], varargs=None, keywords=None, defaults=(None,)), ('document', '3229d06517f794e86ca3da14c38b1465'))

paddle/fluid/operators/bilinear_tensor_product_op.cc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,11 @@ class BilinearTensorProductOp : public framework::OperatorWithKernel {
3838
PADDLE_ENFORCE_EQ(y_dims.size(), 2UL, "The input(Y) must be a 2D Tensor.");
3939
PADDLE_ENFORCE_EQ(weight_dims.size(), 3UL,
4040
"The input(Weight) must be a 3D tensor.");
41-
PADDLE_ENFORCE_EQ(x_dims[0], y_dims[0],
42-
"The first dimension(batch_size) of input(X) must be "
43-
"equal to the first dimension of the input(Y).");
41+
if (ctx->IsRuntime() || (x_dims[0] > 0 && y_dims[0] > 0)) {
42+
PADDLE_ENFORCE_EQ(x_dims[0], y_dims[0],
43+
"The first dimension(batch_size) of input(X) must be "
44+
"equal to the first dimension of the input(Y).");
45+
}
4446
PADDLE_ENFORCE_EQ(x_dims[1], weight_dims[1],
4547
"The second dimension of input(X) must be equal to "
4648
"the second dimension of the input(Weight).");

paddle/fluid/operators/crf_decoding_op.cc

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,20 +95,23 @@ class CRFDecodingOp : public framework::OperatorWithKernel {
9595
transition_dims[0] - 2, transition_dims[1],
9696
"An invalid dimension for the Input(Transition), which should "
9797
"be a 2-D tensor with shape [(D + 2) x D].");
98-
PADDLE_ENFORCE_EQ(
99-
emission_dims[1], transition_dims[1],
100-
"The 2nd dimension of the Input(Emission) and the Input(Transition) "
101-
"should be equal to the tag number.");
102-
98+
if (ctx->IsRuntime() || (emission_dims[1] > 0 && transition_dims[1] > 0)) {
99+
PADDLE_ENFORCE_EQ(
100+
emission_dims[1], transition_dims[1],
101+
"The 2nd dimension of the Input(Emission) and the Input(Transition) "
102+
"should be equal to the tag number.");
103+
}
103104
if (ctx->HasInput("Label")) {
104105
auto label_dims = ctx->GetInputDim("Label");
105106
PADDLE_ENFORCE(label_dims.size() == 2UL && label_dims[1] == 1UL,
106107
"The Input(Label) should be a 2-D tensor with the 2nd "
107108
"dimensions fixed to 1.");
108-
PADDLE_ENFORCE_EQ(
109-
emission_dims[0], label_dims[0],
110-
"The height of Input(Emission) and the height of Input(Label) "
111-
"should be the same.");
109+
if (ctx->IsRuntime() || (emission_dims[0] > 0 && label_dims[0] > 0)) {
110+
PADDLE_ENFORCE_EQ(
111+
emission_dims[0], label_dims[0],
112+
"The height of Input(Emission) and the height of Input(Label) "
113+
"should be the same.");
114+
}
112115
}
113116

114117
ctx->ShareLoD("Emission", /*->*/ "ViterbiPath");

paddle/fluid/operators/log_loss_op.cc

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,18 @@ class LogLossOp : public framework::OperatorWithKernel {
3131
auto pred_dims = ctx->GetInputDim("Predicted");
3232
auto label_dims = ctx->GetInputDim("Labels");
3333

34-
PADDLE_ENFORCE_EQ(pred_dims, label_dims);
34+
if (ctx->IsRuntime() || (framework::product(pred_dims) > 0 &&
35+
framework::product(label_dims) > 0)) {
36+
PADDLE_ENFORCE_EQ(pred_dims, label_dims);
37+
}
3538
PADDLE_ENFORCE_EQ(pred_dims.size(), 2,
3639
"The rank of Input(Predicted) must be 2 and the shape is "
3740
"[batch_size, 1].");
38-
PADDLE_ENFORCE_EQ(pred_dims[1], 1,
39-
"Each row of Input(Predicted) contains a real value, "
40-
"so the 2nd dimension of Input(X) must be 1.");
41-
41+
if (ctx->IsRuntime()) {
42+
PADDLE_ENFORCE_EQ(pred_dims[1], 1,
43+
"Each row of Input(Predicted) contains a real value, "
44+
"so the 2nd dimension of Input(X) must be 1.");
45+
}
4246
ctx->SetOutputDim("Loss", {pred_dims[0], 1});
4347
ctx->ShareLoD("Predicted", "Loss");
4448
}

paddle/fluid/operators/nce_op.cc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ class NCEOp : public framework::OperatorWithKernel {
3636

3737
auto x_dims = ctx->GetInputDim("Input");
3838
auto label_dims = ctx->GetInputDim("Label");
39-
PADDLE_ENFORCE_EQ(x_dims[0], label_dims[0]);
39+
if (ctx->IsRuntime() || (x_dims[0] > 0 && label_dims[0] > 0)) {
40+
PADDLE_ENFORCE_EQ(x_dims[0], label_dims[0]);
41+
}
4042
int num_true_classes = label_dims.size() == 2 ? label_dims[1] : 1;
4143
if (ctx->HasInput("Bias")) {
4244
PADDLE_ENFORCE_EQ(ctx->GetInputDim("Weight")[0],
@@ -60,7 +62,8 @@ class NCEOp : public framework::OperatorWithKernel {
6062
// set dims of output(SampleOut)
6163
std::vector<int64_t> sample_out_dims;
6264
sample_out_dims.push_back(x_dims[0]);
63-
sample_out_dims.push_back(num_neg_samples + num_true_classes);
65+
sample_out_dims.push_back(
66+
(num_true_classes == -1) ? -1 : (num_neg_samples + num_true_classes));
6467
ctx->SetOutputDim("SampleLogits", framework::make_ddim(sample_out_dims));
6568
ctx->SetOutputDim("SampleLabels", framework::make_ddim(sample_out_dims));
6669
}

paddle/fluid/operators/one_hot_op.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ class OneHotOp : public framework::OperatorWithKernel {
3030
auto x_dims = ctx->GetInputDim("X");
3131
PADDLE_ENFORCE_GE(x_dims.size(), 2,
3232
"Rank of Input(X) should be at least 2.");
33-
PADDLE_ENFORCE_GE(x_dims[x_dims.size() - 1], 1U,
34-
"Last dimension of Input(X) should be 1.");
35-
33+
if (ctx->IsRuntime() || x_dims[x_dims.size() - 1] > 0) {
34+
PADDLE_ENFORCE_GE(x_dims[x_dims.size() - 1], 1U,
35+
"Last dimension of Input(X) should be 1.");
36+
}
3637
int depth = ctx->Attrs().Get<int>("depth");
3738

3839
PADDLE_ENFORCE_GT(depth, 0, "Should provide a positive depth (%d).", depth);

paddle/fluid/operators/random_crop_op.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ class RandomCropOpInferShape : public framework::InferShapeBase {
6060
for (size_t i = 1; i <= shape.size(); ++i) {
6161
size_t x_i = x_dim.size() - i;
6262
size_t shape_i = shape.size() - i;
63-
PADDLE_ENFORCE_GE(x_dim[x_i], shape[shape_i]);
63+
if (ctx->IsRuntime() || (x_dim[x_i] > 0 && shape[shape_i] > 0)) {
64+
PADDLE_ENFORCE_GE(x_dim[x_i], shape[shape_i]);
65+
}
6466
out_dim[x_i] = shape[shape_i];
6567
}
6668
ctx->SetOutputDim("Out", framework::make_ddim(out_dim));

python/paddle/fluid/layers/nn.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6261,7 +6261,7 @@ def one_hot(input, depth):
62616261
Examples:
62626262
.. code-block:: python
62636263
6264-
label = layers.data(name="label", shape=[1], dtype="float32")
6264+
label = layers.data(name="label", shape=[1], dtype="int64")
62656265
one_hot_label = layers.one_hot(input=label, depth=10)
62666266
"""
62676267
helper = LayerHelper("one_hot", **locals())

0 commit comments

Comments
 (0)