Skip to content

Commit c58ce0a

Browse files
committed
Merge remote-tracking branch 'baidu/develop' into update_getstarted_doc
2 parents 26922a3 + 84aea8a commit c58ce0a

Some content is hidden

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

60 files changed

+2969
-1215
lines changed

benchmark/cluster/vgg16/vgg16_fluid.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
2-
#
2+
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
55
# You may obtain a copy of the License at
6-
#
6+
#
77
# http://www.apache.org/licenses/LICENSE-2.0
8-
#
8+
#
99
# Unless required by applicable law or agreed to in writing, software
1010
# distributed under the License is distributed on an "AS IS" BASIS,
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -138,13 +138,14 @@ def main():
138138
avg_cost = fluid.layers.mean(x=cost)
139139

140140
# Evaluator
141-
accuracy = fluid.evaluator.Accuracy(input=predict, label=label)
141+
batch_size = fluid.layers.create_tensor(dtype='int64')
142+
batch_acc = fluid.layers.accuracy(
143+
input=predict, label=label, total=batch_size)
142144

143145
# inference program
144146
inference_program = fluid.default_main_program().clone()
145147
with fluid.program_guard(inference_program):
146-
test_target = accuracy.metrics + accuracy.states
147-
inference_program = fluid.io.get_inference_program(test_target)
148+
inference_program = fluid.io.get_inference_program(batch_acc)
148149

149150
# Optimization
150151
optimizer = fluid.optimizer.Adam(learning_rate=args.learning_rate)
@@ -157,27 +158,30 @@ def main():
157158

158159
# test
159160
def test(exe):
160-
accuracy.reset(exe)
161+
test_pass_acc = fluid.average.WeightedAverage()
161162
for batch_id, data in enumerate(test_reader()):
162163
img_data = np.array(map(lambda x: x[0].reshape(data_shape),
163164
data)).astype("float32")
164165
y_data = np.array(map(lambda x: x[1], data)).astype("int64")
165166
y_data = y_data.reshape([-1, 1])
166167

167-
exe.run(inference_program,
168-
feed={"pixel": img_data,
169-
"label": y_data})
168+
outs = exe.run(inference_program,
169+
feed={"pixel": img_data,
170+
"label": y_data},
171+
fetch_list=[batch_acc, batch_size])
172+
test_pass_acc.add(value=np.array(outs[0]), weight=np.array(outs[1]))
170173

171-
return accuracy.eval(exe)
174+
return test_pass_acc.eval()
172175

173176
def train_loop(exe, trainer_prog):
174177
iters = 0
175178
ts = time.time()
179+
train_pass_acc = fluid.average.WeightedAverage()
176180
for pass_id in range(args.num_passes):
177181
# train
178182
start_time = time.time()
179183
num_samples = 0
180-
accuracy.reset(exe)
184+
train_pass_acc.reset()
181185
with profiler.profiler("CPU", 'total') as prof:
182186
for batch_id, data in enumerate(train_reader()):
183187
ts = time.time()
@@ -187,21 +191,22 @@ def train_loop(exe, trainer_prog):
187191
y_data = np.array(map(lambda x: x[1], data)).astype("int64")
188192
y_data = y_data.reshape([-1, 1])
189193

190-
loss, acc = exe.run(
194+
loss, acc, b_size = exe.run(
191195
trainer_prog,
192196
feed={"pixel": img_data,
193197
"label": y_data},
194-
fetch_list=[avg_cost] + accuracy.metrics)
198+
fetch_list=[avg_cost, batch_acc, batch_size])
195199
iters += 1
196200
num_samples += len(data)
201+
train_pass_acc.add(value=acc, weight=b_size)
197202
print(
198203
"Pass = %d, Iters = %d, Loss = %f, Accuracy = %f, Speed = %.2f img/s"
199204
% (pass_id, iters, loss, acc,
200205
len(data) / (time.time() - ts))
201206
) # The accuracy is the accumulation of batches, but not the current batch.
202207

203208
pass_elapsed = time.time() - start_time
204-
pass_train_acc = accuracy.eval(exe)
209+
pass_train_acc = train_pass_acc.eval()
205210
pass_test_acc = test(exe)
206211
print(
207212
"Pass = %d, Training performance = %f imgs/s, Train accuracy = %f, Test accuracy = %f\n"

cmake/external/snappy.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ ExternalProject_Add(
3939
-DCMAKE_INSTALL_LIBDIR=${SNAPPY_INSTALL_DIR}/lib
4040
-DCMAKE_POSITION_INDEPENDENT_CODE=ON
4141
-DBUILD_TESTING=OFF
42+
-DSNAPPY_BUILD_TESTS:BOOL=OFF
4243
-DCMAKE_BUILD_TYPE=${THIRD_PARTY_BUILD_TYPE}
4344
${EXTERNAL_OPTIONAL_ARGS}
4445
CMAKE_CACHE_ARGS -DCMAKE_INSTALL_PREFIX:PATH=${SNAPPY_INSTALL_DIR}

paddle/fluid/framework/CMakeLists.txt

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ cc_library(ddim SRCS ddim.cc DEPS eigen3 boost)
55
cc_test(ddim_test SRCS ddim_test.cc DEPS ddim)
66
nv_test(dim_test SRCS dim_test.cu DEPS ddim)
77

8-
if (WITH_GPU)
8+
if(WITH_GPU)
99
nv_library(tensor SRCS tensor.cc tensor_util.cu DEPS ddim place paddle_memory device_context framework_proto)
1010
else()
1111
cc_library(tensor SRCS tensor.cc tensor_util.cc DEPS ddim place paddle_memory device_context framework_proto)
12-
endif ()
12+
endif()
1313

1414
cc_test(tensor_test SRCS tensor_test.cc DEPS tensor)
15-
if (WITH_GPU)
15+
if(WITH_GPU)
1616
nv_test(tensor_util_test SRCS tensor_util_test.cc tensor_util_test.cu DEPS tensor)
1717
else()
1818
cc_test(tensor_util_test SRCS tensor_util_test.cc DEPS tensor)
@@ -39,8 +39,13 @@ cc_library(data_device_transform SRCS data_device_transform.cc DEPS tensor)
3939
nv_test(data_device_transform_test SRCS data_device_transform_test.cu
4040
DEPS operator op_registry init math_function)
4141

42-
cc_library(data_type_transform SRCS data_type_transform.cc DEPS tensor)
43-
cc_test(data_type_transform_test SRCS data_type_transform_test.cc DEPS data_type_transform)
42+
if(WITH_GPU)
43+
nv_library(data_type_transform SRCS data_type_transform.cu DEPS tensor)
44+
nv_test(data_type_transform_test SRCS data_type_transform_test.cc data_type_transform_test.cu DEPS data_type_transform)
45+
else()
46+
cc_library(data_type_transform SRCS data_type_transform.cc DEPS tensor)
47+
cc_test(data_type_transform_test SRCS data_type_transform_test.cc DEPS data_type_transform)
48+
endif()
4449

4550
cc_library(data_layout_transform SRCS data_layout_transform.cc DEPS tensor math_function)
4651
cc_test(data_layout_transform_test SRCS data_layout_transform_test.cc DEPS data_layout_transform)

paddle/fluid/framework/channel.h

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,19 @@ class Channel {
2828
virtual bool Send(T*) = 0;
2929
virtual bool Receive(T*) = 0;
3030
virtual size_t Cap() = 0;
31+
virtual void Lock() = 0;
32+
virtual void Unlock() = 0;
3133
virtual void Close() = 0;
3234
virtual ~Channel() {}
3335
};
3436

3537
// Forward declaration of channel implementations.
36-
namespace details {
3738
template <typename T>
38-
class Buffered;
39-
template <typename T>
40-
class UnBuffered;
41-
} // namespace details
39+
class ChannelImpl;
4240

4341
template <typename T>
4442
Channel<T>* MakeChannel(size_t buffer_size) {
45-
if (buffer_size > 0) {
46-
return new details::Buffered<T>(buffer_size);
47-
}
48-
return new details::UnBuffered<T>();
43+
return new ChannelImpl<T>(buffer_size);
4944
}
5045

5146
template <typename T>
@@ -89,6 +84,19 @@ class ChannelHolder {
8984
if (IsInitialized()) holder_->Close();
9085
}
9186

87+
size_t Cap() {
88+
if (IsInitialized()) return holder_->Cap();
89+
return -1;
90+
}
91+
92+
void Lock() {
93+
if (IsInitialized()) holder_->Lock();
94+
}
95+
96+
void Unlock() {
97+
if (IsInitialized()) holder_->Unlock();
98+
}
99+
92100
inline bool IsInitialized() const { return holder_ != nullptr; }
93101

94102
inline const std::type_index Type() {
@@ -106,6 +114,9 @@ class ChannelHolder {
106114
virtual const std::type_index Type() const = 0;
107115
virtual void* Ptr() const = 0;
108116
virtual void Close() = 0;
117+
virtual void Lock() = 0;
118+
virtual void Unlock() = 0;
119+
virtual size_t Cap() = 0;
109120
};
110121

111122
template <typename T>
@@ -115,11 +126,28 @@ class ChannelHolder {
115126
}
116127

117128
virtual const std::type_index Type() const { return type_; }
129+
118130
virtual void* Ptr() const { return static_cast<void*>(channel_.get()); }
131+
119132
virtual void Close() {
120133
if (channel_) channel_->Close();
121134
}
122135

136+
virtual size_t Cap() {
137+
if (channel_)
138+
return channel_->Cap();
139+
else
140+
return -1;
141+
}
142+
143+
virtual void Lock() {
144+
if (channel_) channel_->Lock();
145+
}
146+
147+
virtual void Unlock() {
148+
if (channel_) channel_->Unlock();
149+
}
150+
123151
std::unique_ptr<Channel<T>> channel_;
124152
const std::type_index type_;
125153
};
@@ -131,5 +159,4 @@ class ChannelHolder {
131159
} // namespace framework
132160
} // namespace paddle
133161

134-
#include "paddle/fluid/framework/details/buffered_channel.h"
135-
#include "paddle/fluid/framework/details/unbuffered_channel.h"
162+
#include "paddle/fluid/framework/channel_impl.h"

0 commit comments

Comments
 (0)