未验证 提交 fb34bdb4 编写于 作者: W wangchaochaohu 提交者: GitHub

API/OP(fill_constant) error message enhancement (#23584)

上级 25ef38bc
...@@ -789,7 +789,6 @@ void ParallelExecutor::BCastParamsToDevices( ...@@ -789,7 +789,6 @@ void ParallelExecutor::BCastParamsToDevices(
FetchResultType ParallelExecutor::Run( FetchResultType ParallelExecutor::Run(
const std::vector<std::string> &fetch_tensors, bool return_merged) { const std::vector<std::string> &fetch_tensors, bool return_merged) {
VLOG(3) << "enter ParallelExecutor Run"; VLOG(3) << "enter ParallelExecutor Run";
platform::RecordEvent parallel_executor_event("ParallelExecutor::Run");
#ifdef WITH_GPERFTOOLS #ifdef WITH_GPERFTOOLS
if (gProfileStarted) { if (gProfileStarted) {
ProfilerFlush(); ProfilerFlush();
......
...@@ -22,8 +22,7 @@ class FillConstantOp : public framework::OperatorWithKernel { ...@@ -22,8 +22,7 @@ class FillConstantOp : public framework::OperatorWithKernel {
using framework::OperatorWithKernel::OperatorWithKernel; using framework::OperatorWithKernel::OperatorWithKernel;
void InferShape(framework::InferShapeContext* ctx) const override { void InferShape(framework::InferShapeContext* ctx) const override {
PADDLE_ENFORCE_EQ(ctx->HasOutput("Out"), true, OP_INOUT_CHECK(ctx->HasOutput("Out"), "Output", "Out", "FillConstant");
"Output(Out) of FillConstantOp should not be null.");
auto& shape = ctx->Attrs().Get<std::vector<int64_t>>("shape"); auto& shape = ctx->Attrs().Get<std::vector<int64_t>>("shape");
......
...@@ -49,11 +49,11 @@ inline framework::DDim GetShape(const framework::ExecutionContext &ctx) { ...@@ -49,11 +49,11 @@ inline framework::DDim GetShape(const framework::ExecutionContext &ctx) {
auto tensor = shape_tensor_list[i]; auto tensor = shape_tensor_list[i];
PADDLE_ENFORCE_EQ( PADDLE_ENFORCE_EQ(
tensor->dims(), framework::make_ddim({1}), tensor->dims(), framework::make_ddim({1}),
"ShapeError: If the element type of 'shape' in FillConstantOp is " platform::errors::InvalidArgument(
"Tensor, " "If the element type of 'shape'(tensor_list type) in "
"the element's shape must be [1]. But received the element's shape " "FillConstantOp is Tensor, the shape of this Tensor element must "
"is [%s]", "be [1]. But received the Tensor element's shape is [%s]",
tensor->dims()); tensor->dims()));
if (platform::is_gpu_place(tensor->place())) { if (platform::is_gpu_place(tensor->place())) {
framework::Tensor temp; framework::Tensor temp;
TensorCopySync(*tensor, platform::CPUPlace(), &temp); TensorCopySync(*tensor, platform::CPUPlace(), &temp);
...@@ -124,9 +124,9 @@ class FillConstantKernel : public framework::OpKernel<T> { ...@@ -124,9 +124,9 @@ class FillConstantKernel : public framework::OpKernel<T> {
tensor = out_var->GetMutable<framework::SelectedRows>()->mutable_value(); tensor = out_var->GetMutable<framework::SelectedRows>()->mutable_value();
tensor->Resize(shape); tensor->Resize(shape);
} else { } else {
PADDLE_THROW( PADDLE_THROW(platform::errors::Unimplemented(
"fill constant op's output only" "In fill constant Op, the output only supports SelectedRows and "
"supports SelectedRows and LoDTensor"); "LoDTensor."));
} }
platform::DeviceContextPool &pool = platform::DeviceContextPool::Instance(); platform::DeviceContextPool &pool = platform::DeviceContextPool::Instance();
......
...@@ -628,11 +628,18 @@ def fill_constant(shape, dtype, value, force_cpu=False, out=None): ...@@ -628,11 +628,18 @@ def fill_constant(shape, dtype, value, force_cpu=False, out=None):
out.stop_gradient = True out.stop_gradient = True
return out return out
helper = LayerHelper("fill_constant", **locals()) check_dtype(dtype, 'dtype',
check_dtype(dtype, 'create data type',
['bool', 'float16', 'float32', 'float64', 'int32', 'int64'], ['bool', 'float16', 'float32', 'float64', 'int32', 'int64'],
'fill_constant') 'fill_constant')
check_type(shape, 'shape', (Variable, list, tuple), 'fill_constant') check_type(shape, 'shape', (Variable, list, tuple), 'fill_constant')
if isinstance(shape, Variable):
check_variable_and_dtype(shape, 'shape', ['int32', 'int64'],
'fill_constant')
if out is not None:
check_variable_and_dtype(out, 'out', [convert_dtype(dtype)],
'fill_constant')
helper = LayerHelper("fill_constant", **locals())
inputs = utils._get_shape_tensor_inputs( inputs = utils._get_shape_tensor_inputs(
inputs=inputs, inputs=inputs,
helper=helper, helper=helper,
...@@ -642,12 +649,6 @@ def fill_constant(shape, dtype, value, force_cpu=False, out=None): ...@@ -642,12 +649,6 @@ def fill_constant(shape, dtype, value, force_cpu=False, out=None):
if out is None: if out is None:
out = helper.create_variable_for_type_inference(dtype=dtype) out = helper.create_variable_for_type_inference(dtype=dtype)
else:
check_dtype(
dtype, 'create data type',
convert_dtype(out.dtype), 'fill_constant',
'(The create data type in fill_constant must be the same with out data type.)'
)
attrs['dtype'] = out.dtype attrs['dtype'] = out.dtype
helper.append_op( helper.append_op(
type='fill_constant', type='fill_constant',
......
...@@ -23,6 +23,7 @@ import paddle.fluid.core as core ...@@ -23,6 +23,7 @@ import paddle.fluid.core as core
from paddle.fluid.op import Operator from paddle.fluid.op import Operator
import paddle.fluid as fluid import paddle.fluid as fluid
from paddle.fluid import compiler, Program, program_guard from paddle.fluid import compiler, Program, program_guard
import numpy as np
# Situation 1: Attr(shape) is a list(without tensor) # Situation 1: Attr(shape) is a list(without tensor)
...@@ -263,12 +264,12 @@ class TestFillConstantOp2_ValueTensor(OpTest): ...@@ -263,12 +264,12 @@ class TestFillConstantOp2_ValueTensor(OpTest):
# Test python API # Test python API
class TestFillConstantAPI(unittest.TestCase): class TestFillConstantAPI(unittest.TestCase):
def test_api(self): def test_api(self):
positive_2_int32 = fluid.layers.fill_constant([1], "int32", 2)
positive_2_int32 = fluid.layers.fill_constant([1], "int32", 2)
positive_2_int64 = fluid.layers.fill_constant([1], "int64", 2) positive_2_int64 = fluid.layers.fill_constant([1], "int64", 2)
shape_tensor_int32 = fluid.data( shape_tensor_int32 = fluid.data(
name="shape_tensor_int32", shape=[2], dtype="int32") name="shape_tensor_int32", shape=[2], dtype="int32")
shape_tensor_int64 = fluid.data( shape_tensor_int64 = fluid.data(
name="shape_tensor_int64", shape=[2], dtype="int64") name="shape_tensor_int64", shape=[2], dtype="int64")
...@@ -349,6 +350,15 @@ class TestFillConstantOpError(unittest.TestCase): ...@@ -349,6 +350,15 @@ class TestFillConstantOpError(unittest.TestCase):
dtype='float64', dtype='float64',
out=x2) out=x2)
x3 = np.random.randn(100, 100).astype('int32')
self.assertRaises(
TypeError,
fluid.layers.fill_constant,
shape=[100, 100],
value=5,
dtype='float64',
out=x3)
# The argument shape's type of fill_constant_op must be list, tuple or Variable. # The argument shape's type of fill_constant_op must be list, tuple or Variable.
def test_shape_type(): def test_shape_type():
fluid.layers.fill_constant(shape=1, dtype="float32", value=1) fluid.layers.fill_constant(shape=1, dtype="float32", value=1)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册