Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleDetection
提交
f3cdeb9a
P
PaddleDetection
项目概览
PaddlePaddle
/
PaddleDetection
大约 1 年 前同步成功
通知
695
Star
11112
Fork
2696
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
184
列表
看板
标记
里程碑
合并请求
40
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleDetection
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
184
Issue
184
列表
看板
标记
里程碑
合并请求
40
合并请求
40
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
f3cdeb9a
编写于
3月 08, 2018
作者:
C
chengduo
提交者:
GitHub
3月 08, 2018
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #8820 from chengduoZH/feature/refine_elementwise_
[Speed] Refine elementwise sub,div,min,max gradient functor
上级
e1348e18
8b30fada
变更
5
显示空白变更内容
内联
并排
Showing
5 changed file
with
34 addition
and
277 deletion
+34
-277
paddle/fluid/operators/elementwise_div_op.h
paddle/fluid/operators/elementwise_div_op.h
+7
-72
paddle/fluid/operators/elementwise_max_op.h
paddle/fluid/operators/elementwise_max_op.h
+8
-71
paddle/fluid/operators/elementwise_min_op.h
paddle/fluid/operators/elementwise_min_op.h
+8
-71
paddle/fluid/operators/elementwise_mul_op.h
paddle/fluid/operators/elementwise_mul_op.h
+5
-6
paddle/fluid/operators/elementwise_sub_op.h
paddle/fluid/operators/elementwise_sub_op.h
+6
-57
未找到文件。
paddle/fluid/operators/elementwise_div_op.h
浏览文件 @
f3cdeb9a
...
...
@@ -41,77 +41,14 @@ class ElementwiseDivKernel : public framework::OpKernel<T> {
};
template
<
typename
T
>
struct
ElementwiseDivGradFunctor
{
template
<
typename
Device
,
typename
X
,
typename
Y
,
typename
Z
,
typename
dX
,
typename
dY
,
typename
dZ
>
void
operator
()(
Device
d
,
X
x
,
Y
y
,
Z
z
,
dX
dx
,
dY
dy
,
dZ
dz
)
{
auto
y_e
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
y
);
auto
z_e
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
z
);
auto
dz_e
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
dz
);
if
(
dx
)
{
auto
dx_e
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
dx
);
dx_e
.
device
(
d
)
=
dz_e
/
y_e
;
}
if
(
dy
)
{
auto
dy_e
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
dy
);
dy_e
.
device
(
d
)
=
-
1.0
*
dz_e
*
z_e
/
y_e
;
}
}
};
template
<
typename
T
>
struct
ElementwiseDivBroadCastGradFunctor
{
template
<
typename
Device
,
typename
X
,
typename
Y
,
typename
Z
,
typename
dX
,
typename
dY
,
typename
dZ
,
typename
Pre
,
typename
N
>
void
operator
()(
Device
d
,
X
x
,
Y
y
,
Z
z
,
dX
dx
,
dY
dy
,
dZ
dz
,
Pre
pre
,
N
n
)
{
auto
x_e
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
x
);
auto
y_e
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
y
);
auto
dz_e
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
dz
);
auto
y_e_bcast
=
y_e
.
reshape
(
Eigen
::
DSizes
<
int
,
2
>
(
1
,
n
))
.
broadcast
(
Eigen
::
DSizes
<
int
,
2
>
(
pre
,
1
))
.
reshape
(
Eigen
::
DSizes
<
int
,
1
>
(
x_e
.
size
()));
if
(
dx
)
{
auto
dx_e
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
dx
);
dx_e
.
device
(
d
)
=
dz_e
/
y_e_bcast
;
}
if
(
dy
)
{
auto
dy_e
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
dy
);
dy_e
.
device
(
d
)
=
(
-
1.0
*
(
x_e
*
dz_e
)
/
(
y_e_bcast
*
y_e_bcast
))
.
reshape
(
Eigen
::
DSizes
<
int
,
2
>
(
pre
,
n
))
.
sum
(
Eigen
::
array
<
int
,
1
>
{{
0
}});
}
}
struct
DivGradDX
{
HOSTDEVICE
T
operator
()(
T
x
,
T
y
,
T
out
,
T
dout
)
const
{
return
dout
/
y
;
}
};
template
<
typename
T
>
struct
ElementwiseDivBroadCast2GradFunctor
{
template
<
typename
Device
,
typename
X
,
typename
Y
,
typename
Z
,
typename
dX
,
typename
dY
,
typename
dZ
,
typename
Pre
,
typename
N
,
typename
Post
>
void
operator
()(
Device
d
,
X
x
,
Y
y
,
Z
z
,
dX
dx
,
dY
dy
,
dZ
dz
,
Pre
pre
,
N
n
,
Post
post
)
{
auto
x_e
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
x
);
auto
y_e
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
y
);
auto
dz_e
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
dz
);
auto
y_e_bcast
=
y_e
.
reshape
(
Eigen
::
DSizes
<
int
,
3
>
(
1
,
n
,
1
))
.
broadcast
(
Eigen
::
DSizes
<
int
,
3
>
(
pre
,
1
,
post
))
.
reshape
(
Eigen
::
DSizes
<
int
,
1
>
(
x_e
.
size
()));
if
(
dx
)
{
auto
dx_e
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
dx
);
dx_e
.
device
(
d
)
=
dz_e
/
y_e_bcast
;
}
if
(
dy
)
{
auto
dy_e
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
dy
);
dy_e
.
device
(
d
)
=
(
-
1.0
*
(
x_e
*
dz_e
)
/
(
y_e_bcast
*
y_e_bcast
))
.
reshape
(
Eigen
::
DSizes
<
int
,
3
>
(
pre
,
n
,
post
))
.
sum
(
Eigen
::
array
<
int
,
2
>
{{
0
,
2
}});
}
struct
DivGradDY
{
HOSTDEVICE
T
operator
()(
T
x
,
T
y
,
T
out
,
T
dout
)
const
{
return
-
dout
*
x
/
(
y
*
y
);
}
};
...
...
@@ -128,10 +65,8 @@ class ElementwiseDivGradKernel : public framework::OpKernel<T> {
auto
*
dx
=
ctx
.
Output
<
Tensor
>
(
framework
::
GradVarName
(
"X"
));
auto
*
dy
=
ctx
.
Output
<
Tensor
>
(
framework
::
GradVarName
(
"Y"
));
int
axis
=
ctx
.
Attr
<
int
>
(
"axis"
);
ElementwiseGradCompute
<
DeviceContext
,
T
,
ElementwiseDivGradFunctor
<
T
>
,
ElementwiseDivBroadCastGradFunctor
<
T
>
,
ElementwiseDivBroadCast2GradFunctor
<
T
>>
(
ctx
,
x
,
y
,
out
,
dout
,
axis
,
dx
,
dy
);
ElemwiseGradCompute
<
DeviceContext
,
T
,
DivGradDX
<
T
>
,
DivGradDY
<
T
>>
(
ctx
,
*
x
,
*
y
,
*
out
,
*
dout
,
axis
,
dx
,
dy
,
DivGradDX
<
T
>
(),
DivGradDY
<
T
>
());
}
};
...
...
paddle/fluid/operators/elementwise_max_op.h
浏览文件 @
f3cdeb9a
...
...
@@ -41,76 +41,16 @@ class ElementwiseMaxKernel : public framework::OpKernel<T> {
};
template
<
typename
T
>
struct
ElementwiseMaxGradFunctor
{
template
<
typename
Device
,
typename
X
,
typename
Y
,
typename
Z
,
typename
dX
,
typename
dY
,
typename
dZ
>
void
operator
()(
Device
d
,
X
x
,
Y
y
,
Z
z
,
dX
dx
,
dY
dy
,
dZ
dz
)
{
auto
x_e
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
x
);
auto
y_e
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
y
);
auto
dz_e
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
dz
);
if
(
dx
)
{
auto
dx_e
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
dx
);
dx_e
.
device
(
d
)
=
(
x_e
>
y_e
).
template
cast
<
T
>()
*
dz_e
;
}
if
(
dy
)
{
auto
dy_e
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
dy
);
dy_e
.
device
(
d
)
=
(
x_e
<=
y_e
).
template
cast
<
T
>()
*
dz_e
;
}
struct
MaxGradDx
{
HOSTDEVICE
T
operator
()(
T
x
,
T
y
,
T
out
,
T
dout
)
const
{
return
dout
*
(
x
>
y
);
}
};
template
<
typename
T
>
struct
ElementwiseMaxBroadCastGradFunctor
{
template
<
typename
Device
,
typename
X
,
typename
Y
,
typename
Z
,
typename
dX
,
typename
dY
,
typename
dZ
,
typename
Pre
,
typename
N
>
void
operator
()(
Device
d
,
X
x
,
Y
y
,
Z
z
,
dX
dx
,
dY
dy
,
dZ
dz
,
Pre
pre
,
N
n
)
{
auto
x_e
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
x
);
auto
y_e
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
y
);
auto
dz_e
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
dz
);
auto
y_e_bcast
=
y_e
.
reshape
(
Eigen
::
DSizes
<
int
,
2
>
(
1
,
n
))
.
broadcast
(
Eigen
::
DSizes
<
int
,
2
>
(
pre
,
1
))
.
reshape
(
Eigen
::
DSizes
<
int
,
1
>
(
x_e
.
size
()));
if
(
dx
)
{
auto
dx_e
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
dx
);
dx_e
.
device
(
d
)
=
(
x_e
>
y_e_bcast
).
template
cast
<
T
>()
*
dz_e
;
}
if
(
dy
)
{
auto
dy_e
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
dy
);
dy_e
.
device
(
d
)
=
((
x_e
<=
y_e_bcast
).
template
cast
<
T
>()
*
dz_e
)
.
reshape
(
Eigen
::
DSizes
<
int
,
2
>
(
pre
,
n
))
.
sum
(
Eigen
::
array
<
int
,
1
>
{{
0
}});
}
}
};
template
<
typename
T
>
struct
ElementwiseMaxBroadCast2GradFunctor
{
template
<
typename
Device
,
typename
X
,
typename
Y
,
typename
Z
,
typename
dX
,
typename
dY
,
typename
dZ
,
typename
Pre
,
typename
N
,
typename
Post
>
void
operator
()(
Device
d
,
X
x
,
Y
y
,
Z
z
,
dX
dx
,
dY
dy
,
dZ
dz
,
Pre
pre
,
N
n
,
Post
post
)
{
auto
x_e
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
x
);
auto
y_e
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
y
);
auto
dz_e
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
dz
);
auto
y_e_bcast
=
y_e
.
reshape
(
Eigen
::
DSizes
<
int
,
3
>
(
1
,
n
,
1
))
.
broadcast
(
Eigen
::
DSizes
<
int
,
3
>
(
pre
,
1
,
post
))
.
reshape
(
Eigen
::
DSizes
<
int
,
1
>
(
x_e
.
size
()));
if
(
dx
)
{
auto
dx_e
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
dx
);
dx_e
.
device
(
d
)
=
(
x_e
>
y_e_bcast
).
template
cast
<
T
>()
*
dz_e
;
}
if
(
dy
)
{
auto
dy_e
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
dy
);
dy_e
.
device
(
d
)
=
((
x_e
<=
y_e_bcast
).
template
cast
<
T
>()
*
dz_e
)
.
reshape
(
Eigen
::
DSizes
<
int
,
3
>
(
pre
,
n
,
post
))
.
sum
(
Eigen
::
array
<
int
,
2
>
{{
0
,
2
}});
}
struct
MaxGradDy
{
HOSTDEVICE
T
operator
()(
T
x
,
T
y
,
T
out
,
T
dout
)
const
{
return
dout
*
(
x
<=
y
);
}
};
...
...
@@ -127,12 +67,9 @@ class ElementwiseMaxGradKernel : public framework::OpKernel<T> {
auto
*
dx
=
ctx
.
Output
<
Tensor
>
(
framework
::
GradVarName
(
"X"
));
auto
*
dy
=
ctx
.
Output
<
Tensor
>
(
framework
::
GradVarName
(
"Y"
));
int
axis
=
ctx
.
Attr
<
int
>
(
"axis"
);
ElementwiseGradCompute
<
DeviceContext
,
T
,
ElementwiseMaxGradFunctor
<
T
>
,
ElementwiseMaxBroadCastGradFunctor
<
T
>
,
ElementwiseMaxBroadCast2GradFunctor
<
T
>>
(
ctx
,
x
,
y
,
out
,
dout
,
axis
,
dx
,
dy
);
ElemwiseGradCompute
<
DeviceContext
,
T
,
MaxGradDx
<
T
>
,
MaxGradDy
<
T
>>
(
ctx
,
*
x
,
*
y
,
*
out
,
*
dout
,
axis
,
dx
,
dy
,
MaxGradDx
<
T
>
(),
MaxGradDy
<
T
>
());
}
};
}
// namespace operators
}
// namespace paddle
paddle/fluid/operators/elementwise_min_op.h
浏览文件 @
f3cdeb9a
...
...
@@ -41,76 +41,16 @@ class ElementwiseMinKernel : public framework::OpKernel<T> {
};
template
<
typename
T
>
struct
ElementwiseMinGradFunctor
{
template
<
typename
Device
,
typename
X
,
typename
Y
,
typename
Z
,
typename
dX
,
typename
dY
,
typename
dZ
>
void
operator
()(
Device
d
,
X
x
,
Y
y
,
Z
z
,
dX
dx
,
dY
dy
,
dZ
dz
)
{
auto
x_e
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
x
);
auto
y_e
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
y
);
auto
dz_e
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
dz
);
if
(
dx
)
{
auto
dx_e
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
dx
);
dx_e
.
device
(
d
)
=
(
x_e
<
y_e
).
template
cast
<
T
>()
*
dz_e
;
}
if
(
dy
)
{
auto
dy_e
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
dy
);
dy_e
.
device
(
d
)
=
(
x_e
>=
y_e
).
template
cast
<
T
>()
*
dz_e
;
}
struct
MinGradDx
{
HOSTDEVICE
T
operator
()(
T
x
,
T
y
,
T
out
,
T
dout
)
const
{
return
dout
*
(
x
<
y
);
}
};
template
<
typename
T
>
struct
ElementwiseMinBroadCastGradFunctor
{
template
<
typename
Device
,
typename
X
,
typename
Y
,
typename
Z
,
typename
dX
,
typename
dY
,
typename
dZ
,
typename
Pre
,
typename
N
>
void
operator
()(
Device
d
,
X
x
,
Y
y
,
Z
z
,
dX
dx
,
dY
dy
,
dZ
dz
,
Pre
pre
,
N
n
)
{
auto
x_e
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
x
);
auto
y_e
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
y
);
auto
dz_e
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
dz
);
auto
y_e_bcast
=
y_e
.
reshape
(
Eigen
::
DSizes
<
int
,
2
>
(
1
,
n
))
.
broadcast
(
Eigen
::
DSizes
<
int
,
2
>
(
pre
,
1
))
.
reshape
(
Eigen
::
DSizes
<
int
,
1
>
(
x_e
.
size
()));
if
(
dx
)
{
auto
dx_e
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
dx
);
dx_e
.
device
(
d
)
=
(
x_e
<
y_e_bcast
).
template
cast
<
T
>()
*
dz_e
;
}
if
(
dy
)
{
auto
dy_e
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
dy
);
dy_e
.
device
(
d
)
=
((
x_e
>=
y_e_bcast
).
template
cast
<
T
>()
*
dz_e
)
.
reshape
(
Eigen
::
DSizes
<
int
,
2
>
(
pre
,
n
))
.
sum
(
Eigen
::
array
<
int
,
1
>
{{
0
}});
}
}
};
template
<
typename
T
>
struct
ElementwiseMinBroadCast2GradFunctor
{
template
<
typename
Device
,
typename
X
,
typename
Y
,
typename
Z
,
typename
dX
,
typename
dY
,
typename
dZ
,
typename
Pre
,
typename
N
,
typename
Post
>
void
operator
()(
Device
d
,
X
x
,
Y
y
,
Z
z
,
dX
dx
,
dY
dy
,
dZ
dz
,
Pre
pre
,
N
n
,
Post
post
)
{
auto
x_e
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
x
);
auto
y_e
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
y
);
auto
dz_e
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
dz
);
auto
y_e_bcast
=
y_e
.
reshape
(
Eigen
::
DSizes
<
int
,
3
>
(
1
,
n
,
1
))
.
broadcast
(
Eigen
::
DSizes
<
int
,
3
>
(
pre
,
1
,
post
))
.
reshape
(
Eigen
::
DSizes
<
int
,
1
>
(
x_e
.
size
()));
if
(
dx
)
{
auto
dx_e
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
dx
);
dx_e
.
device
(
d
)
=
(
x_e
<
y_e_bcast
).
template
cast
<
T
>()
*
dz_e
;
}
if
(
dy
)
{
auto
dy_e
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
dy
);
dy_e
.
device
(
d
)
=
((
x_e
>=
y_e_bcast
).
template
cast
<
T
>()
*
dz_e
)
.
reshape
(
Eigen
::
DSizes
<
int
,
3
>
(
pre
,
n
,
post
))
.
sum
(
Eigen
::
array
<
int
,
2
>
{{
0
,
2
}});
}
struct
MinGradDy
{
HOSTDEVICE
T
operator
()(
T
x
,
T
y
,
T
out
,
T
dout
)
const
{
return
dout
*
(
x
>=
y
);
}
};
...
...
@@ -127,12 +67,9 @@ class ElementwiseMinGradKernel : public framework::OpKernel<T> {
auto
*
dx
=
ctx
.
Output
<
Tensor
>
(
framework
::
GradVarName
(
"X"
));
auto
*
dy
=
ctx
.
Output
<
Tensor
>
(
framework
::
GradVarName
(
"Y"
));
int
axis
=
ctx
.
Attr
<
int
>
(
"axis"
);
ElementwiseGradCompute
<
DeviceContext
,
T
,
ElementwiseMinGradFunctor
<
T
>
,
ElementwiseMinBroadCastGradFunctor
<
T
>
,
ElementwiseMinBroadCast2GradFunctor
<
T
>>
(
ctx
,
x
,
y
,
out
,
dout
,
axis
,
dx
,
dy
);
ElemwiseGradCompute
<
DeviceContext
,
T
,
MinGradDx
<
T
>
,
MinGradDy
<
T
>>
(
ctx
,
*
x
,
*
y
,
*
out
,
*
dout
,
axis
,
dx
,
dy
,
MinGradDx
<
T
>
(),
MinGradDy
<
T
>
());
}
};
}
// namespace operators
}
// namespace paddle
paddle/fluid/operators/elementwise_mul_op.h
浏览文件 @
f3cdeb9a
...
...
@@ -40,14 +40,15 @@ class ElementwiseMulKernel : public framework::OpKernel<T> {
};
template
<
typename
T
>
struct
IdentityGrad_
DX
{
struct
MulGrad
DX
{
HOSTDEVICE
T
operator
()(
T
x
,
T
y
,
T
out
,
T
dout
)
const
{
return
dout
*
y
;
}
};
template
<
typename
T
>
struct
IdentityGrad_
DY
{
struct
MulGrad
DY
{
HOSTDEVICE
T
operator
()(
T
x
,
T
y
,
T
out
,
T
dout
)
const
{
return
dout
*
x
;
}
};
template
<
typename
DeviceContext
,
typename
T
>
class
ElementwiseMulGradKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
...
...
@@ -61,10 +62,8 @@ class ElementwiseMulGradKernel : public framework::OpKernel<T> {
auto
*
dx
=
ctx
.
Output
<
Tensor
>
(
framework
::
GradVarName
(
"X"
));
auto
*
dy
=
ctx
.
Output
<
Tensor
>
(
framework
::
GradVarName
(
"Y"
));
int
axis
=
ctx
.
Attr
<
int
>
(
"axis"
);
ElemwiseGradCompute
<
DeviceContext
,
T
,
IdentityGrad_DX
<
T
>
,
IdentityGrad_DY
<
T
>>
(
ctx
,
*
x
,
*
y
,
*
out
,
*
dout
,
axis
,
dx
,
dy
,
IdentityGrad_DX
<
T
>
(),
IdentityGrad_DY
<
T
>
());
ElemwiseGradCompute
<
DeviceContext
,
T
,
MulGradDX
<
T
>
,
MulGradDY
<
T
>>
(
ctx
,
*
x
,
*
y
,
*
out
,
*
dout
,
axis
,
dx
,
dy
,
MulGradDX
<
T
>
(),
MulGradDY
<
T
>
());
}
};
}
// namespace operators
...
...
paddle/fluid/operators/elementwise_sub_op.h
浏览文件 @
f3cdeb9a
...
...
@@ -40,61 +40,13 @@ class ElementwiseSubKernel : public framework::OpKernel<T> {
};
template
<
typename
T
>
struct
ElementwiseSubGradFunctor
{
template
<
typename
Device
,
typename
X
,
typename
Y
,
typename
Z
,
typename
dX
,
typename
dY
,
typename
dZ
>
void
operator
()(
Device
d
,
X
x
,
Y
y
,
Z
z
,
dX
dx
,
dY
dy
,
dZ
dz
)
{
auto
dz_e
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
dz
);
if
(
dx
)
{
auto
dx_e
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
dx
);
dx_e
.
device
(
d
)
=
dz_e
;
}
if
(
dy
)
{
auto
dy_e
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
dy
);
dy_e
.
device
(
d
)
=
(
-
1.0
)
*
dz_e
;
}
}
struct
SubGradDX
{
HOSTDEVICE
T
operator
()(
T
x
,
T
y
,
T
out
,
T
dout
)
const
{
return
dout
;
}
};
template
<
typename
T
>
struct
ElementwiseSubBroadCastGradFunctor
{
template
<
typename
Device
,
typename
X
,
typename
Y
,
typename
Z
,
typename
dX
,
typename
dY
,
typename
dZ
,
typename
Pre
,
typename
N
>
void
operator
()(
Device
d
,
X
x
,
Y
y
,
Z
z
,
dX
dx
,
dY
dy
,
dZ
dz
,
Pre
pre
,
N
n
)
{
auto
dz_e
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
dz
);
if
(
dx
)
{
auto
dx_e
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
dx
);
dx_e
.
device
(
d
)
=
dz_e
;
}
if
(
dy
)
{
auto
dy_e
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
dy
);
dy_e
.
device
(
d
)
=
(
-
1.0
)
*
dz_e
.
reshape
(
Eigen
::
DSizes
<
int
,
2
>
(
pre
,
n
))
.
sum
(
Eigen
::
array
<
int
,
1
>
{{
0
}});
}
}
};
template
<
typename
T
>
struct
ElementwiseSubBroadCast2GradFunctor
{
template
<
typename
Device
,
typename
X
,
typename
Y
,
typename
Z
,
typename
dX
,
typename
dY
,
typename
dZ
,
typename
Pre
,
typename
N
,
typename
Post
>
void
operator
()(
Device
d
,
X
x
,
Y
y
,
Z
z
,
dX
dx
,
dY
dy
,
dZ
dz
,
Pre
pre
,
N
n
,
Post
post
)
{
auto
dz_e
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
dz
);
if
(
dx
)
{
auto
dx_e
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
dx
);
dx_e
.
device
(
d
)
=
dz_e
;
}
if
(
dy
)
{
auto
dy_e
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
dy
);
dy_e
.
device
(
d
)
=
(
-
1.0
)
*
dz_e
.
reshape
(
Eigen
::
DSizes
<
int
,
3
>
(
pre
,
n
,
post
))
.
sum
(
Eigen
::
array
<
int
,
2
>
{{
0
,
2
}});
}
}
struct
SubGradDY
{
HOSTDEVICE
T
operator
()(
T
x
,
T
y
,
T
out
,
T
dout
)
const
{
return
-
dout
;
}
};
template
<
typename
DeviceContext
,
typename
T
>
...
...
@@ -110,12 +62,9 @@ class ElementwiseSubGradKernel : public framework::OpKernel<T> {
auto
*
dx
=
ctx
.
Output
<
Tensor
>
(
framework
::
GradVarName
(
"X"
));
auto
*
dy
=
ctx
.
Output
<
Tensor
>
(
framework
::
GradVarName
(
"Y"
));
int
axis
=
ctx
.
Attr
<
int
>
(
"axis"
);
ElementwiseGradCompute
<
DeviceContext
,
T
,
ElementwiseSubGradFunctor
<
T
>
,
ElementwiseSubBroadCastGradFunctor
<
T
>
,
ElementwiseSubBroadCast2GradFunctor
<
T
>>
(
ctx
,
x
,
y
,
out
,
dout
,
axis
,
dx
,
dy
);
ElemwiseGradCompute
<
DeviceContext
,
T
,
SubGradDX
<
T
>
,
SubGradDY
<
T
>>
(
ctx
,
*
x
,
*
y
,
*
out
,
*
dout
,
axis
,
dx
,
dy
,
SubGradDX
<
T
>
(),
SubGradDY
<
T
>
());
}
};
}
// namespace operators
}
// namespace paddle
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录