-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
[ Dy2Static ] Add closure analysis for control flow and add some unittest #43713
Merged
2742195759
merged 5 commits into
PaddlePaddle:develop
from
2742195759:dy2static-static-analysis
Jun 24, 2022
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b258c6b
add closure analysis for control flow and add some unittest
2742195759 920d882
finetune the design of FunctionScopeVisitor
2742195759 7701053
fix
2742195759 5f84ead
fix python check
2742195759 c828f7f
fix code by code review
2742195759 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 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
134 changes: 134 additions & 0 deletions
134
python/paddle/fluid/tests/unittests/dygraph_to_static/test_closure_analysis.py
This file contains 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,134 @@ | ||
# Copyright (c) 2020 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. | ||
|
||
from __future__ import print_function | ||
|
||
import unittest | ||
|
||
import paddle | ||
from paddle.fluid.dygraph.dygraph_to_static.loop_transformer import FunctionNameLivenessAnalysis | ||
from paddle.utils import gast | ||
import inspect | ||
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. import语句注意要清理下,很多import语句是没有在这个单测里用到的 |
||
|
||
|
||
class JudgeVisitor(gast.NodeVisitor): | ||
|
||
def __init__(self, ans): | ||
self.ans = ans | ||
|
||
def visit_FunctionDef(self, node): | ||
scope = node.pd_scope | ||
expected = self.ans.get(node.name, set()) | ||
assert scope.created_vars() == expected, "Not Equals." | ||
self.generic_visit(node) | ||
|
||
|
||
def test_normal_0(x): | ||
|
||
def func(): | ||
if True: | ||
i = 1 | ||
|
||
func() | ||
return i | ||
|
||
|
||
def test_normal_argument(x): | ||
x = 1 | ||
|
||
def func(): | ||
if True: | ||
print(x) | ||
i = 1 | ||
|
||
func() | ||
return x | ||
|
||
|
||
def test_global(x): | ||
global t | ||
t = 10 | ||
|
||
def func(): | ||
if True: | ||
print(x) | ||
i = 1 | ||
|
||
func() | ||
return x | ||
|
||
|
||
def test_nonlocal(x, *args, **kargs): | ||
i = 10 | ||
|
||
def func(*args, **kargs): | ||
nonlocal i | ||
k = 10 | ||
if True: | ||
print(x) | ||
i = 1 | ||
|
||
func(*args, **kargs) | ||
return x | ||
|
||
|
||
class TestClosureAnalysis(unittest.TestCase): | ||
|
||
def setUp(self): | ||
self.init_dygraph_func() | ||
|
||
def init_dygraph_func(self): | ||
self.all_dygraph_funcs = [ | ||
test_nonlocal, test_global, test_normal_0, test_normal_argument | ||
] | ||
self.answer = [ | ||
{ | ||
'func': set('k'), | ||
'test_nonlocal': set('i') | ||
}, | ||
{ | ||
'func': set({'i'}), | ||
}, | ||
{ | ||
'func': set('i'), | ||
}, | ||
{ | ||
'func': set('i'), | ||
}, | ||
] | ||
|
||
def test_main(self): | ||
for ans, func in zip(self.answer, self.all_dygraph_funcs): | ||
test_func = inspect.getsource(func) | ||
gast_root = gast.parse(test_func) | ||
name_visitor = FunctionNameLivenessAnalysis(gast_root) | ||
JudgeVisitor(ans).visit(gast_root) | ||
|
||
|
||
def TestClosureAnalysis_Attribute_func(): | ||
# in this function, only self is a Name, self.current is a Attribute. self is read and self.current.function is store() | ||
i = 0 | ||
self.current.function = 12 | ||
|
||
|
||
class TestClosureAnalysis_Attribute(TestClosureAnalysis): | ||
|
||
def init_dygraph_func(self): | ||
|
||
self.all_dygraph_funcs = [TestClosureAnalysis_Attribute_func] | ||
self.answer = [{"TestClosureAnalysis_Attribute_func": set({'i'})}] | ||
|
||
|
||
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.
下个PR我们可以把类似的公用类、分析函数统一放到一个文件里,这样可以被其他transofmer导入使用