Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
N
NiuTrans.Tensor
概览
Overview
Details
Activity
Cycle Analytics
版本库
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
问题
0
Issues
0
列表
Board
标记
里程碑
合并请求
0
Merge Requests
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
Snippets
成员
Collapse sidebar
Close sidebar
活动
图像
聊天
创建新问题
作业
提交
Issue Boards
Open sidebar
杨迪
NiuTrans.Tensor
Commits
32e87681
Commit
32e87681
authored
5 years ago
by
liyinqiao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Support sorted TopK function.
Add a new parameter isSorted in TopK function to sort the items in the top-k pool.
parent
48bdcb49
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
34 行增加
和
13 行删除
+34
-13
source/tensor/core/sort/TopK.cpp
+6
-4
source/tensor/core/sort/TopK.cu
+25
-6
source/tensor/core/sort/TopK.cuh
+1
-1
source/tensor/core/sort/TopK.h
+2
-2
没有找到文件。
source/tensor/core/sort/TopK.cpp
查看文件 @
32e87681
...
...
@@ -35,8 +35,9 @@ get the top-k items along a given dimension
>> index - index of the top-k items
>> dim - the dimension along which the sorting is performed
>> k - how many items returned after sorting
>> isSorted - indicates whether the k items are sorted
*/
void
_TopK
(
const
XTensor
*
a
,
XTensor
*
b
,
XTensor
*
index
,
int
dim
,
int
k
)
void
_TopK
(
const
XTensor
*
a
,
XTensor
*
b
,
XTensor
*
index
,
int
dim
,
int
k
,
bool
isSorted
)
{
dim
=
MODX
(
dim
,
a
->
order
);
...
...
@@ -58,7 +59,7 @@ void _TopK(const XTensor * a, XTensor * b, XTensor * index, int dim, int k)
if
(
a
->
devID
>=
0
||
b
->
devID
>=
0
)
{
#ifdef USE_CUDA
_CudaTopK
(
a
,
b
,
index
,
dim
,
k
);
_CudaTopK
(
a
,
b
,
index
,
dim
,
k
,
isSorted
);
#else
ShowNTErrors
(
"Plesae specify USE_CUDA and recompile the code!"
);
#endif
...
...
@@ -116,15 +117,16 @@ get the top-k items along a given dimension
>> index - index of the top-k items
>> dim - the dimension along which the sorting is performed
>> k - how many items returned after sorting
>> isSorted - indicates whether the k items are sorted
*/
void
TopK
(
XTensor
&
a
,
XTensor
&
b
,
XTensor
&
index
,
int
dim
,
int
k
)
void
TopK
(
XTensor
&
a
,
XTensor
&
b
,
XTensor
&
index
,
int
dim
,
int
k
,
bool
isSorted
)
{
dim
=
MODX
(
dim
,
a
.
order
);
if
(
a
.
dimSize
[
dim
]
<=
k
)
_Sort
(
&
a
,
&
b
,
&
index
,
dim
);
else
_TopK
(
&
a
,
&
b
,
&
index
,
dim
,
k
);
_TopK
(
&
a
,
&
b
,
&
index
,
dim
,
k
,
isSorted
);
/* tensor connection */
//TensorList list(2);
...
...
This diff is collapsed.
Click to expand it.
source/tensor/core/sort/TopK.cu
查看文件 @
32e87681
...
...
@@ -374,9 +374,10 @@ get the top-k items
>> minValue - min value of an item
>> output - the output data array
>> index - the output index array
>> isSorted - indicates whether the k items are sorted
*/
template<class T> __global__
void KernelTopK3(T * input, int stride, int strideNum, int blockNum, int k, T minValue, T * output, int * index)
void KernelTopK3(T * input, int stride, int strideNum, int blockNum, int k, T minValue, T * output, int * index
, bool isSorted
)
{
__shared__ CudaHeapNode<T> heapData[(SHARED_MEMORY_SIZE - 512 * sizeof(T)) / sizeof(CudaHeapNode<T>)];
__shared__ T eachHeapMaxValue[512];
...
...
@@ -479,9 +480,22 @@ void KernelTopK3(T * input, int stride, int strideNum, int blockNum, int k, T mi
int offset = stride * k * blockIndex + offsetInBlock;
T * dOutput = output + offset;
int * indexOutput = index + offset;
for (int q = 0; q < k; ++q){
dOutput[stride * q] = ansHeapData.items[q].value;
indexOutput[stride * q] = ansHeapData.items[q].index;
if (isSorted)
{
for (int q = k - 1; q >= 0; q--) {
dOutput[stride * q] = ansHeapData.items[0].value;
indexOutput[stride * q] = ansHeapData.items[0].index;
ansHeapData.items[0] = ansHeapData.items[ansHeapData.count - 1];
ansHeapData.count--;
ansHeapData.Down(0);
}
}
else
{
for (int q = 0; q < k; ++q) {
dOutput[stride * q] = ansHeapData.items[q].value;
indexOutput[stride * q] = ansHeapData.items[q].index;
}
}
}
}
...
...
@@ -803,8 +817,9 @@ get the top-k items along a given dimension
>> index - index of the top-k items
>> dim - the dimension along which the sorting is performed
>> k - how many items returned after sorting
>> isSorted - indicates whether the k items are sorted
*/
void _CudaTopK(const XTensor * a, XTensor * b, XTensor * index, int dim, int k)
void _CudaTopK(const XTensor * a, XTensor * b, XTensor * index, int dim, int k
, bool isSorted
)
{
CheckNTErrors((a->unitSize == b->unitSize), "Unmatched input tensors!");
CheckNTErrors((a->order == b->order), "Unmatched input tensors!");
...
...
@@ -846,7 +861,7 @@ void _CudaTopK(const XTensor * a, XTensor * b, XTensor * index, int dim, int k)
if (a->dataType == DEFAULT_DTYPE) {
KernelTopK3<DTYPE> <<<dim3(cudaGrids[0], cudaGrids[1]), dim3(cudaBlocks[0], cudaBlocks[1]) >>>
((DTYPE*)a->data, stride, strideNumA, blockNum, k, DTYPE_MIN,
(DTYPE*)b->data, (int*)index->data);
(DTYPE*)b->data, (int*)index->data
, isSorted
);
}
else {
ShowNTErrors("TODO!");
...
...
@@ -882,6 +897,10 @@ void _CudaTopK(const XTensor * a, XTensor * b, XTensor * index, int dim, int k)
KernelTopKRadixSelect<DTYPE> <<<dim3(cudaGrids[0], cudaGrids[1]), dim3(cudaBlocks[0], cudaBlocks[1]) >>> (goutput, stride, strideNumA, blockNum, k, DTYPE_MIN, (DTYPE *)b->data, (int *)index->data, stride * strideNumA * blockNum);
deconvert2floatV2 <<<dim3(cudaGrids[0], cudaGrids[1]), dim3(cudaBlocks[0], cudaBlocks[1]) >>> ((unsigned int *)a->data, (float *)goutput, stride, strideNumA, blockNum, strideNumA*blockNum*stride);
if (isSorted)
{
ShowNTErrors("TODO!");
}
}
}
...
...
This diff is collapsed.
Click to expand it.
source/tensor/core/sort/TopK.cuh
查看文件 @
32e87681
...
...
@@ -29,7 +29,7 @@ namespace nts { // namespace nts(NiuTrans.Tensor)
#ifdef USE_CUDA
/* get the top-k items along a given dimension */
void _CudaTopK(const XTensor * a, XTensor * b, XTensor * index, int dim, int k);
void _CudaTopK(const XTensor * a, XTensor * b, XTensor * index, int dim, int k
, bool isSorted
);
#endif // USE_CUDA
...
...
This diff is collapsed.
Click to expand it.
source/tensor/core/sort/TopK.h
查看文件 @
32e87681
...
...
@@ -27,10 +27,10 @@
namespace
nts
{
// namespace nts(NiuTrans.Tensor)
/* get the top-k items along a given dimension */
void
_TopK
(
const
XTensor
*
a
,
XTensor
*
b
,
XTensor
*
index
,
int
dim
,
int
k
);
void
_TopK
(
const
XTensor
*
a
,
XTensor
*
b
,
XTensor
*
index
,
int
dim
,
int
k
,
bool
isSorted
=
false
);
/* get the top-k items along a given dimension */
void
TopK
(
XTensor
&
a
,
XTensor
&
b
,
XTensor
&
index
,
int
dim
,
int
k
);
void
TopK
(
XTensor
&
a
,
XTensor
&
b
,
XTensor
&
index
,
int
dim
,
int
k
,
bool
isSorted
=
false
);
}
// namespace nts(NiuTrans.Tensor)
...
...
This diff is collapsed.
Click to expand it.
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论