-
Notifications
You must be signed in to change notification settings - Fork 491
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure dist runtime is in sync before shutting down. (#5714)
* added the new dist runtime class. * convert the class to singleton * the tests passed. * cleaned up * fix a build error * Use std::call_once. But it results in early shutting down the dist rt service before the job finishes. * Revert "Use std::call_once. But it results in early shutting down the dist rt service before the job finishes." This reverts commit d6e2b98. * fix comments * fix comment
- Loading branch information
1 parent
8f121c3
commit fa7828f
Showing
8 changed files
with
117 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#include "torch_xla/csrc/runtime/distributed_runtime.h" | ||
|
||
#include "torch_xla/csrc/runtime/debug_macros.h" | ||
#include "torch_xla/csrc/runtime/sys_util.h" | ||
|
||
namespace torch_xla { | ||
namespace runtime { | ||
|
||
const std::string DistributedRuntime::default_coordinator_port = "8547"; | ||
|
||
DistributedRuntime::DistributedRuntime(int global_rank, std::string master_addr, | ||
std::string port) { | ||
std::string dist_service_addr = absl::StrJoin({master_addr, port}, ":"); | ||
if (global_rank == 0) { | ||
int local_world_size = sys_util::GetEnvInt("LOCAL_WORLD_SIZE", 1); | ||
int global_world_size = sys_util::GetEnvInt("WORLD_SIZE", local_world_size); | ||
xla::CoordinationServiceImpl::Options service_options; | ||
service_options.num_nodes = global_world_size; | ||
xla::StatusOr<std::unique_ptr<xla::DistributedRuntimeService>> | ||
dist_runtime_service = xla::GetDistributedRuntimeService( | ||
dist_service_addr, service_options); | ||
XLA_CHECK(dist_runtime_service.ok()) | ||
<< "Failed to initialize distributed runtime service."; | ||
dist_runtime_service_ = std::move(dist_runtime_service.value()); | ||
} | ||
|
||
xla::DistributedRuntimeClient::Options client_options; | ||
client_options.node_id = global_rank; | ||
dist_runtime_client_ = | ||
xla::GetDistributedRuntimeClient(dist_service_addr, client_options); | ||
XLA_CHECK(dist_runtime_client_->Connect().ok()) | ||
<< "Failed to initialize distributed runtime client"; | ||
} | ||
|
||
DistributedRuntime::~DistributedRuntime() { | ||
if (dist_runtime_client_ != nullptr) { | ||
XLA_CHECK(dist_runtime_client_->Shutdown().ok()) | ||
<< "Failed to shut down the distributed runtime client."; | ||
dist_runtime_client_ = nullptr; | ||
} | ||
if (dist_runtime_service_ != nullptr) { | ||
dist_runtime_service_->Shutdown(); | ||
dist_runtime_service_ = nullptr; | ||
} | ||
} | ||
|
||
std::shared_ptr<xla::DistributedRuntimeClient> DistributedRuntime::GetClient() { | ||
XLA_CHECK(dist_runtime_client_ != nullptr) | ||
<< "distributed runtime client is null."; | ||
return dist_runtime_client_; | ||
} | ||
|
||
} // namespace runtime | ||
} // namespace torch_xla |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#ifndef XLA_CLIENT_DISTRIBUTED_RUNTIME_H_ | ||
#define XLA_CLIENT_DISTRIBUTED_RUNTIME_H_ | ||
|
||
#include <memory> | ||
|
||
#include "xla/pjrt/distributed/distributed.h" | ||
|
||
namespace torch_xla { | ||
namespace runtime { | ||
|
||
class DistributedRuntime { | ||
public: | ||
static const std::string default_coordinator_port; | ||
static DistributedRuntime& getInstance(int global_rank, | ||
std::string master_addr, | ||
std::string port) { | ||
static DistributedRuntime dist_runtime_instance(global_rank, master_addr, | ||
port); | ||
return dist_runtime_instance; | ||
} | ||
~DistributedRuntime(); | ||
DistributedRuntime(DistributedRuntime const&) = delete; | ||
void operator=(DistributedRuntime const&) = delete; | ||
|
||
std::shared_ptr<xla::DistributedRuntimeClient> GetClient(); | ||
|
||
private: | ||
DistributedRuntime(int global_rank, std::string master_addr, | ||
std::string port); | ||
|
||
std::unique_ptr<xla::DistributedRuntimeService> dist_runtime_service_; | ||
std::shared_ptr<xla::DistributedRuntimeClient> dist_runtime_client_; | ||
}; | ||
|
||
} // namespace runtime | ||
} // namespace torch_xla | ||
|
||
#endif // XLA_CLIENT_DISTRIBUTED_RUNTIME_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters