Add IndicateVarDataType interface to block tensor is not initialized problem in OP GetExceptedKernelType !20044
Created by: chenwhql
In some cases, paddle will throw errors like holder_ should not be null. Tensor not initialized yet when Tensor::type() is called.
. This error is caused by the direct use of the tensor in the Variable
without initialization, but this error message is not user friendly.
By analyzing issues, I found that GetExceptedKernelType
is an important source of this problem. Our developers get the tensor type of input variable but don't check if the tensor was initialized. So I think we can add a funciton to deal with this problem.
If Op doesn't overwrite the GetExceptedKernelType
function, use the OperatorWithKernel::GetExpectedKernelType
function, it wil check if tensor was initialized, so I add the IndicateVarDataType
which is just did what we should have done.
Then the interface GetDataTypeOfVar
can be removed, and we should add a rule that developers must use this IndicateVarDataType
interface in Ops' GetExceptedKernelType
.
The error cases:
https://github.com/PaddlePaddle/Paddle/issues/19522
https://github.com/PaddlePaddle/Paddle/issues/18432
This PR's results:
- original error message:
holder_ should not be null
Tensor not initialized yet when Tensor::type() is called. at [/paddle/paddle/fluid/framework/tensor.h:139]
- new error message:
The Tensor in the save Op's Input Variable X(mean_0.tmp_0) is not initialized. at [/work/paddle/paddle/fluid/framework/operator.cc:1162]
[operator < save > error]
Use Cases
-
GetDataTypeOfVar(ctx.InputVar("X")) -> IndicateVarDataType(ctx, "X")
-
ctx.Input<Tensor>("X")->type() -> IndicateVarDataType(ctx, "X")
Test Case
Code
import paddle.fluid as fluid
place = fluid.CPUPlace()
exe = fluid.Executor(place)
x = fluid.layers.data(name='X', shape=[13], dtype='float32')
y = fluid.layers.data(name='Y', shape=[1], dtype='float32')
y_ = fluid.layers.fc(input=x, size=1, act=None)
loss = fluid.layers.square_error_cost(input=y_, label=y)
avg_loss = fluid.layers.mean(loss)
avg_loss.persistable = True
fluid.optimizer.SGD(learning_rate=0.01).minimize(avg_loss)
exe.run(fluid.default_startup_program())
fluid.io.save_inference_model(dirname="./save_op_test",
feeded_var_names=['X', 'Y'],
target_vars=[avg_loss],
executor=exe)
Result
--------------------------------------------
C++ Call Stacks (More useful to developers):
--------------------------------------------
0 std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > paddle::platform::GetTraceBackString<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, char const*, int)
1 paddle::platform::EnforceNotMet::EnforceNotMet(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, char const*, int)
2 paddle::framework::OperatorWithKernel::ParseInputDataType(paddle::framework::ExecutionContext const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, paddle::framework::proto::VarType_Type*) const
3 paddle::framework::OperatorWithKernel::IndicateVarDataType(paddle::framework::ExecutionContext const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) const
4 paddle::operators::SaveOp::GetExpectedKernelType(paddle::framework::ExecutionContext const&) const
5 paddle::framework::OperatorWithKernel::ChooseKernel(paddle::framework::RuntimeContext const&, paddle::framework::Scope const&, boost::variant<paddle::platform::CUDAPlace, paddle::platform::CPUPlace, paddle::platform::CUDAPinnedPlace, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_> const&) const
6 paddle::framework::OperatorWithKernel::RunImpl(paddle::framework::Scope const&, boost::variant<paddle::platform::CUDAPlace, paddle::platform::CPUPlace, paddle::platform::CUDAPinnedPlace, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_> const&, paddle::framework::RuntimeContext*) const
7 paddle::framework::OperatorWithKernel::RunImpl(paddle::framework::Scope const&, boost::variant<paddle::platform::CUDAPlace, paddle::platform::CPUPlace, paddle::platform::CUDAPinnedPlace, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_> const&) const
8 paddle::framework::OperatorBase::Run(paddle::framework::Scope const&, boost::variant<paddle::platform::CUDAPlace, paddle::platform::CPUPlace, paddle::platform::CUDAPinnedPlace, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_> const&)
9 paddle::framework::Executor::RunPreparedContext(paddle::framework::ExecutorPrepareContext*, paddle::framework::Scope*, bool, bool, bool)
10 paddle::framework::Executor::Run(paddle::framework::ProgramDesc const&, paddle::framework::Scope*, int, bool, bool, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&, bool)
------------------------------------------
Python Call Stacks (More useful to users):
------------------------------------------
File "/usr/local/lib/python3.5/dist-packages/paddle/fluid/framework.py", line 2423, in append_op
attrs=kwargs.get("attrs", None))
File "/usr/local/lib/python3.5/dist-packages/paddle/fluid/io.py", line 241, in save_vars
attrs={'file_path': save_file_path})
File "/usr/local/lib/python3.5/dist-packages/paddle/fluid/io.py", line 216, in save_vars
filename=filename)
File "/usr/local/lib/python3.5/dist-packages/paddle/fluid/io.py", line 571, in save_persistables
filename=filename)
File "/usr/local/lib/python3.5/dist-packages/paddle/fluid/io.py", line 1197, in save_inference_model
save_persistables(executor, save_dirname, main_program, params_filename)
File "save_op.py", line 22, in <module>
executor=exe)
----------------------
Error Message Summary:
----------------------
PaddleCheckError: Expected t->IsInitialized() == true, but received t->IsInitialized():0 != true:1.
The Tensor in the save Op's Input Variable X(mean_0.tmp_0) is not initialized. at [/work/paddle/paddle/fluid/framework/operator.cc:1162]
[operator < save > error]