0 | {--
1 | Copyright (C) 2021 Joel Berkeley
3 | This program is free software: you can redistribute it and/or modify
4 | it under the terms of the GNU Affero General Public License as published
5 | by the Free Software Foundation, either version 3 of the License, or
6 | (at your option) any later version.
8 | This program is distributed in the hope that it will be useful,
9 | but WITHOUT ANY WARRANTY; without even the implied warranty of
10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 | GNU Affero General Public License for more details.
13 | You should have received a copy of the GNU Affero General Public License
14 | along with this program. If not, see <https://www.gnu.org/licenses/>.
15 | --}
16 | ||| Defines `Tensor`, an array of numbers or booleans, along with a number of functions operating on
17 | ||| `Tensor`s. `Tensor` operations typically leverage hardware acceleration and graph compilation.
18 | ||| spidr tracks tensor shape and data type in the types, so you can be sure that if your tensor
19 | ||| code compiles, these are consistent.
20 | |||
21 | ||| spidr achieves efficient reuse of tensor computations with `Tag`. See the tutorial
22 | ||| _Nuisances in the Tensor API_ for a discussion of pitfalls to avoid when using `Tag`.
43 | ||| A scalar or array. Construct a `Tensor` with function `tensor`.
44 | export
54 | ||| The effect of tagging nodes in a computational graph.
55 | export
63 | export
67 | export
72 | export
76 | export
82 | ||| Mark an expression to be efficiently reused. For example, in
83 | ||| ```
84 | ||| bad : Tensor [9999999] F64
85 | ||| bad = let x = fill {shape = [9999999]} 1.0 in x + x
86 | |||
87 | ||| good : Tag $ Tensor [9999999] F64
88 | ||| good = do x <- tag $ fill {shape = [9999999]} 1.0
89 | ||| pure (x + x)
90 | ||| ```
91 | ||| the large vector `x` is calculated twice in `bad`, but once in `good`, as `tag` marks it for
92 | ||| sharing.
93 | |||
94 | ||| Types that implement this interface should `tag` constituent components it deems worth sharing.
95 | ||| For example, see the implementation for tuples.
96 | |||
97 | ||| See tutorial _Nuisances in the Tensor API_ for details.
103 | export
107 | export
117 | ||| Construct a `Tensor` from `Literal` data. For example
118 | ||| ```
119 | ||| x : Tensor [2, 3] S32
120 | ||| x = tensor [[1, 2, 3],
121 | ||| [4, 5, 6]]
122 | ||| ```
123 | export
128 | export
135 | %hide Literal.All2.All2
137 | ||| A function type. For example `Func [Nat, String] Bool` is `Nat -> String -> Bool`.
155 | forall arity, rshapes, rdtypes . (shapes : Vect arity Shape) -> (dtypes : Vect arity DType) ->
174 | where
206 | rightAppIsMapAppRight f g (x :: xs) (y :: ys) = cong2 (::) Refl (rightAppIsMapAppRight f g xs ys)
208 | ||| Just-in-time compile a tensor graph into a function that can be cheaply applied to arbitrary
209 | ||| literals of the correct shapes and element types. For example,
210 | ||| ```
211 | ||| f : Tensor [2] F64 -> Tag $ All2 Tensor [[2], []] [F64, F64]
212 | ||| f x = do
213 | ||| y <- reduce @{Sum} [0] x
214 | ||| pure [-x, y]
215 | |||
216 | ||| g : Device -> IO ()
217 | ||| g dev =
218 | ||| do fJit <- jit {shapes = [_], dtypes = [_]} dev f
219 | ||| let [x, y] = fJit [-1.0, 4.0]
220 | ||| printLn x -- [1.0, -4.0]
221 | ||| printLn y -- 3.0
222 | ||| ```
223 | ||| While `eval` needs to be recompiled every time you want to change tensor values, `jit`
224 | ||| compiles the function *once*, then reuses it for each application.
249 | ||| Convenience alias for `jit` on constant tensor graphs.
250 | |||
251 | ||| **Note:** Each call to `eval` will compile the tensor graph, even if the graphs only differ
252 | ||| in the values of their tensor constants. This can be expensive. If you want to efficiently
253 | ||| run your graph for different tensor values, use `jit` and make these tensor values
254 | ||| arguments to the graph.
255 | |||
256 | ||| In contrast to `Tensor.eval` when called on multiple tensors, this function constructs and
257 | ||| compiles the graph just once.
265 | ||| A convenience alias for `List.Tag.eval` when no tagging is used.
275 | ||| A convenience alias for `List.Tag.eval` for a single tensor.
280 | ||| A convenience alias for `List.Tag.eval` for a single tensor and no tagging.
285 | ||| A string representation of a tensor graph.
286 | |||
287 | ||| There are no guarantees whatsoever as to the string structure and contents.
297 | ||| Positive infinity.
298 | export
301 | ||| NaN (not a number).
302 | export
306 | ||| Compares less than or equal to any other value (except NaN).
307 | export
319 | ||| Compares greater than or equal to any other value (except NaN).
320 | export
332 | ||| The most negative possible finite float, approx. -1.8e308
333 | export
337 | ||| The most positive possible finite float, approx. 1.8e308
338 | export
342 | ||| Cast the element type. For example, `castDtype (tensor {dtype = S32} [1, -2])` is
343 | ||| `tensor {dtype = F64} [1.0, -2.0]`.
344 | export
360 | covering
377 | where
394 | ||| Function abstraction in the framework IR.
395 | |||
396 | ||| Operations in spidr are, by default, inlined, which can lead to large IRs. `func` abstracts
397 | ||| (and names) a function in the IR. The resulting function will have the exact same semantics as
398 | ||| the input, though may perform differently, depending on ML compiler behaviour.
399 | |||
400 | ||| Implementation note: MLIR (more specifically the "func" dialect) only supports named functions
401 | ||| at the top (or module) level. To achieve this, `func` lifts its function to the top level,
402 | ||| and automatically converts any variable capture to new function arguments. As such, the IR
403 | ||| may appear different to the Idris code from which it derives.
415 | where
420 | ||| (Experimental) function vectorization.
421 | |||
422 | ||| Lift a function on tensors, so that it applies element-wise across the common leading dimension
423 | ||| of its tensor arguments. For example, for
424 | ||| ```
425 | ||| xs : Tensor [2, 3, 3] S32
426 | ||| xs = tensor [[[ 0, 1, 2],
427 | ||| [ 3, 4, 5],
428 | ||| [ 6, 7, 8]],
429 | ||| [[ 9, 10, 11],
430 | ||| [12, 13, 14],
431 | ||| [15, 16, 17]]]
432 | |||
433 | ||| ys : Tensor [2] S32
434 | ||| ys = Tensor [2, -1]
435 | ||| ```
436 | ||| `do !(vmap (\x, y => pure $ y * diag x)) xs ys` produces `tensor [[0, 8, 16], [-9, -13, -17]]`.
437 | |||
438 | ||| **Warning:** `vmap` is experimental, and only implemented for a subset of the tensor API. You
439 | ||| can see approximately which operations are supported on the Enzyme [tracking issue](https://github.com/EnzymeAD/Enzyme-JAX/issues/152).
453 | where
460 | ||| (Experimental) reverse-mode automatic differentiation.
461 | |||
462 | ||| `grad` can be applied repeatedly to obtain higher derivatives, though we do not yet support
463 | ||| derivatives of vector-valued functions.
464 | |||
465 | ||| For example, for
466 | ||| ```
467 | ||| f : Tensor [2] F64 -> Tag $ Tensor [] F64
468 | ||| f x = do
469 | ||| x <- tag x
470 | ||| let (x0, x1) = (slice [at 0] x, slice [at 1] x)
471 | ||| pure $ x0 / x1
472 | ||| ```
473 | ||| `grad f (tensor [3.0, 2.0])` produces `tensor [0.5, -0.75]`.
474 | |||
475 | ||| **Warning:** `grad` is experimental, and only implemented for a subset of the tensor API. You
476 | ||| can see approximately which operations are supported on the Enzyme [tracking issue](https://github.com/EnzymeAD/Enzyme-JAX/issues/88).
478 | grad : (Tensor shape F64 -> Tag $ Tensor [] F64) -> Tensor shape F64 -> Tag $ Tensor shape F64
481 | %hide Prelude.Interfaces.product
493 | export
498 | ||| Reshape a `Tensor`. For example, `reshape {to = [2, 1]} (tensor [3, 4])` is
499 | ||| `tensor [[3], [4]]`. The output can have a different rank to the input.
500 | export
508 | ||| Add a dimension of length one at the specified `axis`. The new dimension will be at the
509 | ||| specified `axis` in the new `Tensor` (as opposed to the original `Tensor`). For example,
510 | ||| `expand 1 $ tensor [[1, 2], [3, 4], [5, 6]]` is `tensor [[[1, 2]], [[3, 4]], [[5, 6]]]`.
511 | export
520 | ||| A `Squeezable from to` constitutes proof that the shape `from` can be squeezed to the
521 | ||| shape `to`. Squeezing is the process of removing any number of dimensions of length one.
524 | ||| Proof that a shape can be squeezed to itself. For example:
525 | |||
526 | ||| [] to []
527 | ||| [3, 4] to [3, 4]
530 | ||| Proof that any dimensions (including those of length 1) can be preserved in the process of
531 | ||| squeezing. For example:
532 | |||
533 | ||| ...
536 | ||| Proof that any dimensions of length one can be squeezed out. For example:
537 | |||
538 | ||| [1, 3, 1, 1, 4] to [3, 4]
541 | ||| Remove dimensions of length one from a `Tensor` such that it has the desired shape. For example:
542 | |||
543 | ||| ```
544 | ||| x : Tensor [2, 1, 3, 1] S32
545 | ||| x = tensor [[[[4], [5], [6]]],
546 | ||| [[[7], [8], [9]]]]
547 | |||
548 | ||| y : Tensor [2, 1, 3] S32
549 | ||| y = squeeze x
550 | ||| ```
551 | ||| is
552 | ||| ```
553 | ||| y : Tensor [2, 1, 3] S32
554 | ||| y = tensor [[[4, 5, 6]],
555 | ||| [[7, 8, 9]]]
556 | ||| ```
557 | export
565 | ||| A `SliceOrIndex d` is a valid slice or index into a dimension of size `d`. See `slice` for
566 | ||| details.
567 | export
579 | ||| Index at `idx`. See `slice` for details.
585 | ||| Index at the specified index. See `slice` for details.
590 | ||| Slice from `from` (inclusive) to `to` (exclusive). See `slice` for details.
600 | ||| Slice `size` elements starting at the specified scalar `U64` index. See `slice` for details.
605 | ||| Slice across all indices along an axis. See `slice` for details.
610 | ||| A `MultiSlice shape` is a valid multi-dimensional slice into a tensor with shape `shape`.
611 | ||| See `slice` for details.
618 | ||| The shape of a tensor produced by slicing with the specified multi-dimensional slice. See
619 | ||| `Tensor.slice` for details.
628 | ||| Slice or index `Tensor` axes. Each axis can be sliced or indexed, and this can be done with
629 | ||| either static (`Nat`) or dynamic (scalar `U64`) indices.
630 | |||
631 | ||| **Static indices**
632 | |||
633 | ||| Static indices are `Nat`s. For example, for
634 | ||| ```
635 | ||| x : Tensor [5, 6] S32
636 | ||| x = tensor [[ 0, 1, 2, 3, 4, 5],
637 | ||| [ 6, 7, 8, 9, 10, 11],
638 | ||| [12, 13, 14, 15, 16, 17],
639 | ||| [18, 19, 20, 21, 22, 23],
640 | ||| [24, 25, 26, 27, 28, 29]]
641 | ||| ```
642 | ||| we can index as `slice [at 1] x` to get
643 | ||| ```
644 | ||| x : Tensor [6] S32
645 | ||| x = tensor [6, 7, 8, 9, 10, 11]
646 | ||| ```
647 | ||| or we can slice as `slice [2.to 4] x` to get
648 | ||| ```
649 | ||| x : Tensor [2, 6] S32
650 | ||| x = tensor [[12, 13, 14, 15, 16, 17],
651 | ||| [18, 19, 20, 21, 22, 23]]
652 | ||| ```
653 | ||| Note that in `2.to 4`, the 2 is inclusive, and the 4 exclusive, so we return indices 2 and 3.
654 | |||
655 | ||| **Dynamic indices**
656 | |||
657 | ||| Dynamic indices are scalar `U64` values, and the API works slightly differently because we
658 | ||| can't know the value of dynamic indices until the graph is executed. For indexing, with scalar
659 | ||| `U64` index `i` in `slice [at i] x`, `i` is clamped to be a valid index into that dimension.
660 | ||| For example, for `i = tensor 1`, `slice [at i] x` is
661 | ||| ```
662 | ||| x : Tensor [6] S32
663 | ||| x = tensor [6, 7, 8, 9, 10, 11]
664 | ||| ```
665 | ||| as in the static case. However, for `i = tensor 10`, `slice [at i] x` returns the last row
666 | ||| ```
667 | ||| x : Tensor [6] S32
668 | ||| x = tensor [24, 25, 26, 27, 28, 29]
669 | ||| ```
670 | ||| We can also slice by specifying a scalar `U64` start index, and a static size, as
671 | ||| `slice [i.size 2] x` with `i = tensor 2` to get
672 | ||| ```
673 | ||| x : Tensor [2, 6] S32
674 | ||| x = tensor [[12, 13, 14, 15, 16, 17],
675 | ||| [18, 19, 20, 21, 22, 23]]
676 | ||| ```
677 | ||| For a given slice `size`, the dynamic start index is clamped such that we always get `size`
678 | ||| elements along that axis. For example, `slice [i.size 2] x` with `i = tensor 4` is
679 | ||| ```
680 | ||| x : Tensor [2, 6] S32
681 | ||| x = tensor [[18, 19, 20, 21, 22, 23],
682 | ||| [24, 25, 26, 27, 28, 29]]
683 | ||| ```
684 | ||| which starts at index 3 rather than index 4.
685 | |||
686 | ||| **Mixed static, dynamic, slicing and indexing**
687 | |||
688 | ||| Each axis can only be sliced or indexed, and must use only static or dynamic indices. However,
689 | ||| across axes, we can mix these four arbitrarily. For example, with `slice [2.to 4, at 1] x` to
690 | ||| get
691 | ||| ```
692 | ||| x : Tensor [2] S32
693 | ||| x = tensor [13, 19]
694 | ||| ```
695 | ||| or with `i = tensor 2` in `slice [at 1, i.size 2] x` to get
696 | ||| ```
697 | ||| x : Tensor [2] S32
698 | ||| x = tensor [7, 8]
699 | ||| ```
700 | |||
701 | ||| Slices and indices apply to the leading axes of the tensor. For trailing axes omitted from the
702 | ||| multi-dimensional slice, the whole of the axis is returned. If we want to slice or index over
703 | ||| later axes and retain all indices in a leading axis, we can use the convenience function `all`,
704 | ||| as `slice [all, at 3] x` to get
705 | ||| ```
706 | ||| x : Tensor [5] S32
707 | ||| x = tensor [[3], [9], [15], [21], [27]]
708 | ||| ```
709 | ||| This is exactly the same as the more manual `slice [0.to 5, at 3] x` and
710 | ||| `slice [(tensor 0).size 5, at 3] x`.
711 | |||
712 | ||| @at The multi-dimensional slices and indices at which to slice the tensor.
713 | export
716 | let x = val0 $ Slice (mapd start (const 0) at) (mapd stop id at) (replicate (length shape) 1) x
717 | -- we shortcut DynamicSlice to allow autodiff for static slicing
721 | where
761 | ||| The starting indices of a slice with shape `sizes`, into a tensor with shape `bounds`.
762 | export
777 | ||| Scalar.
783 | ||| A `Nat` starting index. This is statically restricted to be in bounds.
793 | ||| A scalar `U64` starting index. This is dynamically truncated to be in bounds.
802 | ||| Replace a slice of a tensor. For example, for
803 | ||| ```
804 | ||| target : Tensor [3, 4] S32
805 | ||| target = tensor [[ 0, 1, 2, 3],
806 | ||| [ 4, 5, 6, 7],
807 | ||| [ 8, 9, 10, 11]]
808 | |||
809 | ||| update : Tensor [2, 2] S32
810 | ||| update = tensor [[12, 13],
811 | ||| [14, 15]]
812 | ||| ```
813 | ||| `updateSlice [0, 1] update target` is
814 | ||| ```
815 | ||| y : Tensor [3, 4] S32
816 | ||| y = tensor [[ 0, 12, 13, 3],
817 | ||| [ 4, 14, 15, 7],
818 | ||| [ 8, 9, 10, 11]]
819 | ||| ```
820 | ||| The starting index can be specified along each axis using either a `Nat` or a `U64` scalar.
821 | ||| Note that the updated slice will always be replaced by `update`, and lie fully within `target`.
822 | ||| This is checked statically for `Nat`. However, for `U64` scalar, the start index `at` is
823 | ||| truncated to the maximum index for which the slice remains within `target`. For example,
824 | ||| `updateSlice [0, (tensor $ Scalar 2)] update target` and
825 | ||| `updateSlice [0, (tensor $ Scalar 3)] update target`
826 | ||| ```
827 | ||| are both
828 | ||| ```
829 | ||| y : Tensor [3, 4] S32
830 | ||| y = tensor [[ 0, 1, 12, 13],
831 | ||| [ 4, 5, 14, 15],
832 | ||| [ 8, 9, 10, 11]]
833 | ||| ```
834 | |||
835 | ||| @at The starting indices of the slice to replace.
836 | ||| @update The tensor to replace the slice with.
837 | ||| @target The tensor in which to replace the slice.
838 | export
847 | where
876 | ||| Concatenate tensors along a common axis. For example,
877 | ||| `concat {leading = []} [tensor [[1, 2]], tensor [[3, 4]], tensor [[5, 6]]]` and
878 | ||| `concat {leading = [_]} [tensor [[1], [3], [5]], tensor [[2], [4], [6]]]` are both
879 | ||| `tensor [[1, 2], [3, 4], [5, 6]]`.
880 | export
885 | -- much of this function is a workaround for https://github.com/idris-lang/Idris2/issues/3860
890 | in t0 {shape} $ Concat (length leading) (x.value :: fromList (forget $ mapProperty (.value) xs))
892 | ||| Transpose a matrix. For example, `(tensor [[1, 2], [3, 4]]).T` is `tensor [[1, 3], [2, 4]]`.
893 | export
897 | ||| Transpose axes of a tensor. This is a more general version of `(.T)`, in which you can
898 | ||| transpose any number of axes in a tensor of arbitrary rank. The i'th axis in the resulting
899 | ||| tensor corresponds to the `index i ordering`'th axis in the input tensor. For example, for
900 | ||| ```
901 | ||| x : Tensor [2, 3, 4] S32
902 | ||| x = tensor [[[ 0, 1, 2, 3],
903 | ||| [ 4, 5, 6, 7],
904 | ||| [ 8, 9, 10, 11]],
905 | ||| [[12, 13, 14, 15],
906 | ||| [16, 17, 18, 19],
907 | ||| [20, 21, 22, 23]]]
908 | ||| ```
909 | ||| `transpose [0, 2, 1] x` is
910 | ||| ```
911 | ||| x : Tensor [2, 4, 3] S32
912 | ||| x = tensor [[[ 0, 4, 8],
913 | ||| [ 1, 5, 9],
914 | ||| [ 2, 6, 10],
915 | ||| [ 3, 7, 11]],
916 | ||| [[12, 16, 20],
917 | ||| [13, 17, 21],
918 | ||| [14, 18, 22],
919 | ||| [15, 19, 23]]]
920 | ||| ```
921 | ||| `transpose [2, 0, 1] x` is
922 | ||| ```
923 | ||| x : Tensor [4, 2, 3] S32
924 | ||| x = tensor [[[ 0, 4, 8],
925 | ||| [12, 16, 20]],
926 | ||| [[ 1, 5, 9],
927 | ||| [13, 17, 21]],
928 | ||| [[ 2, 6, 10],
929 | ||| [14, 18, 22]],
930 | ||| [[ 3, 7, 11],
931 | ||| [15, 19, 23]]]
932 | ||| ```
933 | |||
934 | ||| In order to see what effect transposing a tensor has, it can help to bear in mind the following:
935 | ||| * if an element can be found with `slice [at 3, at 4, at 5] x` in the original tensor,
936 | ||| that same element can instead be found with `slice [at 5, at 3, at 4]` given a
937 | ||| `transpose [2, 0, 1]`. That is, transposing axes re-orders indices when indexing.
938 | ||| * with `transpose [2, 0, 1]`, traversing the first axis in the result is equivalent to
939 | ||| traversing the last axis in the input. Similarly, traversing the last axis in the result is
940 | ||| equivalent to traversing the second axis in the input.
941 | export
951 | ||| A `DimBroadcastable from to` proves that a dimension of size `from` can be broadcast to a
952 | ||| dimension of size `to`.
955 | ||| Proof that any dimension can be broadcast to itself. For example in shapes `[2, 3]` to
956 | ||| `[2, 3]`.
959 | ||| Proof that a dimension of length one can be broadcast to any size. For example in shapes
960 | ||| `[2, 1]` to `[2, 3]`
963 | ||| Proof that any dimension can be broadcast to zero. For example in shapes `[2, 3]` to `[2, 0]`.
967 | ||| A `Broadcastable from to` constitutes proof that the shape `from` can be broadcast to the
968 | ||| shape `to`.
971 | ||| Proof that a shape can be broadcast to itself. For example:
972 | |||
973 | ||| [] to []
974 | ||| [3, 4] to [3, 4]
975 | |||
976 | ||| Implementation note: we could have used `Broadcast [] []`, which would have resulted in more
977 | ||| atomic constructors for `Broadcastable`, but the author guesses that this implementation helps
978 | ||| the type checker avoid applications of `Match`.
981 | ||| Proof that a dimension of size `f` can be broadcast to size `t` if these dimensions
982 | ||| are `DimBroadcastable f t`. For example:
983 | |||
984 | ||| [2, 3] to [2, 3]
985 | ||| [2, 1] to [2, 3]
986 | ||| [2, 1] to [2, 0]
993 | ||| Proof that broadcasting can add outer dimensions i.e. nesting. For example:
994 | |||
995 | ||| [3] to [1, 3]
996 | ||| [3] to [5, 3]
999 | ||| A shape can be extended with any number of leading dimensions.
1000 | |||
1001 | ||| @leading The leading dimensions.
1002 | export
1007 | ||| A scalar can be broadcast to any shape.
1008 | %hint
1009 | export
1013 | ||| Broadcast a `Tensor` to a new compatible shape. For example,
1014 | ||| ```
1015 | ||| x : Tensor [2, 3] S32
1016 | ||| x = broadcast (tensor [4, 5, 6])
1017 | ||| ```
1018 | ||| is
1019 | ||| ```
1020 | ||| x : Tensor [2, 3] S32
1021 | ||| x = tensor [[4, 5, 6], [4, 5, 6]]
1022 | ||| ```
1023 | export
1031 | ||| A `Tensor` where every element has the specified value. For example,
1032 | ||| ```
1033 | ||| fives : Tensor [2, 3] S32
1034 | ||| fives = fill 5
1035 | ||| ```
1036 | ||| is
1037 | ||| ```
1038 | ||| fives : Tensor [2, 3] S32
1039 | ||| fives = tensor [[5, 5, 5],
1040 | ||| [5, 5, 5]]
1041 | ||| ```
1042 | export
1046 | ||| A constant where values increment from zero along the specified `axis`. For example,
1047 | ||| ```
1048 | ||| x : Tensor [3, 5] S32
1049 | ||| x = iota 1
1050 | ||| ```
1051 | ||| is the same as
1052 | ||| ```
1053 | ||| x : Tensor [3, 5] S32
1054 | ||| x = tensor [[0, 1, 2, 3, 4],
1055 | ||| [0, 1, 2, 3, 4],
1056 | ||| [0, 1, 2, 3, 4]]
1057 | ||| ```
1058 | ||| and
1059 | ||| ```
1060 | ||| x : Tensor [3, 5] S32
1061 | ||| x = iota 0
1062 | ||| ```
1063 | ||| is the same as
1064 | ||| ```
1065 | ||| x : Tensor [3, 5] S32
1066 | ||| x = tensor [[0, 0, 0, 0, 0],
1067 | ||| [1, 1, 1, 1, 1],
1068 | ||| [2, 2, 2, 2, 2]]
1069 | ||| ```
1070 | export
1080 | ||| A while-loop operating on a single tensor.
1081 | |||
1082 | ||| `while1` iteratively checks if the tensor satisfies `condition`, and if it does, updates it with
1083 | ||| `body`.
1084 | |||
1085 | ||| **Note:** The XLA plugins impose heuristic-based rules on variable capture in higher-order
1086 | ||| functions. We do not comprehensively understand these rules, but you *might* experience runtime
1087 | ||| errors if you capture variables in `condition` or `body`, especially variables other than
1088 | ||| constants, or variables that depend on parameters to enclosing (StableHLO) scopes.
1089 | |||
1090 | ||| @condition The guard condition for each iteration.
1091 | ||| @body The update step.
1092 | ||| @initial The initial tensor.
1102 | ||| A while-loop operating on two tensors.
1103 | |||
1104 | ||| `while2` iteratively checks if the tensors together satisfy `condition`, and if so, updates them
1105 | ||| with `body`.
1106 | |||
1107 | ||| **Note:** The XLA plugins impose heuristic-based rules on variable capture in higher-order
1108 | ||| functions. We do not comprehensively understand these rules, but you *might* experience runtime
1109 | ||| errors if you capture variables in `condition` or `body`, especially variables other than
1110 | ||| constants, or variables that depend on parameters to enclosing (StableHLO) scopes.
1111 | |||
1112 | ||| @condition The guard condition for each iteration.
1113 | ||| @body The update step.
1114 | ||| @initial One initial tensor.
1115 | ||| @initial' The other initial tensor.
1123 | res <- tag $ Concrete $ While !(mkFn1 [_, _] [_, _] condition) !(mkFn [_, _] [_, _] body) [i, i']
1126 | ||| Reduce elements along one `axis` of a `Tensor` according to a specified `reducer` `Monoid`.
1127 | ||| For example, if `x = tensor [[0, 1, 2], [3, 4, 5]]`, then reduce @{Sum} 0 x` produces
1128 | ||| `tensor [3, 5, 7]`, and `reduce @{Sum} 1 x` produces `tensor [3, 12]`.
1129 | |||
1130 | ||| **Note:** `Semigroup` doesn't use `Tag`, which limits the functions that can be used in
1131 | ||| `reduce`. However, the most commonly used semigroups don't need `Tag`, including `Sum`,
1132 | ||| `Prod`, `Min` and `Max`, so for ergonomics, we have opted to use `Monoid` as is. We can
1133 | ||| provide an overloaded variant if requested.
1134 | |||
1135 | ||| **Note:** The monoid should not depend on values bound to outer scopes, else runtime errors may
1136 | ||| occur.
1137 | |||
1138 | ||| @reducer How to reduce elements along the given `axis`.
1139 | ||| @axis The axis along which to reduce elements.
1140 | export
1156 | ||| Sort the elements of a `Tensor` along a specified `dimension` according to a scalar-wise
1157 | ||| ordering. For sorting function `f`, elements are sorted such that for consecutive sorted
1158 | ||| elements `a` and `b`, either `f a b` is true, or `f a b` *and* `f b a` are false.
1159 | |||
1160 | ||| **Note:** Sorting is not stable, meaning elements that compare equal according the ordering may
1161 | ||| be sorted in a different order to the order they appear in the input.
1162 | |||
1163 | ||| **Note:** `sort` is limited to use comparison function without `Tag`. However, since the most
1164 | ||| commonly-used functions, including (>), (<), (>=), and (<=), don't use `Tag`, we have opted to
1165 | ||| omit it for ergonomics. We can trivially provide an overloaded variant if requested.
1166 | |||
1167 | ||| **Note:** The comparison function should not depend on values bound to outer scopes, else
1168 | ||| runtime errors may occur.
1169 | |||
1170 | ||| For example, for `x = tensor [[1, 6, 4], [3, 2, 5]]`, `sort (<) 0 x` produces
1171 | ||| `tensor [[1, 2, 4], [3, 6, 5]]`, while `sort (<) 1 x` produces
1172 | ||| `tensor [[1, 4, 6], [2, 3, 5]]`.
1173 | export
1183 | ||| Reverse elements along the specified axes. For example, for
1184 | ||| ```
1185 | ||| x : Tensor [2, 3] S32
1186 | ||| x = tensor [[-2, -1, 0],
1187 | ||| [ 1, 2, 3]]
1188 | ||| ```
1189 | ||| `reverse [0] x` is
1190 | ||| ```
1191 | ||| x : Tensor [2, 3] S32
1192 | ||| x = tensor [[ 1, 2, 3],
1193 | ||| [-2, -1, 0]]
1194 | ||| ```
1195 | ||| `reverse [1] x` is
1196 | ||| ```
1197 | ||| x : Tensor [2, 3] S32
1198 | ||| x = tensor [[ 0, -1, -2],
1199 | ||| [ 3, 2, 1]]
1200 | ||| ```
1201 | ||| and `reverse [0, 1] x` is
1202 | ||| ```
1203 | ||| x : Tensor [2, 3] S32
1204 | ||| x = tensor [[ 3, 2, 1],
1205 | ||| [ 0, -1, -2]]
1206 | ||| ```
1207 | |||
1208 | ||| **Note:** This function requires `axes` is ordered simply so that elements are unique.
1209 | ||| The ordering itself is irrelevant to the implementation, but ensures uniqueness without using
1210 | ||| proofs of contradiction that can be difficult for Idris to construct.
1211 | export
1232 | ||| Element-wise equality. For example, `tensor [1, 2] /= tensor [1, 3]` is
1233 | ||| `tensor [True, False]`.
1234 | export
1238 | ||| Element-wise inequality. For example, `tensor [1, 2] /= tensor [1, 3]` is
1239 | ||| `tensor [False, True]`.
1240 | export
1244 | ||| Element-wise less than. For example, `tensor [1, 2, 3] < tensor [2, 2, 2]` is
1245 | ||| `tensor [True, False, False]`.
1246 | export
1250 | ||| Element-wise greater than. For example, `tensor [1, 2, 3] > tensor [2, 2, 2]` is
1251 | ||| `tensor [False, False, True]`.
1252 | export
1256 | ||| Element-wise less than or equal. For example, `tensor [1, 2, 3] <= tensor [2, 2, 2]`
1257 | ||| is `tensor [True, True, False]`.
1258 | export
1262 | ||| Element-wise greater than or equal. For example,
1263 | ||| `tensor [1, 2, 3] >= tensor [2, 2, 2]` is `tensor [False, True, True]`.
1264 | export
1268 | ||| Element-wise boolean and. For example,
1269 | ||| `tensor [True, True, False, False] && tensor [True, False, True, False]` is
1270 | ||| `tensor [True, False, False, False]`.
1271 | export
1276 | export
1281 | export
1285 | ||| Element-wise boolean or. For example,
1286 | ||| `tensor [True, True, False, False] || tensor [True, False, True, False]` is
1287 | ||| `tensor [True, True, True, False]`.
1288 | export
1293 | export
1298 | export
1302 | ||| Element-wise boolean negation. For example, `not (tensor [True, False])` is
1303 | ||| `tensor [False, True]`.
1304 | export
1308 | ||| Elementwise, bitwise "and".
1309 | export
1313 | ||| Elementwise, bitwise "or".
1314 | export
1318 | ||| Elementwise, bitwise exclusive "or".
1319 | export
1323 | ||| Elementwise, bitwise left shift by `n` bits.
1324 | export
1326 | shiftL (MkTensor x) (MkTensor n) = t0 $ BinaryElementwise ShiftLeft x (val0 $ Convert dtype shape n)
1331 | ||| Elementwise, bitwise right shift by `n` bits. If the `ShiftRule` is `Logical`, the `n` left-most
1332 | ||| bits are zero. If it is `Arithmetic`, they are copied from the original left-most bit.
1333 | export
1346 | ||| Elementwise, bitwise complement.
1347 | export
1351 | ||| Elementwise, the number of bits set in the operand.
1352 | export
1356 | ||| Elementwise, the number of leading zeros in the operand.
1357 | export
1361 | ||| Choose elements from two `Tensor`s based on a `Tensor` of predicates. For each element in the
1362 | ||| predicates, the output will use the corresponding element from `onTrue` if the element is
1363 | ||| truthy, else the element from `onFalse`. For example, for
1364 | ||| ```
1365 | ||| preds : Tensor [3] PRED
1366 | ||| preds = tensor [False, True, False]
1367 | |||
1368 | ||| onTrue : Tensor [3] S32
1369 | ||| onTrue = tensor [1, 2, 3]
1370 | |||
1371 | ||| onFalse : Tensor [3] S32
1372 | ||| onFalse = tensor [4, 5, 6]
1373 | ||| ```
1374 | ||| `select preds onTrue onFalse` is `tensor [4, 2, 6]`.
1375 | |||
1376 | ||| @onTrue The elements to choose where the predicate elements are truthy.
1377 | ||| @onFalse The elements to choose where the predicate elements are falsy.
1378 | export
1385 | ||| Use a scalar predicate to evaluate one of two branches. If the predicate is truthy,
1386 | ||| evaluate `onTrue`, else `onFalse`. Each branch is evaluated lazily; only one will be
1387 | ||| evaluated.
1388 | |||
1389 | ||| For example, for
1390 | ||| ```
1391 | ||| f : Tensor [] F64 -> Tag $ Tensor [] F64
1392 | ||| f x = if_ (x < 1.0) (pure $ cos x) (do x <- tag x; x * x)
1393 | ||| ```
1394 | ||| `f 0.0` produces `1.0`, and `f 4.0` produces `8.0`.
1395 | |||
1396 | ||| **Note:** Branches to `if_` are interpreted as constant StableHLO functions. The XLA plugins
1397 | ||| impose heuristic-based rules on variable capture in higher-order functions. We do not
1398 | ||| comprehensively understand these rules, but you *might* experience runtime errors if you capture
1399 | ||| variables in the branches, especially variables other than constants, or variables that depend
1400 | ||| on parameters to enclosing (StableHLO) scopes.
1401 | |||
1402 | ||| @onTrue The branch to evaluate if the predicate is truthy.
1403 | ||| @onFalse The branch to evaluate if the predicate is falsy.
1404 | export
1413 | ||| Run the branch in `branches` specified by `index`, and return its result. If `index` is out of
1414 | ||| bounds of `branches`, uses the last branch. Each branch is evaluated lazily; only one is
1415 | ||| evaluated.
1416 | |||
1417 | ||| For example, for
1418 | ||| ```
1419 | ||| f : Int32 -> Tag $ Tensor [] F64
1420 | ||| f x = case_ (tensor x) (pure $ cos 0.0) (pure $ sin pi) (square 2.0)
1421 | ||| ```
1422 | ||| `f 0` produces `1.0` and `f 1` produces `-1.0`, while `f (-1)`, `f 2`, and `f 3`
1423 | ||| all produce `8.0`.
1424 | |||
1425 | ||| **Note:** Branches are interpreted as constant StableHLO functions. The XLA plugins
1426 | ||| impose heuristic-based rules on variable capture in higher-order functions. We do not
1427 | ||| comprehensively understand these rules, but you *might* experience runtime errors if you capture
1428 | ||| variables in the branches, especially variables other than constants, or variables that depend
1429 | ||| on parameters to enclosing (StableHLO) scopes.
1430 | export
1439 | ||| The identity tensor, with inferred shape and element type. For example,
1440 | ||| ```
1441 | ||| x : Tensor [2, 2] S32
1442 | ||| x = identity
1443 | ||| ```
1444 | ||| is
1445 | ||| ```
1446 | ||| x : Tensor [2, 2] S32
1447 | ||| x = tensor [[1, 0],
1448 | ||| [0, 1]]
1449 | ||| ```
1450 | export
1456 | -- see https://www.python.org/dev/peps/pep-0465/#precedence-and-associativity
1460 | ||| Vector dot product with a tensor of any rank. The vector dot product is with the first axis of
1461 | ||| the right-hand side tensor. For example `tensor [0, 1, 2] @@ tensor [-1, -3, -1]` is
1462 | ||| `-1`.
1463 | export
1468 | ||| Matrix multiplication with a matrix or vector. Contraction is along the last axis of the first
1469 | ||| and the first axis of the last. For example,
1470 | ||| ```
1471 | ||| x : Tensor [2, 3] S32
1472 | ||| x = tensor [[-1, -2, -3],
1473 | ||| [ 0, 1, 2]]
1474 | |||
1475 | ||| y : Tensor [3, 1] S32
1476 | ||| y = tensor [[4, 0, 5]]
1477 | |||
1478 | ||| z : Tensor [2, 1] S32
1479 | ||| z = x @@ y
1480 | ||| ```
1481 | ||| is
1482 | ||| ```
1483 | ||| z : Tensor [2, 1] S32
1484 | ||| z = tensor [-19, 10]
1485 | ||| ```
1486 | export
1492 | (MkTensor x) @@ (MkTensor x') = t0 $ DotGeneral [] [] [1] [0] (TensorType (n :: tl) dtype) x x'
1494 | ||| The output shape of a `dotGeneral` operation.
1502 | Shape
1510 | ||| Matrix multiplication.
1511 | |||
1512 | ||| This is a much more general version of `(@@)`, in which you can specify any number of batch
1513 | ||| and contracting axes. Matrix multiplication is done over each contracting axis.
1514 | ||| The operation is vectorized over batch axes. For each contracting axis on the left-hand
1515 | ||| operand, there is one contracting axis on the right-hand operand. These can be different axes
1516 | ||| in each operand. The same is true for each batch axis.
1517 | |||
1518 | ||| For example, we can vectorize over a typical rank-two matrix multiplication as follows: given
1519 | ||| two inputs tensors
1520 | ||| ```
1521 | ||| let x : Tensor [3, 4, 5, 6] F64
1522 | ||| y : Tensor [3, 4, 6, 7] F64
1523 | ||| ```
1524 | ||| we do
1525 | ||| ```
1526 | ||| let z : Tensor [3, 4, 5, 7] F64 = dotGeneral [0, 1] [0, 1] [3] [2] x y
1527 | ||| ```
1528 | ||| Here, we vectorized over the first two axes `[0, 1]`, and do standard matrix multiplication
1529 | ||| over the remaining axes by specifying the axes 3 and 2 respectively as contracting axes. Notice
1530 | ||| how the batch axes appear once each at the start of the output shape, and the contracting axis
1531 | ||| disappears. Remaining axes appear in order from left to right.
1532 | |||
1533 | ||| Note this API is somewhat of a quickfix to bring general matrix multiplication to the tensor
1534 | ||| API. It is not thoroughly tested. Expect it to change in the future.
1535 | export
1554 | ||| Element-wise addition. For example, `tensor [1, 2] + tensor [3, 4]` is
1555 | ||| `tensor [4, 6]`.
1556 | export
1561 | export
1566 | export
1571 | ||| Element-wise negation. For example, `- tensor [1, -2]` is `tensor [-1, 2]`.
1572 | export
1576 | ||| Element-wise subtraction. For example, `tensor [3, 4] - tensor [4, 2]` is
1577 | ||| `tensor [-1, 2]`.
1578 | export
1582 | ||| Element-wise multiplication. For example, `tensor [2, 3] * tensor [4, 5]` is
1583 | ||| `tensor [8, 15]`.
1584 | export
1589 | export
1594 | export
1599 | ||| Element-wise floating point division. For example, `tensor [2, 3] / tensor [4, 5]` is
1600 | ||| `tensor [0.5, 0.6]`.
1601 | export
1608 | ||| Element-wise division of natural numbers. For example,
1609 | ||| `div (tensor [13, 8]) [3, 4]` is `tensor [4, 2]`.
1610 | |||
1611 | ||| **Note:** Broadcasting a single value into a large `Literal` will be slower than for a `Tensor`.
1612 | ||| For this reason, if you're using a single common denominator, consider using `Scalarwise.div`.
1613 | export
1622 | ||| Overload of `div` for common denominator. The denominator is broadcast to match the numerator.
1623 | export
1633 | ||| Element-wise remainder for natural numbers. For example,
1634 | ||| `rem (tensor [13, 8]) [3, 4]` is `tensor [1, 0]`.
1635 | |||
1636 | ||| **Note:** Broadcasting a single value into a large `Literal` will be slower than for a
1637 | ||| `Tensor`. For this reason, if you're using a single common denominator, consider using
1638 | ||| `Scalarwise.rem`.
1639 | export
1648 | ||| Overload of `rem` for a common denominator. The denominator is broadcast to match the
1649 | ||| numerator.
1650 | export
1659 | ||| Each element in `base` raised to the power of the corresponding element in `exponent`.
1660 | ||| example, `pow (tensor [2, 25, -9]) (tensor [3, -0.5, 0.5])` is `tensor [8, 0.2, nan]`.
1661 | |||
1662 | ||| **Note:** The first root is used. Fractional powers of negative numbers return NaN.
1663 | export
1667 | ||| Element-wise, whether a number is *not* infinity or NaN.
1668 | export
1672 | ||| Element-wise sign. For example, `sign (tensor [-2, 0, 2, nan])` is `tensor [-1, 0, 1, nan]`.
1673 | export
1677 | ||| Element-wise absolute value. For example, `abs (tensor [-2, 3])` is `tensor [2, 3]`.
1678 | export
1682 | ||| The element-wise natural exponential. For example, `exp (tensor [-1, 0, 2])` is
1683 | ||| `tensor [1 / euler, 1, pow euler 2]`.
1684 | export
1688 | ||| The element-wise natural exponential, minus one, equivalent to `(`subtract` 1) . exp`.
1689 | export
1693 | ||| The element-wise floor function. For example,
1694 | ||| `floor (tensor [-1.6, -1.5, -1.4, -1.0, 1.0, 1.4, 1.5, 1.6])` is
1695 | ||| `tensor [-2.0, -2.0, -2.0, -1.0, 1.0, 1.0, 1.0, 1.0]`.
1696 | export
1700 | ||| The element-wise ceiling function. For example,
1701 | ||| `ceil (tensor [-1.6, -1.5, -1.4, -1.0, 1.0, 1.4, 1.5, 1.6])` is
1702 | ||| `tensor [-1.0, -1.0, -1.0, -1.0, 1.0, 2.0, 2.0, 2.0]`.
1703 | export
1710 | ||| Round each element to the nearest integer, breaking ties according to the `BreakTies`.
1711 | |||
1712 | ||| `AwayFromZero` breaks ties away from zero, for example
1713 | ||| `round (tensor [-1.6, -1.5, -1.4, -1.0, -0.5, 0.1, 0.5, 1.0, 1.4, 1.5, 1.6])` is
1714 | ||| `tensor [-2.0, -2.0, -1.0, -1.0, -1.0, 0.0, 1.0, 1.0, 1.0, 2.0, 2.0]`.
1715 | |||
1716 | ||| `ToEven` breaks ties to the nearest even, for example,
1717 | ||| `round (tensor [-1.6, -1.5, -1.4, -1.0, -0.5, 0.5, 1.0, 1.4, 1.5, 1.6])` is
1718 | ||| `tensor [-2.0, -2.0, -1.0, -1.0, 0.0, 0.0, 1.0, 1.0, 2.0, 2.0]`
1719 | export
1724 | ||| The element-wise natural logarithm. Negative inputs yield NaN output. For example,
1725 | ||| `log (tensor [1 / euler, 1, euler * euler])` is `tensor [-1, 0, 2]`.
1726 | export
1730 | ||| The element-wise natural logarithm of the input plus one, equivalent to `log . (+ 1)`.
1731 | ||| Inputs less than (-1) yield NaN.
1732 | export
1736 | ||| The element-wise logistic function equivalent to `1 / 1 + exp (-x)`.
1737 | export
1741 | ||| The element-wise sine.
1742 | export
1746 | ||| The element-wise cosine.
1747 | export
1751 | ||| The element-wise tangent.
1752 | export
1756 | ||| The element-wise arcsine.
1757 | export
1761 | ||| The element-wise arccosine.
1762 | export
1766 | ||| The element-wise arctangent.
1767 | export
1771 | ||| The element-wise two-argument arctangent: the angle between the vector (x, y) and the positive
1772 | ||| x-axis.
1773 | export
1777 | ||| The element-wise hyperbolic sine.
1778 | export
1782 | ||| The element-wise hyperbolic cosine.
1783 | export
1787 | ||| The element-wise hyperbolic tangent.
1788 | export
1792 | ||| The element-wise hyperbolic arcsine.
1793 | export
1797 | ||| The element-wise hyperbolic arccosine.
1798 | export
1802 | ||| The element-wise hyperbolic arctangent.
1803 | export
1807 | ||| An approximation to the element-wise error function.
1808 | export
1815 | ||| The element-wise square. For example, `square (tensor [-2, 0, 3])`
1816 | ||| is `tensor [4, 0, 9]`.
1817 | export
1821 | ||| The element-wise square root. The first root is used. Negative inputs yield NaN output.
1822 | ||| For example, `sqrt (tensor [0, 9])` is `tensor [0, 3]`.
1823 | export
1827 | ||| The element-wise reciprocal square root. The first root is used. Negative inputs yield NaN output.
1828 | ||| For example, `rsqrt (tensor [0, 4])` is `tensor [inf, 0.5]`.
1829 | export
1833 | ||| The element-wise cube root. The first root is used. Negative inputs yield negative outputs.
1834 | ||| For example, `cbrt (tensor [8, -27])` is `tensor [2, -3]`.
1835 | export
1839 | ||| The element-wise minimum of the first argument compared to the second. For example,
1840 | ||| `min (tensor [-3, -1, 3]) (tensor [-1, 0, 1])` is `tensor [-3, -1, 1]`.
1841 | export
1846 | export
1851 | export
1856 | ||| The element-wise maximum of the first argument compared to the second. For example,
1857 | ||| `max (tensor [-3, -1, 3]) (tensor [-1, 0, 1])` is `tensor [-1, 0, 3]`.
1858 | export
1863 | export
1868 | export
1873 | ||| The diagonal of a matrix as a vector. For example, for
1874 | ||| ```
1875 | ||| x : Tensor [3, 3] S32
1876 | ||| x = tensor [[0, 1, 2],
1877 | ||| [3, 4, 5],
1878 | ||| [6, 7, 8]]
1879 | ||| ```
1880 | ||| `diag x` is `tensor [0, 4, 8]`.
1881 | export
1920 | ||| The first index of the maximum value in a vector. For example,
1921 | ||| `argmax (tensor [-1, 3, -2, -2, 3])` produces `tensor 1`. If the vector contains NaN values,
1922 | ||| `argmax` returns the index of the first NaN.
1923 | export
1927 | ||| The first index of the minimum value in a vector. For example,
1928 | ||| `argmin (tensor [-1, 3, -2, -2, 3])` produces `tensor 2`. If the vector contains NaN values,
1929 | ||| `argmin` returns the index of the first NaN.
1930 | export
1934 | ||| Represents the upper- or lower-triangular component of a matrix.
1938 | ||| Get the upper- or lower-triangular component of a matrix, always including the matrix diagonal.
1939 | ||| Remaining elements will be zero. For example, for
1940 | ||| ```
1941 | ||| x : Tensor [3, 3] S32
1942 | ||| x = tensor [[1, 2, 3],
1943 | ||| [4, 5, 6],
1944 | ||| [7, 8, 9]]
1945 | ||| ```
1946 | ||| `triangle Lower x` produces
1947 | ||| ```
1948 | ||| x : Tensor [3, 3] S32
1949 | ||| x = tensor [[1, 0, 0],
1950 | ||| [4, 5, 0],
1951 | ||| [7, 8, 9]]
1952 | ||| ```
1953 | export
1967 | where
1973 | Refl
1975 | ||| Cholesky decomposition. Computes the lower triangular matrix `L` from the symmetric, positive
1976 | ||| semi-definite matrix `X` s.t. `X = L @@ L.T`. Values will be NaN if the input matrix is not
1977 | ||| positive semi-definite. The remaining matrix components - those not in the lower triangle or
1978 | ||| diagonal - will always be zero.
1979 | export
1986 | ||| Solve the set of linear equations `a @@ x = b` for `x` where `a` is a lower-triangular matrix.
1987 | ||| `a` is given by the lower-triangular elements of the first argument. Values in the
1988 | ||| upper-triangular part are ignored. If `a` is lower-triangular already,
1989 | ||| this is written `a |\ b`.
1990 | |||
1991 | ||| The operator is shaped like the lower-triangular portion of a matrix to signal that it uses
1992 | ||| this portion of its argument. This is in contrast to `(\|)`.
1993 | export
1997 | ||| Solve the set of linear equations `a @@ x = b` for `x` where `a` is an upper-triangular
1998 | ||| matrix. `a` is given by the upper-triangular elements of the first argument. Values in the
1999 | ||| lower-triangular part are ignored. If `a` is upper-triangular already, this is written
2000 | ||| `a \| b`.
2001 | |||
2002 | ||| The operator is shaped like the upper-triangular portion of a matrix to signal that it uses
2003 | ||| this portion of its argument. This is in contrast to `(|\)`.
2004 | export
2009 | ||| Solve the set of linear equations `a @@ x = b` for `x` where `a` is a lower-triangular matrix.
2010 | ||| `a` is given by the lower-triangular elements of the first argument. Values in the
2011 | ||| upper-triangular part are ignored. If `a` is lower-triangular already,
2012 | ||| this is written `a |\ b`.
2013 | |||
2014 | ||| The operator is shaped like the lower-triangular portion of a matrix to signal that it uses
2015 | ||| this portion of its argument. This is in contrast to `(\|)`.
2016 | export
2020 | ||| Solve the set of linear equations `a @@ x = b` for `x` where `a` is an upper-triangular
2021 | ||| matrix. `a` is given by the upper-triangular elements of the first argument. Values in the
2022 | ||| lower-triangular part are ignored. If `a` is upper-triangular already, this is written
2023 | ||| `a \| b`.
2024 | |||
2025 | ||| The operator is shaped like the upper-triangular portion of a matrix to signal that it uses
2026 | ||| this portion of its argument. This is in contrast to `(|\)`.
2027 | export
2031 | ||| Sum the elements along the diagonal of the input. For example,
2032 | ||| `trace (tensor [[-1, 5], [1, 4]])` produces `3`.
2033 | export
2040 | ||| A `Rand a` produces a pseudo-random value of type `a` from a `Tensor [2] U64` state.
2041 | ||| The state is updated every time a new value is generated.
2046 | ||| Generate independent and identically distributed (IID) uniform samples.
2047 | |||
2048 | ||| The generated samples are a deterministic function of the input key and state, but may vary
2049 | ||| between PJRT plugin and library version.
2050 | |||
2051 | ||| Example usage, multiplying two uniform samples
2052 | ||| ```
2053 | ||| x : Tag $ Tensor [3] U64
2054 | ||| x = let seed = tensor [1, 1] in evalStateT seed [| rng * rng |]
2055 | ||| ```
2056 | export
2062 | ||| Generate independent and identically distributed (IID) from the uniform distribution U(0, 1).
2063 | |||
2064 | ||| The generated samples are a deterministic function of the input key and state, but may vary
2065 | ||| between PJRT plugin and library version.
2066 | |||
2067 | ||| Example usage, multiplying two uniform samples
2068 | ||| ```
2069 | ||| x : Rand $ Tensor [3] F64
2070 | ||| x = [| uniform * uniform |]
2071 | ||| ```
2072 | export
2080 | ||| Generate independent and identically distributed (IID) samples from the standard normal
2081 | ||| distribution N(0, 1).
2082 | |||
2083 | ||| The generated samples are a deterministic function of the input key and state, but may vary
2084 | ||| between PJRT plugin and library version.
2085 | |||
2086 | ||| Example usage, multiplying two normal samples
2087 | ||| ```
2088 | ||| x : Rand $ Tensor [3] F64
2089 | ||| x = [| normal * normal |]
2090 | ||| ```
2091 | export