|
| 1 | +/* Copyright 2026 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_RUNTIME_CHIP_ID_H_ |
| 17 | +#define XLA_RUNTIME_CHIP_ID_H_ |
| 18 | + |
| 19 | +#include <cstddef> |
| 20 | +#include <cstdint> |
| 21 | +#include <string> |
| 22 | + |
| 23 | +#include "absl/strings/str_cat.h" |
| 24 | +#include "absl/strings/str_join.h" |
| 25 | +#include "absl/strings/string_view.h" |
| 26 | +#include "absl/types/span.h" |
| 27 | +#include "xla/tsl/lib/gtl/int_type.h" |
| 28 | + |
| 29 | +namespace xla { |
| 30 | + |
| 31 | +// Some of the accelerator devices consist of multiple chips, and XLA might need |
| 32 | +// to address them separately. For example GB200 (1) from NVIDIA can be viewed |
| 33 | +// as a single device that consists of one Grace CPU and two Blackwell GPUs (one |
| 34 | +// `LocalDeviceId` with two `LocalChipId`s). Some TPU chips from Google have two |
| 35 | +// tensor cores (2) that appear as a single device to JAX/XLA users, and XLA |
| 36 | +// (including PjRt runtime) need to address these chips separately. |
| 37 | +// |
| 38 | +// (1) https://www.nvidia.com/en-us/data-center/gb200-nvl72/ |
| 39 | +// (2) |
| 40 | +// https://docs.jax.dev/en/latest/pallas/tpu/pipelining.html#tpus-in-megacore-configuration |
| 41 | + |
| 42 | +// Strongly-typed integer type for identifying local chips that belong to one of |
| 43 | +// the local devices. |
| 44 | +TSL_LIB_GTL_DEFINE_INT_TYPE(LocalChipId, int32_t); |
| 45 | + |
| 46 | +// Strongly-typed integer type for identifying global chips in a distributed |
| 47 | +// execution. |
| 48 | +TSL_LIB_GTL_DEFINE_INT_TYPE(GlobalChipId, int32_t); |
| 49 | + |
| 50 | +template <typename Sink> |
| 51 | +void AbslStringify(Sink& sink, LocalChipId id) { |
| 52 | + absl::Format(&sink, "%d", id.value()); |
| 53 | +} |
| 54 | + |
| 55 | +template <typename Sink> |
| 56 | +void AbslStringify(Sink& sink, GlobalChipId id) { |
| 57 | + absl::Format(&sink, "%d", id.value()); |
| 58 | +} |
| 59 | + |
| 60 | +// StrJoin for global chip ids that shortens long list of ids for readability. |
| 61 | +// |
| 62 | +// It is not uncommon to see in XLA a list of global chips with more than 1k |
| 63 | +// of entries. We don't need to print them all to get a human readable list |
| 64 | +// of chips for logging and debugging. |
| 65 | +inline std::string HumanReadableChips(absl::Span<const GlobalChipId> chips, |
| 66 | + absl::string_view separator = ",", |
| 67 | + size_t first = 8, size_t last = 2) { |
| 68 | + if (chips.size() > first + last) { |
| 69 | + return absl::StrCat(absl::StrJoin(chips.first(first), separator), "...", |
| 70 | + absl::StrJoin(chips.last(last), separator)); |
| 71 | + } |
| 72 | + return absl::StrJoin(chips, separator); |
| 73 | +} |
| 74 | + |
| 75 | +} // namespace xla |
| 76 | + |
| 77 | +#endif // XLA_RUNTIME_CHIP_ID_H_ |
0 commit comments