Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
bdb80550
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看板
未验证
提交
bdb80550
编写于
8月 21, 2020
作者:
W
wangchaochaohu
提交者:
GitHub
8月 21, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
【API2.0】add numel API for paddle test=develop (#26311)
上级
623e14fb
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
162 addition
and
8 deletion
+162
-8
paddle/fluid/operators/size_op.cc
paddle/fluid/operators/size_op.cc
+1
-0
paddle/fluid/operators/size_op.cu
paddle/fluid/operators/size_op.cu
+6
-5
python/paddle/__init__.py
python/paddle/__init__.py
+1
-0
python/paddle/fluid/layers/nn.py
python/paddle/fluid/layers/nn.py
+11
-2
python/paddle/fluid/tests/unittests/test_numel_op.py
python/paddle/fluid/tests/unittests/test_numel_op.py
+101
-0
python/paddle/tensor/__init__.py
python/paddle/tensor/__init__.py
+2
-0
python/paddle/tensor/stat.py
python/paddle/tensor/stat.py
+40
-1
未找到文件。
paddle/fluid/operators/size_op.cc
浏览文件 @
bdb80550
...
...
@@ -54,5 +54,6 @@ REGISTER_OPERATOR(
paddle
::
framework
::
EmptyGradOpMaker
<
paddle
::
framework
::
OpDesc
>
,
paddle
::
framework
::
EmptyGradOpMaker
<
paddle
::
imperative
::
OpBase
>
);
REGISTER_OP_CPU_KERNEL
(
size
,
ops
::
SizeKernel
<
int
>
,
ops
::
SizeKernel
<
int32_t
>
,
ops
::
SizeKernel
<
paddle
::
platform
::
float16
>
,
ops
::
SizeKernel
<
float
>
,
ops
::
SizeKernel
<
double
>
,
ops
::
SizeKernel
<
bool
>
);
paddle/fluid/operators/size_op.cu
浏览文件 @
bdb80550
...
...
@@ -14,8 +14,9 @@ limitations under the License. */
#include "paddle/fluid/operators/size_op.h"
REGISTER_OP_CUDA_KERNEL
(
size
,
paddle
::
operators
::
SizeKernel
<
int
>
,
paddle
::
operators
::
SizeKernel
<
int32_t
>
,
paddle
::
operators
::
SizeKernel
<
float
>
,
paddle
::
operators
::
SizeKernel
<
bool
>
,
paddle
::
operators
::
SizeKernel
<
double
>
);
REGISTER_OP_CUDA_KERNEL
(
size
,
paddle
::
operators
::
SizeKernel
<
int
>
,
paddle
::
operators
::
SizeKernel
<
int32_t
>
,
paddle
::
operators
::
SizeKernel
<
paddle
::
platform
::
float16
>
,
paddle
::
operators
::
SizeKernel
<
float
>
,
paddle
::
operators
::
SizeKernel
<
bool
>
,
paddle
::
operators
::
SizeKernel
<
double
>
);
python/paddle/__init__.py
浏览文件 @
bdb80550
...
...
@@ -237,6 +237,7 @@ from .tensor.stat import reduce_mean #DEFINE_ALIAS
from
.tensor.stat
import
std
#DEFINE_ALIAS
from
.tensor.stat
import
var
#DEFINE_ALIAS
from
.fluid.data
import
data
from
.tensor.stat
import
numel
#DEFINE_ALIAS
from
.device
import
get_cudnn_version
from
.device
import
set_device
from
.device
import
get_device
...
...
python/paddle/fluid/layers/nn.py
浏览文件 @
bdb80550
...
...
@@ -11182,6 +11182,7 @@ def rank(input):
return out
@deprecated(since="2.0.0", update_to="paddle.numel")
def size(input):
"""
**Size Layer**
...
...
@@ -11189,11 +11190,14 @@ def size(input):
Returns the number of elements for a tensor, which is a int64 Tensor with shape [1].
Args:
input (
Variable): The input variable
.
input (
Tensor): The input Tensor, it's data type can be bool, float16, float32, float64, int32, int64
.
Returns:
Variable: The number of elements for the input variable
.
Tensor: The number of elements for the input Tensor
.
Raises:
TypeError: ``input`` must be a Tensor and the data type of ``input`` must be one of bool, float16, float32, float64, int32, int64.
Examples:
.. code-block:: python
...
...
@@ -11204,6 +11208,11 @@ def size(input):
rank = layers.size(input) # 300
"""
if in_dygraph_mode():
return core.ops.size(x)
check_variable_and_dtype(
x, 'x', ['bool', 'float16', 'float32', 'float64', 'int32', 'int64'],
"size")
helper = LayerHelper('size', **locals())
out = helper.create_variable_for_type_inference(dtype='int64')
helper.append_op(type='size', inputs={'Input': input}, outputs={'Out': out})
...
...
python/paddle/fluid/tests/unittests/test_numel_op.py
0 → 100644
浏览文件 @
bdb80550
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from
__future__
import
print_function
import
unittest
import
numpy
as
np
from
op_test
import
OpTest
import
paddle.fluid.core
as
core
import
paddle.fluid
as
fluid
from
paddle.fluid
import
Program
,
program_guard
import
functools
import
paddle
class
TestNumelOp
(
OpTest
):
def
setUp
(
self
):
self
.
op_type
=
"size"
self
.
init
()
x
=
np
.
random
.
random
((
self
.
shape
)).
astype
(
"float64"
)
self
.
inputs
=
{
'Input'
:
x
,
}
self
.
outputs
=
{
'Out'
:
np
.
array
([
np
.
size
(
x
)])}
def
test_check_output
(
self
):
self
.
check_output
()
def
init
(
self
):
self
.
shape
=
(
6
,
56
,
8
,
55
)
class
TestNumelOp1
(
TestNumelOp
):
def
init
(
self
):
self
.
shape
=
(
11
,
66
)
class
TestNumelOp2
(
TestNumelOp
):
def
init
(
self
):
self
.
shape
=
(
0
,
)
class
TestNumelOoAPI
(
unittest
.
TestCase
):
def
test_numel_static
(
self
):
main_program
=
fluid
.
Program
()
startup_program
=
fluid
.
Program
()
with
fluid
.
program_guard
(
main_program
,
startup_program
):
shape1
=
[
2
,
1
,
4
,
5
]
shape2
=
[
1
,
4
,
5
]
x_1
=
paddle
.
data
(
shape
=
shape1
,
dtype
=
'int32'
,
name
=
'x_1'
)
x_2
=
paddle
.
data
(
shape
=
shape2
,
dtype
=
'int32'
,
name
=
'x_2'
)
input_1
=
np
.
random
.
random
(
shape1
).
astype
(
"int32"
)
input_2
=
np
.
random
.
random
(
shape2
).
astype
(
"int32"
)
out_1
=
paddle
.
numel
(
x_1
)
out_2
=
paddle
.
numel
(
x_2
)
exe
=
paddle
.
static
.
Executor
(
place
=
paddle
.
CPUPlace
())
res_1
,
res_2
=
exe
.
run
(
feed
=
{
"x_1"
:
input_1
,
"x_2"
:
input_2
,
},
fetch_list
=
[
out_1
,
out_2
])
assert
(
np
.
array_equal
(
res_1
,
np
.
array
([
np
.
size
(
input_1
)]).
astype
(
"int64"
)))
assert
(
np
.
array_equal
(
res_2
,
np
.
array
([
np
.
size
(
input_2
)]).
astype
(
"int64"
)))
def
test_numel_imperative
(
self
):
paddle
.
disable_static
(
paddle
.
CPUPlace
())
input_1
=
np
.
random
.
random
([
2
,
1
,
4
,
5
]).
astype
(
"int32"
)
input_2
=
np
.
random
.
random
([
1
,
4
,
5
]).
astype
(
"int32"
)
x_1
=
paddle
.
to_variable
(
input_1
)
x_2
=
paddle
.
to_variable
(
input_2
)
out_1
=
paddle
.
numel
(
x_1
)
out_2
=
paddle
.
numel
(
x_2
)
assert
(
np
.
array_equal
(
out_1
.
numpy
().
item
(
0
),
np
.
size
(
input_1
)))
assert
(
np
.
array_equal
(
out_2
.
numpy
().
item
(
0
),
np
.
size
(
input_2
)))
paddle
.
enable_static
()
def
test_error
(
self
):
main_program
=
fluid
.
Program
()
startup_program
=
fluid
.
Program
()
with
fluid
.
program_guard
(
main_program
,
startup_program
):
def
test_x_type
():
shape
=
[
1
,
4
,
5
]
input_1
=
np
.
random
.
random
(
shape
).
astype
(
"int32"
)
out_1
=
paddle
.
numel
(
input_1
)
self
.
assertRaises
(
TypeError
,
test_x_type
)
if
__name__
==
'__main__'
:
unittest
.
main
()
python/paddle/tensor/__init__.py
浏览文件 @
bdb80550
...
...
@@ -181,5 +181,7 @@ from .stat import mean #DEFINE_ALIAS
from
.stat
import
reduce_mean
#DEFINE_ALIAS
from
.stat
import
std
#DEFINE_ALIAS
from
.stat
import
var
#DEFINE_ALIAS
from
.stat
import
numel
#DEFINE_ALIAS
# from .tensor import Tensor #DEFINE_ALIAS
# from .tensor import LoDTensor #DEFINE_ALIAS
# from .tensor import LoDTensorArray #DEFINE_ALIAS
python/paddle/tensor/stat.py
浏览文件 @
bdb80550
...
...
@@ -15,9 +15,10 @@
# TODO: define statistical functions of a tensor
from
..fluid.layers
import
reduce_mean
#DEFINE_ALIAS
__all__
=
[
'mean'
,
'reduce_mean'
,
'std'
,
'var'
]
__all__
=
[
'mean'
,
'reduce_mean'
,
'std'
,
'var'
,
'numel'
]
import
numpy
as
np
from
..fluid.framework
import
Variable
from
..fluid.layer_helper
import
LayerHelper
from
..fluid.framework
import
core
,
in_dygraph_mode
from
..fluid
import
layers
...
...
@@ -244,3 +245,41 @@ def std(input, axis=None, keepdim=False, unbiased=True, out=None, name=None):
return
out
else
:
return
tmp
def
numel
(
x
,
name
=
None
):
"""
Returns the number of elements for a tensor, which is a int64 Tensor with shape [1] in static mode
or a scalar value in imperative mode
Args:
x (Tensor): The input Tensor, it's data type can be bool, float16, float32, float64, int32, int64.
Returns:
Tensor: The number of elements for the input Tensor.
Raises:
TypeError: ``x`` must be a Tensor and the data type of ``x`` must be one of bool, float16, float32, float64, int32, int64.
Examples:
.. code-block:: python
import paddle
paddle.disable_static()
x = paddle.full(shape=[4, 5, 7], fill_value=0, dtype='int32')
numel = paddle.numel(x) # 140
"""
if
in_dygraph_mode
():
return
core
.
ops
.
size
(
x
)
if
not
isinstance
(
x
,
Variable
):
raise
TypeError
(
"x must be a Tensor in numel"
)
helper
=
LayerHelper
(
'numel'
,
**
locals
())
out
=
helper
.
create_variable_for_type_inference
(
dtype
=
core
.
VarDesc
.
VarType
.
INT64
)
helper
.
append_op
(
type
=
'size'
,
inputs
=
{
'Input'
:
x
},
outputs
=
{
'Out'
:
out
})
return
out
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录