Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
03ca04fe
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看板
未验证
提交
03ca04fe
编写于
8月 08, 2023
作者:
N
niuliling123
提交者:
GitHub
8月 08, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Modefied reduce op for store temp_data with MpType (#55709)
上级
d3c9c079
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
65 addition
and
36 deletion
+65
-36
paddle/phi/kernels/funcs/reduce_function.h
paddle/phi/kernels/funcs/reduce_function.h
+65
-36
未找到文件。
paddle/phi/kernels/funcs/reduce_function.h
浏览文件 @
03ca04fe
...
...
@@ -233,7 +233,7 @@ struct OneDimIndexCal {
};
// reduce config
template
<
typename
Ty
>
template
<
typename
Ty
,
typename
MPType
>
struct
ReduceConfig
{
ReduceConfig
(
const
std
::
vector
<
int
>&
origin_reduce_dims
,
const
std
::
vector
<
int
>&
origin_x_dim
)
...
...
@@ -250,7 +250,7 @@ struct ReduceConfig {
bool
should_reduce_again
=
false
;
bool
reduce_last_dim
=
false
;
bool
vectorize_input
=
false
;
Ty
*
output
_data
;
MPType
*
tmp
_data
;
dim3
block
;
dim3
grid
;
...
...
@@ -288,11 +288,9 @@ struct ReduceConfig {
const
KPDevice
&
dev_ctx
,
phi
::
DenseTensor
*
tmp
)
{
if
(
should_reduce_again
)
{
tmp
->
Resize
(
phi
::
make_ddim
(
{
static_cast
<
int64_t
>
(
left_num
*
grid
.
z
*
grid
.
y
*
sizeof
(
Ty
))}));
output_data
=
dev_ctx
.
Alloc
<
Ty
>
(
tmp
);
}
else
{
output_data
=
y_data
;
tmp
->
Resize
(
phi
::
make_ddim
({
static_cast
<
int64_t
>
(
left_num
*
grid
.
z
*
grid
.
y
)}));
tmp_data
=
dev_ctx
.
Alloc
<
MPType
>
(
tmp
);
}
}
...
...
@@ -583,7 +581,9 @@ __global__ void ReduceAnyKernel(const Tx* x,
const
Calculator
reduce_index_calculator
,
const
Calculator
left_index_calculator
,
const
kps
::
DimConfig
dim
,
bool
is_mean
)
{
bool
is_mean
,
MPType
*
tmp_data
,
bool
need_store_tmp
=
false
)
{
int
input_idx
,
left_idx
,
stride
;
int
block_size
=
0
;
bool
need_store
=
true
;
...
...
@@ -686,9 +686,15 @@ __global__ void ReduceAnyKernel(const Tx* x,
if
(
is_mean
)
{
reduce_var
=
reduce_var
/
static_cast
<
MPType
>
(
reduce_num
);
}
Ty
result
=
static_cast
<
Ty
>
(
reduce_var
);
kps
::
details
::
WriteData
<
Ty
>
(
y
+
store_offset
+
i
,
&
result
,
static_cast
<
int
>
(
need_store
));
if
(
!
need_store_tmp
)
{
Ty
result
=
static_cast
<
Ty
>
(
reduce_var
);
kps
::
details
::
WriteData
<
Ty
>
(
y
+
store_offset
+
i
,
&
result
,
static_cast
<
int
>
(
need_store
));
}
else
{
kps
::
details
::
WriteData
<
MPType
>
(
tmp_data
+
store_offset
+
i
,
&
reduce_var
,
static_cast
<
int
>
(
need_store
));
}
}
}
...
...
@@ -707,7 +713,9 @@ __global__ void ReduceHigherDimKernel(const Tx* x,
int
blocking_size
,
const
kps
::
DimConfig
dim
,
int
mean_div
,
bool
is_mean
)
{
bool
is_mean
,
MPType
*
tmp_data
,
bool
need_store_tmp
=
false
)
{
// when reduce_dim.size() == 1 and reduce_dim[0] != x_dim.size() - 1, this
// function will be used
auto
block
=
ReduceIndexMapping
<
false
>
(
dim
);
...
...
@@ -739,9 +747,14 @@ __global__ void ReduceHigherDimKernel(const Tx* x,
if
(
is_mean
)
{
reduce_var
=
reduce_var
/
static_cast
<
MPType
>
(
mean_div
);
}
Ty
result
=
static_cast
<
Ty
>
(
reduce_var
);
kps
::
WriteData
<
Ty
,
1
,
1
,
false
>
(
y
+
store_offset
+
idx
,
&
result
,
block
.
BlockDimX
());
if
(
!
need_store_tmp
)
{
Ty
result
=
static_cast
<
Ty
>
(
reduce_var
);
kps
::
WriteData
<
Ty
,
1
,
1
,
false
>
(
y
+
store_offset
+
idx
,
&
result
,
block
.
BlockDimX
());
}
else
{
kps
::
WriteData
<
MPType
,
1
,
1
,
false
>
(
tmp_data
+
store_offset
+
idx
,
&
reduce_var
,
block
.
BlockDimX
());
}
}
if
(
idx
<
left_num
)
{
...
...
@@ -763,8 +776,14 @@ __global__ void ReduceHigherDimKernel(const Tx* x,
if
(
is_mean
)
{
reduce_var
=
reduce_var
/
static_cast
<
MPType
>
(
mean_div
);
}
Ty
result
=
static_cast
<
Ty
>
(
reduce_var
);
kps
::
WriteData
<
Ty
,
1
,
1
,
true
>
(
y
+
store_offset
+
idx
,
&
result
,
dim
.
rem_x
);
if
(
!
need_store_tmp
)
{
Ty
result
=
static_cast
<
Ty
>
(
reduce_var
);
kps
::
WriteData
<
Ty
,
1
,
1
,
true
>
(
y
+
store_offset
+
idx
,
&
result
,
dim
.
rem_x
);
}
else
{
kps
::
WriteData
<
MPType
,
1
,
1
,
true
>
(
tmp_data
+
store_offset
+
idx
,
&
reduce_var
,
dim
.
rem_x
);
}
}
}
...
...
@@ -779,7 +798,7 @@ static void LaunchReduceKernel(const Tx* x_data,
const
TransformOp
&
transform
,
MPType
init
,
KPStream
stream
,
ReduceConfig
<
Ty
>
config
,
ReduceConfig
<
Ty
,
MPType
>
config
,
bool
is_mean
=
false
)
{
if
(
config
.
reduce_type
==
kReduceLastDim
)
{
int
stride_reduce
=
1
;
...
...
@@ -806,7 +825,7 @@ static void LaunchReduceKernel(const Tx* x_data,
ReduceAnyKernel
<
Tx
,
Ty
,
MPType
,
ReduceOp
,
TransformOp
,
OneDimIndexCal
>
<<<
grid_num
,
block_num
,
0
,
stream
>>>
(
x_data
,
config
.
output
_data
,
y
_data
,
reducer
,
transform
,
init
,
...
...
@@ -816,7 +835,9 @@ static void LaunchReduceKernel(const Tx* x_data,
reduce_index_calculator
,
left_index_calculator
,
dim
,
is_mean
&&
(
!
config
.
should_reduce_again
));
is_mean
&&
(
!
config
.
should_reduce_again
),
config
.
tmp_data
,
config
.
should_reduce_again
);
}
else
{
int
reduce_rank
=
config
.
reduce_strides
.
size
();
int
left_rank
=
config
.
left_strides
.
size
();
...
...
@@ -845,7 +866,7 @@ static void LaunchReduceKernel(const Tx* x_data,
ReduceAnyKernel
<
Tx
,
Ty
,
MPType
,
ReduceOp
,
TransformOp
,
IndexCalculator
>
<<<
grid_num
,
block_num
,
0
,
stream
>>>
(
x_data
,
config
.
output
_data
,
y
_data
,
reducer
,
transform
,
init
,
...
...
@@ -855,7 +876,9 @@ static void LaunchReduceKernel(const Tx* x_data,
reduce_index_calculator
,
left_index_calculator
,
dim
,
is_mean
&&
(
!
config
.
should_reduce_again
));
is_mean
&&
(
!
config
.
should_reduce_again
),
config
.
tmp_data
,
config
.
should_reduce_again
);
}
if
(
config
.
should_reduce_again
)
{
...
...
@@ -879,23 +902,25 @@ static void LaunchReduceKernel(const Tx* x_data,
auto
grid_size
=
grid
;
auto
block_size
=
block
;
#endif
ReduceHigherDimKernel
<
Ty
,
ReduceHigherDimKernel
<
MPType
,
Ty
,
MPType
,
ReduceOp
,
kps
::
IdentityFunctor
<
Ty
,
MPType
>>
kps
::
IdentityFunctor
<
MPType
,
MPType
>>
<<<
grid_size
,
block_size
,
0
,
stream
>>>
(
config
.
output
_data
,
config
.
tmp
_data
,
y_data
,
reducer
,
kps
::
IdentityFunctor
<
Ty
,
MPType
>
(),
kps
::
IdentityFunctor
<
MPType
,
MPType
>
(),
init
,
config
.
grid
.
y
,
config
.
left_num
,
config
.
grid
.
y
,
dim
,
config
.
reduce_num
,
is_mean
);
is_mean
,
config
.
tmp_data
,
false
);
}
}
...
...
@@ -1008,7 +1033,8 @@ void ReduceKernel(const KPDevice& dev_ctx,
return
;
}
auto
config
=
ReduceConfig
<
Ty
>
(
origin_reduce_dims
,
x_dim
);
using
MPType
=
typename
phi
::
dtype
::
MPTypeTrait
<
Ty
>::
Type
;
auto
config
=
ReduceConfig
<
Ty
,
MPType
>
(
origin_reduce_dims
,
x_dim
);
config
.
Run
(
dev_ctx
);
int
numel
=
x
.
numel
();
// after config.run()
...
...
@@ -1051,7 +1077,6 @@ void ReduceKernel(const KPDevice& dev_ctx,
}
#endif
using
MPType
=
typename
phi
::
dtype
::
MPTypeTrait
<
Ty
>::
Type
;
auto
reducer
=
ReduceOp
<
MPType
>
();
// launch ReduceHigherDimKernel
// when reduce_dim.size() == 1 and reduce_dim[0] != x_dim.size() - 1, this
...
...
@@ -1081,7 +1106,7 @@ void ReduceKernel(const KPDevice& dev_ctx,
ReduceHigherDimKernel
<
Tx
,
Ty
,
MPType
,
ReduceOp
<
MPType
>
,
TransformOp
>
<<<
grid_num
,
block_num
,
0
,
stream
>>>
(
x_data
,
config
.
output
_data
,
y
_data
,
reducer
,
transform
,
reducer
.
initial
(),
...
...
@@ -1090,7 +1115,9 @@ void ReduceKernel(const KPDevice& dev_ctx,
config
.
blocking_size
,
dim
,
config
.
reduce_num
,
is_mean
&&
(
!
config
.
should_reduce_again
));
is_mean
&&
(
!
config
.
should_reduce_again
),
config
.
tmp_data
,
config
.
should_reduce_again
);
if
(
config
.
should_reduce_again
)
{
dim3
block
=
dim3
(
config
.
block
.
x
,
1
,
1
);
...
...
@@ -1106,23 +1133,25 @@ void ReduceKernel(const KPDevice& dev_ctx,
auto
grid_size
=
grid
;
auto
block_size
=
block
;
#endif
ReduceHigherDimKernel
<
Ty
,
ReduceHigherDimKernel
<
MPType
,
Ty
,
MPType
,
ReduceOp
<
MPType
>
,
kps
::
IdentityFunctor
<
Ty
,
MPType
>>
kps
::
IdentityFunctor
<
MPType
,
MPType
>>
<<<
grid_size
,
block_size
,
0
,
stream
>>>
(
config
.
output
_data
,
config
.
tmp
_data
,
y_data
,
reducer
,
kps
::
IdentityFunctor
<
Ty
,
MPType
>
(
config
.
grid
.
y
),
kps
::
IdentityFunctor
<
MPType
,
MPType
>
(
config
.
grid
.
y
),
reducer
.
initial
(),
config
.
grid
.
y
,
config
.
left_num
,
config
.
grid
.
y
,
dim2
,
config
.
reduce_num
,
is_mean
);
is_mean
,
config
.
tmp_data
,
false
);
}
return
;
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录