Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
8cbeefea
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看板
未验证
提交
8cbeefea
编写于
4月 12, 2023
作者:
Z
Zhang Zheng
提交者:
GitHub
4月 12, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Optimize performance of unique kernel (#52736)
* Optimize performance of unique kernel * fix ci
上级
c376a940
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
41 addition
and
100 deletion
+41
-100
paddle/phi/kernels/gpu/unique_kernel.cu
paddle/phi/kernels/gpu/unique_kernel.cu
+41
-100
未找到文件。
paddle/phi/kernels/gpu/unique_kernel.cu
浏览文件 @
8cbeefea
...
...
@@ -30,6 +30,7 @@
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/core/tensor_utils.h"
#include "paddle/phi/kernels/funcs/unique_functor.h"
#include "paddle/phi/kernels/index_select_kernel.h"
namespace
phi
{
...
...
@@ -98,76 +99,6 @@ struct BinaryNotEqual {
}
};
// index_select() function for DenseTensor
template
<
typename
Context
,
typename
InT
,
typename
IndexT
>
void
IndexSelect
(
const
Context
&
context
,
const
DenseTensor
&
input
,
const
DenseTensor
&
index
,
DenseTensor
*
output
,
int
dim
)
{
auto
input_dim
=
input
.
dims
();
auto
input_dim_size
=
input_dim
.
size
();
auto
output_dim
=
output
->
dims
();
auto
slice_size
=
1
;
for
(
auto
i
=
dim
+
1
;
i
<
input_dim_size
;
i
++
)
{
slice_size
*=
input_dim
[
i
];
}
auto
input_width
=
slice_size
*
input_dim
[
dim
];
auto
output_width
=
slice_size
*
output_dim
[
dim
];
auto
outer_nums
=
1
;
for
(
auto
i
=
0
;
i
<
dim
;
i
++
)
{
outer_nums
*=
input_dim
[
i
];
}
auto
index_size
=
index
.
dims
()[
0
];
std
::
vector
<
InT
>
input_vec
;
std
::
vector
<
IndexT
>
index_vec
;
phi
::
TensorToVector
(
input
,
context
,
&
input_vec
);
phi
::
TensorToVector
(
index
,
context
,
&
index_vec
);
std
::
vector
<
InT
>
out_vec
(
output
->
numel
());
for
(
int
i
=
0
;
i
<
index_size
;
i
++
)
{
PADDLE_ENFORCE_GE
(
index_vec
[
i
],
0
,
phi
::
errors
::
InvalidArgument
(
"Variable value (index) of OP(index_select) "
"expected >= 0 and < %ld, but got %ld. Please check input "
"value."
,
input_dim
[
dim
],
index_vec
[
i
]));
PADDLE_ENFORCE_LT
(
index_vec
[
i
],
input_dim
[
dim
],
phi
::
errors
::
InvalidArgument
(
"Variable value (index) of OP(index_select) "
"expected >= 0 and < %ld, but got %ld. Please check input "
"value."
,
input_dim
[
dim
],
index_vec
[
i
]));
}
for
(
auto
i
=
0
;
i
<
outer_nums
;
i
++
)
{
auto
input_start_offset
=
i
*
input_width
;
auto
output_start_offset
=
i
*
output_width
;
for
(
auto
j
=
0
;
j
<
index_size
;
j
++
)
{
IndexT
index_value
=
index_vec
[
j
];
for
(
auto
k
=
0
;
k
<
slice_size
;
k
++
)
{
out_vec
[
output_start_offset
+
j
*
slice_size
+
k
]
=
input_vec
[
input_start_offset
+
index_value
*
slice_size
+
k
];
}
}
}
context
.
template
Alloc
<
IndexT
>(
output
);
phi
::
TensorFromVector
(
out_vec
,
context
,
output
);
output
->
Resize
(
output_dim
);
}
// The core logic of computing Unique for a flattend DenseTensor
template
<
typename
Context
,
typename
InT
,
...
...
@@ -354,24 +285,29 @@ static void UniqueDimsCUDATensor(const Context& context,
int
axis
)
{
// 1. Transpose & reshape
// Transpose tensor: eg. axis=1, [dim0, dim1, dim2] -> [dim1, dim0, dim2]
std
::
vector
<
int
>
permute
(
in
.
dims
().
size
());
std
::
iota
(
permute
.
begin
(),
permute
.
end
(),
0
);
permute
[
axis
]
=
0
;
permute
[
0
]
=
axis
;
std
::
vector
<
int64_t
>
in_trans_dims_vec
(
phi
::
vectorize
(
in
.
dims
()));
in_trans_dims_vec
[
axis
]
=
in
.
dims
()[
0
];
in_trans_dims_vec
[
0
]
=
in
.
dims
()[
axis
];
DenseTensor
in_trans
;
std
::
vector
<
int64_t
>
in_trans_dims_vec
(
phi
::
vectorize
(
in
.
dims
()));
auto
in_trans_dims
=
phi
::
make_ddim
(
in_trans_dims_vec
);
in_trans
.
Resize
(
in_trans_dims
);
context
.
template
Alloc
<
InT
>(
&
in_trans
);
phi
::
funcs
::
TransCompute
<
Context
,
InT
>
(
in
.
dims
().
size
(),
// num of dims
context
,
// device
in
,
// original DenseTensor
&
in_trans
,
// DenseTensor after reshape
permute
);
// index of axis
std
::
vector
<
int
>
permute
(
in
.
dims
().
size
());
bool
is_transpose
=
axis
!=
0
;
if
(
is_transpose
)
{
std
::
iota
(
permute
.
begin
(),
permute
.
end
(),
0
);
permute
[
axis
]
=
0
;
permute
[
0
]
=
axis
;
in_trans_dims_vec
[
axis
]
=
in
.
dims
()[
0
];
in_trans_dims_vec
[
0
]
=
in
.
dims
()[
axis
];
in_trans_dims
=
phi
::
make_ddim
(
in_trans_dims_vec
);
in_trans
.
Resize
(
in_trans_dims
);
context
.
template
Alloc
<
InT
>(
&
in_trans
);
phi
::
funcs
::
TransCompute
<
Context
,
InT
>
(
in
.
dims
().
size
(),
// num of dims
context
,
// device
in
,
// original DenseTensor
&
in_trans
,
// DenseTensor after reshape
permute
);
// index of axis
}
else
{
in_trans
.
ShareDataWith
(
in
);
}
// Reshape tensor: eg. [dim1, dim0, dim2] -> [dim1, dim0*dim2]
auto
in_trans_flat_dims
=
phi
::
flatten_to_2d
(
in_trans_dims
,
1
);
in_trans
.
Resize
(
in_trans_flat_dims
);
...
...
@@ -407,22 +343,27 @@ static void UniqueDimsCUDATensor(const Context& context,
row
);
// 3. Select indices and reshape back to get 'out'
DenseTensor
out_trans
;
std
::
vector
<
int64_t
>
out_trans_dims_vec
=
in_trans_dims_vec
;
out_trans_dims_vec
[
0
]
=
indices
->
numel
();
out_trans
.
Resize
(
phi
::
make_ddim
(
out_trans_dims_vec
));
context
.
template
Alloc
<
InT
>(
&
out_trans
);
IndexSelect
<
Context
,
InT
,
IndexT
>
(
context
,
in_trans
,
*
indices
,
&
out_trans
,
0
);
std
::
swap
(
out_trans_dims_vec
[
0
],
out_trans_dims_vec
[
axis
]);
out
->
Resize
(
phi
::
make_ddim
(
out_trans_dims_vec
));
context
.
template
Alloc
<
InT
>(
out
);
std
::
vector
<
DenseTensor
>
out_trans_unbind
=
phi
::
funcs
::
Unbind
(
out_trans
);
phi
::
funcs
::
ConcatFunctor
<
Context
,
InT
>
concat_functor
;
concat_functor
(
context
,
out_trans_unbind
,
0
,
&
out_trans
);
phi
::
funcs
::
TransCompute
<
Context
,
InT
>
(
out_trans
.
dims
().
size
(),
context
,
out_trans
,
out
,
permute
);
if
(
is_transpose
)
{
DenseTensor
out_trans
;
out_trans
.
Resize
(
phi
::
make_ddim
(
out_trans_dims_vec
));
context
.
template
Alloc
<
InT
>(
&
out_trans
);
phi
::
IndexSelectKernel
<
InT
,
Context
>
(
context
,
in_trans
,
*
indices
,
0
,
&
out_trans
);
std
::
swap
(
out_trans_dims_vec
[
0
],
out_trans_dims_vec
[
axis
]);
out
->
Resize
(
phi
::
make_ddim
(
out_trans_dims_vec
));
context
.
template
Alloc
<
InT
>(
out
);
phi
::
funcs
::
TransCompute
<
Context
,
InT
>
(
out_trans
.
dims
().
size
(),
context
,
out_trans
,
out
,
permute
);
}
else
{
out
->
Resize
(
phi
::
make_ddim
(
out_trans_dims_vec
));
context
.
template
Alloc
<
InT
>(
out
);
phi
::
IndexSelectKernel
<
InT
,
Context
>
(
context
,
in_trans
,
*
indices
,
0
,
out
);
}
}
// functor for processing a flattend DenseTensor
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录