|
| 1 | +import scala.compiletime.ops.int.* |
| 2 | + |
| 3 | +object Tensors: |
| 4 | + import Shape.Reverse |
| 5 | + |
| 6 | + type Supported = Int | Long | Float | Double | Byte | Short | Boolean | String |
| 7 | + |
| 8 | + type TensorTypeDenotation = String & Singleton |
| 9 | + |
| 10 | + type Axes = Tuple3[TensorTypeDenotation, TensorShapeDenotation, Shape] |
| 11 | + |
| 12 | + opaque type Tensor[T <: Supported, +Ax <: Axes] = Tuple2[Array[T], Ax] |
| 13 | + |
| 14 | + type SparseTensor[T <: Supported, A <: Axes] = Tensor[T, A] |
| 15 | + |
| 16 | + type KeepOrReduceDims[S <: Shape, AxisIndices <: None.type | Indices, KeepDims <: (Boolean & Singleton)] <: Shape = KeepDims match |
| 17 | + case true => ReduceKeepDims[S, AxisIndices] |
| 18 | + case false => Shape.Reduce[S, AxisIndices] |
| 19 | + |
| 20 | + type KeepOrReduceDimDenotations[Td <: TensorShapeDenotation, AxisIndices <: None.type | Indices, KeepDims <: (Boolean & Singleton)] <: TensorShapeDenotation = KeepDims match |
| 21 | + case true => Td |
| 22 | + case false => TensorShapeDenotation.Reduce[Td, AxisIndices] |
| 23 | + |
| 24 | + type ReduceKeepDims[S <: Shape, AxisIndices <: None.type | Indices] <: Shape = AxisIndices match |
| 25 | + case None.type => SNil |
| 26 | + case Indices => ReduceKeepDimsLoop[S, AxisIndices, 0] |
| 27 | + |
| 28 | + protected type ReduceKeepDimsLoop[ReplaceFrom <: Shape, ToReplace <: Indices, I <: Index] <: Shape = ReplaceFrom match |
| 29 | + case head #: tail => Indices.Contains[ToReplace, I] match |
| 30 | + case true => 1 #: ReduceKeepDimsLoop[tail, Indices.RemoveValue[ToReplace, I], S[I]] |
| 31 | + case false => head #: ReduceKeepDimsLoop[tail, ToReplace, S[I]] |
| 32 | + case SNil => ToReplace match { case INil => SNil } |
| 33 | + |
| 34 | + type AddGivenAxisSize[S <: Shape, S1 <: Shape, AxisIndices <: None.type | Indices] <: Shape = AxisIndices match |
| 35 | + case None.type => SNil |
| 36 | + case Indices => AddGivenAxisSizeLoop[S, S1, AxisIndices, 0] |
| 37 | + |
| 38 | + protected type AddGivenAxisSizeLoop[First <: Shape, Second <: Shape, AxisIndex <: Indices, I <: Index] <: Shape = First match |
| 39 | + case head #: tail => Indices.Contains[AxisIndex, I] match |
| 40 | + case true => Second match |
| 41 | + case secondHead #: secondTail => (head + secondHead) #: AddGivenAxisSizeLoop[tail, secondTail, Indices.RemoveValue[AxisIndex, I], S[I]] |
| 42 | + case SNil => AxisIndex match { case INil => SNil } |
| 43 | + case false => Second match |
| 44 | + case secondHead #: secondTail => (head) #: AddGivenAxisSizeLoop[tail, secondTail, AxisIndex, S[I]] |
| 45 | + case SNil => AxisIndex match { case INil => SNil } |
| 46 | + |
| 47 | + type UnsqueezeShape[S <: Shape, AxisIndex <: None.type | Indices] <: Shape = AxisIndex match |
| 48 | + case None.type => SNil |
| 49 | + case Indices => UnsqueezeShapeLoop[S, AxisIndex, 0] |
| 50 | + |
| 51 | + protected type UnsqueezeShapeLoop[ToUnsqueeze <: Shape, AxisIndex <: Indices, I <: Index] <: Shape = ToUnsqueeze match |
| 52 | + case head #: tail => Indices.Contains[AxisIndex, I] match |
| 53 | + case true => 1 #: head #: UnsqueezeShapeLoop[tail, Indices.RemoveValue[AxisIndex, I], S[I]] |
| 54 | + case false => head #: UnsqueezeShapeLoop[tail, AxisIndex, S[I]] |
| 55 | + case SNil => AxisIndex match { case INil => SNil } |
| 56 | + |
| 57 | + type GatheredShape[S <: Shape, AxisIndex <: None.type | Indices, AxisIndices <: Indices] <: Shape = AxisIndex match |
| 58 | + case None.type => SNil |
| 59 | + case Indices => GatheredShapeLoop[S, AxisIndex, 0, AxisIndices] |
| 60 | + |
| 61 | + protected type GatheredShapeLoop[ToGather <: Shape, AxisIndex <: Indices, I <: Index, AxisIndices <: Indices] <: Shape = ToGather match |
| 62 | + case head #: tail => Indices.Contains[AxisIndex, I] match |
| 63 | + case true => IndicesSize[AxisIndices] #: GatheredShapeLoop[tail, Indices.RemoveValue[AxisIndex, I], S[I], AxisIndices] |
| 64 | + case false => head #: GatheredShapeLoop[tail, AxisIndex, S[I], AxisIndices] |
| 65 | + case SNil => AxisIndex match { case INil => SNil } |
| 66 | + |
| 67 | + type IndicesSize[AxisIndices <: Indices] = IndicesSizeLoop[AxisIndices, 0] |
| 68 | + |
| 69 | + type IndicesSizeLoop[AxisIndices <: Indices, Acc <: Dimension] <: Dimension = AxisIndices match |
| 70 | + case head ::: tail => IndicesSizeLoop[tail, S[Acc]] |
| 71 | + case INil => Acc |
| 72 | + |
| 73 | + type FlattenedShape[S <: Shape, AxisIndex <: None.type | Indices] <: Shape = AxisIndex match |
| 74 | + case None.type => SNil |
| 75 | + case Indices => FlattenedShapeLoop[S, AxisIndex, 0, 1] |
| 76 | + |
| 77 | + protected type FlattenedShapeLoop[ToFlatten <: Shape, AxisIndex <: Indices, I <: Index, Acc <: Index] <: Shape = ToFlatten match |
| 78 | + case head #: tail => Indices.Contains[AxisIndex, I] match |
| 79 | + case true => Acc #: FlattenedShapeLoop[tail, Indices.RemoveValue[AxisIndex, I], S[I], head] |
| 80 | + case false => FlattenedShapeLoop[tail, AxisIndex, S[I], head * Acc] |
| 81 | + case SNil => AxisIndex match { case INil => Acc #: SNil } |
| 82 | + |
| 83 | + type SlicedShape[AxisIndicesStarts <: None.type | Indices, AxisIndicesEnds <: None.type | Indices] <: Shape = AxisIndicesStarts match |
| 84 | + case None.type => SNil |
| 85 | + case Indices => AxisIndicesEnds match |
| 86 | + case None.type => SNil |
| 87 | + case Indices => SlicedShapeLoop[AxisIndicesStarts, AxisIndicesEnds] |
| 88 | + |
| 89 | + protected type SlicedShapeLoop[Starts <: Indices, Ends <: Indices] <: Shape = Starts match |
| 90 | + case head ::: tail => Ends match |
| 91 | + case endsHead ::: endsTail => (endsHead - head) #: SlicedShapeLoop[tail, endsTail] |
| 92 | + case INil => SNil |
| 93 | + case INil => Ends match { case INil => SNil } |
| 94 | + |
| 95 | + type PaddedShape[PadFrom <: Shape, AxisBefore <: None.type | Shape, AxisAfter <: None.type | Shape] <: Shape = AxisBefore match |
| 96 | + case None.type => PadFrom |
| 97 | + case Shape => AxisAfter match |
| 98 | + case None.type => PadFrom |
| 99 | + case Shape => Reverse[PaddedShapeLoop[Reverse[PadFrom], Reverse[AxisBefore], Reverse[AxisAfter]]] |
| 100 | + |
| 101 | + protected type PaddedShapeLoop[PadFrom <: Shape, Before <: Shape, After <: Shape] <: Shape = Before match |
| 102 | + case head #: tail => After match |
| 103 | + case afterHead #: afterTail => PadFrom match |
| 104 | + case padFromHead #: padFromTail => (head + padFromHead + afterHead) #: PaddedShapeLoop[padFromTail, tail, afterTail] |
| 105 | + case SNil => SNil |
| 106 | + case SNil => SNil |
| 107 | + case SNil => After match |
| 108 | + case SNil => PadFrom match |
| 109 | + case padFromHead #: padFromTail => padFromHead #: PaddedShapeLoop[padFromTail, SNil, SNil] |
| 110 | + case SNil => SNil |
| 111 | + |
| 112 | + type TiledShape[TileFrom <: Shape, AxisRepeats <: None.type | Indices] <: Shape = AxisRepeats match |
| 113 | + case None.type => SNil |
| 114 | + case Indices => TiledShapeLoop[TileFrom, AxisRepeats] |
| 115 | + |
| 116 | + protected type TiledShapeLoop[TileFrom <: Shape, Repeats <: Indices] <: Shape = Repeats match |
| 117 | + case head ::: tail => TileFrom match |
| 118 | + case tileFromHead #: tileFromTail => (head * tileFromHead) #: TiledShapeLoop[tileFromTail, tail] |
| 119 | + case SNil => SNil |
| 120 | + case INil => SNil |
| 121 | + |
| 122 | + type PoolShape[From <: Shape, KernelShape <: None.type | Shape] <: Shape = KernelShape match |
| 123 | + case None.type => SNil |
| 124 | + case Shape => Reverse[PoolShapeLoop[Reverse[From], Reverse[KernelShape]]] |
| 125 | + |
| 126 | + protected type PoolShapeLoop[From <: Shape, KernelShape <: Shape] <: Shape = KernelShape match |
| 127 | + case head #: tail => From match |
| 128 | + case fromHead #: fromTail => (fromHead - head + 1) #: PoolShapeLoop[fromTail, tail] |
| 129 | + case SNil => SNil |
| 130 | + case SNil => From |
0 commit comments