Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
7e34b8e3
P
Paddle
项目概览
PaddlePaddle
/
Paddle
1 年多 前同步成功
通知
2302
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看板
提交
7e34b8e3
编写于
11月 01, 2017
作者:
Z
zchen0211
浏览文件
操作
浏览文件
下载
差异文件
Merge branch 'develop' of
https://github.com/PaddlePaddle/Paddle
into develop
上级
31187e7e
0885de47
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
287 addition
and
1 deletion
+287
-1
paddle/operators/rnn_memory_helper_op.cc
paddle/operators/rnn_memory_helper_op.cc
+154
-0
python/paddle/v2/framework/framework.py
python/paddle/v2/framework/framework.py
+3
-1
python/paddle/v2/framework/tests/test_rnn_memory_helper_op.py
...on/paddle/v2/framework/tests/test_rnn_memory_helper_op.py
+130
-0
未找到文件。
paddle/operators/rnn_memory_helper_op.cc
0 → 100644
浏览文件 @
7e34b8e3
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
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 "paddle/framework/op_registry.h"
#include "paddle/framework/operator.h"
namespace
paddle
{
namespace
operators
{
class
RNNMemoryHelperOp
:
public
framework
::
OperatorBase
{
public:
RNNMemoryHelperOp
(
const
std
::
string
&
type
,
const
framework
::
VariableNameMap
&
inputs
,
const
framework
::
VariableNameMap
&
outputs
,
const
framework
::
AttributeMap
&
attrs
)
:
OperatorBase
(
type
,
inputs
,
outputs
,
attrs
)
{}
void
Run
(
const
framework
::
Scope
&
scope
,
const
platform
::
DeviceContext
&
dev_ctx
)
const
override
{
auto
mem_var_name
=
Input
(
"X"
);
auto
*
mem_var
=
scope
.
FindVar
(
mem_var_name
);
PADDLE_ENFORCE
(
mem_var
!=
nullptr
,
"Cannot find mem_var in scope, mem_var_name is %s"
,
mem_var_name
);
auto
out_name
=
this
->
Output
(
"Out"
);
auto
*
out_var
=
scope
.
FindVar
(
out_name
);
PADDLE_ENFORCE
(
out_var
!=
nullptr
,
"Cannot find out_var in scope, out_var_name is %s"
,
out_name
);
auto
*
out_tensor
=
out_var
->
GetMutable
<
framework
::
LoDTensor
>
();
auto
&
mem_tensor
=
mem_var
->
Get
<
framework
::
LoDTensor
>
();
out_tensor
->
ShareDataWith
(
mem_tensor
);
out_tensor
->
set_lod
(
mem_tensor
.
lod
());
}
};
class
RNNMemoryHelperOpShapeInference
:
public
framework
::
InferShapeBase
{
public:
void
operator
()(
framework
::
InferShapeContext
*
ctx
)
const
override
{
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"X"
),
""
);
PADDLE_ENFORCE
(
ctx
->
HasOutput
(
"Out"
),
""
);
ctx
->
SetOutputDim
(
"Out"
,
ctx
->
GetInputDim
(
"X"
));
ctx
->
ShareLoD
(
"X"
,
/*->*/
"Out"
);
}
};
class
RNNMemoryHelperOpInfoMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
public:
RNNMemoryHelperOpInfoMaker
(
framework
::
OpProto
*
proto
,
framework
::
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"X"
,
""
);
AddOutput
(
"Out"
,
""
);
AddAttr
<
int
>
(
"data_type"
,
"(int, default 5 (FP32)) "
"Output data type"
)
.
SetDefault
(
framework
::
DataType
::
FP32
);
AddComment
(
""
);
}
};
class
RNNMemoryHelperGradOp
:
public
framework
::
OperatorBase
{
public:
RNNMemoryHelperGradOp
(
const
std
::
string
&
type
,
const
framework
::
VariableNameMap
&
inputs
,
const
framework
::
VariableNameMap
&
outputs
,
const
framework
::
AttributeMap
&
attrs
)
:
OperatorBase
(
type
,
inputs
,
outputs
,
attrs
)
{}
void
Run
(
const
framework
::
Scope
&
scope
,
const
platform
::
DeviceContext
&
dev_ctx
)
const
override
{
auto
out_grad_var_name
=
Input
(
framework
::
GradVarName
(
"Out"
));
auto
*
out_grad_var
=
scope
.
FindVar
(
out_grad_var_name
);
auto
in_grad_var_name
=
Output
(
framework
::
GradVarName
(
"X"
));
auto
*
in_grad_var
=
scope
.
FindVar
(
in_grad_var_name
);
PADDLE_ENFORCE
(
in_grad_var
!=
nullptr
,
"Cannot find in_grad_var in scope, name is %s"
,
in_grad_var_name
);
if
(
out_grad_var
==
nullptr
)
{
VLOG
(
5
)
<<
"Using fill constant 0 as starting gradient"
;
auto
in_var_name
=
Input
(
"X"
);
auto
*
in_var
=
scope
.
FindVar
(
in_var_name
);
auto
&
in_var_tensor
=
in_var
->
Get
<
framework
::
LoDTensor
>
();
framework
::
AttributeMap
attrs
;
attrs
[
"data_type"
]
=
framework
::
ToDataType
(
in_var_tensor
.
type
());
attrs
[
"shape"
]
=
framework
::
vectorize2int
(
in_var_tensor
.
dims
());
attrs
[
"value"
]
=
0.0
f
;
auto
zero_op
=
framework
::
OpRegistry
::
CreateOp
(
"fill_constant"
,
{},
{{
"Out"
,
{
in_grad_var_name
}}},
attrs
);
zero_op
->
Run
(
scope
,
dev_ctx
);
}
else
{
auto
&
out_grad_tensor
=
out_grad_var
->
Get
<
framework
::
LoDTensor
>
();
auto
*
in_grad_tensor
=
in_grad_var
->
GetMutable
<
framework
::
LoDTensor
>
();
in_grad_tensor
->
ShareDataWith
(
out_grad_tensor
);
in_grad_tensor
->
set_lod
(
out_grad_tensor
.
lod
());
}
}
};
class
RNNMemoryHelperGradOpInfoMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
public:
RNNMemoryHelperGradOpInfoMaker
(
framework
::
OpProto
*
proto
,
framework
::
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
framework
::
GradVarName
(
"Out"
),
""
);
AddInput
(
"X"
,
""
);
AddInput
(
"Out"
,
""
);
AddOutput
(
framework
::
GradVarName
(
"X"
),
""
);
AddAttr
<
int
>
(
"data_type"
,
"(int, default 5 (FP32)) "
"Output data type"
)
.
SetDefault
(
framework
::
DataType
::
FP32
);
AddComment
(
""
);
}
};
class
RNNMemoryHelperGradOpShapeInference
:
public
framework
::
InferShapeBase
{
public:
void
operator
()(
framework
::
InferShapeContext
*
ctx
)
const
override
{
auto
x_grad_name
=
framework
::
GradVarName
(
"X"
);
auto
out_grad_name
=
framework
::
GradVarName
(
"Out"
);
PADDLE_ENFORCE
(
ctx
->
HasInput
(
out_grad_name
),
""
);
PADDLE_ENFORCE
(
ctx
->
HasOutput
(
x_grad_name
),
""
);
ctx
->
SetOutputDim
(
x_grad_name
,
ctx
->
GetInputDim
(
out_grad_name
));
ctx
->
ShareLoD
(
out_grad_name
,
/*->*/
x_grad_name
);
}
};
}
// namespace operators
}
// namespace paddle
REGISTER_OPERATOR
(
rnn_memory_helper
,
paddle
::
operators
::
RNNMemoryHelperOp
,
paddle
::
operators
::
RNNMemoryHelperOpInfoMaker
,
paddle
::
operators
::
RNNMemoryHelperOpShapeInference
,
paddle
::
framework
::
DefaultGradOpDescMaker
<
true
>
);
REGISTER_OPERATOR
(
rnn_memory_helper_grad
,
paddle
::
operators
::
RNNMemoryHelperGradOp
,
paddle
::
operators
::
RNNMemoryHelperGradOpInfoMaker
,
paddle
::
operators
::
RNNMemoryHelperGradOpShapeInference
);
python/paddle/v2/framework/framework.py
浏览文件 @
7e34b8e3
...
...
@@ -264,7 +264,9 @@ class Operator(object):
self
.
desc
.
set_attr
(
attr_name
,
attrs
[
attr_name
])
self
.
desc
.
check_attrs
()
no_kernel_op_set
=
{
'feed'
,
'fetch'
,
'save'
,
'load'
}
no_kernel_op_set
=
{
'feed'
,
'fetch'
,
'save'
,
'load'
,
'rnn_memory_helper_grad'
}
if
type
not
in
no_kernel_op_set
:
self
.
desc
.
infer_var_type
(
self
.
block
.
desc
)
self
.
desc
.
infer_shape
(
self
.
block
.
desc
)
...
...
python/paddle/v2/framework/tests/test_rnn_memory_helper_op.py
0 → 100644
浏览文件 @
7e34b8e3
import
unittest
from
paddle.v2.framework.framework
import
Program
from
paddle.v2.framework.executor
import
Executor
from
paddle.v2.framework.backward
import
append_backward_ops
import
numpy
as
np
import
paddle.v2.framework.core
as
core
def
create_tensor
(
np_data
,
place
):
tensor
=
core
.
LoDTensor
()
tensor
.
set
(
np_data
,
place
)
return
tensor
class
RNNMemoryHelperOpTest
(
unittest
.
TestCase
):
def
setUp
(
self
):
self
.
program
=
Program
()
self
.
place
=
core
.
CPUPlace
()
self
.
X
=
self
.
program
.
global_block
().
create_var
(
name
=
'X'
,
shape
=
[
2
,
3
],
dtype
=
'float32'
)
self
.
Out
=
self
.
program
.
global_block
().
create_var
(
name
=
'Out'
,
shape
=
[
2
,
3
],
dtype
=
'float32'
)
self
.
program
.
global_block
().
append_op
(
type
=
'rnn_memory_helper'
,
inputs
=
{
"X"
:
self
.
X
},
outputs
=
{
"Out"
:
self
.
Out
},
attrs
=
{})
def
test_forward
(
self
):
x_np
=
np
.
random
.
normal
(
size
=
(
2
,
3
)).
astype
(
"float32"
)
self
.
feed_map
=
{
'X'
:
create_tensor
(
x_np
,
self
.
place
)}
self
.
fetch_list
=
[
self
.
Out
]
exe
=
Executor
(
self
.
place
)
out
=
exe
.
run
(
self
.
program
,
feed
=
self
.
feed_map
,
fetch_list
=
self
.
fetch_list
)
np
.
isclose
(
np
.
array
(
out
[
0
]),
x_np
,
rtol
=
1e-5
)
class
RNNMemoryHelperGradOpTest
(
unittest
.
TestCase
):
def
setUp
(
self
):
self
.
program
=
Program
()
self
.
place
=
core
.
CPUPlace
()
self
.
input_names
=
[
'X'
,
'Out'
,
'Out@GRAD'
]
self
.
input_vars
=
{
name
:
self
.
program
.
global_block
().
create_var
(
name
=
name
,
shape
=
[
2
,
3
],
dtype
=
'float32'
)
for
name
in
self
.
input_names
}
self
.
output_names
=
[
'X@GRAD'
]
self
.
output_vars
=
{
name
:
self
.
program
.
global_block
().
create_var
(
name
=
name
,
shape
=
[
2
,
3
],
dtype
=
'float32'
)
for
name
in
self
.
output_names
}
self
.
program
.
global_block
().
append_op
(
type
=
'rnn_memory_helper_grad'
,
inputs
=
self
.
input_vars
,
outputs
=
self
.
output_vars
,
attrs
=
{})
def
test_backward
(
self
):
self
.
feed_map
=
{
name
:
create_tensor
(
np
.
random
.
normal
(
size
=
(
2
,
3
)).
astype
(
"float32"
),
self
.
place
)
for
name
in
self
.
input_names
}
self
.
fetch_list
=
[
self
.
output_vars
[
'X@GRAD'
]]
exe
=
Executor
(
self
.
place
)
out
=
exe
.
run
(
self
.
program
,
feed
=
self
.
feed_map
,
fetch_list
=
self
.
fetch_list
)
np
.
isclose
(
np
.
array
(
out
[
0
]),
self
.
feed_map
[
'Out@GRAD'
],
rtol
=
1e-5
)
class
RNNMemoryHelperGradOpWithoutInputTest
(
unittest
.
TestCase
):
def
setUp
(
self
):
self
.
program
=
Program
()
self
.
fake_program
=
Program
()
self
.
place
=
core
.
CPUPlace
()
self
.
input_names
=
[
'X'
,
'Out'
]
self
.
input_vars
=
{
name
:
self
.
program
.
global_block
().
create_var
(
name
=
name
,
shape
=
[
2
,
3
],
dtype
=
'float32'
)
for
name
in
self
.
input_names
}
self
.
input_vars
[
"Out@GRAD"
]
=
\
self
.
fake_program
.
global_block
().
create_var
(
name
=
"Out@GRAD"
,
shape
=
[
2
,
3
],
dtype
=
'float32'
)
self
.
output_names
=
[
'X@GRAD'
]
self
.
output_vars
=
{
name
:
self
.
program
.
global_block
().
create_var
(
name
=
name
,
shape
=
[
2
,
3
],
dtype
=
'float32'
)
for
name
in
self
.
output_names
}
self
.
program
.
global_block
().
append_op
(
type
=
'rnn_memory_helper_grad'
,
inputs
=
self
.
input_vars
,
outputs
=
self
.
output_vars
,
attrs
=
{})
def
test_backward
(
self
):
self
.
feed_map
=
{
name
:
create_tensor
(
np
.
random
.
normal
(
size
=
(
2
,
3
)).
astype
(
"float32"
),
self
.
place
)
for
name
in
[
'X'
,
'Out'
]
}
self
.
fetch_list
=
[
self
.
output_vars
[
'X@GRAD'
]]
exe
=
Executor
(
self
.
place
)
out
=
exe
.
run
(
self
.
program
,
feed
=
self
.
feed_map
,
fetch_list
=
self
.
fetch_list
)
np
.
isclose
(
np
.
array
(
out
[
0
]),
np
.
zeros
(
shape
=
(
2
,
3
)).
astype
(
"float32"
),
rtol
=
1e-5
)
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录