Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
ea4d08da
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看板
提交
ea4d08da
编写于
12月 29, 2016
作者:
X
xutianbing
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update interface of context projection functions, Tensor -> Matrix/Vector
上级
2c37ad7e
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
207 addition
and
181 deletion
+207
-181
paddle/function/ContextProjectionOp.cpp
paddle/function/ContextProjectionOp.cpp
+110
-75
paddle/function/ContextProjectionOp.h
paddle/function/ContextProjectionOp.h
+23
-22
paddle/function/ContextProjectionOpGpu.cu
paddle/function/ContextProjectionOpGpu.cu
+57
-73
paddle/function/ContextProjectionOpTest.cpp
paddle/function/ContextProjectionOpTest.cpp
+1
-2
paddle/function/Function.h
paddle/function/Function.h
+13
-0
paddle/gserver/layers/ContextProjection.cpp
paddle/gserver/layers/ContextProjection.cpp
+3
-9
未找到文件。
paddle/function/ContextProjectionOp.cpp
浏览文件 @
ea4d08da
...
...
@@ -19,35 +19,17 @@ limitations under the License. */
namespace
paddle
{
template
<
>
void
ContextProjectionForward
<
DEVICE_TYPE_CPU
>
(
Tensor
&
outpu
t
,
const
Tensor
&
inpu
t
,
const
Tensor
&
weigh
t
,
const
Tensor
&
sequence
,
void
ContextProjectionForward
<
DEVICE_TYPE_CPU
>
(
CpuMatrix
*
out_ma
t
,
const
CpuMatrix
*
input_ma
t
,
const
CpuMatrix
*
weight_ma
t
,
const
CpuIVector
&
seq_vec
,
size_t
context_length
,
int
context_start
,
size_t
begin_pad
,
bool
is_padding
)
{
CHECK
(
output
.
getData
()
&&
input
.
getData
()
&&
sequence
.
getData
());
CHECK_EQ
(
output
.
dims_
.
size
(),
2
);
CHECK_EQ
(
input
.
dims_
.
size
(),
2
);
CHECK_EQ
(
weight
.
dims_
.
size
(),
2
);
CHECK_EQ
(
sequence
.
dims_
.
size
(),
1
);
auto
out_mat
=
std
::
make_shared
<
CpuMatrix
>
(
output
.
getData
(),
output
.
dims_
[
0
],
output
.
dims_
[
1
]);
const
auto
in_mat
=
std
::
make_shared
<
CpuMatrix
>
(
input
.
getData
(),
input
.
dims_
[
0
],
input
.
dims_
[
1
]);
const
auto
weight_mat
=
!
weight
.
getData
()
?
nullptr
:
std
::
make_shared
<
CpuMatrix
>
(
weight
.
getData
(),
weight
.
dims_
[
0
],
weight
.
dims_
[
1
]);
CpuIVector
seq_vec
(
sequence
.
dims_
[
0
],
reinterpret_cast
<
int
*>
(
sequence
.
getData
()));
CHECK_EQ
(
out_mat
->
getWidth
(),
in_mat
->
getWidth
()
*
context_length
);
size_t
begin_pad
)
{
const
int
*
starts
=
seq_vec
.
getData
();
const
size_t
num_sequences
=
seq_vec
.
getSize
()
-
1
;
auto
w_mat
=
const_cast
<
CpuMatrix
*>
(
weight_mat
);
auto
in_mat
=
const_cast
<
CpuMatrix
*>
(
input_mat
);
for
(
size_t
i
=
0
;
i
<
num_sequences
;
++
i
)
{
for
(
size_t
j
=
0
;
j
<
context_length
;
++
j
)
{
int
begin
=
starts
[
i
]
+
context_start
+
j
;
...
...
@@ -58,8 +40,8 @@ void ContextProjectionForward<DEVICE_TYPE_CPU>(Tensor& output,
int64_t
pad_size
=
std
::
min
(
starts
[
i
]
-
begin
,
starts
[
i
+
1
]
-
starts
[
i
]);
MatrixPtr
mat
=
out_mat
->
subMatrix
(
starts
[
i
],
pad_size
);
if
(
is_padding
&&
weight
_mat
)
{
MatrixPtr
sub
=
w
eight
_mat
->
subMatrix
(
j
,
pad_size
);
if
(
w
_mat
)
{
MatrixPtr
sub
=
w_mat
->
subMatrix
(
j
,
pad_size
);
mat
->
addAtOffset
(
*
sub
,
j
*
in_mat
->
getWidth
());
}
dst_begin
=
starts
[
i
]
+
pad_size
;
...
...
@@ -69,8 +51,8 @@ void ContextProjectionForward<DEVICE_TYPE_CPU>(Tensor& output,
int64_t
pad_size
=
std
::
min
(
end
-
starts
[
i
+
1
],
starts
[
i
+
1
]
-
starts
[
i
]);
MatrixPtr
mat
=
out_mat
->
subMatrix
(
starts
[
i
+
1
]
-
pad_size
,
pad_size
);
if
(
is_padding
&&
weight
_mat
)
{
MatrixPtr
sub
=
w
eight
_mat
->
subMatrix
(
if
(
w
_mat
)
{
MatrixPtr
sub
=
w_mat
->
subMatrix
(
begin_pad
+
context_start
+
j
-
pad_size
,
pad_size
);
mat
->
addAtOffset
(
*
sub
,
j
*
in_mat
->
getWidth
());
}
...
...
@@ -98,7 +80,6 @@ public:
context_length_
=
config
.
get
<
size_t
>
(
"context_length"
);
context_start_
=
config
.
get
<
int
>
(
"context_start"
);
begin_pad_
=
config
.
get
<
size_t
>
(
"begin_pad"
);
is_padding_
=
config
.
get
<
bool
>
(
"is_padding"
);
}
void
calc
(
const
Arguments
&
inputs
,
...
...
@@ -108,59 +89,58 @@ public:
CHECK_EQ
(
1
,
outputs
.
size
());
CHECK_EQ
(
0
,
inouts
.
size
());
ContextProjectionForward
<
Device
>
((
Tensor
&
)
outputs
[
0
],
inputs
[
0
],
inputs
[
1
],
inputs
[
2
],
CHECK
(
outputs
[
0
].
getData
()
&&
inputs
[
0
].
getData
()
&&
inputs
[
2
].
getData
());
CHECK_EQ
(
outputs
[
0
].
dims_
.
size
(),
2
);
CHECK_EQ
(
inputs
[
0
].
dims_
.
size
(),
2
);
CHECK_EQ
(
inputs
[
1
].
dims_
.
size
(),
2
);
CHECK_EQ
(
inputs
[
2
].
dims_
.
size
(),
1
);
/// dim of output = dim of input * context_length
CHECK_EQ
(
outputs
[
0
].
dims_
[
1
],
inputs
[
0
].
dims_
[
1
]
*
context_length_
);
/// dim of input == dim of weight
CHECK_EQ
(
inputs
[
0
].
dims_
[
1
],
inputs
[
1
].
dims_
[
1
]);
/// input and output has the same batch_size
CHECK_EQ
(
inputs
[
0
].
dims_
[
0
],
outputs
[
0
].
dims_
[
0
]);
auto
out_mat
=
std
::
make_shared
<
typename
MatrixT
<
Device
>::
type
>
(
outputs
[
0
].
getData
(),
outputs
[
0
].
dims_
[
0
],
outputs
[
0
].
dims_
[
1
]);
const
auto
in_mat
=
std
::
make_shared
<
typename
MatrixT
<
Device
>::
type
>
(
inputs
[
0
].
getData
(),
inputs
[
0
].
dims_
[
0
],
inputs
[
0
].
dims_
[
1
]);
const
auto
w_mat
=
!
inputs
[
1
].
getData
()
?
nullptr
:
std
::
make_shared
<
typename
MatrixT
<
Device
>::
type
>
(
inputs
[
1
].
getData
(),
inputs
[
1
].
dims_
[
0
],
inputs
[
1
].
dims_
[
1
]);
typename
SequenceT
<
Device
>::
type
seq_vec
(
inputs
[
2
].
dims_
[
0
],
reinterpret_cast
<
int
*>
(
inputs
[
2
].
getData
()));
ContextProjectionForward
<
Device
>
(
out_mat
.
get
(),
in_mat
.
get
(),
w_mat
.
get
(),
seq_vec
,
context_length_
,
context_start_
,
begin_pad_
,
is_padding_
);
begin_pad_
);
}
private:
size_t
context_length_
;
int
context_start_
;
size_t
begin_pad_
;
bool
is_padding_
;
};
template
<
>
void
ContextProjectionBackward
<
DEVICE_TYPE_CPU
>
(
Tensor
&
out_grad
,
Tensor
&
in_grad
,
Tensor
&
w_grad
,
const
Tensor
&
sequence
,
void
ContextProjectionBackward
<
DEVICE_TYPE_CPU
>
(
CpuMatrix
*
out_grad_mat
,
CpuMatrix
*
in_grad_mat
,
CpuMatrix
*
w_grad_mat
,
const
CpuIVector
&
seq_vec
,
size_t
context_length
,
int
context_start
,
size_t
begin_pad
,
bool
is_padding
,
size_t
total_pad
)
{
CHECK
(
out_grad
.
getData
()
&&
sequence
.
getData
());
CHECK_EQ
(
out_grad
.
dims_
.
size
(),
2
);
CHECK_EQ
(
in_grad
.
dims_
.
size
(),
2
);
CHECK_EQ
(
w_grad
.
dims_
.
size
(),
2
);
CHECK_EQ
(
sequence
.
dims_
.
size
(),
1
);
auto
out_grad_mat
=
std
::
make_shared
<
CpuMatrix
>
(
out_grad
.
getData
(),
out_grad
.
dims_
[
0
],
out_grad
.
dims_
[
1
]);
const
auto
in_grad_mat
=
!
in_grad
.
getData
()
?
nullptr
:
std
::
make_shared
<
CpuMatrix
>
(
in_grad
.
getData
(),
in_grad
.
dims_
[
0
],
in_grad
.
dims_
[
1
]);
const
auto
w_grad_mat
=
!
w_grad
.
getData
()
?
nullptr
:
std
::
make_shared
<
CpuMatrix
>
(
w_grad
.
getData
(),
w_grad
.
dims_
[
0
],
w_grad
.
dims_
[
1
]);
CpuIVector
seq_vec
(
sequence
.
dims_
[
0
],
reinterpret_cast
<
int
*>
(
sequence
.
getData
()));
CHECK_EQ
(
out_grad_mat
->
getWidth
(),
in_grad_mat
->
getWidth
()
*
context_length
);
CHECK
(
out_grad_mat
);
size_t
input_dim
=
in_grad_mat
?
in_grad_mat
->
getWidth
()
:
w_grad_mat
?
w_grad_mat
->
getWidth
()
:
0
;
CHECK_EQ
(
out_grad_mat
->
getWidth
(),
input_dim
*
context_length
);
const
int
*
starts
=
seq_vec
.
getData
();
size_t
num_sequences
=
seq_vec
.
getSize
()
-
1
;
for
(
size_t
i
=
0
;
i
<
num_sequences
;
++
i
)
{
...
...
@@ -226,10 +206,38 @@ public:
CHECK_EQ
(
1
,
outputs
.
size
());
CHECK_EQ
(
0
,
inouts
.
size
());
ContextProjectionBackward
<
Device
>
((
Tensor
&
)
outputs
[
0
],
(
Tensor
&
)
inputs
[
0
],
(
Tensor
&
)
inputs
[
1
],
inputs
[
2
],
CHECK
(
outputs
[
0
].
getData
()
&&
inputs
[
2
].
getData
());
CHECK_EQ
(
outputs
[
0
].
dims_
.
size
(),
2
);
CHECK_EQ
(
inputs
[
0
].
dims_
.
size
(),
2
);
CHECK_EQ
(
inputs
[
1
].
dims_
.
size
(),
2
);
CHECK_EQ
(
inputs
[
2
].
dims_
.
size
(),
1
);
/// dim of input == dim of weight
CHECK_EQ
(
inputs
[
0
].
dims_
[
1
],
inputs
[
1
].
dims_
[
1
]);
/// input and output has the same batch_size
CHECK_EQ
(
inputs
[
0
].
dims_
[
0
],
outputs
[
0
].
dims_
[
0
]);
/// dim of output = dim of input * context_length
CHECK_EQ
(
outputs
[
0
].
dims_
[
1
],
inputs
[
0
].
dims_
[
1
]
*
context_length_
);
auto
out_grad_mat
=
std
::
make_shared
<
typename
MatrixT
<
Device
>::
type
>
(
outputs
[
0
].
getData
(),
outputs
[
0
].
dims_
[
0
],
outputs
[
0
].
dims_
[
1
]);
auto
in_grad_mat
=
!
inputs
[
0
].
getData
()
?
nullptr
:
std
::
make_shared
<
typename
MatrixT
<
Device
>::
type
>
(
inputs
[
0
].
getData
(),
inputs
[
0
].
dims_
[
0
],
inputs
[
0
].
dims_
[
1
]);
auto
w_grad_mat
=
!
inputs
[
1
].
getData
()
?
nullptr
:
std
::
make_shared
<
typename
MatrixT
<
Device
>::
type
>
(
inputs
[
1
].
getData
(),
inputs
[
1
].
dims_
[
0
],
inputs
[
1
].
dims_
[
1
]);
typename
SequenceT
<
Device
>::
type
seq_vec
(
inputs
[
2
].
dims_
[
0
],
reinterpret_cast
<
int
*>
(
inputs
[
2
].
getData
()));
ContextProjectionBackward
<
Device
>
(
out_grad_mat
.
get
(),
in_grad_mat
?
in_grad_mat
.
get
()
:
nullptr
,
w_grad_mat
?
w_grad_mat
.
get
()
:
nullptr
,
seq_vec
,
context_length_
,
context_start_
,
begin_pad_
,
...
...
@@ -264,10 +272,24 @@ public:
CHECK_EQ
(
2
,
inputs
.
size
());
CHECK_EQ
(
1
,
outputs
.
size
());
CHECK_EQ
(
0
,
inouts
.
size
());
CHECK
(
inputs
[
0
].
getData
()
&&
outputs
[
0
].
getData
()
&&
inputs
[
1
].
getData
());
CHECK_EQ
(
outputs
[
0
].
dims_
.
size
(),
2
);
CHECK_EQ
(
inputs
[
0
].
dims_
.
size
(),
2
);
CHECK_EQ
(
inputs
[
1
].
dims_
.
size
(),
1
);
CHECK_EQ
(
outputs
[
0
].
dims_
[
1
],
inputs
[
0
].
dims_
[
1
]
*
context_length_
);
/// input and output has the same batch_size
CHECK_EQ
(
inputs
[
0
].
dims_
[
0
],
outputs
[
0
].
dims_
[
0
]);
ContextProjectionBackwardData
<
Device
>
((
Tensor
&
)
outputs
[
0
],
(
Tensor
&
)
inputs
[
0
],
inputs
[
1
],
auto
out_grad_mat
=
std
::
make_shared
<
typename
MatrixT
<
Device
>::
type
>
(
outputs
[
0
].
getData
(),
outputs
[
0
].
dims_
[
0
],
outputs
[
0
].
dims_
[
1
]);
const
auto
in_grad_mat
=
std
::
make_shared
<
typename
MatrixT
<
Device
>::
type
>
(
inputs
[
0
].
getData
(),
inputs
[
0
].
dims_
[
0
],
inputs
[
0
].
dims_
[
1
]);
typename
SequenceT
<
Device
>::
type
seq_vec
(
inputs
[
1
].
dims_
[
0
],
reinterpret_cast
<
int
*>
(
inputs
[
1
].
getData
()));
ContextProjectionBackwardData
<
Device
>
(
out_grad_mat
.
get
(),
in_grad_mat
.
get
(),
seq_vec
,
context_length_
,
context_start_
);
}
...
...
@@ -299,9 +321,22 @@ public:
CHECK_EQ
(
1
,
outputs
.
size
());
CHECK_EQ
(
0
,
inouts
.
size
());
ContextProjectionBackwardWeight
<
Device
>
((
Tensor
&
)
outputs
[
0
],
(
Tensor
&
)
inputs
[
0
],
inputs
[
1
],
CHECK
(
inputs
[
0
].
getData
()
&&
outputs
[
0
].
getData
()
&&
inputs
[
1
].
getData
());
CHECK_EQ
(
outputs
[
0
].
dims_
.
size
(),
2
);
CHECK_EQ
(
inputs
[
0
].
dims_
.
size
(),
2
);
CHECK_EQ
(
inputs
[
1
].
dims_
.
size
(),
1
);
CHECK_EQ
(
outputs
[
0
].
dims_
[
1
],
inputs
[
0
].
dims_
[
1
]
*
context_length_
);
auto
out_grad_mat
=
std
::
make_shared
<
typename
MatrixT
<
Device
>::
type
>
(
outputs
[
0
].
getData
(),
outputs
[
0
].
dims_
[
0
],
outputs
[
0
].
dims_
[
1
]);
auto
w_grad_mat
=
std
::
make_shared
<
typename
MatrixT
<
Device
>::
type
>
(
inputs
[
0
].
getData
(),
inputs
[
0
].
dims_
[
0
],
inputs
[
0
].
dims_
[
1
]);
typename
SequenceT
<
Device
>::
type
seq_vec
(
inputs
[
1
].
dims_
[
0
],
reinterpret_cast
<
int
*>
(
inputs
[
1
].
getData
()));
ContextProjectionBackwardWeight
<
Device
>
(
out_grad_mat
.
get
(),
w_grad_mat
.
get
(),
seq_vec
,
context_length_
,
context_start_
,
total_pad_
,
...
...
paddle/function/ContextProjectionOp.h
浏览文件 @
ea4d08da
...
...
@@ -32,14 +32,13 @@ namespace paddle {
*
*/
template
<
DeviceType
Device
>
void
ContextProjectionForward
(
Tensor
&
output
,
const
Tensor
&
input
,
const
Tensor
&
weight
,
const
Tensor
&
sequence
,
void
ContextProjectionForward
(
typename
MatrixT
<
Device
>::
type
*
output
,
const
typename
MatrixT
<
Device
>::
type
*
input
,
const
typename
MatrixT
<
Device
>::
type
*
weight
,
const
typename
SequenceT
<
Device
>::
type
&
sequence
,
size_t
context_length
,
int
context_start
,
size_t
begin_pad
,
bool
is_padding
);
size_t
begin_pad
);
/**
* \brief Context Projection Backward.
...
...
@@ -55,10 +54,10 @@ void ContextProjectionForward(Tensor& output,
*
*/
template
<
DeviceType
Device
>
void
ContextProjectionBackward
(
Tensor
&
out_grad
,
Tensor
&
in_grad
,
Tensor
&
w_grad
,
const
Tensor
&
sequence
,
void
ContextProjectionBackward
(
typename
MatrixT
<
Device
>::
type
*
out_grad
,
typename
MatrixT
<
Device
>::
type
*
in_grad
,
typename
MatrixT
<
Device
>::
type
*
w_grad
,
const
typename
SequenceT
<
Device
>::
type
&
seq_vec
,
size_t
context_length
,
int
context_start
,
size_t
begin_pad
,
...
...
@@ -66,19 +65,21 @@ void ContextProjectionBackward(Tensor& out_grad,
size_t
total_pad
);
template
<
DeviceType
Device
>
void
ContextProjectionBackwardData
(
Tensor
&
out_grad
,
Tensor
&
in_grad
,
const
Tensor
&
sequence
,
size_t
context_length
,
int
context_start
);
void
ContextProjectionBackwardData
(
typename
MatrixT
<
Device
>::
type
*
out_grad
,
typename
MatrixT
<
Device
>::
type
*
in_grad
,
const
typename
SequenceT
<
Device
>::
type
&
sequence
,
size_t
context_length
,
int
context_start
);
template
<
DeviceType
Device
>
void
ContextProjectionBackwardWeight
(
Tensor
&
out_grad
,
Tensor
&
w_grad
,
const
Tensor
&
sequence
,
size_t
context_length
,
int
context_start
,
size_t
total_pad
,
size_t
begin_pad
);
void
ContextProjectionBackwardWeight
(
typename
MatrixT
<
Device
>::
type
*
out_grad
,
typename
MatrixT
<
Device
>::
type
*
w_grad
,
const
typename
SequenceT
<
Device
>::
type
&
seq_vec
,
size_t
context_length
,
int
context_start
,
size_t
total_pad
,
size_t
begin_pad
);
}
// namespace paddle
paddle/function/ContextProjectionOpGpu.cu
浏览文件 @
ea4d08da
...
...
@@ -75,18 +75,16 @@ __global__ void KeContextProjectionForward(const real* input,
void
hl_context_projection_forward
(
const
real
*
input
,
const
int
*
sequence
,
real
*
weight
,
const
real
*
weight
,
real
*
output
,
int
num_sequences
,
int
input_dim
,
int
context_length
,
int
context_start
,
int
begin_pad
,
bool
is_padding
)
{
int
begin_pad
)
{
CHECK_NOTNULL
(
input
);
CHECK_NOTNULL
(
sequence
);
CHECK_NOTNULL
(
output
);
CHECK
(
!
is_padding
||
weight
);
int
block_size
=
128
;
int
blocks_x
=
num_sequences
;
...
...
@@ -94,7 +92,7 @@ void hl_context_projection_forward(const real* input,
dim3
threads
(
block_size
,
1
);
dim3
grid
(
blocks_x
,
blocks_y
);
if
(
is_padding
)
{
if
(
weight
)
{
KeContextProjectionForward
<
true
><<<
grid
,
threads
,
0
,
STREAM_DEFAULT
>>>
(
input
,
sequence
,
weight
,
output
,
input_dim
,
context_length
,
context_start
,
begin_pad
);
...
...
@@ -107,31 +105,23 @@ void hl_context_projection_forward(const real* input,
}
template
<
>
void
ContextProjectionForward
<
DEVICE_TYPE_GPU
>
(
Tensor
&
output
,
const
Tensor
&
input
,
const
Tensor
&
weight
,
const
Tens
or
&
sequence
,
void
ContextProjectionForward
<
DEVICE_TYPE_GPU
>
(
GpuMatrix
*
output
,
const
GpuMatrix
*
input
,
const
GpuMatrix
*
weight
,
const
GpuIVect
or
&
sequence
,
size_t
context_length
,
int
context_start
,
size_t
begin_pad
,
bool
is_padding
)
{
CHECK
(
output
.
getData
()
&&
input
.
getData
()
&&
sequence
.
getData
());
CHECK_EQ
(
output
.
dims_
.
size
(),
2
);
CHECK_EQ
(
input
.
dims_
.
size
(),
2
);
CHECK_EQ
(
weight
.
dims_
.
size
(),
2
);
CHECK_EQ
(
sequence
.
dims_
.
size
(),
1
);
CHECK_EQ
(
output
.
dims_
[
1
],
input
.
dims_
[
1
]
*
context_length
);
hl_context_projection_forward
(
input
.
getData
(),
reinterpret_cast
<
int
*>
(
sequence
.
getData
()),
weight
.
getData
(),
output
.
getData
(),
sequence
.
dims_
[
0
]
-
1
,
input
.
dims_
[
1
],
size_t
begin_pad
)
{
CHECK
(
input
&&
output
);
hl_context_projection_forward
(
input
->
getData
(),
sequence
.
getData
(),
weight
?
weight
->
getData
()
:
nullptr
,
output
->
getData
(),
sequence
.
getSize
()
-
1
,
input
->
getWidth
(),
context_length
,
context_start
,
begin_pad
,
is_padding
);
begin_pad
);
}
__global__
void
KeContextProjectionBackwardData
(
real
*
out_grad
,
...
...
@@ -200,22 +190,17 @@ void hl_context_projection_backward_data(real* out_grad,
}
template
<
>
void
ContextProjectionBackwardData
<
DEVICE_TYPE_GPU
>
(
Tensor
&
out_grad
,
Tensor
&
in_grad
,
const
Tensor
&
sequence
,
size_t
context_length
,
int
context_start
)
{
CHECK
(
in_grad
.
getData
()
&&
out_grad
.
getData
()
&&
sequence
.
getData
());
CHECK_EQ
(
out_grad
.
dims_
.
size
(),
2
);
CHECK_EQ
(
in_grad
.
dims_
.
size
(),
2
);
CHECK_EQ
(
sequence
.
dims_
.
size
(),
1
);
CHECK_EQ
(
out_grad
.
dims_
[
1
],
in_grad
.
dims_
[
1
]
*
context_length
);
hl_context_projection_backward_data
(
out_grad
.
getData
(),
reinterpret_cast
<
int
*>
(
sequence
.
getData
()),
in_grad
.
getData
(),
sequence
.
dims_
[
0
]
-
1
,
in_grad
.
dims_
[
1
],
void
ContextProjectionBackwardData
<
DEVICE_TYPE_GPU
>
(
GpuMatrix
*
out_grad
,
GpuMatrix
*
in_grad
,
const
GpuIVector
&
sequence
,
size_t
context_length
,
int
context_start
)
{
CHECK
(
in_grad
&&
out_grad
);
hl_context_projection_backward_data
(
out_grad
->
getData
(),
sequence
.
getData
(),
in_grad
->
getData
(),
sequence
.
getSize
()
-
1
,
in_grad
->
getWidth
(),
context_length
,
context_start
);
}
...
...
@@ -320,24 +305,20 @@ void hl_context_projection_backward_weight(real* out_grad,
}
template
<
>
void
ContextProjectionBackwardWeight
<
DEVICE_TYPE_GPU
>
(
Tensor
&
out_grad
,
Tensor
&
w_grad
,
const
Tensor
&
sequence
,
size_t
context_length
,
int
context_start
,
size_t
total_pad
,
size_t
begin_pad
)
{
CHECK
(
w_grad
.
getData
()
&&
out_grad
.
getData
()
&&
sequence
.
getData
());
CHECK_EQ
(
out_grad
.
dims_
.
size
(),
2
);
CHECK_EQ
(
w_grad
.
dims_
.
size
(),
2
);
CHECK_EQ
(
sequence
.
dims_
.
size
(),
1
);
CHECK_EQ
(
out_grad
.
dims_
[
1
],
w_grad
.
dims_
[
1
]
*
context_length
);
hl_context_projection_backward_weight
(
out_grad
.
getData
(),
reinterpret_cast
<
int
*>
(
sequence
.
getData
()),
w_grad
.
getData
(),
sequence
.
dims_
[
0
]
-
1
,
w_grad
.
dims_
[
1
],
void
ContextProjectionBackwardWeight
<
DEVICE_TYPE_GPU
>
(
GpuMatrix
*
out_grad
,
GpuMatrix
*
w_grad
,
const
GpuIVector
&
seq_vec
,
size_t
context_length
,
int
context_start
,
size_t
total_pad
,
size_t
begin_pad
)
{
CHECK
(
out_grad
&&
w_grad
);
hl_context_projection_backward_weight
(
out_grad
->
getData
(),
seq_vec
.
getData
(),
w_grad
->
getData
(),
seq_vec
.
getSize
()
-
1
,
w_grad
->
getWidth
(),
total_pad
,
context_length
,
context_start
,
...
...
@@ -345,24 +326,27 @@ void ContextProjectionBackwardWeight<DEVICE_TYPE_GPU>(Tensor& out_grad,
}
template
<
>
void
ContextProjectionBackward
<
DEVICE_TYPE_GPU
>
(
Tensor
&
out_grad
,
Tensor
&
in_grad
,
Tensor
&
w_grad
,
const
Tensor
&
sequence
,
size_t
context_length
,
int
context_start
,
size_t
begin_pad
,
bool
is_padding
,
size_t
total_pad
)
{
if
(
in_grad
.
getData
())
{
ContextProjectionBackwardData
<
DEVICE_TYPE_GPU
>
(
out_grad
,
void
ContextProjectionBackward
<
DEVICE_TYPE_GPU
>
(
GpuMatrix
*
out_grad
,
GpuMatrix
*
in_grad
,
GpuMatrix
*
w_grad
,
const
GpuIVector
&
sequence
,
size_t
context_length
,
int
context_start
,
size_t
begin_pad
,
bool
is_padding
,
size_t
total_pad
)
{
CHECK
(
out_grad
);
if
(
in_grad
)
{
ContextProjectionBackwardData
<
DEVICE_TYPE_GPU
>
(
out_grad
,
in_grad
,
sequence
,
context_length
,
context_start
);
}
if
(
is_padding
&&
w_grad
.
getData
())
{
ContextProjectionBackwardWeight
<
DEVICE_TYPE_GPU
>
(
out_grad
,
if
(
is_padding
&&
w_grad
)
{
ContextProjectionBackwardWeight
<
DEVICE_TYPE_GPU
>
(
out_grad
,
w_grad
,
sequence
,
context_length
,
...
...
paddle/function/ContextProjectionOpTest.cpp
浏览文件 @
ea4d08da
...
...
@@ -32,8 +32,7 @@ void testMatrixProjectionForward(int context_start,
FuncConfig
()
.
set
(
"context_length"
,
context_length
)
.
set
(
"context_start"
,
context_start
)
.
set
(
"begin_pad"
,
std
::
max
(
0
,
-
context_start
))
.
set
(
"is_padding"
,
is_padding
));
.
set
(
"begin_pad"
,
std
::
max
(
0
,
-
context_start
)));
CpuMatrix
cpu_in
(
batch_size
,
input_dim
);
cpu_in
.
randomizeUniform
();
...
...
paddle/function/Function.h
浏览文件 @
ea4d08da
...
...
@@ -40,6 +40,19 @@ struct MatrixT<DEVICE_TYPE_GPU> {
using
type
=
GpuMatrix
;
};
template
<
DeviceType
Device
>
struct
SequenceT
;
template
<
>
struct
SequenceT
<
DEVICE_TYPE_CPU
>
{
using
type
=
CpuIVector
;
};
template
<
>
struct
SequenceT
<
DEVICE_TYPE_GPU
>
{
using
type
=
GpuIVector
;
};
typedef
std
::
vector
<
size_t
>
Dims
;
class
Tensor
{
...
...
paddle/gserver/layers/ContextProjection.cpp
浏览文件 @
ea4d08da
...
...
@@ -53,8 +53,7 @@ bool ContextProjection::init() {
FuncConfig
()
.
set
(
"context_length"
,
context_length
)
.
set
(
"context_start"
,
context_start
)
.
set
(
"begin_pad"
,
beginPad_
)
.
set
(
"is_padding"
,
is_padding
));
.
set
(
"begin_pad"
,
beginPad_
));
createFunction
(
backward_
,
"ContextProjectionBackward"
,
FuncConfig
()
...
...
@@ -112,7 +111,7 @@ void ContextProjection::forward() {
size_t
dim
=
out_
->
value
->
getWidth
();
CHECK_EQ
(
dim
,
input_dim
*
config_
.
context_length
());
size_t
batch_size
=
in_
->
value
->
getHeight
();
CHECK_EQ
(
batch_size
,
out_
->
value
->
getHeight
())
;
CHECK_EQ
(
forward_
.
size
(),
1
)
<<
"Only one forward function here"
;
REGISTER_TIMER_INFO
(
"ContextProjectionForward"
,
getName
().
c_str
());
bool
is_padding
=
config_
.
trainable_padding
();
...
...
@@ -120,12 +119,6 @@ void ContextProjection::forward() {
auto
w_ptr
=
state_
?
state_
.
get
()
:
is_padding
?
weight_
->
getW
().
get
()
:
nullptr
;
auto
start_pos
=
in_
->
sequenceStartPositions
;
/// if use state_ as weight_, w_ptr already has mem, so padding true
forward_
[
0
]
->
init
(
FuncConfig
()
.
set
(
"context_length"
,
config_
.
context_length
())
.
set
(
"context_start"
,
config_
.
context_start
())
.
set
(
"begin_pad"
,
beginPad_
)
.
set
(
"is_padding"
,
state_
?
true
:
is_padding
));
forward_
[
0
]
->
calc
({
Tensor
(
in_
->
value
->
getData
(),
Dims
{
batch_size
,
input_dim
}),
Tensor
(
w_ptr
?
w_ptr
->
getData
()
:
nullptr
,
Dims
{
w_ptr
?
w_ptr
->
getHeight
()
:
0
,
input_dim
}),
...
...
@@ -161,6 +154,7 @@ void ContextProjection::backward(const UpdateCallback& callback) {
CHECK_EQ
(
dim
,
input_dim
*
config_
.
context_length
());
size_t
batch_size
=
in_
->
value
->
getHeight
();
CHECK_EQ
(
batch_size
,
out_
->
value
->
getHeight
());
CHECK_EQ
(
backward_
.
size
(),
1
)
<<
"Only one backward function here"
;
REGISTER_TIMER_INFO
(
"ContextProjectionBackward"
,
getName
().
c_str
());
bool
is_padding
=
config_
.
trainable_padding
();
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录