-
Notifications
You must be signed in to change notification settings - Fork 74k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PJRT] Enable layering_check in the Bazel BUILD.
PiperOrigin-RevId: 446190811
- Loading branch information
1 parent
bedee44
commit f679cb4
Showing
4 changed files
with
156 additions
and
68 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
==============================================================================*/ | ||
|
||
#include "tensorflow/compiler/xla/pjrt/nccl_id_store.h" | ||
|
||
#include <string> | ||
#include <utility> | ||
|
||
#ifdef NCCL_ENABLED | ||
#include "third_party/nccl/nccl.h" | ||
#endif // NCCL_ENABLED | ||
|
||
#include "tensorflow/compiler/xla/util.h" | ||
|
||
namespace xla { | ||
|
||
StatusOr<std::string> NcclIdStore::GetNcclUniqueId( | ||
const gpu::NcclCliqueKey& key) { | ||
// The caller must ensure that threads calling this method concurrently have | ||
// unique keys, otherwise the global key-value store may hold the wrong value. | ||
{ | ||
absl::MutexLock lock(&mu_); | ||
auto it = cache_.find(key); | ||
if (it != cache_.end()) { | ||
return it->second; | ||
} | ||
} | ||
std::string id_string; | ||
int primary_node_id = device_to_node_.at(key.devices()[0]); | ||
if (node_id_ == primary_node_id) { | ||
#ifdef NCCL_ENABLED | ||
ncclUniqueId id; | ||
ncclResult_t r = ncclGetUniqueId(&id); | ||
TF_RET_CHECK(r == ncclSuccess); | ||
id_string = std::string(id.internal, NCCL_UNIQUE_ID_BYTES); | ||
TF_RETURN_IF_ERROR(client_->KeyValueSet(key.ToString(), id_string)); | ||
#else | ||
return FailedPrecondition("NCCL support was not built into XLA binary."); | ||
#endif | ||
} else { | ||
TF_ASSIGN_OR_RETURN(id_string, client_->BlockingKeyValueGet( | ||
key.ToString(), absl::Minutes(5))); | ||
} | ||
absl::MutexLock lock(&mu_); | ||
auto result = cache_.emplace(key, std::move(id_string)); | ||
TF_RET_CHECK(result.second) << "Unique ID already in cache."; | ||
return result.first->second; | ||
} | ||
|
||
} // namespace 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,59 @@ | ||
/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
==============================================================================*/ | ||
|
||
#ifndef TENSORFLOW_COMPILER_XLA_PJRT_NCCL_ID_STORE_H_ | ||
#define TENSORFLOW_COMPILER_XLA_PJRT_NCCL_ID_STORE_H_ | ||
|
||
#include <memory> | ||
#include <utility> | ||
#include <string> | ||
|
||
#include "absl/base/attributes.h" | ||
#include "absl/container/flat_hash_map.h" | ||
#include "absl/synchronization/mutex.h" | ||
#include "tensorflow/compiler/xla/pjrt/distributed/client.h" | ||
#include "tensorflow/compiler/xla/service/global_device_id.h" | ||
#include "tensorflow/compiler/xla/service/gpu/gpu_executable_run_options.h" | ||
#include "tensorflow/compiler/xla/statusor.h" | ||
|
||
namespace xla { | ||
|
||
// A table mapping NcclCliqueKeys to ncclUniqueId values encoded as strings. | ||
// In a distributed setup the table of NCCL IDs is kept on the master node | ||
// (node 0). The node of the first participating device will create the unique | ||
// id. | ||
class NcclIdStore { | ||
public: | ||
NcclIdStore(int node_id, std::shared_ptr<DistributedRuntimeClient> client, | ||
absl::flat_hash_map<GlobalDeviceId, int> device_to_node) | ||
: node_id_(node_id), | ||
client_(std::move(client)), | ||
device_to_node_(std::move(device_to_node)) {} | ||
|
||
StatusOr<std::string> GetNcclUniqueId(const gpu::NcclCliqueKey& key); | ||
|
||
private: | ||
const int node_id_; | ||
const std::shared_ptr<DistributedRuntimeClient> client_; | ||
const absl::flat_hash_map<GlobalDeviceId, int> device_to_node_; | ||
|
||
absl::Mutex mu_; | ||
absl::flat_hash_map<gpu::NcclCliqueKey, std::string> cache_ | ||
ABSL_GUARDED_BY(mu_); | ||
}; | ||
|
||
} // namespace xla | ||
|
||
#endif // TENSORFLOW_COMPILER_XLA_PJRT_NCCL_ID_STORE_H_ |