Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Polish GetPlacesOp #7381

Merged
merged 2 commits into from
Jan 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions paddle/operators/get_places_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,32 +39,34 @@ class GetPlacesOp : public framework::OperatorBase {
: OperatorBase(type, inputs, outputs, attrs) {}
void Run(const framework::Scope &scope,
const platform::Place &place) const override {
std::string device_type = Attr<std::string>("device_type");
bool is_gpu;
if (Attr<std::string>("device_type") == "AUTO") {
is_gpu = platform::is_gpu_place(place);
} else {
is_gpu = Attr<std::string>("device_type") == "CUDA";
}
auto device_count = static_cast<size_t>(Attr<int>("device_count"));
if (device_count == 0) {
if (device_type == "CUDA") {
device_count = CUDADevCount();
} else if (device_type == "CPU") {
device_count = std::thread::hardware_concurrency();
}
device_count =
is_gpu ? CUDADevCount() : std::thread::hardware_concurrency();
}
PADDLE_ENFORCE_NE(device_count, 0, "Cannot indicate %s device count",
device_type);
is_gpu ? "GPU" : "CPU");

auto out_var_name = Output("Out");
auto &places =
*(detail::Ref(scope.FindVar(out_var_name),
"Output variable %s cannot be found", out_var_name)
.GetMutable<platform::PlaceList>());
places.reserve(device_count);
if (device_type == "CUDA") {
if (is_gpu) {
PADDLE_ENFORCE_LE(device_count, CUDADevCount(),
"Only %d CUDA devices found, cannot set to %d",
CUDADevCount(), device_count);
for (size_t i = 0; i < device_count; ++i) {
places.emplace_back(platform::CUDAPlace(i));
places.emplace_back(platform::CUDAPlace(static_cast<int>(i)));
}
} else if (device_type == "CPU") {
} else {
for (size_t i = 0; i < device_count; ++i) {
places.emplace_back(platform::CPUPlace());
}
Expand All @@ -77,10 +79,10 @@ class GetPlacesOpProtoMaker : public framework::OpProtoAndCheckerMaker {
GetPlacesOpProtoMaker(OpProto *proto, OpAttrChecker *op_checker)
: OpProtoAndCheckerMaker(proto, op_checker) {
AddOutput("Out", "vector of Place");
AddAttr<int>("device_count", "device count").SetDefault(1);
AddAttr<std::string>("device_type",
R"(device type must be in ["CPU", "CUDA"])")
.InEnum({"CPU", "CUDA"});
AddAttr<int>("device_count", "device count").SetDefault(0);
AddAttr<std::string>("device_type", "device type")
.InEnum({"CUDA", "CPU", "AUTO"})
.SetDefault("AUTO");
AddComment(R"DOC(
Returns a list of places based on flags. The list will be used for parallel
execution.
Expand Down
17 changes: 10 additions & 7 deletions python/paddle/v2/fluid/layers/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,22 @@

from ..layer_helper import LayerHelper
from ..framework import unique_name
from ..registry import autodoc

__all__ = ['get_places']


def get_places(device_count=0, device_type="CPU"):
@autodoc
def get_places(device_count=None, device_type=None):
helper = LayerHelper('get_places', **locals())
out_places = helper.create_variable(name=unique_name(helper.name + ".out"))
attrs = dict()
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not set device_count and device_type if the user does not specify them.

Because C++ side will set the default value. Do not set default value twice, it makes code harder to maintain.

if device_count is not None:
attrs['device_count'] = int(device_count)
if device_type is not None:
attrs['device_type'] = str(device_type)

helper.append_op(
type='get_places',
outputs={"Out": [out_places]},
attrs={
"device_type": device_type,
'device_count': device_count,
})
type='get_places', outputs={"Out": [out_places]}, attrs=attrs)

return out_places