未验证 提交 fe8d006f 编写于 作者: G Guo Sheng 提交者: GitHub

API/OP(sequence_expand_as) error message enhancement (#23712)

* API/OP(sequence_expand_as) error message enhancement.
test=develop
Co-authored-by: NFrostML <380185688@qq.com>
上级 03ba5b74
......@@ -27,18 +27,18 @@ class SequenceExpandAsOp : public framework::OperatorWithKernel {
protected:
void InferShape(framework::InferShapeContext* ctx) const override {
PADDLE_ENFORCE(ctx->HasInput("X"),
"Input(X) of SequenceExpandAsOp should not be null.");
PADDLE_ENFORCE(ctx->HasInput("Y"),
"Input(Y) of SequenceExpandAsOp should not be null.");
PADDLE_ENFORCE(ctx->HasOutput("Out"),
"Output(Out) of SequenceExpandAsOp should not be null.");
OP_INOUT_CHECK(ctx->HasInputs("X"), "Input", "X", "SequenceExpandAs");
OP_INOUT_CHECK(ctx->HasInputs("Y"), "Input", "Y", "SequenceExpandAs");
OP_INOUT_CHECK(ctx->HasOutput("Out"), "Output", "Out", "SequenceExpandAs");
auto x_dims = ctx->GetInputDim("X");
auto out_dims = x_dims;
PADDLE_ENFORCE_GE(x_dims.size(), 2,
"Dimension number of Input(X) should be at least 2.");
platform::errors::InvalidArgument(
"Dimension number of Input(X) should be at least 2. "
"But received X's dimensions = %d, X's shape = [%s].",
x_dims.size(), x_dims));
if (ctx->IsRuntime()) {
framework::Variable* x_var =
......@@ -50,11 +50,17 @@ class SequenceExpandAsOp : public framework::OperatorWithKernel {
auto& y_lod = y_var->Get<LoDTensor>().lod();
PADDLE_ENFORCE_EQ(y_lod.size(), 1,
"Level number of Input(Y)'s lod should be 1.");
platform::errors::InvalidArgument(
"Level number of Input(Y)'s lod should be 1. But "
"received Y's lod level = %d.",
y_lod.size()));
PADDLE_ENFORCE_EQ(static_cast<size_t>(x_dim[0]), y_lod[0].size() - 1,
"The first dimension of Input(X) should be equal "
"to the size of Input(Y)'s 0 level lod.");
platform::errors::InvalidArgument(
"The first dimension of Input(X) should be one "
"less than the size of Input(Y)'s 0 level lod. But "
"received X's shape[0] = %d, Y's lod[0].size = %d.",
x_dim[0], y_lod[0].size()));
int64_t out_first_dim = 0;
if (y_lod[0].size() <= 1) {
......@@ -138,9 +144,9 @@ class SequenceExpandAsOpGrad : public framework::OperatorWithKernel {
protected:
void InferShape(framework::InferShapeContext* ctx) const override {
PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) should not be null.");
PADDLE_ENFORCE(ctx->HasInput(framework::GradVarName("Out")),
"Input(Out@GRAD) should not be null.");
OP_INOUT_CHECK(ctx->HasInputs("X"), "Input", "X", "SequenceExpandAsGrad");
OP_INOUT_CHECK(ctx->HasInputs(framework::GradVarName("Out")), "Input",
"Out@GRAD", "SequenceExpandAsGrad");
auto x_dims = ctx->GetInputDim("X");
auto x_grad_name = framework::GradVarName("X");
......
......@@ -74,13 +74,25 @@ class SequenceExpandAsKernel : public framework::OpKernel<T> {
auto *y = context.Input<framework::LoDTensor>("Y");
auto *out = context.Output<framework::LoDTensor>("Out");
PADDLE_ENFORCE_EQ(y->lod().empty(), false,
"Input(Y) Tensor of SequenceExpandAsOp does not contain "
"LoD information.");
PADDLE_ENFORCE_EQ(
y->lod().empty(), false,
platform::errors::InvalidArgument(
"Input(Y) of SequenceExpandAsOp has wrong LoD information. "
"Expected Y's lod is not empty, but received empty lod."));
auto &y_lod = y->lod();
PADDLE_ENFORCE_EQ(y_lod.size(), 1, "LoD of Y should be 1.");
PADDLE_ENFORCE_GT(y_lod[0].size(), 1, ".");
PADDLE_ENFORCE_EQ(y_lod.size(), 1,
platform::errors::InvalidArgument(
"Input(Y) of SequenceExpandAsOp has wrong LoD "
"information. Expected Y's lod level = 1, but "
"received lod level = %d.",
y_lod.size()));
PADDLE_ENFORCE_GT(y_lod[0].size(), 1,
platform::errors::InvalidArgument(
"Input(Y) of SequenceExpandAsOp has wrong LoD "
"information. Expected the size of Y's lod[0] > 1, "
"but received lod[0].size = %d.",
y_lod[0].size()));
out->mutable_data<T>(context.GetPlace());
......
......@@ -815,7 +815,7 @@ def sequence_expand_as(x, y, name=None):
Args:
x (Variable): The input variable which is a Tensor or LoDTensor, with the \
dims ``[M, K]``. The data type should be float32, float64, int8, int32 \
dims ``[M, K]``. The data type should be float32, float64, int32 \
or int64.
y (Variable): The input variable which is a LoDTensor with 1-level lod.
name (str, optional): For detailed information, please refer \
......@@ -872,6 +872,9 @@ def sequence_expand_as(x, y, name=None):
"""
assert not in_dygraph_mode(), (
"sequence layer is not supported in dygraph mode yet.")
check_variable_and_dtype(x, 'x', ['float32', 'float64', 'int32', 'int64'],
'sequence_expand_as')
check_type(y, 'y', Variable, 'sequence_expand_as')
helper = LayerHelper('sequence_expand_as', input=x, **locals())
dtype = helper.input_dtype()
tmp = helper.create_variable_for_type_inference(dtype)
......
......@@ -18,7 +18,9 @@ import unittest
import numpy as np
import sys
sys.path.append("../")
import paddle.fluid as fluid
from op_test import OpTest
from paddle.fluid import Program, program_guard
class TestSequenceExpandAs(OpTest):
......@@ -84,5 +86,22 @@ class TestSequenceExpandAsCase3(TestSequenceExpandAs):
self.inputs = {'X': (x_data, x_lod), 'Y': (y_data, y_lod)}
class TestSequenceExpandAsOpError(unittest.TestCase):
def test_errors(self):
with program_guard(Program(), Program()):
# the input x must be Variable
x1 = np.random.random((2, 4)).astype("float32")
self.assertRaises(TypeError, fluid.layers.sequence_expand_as, x1)
# the dtype of input x must be float32, float64, int32 or int64
x2 = fluid.data(name='x2', shape=[None, 4], dtype="bool")
self.assertRaises(TypeError, fluid.layers.sequence_expand_as, x2)
# the input y must be Variable
x3 = fluid.data(name='x3', shape=[None, 4], dtype="float32")
y = np.random.random((2, 4)).astype("float32")
self.assertRaises(TypeError, fluid.layers.sequence_expand_as, x3, y)
if __name__ == '__main__':
unittest.main()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册