Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
f23a1170
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2299
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看板
提交
f23a1170
编写于
12月 28, 2016
作者:
X
xutianbing
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add Context Projection functions to Layer's forward and backward,
resolve merge conflicts
上级
590ecba3
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
137 addition
and
47 deletion
+137
-47
paddle/function/ContextProjectionOp.cpp
paddle/function/ContextProjectionOp.cpp
+1
-1
paddle/function/ContextProjectionOp.h
paddle/function/ContextProjectionOp.h
+0
-0
paddle/function/ContextProjectionOpGpu.cu
paddle/function/ContextProjectionOpGpu.cu
+2
-2
paddle/function/ContextProjectionOpTest.cpp
paddle/function/ContextProjectionOpTest.cpp
+0
-0
paddle/function/Function.cpp
paddle/function/Function.cpp
+2
-2
paddle/gserver/layers/ContextProjection.cpp
paddle/gserver/layers/ContextProjection.cpp
+106
-42
paddle/gserver/layers/ContextProjection.h
paddle/gserver/layers/ContextProjection.h
+2
-0
paddle/gserver/layers/Projection.h
paddle/gserver/layers/Projection.h
+24
-0
未找到文件。
paddle/function/
context_projection_o
p.cpp
→
paddle/function/
ContextProjectionO
p.cpp
浏览文件 @
f23a1170
...
...
@@ -12,7 +12,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#include "
context_projection_o
p.h"
#include "
ContextProjectionO
p.h"
#include "paddle/math/Matrix.h"
#include "paddle/math/Vector.h"
...
...
paddle/function/
context_projection_o
p.h
→
paddle/function/
ContextProjectionO
p.h
浏览文件 @
f23a1170
文件已移动
paddle/function/
context_projection_op_g
pu.cu
→
paddle/function/
ContextProjectionOpG
pu.cu
浏览文件 @
f23a1170
...
...
@@ -13,7 +13,7 @@ See the License for the specific language governing permissions and
limitations under the License. */
#include "hl_base.h"
#include "
context_projection_o
p.h"
#include "
ContextProjectionO
p.h"
namespace
paddle
{
...
...
@@ -327,7 +327,7 @@ void ContextProjectionBackwardWeight<DEVICE_TYPE_GPU>(Tensor& out_grad,
int
context_start
,
size_t
total_pad
,
size_t
begin_pad
)
{
CHECK
(
w_grad
.
getData
()
&&
out_grad
.
getData
());
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
);
...
...
paddle/function/
context_projection_op_t
est.cpp
→
paddle/function/
ContextProjectionOpT
est.cpp
浏览文件 @
f23a1170
文件已移动
paddle/function/Function.cpp
浏览文件 @
f23a1170
...
...
@@ -60,14 +60,14 @@ FuncConfig& FuncConfig::set<real>(const std::string& key, real v) {
template
<
>
FuncConfig
&
FuncConfig
::
set
<
int
>
(
const
std
::
string
&
key
,
int
v
)
{
CHECK
(
valueMap_
.
count
(
key
)
==
0
)
<<
"Duplicated value: "
<<
key
;
CHECK
_EQ
(
valueMap_
.
count
(
key
),
0
)
<<
"Duplicated value: "
<<
key
;
valueMap_
[
key
].
i
=
v
;
return
*
this
;
}
template
<
>
FuncConfig
&
FuncConfig
::
set
<
bool
>
(
const
std
::
string
&
key
,
bool
v
)
{
CHECK
(
valueMap_
.
count
(
key
)
==
0
)
<<
"Duplicated value: "
<<
key
;
CHECK
_EQ
(
valueMap_
.
count
(
key
),
0
)
<<
"Duplicated value: "
<<
key
;
valueMap_
[
key
].
b
=
v
;
return
*
this
;
}
...
...
paddle/gserver/layers/ContextProjection.cpp
浏览文件 @
f23a1170
...
...
@@ -38,6 +38,53 @@ ContextProjection::ContextProjection(const ProjectionConfig& config,
CHECK_EQ
(
inputDim
*
totalPad
,
parameter
->
getSize
());
weight_
.
reset
(
new
Weight
(
totalPad
,
inputDim
,
parameter
));
}
// init forward_ and backward_ functions
init
();
}
bool
ContextProjection
::
init
()
{
size_t
context_length
=
config_
.
context_length
();
int
context_start
=
config_
.
context_start
();
bool
is_padding
=
config_
.
trainable_padding
();
size_t
total_pad
=
is_padding
?
beginPad_
+
endPad_
:
0
;
if
(
!
useGpu_
)
{
// CPU functions
createFunction
(
forward_
,
"ContextProjectionForward-CPU"
,
FuncConfig
()
.
set
(
"context_length"
,
context_length
)
.
set
(
"context_start"
,
context_start
)
.
set
(
"begin_pad"
,
beginPad_
)
.
set
(
"is_padding"
,
is_padding
));
createFunction
(
backward_
,
"ContextProjectionBackward-CPU"
,
FuncConfig
()
.
set
(
"context_length"
,
context_length
)
.
set
(
"context_start"
,
context_start
)
.
set
(
"begin_pad"
,
beginPad_
)
.
set
(
"is_padding"
,
is_padding
));
}
else
{
// GPU functions
createFunction
(
forward_
,
"ContextProjectionForward-GPU"
,
FuncConfig
()
.
set
(
"context_length"
,
context_length
)
.
set
(
"context_start"
,
context_start
)
.
set
(
"begin_pad"
,
beginPad_
)
.
set
(
"is_padding"
,
is_padding
));
createFunction
(
backward_
,
"ContextProjectionBackwardData-GPU"
,
FuncConfig
()
.
set
(
"context_length"
,
context_length
)
.
set
(
"context_start"
,
context_start
));
createFunction
(
backward_
,
"ContextProjectionBackwardWeight-GPU"
,
FuncConfig
()
.
set
(
"context_length"
,
context_length
)
.
set
(
"context_start"
,
context_start
)
.
set
(
"begin_pad"
,
beginPad_
)
.
set
(
"total_pad"
,
total_pad
));
}
return
true
;
}
void
ContextProjection
::
resetState
()
{
...
...
@@ -78,25 +125,35 @@ LayerStatePtr ContextProjection::getState() {
}
void
ContextProjection
::
forward
()
{
CHECK
(
in_
->
value
);
CHECK
(
in_
->
value
&&
out_
->
value
);
CHECK
(
in_
->
sequenceStartPositions
);
auto
startPositions
=
in_
->
sequenceStartPositions
->
getVector
(
useGpu_
);
int64_t
inputDim
=
in_
->
value
->
getWidth
(
);
int64_t
dim
=
out_
->
value
->
getWidth
();
CHECK_EQ
(
dim
,
inputDim
*
config_
.
context_length
());
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
();
CHECK_EQ
(
batch_size
,
out_
->
value
->
getHeight
());
REGISTER_TIMER_INFO
(
"ContextProjectionForward"
,
getName
().
c_str
());
bool
isPadding
=
config_
.
trainable_padding
();
out_
->
value
->
contextProjectionForward
(
*
(
in_
->
value
),
state_
?
state_
.
get
()
:
isPadding
?
weight_
->
getW
().
get
()
:
nullptr
,
*
startPositions
,
config_
.
context_length
(),
config_
.
context_start
(),
beginPad_
,
state_
?
true
:
isPadding
);
bool
is_padding
=
config_
.
trainable_padding
();
/// first use state_, otherwise use weight_(padding false === w nullptr)
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
}),
Tensor
(
reinterpret_cast
<
real
*>
(
const_cast
<
int
*>
(
start_pos
->
getData
(
useGpu_
))),
Dims
{
start_pos
->
getSize
()})},
{
Tensor
(
out_
->
value
->
getData
(),
Dims
{
batch_size
,
dim
})},
{});
if
(
state_
&&
config_
.
context_start
()
<
0
)
{
CHECK_EQ
(
1
,
in_
->
getNumSequences
());
...
...
@@ -118,39 +175,46 @@ void ContextProjection::forward() {
}
void
ContextProjection
::
backward
(
const
UpdateCallback
&
callback
)
{
CHECK
(
in_
->
value
);
int64_t
inputDim
=
in_
->
value
->
getWidth
();
int64_t
dim
=
out_
->
value
->
getWidth
();
CHECK_EQ
(
dim
,
inputDim
*
config_
.
context_length
());
auto
startPositions
=
in_
->
sequenceStartPositions
->
getVector
(
useGpu_
);
CHECK
(
in_
->
value
&&
out_
->
value
&&
out_
->
grad
);
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
();
CHECK_EQ
(
batch_size
,
out_
->
value
->
getHeight
());
REGISTER_TIMER_INFO
(
"ContextProjectionBackward"
,
getName
().
c_str
());
bool
isPadding
=
config_
.
trainable_padding
();
bool
is_padding
=
config_
.
trainable_padding
();
auto
start_pos
=
in_
->
sequenceStartPositions
;
if
(
!
out_
->
grad
->
useGpu
())
{
out_
->
grad
->
contextProjectionBackward
(
in_
->
grad
.
get
(),
isPadding
?
weight_
->
getWGrad
().
get
()
:
nullptr
,
*
startPositions
,
config_
.
context_length
(),
config_
.
context_start
(),
beginPad_
,
isPadding
);
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
})},
{});
}
else
{
if
(
in_
->
grad
)
{
out_
->
grad
->
contextProjectionBackwardData
(
*
(
in_
->
grad
),
*
startPositions
,
config_
.
context_length
(),
config_
.
context_start
());
backward_
[
0
]
->
calc
(
{
Tensor
(
in_
->
grad
->
getData
(),
Dims
{
batch_size
,
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
})},
{});
}
if
(
isPadding
&&
weight_
->
getWGrad
())
{
out_
->
grad
->
contextProjectionBackwardWeight
(
*
(
weight_
->
getWGrad
()
),
*
startPositions
,
config_
.
context_length
(
),
config_
.
context_start
()
,
weight_
->
getWGrad
()
->
getHeight
()
,
beginPad_
);
if
(
is_padding
&&
weight_
->
getWGrad
())
{
backward_
[
1
]
->
calc
(
{
Tensor
(
weight_
->
getWGrad
()
->
getData
(),
Dims
{
weight_
->
getWGrad
()
->
getHeight
(),
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
})}
,
{}
);
}
}
...
...
paddle/gserver/layers/ContextProjection.h
浏览文件 @
f23a1170
...
...
@@ -61,6 +61,8 @@ public:
virtual
LayerStatePtr
getState
();
virtual
bool
init
();
protected:
std
::
unique_ptr
<
Weight
>
weight_
;
/// number of extra timesteps added at the beginning
...
...
paddle/gserver/layers/Projection.h
浏览文件 @
f23a1170
...
...
@@ -88,11 +88,31 @@ public:
*/
virtual
LayerStatePtr
getState
()
{
return
nullptr
;
}
/**
* init forward_ and backward_ functions
*/
virtual
bool
init
()
{
return
true
;
}
/**
* Get output size of projection.
*/
size_t
getOutputSize
()
const
{
return
config_
.
output_size
();
}
protected:
/**
* Create layer function. Function is called in forward or backward.
* \param function, Layer::forward_ or Layer::backward_
* \param name, function name, include -GPU or -CPU
* \param config, initialization configuration for the function
*/
void
createFunction
(
std
::
vector
<
std
::
shared_ptr
<
FunctionBase
>>&
function
,
const
std
::
string
&
name
,
const
FuncConfig
&
config
)
{
function
.
emplace_back
(
FunctionBase
::
funcRegistrar_
.
createByType
(
name
));
auto
&
func
=
function
.
back
();
func
->
init
(
config
);
}
protected:
/// Config of projection
ProjectionConfig
config_
;
...
...
@@ -106,5 +126,9 @@ protected:
const
Argument
*
out_
;
/// Store `passType` passed to forward()
PassType
passType_
;
/// Layer forward function
std
::
vector
<
std
::
shared_ptr
<
FunctionBase
>>
forward_
;
/// Layer backward function
std
::
vector
<
std
::
shared_ptr
<
FunctionBase
>>
backward_
;
};
}
// namespace paddle
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录