|
| 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 | +#include "xla/backends/gpu/libraries/cub/cub_scratch_size_deviceless_lookup.h" |
| 17 | + |
| 18 | +#include <algorithm> |
| 19 | +#include <cstdint> |
| 20 | +#include <optional> |
| 21 | +#include <utility> |
| 22 | + |
| 23 | +#include "absl/status/status.h" |
| 24 | +#include "absl/status/statusor.h" |
| 25 | +#include "absl/strings/string_view.h" |
| 26 | +#include "xla/backends/gpu/libraries/cub/cub_sort_utils.h" |
| 27 | +#include "xla/stream_executor/semantic_version.h" |
| 28 | + |
| 29 | +namespace xla::gpu { |
| 30 | + |
| 31 | +absl::StatusOr<CubScratchSizeDevicelessLookup> |
| 32 | +CubScratchSizeDevicelessLookup::Create(CubScratchSizeLookupTable proto) { |
| 33 | + for (const auto& entry : proto.entries()) { |
| 34 | + for (int i = 1; i < entry.scratch_size_recordings_size(); ++i) { |
| 35 | + if (entry.scratch_size_recordings(i).num_items() <= |
| 36 | + entry.scratch_size_recordings(i - 1).num_items()) { |
| 37 | + return absl::InvalidArgumentError( |
| 38 | + "scratch_size_recordings must be sorted by num_items"); |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + return CubScratchSizeDevicelessLookup(std::move(proto)); |
| 43 | +} |
| 44 | + |
| 45 | +CubScratchSizeDevicelessLookup::CubScratchSizeDevicelessLookup( |
| 46 | + CubScratchSizeLookupTable proto) |
| 47 | + : proto_(std::move(proto)) {} |
| 48 | + |
| 49 | +const CubScratchSizeEntry* CubScratchSizeDevicelessLookup::FindEntry( |
| 50 | + stream_executor::SemanticVersion cub_version, absl::string_view device_name, |
| 51 | + int32_t key_type_size, std::optional<int32_t> value_type_size, |
| 52 | + bool is_segmented) const { |
| 53 | + for (const CubScratchSizeEntry& entry : proto_.entries()) { |
| 54 | + bool version_matched = |
| 55 | + std::find(entry.cub_version().begin(), entry.cub_version().end(), |
| 56 | + cub_version.ToString()) != entry.cub_version().end(); |
| 57 | + |
| 58 | + if (version_matched && entry.device_name() == device_name && |
| 59 | + entry.key_type_size() == key_type_size && |
| 60 | + entry.value_type_size() == value_type_size.value_or(0) && |
| 61 | + entry.is_segmented() == is_segmented) { |
| 62 | + return &entry; |
| 63 | + } |
| 64 | + } |
| 65 | + return nullptr; |
| 66 | +} |
| 67 | + |
| 68 | +std::optional<int64_t> CubScratchSizeDevicelessLookup::Lookup( |
| 69 | + stream_executor::SemanticVersion cub_version, absl::string_view device_name, |
| 70 | + int32_t key_type_size, std::optional<int32_t> value_type_size, |
| 71 | + int64_t num_items, int64_t batch_size) const { |
| 72 | + const CubScratchSizeEntry* entry = |
| 73 | + FindEntry(cub_version, device_name, key_type_size, value_type_size, |
| 74 | + /*is_segmented=*/batch_size > 1); |
| 75 | + if (entry == nullptr) { |
| 76 | + return std::nullopt; |
| 77 | + } |
| 78 | + |
| 79 | + auto it = std::lower_bound( |
| 80 | + entry->scratch_size_recordings().begin(), |
| 81 | + entry->scratch_size_recordings().end(), num_items, |
| 82 | + [](const CubScratchSizeEntry::ScratchSizeRecord& record, |
| 83 | + int64_t num_items) { return record.num_items() < num_items; }); |
| 84 | + |
| 85 | + if (it == entry->scratch_size_recordings().end()) { |
| 86 | + return std::nullopt; |
| 87 | + } |
| 88 | + |
| 89 | + return AddSegmentedSortOffsetsToScratchSize(it->scratch_space_bytes(), |
| 90 | + batch_size); |
| 91 | +} |
| 92 | + |
| 93 | +bool CubScratchSizeDevicelessLookup::CanLookup( |
| 94 | + stream_executor::SemanticVersion cub_version, absl::string_view device_name, |
| 95 | + int32_t key_type_size, std::optional<int32_t> value_type_size, |
| 96 | + int64_t num_items, int64_t batch_size) const { |
| 97 | + const CubScratchSizeEntry* entry = FindEntry( |
| 98 | + cub_version, device_name, key_type_size, value_type_size, batch_size > 1); |
| 99 | + if (entry == nullptr || entry->scratch_size_recordings().empty()) { |
| 100 | + return false; |
| 101 | + } |
| 102 | + |
| 103 | + return entry->scratch_size_recordings() |
| 104 | + .Get(entry->scratch_size_recordings().size() - 1) |
| 105 | + .num_items() >= num_items; |
| 106 | +} |
| 107 | + |
| 108 | +} // namespace xla::gpu |
0 commit comments