This change makes several API changes: * we allow the client to provide a mapping from the local device ordinals on the machine to global device IDs. If provided, we interpret the device IDs in the DeviceAssignment provided by the client as global IDs, not as local device ordinals. This allows us to describe computations that cross a host boundary. * we allow the client to provide a callback for manufacturing a ncclUniqueId for a particular subset of global devices. The idea is that the client should use some other distributed system of their own (e.g., MPI) to share ncclUniqueId values needed for a computation. NCCL allows for cross-host/process collectives iff the same ncclUniqueId value is used. Refactors the common collective logic and the NCCL collective logic in particular to support a local/global distinction. PiperOrigin-RevId: 296505571 Change-Id: I5ed42d65597b0960df78890745421f77e9789ba3
107 lines
3.3 KiB
C++
107 lines
3.3 KiB
C++
/* Copyright 2019 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/refcounting_hash_map.h"
|
|
|
|
#include <functional>
|
|
|
|
#include "tensorflow/compiler/xla/test.h"
|
|
#include "tensorflow/compiler/xla/types.h"
|
|
|
|
namespace xla {
|
|
namespace {
|
|
|
|
struct DeleteNotifier {
|
|
DeleteNotifier() = default;
|
|
DeleteNotifier(const DeleteNotifier&) = delete;
|
|
DeleteNotifier& operator=(const DeleteNotifier&) = delete;
|
|
DeleteNotifier(DeleteNotifier&& o) noexcept : fn(std::move(o.fn)) {
|
|
o.fn = nullptr;
|
|
}
|
|
DeleteNotifier& operator=(DeleteNotifier&& o) noexcept {
|
|
fn = o.fn;
|
|
o.fn = nullptr;
|
|
return *this;
|
|
}
|
|
|
|
~DeleteNotifier() {
|
|
if (fn) {
|
|
fn();
|
|
}
|
|
}
|
|
|
|
std::function<void()> fn;
|
|
};
|
|
|
|
TEST(RefcountingHashMapTest, PointerIdentity) {
|
|
RefcountingHashMap<int, int> m;
|
|
auto factory = [](const int&) { return absl::make_unique<int>(); };
|
|
std::shared_ptr<int> a = m.GetOrCreateIfAbsent(0, factory);
|
|
std::shared_ptr<int> b = m.GetOrCreateIfAbsent(0, factory);
|
|
std::shared_ptr<int> c = m.GetOrCreateIfAbsent(1, factory);
|
|
EXPECT_EQ(a.get(), b.get());
|
|
EXPECT_NE(a.get(), c.get());
|
|
}
|
|
|
|
TEST(RefcountingHashMapTest, DefaultInitialized) {
|
|
RefcountingHashMap<int, int> m;
|
|
auto factory = [](const int&) { return absl::make_unique<int>(); };
|
|
EXPECT_EQ(*m.GetOrCreateIfAbsent(42, factory), 0);
|
|
}
|
|
|
|
TEST(RefcountingHashMapTest, DeletesEagerly) {
|
|
RefcountingHashMap<int, DeleteNotifier> m;
|
|
bool deleted = false;
|
|
auto factory = [](const int&) { return absl::make_unique<DeleteNotifier>(); };
|
|
auto handle = m.GetOrCreateIfAbsent(0, factory);
|
|
handle->fn = [&] { deleted = true; };
|
|
EXPECT_FALSE(deleted);
|
|
handle = nullptr;
|
|
EXPECT_TRUE(deleted);
|
|
}
|
|
|
|
TEST(RefcountingHashMapTest, CustomFactory) {
|
|
RefcountingHashMap<int, int> m;
|
|
auto factory = [](const int& x) { return absl::make_unique<int>(x + 1); };
|
|
EXPECT_EQ(*m.GetOrCreateIfAbsent(0, factory), 1);
|
|
EXPECT_EQ(*m.GetOrCreateIfAbsent(100, factory), 101);
|
|
}
|
|
|
|
TEST(RefcountingHashMapTest, ForEachEmpty) {
|
|
RefcountingHashMap<int, int> m;
|
|
int64 count = 0;
|
|
m.ForEach([&](const int&, std::shared_ptr<int>) { ++count; });
|
|
EXPECT_EQ(count, 0);
|
|
}
|
|
|
|
TEST(RefcountingHashMapTest, ForEachNonempty) {
|
|
RefcountingHashMap<int, int> m;
|
|
auto factory = [](const int&) { return absl::make_unique<int>(); };
|
|
auto a = m.GetOrCreateIfAbsent(0, factory);
|
|
auto b = m.GetOrCreateIfAbsent(1, factory);
|
|
|
|
std::vector<int> seen_keys;
|
|
std::vector<int*> seen_values;
|
|
m.ForEach([&](const int& k, std::shared_ptr<int> v) {
|
|
seen_keys.push_back(k);
|
|
seen_values.push_back(v.get());
|
|
});
|
|
EXPECT_THAT(seen_keys, testing::UnorderedElementsAre(0, 1));
|
|
EXPECT_THAT(seen_values, testing::UnorderedElementsAre(a.get(), b.get()));
|
|
}
|
|
|
|
} // anonymous namespace
|
|
} // namespace xla
|