Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
41c52d3b
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看板
体验新版 GitCode,发现更多精彩内容 >>
提交
41c52d3b
编写于
1月 05, 2017
作者:
H
hedaoyuan
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Modify the argument type of ContextProjectionFunc
上级
68156c88
变更
8
展开全部
隐藏空白更改
内联
并排
Showing
8 changed file
with
2048 addition
and
156 deletion
+2048
-156
paddle/function/CMakeLists.txt
paddle/function/CMakeLists.txt
+8
-4
paddle/function/ContextProjectionOp.cpp
paddle/function/ContextProjectionOp.cpp
+77
-84
paddle/function/ContextProjectionOp.h
paddle/function/ContextProjectionOp.h
+28
-26
paddle/function/ContextProjectionOpGpu.cu
paddle/function/ContextProjectionOpGpu.cu
+20
-24
paddle/function/TensorTypeTest.cpp
paddle/function/TensorTypeTest.cpp
+17
-0
paddle/gserver/layers/ContextProjection.cpp
paddle/gserver/layers/ContextProjection.cpp
+24
-18
paddle/math/Matrix.h
paddle/math/Matrix.h
+4
-0
paddle/math/Matrix.h~RFbb8b484f.TMP
paddle/math/Matrix.h~RFbb8b484f.TMP
+1870
-0
未找到文件。
paddle/function/CMakeLists.txt
浏览文件 @
41c52d3b
...
...
@@ -3,6 +3,7 @@ file(GLOB cpp_files . *Op.cpp)
list
(
APPEND h_files Function.h
)
list
(
APPEND cpp_files Function.cpp
)
list
(
APPEND cpp_files BufferArg.cpp
)
if
(
WITH_GPU
)
file
(
GLOB cu_files . *OpGpu.cu
)
...
...
@@ -16,10 +17,13 @@ if(WITH_TESTING)
# TODO:
# file(GLOB test_files . *OpTest.cpp)
# add_executable(${test_bin} EXCLUDE_FROM_ALL ${test_files})
add_simple_unittest
(
CrossMapNormalOpTest
)
add_unittest
(
ContextProjectionOpTest
ContextProjectionOpTest.cpp
../gserver/tests/TestUtil.cpp
)
# add_simple_unittest(CrossMapNormalOpTest)
add_simple_unittest
(
TensorShapeTest
)
add_simple_unittest
(
TensorTypeTest
)
add_simple_unittest
(
BufferArgTest
)
# add_unittest(ContextProjectionOpTest
# ContextProjectionOpTest.cpp
# ../gserver/tests/TestUtil.cpp)
endif
()
endif
()
...
...
paddle/function/ContextProjectionOp.cpp
浏览文件 @
41c52d3b
...
...
@@ -19,17 +19,15 @@ limitations under the License. */
namespace
paddle
{
template
<
>
void
ContextProjectionForward
<
DEVICE_TYPE_CPU
>
(
CpuMatrix
*
out_mat
,
const
CpuMatrix
*
input_mat
,
const
CpuMatrix
*
weight_mat
,
void
ContextProjectionForward
<
DEVICE_TYPE_CPU
>
(
CpuMatrix
&
out_mat
,
const
CpuMatrix
&
input_mat
,
const
CpuMatrix
&
weight_mat
,
const
CpuIVector
&
seq_vec
,
size_t
context_length
,
int
context_start
,
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
;
...
...
@@ -39,10 +37,11 @@ void ContextProjectionForward<DEVICE_TYPE_CPU>(CpuMatrix* out_mat,
if
(
begin
<
starts
[
i
])
{
int64_t
pad_size
=
std
::
min
(
starts
[
i
]
-
begin
,
starts
[
i
+
1
]
-
starts
[
i
]);
MatrixPtr
mat
=
out_mat
->
subMatrix
(
starts
[
i
],
pad_size
);
if
(
w_mat
)
{
MatrixPtr
sub
=
w_mat
->
subMatrix
(
j
,
pad_size
);
mat
->
addAtOffset
(
*
sub
,
j
*
in_mat
->
getWidth
());
MatrixPtr
mat
=
out_mat
.
subMatrix
(
starts
[
i
],
pad_size
);
if
(
weight_mat
)
{
MatrixPtr
sub
=
const_cast
<
CpuMatrix
&>
(
weight_mat
).
subMatrix
(
j
,
pad_size
);
mat
->
addAtOffset
(
*
sub
,
j
*
input_mat
.
getWidth
());
}
dst_begin
=
starts
[
i
]
+
pad_size
;
begin
=
starts
[
i
];
...
...
@@ -50,19 +49,22 @@ void ContextProjectionForward<DEVICE_TYPE_CPU>(CpuMatrix* out_mat,
if
(
end
>
starts
[
i
+
1
])
{
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
(
w_mat
)
{
MatrixPtr
sub
=
w_mat
->
subMatrix
(
begin_pad
+
context_start
+
j
-
pad_size
,
pad_size
);
mat
->
addAtOffset
(
*
sub
,
j
*
in_mat
->
getWidth
());
MatrixPtr
mat
=
out_mat
.
subMatrix
(
starts
[
i
+
1
]
-
pad_size
,
pad_size
);
if
(
weight_mat
)
{
MatrixPtr
sub
=
const_cast
<
CpuMatrix
&>
(
weight_mat
)
.
subMatrix
(
begin_pad
+
context_start
+
j
-
pad_size
,
pad_size
);
mat
->
addAtOffset
(
*
sub
,
j
*
input_mat
.
getWidth
());
}
dst_end
=
starts
[
i
+
1
]
-
pad_size
;
end
=
starts
[
i
+
1
];
}
if
(
end
<=
begin
)
continue
;
MatrixPtr
src
=
in_mat
->
subMatrix
(
begin
,
end
-
begin
);
MatrixPtr
dst
=
out_mat
->
subMatrix
(
dst_begin
,
dst_end
-
dst_begin
);
dst
->
addAtOffset
(
*
src
,
j
*
in_mat
->
getWidth
());
MatrixPtr
src
=
const_cast
<
CpuMatrix
&>
(
input_mat
).
subMatrix
(
begin
,
end
-
begin
);
MatrixPtr
dst
=
out_mat
.
subMatrix
(
dst_begin
,
dst_end
-
dst_begin
);
dst
->
addAtOffset
(
*
src
,
j
*
input_mat
.
getWidth
());
}
}
}
...
...
@@ -82,40 +84,34 @@ public:
begin_pad_
=
config
.
get
<
size_t
>
(
"begin_pad"
);
}
void
calc
(
const
Argument
s
&
inputs
,
const
Argument
s
&
outputs
,
const
Argument
s
&
inouts
)
override
{
void
calc
(
const
BufferArg
s
&
inputs
,
const
BufferArg
s
&
outputs
,
const
BufferArg
s
&
inouts
)
override
{
CHECK_EQ
(
3
,
inputs
.
size
());
CHECK_EQ
(
1
,
outputs
.
size
());
CHECK_EQ
(
0
,
inouts
.
size
());
CHECK
(
outputs
[
0
].
getData
()
&&
inputs
[
0
].
getData
()
&&
inputs
[
2
].
getD
ata
());
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
);
CHECK
(
outputs
[
0
].
data
()
&&
inputs
[
0
].
data
()
&&
inputs
[
2
].
d
ata
());
CHECK_EQ
(
outputs
[
0
].
shape
().
ndims
(),
2
);
CHECK_EQ
(
inputs
[
0
].
shape
().
ndims
(),
2
);
CHECK_EQ
(
inputs
[
1
].
shape
().
ndims
(),
2
);
CHECK_EQ
(
inputs
[
2
].
shape
().
ndims
(),
1
);
/// dim of output = dim of input * context_length
CHECK_EQ
(
outputs
[
0
].
dims_
[
1
],
inputs
[
0
].
dims_
[
1
]
*
context_length_
);
CHECK_EQ
(
outputs
[
0
].
shape
()[
1
],
inputs
[
0
].
shape
()
[
1
]
*
context_length_
);
/// dim of input == dim of weight
CHECK_EQ
(
inputs
[
0
].
dims_
[
1
],
inputs
[
1
].
dims_
[
1
]);
CHECK_EQ
(
inputs
[
0
].
shape
()[
1
],
inputs
[
1
].
shape
()
[
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
(),
CHECK_EQ
(
inputs
[
0
].
shape
()[
0
],
outputs
[
0
].
shape
()[
0
]);
auto
out_mat
=
outputs
[
0
].
matrix
<
Device
>
();
auto
in_mat
=
inputs
[
0
].
matrix
<
Device
>
();
auto
w_mat
=
!
inputs
[
1
].
data
()
?
typename
Tensor
<
real
,
Device
>::
Matrix
(
nullptr
,
0
,
0
)
:
inputs
[
1
].
matrix
<
Device
>
();
auto
seq_vec
=
inputs
[
2
].
vector
<
int
,
Device
>
();
ContextProjectionForward
<
Device
>
(
out_mat
,
in_mat
,
w_mat
,
seq_vec
,
context_length_
,
context_start_
,
...
...
@@ -129,18 +125,17 @@ private:
};
template
<
>
void
ContextProjectionBackward
<
DEVICE_TYPE_CPU
>
(
CpuMatrix
*
out_grad_mat
,
CpuMatrix
*
in_grad_mat
,
CpuMatrix
*
w_grad_mat
,
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_mat
);
size_t
input_dim
=
in_grad_mat
?
in_grad_mat
->
getWidth
()
:
w_grad_mat
?
w_grad_mat
->
getWidth
()
:
0
;
size_t
input_dim
=
in_grad_mat
?
in_grad_mat
.
getWidth
()
:
w_grad_mat
?
w_grad_mat
.
getWidth
()
:
0
;
const
int
*
starts
=
seq_vec
.
getData
();
size_t
num_sequences
=
seq_vec
.
getSize
()
-
1
;
for
(
size_t
i
=
0
;
i
<
num_sequences
;
++
i
)
{
...
...
@@ -153,8 +148,8 @@ void ContextProjectionBackward<DEVICE_TYPE_CPU>(CpuMatrix* out_grad_mat,
int64_t
pad_size
=
std
::
min
(
starts
[
i
]
-
begin
,
starts
[
i
+
1
]
-
starts
[
i
]);
if
(
is_padding
&&
w_grad_mat
)
{
MatrixPtr
mat
=
out_grad_mat
->
subMatrix
(
starts
[
i
],
pad_size
);
MatrixPtr
sub
=
w_grad_mat
->
subMatrix
(
j
,
pad_size
);
MatrixPtr
mat
=
out_grad_mat
.
subMatrix
(
starts
[
i
],
pad_size
);
MatrixPtr
sub
=
w_grad_mat
.
subMatrix
(
j
,
pad_size
);
sub
->
addAtOffset
(
*
mat
,
j
*
input_dim
);
}
dst_begin
=
starts
[
i
]
+
pad_size
;
...
...
@@ -165,8 +160,8 @@ void ContextProjectionBackward<DEVICE_TYPE_CPU>(CpuMatrix* out_grad_mat,
std
::
min
(
end
-
starts
[
i
+
1
],
starts
[
i
+
1
]
-
starts
[
i
]);
if
(
is_padding
&&
w_grad_mat
)
{
MatrixPtr
mat
=
out_grad_mat
->
subMatrix
(
starts
[
i
+
1
]
-
pad_size
,
pad_size
);
MatrixPtr
sub
=
w_grad_mat
->
subMatrix
(
out_grad_mat
.
subMatrix
(
starts
[
i
+
1
]
-
pad_size
,
pad_size
);
MatrixPtr
sub
=
w_grad_mat
.
subMatrix
(
begin_pad
+
context_start
+
j
-
pad_size
,
pad_size
);
sub
->
addAtOffset
(
*
mat
,
j
*
input_dim
);
}
...
...
@@ -175,8 +170,8 @@ void ContextProjectionBackward<DEVICE_TYPE_CPU>(CpuMatrix* out_grad_mat,
}
if
(
end
<=
begin
)
continue
;
if
(
!
in_grad_mat
)
continue
;
MatrixPtr
src
=
in_grad_mat
->
subMatrix
(
begin
,
end
-
begin
);
MatrixPtr
dst
=
out_grad_mat
->
subMatrix
(
dst_begin
,
dst_end
-
dst_begin
);
MatrixPtr
src
=
in_grad_mat
.
subMatrix
(
begin
,
end
-
begin
);
MatrixPtr
dst
=
out_grad_mat
.
subMatrix
(
dst_begin
,
dst_end
-
dst_begin
);
src
->
addAtOffset
(
*
dst
,
j
*
input_dim
);
}
}
...
...
@@ -199,44 +194,37 @@ public:
total_pad_
=
config
.
get
<
size_t
>
(
"total_pad"
);
}
void
calc
(
const
Argument
s
&
inputs
,
const
Argument
s
&
outputs
,
const
Argument
s
&
inouts
)
override
{
void
calc
(
const
BufferArg
s
&
inputs
,
const
BufferArg
s
&
outputs
,
const
BufferArg
s
&
inouts
)
override
{
CHECK_EQ
(
3
,
inputs
.
size
());
CHECK_EQ
(
1
,
outputs
.
size
());
CHECK_EQ
(
0
,
inouts
.
size
());
CHECK
(
outputs
[
0
].
getData
()
&&
inputs
[
2
].
getD
ata
());
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
);
CHECK
(
outputs
[
0
].
data
()
&&
inputs
[
2
].
d
ata
());
CHECK_EQ
(
outputs
[
0
].
shape
().
ndims
(),
2
);
CHECK_EQ
(
inputs
[
0
].
shape
().
ndims
(),
2
);
CHECK_EQ
(
inputs
[
1
].
shape
().
ndims
(),
2
);
CHECK_EQ
(
inputs
[
2
].
shape
().
ndims
(),
1
);
/// dim of input == dim of weight
CHECK_EQ
(
inputs
[
0
].
dims_
[
1
],
inputs
[
1
].
dims_
[
1
]);
CHECK_EQ
(
inputs
[
0
].
shape
()[
1
],
inputs
[
1
].
shape
()
[
1
]);
/// input and output has the same batch_size
CHECK_EQ
(
inputs
[
0
].
dims_
[
0
],
outputs
[
0
].
dims_
[
0
]);
CHECK_EQ
(
inputs
[
0
].
shape
()[
0
],
outputs
[
0
].
shape
()
[
0
]);
/// dim of output = dim of input * context_length
CHECK_EQ
(
outputs
[
0
].
dims_
[
1
],
inputs
[
0
].
dims_
[
1
]
*
context_length_
);
CHECK_EQ
(
outputs
[
0
].
shape
()[
1
],
inputs
[
0
].
shape
()
[
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
out_grad_mat
=
outputs
[
0
].
matrix
<
Device
>
();
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
,
!
inputs
[
0
].
data
()
?
typename
Tensor
<
real
,
Device
>::
Matrix
(
nullptr
,
0
,
0
)
:
inputs
[
0
].
matrix
<
Device
>
();
auto
w_grad_mat
=
!
inputs
[
1
].
data
()
?
typename
Tensor
<
real
,
Device
>::
Matrix
(
nullptr
,
0
,
0
)
:
inputs
[
1
].
matrix
<
Device
>
();
auto
seq_vec
=
inputs
[
2
].
vector
<
int
,
Device
>
();
ContextProjectionBackward
<
Device
>
(
out_grad_mat
,
in_grad_mat
,
w_grad_mat
,
seq_vec
,
context_length_
,
context_start_
,
...
...
@@ -253,6 +241,7 @@ private:
size_t
total_pad_
;
};
#if 0
/**
* \param inputs[0] input grad.
* \param inputs[1] input sequence.
...
...
@@ -272,6 +261,7 @@ 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);
...
...
@@ -349,6 +339,7 @@ private:
size_t begin_pad_;
size_t total_pad_;
};
#endif
REGISTER_TYPED_FUNC
(
ContextProjectionForward
,
CPU
,
...
...
@@ -363,6 +354,7 @@ REGISTER_TYPED_FUNC(ContextProjectionForward,
REGISTER_TYPED_FUNC
(
ContextProjectionBackward
,
GPU
,
ContextProjectionBackwardFunc
);
#if 0
REGISTER_TYPED_FUNC(ContextProjectionBackwardData,
GPU,
ContextProjectionBackwardDataFunc);
...
...
@@ -370,4 +362,5 @@ REGISTER_TYPED_FUNC(ContextProjectionBackwardWeight,
GPU,
ContextProjectionBackwardWeightFunc);
#endif
#endif
}
// namespace paddle
paddle/function/ContextProjectionOp.h
浏览文件 @
41c52d3b
...
...
@@ -31,14 +31,15 @@ namespace paddle {
* \param[in] is_padding whether padding 0 or not.
*
*/
template
<
DeviceType
Device
>
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
);
template
<
DeviceType
DType
>
void
ContextProjectionForward
(
typename
Tensor
<
real
,
DType
>::
Matrix
&
output
,
const
typename
Tensor
<
real
,
DType
>::
Matrix
&
input
,
const
typename
Tensor
<
real
,
DType
>::
Matrix
&
weight
,
const
typename
Tensor
<
int
,
DType
>::
Vector
&
sequence
,
size_t
context_length
,
int
context_start
,
size_t
begin_pad
);
/**
* \brief Context Projection Backward.
...
...
@@ -53,30 +54,31 @@ void ContextProjectionForward(typename MatrixT<Device>::type* output,
* \param[in] is_padding whether padding 0 or not.
*
*/
template
<
DeviceType
Device
>
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
,
bool
is_padding
,
size_t
total_pad
);
template
<
DeviceType
DType
>
void
ContextProjectionBackward
(
typename
Tensor
<
real
,
DType
>::
Matrix
&
out_grad
,
typename
Tensor
<
real
,
DType
>::
Matrix
&
in_grad
,
typename
Tensor
<
real
,
DType
>::
Matrix
&
w_grad
,
const
typename
Tensor
<
int
,
DType
>::
Vector
&
seq_vec
,
size_t
context_length
,
int
context_start
,
size_t
begin_pad
,
bool
is_padding
,
size_t
total_pad
);
template
<
DeviceType
D
evic
e
>
template
<
DeviceType
D
Typ
e
>
void
ContextProjectionBackwardData
(
typename
MatrixT
<
Device
>::
type
*
out_grad
,
typename
MatrixT
<
Device
>::
type
*
in_grad
,
const
typename
SequenceT
<
Device
>::
type
&
sequence
,
typename
Tensor
<
real
,
DType
>::
Matrix
&
out_grad
,
typename
Tensor
<
real
,
DType
>::
Matrix
&
in_grad
,
const
typename
Tensor
<
int
,
DType
>::
Vector
&
sequence
,
size_t
context_length
,
int
context_start
);
template
<
DeviceType
D
evic
e
>
template
<
DeviceType
D
Typ
e
>
void
ContextProjectionBackwardWeight
(
typename
MatrixT
<
Device
>::
type
*
out_grad
,
typename
MatrixT
<
Device
>::
type
*
w_grad
,
const
typename
SequenceT
<
Device
>::
type
&
seq_vec
,
typename
Tensor
<
real
,
DType
>::
Matrix
&
out_grad
,
typename
Tensor
<
real
,
DType
>::
Matrix
&
w_grad
,
const
typename
Tensor
<
int
,
DType
>::
Vector
&
seq_vec
,
size_t
context_length
,
int
context_start
,
size_t
total_pad
,
...
...
paddle/function/ContextProjectionOpGpu.cu
浏览文件 @
41c52d3b
...
...
@@ -120,20 +120,19 @@ void hl_context_projection_forward(const real* input,
}
template
<
>
void
ContextProjectionForward
<
DEVICE_TYPE_GPU
>
(
GpuMatrix
*
output
,
const
GpuMatrix
*
input
,
const
GpuMatrix
*
weight
,
void
ContextProjectionForward
<
DEVICE_TYPE_GPU
>
(
GpuMatrix
&
output
,
const
GpuMatrix
&
input
,
const
GpuMatrix
&
weight
,
const
GpuIVector
&
sequence
,
size_t
context_length
,
int
context_start
,
size_t
begin_pad
)
{
CHECK
(
input
&&
output
);
hl_context_projection_forward
(
input
->
getData
(),
hl_context_projection_forward
(
input
.
getData
(),
sequence
.
getData
(),
weight
?
weight
->
getData
()
:
nullptr
,
output
->
getData
(),
weight
?
weight
.
getData
()
:
nullptr
,
output
.
getData
(),
sequence
.
getSize
()
-
1
,
input
->
getWidth
(),
input
.
getWidth
(),
context_length
,
context_start
,
begin_pad
);
...
...
@@ -217,17 +216,16 @@ void hl_context_projection_backward_data(real* out_grad,
}
template
<
>
void
ContextProjectionBackwardData
<
DEVICE_TYPE_GPU
>
(
GpuMatrix
*
out_grad
,
GpuMatrix
*
in_grad
,
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
(),
hl_context_projection_backward_data
(
out_grad
.
getData
(),
sequence
.
getData
(),
in_grad
->
getData
(),
in_grad
.
getData
(),
sequence
.
getSize
()
-
1
,
in_grad
->
getWidth
(),
in_grad
.
getWidth
(),
context_length
,
context_start
);
}
...
...
@@ -348,19 +346,18 @@ void hl_context_projection_backward_weight(real* out_grad,
template
<
>
void
ContextProjectionBackwardWeight
<
DEVICE_TYPE_GPU
>
(
GpuMatrix
*
out_grad
,
GpuMatrix
*
w_grad
,
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
(),
hl_context_projection_backward_weight
(
out_grad
.
getData
(),
seq_vec
.
getData
(),
w_grad
->
getData
(),
w_grad
.
getData
(),
seq_vec
.
getSize
()
-
1
,
w_grad
->
getWidth
(),
w_grad
.
getWidth
(),
total_pad
,
context_length
,
context_start
,
...
...
@@ -368,16 +365,15 @@ void ContextProjectionBackwardWeight<DEVICE_TYPE_GPU>(
}
template
<
>
void
ContextProjectionBackward
<
DEVICE_TYPE_GPU
>
(
GpuMatrix
*
out_grad
,
GpuMatrix
*
in_grad
,
GpuMatrix
*
w_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
,
...
...
paddle/function/TensorTypeTest.cpp
浏览文件 @
41c52d3b
...
...
@@ -44,4 +44,21 @@ TEST(TensorType, Vector) {
EXPECT_EQ
(
gpuIVector
.
getSize
(),
100
);
}
TEST
(
TensorType
,
EmptyMatrix
)
{
CpuMatrix
empty
(
nullptr
,
0
,
0
);
CpuMatrix
nonEmpty
(
10
,
10
);
EXPECT_EQ
(
empty
.
isEmpty
(),
true
);
EXPECT_EQ
(
nonEmpty
.
isEmpty
(),
false
);
CHECK
(
nonEmpty
);
auto
function
=
[](
const
CpuMatrix
&
matrix
)
{
if
(
matrix
)
{
EXPECT_NE
(
matrix
.
getData
(),
nullptr
);
}
else
{
EXPECT_EQ
(
matrix
.
getData
(),
nullptr
);
}
};
function
(
empty
);
function
(
nonEmpty
);
}
}
// namespace paddle
paddle/gserver/layers/ContextProjection.cpp
浏览文件 @
41c52d3b
...
...
@@ -110,7 +110,7 @@ void ContextProjection::forward() {
size_t
input_dim
=
in_
->
value
->
getWidth
();
size_t
dim
=
out_
->
value
->
getWidth
();
CHECK_EQ
(
dim
,
input_dim
*
config_
.
context_length
());
size_t
batch_size
=
in_
->
value
->
getHeight
();
//
size_t batch_size = in_->value->getHeight();
CHECK_EQ
(
forward_
.
size
(),
1
)
<<
"Only one forward function here"
;
REGISTER_TIMER_INFO
(
"ContextProjectionForward"
,
getName
().
c_str
());
...
...
@@ -119,14 +119,17 @@ void ContextProjection::forward() {
auto
w_ptr
=
state_
?
state_
.
get
()
:
is_padding
?
weight_
->
getW
().
get
()
:
nullptr
;
auto
start_pos
=
in_
->
sequenceStartPositions
;
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
}),
Tensor
(
reinterpret_cast
<
real
*>
(
const_cast
<
int
*>
(
start_pos
->
getData
(
useGpu_
))),
Dims
{
start_pos
->
getSize
()})},
{
Tensor
(
out_
->
value
->
getData
(),
Dims
{
batch_size
,
dim
})},
{});
BufferArgs
inputs
;
BufferArgs
outputs
;
BufferArgs
inouts
;
inputs
.
addArg
(
*
in_
->
value
);
inputs
.
addArg
(
CpuMatrix
(
w_ptr
?
w_ptr
->
getData
()
:
nullptr
,
w_ptr
?
w_ptr
->
getHeight
()
:
0
,
input_dim
));
inputs
.
addArg
(
*
in_
->
sequenceStartPositions
->
getVector
(
useGpu_
));
outputs
.
addArg
(
*
out_
->
value
);
forward_
[
0
]
->
calc
(
inputs
,
outputs
,
inouts
);
if
(
state_
&&
config_
.
context_start
()
<
0
)
{
CHECK_EQ
(
1
,
in_
->
getNumSequences
());
...
...
@@ -160,15 +163,18 @@ void ContextProjection::backward(const UpdateCallback& callback) {
bool
is_padding
=
config_
.
trainable_padding
();
auto
start_pos
=
in_
->
sequenceStartPositions
;
auto
w_ptr
=
is_padding
?
weight_
->
getWGrad
()
:
nullptr
;
backward_
[
0
]
->
calc
({
Tensor
(
in_
->
grad
?
in_
->
grad
->
getData
()
:
nullptr
,
Dims
{
batch_size
,
input_dim
}),
Tensor
(
w_ptr
?
w_ptr
->
getData
()
:
nullptr
,
Dims
{
w_ptr
?
w_ptr
->
getHeight
()
:
0
,
input_dim
}),
Tensor
(
reinterpret_cast
<
real
*>
(
const_cast
<
int
*>
(
start_pos
->
getData
(
useGpu_
))),
Dims
{
start_pos
->
getSize
()})},
{
Tensor
(
out_
->
grad
->
getData
(),
Dims
{
batch_size
,
dim
})},
{});
BufferArgs
inputs
;
BufferArgs
outputs
;
BufferArgs
inouts
;
inputs
.
addArg
(
CpuMatrix
(
in_
->
grad
?
in_
->
grad
->
getData
()
:
nullptr
,
batch_size
,
input_dim
));
inputs
.
addArg
(
CpuMatrix
(
w_ptr
?
w_ptr
->
getData
()
:
nullptr
,
w_ptr
?
w_ptr
->
getHeight
()
:
0
,
input_dim
));
inputs
.
addArg
(
*
in_
->
sequenceStartPositions
->
getVector
(
useGpu_
));
outputs
.
addArg
(
*
out_
->
grad
);
backward_
[
0
]
->
calc
(
inputs
,
outputs
,
inouts
);
if
(
config_
.
trainable_padding
())
{
weight_
->
getParameterPtr
()
->
incUpdate
(
callback
);
...
...
paddle/math/Matrix.h
浏览文件 @
41c52d3b
...
...
@@ -1091,6 +1091,10 @@ public:
TensorCpuApply
<
real
>
(
*
this
,
expr
);
}
}
bool
isEmpty
()
const
{
return
data_
==
nullptr
;
}
explicit
operator
bool
()
const
{
return
!
isEmpty
();
}
};
inline
std
::
ostream
&
operator
<<
(
std
::
ostream
&
os
,
const
Matrix
&
mat
)
{
...
...
paddle/math/Matrix.h~RFbb8b484f.TMP
0 → 100644
浏览文件 @
41c52d3b
此差异已折叠。
点击以展开。
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录