Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
dc956767
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看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
dc956767
编写于
3月 13, 2023
作者:
A
Aurelius84
提交者:
GitHub
3月 13, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix ProxyLayer @to_static not take effect problem (#51464)
上级
afa26a59
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
60 addition
and
3 deletion
+60
-3
python/paddle/distributed/auto_parallel/helper.py
python/paddle/distributed/auto_parallel/helper.py
+8
-0
python/paddle/fluid/tests/unittests/auto_parallel/test_to_static.py
...dle/fluid/tests/unittests/auto_parallel/test_to_static.py
+20
-1
python/paddle/jit/dy2static/utils.py
python/paddle/jit/dy2static/utils.py
+32
-2
未找到文件。
python/paddle/distributed/auto_parallel/helper.py
浏览文件 @
dc956767
...
...
@@ -12,11 +12,13 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import
inspect
import
logging
from
collections
import
defaultdict
from
paddle.jit
import
not_to_static
,
to_static
from
paddle.jit.dy2static.program_translator
import
StaticFunction
from
paddle.jit.dy2static.utils
import
as_not_paddle_func
from
paddle.nn
import
Layer
from
paddle.static
import
Parameter
,
global_scope
,
program_guard
...
...
@@ -47,6 +49,12 @@ class ProxyLayer(Layer):
self
.
_loss_vars
=
defaultdict
(
list
)
self
.
_metric_vars
=
defaultdict
(
list
)
# Consider ProxyLayer as not Paddle inner function because it contains
# user-defined layer.
as_not_paddle_func
(
inspect
.
getmodule
(
ProxyLayer
).
__name__
+
".ProxyLayer"
)
def
_train
(
self
,
inputs
,
labels
):
"""
Train process of inner_layer with forward/loss/metric logic.
...
...
python/paddle/fluid/tests/unittests/auto_parallel/test_to_static.py
浏览文件 @
dc956767
...
...
@@ -20,10 +20,11 @@ import paddle
import
paddle.nn
as
nn
import
paddle.nn.functional
as
F
from
paddle
import
LazyGuard
from
paddle.distributed.auto_parallel.helper
import
ProgramHelper
from
paddle.distributed.auto_parallel.helper
import
ProgramHelper
,
ProxyLayer
from
paddle.distributed.fleet
import
auto
from
paddle.fluid.framework
import
_non_static_mode
from
paddle.io
import
Dataset
from
paddle.jit.dy2static.utils
import
is_paddle_func
from
paddle.static
import
InputSpec
batch_size
=
4
...
...
@@ -182,5 +183,23 @@ class TestLazyInit(unittest.TestCase):
program_helper
.
reset
()
class
TestIgnoreProxyLayer
(
unittest
.
TestCase
):
def
test_is_paddle_func
(
self
):
mlp
=
MLPLayer
(
hidden_size
=
hidden_size
,
intermediate_size
=
4
*
hidden_size
,
dropout_ratio
=
0.1
,
initializer_range
=
0.02
,
)
loss
=
paddle
.
nn
.
CrossEntropyLoss
()
metrics
=
paddle
.
metric
.
Accuracy
()
proxy_layer
=
ProxyLayer
(
mlp
,
loss
,
metrics
)
self
.
assertFalse
(
is_paddle_func
(
proxy_layer
.
_train
))
self
.
assertFalse
(
is_paddle_func
(
proxy_layer
.
_eval
))
self
.
assertFalse
(
is_paddle_func
(
proxy_layer
.
_predict
))
if
__name__
==
"__main__"
:
unittest
.
main
()
python/paddle/jit/dy2static/utils.py
浏览文件 @
dc956767
...
...
@@ -296,21 +296,51 @@ def is_paddle_api(node):
return
is_api_in_module
(
node
,
PADDLE_MODULE_PREFIX
)
def
is_paddle_func
(
func
):
# NOTE(Aurelius84): Consider the following paddle inner API as common case to
# apply @to_static code transformation as usual. Because they contains
# user-defined layer, like paddle.distributed.auto_parallel.helper.ProxyLayer.
AS_NOT_INNER_FUNC_LIST
=
set
()
def
as_not_paddle_func
(
path
):
"""
Append API or class as ignored case for is_paddle_func, and they
will be retured False while calling is_paddle_func(func).
"""
global
INNER_FUNC_WHITE_LIST
AS_NOT_INNER_FUNC_LIST
.
add
(
path
)
def
is_paddle_func
(
func
,
ignore_white_list
=
True
):
"""
Return True if function is defined in Paddle module.
Skip to check APIs in white list if specifying ignore_white_list as True.
"""
def
in_white_list
(
module
,
func_name
):
if
func_name
is
None
:
return
False
return
(
module
.
__name__
+
'.'
+
func_name
)
in
AS_NOT_INNER_FUNC_LIST
try
:
if
isinstance
(
func
,
functools
.
partial
):
func
=
func
.
func
func_name
=
getattr
(
func
,
'__name__'
,
None
)
# In case of dynamically monkey patch customised function
# into paddle class obj, so we consider its class module
# path as prefix.
if
hasattr
(
func
,
"__self__"
):
func
=
func
.
__self__
func_name
=
func
.
__class__
.
__name__
elif
inspect
.
ismethod
(
func
):
func
=
func
.
__func__
m
=
inspect
.
getmodule
(
func
)
return
m
is
not
None
and
m
.
__name__
.
startswith
(
PADDLE_MODULE_PREFIX
)
flag
=
m
is
not
None
and
m
.
__name__
.
startswith
(
PADDLE_MODULE_PREFIX
)
if
ignore_white_list
:
flag
=
flag
and
not
in_white_list
(
m
,
func_name
)
return
flag
except
Exception
:
return
False
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录