Skip to content
This repository was archived by the owner on Nov 27, 2024. It is now read-only.

Commit f1694e2

Browse files
authored
Merge pull request #126 from saddam213/TensorExtensions
Move tensor math to OnnxStack.Core
2 parents a16659c + 922f65d commit f1694e2

File tree

21 files changed

+269
-365
lines changed

21 files changed

+269
-365
lines changed

OnnxStack.Core/Extensions/TensorExtension.cs

Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,256 @@
22
using System;
33
using System.Collections.Generic;
44
using System.Linq;
5+
using System.Numerics.Tensors;
6+
using System.Numerics;
57

68
namespace OnnxStack.Core
79
{
810
public static class TensorExtension
911
{
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+
10255
/// <summary>
11256
/// Repeats the specified Tensor along the 0 axis.
12257
/// </summary>

OnnxStack.StableDiffusion/Common/DiffusionProgress.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11
using Microsoft.ML.OnnxRuntime.Tensors;
2-
using System;
3-
using System.Collections.Generic;
4-
using System.Linq;
5-
using System.Text;
6-
using System.Threading.Tasks;
72

83
namespace OnnxStack.StableDiffusion.Common
94
{

OnnxStack.StableDiffusion/Config/ModelOptions.cs

Lines changed: 0 additions & 9 deletions
This file was deleted.

OnnxStack.StableDiffusion/Diffusers/DiffuserBase.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
using Microsoft.Extensions.Logging;
22
using Microsoft.ML.OnnxRuntime.Tensors;
33
using OnnxStack.Core;
4-
using OnnxStack.Core.Image;
54
using OnnxStack.Core.Model;
65
using OnnxStack.StableDiffusion.Common;
76
using OnnxStack.StableDiffusion.Config;
87
using OnnxStack.StableDiffusion.Enums;
9-
using OnnxStack.StableDiffusion.Helpers;
108
using OnnxStack.StableDiffusion.Models;
119
using System;
1210
using System.Collections.Generic;
@@ -158,7 +156,7 @@ protected virtual async Task<DenseTensor<float>> DecodeLatentsAsync(PromptOption
158156
/// <returns></returns>
159157
protected static DenseTensor<float> CreateTimestepTensor(int timestep)
160158
{
161-
return TensorHelper.CreateTensor(new float[] { timestep }, new int[] { 1 });
159+
return new DenseTensor<float>(new float[] { timestep }, new int[] { 1 });
162160
}
163161

164162

OnnxStack.StableDiffusion/Diffusers/InstaFlow/ControlNetDiffuser.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using OnnxStack.StableDiffusion.Common;
77
using OnnxStack.StableDiffusion.Config;
88
using OnnxStack.StableDiffusion.Enums;
9-
using OnnxStack.StableDiffusion.Helpers;
109
using OnnxStack.StableDiffusion.Models;
1110
using System;
1211
using System.Collections.Generic;
@@ -190,7 +189,7 @@ protected override Task<DenseTensor<float>> PrepareLatentsAsync(PromptOptions pr
190189
/// <returns></returns>
191190
protected static DenseTensor<double> CreateConditioningScaleTensor(float conditioningScale)
192191
{
193-
return TensorHelper.CreateTensor(new double[] { conditioningScale }, new int[] { 1 });
192+
return new DenseTensor<double>(new double[] { conditioningScale }, new int[] { 1 });
194193
}
195194

196195

OnnxStack.StableDiffusion/Diffusers/LatentConsistency/ControlNetDiffuser.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using OnnxStack.StableDiffusion.Common;
77
using OnnxStack.StableDiffusion.Config;
88
using OnnxStack.StableDiffusion.Enums;
9-
using OnnxStack.StableDiffusion.Helpers;
109
using OnnxStack.StableDiffusion.Models;
1110
using System;
1211
using System.Collections.Generic;
@@ -188,7 +187,7 @@ protected override Task<DenseTensor<float>> PrepareLatentsAsync(PromptOptions pr
188187
/// <returns></returns>
189188
protected static DenseTensor<double> CreateConditioningScaleTensor(float conditioningScale)
190189
{
191-
return TensorHelper.CreateTensor(new double[] { conditioningScale }, new int[] { 1 });
190+
return new DenseTensor<double>(new double[] { conditioningScale }, new int[] { 1 });
192191
}
193192

194193

OnnxStack.StableDiffusion/Diffusers/LatentConsistencyXL/ControlNetDiffuser.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using OnnxStack.StableDiffusion.Common;
77
using OnnxStack.StableDiffusion.Config;
88
using OnnxStack.StableDiffusion.Enums;
9-
using OnnxStack.StableDiffusion.Helpers;
109
using OnnxStack.StableDiffusion.Models;
1110
using System;
1211
using System.Collections.Generic;
@@ -192,7 +191,7 @@ protected override Task<DenseTensor<float>> PrepareLatentsAsync(PromptOptions pr
192191
/// <returns></returns>
193192
protected static DenseTensor<double> CreateConditioningScaleTensor(float conditioningScale)
194193
{
195-
return TensorHelper.CreateTensor(new double[] { conditioningScale }, new int[] { 1 });
194+
return new DenseTensor<double>(new double[] { conditioningScale }, new int[] { 1 });
196195
}
197196

198197

OnnxStack.StableDiffusion/Diffusers/StableDiffusion/ControlNetDiffuser.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using OnnxStack.StableDiffusion.Common;
77
using OnnxStack.StableDiffusion.Config;
88
using OnnxStack.StableDiffusion.Enums;
9-
using OnnxStack.StableDiffusion.Helpers;
109
using OnnxStack.StableDiffusion.Models;
1110
using System;
1211
using System.Collections.Generic;
@@ -184,7 +183,7 @@ protected override Task<DenseTensor<float>> PrepareLatentsAsync(PromptOptions pr
184183
/// <returns></returns>
185184
protected static DenseTensor<double> CreateConditioningScaleTensor(float conditioningScale)
186185
{
187-
return TensorHelper.CreateTensor(new double[] { conditioningScale }, new int[] { 1 });
186+
return new DenseTensor<double>(new double[] { conditioningScale }, new int[] { 1 });
188187
}
189188

190189

OnnxStack.StableDiffusion/Diffusers/StableDiffusionXL/ControlNetDiffuser.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using OnnxStack.StableDiffusion.Common;
77
using OnnxStack.StableDiffusion.Config;
88
using OnnxStack.StableDiffusion.Enums;
9-
using OnnxStack.StableDiffusion.Helpers;
109
using OnnxStack.StableDiffusion.Models;
1110
using System;
1211
using System.Collections.Generic;
@@ -194,7 +193,7 @@ protected override Task<DenseTensor<float>> PrepareLatentsAsync(PromptOptions pr
194193
/// <returns></returns>
195194
protected static DenseTensor<double> CreateConditioningScaleTensor(float conditioningScale)
196195
{
197-
return TensorHelper.CreateTensor(new double[] { conditioningScale }, new int[] { 1 });
196+
return new DenseTensor<double>(new double[] { conditioningScale }, new int[] { 1 });
198197
}
199198

200199

0 commit comments

Comments
 (0)