|
| 1 | +/* Copyright 2024 The OpenXLA Authors. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. |
| 14 | +==============================================================================*/ |
| 15 | + |
| 16 | +#ifndef XLA_BACKENDS_GPU_COLLECTIVES_GPU_CLIQUE_KEY_H_ |
| 17 | +#define XLA_BACKENDS_GPU_COLLECTIVES_GPU_CLIQUE_KEY_H_ |
| 18 | + |
| 19 | +#include <cstdint> |
| 20 | +#include <string> |
| 21 | +#include <vector> |
| 22 | + |
| 23 | +#include "absl/hash/hash.h" |
| 24 | +#include "xla/core/collectives/clique_key.h" |
| 25 | +#include "xla/service/global_device_id.h" |
| 26 | +#include "xla/tsl/lib/gtl/int_type.h" |
| 27 | + |
| 28 | +namespace xla::gpu { |
| 29 | + |
| 30 | +// In XLA:GPU we use different streams for different kinds of collective |
| 31 | +// operations, and include the async stream kind into the GPU clique key. |
| 32 | +// |
| 33 | +// We carefully isolate different kinds of collectives using separate |
| 34 | +// communicators and guarantee that all collective operations have a total order |
| 35 | +// that will not create a deadlock. |
| 36 | +enum class AsyncStreamKind : int64_t { |
| 37 | + kCollective = 0, // Stream for asynchronous collective ops. |
| 38 | + kP2P0 = 1, // One Stream for P2P Send and Recv ops. |
| 39 | + kP2P1 = 2, // Another Stream for P2P Send and Recv ops. |
| 40 | + kMemCpyP2P = 3, // Stream for MemCpyP2P |
| 41 | +}; |
| 42 | + |
| 43 | +inline constexpr int64_t kAsyncStreamTotal = |
| 44 | + static_cast<int64_t>(AsyncStreamKind::kMemCpyP2P) + 1; |
| 45 | + |
| 46 | +// Strongly-typed wrapper to represent collective stream ID. |
| 47 | +TSL_LIB_GTL_DEFINE_INT_TYPE(CollectiveStreamId, uint64_t); |
| 48 | + |
| 49 | +// Assigns a unique ID to a stream for asynchronous or synchronous execution. |
| 50 | +// These IDs can be used, for example, to look up the NCCL communicator. |
| 51 | +CollectiveStreamId GetCollectiveStreamId( |
| 52 | + bool is_async, AsyncStreamKind stream_kind = AsyncStreamKind::kCollective); |
| 53 | + |
| 54 | +// Clique key for identifying a particular collectives clique on a GPU backend. |
| 55 | +class GpuCliqueKey : public CliqueKey { |
| 56 | + public: |
| 57 | + explicit GpuCliqueKey( |
| 58 | + std::vector<GlobalDeviceId> devices, |
| 59 | + CollectiveStreamId stream_id = CollectiveStreamId(0), |
| 60 | + AsyncStreamKind stream_kind = AsyncStreamKind::kCollective, |
| 61 | + std::vector<std::vector<GlobalDeviceId>> participant_groups = {}); |
| 62 | + |
| 63 | + CollectiveStreamId stream_id() const; |
| 64 | + |
| 65 | + // Returns true if this clique is a subset of `other`: both cliques have the |
| 66 | + // same `stream_id` and all clique devices are part of `other` clique. |
| 67 | + bool IsSubsetOf(const CliqueKey& other) const final; |
| 68 | + |
| 69 | + // Returns the stream kind for this clique key, stream kind will be used to |
| 70 | + // specify what configuration to pass for each type of operation. |
| 71 | + AsyncStreamKind stream_kind() const { return stream_kind_; } |
| 72 | + |
| 73 | + std::string ToString() const final; |
| 74 | + |
| 75 | + // GPU clique keys have a total order on which we rely on for acquiring |
| 76 | + // cliques in the same order across all participating devices. |
| 77 | + friend bool operator==(const GpuCliqueKey& a, const GpuCliqueKey& b); |
| 78 | + friend bool operator<(const GpuCliqueKey& a, const GpuCliqueKey& b); |
| 79 | + friend bool operator>(const GpuCliqueKey& a, const GpuCliqueKey& b); |
| 80 | + |
| 81 | + private: |
| 82 | + void HashValue(absl::HashState state) const final; |
| 83 | + |
| 84 | + CollectiveStreamId stream_id_; |
| 85 | + AsyncStreamKind stream_kind_; |
| 86 | + |
| 87 | + // The full list of groups across all devices which this clique is a part of. |
| 88 | + // |
| 89 | + // When GPU communicator splitting is enabled, this is used to distinguish |
| 90 | + // which cliques can be reused from the cache or must be split in order to |
| 91 | + // prevent a deadlock situation. |
| 92 | + // |
| 93 | + // For example, imagine we have a communicator with devices = [0,1] and |
| 94 | + // groups = [0, 1] Later on, we may want to create communicators [0, 1] and |
| 95 | + // [2, 3] by splitting [0, 1, 2, 3] If ranks 0 and 1 reuse the existing |
| 96 | + // [0, 1] clique but ranks 2 and 3 initiate a split, there will be a deadlock |
| 97 | + // since ranks 2, 3 and will be waiting forever for 0, 1 to join the split. |
| 98 | + // |
| 99 | + // Having the participating groups as part of the cache key will prevent such |
| 100 | + // situations |
| 101 | + std::vector<std::vector<GlobalDeviceId>> participant_groups_; |
| 102 | +}; |
| 103 | + |
| 104 | +bool operator==(const GpuCliqueKey& a, const GpuCliqueKey& b); |
| 105 | +bool operator<(const GpuCliqueKey& a, const GpuCliqueKey& b); |
| 106 | + |
| 107 | +} // namespace xla::gpu |
| 108 | + |
| 109 | +#endif // XLA_BACKENDS_GPU_COLLECTIVES_GPU_CLIQUE_KEY_H_ |
0 commit comments