Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Crayon鑫
Paddle
提交
6ee22c4f
P
Paddle
项目概览
Crayon鑫
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
6ee22c4f
编写于
6月 12, 2018
作者:
Y
Yibing Liu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add gpu kernel for argsort op
上级
2c2120c8
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
140 addition
and
0 deletion
+140
-0
paddle/fluid/operators/argsort_op.cu
paddle/fluid/operators/argsort_op.cu
+140
-0
未找到文件。
paddle/fluid/operators/argsort_op.cu
0 → 100644
浏览文件 @
6ee22c4f
/* Copyright (c) 2016 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. */
#include <thrust/execution_policy.h>
#include <thrust/sort.h>
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/operators/argsort_op.h"
#include "paddle/fluid/platform/assert.h"
#include "paddle/fluid/platform/cuda_device_function.h"
#include "paddle/fluid/platform/cuda_primitives.h"
namespace
paddle
{
namespace
operators
{
using
Tensor
=
framework
::
Tensor
;
using
platform
::
PADDLE_CUDA_NUM_THREADS
;
template
<
typename
T
>
__global__
void
PermuteInData
(
const
T
*
in
,
const
int64_t
*
trg_idx
,
int64_t
n
,
T
*
med_out
)
{
int
index
=
threadIdx
.
x
+
blockDim
.
x
*
blockIdx
.
x
;
if
(
index
<
n
)
{
med_out
[
trg_idx
[
index
]]
=
in
[
index
];
}
}
template
<
typename
T
>
__global__
void
Sort
(
int64_t
axis_dim
,
int64_t
groups
,
T
*
med_out
,
int64_t
*
med_ids
)
{
int
index
=
threadIdx
.
x
+
blockDim
.
x
*
blockIdx
.
x
;
if
(
index
<
groups
)
{
thrust
::
sort_by_key
(
thrust
::
device
,
med_out
+
index
*
axis_dim
,
med_out
+
axis_dim
*
(
1
+
index
),
med_ids
+
index
*
axis_dim
);
}
}
template
<
typename
T
>
__global__
void
PermuteMediateData
(
const
T
*
med_out
,
const
int64_t
*
med_ids
,
const
int64_t
*
trg_idx
,
int64_t
n
,
T
*
out
,
int64_t
*
indices
)
{
int
index
=
threadIdx
.
x
+
blockDim
.
x
*
blockIdx
.
x
;
if
(
index
<
n
)
{
out
[
index
]
=
med_out
[
trg_idx
[
index
]];
indices
[
index
]
=
med_ids
[
trg_idx
[
index
]];
}
}
template
<
typename
T
>
class
ArgsortOpCUDAKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
auto
*
input
=
ctx
.
Input
<
Tensor
>
(
"X"
);
auto
*
output
=
ctx
.
Output
<
Tensor
>
(
"Out"
);
auto
*
indices
=
ctx
.
Output
<
Tensor
>
(
"Indices"
);
int
axis
=
ctx
.
Attr
<
int
>
(
"axis"
);
auto
in_dims
=
input
->
dims
();
axis
=
(
axis
==
-
1
)
?
(
in_dims
.
size
()
-
1
)
:
axis
;
const
T
*
in_data
=
input
->
data
<
T
>
();
T
*
out_data
=
output
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
int64_t
*
ids_data
=
indices
->
mutable_data
<
int64_t
>
(
ctx
.
GetPlace
());
int64_t
numel
=
input
->
numel
();
int64_t
groups
=
numel
/
in_dims
[
axis
];
// Mediate tensor for sorting
Tensor
mediate_output
;
T
*
med_out_data
=
mediate_output
.
mutable_data
<
T
>
(
input
->
dims
(),
ctx
.
GetPlace
());
// The target index of each elemement in mediate tensor
std
::
vector
<
int64_t
>
target_idx
(
numel
,
0
);
// To record the index along the given axis for the data in mediate tensor
std
::
vector
<
int64_t
>
mediate_indices
(
numel
,
0
);
std
::
vector
<
int64_t
>
in_dims_out_axis
=
vectorize
(
in_dims
);
in_dims_out_axis
.
erase
(
in_dims_out_axis
.
begin
()
+
axis
);
for
(
int64_t
index
=
0
;
index
<
numel
;
++
index
)
{
int64_t
tmp
=
index
;
int64_t
pos_in_axis
=
0
;
std
::
vector
<
int64_t
>
shape
;
for
(
int64_t
j
=
in_dims
.
size
()
-
1
;
j
>=
0
;
--
j
)
{
if
(
j
!=
axis
)
{
shape
.
push_back
(
tmp
%
in_dims
[
j
]);
}
else
{
pos_in_axis
=
tmp
%
in_dims
[
j
];
}
tmp
/=
in_dims
[
j
];
}
std
::
reverse
(
shape
.
begin
(),
shape
.
end
());
int64_t
group
=
(
shape
.
size
()
>
0
)
?
shape
[
0
]
:
0
;
for
(
size_t
j
=
0
;
j
<
shape
.
size
()
-
1
;
++
j
)
{
group
=
group
*
in_dims_out_axis
[
j
+
1
]
+
shape
[
j
+
1
];
}
target_idx
[
index
]
=
group
*
in_dims
[
axis
]
+
pos_in_axis
;
mediate_indices
[
target_idx
[
index
]]
=
pos_in_axis
;
}
thrust
::
device_vector
<
int64_t
>
med_ids_dev
(
mediate_indices
.
begin
(),
mediate_indices
.
end
());
int64_t
*
med_ids_data
=
thrust
::
raw_pointer_cast
(
med_ids_dev
.
data
());
thrust
::
device_vector
<
int64_t
>
trg_idx_dev
(
target_idx
.
begin
(),
target_idx
.
end
());
int64_t
*
trg_idx
=
thrust
::
raw_pointer_cast
(
trg_idx_dev
.
data
());
auto
stream
=
reinterpret_cast
<
const
platform
::
CUDADeviceContext
&>
(
ctx
.
device_context
())
.
stream
();
auto
num_threads
=
PADDLE_CUDA_NUM_THREADS
;
PermuteInData
<<<
(
numel
-
1
)
/
num_threads
+
1
,
num_threads
,
0
,
stream
>>>
(
in_data
,
trg_idx
,
numel
,
med_out_data
);
Sort
<<<
(
groups
-
1
)
/
num_threads
+
1
,
num_threads
,
0
,
stream
>>>
(
in_dims
[
axis
],
groups
,
med_out_data
,
med_ids_data
);
PermuteMediateData
<<<
(
numel
-
1
)
/
num_threads
+
1
,
num_threads
,
0
,
stream
>>>
(
med_out_data
,
med_ids_data
,
trg_idx
,
numel
,
out_data
,
ids_data
);
}
};
}
// namespace operators
}
// namespace paddle
REGISTER_OP_CUDA_KERNEL
(
argsort
,
paddle
::
operators
::
ArgsortOpCUDAKernel
<
float
>
,
paddle
::
operators
::
ArgsortOpCUDAKernel
<
double
>
);
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录