-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathops.go
More file actions
902 lines (833 loc) · 32.1 KB
/
Copy pathops.go
File metadata and controls
902 lines (833 loc) · 32.1 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
// Copyright 2023-2026 The GoMLX Authors. SPDX-License-Identifier: Apache-2.0
package xla
// This file contains manually implemented operations.
import (
"reflect"
"github.com/gomlx/compute"
"github.com/gomlx/compute/dtypes"
"github.com/gomlx/compute/shapes"
"github.com/gomlx/compute/support/xslices"
"github.com/gomlx/go-xla/stablehlo"
stablehlotypes "github.com/gomlx/go-xla/types"
stablehloshapes "github.com/gomlx/go-xla/types/shapes"
"github.com/pkg/errors"
)
// adjustAxisToRank returns a positive axis, adjusting negative numbers to the correct rank.
func adjustAxisToRank(axis, rank int) (int, error) {
if axis < -rank || axis >= rank {
return -1, errors.Errorf("axis %d is out of range for the rank %d", axis, rank)
}
if axis < 0 {
axis += rank
}
return axis, nil
}
// Identity returns an Op whose output is the same as its input.
// It's a no-op that can serve as a place-holder.
func (f *Function) Identity(x compute.Value) (compute.Value, error) {
if err := f.CheckValid(); err != nil {
return nil, err
}
nodes, err := f.verifyAndCastValues("OpShape", x)
if err != nil {
return nil, err
}
return nodes[0], nil
}
// BroadcastInDim broadcasts x to an output with the given shape.
// broadcastAxes has an output axes value for each x axes (len(broadcastAxes) == x.Shape.Rank()).
// The i-th axis of x is mapped to the broadcastAxes[i]-th dimension of the output.
// broadcastAxes must be also increasing: this operation cannot be used to transpose axes, it will only
// broadcast and introduce new axes in-between.
// This also requires that the i-th input axis is either 1 or is the same as the
// output dimension it's broadcasting into.
// For example, say operand `x = (s32)[2]{1, 2}`; outputShape = `(s32)[2,2]`:
// - Specifying []int{1} as broadcastAxes will generate output
// {{1, 2},
// {1, 2}}
// - On the other hand, specifying []int{0} as broadcastAxes
// will generate output
// {{1 , 1},
// {2 , 2}}
func (f *Function) BroadcastInDim(x compute.Value, outputShape shapes.Shape, broadcastAxes []int) (compute.Value, error) {
nodes, err := f.verifyAndCastValues("BroadcastInDim", x)
if err != nil {
return nil, err
}
value, err := stablehlo.BroadcastInDim(nodes[0].value, ShapeToXLA(outputShape), broadcastAxes)
if err != nil {
return nil, err
}
return f.newNode(value), nil
}
// Iota implements compute.Function interface.
func (f *Function) Iota(shape shapes.Shape, iotaAxis int) (compute.Value, error) {
value, err := f.fn.Iota(ShapeToXLA(shape), iotaAxis)
if err != nil {
return nil, err
}
return f.newNode(value), nil
}
// Reshape implements compute.Function interface.
func (f *Function) Reshape(x compute.Value, dimensions ...int) (compute.Value, error) {
nodes, err := f.verifyAndCastValues("Reshape", x)
if err != nil {
return nil, err
}
xNode := nodes[0]
dtype := xNode.shape.DType
shape := stablehloshapes.Make(dtype, dimensions...)
value, err := stablehlo.Reshape(xNode.value, shape)
if err != nil {
return nil, err
}
return f.newNode(value), nil
}
// Slice implements compute.Function interface.
func (f *Function) Slice(x compute.Value, starts, limits, strides []int) (compute.Value, error) {
nodes, err := f.verifyAndCastValues("Slice", x)
if err != nil {
return nil, err
}
xNode := nodes[0]
value, err := stablehlo.Slice(xNode.value, starts, limits, strides)
if err != nil {
return nil, err
}
return f.newNode(value), nil
}
func compareTypeForDType(dtype dtypes.DType) stablehlotypes.ComparisonType {
// Find compareType
compareType := stablehlotypes.CompareFloat
if !dtype.IsFloat() && !dtype.IsComplex() {
if dtype.IsUnsigned() || dtype == dtypes.Bool {
compareType = stablehlotypes.CompareUnsigned
} else {
compareType = stablehlotypes.CompareSigned
}
}
return compareType
}
// comparison generic operation.
func (f *Function) comparison(opType compute.OpType, lhs, rhs compute.Value) (compute.Value, error) {
lhsNode, rhsNode, err := f.builder.broadcastForBinaryOps(opType, lhs, rhs)
if err != nil {
return nil, err
}
compareType := compareTypeForDType(lhsNode.shape.DType)
var direction stablehlotypes.ComparisonDirection
switch opType {
case compute.OpTypeEqual:
direction = stablehlotypes.CompareEQ
case compute.OpTypeNotEqual:
direction = stablehlotypes.CompareNE
case compute.OpTypeGreaterThan:
direction = stablehlotypes.CompareGT
case compute.OpTypeGreaterOrEqual:
direction = stablehlotypes.CompareGE
case compute.OpTypeLessThan:
direction = stablehlotypes.CompareLT
case compute.OpTypeLessOrEqual:
direction = stablehlotypes.CompareLE
case compute.OpTypeEqualTotalOrder:
direction = stablehlotypes.CompareEQ
compareType = stablehlotypes.CompareTotalOrder
case compute.OpTypeNotEqualTotalOrder:
direction = stablehlotypes.CompareNE
compareType = stablehlotypes.CompareTotalOrder
case compute.OpTypeGreaterThanTotalOrder:
direction = stablehlotypes.CompareGT
compareType = stablehlotypes.CompareTotalOrder
case compute.OpTypeGreaterOrEqualTotalOrder:
direction = stablehlotypes.CompareGE
compareType = stablehlotypes.CompareTotalOrder
case compute.OpTypeLessThanTotalOrder:
direction = stablehlotypes.CompareLT
compareType = stablehlotypes.CompareTotalOrder
case compute.OpTypeLessOrEqualTotalOrder:
direction = stablehlotypes.CompareLE
compareType = stablehlotypes.CompareTotalOrder
default:
return nil, errors.Errorf("unsupported comparison operation %v", opType)
}
value, err := stablehlo.Compare(lhsNode.value, rhsNode.value, direction, compareType)
if err != nil {
return nil, err
}
return f.newNode(value), nil
}
// Equal returns the element-wise operation.
// Standard broadcasting rules apply (see documentation).
func (f *Function) Equal(lhs, rhs compute.Value) (compute.Value, error) {
return f.comparison(compute.OpTypeEqual, lhs, rhs)
}
// NotEqual returns the element-wise operation.
// Standard broadcasting rules apply (see documentation).
func (f *Function) NotEqual(lhs, rhs compute.Value) (compute.Value, error) {
return f.comparison(compute.OpTypeNotEqual, lhs, rhs)
}
// GreaterThan returns the element-wise operation.
// Standard broadcasting rules apply (see documentation).
func (f *Function) GreaterThan(lhs, rhs compute.Value) (compute.Value, error) {
return f.comparison(compute.OpTypeGreaterThan, lhs, rhs)
}
// GreaterOrEqual returns the element-wise operation.
// Standard broadcasting rules apply (see documentation).
func (f *Function) GreaterOrEqual(lhs, rhs compute.Value) (compute.Value, error) {
return f.comparison(compute.OpTypeGreaterOrEqual, lhs, rhs)
}
// LessThan returns the element-wise operation.
// Standard broadcasting rules apply (see documentation).
func (f *Function) LessThan(lhs, rhs compute.Value) (compute.Value, error) {
return f.comparison(compute.OpTypeLessThan, lhs, rhs)
}
// LessOrEqual returns the element-wise operation.
// Standard broadcasting rules apply (see documentation).
func (f *Function) LessOrEqual(lhs, rhs compute.Value) (compute.Value, error) {
return f.comparison(compute.OpTypeLessOrEqual, lhs, rhs)
}
// EqualTotalOrder returns the element-wise operation.
// Standard broadcasting rules apply (see documentation).
//
// The "TotalOrder" version of the operation enforces `-NaN < -Inf < -Finite < -0 < +0 < +Finite < +Inf < +NaN`.
func (f *Function) EqualTotalOrder(lhs, rhs compute.Value) (compute.Value, error) {
return f.comparison(compute.OpTypeEqualTotalOrder, lhs, rhs)
}
// NotEqualTotalOrder returns the element-wise operation.
// Standard broadcasting rules apply (see documentation).
//
// The "TotalOrder" version of the operation enforces `-NaN < -Inf < -Finite < -0 < +0 < +Finite < +Inf < +NaN`.
func (f *Function) NotEqualTotalOrder(lhs, rhs compute.Value) (compute.Value, error) {
return f.comparison(compute.OpTypeNotEqualTotalOrder, lhs, rhs)
}
// GreaterThanTotalOrder returns the element-wise operation.
// Standard broadcasting rules apply (see documentation).
//
// The "TotalOrder" version of the operation enforces `-NaN < -Inf < -Finite < -0 < +0 < +Finite < +Inf < +NaN`.
func (f *Function) GreaterThanTotalOrder(lhs, rhs compute.Value) (compute.Value, error) {
return f.comparison(compute.OpTypeGreaterThanTotalOrder, lhs, rhs)
}
// GreaterOrEqualTotalOrder returns the element-wise operation.
// Standard broadcasting rules apply (see documentation).
//
// The "TotalOrder" version of the operation enforces `-NaN < -Inf < -Finite < -0 < +0 < +Finite < +Inf < +NaN`.
func (f *Function) GreaterOrEqualTotalOrder(lhs, rhs compute.Value) (compute.Value, error) {
return f.comparison(compute.OpTypeGreaterOrEqualTotalOrder, lhs, rhs)
}
// LessThanTotalOrder returns the element-wise operation.
// Standard broadcasting rules apply (see documentation).
//
// The "TotalOrder" version of the operation enforces `-NaN < -Inf < -Finite < -0 < +0 < +Finite < +Inf < +NaN`.
func (f *Function) LessThanTotalOrder(lhs, rhs compute.Value) (compute.Value, error) {
return f.comparison(compute.OpTypeLessThanTotalOrder, lhs, rhs)
}
// LessOrEqualTotalOrder returns the element-wise operation.
// Standard broadcasting rules apply (see documentation).
//
// The "TotalOrder" version of the operation enforces `-NaN < -Inf < -Finite < -0 < +0 < +Finite < +Inf < +NaN`.
func (f *Function) LessOrEqualTotalOrder(lhs, rhs compute.Value) (compute.Value, error) {
return f.comparison(compute.OpTypeLessOrEqualTotalOrder, lhs, rhs)
}
// Abs returns the Op that represents the output of the corresponding operation.
//
// It is special-cased here because StableHLO doesn't define the Abs() of complex numbers.
func (f *Function) Abs(operand compute.Value) (compute.Value, error) {
nodes, err := f.verifyAndCastValues("Abs", operand)
if err != nil {
return nil, err
}
if nodes[0].shape.DType.IsComplex() {
realV, err := f.Real(operand)
if err != nil {
return nil, err
}
imagV, err := f.Imag(operand)
if err != nil {
return nil, err
}
realV2, err := f.Mul(realV, realV)
if err != nil {
return nil, err
}
imagV2, err := f.Mul(imagV, imagV)
if err != nil {
return nil, err
}
lenV2, err := f.Add(realV2, imagV2)
if err != nil {
return nil, err
}
lenV, err := f.Sqrt(lenV2)
if err != nil {
return nil, err
}
return lenV, nil
}
// Normal absolute value.
value, err := stablehlo.Abs(nodes[0].value)
if err != nil {
return nil, err
}
return f.newNode(value), nil
}
// Conj returns the conjugate of a complex number. E.g: Conj(1+3i) = 1-3i
func (f *Function) Conj(operand compute.Value) (compute.Value, error) {
nodes, err := f.verifyAndCastValues("Conj", operand)
if err != nil {
return nil, err
}
operandNode := nodes[0]
realValue, err := stablehlo.Real(operandNode.value)
if err != nil {
return nil, err
}
imagValue, err := stablehlo.Imag(operandNode.value)
if err != nil {
return nil, err
}
imagValue, err = stablehlo.Negate(imagValue)
if err != nil {
return nil, err
}
value, err := stablehlo.Complex(realValue, imagValue)
if err != nil {
return nil, err
}
return f.newNode(value), nil
}
// Clamp returns the element-wise clamping operation.
//
// The values max and min can either be a scalar or have the same shape as x.
func (f *Function) Clamp(min, a compute.Value, max compute.Value) (compute.Value, error) {
nodes, err := f.verifyAndCastValues("Clamp", min, a, max)
if err != nil {
return nil, err
}
minNode := nodes[0]
aNode := nodes[1]
maxNode := nodes[2]
value, err := stablehlo.Clamp(minNode.value, aNode.value, maxNode.value)
if err != nil {
return nil, err
}
return f.newNode(value), nil
}
// Gather is a powerful but cumbersome Gather operation. See details in the backend.
//
// Notice GoMLX backend Gather operation doesn't support batching axes, which StableHLO does.
// For compatibility, we simply leave them empty.
func (f *Function) Gather(operand, startIndices compute.Value, indexVectorAxis int, offsetOutputAxes, collapsedSliceAxes, startIndexMap, sliceSizes []int, indicesAreSorted bool) (compute.Value, error) {
nodes, err := f.verifyAndCastValues("Gather", operand, startIndices)
if err != nil {
return nil, err
}
operandNode := nodes[0]
startIndicesNode := nodes[1]
var operandBatchingAxes, startIndicesBatchingAxes []int
value, err := stablehlo.Gather(operandNode.value, startIndicesNode.value, indexVectorAxis,
offsetOutputAxes, collapsedSliceAxes, operandBatchingAxes,
startIndicesBatchingAxes, startIndexMap, sliceSizes, indicesAreSorted)
if err != nil {
return nil, err
}
return f.newNode(value), nil
}
// Concatenate operands on the given axis.
//
// All axes that are not being concatenated must match dimensions, except on the axes being concatenated.
// It doesn't work with scalars -- use ExpandAxes.
// If there is only one operand, it is returned and this is a no-op.
func (f *Function) Concatenate(axis int, operands ...compute.Value) (compute.Value, error) {
operandsNodes, err := f.verifyAndCastValues("Concatenate", operands...)
if err != nil {
return nil, err
}
operandsValues := make([]*stablehlo.Value, len(operandsNodes))
for i, node := range operandsNodes {
operandsValues[i] = node.value
}
value, err := stablehlo.Concatenate(axis, operandsValues...)
if err != nil {
return nil, err
}
return f.newNode(value), nil
}
// Where implements compute.Function interface.
func (f *Function) Where(condition, onTrue, onFalse compute.Value) (compute.Value, error) {
operandsNodes, err := f.verifyAndCastValues("Where", condition, onTrue, onFalse)
if err != nil {
return nil, err
}
conditionN, onTrueN, onFalseN := operandsNodes[0], operandsNodes[1], operandsNodes[2]
// Where allows onTrue and onFalse to be broadcast automatically if they are scalars, while stablehlo.Select doesn't.
// We perform their broadcasting here but leave the condition broadcasting to be handled by stablehlo.Select.
outputDims := conditionN.shape.Dimensions
if !onTrueN.shape.IsScalar() {
outputDims = onTrueN.shape.Dimensions
}
if !onFalseN.shape.IsScalar() {
outputDims = onFalseN.shape.Dimensions
}
if onTrueN.shape.IsScalar() && len(outputDims) > 0 {
onTrue, err = f.BroadcastInDim(onTrue, shapes.Make(onTrueN.shape.DType, outputDims...), nil)
if err != nil {
return nil, errors.WithMessage(err, "while broadcasting onTrue for op Where()")
}
onTrueN = onTrue.(*Node)
}
if onFalseN.shape.IsScalar() && len(outputDims) > 0 {
onFalse, err = f.BroadcastInDim(onFalse, shapes.Make(onTrueN.shape.DType, outputDims...), nil)
if err != nil {
return nil, errors.WithMessage(err, "while broadcasting onFalse for op Where()")
}
onFalseN = onFalse.(*Node)
}
// Where operation is called Select in stablehlo.
value, err := stablehlo.Select(conditionN.value, onTrueN.value, onFalseN.value)
if err != nil {
return nil, err
}
return f.newNode(value), nil
}
// IsNaN implements compute.Function interface.
func (f *Function) IsNaN(x compute.Value) (compute.Value, error) {
result, err := f.NotEqual(x, x)
if err != nil {
return nil, errors.WithMessage(err, "while building op IsNaN")
}
return result, nil
}
const stableHLOConstantOp = "Constant"
// Bitcast implements compute.Function interface.
func (f *Function) Bitcast(x compute.Value, targetDType dtypes.DType) (compute.Value, error) {
nodes, err := f.verifyAndCastValues("Bitcast", x)
if err != nil {
return nil, err
}
xValue := nodes[0].value
if targetDType.IsPacked() && xValue.OpName() == stableHLOConstantOp {
return nil, errors.Errorf("Cannot bitcast constant value to packed sub-byte type %s (x.Shape is %s): see details in "+
"https://github.com/openxla/xla/issues/38964", targetDType, xValue.Shape())
}
value, err := stablehlo.BitcastConvert(nodes[0].value, targetDType)
if err != nil {
return nil, err
}
return f.newNode(value), nil
}
// Transpose implements compute.Function interface.
// It transposes input tensor x according to the given permutation axes.
func (f *Function) Transpose(x compute.Value, permutation ...int) (compute.Value, error) {
nodes, err := f.verifyAndCastValues("Transpose", x)
if err != nil {
return nil, err
}
value, err := stablehlo.Transpose(nodes[0].value, permutation...)
if err != nil {
return nil, err
}
return f.newNode(value), nil
}
// RNGBitGenerator generates the given shape filled with random bits.
//
// It takes as input a state (usually [3]uint64) and returns the updated state and the generated values (with random bits).
//
// Currently, the backend only supports the Philox algorithm. See https://dl.acm.org/doi/10.1145/2063384.2063405
func (f *Function) RNGBitGenerator(state compute.Value, shape shapes.Shape) (newState compute.Value, values compute.Value, err error) {
nodes, err := f.verifyAndCastValues("RNGBitGenerator", state)
if err != nil {
return nil, nil, err
}
shloShape := ShapeToXLA(shape)
if !shloShape.Ok() {
return nil, nil, errors.Errorf("RNGBitGenerator: invalid shape: %s", shape)
}
newStateV, valueV, err := stablehlo.RNGBitGenerator(nodes[0].value, shloShape, stablehlotypes.RNGPhilox)
if err != nil {
return nil, nil, err
}
return f.newNode(newStateV), f.newNode(valueV), nil
}
// ConvertDType implements compute.Function interface.
func (f *Function) ConvertDType(x compute.Value, dtype dtypes.DType) (compute.Value, error) {
nodes, err := f.verifyAndCastValues("ConvertDType", x)
if err != nil {
return nil, err
}
output, err := stablehlo.Convert(nodes[0].value, dtype)
if err != nil {
return nil, err
}
return f.newNode(output), nil
}
// Pad injects padding on the start, end, or interior (in between each element) of the given operand.
// There must be at most `operand.Rank()` axesConfig values. Missing PadAxis are assumed to be zeros,
// that is, no padding for those axes.
func (f *Function) Pad(x, fillValue compute.Value, axesConfig ...compute.PadAxis) (compute.Value, error) {
nodes, err := f.verifyAndCastValues("ConvertDType", x, fillValue)
if err != nil {
return nil, err
}
xN := nodes[0]
if xN.shape.IsScalar() {
// No axes to be padded.
return x, nil
}
rank := xN.shape.Rank()
if len(axesConfig) > rank {
return nil, errors.Errorf("Pad: too many axesConfig values: %d > x.Rank()=%d", len(axesConfig), rank)
}
padStart, padEnd, padInterior := make([]int, rank), make([]int, rank), make([]int, rank)
for i, axisConfig := range axesConfig {
padStart[i] = axisConfig.Start
padEnd[i] = axisConfig.End
padInterior[i] = axisConfig.Interior
}
output, err := stablehlo.Pad(xN.value, nodes[1].value, padStart, padEnd, padInterior)
if err != nil {
return nil, err
}
return f.newNode(output), nil
}
// ConvGeneral is a generic Convolution operation with arbitrary number of spatial axes, strides,
// paddings, dilations, and grouping.
//
// Arguments:
//
// - input: it must have one batch and one channel axis, and arbitrary number of spatial axes.
// - kernel: its rank must match the input's spatial axes.
// - axes: defines how the axes of input and kernel are mapped.
// - strides: stride of the convolution window, how it moves. If set, one value per spatial axis,
// and values must be >= 1. If not set, strides default to 1.
// - paddings: padding applied to the start and end of each axis of the input.
// If nil, it defaults to no padding.
// - inputDilations: "virtually" expand the input by inserting `2-1` copies of `0` (or whatever
// is the reduciton "zero" value) between the elements in each dimension.
// If nil, it's assumed to be 1 (no dilation) for each axis. Values must be >= 1.
// - kernelDilations: "virtually" expand the kernel by inserting `2-1` copies of `0` between the
// elements in each dimension.
// If nil, it's assumed to be 1 (no dilation) for each axis. Values must be >= 1.
// Also known as "atrous convolution".
// - channelGroupCount: number of input channels to group together for the convolution.
// (aka "grouped convolution"). If <= 1 it's disabled.
// - batchGroupCount: number of input batches to group together for the convolution.
// If <= 1 it's disabled.
//
// There is a more detailed description in https://www.tensorflow.org/xla/operation_semantics#convwithgeneralpadding_convolution.
// Also useful, https://arxiv.org/pdf/1603.07285v1.pdf.
// Note:
// - Another common term for "channels" is "features".
// - "Kernel" is also commonly called "weights" or "filters".
func (f *Function) ConvGeneral(input, kernel compute.Value,
axes compute.ConvolveAxesConfig, strides []int, paddings [][2]int,
inputDilations, kernelDilations []int, channelGroupCount, batchGroupCount int) (compute.Value, error) {
nodes, err := f.verifyAndCastValues("ConvGeneral", input, kernel)
if err != nil {
return nil, err
}
inputN, kernelN := nodes[0], nodes[1]
spatialRank := inputN.shape.Rank() - 2
if spatialRank < 0 {
return nil, errors.Errorf("ConvGeneral: invalid input shape %s, it requires one batch and one channel axis, "+
"plus at least one spatial dimension", inputN.shape)
}
// Set default values to specification.
if len(strides) == 0 {
strides = xslices.SliceWithValue(spatialRank, 1)
}
if len(paddings) == 0 {
paddings = xslices.SliceWithValue(spatialRank, [2]int{0, 0})
}
if len(inputDilations) == 0 {
inputDilations = xslices.SliceWithValue(spatialRank, 1)
}
if len(kernelDilations) == 0 {
kernelDilations = xslices.SliceWithValue(spatialRank, 1)
}
if channelGroupCount <= 1 {
channelGroupCount = 1
}
if batchGroupCount <= 1 {
batchGroupCount = 1
}
output, err := stablehlo.Convolution(inputN.value, kernelN.value,
strides, paddings, inputDilations, kernelDilations,
axes.InputBatch, axes.InputChannels, axes.InputSpatial,
axes.KernelInputChannels, axes.KernelOutputChannels, axes.KernelSpatial,
axes.OutputBatch, axes.OutputChannels, axes.OutputSpatial,
channelGroupCount, batchGroupCount,
stablehlotypes.DotGeneralPrecisionDefault, stablehlotypes.DotGeneralPrecisionDefault)
if err != nil {
return nil, err
}
return f.newNode(output), nil
}
// Reverse implements the compute.Function interface.
func (f *Function) Reverse(x compute.Value, axes ...int) (compute.Value, error) {
nodes, err := f.verifyAndCastValues("Reverse", x)
if err != nil {
return nil, err
}
xN := nodes[0]
output, err := stablehlo.Reverse(xN.value, axes...)
if err != nil {
return nil, err
}
return f.newNode(output), nil
}
// FFT implements the Fast Fourier Transform operation.
// fftType specifies the type of FFT operation to perform.
// fftLength specifies the length of the transform for each axis.
func (f *Function) FFT(x compute.Value, fftType compute.FFTType, fftLength []int) (compute.Value, error) {
nodes, err := f.verifyAndCastValues("FFT", x)
if err != nil {
return nil, err
}
xNode := nodes[0]
value, err := stablehlo.FFT(xNode.value, stablehlotypes.FFTType(fftType), fftLength...)
if err != nil {
return nil, err
}
return f.newNode(value), nil
}
// extractStartIndexValues extracts start index values from nodes, handling both 1D tensor and scalar cases
func (f *Function) extractStartIndexValues(startIndexNodes []*Node, rank int) ([]*stablehlo.Value, error) {
var startIndexValues []*stablehlo.Value
if len(startIndexNodes) == 1 && !startIndexNodes[0].shape.IsScalar() && startIndexNodes[0].shape.Rank() == 1 {
// Special case: single 1D start indices tensor
for i := range rank {
sliced, err := stablehlo.Slice(startIndexNodes[0].value, []int{i}, []int{i + 1}, []int{1})
if err != nil {
return nil, err
}
reshaped, err := stablehlo.Reshape(sliced, stablehloshapes.Make(startIndexNodes[0].shape.DType))
if err != nil {
return nil, err
}
startIndexValues = append(startIndexValues, reshaped)
}
} else {
// Normal case: one scalar tensor per axis
startIndexValues = make([]*stablehlo.Value, len(startIndexNodes))
for i, node := range startIndexNodes {
startIndexValues[i] = node.value
}
}
return startIndexValues, nil
}
// DynamicSlice extracts a slice from the operand at the startIndices position and the given sliceSizes.
//
// - operand: tensor from where to take the slice.
// - startIndices: scalar tensors, one per axis of operand: len(startIndices) == operand.Rank().
// - sliceSizes: static values and fixed to keep the shape of the output static.
//
// The startIndices are adjusted as follows:
//
// adjustedStartIndices[i] = clamp(0, StartIndices[i], operand.Dimensions[i] - sliceSizes[i])
//
// See description in https://openxla.org/xla/operation_semantics#dynamicslice
func (f *Function) DynamicSlice(operand compute.Value, startIndices []compute.Value, sliceDims []int) (compute.Value, error) {
nodes, err := f.verifyAndCastValues("DynamicSlice", append([]compute.Value{operand}, startIndices...)...)
if err != nil {
return nil, err
}
operandNode := nodes[0]
startIndexValues, err := f.extractStartIndexValues(nodes[1:], operandNode.shape.Rank())
if err != nil {
return nil, err
}
value, err := stablehlo.DynamicSlice(operandNode.value, startIndexValues, sliceDims)
if err != nil {
return nil, err
}
return f.newNode(value), nil
}
// DynamicUpdateSlice updates the operand with the values given in update, at the position given by startIndices.
//
// - operand: original value that to be updated.
// - update: values to "paste" on top of operand, at position startIndices.
// - startIndices: scalar tensors, one per axis of operand: len(startIndices) == operand.Rank().
// - sliceSizes: static values and fixed to keep the shape of the output static.
//
// It returns a value with the same shape as the operand, with the values updated.
//
// The startIndices are adjusted as follows:
//
// adjustedStartIndices[i] = clamp(0, StartIndices[i], operand.Dimensions[i] - update.Dimensions[i])
func (f *Function) DynamicUpdateSlice(operand, update compute.Value, startIndices []compute.Value) (compute.Value, error) {
nodes, err := f.verifyAndCastValues("DynamicUpdateSlice", append([]compute.Value{operand, update}, startIndices...)...)
if err != nil {
return nil, err
}
operandNode := nodes[0]
updateNode := nodes[1]
startIndexValues, err := f.extractStartIndexValues(nodes[2:], operandNode.shape.Rank())
if err != nil {
return nil, err
}
value, err := stablehlo.DynamicUpdateSlice(operandNode.value, updateNode.value, startIndexValues)
if err != nil {
return nil, err
}
return f.newNode(value), nil
}
// BatchNormForInference implements compute.Function interface.
func (f *Function) BatchNormForInference(input, scale, offset, mean, variance compute.Value, epsilon float32, featureAxis int) (compute.Value, error) {
nodes, err := f.verifyAndCastValues("BatchNormForInference", input, scale, offset, mean, variance)
if err != nil {
return nil, err
}
inputN, scaleN, offsetN, meanN, varN := nodes[0], nodes[1], nodes[2], nodes[3], nodes[4]
value, err := stablehlo.BatchNormInference(inputN.value, scaleN.value, offsetN.value, meanN.value, varN.value, epsilon, featureAxis)
if err != nil {
return nil, err
}
return f.newNode(value), nil
}
// BatchNormForTraining implements compute.Function interface.
func (f *Function) BatchNormForTraining(input, scale, offset compute.Value, epsilon float32, featureAxis int) (output, batchMean, batchVar compute.Value, err error) {
nodes, err := f.verifyAndCastValues("BatchNormForTraining", input, scale, offset)
if err != nil {
return nil, nil, nil, err
}
inputN, scaleN, offsetN := nodes[0], nodes[1], nodes[2]
outputV, batchMeanV, batchVarV, err := stablehlo.BatchNormTraining(inputN.value, scaleN.value, offsetN.value, epsilon, featureAxis)
if err != nil {
return nil, nil, nil, err
}
return f.newNode(outputV), f.newNode(batchMeanV), f.newNode(batchVarV), nil
}
// BatchNormGradient implements compute.Function interface.
func (f *Function) BatchNormGradient(gradOutput, input, scale, mean, variance compute.Value, epsilon float32, featureAxis int) (gradInput, gradScale, gradOffset compute.Value, err error) {
nodes, err := f.verifyAndCastValues("BatchNormGradient", gradOutput, input, scale, mean, variance)
if err != nil {
return nil, nil, nil, err
}
gradOutputN, inputN, scaleN, meanN, varN := nodes[0], nodes[1], nodes[2], nodes[3], nodes[4]
gradInputV, gradScaleV, gradOffsetV, err := stablehlo.BatchNormGradient(gradOutputN.value, inputN.value, scaleN.value, meanN.value, varN.value, epsilon, featureAxis)
if err != nil {
return nil, nil, nil, err
}
return f.newNode(gradInputV), f.newNode(gradScaleV), f.newNode(gradOffsetV), nil
}
// Call implements the compute.Function interface.
func (f *Function) Call(target compute.Function, inputs ...compute.Value) ([]compute.Value, error) {
nodes, err := f.verifyAndCastValues("Call", inputs...)
if err != nil {
return nil, err
}
targetF, ok := target.(*Function)
if !ok {
return nil, errors.Errorf("Call target function must be of type *xla.Function, but got %T", target)
}
if targetF.builder != f.builder {
return nil, errors.Errorf("Call target function must be from the same builder")
}
inputValues := make([]*stablehlo.Value, len(nodes))
for i, n := range nodes {
inputValues[i] = n.value
}
outputValues, err := stablehlo.Call(targetF.fn, inputValues...)
if err != nil {
return nil, err
}
outputNodes := make([]compute.Value, len(outputValues))
for i, v := range outputValues {
outputNodes[i] = f.newNode(v)
}
return outputNodes, nil
}
// OptimizationBarrier returned values are identity to the operands, but they become only available
// in compilation time once all the inputs are calculated.
func (f *Function) OptimizationBarrier(operands ...compute.Value) ([]compute.Value, error) {
if len(operands) == 0 {
return nil, errors.New("OptimizationBarrier requires at least one operand")
}
if len(operands) == 1 && f.builder.backend.plugin.IsCPU() {
// No-op: XLA CPU compiler has a bug with single-operand OptimizationBarrier and produces garbage/crashes.
return operands, nil
}
nodes, err := f.verifyAndCastValues("OptimizationBarrier", operands...)
if err != nil {
return nil, err
}
operandValues := xslices.Map(nodes, func(n *Node) *stablehlo.Value { return n.value })
values, err := stablehlo.OptimizationBarrier(operandValues...)
if err != nil {
return nil, err
}
outputNodes := xslices.Map(values, func(v *stablehlo.Value) compute.Value { return f.newNode(v) })
return outputNodes, nil
}
// SchedulingBarrier introduces a scheduling barrier.
// Returned value is identity to the operand, but it is guaranteed to depend on all the dependencies.
//
// Since XLA/StableHLO don't support this operation, we "hack" it by means of a no-op: taking a scalar of each depdency,
// multiply by 0 and add them to the operand.
//
// See: https://github.com/openxla/stablehlo/issues/2923.
func (f *Function) SchedulingBarrier(operand compute.Value, dependencies ...compute.Value) (compute.Value, error) {
if len(dependencies) == 0 {
return operand, nil
}
nodes, err := f.verifyAndCastValues("SchedulingBarrier", append([]compute.Value{operand}, dependencies...)...)
if err != nil {
return nil, err
}
opNode := nodes[0]
depNodes := nodes[1:]
opDType := opNode.shape.DType
// Create a constant zero of the operand's dtype.
zeroFlat := reflect.MakeSlice(reflect.SliceOf(opDType.GoType()), 1, 1)
zeroConst, err := f.Constant(zeroFlat.Interface())
if err != nil {
return nil, errors.WithMessage(err, "SchedulingBarrier: failed to create constant zero")
}
// Add OptimizationBarrier on the constant zero so it doesn't get optimized away.
zeroBarriers, err := f.OptimizationBarrier(zeroConst)
if err != nil {
return nil, errors.WithMessage(err, "SchedulingBarrier: failed to apply OptimizationBarrier to zero")
}
zero := zeroBarriers[0]
accumulatedZero := zero
for _, dep := range depNodes {
var scalarDep compute.Value
if dep.shape.Rank() == 0 {
scalarDep = dep
} else {
starts := make([]int, dep.shape.Rank())
limits := xslices.SliceWithValue(dep.shape.Rank(), 1)
slice, err := f.Slice(dep, starts, limits, nil)
if err != nil {
return nil, err
}
scalarDep, err = f.Reshape(slice)
if err != nil {
return nil, err
}
}
if dep.shape.DType != opDType {
var err error
scalarDep, err = f.ConvertDType(scalarDep, opDType)
if err != nil {
return nil, err
}
}
// Multiply the scalar dependency by the constant zero.
term, err := f.Mul(scalarDep, zero)
if err != nil {
return nil, err
}
// Add it to our accumulated zero.
accumulatedZero, err = f.Add(accumulatedZero, term)
if err != nil {
return nil, err
}
}
// Finally, add the accumulated zero (which depends on all dependencies) to the operand.
operandReady, err := f.Add(operand, accumulatedZero)
if err != nil {
return nil, err
}
return operandReady, nil
}