Skip to content

Commit 3c5bbf4

Browse files
committed
make unit test to work
1 parent e39adc8 commit 3c5bbf4

File tree

5 files changed

+53
-81
lines changed

5 files changed

+53
-81
lines changed

paddle/fluid/framework/details/nccl_all_reduce_op_handle.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
#include "paddle/fluid/framework/details/nccl_all_reduce_op_handle.h"
1616
#include <algorithm>
17-
#include "paddle/fluid/framework/details/reduce_util.h"
17+
#include "paddle/fluid/framework/details/reduce_and_gather.h"
1818

1919
namespace paddle {
2020
namespace framework {

paddle/fluid/framework/details/reduce_op_handle.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ void ReduceOpHandle::RunImpl() {
121121
auto &p = in_places[i];
122122
auto &lod_tensor = lod_tensors[i];
123123
int dev_id = boost::get<platform::CUDAPlace>(p).device;
124-
auto &nccl_ctx = nccl_ctxs_.at(dev_id);
124+
auto &nccl_ctx = nccl_ctxs_->at(dev_id);
125125
auto stream = nccl_ctx.stream();
126126
auto comm = nccl_ctx.comm_;
127127

paddle/fluid/framework/details/reduce_op_handle.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,15 @@ struct ReduceOpHandle : public OpHandleBase {
3434
const std::vector<platform::Place> &places_;
3535

3636
#ifdef PADDLE_WITH_CUDA
37-
const platform::NCCLContextMap &nccl_ctxs_;
37+
const platform::NCCLContextMap *nccl_ctxs_;
3838
ReduceOpHandle(const std::vector<Scope *> &local_scopes,
3939
const std::vector<platform::Place> &places,
40-
const platform::NCCLContextMap &nccl_ctxs)
40+
const platform::NCCLContextMap *nccl_ctxs)
4141
: local_scopes_(local_scopes), places_(places), nccl_ctxs_(nccl_ctxs) {
42-
for (auto &p_ctx : nccl_ctxs_.contexts_) {
43-
dev_ctxes_[platform::CUDAPlace(p_ctx.first)] = p_ctx.second.ctx_.get();
42+
if (nccl_ctxs_) {
43+
for (auto &p_ctx : nccl_ctxs_->contexts_) {
44+
dev_ctxes_[platform::CUDAPlace(p_ctx.first)] = p_ctx.second.ctx_.get();
45+
}
4446
}
4547
}
4648
#else

paddle/fluid/framework/details/reduce_op_handle_test.cc

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ struct TestReduceOpHandle {
4444
ctxs_[j]->Wait();
4545
}
4646
#ifdef PADDLE_WITH_CUDA
47-
nccl_ctxs_->WaitAll();
47+
if (nccl_ctxs_) {
48+
nccl_ctxs_->WaitAll();
49+
}
4850
#endif
4951
}
5052

@@ -64,6 +66,7 @@ struct TestReduceOpHandle {
6466
gpu_list_.push_back(p);
6567
ctxs_.emplace_back(new p::CUDADeviceContext(p));
6668
}
69+
nccl_ctxs_.reset(new platform::NCCLContextMap(gpu_list_));
6770
#else
6871
PADDLE_THROW("CUDA is not support.");
6972
#endif
@@ -74,10 +77,10 @@ struct TestReduceOpHandle {
7477
gpu_list_.push_back(p);
7578
ctxs_.emplace_back(new p::CPUDeviceContext(p));
7679
}
77-
}
7880
#ifdef PADDLE_WITH_CUDA
79-
nccl_ctxs_.reset(new platform::NCCLContextMap(gpu_list_));
81+
nccl_ctxs_.reset(nullptr);
8082
#endif
83+
}
8184
}
8285

8386
void InitReduceOp(size_t input_scope_idx) {
@@ -87,15 +90,27 @@ struct TestReduceOpHandle {
8790
}
8891
local_scopes_[input_scope_idx]->Var("input");
8992

93+
if (use_gpu_) {
94+
#ifdef PADDLE_WITH_CUDA
95+
op_handle_.reset(
96+
new ReduceOpHandle(local_scopes_, gpu_list_, nccl_ctxs_.get()));
97+
#else
98+
PADDLE_THROW("CUDA is not support.");
99+
#endif
100+
} else {
90101
#ifdef PADDLE_WITH_CUDA
91-
op_handle_.reset(new ReduceOpHandle(local_scopes_, gpu_list_, *nccl_ctxs_));
102+
op_handle_.reset(
103+
new ReduceOpHandle(local_scopes_, gpu_list_, nccl_ctxs_.get()));
92104
#else
93-
op_handle_.reset(new ReduceOpHandle(local_scopes_, gpu_list_));
105+
op_handle_.reset(new ReduceOpHandle(local_scopes_, gpu_list_));
94106
#endif
107+
}
95108

96109
// add input
97110
for (size_t j = 0; j < gpu_list_.size(); ++j) {
98-
op_handle_->dev_ctxes_[gpu_list_[j]] = ctxs_[j].get();
111+
if (!use_gpu_) {
112+
op_handle_->dev_ctxes_[gpu_list_[j]] = ctxs_[j].get();
113+
}
99114
vars_.emplace_back(new VarHandle());
100115
VarHandle *in_var_handle = static_cast<VarHandle *>(vars_.back().get());
101116
in_var_handle->place_ = gpu_list_[j];
@@ -236,25 +251,31 @@ TEST(ReduceTester, TestCPUReduceTestSelectedRows) {
236251
test_op.InitReduceOp(input_scope_idx);
237252
test_op.TestReduceSelectedRows(input_scope_idx);
238253
}
254+
TEST(ReduceTester, TestCPUReduceTestLodTensor) {
255+
TestReduceOpHandle test_op;
256+
size_t input_scope_idx = 0;
257+
test_op.InitCtxOnGpu(false);
258+
test_op.InitReduceOp(input_scope_idx);
259+
test_op.TestReduceLodTensors(input_scope_idx);
260+
}
261+
#ifdef PADDLE_WITH_CUDA
239262

240-
// #ifdef PADDLE_WITH_CUDA
241-
//
242-
// TEST(ReduceTester, TestGPUReduceTestSelectedRows) {
243-
// TestReduceOpHandle test_op;
244-
// size_t input_scope_idx = 0;
245-
// test_op.InitCtxOnGpu(true);
246-
// test_op.InitReduceOp(input_scope_idx);
247-
// test_op.TestReduceSelectedRows(input_scope_idx);
248-
// }
249-
//
250-
// TEST(ReduceTester, TestCPUReduceTestLodTensor) {
251-
// TestReduceOpHandle test_op;
252-
// size_t input_scope_idx = 0;
253-
// test_op.InitCtxOnGpu(true);
254-
// test_op.InitReduceOp(input_scope_idx);
255-
// test_op.TestReduceLodTensors(input_scope_idx);
256-
// }
257-
// #endif
263+
TEST(ReduceTester, TestGPUReduceTestSelectedRows) {
264+
TestReduceOpHandle test_op;
265+
size_t input_scope_idx = 0;
266+
test_op.InitCtxOnGpu(true);
267+
test_op.InitReduceOp(input_scope_idx);
268+
test_op.TestReduceSelectedRows(input_scope_idx);
269+
}
270+
271+
TEST(ReduceTester, TestGPUReduceTestLodTensor) {
272+
TestReduceOpHandle test_op;
273+
size_t input_scope_idx = 0;
274+
test_op.InitCtxOnGpu(true);
275+
test_op.InitReduceOp(input_scope_idx);
276+
test_op.TestReduceLodTensors(input_scope_idx);
277+
}
278+
#endif
258279

259280
} // namespace details
260281
} // namespace framework

paddle/fluid/framework/details/reduce_util.h

Lines changed: 0 additions & 51 deletions
This file was deleted.

0 commit comments

Comments
 (0)