-
Notifications
You must be signed in to change notification settings - Fork 343
/
pjrt_c_api_client.cc
1624 lines (1418 loc) · 63.2 KB
/
pjrt_c_api_client.cc
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright 2022 The TensorFlow Authors. All Rights Reserved.
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.
==============================================================================*/
#include "xla/pjrt/pjrt_c_api_client.h"
#include <functional>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>
#include "absl/cleanup/cleanup.h"
#include "absl/strings/string_view.h"
#include "mlir/Bytecode/BytecodeWriter.h" // from @llvm-project
#include "xla/hlo/ir/hlo_module.h"
#include "xla/pjrt/c/pjrt_c_api.h"
#include "xla/pjrt/c/pjrt_c_api_helpers.h"
#include "xla/pjrt/pjrt_api.h"
#include "xla/pjrt/pjrt_client.h"
#include "xla/pjrt/pjrt_executable.h"
#include "xla/pjrt/pjrt_future.h"
#include "xla/service/hlo.pb.h"
#include "xla/shape.h"
#include "xla/shape_util.h"
#include "xla/util.h"
#include "xla/xla_data.pb.h"
#include "tsl/platform/status.h"
namespace xla {
// Helper macros
// Return error status if not success and frees the PJRT_Error returned by
// `expr`.
#define RETURN_STATUS_IF_ERROR(expr, c_api) \
do { \
PJRT_Error* error = (expr); \
std::unique_ptr<PJRT_Error, pjrt::PJRT_ErrorDeleter> _error( \
error, pjrt::MakeErrorDeleter(c_api)); \
xla::Status _status = pjrt::PjrtErrorToStatus(_error.get(), c_api); \
if (!_status.ok()) { \
return _status; \
} \
} while (false)
// Return error future if not success and frees the PJRT_Error returned by
// `expr`.
#define RETURN_FUTURE_IF_ERROR(expr, c_api) \
do { \
PJRT_Error* error = (expr); \
std::unique_ptr<PJRT_Error, pjrt::PJRT_ErrorDeleter> _error( \
error, pjrt::MakeErrorDeleter(c_api)); \
xla::Status _status = pjrt::PjrtErrorToStatus(_error.get(), c_api); \
if (!_status.ok()) { \
return PjRtFuture<Status>(_status); \
} \
} while (false)
// ---------------------------------- Client -----------------------------------
PjRtCApiClient::PjRtCApiClient(const PJRT_Api* c_api, PJRT_Client* c_client)
: c_api_(c_api),
c_client_(std::unique_ptr<PJRT_Client, ::pjrt::PJRT_ClientDeleter>(
c_client, ::pjrt::MakeClientDeleter(c_api))),
// Example platform version string:
// PJRT C API
// TFRT TPU v2
// Built on Mar 4 2021 15:25:57 (1614900357) cl/360760169
platform_version_(absl::StrCat(
"PJRT C API\n", ::pjrt::GetPlatformVersion(c_client, c_api))) {
InitDevices();
LOG(INFO) << "PjRtCApiClient created.";
}
void PjRtCApiClient::InitDevices() {
PJRT_Client_Devices_Args devices_args;
devices_args.struct_size = PJRT_Client_Devices_Args_STRUCT_SIZE;
devices_args.priv = nullptr;
devices_args.client = c_client_.get();
pjrt::LogFatalIfPjrtError(c_api_->PJRT_Client_Devices(&devices_args), c_api_);
const size_t n = devices_args.num_devices;
c_to_cpp_device_map_.reserve(n);
owned_devices_.reserve(n);
devices_.reserve(n);
for (size_t i = 0; i < n; ++i) {
PJRT_Device* device = devices_args.devices[i];
std::unique_ptr<PjRtCApiDevice>& cpp_device = owned_devices_.emplace_back(
std::make_unique<PjRtCApiDevice>(device, this));
devices_.push_back(cpp_device.get());
c_to_cpp_device_map_[device] = cpp_device.get();
}
PJRT_Client_AddressableDevices_Args address_args;
address_args.struct_size = PJRT_Client_AddressableDevices_Args_STRUCT_SIZE;
address_args.priv = nullptr;
address_args.client = c_client_.get();
pjrt::LogFatalIfPjrtError(
c_api_->PJRT_Client_AddressableDevices(&address_args), c_api_);
const size_t m = address_args.num_addressable_devices;
addressable_devices_.reserve(m);
for (size_t i = 0; i < m; ++i) {
PJRT_Device* c_device = address_args.addressable_devices[i];
addressable_devices_.push_back(GetCppDevice(c_device));
}
}
int PjRtCApiClient::device_count() const { return devices_.size(); }
int PjRtCApiClient::addressable_device_count() const {
return addressable_devices_.size();
}
absl::Span<PjRtDevice* const> PjRtCApiClient::devices() const {
return devices_;
}
absl::Span<PjRtDevice* const> PjRtCApiClient::addressable_devices() const {
return addressable_devices_;
}
absl::string_view PjRtCApiClient::platform_name() const {
PJRT_Client_PlatformName_Args args;
args.client = c_client_.get();
args.struct_size = PJRT_Client_PlatformName_Args_STRUCT_SIZE;
args.priv = nullptr;
pjrt::LogFatalIfPjrtError(c_api_->PJRT_Client_PlatformName(&args), c_api_);
absl::string_view platform_name(args.platform_name, args.platform_name_size);
return platform_name;
}
int PjRtCApiClient::process_index() const {
PJRT_Client_ProcessIndex_Args process_index_args;
process_index_args.struct_size = PJRT_Client_ProcessIndex_Args_STRUCT_SIZE;
process_index_args.priv = nullptr;
process_index_args.client = c_client_.get();
pjrt::LogFatalIfPjrtError(
c_api_->PJRT_Client_ProcessIndex(&process_index_args), c_api_);
return process_index_args.process_index;
}
absl::string_view PjRtCApiClient::platform_version() const {
return platform_version_;
}
static DeviceAssignment CalculateDefaultAssignment(
int num_replicas, int num_partitions,
absl::Span<const int> device_assignment) {
DeviceAssignment cpp_device_assignment(num_replicas, num_partitions);
const int* iterator = device_assignment.begin();
for (int replica = 0; replica < num_replicas; ++replica) {
for (int partition = 0; partition < num_partitions; ++partition) {
cpp_device_assignment(replica, partition) = *(iterator++);
}
}
return cpp_device_assignment;
}
StatusOr<DeviceAssignment> PjRtCApiClient::GetDefaultDeviceAssignment(
int num_replicas, int num_partitions) const {
PJRT_Client_DefaultDeviceAssignment_Args args;
args.struct_size = PJRT_Client_DefaultDeviceAssignment_Args_STRUCT_SIZE;
args.priv = nullptr;
args.client = c_client_.get();
args.num_replicas = num_replicas;
args.num_partitions = num_partitions;
std::vector<int> assignment_buffer(num_replicas * num_partitions);
args.default_assignment_size = assignment_buffer.size();
args.default_assignment = assignment_buffer.data();
RETURN_STATUS_IF_ERROR(c_api_->PJRT_Client_DefaultDeviceAssignment(&args),
c_api_);
absl::Span<const int> param{args.default_assignment,
args.default_assignment_size};
return CalculateDefaultAssignment(args.num_replicas, args.num_partitions,
param);
}
StatusOr<std::optional<std::string>> PjRtCApiClient::ExecutableFingerprint(
const PjRtLoadedExecutable& executable) const {
return {std::nullopt};
}
StatusOr<PjRtDevice*> PjRtCApiClient::LookupDevice(int device_id) const {
PJRT_Client_LookupDevice_Args args;
args.struct_size = PJRT_Client_LookupDevice_Args_STRUCT_SIZE;
args.priv = nullptr;
args.client = c_client_.get();
args.id = device_id;
RETURN_STATUS_IF_ERROR(c_api_->PJRT_Client_LookupDevice(&args), c_api_);
return GetCppDevice(args.device);
}
StatusOr<PjRtDevice*> PjRtCApiClient::LookupAddressableDevice(
int local_hardware_id) const {
PJRT_Client_LookupAddressableDevice_Args args;
args.struct_size = PJRT_Client_LookupAddressableDevice_Args_STRUCT_SIZE;
args.priv = nullptr;
args.client = c_client_.get();
args.local_hardware_id = local_hardware_id;
RETURN_STATUS_IF_ERROR(c_api_->PJRT_Client_LookupAddressableDevice(&args),
c_api_);
return GetCppDevice(args.addressable_device);
}
// Initializes `PJRT_Client_Compile_Args`, which will be used to call
// API PJRT_Client_Compile().
static StatusOr<std::unique_ptr<PjRtLoadedExecutable>> InitializeArgsAndCompile(
PjRtCApiClient* api_client, const PJRT_Api* c_api, PJRT_Client* client,
const CompileOptions& options, const std::string& code,
const std::string& format) {
PJRT_Client_Compile_Args args;
args.struct_size = PJRT_Client_Compile_Args_STRUCT_SIZE;
args.priv = nullptr;
args.client = client;
TF_ASSIGN_OR_RETURN(const CompileOptionsProto options_proto,
options.ToProto());
std::string options_str = options_proto.SerializeAsString();
args.compile_options = options_str.c_str();
args.compile_options_size = options_str.size();
PJRT_Program program;
program.struct_size = PJRT_Program_STRUCT_SIZE;
program.priv = nullptr;
program.code = const_cast<char*>(code.c_str());
program.code_size = code.size();
program.format = format.c_str();
program.format_size = format.size();
args.program = &program;
RETURN_STATUS_IF_ERROR(c_api->PJRT_Client_Compile(&args), c_api);
std::unique_ptr<PjRtLoadedExecutable> ret =
std::make_unique<PjRtCApiLoadedExecutable>(api_client, args.executable);
return ret;
}
StatusOr<std::unique_ptr<PjRtLoadedExecutable>> PjRtCApiClient::Compile(
const XlaComputation& computation, CompileOptions options) {
std::string module_str = computation.proto().SerializeAsString();
std::string format(pjrt::kHloFormat);
return InitializeArgsAndCompile(this, c_api_, c_client_.get(), options,
module_str, format);
}
StatusOr<std::unique_ptr<PjRtLoadedExecutable>> PjRtCApiClient::Compile(
mlir::ModuleOp module, CompileOptions options) {
std::string module_bytecode;
{
llvm::raw_string_ostream os(module_bytecode);
if (mlir::failed(mlir::writeBytecodeToFile(module, os)))
return absl::UnknownError("writeBytecodeToFile() failed.");
}
std::string format(pjrt::kMlirFormat);
return InitializeArgsAndCompile(this, c_api_, c_client_.get(), options,
module_bytecode, format);
}
StatusOr<std::unique_ptr<PjRtLoadedExecutable>>
PjRtCApiClient::DeserializeExecutable(absl::string_view serialized,
std::optional<CompileOptions> options) {
PJRT_Executable_DeserializeAndLoad_Args des_args;
des_args.struct_size = PJRT_Executable_DeserializeAndLoad_Args_STRUCT_SIZE;
des_args.priv = nullptr;
des_args.client = c_client_.get();
des_args.serialized_executable = serialized.data();
des_args.serialized_executable_size = serialized.length();
const PJRT_Api* api = pjrt_c_api();
RETURN_STATUS_IF_ERROR(api->PJRT_Executable_DeserializeAndLoad(&des_args),
api);
PJRT_LoadedExecutable* c_exec = des_args.loaded_executable;
CHECK(c_exec != nullptr);
return std::unique_ptr<PjRtLoadedExecutable>(
std::make_unique<PjRtCApiLoadedExecutable>(this, c_exec));
}
StatusOr<std::uintptr_t> PjRtCApiClient::UnsafeBufferPointer(
PjRtBuffer* buffer) {
// Validate that the buffer's client matches the function call's client, since
// that could be a common error.
// Not doing input nullptr validation since such cases should be rare, and
// crashes should bubble up the call stack to higher layers. See b/248334153
// for the considerations that went into this.
if (buffer->client() != this) {
return InvalidArgument(
"buffer passed to PjRtCApiClient::UnsafeBufferPointer() is from a "
"different client than that of the function call. Buffer's client "
"platform: '%s', function call's client platform: '%s'.",
buffer->client()->platform_name(), this->platform_name());
}
PJRT_Buffer_UnsafePointer_Args args;
args.struct_size = PJRT_Buffer_UnsafePointer_Args_STRUCT_SIZE;
args.priv = nullptr;
args.buffer =
tensorflow::down_cast<const PjRtCApiBuffer*>(buffer)->c_buffer();
RETURN_STATUS_IF_ERROR(c_api_->PJRT_Buffer_UnsafePointer(&args), c_api_);
return args.buffer_pointer;
}
StatusOr<std::unique_ptr<PjRtBuffer>> PjRtCApiClient::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,
std::function<void()> on_done_with_host_buffer, PjRtDevice* device) {
if (host_buffer_semantics != HostBufferSemantics::kImmutableOnlyDuringCall &&
host_buffer_semantics != HostBufferSemantics::kZeroCopy &&
host_buffer_semantics !=
HostBufferSemantics::kImmutableUntilTransferCompletes) {
return Unimplemented(
"PJRT C API does not support HostBufferSemantics other than "
"HostBufferSemantics::kImmutableOnlyDuringCall, "
"HostBufferSemantics::kZeroCopy and "
"HostBufferSemantics::kImmutableUntilTransferCompletes.");
}
PJRT_Client_BufferFromHostBuffer_Args args;
args.struct_size = PJRT_Client_BufferFromHostBuffer_Args_STRUCT_SIZE;
args.priv = nullptr;
args.client = c_client_.get();
args.data = data;
args.type = ::pjrt::ConvertToPjRtBufferType(type);
args.dims = dims.data();
args.num_dims = dims.size();
if (byte_strides.has_value()) {
args.byte_strides = byte_strides.value().data();
args.num_byte_strides = byte_strides.value().size();
} else {
args.byte_strides = nullptr;
args.num_byte_strides = 0;
}
args.host_buffer_semantics =
::pjrt::ConvertToPjRtHostBufferSemantics(host_buffer_semantics);
args.device = tensorflow::down_cast<PjRtCApiDevice*>(device)->c_device();
RETURN_STATUS_IF_ERROR(c_api_->PJRT_Client_BufferFromHostBuffer(&args),
c_api_);
auto buffer = std::unique_ptr<PjRtBuffer>(
std::make_unique<PjRtCApiBuffer>(this, args.buffer));
std::unique_ptr<PJRT_Event, ::pjrt::PJRT_EventDeleter> event(
args.done_with_host_buffer, ::pjrt::MakeEventDeleter(c_api_));
if (on_done_with_host_buffer) {
PJRT_Event_OnReady_Args event_args;
event_args.struct_size = PJRT_Event_OnReady_Args_STRUCT_SIZE;
event_args.priv = nullptr;
event_args.event = event.get();
event_args.user_arg = new std::function<void(PJRT_Error*)>(
[on_done_with_host_buffer = std::move(on_done_with_host_buffer),
c_api = c_api_](PJRT_Error* error) {
if (error) {
::pjrt::MakeErrorDeleter(c_api)(error);
}
on_done_with_host_buffer();
});
event_args.callback = [](PJRT_Error* error, void* args) {
std::function<void(PJRT_Error*)>* on_done_with_host_buffer =
reinterpret_cast<std::function<void(PJRT_Error*)>*>(args);
(*on_done_with_host_buffer)(error);
delete on_done_with_host_buffer;
};
RETURN_STATUS_IF_ERROR(c_api_->PJRT_Event_OnReady(&event_args), c_api_);
}
return buffer;
}
const PJRT_Api* PjRtCApiClient::pjrt_c_api() const { return c_api_; }
// --------------------------------- Devices -----------------------------------
PjRtCApiDeviceDescription::PjRtCApiDeviceDescription(
const PJRT_Api* c_api, PJRT_DeviceDescription* device_description)
: c_api_(c_api), device_description_(device_description) {
InitAttributes();
}
int PjRtCApiDeviceDescription::id() const {
PJRT_DeviceDescription_Id_Args args;
args.struct_size = PJRT_DeviceDescription_Id_Args_STRUCT_SIZE;
args.priv = nullptr;
args.device_description = device_description_;
pjrt::LogFatalIfPjrtError(c_api_->PJRT_DeviceDescription_Id(&args), c_api_);
return args.id;
}
int PjRtCApiDeviceDescription::process_index() const {
PJRT_DeviceDescription_ProcessIndex_Args args;
args.struct_size = PJRT_DeviceDescription_ProcessIndex_Args_STRUCT_SIZE;
args.priv = nullptr;
args.device_description = device_description_;
pjrt::LogFatalIfPjrtError(c_api_->PJRT_DeviceDescription_ProcessIndex(&args),
c_api_);
return args.process_index;
}
void PjRtCApiDeviceDescription::InitAttributes() {
attributes_ = {};
PJRT_DeviceDescription_Attributes_Args args;
args.struct_size = PJRT_DeviceDescription_Attributes_Args_STRUCT_SIZE;
args.priv = nullptr;
args.device_description = device_description_;
pjrt::LogFatalIfPjrtError(c_api_->PJRT_DeviceDescription_Attributes(&args),
c_api_);
for (int i = 0; i < args.num_attributes; ++i) {
const auto& attribute = args.attributes[i];
std::string attribute_name(attribute.name, attribute.name_size);
switch (attribute.type) {
case PJRT_NamedValue_Type::PJRT_NamedValue_kString: {
std::string string_value(attribute.string_value, attribute.value_size);
attributes_[attribute_name] = PjRtDeviceAttribute(string_value);
break;
}
case PJRT_NamedValue_Type::PJRT_NamedValue_kInt64: {
attributes_[attribute_name] =
PjRtDeviceAttribute(attribute.int64_value);
break;
}
case PJRT_NamedValue_Type::PJRT_NamedValue_kInt64List: {
const int64_t* array_ptr(attribute.int64_array_value);
std::vector<int64_t> int64_array(array_ptr,
array_ptr + attribute.value_size);
attributes_[attribute_name] = PjRtDeviceAttribute(int64_array);
break;
}
// Do not allow other types (such as
// PJRT_NamedValue::PJRT_NamedValue_kFloat) since device attributes
// currently should not return other types. Also C API client currently
// does not support forward compatibility (such as if the underlying
// PJRT library is a newer version that returns types not supported by
// this client). Failing here to prevent undefined behavior.
default: {
LOG(FATAL) << "PJRT_DeviceDescription_Attributes() returned attribute '"
<< attribute_name << "' with unsupported type "
<< attribute.type
<< " to PjRtCApiDeviceDescription::InitAttributes()";
break;
}
}
}
}
const absl::flat_hash_map<std::string, PjRtDeviceAttribute>&
PjRtCApiDeviceDescription::Attributes() const {
return attributes_;
}
absl::string_view PjRtCApiDeviceDescription::device_kind() const {
PJRT_DeviceDescription_Kind_Args args;
args.struct_size = PJRT_DeviceDescription_Kind_Args_STRUCT_SIZE;
args.priv = nullptr;
args.device_description = device_description_;
pjrt::LogFatalIfPjrtError(c_api_->PJRT_DeviceDescription_Kind(&args), c_api_);
absl::string_view device_kind(args.device_kind, args.device_kind_size);
return device_kind;
}
absl::string_view PjRtCApiDeviceDescription::DebugString() const {
PJRT_DeviceDescription_DebugString_Args args;
args.struct_size = PJRT_DeviceDescription_DebugString_Args_STRUCT_SIZE;
args.priv = nullptr;
args.device_description = device_description_;
pjrt::LogFatalIfPjrtError(c_api_->PJRT_DeviceDescription_DebugString(&args),
c_api_);
absl::string_view debug_string(args.debug_string, args.debug_string_size);
return debug_string;
}
absl::string_view PjRtCApiDeviceDescription::ToString() const {
PJRT_DeviceDescription_ToString_Args args;
args.struct_size = PJRT_DeviceDescription_ToString_Args_STRUCT_SIZE;
args.priv = nullptr;
args.device_description = device_description_;
pjrt::LogFatalIfPjrtError(c_api_->PJRT_DeviceDescription_ToString(&args),
c_api_);
absl::string_view to_string(args.to_string, args.to_string_size);
return to_string;
}
PjRtCApiDevice::PjRtCApiDevice(PJRT_Device* device, PjRtCApiClient* client)
: client_(client),
device_(device),
description_(client->pjrt_c_api(),
pjrt::GetDeviceDescription(client->pjrt_c_api(), device)) {}
PjRtClient* PjRtCApiDevice::client() const { return client_; }
bool PjRtCApiDevice::IsAddressable() const {
PJRT_Device_IsAddressable_Args args;
args.struct_size = PJRT_Device_IsAddressable_Args_STRUCT_SIZE;
args.priv = nullptr;
args.device = device_;
const PJRT_Api* api = client_->pjrt_c_api();
pjrt::LogFatalIfPjrtError(api->PJRT_Device_IsAddressable(&args), api);
return args.is_addressable;
}
int PjRtCApiDevice::local_hardware_id() const {
PJRT_Device_LocalHardwareId_Args args;
args.struct_size = PJRT_Device_LocalHardwareId_Args_STRUCT_SIZE;
args.priv = nullptr;
args.device = device_;
const PJRT_Api* api = client_->pjrt_c_api();
pjrt::LogFatalIfPjrtError(api->PJRT_Device_LocalHardwareId(&args), api);
return args.local_hardware_id;
}
// ------------------------------- Executables ---------------------------------
PjRtCApiExecutable::PjRtCApiExecutable(const PJRT_Api* c_api,
PJRT_Executable* executable)
: c_api_(c_api),
executable_(executable, ::pjrt::MakeExecutableDeleter(c_api)) {}
absl::string_view PjRtCApiExecutable::name() const {
auto* c_api = pjrt_c_api();
auto* executable = c_executable();
PJRT_Executable_Name_Args args;
args.executable = executable;
args.struct_size = PJRT_Executable_Name_Args_STRUCT_SIZE;
args.priv = nullptr;
pjrt::LogFatalIfPjrtError(c_api->PJRT_Executable_Name(&args), c_api);
return absl::string_view(args.executable_name, args.executable_name_size);
}
int PjRtCApiExecutable::num_replicas() const {
auto* c_api = pjrt_c_api();
auto* executable = c_executable();
PJRT_Executable_NumReplicas_Args args;
args.executable = executable;
args.struct_size = PJRT_Executable_NumReplicas_Args_STRUCT_SIZE;
args.priv = nullptr;
pjrt::LogFatalIfPjrtError(c_api->PJRT_Executable_NumReplicas(&args), c_api);
return args.num_replicas;
}
int PjRtCApiExecutable::num_partitions() const {
auto* c_api = pjrt_c_api();
auto* executable = c_executable();
PJRT_Executable_NumPartitions_Args args;
args.executable = executable;
args.struct_size = PJRT_Executable_NumPartitions_Args_STRUCT_SIZE;
args.priv = nullptr;
pjrt::LogFatalIfPjrtError(c_api->PJRT_Executable_NumPartitions(&args), c_api);
return args.num_partitions;
}
int64_t PjRtCApiExecutable::SizeOfGeneratedCodeInBytes() const {
auto* c_api = pjrt_c_api();
auto* executable = c_executable();
PJRT_Executable_SizeOfGeneratedCodeInBytes_Args args;
args.struct_size =
PJRT_Executable_SizeOfGeneratedCodeInBytes_Args_STRUCT_SIZE;
args.priv = nullptr;
args.executable = executable;
pjrt::LogFatalIfPjrtError(
c_api->PJRT_Executable_SizeOfGeneratedCodeInBytes(&args), c_api);
return args.size_in_bytes;
}
StatusOr<std::vector<std::shared_ptr<HloModule>>>
PjRtCApiExecutable::GetHloModules() const {
auto* c_api = pjrt_c_api();
auto* executable = c_executable();
PJRT_Executable_OptimizedProgram_Args args;
args.struct_size = PJRT_Executable_OptimizedProgram_Args_STRUCT_SIZE;
args.priv = nullptr;
args.executable = executable;
PJRT_Program program;
program.struct_size = PJRT_Program_STRUCT_SIZE;
program.priv = nullptr;
program.code = nullptr;
args.program = &program;
RETURN_STATUS_IF_ERROR(c_api->PJRT_Executable_OptimizedProgram(&args), c_api);
constexpr size_t TWO_GIBIBYTES = 2ull * 1024 * 1024 * 1024;
const size_t code_size = args.program->code_size;
CHECK(code_size < TWO_GIBIBYTES);
std::string code(code_size, ' ');
args.program->code = code.data();
RETURN_STATUS_IF_ERROR(c_api->PJRT_Executable_OptimizedProgram(&args), c_api);
absl::string_view program_format(program.format, program.format_size);
if (program_format != ::pjrt::kHloWithConfigFormat) {
return xla::InternalError(
"expected program format `hlo_with_config` but got %s", program_format);
}
HloModuleProtoWithConfig proto;
proto.ParseFromString(code);
std::vector<std::shared_ptr<HloModule>> out;
TF_ASSIGN_OR_RETURN(std::unique_ptr<HloModule> module,
HloModule::CreateFromProtoWithConfig(proto));
out.push_back(std::move(module));
return out;
}
StatusOr<std::string> PjRtCApiExecutable::SerializeExecutable() const {
auto* c_api = pjrt_c_api();
auto* executable = c_executable();
PJRT_Executable_Serialize_Args ser_args;
ser_args.struct_size = PJRT_Executable_Serialize_Args_STRUCT_SIZE;
ser_args.priv = nullptr;
ser_args.executable = executable;
ser_args.serialized_executable = nullptr;
RETURN_STATUS_IF_ERROR(c_api->PJRT_Executable_Serialize(&ser_args), c_api);
PJRT_SerializedExecutable* c_serialized_exec = ser_args.serialized_executable;
std::unique_ptr<PJRT_SerializedExecutable,
::pjrt::PJRT_SerializedExecutableDeleter>
serialized_executable(c_serialized_exec,
::pjrt::MakeSerializedExecutableDeleter(c_api));
PJRT_SerializedExecutable_Data_Args data_args;
data_args.struct_size = PJRT_SerializedExecutable_Data_Args_STRUCT_SIZE;
data_args.priv = nullptr;
data_args.serialized_executable = c_serialized_exec;
data_args.data = nullptr;
data_args.data_size = 0;
RETURN_STATUS_IF_ERROR(c_api->PJRT_SerializedExecutable_Data(&data_args),
c_api);
return std::string(data_args.data, data_args.data_size);
}
// ------------------------ Loaded Executables ---------------------------------
PjRtCApiLoadedExecutable::PjRtCApiLoadedExecutable(
PjRtCApiClient* client, PJRT_LoadedExecutable* executable)
: client_(client),
loaded_executable_(executable, ::pjrt::MakeLoadedExecutableDeleter(
client->pjrt_c_api())) {
PJRT_LoadedExecutable_GetExecutable_Args args;
args.struct_size = PJRT_LoadedExecutable_GetExecutable_Args_STRUCT_SIZE;
args.priv = nullptr;
args.loaded_executable = c_loaded_executable();
args.executable = nullptr;
pjrt::LogFatalIfPjrtError(
pjrt_c_api()->PJRT_LoadedExecutable_GetExecutable(&args), pjrt_c_api());
executable_ =
std::make_unique<PjRtCApiExecutable>(pjrt_c_api(), args.executable);
InitDevices();
}
void PjRtCApiLoadedExecutable::InitDevices() {
PJRT_LoadedExecutable_AddressableDevices_Args args;
args.struct_size = PJRT_LoadedExecutable_AddressableDevices_Args_STRUCT_SIZE;
args.priv = nullptr;
args.executable = c_loaded_executable();
args.addressable_devices = nullptr;
args.num_addressable_devices = 0;
const PJRT_Api* api = pjrt_c_api();
pjrt::LogFatalIfPjrtError(
api->PJRT_LoadedExecutable_AddressableDevices(&args), api);
const size_t num_addressable_devices = args.num_addressable_devices;
addressable_devices_.reserve(num_addressable_devices);
for (size_t i = 0; i < num_addressable_devices; ++i) {
PJRT_Device* device = args.addressable_devices[i];
PjRtCApiDevice* c_api_device = client_->GetCppDevice(device);
addressable_devices_.push_back(c_api_device);
}
}
static std::vector<std::vector<PJRT_Buffer*>> Convert2DCppBuffersToCBuffers(
absl::Span<const std::vector<PjRtBuffer*>> cpp_lists) {
std::vector<std::vector<PJRT_Buffer*>> c_lists;
c_lists.reserve(cpp_lists.size());
for (const auto& cpp_list : cpp_lists) {
auto& c_list = c_lists.emplace_back();
c_list.reserve(cpp_list.size());
for (PjRtBuffer* buffer : cpp_list) {
auto* c_api_argument = tensorflow::down_cast<PjRtCApiBuffer*>(buffer);
c_list.push_back(c_api_argument->c_buffer());
}
}
return c_lists;
}
static std::vector<std::vector<std::unique_ptr<PjRtBuffer>>>
Convert2DCBuffersToCppBuffers(PJRT_Buffer*** c_lists, size_t outer_size,
int inner_size, xla::PjRtCApiClient* client) {
std::vector<std::vector<std::unique_ptr<PjRtBuffer>>> ret;
for (size_t i = 0; i < outer_size; ++i) {
auto& output_list = ret.emplace_back();
output_list.reserve(inner_size);
for (size_t j = 0; j < inner_size; ++j) {
output_list.push_back(
std::make_unique<PjRtCApiBuffer>(client, c_lists[i][j]));
}
}
return ret;
}
// Wraps original `xla::SendCallback` inside `PJRT_SendCallbackInfo` using
// 1) void* `user_arg` to capture `cpp_send_callback.callback` (std::function)
// 2) `PJRT_SendCallback` function pointer, which reinterprets and calls
// `user_arg` to call `cpp_send_callback.callback` function. This appends to
// `send_callback_functions`, which must be kept alive for as lnog as the
// returned PJRT_SendCallbackInfo is needed.
//
// TODO(yeounoh) move this to pjrt_c_api_helpers after implementing C API for
// the opaque types `PJRT_Chunk` and `PJRT_CopyToDeviceStream`.
PJRT_SendCallbackInfo CppSendCallbackToC(
const xla::SendCallback& cpp_send_callback,
PjRtCApiLoadedExecutable::SendCallbackFunction* send_callback_function) {
*send_callback_function =
[&send_callback = cpp_send_callback.callback](
PJRT_Chunk* chunk, PJRT_CallbackError* callback_error,
size_t total_size_in_bytes, bool done) -> PJRT_Error* {
// PJRT C API doesn't support
// use_major_to_minor_data_layout_for_callbacks = false
xla::Shape dummy_shape;
xla::Status status = send_callback(xla::PjRtTransferMetadata{dummy_shape},
::pjrt::ConvertToCppChunk(*chunk),
total_size_in_bytes, done);
if (!status.ok()) {
absl::string_view message = status.message();
return (*callback_error)(pjrt::StatusCodeToPjrtErrorCode(status.code()),
message.data(), message.size());
}
return nullptr;
};
return PJRT_SendCallbackInfo{
/*channel_id=*/cpp_send_callback.channel_id,
/*user_arg=*/send_callback_function,
/*send_callback=*/
[](PJRT_Chunk* chunk, PJRT_CallbackError* callback_error,
size_t total_size_in_bytes, bool done, void* user_arg) -> PJRT_Error* {
// PJRT_SendCallback, `send_callback` is internal C interface callback
// representation that cpatures the client C++ callback in void*
// `user_arg` and reinterprets in the lower-level runtime for execution.
// `user_arg` captures `send_callback_function` which is
// SendCallbackFunction*.
PjRtCApiLoadedExecutable::SendCallbackFunction* send_callback =
reinterpret_cast<PjRtCApiLoadedExecutable::SendCallbackFunction*>(
user_arg);
return (*send_callback)(chunk, callback_error, total_size_in_bytes,
done);
}};
}
CApiCopyToDeviceStream::CApiCopyToDeviceStream(
PJRT_CopyToDeviceStream* c_stream, const PJRT_Api* c_api)
: CopyToDeviceStream(/*total_bytes=*/0, /*granule_bytes=*/0),
c_stream_(c_stream),
c_api_(c_api) {
PJRT_CopyToDeviceStream_TotalBytes_Args total_bytes_args;
total_bytes_args.struct_size =
PJRT_CopyToDeviceStream_TotalBytes_Args_STRUCT_SIZE;
total_bytes_args.priv = nullptr;
total_bytes_args.stream = c_stream_;
pjrt::LogFatalIfPjrtError(
c_api_->PJRT_CopyToDeviceStream_TotalBytes(&total_bytes_args), c_api_);
total_bytes_ = total_bytes_args.total_bytes;
PJRT_CopyToDeviceStream_GranuleSize_Args granule_size_args;
granule_size_args.struct_size =
PJRT_CopyToDeviceStream_GranuleSize_Args_STRUCT_SIZE;
granule_size_args.priv = nullptr;
granule_size_args.stream = c_stream_;
pjrt::LogFatalIfPjrtError(
c_api_->PJRT_CopyToDeviceStream_GranuleSize(&granule_size_args), c_api_);
granule_bytes_ = granule_size_args.granule_size_in_bytes;
}
PjRtFuture<Status> CApiCopyToDeviceStream::AddChunk(PjRtChunk chunk) {
PJRT_Chunk c_chunk = ::pjrt::ConvertFromCppChunk(std::move(chunk));
PJRT_CopyToDeviceStream_AddChunk_Args add_chunk_args;
add_chunk_args.struct_size =
PJRT_CopyToDeviceStream_AddChunk_Args_STRUCT_SIZE;
add_chunk_args.priv = nullptr;
add_chunk_args.stream = c_stream_;
add_chunk_args.chunk = &c_chunk;
PJRT_CopyToDeviceStream_CurrentBytes_Args current_bytes_args;
current_bytes_args.struct_size =
PJRT_CopyToDeviceStream_CurrentBytes_Args_STRUCT_SIZE;
current_bytes_args.priv = nullptr;
current_bytes_args.stream = c_stream_;
{
absl::MutexLock lock(&mu_);
RETURN_FUTURE_IF_ERROR(
c_api_->PJRT_CopyToDeviceStream_AddChunk(&add_chunk_args), c_api_);
RETURN_FUTURE_IF_ERROR(
c_api_->PJRT_CopyToDeviceStream_CurrentBytes(¤t_bytes_args),
c_api_);
current_bytes_ = current_bytes_args.current_bytes;
}
CHECK(add_chunk_args.transfer_complete != nullptr);
return ::pjrt::ConvertCEventToCppFuture(add_chunk_args.transfer_complete,
c_api_);
}
// Wraps original `xla::RecvCallback` inside `PJRT_RecvCallbackInfo` using
// 1) void* `user_arg` to capture `cpp_recv_callback.callback` (std::function)
// 2) `PJRT_RecvCallback` function pointer, which reinterprets and calls
// `user_arg` to call `cpp_send_callback.callback` function. This appends to
// `recv_callback_functions`, which must be kept alive for as lnog as the
// returned PJRT_RecvCallbackInfo is needed.
//
// TODO(yeounoh) move this to pjrt_c_api_helpers after implementing C API for
// the opaque types `PJRT_Chunk` and `PJRT_CopyToDeviceStream`.
PJRT_RecvCallbackInfo CppRecvCallbackToC(
const xla::RecvCallback& cpp_recv_callback, const PJRT_Api* c_api,
PjRtCApiLoadedExecutable::RecvCallbackFunction* recv_callback_function) {
*recv_callback_function = [&recv_callback = cpp_recv_callback.callback,
c_api](PJRT_CopyToDeviceStream* stream) {
// PJRT C API doesn't support
// use_major_to_minor_data_layout_for_callbacks = false
xla::Shape dummy_shape;
recv_callback(xla::PjRtTransferMetadata{dummy_shape},
std::make_unique<CApiCopyToDeviceStream>(stream, c_api));
};
return PJRT_RecvCallbackInfo{
/*channel_id=*/cpp_recv_callback.channel_id,
/*user_arg=*/recv_callback_function,
/*recv_callback=*/
[](PJRT_CopyToDeviceStream* stream, void* user_arg) {
// PJRT_RecvCallback, `recv_callback` is internal C interface callback
// representation that cpatures the client C++ callback in void*
// `user_arg` and reinterprets in the lower-level runtime for execution.
// `user_arg` captures `recv_callback_function` which is
// RecvCallbackFunction*.
PjRtCApiLoadedExecutable::RecvCallbackFunction* recv_callback =
reinterpret_cast<PjRtCApiLoadedExecutable::RecvCallbackFunction*>(
user_arg);
(*recv_callback)(stream);
}};
}
static void CppSendCallbackListsToC(
absl::Span<const std::vector<xla::SendCallback>> cpp_lists,
std::vector<PjRtCApiLoadedExecutable::SendCallbackFunction>&
send_callback_functions,
std::vector<std::vector<PJRT_SendCallbackInfo>>& c_lists) {
if (cpp_lists.empty()) return;
send_callback_functions.resize(cpp_lists.size() * cpp_lists[0].size());
c_lists.reserve(cpp_lists.size());
int func_count = 0;
for (const std::vector<xla::SendCallback>& cpp_list : cpp_lists) {
std::vector<PJRT_SendCallbackInfo>& c_list = c_lists.emplace_back();
c_list.reserve(cpp_list.size());
for (const xla::SendCallback& cpp_callback : cpp_list) {
c_list.emplace_back(CppSendCallbackToC(
cpp_callback, &send_callback_functions[func_count++]));
}
}
}
static void CppRecvCallbackListsToC(
absl::Span<const std::vector<xla::RecvCallback>> cpp_lists,
const PJRT_Api* c_api,
std::vector<PjRtCApiLoadedExecutable::RecvCallbackFunction>&
recv_callback_functions,
std::vector<std::vector<PJRT_RecvCallbackInfo>>& c_lists) {
if (cpp_lists.empty()) return;
recv_callback_functions.resize(cpp_lists.size() * cpp_lists[0].size());
c_lists.reserve(cpp_lists.size());
int func_count = 0;
for (const auto& cpp_list : cpp_lists) {
std::vector<PJRT_RecvCallbackInfo>& c_list = c_lists.emplace_back();
c_list.reserve(cpp_list.size());
for (const auto& cpp_callback : cpp_list) {
c_list.emplace_back(CppRecvCallbackToC(
cpp_callback, c_api, &recv_callback_functions[func_count++]));
}
}
}
xla::StatusOr<PJRT_LoadedExecutable_Execute_Args>
PjRtCApiLoadedExecutable::GetCommonExecuteArgs(
absl::Span<const std::vector<PjRtBuffer*>> argument_handles,
const ExecuteOptions& options, PJRT_ExecuteOptions& c_options,
std::vector<std::vector<PJRT_Buffer*>>& c_argument_lists_storage,
std::vector<PJRT_Buffer**>& c_arguments,
std::vector<std::vector<PJRT_Buffer*>>& c_output_lists_storage,
std::vector<PJRT_Buffer**>& c_output_lists,
std::optional<std::vector<PJRT_Event*>>& device_complete_events,
SendRecvCallbackData& callback_data) {
bool using_host_callbacks =
!options.send_callbacks.empty() || !options.recv_callbacks.empty();
if (using_host_callbacks &&
!options.use_major_to_minor_data_layout_for_callbacks) {
return Unimplemented(
"PJRT C API doesn't support "
"ExecuteOptions::use_major_to_minor_data_layout_for_callbacks = false");
}
PJRT_LoadedExecutable_Execute_Args args;
args.struct_size = PJRT_LoadedExecutable_Execute_Args_STRUCT_SIZE;
args.priv = nullptr;
args.executable = c_loaded_executable();
args.options = &c_options;
args.options->struct_size = PJRT_ExecuteOptions_STRUCT_SIZE;
args.options->launch_id = options.launch_id;
args.num_devices = argument_handles.size();
CHECK_GT(args.num_devices, 0);
args.num_args = argument_handles[0].size();
if (device_complete_events.has_value() || using_host_callbacks) {
device_complete_events->resize(args.num_devices);
args.device_complete_events = device_complete_events->data();
} else {
args.device_complete_events = nullptr;
}
// Populates `args.argument_lists` from `argument_handles`.
c_argument_lists_storage = Convert2DCppBuffersToCBuffers(argument_handles);
c_arguments.reserve(c_argument_lists_storage.size());
for (auto& argument_list : c_argument_lists_storage) {
c_arguments.push_back(argument_list.data());
}
args.argument_lists = c_arguments.data();
// Allocates memory for output. `c_buffer_lists_storage` and `c_buffer_lists`
// needs to stay alive during the call of `PJRT_LoadedExecutable_Execute`.
PJRT_Executable_NumOutputs_Args numoutputs_args;
numoutputs_args.struct_size = PJRT_Executable_NumOutputs_Args_STRUCT_SIZE;
numoutputs_args.priv = nullptr;
numoutputs_args.executable = c_executable();
RETURN_STATUS_IF_ERROR(
pjrt_c_api()->PJRT_Executable_NumOutputs(&numoutputs_args), pjrt_c_api());
size_t outer_size = args.num_devices;
size_t inner_size = numoutputs_args.num_outputs;
c_output_lists_storage.resize(outer_size);
c_output_lists.resize(outer_size);
for (int i = 0; i < outer_size; ++i) {
c_output_lists_storage[i].resize(inner_size);
c_output_lists[i] = c_output_lists_storage[i].data();
}
args.output_lists = c_output_lists.data();
// Allocates memory for callbacks. `callback_data` needs to stay alive during
// the execution.
if (!options.send_callbacks.empty()) {
CppSendCallbackListsToC(options.send_callbacks,
callback_data.send_callback_functions,
callback_data.c_send_callbacks);
for (auto& c_send_callback_list : callback_data.c_send_callbacks) {
callback_data.c_send_callback_lists.push_back(
c_send_callback_list.data());
}
args.options->send_callbacks = callback_data.c_send_callback_lists.data();
args.options->num_send_ops = options.send_callbacks[0].size();
}
if (!options.recv_callbacks.empty()) {
CppRecvCallbackListsToC(options.recv_callbacks, pjrt_c_api(),
callback_data.recv_callback_functions,
callback_data.c_recv_callbacks);
for (auto& c_recv_callback_list : callback_data.c_recv_callbacks) {
callback_data.c_recv_callback_lists.push_back(
c_recv_callback_list.data());
}
args.options->recv_callbacks = callback_data.c_recv_callback_lists.data();