Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
14b91f60
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看板
未验证
提交
14b91f60
编写于
4月 02, 2022
作者:
H
hong
提交者:
GitHub
4月 02, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add topk cast (#41304)
上级
7dd4a9fe
变更
9
隐藏空白更改
内联
并排
Showing
9 changed file
with
51 addition
and
5 deletion
+51
-5
python/paddle/fluid/layers/nn.py
python/paddle/fluid/layers/nn.py
+7
-1
python/paddle/fluid/layers/tensor.py
python/paddle/fluid/layers/tensor.py
+6
-1
python/paddle/fluid/tests/unittests/op_test.py
python/paddle/fluid/tests/unittests/op_test.py
+2
-0
python/paddle/fluid/tests/unittests/test_cast_op.py
python/paddle/fluid/tests/unittests/test_cast_op.py
+15
-0
python/paddle/fluid/tests/unittests/test_reduce_op.py
python/paddle/fluid/tests/unittests/test_reduce_op.py
+1
-0
python/paddle/fluid/tests/unittests/test_top_k_v2_op.py
python/paddle/fluid/tests/unittests/test_top_k_v2_op.py
+2
-2
python/paddle/tensor/search.py
python/paddle/tensor/search.py
+6
-0
python/paddle/utils/code_gen/api.yaml
python/paddle/utils/code_gen/api.yaml
+2
-1
python/paddle/utils/code_gen/backward.yaml
python/paddle/utils/code_gen/backward.yaml
+10
-0
未找到文件。
python/paddle/fluid/layers/nn.py
浏览文件 @
14b91f60
...
@@ -4791,7 +4791,7 @@ def reduce_prod(input, dim=None, keep_dim=False, name=None):
...
@@ -4791,7 +4791,7 @@ def reduce_prod(input, dim=None, keep_dim=False, name=None):
fluid.layers.reduce_prod(y, dim=[1, 2]) # [24.0, 1680.0]
fluid.layers.reduce_prod(y, dim=[1, 2]) # [24.0, 1680.0]
fluid.layers.reduce_prod(y, dim=[0, 1]) # [105.0, 384.0]
fluid.layers.reduce_prod(y, dim=[0, 1]) # [105.0, 384.0]
"""
"""
helper = LayerHelper('reduce_prod', **locals())
if dim is not None and not isinstance(dim, list):
if dim is not None and not isinstance(dim, list):
if isinstance(dim, tuple):
if isinstance(dim, tuple):
dim = list(dim)
dim = list(dim)
...
@@ -4801,6 +4801,12 @@ def reduce_prod(input, dim=None, keep_dim=False, name=None):
...
@@ -4801,6 +4801,12 @@ def reduce_prod(input, dim=None, keep_dim=False, name=None):
raise TypeError(
raise TypeError(
"The type of axis must be int, list or tuple, but received {}".
"The type of axis must be int, list or tuple, but received {}".
format(type(dim)))
format(type(dim)))
if in_dygraph_mode():
return _C_ops.final_state_reduce_prod(
input, dim if dim != None and dim != [] else [0], keep_dim, True if
dim == None or dim == [] or len(dim) == len(input.shape) else False)
helper = LayerHelper('reduce_prod', **locals())
check_variable_and_dtype(
check_variable_and_dtype(
input, 'input', ['float32', 'float64', 'int32', 'int64'], 'reduce_prod')
input, 'input', ['float32', 'float64', 'int32', 'int64'], 'reduce_prod')
out = helper.create_variable_for_type_inference(dtype=helper.input_dtype())
out = helper.create_variable_for_type_inference(dtype=helper.input_dtype())
...
...
python/paddle/fluid/layers/tensor.py
浏览文件 @
14b91f60
...
@@ -21,7 +21,7 @@ import warnings
...
@@ -21,7 +21,7 @@ import warnings
from
..layer_helper
import
LayerHelper
from
..layer_helper
import
LayerHelper
from
..param_attr
import
ParamAttr
from
..param_attr
import
ParamAttr
from
..initializer
import
Initializer
from
..initializer
import
Initializer
from
..framework
import
convert_np_dtype_to_dtype_
,
_non_static_mode
,
_varbase_creator
,
device_guard
,
_in_legacy_dygraph
from
..framework
import
convert_np_dtype_to_dtype_
,
_non_static_mode
,
_varbase_creator
,
device_guard
,
_in_legacy_dygraph
,
in_dygraph_mode
from
..framework
import
Variable
from
..framework
import
Variable
from
..initializer
import
Constant
from
..initializer
import
Constant
from
..core
import
VarDesc
from
..core
import
VarDesc
...
@@ -243,6 +243,11 @@ def cast(x, dtype):
...
@@ -243,6 +243,11 @@ def cast(x, dtype):
x = paddle.to_tensor([2, 3, 4], 'float64')
x = paddle.to_tensor([2, 3, 4], 'float64')
y = paddle.cast(x, 'uint8')
y = paddle.cast(x, 'uint8')
"""
"""
if
in_dygraph_mode
():
if
not
isinstance
(
dtype
,
core
.
VarDesc
.
VarType
):
dtype
=
convert_np_dtype_to_dtype_
(
dtype
)
return
_C_ops
.
final_state_cast
(
x
,
dtype
)
if
_non_static_mode
():
if
_non_static_mode
():
if
not
isinstance
(
dtype
,
core
.
VarDesc
.
VarType
):
if
not
isinstance
(
dtype
,
core
.
VarDesc
.
VarType
):
dtype
=
convert_np_dtype_to_dtype_
(
dtype
)
dtype
=
convert_np_dtype_to_dtype_
(
dtype
)
...
...
python/paddle/fluid/tests/unittests/op_test.py
浏览文件 @
14b91f60
...
@@ -1559,6 +1559,8 @@ class OpTest(unittest.TestCase):
...
@@ -1559,6 +1559,8 @@ class OpTest(unittest.TestCase):
def
_compare_numpy
(
self
,
name
,
actual_np
,
expect_np
):
def
_compare_numpy
(
self
,
name
,
actual_np
,
expect_np
):
with
_test_eager_guard
():
with
_test_eager_guard
():
print
(
actual_np
)
print
(
expect_np
)
super
().
_compare_numpy
(
name
,
actual_np
,
expect_np
)
super
().
_compare_numpy
(
name
,
actual_np
,
expect_np
)
def
convert_uint16_to_float_ifneed
(
self
,
actual_np
,
expect_np
):
def
convert_uint16_to_float_ifneed
(
self
,
actual_np
,
expect_np
):
...
...
python/paddle/fluid/tests/unittests/test_cast_op.py
浏览文件 @
14b91f60
...
@@ -22,6 +22,7 @@ import paddle.fluid.core as core
...
@@ -22,6 +22,7 @@ import paddle.fluid.core as core
import
paddle.fluid
as
fluid
import
paddle.fluid
as
fluid
from
paddle.fluid
import
compiler
,
Program
,
program_guard
from
paddle.fluid
import
compiler
,
Program
,
program_guard
from
op_test
import
OpTest
,
convert_uint16_to_float
,
convert_float_to_uint16
from
op_test
import
OpTest
,
convert_uint16_to_float
,
convert_float_to_uint16
from
paddle.fluid.framework
import
_test_eager_guard
class
TestCastOpFp32ToFp64
(
OpTest
):
class
TestCastOpFp32ToFp64
(
OpTest
):
...
@@ -115,6 +116,20 @@ class TestCastOpError(unittest.TestCase):
...
@@ -115,6 +116,20 @@ class TestCastOpError(unittest.TestCase):
self
.
assertRaises
(
TypeError
,
fluid
.
layers
.
cast
,
x1
,
'int32'
)
self
.
assertRaises
(
TypeError
,
fluid
.
layers
.
cast
,
x1
,
'int32'
)
class
TestCastOpEager
(
unittest
.
TestCase
):
def
test_eager
(
self
):
with
paddle
.
fluid
.
dygraph
.
base
.
guard
():
with
_test_eager_guard
():
x
=
paddle
.
ones
([
2
,
2
],
dtype
=
"float16"
)
x
.
stop_gradient
=
False
out
=
paddle
.
cast
(
x
,
"float32"
)
self
.
assertTrue
(
np
.
array_equal
(
out
,
np
.
ones
([
2
,
2
]).
astype
(
"float32"
)))
out
.
backward
()
self
.
assertTrue
(
np
.
array_equal
(
x
.
gradient
(),
x
.
numpy
()))
self
.
assertTrue
(
x
.
gradient
().
dtype
==
np
.
float16
)
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
paddle
.
enable_static
()
paddle
.
enable_static
()
unittest
.
main
()
unittest
.
main
()
python/paddle/fluid/tests/unittests/test_reduce_op.py
浏览文件 @
14b91f60
...
@@ -241,6 +241,7 @@ class TestMin8DOp(OpTest):
...
@@ -241,6 +241,7 @@ class TestMin8DOp(OpTest):
class
TestProdOp
(
OpTest
):
class
TestProdOp
(
OpTest
):
def
setUp
(
self
):
def
setUp
(
self
):
self
.
op_type
=
"reduce_prod"
self
.
op_type
=
"reduce_prod"
self
.
python_api
=
paddle
.
prod
self
.
init_data_type
()
self
.
init_data_type
()
self
.
inputs
=
{
'X'
:
np
.
random
.
random
((
5
,
6
,
10
)).
astype
(
self
.
data_type
)}
self
.
inputs
=
{
'X'
:
np
.
random
.
random
((
5
,
6
,
10
)).
astype
(
self
.
data_type
)}
self
.
outputs
=
{
'Out'
:
self
.
inputs
[
'X'
].
prod
(
axis
=
0
)}
self
.
outputs
=
{
'Out'
:
self
.
inputs
[
'X'
].
prod
(
axis
=
0
)}
...
...
python/paddle/fluid/tests/unittests/test_top_k_v2_op.py
浏览文件 @
14b91f60
...
@@ -57,10 +57,10 @@ class TestTopkOp(OpTest):
...
@@ -57,10 +57,10 @@ class TestTopkOp(OpTest):
self
.
outputs
=
{
'Out'
:
output
,
'Indices'
:
indices
}
self
.
outputs
=
{
'Out'
:
output
,
'Indices'
:
indices
}
def
test_check_output
(
self
):
def
test_check_output
(
self
):
self
.
check_output
(
check_eager
=
Fals
e
)
self
.
check_output
(
check_eager
=
Tru
e
)
def
test_check_grad
(
self
):
def
test_check_grad
(
self
):
self
.
check_grad
(
set
([
'X'
]),
'Out'
,
check_eager
=
Fals
e
)
self
.
check_grad
(
set
([
'X'
]),
'Out'
,
check_eager
=
Tru
e
)
class
TestTopkOp1
(
TestTopkOp
):
class
TestTopkOp1
(
TestTopkOp
):
...
...
python/paddle/tensor/search.py
浏览文件 @
14b91f60
...
@@ -858,6 +858,12 @@ def topk(x, k, axis=None, largest=True, sorted=True, name=None):
...
@@ -858,6 +858,12 @@ def topk(x, k, axis=None, largest=True, sorted=True, name=None):
"""
"""
if
in_dygraph_mode
():
if
axis
==
None
:
axis
=
-
1
out
,
indices
=
_C_ops
.
final_state_top_k
(
x
,
k
,
axis
,
largest
,
sorted
)
return
out
,
indices
if
_non_static_mode
():
if
_non_static_mode
():
if
axis
is
None
:
if
axis
is
None
:
out
,
indices
=
_C_ops
.
top_k_v2
(
x
,
'k'
,
out
,
indices
=
_C_ops
.
top_k_v2
(
x
,
'k'
,
...
...
python/paddle/utils/code_gen/api.yaml
浏览文件 @
14b91f60
...
@@ -1123,7 +1123,8 @@
...
@@ -1123,7 +1123,8 @@
infer_meta
:
infer_meta
:
func
:
ReduceInferMetaBase
func
:
ReduceInferMetaBase
kernel
:
kernel
:
func
:
reduce_prod
func
:
prod_raw
backward
:
reduce_prod_grad
-
api
:
relu
-
api
:
relu
args
:
(Tensor x)
args
:
(Tensor x)
...
...
python/paddle/utils/code_gen/backward.yaml
浏览文件 @
14b91f60
...
@@ -721,6 +721,16 @@
...
@@ -721,6 +721,16 @@
kernel
:
kernel
:
func
:
reciprocal_grad
func
:
reciprocal_grad
-
backward_api
:
reduce_prod_grad
forward
:
reduce_prod (Tensor x, int64_t[] dims, bool keep_dim, bool reduce_all) -> Tensor(out)
args
:
(Tensor x, Tensor out, Tensor out_grad, int64_t[] dims, bool keep_dim, bool reduce_all)
output
:
Tensor(x_grad)
infer_meta
:
func
:
UnchangedInferMeta
param
:
[
x
]
kernel
:
func
:
reduce_prod_grad
-
backward_api
:
relu_double_grad
-
backward_api
:
relu_double_grad
forward
:
relu_grad (Tensor out, Tensor grad_out) -> Tensor(grad_x)
forward
:
relu_grad (Tensor out, Tensor grad_out) -> Tensor(grad_x)
args
:
(Tensor out, Tensor grad_x_grad)
args
:
(Tensor out, Tensor grad_x_grad)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录