Skip to content

Commit 2ac5772

Browse files
committed
Merge branch 'develop' of github.com:baidu/Paddle into feature/fill_constant_force_cpu
2 parents 0ede2a7 + c365c61 commit 2ac5772

Some content is hidden

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

43 files changed

+981
-241
lines changed

benchmark/paddle/image/resnet.py

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
#!/usr/bin/env python
2+
from paddle.trainer_config_helpers import *
3+
4+
height = 224
5+
width = 224
6+
num_class = 1000
7+
batch_size = get_config_arg('batch_size', int, 64)
8+
layer_num = get_config_arg("layer_num", int, 50)
9+
is_test = get_config_arg("is_test", bool, False)
10+
11+
args = {'height': height, 'width': width, 'color': True, 'num_class': num_class}
12+
define_py_data_sources2(
13+
"train.list", None, module="provider", obj="process", args=args)
14+
15+
settings(
16+
batch_size=batch_size,
17+
learning_rate=0.01 / batch_size,
18+
learning_method=MomentumOptimizer(0.9),
19+
regularization=L2Regularization(0.0005 * batch_size))
20+
21+
22+
#######################Network Configuration #############
23+
def conv_bn_layer(name,
24+
input,
25+
filter_size,
26+
num_filters,
27+
stride,
28+
padding,
29+
channels=None,
30+
active_type=ReluActivation()):
31+
"""
32+
A wrapper for conv layer with batch normalization layers.
33+
Note:
34+
conv layer has no activation.
35+
"""
36+
37+
tmp = img_conv_layer(
38+
name=name + "_conv",
39+
input=input,
40+
filter_size=filter_size,
41+
num_channels=channels,
42+
num_filters=num_filters,
43+
stride=stride,
44+
padding=padding,
45+
act=LinearActivation(),
46+
bias_attr=False)
47+
return batch_norm_layer(
48+
name=name + "_bn", input=tmp, act=active_type, use_global_stats=is_test)
49+
50+
51+
def bottleneck_block(name, input, num_filters1, num_filters2):
52+
"""
53+
A wrapper for bottlenect building block in ResNet.
54+
Last conv_bn_layer has no activation.
55+
Addto layer has activation of relu.
56+
"""
57+
last_name = conv_bn_layer(
58+
name=name + '_branch2a',
59+
input=input,
60+
filter_size=1,
61+
num_filters=num_filters1,
62+
stride=1,
63+
padding=0)
64+
last_name = conv_bn_layer(
65+
name=name + '_branch2b',
66+
input=last_name,
67+
filter_size=3,
68+
num_filters=num_filters1,
69+
stride=1,
70+
padding=1)
71+
last_name = conv_bn_layer(
72+
name=name + '_branch2c',
73+
input=last_name,
74+
filter_size=1,
75+
num_filters=num_filters2,
76+
stride=1,
77+
padding=0,
78+
active_type=LinearActivation())
79+
80+
return addto_layer(
81+
name=name + "_addto", input=[input, last_name], act=ReluActivation())
82+
83+
84+
def mid_projection(name, input, num_filters1, num_filters2, stride=2):
85+
"""
86+
A wrapper for middile projection in ResNet.
87+
projection shortcuts are used for increasing dimensions,
88+
and other shortcuts are identity
89+
branch1: projection shortcuts are used for increasing
90+
dimensions, has no activation.
91+
branch2x: bottleneck building block, shortcuts are identity.
92+
"""
93+
# stride = 2
94+
branch1 = conv_bn_layer(
95+
name=name + '_branch1',
96+
input=input,
97+
filter_size=1,
98+
num_filters=num_filters2,
99+
stride=stride,
100+
padding=0,
101+
active_type=LinearActivation())
102+
103+
last_name = conv_bn_layer(
104+
name=name + '_branch2a',
105+
input=input,
106+
filter_size=1,
107+
num_filters=num_filters1,
108+
stride=stride,
109+
padding=0)
110+
last_name = conv_bn_layer(
111+
name=name + '_branch2b',
112+
input=last_name,
113+
filter_size=3,
114+
num_filters=num_filters1,
115+
stride=1,
116+
padding=1)
117+
118+
last_name = conv_bn_layer(
119+
name=name + '_branch2c',
120+
input=last_name,
121+
filter_size=1,
122+
num_filters=num_filters2,
123+
stride=1,
124+
padding=0,
125+
active_type=LinearActivation())
126+
127+
return addto_layer(
128+
name=name + "_addto", input=[branch1, last_name], act=ReluActivation())
129+
130+
131+
img = data_layer(name='image', size=height * width * 3)
132+
133+
134+
def deep_res_net(res2_num=3, res3_num=4, res4_num=6, res5_num=3):
135+
"""
136+
A wrapper for 50,101,152 layers of ResNet.
137+
res2_num: number of blocks stacked in conv2_x
138+
res3_num: number of blocks stacked in conv3_x
139+
res4_num: number of blocks stacked in conv4_x
140+
res5_num: number of blocks stacked in conv5_x
141+
"""
142+
# For ImageNet
143+
# conv1: 112x112
144+
tmp = conv_bn_layer(
145+
"conv1",
146+
input=img,
147+
filter_size=7,
148+
channels=3,
149+
num_filters=64,
150+
stride=2,
151+
padding=3)
152+
tmp = img_pool_layer(name="pool1", input=tmp, pool_size=3, stride=2)
153+
154+
# conv2_x: 56x56
155+
tmp = mid_projection(
156+
name="res2_1", input=tmp, num_filters1=64, num_filters2=256, stride=1)
157+
for i in xrange(2, res2_num + 1, 1):
158+
tmp = bottleneck_block(
159+
name="res2_" + str(i), input=tmp, num_filters1=64, num_filters2=256)
160+
161+
# conv3_x: 28x28
162+
tmp = mid_projection(
163+
name="res3_1", input=tmp, num_filters1=128, num_filters2=512)
164+
for i in xrange(2, res3_num + 1, 1):
165+
tmp = bottleneck_block(
166+
name="res3_" + str(i),
167+
input=tmp,
168+
num_filters1=128,
169+
num_filters2=512)
170+
171+
# conv4_x: 14x14
172+
tmp = mid_projection(
173+
name="res4_1", input=tmp, num_filters1=256, num_filters2=1024)
174+
for i in xrange(2, res4_num + 1, 1):
175+
tmp = bottleneck_block(
176+
name="res4_" + str(i),
177+
input=tmp,
178+
num_filters1=256,
179+
num_filters2=1024)
180+
181+
# conv5_x: 7x7
182+
tmp = mid_projection(
183+
name="res5_1", input=tmp, num_filters1=512, num_filters2=2048)
184+
for i in xrange(2, res5_num + 1, 1):
185+
tmp = bottleneck_block(
186+
name="res5_" + str(i),
187+
input=tmp,
188+
num_filters1=512,
189+
num_filters2=2048)
190+
191+
tmp = img_pool_layer(
192+
name='avgpool',
193+
input=tmp,
194+
pool_size=7,
195+
stride=1,
196+
pool_type=AvgPooling())
197+
198+
return fc_layer(input=tmp, size=num_class, act=SoftmaxActivation())
199+
200+
201+
if layer_num == 50:
202+
resnet = deep_res_net(3, 4, 6, 3)
203+
elif layer_num == 101:
204+
resnet = deep_res_net(3, 4, 23, 3)
205+
elif layer_num == 152:
206+
resnet = deep_res_net(3, 8, 36, 3)
207+
else:
208+
print("Wrong layer number.")
209+
210+
lbl = data_layer(name="label", size=num_class)
211+
loss = cross_entropy(name='loss', input=resnet, label=lbl)
212+
inputs(img, lbl)
213+
outputs(loss)

benchmark/paddle/image/run_mkldnn.sh

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,23 @@ function train() {
55
export OMP_DYNAMIC="FALSE"
66
export KMP_AFFINITY="granularity=fine,compact,0,0"
77
topology=$1
8-
bs=$2
9-
use_mkldnn=$3
10-
if [ $3 == "True" ]; then
8+
layer_num=$2
9+
bs=$3
10+
use_mkldnn=$4
11+
if [ $4 == "True" ]; then
1112
thread=1
12-
log="logs/${topology}-mkldnn-${bs}.log"
13-
elif [ $3 == "False" ]; then
13+
log="logs/${topology}-${layer_num}-mkldnn-${bs}.log"
14+
elif [ $4 == "False" ]; then
1415
thread=`nproc`
1516
# each trainer_count use only 1 core to avoid conflict
1617
export OMP_NUM_THREADS=1
1718
export MKL_NUM_THREADS=1
18-
log="logs/${topology}-${thread}mklml-${bs}.log"
19+
log="logs/${topology}-${layer_num}-${thread}mklml-${bs}.log"
1920
else
2021
echo "Wrong input $3, use True or False."
2122
exit 0
2223
fi
23-
args="batch_size=${bs}"
24+
args="batch_size=${bs},layer_num=${layer_num}"
2425
config="${topology}.py"
2526
paddle train --job=time \
2627
--config=$config \
@@ -40,12 +41,9 @@ if [ ! -d "logs" ]; then
4041
mkdir logs
4142
fi
4243

43-
#========== mkldnn ==========#
44-
train vgg 64 True
45-
train vgg 128 True
46-
train vgg 256 True
47-
48-
#========== mklml ===========#
49-
train vgg 64 False
50-
train vgg 128 False
51-
train vgg 256 False
44+
for use_mkldnn in True False; do
45+
for batchsize in 64 128 256; do
46+
train vgg 19 $batchsize $use_mkldnn
47+
train resnet 50 $batchsize $use_mkldnn
48+
done
49+
done

doc/design/float16.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,6 @@ After float16 class is available, some of the future items are below:
5555

5656
- Update pybind/tensor_py.h to bind c++ float16 with numpy float16.
5757

58-
- Modify `IndicateDataType()` method in `framework/operator.h` to make it compatible with float16.
58+
- Modify `GetKernelType()` method in `framework/operator.h` to make it compatible with float16.
5959

6060
- Create a type-casting operator that can convert the data type in tensor between float16 and other types.

paddle/framework/ddim.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ int64_t DDim::operator[](int idx) const {
124124
return boost::apply_visitor(DynamicConstIndexer(idx), var);
125125
}
126126

127-
int64_t DDim::size() const { return arity(*this); }
127+
int DDim::size() const { return arity(*this); }
128128

129129
bool DDim::operator==(DDim d) const {
130130
if (var.which() != d.getVar().which()) {

paddle/framework/ddim.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ struct DDim {
7171

7272
DDim operator*(DDim d) const;
7373

74-
int64_t size() const;
74+
int size() const;
7575
};
7676

7777
/**

paddle/framework/lod_rank_table.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ void LoDRankTable::Reset(const LoD& lod, size_t level) {
3131
TableItem item;
3232
item.index = i;
3333
item.length = vec[i + 1] - vec[i];
34+
VLOG(10) << "Add item to rank table " << item.index << " " << item.length;
3435
items_.emplace_back(item);
3536
}
3637
// NOTE(yuyang18):

paddle/framework/lod_tensor.cc

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,20 @@
2727
namespace paddle {
2828
namespace framework {
2929

30+
std::ostream& operator<<(std::ostream& os, const LoD& lod) {
31+
os << "{";
32+
for (auto& v : lod) {
33+
os << "{";
34+
for (auto& i : v) {
35+
os << i << ",";
36+
}
37+
os << "}";
38+
}
39+
os << "}";
40+
41+
return os;
42+
}
43+
3044
LoD SliceLevels(const LoD& in, size_t level_begin, size_t level_end) {
3145
LoD new_lod;
3246
new_lod.reserve(level_end - level_begin);
@@ -136,37 +150,35 @@ void LoDTensor::ShrinkInLevel(size_t level, size_t elem_begin,
136150
ShareDataWith(Slice(begin, end));
137151
}
138152

139-
void GetFineGrainedLoDLength(const LoD& lod, size_t start_idx, size_t end_idx,
140-
std::vector<std::vector<size_t>>* lod_length,
141-
size_t* start_offset) {
142-
lod_length->clear();
143-
PADDLE_ENFORCE(start_idx < lod.size() - 1,
144-
"start_idx should be >= 0 and < lod.size() - 1.");
145-
PADDLE_ENFORCE(end_idx < lod.size(),
146-
"end_idx should be >= 0 and < lod.size().");
147-
PADDLE_ENFORCE_LE(start_idx, end_idx,
148-
"start_idx should be less than end_idx.");
149-
for (size_t level_idx = 0; level_idx < lod.size(); ++level_idx) {
153+
using LoDAndOffset = std::pair<LoD, std::pair<size_t, size_t>>;
154+
LoDAndOffset GetSubLoDAndAbsoluteOffset(const LoD& lod, size_t start_idx,
155+
size_t end_idx, size_t start_level) {
156+
LoD sub_lod;
157+
158+
for (size_t level_idx = start_level; level_idx < lod.size(); ++level_idx) {
159+
PADDLE_ENFORCE_LE(start_idx, end_idx);
160+
PADDLE_ENFORCE_LT(end_idx, lod[level_idx].size());
150161
std::vector<size_t> level_lens;
151162
for (size_t i = start_idx; i < end_idx; ++i) {
152163
level_lens.push_back(lod[level_idx][i + 1] - lod[level_idx][i]);
153164
}
154-
lod_length->emplace_back(level_lens);
165+
sub_lod.emplace_back(level_lens);
155166
start_idx = lod[level_idx][start_idx];
156167
end_idx = lod[level_idx][end_idx];
157168
}
158-
*start_offset = start_idx;
169+
170+
return LoDAndOffset{sub_lod, {start_idx, end_idx}};
159171
}
160172

161-
void AppendLoD(LoD* lod, const std::vector<std::vector<size_t>>& lod_length) {
162-
PADDLE_ENFORCE_EQ(
163-
lod->size(), lod_length.size(),
173+
void AppendLoD(LoD* lod, const LoD& lod_length) {
174+
PADDLE_ENFORCE(
175+
lod->empty() || lod->size() == lod_length.size(),
164176
"The lod_length should has the same size with the appended lod.");
177+
if (lod->empty()) {
178+
*lod = LoD(lod_length.size(), std::vector<size_t>({0}));
179+
}
165180
for (size_t i = 0; i < lod->size(); ++i) {
166181
auto& level = (*lod)[i];
167-
if (level.empty()) {
168-
level.push_back(0);
169-
}
170182
for (size_t len : lod_length[i]) {
171183
level.push_back(level.back() + len);
172184
}

paddle/framework/lod_tensor.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ using Vector = thrust::host_vector<
5656
*/
5757
using LoD = std::vector<Vector<size_t>>;
5858

59+
std::ostream& operator<<(std::ostream& os, const LoD& lod);
60+
5961
/*
6062
* Slice levels from a LoD.
6163
* NOTE the lowest level should always be the absolute offsets of the underlying
@@ -181,11 +183,10 @@ LoDTensor LodExpand(const LoDTensor& source, const LoD& lod, size_t level,
181183
return tensor;
182184
}
183185

184-
void GetFineGrainedLoDLength(const LoD& lod, size_t start_idx, size_t end_idx,
185-
std::vector<std::vector<size_t>>* lod_length,
186-
size_t* start_offset);
186+
std::pair<LoD, std::pair<size_t, size_t>> GetSubLoDAndAbsoluteOffset(
187+
const LoD& lod, size_t start_idx, size_t end_idx, size_t start_level);
187188

188-
void AppendLoD(LoD* lod, const std::vector<std::vector<size_t>>& lod_length);
189+
void AppendLoD(LoD* lod, const LoD& lod_length);
189190

190191
} // namespace framework
191192
} // namespace paddle

0 commit comments

Comments
 (0)