-
Notifications
You must be signed in to change notification settings - Fork 5.9k
add python interface of sub_graph #36120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
zhhsplendid
merged 6 commits into
PaddlePaddle:develop
from
huangxu96:subgraph_python_interface
Oct 8, 2021
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4167ec4
add python interface of sub_graph
huangxu96 7c029dd
for Debug
huangxu96 088225d
update Debug info
huangxu96 e65daeb
fix a bug in subgraph irGraph, add the arguement of for_test in subgraph
huangxu96 69b73e6
remove some Debug info
huangxu96 b2a088e
add CPU test case for Windows CI
huangxu96 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
python/paddle/fluid/tests/unittests/ir/test_ir_subgraph_python_interface.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| # 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 import core | ||
| 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 cond(i, ten): | ||
| return i < ten | ||
|
|
||
| def body(i, ten): | ||
| i = i + 1 | ||
| return [i, ten] | ||
|
|
||
| main_program = paddle.static.default_main_program() | ||
| startup_program = paddle.static.default_startup_program() | ||
| with paddle.static.program_guard(main_program, startup_program): | ||
| i = paddle.full( | ||
| shape=[1], fill_value=0, dtype='int64') # loop counter | ||
| ten = paddle.full( | ||
| shape=[1], fill_value=10, dtype='int64') # loop length | ||
| i, ten = paddle.static.nn.while_loop(cond, body, [i, ten]) | ||
| # loss = linear_fc(3) | ||
| # opt = fluid.optimizer.Adam(learning_rate=0.001) | ||
| # opt.minimize(loss) | ||
| core_graph = core.Graph(main_program.desc) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 我觉得 |
||
| print("block_size = ", len(main_program.blocks)) | ||
| print(core_graph) | ||
| graph = IrGraph(core_graph) | ||
| sub_graph = graph.get_sub_graph(0) | ||
| all_sub_graphs = graph.all_sub_graphs() | ||
| print(graph) | ||
| print("all_sub_graphs[0]", all_sub_graphs[0]) | ||
| transform_pass = QuantizationTransformPass( | ||
| scope=fluid.global_scope(), | ||
| place=fluid.CUDAPlace(0), | ||
| activation_quantize_type='abs_max', | ||
| weight_quantize_type='range_abs_max') | ||
| print("sub_graph_size:", len(all_sub_graphs)) | ||
| for sub_graph in all_sub_graphs: | ||
| print("sub_graph:", sub_graph) # subgraph_ | ||
| nodes = sub_graph.all_op_nodes() | ||
| print("all nodes inside build graph func:", | ||
| nodes) #这里 all nodes 不为空,所以可以作量化Pass | ||
| transform_pass.apply(sub_graph) | ||
| print("Done quant pass applied ") | ||
| return all_sub_graphs | ||
|
||
|
|
||
| def test_quant_sub_graphs(self): | ||
| sub_graphs = self.build_graph_with_sub_graph() | ||
| transform_pass = QuantizationTransformPass( | ||
| scope=fluid.global_scope(), | ||
| place=fluid.CUDAPlace(0), | ||
| activation_quantize_type='abs_max', | ||
| weight_quantize_type='range_abs_max') | ||
| print("sub_graph_size:", len(sub_graphs)) | ||
| for sub_graph in sub_graphs: | ||
| print("sub_graph:", | ||
| sub_graph) # 这里object地址都和返回前一样,但是拿到的all_op_nodes为空set() | ||
| nodes = sub_graph.all_op_nodes() | ||
| print("all nodes outside build graph func:", nodes) | ||
| transform_pass.apply(sub_graph) #因为拿不到 all_op_nodes 所以pass会失败 | ||
| #program = sub_graph.to_program() | ||
| # print("sub_program",program) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[](Graph *) {}是为了防止外部析构?不能直接返回裸指针么?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个是防止double free,目的是让该指针在python析构,不然会在C++和python都析构一次。后面加了注释对这个的解释。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does
return_value_policy::referencework for that?