Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
d94fd972
P
Paddle
项目概览
PaddlePaddle
/
Paddle
1 年多 前同步成功
通知
2302
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
d94fd972
编写于
3月 13, 2019
作者:
L
luotao1
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add runtime_context_cache_pass
test=develop
上级
b561ad1e
变更
11
隐藏空白更改
内联
并排
Showing
11 changed file
with
127 addition
and
22 deletion
+127
-22
paddle/fluid/framework/ir/CMakeLists.txt
paddle/fluid/framework/ir/CMakeLists.txt
+1
-0
paddle/fluid/framework/ir/runtime_context_cache_pass.cc
paddle/fluid/framework/ir/runtime_context_cache_pass.cc
+39
-0
paddle/fluid/framework/ir/runtime_context_cache_pass.h
paddle/fluid/framework/ir/runtime_context_cache_pass.h
+32
-0
paddle/fluid/framework/operator.cc
paddle/fluid/framework/operator.cc
+7
-13
paddle/fluid/framework/operator.h
paddle/fluid/framework/operator.h
+8
-0
paddle/fluid/framework/scope.cc
paddle/fluid/framework/scope.cc
+0
-4
paddle/fluid/framework/scope.h
paddle/fluid/framework/scope.h
+0
-4
paddle/fluid/inference/api/analysis_config.cc
paddle/fluid/inference/api/analysis_config.cc
+8
-0
paddle/fluid/inference/api/paddle_analysis_config.h
paddle/fluid/inference/api/paddle_analysis_config.h
+26
-0
paddle/fluid/inference/tests/api/config_printer.h
paddle/fluid/inference/tests/api/config_printer.h
+2
-1
paddle/fluid/pybind/inference_api.cc
paddle/fluid/pybind/inference_api.cc
+4
-0
未找到文件。
paddle/fluid/framework/ir/CMakeLists.txt
浏览文件 @
d94fd972
...
...
@@ -66,6 +66,7 @@ pass_library(conv_elementwise_add_fuse_pass inference)
pass_library
(
conv_affine_channel_fuse_pass inference
)
pass_library
(
transpose_flatten_concat_fuse_pass inference
)
pass_library
(
identity_scale_op_clean_pass base
)
pass_library
(
runtime_context_cache_pass base
)
# There may be many transpose-flatten structures in a model, and the output of
# these structures will be used as inputs to the concat Op. This pattern will
...
...
paddle/fluid/framework/ir/runtime_context_cache_pass.cc
0 → 100644
浏览文件 @
d94fd972
/* Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#include "paddle/fluid/framework/ir/runtime_context_cache_pass.h"
#include <memory>
#include "paddle/fluid/framework/operator.h"
namespace
paddle
{
namespace
framework
{
namespace
ir
{
std
::
unique_ptr
<
ir
::
Graph
>
RuntimeContextCachePass
::
ApplyImpl
(
std
::
unique_ptr
<
ir
::
Graph
>
graph
)
const
{
VLOG
(
3
)
<<
"Applies Runtime Context Cache strategy."
;
for
(
const
Node
*
n
:
graph
->
Nodes
())
{
if
(
n
->
IsOp
())
{
n
->
Op
()
->
SetAttr
(
kEnableRuntimeContext
,
true
);
}
}
return
graph
;
}
}
// namespace ir
}
// namespace framework
}
// namespace paddle
REGISTER_PASS
(
runtime_context_cache_pass
,
paddle
::
framework
::
ir
::
RuntimeContextCachePass
);
paddle/fluid/framework/ir/runtime_context_cache_pass.h
0 → 100644
浏览文件 @
d94fd972
/* Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#pragma once
#include <memory>
#include "paddle/fluid/framework/ir/pass.h"
namespace
paddle
{
namespace
framework
{
namespace
ir
{
class
RuntimeContextCachePass
:
public
Pass
{
protected:
std
::
unique_ptr
<
ir
::
Graph
>
ApplyImpl
(
std
::
unique_ptr
<
ir
::
Graph
>
graph
)
const
override
;
};
}
// namespace ir
}
// namespace framework
}
// namespace paddle
paddle/fluid/framework/operator.cc
浏览文件 @
d94fd972
...
...
@@ -20,7 +20,6 @@ limitations under the License. */
#include <string>
#include <vector>
#include "paddle/fluid/framework/data_transform.h"
#include "paddle/fluid/framework/details/op_handle_base.h"
#include "paddle/fluid/framework/executor.h"
#include "paddle/fluid/framework/lod_tensor.h"
#include "paddle/fluid/framework/op_proto_maker.h"
...
...
@@ -877,19 +876,14 @@ std::vector<KernelConfig>* OperatorWithKernel::GetKernelConfig(
void
OperatorWithKernel
::
RunImpl
(
const
Scope
&
scope
,
const
platform
::
Place
&
place
)
const
{
const
Scope
*
cur_scope
=
&
scope
;
// RuntimeContext is used to relate input/output names of Operator with
// the corresponding variables in Scope.
// In a same Scope, since the input/output names of Operator do not change
// in the execution, RuntimeContext could be created only at the first
// iteration of the execution to save the elapsed time.
// Note that the Scope should not be the local scope, since local scope
// would be cleaned regularly.
if
(
scope
.
FindVar
(
details
::
kLocalExecScopeName
))
{
if
(
!
HasAttr
(
kEnableRuntimeContext
))
{
runtime_ctx_
.
reset
(
new
RuntimeContext
(
Inputs
(),
Outputs
(),
scope
));
}
else
if
(
!
runtime_ctx_
||
pre_scope_
!=
cur_scope
)
{
runtime_ctx_
.
reset
(
new
RuntimeContext
(
Inputs
(),
Outputs
(),
scope
));
pre_scope_
=
cur_scope
;
}
else
{
const
Scope
*
cur_scope
=
&
scope
;
if
(
!
runtime_ctx_
||
pre_scope_
!=
cur_scope
)
{
runtime_ctx_
.
reset
(
new
RuntimeContext
(
Inputs
(),
Outputs
(),
scope
));
pre_scope_
=
cur_scope
;
}
}
platform
::
DeviceContextPool
&
pool
=
platform
::
DeviceContextPool
::
Instance
();
auto
*
dev_ctx
=
pool
.
Get
(
place
);
...
...
paddle/fluid/framework/operator.h
浏览文件 @
d94fd972
...
...
@@ -62,6 +62,14 @@ constexpr char kZeroVarSuffix[] = "@ZERO";
/// Variables with this suffix are the new Gradient.
constexpr
char
kNewGradSuffix
[]
=
"@NEWGRAD@"
;
/// RuntimeContext is used to relate input/output names of Operator with
/// the corresponding variables in Scope.
/// If an Op has attribute kEnableRuntimeContext, it means that in a same Scope,
/// since the input/output names of this Op do not change in the execution,
/// RuntimeContext could be created only at the first iteration of this Op's
/// execution to save the elapsed time.
constexpr
char
kEnableRuntimeContext
[]
=
"@ENABLE_RUNTIME_CONTEXT@"
;
// define some kernel priority
/* Define multiple kernel type fallback order*/
extern
std
::
vector
<
std
::
tuple
<
platform
::
Place
,
LibraryType
>>
kKernelPriority
;
...
...
paddle/fluid/framework/scope.cc
浏览文件 @
d94fd972
...
...
@@ -107,10 +107,6 @@ const Scope* Scope::FindScope(const Variable* var) const {
return
FindScopeInternal
(
var
);
}
bool
Scope
::
HasLocalVar
(
const
std
::
string
&
name
)
const
{
return
vars_
.
find
(
name
)
!=
vars_
.
end
();
}
void
Scope
::
DropKids
()
{
SCOPE_KIDS_WRITER_LOCK
for
(
Scope
*
s
:
kids_
)
delete
s
;
...
...
paddle/fluid/framework/scope.h
浏览文件 @
d94fd972
...
...
@@ -75,10 +75,6 @@ class Scope {
/// Caller doesn't own the returned Variable.
Variable
*
FindLocalVar
(
const
std
::
string
&
name
)
const
;
/// Find whether a variable in the current scope.
/// Return false if cannot find.
bool
HasLocalVar
(
const
std
::
string
&
name
)
const
;
const
Scope
*
parent
()
const
{
return
parent_
;
}
/// Find the scope or an ancestor scope that contains the given variable.
...
...
paddle/fluid/inference/api/analysis_config.cc
浏览文件 @
d94fd972
...
...
@@ -118,6 +118,9 @@ AnalysisConfig::AnalysisConfig(const AnalysisConfig &other) {
CP_MEMBER
(
serialized_info_cache_
);
// framework related.
CP_MEMBER
(
enable_runtime_context_cache_
);
if
(
use_gpu_
)
{
pass_builder_
.
reset
(
new
GpuPassStrategy
(
*
static_cast
<
GpuPassStrategy
*>
(
other
.
pass_builder
())));
...
...
@@ -225,6 +228,10 @@ void AnalysisConfig::Update() {
if
(
ir_debug_
)
{
pass_builder
()
->
TurnOnDebug
();
}
if
(
enable_runtime_context_cache_
)
{
pass_builder
()
->
AppendPass
(
"runtime_context_cache_pass"
);
}
}
std
::
string
AnalysisConfig
::
SerializeInfoCache
()
{
...
...
@@ -258,6 +265,7 @@ std::string AnalysisConfig::SerializeInfoCache() {
ss
<<
specify_input_name_
;
ss
<<
cpu_math_library_num_threads_
;
ss
<<
enable_runtime_context_cache_
;
return
ss
.
str
();
}
...
...
paddle/fluid/inference/api/paddle_analysis_config.h
浏览文件 @
d94fd972
...
...
@@ -194,6 +194,23 @@ struct AnalysisConfig {
/** Tell whether the memory optimization is activated. */
bool
enable_memory_optim
()
const
;
// framework related
/** \brief Control whether to perform runtime context cache optimization.
*
* If turned off, in Op's every execution, RuntimeContext would be called to
* relate input/output names of this Op with the corresponding variables in
* Scope.
*/
void
SwitchRuntimeContextCache
(
int
x
=
true
)
{
enable_runtime_context_cache_
=
x
;
}
/** A boolean state tell whether the runtime context cache optimization is
* actived.
*/
bool
runtime_context_cache_enabled
()
const
{
return
enable_runtime_context_cache_
;
}
friend
class
::
paddle
::
AnalysisPredictor
;
/** NOTE just for developer, not an official API, easily to be broken.
...
...
@@ -254,6 +271,15 @@ struct AnalysisConfig {
int
cpu_math_library_num_threads_
{
1
};
// framework related
// RuntimeContext is used to relate input/output names of Operator with
// the corresponding variables in Scope.
// If enable_runtime_context_cache_ is true, it means that in a same Scope,
// since the input/output names of this Op do not change in the execution,
// RuntimeContext could be created only at the first iteration of this Op's
// execution to save the elapsed time.
bool
enable_runtime_context_cache_
{
true
};
// A runtime cache, shouldn't be transferred to others.
std
::
string
serialized_info_cache_
;
...
...
paddle/fluid/inference/tests/api/config_printer.h
浏览文件 @
d94fd972
...
...
@@ -72,7 +72,8 @@ std::ostream &operator<<(std::ostream &os, const AnalysisConfig &config) {
}
os
<<
GenSpaces
(
num_spaces
)
<<
"enable_ir_optim: "
<<
config
.
ir_optim
()
<<
"
\n
"
;
os
<<
GenSpaces
(
num_spaces
)
<<
"enable_ir_optim: "
<<
config
.
ir_optim
()
os
<<
GenSpaces
(
num_spaces
)
<<
"use_runtime_context_cache: "
<<
config
.
runtime_context_cache_enabled
()
<<
"
\n
"
;
os
<<
GenSpaces
(
num_spaces
)
<<
"use_feed_fetch_ops: "
<<
config
.
use_feed_fetch_ops_enabled
()
<<
"
\n
"
;
...
...
paddle/fluid/pybind/inference_api.cc
浏览文件 @
d94fd972
...
...
@@ -242,6 +242,10 @@ void BindAnalysisConfig(py::module *m) {
.
def
(
"set_mkldnn_op"
,
&
AnalysisConfig
::
SetMKLDNNOp
)
.
def
(
"set_model_buffer"
,
&
AnalysisConfig
::
SetModelBuffer
)
.
def
(
"model_from_memory"
,
&
AnalysisConfig
::
model_from_memory
)
.
def
(
"runtime_context_cache_enabled"
,
&
AnalysisConfig
::
runtime_context_cache_enabled
)
.
def
(
"switch_runtime_context_cache"
,
&
AnalysisConfig
::
SwitchRuntimeContextCache
,
py
::
arg
(
"x"
)
=
true
)
.
def
(
"pass_builder"
,
&
AnalysisConfig
::
pass_builder
,
py
::
return_value_policy
::
reference
);
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录