未验证 提交 e4791d88 编写于 作者: X xiongkun 提交者: GitHub

Fix test rnn memory helper op (#37474)

* clear LoDTensorArray

* fix  bugs

* fix

* fix gpu
上级 81861f69
...@@ -343,7 +343,6 @@ void InterpreterCore::RunInstruction(const Instruction& instr_node) { ...@@ -343,7 +343,6 @@ void InterpreterCore::RunInstruction(const Instruction& instr_node) {
Scope* local_scope = create_local_scope_ Scope* local_scope = create_local_scope_
? global_scope_->GetMutableLocalScope() ? global_scope_->GetMutableLocalScope()
: global_scope_->GetMutableScope(); : global_scope_->GetMutableScope();
auto op_with_kernel = dynamic_cast<const framework::OperatorWithKernel*>(op); auto op_with_kernel = dynamic_cast<const framework::OperatorWithKernel*>(op);
{ {
platform::RecordEvent infershape_event("InferShape"); platform::RecordEvent infershape_event("InferShape");
...@@ -354,8 +353,7 @@ void InterpreterCore::RunInstruction(const Instruction& instr_node) { ...@@ -354,8 +353,7 @@ void InterpreterCore::RunInstruction(const Instruction& instr_node) {
if (op_with_kernel != nullptr && if (op_with_kernel != nullptr &&
FLAGS_new_executor_use_inplace) { // TODO(xiongkun03) Does operator FLAGS_new_executor_use_inplace) { // TODO(xiongkun03) Does operator
// base support // base support inplace ?
// inplace ?
for (auto& pair : instr_node.InplaceInfo()) { for (auto& pair : instr_node.InplaceInfo()) {
const auto& in = paddle::framework::details::GetTensorFromVar(pair.first); const auto& in = paddle::framework::details::GetTensorFromVar(pair.first);
auto* out = auto* out =
......
...@@ -79,6 +79,7 @@ void InterpreterCoreGarbageCollector::Add(paddle::framework::Variable* var, ...@@ -79,6 +79,7 @@ void InterpreterCoreGarbageCollector::Add(paddle::framework::Variable* var,
for (auto& t : *tensor_arr) { for (auto& t : *tensor_arr) {
Add(t.MoveMemoryHolder(), event, ctx); Add(t.MoveMemoryHolder(), event, ctx);
} }
tensor_arr->clear();
} else if (var->IsType<std::vector<Scope*>>()) { } else if (var->IsType<std::vector<Scope*>>()) {
// NOTE(@xiongkun03) conditional_op / while_op will create a STEP_SCOPE // NOTE(@xiongkun03) conditional_op / while_op will create a STEP_SCOPE
// refer to executor.cc to see what old garbage collector does. // refer to executor.cc to see what old garbage collector does.
......
...@@ -411,6 +411,7 @@ void build_op_func_list(const platform::Place& place, ...@@ -411,6 +411,7 @@ void build_op_func_list(const platform::Place& place,
for (auto& t : *lod_tensor_arr) { for (auto& t : *lod_tensor_arr) {
garbages->emplace_back(t.MoveMemoryHolder()); garbages->emplace_back(t.MoveMemoryHolder());
} }
lod_tensor_arr->clear();
} else { } else {
PADDLE_THROW(platform::errors::Unimplemented( PADDLE_THROW(platform::errors::Unimplemented(
"Type %s of variable %s is not supported eager deletion.", "Type %s of variable %s is not supported eager deletion.",
......
...@@ -109,7 +109,11 @@ class RNNMemoryHelperGradOp : public framework::OperatorBase { ...@@ -109,7 +109,11 @@ class RNNMemoryHelperGradOp : public framework::OperatorBase {
platform::DeviceContextPool &pool = platform::DeviceContextPool::Instance(); platform::DeviceContextPool &pool = platform::DeviceContextPool::Instance();
auto &dev_ctx = *pool.Get(dev_place); auto &dev_ctx = *pool.Get(dev_place);
if (out_grad_var == nullptr) { // NOTE(xiongkun03): In standalone executor, after each run, the
// var.tensor.holder will be delete instead of variable. So we need exam the
// IsInitialized().
if (out_grad_var == nullptr ||
!out_grad_var->Get<framework::LoDTensor>().IsInitialized()) {
VLOG(5) << "Using fill constant 0 as starting gradient"; VLOG(5) << "Using fill constant 0 as starting gradient";
auto in_var_name = Input("X"); auto in_var_name = Input("X");
auto *in_var = scope.FindVar(in_var_name); auto *in_var = scope.FindVar(in_var_name);
......
...@@ -129,6 +129,7 @@ template <typename DeviceContext, typename T> ...@@ -129,6 +129,7 @@ template <typename DeviceContext, typename T>
class SumKernel : public framework::OpKernel<T> { class SumKernel : public framework::OpKernel<T> {
public: public:
void Compute(const framework::ExecutionContext &context) const override { void Compute(const framework::ExecutionContext &context) const override {
VLOG(10) << "start sum kernel";
auto in_vars = context.MultiInputVar("X"); auto in_vars = context.MultiInputVar("X");
size_t in_num = in_vars.size(); size_t in_num = in_vars.size();
auto out_var = context.OutputVar("Out"); auto out_var = context.OutputVar("Out");
...@@ -138,7 +139,8 @@ class SumKernel : public framework::OpKernel<T> { ...@@ -138,7 +139,8 @@ class SumKernel : public framework::OpKernel<T> {
if (out_var->IsType<framework::LoDTensor>()) { if (out_var->IsType<framework::LoDTensor>()) {
auto *out = out_var->GetMutable<framework::LoDTensor>(); auto *out = out_var->GetMutable<framework::LoDTensor>();
auto *out_ptr = out->mutable_data<T>(context.GetPlace()); auto *out_ptr = out->mutable_data<T>(context.GetPlace());
if (in_num >= 1 && in_vars[0]->IsType<framework::LoDTensor>()) { if (in_num >= 1 && in_vars[0]->IsType<framework::LoDTensor>() &&
in_vars[0]->Get<framework::LoDTensor>().IsInitialized()) {
auto &in_0_tensor = in_vars[0]->Get<framework::LoDTensor>(); auto &in_0_tensor = in_vars[0]->Get<framework::LoDTensor>();
if (in_0_tensor.numel() > 0) { if (in_0_tensor.numel() > 0) {
in_place = (in_0_tensor.data<T>() == out_ptr); in_place = (in_0_tensor.data<T>() == out_ptr);
...@@ -151,7 +153,9 @@ class SumKernel : public framework::OpKernel<T> { ...@@ -151,7 +153,9 @@ class SumKernel : public framework::OpKernel<T> {
int start = in_place ? 1 : 0; int start = in_place ? 1 : 0;
if (!in_place) { if (!in_place) {
if ((in_num >= 2) && in_vars[0]->IsType<framework::LoDTensor>() && if ((in_num >= 2) && in_vars[0]->IsType<framework::LoDTensor>() &&
in_vars[1]->IsType<framework::LoDTensor>()) { in_vars[1]->IsType<framework::LoDTensor>() &&
in_vars[0]->Get<framework::LoDTensor>().IsInitialized() &&
in_vars[1]->Get<framework::LoDTensor>().IsInitialized()) {
auto &in_0 = in_vars[0]->Get<framework::LoDTensor>(); auto &in_0 = in_vars[0]->Get<framework::LoDTensor>();
auto &in_1 = in_vars[1]->Get<framework::LoDTensor>(); auto &in_1 = in_vars[1]->Get<framework::LoDTensor>();
if (in_0.numel() && in_1.numel()) { if (in_0.numel() && in_1.numel()) {
...@@ -162,6 +166,7 @@ class SumKernel : public framework::OpKernel<T> { ...@@ -162,6 +166,7 @@ class SumKernel : public framework::OpKernel<T> {
} }
} }
if (start != 2) { if (start != 2) {
VLOG(10) << "Fill with constant = 0 in sum kernel.";
math::SetConstant<DeviceContext, T> constant_functor; math::SetConstant<DeviceContext, T> constant_functor;
constant_functor(context.template device_context<DeviceContext>(), constant_functor(context.template device_context<DeviceContext>(),
out, static_cast<T>(0)); out, static_cast<T>(0));
...@@ -173,7 +178,7 @@ class SumKernel : public framework::OpKernel<T> { ...@@ -173,7 +178,7 @@ class SumKernel : public framework::OpKernel<T> {
for (size_t i = start; i < in_num; i++) { for (size_t i = start; i < in_num; i++) {
if (in_vars[i]->IsType<framework::LoDTensor>()) { if (in_vars[i]->IsType<framework::LoDTensor>()) {
auto &in_t = in_vars[i]->Get<framework::LoDTensor>(); auto &in_t = in_vars[i]->Get<framework::LoDTensor>();
if (in_t.numel() == 0) { if (!in_t.IsInitialized() || in_t.numel() == 0) {
continue; continue;
} }
auto in = EigenVector<T>::Flatten(in_t); auto in = EigenVector<T>::Flatten(in_t);
...@@ -200,6 +205,7 @@ class SumKernel : public framework::OpKernel<T> { ...@@ -200,6 +205,7 @@ class SumKernel : public framework::OpKernel<T> {
"unsupport type: %s.", "unsupport type: %s.",
framework::ToTypeName(out_var->Type()))); framework::ToTypeName(out_var->Type())));
} }
VLOG(10) << "end sum kernel";
} }
}; };
} // namespace operators } // namespace operators
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册