Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
4a66e7cf
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看板
未验证
提交
4a66e7cf
编写于
12月 02, 2022
作者:
Y
Yiqun Liu
提交者:
GitHub
12月 02, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Optimize the python overhead of reshape and layer_norm. (#48635)
上级
5bcf35cd
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
18 addition
and
33 deletion
+18
-33
python/paddle/nn/functional/norm.py
python/paddle/nn/functional/norm.py
+4
-9
python/paddle/tensor/manipulation.py
python/paddle/tensor/manipulation.py
+14
-24
未找到文件。
python/paddle/nn/functional/norm.py
浏览文件 @
4a66e7cf
...
@@ -382,16 +382,11 @@ def layer_norm(
...
@@ -382,16 +382,11 @@ def layer_norm(
)
)
if
in_dygraph_mode
():
if
in_dygraph_mode
():
(
out
,
_
,
_
=
_C_ops
.
layer_norm
(
x
,
weight
,
bias
,
epsilon
,
begin_norm_axis
)
pre_act
,
return
out
_
,
_
,
)
=
_C_ops
.
layer_norm
(
x
,
weight
,
bias
,
epsilon
,
begin_norm_axis
)
return
dygraph_utils
.
_append_activation_in_dygraph
(
pre_act
,
act
=
None
)
if
_in_legacy_dygraph
():
if
_in_legacy_dygraph
():
pre_ac
t
,
_
,
_
=
_legacy_C_ops
.
layer_norm
(
ou
t
,
_
,
_
=
_legacy_C_ops
.
layer_norm
(
x
,
x
,
weight
,
weight
,
bias
,
bias
,
...
@@ -400,7 +395,7 @@ def layer_norm(
...
@@ -400,7 +395,7 @@ def layer_norm(
'begin_norm_axis'
,
'begin_norm_axis'
,
begin_norm_axis
,
begin_norm_axis
,
)
)
return
dygraph_utils
.
_append_activation_in_dygraph
(
pre_act
,
act
=
None
)
return
out
check_variable_and_dtype
(
check_variable_and_dtype
(
x
,
'input'
,
[
'float16'
,
'float32'
,
'float64'
],
'LayerNorm'
x
,
'input'
,
[
'float16'
,
'float32'
,
'float64'
],
'LayerNorm'
...
...
python/paddle/tensor/manipulation.py
浏览文件 @
4a66e7cf
...
@@ -14,7 +14,6 @@
...
@@ -14,7 +14,6 @@
# TODO: define functions to manipulate a tensor
# TODO: define functions to manipulate a tensor
import
warnings
from
collections
import
Counter
from
collections
import
Counter
import
numpy
as
np
import
numpy
as
np
...
@@ -22,7 +21,7 @@ import numpy as np
...
@@ -22,7 +21,7 @@ import numpy as np
import
paddle
import
paddle
from
paddle
import
_C_ops
,
_legacy_C_ops
from
paddle
import
_C_ops
,
_legacy_C_ops
from
..common_ops_import
import
_varbase_creator
,
dygraph_utils
,
fill_constant
from
..common_ops_import
import
_varbase_creator
,
fill_constant
from
..fluid.data_feeder
import
(
from
..fluid.data_feeder
import
(
check_dtype
,
check_dtype
,
check_type
,
check_type
,
...
@@ -3564,16 +3563,9 @@ def reshape(x, shape, name=None):
...
@@ -3564,16 +3563,9 @@ def reshape(x, shape, name=None):
"""
"""
actual_shape
=
None
actual_shape
=
None
act
=
None
inplace
=
False
if
in_dygraph_mode
():
if
in_dygraph_mode
():
tmp_tensor_type
=
core
.
eager
.
Tensor
tmp_tensor_type
=
core
.
eager
.
Tensor
# TODO(zhiqiu): enable inplace in dygraph mode.
if
inplace
:
warnings
.
warn
(
"Inplace on reshape is not allowed and will be discarded in dygraph mode currently."
)
if
isinstance
(
shape
,
(
list
,
tuple
)):
if
isinstance
(
shape
,
(
list
,
tuple
)):
shape
=
[
shape
=
[
item
.
numpy
().
item
(
0
)
item
.
numpy
().
item
(
0
)
...
@@ -3581,8 +3573,11 @@ def reshape(x, shape, name=None):
...
@@ -3581,8 +3573,11 @@ def reshape(x, shape, name=None):
else
item
else
item
for
item
in
shape
for
item
in
shape
]
]
out
=
_C_ops
.
reshape
(
x
,
shape
)
if
shape
==
x
.
shape
:
elif
isinstance
(
shape
,
tmp_tensor_type
):
out
=
x
else
:
out
=
_C_ops
.
reshape
(
x
,
shape
)
elif
isinstance
(
shape
,
core
.
eager
.
Tensor
):
shape
.
stop_gradient
=
True
shape
.
stop_gradient
=
True
out
=
_C_ops
.
reshape
(
x
,
shape
)
out
=
_C_ops
.
reshape
(
x
,
shape
)
else
:
else
:
...
@@ -3591,14 +3586,10 @@ def reshape(x, shape, name=None):
...
@@ -3591,14 +3586,10 @@ def reshape(x, shape, name=None):
" got '{}.'"
.
format
(
type
(
shape
))
" got '{}.'"
.
format
(
type
(
shape
))
)
)
return
dygraph_utils
.
_append_activation_in_dygraph
(
out
,
act
)
return
out
else
:
else
:
if
_in_legacy_dygraph
():
if
_in_legacy_dygraph
():
tmp_tensor_type
=
Variable
tmp_tensor_type
=
Variable
if
inplace
:
warnings
.
warn
(
"Inplace on reshape is not allowed and will be discarded in dygraph mode currently."
)
if
isinstance
(
shape
,
(
list
,
tuple
)):
if
isinstance
(
shape
,
(
list
,
tuple
)):
shape
=
[
shape
=
[
item
.
numpy
().
item
(
0
)
if
isinstance
(
item
,
Variable
)
else
item
item
.
numpy
().
item
(
0
)
if
isinstance
(
item
,
Variable
)
else
item
...
@@ -3614,7 +3605,7 @@ def reshape(x, shape, name=None):
...
@@ -3614,7 +3605,7 @@ def reshape(x, shape, name=None):
" got '{}.'"
.
format
(
type
(
shape
))
" got '{}.'"
.
format
(
type
(
shape
))
)
)
return
dygraph_utils
.
_append_activation_in_dygraph
(
out
,
act
)
return
out
check_variable_and_dtype
(
check_variable_and_dtype
(
x
,
x
,
...
@@ -3690,11 +3681,7 @@ def reshape(x, shape, name=None):
...
@@ -3690,11 +3681,7 @@ def reshape(x, shape, name=None):
actual_shape
.
stop_gradient
=
True
actual_shape
.
stop_gradient
=
True
inputs
[
"Shape"
]
=
actual_shape
inputs
[
"Shape"
]
=
actual_shape
out
=
(
out
=
helper
.
create_variable_for_type_inference
(
dtype
=
x
.
dtype
)
x
if
inplace
else
helper
.
create_variable_for_type_inference
(
dtype
=
x
.
dtype
)
)
x_shape
=
helper
.
create_variable_for_type_inference
(
dtype
=
x
.
dtype
)
x_shape
=
helper
.
create_variable_for_type_inference
(
dtype
=
x
.
dtype
)
helper
.
append_op
(
helper
.
append_op
(
type
=
"reshape2"
,
type
=
"reshape2"
,
...
@@ -3703,7 +3690,7 @@ def reshape(x, shape, name=None):
...
@@ -3703,7 +3690,7 @@ def reshape(x, shape, name=None):
outputs
=
{
"Out"
:
out
,
"XShape"
:
x_shape
},
outputs
=
{
"Out"
:
out
,
"XShape"
:
x_shape
},
)
)
return
helper
.
append_activation
(
out
)
return
out
@
inplace_apis_in_dygraph_only
@
inplace_apis_in_dygraph_only
...
@@ -3721,7 +3708,10 @@ def reshape_(x, shape, name=None):
...
@@ -3721,7 +3708,10 @@ def reshape_(x, shape, name=None):
else
item
else
item
for
item
in
shape
for
item
in
shape
]
]
out
=
_C_ops
.
reshape_
(
x
,
shape
)
if
shape
==
x
.
shape
:
out
=
x
else
:
out
=
_C_ops
.
reshape_
(
x
,
shape
)
elif
isinstance
(
shape
,
tmp_tensor_type
):
elif
isinstance
(
shape
,
tmp_tensor_type
):
shape
.
stop_gradient
=
True
shape
.
stop_gradient
=
True
out
=
_C_ops
.
reshape_
(
x
,
shape
)
out
=
_C_ops
.
reshape_
(
x
,
shape
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录