-
Notifications
You must be signed in to change notification settings - Fork 784
Expand file tree
/
Copy pathhlo_instructions.h
More file actions
3008 lines (2561 loc) · 121 KB
/
hlo_instructions.h
File metadata and controls
3008 lines (2561 loc) · 121 KB
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 2018 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.
==============================================================================*/
// All HloInstruction subclasses are put in this file.
#ifndef XLA_HLO_IR_HLO_INSTRUCTIONS_H_
#define XLA_HLO_IR_HLO_INSTRUCTIONS_H_
#include <cstdint>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>
#include "absl/base/attributes.h"
#include "absl/container/inlined_vector.h"
#include "absl/functional/function_ref.h"
#include "absl/hash/hash.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
#include "absl/synchronization/mutex.h"
#include "absl/types/span.h"
#include "xla/comparison_util.h"
#include "xla/hlo/ir/hlo_casting_utils.h"
#include "xla/hlo/ir/hlo_clone_context.h"
#include "xla/hlo/ir/hlo_computation.h"
#include "xla/hlo/ir/hlo_domain_metadata.h"
#include "xla/hlo/ir/hlo_instruction.h"
#include "xla/hlo/ir/hlo_opcode.h"
#include "xla/hlo/ir/hlo_print_options.h"
#include "xla/hlo/ir/replica_group.h"
#include "xla/layout.h"
#include "xla/literal.h"
#include "xla/literal_pool.h"
#include "xla/printer.h"
#include "xla/service/hlo.pb.h"
#include "xla/shape.h"
#include "xla/shape_util.h"
#include "xla/tsl/lib/gtl/iterator_range.h"
#include "xla/tsl/platform/logging.h" // IWYU pragma: keep
#include "xla/window_util.h"
#include "xla/xla_data.pb.h"
namespace xla {
// Base class for instructions with a dimensions vector.
class HloDimensionsInstruction : public HloInstruction {
public:
absl::Span<const int64_t> dimensions() const override { return dimensions_; }
std::vector<int64_t>* mutable_dimensions() override { return &dimensions_; }
void ToProto(HloInstructionProto* proto) const override;
static bool ClassOf(const HloInstruction* hlo) {
switch (hlo->opcode()) {
case HloOpcode::kBroadcast:
case HloOpcode::kConcatenate:
case HloOpcode::kReduce:
case HloOpcode::kReverse:
case HloOpcode::kSort:
case HloOpcode::kTranspose:
return true;
default:
return false;
}
}
protected:
HloDimensionsInstruction(HloOpcode opcode, const Shape& shape,
absl::Span<const int64_t> dimensions)
: HloInstruction(opcode, shape),
dimensions_(dimensions.begin(), dimensions.end()) {}
void PrintExtraAttributesImpl(AttributePrinter& printer,
const HloPrintOptions& options) const override;
bool IdenticalSlowPath(
const HloInstruction& other,
absl::FunctionRef<bool(const HloComputation*, const HloComputation*)>
eq_computations) const override;
std::vector<int64_t> dimensions_;
};
class HloBatchNormInstruction : public HloInstruction {
public:
// Returns feature_index field associated with the instruction. The index
// represents the index of the feature dimension.
int64_t feature_index() const { return feature_index_; }
// Returns a epsilon value associated with the instruction. The is a small
// number added to the variance to avoid divide-by-zero error.
float epsilon() const { return epsilon_; }
void ToProto(HloInstructionProto* proto) const override;
static bool ClassOf(const HloInstruction* hlo) {
switch (hlo->opcode()) {
case HloOpcode::kBatchNormGrad:
case HloOpcode::kBatchNormInference:
case HloOpcode::kBatchNormTraining:
return true;
default:
return false;
}
}
protected:
explicit HloBatchNormInstruction(HloOpcode opcode, const Shape& shape,
HloInstruction* operand,
HloInstruction* scale, float epsilon,
int64_t feature_index);
private:
void PrintExtraAttributesImpl(AttributePrinter& printer,
const HloPrintOptions& options) const override;
bool IdenticalSlowPath(
const HloInstruction& other,
absl::FunctionRef<bool(const HloComputation*, const HloComputation*)>
eq_computations) const override;
// A small float number added to the variance to avoid divide-by-zero error.
float epsilon_ = 0.0f;
// An integer value representing the index of the feature dimension.
int64_t feature_index_ = -1;
};
class HloBatchNormTrainingInstruction : public HloBatchNormInstruction {
public:
explicit HloBatchNormTrainingInstruction(
const Shape& shape, HloInstruction* operand, HloInstruction* scale,
HloInstruction* offset, float epsilon, int64_t feature_index);
static bool ClassOf(const HloInstruction* hlo) {
return hlo->opcode() == HloOpcode::kBatchNormTraining;
}
private:
// Implementation for non-common logic of CloneWithNewOperands.
std::unique_ptr<HloInstruction> CloneWithNewOperandsImpl(
const Shape& shape, absl::Span<HloInstruction* const> new_operands,
HloCloneContext* context) const override;
};
class HloBatchNormInferenceInstruction : public HloBatchNormInstruction {
public:
explicit HloBatchNormInferenceInstruction(
const Shape& shape, HloInstruction* operand, HloInstruction* scale,
HloInstruction* offset, HloInstruction* mean, HloInstruction* variance,
float epsilon, int64_t feature_index);
static bool ClassOf(const HloInstruction* hlo) {
return hlo->opcode() == HloOpcode::kBatchNormInference;
}
private:
// Implementation for non-common logic of CloneWithNewOperands.
std::unique_ptr<HloInstruction> CloneWithNewOperandsImpl(
const Shape& shape, absl::Span<HloInstruction* const> new_operands,
HloCloneContext* context) const override;
};
class HloBatchNormGradInstruction : public HloBatchNormInstruction {
public:
explicit HloBatchNormGradInstruction(
const Shape& shape, HloInstruction* operand, HloInstruction* scale,
HloInstruction* mean, HloInstruction* variance,
HloInstruction* grad_output, float epsilon, int64_t feature_index);
static bool ClassOf(const HloInstruction* hlo) {
return hlo->opcode() == HloOpcode::kBatchNormGrad;
}
private:
// Implementation for non-common logic of CloneWithNewOperands.
std::unique_ptr<HloInstruction> CloneWithNewOperandsImpl(
const Shape& shape, absl::Span<HloInstruction* const> new_operands,
HloCloneContext* context) const override;
};
class HloFftInstruction : public HloInstruction {
public:
explicit HloFftInstruction(const Shape& shape, HloInstruction* operand,
FftType fft_type,
absl::Span<const int64_t> fft_length);
FftType fft_type() const { return fft_type_; }
const std::vector<int64_t>& fft_length() const { return fft_length_; }
void ToProto(HloInstructionProto* proto) const override;
static bool ClassOf(const HloInstruction* hlo) {
return hlo->opcode() == HloOpcode::kFft;
}
private:
void PrintExtraAttributesImpl(AttributePrinter& printer,
const HloPrintOptions& options) const override;
bool IdenticalSlowPath(
const HloInstruction& other,
absl::FunctionRef<bool(const HloComputation*, const HloComputation*)>
eq_computations) const override;
// Implementation for non-common logic of CloneWithNewOperands.
std::unique_ptr<HloInstruction> CloneWithNewOperandsImpl(
const Shape& shape, absl::Span<HloInstruction* const> new_operands,
HloCloneContext* context) const override;
// Describes FFT type for an FFT instruction.
FftType fft_type_ = FftType::FFT;
// Indicates the FFT length for an FFT instruction.
std::vector<int64_t> fft_length_;
};
class HloAliasible {
public:
// Gets a list of output/operand buffer pairs that alias each other, where the
// output buffer is represented as a ShapeIndex, and the operand buffer is
// represented as the operand index and the ShapeIndex. By default this list
// is empty.
const std::vector<std::pair<ShapeIndex, std::pair<int64_t, ShapeIndex>>>&
output_to_operand_aliasing() const {
return output_to_operand_aliasing_;
}
// Sets the list of output/operand buffer pairs that alias each other.
void set_output_to_operand_aliasing(
std::vector<std::pair<ShapeIndex, std::pair<int64_t, ShapeIndex>>>
aliasing) {
output_to_operand_aliasing_ = std::move(aliasing);
}
private:
// A list of output/operand buffer pairs that alias each other. See comment of
// output_to_operand_aliasing().
std::vector<std::pair<ShapeIndex, std::pair<int64_t, ShapeIndex>>>
output_to_operand_aliasing_;
};
class HloAsyncInstruction : public HloInstruction {
public:
// Constructs async-{update,done}.
HloAsyncInstruction(HloOpcode opcode, const Shape& shape,
HloInstruction* operand);
HloComputation* async_wrapped_computation() const;
HloInstruction* async_wrapped_instruction() const;
HloOpcode async_wrapped_opcode() const;
// Async thread name is a unique thread name for one or more async groups.
// Typically one HLO module contains a main thread as well as one or more
// parallel threads.
virtual absl::string_view async_execution_thread() const;
virtual void set_async_execution_thread(
absl::string_view async_execution_thread) {}
void ToProto(HloInstructionProto* proto) const override {
HloInstruction::ToProto(proto);
}
static bool ClassOf(const HloInstruction* hlo) {
switch (hlo->opcode()) {
case HloOpcode::kAsyncStart:
case HloOpcode::kAsyncUpdate:
case HloOpcode::kAsyncDone:
return true;
default:
return false;
}
}
// Returns async-start instruction of the async chain.
HloAsyncInstruction* async_chain_start() const;
// Returns async-done instruction of the async chain.
HloAsyncInstruction* async_chain_done() const;
// Returns the chain of async op referencing this computation,
// where *begin(GetAsyncChain()) is the async-start op and
// *end(GetAsyncChain()) is the async-done op.
std::vector<HloAsyncInstruction*> GetAsyncChain() const;
bool HasSideEffect() const override {
return async_wrapped_instruction()->HasSideEffect();
}
void UpdateAsyncChain();
// Helper to constructs async-{start,update,done}.
HloAsyncInstruction(HloOpcode opcode, const Shape& shape,
absl::Span<HloInstruction* const> operands,
HloOpcode async_wrapped_opcode);
protected:
// Updates all future instructions in the async chain to match the shape of
// the current instruction.
void UpdateChainShapes();
private:
// async-{update,done} inherit all their attributes from async-start,
// so they shouldn't print any.
void PrintExtraAttributesImpl(AttributePrinter& printer,
const HloPrintOptions& options) const override {
}
bool IdenticalSlowPath(
const HloInstruction& other,
absl::FunctionRef<bool(const HloComputation*, const HloComputation*)>
eq_computations) const override;
std::unique_ptr<HloInstruction> CloneWithNewOperandsImpl(
const Shape& shape, absl::Span<HloInstruction* const> new_operands,
HloCloneContext* context) const override;
HloAsyncInstruction* async_chain_next_ = nullptr;
};
// Creates async-start.
class HloAsyncStartInstruction : public HloAsyncInstruction,
public HloAliasible {
public:
using HloAliasible::output_to_operand_aliasing;
using HloAliasible::set_output_to_operand_aliasing;
HloAsyncStartInstruction(
HloOpcode opcode, const Shape& shape,
absl::Span<HloInstruction* const> operands,
HloComputation* async_computation,
absl::string_view async_execution_thread = kMainExecutionThread);
// Adds a new operand to the async-start instruction.
HloInstruction* AddCallOperand(HloInstruction* new_operand);
// Clones the async-start instruction with new operands and a new computation.
std::unique_ptr<HloInstruction> CloneWithNewOperandsAndComputation(
const Shape& shape, absl::Span<HloInstruction* const> new_operands,
HloComputation* new_computation,
HloCloneContext* context = nullptr) const;
absl::string_view async_execution_thread() const override {
return async_execution_thread_;
};
void set_async_execution_thread(
absl::string_view async_execution_thread) override;
void ToProto(HloInstructionProto* proto) const override;
static bool ClassOf(const HloInstruction* hlo) {
switch (hlo->opcode()) {
case HloOpcode::kAsyncStart:
return true;
default:
return false;
}
}
private:
void PrintExtraAttributesImpl(AttributePrinter& printer,
const HloPrintOptions& options) const override;
bool IdenticalSlowPath(
const HloInstruction& other,
absl::FunctionRef<bool(const HloComputation*, const HloComputation*)>
eq_computations) const override;
std::unique_ptr<HloInstruction> CloneWithNewOperandsImpl(
const Shape& shape, absl::Span<HloInstruction* const> new_operands,
HloComputation* new_computation, HloCloneContext* context) const;
std::unique_ptr<HloInstruction> CloneWithNewOperandsImpl(
const Shape& shape, absl::Span<HloInstruction* const> new_operands,
HloCloneContext* context) const override;
std::string async_execution_thread_ = kMainExecutionThread;
};
class HloCopyStartInstruction : public HloInstruction {
public:
explicit HloCopyStartInstruction(
const Shape& shape, HloInstruction* operand,
std::optional<int> cross_program_prefetch_index);
std::optional<int> cross_program_prefetch_index() const {
return cross_program_prefetch_index_;
}
void ToProto(HloInstructionProto* proto) const override;
static bool ClassOf(const HloInstruction* hlo) {
return hlo->opcode() == HloOpcode::kCopyStart;
}
private:
void PrintExtraAttributesImpl(AttributePrinter& printer,
const HloPrintOptions& options) const override;
bool IdenticalSlowPath(
const HloInstruction& other,
absl::FunctionRef<bool(const HloComputation*, const HloComputation*)>
eq_computations) const override;
std::unique_ptr<HloInstruction> CloneWithNewOperandsImpl(
const Shape& shape, absl::Span<HloInstruction* const> new_operands,
HloCloneContext* context) const override;
// Each cross program prefetched buffer has a unique index. The indices are
// assigned contiguously starting from zero in
// MsaAlgorithm::AllocateCrossProgramPrefetchBuffer. This value is used during
// codegen to determine which buffer is being speculated at runtime. One
// possible implementation is to initialize an array with boolean values
// indicating whether the cross program prefetch succeeds or fails for each
// buffer.
std::optional<int> cross_program_prefetch_index_;
};
class HloCompareInstruction : public HloInstruction {
public:
explicit HloCompareInstruction(const Shape& shape, HloInstruction* lhs,
HloInstruction* rhs,
ComparisonDirection direction,
std::optional<Comparison::Type> type);
ComparisonDirection direction() const { return compare_.GetDirection(); }
ComparisonOrder order() const { return compare_.GetOrder(); }
Comparison::Type type() const { return compare_.GetType(); }
void ToProto(HloInstructionProto* proto) const override;
static bool ClassOf(const HloInstruction* hlo) {
return hlo->opcode() == HloOpcode::kCompare;
}
private:
void PrintExtraAttributesImpl(AttributePrinter& printer,
const HloPrintOptions& options) const override;
bool IdenticalSlowPath(
const HloInstruction& other,
absl::FunctionRef<bool(const HloComputation*, const HloComputation*)>
eq_computations) const override;
std::unique_ptr<HloInstruction> CloneWithNewOperandsImpl(
const Shape& shape, absl::Span<HloInstruction* const> new_operands,
HloCloneContext* context) const override;
Comparison compare_;
};
class HloTriangularSolveInstruction : public HloInstruction {
public:
explicit HloTriangularSolveInstruction(const Shape& shape, HloInstruction* a,
HloInstruction* b,
const TriangularSolveOptions& options);
const TriangularSolveOptions& triangular_solve_options() const {
return triangular_solve_options_;
}
void ToProto(HloInstructionProto* proto) const override;
static bool ClassOf(const HloInstruction* hlo) {
return hlo->opcode() == HloOpcode::kTriangularSolve;
}
private:
void PrintExtraAttributesImpl(AttributePrinter& printer,
const HloPrintOptions& options) const override;
bool IdenticalSlowPath(
const HloInstruction& other,
absl::FunctionRef<bool(const HloComputation*, const HloComputation*)>
eq_computations) const override;
// Implementation for non-common logic of CloneWithNewOperands.
std::unique_ptr<HloInstruction> CloneWithNewOperandsImpl(
const Shape& shape, absl::Span<HloInstruction* const> new_operands,
HloCloneContext* context) const override;
TriangularSolveOptions triangular_solve_options_;
};
class HloCholeskyInstruction : public HloInstruction {
public:
explicit HloCholeskyInstruction(const Shape& shape, HloInstruction* a,
const CholeskyOptions& options);
const CholeskyOptions& cholesky_options() const { return cholesky_options_; }
void ToProto(HloInstructionProto* proto) const override;
static bool ClassOf(const HloInstruction* hlo) {
return hlo->opcode() == HloOpcode::kCholesky;
}
private:
void PrintExtraAttributesImpl(AttributePrinter& printer,
const HloPrintOptions& options) const override;
bool IdenticalSlowPath(
const HloInstruction& other,
absl::FunctionRef<bool(const HloComputation*, const HloComputation*)>
eq_computations) const override;
// Implementation for non-common logic of CloneWithNewOperands.
std::unique_ptr<HloInstruction> CloneWithNewOperandsImpl(
const Shape& shape, absl::Span<HloInstruction* const> new_operands,
HloCloneContext* context) const override;
CholeskyOptions cholesky_options_;
};
// Class that represents instructions that synchronize and transfer data between
// partitioned devices. Send/Recv and collective instructions (AllReduce,
// AllToAll, CollectivePermute, CollectiveBroadcast) belong to this instruction
// type. A group of instructions (of the same opcode) with the same channel_id
// communicate during execution.
class HloChannelInstruction : public HloInstruction {
public:
// Returns the channel id associated with the instruction. The id is
// shared between each Send/Recv pair or a group of collective instructions
// and is globally unique to identify each channel.
std::optional<int64_t> channel_id() const { return channel_id_; }
void set_channel_id(const std::optional<int64_t>& channel_id);
// Whether this instruction is identical to `other` except for the values of
// channel IDs, as long as both have channel IDs or neither has a channel ID.
virtual bool IdenticalSlowPathIgnoringChannelIdValues(
const HloInstruction& other,
absl::FunctionRef<bool(const HloComputation*, const HloComputation*)>
eq_computations) const {
return channel_id_.has_value() == other.channel_id().has_value();
}
static bool ClassOf(const HloInstruction* hlo);
protected:
explicit HloChannelInstruction(HloOpcode opcode, const Shape& shape,
const std::optional<int64_t>& channel_id);
void ToProto(HloInstructionProto* proto) const override;
void PrintExtraAttributesImpl(AttributePrinter& printer,
const HloPrintOptions& options) const override;
// Do not override IdenticalSlowPath(). Override
// IdenticalSlowPathIgnoringChannelIdValues() instead.
bool IdenticalSlowPath(
const HloInstruction& other,
absl::FunctionRef<bool(const HloComputation*, const HloComputation*)>
eq_computations) const final;
std::optional<int64_t> channel_id_;
};
// Class that represents a top-k instruction.
class HloTopKInstruction : public HloInstruction {
public:
HloTopKInstruction(const Shape& shape, HloInstruction* input, int64_t k,
bool largest);
void ToProto(HloInstructionProto* proto) const override;
static bool ClassOf(const HloInstruction* hlo) {
return hlo->opcode() == HloOpcode::kTopK;
}
// Returns how many K-s does it need.
int64_t k() const { return k_; }
// Returns whether the largest or smallest K values should be computed.
bool largest() const { return largest_; }
void PrintExtraAttributesImpl(AttributePrinter& printer,
const HloPrintOptions& options) const override;
private:
bool IdenticalSlowPath(
const HloInstruction& other,
absl::FunctionRef<bool(const HloComputation*, const HloComputation*)>
eq_computations) const override;
std::unique_ptr<HloInstruction> CloneWithNewOperandsImpl(
const Shape& shape, absl::Span<HloInstruction* const> new_operands,
HloCloneContext* context) const override;
int64_t k_;
bool largest_;
};
class HloSendRecvInstruction : public HloChannelInstruction {
public:
// Returns whether this send/recv instruction sends data to/from the host.
bool is_host_transfer() const { return is_host_transfer_; }
void ToProto(HloInstructionProto* proto) const override;
static bool ClassOf(const HloInstruction* hlo) {
switch (hlo->opcode()) {
case HloOpcode::kSend:
case HloOpcode::kSendDone:
case HloOpcode::kRecv:
case HloOpcode::kRecvDone:
return true;
default:
return false;
}
}
protected:
explicit HloSendRecvInstruction(HloOpcode opcode, const Shape& shape,
std::optional<int64_t> channel_id,
bool is_host_transfer);
private:
void PrintExtraAttributesImpl(AttributePrinter& printer,
const HloPrintOptions& options) const override;
bool IdenticalSlowPathIgnoringChannelIdValues(
const HloInstruction& other,
absl::FunctionRef<bool(const HloComputation*, const HloComputation*)>
eq_computations) const override;
// Whether this send/recv instruction sends data to/from the host.
bool is_host_transfer_;
};
class HloSendInstruction : public HloSendRecvInstruction {
public:
explicit HloSendInstruction(HloInstruction* operand, HloInstruction* token,
std::optional<int64_t> channel_id,
bool is_host_transfer);
static bool ClassOf(const HloInstruction* hlo) {
return hlo->opcode() == HloOpcode::kSend;
}
private:
// Implementation for non-common logic of CloneWithNewOperands.
std::unique_ptr<HloInstruction> CloneWithNewOperandsImpl(
const Shape& shape, absl::Span<HloInstruction* const> new_operands,
HloCloneContext* context) const override;
};
class HloSendDoneInstruction : public HloSendRecvInstruction {
public:
explicit HloSendDoneInstruction(HloSendInstruction* operand,
bool is_host_transfer);
explicit HloSendDoneInstruction(HloInstruction* operand,
std::optional<int64_t> channel_id,
bool is_host_transfer);
static bool ClassOf(const HloInstruction* hlo) {
return hlo->opcode() == HloOpcode::kSendDone;
}
private:
// Implementation for non-common logic of CloneWithNewOperands.
std::unique_ptr<HloInstruction> CloneWithNewOperandsImpl(
const Shape& shape, absl::Span<HloInstruction* const> new_operands,
HloCloneContext* context) const override;
};
class HloRecvInstruction : public HloSendRecvInstruction {
public:
explicit HloRecvInstruction(const Shape& shape, HloInstruction* token,
std::optional<int64_t> channel_id,
bool is_host_transfer);
static bool ClassOf(const HloInstruction* hlo) {
return hlo->opcode() == HloOpcode::kRecv;
}
private:
// Implementation for non-common logic of CloneWithNewOperands.
std::unique_ptr<HloInstruction> CloneWithNewOperandsImpl(
const Shape& shape, absl::Span<HloInstruction* const> new_operands,
HloCloneContext* context) const override;
};
class HloRecvDoneInstruction : public HloSendRecvInstruction {
public:
explicit HloRecvDoneInstruction(HloRecvInstruction* operand,
bool is_host_transfer);
explicit HloRecvDoneInstruction(HloInstruction* operand,
std::optional<int64_t> channel_id,
bool is_host_transfer);
static bool ClassOf(const HloInstruction* hlo) {
return hlo->opcode() == HloOpcode::kRecvDone;
}
private:
// Implementation for non-common logic of CloneWithNewOperands.
std::unique_ptr<HloInstruction> CloneWithNewOperandsImpl(
const Shape& shape, absl::Span<HloInstruction* const> new_operands,
HloCloneContext* context) const override;
};
class HloCollectiveInstruction : public HloChannelInstruction {
public:
const std::vector<ReplicaGroup>& replica_groups() const {
return device_list_->replica_groups();
}
const std::shared_ptr<CollectiveDeviceListBase>& device_list() const {
return device_list_;
}
void set_device_list(std::shared_ptr<CollectiveDeviceListBase> device_list);
// Returns true if the layout of the AllReduce is enforced by XLA client (as
// the layout set in the shape). The only reason for the client to set the
// layout is to separately compile computations that communicate with
// AllReduce. Since this field is only set `true` by the client, the compiler
// only needs to propagate existing values (e.g., Clone, X64Rewriter) or set
// `false` for all other cases.
//
// When this is `true`, there may be communication endpoints outside the
// current compilation unit, so the compiler considers this AllReduce as
// side-effecting to disable compiler transformations. The compiler is free to
// transform unconstrained AllReduces differently across compilation units.
// It is an error for an HloModule to have a mix of constrained and
// unconstrained AllReduce instructions (checked by HloVerifier).
bool constrain_layout() const { return constrain_layout_; }
static bool ClassOf(const HloInstruction* hlo);
// TODO(b/462498901): We are trending towards removing channel IDs for
// collectives - or, rather, turning it into a boolean field indicating
// whether the collective is cross-replica or cross-partition. This method is
// a temporary crutch to be consistent without generating unique channel IDs
// whenever a new collective is created.
static int64_t GetDefaultChannelId() {
constexpr int64_t kDefaultCollectiveChannelId = 1;
return kDefaultCollectiveChannelId;
}
protected:
explicit HloCollectiveInstruction(
HloOpcode opcode, const Shape& shape,
absl::Span<HloInstruction* const> operands,
std::shared_ptr<CollectiveDeviceListBase> device_list,
bool constrain_layout, const std::optional<int64_t>& channel_id);
void ToProto(HloInstructionProto* proto) const override;
void PrintExtraAttributesImpl(AttributePrinter& printer,
const HloPrintOptions& options) const override;
bool IdenticalSlowPathIgnoringChannelIdValues(
const HloInstruction& other,
absl::FunctionRef<bool(const HloComputation*, const HloComputation*)>
eq_computations) const override;
std::shared_ptr<CollectiveDeviceListBase> device_list_;
bool constrain_layout_;
};
class HloAllGatherInstruction : public HloCollectiveInstruction {
public:
explicit HloAllGatherInstruction(
HloOpcode opcode, const Shape& shape,
absl::Span<HloInstruction* const> operands, int64_t all_gather_dimension,
std::shared_ptr<CollectiveDeviceListBase> device_list,
bool constrain_layout, const std::optional<int64_t>& channel_id,
bool use_global_device_ids);
ABSL_DEPRECATED("Use CollectiveDeviceList instead of list of ReplicaGroup.")
explicit HloAllGatherInstruction(
HloOpcode opcode, const Shape& shape,
absl::Span<HloInstruction* const> operands, int64_t all_gather_dimension,
absl::Span<const ReplicaGroup> replica_groups, bool constrain_layout,
const std::optional<int64_t>& channel_id, bool use_global_device_ids);
// Same as HloAllReduceInstruction::use_global_device_ids.
bool use_global_device_ids() const { return use_global_device_ids_; }
void set_use_global_device_ids(bool value) { use_global_device_ids_ = value; }
// The dimension on which data from different participants are concatenated.
int64_t all_gather_dimension() const { return all_gather_dimension_; }
absl::Span<const int64_t> dimensions() const override {
return absl::MakeConstSpan(&all_gather_dimension_, 1);
}
void set_all_gather_dimension(int64_t dim) { all_gather_dimension_ = dim; }
static bool ClassOf(const HloInstruction* hlo) {
return hlo->opcode() == HloOpcode::kAllGather ||
hlo->opcode() == HloOpcode::kAllGatherStart;
}
protected:
void PrintExtraAttributesImpl(AttributePrinter& printer,
const HloPrintOptions& options) const override;
void ToProto(HloInstructionProto* proto) const override;
private:
bool IdenticalSlowPathIgnoringChannelIdValues(
const HloInstruction& other,
absl::FunctionRef<bool(const HloComputation*, const HloComputation*)>
eq_computations) const override;
// Implementation for non-common logic of CloneWithNewOperands.
std::unique_ptr<HloInstruction> CloneWithNewOperandsImpl(
const Shape& shape, absl::Span<HloInstruction* const> new_operands,
HloCloneContext* context) const override;
int64_t all_gather_dimension_;
bool use_global_device_ids_;
};
// Base class for all-reduce and all-reduce scatter instructions.
class HloAllReduceInstructionBase : public HloCollectiveInstruction {
public:
explicit HloAllReduceInstructionBase(
HloOpcode opcode, const Shape& shape,
absl::Span<HloInstruction* const> operands,
HloComputation* reduce_computation,
std::shared_ptr<CollectiveDeviceListBase> device_list,
bool constrain_layout, const std::optional<int64_t>& channel_id,
bool use_global_device_ids);
// Returns true if the ids in the ReplicaGroup config represent a global id of
// (replica_id * partition_count + partition_id) instead of a replica id.
// This enables more flexible grouping of devices if this all-reduce is both
// cross-partition and cross-replica.
//
// For example with 2 replicas and 4 partitions,
// replica_groups={{0,1,4,5},{2,3,6,7}}, use_global_device_ids=true means that
// group[0] = (0,0), (0,1), (1,0), (1,1)
// group[1] = (0,2), (0,3), (1,2), (1,3)
// where each pair is (replica_id, partition_id).
bool use_global_device_ids() const { return use_global_device_ids_; }
void set_use_global_device_ids(bool value) { use_global_device_ids_ = value; }
static bool ClassOf(const HloInstruction* hlo);
protected:
void PrintExtraAttributesImpl(AttributePrinter& printer,
const HloPrintOptions& options) const override;
void ToProto(HloInstructionProto* proto) const override;
bool IdenticalSlowPathIgnoringChannelIdValues(
const HloInstruction& other,
absl::FunctionRef<bool(const HloComputation*, const HloComputation*)>
eq_computations) const override;
private:
bool use_global_device_ids_;
};
class HloAllReduceInstruction : public HloAllReduceInstructionBase {
public:
using HloAllReduceInstructionBase::HloAllReduceInstructionBase;
static bool ClassOf(const HloInstruction* hlo) {
return hlo->opcode() == HloOpcode::kAllReduce ||
hlo->opcode() == HloOpcode::kAllReduceStart;
}
// Returns true if the AllReduce does no communication, so it's equivalent
// to a mem copy.
bool IsNoop() const;
private:
// Implementation for non-common logic of CloneWithNewOperands.
std::unique_ptr<HloInstruction> CloneWithNewOperandsImpl(
const Shape& shape, absl::Span<HloInstruction* const> new_operands,
HloCloneContext* context) const override;
};
class HloReduceScatterInstruction : public HloAllReduceInstructionBase {
public:
explicit HloReduceScatterInstruction(
const Shape& shape, absl::Span<HloInstruction* const> operands,
HloComputation* reduce_computation,
std::shared_ptr<CollectiveDeviceListBase> device_list,
bool constrain_layout, const std::optional<int64_t>& channel_id,
bool use_global_device_ids, int64_t scatter_dimension);
ABSL_DEPRECATED("Use CollectiveDeviceList instead of list of ReplicaGroup.")
explicit HloReduceScatterInstruction(
const Shape& shape, absl::Span<HloInstruction* const> operands,
HloComputation* reduce_computation,
absl::Span<const ReplicaGroup> replica_groups, bool constrain_layout,
const std::optional<int64_t>& channel_id, bool use_global_device_ids,
int64_t scatter_dimension);
// The dimension on which reduced data is scattered to different participants.
int64_t scatter_dimension() const { return scatter_dimension_; }
absl::Span<const int64_t> dimensions() const override {
return absl::MakeConstSpan(&scatter_dimension_, 1);
}
static bool ClassOf(const HloInstruction* hlo) {
return hlo->opcode() == HloOpcode::kReduceScatter;
}
protected:
void PrintExtraAttributesImpl(AttributePrinter& printer,
const HloPrintOptions& options) const override;
void ToProto(HloInstructionProto* proto) const override;
private:
bool IdenticalSlowPathIgnoringChannelIdValues(
const HloInstruction& other,
absl::FunctionRef<bool(const HloComputation*, const HloComputation*)>
eq_computations) const override;
// Implementation for non-common logic of CloneWithNewOperands.
std::unique_ptr<HloInstruction> CloneWithNewOperandsImpl(
const Shape& shape, absl::Span<HloInstruction* const> new_operands,
HloCloneContext* context) const override;
int64_t scatter_dimension_;
};
class HloAllToAllInstruction : public HloCollectiveInstruction {
public:
explicit HloAllToAllInstruction(
const Shape& shape, absl::Span<HloInstruction* const> operands,
std::shared_ptr<CollectiveDeviceListBase> device_list,
bool constrain_layout, const std::optional<int64_t>& channel_id,
const std::optional<int64_t>& split_dimension);
ABSL_DEPRECATED("Use CollectiveDeviceList instead of list of ReplicaGroup.")
explicit HloAllToAllInstruction(
const Shape& shape, absl::Span<HloInstruction* const> operands,
absl::Span<const ReplicaGroup> replica_groups, bool constrain_layout,
const std::optional<int64_t>& channel_id,
const std::optional<int64_t>& split_dimension);
// AllToAll can optionally take a split dimension, which means that this
// AllToAll takes a single (flattened) array operand and produces an array
// output (instead of taking a list of operands and producing a tuple).
//
// split_dimension specifies which dimension in the operand is split across
// devices in each replica_group, and also means the concatenated dimension
// on the output (i.e., input and the output shapes are the same).
std::optional<int64_t> split_dimension() const { return split_dimension_; }
void set_split_dimension(int64_t dim) { split_dimension_ = dim; }
static bool ClassOf(const HloInstruction* hlo) {
return hlo->opcode() == HloOpcode::kAllToAll;
}
protected:
void PrintExtraAttributesImpl(AttributePrinter& printer,
const HloPrintOptions& options) const override;
void ToProto(HloInstructionProto* proto) const override;
private:
bool IdenticalSlowPathIgnoringChannelIdValues(
const HloInstruction& other,
absl::FunctionRef<bool(const HloComputation*, const HloComputation*)>
eq_computations) const override;
// Implementation for non-common logic of CloneWithNewOperands.
std::unique_ptr<HloInstruction> CloneWithNewOperandsImpl(
const Shape& shape, absl::Span<HloInstruction* const> new_operands,
HloCloneContext* context) const override;
std::optional<int64_t> split_dimension_;
};
class HloRaggedAllToAllInstruction : public HloCollectiveInstruction {
public:
explicit HloRaggedAllToAllInstruction(
const Shape& shape, absl::Span<HloInstruction* const> operands,
std::shared_ptr<CollectiveDeviceListBase> device_list,
const std::optional<int64_t>& channel_id);
ABSL_DEPRECATED("Use CollectiveDeviceList instead of list of ReplicaGroup.")
explicit HloRaggedAllToAllInstruction(
HloOpcode opcode, const Shape& shape,
absl::Span<HloInstruction* const> operands,
absl::Span<const ReplicaGroup> replica_groups,
const std::optional<int64_t>& channel_id);
static bool ClassOf(const HloInstruction* hlo) {
return hlo->opcode() == HloOpcode::kRaggedAllToAll;
}
protected:
void PrintExtraAttributesImpl(AttributePrinter& printer,
const HloPrintOptions& options) const override;
void ToProto(HloInstructionProto* proto) const override;
private:
// Implementation for non-common logic of CloneWithNewOperands.
std::unique_ptr<HloInstruction> CloneWithNewOperandsImpl(
const Shape& shape, absl::Span<HloInstruction* const> new_operands,
HloCloneContext* context) const override;
};
class HloCollectiveBroadcastInstruction : public HloCollectiveInstruction {
public:
explicit HloCollectiveBroadcastInstruction(
HloOpcode opcode, const Shape& shape,
absl::Span<HloInstruction* const> operands,
std::shared_ptr<CollectiveDeviceListBase> device_list,
bool constrain_layout, const std::optional<int64_t>& channel_id);
ABSL_DEPRECATED("Use CollectiveDeviceList instead of list of ReplicaGroup.")
explicit HloCollectiveBroadcastInstruction(
HloOpcode opcode, const Shape& shape,
absl::Span<HloInstruction* const> operands,
absl::Span<const ReplicaGroup> replica_groups, bool constrain_layout,
const std::optional<int64_t>& channel_id);
void ToProto(HloInstructionProto* proto) const override;
static bool ClassOf(const HloInstruction* hlo) {
return hlo->opcode() == HloOpcode::kCollectiveBroadcast;