Skip to content

Commit 5f98500

Browse files
dzhwinterYang Yang(Tony)
authored andcommitted
Make init device on all gpu by default (#7345)
* "init use all default devices" * "fix init test"
1 parent f8d13c2 commit 5f98500

File tree

10 files changed

+30
-58
lines changed

10 files changed

+30
-58
lines changed

paddle/framework/device_data_transform_test.cu

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,7 @@ static void BuildVar(const std::string& param_name,
105105
TEST(Operator, CPUtoGPU) {
106106
using namespace paddle::framework;
107107
using namespace paddle::platform;
108-
109-
ASSERT_EQ(InitDevices({"CPU", "GPU:0"}), true);
108+
InitDevices();
110109

111110
paddle::framework::Scope scope;
112111
paddle::platform::CPUPlace cpu_place;

paddle/framework/init.cc

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -40,40 +40,23 @@ void InitGflags(std::vector<std::string> &argv) {
4040
});
4141
}
4242

43-
bool InitDevices(const std::vector<std::string> &devices) {
44-
// device format
45-
// CPU
46-
// GPU:1
47-
// TODO(dzhwinter) : add device format annotation for users.
43+
void InitDevices() {
44+
/*Init all avaiable devices by default */
45+
4846
std::vector<platform::Place> places;
49-
for (auto &device : devices) {
50-
auto p = string::Piece(device);
51-
if (string::HasPrefix(p, "CPU")) {
52-
places.emplace_back(platform::CPUPlace());
53-
} else if (string::HasPrefix(p, "GPU")) {
47+
places.emplace_back(platform::CPUPlace());
48+
5449
#ifdef PADDLE_WITH_CUDA
55-
auto pos = string::RFind(p, ':', string::Piece::npos);
56-
auto number = device.substr(pos + 1);
57-
places.emplace_back(platform::CUDAPlace(std::stoi(number)));
50+
int count = platform::GetCUDADeviceCount();
51+
for (int i = 0; i < count; ++i) {
52+
places.emplace_back(platform::CUDAPlace(i));
53+
}
5854
#else
59-
LOG(WARNING)
60-
<< "'GPU' is not supported, Please re-compile with WITH_GPU option";
55+
LOG(WARNING)
56+
<< "'GPU' is not supported, Please re-compile with WITH_GPU option";
6157
#endif
62-
} else {
63-
return false;
64-
}
65-
}
6658

67-
if (std::find_if(places.begin(), places.end(),
68-
[&](const platform::Place &place) {
69-
return platform::is_cpu_place(place);
70-
}) == places.end()) {
71-
places.emplace_back(platform::CPUPlace());
72-
LOG(WARNING) << "Not specified CPU device, create CPU by Default.";
73-
}
7459
platform::DeviceContextPool::Init(places);
75-
// framework::UseALL();
76-
return true;
7760
}
7861

7962
void InitGLOG(const std::string &prog_name) {

paddle/framework/init.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ void InitGflags(std::vector<std::string> &argv);
2424

2525
void InitGLOG(const std::string &prog_name);
2626

27-
bool InitDevices(const std::vector<std::string> &devices);
27+
void InitDevices();
2828

2929
} // namespace framework
3030
} // namespace paddle

paddle/framework/init_test.cc

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,13 @@ limitations under the License. */
1414
#include "gtest/gtest.h"
1515

1616
#include "paddle/framework/init.h"
17+
#include "paddle/platform/device_context.h"
1718

18-
TEST(Init, InitDevices) {
19+
TEST(InitDevices, CPU) {
1920
using paddle::framework::InitDevices;
20-
std::vector<std::string> ds1 = {"CPU"};
21-
ASSERT_EQ(InitDevices(ds1), true);
21+
using paddle::platform::DeviceContextPool;
2222

23-
#ifdef PADDLE_WITH_CUDA
24-
std::vector<std::string> ds2 = {"CPU", "GPU:0", "GPU:1"};
25-
ASSERT_EQ(InitDevices(ds2), true);
26-
27-
// test re-init
28-
std::vector<std::string> ds3 = {"GPU:0", "GPU:1"};
29-
ASSERT_EQ(InitDevices(ds3), true);
30-
#endif
23+
InitDevices();
24+
DeviceContextPool& pool = DeviceContextPool::Instance();
25+
ASSERT_GE(pool.size(), 1U);
3126
}

paddle/framework/operator_test.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ REGISTER_OP_WITHOUT_GRADIENT(test_operator,
6969
paddle::framework::OpWithoutKernelCheckerMaker);
7070

7171
TEST(OperatorBase, all) {
72-
paddle::framework::InitDevices({"CPU"});
72+
paddle::framework::InitDevices();
7373
paddle::framework::proto::OpDesc op_desc;
7474
op_desc.set_type("test_operator");
7575
BuildVar("input", {"IN1"}, op_desc.add_inputs());
@@ -195,7 +195,7 @@ REGISTER_OP_CPU_KERNEL(op_with_kernel,
195195

196196
// test with single input
197197
TEST(OpKernel, all) {
198-
paddle::framework::InitDevices({"CPU"});
198+
paddle::framework::InitDevices();
199199
paddle::framework::proto::OpDesc op_desc;
200200
op_desc.set_type("op_with_kernel");
201201
BuildVar("x", {"IN1"}, op_desc.add_inputs());
@@ -225,7 +225,7 @@ REGISTER_OP_CPU_KERNEL(op_multi_inputs_with_kernel,
225225
TEST(OpKernel, multi_inputs) {
226226
using namespace paddle::framework;
227227

228-
paddle::framework::InitDevices({"CPU"});
228+
paddle::framework::InitDevices();
229229
proto::OpDesc op_desc;
230230

231231
op_desc.set_type("op_multi_inputs_with_kernel");
@@ -264,7 +264,7 @@ class OperatorClone : public paddle::framework::OperatorBase {
264264
};
265265

266266
TEST(Operator, Clone) {
267-
paddle::framework::InitDevices({"CPU"});
267+
paddle::framework::InitDevices();
268268
OperatorClone a("ABC", paddle::framework::VariableNameMap{},
269269
paddle::framework::VariableNameMap{},
270270
paddle::framework::AttributeMap{});

paddle/inference/inference.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ void InferenceEngine::Execute(const std::vector<framework::LoDTensor>& feeds,
169169
}
170170

171171
auto* place = new platform::CPUPlace();
172-
framework::InitDevices({"CPU"});
172+
framework::InitDevices();
173173
framework::Executor* executor = new framework::Executor(*place);
174174
framework::Scope* scope = new framework::Scope();
175175

paddle/platform/device_context.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ class DeviceContextPool {
185185
const typename DefaultDeviceContextType<Place>::TYPE*>(Get(place));
186186
}
187187

188+
size_t size() const { return device_contexts_.size(); }
189+
188190
private:
189191
static DeviceContextPool* pool;
190192
constexpr static int LEFT_SHIFT = 8;

paddle/testing/paddle_gtest_main.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ int main(int argc, char** argv) {
3434
google::ParseCommandLineFlags(&new_argc, &new_argv_address, false);
3535
testing::InitGoogleTest(&argc, argv);
3636
paddle::memory::Used(paddle::platform::CPUPlace());
37-
std::vector<std::string> devs = {"CPU"};
37+
3838
#ifdef PADDLE_WITH_CUDA
3939
paddle::memory::Used(paddle::platform::CUDAPlace(0));
40-
devs.push_back("GPU:0");
4140
#endif
42-
paddle::framework::InitDevices(devs);
41+
42+
paddle::framework::InitDevices();
4343
return RUN_ALL_TESTS();
4444
}

python/paddle/v2/fluid/__init__.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,7 @@ def __bootstrap__():
6262
core.init_gflags([sys.argv[0]] +
6363
["--tryfromenv=" + ",".join(read_env_flags)])
6464
core.init_glog(sys.argv[0])
65-
66-
if core.is_compile_gpu():
67-
core.init_devices(["CPU", "GPU:0"])
68-
else:
69-
core.init_devices(["CPU"])
65+
core.init_devices()
7066

7167

7268
__bootstrap__()

python/paddle/v2/fluid/tests/test_batch_norm_op.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -341,9 +341,6 @@ def test_with_place(place, data_layout, shape):
341341
if core.is_compile_gpu() and core.op_support_gpu("batch_norm"):
342342
places.append(core.CUDAPlace(0))
343343

344-
core.init_devices(["CPU", "GPU:0"])
345-
else:
346-
core.init_devices(["CPU"])
347344
for place in places:
348345
for data_format in ["NCHW", "NHWC"]:
349346
test_with_place(place, data_format, [2, 3, 4, 5])

0 commit comments

Comments
 (0)