Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
2314f2eb
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看板
未验证
提交
2314f2eb
编写于
12月 26, 2018
作者:
W
whs
提交者:
GitHub
12月 26, 2018
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Make topk op support variable k. (#15044)
* Make topk op support variable k. test=develop * Fix tensor type. test=develop
上级
09bd8fa6
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
57 addition
and
8 deletion
+57
-8
paddle/fluid/operators/top_k_op.cc
paddle/fluid/operators/top_k_op.cc
+14
-1
paddle/fluid/operators/top_k_op.cu
paddle/fluid/operators/top_k_op.cu
+11
-0
paddle/fluid/operators/top_k_op.h
paddle/fluid/operators/top_k_op.h
+10
-2
python/paddle/fluid/layers/nn.py
python/paddle/fluid/layers/nn.py
+9
-3
python/paddle/fluid/tests/unittests/test_top_k_op.py
python/paddle/fluid/tests/unittests/test_top_k_op.py
+13
-2
未找到文件。
paddle/fluid/operators/top_k_op.cc
浏览文件 @
2314f2eb
...
...
@@ -21,7 +21,7 @@ class TopkOp : public framework::OperatorWithKernel {
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
{
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
{
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"X"
),
"Input(X) of TopkOp should not be null."
);
PADDLE_ENFORCE
(
ctx
->
HasOutput
(
"Out"
),
...
...
@@ -44,12 +44,25 @@ class TopkOp : public framework::OperatorWithKernel {
ctx
->
ShareLoD
(
"X"
,
"Out"
);
ctx
->
ShareLoD
(
"X"
,
"Indices"
);
}
protected:
framework
::
OpKernelType
GetExpectedKernelType
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
framework
::
LibraryType
library_
{
framework
::
LibraryType
::
kPlain
};
framework
::
DataLayout
layout_
=
framework
::
DataLayout
::
kAnyLayout
;
return
framework
::
OpKernelType
(
ctx
.
Input
<
Tensor
>
(
"X"
)
->
type
(),
ctx
.
device_context
(),
layout_
,
library_
);
}
};
class
TopkOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
public:
void
Make
()
override
{
AddInput
(
"X"
,
"(Tensor) The input of Topk op"
);
AddInput
(
"K"
,
"(Tensor) Number of top elements to look for along "
"the last dimension (along each row for matrices)."
)
.
AsDispensable
();
AddOutput
(
"Out"
,
"(Tensor) The output tensor of Topk op"
);
AddOutput
(
"Indices"
,
"(Tensor) The indices of Topk elements of input"
);
AddComment
(
R"DOC(
...
...
paddle/fluid/operators/top_k_op.cu
浏览文件 @
2314f2eb
...
...
@@ -327,6 +327,17 @@ class TopkOpCUDAKernel : public framework::OpKernel<T> {
auto
*
indices
=
ctx
.
Output
<
Tensor
>
(
"Indices"
);
size_t
k
=
static_cast
<
int
>
(
ctx
.
Attr
<
int
>
(
"k"
));
auto
*
k_t
=
ctx
.
Input
<
Tensor
>
(
"K"
);
if
(
k_t
)
{
Tensor
k_host
;
framework
::
TensorCopySync
(
*
k_t
,
platform
::
CPUPlace
(),
&
k_host
);
k
=
k_host
.
data
<
int
>
()[
0
];
framework
::
DDim
output_dims
=
output
->
dims
();
output_dims
[
output_dims
.
size
()
-
1
]
=
k
;
output
->
Resize
(
output_dims
);
indices
->
Resize
(
output_dims
);
}
const
T
*
input_data
=
input
->
data
<
T
>
();
T
*
output_data
=
output
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
// FIXME(typhoonzero): data is always converted to type T?
...
...
paddle/fluid/operators/top_k_op.h
浏览文件 @
2314f2eb
...
...
@@ -37,8 +37,16 @@ class TopkKernel : public framework::OpKernel<T> {
auto
*
input
=
ctx
.
Input
<
Tensor
>
(
"X"
);
auto
*
output
=
ctx
.
Output
<
Tensor
>
(
"Out"
);
auto
*
indices
=
ctx
.
Output
<
Tensor
>
(
"Indices"
);
// k is determined by Attr
const
size_t
k
=
static_cast
<
int
>
(
ctx
.
Attr
<
int
>
(
"k"
));
size_t
k
=
static_cast
<
int
>
(
ctx
.
Attr
<
int
>
(
"k"
));
auto
*
k_t
=
ctx
.
Input
<
Tensor
>
(
"K"
);
if
(
k_t
)
{
k
=
k_t
->
data
<
int
>
()[
0
];
framework
::
DDim
output_dims
=
output
->
dims
();
output_dims
[
output_dims
.
size
()
-
1
]
=
k
;
output
->
Resize
(
output_dims
);
indices
->
Resize
(
output_dims
);
}
T
*
output_data
=
output
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
int64_t
*
indices_data
=
indices
->
mutable_data
<
int64_t
>
(
ctx
.
GetPlace
());
...
...
python/paddle/fluid/layers/nn.py
浏览文件 @
2314f2eb
...
...
@@ -4530,7 +4530,7 @@ def topk(input, k, name=None):
Args:
input(Variable): The input variable which can be a vector or Tensor with
higher rank.
k(int): The number of top elements to look for along the last dimension
k(int
| Variable
): The number of top elements to look for along the last dimension
of input.
name(str|None): A name for this layer(optional). If set None, the layer
will be named automatically.
...
...
@@ -4553,12 +4553,18 @@ def topk(input, k, name=None):
helper
=
LayerHelper
(
"top_k"
,
**
locals
())
values
=
helper
.
create_variable_for_type_inference
(
dtype
=
input
.
dtype
)
indices
=
helper
.
create_variable_for_type_inference
(
dtype
=
"int64"
)
inputs
=
{
"X"
:
[
input
]}
attrs
=
None
if
isinstance
(
k
,
Variable
):
inputs
[
'K'
]
=
k
else
:
attrs
=
{
'k'
:
k
}
helper
.
append_op
(
type
=
"top_k"
,
inputs
=
{
"X"
:
[
input
]}
,
inputs
=
inputs
,
outputs
=
{
"Out"
:
[
values
],
"Indices"
:
[
indices
]},
attrs
=
{
"k"
:
k
}
)
attrs
=
attrs
)
values
.
stop_gradient
=
True
indices
.
stop_gradient
=
True
return
values
,
indices
...
...
python/paddle/fluid/tests/unittests/test_top_k_op.py
浏览文件 @
2314f2eb
...
...
@@ -21,6 +21,7 @@ from op_test import OpTest
class
TestTopkOp
(
OpTest
):
def
setUp
(
self
):
self
.
variable_k
=
False
self
.
set_args
()
self
.
op_type
=
"top_k"
self
.
dtype
=
np
.
float32
...
...
@@ -30,9 +31,12 @@ class TestTopkOp(OpTest):
input
=
np
.
random
.
random
((
self
.
row
,
k
)).
astype
(
self
.
dtype
)
output
=
np
.
ndarray
((
self
.
row
,
k
))
indices
=
np
.
ndarray
((
self
.
row
,
k
)).
astype
(
"int64"
)
self
.
inputs
=
{
'X'
:
input
}
self
.
attrs
=
{
'k'
:
k
}
if
self
.
variable_k
:
self
.
inputs
[
'K'
]
=
np
.
array
([
k
]).
astype
(
"int32"
)
else
:
self
.
attrs
=
{
'k'
:
k
}
for
rowid
in
range
(
self
.
row
):
row
=
input
[
rowid
]
...
...
@@ -118,5 +122,12 @@ class TestTopkOp4(TestTopkOp):
self
.
top_k
=
1
class
TestTopkOp5
(
TestTopkOp
):
def
set_args
(
self
):
self
.
row
=
40000
self
.
top_k
=
3
self
.
variable_k
=
True
if
__name__
==
"__main__"
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录