Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
140d786d
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 2 年 前同步成功
通知
2325
Star
20933
Fork
5424
代码
文件
提交
分支
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看板
未验证
提交
140d786d
编写于
12月 27, 2022
作者:
姜
姜永久
提交者:
GitHub
12月 27, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
rm in_legacy_dygraph python/paddle/nn/functional/ part1 (#49258)
* rm in_legacy_dygraph nn part1 * rm non_static_mode * modify rrelu
上级
861fef52
变更
5
显示空白变更内容
内联
并排
Showing
5 changed file
with
508 addition
and
785 deletion
+508
-785
python/paddle/nn/functional/activation.py
python/paddle/nn/functional/activation.py
+317
-377
python/paddle/nn/functional/common.py
python/paddle/nn/functional/common.py
+90
-140
python/paddle/nn/functional/conv.py
python/paddle/nn/functional/conv.py
+1
-107
python/paddle/nn/functional/distance.py
python/paddle/nn/functional/distance.py
+25
-31
python/paddle/nn/layer/norm.py
python/paddle/nn/layer/norm.py
+75
-130
未找到文件。
python/paddle/nn/functional/activation.py
浏览文件 @
140d786d
...
@@ -18,11 +18,7 @@ from paddle.framework import core
...
@@ -18,11 +18,7 @@ from paddle.framework import core
from
paddle.utils.inplace_utils
import
inplace_apis_in_dygraph_only
from
paddle.utils.inplace_utils
import
inplace_apis_in_dygraph_only
from
...fluid.data_feeder
import
check_dtype
,
check_variable_and_dtype
from
...fluid.data_feeder
import
check_dtype
,
check_variable_and_dtype
from
...fluid.framework
import
(
from
...fluid.framework
import
convert_np_dtype_to_dtype_
,
in_dygraph_mode
_in_legacy_dygraph
,
convert_np_dtype_to_dtype_
,
in_dygraph_mode
,
)
from
...fluid.layer_helper
import
LayerHelper
from
...fluid.layer_helper
import
LayerHelper
from
...tensor.manipulation
import
chunk
from
...tensor.manipulation
import
chunk
from
...tensor.math
import
tanh
# noqa: F401
from
...tensor.math
import
tanh
# noqa: F401
...
@@ -62,13 +58,12 @@ def celu(x, alpha=1.0, name=None):
...
@@ -62,13 +58,12 @@ def celu(x, alpha=1.0, name=None):
"""
"""
if
alpha
==
0
:
if
alpha
==
0
:
raise
ZeroDivisionError
(
"alpha cannot be 0 for celu"
)
raise
ZeroDivisionError
(
"alpha cannot be 0 for celu"
)
if
_in_legacy_dygraph
():
return
_legacy_C_ops
.
celu
(
x
,
'alpha'
,
alpha
)
if
in_dygraph_mode
():
if
in_dygraph_mode
():
return
_C_ops
.
celu
(
x
,
alpha
)
return
_C_ops
.
celu
(
x
,
alpha
)
else
:
check_variable_and_dtype
(
x
,
'x'
,
[
'float16'
,
'float32'
,
'float64'
],
'celu'
)
check_variable_and_dtype
(
x
,
'x'
,
[
'float16'
,
'float32'
,
'float64'
],
'celu'
)
helper
=
LayerHelper
(
"celu"
,
**
locals
())
helper
=
LayerHelper
(
"celu"
,
**
locals
())
out
=
helper
.
create_variable_for_type_inference
(
x
.
dtype
)
out
=
helper
.
create_variable_for_type_inference
(
x
.
dtype
)
helper
.
append_op
(
helper
.
append_op
(
...
@@ -117,10 +112,10 @@ def elu(x, alpha=1.0, name=None):
...
@@ -117,10 +112,10 @@ def elu(x, alpha=1.0, name=None):
if
in_dygraph_mode
():
if
in_dygraph_mode
():
return
_C_ops
.
elu
(
x
,
alpha
)
return
_C_ops
.
elu
(
x
,
alpha
)
if
_in_legacy_dygraph
()
:
else
:
return
_legacy_C_ops
.
elu
(
x
,
'alpha'
,
alpha
)
check_variable_and_dtype
(
x
,
'x'
,
[
'float16'
,
'float32'
,
'float64'
],
'elu'
check_variable_and_dtype
(
x
,
'x'
,
[
'float16'
,
'float32'
,
'float64'
],
'elu'
)
)
helper
=
LayerHelper
(
"elu"
,
**
locals
())
helper
=
LayerHelper
(
"elu"
,
**
locals
())
out
=
helper
.
create_variable_for_type_inference
(
x
.
dtype
)
out
=
helper
.
create_variable_for_type_inference
(
x
.
dtype
)
helper
.
append_op
(
helper
.
append_op
(
...
@@ -187,11 +182,10 @@ def gelu(x, approximate=False, name=None):
...
@@ -187,11 +182,10 @@ def gelu(x, approximate=False, name=None):
if
in_dygraph_mode
():
if
in_dygraph_mode
():
return
_C_ops
.
gelu
(
x
,
approximate
)
return
_C_ops
.
gelu
(
x
,
approximate
)
else
:
if
_in_legacy_dygraph
():
check_variable_and_dtype
(
return
_legacy_C_ops
.
gelu
(
x
,
'approximate'
,
approximate
)
x
,
'x'
,
[
'float16'
,
'float32'
,
'float64'
],
'gelu'
)
check_variable_and_dtype
(
x
,
'x'
,
[
'float16'
,
'float32'
,
'float64'
],
'gelu'
)
helper
=
LayerHelper
(
"gelu"
,
**
locals
())
helper
=
LayerHelper
(
"gelu"
,
**
locals
())
out
=
helper
.
create_variable_for_type_inference
(
x
.
dtype
)
out
=
helper
.
create_variable_for_type_inference
(
x
.
dtype
)
helper
.
append_op
(
helper
.
append_op
(
...
@@ -238,10 +232,7 @@ def hardshrink(x, threshold=0.5, name=None):
...
@@ -238,10 +232,7 @@ def hardshrink(x, threshold=0.5, name=None):
"""
"""
if
in_dygraph_mode
():
if
in_dygraph_mode
():
return
_C_ops
.
hardshrink
(
x
,
threshold
)
return
_C_ops
.
hardshrink
(
x
,
threshold
)
else
:
if
_in_legacy_dygraph
():
return
_legacy_C_ops
.
hard_shrink
(
x
,
'threshold'
,
threshold
)
check_variable_and_dtype
(
check_variable_and_dtype
(
x
,
'x'
,
[
'float16'
,
'float32'
,
'float64'
],
'hardshrink'
x
,
'x'
,
[
'float16'
,
'float32'
,
'float64'
],
'hardshrink'
)
)
...
@@ -292,10 +283,7 @@ def hardtanh(x, min=-1.0, max=1.0, name=None):
...
@@ -292,10 +283,7 @@ def hardtanh(x, min=-1.0, max=1.0, name=None):
if
in_dygraph_mode
():
if
in_dygraph_mode
():
return
_C_ops
.
hardtanh
(
x
,
min
,
max
)
return
_C_ops
.
hardtanh
(
x
,
min
,
max
)
else
:
if
_in_legacy_dygraph
():
return
_legacy_C_ops
.
brelu
(
x
,
't_min'
,
min
,
't_max'
,
max
)
check_variable_and_dtype
(
check_variable_and_dtype
(
x
,
'x'
,
[
'float16'
,
'float32'
,
'float64'
],
'hardtanh'
x
,
'x'
,
[
'float16'
,
'float32'
,
'float64'
],
'hardtanh'
)
)
...
@@ -349,10 +337,7 @@ def hardsigmoid(x, slope=0.1666667, offset=0.5, name=None):
...
@@ -349,10 +337,7 @@ def hardsigmoid(x, slope=0.1666667, offset=0.5, name=None):
if
in_dygraph_mode
():
if
in_dygraph_mode
():
return
_C_ops
.
hardsigmoid
(
x
,
slope
,
offset
)
return
_C_ops
.
hardsigmoid
(
x
,
slope
,
offset
)
else
:
if
_in_legacy_dygraph
():
return
_legacy_C_ops
.
hard_sigmoid
(
x
,
'slope'
,
slope
,
'offset'
,
offset
)
check_variable_and_dtype
(
check_variable_and_dtype
(
x
,
'x'
,
[
'float16'
,
'float32'
,
'float64'
],
'hardsigmoid'
x
,
'x'
,
[
'float16'
,
'float32'
,
'float64'
],
'hardsigmoid'
)
)
...
@@ -401,19 +386,18 @@ def hardswish(x, name=None):
...
@@ -401,19 +386,18 @@ def hardswish(x, name=None):
x = paddle.to_tensor([-4., 5., 1.])
x = paddle.to_tensor([-4., 5., 1.])
out = F.hardswish(x) # [0., 5., 0.666667]
out = F.hardswish(x) # [0., 5., 0.666667]
"""
"""
if
_in_legacy_dygraph
():
return
_legacy_C_ops
.
hard_swish
(
x
)
if
in_dygraph_mode
():
if
in_dygraph_mode
():
return
_C_ops
.
hardswish
(
x
)
return
_C_ops
.
hardswish
(
x
)
else
:
check_variable_and_dtype
(
check_variable_and_dtype
(
x
,
'x'
,
[
'float16'
,
'float32'
,
'float64'
],
'hardswish'
x
,
'x'
,
[
'float16'
,
'float32'
,
'float64'
],
'hardswish'
)
)
helper
=
LayerHelper
(
'hardswish'
,
**
locals
())
helper
=
LayerHelper
(
'hardswish'
,
**
locals
())
out
=
helper
.
create_variable_for_type_inference
(
x
.
dtype
)
out
=
helper
.
create_variable_for_type_inference
(
x
.
dtype
)
helper
.
append_op
(
type
=
'hard_swish'
,
inputs
=
{
'X'
:
x
},
outputs
=
{
'Out'
:
out
})
helper
.
append_op
(
type
=
'hard_swish'
,
inputs
=
{
'X'
:
x
},
outputs
=
{
'Out'
:
out
}
)
return
out
return
out
...
@@ -453,10 +437,7 @@ def leaky_relu(x, negative_slope=0.01, name=None):
...
@@ -453,10 +437,7 @@ def leaky_relu(x, negative_slope=0.01, name=None):
"""
"""
if
in_dygraph_mode
():
if
in_dygraph_mode
():
return
_C_ops
.
leaky_relu
(
x
,
negative_slope
)
return
_C_ops
.
leaky_relu
(
x
,
negative_slope
)
else
:
if
_in_legacy_dygraph
():
return
_legacy_C_ops
.
leaky_relu
(
x
,
'alpha'
,
negative_slope
)
check_variable_and_dtype
(
check_variable_and_dtype
(
x
,
'x'
,
[
'float16'
,
'float32'
,
'float64'
],
'leaky_relu'
x
,
'x'
,
[
'float16'
,
'float32'
,
'float64'
],
'leaky_relu'
)
)
...
@@ -559,11 +540,7 @@ def prelu(x, weight, data_format="NCHW", name=None):
...
@@ -559,11 +540,7 @@ def prelu(x, weight, data_format="NCHW", name=None):
if
in_dygraph_mode
():
if
in_dygraph_mode
():
return
_C_ops
.
prelu
(
x
,
weight
,
data_format
,
mode
)
return
_C_ops
.
prelu
(
x
,
weight
,
data_format
,
mode
)
if
_in_legacy_dygraph
():
else
:
return
_legacy_C_ops
.
prelu
(
x
,
weight
,
'mode'
,
mode
,
'data_format'
,
data_format
)
helper
=
LayerHelper
(
'prelu'
,
**
locals
())
helper
=
LayerHelper
(
'prelu'
,
**
locals
())
out
=
helper
.
create_variable_for_type_inference
(
x
.
dtype
)
out
=
helper
.
create_variable_for_type_inference
(
x
.
dtype
)
helper
.
append_op
(
helper
.
append_op
(
...
@@ -681,12 +658,12 @@ def rrelu(x, lower=1.0 / 8.0, upper=1.0 / 3.0, training=True, name=None):
...
@@ -681,12 +658,12 @@ def rrelu(x, lower=1.0 / 8.0, upper=1.0 / 3.0, training=True, name=None):
is_test
=
not
training
is_test
=
not
training
if
_in_legacy_dygraph
():
if
in_dygraph_mode
():
out
,
noise
=
_legacy_C_ops
.
rrelu
(
out
,
noise
=
_legacy_C_ops
.
rrelu
(
x
,
'lower'
,
lower
,
'upper'
,
upper
,
'is_test'
,
is_test
x
,
'lower'
,
lower
,
'upper'
,
upper
,
'is_test'
,
is_test
)
)
return
out
return
out
else
:
helper
=
LayerHelper
(
'rrelu'
,
**
locals
())
helper
=
LayerHelper
(
'rrelu'
,
**
locals
())
out
=
helper
.
create_variable_for_type_inference
(
x
.
dtype
)
out
=
helper
.
create_variable_for_type_inference
(
x
.
dtype
)
noise
=
helper
.
create_variable_for_type_inference
(
dtype
=
x
.
dtype
)
noise
=
helper
.
create_variable_for_type_inference
(
dtype
=
x
.
dtype
)
...
@@ -729,9 +706,10 @@ def relu(x, name=None):
...
@@ -729,9 +706,10 @@ def relu(x, name=None):
if
in_dygraph_mode
():
if
in_dygraph_mode
():
return
_C_ops
.
relu
(
x
)
return
_C_ops
.
relu
(
x
)
if
_in_legacy_dygraph
():
else
:
return
_legacy_C_ops
.
relu
(
x
)
check_variable_and_dtype
(
check_variable_and_dtype
(
x
,
'x'
,
[
'float16'
,
'float32'
,
'float64'
],
'relu'
)
x
,
'x'
,
[
'float16'
,
'float32'
,
'float64'
],
'relu'
)
helper
=
LayerHelper
(
'relu'
,
**
locals
())
helper
=
LayerHelper
(
'relu'
,
**
locals
())
out
=
helper
.
create_variable_for_type_inference
(
x
.
dtype
)
out
=
helper
.
create_variable_for_type_inference
(
x
.
dtype
)
helper
.
append_op
(
type
=
'relu'
,
inputs
=
{
'X'
:
x
},
outputs
=
{
'Out'
:
out
})
helper
.
append_op
(
type
=
'relu'
,
inputs
=
{
'X'
:
x
},
outputs
=
{
'Out'
:
out
})
...
@@ -744,10 +722,7 @@ def relu_(x, name=None):
...
@@ -744,10 +722,7 @@ def relu_(x, name=None):
Inplace version of ``relu`` API, the output Tensor will be inplaced with input ``x``.
Inplace version of ``relu`` API, the output Tensor will be inplaced with input ``x``.
Please refer to :ref:`api_nn_cn_relu`.
Please refer to :ref:`api_nn_cn_relu`.
"""
"""
if
in_dygraph_mode
():
return
_C_ops
.
relu_
(
x
)
return
_C_ops
.
relu_
(
x
)
if
_in_legacy_dygraph
():
return
_legacy_C_ops
.
relu_
(
x
)
def
log_sigmoid
(
x
,
name
=
None
):
def
log_sigmoid
(
x
,
name
=
None
):
...
@@ -777,16 +752,15 @@ def log_sigmoid(x, name=None):
...
@@ -777,16 +752,15 @@ def log_sigmoid(x, name=None):
if
in_dygraph_mode
():
if
in_dygraph_mode
():
return
_C_ops
.
logsigmoid
(
x
)
return
_C_ops
.
logsigmoid
(
x
)
else
:
if
_in_legacy_dygraph
():
return
_legacy_C_ops
.
logsigmoid
(
x
)
check_variable_and_dtype
(
check_variable_and_dtype
(
x
,
'x'
,
[
'float16'
,
'float32'
,
'float64'
],
'log_sigmoid'
x
,
'x'
,
[
'float16'
,
'float32'
,
'float64'
],
'log_sigmoid'
)
)
helper
=
LayerHelper
(
"log_sigmoid"
,
**
locals
())
helper
=
LayerHelper
(
"log_sigmoid"
,
**
locals
())
out
=
helper
.
create_variable_for_type_inference
(
x
.
dtype
)
out
=
helper
.
create_variable_for_type_inference
(
x
.
dtype
)
helper
.
append_op
(
type
=
'logsigmoid'
,
inputs
=
{
'X'
:
x
},
outputs
=
{
'Out'
:
out
})
helper
.
append_op
(
type
=
'logsigmoid'
,
inputs
=
{
'X'
:
x
},
outputs
=
{
'Out'
:
out
}
)
return
out
return
out
...
@@ -844,10 +818,9 @@ def maxout(x, groups, axis=1, name=None):
...
@@ -844,10 +818,9 @@ def maxout(x, groups, axis=1, name=None):
# [0.95313174 0.6228939 0.7129065 0.7087491 ]
# [0.95313174 0.6228939 0.7129065 0.7087491 ]
# [0.7142536 0.88725346 0.61093384 0.38833922]]]]
# [0.7142536 0.88725346 0.61093384 0.38833922]]]]
"""
"""
if
_in_legacy_dygraph
():
return
_legacy_C_ops
.
maxout
(
x
,
'groups'
,
groups
,
'axis'
,
axis
)
if
in_dygraph_mode
():
if
in_dygraph_mode
():
return
_C_ops
.
maxout
(
x
,
groups
,
axis
)
return
_C_ops
.
maxout
(
x
,
groups
,
axis
)
else
:
check_variable_and_dtype
(
x
,
'x'
,
[
'float32'
,
'float64'
],
'maxout'
)
check_variable_and_dtype
(
x
,
'x'
,
[
'float32'
,
'float64'
],
'maxout'
)
if
axis
not
in
[
1
,
-
1
,
3
]:
if
axis
not
in
[
1
,
-
1
,
3
]:
raise
ValueError
(
raise
ValueError
(
...
@@ -963,10 +936,10 @@ def selu(
...
@@ -963,10 +936,10 @@ def selu(
if
in_dygraph_mode
():
if
in_dygraph_mode
():
return
_C_ops
.
selu
(
x
,
scale
,
alpha
)
return
_C_ops
.
selu
(
x
,
scale
,
alpha
)
if
_in_legacy_dygraph
()
:
else
:
return
_legacy_C_ops
.
selu
(
x
,
'scale'
,
scale
,
'alpha'
,
alpha
)
check_variable_and_dtype
(
x
,
'x'
,
[
'float16'
,
'float32'
,
'float64'
],
'selu'
check_variable_and_dtype
(
x
,
'x'
,
[
'float16'
,
'float32'
,
'float64'
],
'selu'
)
)
helper
=
LayerHelper
(
'selu'
,
**
locals
())
helper
=
LayerHelper
(
'selu'
,
**
locals
())
out
=
helper
.
create_variable_for_type_inference
(
x
.
dtype
)
out
=
helper
.
create_variable_for_type_inference
(
x
.
dtype
)
helper
.
append_op
(
helper
.
append_op
(
...
@@ -1007,10 +980,10 @@ def silu(x, name=None):
...
@@ -1007,10 +980,10 @@ def silu(x, name=None):
if
in_dygraph_mode
():
if
in_dygraph_mode
():
return
_C_ops
.
silu
(
x
)
return
_C_ops
.
silu
(
x
)
if
_in_legacy_dygraph
()
:
else
:
return
_legacy_C_ops
.
silu
(
x
)
check_variable_and_dtype
(
x
,
'x'
,
[
'float16'
,
'float32'
,
'float64'
],
'silu'
check_variable_and_dtype
(
x
,
'x'
,
[
'float16'
,
'float32'
,
'float64'
],
'silu'
)
)
helper
=
LayerHelper
(
"silu"
,
**
locals
())
helper
=
LayerHelper
(
"silu"
,
**
locals
())
out
=
helper
.
create_variable_for_type_inference
(
x
.
dtype
)
out
=
helper
.
create_variable_for_type_inference
(
x
.
dtype
)
helper
.
append_op
(
type
=
'silu'
,
inputs
=
{
'X'
:
x
},
outputs
=
{
'Out'
:
out
})
helper
.
append_op
(
type
=
'silu'
,
inputs
=
{
'X'
:
x
},
outputs
=
{
'Out'
:
out
})
...
@@ -1132,22 +1105,11 @@ def softmax(x, axis=-1, dtype=None, name=None):
...
@@ -1132,22 +1105,11 @@ def softmax(x, axis=-1, dtype=None, name=None):
if
(
dtype
is
not
None
)
and
(
not
isinstance
(
dtype
,
core
.
VarDesc
.
VarType
)):
if
(
dtype
is
not
None
)
and
(
not
isinstance
(
dtype
,
core
.
VarDesc
.
VarType
)):
dtype
=
convert_np_dtype_to_dtype_
(
dtype
)
dtype
=
convert_np_dtype_to_dtype_
(
dtype
)
use_cudnn
=
True
if
in_dygraph_mode
():
if
in_dygraph_mode
():
outs_cast
=
x
if
dtype
is
None
else
_C_ops
.
cast
(
x
,
dtype
)
outs_cast
=
x
if
dtype
is
None
else
_C_ops
.
cast
(
x
,
dtype
)
return
_C_ops
.
softmax
(
outs_cast
,
axis
)
return
_C_ops
.
softmax
(
outs_cast
,
axis
)
else
:
if
_in_legacy_dygraph
():
use_cudnn
=
True
outs_cast
=
(
x
if
dtype
is
None
else
_legacy_C_ops
.
cast
(
x
,
'in_dtype'
,
x
.
dtype
,
'out_dtype'
,
dtype
)
)
return
_legacy_C_ops
.
softmax
(
outs_cast
,
'axis'
,
axis
,
'use_cudnn'
,
use_cudnn
)
if
dtype
is
None
:
if
dtype
is
None
:
check_variable_and_dtype
(
check_variable_and_dtype
(
x
,
'x'
,
[
'float16'
,
'float32'
,
'float64'
],
'softmax'
x
,
'x'
,
[
'float16'
,
'float32'
,
'float64'
],
'softmax'
...
@@ -1172,7 +1134,9 @@ def softmax(x, axis=-1, dtype=None, name=None):
...
@@ -1172,7 +1134,9 @@ def softmax(x, axis=-1, dtype=None, name=None):
attrs
=
{
'in_dtype'
:
x
.
dtype
,
'out_dtype'
:
dtype
},
attrs
=
{
'in_dtype'
:
x
.
dtype
,
'out_dtype'
:
dtype
},
)
)
outs_softmax
=
helper
.
create_variable_for_type_inference
(
outs_cast
.
dtype
)
outs_softmax
=
helper
.
create_variable_for_type_inference
(
outs_cast
.
dtype
)
helper
.
append_op
(
helper
.
append_op
(
type
=
'softmax'
,
type
=
'softmax'
,
inputs
=
{
'X'
:
outs_cast
},
inputs
=
{
'X'
:
outs_cast
},
...
@@ -1191,9 +1155,6 @@ def softmax_(x, axis=-1, dtype=None, name=None):
...
@@ -1191,9 +1155,6 @@ def softmax_(x, axis=-1, dtype=None, name=None):
"""
"""
if
(
dtype
is
not
None
)
and
(
not
isinstance
(
dtype
,
core
.
VarDesc
.
VarType
)):
if
(
dtype
is
not
None
)
and
(
not
isinstance
(
dtype
,
core
.
VarDesc
.
VarType
)):
dtype
=
convert_np_dtype_to_dtype_
(
dtype
)
dtype
=
convert_np_dtype_to_dtype_
(
dtype
)
use_cudnn
=
True
if
in_dygraph_mode
():
outs_cast
=
(
outs_cast
=
(
x
x
if
dtype
is
None
if
dtype
is
None
...
@@ -1201,16 +1162,6 @@ def softmax_(x, axis=-1, dtype=None, name=None):
...
@@ -1201,16 +1162,6 @@ def softmax_(x, axis=-1, dtype=None, name=None):
)
)
return
_C_ops
.
softmax_
(
outs_cast
,
axis
)
return
_C_ops
.
softmax_
(
outs_cast
,
axis
)
if
_in_legacy_dygraph
():
outs_cast
=
(
x
if
dtype
is
None
else
_legacy_C_ops
.
cast
(
x
,
'in_dtype'
,
x
.
dtype
,
'out_dtype'
,
dtype
)
)
return
_legacy_C_ops
.
softmax_
(
outs_cast
,
'axis'
,
axis
,
'use_cudnn'
,
use_cudnn
)
def
softplus
(
x
,
beta
=
1
,
threshold
=
20
,
name
=
None
):
def
softplus
(
x
,
beta
=
1
,
threshold
=
20
,
name
=
None
):
r
"""
r
"""
...
@@ -1243,10 +1194,7 @@ def softplus(x, beta=1, threshold=20, name=None):
...
@@ -1243,10 +1194,7 @@ def softplus(x, beta=1, threshold=20, name=None):
if
in_dygraph_mode
():
if
in_dygraph_mode
():
return
_C_ops
.
softplus
(
x
,
beta
,
threshold
)
return
_C_ops
.
softplus
(
x
,
beta
,
threshold
)
else
:
if
_in_legacy_dygraph
():
return
_legacy_C_ops
.
softplus
(
x
,
'beta'
,
beta
,
'threshold'
,
threshold
)
check_variable_and_dtype
(
check_variable_and_dtype
(
x
,
'x'
,
[
'float16'
,
'float32'
,
'float64'
],
'softplus'
x
,
'x'
,
[
'float16'
,
'float32'
,
'float64'
],
'softplus'
)
)
...
@@ -1305,9 +1253,7 @@ def softshrink(x, threshold=0.5, name=None):
...
@@ -1305,9 +1253,7 @@ def softshrink(x, threshold=0.5, name=None):
if
in_dygraph_mode
():
if
in_dygraph_mode
():
return
_C_ops
.
softshrink
(
x
,
threshold
)
return
_C_ops
.
softshrink
(
x
,
threshold
)
if
_in_legacy_dygraph
():
else
:
return
_legacy_C_ops
.
softshrink
(
x
,
'lambda'
,
threshold
)
check_variable_and_dtype
(
check_variable_and_dtype
(
x
,
'x'
,
[
'float16'
,
'float32'
,
'float64'
],
'softshrink'
x
,
'x'
,
[
'float16'
,
'float32'
,
'float64'
],
'softshrink'
)
)
...
@@ -1392,14 +1338,17 @@ def swish(x, name=None):
...
@@ -1392,14 +1338,17 @@ def swish(x, name=None):
"""
"""
if
in_dygraph_mode
():
if
in_dygraph_mode
():
return
_C_ops
.
swish
(
x
)
return
_C_ops
.
swish
(
x
)
if
_in_legacy_dygraph
()
:
else
:
return
_legacy_C_ops
.
swish
(
x
,
'beta'
,
1.0
)
check_variable_and_dtype
(
x
,
'x'
,
[
'float16'
,
'float32'
,
'float64'
],
'swish'
check_variable_and_dtype
(
x
,
'x'
,
[
'float16'
,
'float32'
,
'float64'
],
'swish'
)
)
helper
=
LayerHelper
(
'swish'
,
**
locals
())
helper
=
LayerHelper
(
'swish'
,
**
locals
())
out
=
helper
.
create_variable_for_type_inference
(
x
.
dtype
)
out
=
helper
.
create_variable_for_type_inference
(
x
.
dtype
)
helper
.
append_op
(
helper
.
append_op
(
type
=
'swish'
,
inputs
=
{
'X'
:
x
},
outputs
=
{
'Out'
:
out
},
attrs
=
{
'beta'
:
1.0
}
type
=
'swish'
,
inputs
=
{
'X'
:
x
},
outputs
=
{
'Out'
:
out
},
attrs
=
{
'beta'
:
1.0
},
)
)
return
out
return
out
...
@@ -1435,10 +1384,10 @@ def mish(x, name=None):
...
@@ -1435,10 +1384,10 @@ def mish(x, name=None):
"""
"""
if
in_dygraph_mode
():
if
in_dygraph_mode
():
return
_C_ops
.
mish
(
x
,
20
)
return
_C_ops
.
mish
(
x
,
20
)
if
_in_legacy_dygraph
()
:
else
:
return
_legacy_C_ops
.
mish
(
x
)
check_variable_and_dtype
(
x
,
'x'
,
[
'float16'
,
'float32'
,
'float64'
],
'mish'
check_variable_and_dtype
(
x
,
'x'
,
[
'float16'
,
'float32'
,
'float64'
],
'mish'
)
)
helper
=
LayerHelper
(
'mish'
,
**
locals
())
helper
=
LayerHelper
(
'mish'
,
**
locals
())
out
=
helper
.
create_variable_for_type_inference
(
x
.
dtype
)
out
=
helper
.
create_variable_for_type_inference
(
x
.
dtype
)
helper
.
append_op
(
type
=
'mish'
,
inputs
=
{
'X'
:
x
},
outputs
=
{
'Out'
:
out
})
helper
.
append_op
(
type
=
'mish'
,
inputs
=
{
'X'
:
x
},
outputs
=
{
'Out'
:
out
})
...
@@ -1474,16 +1423,15 @@ def tanhshrink(x, name=None):
...
@@ -1474,16 +1423,15 @@ def tanhshrink(x, name=None):
"""
"""
if
in_dygraph_mode
():
if
in_dygraph_mode
():
return
_C_ops
.
tanh_shrink
(
x
)
return
_C_ops
.
tanh_shrink
(
x
)
else
:
if
_in_legacy_dygraph
():
return
_legacy_C_ops
.
tanh_shrink
(
x
)
check_variable_and_dtype
(
check_variable_and_dtype
(
x
,
'x'
,
[
'float16'
,
'float32'
,
'float64'
],
'tanhshrink'
x
,
'x'
,
[
'float16'
,
'float32'
,
'float64'
],
'tanhshrink'
)
)
helper
=
LayerHelper
(
'tanh_shrink'
,
**
locals
())
helper
=
LayerHelper
(
'tanh_shrink'
,
**
locals
())
out
=
helper
.
create_variable_for_type_inference
(
x
.
dtype
)
out
=
helper
.
create_variable_for_type_inference
(
x
.
dtype
)
helper
.
append_op
(
type
=
'tanh_shrink'
,
inputs
=
{
'X'
:
x
},
outputs
=
{
'Out'
:
out
})
helper
.
append_op
(
type
=
'tanh_shrink'
,
inputs
=
{
'X'
:
x
},
outputs
=
{
'Out'
:
out
}
)
return
out
return
out
...
@@ -1525,10 +1473,7 @@ def thresholded_relu(x, threshold=1.0, name=None):
...
@@ -1525,10 +1473,7 @@ def thresholded_relu(x, threshold=1.0, name=None):
if
in_dygraph_mode
():
if
in_dygraph_mode
():
return
_C_ops
.
thresholded_relu
(
x
,
threshold
)
return
_C_ops
.
thresholded_relu
(
x
,
threshold
)
else
:
if
_in_legacy_dygraph
():
return
_legacy_C_ops
.
thresholded_relu
(
x
,
'threshold'
,
threshold
)
check_variable_and_dtype
(
check_variable_and_dtype
(
x
,
'x'
,
[
'float16'
,
'float32'
,
'float64'
],
'thresholded_relu'
x
,
'x'
,
[
'float16'
,
'float32'
,
'float64'
],
'thresholded_relu'
)
)
...
@@ -1605,12 +1550,7 @@ def log_softmax(x, axis=-1, dtype=None, name=None):
...
@@ -1605,12 +1550,7 @@ def log_softmax(x, axis=-1, dtype=None, name=None):
if
dtype
is
not
None
:
if
dtype
is
not
None
:
x
=
_C_ops
.
cast
(
x
,
dtype
)
x
=
_C_ops
.
cast
(
x
,
dtype
)
return
_C_ops
.
log_softmax
(
x
,
axis
)
return
_C_ops
.
log_softmax
(
x
,
axis
)
else
:
if
_in_legacy_dygraph
():
if
dtype
is
not
None
:
x
=
_legacy_C_ops
.
cast
(
x
,
'in_dtype'
,
x
.
dtype
,
'out_dtype'
,
dtype
)
return
_legacy_C_ops
.
log_softmax
(
x
,
'axis'
,
axis
)
if
dtype
is
None
:
if
dtype
is
None
:
check_variable_and_dtype
(
check_variable_and_dtype
(
x
,
'x'
,
[
'float16'
,
'float32'
,
'float64'
],
'log_softmax'
x
,
'x'
,
[
'float16'
,
'float32'
,
'float64'
],
'log_softmax'
...
...
python/paddle/nn/functional/common.py
浏览文件 @
140d786d
...
@@ -25,11 +25,7 @@ from ...fluid.data_feeder import (
...
@@ -25,11 +25,7 @@ from ...fluid.data_feeder import (
check_type
,
check_type
,
check_variable_and_dtype
,
check_variable_and_dtype
,
)
)
from
...fluid.framework
import
(
from
...fluid.framework
import
in_dygraph_mode
_in_legacy_dygraph
,
_non_static_mode
,
in_dygraph_mode
,
)
from
...tensor
import
clip
,
concat
,
sqrt
,
sum
from
...tensor
import
clip
,
concat
,
sqrt
,
sum
from
...tensor.creation
import
zeros
from
...tensor.creation
import
zeros
...
@@ -927,9 +923,7 @@ def bilinear(x1, x2, weight, bias=None, name=None):
...
@@ -927,9 +923,7 @@ def bilinear(x1, x2, weight, bias=None, name=None):
if
in_dygraph_mode
():
if
in_dygraph_mode
():
return
_C_ops
.
bilinear_tensor_product
(
x1
,
x2
,
weight
,
bias
)
return
_C_ops
.
bilinear_tensor_product
(
x1
,
x2
,
weight
,
bias
)
elif
_non_static_mode
():
else
:
return
_legacy_C_ops
.
bilinear_tensor_product
(
x1
,
x2
,
weight
,
bias
)
check_variable_and_dtype
(
x1
,
'x1'
,
[
'float32'
,
'float64'
],
'bilinear'
)
check_variable_and_dtype
(
x1
,
'x1'
,
[
'float32'
,
'float64'
],
'bilinear'
)
check_variable_and_dtype
(
x2
,
'x2'
,
[
'float32'
,
'float64'
],
'bilinear'
)
check_variable_and_dtype
(
x2
,
'x2'
,
[
'float32'
,
'float64'
],
'bilinear'
)
...
@@ -1118,11 +1112,10 @@ def dropout(
...
@@ -1118,11 +1112,10 @@ def dropout(
'downgrade_in_infer'
if
mode
==
'downscale_in_infer'
else
mode
'downgrade_in_infer'
if
mode
==
'downscale_in_infer'
else
mode
)
# semantic transfer
)
# semantic transfer
if
_non_static
_mode
():
if
in_dygraph
_mode
():
if
default_main_program
().
random_seed
!=
0
:
if
default_main_program
().
random_seed
!=
0
:
seed
=
default_main_program
().
random_seed
seed
=
default_main_program
().
random_seed
if
in_dygraph_mode
():
out
,
mask
=
_C_ops
.
dropout
(
out
,
mask
=
_C_ops
.
dropout
(
x
,
x
,
None
,
None
,
...
@@ -1134,21 +1127,7 @@ def dropout(
...
@@ -1134,21 +1127,7 @@ def dropout(
)
)
return
out
return
out
out
,
mask
=
_legacy_C_ops
.
dropout
(
else
:
x
,
'dropout_prob'
,
p
,
'is_test'
,
not
training
,
'fix_seed'
,
seed
is
not
None
,
'seed'
,
seed
if
seed
is
not
None
else
0
,
'dropout_implementation'
,
mode
,
)
return
out
helper
=
LayerHelper
(
'dropout'
,
**
locals
())
helper
=
LayerHelper
(
'dropout'
,
**
locals
())
check_variable_and_dtype
(
check_variable_and_dtype
(
x
,
'x'
,
[
'float16'
,
'float32'
,
'float64'
],
'dropout'
x
,
'x'
,
[
'float16'
,
'float32'
,
'float64'
],
'dropout'
...
@@ -1683,23 +1662,6 @@ def pad(x, pad, mode='constant', value=0.0, data_format="NCHW", name=None):
...
@@ -1683,23 +1662,6 @@ def pad(x, pad, mode='constant', value=0.0, data_format="NCHW", name=None):
if
isinstance
(
pad
,
Variable
):
if
isinstance
(
pad
,
Variable
):
pad
=
pad
.
numpy
().
tolist
()
pad
=
pad
.
numpy
().
tolist
()
out
=
_C_ops
.
pad3d
(
x
,
pad
,
mode
,
value
,
data_format
)
out
=
_C_ops
.
pad3d
(
x
,
pad
,
mode
,
value
,
data_format
)
else
:
if
_in_legacy_dygraph
():
if
isinstance
(
pad
,
Variable
):
pad
=
pad
.
numpy
().
tolist
()
out
=
_legacy_C_ops
.
pad3d
(
x
,
"paddings"
,
pad
,
"mode"
,
mode
,
"value"
,
value
,
"data_format"
,
data_format
,
"name"
,
name
,
)
else
:
else
:
attrs
=
{
'mode'
:
mode
,
'value'
:
value
,
'data_format'
:
data_format
}
attrs
=
{
'mode'
:
mode
,
'value'
:
value
,
'data_format'
:
data_format
}
inputs
=
{
'X'
:
[
x
]}
inputs
=
{
'X'
:
[
x
]}
...
@@ -1872,16 +1834,6 @@ def linear(x, weight, bias=None, name=None):
...
@@ -1872,16 +1834,6 @@ def linear(x, weight, bias=None, name=None):
if
in_dygraph_mode
():
if
in_dygraph_mode
():
# TODO(jiabin): using addmm for fast forward route
# TODO(jiabin): using addmm for fast forward route
return
_C_ops
.
linear
(
x
,
weight
,
bias
)
return
_C_ops
.
linear
(
x
,
weight
,
bias
)
else
:
if
_in_legacy_dygraph
():
pre_bias
=
_legacy_C_ops
.
matmul_v2
(
x
,
weight
,
'trans_x'
,
False
,
'trans_y'
,
False
)
if
bias
is
None
:
return
pre_bias
return
_legacy_C_ops
.
elementwise_add
(
pre_bias
,
bias
)
else
:
else
:
helper
=
LayerHelper
(
'linear'
,
**
locals
())
helper
=
LayerHelper
(
'linear'
,
**
locals
())
dtype
=
x
.
dtype
dtype
=
x
.
dtype
...
@@ -1889,9 +1841,7 @@ def linear(x, weight, bias=None, name=None):
...
@@ -1889,9 +1841,7 @@ def linear(x, weight, bias=None, name=None):
check_variable_and_dtype
(
check_variable_and_dtype
(
x
,
'x'
,
[
'float16'
,
'float32'
,
'float64'
],
'linear'
x
,
'x'
,
[
'float16'
,
'float32'
,
'float64'
],
'linear'
)
)
check_dtype
(
check_dtype
(
dtype
,
'dtype'
,
[
'float16'
,
'float32'
,
'float64'
],
'linear'
)
dtype
,
'dtype'
,
[
'float16'
,
'float32'
,
'float64'
],
'linear'
)
inputs
=
{
'X'
:
[
x
],
'Y'
:
[
weight
]}
inputs
=
{
'X'
:
[
x
],
'Y'
:
[
weight
]}
attrs
=
{
'trans_x'
:
False
,
'trans_y'
:
False
}
attrs
=
{
'trans_x'
:
False
,
'trans_y'
:
False
}
...
...
python/paddle/nn/functional/conv.py
浏览文件 @
140d786d
...
@@ -19,11 +19,7 @@ from paddle.device import (
...
@@ -19,11 +19,7 @@ from paddle.device import (
is_compiled_with_npu
,
is_compiled_with_npu
,
is_compiled_with_rocm
,
is_compiled_with_rocm
,
)
)
from
paddle.fluid.framework
import
(
from
paddle.fluid.framework
import
_global_flags
,
in_dygraph_mode
_global_flags
,
_in_legacy_dygraph
,
in_dygraph_mode
,
)
from
paddle.tensor.math
import
_add_with_axis
from
paddle.tensor.math
import
_add_with_axis
from
...device
import
get_cudnn_version
from
...device
import
get_cudnn_version
...
@@ -489,30 +485,6 @@ def conv1d(
...
@@ -489,30 +485,6 @@ def conv1d(
)
)
if
bias
is
not
None
:
if
bias
is
not
None
:
out
=
_add_with_axis
(
out
,
bias
,
axis
=
channel_dim
)
out
=
_add_with_axis
(
out
,
bias
,
axis
=
channel_dim
)
elif
_in_legacy_dygraph
():
attrs
=
(
'strides'
,
stride
,
'paddings'
,
padding
,
'dilations'
,
dilation
,
'groups'
,
groups
,
'use_cudnn'
,
use_cudnn
,
'use_mkldnn'
,
False
,
'fuse_relu_before_depthwise_conv'
,
False
,
"padding_algorithm"
,
padding_algorithm
,
"data_format"
,
conv2d_data_format
,
)
out
=
getattr
(
_legacy_C_ops
,
l_type
)(
x
,
weight
,
*
attrs
)
if
bias
is
not
None
:
out
=
_add_with_axis
(
out
,
bias
,
axis
=
channel_dim
)
else
:
else
:
inputs
=
{
'Input'
:
[
x
],
'Filter'
:
[
weight
]}
inputs
=
{
'Input'
:
[
x
],
'Filter'
:
[
weight
]}
attrs
=
{
attrs
=
{
...
@@ -1044,30 +1016,6 @@ def conv1d_transpose(
...
@@ -1044,30 +1016,6 @@ def conv1d_transpose(
)
)
if
bias
is
not
None
:
if
bias
is
not
None
:
out
=
_add_with_axis
(
out
,
bias
,
axis
=
channel_dim
)
out
=
_add_with_axis
(
out
,
bias
,
axis
=
channel_dim
)
elif
_in_legacy_dygraph
():
attrs
=
(
'output_padding'
,
output_padding
,
'output_size'
,
output_size
,
'strides'
,
stride
,
'paddings'
,
padding
,
'padding_algorithm'
,
padding_algorithm
,
'dilations'
,
dilation
,
'groups'
,
groups
,
'use_cudnn'
,
use_cudnn
,
'data_format'
,
conv2d_data_format
,
)
out
=
getattr
(
_legacy_C_ops
,
op_type
)(
x
,
weight
,
*
attrs
)
if
bias
is
not
None
:
out
=
_add_with_axis
(
out
,
bias
,
axis
=
channel_dim
)
else
:
else
:
inputs
=
{
'Input'
:
[
x
],
'Filter'
:
[
weight
]}
inputs
=
{
'Input'
:
[
x
],
'Filter'
:
[
weight
]}
attrs
=
{
attrs
=
{
...
@@ -1350,33 +1298,6 @@ def conv2d_transpose(
...
@@ -1350,33 +1298,6 @@ def conv2d_transpose(
return
_add_with_axis
(
pre_bias
,
bias
,
axis
=
channel_dim
)
return
_add_with_axis
(
pre_bias
,
bias
,
axis
=
channel_dim
)
else
:
else
:
return
pre_bias
return
pre_bias
if
_in_legacy_dygraph
():
attrs
=
(
'output_padding'
,
output_padding
,
'output_size'
,
output_size
,
'strides'
,
stride
,
'paddings'
,
padding
,
'padding_algorithm'
,
padding_algorithm
,
'dilations'
,
dilation
,
'groups'
,
groups
,
'use_cudnn'
,
use_cudnn
,
'data_format'
,
data_format
,
)
pre_bias
=
getattr
(
_legacy_C_ops
,
op_type
)(
x
,
weight
,
*
attrs
)
if
bias
is
not
None
:
out
=
_add_with_axis
(
pre_bias
,
bias
,
axis
=
channel_dim
)
else
:
out
=
pre_bias
else
:
else
:
inputs
=
{
'Input'
:
[
x
],
'Filter'
:
[
weight
]}
inputs
=
{
'Input'
:
[
x
],
'Filter'
:
[
weight
]}
attrs
=
{
attrs
=
{
...
@@ -1823,33 +1744,6 @@ def conv3d_transpose(
...
@@ -1823,33 +1744,6 @@ def conv3d_transpose(
return
_add_with_axis
(
pre_bias
,
bias
,
axis
=
channel_dim
)
return
_add_with_axis
(
pre_bias
,
bias
,
axis
=
channel_dim
)
else
:
else
:
return
pre_bias
return
pre_bias
if
_in_legacy_dygraph
():
attrs
=
(
'output_padding'
,
output_padding
,
'output_size'
,
output_size
,
'paddings'
,
padding
,
"padding_algorithm"
,
padding_algorithm
,
'strides'
,
stride
,
'dilations'
,
dilation
,
'groups'
,
groups
,
'use_cudnn'
,
use_cudnn
,
"data_format"
,
data_format_
,
)
pre_bias
=
getattr
(
_legacy_C_ops
,
op_type
)(
x
,
weight
,
*
attrs
)
if
bias
is
not
None
:
out
=
_add_with_axis
(
pre_bias
,
bias
,
axis
=
channel_dim
)
else
:
out
=
pre_bias
else
:
else
:
inputs
=
{
'Input'
:
[
x
],
'Filter'
:
[
weight
]}
inputs
=
{
'Input'
:
[
x
],
'Filter'
:
[
weight
]}
attrs
=
{
attrs
=
{
...
...
python/paddle/nn/functional/distance.py
浏览文件 @
140d786d
...
@@ -13,8 +13,8 @@
...
@@ -13,8 +13,8 @@
# limitations under the License.
# limitations under the License.
import
paddle
import
paddle
from
paddle
import
_C_ops
,
_legacy_C_ops
from
paddle
import
_C_ops
from
paddle.fluid.framework
import
_in_legacy_dygraph
,
in_dygraph_mode
from
paddle.fluid.framework
import
in_dygraph_mode
from
...fluid.data_feeder
import
check_type
,
check_variable_and_dtype
from
...fluid.data_feeder
import
check_type
,
check_variable_and_dtype
from
...fluid.layer_helper
import
LayerHelper
from
...fluid.layer_helper
import
LayerHelper
...
@@ -81,19 +81,13 @@ def pairwise_distance(x, y, p=2.0, epsilon=1e-6, keepdim=False, name=None):
...
@@ -81,19 +81,13 @@ def pairwise_distance(x, y, p=2.0, epsilon=1e-6, keepdim=False, name=None):
sub
=
_C_ops
.
add
(
sub
,
epsilon
)
sub
=
_C_ops
.
add
(
sub
,
epsilon
)
return
_C_ops
.
p_norm
(
sub
,
p
,
-
1
,
0.0
,
keepdim
,
False
)
return
_C_ops
.
p_norm
(
sub
,
p
,
-
1
,
0.0
,
keepdim
,
False
)
if
_in_legacy_dygraph
():
else
:
sub
=
_legacy_C_ops
.
elementwise_sub
(
x
,
y
)
check_variable_and_dtype
(
if
epsilon
!=
0.0
:
x
,
'x'
,
[
'float32'
,
'float64'
],
'PairwiseDistance'
epsilon
=
paddle
.
fluid
.
dygraph
.
base
.
to_variable
(
[
epsilon
],
dtype
=
sub
.
dtype
)
)
sub
=
_legacy_C_ops
.
elementwise_add
(
sub
,
epsilon
)
check_variable_and_dtype
(
return
_legacy_C_ops
.
p_norm
(
y
,
'y'
,
[
'float32'
,
'float64'
],
'PairwiseDistance'
sub
,
'axis'
,
-
1
,
'porder'
,
p
,
'keepdim'
,
keepdim
,
'epsilon'
,
0.0
)
)
check_variable_and_dtype
(
x
,
'x'
,
[
'float32'
,
'float64'
],
'PairwiseDistance'
)
check_variable_and_dtype
(
y
,
'y'
,
[
'float32'
,
'float64'
],
'PairwiseDistance'
)
sub
=
paddle
.
subtract
(
x
,
y
)
sub
=
paddle
.
subtract
(
x
,
y
)
if
epsilon
!=
0.0
:
if
epsilon
!=
0.0
:
epsilon_var
=
sub
.
block
.
create_var
(
dtype
=
sub
.
dtype
)
epsilon_var
=
sub
.
block
.
create_var
(
dtype
=
sub
.
dtype
)
...
...
python/paddle/nn/layer/norm.py
浏览文件 @
140d786d
...
@@ -34,17 +34,11 @@ import numpy as np
...
@@ -34,17 +34,11 @@ import numpy as np
from
paddle
import
_C_ops
,
_legacy_C_ops
,
in_dynamic_mode
from
paddle
import
_C_ops
,
_legacy_C_ops
,
in_dynamic_mode
from
paddle.device
import
get_all_custom_device_type
from
paddle.device
import
get_all_custom_device_type
from
paddle.fluid.framework
import
_in_legacy_dygraph
,
in_dygraph_mode
from
paddle.fluid.framework
import
in_dygraph_mode
from
...fluid
import
dygraph_utils
from
...fluid
import
dygraph_utils
from
...fluid.data_feeder
import
check_variable_and_dtype
from
...fluid.data_feeder
import
check_variable_and_dtype
from
...framework
import
(
from
...framework
import
ParamAttr
,
_global_flags
,
get_default_dtype
,
no_grad
ParamAttr
,
_global_flags
,
_non_static_mode
,
get_default_dtype
,
no_grad
,
)
from
..
import
Layer
from
..
import
Layer
from
..
import
functional
as
F
from
..
import
functional
as
F
from
..functional
import
batch_norm
,
instance_norm
,
layer_norm
from
..functional
import
batch_norm
,
instance_norm
,
layer_norm
...
@@ -492,20 +486,6 @@ class GroupNorm(Layer):
...
@@ -492,20 +486,6 @@ class GroupNorm(Layer):
dtype
=
input
.
dtype
,
stop_gradient
=
True
dtype
=
input
.
dtype
,
stop_gradient
=
True
)
)
if
_in_legacy_dygraph
():
pre_act
,
_
,
_
=
_legacy_C_ops
.
group_norm
(
input
,
self
.
weight
,
self
.
bias
,
mean_out
,
variance_out
,
'epsilon'
,
self
.
_epsilon
,
'groups'
,
self
.
_num_groups
,
)
return
pre_act
inputs
=
{
'X'
:
input
}
inputs
=
{
'X'
:
input
}
if
self
.
bias
is
not
None
:
if
self
.
bias
is
not
None
:
inputs
[
'Bias'
]
=
self
.
bias
inputs
[
'Bias'
]
=
self
.
bias
...
@@ -1005,13 +985,6 @@ class BatchNorm(Layer):
...
@@ -1005,13 +985,6 @@ class BatchNorm(Layer):
self
.
_trainable_statistics
=
trainable_statistics
self
.
_trainable_statistics
=
trainable_statistics
def
forward
(
self
,
input
):
def
forward
(
self
,
input
):
# create output
# mean and mean_out share the same memory
mean_out
=
self
.
_mean
# variance and variance out share the same memory
variance_out
=
self
.
_variance
if
_non_static_mode
():
if
in_dygraph_mode
():
if
in_dygraph_mode
():
batch_norm_out
,
t1
,
t2
,
t3
,
t4
,
_
=
_C_ops
.
batch_norm
(
batch_norm_out
,
t1
,
t2
,
t3
,
t4
,
_
=
_C_ops
.
batch_norm
(
input
,
input
,
...
@@ -1029,42 +1002,12 @@ class BatchNorm(Layer):
...
@@ -1029,42 +1002,12 @@ class BatchNorm(Layer):
return
dygraph_utils
.
_append_activation_in_dygraph
(
return
dygraph_utils
.
_append_activation_in_dygraph
(
batch_norm_out
,
act
=
self
.
_act
,
use_mkldnn
=
self
.
_use_mkldnn
batch_norm_out
,
act
=
self
.
_act
,
use_mkldnn
=
self
.
_use_mkldnn
)
)
else
:
elif
_in_legacy_dygraph
():
# create output
attrs
=
(
# mean and mean_out share the same memory
"momentum"
,
mean_out
=
self
.
_mean
self
.
_momentum
,
# variance and variance out share the same memory
"epsilon"
,
variance_out
=
self
.
_variance
self
.
_epsilon
,
"is_test"
,
not
self
.
training
,
"data_layout"
,
self
.
_data_layout
,
"use_mkldnn"
,
self
.
_use_mkldnn
,
"fuse_with_relu"
,
self
.
_fuse_with_relu
,
"use_global_stats"
,
self
.
_use_global_stats
,
'trainable_statistics'
,
self
.
_trainable_statistics
,
)
batch_norm_out
,
_
,
_
,
_
,
_
,
_
=
_legacy_C_ops
.
batch_norm
(
input
,
self
.
weight
,
self
.
bias
,
self
.
_mean
,
self
.
_variance
,
None
,
mean_out
,
variance_out
,
*
attrs
)
return
dygraph_utils
.
_append_activation_in_dygraph
(
batch_norm_out
,
act
=
self
.
_act
,
use_mkldnn
=
self
.
_use_mkldnn
)
check_variable_and_dtype
(
check_variable_and_dtype
(
input
,
'input'
,
[
'float16'
,
'float32'
,
'float64'
],
'BatchNorm'
input
,
'input'
,
[
'float16'
,
'float32'
,
'float64'
],
'BatchNorm'
)
)
...
@@ -1101,7 +1044,9 @@ class BatchNorm(Layer):
...
@@ -1101,7 +1044,9 @@ class BatchNorm(Layer):
batch_norm_out
=
(
batch_norm_out
=
(
input
input
if
self
.
_in_place
if
self
.
_in_place
else
self
.
_helper
.
create_variable_for_type_inference
(
self
.
_dtype
)
else
self
.
_helper
.
create_variable_for_type_inference
(
self
.
_dtype
)
)
)
outputs
=
{
outputs
=
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录