Skip to content

Commit c1738e2

Browse files
committed
Merge branch 'develop' into stride
2 parents dd61304 + b22cd96 commit c1738e2

File tree

21 files changed

+463
-287
lines changed

21 files changed

+463
-287
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ include(external/python) # download, build, install python
6464
include(external/openblas) # download, build, install openblas
6565
include(external/swig) # download, build, install swig
6666
include(external/warpctc) # download, build, install warpctc
67+
include(external/any) # download libn::any
6768

6869
include(package) # set paddle packages
6970
include(cpplint) # set paddle c++ style

cmake/external/any.cmake

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
INCLUDE(ExternalProject)
2+
3+
SET(ANY_SOURCE_DIR ${THIRD_PARTY_PATH}/any)
4+
5+
INCLUDE_DIRECTORIES(${ANY_SOURCE_DIR}/src/linb_any)
6+
7+
ExternalProject_Add(
8+
linb_any
9+
${EXTERNAL_PROJECT_LOG_ARGS}
10+
GIT_REPOSITORY "https://github.com/thelink2012/any.git"
11+
GIT_TAG "8fef1e93710a0edf8d7658999e284a1142c4c020"
12+
PREFIX ${ANY_SOURCE_DIR}
13+
UPDATE_COMMAND ""
14+
CONFIGURE_COMMAND ""
15+
BUILD_COMMAND ""
16+
INSTALL_COMMAND ""
17+
TEST_COMMAND ""
18+
)
19+
20+
add_definitions(-DANY_IMPL_ANY_CAST_MOVEABLE)

demo/seqToseq/api_train_v2.py

Lines changed: 56 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import sys
2+
23
import paddle.v2 as paddle
34

45

5-
def seqToseq_net(source_dict_dim, target_dict_dim):
6+
def seqToseq_net(source_dict_dim, target_dict_dim, is_generating=False):
67
### Network Architecture
78
word_vector_dim = 512 # dimension of word vector
89
decoder_size = 512 # dimension of hidden unit in GRU Decoder network
910
encoder_size = 512 # dimension of hidden unit in GRU Encoder network
1011

12+
beam_size = 3
13+
max_length = 250
14+
1115
#### Encoder
1216
src_word_id = paddle.layer.data(
1317
name='source_language_word',
@@ -67,30 +71,57 @@ def gru_decoder_with_attention(enc_vec, enc_proj, current_word):
6771
group_input2 = paddle.layer.StaticInputV2(input=encoded_proj, is_seq=True)
6872
group_inputs = [group_input1, group_input2]
6973

70-
trg_embedding = paddle.layer.embedding(
71-
input=paddle.layer.data(
72-
name='target_language_word',
73-
type=paddle.data_type.integer_value_sequence(target_dict_dim)),
74-
size=word_vector_dim,
75-
param_attr=paddle.attr.ParamAttr(name='_target_language_embedding'))
76-
group_inputs.append(trg_embedding)
77-
78-
# For decoder equipped with attention mechanism, in training,
79-
# target embeding (the groudtruth) is the data input,
80-
# while encoded source sequence is accessed to as an unbounded memory.
81-
# Here, the StaticInput defines a read-only memory
82-
# for the recurrent_group.
83-
decoder = paddle.layer.recurrent_group(
84-
name=decoder_group_name,
85-
step=gru_decoder_with_attention,
86-
input=group_inputs)
87-
88-
lbl = paddle.layer.data(
89-
name='target_language_next_word',
90-
type=paddle.data_type.integer_value_sequence(target_dict_dim))
91-
cost = paddle.layer.classification_cost(input=decoder, label=lbl)
92-
93-
return cost
74+
if not is_generating:
75+
trg_embedding = paddle.layer.embedding(
76+
input=paddle.layer.data(
77+
name='target_language_word',
78+
type=paddle.data_type.integer_value_sequence(target_dict_dim)),
79+
size=word_vector_dim,
80+
param_attr=paddle.attr.ParamAttr(name='_target_language_embedding'))
81+
group_inputs.append(trg_embedding)
82+
83+
# For decoder equipped with attention mechanism, in training,
84+
# target embeding (the groudtruth) is the data input,
85+
# while encoded source sequence is accessed to as an unbounded memory.
86+
# Here, the StaticInput defines a read-only memory
87+
# for the recurrent_group.
88+
decoder = paddle.layer.recurrent_group(
89+
name=decoder_group_name,
90+
step=gru_decoder_with_attention,
91+
input=group_inputs)
92+
93+
lbl = paddle.layer.data(
94+
name='target_language_next_word',
95+
type=paddle.data_type.integer_value_sequence(target_dict_dim))
96+
cost = paddle.layer.classification_cost(input=decoder, label=lbl)
97+
98+
return cost
99+
else:
100+
# In generation, the decoder predicts a next target word based on
101+
# the encoded source sequence and the last generated target word.
102+
103+
# The encoded source sequence (encoder's output) must be specified by
104+
# StaticInput, which is a read-only memory.
105+
# Embedding of the last generated word is automatically gotten by
106+
# GeneratedInputs, which is initialized by a start mark, such as <s>,
107+
# and must be included in generation.
108+
109+
trg_embedding = paddle.layer.GeneratedInputV2(
110+
size=target_dict_dim,
111+
embedding_name='_target_language_embedding',
112+
embedding_size=word_vector_dim)
113+
group_inputs.append(trg_embedding)
114+
115+
beam_gen = paddle.layer.beam_search(
116+
name=decoder_group_name,
117+
step=gru_decoder_with_attention,
118+
input=group_inputs,
119+
bos_id=0,
120+
eos_id=1,
121+
beam_size=beam_size,
122+
max_length=max_length)
123+
124+
return beam_gen
94125

95126

96127
def main():

paddle/function/Function.cpp

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -16,66 +16,6 @@ limitations under the License. */
1616

1717
namespace paddle {
1818

19-
template <>
20-
size_t FuncConfig::get<size_t>(const std::string& key) const {
21-
auto it = valueMap_.find(key);
22-
CHECK(it != valueMap_.end()) << "Cannot find value: '" << key << "'";
23-
return it->second.s;
24-
}
25-
26-
template <>
27-
real FuncConfig::get<real>(const std::string& key) const {
28-
auto it = valueMap_.find(key);
29-
CHECK(it != valueMap_.end()) << "Cannot find value: '" << key << "'";
30-
return it->second.r;
31-
}
32-
33-
template <>
34-
int FuncConfig::get<int>(const std::string& key) const {
35-
auto it = valueMap_.find(key);
36-
CHECK(it != valueMap_.end()) << "Cannot find value: '" << key << "'";
37-
return it->second.i;
38-
}
39-
40-
template <>
41-
bool FuncConfig::get<bool>(const std::string& key) const {
42-
auto it = valueMap_.find(key);
43-
CHECK(it != valueMap_.end()) << "Cannot find value: '" << key << "'";
44-
return it->second.b;
45-
}
46-
47-
template <>
48-
FuncConfig& FuncConfig::set<size_t>(const std::string& key, size_t v) {
49-
CHECK_EQ(static_cast<int>(valueMap_.count(key)), 0) << "Duplicated value: "
50-
<< key;
51-
valueMap_[key].s = v;
52-
return *this;
53-
}
54-
55-
template <>
56-
FuncConfig& FuncConfig::set<real>(const std::string& key, real v) {
57-
CHECK_EQ(static_cast<int>(valueMap_.count(key)), 0) << "Duplicated value: "
58-
<< key;
59-
valueMap_[key].r = v;
60-
return *this;
61-
}
62-
63-
template <>
64-
FuncConfig& FuncConfig::set<int>(const std::string& key, int v) {
65-
CHECK_EQ(static_cast<int>(valueMap_.count(key)), 0) << "Duplicated value: "
66-
<< key;
67-
valueMap_[key].i = v;
68-
return *this;
69-
}
70-
71-
template <>
72-
FuncConfig& FuncConfig::set<bool>(const std::string& key, bool v) {
73-
CHECK_EQ(static_cast<int>(valueMap_.count(key)), 0) << "Duplicated value: "
74-
<< key;
75-
valueMap_[key].b = v;
76-
return *this;
77-
}
78-
7919
void BufferArgs::addArg(const Matrix& arg,
8020
const TensorShape& shape,
8121
ArgType argType) {

paddle/function/Function.h

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,49 @@ limitations under the License. */
1818
#include <vector>
1919
#include "BufferArg.h"
2020
#include "paddle/math/Matrix.h"
21+
#include "paddle/utils/Any.h"
2122
#include "paddle/utils/ClassRegistrar.h"
23+
#include "paddle/utils/Error.h"
2224

2325
namespace paddle {
2426

2527
/**
2628
* Function Configuration.
2729
* The argument type of Function::init.
28-
* Follow-up will consider moving this data structure to Proto inside.
2930
*/
3031
class FuncConfig {
3132
public:
32-
union value {
33-
size_t s;
34-
real r;
35-
int i;
36-
bool b;
37-
};
38-
3933
template <typename T>
40-
T get(const std::string& key) const;
34+
T get(const std::string& key, Error* err = nullptr) const {
35+
try {
36+
return any_cast<T>(valueMap_.at(key));
37+
} catch (std::exception& e) { // could be cast or out of range exception.
38+
if (err) {
39+
*err = Error(e.what());
40+
} else {
41+
LOG(FATAL) << "Cannot get key " << key << "with error " << e.what();
42+
}
43+
return T();
44+
}
45+
}
4146

4247
template <typename T>
43-
FuncConfig& set(const std::string& key, T v);
48+
FuncConfig& set(const std::string& key, T v, Error* err = nullptr) {
49+
auto it = valueMap_.find(key);
50+
if (it != valueMap_.end()) { // already contains key.
51+
if (err) {
52+
*err = Error("Key %s is already set in FuncConfig", key.c_str());
53+
} else {
54+
LOG(FATAL) << "Key " << key << " is already set in FuncConfig.";
55+
}
56+
return *this;
57+
}
58+
valueMap_[key] = any(v);
59+
return *this;
60+
}
4461

4562
protected:
46-
std::map<std::string, value> valueMap_;
63+
mutable std::unordered_map<std::string, any> valueMap_;
4764
};
4865

4966
/**

paddle/function/PadOp.cpp

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ void Pad<DEVICE_TYPE_CPU>(real* outputs,
2525
const int inH,
2626
const int inW,
2727
const PadConf& pad) {
28-
int cstart = pad.channelStart, cend = pad.channelEnd;
29-
int hstart = pad.heightStart, hend = pad.heightEnd;
30-
int wstart = pad.widthStart, wend = pad.widthEnd;
28+
int cstart = pad.channel[0], cend = pad.channel[1];
29+
int hstart = pad.height[0], hend = pad.height[1];
30+
int wstart = pad.width[0], wend = pad.width[1];
3131
int outC = inC + cstart + cend;
3232
int outH = inH + hstart + hend;
3333
int outW = inW + wstart + wend;
@@ -51,9 +51,9 @@ void PadGrad<DEVICE_TYPE_CPU>(real* inGrad,
5151
const int inH,
5252
const int inW,
5353
const PadConf& pad) {
54-
int cstart = pad.channelStart, cend = pad.channelEnd;
55-
int hstart = pad.heightStart, hend = pad.heightEnd;
56-
int wstart = pad.widthStart, wend = pad.widthEnd;
54+
int cstart = pad.channel[0], cend = pad.channel[1];
55+
int hstart = pad.height[0], hend = pad.height[1];
56+
int wstart = pad.width[0], wend = pad.width[1];
5757
int outC = inC + cstart + cend;
5858
int outH = inH + hstart + hend;
5959
int outW = inW + wstart + wend;
@@ -71,6 +71,12 @@ void PadGrad<DEVICE_TYPE_CPU>(real* inGrad,
7171
}
7272
}
7373

74+
static inline PadConf castToPadConf(const FuncConfig& conf) {
75+
return {conf.get<std::vector<uint32_t>>("channel"),
76+
conf.get<std::vector<uint32_t>>("height"),
77+
conf.get<std::vector<uint32_t>>("width")};
78+
}
79+
7480
/**
7581
* \brief Padding zeros to input according to the specify dimension.
7682
* The struct pad_ contains the padding size in each dimension.
@@ -127,14 +133,7 @@ void PadGrad<DEVICE_TYPE_CPU>(real* inGrad,
127133
template <DeviceType Device>
128134
class PadFunc : public FunctionBase {
129135
public:
130-
void init(const FuncConfig& config) override {
131-
pad_.channelStart = config.get<int>("cstart");
132-
pad_.channelEnd = config.get<int>("cend");
133-
pad_.heightStart = config.get<int>("hstart");
134-
pad_.heightEnd = config.get<int>("hend");
135-
pad_.widthStart = config.get<int>("wstart");
136-
pad_.widthEnd = config.get<int>("wend");
137-
}
136+
void init(const FuncConfig& config) override { pad_ = castToPadConf(config); }
138137

139138
void calc(const BufferArgs& inputs, const BufferArgs& outputs) override {
140139
CHECK_EQ(1UL, inputs.size());
@@ -175,14 +174,7 @@ class PadFunc : public FunctionBase {
175174
template <DeviceType Device>
176175
class PadGradFunc : public FunctionBase {
177176
public:
178-
void init(const FuncConfig& config) override {
179-
pad_.channelStart = config.get<int>("cstart");
180-
pad_.channelEnd = config.get<int>("cend");
181-
pad_.heightStart = config.get<int>("hstart");
182-
pad_.heightEnd = config.get<int>("hend");
183-
pad_.widthStart = config.get<int>("wstart");
184-
pad_.widthEnd = config.get<int>("wend");
185-
}
177+
void init(const FuncConfig& config) override { pad_ = castToPadConf(config); }
186178

187179
void calc(const BufferArgs& inputs, const BufferArgs& outputs) override {
188180
CHECK_EQ(1UL, inputs.size());

paddle/function/PadOp.h

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,12 @@ limitations under the License. */
1919
namespace paddle {
2020

2121
struct PadConf {
22-
/// how many values to add before the data along channel dimension.
23-
int channelStart;
24-
/// how many values to add after the data along channel dimension.
25-
int channelEnd;
26-
/// how many values to add before the data along height dimension.
27-
int heightStart;
28-
/// how many values to add after the data along height dimension.
29-
int heightEnd;
30-
/// how many values to add before the data along width dimension.
31-
int widthStart;
32-
/// how many values to add after the data along width dimension.
33-
int widthEnd;
22+
/// how many values to add before/after the data along channel dimension.
23+
std::vector<uint32_t> channel;
24+
/// how many values to add before/after the data along height dimension.
25+
std::vector<uint32_t> height;
26+
/// how many values to add before/after the data along width dimension.
27+
std::vector<uint32_t> width;
3428
};
3529

3630
/**

paddle/gserver/layers/PadLayer.cpp

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,34 +36,25 @@ bool PadLayer::init(const LayerMap& layerMap,
3636
CHECK_EQ(2, pad_conf.pad_c_size());
3737
CHECK_EQ(2, pad_conf.pad_h_size());
3838
CHECK_EQ(2, pad_conf.pad_w_size());
39-
padc_.push_back(pad_conf.pad_c(0));
40-
padc_.push_back(pad_conf.pad_c(1));
41-
padh_.push_back(pad_conf.pad_h(0));
42-
padh_.push_back(pad_conf.pad_h(1));
43-
padw_.push_back(pad_conf.pad_w(0));
44-
padw_.push_back(pad_conf.pad_w(1));
39+
padc_ = {pad_conf.pad_c(0), pad_conf.pad_c(1)};
40+
padh_ = {pad_conf.pad_h(0), pad_conf.pad_h(1)};
41+
padw_ = {pad_conf.pad_w(0), pad_conf.pad_w(1)};
4542

4643
outDims_ = TensorShape(4);
4744
setOutDims(0);
4845

4946
createFunction(forward_,
5047
"Pad",
5148
FuncConfig()
52-
.set("cstart", padc_[0])
53-
.set("cend", padc_[1])
54-
.set("hstart", padh_[0])
55-
.set("hend", padh_[1])
56-
.set("wstart", padw_[0])
57-
.set("wend", padw_[1]));
49+
.set("channel", padc_)
50+
.set("height", padh_)
51+
.set("width", padw_));
5852
createFunction(backward_,
5953
"PadGrad",
6054
FuncConfig()
61-
.set("cstart", padc_[0])
62-
.set("cend", padc_[1])
63-
.set("hstart", padh_[0])
64-
.set("hend", padh_[1])
65-
.set("wstart", padw_[0])
66-
.set("wend", padw_[1]));
55+
.set("channel", padc_)
56+
.set("height", padh_)
57+
.set("width", padw_));
6758

6859
return true;
6960
}

0 commit comments

Comments
 (0)