-
Notifications
You must be signed in to change notification settings - Fork 74k
/
tf_pjrt_client.h
398 lines (362 loc) · 15.7 KB
/
tf_pjrt_client.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/* Copyright 2023 The OpenXLA Authors.
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.
==============================================================================*/
#ifndef XLA_PJRT_TF_PJRT_CLIENT_H_
#define XLA_PJRT_TF_PJRT_CLIENT_H_
#include <functional>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>
#include "absl/base/thread_annotations.h"
#include "absl/container/flat_hash_map.h"
#include "absl/container/flat_hash_set.h"
#include "absl/synchronization/mutex.h"
#include "xla/pjrt/pjrt_client.h"
#include "xla/pjrt/pjrt_future.h"
#include "tsl/platform/errors.h"
namespace xla {
class TfPjRtClient;
// Wrapper for PjRtBuffer that translates the device.
class TfPjRtBuffer : public PjRtBuffer {
public:
TfPjRtBuffer(TfPjRtClient* client, std::unique_ptr<PjRtBuffer> wrapped);
~TfPjRtBuffer() override;
PjRtBuffer* wrapped() const { return wrapped_.get(); }
const Shape& on_device_shape() const override {
return wrapped_->on_device_shape();
}
StatusOr<Shape> logical_on_device_shape() override {
return wrapped_->logical_on_device_shape();
}
PjRtMemorySpace* memory_space() const override {
return wrapped_->memory_space();
}
PjRtDevice* device() const override { return wrapped_->device(); }
PjRtClient* client() const override;
StatusOr<std::unique_ptr<ExternalReference>> AcquireExternalReference()
override {
return wrapped_->AcquireExternalReference();
}
PjRtFuture<> ToLiteral(MutableLiteralBase* literal) override {
return wrapped_->ToLiteral(literal);
}
PjRtFuture<> LazyToLiteral(
absl::AnyInvocable<absl::StatusOr<MutableLiteralBase*>() &&> generator)
override {
return wrapped_->LazyToLiteral(std::move(generator));
}
StatusOr<size_t> GetOnDeviceSizeInBytes() const override {
return wrapped_->GetOnDeviceSizeInBytes();
}
PjRtFuture<> CopyRawToHost(void* dst, int64_t offset,
int64_t transfer_size) override {
return wrapped_->CopyRawToHost(dst, offset, transfer_size);
}
void Delete() override { wrapped_->Delete(); }
StatusOr<std::unique_ptr<ExternalReference>> ReleaseDeviceMemoryOwnership(
bool wait_for_operations_to_complete) override {
return wrapped_->ReleaseDeviceMemoryOwnership(
wait_for_operations_to_complete);
}
bool IsDeleted() override { return wrapped_->IsDeleted(); }
StatusOr<std::unique_ptr<PjRtBuffer>> CopyToDevice(
PjRtDevice* dst_device) override;
StatusOr<std::unique_ptr<PjRtBuffer>> CopyToMemorySpace(
PjRtMemorySpace* dst_memory_space) override {
return Unimplemented("CopyToMemorySpace not implemented");
}
void CopyToRemoteDevice(
PjRtFuture<StatusOr<std::string>> serialized_descriptor,
RemoteSendCallback on_done) override {
wrapped_->CopyToRemoteDevice(std::move(serialized_descriptor),
std::move(on_done));
}
void CopyToRemoteDeviceScattered(
PjRtFuture<StatusOr<std::vector<std::string>>> serialized_descriptors,
std::vector<RemoteSendCallback> callbacks,
const ScatterDetails& scatter_details) override {
return wrapped_->CopyToRemoteDeviceScattered(
std::move(serialized_descriptors), std::move(callbacks),
scatter_details);
}
PjRtFuture<> GetReadyFuture() override { return wrapped_->GetReadyFuture(); }
bool IsOnCpu() const override { return wrapped_->IsOnCpu(); }
// Not thread-safe. The caller should promises to have some external
// synchronization that ensures that all uses of the buffer have completed
// (and a thread synchronization has occurred that involves all the necessary
// memory barriers) before this method is called.
void DestroyWrappedBuffer() { wrapped_.reset(nullptr); }
private:
TfPjRtClient* client_;
std::unique_ptr<PjRtBuffer> wrapped_;
};
// Wrapper for PjRtLoadedExecutable that wraps and unwraps argument and result
// buffers.
class TfPjRtExecutable : public PjRtLoadedExecutable {
public:
TfPjRtExecutable(TfPjRtClient* client,
std::unique_ptr<PjRtLoadedExecutable> wrapped);
PjRtLoadedExecutable* wrapped() const { return wrapped_.get(); }
PjRtClient* client() const override;
absl::string_view name() const override { return wrapped_->name(); }
int num_replicas() const override { return wrapped_->num_replicas(); }
int num_partitions() const override { return wrapped_->num_partitions(); }
int64_t SizeOfGeneratedCodeInBytes() const override {
return wrapped_->SizeOfGeneratedCodeInBytes();
}
const DeviceAssignment& device_assignment() const override {
return wrapped_->device_assignment();
}
absl::Span<const LogicalDeviceIds> addressable_device_logical_ids()
const override {
return wrapped_->addressable_device_logical_ids();
}
absl::Span<PjRtDevice* const> addressable_devices() const override {
return wrapped_->addressable_devices();
}
StatusOr<std::vector<std::shared_ptr<HloModule>>> GetHloModules()
const override {
return wrapped_->GetHloModules();
}
StatusOr<std::vector<std::vector<absl::string_view>>> GetOutputMemoryKinds()
const override {
return wrapped_->GetOutputMemoryKinds();
}
using PjRtLoadedExecutable::Execute;
StatusOr<std::vector<std::vector<std::unique_ptr<PjRtBuffer>>>> Execute(
absl::Span<const std::vector<PjRtBuffer*>> argument_handles,
const ExecuteOptions& options,
std::optional<std::vector<PjRtFuture<Status>>>& returned_futures)
override;
using PjRtLoadedExecutable::ExecuteSharded;
StatusOr<std::vector<std::unique_ptr<PjRtBuffer>>> ExecuteSharded(
absl::Span<PjRtBuffer* const> argument_handles, PjRtDevice* device,
const ExecuteOptions& options,
std::optional<PjRtFuture<Status>>& returned_future,
bool fill_future) override;
using PjRtLoadedExecutable::ExecutePortable;
StatusOr<std::vector<std::unique_ptr<PjRtBuffer>>> ExecutePortable(
absl::Span<PjRtBuffer* const> argument_handles, PjRtDevice* device,
const ExecuteOptions& options,
std::optional<PjRtFuture<Status>>& returned_future,
bool fill_future) override;
void Delete() override { return wrapped_->Delete(); }
bool IsDeleted() override { return wrapped_->IsDeleted(); }
bool IsReturnedFutureSupported() const override {
return wrapped_->IsReturnedFutureSupported();
}
StatusOr<std::string> SerializeExecutable() const override {
return wrapped_->SerializeExecutable();
}
StatusOr<struct CompileOptions> GetCompileOptions() const override {
return wrapped_->GetCompileOptions();
}
StatusOr<std::string> FingerprintExecutable() const override {
return wrapped_->FingerprintExecutable();
}
private:
TfPjRtClient* client_;
std::unique_ptr<PjRtLoadedExecutable> wrapped_;
};
// A thin wrapper of PjRtClient that includes management of PjRtBuffer it
// creates.
class TfPjRtClient : public PjRtClient {
public:
static std::unique_ptr<TfPjRtClient> CreateTfPjRtClient(
std::unique_ptr<PjRtClient> wrapped);
explicit TfPjRtClient(std::unique_ptr<PjRtClient> wrapped);
~TfPjRtClient() override;
int process_index() const override { return wrapped_->process_index(); }
int device_count() const override { return wrapped_->device_count(); }
int addressable_device_count() const override {
return wrapped_->addressable_device_count();
}
absl::Span<PjRtDevice* const> devices() const override {
return wrapped_->devices();
}
absl::Span<PjRtDevice* const> addressable_devices() const override {
return wrapped_->addressable_devices();
}
StatusOr<PjRtDevice*> LookupDevice(int device_id) const override {
return LookupDevice(PjRtGlobalDeviceId(device_id));
}
StatusOr<PjRtDevice*> LookupDevice(
PjRtGlobalDeviceId global_device_id) const override {
return wrapped_->LookupDevice(global_device_id.value());
}
StatusOr<PjRtDevice*> LookupAddressableDevice(
int local_hardware_id) const override {
return LookupAddressableDevice(PjRtLocalDeviceId(local_hardware_id));
}
StatusOr<PjRtDevice*> LookupAddressableDevice(
PjRtLocalDeviceId local_device_id) const override {
if (wrapped_ == nullptr) {
return tsl::errors::Internal(
"Wrapped PJRT client in TfPjRtClient is already destroyed.");
}
return wrapped_->LookupAddressableDevice(local_device_id);
}
absl::Span<PjRtMemorySpace* const> memory_spaces() const override {
return wrapped_->memory_spaces();
}
PjRtPlatformId platform_id() const override {
return wrapped_->platform_id();
}
absl::string_view platform_name() const override {
return wrapped_->platform_name();
}
absl::string_view platform_version() const override {
return wrapped_->platform_version();
}
PjRtRuntimeType runtime_type() const override {
return wrapped_->runtime_type();
}
StatusOr<DeviceAssignment> GetDefaultDeviceAssignment(
int num_replicas, int num_partitions) const override {
return wrapped_->GetDefaultDeviceAssignment(num_replicas, num_partitions);
}
StatusOr<Layout> GetDefaultLayout(PrimitiveType element_type,
absl::Span<const int64_t> dims) override {
return wrapped_->GetDefaultLayout(element_type, dims);
}
StatusOr<std::unique_ptr<HloCostAnalysis>> GetHloCostAnalysis()
const override {
return wrapped_->GetHloCostAnalysis();
}
StatusOr<std::unique_ptr<PjRtLoadedExecutable>> Compile(
const XlaComputation& computation, CompileOptions options) override {
return WrapExecutable(wrapped_->Compile(computation, options));
}
StatusOr<std::unique_ptr<PjRtLoadedExecutable>> Compile(
mlir::ModuleOp module, CompileOptions options) override {
return WrapExecutable(wrapped_->Compile(std::move(module), options));
}
StatusOr<std::unique_ptr<PjRtLoadedExecutable>> DeserializeExecutable(
absl::string_view serialized,
std::optional<CompileOptions> options) override {
return WrapExecutable(wrapped_->DeserializeExecutable(serialized, options));
}
StatusOr<std::unique_ptr<PjRtBuffer>> CreateUninitializedBuffer(
const Shape& shape, PjRtDevice* device) override {
return Unimplemented(
"CreateUninitializedBuffer not supported for TfPjRtClient.");
}
StatusOr<std::unique_ptr<AsyncHostToDeviceTransferManager>>
CreateBuffersForAsyncHostToDevice(absl::Span<const Shape> shapes,
PjRtDevice* device) override {
return Unimplemented(
"AsyncHostToDeviceTransferManager not supported for Tf.");
}
StatusOr<std::unique_ptr<AsyncHostToDeviceTransferManager>>
CreateBuffersForAsyncHostToDevice(absl::Span<const Shape> shapes,
PjRtMemorySpace* memory_space) override {
return Unimplemented(
"AsyncHostToDeviceTransferManager not supported for Tf.");
}
StatusOr<std::unique_ptr<PjRtBuffer>> BufferFromHostBuffer(
const void* data, PrimitiveType type, absl::Span<int64_t const> dims,
std::optional<absl::Span<int64_t const>> byte_strides,
HostBufferSemantics host_buffer_semantics,
absl::AnyInvocable<void() &&> on_done_with_host_buffer,
PjRtDevice* device) override {
return WrapBuffer(wrapped_->BufferFromHostBuffer(
data, type, dims, byte_strides, host_buffer_semantics,
std::move(on_done_with_host_buffer), device));
}
StatusOr<std::unique_ptr<PjRtBuffer>> BufferFromHostBuffer(
const void* data, PrimitiveType type, absl::Span<int64_t const> dims,
std::optional<absl::Span<int64_t const>> byte_strides,
HostBufferSemantics host_buffer_semantics,
absl::AnyInvocable<void() &&> on_done_with_host_buffer,
PjRtDevice* device, const Layout* device_layout) override {
return WrapBuffer(wrapped_->BufferFromHostBuffer(
data, type, dims, byte_strides, host_buffer_semantics,
std::move(on_done_with_host_buffer), device, device_layout));
}
StatusOr<std::unique_ptr<PjRtBuffer>> BufferFromHostLiteral(
const LiteralSlice& literal, PjRtDevice* device) override {
return WrapBuffer(wrapped_->BufferFromHostLiteral(literal, device));
}
StatusOr<std::unique_ptr<PjRtBuffer>> CreateViewOfDeviceBuffer(
void* device_ptr, const Shape& shape, PjRtDevice* device,
std::function<void()> on_delete_callback,
std::optional<std::intptr_t> stream) override {
return WrapBuffer(wrapped_->CreateViewOfDeviceBuffer(
device_ptr, shape, device, on_delete_callback, stream));
}
StatusOr<std::uintptr_t> UnsafeBufferPointer(PjRtBuffer* buffer) override {
return wrapped_->UnsafeBufferPointer(UnwrapBuffer(buffer));
}
StatusOr<std::vector<std::unique_ptr<PjRtBuffer>>>
MakeCrossHostReceiveBuffers(absl::Span<const Shape> shapes,
PjRtDevice* device,
PjRtCrossHostRecvNotifier notifier) override {
return wrapped_->MakeCrossHostReceiveBuffers(shapes, device,
std::move(notifier));
}
StatusOr<std::vector<std::unique_ptr<PjRtBuffer>>>
MakeCrossHostReceiveBuffersForGather(
absl::Span<const Shape> shapes, std::vector<GatherDetails> gather_details,
PjRtDevice* device, PjRtCrossHostRecvNotifier notifier) override {
return wrapped_->MakeCrossHostReceiveBuffersForGather(
shapes, std::move(gather_details), device, std::move(notifier));
}
StatusOr<ChannelHandle> CreateChannelHandle() override {
return wrapped_->CreateChannelHandle();
}
StatusOr<ChannelHandle> CreateDeviceToHostChannelHandle() override {
return wrapped_->CreateDeviceToHostChannelHandle();
}
StatusOr<ChannelHandle> CreateHostToDeviceChannelHandle() override {
return wrapped_->CreateHostToDeviceChannelHandle();
}
StatusOr<const PjRtTopologyDescription*> GetTopologyDescription()
const override {
return wrapped_->GetTopologyDescription();
}
Status Defragment() override { return wrapped_->Defragment(); }
PjRtClient* wrapped() const { return wrapped_.get(); }
StatusOr<std::unique_ptr<PjRtBuffer>> WrapBuffer(
StatusOr<std::unique_ptr<PjRtBuffer>> to_wrap);
// Tracks a non-owning pointer of TfPjRtBuffer in TfPjRtClient.
void TrackBuffer(TfPjRtBuffer* buffer);
// Untracks a TfPjRtBuffer in TfPjRtClient.
void UntrackBuffer(const TfPjRtBuffer* buffer);
// Destroy all the wrapped PjRtBuffer in the tracked set of TfPjRtBuffers and
// then destroy the wrapped PjRtClient.
void DestroyWrappedBuffersAndClient();
private:
// Unwraps a TfPjRtBuffer.
PjRtBuffer* UnwrapBuffer(PjRtBuffer* buffer) const {
return tensorflow::down_cast<TfPjRtBuffer*>(buffer)->wrapped();
}
// Unwraps a TfPjRtExecutable.
const PjRtLoadedExecutable& UnwrapExecutable(
const PjRtLoadedExecutable& executable) const {
return *tensorflow::down_cast<const TfPjRtExecutable*>(&executable)
->wrapped();
}
StatusOr<std::unique_ptr<PjRtLoadedExecutable>> WrapExecutable(
StatusOr<std::unique_ptr<PjRtLoadedExecutable>> to_wrap);
std::unique_ptr<PjRtClient> wrapped_;
absl::flat_hash_map<int, int> mutex_id_from_device_id_;
// Depending on `sizeof(absl::flat_hash_set<TfPjRtBuffer*>)`, might need to
// add some padding to the struct.
struct DeviceBuffers {
absl::Mutex mu;
absl::flat_hash_set<TfPjRtBuffer*> alive_buffers ABSL_GUARDED_BY(mu);
};
std::vector<DeviceBuffers> alive_buffers_;
};
} // namespace xla
#endif // XLA_PJRT_TF_PJRT_CLIENT_H_