Skip to content

Commit cf2608e

Browse files
committed
update to the develop branch.
2 parents 64fe9bc + 154e1d0 commit cf2608e

File tree

131 files changed

+3175
-1522
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

131 files changed

+3175
-1522
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ COPY ./paddle/scripts/docker/root/ /root/
2222

2323
RUN apt-get update && \
2424
apt-get install -y \
25-
git python-pip python-dev openssh-server bison \
25+
git python-pip python-dev openssh-server bison libnccl-dev \
2626
wget unzip unrar tar xz-utils bzip2 gzip coreutils ntp \
2727
curl sed grep graphviz libjpeg-dev zlib1g-dev \
2828
python-matplotlib gcc-4.8 g++-4.8 \

doc/design/block.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ OpDesc {
189189
inputs = {0} // the index of x in vars of BlockDesc above
190190
outputs = {5, 3} // indices of act and hidden_out in vars of BlockDesc above
191191
attrs {
192-
"memories" : {1} // the index of h
192+
"states" : {1} // the index of h
193193
"step_net" : <above step net>
194194
}
195195
};

doc/design/register_grad_op.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33

44
## The Problem Posed
55

6-
Currently, for each C++ operator class definition, there registers a *gradient operator creator* function, which takes a C++ operator instance and returns the corresponding gradient operator instance.
6+
Currently, for each C++ operator class definition, a *gradient operator creator* function is registered, which takes as input a C++ operator instance and returns the corresponding gradient operator instance.
77

8-
However, we noticed two problems with the current deisgn:
8+
However, we noticed two problems with the current design:
99

10-
1. As we decided to separate the *compilation* and *execution* phases, we need to change the creator to take an `OpDesc` protobuf message in a `ProgramDesc` and inserts corresponding `OpDesc` messages into the `ProgramDesc` message.
10+
1. As we decided to separate the *compilation* and the *execution* phases, we need to change the creator to take an `OpDesc` protobuf message in a `ProgramDesc` and inserts corresponding `OpDesc` messages into the `ProgramDesc` message.
1111

12-
1. Some operator's gradient computation requires more than one gradient operators. For example, the gradient of *minus* consists of two operators -- an identity operaotr and a scale operator. So we need to make the registration mechanism to support the mapping from an operator to a set of operators for gradient computation.
12+
1. For some operators, the gradient computation can be written in terms of existing operators. For example, the gradient of *minus* operator consists of two operators -- an *identity* operator followed by a *scale* operator. Hence the registration mechanism needs to support mapping from an operator to a set of operators for the gradient computation.
1313

1414
## The Current Implementation
1515

16-
The C++ class `OpInfos` store in a association map which key is the operator type. The `grad_op_type` indicate associated gradient operator type. Operator can create gradient operator by `OpInfo::creator_` of gradient. The pseudo code is
16+
Instances of the C++ class `OpInfo` are stored an associative map whose key is the operator type. The `grad_op_type` indicates the associated gradient operator type. An operator can create the gradient operator by invoking `OpInfo::creator_` of the gradient operator. The pseudo code is as follows
1717

1818
```cpp
1919
struct OpInfo {
@@ -31,16 +31,16 @@ OperatorBase* CreateGradientOperator(const OperatorBase& op) {
3131
3232
## Proposed Solution
3333
34-
The mapping relationship between an operator and its gradient operators is a function. The interface of that function is:
34+
The mapping relationship between an operator and its gradient operators is a function. The interface of this function is:
3535
3636
```cpp
3737
// (OpDesc) --> vector<OpDesc>
3838
std::function<std::vector<OpDescBind>(const OpDescBind&)>;
3939
```
4040

41-
The function takes an `OpDescBind` of the forward operator and returns one or many gradient operator descriptions. `OpDescBind` is a C++ wrapper for protobuf message `OpDesc` to manipulate `OpDesc` fast.
41+
The function takes an `OpDescBind` of the forward operator and returns one or many gradient operator descriptions. `OpDescBind` is a C++ wrapper for the protobuf message `OpDesc` for rapid manipulation of `OpDesc`.
4242

43-
The `GradOpDescMaker` will be registered in `OpInfo`, to replace `grad_op_type_` field. The `OpInfo` should be
43+
The `GradOpDescMaker` will be registered in `OpInfo` and will replace the `grad_op_type_` field. The `OpInfo` should look like
4444

4545
```cpp
4646
struct OpInfo {
@@ -49,7 +49,7 @@ struct OpInfo {
4949
};
5050
```
5151
52-
The `grad_op_maker_ ` is `nullptr` if the operator does not have associated gradient operators.
52+
The `grad_op_maker_ ` is a `nullptr` if the operator does not have any associated gradient operators.
5353
5454
We propose a base class called `GradOpDescMakerBase` to let operator developers generate `Gradient Operators` easily. The public interface of that class is
5555
@@ -74,7 +74,7 @@ func = [] (const OpDescBind& fwd_op) {
7474

7575
We can write many helper functions since the `GradOpDescMakerBase` is a class now. The basic helper functions get the variables of `Input`, `Output`, `InputGradient` and `OutputGradient` in the forwarding operator.
7676

77-
We should chagne register macros at the same time. In the current solution, there is no difference between forwarding operators and backward operators. So `REGISTER_OP` just register one operator. If the `REGISTER_OPERATOR ` contains `OpProtoAndCheckerMaker` and `GradOpDescMaker`, we just list them in the same macro. It can be done by a macro contains `__VA_ARGS__`.
77+
We should change register macros at the same time. In the current solution, there is no difference between forwarding operators and backward operators. So `REGISTER_OP` just register one operator. If the `REGISTER_OPERATOR ` contains `OpProtoAndCheckerMaker` and `GradOpDescMaker`, we just list them in the same macro. It can be done by a macro contains `__VA_ARGS__`.
7878

7979
The user interface should be
8080

doc/faq/local/index_cn.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ decoder_inputs = paddle.layer.fc(
174174
1. 两者都是对梯度的截断,但截断时机不同,前者在 :code:`optimzier` 更新网络参数时应用;后者在激活函数反向计算时被调用;
175175
2. 截断对象不同:前者截断可学习参数的梯度,后者截断回传给前层的梯度;
176176

177-
除此之外,还可以通过减小学习律或者对数据进行归一化处理来解决这类问题
177+
除此之外,还可以通过减小学习率或者对数据进行归一化处理来解决这类问题
178178

179179
5. 如何调用 infer 接口输出多个layer的预测结果
180180
-----------------------------------------------

paddle/capi/CMakeLists.txt

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,37 @@ add_style_check_target(paddle_capi ${CAPI_SOURCES} ${CAPI_HEADER}
2828

2929
add_dependencies(paddle_capi paddle_proto)
3030

31-
# combine all paddle static libraries together, into libpaddle_capi_whole.a
32-
# user should use PaddleCAPI as -lpaddle_capi_whole
33-
set(PADDLE_CAPI_INFER_LIBS
34-
paddle_utils
35-
paddle_parameter
36-
paddle_math
37-
paddle_cuda
38-
paddle_function
39-
paddle_gserver
40-
paddle_proto)
41-
31+
# TODO: paddle_capi_whole will be removed.
32+
if(MOBILE_INFERENCE)
33+
set(PADDLE_CAPI_INFER_LIBS
34+
paddle_utils
35+
paddle_parameter
36+
paddle_math
37+
paddle_cuda
38+
paddle_function
39+
paddle_gserver
40+
paddle_proto)
41+
else()
42+
set(PADDLE_CAPI_INFER_LIBS
43+
paddle_utils
44+
paddle_parameter
45+
paddle_math
46+
paddle_cuda
47+
paddle_function
48+
paddle_gserver
49+
paddle_proto
50+
paddle_pserver
51+
paddle_network)
52+
endif()
4253
cc_library(paddle_capi_whole DEPS paddle_capi ${PADDLE_CAPI_INFER_LIBS})
4354

44-
# No shared library for iOS
55+
# Link the static library for inference
56+
cc_library(paddle_capi_engine DEPS paddle_capi paddle_utils paddle_parameter paddle_math paddle_cuda paddle_proto)
57+
cc_library(paddle_capi_layers DEPS paddle_function paddle_gserver)
58+
59+
# Link the shared library for inference
4560
if(NOT IOS)
46-
set(LINK_FLAGS " -Wl,--retain-symbols-file ${CMAKE_CURRENT_SOURCE_DIR}/export.sym -Wl,--version-script ${CMAKE_CURRENT_SOURCE_DIR}/export.map")
47-
# TODO: merge mkl into paddle_capi_shared
61+
set(LINK_FLAGS "-Wl,--version-script ${CMAKE_CURRENT_SOURCE_DIR}/paddle_capi.map")
4862
add_library(paddle_capi_shared SHARED ${CAPI_SOURCES})
4963
set_target_properties(paddle_capi_shared PROPERTIES LINK_FLAGS "${LINK_FLAGS}")
5064
target_include_directories(paddle_capi_shared PUBLIC ${CMAKE_CURRENT_BINARY_DIR})
@@ -53,9 +67,10 @@ endif()
5367

5468
# install library & headers.
5569
install(FILES ${CAPI_HEADERS} DESTINATION include/paddle)
70+
install(FILES paddle_capi.map DESTINATION include/paddle)
5671
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/config.h DESTINATION include/paddle)
5772
if(ANDROID)
58-
install(TARGETS paddle_capi_whole paddle_capi_shared
73+
install(TARGETS paddle_capi_whole paddle_capi_engine paddle_capi_layers paddle_capi_shared
5974
ARCHIVE DESTINATION lib/${ANDROID_ABI}
6075
LIBRARY DESTINATION lib/${ANDROID_ABI})
6176
execute_process(
@@ -80,7 +95,7 @@ if(ANDROID)
8095
)"
8196
)
8297
else(ANDROID)
83-
install(TARGETS paddle_capi_whole ARCHIVE DESTINATION lib)
98+
install(TARGETS paddle_capi_whole paddle_capi_engine paddle_capi_layers ARCHIVE DESTINATION lib)
8499
if(NOT IOS)
85100
install(TARGETS paddle_capi_shared DESTINATION lib)
86101
endif()

paddle/capi/export.sym

Whitespace-only changes.
File renamed without changes.

paddle/framework/CMakeLists.txt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,15 @@ cc_test(scope_test SRCS scope_test.cc DEPS scope)
1919
proto_library(framework_proto SRCS framework.proto)
2020

2121
cc_library(attribute SRCS attribute.cc DEPS framework_proto)
22-
cc_library(proto_desc SRCS var_desc.cc op_desc.cc block_desc.cc program_desc.cc DEPS attribute ddim op_info)
23-
cc_test(program_desc_test SRCS program_desc_test.cc DEPS proto_desc
24-
device_context)
22+
cc_test(program_desc_test SRCS program_desc_test.cc DEPS proto_desc)
2523
cc_library(op_proto_maker SRCS op_proto_maker.cc DEPS framework_proto attribute)
2624
cc_test(op_proto_maker_test SRCS op_proto_maker_test.cc DEPS op_proto_maker)
2725
cc_library(op_info SRCS op_info.cc DEPS attribute framework_proto)
28-
cc_library(operator SRCS operator.cc DEPS op_info device_context tensor scope proto_desc glog)
26+
cc_library(operator SRCS operator.cc DEPS op_info device_context tensor scope glog)
2927
cc_test(operator_test SRCS operator_test.cc DEPS operator op_registry)
28+
cc_library(proto_desc SRCS var_desc.cc op_desc.cc block_desc.cc program_desc.cc DEPS attribute ddim op_info operator)
3029

31-
cc_library(op_registry SRCS op_registry.cc DEPS op_proto_maker op_info operator glog)
30+
cc_library(op_registry SRCS op_registry.cc DEPS op_proto_maker op_info operator glog proto_desc)
3231
cc_test(op_registry_test SRCS op_registry_test.cc DEPS op_registry)
3332

3433
py_proto_compile(framework_py_proto SRCS framework.proto)
@@ -44,7 +43,7 @@ add_custom_command(TARGET framework_py_proto POST_BUILD
4443
cc_library(backward SRCS backward.cc DEPS net_op)
4544
cc_test(backward_test SRCS backward_test.cc DEPS backward recurrent_op device_context)
4645

47-
cc_library(executor SRCS executor.cc DEPS op_registry device_context scope framework_proto backward)
46+
cc_library(executor SRCS executor.cc DEPS op_registry device_context scope framework_proto backward glog)
4847

4948
cc_library(prune SRCS prune.cc DEPS framework_proto)
5049
cc_test(prune_test SRCS prune_test.cc DEPS op_info prune recurrent_op device_context)

paddle/framework/backward.cc

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include "paddle/framework/block_desc.h"
2323
#include "paddle/framework/op_registry.h"
24+
#include "paddle/operators/dynamic_recurrent_op.h"
2425
#include "paddle/operators/net_op.h"
2526
#include "paddle/operators/recurrent_op.h"
2627

@@ -220,8 +221,7 @@ static std::unique_ptr<OperatorBase> BackwardRecursive(
220221
// process recurrent gradient op as a special operator.
221222
if (forwardOp.Type() == "recurrent") {
222223
// NOTE clean up cycle call somewhere (RNN's stepnet constains itself),
223-
// or
224-
// this will result in infinite loop.
224+
// or this will result in infinite loop.
225225
const auto& rnnop =
226226
*static_cast<const operators::RecurrentOp*>(&forwardOp);
227227
auto rnn_grad_op =
@@ -231,6 +231,18 @@ static std::unique_ptr<OperatorBase> BackwardRecursive(
231231
// create stepnet's gradient op
232232
rnn_grad_op->set_stepnet(
233233
BackwardRecursive(stepnet_op, no_grad_names, grad_to_var, uniq_id));
234+
} else if (forwardOp.Type() == "dynamic_recurrent") {
235+
// NOTE clean up cycle call somewhere (RNN's stepnet constains itself),
236+
// or this will result in infinite loop.
237+
const auto& rnnop =
238+
*static_cast<const operators::DynamicRecurrentOp*>(&forwardOp);
239+
auto rnn_grad_op =
240+
static_cast<operators::DynamicRecurrentGradientOp*>(grad_op.get());
241+
const auto& stepnet_op =
242+
*static_cast<const OperatorBase*>(&rnnop.rnn.GetStepUnit());
243+
// create stepnet's gradient op
244+
rnn_grad_op->rnn.SetStepUnit(
245+
BackwardRecursive(stepnet_op, no_grad_names, grad_to_var, uniq_id));
234246
}
235247

236248
if (net->ops_.empty()) { // Current no aux op is added to network

paddle/framework/block_desc.cc

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,19 @@ bool BlockDescBind::HasVar(const std::string &name) const {
4141
return vars_.find(name) != vars_.end();
4242
}
4343

44+
VarDescBind *BlockDescBind::FindVarRecursive(const std::string &name) const {
45+
auto it = vars_.find(name);
46+
if (it == vars_.end()) {
47+
return Parent() == kNoneBlockIndex ? nullptr
48+
: ParentBlock()->FindVarRecursive(name);
49+
}
50+
return it->second.get();
51+
}
52+
53+
bool BlockDescBind::HasVarRecursive(const std::string &name) const {
54+
return FindVarRecursive(name) != nullptr;
55+
}
56+
4457
std::vector<VarDescBind *> BlockDescBind::AllVars() const {
4558
std::vector<VarDescBind *> res;
4659
for (const auto &p : vars_) {
@@ -97,7 +110,7 @@ void BlockDescBind::Flush() {
97110
}
98111

99112
BlockDescBind *BlockDescBind::ParentBlock() const {
100-
if (this->desc_->parent_idx() == -1) {
113+
if (this->desc_->parent_idx() == kNoneBlockIndex) {
101114
return nullptr;
102115
}
103116
return prog_->Block(static_cast<size_t>(this->desc_->parent_idx()));

0 commit comments

Comments
 (0)