Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
29e540af
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看板
未验证
提交
29e540af
编写于
12月 21, 2021
作者:
H
huangxu96
提交者:
GitHub
12月 21, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add python interface of sub_graph (#36120) (#38235)
Add python interface of subgraph: 1. all_sub_graphs() 2. get_sub_graph(idx)
上级
d70a06cc
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
128 addition
and
4 deletion
+128
-4
paddle/fluid/pybind/ir.cc
paddle/fluid/pybind/ir.cc
+9
-1
python/paddle/fluid/framework.py
python/paddle/fluid/framework.py
+23
-3
python/paddle/fluid/tests/unittests/ir/test_ir_subgraph_python_interface.py
...d/tests/unittests/ir/test_ir_subgraph_python_interface.py
+96
-0
未找到文件。
paddle/fluid/pybind/ir.cc
浏览文件 @
29e540af
...
...
@@ -125,7 +125,15 @@ void BindGraph(py::module *m) {
return_value_policy
::
reference
)
.
def
(
"resolve_hazard"
,
&
Graph
::
ResolveHazard
)
.
def
(
"origin_program_desc"
,
&
Graph
::
OriginProgram
,
return_value_policy
::
reference
);
return_value_policy
::
reference
)
.
def
(
"sub_graph_size"
,
&
Graph
::
SubGraphsSize
)
.
def
(
"get_sub_graph"
,
[](
Graph
&
self
,
int
i
)
{
/* Here we use a lambda function as an empty deleter to avoid the double
free of smart pointer.
Otherwise, this shared pointer will be free both in python and
cpp scope, which will lead a core dumped. */
return
std
::
shared_ptr
<
Graph
>
(
self
.
GetSubGraph
(
i
),
[](
Graph
*
)
{});
});
}
void
BindNode
(
py
::
module
*
m
)
{
...
...
python/paddle/fluid/framework.py
浏览文件 @
29e540af
...
...
@@ -3960,6 +3960,23 @@ class IrGraph(object):
"""
return
{
IrOpNode
(
node
)
for
node
in
self
.
graph
.
nodes
()
if
node
.
is_op
()}
def
all_sub_graphs
(
self
,
for_test
=
False
):
"""
Return all sub_graphs included in the main graph as a set.
"""
return
[
IrGraph
(
self
.
graph
.
get_sub_graph
(
i
),
for_test
=
for_test
)
for
i
in
range
(
self
.
graph
.
sub_graph_size
())
]
def
get_sub_graph
(
self
,
i
,
for_test
=
False
):
"""
Return i-th sub_graph in the main graph.
"""
return
IrGraph
(
self
.
graph
.
get_sub_graph
(
i
),
for_test
=
for_test
)
def
create_persistable_node
(
self
,
name
,
var_type
,
shape
,
var_dtype
):
"""
Create a persistable variable node in the graph. In IrGraph,
...
...
@@ -4106,8 +4123,10 @@ class IrGraph(object):
node_in(IrNode): the input node.
node_out(IrNode): the output node.
"""
assert
node_in
.
node
in
self
.
graph
.
nodes
()
and
node_out
.
node
in
self
.
graph
.
nodes
(),
\
'The two arguments(node_in&node_out) must be in the graph nodes.'
assert
node_in
.
node
in
self
.
graph
.
nodes
(),
(
'node_in(%s) must be in the graph nodes.'
%
node_in
.
node
.
name
())
assert
node_out
.
node
in
self
.
graph
.
nodes
(),
(
'node_out(%s) must be in the graph nodes.'
%
node_out
.
node
.
name
())
node_in
.
append_output
(
node_out
)
node_out
.
append_input
(
node_in
)
...
...
@@ -4269,7 +4288,8 @@ class IrGraph(object):
for
n
in
nodes
:
if
n
.
name
()
==
node_name
:
target_node
=
n
assert
target_node
is
not
None
,
"Cannot find the target node in the giving set."
assert
target_node
is
not
None
,
(
"Cannot find the target node (%s)in the giving set."
%
node_name
)
return
target_node
def
_update_desc_attr
(
self
,
desc
,
name
,
val
):
...
...
python/paddle/fluid/tests/unittests/ir/test_ir_subgraph_python_interface.py
0 → 100644
浏览文件 @
29e540af
# Copyright (c) 2021 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.
import
unittest
import
paddle
import
paddle.fluid
as
fluid
import
six
from
paddle.fluid.framework
import
IrGraph
from
paddle.fluid.framework
import
IrNode
from
paddle.fluid.tests.unittests.op_test
import
OpTestTool
from
paddle.fluid
import
core
import
paddle.fluid.layers
as
layers
from
paddle.fluid.framework
import
Program
,
program_guard
,
default_startup_program
from
paddle.fluid.contrib.slim.quantization
import
QuantizationTransformPass
paddle
.
enable_static
()
class
TestQuantizationSubGraph
(
unittest
.
TestCase
):
def
build_graph_with_sub_graph
(
self
):
def
linear_fc
(
num
):
data
=
fluid
.
layers
.
data
(
name
=
'image'
,
shape
=
[
1
,
32
,
32
],
dtype
=
'float32'
)
label
=
fluid
.
layers
.
data
(
name
=
'label'
,
shape
=
[
1
],
dtype
=
'int64'
)
hidden
=
data
for
_
in
six
.
moves
.
xrange
(
num
):
hidden
=
fluid
.
layers
.
fc
(
hidden
,
size
=
128
,
act
=
'relu'
)
loss
=
fluid
.
layers
.
cross_entropy
(
input
=
hidden
,
label
=
label
)
loss
=
fluid
.
layers
.
mean
(
loss
)
return
loss
main_program
=
Program
()
startup_program
=
Program
()
def
true_func
():
return
linear_fc
(
3
)
def
false_func
():
return
linear_fc
(
5
)
with
program_guard
(
main_program
,
startup_program
):
x
=
layers
.
fill_constant
(
shape
=
[
1
],
dtype
=
'float32'
,
value
=
0.1
)
y
=
layers
.
fill_constant
(
shape
=
[
1
],
dtype
=
'float32'
,
value
=
0.23
)
pred
=
layers
.
less_than
(
y
,
x
)
out
=
layers
.
cond
(
pred
,
true_func
,
false_func
)
core_graph
=
core
.
Graph
(
main_program
.
desc
)
# We should create graph for test, otherwise it will throw a
# error that it cannot find the node of "STEP_COUNTER"
graph
=
IrGraph
(
core_graph
,
for_test
=
True
)
sub_graph
=
graph
.
get_sub_graph
(
0
)
all_sub_graphs
=
graph
.
all_sub_graphs
(
for_test
=
True
)
# same reason for subgraph
# Should return graph and sub_graphs at the same time. If only return sub_graph, the graph will
# be destructed and the sub_graphs will be empty.
return
graph
,
all_sub_graphs
def
test_quant_sub_graphs
(
self
,
use_cuda
=
False
):
graph
,
sub_graphs
=
self
.
build_graph_with_sub_graph
()
place
=
fluid
.
CUDAPlace
(
0
)
if
use_cuda
else
fluid
.
CPUPlace
()
transform_pass
=
QuantizationTransformPass
(
scope
=
fluid
.
global_scope
(),
place
=
place
,
activation_quantize_type
=
'abs_max'
,
weight_quantize_type
=
'range_abs_max'
)
Find_inserted_quant_op
=
False
for
sub_graph
in
sub_graphs
:
transform_pass
.
apply
(
sub_graph
)
for
op
in
sub_graph
.
all_op_nodes
():
if
'quantize'
in
op
.
name
():
Find_inserted_quant_op
=
True
self
.
assertTrue
(
Find_inserted_quant_op
)
def
test_quant_sub_graphs_cpu
(
self
):
self
.
test_quant_sub_graphs
(
use_cuda
=
False
)
@
OpTestTool
.
skip_if
(
not
paddle
.
is_compiled_with_cuda
(),
"Not GPU version paddle"
)
def
test_quant_sub_graphs_gpu
(
self
):
self
.
test_quant_sub_graphs
(
use_cuda
=
True
)
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录