Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Crayon鑫
Paddle
提交
2d0f849e
P
Paddle
项目概览
Crayon鑫
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
2d0f849e
编写于
5月 14, 2020
作者:
H
Huihuang Zheng
提交者:
GitHub
5月 14, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[Dy2Stat] Add assert for ProgramTranslator (#24492)
Add assert grammar for ProgramTranslator
上级
53e3c534
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
138 addition
and
2 deletion
+138
-2
python/paddle/fluid/dygraph/dygraph_to_static/assert_transformer.py
...dle/fluid/dygraph/dygraph_to_static/assert_transformer.py
+53
-0
python/paddle/fluid/dygraph/dygraph_to_static/ast_transformer.py
...paddle/fluid/dygraph/dygraph_to_static/ast_transformer.py
+6
-2
python/paddle/fluid/dygraph/dygraph_to_static/static_analysis.py
...paddle/fluid/dygraph/dygraph_to_static/static_analysis.py
+8
-0
python/paddle/fluid/tests/unittests/dygraph_to_static/test_assert.py
...le/fluid/tests/unittests/dygraph_to_static/test_assert.py
+71
-0
未找到文件。
python/paddle/fluid/dygraph/dygraph_to_static/assert_transformer.py
0 → 100644
浏览文件 @
2d0f849e
# 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
gast
from
paddle.fluid.dygraph.dygraph_to_static.static_analysis
import
AstNodeWrapper
from
paddle.fluid.dygraph.dygraph_to_static.static_analysis
import
NodeVarType
from
paddle.fluid.dygraph.dygraph_to_static.static_analysis
import
StaticAnalysisVisitor
class
AssertTransformer
(
gast
.
NodeTransformer
):
"""
A class transforms python assert to fluid.layers.Assert.
"""
def
__init__
(
self
,
wrapper_root
):
assert
isinstance
(
wrapper_root
,
AstNodeWrapper
),
"Input non-AstNodeWrapper node for the initialization of AssertTransformer."
self
.
wrapper_root
=
wrapper_root
self
.
root
=
wrapper_root
.
node
self
.
static_analysis_visitor
=
StaticAnalysisVisitor
(
self
.
root
)
def
transform
(
self
):
self
.
visit
(
self
.
root
)
def
visit_Assert
(
self
,
node
):
if
not
self
.
static_analysis_visitor
.
is_tensor_node
(
node
.
test
):
return
node
cast_node
=
gast
.
Call
(
func
=
gast
.
parse
(
"fluid.layers.cast"
).
body
[
0
].
value
,
args
=
[
node
.
test
,
gast
.
Constant
(
value
=
"bool"
,
kind
=
None
)],
keywords
=
[])
assert_node
=
gast
.
Call
(
func
=
gast
.
parse
(
"fluid.layers.Assert"
).
body
[
0
].
value
,
args
=
[
cast_node
],
keywords
=
[])
return
gast
.
Expr
(
value
=
assert_node
)
python/paddle/fluid/dygraph/dygraph_to_static/ast_transformer.py
浏览文件 @
2d0f849e
...
@@ -22,14 +22,15 @@ import gast
...
@@ -22,14 +22,15 @@ import gast
import
inspect
import
inspect
import
textwrap
import
textwrap
from
paddle.fluid.dygraph.dygraph_to_static.assert_transformer
import
AssertTransformer
from
paddle.fluid.dygraph.dygraph_to_static.call_transformer
import
CallTransformer
from
paddle.fluid.dygraph.dygraph_to_static.basic_api_transformer
import
BasicApiTransformer
from
paddle.fluid.dygraph.dygraph_to_static.basic_api_transformer
import
BasicApiTransformer
from
paddle.fluid.dygraph.dygraph_to_static.break_continue_transformer
import
BreakContinueTransformer
from
paddle.fluid.dygraph.dygraph_to_static.break_continue_transformer
import
BreakContinueTransformer
from
paddle.fluid.dygraph.dygraph_to_static.ifelse_transformer
import
IfElseTransformer
from
paddle.fluid.dygraph.dygraph_to_static.ifelse_transformer
import
IfElseTransformer
from
paddle.fluid.dygraph.dygraph_to_static.list_transformer
import
ListTransformer
from
paddle.fluid.dygraph.dygraph_to_static.list_transformer
import
ListTransformer
from
paddle.fluid.dygraph.dygraph_to_static.loop_transformer
import
LoopTransformer
from
paddle.fluid.dygraph.dygraph_to_static.loop_transformer
import
LoopTransformer
from
paddle.fluid.dygraph.dygraph_to_static.tensor_shape_transformer
import
TensorShapeTransformer
from
paddle.fluid.dygraph.dygraph_to_static.call_transformer
import
CallTransformer
from
paddle.fluid.dygraph.dygraph_to_static.print_transformer
import
PrintTransformer
from
paddle.fluid.dygraph.dygraph_to_static.print_transformer
import
PrintTransformer
from
paddle.fluid.dygraph.dygraph_to_static.tensor_shape_transformer
import
TensorShapeTransformer
from
paddle.fluid.dygraph.dygraph_to_static.static_analysis
import
StaticAnalysisVisitor
from
paddle.fluid.dygraph.dygraph_to_static.static_analysis
import
StaticAnalysisVisitor
from
paddle.fluid.dygraph.dygraph_to_static.utils
import
ast_to_func
from
paddle.fluid.dygraph.dygraph_to_static.utils
import
ast_to_func
...
@@ -80,6 +81,9 @@ class DygraphToStaticAst(gast.NodeTransformer):
...
@@ -80,6 +81,9 @@ class DygraphToStaticAst(gast.NodeTransformer):
# Transform all if/else statement of Dygraph into Static Graph.
# Transform all if/else statement of Dygraph into Static Graph.
IfElseTransformer
(
node_wrapper
).
transform
()
IfElseTransformer
(
node_wrapper
).
transform
()
# Transform python assert statement
AssertTransformer
(
node_wrapper
).
transform
()
# Transform all python print statement
# Transform all python print statement
PrintTransformer
(
node_wrapper
).
transform
()
PrintTransformer
(
node_wrapper
).
transform
()
...
...
python/paddle/fluid/dygraph/dygraph_to_static/static_analysis.py
浏览文件 @
2d0f849e
...
@@ -256,6 +256,14 @@ class StaticAnalysisVisitor(object):
...
@@ -256,6 +256,14 @@ class StaticAnalysisVisitor(object):
def
get_var_env
(
self
):
def
get_var_env
(
self
):
return
self
.
var_env
return
self
.
var_env
def
is_tensor_node
(
self
,
node
):
tensor_types
=
{
NodeVarType
.
TENSOR
,
NodeVarType
.
PADDLE_RETURN_TYPES
}
node_wrapper
=
self
.
node_to_wrapper_map
.
get
(
node
,
None
)
if
node_wrapper
is
None
:
return
False
if
node_wrapper
.
node_var_type
&
tensor_types
:
return
True
def
_get_constant_node_type
(
self
,
node
):
def
_get_constant_node_type
(
self
,
node
):
assert
isinstance
(
node
,
gast
.
Constant
),
\
assert
isinstance
(
node
,
gast
.
Constant
),
\
"Type of input node should be gast.Constant, but received %s"
%
type
(
node
)
"Type of input node should be gast.Constant, but received %s"
%
type
(
node
)
...
...
python/paddle/fluid/tests/unittests/dygraph_to_static/test_assert.py
0 → 100644
浏览文件 @
2d0f849e
# 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
numpy
import
unittest
import
paddle.fluid
as
fluid
from
paddle.fluid.dygraph.dygraph_to_static
import
ProgramTranslator
from
paddle.fluid.dygraph.jit
import
declarative
@
declarative
def
dyfunc_assert_variable
(
x
):
x_v
=
fluid
.
dygraph
.
to_variable
(
x
)
assert
x_v
@
declarative
def
dyfunc_assert_non_variable
(
x
=
True
):
assert
x
class
TestAssertVariable
(
unittest
.
TestCase
):
def
_run
(
self
,
func
,
x
,
with_exception
,
to_static
):
ProgramTranslator
().
enable
(
to_static
)
if
with_exception
:
with
self
.
assertRaises
(
BaseException
):
with
fluid
.
dygraph
.
guard
():
func
(
x
)
else
:
with
fluid
.
dygraph
.
guard
():
func
(
x
)
def
_run_dy_static
(
self
,
func
,
x
,
with_exception
):
self
.
_run
(
func
,
x
,
with_exception
,
True
)
self
.
_run
(
func
,
x
,
with_exception
,
False
)
def
test_non_variable
(
self
):
self
.
_run_dy_static
(
dyfunc_assert_non_variable
,
x
=
False
,
with_exception
=
True
)
self
.
_run_dy_static
(
dyfunc_assert_non_variable
,
x
=
True
,
with_exception
=
False
)
def
test_bool_variable
(
self
):
self
.
_run_dy_static
(
dyfunc_assert_variable
,
x
=
numpy
.
array
([
False
]),
with_exception
=
True
)
self
.
_run_dy_static
(
dyfunc_assert_variable
,
x
=
numpy
.
array
([
True
]),
with_exception
=
False
)
def
test_int_variable
(
self
):
self
.
_run_dy_static
(
dyfunc_assert_variable
,
x
=
numpy
.
array
([
0
]),
with_exception
=
True
)
self
.
_run_dy_static
(
dyfunc_assert_variable
,
x
=
numpy
.
array
([
1
]),
with_exception
=
False
)
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录