|
| 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 | +#include "xla/service/gpu/runtime/address_computation_thunk.h" |
| 17 | + |
| 18 | +#include <cstdint> |
| 19 | +#include <memory> |
| 20 | +#include <optional> |
| 21 | +#include <utility> |
| 22 | +#include <vector> |
| 23 | + |
| 24 | +#include "absl/status/status.h" |
| 25 | +#include "absl/strings/str_format.h" |
| 26 | +#include "absl/synchronization/mutex.h" |
| 27 | +#include "llvm/ADT/STLExtras.h" |
| 28 | +#include "xla/service/buffer_assignment.h" |
| 29 | +#include "xla/service/gpu/buffer_allocations.h" |
| 30 | +#include "xla/service/gpu/ir_emission_utils.h" |
| 31 | +#include "xla/service/gpu/runtime/sequential_thunk.h" |
| 32 | +#include "xla/service/gpu/thunk.h" |
| 33 | +#include "xla/shape.h" |
| 34 | +#include "xla/shape_util.h" |
| 35 | +#include "xla/status.h" |
| 36 | +#include "xla/status_macros.h" |
| 37 | +#include "xla/stream_executor/device_memory.h" |
| 38 | +#include "xla/stream_executor/memory_allocation.h" |
| 39 | +#include "tsl/platform/errors.h" |
| 40 | +#include "tsl/platform/statusor.h" |
| 41 | + |
| 42 | +namespace xla { |
| 43 | +namespace gpu { |
| 44 | + |
| 45 | +AddressComputationThunk::AddressComputationThunk( |
| 46 | + ThunkInfo thunk_info, std::unique_ptr<ThunkSequence> embedded_thunk, |
| 47 | + std::vector<std::optional<const BufferAllocation::Slice>> operands, |
| 48 | + std::vector<std::optional<const BufferAllocation::Slice>> results, |
| 49 | + std::vector<std::optional<const BufferAllocation::Slice>> |
| 50 | + offset_buffer_indices, |
| 51 | + std::vector<std::optional<const Shape>> orig_shapes, |
| 52 | + std::vector<std::optional<const Shape>> sliced_shapes) |
| 53 | + : Thunk(Kind::kAddressComputation, thunk_info), |
| 54 | + embedded_thunk_(std::make_unique<SequentialThunk>( |
| 55 | + ThunkInfo(thunk_info.op), std::move(*embedded_thunk))), |
| 56 | + embedded_thunk_operands_(std::move(operands)), |
| 57 | + embedded_thunk_results_(std::move(results)), |
| 58 | + offset_buffer_indices_(std::move(offset_buffer_indices)), |
| 59 | + orig_shapes_(std::move(orig_shapes)), |
| 60 | + sliced_shapes_(std::move(sliced_shapes)) {} |
| 61 | + |
| 62 | +absl::Status AddressComputationThunk::Prepare( |
| 63 | + const PrepareParams& params, ResourceRequests& resource_requests) { |
| 64 | + auto num_operands = embedded_thunk_operands_.size(); |
| 65 | + TF_RET_CHECK(num_operands == offset_buffer_indices_.size()); |
| 66 | + TF_RET_CHECK(num_operands == orig_shapes_.size()); |
| 67 | + TF_RET_CHECK(num_operands == sliced_shapes_.size()); |
| 68 | + for (unsigned i = 0; i < num_operands; ++i) { |
| 69 | + if (sliced_shapes_[i].has_value()) { |
| 70 | + TF_RET_CHECK(embedded_thunk_operands_[i].has_value()); |
| 71 | + TF_RET_CHECK(offset_buffer_indices_[i].has_value()); |
| 72 | + TF_RET_CHECK(sliced_shapes_[i]->IsArray()); |
| 73 | + TF_RET_CHECK(orig_shapes_[i].has_value() && orig_shapes_[i]->IsArray()); |
| 74 | + } |
| 75 | + } |
| 76 | + TF_RETURN_IF_ERROR(embedded_thunk_->Prepare(params, resource_requests)); |
| 77 | + return absl::OkStatus(); |
| 78 | +} |
| 79 | + |
| 80 | +absl::Status AddressComputationThunk::Initialize( |
| 81 | + const InitializeParams& params) { |
| 82 | + TF_RETURN_IF_ERROR(embedded_thunk_->Initialize(params)); |
| 83 | + |
| 84 | + unsigned num_offsets = 0; |
| 85 | + for (auto maybe_shape : sliced_shapes_) { |
| 86 | + num_offsets += (maybe_shape == std::nullopt) ? 1 : maybe_shape->rank(); |
| 87 | + } |
| 88 | + absl::MutexLock lock(&mutex_); |
| 89 | + if (auto it = offsets_.find(params.executor); it == offsets_.end()) { |
| 90 | + TF_ASSIGN_OR_RETURN( |
| 91 | + std::unique_ptr<se::MemoryAllocation> allocation, |
| 92 | + params.executor->HostMemoryAllocate(num_offsets * sizeof(int64_t))); |
| 93 | + offsets_.emplace(params.executor, std::move(allocation)); |
| 94 | + } |
| 95 | + |
| 96 | + return absl::OkStatus(); |
| 97 | +} |
| 98 | + |
| 99 | +absl::Status AddressComputationThunk::ExecuteOnStream( |
| 100 | + const ExecuteParams& params) { |
| 101 | + auto& stream = *params.stream; |
| 102 | + |
| 103 | + // Get memory allocation for copying offsets from device. |
| 104 | + int64_t* offsets_base = [&] { |
| 105 | + absl::MutexLock lock(&mutex_); |
| 106 | + return reinterpret_cast<int64_t*>(offsets_.at(stream.parent())->opaque()); |
| 107 | + }(); |
| 108 | + |
| 109 | + std::vector<se::DeviceMemoryBase> new_buffers; |
| 110 | + const BufferAllocations& orig_allocations = *params.buffer_allocations; |
| 111 | + for (unsigned i = 0; i < offset_buffer_indices_.size(); ++i) { |
| 112 | + if (embedded_thunk_operands_[i] == std::nullopt) { |
| 113 | + new_buffers.push_back(se::DeviceMemoryBase()); |
| 114 | + continue; |
| 115 | + } |
| 116 | + |
| 117 | + se::DeviceMemoryBase orig_operand = |
| 118 | + orig_allocations.GetDeviceAddress(*embedded_thunk_operands_[i]); |
| 119 | + if (offset_buffer_indices_[i] == std::nullopt) { |
| 120 | + new_buffers.push_back(orig_operand); |
| 121 | + continue; |
| 122 | + } |
| 123 | + |
| 124 | + se::DeviceMemoryBase offset_src = |
| 125 | + orig_allocations.GetDeviceAddress(*offset_buffer_indices_[i]); |
| 126 | + |
| 127 | + // Copy the ith offset from device to host. |
| 128 | + const Shape& src_shape = *orig_shapes_[i]; |
| 129 | + const Shape& dst_shape = *sliced_shapes_[i]; |
| 130 | + int64_t* offset_dst = &offsets_base[i]; |
| 131 | + TF_RETURN_IF_ERROR(stream.Memcpy(offset_dst, offset_src, |
| 132 | + dst_shape.rank() * sizeof(int64_t))); |
| 133 | + |
| 134 | + if (absl::Status blocked = stream.BlockHostUntilDone(); !blocked.ok()) { |
| 135 | + return absl::InternalError(absl::StrFormat( |
| 136 | + "Failed to retrieve all slice offset values on stream %p: %s", |
| 137 | + &stream, blocked.message())); |
| 138 | + } |
| 139 | + |
| 140 | + // Compute new slice. No need to copy the content to new buffers as we can |
| 141 | + // reuse the original buffers since slices are contiguous. |
| 142 | + TF_RET_CHECK(IsContiguousSlice(src_shape, dst_shape)); |
| 143 | + |
| 144 | + int64_t new_size = ShapeUtil::ByteSizeOf(dst_shape); |
| 145 | + BufferAllocation::Slice orig_slice = *embedded_thunk_operands_[i]; |
| 146 | + |
| 147 | + int64_t new_offset = orig_slice.offset(); |
| 148 | + std::vector<int64_t> slice_starts(offset_dst, |
| 149 | + offset_dst + dst_shape.rank()); |
| 150 | + for (auto [start, stride] : |
| 151 | + llvm::zip(slice_starts, *ShapeUtil::ByteStrides(src_shape))) { |
| 152 | + new_offset += start * stride; |
| 153 | + } |
| 154 | + |
| 155 | + new_buffers.push_back(orig_operand.GetByteSlice(new_offset, new_size)); |
| 156 | + } |
| 157 | + |
| 158 | + // TODO(vuson): handle DUS too. For now just copy the results over. |
| 159 | + for (auto result : embedded_thunk_results_) { |
| 160 | + if (result == std::nullopt) { |
| 161 | + new_buffers.push_back(se::DeviceMemoryBase()); |
| 162 | + } else { |
| 163 | + se::DeviceMemoryBase orig_result = |
| 164 | + orig_allocations.GetDeviceAddress(*result); |
| 165 | + new_buffers.push_back(orig_result); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + // Safe to create a local BufferAllocations here since buffers are only slices |
| 170 | + // of bigger ones allocated elsewhere. |
| 171 | + BufferAllocations new_allocations(new_buffers, |
| 172 | + orig_allocations.device_ordinal(), |
| 173 | + orig_allocations.memory_allocator(), |
| 174 | + orig_allocations.external_allocations()); |
| 175 | + |
| 176 | + Thunk::ExecuteParams new_params = |
| 177 | + Thunk::ExecuteParams::CloneWithNewAllocations(params, new_allocations); |
| 178 | + |
| 179 | + // Execute the underlying custom call thunk with the new buffers. |
| 180 | + TF_RETURN_IF_ERROR(embedded_thunk_->ExecuteOnStream(new_params)); |
| 181 | + |
| 182 | + return absl::OkStatus(); |
| 183 | +} |
| 184 | + |
| 185 | +} // namespace gpu |
| 186 | +} // namespace xla |
0 commit comments