Skip to content

Commit b97d61a

Browse files
committed
merge baidu/develop
2 parents 50cf103 + d4dabe3 commit b97d61a

File tree

13 files changed

+180
-60
lines changed

13 files changed

+180
-60
lines changed

benchmark/cluster/vgg16/README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,24 @@
88
- cpu MHz : 2101.000
99
- cache size : 20480 KB
1010

11+
### Blas settings
12+
13+
Setting environment variable: `MKL_NUM_THREADS=1`.
14+
1115
### Single Node Single Thread
1216

13-
- PServer Count: 10
14-
- Trainer Count: 20
1517
- Metrics: samples / sec
1618

1719
| Batch Size | 32 | 64 | 128 | 256 |
1820
| -- | -- | -- | -- | -- |
1921
| PaddlePaddle Fluid | 15.44 | 16.32 | 16.74 | 16.79 |
2022
| PaddlePaddle v2 | 15.97 | 17.04 | 17.60 | 17.83 |
21-
| TensorFlow | - | - | - | - |
23+
| TensorFlow | 9.09 | 9.10 | 9.24 | 8.66 |
2224

2325
### Different Batch Size
2426

2527
- PServer Count: 10
2628
- Trainer Count: 20
27-
- Per trainer CPU Core: 1
2829
- Metrics: samples / sec
2930

3031
| Batch Size | 32 | 64 | 128 | 256 |

paddle/fluid/framework/block_desc.cc

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ limitations under the License. */
1616
#include "paddle/fluid/framework/operator.h"
1717
#include "paddle/fluid/framework/program_desc.h"
1818

19+
#include <queue>
20+
1921
namespace paddle {
2022
namespace framework {
2123

@@ -64,12 +66,36 @@ VarDesc *BlockDesc::RenameVar(const std::string &old_name,
6466
VarDesc *BlockDesc::FindVarRecursive(const std::string &name) const {
6567
if (name == kEmptyVarName) return nullptr;
6668

67-
auto it = vars_.find(name);
68-
if (it == vars_.end()) {
69-
return Parent() == kNoneBlockIndex ? nullptr
70-
: ParentBlock()->FindVarRecursive(name);
69+
std::queue<const BlockDesc *> frontier;
70+
std::unordered_set<const BlockDesc *> visited;
71+
72+
frontier.push(this);
73+
74+
while (!frontier.empty()) { // BFS
75+
auto cur = frontier.front();
76+
frontier.pop();
77+
if (visited.count(cur) != 0) {
78+
continue;
79+
}
80+
auto var = cur->FindVar(name);
81+
if (var != nullptr) {
82+
return var;
83+
}
84+
85+
auto fwd = cur->ForwardBlock();
86+
auto parent = cur->ParentBlock();
87+
88+
if (fwd != nullptr) {
89+
frontier.push(fwd);
90+
}
91+
if (parent != nullptr) {
92+
frontier.push(parent);
93+
}
94+
95+
visited.insert(cur);
7196
}
72-
return it->second.get();
97+
98+
return nullptr;
7399
}
74100

75101
VarDesc &BlockDesc::FindRecursiveOrCreateVar(const std::string &name_bytes) {
@@ -155,10 +181,7 @@ void BlockDesc::Flush() {
155181
}
156182

157183
BlockDesc *BlockDesc::ParentBlock() const {
158-
if (this->desc_->parent_idx() == kNoneBlockIndex) {
159-
return nullptr;
160-
}
161-
return prog_->MutableBlock(static_cast<size_t>(this->desc_->parent_idx()));
184+
return prog_->MutableBlock(static_cast<size_t>(desc_->parent_idx()));
162185
}
163186

164187
proto::BlockDesc *BlockDesc::Proto() {
@@ -205,5 +228,16 @@ void BlockDesc::ClearPBVars() {
205228
}
206229
}
207230

231+
void BlockDesc::SetForwardBlockID(int32_t forward_block_id) {
232+
PADDLE_ENFORCE(!desc_->has_forward_block_idx(),
233+
"Parent block ID has been set to %d. Cannot set to %d",
234+
desc_->forward_block_idx(), forward_block_id);
235+
desc_->set_forward_block_idx(forward_block_id);
236+
}
237+
238+
BlockDesc *BlockDesc::ForwardBlock() const {
239+
return prog_->MutableBlock(static_cast<size_t>(desc_->forward_block_idx()));
240+
}
241+
208242
} // namespace framework
209243
} // namespace paddle

paddle/fluid/framework/block_desc.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ class BlockDesc {
4949

5050
int32_t Parent() const { return desc_->parent_idx(); }
5151

52+
int32_t ForwardBlockID() const { return desc_->forward_block_idx(); }
53+
5254
VarDesc *Var(const std::string &name_bytes);
5355

5456
VarDesc *FindVar(const std::string &name_bytes) const;
@@ -75,6 +77,10 @@ class BlockDesc {
7577

7678
BlockDesc *ParentBlock() const;
7779

80+
BlockDesc *ForwardBlock() const;
81+
82+
void SetForwardBlockID(int32_t forward_block_id);
83+
7884
OpDesc *AppendOp();
7985

8086
void AppendAllocatedOp(std::unique_ptr<OpDesc> &&op_desc);
@@ -93,7 +99,7 @@ class BlockDesc {
9399

94100
proto::BlockDesc *Proto();
95101

96-
ProgramDesc *Program() { return this->prog_; }
102+
ProgramDesc *Program() const { return this->prog_; }
97103

98104
private:
99105
void ClearPBOps();

paddle/fluid/framework/channel.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,7 @@ class ChannelHolder {
100100
virtual ~Placeholder() {}
101101
virtual const std::type_index Type() const = 0;
102102
virtual void* Ptr() const = 0;
103-
virtual void Close() const = 0;
104-
std::type_info type_;
103+
virtual void Close() = 0;
105104
};
106105

107106
template <typename T>
@@ -116,7 +115,7 @@ class ChannelHolder {
116115
if (channel_) channel_->Close();
117116
}
118117

119-
std::unique_ptr<Channel<T>*> channel_;
118+
std::unique_ptr<Channel<T>> channel_;
120119
const std::type_index type_;
121120
};
122121

paddle/fluid/framework/channel_test.cc

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ limitations under the License. */
2020
#include "gtest/gtest.h"
2121

2222
using paddle::framework::Channel;
23+
using paddle::framework::ChannelHolder;
2324
using paddle::framework::MakeChannel;
2425
using paddle::framework::CloseChannel;
2526
using paddle::framework::details::Buffered;
@@ -508,3 +509,36 @@ TEST(Channel, UnbufferedChannelDestroyUnblocksSendersTest) {
508509
auto ch = MakeChannel<int>(0);
509510
ChannelDestroyUnblockSenders(ch);
510511
}
512+
513+
void ChannelHolderSendReceive(ChannelHolder *ch) {
514+
unsigned sum_send = 0;
515+
std::thread t([&]() {
516+
for (int i = 0; i < 5; i++) {
517+
EXPECT_EQ(ch->Send(&i), true);
518+
sum_send += i;
519+
}
520+
});
521+
for (int i = 0; i < 5; i++) {
522+
int recv;
523+
EXPECT_EQ(ch->Receive(&recv), true);
524+
EXPECT_EQ(recv, i);
525+
}
526+
527+
ch->close();
528+
t.join();
529+
EXPECT_EQ(sum_send, 10U);
530+
}
531+
532+
TEST(ChannelHolder, ChannelHolderBufferedSendReceiveTest) {
533+
ChannelHolder *ch = new ChannelHolder();
534+
ch->Reset<int>(10);
535+
ChannelHolderSendReceive(ch);
536+
delete ch;
537+
}
538+
539+
TEST(ChannelHolder, ChannelHolderUnBufferedSendReceiveTest) {
540+
ChannelHolder *ch = new ChannelHolder();
541+
ch->Reset<int>(0);
542+
ChannelHolderSendReceive(ch);
543+
delete ch;
544+
}

paddle/fluid/framework/framework.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ message BlockDesc {
158158
required int32 parent_idx = 2;
159159
repeated VarDesc vars = 3;
160160
repeated OpDesc ops = 4;
161+
optional int32 forward_block_idx = 5 [ default = -1 ];
161162
}
162163

163164
// Please refer to

paddle/fluid/framework/program_desc.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,13 @@ class ProgramDesc {
3838

3939
BlockDesc *AppendBlock(const BlockDesc &parent);
4040

41-
BlockDesc *MutableBlock(size_t idx) { return blocks_[idx].get(); }
41+
BlockDesc *MutableBlock(size_t idx) {
42+
if (idx == static_cast<size_t>(kNoneBlockIndex)) {
43+
return nullptr;
44+
} else {
45+
return blocks_[idx].get();
46+
}
47+
}
4248

4349
const BlockDesc &Block(size_t idx) const { return *blocks_[idx]; }
4450

paddle/fluid/operators/while_op.cc

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,8 @@ class WhileGradOpDescMaker : public framework::SingleGradOpDescMaker {
231231
while_grad->SetInput(kStepScopes, Output(kStepScopes));
232232

233233
auto *grad_block = this->grad_block_[0];
234-
auto *fwd_block = grad_block->ParentBlock();
234+
auto *fwd_block = grad_block->ForwardBlock();
235+
auto *parent_block = grad_block->ParentBlock();
235236

236237
// Not all of IGs will be generated by inner gradient operators of while op.
237238
// Ignore IGs that is not generated by the inside block.
@@ -260,33 +261,37 @@ class WhileGradOpDescMaker : public framework::SingleGradOpDescMaker {
260261
for (auto &o : Output(kOutputs)) {
261262
block_ins.insert(o);
262263
}
263-
std::unordered_set<std::string> extra_inputs;
264+
std::unordered_set<std::string> output_grads;
264265
for (const auto *op : grad_block->AllOps()) {
265266
for (auto &input_name : op->InputArgumentNames()) {
266267
// If the input of Op has been recorded or is generated by the forward
267268
// block, do not make it as input again.
269+
270+
// The input is located in I/O or other op's outputs or the variable is
271+
// located in grad_block's parents
268272
if (block_ins.find(input_name) != block_ins.end() ||
269-
fwd_block->FindVar(input_name) != nullptr) {
273+
(fwd_block->FindVarRecursive(input_name) != nullptr ||
274+
parent_block->FindVarRecursive(input_name) != nullptr)) {
270275
continue;
271276
}
272-
extra_inputs.insert(input_name);
277+
output_grads.insert(input_name);
273278
}
274279
for (auto &output_name : op->OutputArgumentNames()) {
275280
block_ins.insert(output_name);
276281
}
277282
}
278283

279-
std::vector<std::string> extra_inputs_list;
280-
extra_inputs_list.resize(extra_inputs.size());
281-
std::copy(extra_inputs.begin(), extra_inputs.end(),
282-
extra_inputs_list.begin());
283-
while_grad->SetInput(framework::GradVarName(kOutputs), extra_inputs_list);
284+
std::vector<std::string> output_grads_list;
285+
output_grads_list.resize(output_grads.size());
286+
std::copy(output_grads.begin(), output_grads.end(),
287+
output_grads_list.begin());
288+
while_grad->SetInput(framework::GradVarName(kOutputs), output_grads_list);
284289

285290
while_grad->SetAttrMap(this->Attrs());
286291
while_grad->SetBlockAttr(kStepBlock, *grad_block);
287292
// record the original output gradient names, since the gradient name of
288293
// while operator could be renamed.
289-
while_grad->SetAttr("original_output_grad", extra_inputs_list);
294+
while_grad->SetAttr("original_output_grad", output_grads_list);
290295

291296
return std::unique_ptr<framework::OpDesc>(while_grad);
292297
}

paddle/fluid/pybind/protobuf.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ void BindBlockDesc(py::module &m) {
155155
py::class_<BlockDesc>(m, "BlockDesc", "")
156156
.def_property_readonly("id", &BlockDesc::ID)
157157
.def_property_readonly("parent", &BlockDesc::Parent)
158+
.def("get_forward_block_idx", &BlockDesc::ForwardBlockID)
159+
.def("set_forward_block_idx", &BlockDesc::SetForwardBlockID)
158160
.def("append_op", &BlockDesc::AppendOp,
159161
py::return_value_policy::reference)
160162
.def("prepend_op", &BlockDesc::PrependOp,

python/paddle/v2/fluid/backward.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,8 @@ def _append_backward_ops_(block,
298298
# If the op has its own sub-block, deal with the sub-block first
299299
if op.has_attr("sub_block"):
300300
sub_block = program.block(op.block_attr("sub_block"))
301-
grad_sub_block = program.create_block(parent_idx=sub_block.idx)
301+
grad_sub_block = program.create_block()
302+
grad_sub_block.set_forward_block_idx(sub_block.idx)
302303
cb = _callback_lookup_(op)
303304
if cb is not None:
304305
if callbacks is None:
@@ -310,6 +311,8 @@ def _append_backward_ops_(block,
310311
else:
311312
_append_backward_ops_(sub_block, sub_block.ops, grad_sub_block,
312313
no_grad_dict, grad_to_var, callbacks)
314+
315+
program.rollback()
313316
grad_sub_block_list.append(grad_sub_block.desc)
314317

315318
# Getting op's corresponding grad_op

0 commit comments

Comments
 (0)