|
2 | 2 | using System;
|
3 | 3 | using System.Collections.Generic;
|
4 | 4 | using System.Linq;
|
| 5 | +using System.Numerics.Tensors; |
| 6 | +using System.Numerics; |
5 | 7 |
|
6 | 8 | namespace OnnxStack.Core
|
7 | 9 | {
|
8 | 10 | public static class TensorExtension
|
9 | 11 | {
|
| 12 | + /// <summary> |
| 13 | + /// Divides the tensor by float. |
| 14 | + /// </summary> |
| 15 | + /// <param name="tensor">The data.</param> |
| 16 | + /// <param name="value">The value.</param> |
| 17 | + /// <returns></returns> |
| 18 | + public static DenseTensor<float> DivideTensorByFloat(this DenseTensor<float> tensor, float value) => tensor.MultiplyTensorByFloat(1 / value); |
| 19 | + |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// Multiplies the tensor by float. |
| 23 | + /// </summary> |
| 24 | + /// <param name="tensor">The data.</param> |
| 25 | + /// <param name="value">The value.</param> |
| 26 | + /// <returns></returns> |
| 27 | + public static DenseTensor<float> MultiplyTensorByFloat(this DenseTensor<float> tensor, float value) |
| 28 | + { |
| 29 | + var result = new DenseTensor<float>(tensor.Dimensions); |
| 30 | + TensorPrimitives.Multiply(tensor.Buffer.Span, value, result.Buffer.Span); |
| 31 | + return result; |
| 32 | + } |
| 33 | + |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Subtracts the float from each element. |
| 37 | + /// </summary> |
| 38 | + /// <param name="tensor">The tensor.</param> |
| 39 | + /// <param name="value">The value.</param> |
| 40 | + /// <returns></returns> |
| 41 | + public static DenseTensor<float> SubtractFloat(this DenseTensor<float> tensor, float value) |
| 42 | + { |
| 43 | + var result = new DenseTensor<float>(tensor.Dimensions); |
| 44 | + TensorPrimitives.Subtract(tensor.Buffer.Span, value, result.Buffer.Span); |
| 45 | + return result; |
| 46 | + } |
| 47 | + |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// Adds the tensors. |
| 51 | + /// </summary> |
| 52 | + /// <param name="tensor">The sample.</param> |
| 53 | + /// <param name="sumTensor">The sum tensor.</param> |
| 54 | + /// <returns></returns> |
| 55 | + public static DenseTensor<float> AddTensors(this DenseTensor<float> tensor, DenseTensor<float> sumTensor) |
| 56 | + { |
| 57 | + var result = new DenseTensor<float>(tensor.Dimensions); |
| 58 | + TensorPrimitives.Add(tensor.Buffer.Span, sumTensor.Buffer.Span, result.Buffer.Span); |
| 59 | + return result; |
| 60 | + } |
| 61 | + |
| 62 | + |
| 63 | + /// <summary> |
| 64 | + /// Sums the tensors. |
| 65 | + /// </summary> |
| 66 | + /// <param name="tensors">The tensor array.</param> |
| 67 | + /// <param name="dimensions">The dimensions.</param> |
| 68 | + /// <returns></returns> |
| 69 | + public static DenseTensor<float> SumTensors(this DenseTensor<float>[] tensors, ReadOnlySpan<int> dimensions) |
| 70 | + { |
| 71 | + var result = new DenseTensor<float>(dimensions); |
| 72 | + for (int m = 0; m < tensors.Length; m++) |
| 73 | + { |
| 74 | + TensorPrimitives.Add(result.Buffer.Span, tensors[m].Buffer.Span, result.Buffer.Span); |
| 75 | + } |
| 76 | + return result; |
| 77 | + } |
| 78 | + |
| 79 | + |
| 80 | + /// <summary> |
| 81 | + /// Subtracts the tensors. |
| 82 | + /// </summary> |
| 83 | + /// <param name="tensor">The sample.</param> |
| 84 | + /// <param name="subTensor">The sub tensor.</param> |
| 85 | + /// <returns></returns> |
| 86 | + public static DenseTensor<float> SubtractTensors(this DenseTensor<float> tensor, DenseTensor<float> subTensor) |
| 87 | + { |
| 88 | + var result = new DenseTensor<float>(tensor.Dimensions); |
| 89 | + TensorPrimitives.Subtract(tensor.Buffer.Span, subTensor.Buffer.Span, result.Buffer.Span); |
| 90 | + return result; |
| 91 | + } |
| 92 | + |
| 93 | + /// <summary> |
| 94 | + /// Divides the tensor by float, mutates the original |
| 95 | + /// </summary> |
| 96 | + /// <param name="tensor">The tensor to mutate.</param> |
| 97 | + /// <param name="value">The value to divide by.</param> |
| 98 | + /// <returns></returns> |
| 99 | + public static DenseTensor<float> DivideBy(this DenseTensor<float> tensor, float value) |
| 100 | + { |
| 101 | + value = 1 / value; |
| 102 | + TensorPrimitives.Multiply(tensor.Buffer.Span, value, tensor.Buffer.Span); |
| 103 | + return tensor; |
| 104 | + } |
| 105 | + |
| 106 | + |
| 107 | + /// <summary> |
| 108 | + /// Multiples the tensor by float, mutates the original |
| 109 | + /// </summary> |
| 110 | + /// <param name="tensor">The tensor to mutate.</param> |
| 111 | + /// <param name="value">The value to multiply by.</param> |
| 112 | + /// <returns></returns> |
| 113 | + public static DenseTensor<float> MultiplyBy(this DenseTensor<float> tensor, float value) => DivideBy(tensor, 1 / value); |
| 114 | + |
| 115 | + |
| 116 | + /// <summary> |
| 117 | + /// Computes the absolute values of the Tensor |
| 118 | + /// </summary> |
| 119 | + /// <param name="tensor">The tensor to mutate.</param> |
| 120 | + /// <returns></returns> |
| 121 | + public static DenseTensor<float> Abs(this DenseTensor<float> tensor) |
| 122 | + { |
| 123 | + TensorPrimitives.Abs(tensor.Buffer.Span, tensor.Buffer.Span); |
| 124 | + return tensor; |
| 125 | + } |
| 126 | + |
| 127 | + |
| 128 | + /// <summary> |
| 129 | + /// Multiplies the specified tensor. |
| 130 | + /// </summary> |
| 131 | + /// <param name="tensor1">The tensor to mutate.</param> |
| 132 | + /// <param name="mulTensor">The tensor to multiply by.</param> |
| 133 | + /// <returns></returns> |
| 134 | + public static DenseTensor<float> Multiply(this DenseTensor<float> tensor, DenseTensor<float> mulTensor) |
| 135 | + { |
| 136 | + TensorPrimitives.Multiply(tensor.Buffer.Span, mulTensor.Buffer.Span, tensor.Buffer.Span); |
| 137 | + return tensor; |
| 138 | + } |
| 139 | + |
| 140 | + |
| 141 | + /// <summary> |
| 142 | + /// Divides the specified tensor. |
| 143 | + /// </summary> |
| 144 | + /// <param name="tensor">The tensor to mutate.</param> |
| 145 | + /// <param name="divTensor">The tensor to divide by.</param> |
| 146 | + /// <returns></returns> |
| 147 | + public static DenseTensor<float> Divide(this DenseTensor<float> tensor, DenseTensor<float> divTensor) |
| 148 | + { |
| 149 | + TensorPrimitives.Divide(tensor.Buffer.Span, divTensor.Buffer.Span, tensor.Buffer.Span); |
| 150 | + return tensor; |
| 151 | + } |
| 152 | + |
| 153 | + |
| 154 | + /// <summary> |
| 155 | + /// Adds the tensors, mutates the original |
| 156 | + /// </summary> |
| 157 | + /// <param name="tensor">The tensor to mutate.</param> |
| 158 | + /// <param name="addTensor">The tensor values to add to tensor.</param> |
| 159 | + /// <returns></returns> |
| 160 | + public static DenseTensor<float> Add(this DenseTensor<float> tensor, DenseTensor<float> addTensor) |
| 161 | + { |
| 162 | + TensorPrimitives.Add(tensor.Buffer.Span, addTensor.Buffer.Span, tensor.Buffer.Span); |
| 163 | + return tensor; |
| 164 | + } |
| 165 | + |
| 166 | + |
| 167 | + /// <summary> |
| 168 | + /// Subtracts the tensors, mutates the original |
| 169 | + /// </summary> |
| 170 | + /// <param name="tensor">The tensor to mutate.</param> |
| 171 | + /// <param name="subTensor">The tensor to subtract from tensor.</param> |
| 172 | + /// <param name="dimensions">The dimensions.</param> |
| 173 | + /// <returns></returns> |
| 174 | + public static DenseTensor<float> Subtract(this DenseTensor<float> tensor, DenseTensor<float> subTensor) |
| 175 | + { |
| 176 | + TensorPrimitives.Subtract(tensor.Buffer.Span, subTensor.Buffer.Span, tensor.Buffer.Span); |
| 177 | + return tensor; |
| 178 | + } |
| 179 | + |
| 180 | + |
| 181 | + /// <summary> |
| 182 | + /// Reorders the tensor. |
| 183 | + /// </summary> |
| 184 | + /// <param name="tensor">The input tensor.</param> |
| 185 | + /// <returns></returns> |
| 186 | + public static DenseTensor<float> ReorderTensor(this DenseTensor<float> tensor, ReadOnlySpan<int> dimensions) |
| 187 | + { |
| 188 | + //reorder from batch channel height width to batch height width channel |
| 189 | + var inputImagesTensor = new DenseTensor<float>(dimensions); |
| 190 | + for (int y = 0; y < tensor.Dimensions[2]; y++) |
| 191 | + { |
| 192 | + for (int x = 0; x < tensor.Dimensions[3]; x++) |
| 193 | + { |
| 194 | + inputImagesTensor[0, y, x, 0] = tensor[0, 0, y, x]; |
| 195 | + inputImagesTensor[0, y, x, 1] = tensor[0, 1, y, x]; |
| 196 | + inputImagesTensor[0, y, x, 2] = tensor[0, 2, y, x]; |
| 197 | + } |
| 198 | + } |
| 199 | + return inputImagesTensor; |
| 200 | + } |
| 201 | + |
| 202 | + private static readonly int vectorSize = Vector<float>.Count; |
| 203 | + |
| 204 | + /// <summary> |
| 205 | + /// Clips the specified Tensor valuse to the specified minimum/maximum. |
| 206 | + /// </summary> |
| 207 | + /// <param name="tensor">The tensor.</param> |
| 208 | + /// <param name="minValue">The minimum value.</param> |
| 209 | + /// <param name="maxValue">The maximum value.</param> |
| 210 | + /// <returns></returns> |
| 211 | + public static DenseTensor<float> Clip(this DenseTensor<float> tensor, float minValue, float maxValue) |
| 212 | + { |
| 213 | + Vector<float> min = new Vector<float>(minValue); |
| 214 | + Vector<float> max = new Vector<float>(maxValue); |
| 215 | + var clipTensor = new DenseTensor<float>(tensor.Dimensions); |
| 216 | + for (int i = 0; i < tensor.Length / vectorSize; i++) |
| 217 | + { |
| 218 | + Vector.Min(min, |
| 219 | + Vector.Max(max, |
| 220 | + new Vector<float>(tensor.Buffer.Span.Slice(i * vectorSize, vectorSize)))) |
| 221 | + .CopyTo(clipTensor.Buffer.Span.Slice(i * vectorSize, vectorSize)); |
| 222 | + } |
| 223 | + for (int i = (int)(tensor.Length - tensor.Length % vectorSize); i < tensor.Length; i++) |
| 224 | + { |
| 225 | + clipTensor.Buffer.Span[i] = Math.Clamp(tensor.Buffer.Span[i], minValue, maxValue); |
| 226 | + } |
| 227 | + return clipTensor; |
| 228 | + |
| 229 | + } |
| 230 | + |
| 231 | + |
| 232 | + /// <summary> |
| 233 | + /// Generate a random Tensor from a normal distribution with mean 0 and variance 1 |
| 234 | + /// </summary> |
| 235 | + /// <param name="random">The random.</param> |
| 236 | + /// <param name="dimensions">The dimensions.</param> |
| 237 | + /// <param name="initNoiseSigma">The initialize noise sigma.</param> |
| 238 | + /// <returns></returns> |
| 239 | + public static DenseTensor<float> NextTensor(this Random random, ReadOnlySpan<int> dimensions, float initNoiseSigma = 1f) |
| 240 | + { |
| 241 | + var latents = new DenseTensor<float>(dimensions); |
| 242 | + for (int i = 0; i < latents.Length; i++) |
| 243 | + { |
| 244 | + // Generate a random number from a normal distribution with mean 0 and variance 1 |
| 245 | + var u1 = random.NextSingle(); // Uniform(0,1) random number |
| 246 | + var u2 = random.NextSingle(); // Uniform(0,1) random number |
| 247 | + var radius = MathF.Sqrt(-2.0f * MathF.Log(u1)); // Radius of polar coordinates |
| 248 | + var theta = 2.0f * MathF.PI * u2; // Angle of polar coordinates |
| 249 | + var standardNormalRand = radius * MathF.Cos(theta); // Standard normal random number |
| 250 | + latents.SetValue(i, standardNormalRand * initNoiseSigma); |
| 251 | + } |
| 252 | + return latents; |
| 253 | + } |
| 254 | + |
10 | 255 | /// <summary>
|
11 | 256 | /// Repeats the specified Tensor along the 0 axis.
|
12 | 257 | /// </summary>
|
|
0 commit comments