Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Oneflow-Inc
oneflow
提交
ddfe95a9
O
oneflow
项目概览
Oneflow-Inc
/
oneflow
上一次同步 接近 3 年
通知
13
Star
2733
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
O
oneflow
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
ddfe95a9
编写于
2月 12, 2019
作者:
S
ScXfjiang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
thread pool version
Former-commit-id: ce92c2d972aa74760351052e9d89cf6e3c40282c
上级
69c9ee85
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
41 addition
and
16 deletion
+41
-16
oneflow/core/kernel/top_k_kernel.cpp
oneflow/core/kernel/top_k_kernel.cpp
+40
-15
oneflow/core/operator/top_k_op.cpp
oneflow/core/operator/top_k_op.cpp
+1
-1
未找到文件。
oneflow/core/kernel/top_k_kernel.cpp
浏览文件 @
ddfe95a9
#include "oneflow/core/kernel/top_k_kernel.h"
#include "oneflow/core/common/balanced_splitter.h"
#include "oneflow/core/thread/thread_manager.h"
namespace
oneflow
{
namespace
{
template
<
typename
T
>
void
ForwardPartDataContent
(
const
T
*
in
,
const
Range
range
,
const
int32_t
instance_size
,
const
int32_t
k
,
const
bool
sorted
,
int32_t
*
fw_buf
,
int32_t
*
out
)
{
FOR_RANGE
(
int32_t
,
i
,
range
.
begin
(),
range
.
end
())
{
const
int32_t
offset
=
i
*
instance_size
;
int32_t
*
indices
=
fw_buf
+
offset
;
const
T
*
values
=
in
+
offset
;
std
::
iota
(
indices
,
indices
+
instance_size
,
0
);
auto
comp
=
[
&
](
const
int32_t
lhs
,
const
int32_t
rhs
)
{
const
T
l
=
values
[
lhs
];
const
T
r
=
values
[
rhs
];
if
(
l
==
r
)
{
return
lhs
<
rhs
;
}
else
{
return
l
>
r
;
}
};
std
::
nth_element
(
indices
,
indices
+
k
,
indices
+
instance_size
,
comp
);
if
(
k
>
1
&&
sorted
)
{
std
::
sort
(
indices
,
indices
+
k
,
comp
);
}
std
::
copy
(
indices
,
indices
+
k
,
out
+
i
*
k
);
}
}
}
// namespace
template
<
typename
T
>
void
TopKKernel
<
T
>::
ForwardDataContent
(
const
KernelCtx
&
ctx
,
std
::
function
<
Blob
*
(
const
std
::
string
&
)
>
BnInOp2Blob
)
const
{
...
...
@@ -17,22 +46,18 @@ void TopKKernel<T>::ForwardDataContent(const KernelCtx& ctx,
int32_t
*
out
=
out_blob
->
mut_dptr
<
int32_t
>
();
const
auto
&
conf
=
this
->
op_conf
().
top_k_conf
();
const
int32_t
k
=
conf
.
k
();
FOR_RANGE
(
int32_t
,
i
,
0
,
instance_num
)
{
std
::
iota
(
fw_buf
,
fw_buf
+
instance_size
,
0
);
const
int32_t
offset
=
i
*
instance_size
;
auto
comp
=
[
&
](
const
int32_t
lhs
,
const
int32_t
rhs
)
{
const
T
l
=
in
[
offset
+
lhs
];
const
T
r
=
in
[
offset
+
rhs
];
if
(
l
==
r
)
{
return
lhs
<
rhs
;
}
else
{
return
l
>
r
;
}
};
std
::
nth_element
(
fw_buf
,
fw_buf
+
k
,
fw_buf
+
instance_size
,
comp
);
if
(
k
>
1
&&
conf
.
sorted
())
{
std
::
sort
(
fw_buf
,
fw_buf
+
k
,
comp
);
}
std
::
copy
(
fw_buf
,
fw_buf
+
k
,
out
+
i
*
k
);
const
int32_t
part_num
=
std
::
min
(
instance_num
,
Global
<
ThreadMgr
>::
Get
()
->
compute_thread_pool
()
->
thread_num
());
const
BalancedSplitter
bs
(
instance_num
,
part_num
);
BlockingCounter
bc
(
part_num
);
FOR_RANGE
(
int32_t
,
part_id
,
0
,
part_num
)
{
Range
range
=
bs
.
At
(
part_id
);
Global
<
ThreadMgr
>::
Get
()
->
compute_thread_pool
()
->
AddWork
([
=
,
&
bc
]()
{
ForwardPartDataContent
(
in
,
range
,
instance_size
,
k
,
conf
.
sorted
(),
fw_buf
,
out
);
bc
.
Decrease
();
});
}
bc
.
WaitUntilCntEqualZero
();
}
ADD_CPU_DEFAULT_KERNEL_CREATOR
(
OperatorConf
::
kTopKConf
,
TopKKernel
,
FLOATING_DATA_TYPE_SEQ
);
...
...
oneflow/core/operator/top_k_op.cpp
浏览文件 @
ddfe95a9
...
...
@@ -18,7 +18,7 @@ void TopKOp::InferBlobDescs(std::function<BlobDesc*(const std::string&)> GetBlob
CHECK_LE
(
conf
.
k
(),
in
->
shape
().
dim_vec
().
back
());
// fw_buf
BlobDesc
*
fw_buf
=
GetBlobDesc4BnInOp
(
"fw_buf"
);
fw_buf
->
mut_shape
()
=
Shape
({
in
->
shape
()
.
dim_vec
().
back
()
});
fw_buf
->
mut_shape
()
=
Shape
({
in
->
shape
()});
fw_buf
->
set_data_type
(
DataType
::
kInt32
);
// out
BlobDesc
*
out
=
GetBlobDesc4BnInOp
(
"out"
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录