Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
0754e09d
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看板
未验证
提交
0754e09d
编写于
12月 02, 2022
作者:
H
heyanru
提交者:
GitHub
12月 02, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[Fluid Clean] remove paddle.fluid.layers.nn.reduce_all,reduce_any (#48269)
上级
a0b91c7b
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
15 addition
and
150 deletion
+15
-150
python/paddle/fluid/contrib/mixed_precision/decorator.py
python/paddle/fluid/contrib/mixed_precision/decorator.py
+1
-1
python/paddle/fluid/layers/nn.py
python/paddle/fluid/layers/nn.py
+0
-133
python/paddle/fluid/layers/rnn.py
python/paddle/fluid/layers/rnn.py
+5
-5
python/paddle/fluid/tests/unittests/dygraph_to_static/transformer_dygraph_model.py
.../unittests/dygraph_to_static/transformer_dygraph_model.py
+1
-1
python/paddle/fluid/tests/unittests/ipu/test_reduce_x_op_ipu.py
.../paddle/fluid/tests/unittests/ipu/test_reduce_x_op_ipu.py
+2
-2
python/paddle/fluid/tests/unittests/test_reduce_op.py
python/paddle/fluid/tests/unittests/test_reduce_op.py
+4
-4
python/paddle/jit/dy2static/convert_operators.py
python/paddle/jit/dy2static/convert_operators.py
+2
-4
未找到文件。
python/paddle/fluid/contrib/mixed_precision/decorator.py
浏览文件 @
0754e09d
...
@@ -460,7 +460,7 @@ class OptimizerWithMixedPrecision:
...
@@ -460,7 +460,7 @@ class OptimizerWithMixedPrecision:
if
self
.
_is_distributed
or
self
.
_use_pure_fp16
:
if
self
.
_is_distributed
or
self
.
_use_pure_fp16
:
with
self
.
_train_program
.
_optimized_guard
([]):
with
self
.
_train_program
.
_optimized_guard
([]):
all_infs
=
layers
.
concat
(
found_infs
)
all_infs
=
layers
.
concat
(
found_infs
)
found_inf
=
layers
.
reduce_
any
(
all_infs
)
found_inf
=
paddle
.
any
(
all_infs
)
return
found_inf
return
found_inf
...
...
python/paddle/fluid/layers/nn.py
浏览文件 @
0754e09d
...
@@ -71,8 +71,6 @@ __all__ = [
...
@@ -71,8 +71,6 @@ __all__ = [
'softmax'
,
'softmax'
,
'pool2d'
,
'pool2d'
,
'batch_norm'
,
'batch_norm'
,
'reduce_all'
,
'reduce_any'
,
'dropout'
,
'dropout'
,
'split'
,
'split'
,
'ctc_greedy_decoder'
,
'ctc_greedy_decoder'
,
...
@@ -2504,137 +2502,6 @@ def reduce_sum(input, dim=None, keep_dim=False, name=None):
...
@@ -2504,137 +2502,6 @@ def reduce_sum(input, dim=None, keep_dim=False, name=None):
return
out
return
out
def
reduce_all
(
input
,
dim
=
None
,
keep_dim
=
False
,
name
=
None
):
"""
This OP computes the ``logical and`` of tensor elements over the given dimension, and output the result.
Args:
input (Tensor): the input tensor, it's data type should be `bool`.
dim (list|int|optional): The dimension along which the logical and is computed.
If :attr:`None`, compute the logical and over all elements of
:attr:`input` and return a Tensor variable with a single element,
otherwise must be in the range :math:`[-rank(input), rank(input))`.
If :math:`dim[i] < 0`, the dimension to reduce is :math:`rank + dim[i]`. The default value is None.
keep_dim (bool): Whether to reserve the reduced dimension in the
output Tensor. The result tensor will have one fewer dimension
than the :attr:`input` unless :attr:`keep_dim` is true. The default value is False.
name(str|None): A name for this layer(optional). If set None, the layer
will be named automatically. The default value is None.
Returns:
Tensor, the output data type is bool. : The reduced tensor variable with ``logical and`` in given dims.
Examples:
.. code-block:: python
import paddle
import paddle.fluid as fluid
import paddle.fluid.layers as layers
import numpy as np
# x is a bool Tensor variable with following elements:
# [[True, False]
# [True, True]]
x = fluid.layers.assign(np.array([[1, 0], [1, 1]], dtype='int32'))
x = fluid.layers.cast(x, 'bool')
out = fluid.layers.reduce_all(x) # False
out = fluid.layers.reduce_all(x, dim=0) # [True, False]
out = fluid.layers.reduce_all(x, dim=-1) # [False, True]
# keep_dim=False, x.shape=(2,2), out.shape=(2,)
out = fluid.layers.reduce_all(x, dim=1, keep_dim=True) # [[False], [True]]
# keep_dim=True, x.shape=(2,2), out.shape=(2,1)
"""
if
dim
is
not
None
and
not
isinstance
(
dim
,
list
):
dim
=
[
dim
]
if
in_dygraph_mode
():
return
_C_ops
.
all
(
input
,
dim
if
dim
is
not
None
else
[],
keep_dim
)
check_variable_and_dtype
(
input
,
'input'
,
(
'bool'
),
'reduce_all'
)
helper
=
LayerHelper
(
'reduce_all'
,
**
locals
())
out
=
helper
.
create_variable_for_type_inference
(
dtype
=
helper
.
input_dtype
())
helper
.
append_op
(
type
=
'reduce_all'
,
inputs
=
{
'X'
:
input
},
outputs
=
{
'Out'
:
out
},
attrs
=
{
'dim'
:
dim
if
dim
is
not
None
and
dim
!=
[]
else
[
0
],
'keep_dim'
:
keep_dim
,
'reduce_all'
:
True
if
dim
is
None
or
dim
==
[]
or
len
(
dim
)
==
len
(
input
.
shape
)
else
False
,
},
)
return
out
def
reduce_any
(
input
,
dim
=
None
,
keep_dim
=
False
,
name
=
None
):
"""
This OP computes the ``logical or`` of tensor elements over the given dimension, and output the result.
Args:
input (Tensor): the input tensor, it's data type should be `bool`.
dim (list|int|optional): The dimension along which the logical and is computed.
If :attr:`None`, compute the logical and over all elements of
:attr:`input` and return a Tensor variable with a single element,
otherwise must be in the range :math:`[-rank(input), rank(input))`.
If :math:`dim[i] < 0`, the dimension to reduce is :math:`rank + dim[i]`. The default value is None.
keep_dim (bool): Whether to reserve the reduced dimension in the
output Tensor. The result tensor will have one fewer dimension
than the :attr:`input` unless :attr:`keep_dim` is true. The default value is False.
name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.
Returns:
Tensor, the output data type is bool. : The reduced tensor variable with ``logical or`` in given dims.
Examples:
.. code-block:: python
import paddle
import paddle.fluid as fluid
import paddle.fluid.layers as layers
import numpy as np
# x is a bool Tensor variable with following elements:
# [[True, False]
# [False, False]]
x = fluid.layers.assign(np.array([[1, 0], [0, 0]], dtype='int32'))
x = fluid.layers.cast(x, 'bool')
out = fluid.layers.reduce_any(x) # True
out = fluid.layers.reduce_any(x, dim=0) # [True, False]
out = fluid.layers.reduce_any(x, dim=-1) # [True, False]
# keep_dim=False, x.shape=(2,2), out.shape=(2,)
out = fluid.layers.reduce_any(x, dim=1,
keep_dim=True) # [[True], [False]]
# keep_dim=True, x.shape=(2,2), out.shape=(2,1)
"""
check_variable_and_dtype
(
input
,
'input'
,
(
'bool'
),
'reduce_any'
)
helper
=
LayerHelper
(
'reduce_any'
,
**
locals
())
out
=
helper
.
create_variable_for_type_inference
(
dtype
=
helper
.
input_dtype
())
if
dim
is
not
None
and
not
isinstance
(
dim
,
list
):
dim
=
[
dim
]
helper
.
append_op
(
type
=
'reduce_any'
,
inputs
=
{
'X'
:
input
},
outputs
=
{
'Out'
:
out
},
attrs
=
{
'dim'
:
dim
if
dim
is
not
None
and
dim
!=
[]
else
[
0
],
'keep_dim'
:
keep_dim
,
'reduce_all'
:
True
if
dim
is
None
or
dim
==
[]
or
len
(
dim
)
==
len
(
input
.
shape
)
else
False
,
},
)
return
out
def
split
(
input
,
num_or_sections
,
dim
=-
1
,
name
=
None
):
def
split
(
input
,
num_or_sections
,
dim
=-
1
,
name
=
None
):
"""
"""
Split the input tensor into multiple sub-Tensors.
Split the input tensor into multiple sub-Tensors.
...
...
python/paddle/fluid/layers/rnn.py
浏览文件 @
0754e09d
...
@@ -1481,7 +1481,7 @@ def _dynamic_decode_imperative(
...
@@ -1481,7 +1481,7 @@ def _dynamic_decode_imperative(
initial_states
,
initial_states
,
initial_finished
,
initial_finished
,
)
)
cond
=
paddle
.
logical_not
((
nn
.
reduce_
all
(
initial_finished
)))
cond
=
paddle
.
logical_not
((
paddle
.
all
(
initial_finished
)))
sequence_lengths
=
tensor
.
cast
(
paddle
.
zeros_like
(
initial_finished
),
"int64"
)
sequence_lengths
=
tensor
.
cast
(
paddle
.
zeros_like
(
initial_finished
),
"int64"
)
outputs
=
None
outputs
=
None
...
@@ -1539,7 +1539,7 @@ def _dynamic_decode_imperative(
...
@@ -1539,7 +1539,7 @@ def _dynamic_decode_imperative(
control_flow
.
increment
(
x
=
step_idx_tensor
,
value
=
1.0
,
in_place
=
True
)
control_flow
.
increment
(
x
=
step_idx_tensor
,
value
=
1.0
,
in_place
=
True
)
step_idx
+=
1
step_idx
+=
1
cond
=
paddle
.
logical_not
(
nn
.
reduce_
all
(
finished
))
cond
=
paddle
.
logical_not
(
paddle
.
all
(
finished
))
if
max_step_num
is
not
None
and
step_idx
>
max_step_num
:
if
max_step_num
is
not
None
and
step_idx
>
max_step_num
:
break
break
...
@@ -1589,7 +1589,7 @@ def _dynamic_decode_declarative(
...
@@ -1589,7 +1589,7 @@ def _dynamic_decode_declarative(
global_finished
.
stop_gradient
=
True
global_finished
.
stop_gradient
=
True
step_idx
=
tensor
.
fill_constant
(
shape
=
[
1
],
dtype
=
"int64"
,
value
=
0
)
step_idx
=
tensor
.
fill_constant
(
shape
=
[
1
],
dtype
=
"int64"
,
value
=
0
)
cond
=
paddle
.
logical_not
((
nn
.
reduce_
all
(
initial_finished
)))
cond
=
paddle
.
logical_not
((
paddle
.
all
(
initial_finished
)))
if
max_step_num
is
not
None
:
if
max_step_num
is
not
None
:
max_step_num
=
tensor
.
fill_constant
(
max_step_num
=
tensor
.
fill_constant
(
shape
=
[
1
],
dtype
=
"int64"
,
value
=
max_step_num
shape
=
[
1
],
dtype
=
"int64"
,
value
=
max_step_num
...
@@ -1720,12 +1720,12 @@ def _dynamic_decode_declarative(
...
@@ -1720,12 +1720,12 @@ def _dynamic_decode_declarative(
)
)
if
max_step_num
is
not
None
:
if
max_step_num
is
not
None
:
paddle
.
logical_and
(
paddle
.
logical_and
(
paddle
.
logical_not
(
nn
.
reduce_
all
(
global_finished
)),
paddle
.
logical_not
(
paddle
.
all
(
global_finished
)),
paddle
.
less_equal
(
step_idx
,
max_step_num
),
paddle
.
less_equal
(
step_idx
,
max_step_num
),
cond
,
cond
,
)
)
else
:
else
:
paddle
.
logical_not
(
nn
.
reduce_
all
(
global_finished
),
cond
)
paddle
.
logical_not
(
paddle
.
all
(
global_finished
),
cond
)
final_outputs
=
map_structure
(
final_outputs
=
map_structure
(
lambda
array
:
tensor
.
tensor_array_to_tensor
(
lambda
array
:
tensor
.
tensor_array_to_tensor
(
...
...
python/paddle/fluid/tests/unittests/dygraph_to_static/transformer_dygraph_model.py
浏览文件 @
0754e09d
...
@@ -873,7 +873,7 @@ class Transformer(Layer):
...
@@ -873,7 +873,7 @@ class Transformer(Layer):
predict_ids
.
append
(
token_indices
)
predict_ids
.
append
(
token_indices
)
parent_ids
.
append
(
beam_indices
)
parent_ids
.
append
(
beam_indices
)
if
layers
.
reduce_
all
(
finished
).
numpy
():
if
paddle
.
all
(
finished
).
numpy
():
break
break
predict_ids
=
paddle
.
stack
(
predict_ids
,
axis
=
0
)
predict_ids
=
paddle
.
stack
(
predict_ids
,
axis
=
0
)
...
...
python/paddle/fluid/tests/unittests/ipu/test_reduce_x_op_ipu.py
浏览文件 @
0754e09d
...
@@ -180,12 +180,12 @@ class TestAll(TestMean):
...
@@ -180,12 +180,12 @@ class TestAll(TestMean):
self
.
fetch_list
=
[
out
.
name
]
self
.
fetch_list
=
[
out
.
name
]
def
set_test_op
(
self
):
def
set_test_op
(
self
):
self
.
op
=
paddle
.
fluid
.
layers
.
reduce_
all
self
.
op
=
paddle
.
all
class
TestAny
(
TestAll
):
class
TestAny
(
TestAll
):
def
set_test_op
(
self
):
def
set_test_op
(
self
):
self
.
op
=
paddle
.
fluid
.
layers
.
reduce_
any
self
.
op
=
paddle
.
any
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
...
...
python/paddle/fluid/tests/unittests/test_reduce_op.py
浏览文件 @
0754e09d
...
@@ -505,12 +505,12 @@ class TestAllOpError(unittest.TestCase):
...
@@ -505,12 +505,12 @@ class TestAllOpError(unittest.TestCase):
with
program_guard
(
Program
(),
Program
()):
with
program_guard
(
Program
(),
Program
()):
# The input type of reduce_all_op must be Variable.
# The input type of reduce_all_op must be Variable.
input1
=
12
input1
=
12
self
.
assertRaises
(
TypeError
,
fluid
.
layers
.
reduce_
all
,
input1
)
self
.
assertRaises
(
TypeError
,
paddle
.
all
,
input1
)
# The input dtype of reduce_all_op must be bool.
# The input dtype of reduce_all_op must be bool.
input2
=
fluid
.
layers
.
data
(
input2
=
fluid
.
layers
.
data
(
name
=
'input2'
,
shape
=
[
12
,
10
],
dtype
=
"int32"
name
=
'input2'
,
shape
=
[
12
,
10
],
dtype
=
"int32"
)
)
self
.
assertRaises
(
TypeError
,
fluid
.
layers
.
reduce_
all
,
input2
)
self
.
assertRaises
(
TypeError
,
paddle
.
all
,
input2
)
class
TestAnyOp
(
OpTest
):
class
TestAnyOp
(
OpTest
):
...
@@ -622,12 +622,12 @@ class TestAnyOpError(unittest.TestCase):
...
@@ -622,12 +622,12 @@ class TestAnyOpError(unittest.TestCase):
with
program_guard
(
Program
(),
Program
()):
with
program_guard
(
Program
(),
Program
()):
# The input type of reduce_any_op must be Variable.
# The input type of reduce_any_op must be Variable.
input1
=
12
input1
=
12
self
.
assertRaises
(
TypeError
,
fluid
.
layers
.
reduce_
any
,
input1
)
self
.
assertRaises
(
TypeError
,
paddle
.
any
,
input1
)
# The input dtype of reduce_any_op must be bool.
# The input dtype of reduce_any_op must be bool.
input2
=
fluid
.
layers
.
data
(
input2
=
fluid
.
layers
.
data
(
name
=
'input2'
,
shape
=
[
12
,
10
],
dtype
=
"int32"
name
=
'input2'
,
shape
=
[
12
,
10
],
dtype
=
"int32"
)
)
self
.
assertRaises
(
TypeError
,
fluid
.
layers
.
reduce_
any
,
input2
)
self
.
assertRaises
(
TypeError
,
paddle
.
any
,
input2
)
class
Test1DReduce
(
OpTest
):
class
Test1DReduce
(
OpTest
):
...
...
python/paddle/jit/dy2static/convert_operators.py
浏览文件 @
0754e09d
...
@@ -27,8 +27,6 @@ from paddle.fluid.layers import (
...
@@ -27,8 +27,6 @@ from paddle.fluid.layers import (
from
paddle.fluid.layers
import
(
from
paddle.fluid.layers
import
(
assign
,
assign
,
fill_constant
,
fill_constant
,
reduce_all
,
reduce_any
,
)
)
from
paddle.fluid.layers
import
(
from
paddle.fluid.layers
import
(
cast
,
cast
,
...
@@ -651,7 +649,7 @@ def convert_shape_compare(left, *args):
...
@@ -651,7 +649,7 @@ def convert_shape_compare(left, *args):
def
reduce_compare
(
x
,
op_str
,
y
):
def
reduce_compare
(
x
,
op_str
,
y
):
element_wise_result
=
eval
(
"x "
+
op_str
+
" y"
)
element_wise_result
=
eval
(
"x "
+
op_str
+
" y"
)
if
op_str
==
"!="
:
if
op_str
==
"!="
:
return
reduce_
any
(
element_wise_result
)
return
paddle
.
any
(
element_wise_result
)
elif
(
elif
(
op_str
==
"is"
op_str
==
"is"
or
op_str
==
"is not"
or
op_str
==
"is not"
...
@@ -660,7 +658,7 @@ def convert_shape_compare(left, *args):
...
@@ -660,7 +658,7 @@ def convert_shape_compare(left, *args):
):
):
return
element_wise_result
return
element_wise_result
else
:
else
:
return
reduce_
all
(
element_wise_result
)
return
paddle
.
all
(
element_wise_result
)
final_result
=
reduce_compare
(
left
,
args
[
0
],
args
[
1
])
final_result
=
reduce_compare
(
left
,
args
[
0
],
args
[
1
])
for
i
in
range
(
1
,
num_cmp
):
for
i
in
range
(
1
,
num_cmp
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录