Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Crayon鑫
Paddle
提交
7ac00dd6
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看板
提交
7ac00dd6
编写于
12月 27, 2017
作者:
C
chengduoZH
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refine
上级
49df2a78
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
142 addition
and
78 deletion
+142
-78
paddle/operators/cos_sim_op.cc
paddle/operators/cos_sim_op.cc
+38
-0
paddle/operators/cos_sim_op.cu
paddle/operators/cos_sim_op.cu
+45
-0
paddle/operators/cos_sim_op.h
paddle/operators/cos_sim_op.h
+59
-78
未找到文件。
paddle/operators/cos_sim_op.cc
浏览文件 @
7ac00dd6
...
...
@@ -149,6 +149,44 @@ class CosSimOpGrad : public framework::OperatorWithKernel {
}
};
template
<
typename
T
>
struct
CosSimDyFunctor
<
platform
::
CPUDeviceContext
,
T
>
{
CosSimDyFunctor
(
const
T
*
x_norm
,
const
T
*
y_norm
,
const
T
*
x
,
const
T
*
y
,
const
T
*
z
,
const
T
*
dz
,
T
*
dy
,
int
cols
)
:
x_norm_
(
x_norm
),
y_norm_
(
y_norm
),
x_
(
x
),
y_
(
y
),
z_
(
z
),
dz_
(
dz
),
dy_
(
dy
),
cols_
(
static_cast
<
size_t
>
(
cols
))
{}
inline
void
operator
()(
size_t
offset
)
const
{
auto
xy_norm_prod
=
x_norm_
[
offset
]
*
y_norm_
[
0
];
auto
dz
=
dz_
[
offset
];
auto
z
=
z_
[
offset
];
auto
*
x
=
x_
+
cols_
*
offset
;
auto
reciprocal_xy_norm_prod
=
1
/
xy_norm_prod
;
auto
y_norm_square
=
y_norm_
[
0
]
*
y_norm_
[
0
];
auto
reciprocal_y_norm_square
=
1
/
y_norm_square
;
for
(
size_t
i
=
0
;
i
<
cols_
;
++
i
)
{
dy_
[
i
]
+=
dz
*
(
x
[
i
]
*
reciprocal_xy_norm_prod
-
z
*
y_
[
i
]
*
reciprocal_y_norm_square
);
}
}
const
T
*
x_norm_
;
const
T
*
y_norm_
;
const
T
*
x_
;
const
T
*
y_
;
const
T
*
z_
;
const
T
*
dz_
;
T
*
dy_
;
const
size_t
cols_
;
};
}
// namespace operators
}
// namespace paddle
...
...
paddle/operators/cos_sim_op.cu
浏览文件 @
7ac00dd6
...
...
@@ -15,6 +15,51 @@
#define EIGEN_USE_GPU
#include "paddle/operators/cos_sim_op.h"
namespace
paddle
{
namespace
operators
{
template
<
typename
T
>
struct
CosSimDyFunctor
<
platform
::
CUDADeviceContext
,
T
>
{
CosSimDyFunctor
(
const
T
*
x_norm
,
const
T
*
y_norm
,
const
T
*
x
,
const
T
*
y
,
const
T
*
z
,
const
T
*
dz
,
T
*
dy
,
int
cols
)
:
x_norm_
(
x_norm
),
y_norm_
(
y_norm
),
x_
(
x
),
y_
(
y
),
z_
(
z
),
dz_
(
dz
),
dy_
(
dy
),
cols_
(
static_cast
<
size_t
>
(
cols
))
{}
inline
void
operator
()(
size_t
offset
)
const
{
auto
xy_norm_prod
=
x_norm_
[
offset
]
*
y_norm_
[
0
];
auto
dz
=
dz_
[
offset
];
auto
z
=
z_
[
offset
];
auto
*
x
=
x_
+
cols_
*
offset
;
auto
reciprocal_xy_norm_prod
=
1
/
xy_norm_prod
;
auto
y_norm_square
=
y_norm_
[
0
]
*
y_norm_
[
0
];
auto
reciprocal_y_norm_square
=
1
/
y_norm_square
;
for
(
size_t
i
=
0
;
i
<
cols_
;
++
i
)
{
T
dy
=
dz
*
(
x
[
i
]
*
reciprocal_xy_norm_prod
-
z
*
y_
[
i
]
*
reciprocal_y_norm_square
);
paddle
::
paddleAtomicAdd
(
dy_
+
i
,
dy
)
}
}
const
T
*
x_norm_
;
const
T
*
y_norm_
;
const
T
*
x_
;
const
T
*
y_
;
const
T
*
z_
;
const
T
*
dz_
;
T
*
dy_
;
const
size_t
cols_
;
};
}
// namespace operators
}
// namespace paddle
namespace
ops
=
paddle
::
operators
;
REGISTER_OP_CUDA_KERNEL
(
cos_sim
,
ops
::
CosSimKernel
<
paddle
::
platform
::
CUDADeviceContext
,
float
>
);
...
...
paddle/operators/cos_sim_op.h
浏览文件 @
7ac00dd6
...
...
@@ -21,10 +21,17 @@ namespace operators {
using
Tensor
=
framework
::
Tensor
;
template
<
typename
IT1
,
typename
IT2
,
typename
Callback
>
static
void
ForEachZip
(
IT1
begin1
,
IT1
last1
,
IT2
begin2
,
Callback
callback
)
{
for
(;
begin1
<
last1
;
++
begin1
,
++
begin2
)
{
callback
(
*
begin1
,
*
begin2
);
template
<
typename
DeviceContext
,
typename
T
>
struct
CosSimDyFunctor
{
CosSimDyFunctor
(
const
T
*
x_norm
,
const
T
*
y_norm
,
const
T
*
x
,
const
T
*
y
,
const
T
*
z
,
const
T
*
dz
,
T
*
dy
,
int
cols
);
inline
void
operator
()(
size_t
)
const
;
};
template
<
typename
Callback
>
static
void
ForEachZip
(
size_t
num
,
Callback
callback
)
{
for
(
size_t
i
=
0
;
i
<
num
;
++
i
)
{
callback
(
i
);
}
}
...
...
@@ -38,16 +45,11 @@ struct CosSimFunctor {
z_
(
z
),
cols_
(
static_cast
<
size_t
>
(
cols
))
{}
inline
void
operator
()(
T
&
x_norm
,
T
&
y_norm
)
const
{
size_t
x_offset
=
&
x_norm
-
x_norm_
;
size_t
y_offset
=
&
y_norm
-
y_norm_
;
auto
*
x
=
x_
+
cols_
*
x_offset
;
T
xx
=
0
,
xy
=
0
;
T
yy
=
0
;
inline
HOSTDEVICE
void
operator
()(
size_t
offset
)
const
{
auto
*
x
=
x_
+
cols_
*
offset
;
T
xx
=
0
,
xy
=
0
,
yy
=
0
;
if
(
same_row
)
{
auto
*
y
=
y_
+
cols_
*
y_
offset
;
auto
*
y
=
y_
+
cols_
*
offset
;
for
(
size_t
i
=
0
;
i
<
cols_
;
++
i
)
{
xx
+=
x
[
i
]
*
x
[
i
];
yy
+=
y
[
i
]
*
y
[
i
];
...
...
@@ -55,21 +57,20 @@ struct CosSimFunctor {
}
xx
=
sqrt
(
xx
);
yy
=
sqrt
(
yy
);
x_norm_
[
x_offset
]
=
xx
;
y_norm_
[
y_offset
]
=
yy
;
z_
[
x_
offset
]
=
xy
/
(
xx
*
yy
);
y_norm_
[
offset
]
=
yy
;
x_norm_
[
offset
]
=
xx
;
z_
[
offset
]
=
xy
/
(
xx
*
yy
);
}
else
{
// This can be wrote in a better way.
auto
*
y
=
y_
;
for
(
size_t
i
=
0
;
i
<
cols_
;
++
i
)
{
xx
+=
x
[
i
]
*
x
[
i
];
yy
+=
y
[
i
]
*
y
[
i
];
// only need
xy
+=
x
[
i
]
*
y
[
i
];
yy
+=
y
_
[
i
]
*
y_
[
i
];
// only need
xy
+=
x
[
i
]
*
y
_
[
i
];
}
xx
=
sqrt
(
xx
);
yy
=
sqrt
(
yy
);
x_norm_
[
x_offset
]
=
xx
;
y_norm_
[
0
]
=
yy
;
z_
[
x_offset
]
=
xy
/
(
xx
*
yy
);
x_norm_
[
offset
]
=
xx
;
z_
[
offset
]
=
xy
/
(
xx
*
yy
);
}
}
...
...
@@ -104,14 +105,12 @@ class CosSimKernel : public framework::OpKernel<T> {
CosSimFunctor
<
T
,
true
>
functor
(
in_x
->
data
<
T
>
(),
in_y
->
data
<
T
>
(),
out_x_norm
->
data
<
T
>
(),
out_y_norm
->
data
<
T
>
(),
out_z
->
data
<
T
>
(),
cols
);
ForEachZip
(
out_x_norm
->
data
<
T
>
(),
out_x_norm
->
data
<
T
>
()
+
rows_x
,
out_y_norm
->
data
<
T
>
(),
functor
);
ForEachZip
(
rows_x
,
functor
);
}
else
{
CosSimFunctor
<
T
,
false
>
functor
(
in_x
->
data
<
T
>
(),
in_y
->
data
<
T
>
(),
out_x_norm
->
data
<
T
>
(),
out_y_norm
->
data
<
T
>
(),
out_z
->
data
<
T
>
(),
cols
);
ForEachZip
(
out_x_norm
->
data
<
T
>
(),
out_x_norm
->
data
<
T
>
()
+
rows_x
,
out_y_norm
->
data
<
T
>
(),
functor
);
ForEachZip
(
rows_x
,
functor
);
}
}
};
...
...
@@ -129,19 +128,15 @@ struct CosSimGradFunctor {
dx_
(
dx
),
cols_
(
static_cast
<
size_t
>
(
cols
))
{}
inline
void
operator
()(
const
T
&
x_norm
,
const
T
&
y_norm
)
const
{
size_t
x_offset
=
&
x_norm
-
x_norm_
;
size_t
y_offset
=
&
y_norm
-
y_norm_
;
inline
HOSTDEVICE
void
operator
()(
size_t
offset
)
const
{
auto
x_norm_square
=
x_norm_
[
offset
]
*
x_norm_
[
offset
];
auto
xy_norm_prod
=
x_norm_
[
offset
]
*
y_norm_
[
offset
];
auto
dz
=
dz_
[
offset
];
auto
z
=
z_
[
offset
];
auto
x_norm_square
=
x_norm_
[
x_offset
]
*
x_norm_
[
x_offset
];
auto
xy_norm_prod
=
x_norm_
[
x_offset
]
*
y_norm_
[
y_offset
];
auto
dz
=
dz_
[
x_offset
];
auto
z
=
z_
[
x_offset
];
auto
*
dx
=
dx_
+
cols_
*
x_offset
;
auto
*
x
=
x_
+
cols_
*
x_offset
;
auto
*
y
=
y_
+
cols_
*
y_offset
;
auto
*
dx
=
dx_
+
cols_
*
offset
;
auto
*
x
=
x_
+
cols_
*
offset
;
auto
*
y
=
y_
+
cols_
*
offset
;
auto
reciprocal_xy_norm_prod
=
1
/
xy_norm_prod
;
auto
reciprocal_x_norm_square
=
1
/
x_norm_square
;
...
...
@@ -161,10 +156,10 @@ struct CosSimGradFunctor {
const
size_t
cols_
;
};
template
<
typename
T
,
bool
Dx
>
template
<
typename
T
>
struct
CosSimDxFunctor
{
CosSimDxFunctor
(
const
T
*
x_norm
,
const
T
*
y_norm
,
const
T
*
x
,
const
T
*
y
,
const
T
*
z
,
const
T
*
dz
,
T
*
dx
,
T
*
dy
,
int
cols
)
const
T
*
z
,
const
T
*
dz
,
T
*
dx
,
int
cols
)
:
x_norm_
(
x_norm
),
y_norm_
(
y_norm
),
x_
(
x
),
...
...
@@ -172,37 +167,23 @@ struct CosSimDxFunctor {
z_
(
z
),
dz_
(
dz
),
dx_
(
dx
),
dy_
(
dy
),
cols_
(
static_cast
<
size_t
>
(
cols
))
{}
inline
void
operator
()(
const
T
&
x_norm
,
const
T
&
y_norm
)
const
{
size_t
x_offset
=
&
x_norm
-
x_norm_
;
auto
xy_norm_prod
=
x_norm_
[
x_offset
]
*
y_norm_
[
0
];
auto
dz
=
dz_
[
x_offset
];
auto
z
=
z_
[
x_offset
];
auto
*
x
=
x_
+
cols_
*
x_offset
;
inline
HOSTDEVICE
void
operator
()(
size_t
offset
)
const
{
auto
xy_norm_prod
=
x_norm_
[
offset
]
*
y_norm_
[
0
];
auto
dz
=
dz_
[
offset
];
auto
z
=
z_
[
offset
];
auto
*
x
=
x_
+
cols_
*
offset
;
auto
reciprocal_xy_norm_prod
=
1
/
xy_norm_prod
;
if
(
Dx
)
{
auto
x_norm_square
=
x_norm_
[
x_offset
]
*
x_norm_
[
x_offset
];
auto
*
dx
=
dx_
+
cols_
*
x_offset
;
auto
*
x
=
x_
+
cols_
*
x_offset
;
auto
x_norm_square
=
x_norm_
[
offset
]
*
x_norm_
[
offset
];
auto
*
dx
=
dx_
+
cols_
*
offset
;
auto
reciprocal_x_norm_square
=
1
/
x_norm_square
;
for
(
size_t
i
=
0
;
i
<
cols_
;
++
i
)
{
dx
[
i
]
=
dz
*
(
y_
[
i
]
*
reciprocal_xy_norm_prod
-
z
*
x
[
i
]
*
reciprocal_x_norm_square
);
}
}
else
{
auto
y_norm_square
=
y_norm_
[
0
]
*
y_norm_
[
0
];
auto
reciprocal_y_norm_square
=
1
/
y_norm_square
;
for
(
size_t
i
=
0
;
i
<
cols_
;
++
i
)
{
dy_
[
i
]
+=
dz
*
(
x
[
i
]
*
reciprocal_xy_norm_prod
-
z
*
y_
[
i
]
*
reciprocal_y_norm_square
);
}
}
}
const
T
*
x_norm_
;
const
T
*
y_norm_
;
const
T
*
x_
;
...
...
@@ -210,7 +191,6 @@ struct CosSimDxFunctor {
const
T
*
z_
;
const
T
*
dz_
;
T
*
dx_
;
T
*
dy_
;
const
size_t
cols_
;
};
...
...
@@ -239,33 +219,34 @@ class CosSimGradKernel : public framework::OpKernel<T> {
in_x_norm
->
data
<
T
>
(),
in_y_norm
->
data
<
T
>
(),
in_x
->
data
<
T
>
(),
in_y
->
data
<
T
>
(),
in_z
->
data
<
T
>
(),
in_grad_z
->
data
<
T
>
(),
out_grad_x
->
mutable_data
<
T
>
(
context
.
GetPlace
()),
cols
);
ForEachZip
(
in_x_norm
->
data
<
T
>
(),
in_x_norm
->
data
<
T
>
()
+
rows_x
,
in_y_norm
->
data
<
T
>
(),
functor
);
ForEachZip
(
rows_x
,
functor
);
}
if
(
out_grad_y
)
{
CosSimGradFunctor
<
T
>
functor
(
in_y_norm
->
data
<
T
>
(),
in_x_norm
->
data
<
T
>
(),
in_y
->
data
<
T
>
(),
in_x
->
data
<
T
>
(),
in_z
->
data
<
T
>
(),
in_grad_z
->
data
<
T
>
(),
out_grad_y
->
mutable_data
<
T
>
(
context
.
GetPlace
()),
cols
);
ForEachZip
(
in_y_norm
->
data
<
T
>
(),
in_y_norm
->
data
<
T
>
()
+
rows_x
,
in_x_norm
->
data
<
T
>
(),
functor
);
ForEachZip
(
rows_x
,
functor
);
}
}
else
{
if
(
out_grad_x
)
{
CosSimDxFunctor
<
T
,
true
>
functor
(
CosSimDxFunctor
<
T
>
functor
(
in_x_norm
->
data
<
T
>
(),
in_y_norm
->
data
<
T
>
(),
in_x
->
data
<
T
>
(),
in_y
->
data
<
T
>
(),
in_z
->
data
<
T
>
(),
in_grad_z
->
data
<
T
>
(),
out_grad_x
->
mutable_data
<
T
>
(
context
.
GetPlace
()),
nullptr
,
cols
);
ForEachZip
(
in_x_norm
->
data
<
T
>
(),
in_x_norm
->
data
<
T
>
()
+
rows_x
,
in_y_norm
->
data
<
T
>
(),
functor
);
out_grad_x
->
mutable_data
<
T
>
(
context
.
GetPlace
()),
cols
);
ForEachZip
(
rows_x
,
functor
);
}
if
(
out_grad_y
)
{
CosSimDxFunctor
<
T
,
false
>
functor
(
out_grad_y
->
mutable_data
<
T
>
(
context
.
GetPlace
());
math
::
SetConstant
<
DeviceContext
,
T
>
set_zero
;
auto
&
dev_ctx
=
context
.
template
device_context
<
DeviceContext
>();
set_zero
(
dev_ctx
,
out_grad_y
,
static_cast
<
T
>
(
0
));
CosSimDyFunctor
<
DeviceContext
,
T
>
functor
(
in_x_norm
->
data
<
T
>
(),
in_y_norm
->
data
<
T
>
(),
in_x
->
data
<
T
>
(),
in_y
->
data
<
T
>
(),
in_z
->
data
<
T
>
(),
in_grad_z
->
data
<
T
>
(),
nullptr
,
out_grad_y
->
mutable_data
<
T
>
(
context
.
GetPlace
()),
cols
);
ForEachZip
(
in_x_norm
->
data
<
T
>
(),
in_x_norm
->
data
<
T
>
()
+
rows_x
,
in_y_norm
->
data
<
T
>
(),
functor
);
in_y
->
data
<
T
>
(),
in_z
->
data
<
T
>
(),
in_grad_z
->
data
<
T
>
(),
out_grad_y
->
data
<
T
>
(),
cols
);
ForEachZip
(
rows_x
,
functor
);
}
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录