未验证 提交 2814d7f6 编写于 作者: H Hui Zhang 提交者: GitHub

Construct exec and ctx only once in cond op to speed up (#47092)

* cond infer apply exec seprate

* fix bugs

* fix as comment
上级 e435d695
...@@ -82,25 +82,29 @@ class ConditionalBlockInferOp : public ConditionalOp { ...@@ -82,25 +82,29 @@ class ConditionalBlockInferOp : public ConditionalOp {
VLOG(3) << "Conditional block.idx = " << block->ID() VLOG(3) << "Conditional block.idx = " << block->ID()
<< ", scope = " << &cur_scope; << ", scope = " << &cur_scope;
if (!exec || !platform::is_same_place(exec->GetPlace(), dev_place)) { if (!exec_ || !platform::is_same_place(exec_->GetPlace(), dev_place)) {
auto &pdesc = *block->Program(); auto &pdesc = *block->Program();
exec.reset(new framework::Executor(dev_place)); exec_.reset(new framework::Executor(dev_place));
if (FLAGS_use_mkldnn) exec->EnableMKLDNN(pdesc); #ifdef PADDLE_WITH_MKLDNN
ctx = exec->Prepare( if (FLAGS_use_mkldnn) exec_->EnableMKLDNN(pdesc);
#endif
ctx_ = exec_->Prepare(
pdesc, block->ID(), std::vector<std::string>(), false); pdesc, block->ID(), std::vector<std::string>(), false);
#ifdef PADDLE_WITH_MKLDNN #ifdef PADDLE_WITH_MKLDNN
platform::AttachPointerHashToMKLDNNKey(exec.get(), dev_place); if (FLAGS_use_mkldnn) {
platform::RegisterModelLayout(ctx->ops_, dev_place); platform::AttachPointerHashToMKLDNNKey(exec_.get(), dev_place);
platform::RegisterModelLayout(ctx_->ops_, dev_place);
}
#endif #endif
} }
exec->RunPreparedContext(ctx.get(), &cur_scope, false, true, false); exec_->RunPreparedContext(ctx_.get(), &cur_scope, false, true, false);
scope.DeleteScope(scopes->front()); scope.DeleteScope(scopes->front());
} }
} }
private: private:
mutable std::shared_ptr<framework::Executor> exec{nullptr}; mutable std::shared_ptr<framework::Executor> exec_{nullptr};
mutable std::unique_ptr<framework::ExecutorPrepareContext> ctx{nullptr}; mutable std::unique_ptr<framework::ExecutorPrepareContext> ctx_{nullptr};
}; };
} // namespace operators } // namespace operators
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册