Skip to content

Refactor GetNumDevices and create GetNumGlobalDevices #9184

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
9 changes: 6 additions & 3 deletions torch_xla/csrc/init_python_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1487,10 +1487,13 @@ void InitXlaModuleBindings(py::module m) {
if (UseVirtualDevice()) {
return 1;
} else {
return runtime::GetComputationClient()->GetNumDevices();
return runtime::GetComputationClient()->GetNumLocalDevices();
}
});
m.def("_xla_get_all_devices", []() {
m.def("_xla_num_devices", []() -> int64_t {
return runtime::GetComputationClient()->GetNumDevices();
});
m.def("_xla_num_global_devices", []() {
std::vector<std::string> all_devices =
runtime::GetComputationClient()->GetAllDevices();
if (UseVirtualDevice()) {
Expand All @@ -1505,7 +1508,7 @@ void InitXlaModuleBindings(py::module m) {
m.def("_xla_get_runtime_devices",
[]() { return runtime::GetComputationClient()->GetLocalDevices(); });
m.def("_xla_num_runtime_devices", []() -> int64_t {
return runtime::GetComputationClient()->GetNumDevices();
Copy link
Collaborator

Choose a reason for hiding this comment

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

would it makes sense to ensure that the both getNumLocalDevices and getNumDevices are exposed into Python (not as torch_xla._XLAC but another wrapper layer beyond that), and to make the naming consistent from bottom to the top - cpp to binding to public facing, documented python api (perhaps XR)?

return runtime::GetComputationClient()->GetNumLocalDevices();
});
m.def("_xla_get_all_runtime_devices", []() {
std::vector<std::string> all_devices =
Expand Down
2 changes: 2 additions & 0 deletions torch_xla/csrc/runtime/computation_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,8 @@ class ComputationClient {

virtual std::intptr_t GetCudaStreamForDevice(int local_device_id) const = 0;

virtual size_t GetNumLocalDevices() const = 0;

virtual size_t GetNumDevices() const = 0;

virtual std::vector<std::string> GetLocalDevices() const = 0;
Expand Down
6 changes: 5 additions & 1 deletion torch_xla/csrc/runtime/ifrt_computation_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -622,10 +622,14 @@ IfrtComputationClient::ExecuteReplicated(
return data_handles;
}

size_t IfrtComputationClient::GetNumDevices() const {
size_t IfrtComputationClient::GetNumLocalDevices() const {
return client_->addressable_device_count();
}

size_t IfrtComputationClient::GetNumDevices() const {
return client_->device_count();
}

std::string IfrtComputationClient::GetDefaultDevice() const {
return IfrtDeviceToString(client_->addressable_devices()[0]);
}
Expand Down
2 changes: 2 additions & 0 deletions torch_xla/csrc/runtime/ifrt_computation_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ class IfrtComputationClient : public ComputationClient {
absl::Span<const std::string> devices,
const ExecuteReplicatedOptions& options) override;

size_t GetNumLocalDevices() const override;

size_t GetNumDevices() const override;

std::string GetDefaultDevice() const override;
Expand Down
6 changes: 5 additions & 1 deletion torch_xla/csrc/runtime/pjrt_computation_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -932,10 +932,14 @@ PjRtComputationClient::ExecuteReplicated(
return data_handles;
}

size_t PjRtComputationClient::GetNumDevices() const {
size_t PjRtComputationClient::GetNumLocalDevices() const {
return client_->addressable_device_count();
}

size_t PjRtComputationClient::GetNumDevices() const {
return client_->device_count();
}

std::string PjRtComputationClient::GetDefaultDevice() const {
return PjRtDeviceToString(client_->addressable_devices()[0]);
}
Expand Down
2 changes: 2 additions & 0 deletions torch_xla/csrc/runtime/pjrt_computation_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ class PjRtComputationClient : public ComputationClient {
absl::Span<const std::string> devices,
const ExecuteReplicatedOptions& options) override;

size_t GetNumLocalDevices() const override;

size_t GetNumDevices() const override;

std::string GetDefaultDevice() const override;
Expand Down
2 changes: 1 addition & 1 deletion torch_xla/csrc/tensor_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ struct XLAGuardImpl : public c10::impl::DeviceGuardImplInterface {
return 0;
}

return client->GetNumDevices();
return client->GetNumLocalDevices();
}
};

Expand Down
Loading