Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
6883403f
P
Paddle
项目概览
机器未来
/
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看板
未验证
提交
6883403f
编写于
7月 20, 2021
作者:
C
crystal
提交者:
GitHub
7月 20, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
optimization of index_select op backward (#32955)
上级
3a5f1f22
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
54 addition
and
31 deletion
+54
-31
paddle/fluid/operators/index_select_op.h
paddle/fluid/operators/index_select_op.h
+54
-31
未找到文件。
paddle/fluid/operators/index_select_op.h
浏览文件 @
6883403f
...
...
@@ -15,6 +15,10 @@
#pragma once
#include <vector>
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/operators/jit/macro.h"
#include "paddle/fluid/operators/math/blas.h"
#include "paddle/fluid/operators/math/math_function.h"
#include "paddle/fluid/platform/cpu_info.h"
namespace
paddle
{
namespace
operators
{
...
...
@@ -38,7 +42,6 @@ void IndexSelectInner(const framework::ExecutionContext& context,
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
];
...
...
@@ -77,7 +80,6 @@ void IndexSelectInner(const framework::ExecutionContext& context,
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
++
)
{
...
...
@@ -98,7 +100,6 @@ class IndexSelectKernel : public framework::OpKernel<T> {
auto
*
inputs_var
=
context
.
InputVar
(
"X"
);
auto
*
index_var
=
context
.
InputVar
(
"Index"
);
auto
*
output_var
=
context
.
OutputVar
(
"Out"
);
auto
&
inputs
=
inputs_var
->
Get
<
LoDTensor
>
();
auto
&
index
=
index_var
->
Get
<
LoDTensor
>
();
auto
*
output
=
output_var
->
GetMutable
<
framework
::
LoDTensor
>
();
...
...
@@ -107,8 +108,8 @@ class IndexSelectKernel : public framework::OpKernel<T> {
if
(
dim
<
0
)
{
dim
+=
inputs
.
dims
().
size
();
}
const
auto
&
index_type
=
index
.
type
();
bool
index_type_match
=
index_type
==
framework
::
proto
::
VarType
::
INT32
||
index_type
==
framework
::
proto
::
VarType
::
INT64
;
PADDLE_ENFORCE_EQ
(
index_type_match
,
true
,
...
...
@@ -129,19 +130,41 @@ class IndexSelectKernel : public framework::OpKernel<T> {
}
};
template
<
typename
T
,
typename
IndexT
=
int
>
template
<
typename
DeviceContext
,
typename
T
,
class
Enable
=
void
>
struct
IndexSelectAdd
{
void
operator
()(
const
framework
::
ExecutionContext
&
ctx
,
int
slice_size
,
const
T
*
src_pointer
,
const
T
*
p_pointer
,
T
*
dist_pointer
)
{
for
(
int
i
=
0
;
i
<
slice_size
;
i
++
)
{
dist_pointer
[
i
]
=
src_pointer
[
i
]
+
p_pointer
[
i
];
}
}
};
template
<
typename
DeviceContext
,
typename
T
>
struct
IndexSelectAdd
<
DeviceContext
,
T
,
typename
std
::
enable_if
<
std
::
is_floating_point
<
T
>::
value
>::
type
>
{
void
operator
()(
const
framework
::
ExecutionContext
&
ctx
,
int
slice_size
,
const
T
*
src_pointer
,
const
T
*
p_pointer
,
T
*
dist_pointer
)
{
auto
blas
=
math
::
GetBlas
<
DeviceContext
,
T
>
(
ctx
);
blas
.
VADD
(
slice_size
,
src_pointer
,
p_pointer
,
dist_pointer
);
}
};
template
<
typename
DeviceContext
,
typename
T
,
typename
IndexT
=
int
>
void
IndexSelectGradInner
(
const
framework
::
ExecutionContext
&
context
,
const
LoDTensor
&
out_grad
,
const
LoDTensor
&
index
,
const
LoDTensor
*
out_grad
,
const
LoDTensor
*
index
,
LoDTensor
*
x_grad
,
int
dim
)
{
std
::
vector
<
T
>
input_vec
;
std
::
vector
<
IndexT
>
index_vec
;
TensorToVector
(
out_grad
,
context
.
device_context
(),
&
input_vec
);
TensorToVector
(
index
,
context
.
device_context
(),
&
index_vec
);
auto
input_dim
=
out_grad
.
dims
();
const
T
*
input_data
=
out_grad
->
data
<
T
>
();
const
IndexT
*
index_data
=
index
->
data
<
IndexT
>
();
const
T
*
p_output
=
x_grad
->
mutable_data
<
T
>
(
context
.
GetPlace
());
T
*
out_data
=
x_grad
->
mutable_data
<
T
>
(
context
.
GetPlace
());
auto
input_dim
=
out_grad
->
dims
();
auto
input_dim_size
=
input_dim
.
size
();
auto
output_dim
=
x_grad
->
dims
();
std
::
vector
<
T
>
out_vec
(
x_grad
->
numel
(),
0
);
auto
&
dev_ctx
=
context
.
template
device_context
<
DeviceContext
>();
math
::
SetConstant
<
DeviceContext
,
T
>
set_constant
;
set_constant
(
dev_ctx
,
x_grad
,
static_cast
<
T
>
(
0.0
));
auto
slice_size
=
1
;
for
(
auto
i
=
dim
+
1
;
i
<
input_dim_size
;
i
++
)
{
...
...
@@ -156,7 +179,7 @@ void IndexSelectGradInner(const framework::ExecutionContext& context,
outer_nums
*=
input_dim
[
i
];
}
auto
index_size
=
index
.
dims
()[
0
];
auto
index_size
=
index
->
dims
()[
0
];
VLOG
(
3
)
<<
"Index_Select_Grad_Debug; outer_nums: "
<<
outer_nums
<<
"; slice_size: "
<<
slice_size
<<
"; input_width: "
<<
input_width
<<
"; output_width: "
<<
output_width
...
...
@@ -167,15 +190,14 @@ void IndexSelectGradInner(const framework::ExecutionContext& context,
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
+
index_value
*
slice_size
+
k
]
+=
input_vec
[
input_start_offset
+
j
*
slice_size
+
k
];
}
IndexT
index_value
=
index_data
[
j
];
auto
src
=
input_data
+
input_start_offset
+
j
*
slice_size
;
auto
p_out
=
p_output
+
output_start_offset
+
index_value
*
slice_size
;
auto
dst
=
out_data
+
output_start_offset
+
index_value
*
slice_size
;
IndexSelectAdd
<
DeviceContext
,
T
>
index_select_add
;
index_select_add
(
context
,
slice_size
,
src
,
p_out
,
dst
);
}
}
x_grad
->
mutable_data
<
T
>
(
context
.
GetPlace
());
framework
::
TensorFromVector
(
out_vec
,
context
.
device_context
(),
x_grad
);
x_grad
->
Resize
(
output_dim
);
}
...
...
@@ -183,19 +205,18 @@ template <typename DeviceContext, typename T>
class
IndexSelectGradKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
auto
*
index_var
=
context
.
InputVar
(
"Index"
);
auto
*
x_grad_var
=
context
.
OutputVar
(
framework
::
GradVarName
(
"X"
));
auto
*
out_grad_var
=
context
.
InputVar
(
framework
::
GradVarName
(
"Out"
));
auto
*
x_grad
=
context
.
Output
<
framework
::
LoDTensor
>
(
framework
::
GradVarName
(
"X"
));
auto
*
index
=
context
.
Input
<
framework
::
LoDTensor
>
(
"Index"
);
auto
*
out_grad
=
context
.
Input
<
framework
::
LoDTensor
>
(
framework
::
GradVarName
(
"Out"
));
auto
&
index
=
index_var
->
Get
<
LoDTensor
>
();
auto
&
out_grad
=
out_grad_var
->
Get
<
LoDTensor
>
();
auto
*
x_grad
=
x_grad_var
->
GetMutable
<
framework
::
LoDTensor
>
();
int
dim
=
context
.
Attr
<
int
>
(
"dim"
);
if
(
dim
<
0
)
{
dim
+=
out_grad
.
dims
().
size
();
dim
+=
out_grad
->
dims
().
size
();
}
const
auto
&
index_type
=
index
->
type
();
const
auto
&
index_type
=
index
.
type
();
bool
index_type_match
=
index_type
==
framework
::
proto
::
VarType
::
INT32
||
index_type
==
framework
::
proto
::
VarType
::
INT64
;
PADDLE_ENFORCE_EQ
(
index_type_match
,
true
,
...
...
@@ -209,9 +230,11 @@ class IndexSelectGradKernel : public framework::OpKernel<T> {
framework
::
proto
::
VarType
::
INT64
)));
if
(
index_type
==
framework
::
proto
::
VarType
::
INT32
)
{
IndexSelectGradInner
<
T
,
int
>
(
context
,
out_grad
,
index
,
x_grad
,
dim
);
IndexSelectGradInner
<
DeviceContext
,
T
,
int
>
(
context
,
out_grad
,
index
,
x_grad
,
dim
);
}
else
if
(
index_type
==
framework
::
proto
::
VarType
::
INT64
)
{
IndexSelectGradInner
<
T
,
int64_t
>
(
context
,
out_grad
,
index
,
x_grad
,
dim
);
IndexSelectGradInner
<
DeviceContext
,
T
,
int64_t
>
(
context
,
out_grad
,
index
,
x_grad
,
dim
);
}
}
};
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录