Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
7ba6279a
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2298
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看板
未验证
提交
7ba6279a
编写于
9月 22, 2020
作者:
A
Aurelius84
提交者:
GitHub
9月 22, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[Dy2stat] Refine error msg of @to_static if not in imperative mode (#27371)
* refine error mesg
上级
dd4c2d86
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
66 addition
and
0 deletion
+66
-0
python/paddle/fluid/dygraph/dygraph_to_static/program_translator.py
...dle/fluid/dygraph/dygraph_to_static/program_translator.py
+17
-0
python/paddle/fluid/dygraph/layers.py
python/paddle/fluid/dygraph/layers.py
+1
-0
python/paddle/fluid/tests/unittests/dygraph_to_static/test_declarative.py
...uid/tests/unittests/dygraph_to_static/test_declarative.py
+19
-0
python/paddle/fluid/tests/unittests/dygraph_to_static/test_program_translator.py
...ts/unittests/dygraph_to_static/test_program_translator.py
+29
-0
未找到文件。
python/paddle/fluid/dygraph/dygraph_to_static/program_translator.py
浏览文件 @
7ba6279a
...
...
@@ -370,6 +370,7 @@ class StaticLayer(object):
Returns:
Traced ConcreteProgram and executable translated Layer.
"""
# 1. unify args/kwargs and replace Tensor with InputSpec
if
len
(
args
)
!=
len
(
self
.
_function_spec
.
args_name
):
args
,
kwargs
=
self
.
_function_spec
.
unified_args_and_kwargs
(
args
,
...
...
@@ -522,6 +523,19 @@ def _switch_declarative_mode_guard_(is_declarative=True):
_in_declarative_mode_
=
original_val
def
_verify_init_in_dynamic_mode
(
class_instance
):
"""
Verifies the instance is initialized in dynamic mode.
"""
if
isinstance
(
class_instance
,
layers
.
Layer
):
if
not
class_instance
.
_init_in_dynamic_mode
:
raise
RuntimeError
(
" `paddle.jit.to_static` is only available in dynamic mode. Please call `paddle.disable_static()` before "
"initializing your Layer class `{}` . Because parameters of Layer class should be initialized firstly "
"in dynamic mode while applying transformation."
.
format
(
class_instance
))
class
ConcreteProgram
(
object
):
__slots__
=
[
...
...
@@ -554,6 +568,9 @@ class ConcreteProgram(object):
func_spec(FunctionSpec): A FunctionSpec instance for decorated function.
input_spec(list[InputSpec]):
"""
# verify the instance is initialized in imperative mode.
_verify_init_in_dynamic_mode
(
class_instance
)
# Transforms dygraph function into static function and caches it.
dygraph_function
=
func_spec
.
dygraph_function
static_func
=
convert_to_static
(
dygraph_function
)
...
...
python/paddle/fluid/dygraph/layers.py
浏览文件 @
7ba6279a
...
...
@@ -91,6 +91,7 @@ class Layer(core.Layer):
self
.
_helper
=
LayerObjectHelper
(
self
.
_full_name
)
self
.
_built
=
False
self
.
_dtype
=
dtype
self
.
_init_in_dynamic_mode
=
framework
.
in_dygraph_mode
()
self
.
_parameters
=
collections
.
OrderedDict
()
# Buffers the variable (not parameter) created in layer
...
...
python/paddle/fluid/tests/unittests/dygraph_to_static/test_declarative.py
浏览文件 @
7ba6279a
...
...
@@ -358,5 +358,24 @@ class TestDecorateModelDirectly(unittest.TestCase):
self
.
assertListEqual
(
list
(
input_shape
),
[
-
1
,
16
,
10
])
class
TestErrorWithInitFromStaticMode
(
unittest
.
TestCase
):
def
test_raise_error
(
self
):
# disable imperative
paddle
.
enable_static
()
net
=
SimpleNet
()
with
self
.
assertRaisesRegexp
(
RuntimeError
,
"only available in dynamic mode"
):
net
.
forward
.
concrete_program
with
self
.
assertRaisesRegexp
(
RuntimeError
,
"only available in dynamic mode"
):
net
.
forward
.
inputs
with
self
.
assertRaisesRegexp
(
RuntimeError
,
"only available in dynamic mode"
):
net
.
forward
.
outputs
if
__name__
==
'__main__'
:
unittest
.
main
()
python/paddle/fluid/tests/unittests/dygraph_to_static/test_program_translator.py
浏览文件 @
7ba6279a
...
...
@@ -21,6 +21,7 @@ import numpy as np
import
textwrap
import
unittest
import
paddle
import
paddle.fluid
as
fluid
from
paddle.fluid.dygraph.dygraph_to_static
import
ProgramTranslator
from
paddle.fluid.dygraph.jit
import
declarative
...
...
@@ -279,5 +280,33 @@ class TestEnableDeclarative(unittest.TestCase):
static_output
.
numpy
(),
dygraph_output
.
numpy
(),
atol
=
1e-4
))
class
Net
(
fluid
.
dygraph
.
layers
.
Layer
):
def
__init__
(
self
):
super
(
Net
,
self
).
__init__
()
def
forward
(
self
,
x
):
return
x
+
1
class
TestErrorWithInitFromStaticMode
(
unittest
.
TestCase
):
def
setUp
(
self
):
self
.
program_translator
=
ProgramTranslator
()
self
.
x
=
np
.
random
.
randn
(
10
,
32
).
astype
(
'float32'
)
def
test_raise_error
(
self
):
# disable imperative
paddle
.
enable_static
()
net
=
Net
()
self
.
program_translator
.
enable
(
True
)
with
self
.
assertRaisesRegexp
(
RuntimeError
,
"only available in dynamic mode"
):
self
.
program_translator
.
get_output
(
net
.
forward
,
self
.
x
)
with
self
.
assertRaisesRegexp
(
RuntimeError
,
"only available in dynamic mode"
):
self
.
program_translator
.
get_program
(
net
.
forward
,
self
.
x
)
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录