Skip to content

Commit

Permalink
Merge pull request #126 from saddam213/TensorExtensions
Browse files Browse the repository at this point in the history
Move tensor math to OnnxStack.Core
  • Loading branch information
saddam213 authored Feb 29, 2024
2 parents a16659c + 922f65d commit f1694e2
Show file tree
Hide file tree
Showing 21 changed files with 269 additions and 365 deletions.
245 changes: 245 additions & 0 deletions OnnxStack.Core/Extensions/TensorExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,256 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics.Tensors;
using System.Numerics;

namespace OnnxStack.Core
{
public static class TensorExtension
{
/// <summary>
/// Divides the tensor by float.
/// </summary>
/// <param name="tensor">The data.</param>
/// <param name="value">The value.</param>
/// <returns></returns>
public static DenseTensor<float> DivideTensorByFloat(this DenseTensor<float> tensor, float value) => tensor.MultiplyTensorByFloat(1 / value);


/// <summary>
/// Multiplies the tensor by float.
/// </summary>
/// <param name="tensor">The data.</param>
/// <param name="value">The value.</param>
/// <returns></returns>
public static DenseTensor<float> MultiplyTensorByFloat(this DenseTensor<float> tensor, float value)
{
var result = new DenseTensor<float>(tensor.Dimensions);
TensorPrimitives.Multiply(tensor.Buffer.Span, value, result.Buffer.Span);
return result;
}


/// <summary>
/// Subtracts the float from each element.
/// </summary>
/// <param name="tensor">The tensor.</param>
/// <param name="value">The value.</param>
/// <returns></returns>
public static DenseTensor<float> SubtractFloat(this DenseTensor<float> tensor, float value)
{
var result = new DenseTensor<float>(tensor.Dimensions);
TensorPrimitives.Subtract(tensor.Buffer.Span, value, result.Buffer.Span);
return result;
}


/// <summary>
/// Adds the tensors.
/// </summary>
/// <param name="tensor">The sample.</param>
/// <param name="sumTensor">The sum tensor.</param>
/// <returns></returns>
public static DenseTensor<float> AddTensors(this DenseTensor<float> tensor, DenseTensor<float> sumTensor)
{
var result = new DenseTensor<float>(tensor.Dimensions);
TensorPrimitives.Add(tensor.Buffer.Span, sumTensor.Buffer.Span, result.Buffer.Span);
return result;
}


/// <summary>
/// Sums the tensors.
/// </summary>
/// <param name="tensors">The tensor array.</param>
/// <param name="dimensions">The dimensions.</param>
/// <returns></returns>
public static DenseTensor<float> SumTensors(this DenseTensor<float>[] tensors, ReadOnlySpan<int> dimensions)
{
var result = new DenseTensor<float>(dimensions);
for (int m = 0; m < tensors.Length; m++)
{
TensorPrimitives.Add(result.Buffer.Span, tensors[m].Buffer.Span, result.Buffer.Span);
}
return result;
}


/// <summary>
/// Subtracts the tensors.
/// </summary>
/// <param name="tensor">The sample.</param>
/// <param name="subTensor">The sub tensor.</param>
/// <returns></returns>
public static DenseTensor<float> SubtractTensors(this DenseTensor<float> tensor, DenseTensor<float> subTensor)
{
var result = new DenseTensor<float>(tensor.Dimensions);
TensorPrimitives.Subtract(tensor.Buffer.Span, subTensor.Buffer.Span, result.Buffer.Span);
return result;
}

/// <summary>
/// Divides the tensor by float, mutates the original
/// </summary>
/// <param name="tensor">The tensor to mutate.</param>
/// <param name="value">The value to divide by.</param>
/// <returns></returns>
public static DenseTensor<float> DivideBy(this DenseTensor<float> tensor, float value)
{
value = 1 / value;
TensorPrimitives.Multiply(tensor.Buffer.Span, value, tensor.Buffer.Span);
return tensor;
}


/// <summary>
/// Multiples the tensor by float, mutates the original
/// </summary>
/// <param name="tensor">The tensor to mutate.</param>
/// <param name="value">The value to multiply by.</param>
/// <returns></returns>
public static DenseTensor<float> MultiplyBy(this DenseTensor<float> tensor, float value) => DivideBy(tensor, 1 / value);


/// <summary>
/// Computes the absolute values of the Tensor
/// </summary>
/// <param name="tensor">The tensor to mutate.</param>
/// <returns></returns>
public static DenseTensor<float> Abs(this DenseTensor<float> tensor)
{
TensorPrimitives.Abs(tensor.Buffer.Span, tensor.Buffer.Span);
return tensor;
}


/// <summary>
/// Multiplies the specified tensor.
/// </summary>
/// <param name="tensor1">The tensor to mutate.</param>
/// <param name="mulTensor">The tensor to multiply by.</param>
/// <returns></returns>
public static DenseTensor<float> Multiply(this DenseTensor<float> tensor, DenseTensor<float> mulTensor)
{
TensorPrimitives.Multiply(tensor.Buffer.Span, mulTensor.Buffer.Span, tensor.Buffer.Span);
return tensor;
}


/// <summary>
/// Divides the specified tensor.
/// </summary>
/// <param name="tensor">The tensor to mutate.</param>
/// <param name="divTensor">The tensor to divide by.</param>
/// <returns></returns>
public static DenseTensor<float> Divide(this DenseTensor<float> tensor, DenseTensor<float> divTensor)
{
TensorPrimitives.Divide(tensor.Buffer.Span, divTensor.Buffer.Span, tensor.Buffer.Span);
return tensor;
}


/// <summary>
/// Adds the tensors, mutates the original
/// </summary>
/// <param name="tensor">The tensor to mutate.</param>
/// <param name="addTensor">The tensor values to add to tensor.</param>
/// <returns></returns>
public static DenseTensor<float> Add(this DenseTensor<float> tensor, DenseTensor<float> addTensor)
{
TensorPrimitives.Add(tensor.Buffer.Span, addTensor.Buffer.Span, tensor.Buffer.Span);
return tensor;
}


/// <summary>
/// Subtracts the tensors, mutates the original
/// </summary>
/// <param name="tensor">The tensor to mutate.</param>
/// <param name="subTensor">The tensor to subtract from tensor.</param>
/// <param name="dimensions">The dimensions.</param>
/// <returns></returns>
public static DenseTensor<float> Subtract(this DenseTensor<float> tensor, DenseTensor<float> subTensor)
{
TensorPrimitives.Subtract(tensor.Buffer.Span, subTensor.Buffer.Span, tensor.Buffer.Span);
return tensor;
}


/// <summary>
/// Reorders the tensor.
/// </summary>
/// <param name="tensor">The input tensor.</param>
/// <returns></returns>
public static DenseTensor<float> ReorderTensor(this DenseTensor<float> tensor, ReadOnlySpan<int> dimensions)
{
//reorder from batch channel height width to batch height width channel
var inputImagesTensor = new DenseTensor<float>(dimensions);
for (int y = 0; y < tensor.Dimensions[2]; y++)
{
for (int x = 0; x < tensor.Dimensions[3]; x++)
{
inputImagesTensor[0, y, x, 0] = tensor[0, 0, y, x];
inputImagesTensor[0, y, x, 1] = tensor[0, 1, y, x];
inputImagesTensor[0, y, x, 2] = tensor[0, 2, y, x];
}
}
return inputImagesTensor;
}

private static readonly int vectorSize = Vector<float>.Count;

/// <summary>
/// Clips the specified Tensor valuse to the specified minimum/maximum.
/// </summary>
/// <param name="tensor">The tensor.</param>
/// <param name="minValue">The minimum value.</param>
/// <param name="maxValue">The maximum value.</param>
/// <returns></returns>
public static DenseTensor<float> Clip(this DenseTensor<float> tensor, float minValue, float maxValue)
{
Vector<float> min = new Vector<float>(minValue);
Vector<float> max = new Vector<float>(maxValue);
var clipTensor = new DenseTensor<float>(tensor.Dimensions);
for (int i = 0; i < tensor.Length / vectorSize; i++)
{
Vector.Min(min,
Vector.Max(max,
new Vector<float>(tensor.Buffer.Span.Slice(i * vectorSize, vectorSize))))
.CopyTo(clipTensor.Buffer.Span.Slice(i * vectorSize, vectorSize));
}
for (int i = (int)(tensor.Length - tensor.Length % vectorSize); i < tensor.Length; i++)
{
clipTensor.Buffer.Span[i] = Math.Clamp(tensor.Buffer.Span[i], minValue, maxValue);
}
return clipTensor;

}


/// <summary>
/// Generate a random Tensor from a normal distribution with mean 0 and variance 1
/// </summary>
/// <param name="random">The random.</param>
/// <param name="dimensions">The dimensions.</param>
/// <param name="initNoiseSigma">The initialize noise sigma.</param>
/// <returns></returns>
public static DenseTensor<float> NextTensor(this Random random, ReadOnlySpan<int> dimensions, float initNoiseSigma = 1f)
{
var latents = new DenseTensor<float>(dimensions);
for (int i = 0; i < latents.Length; i++)
{
// Generate a random number from a normal distribution with mean 0 and variance 1
var u1 = random.NextSingle(); // Uniform(0,1) random number
var u2 = random.NextSingle(); // Uniform(0,1) random number
var radius = MathF.Sqrt(-2.0f * MathF.Log(u1)); // Radius of polar coordinates
var theta = 2.0f * MathF.PI * u2; // Angle of polar coordinates
var standardNormalRand = radius * MathF.Cos(theta); // Standard normal random number
latents.SetValue(i, standardNormalRand * initNoiseSigma);
}
return latents;
}

/// <summary>
/// Repeats the specified Tensor along the 0 axis.
/// </summary>
Expand Down
5 changes: 0 additions & 5 deletions OnnxStack.StableDiffusion/Common/DiffusionProgress.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using Microsoft.ML.OnnxRuntime.Tensors;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OnnxStack.StableDiffusion.Common
{
Expand Down
9 changes: 0 additions & 9 deletions OnnxStack.StableDiffusion/Config/ModelOptions.cs

This file was deleted.

4 changes: 1 addition & 3 deletions OnnxStack.StableDiffusion/Diffusers/DiffuserBase.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using Microsoft.Extensions.Logging;
using Microsoft.ML.OnnxRuntime.Tensors;
using OnnxStack.Core;
using OnnxStack.Core.Image;
using OnnxStack.Core.Model;
using OnnxStack.StableDiffusion.Common;
using OnnxStack.StableDiffusion.Config;
using OnnxStack.StableDiffusion.Enums;
using OnnxStack.StableDiffusion.Helpers;
using OnnxStack.StableDiffusion.Models;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -158,7 +156,7 @@ protected virtual async Task<DenseTensor<float>> DecodeLatentsAsync(PromptOption
/// <returns></returns>
protected static DenseTensor<float> CreateTimestepTensor(int timestep)
{
return TensorHelper.CreateTensor(new float[] { timestep }, new int[] { 1 });
return new DenseTensor<float>(new float[] { timestep }, new int[] { 1 });
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using OnnxStack.StableDiffusion.Common;
using OnnxStack.StableDiffusion.Config;
using OnnxStack.StableDiffusion.Enums;
using OnnxStack.StableDiffusion.Helpers;
using OnnxStack.StableDiffusion.Models;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -190,7 +189,7 @@ protected override Task<DenseTensor<float>> PrepareLatentsAsync(PromptOptions pr
/// <returns></returns>
protected static DenseTensor<double> CreateConditioningScaleTensor(float conditioningScale)
{
return TensorHelper.CreateTensor(new double[] { conditioningScale }, new int[] { 1 });
return new DenseTensor<double>(new double[] { conditioningScale }, new int[] { 1 });
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using OnnxStack.StableDiffusion.Common;
using OnnxStack.StableDiffusion.Config;
using OnnxStack.StableDiffusion.Enums;
using OnnxStack.StableDiffusion.Helpers;
using OnnxStack.StableDiffusion.Models;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -188,7 +187,7 @@ protected override Task<DenseTensor<float>> PrepareLatentsAsync(PromptOptions pr
/// <returns></returns>
protected static DenseTensor<double> CreateConditioningScaleTensor(float conditioningScale)
{
return TensorHelper.CreateTensor(new double[] { conditioningScale }, new int[] { 1 });
return new DenseTensor<double>(new double[] { conditioningScale }, new int[] { 1 });
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using OnnxStack.StableDiffusion.Common;
using OnnxStack.StableDiffusion.Config;
using OnnxStack.StableDiffusion.Enums;
using OnnxStack.StableDiffusion.Helpers;
using OnnxStack.StableDiffusion.Models;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -192,7 +191,7 @@ protected override Task<DenseTensor<float>> PrepareLatentsAsync(PromptOptions pr
/// <returns></returns>
protected static DenseTensor<double> CreateConditioningScaleTensor(float conditioningScale)
{
return TensorHelper.CreateTensor(new double[] { conditioningScale }, new int[] { 1 });
return new DenseTensor<double>(new double[] { conditioningScale }, new int[] { 1 });
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using OnnxStack.StableDiffusion.Common;
using OnnxStack.StableDiffusion.Config;
using OnnxStack.StableDiffusion.Enums;
using OnnxStack.StableDiffusion.Helpers;
using OnnxStack.StableDiffusion.Models;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -184,7 +183,7 @@ protected override Task<DenseTensor<float>> PrepareLatentsAsync(PromptOptions pr
/// <returns></returns>
protected static DenseTensor<double> CreateConditioningScaleTensor(float conditioningScale)
{
return TensorHelper.CreateTensor(new double[] { conditioningScale }, new int[] { 1 });
return new DenseTensor<double>(new double[] { conditioningScale }, new int[] { 1 });
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using OnnxStack.StableDiffusion.Common;
using OnnxStack.StableDiffusion.Config;
using OnnxStack.StableDiffusion.Enums;
using OnnxStack.StableDiffusion.Helpers;
using OnnxStack.StableDiffusion.Models;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -194,7 +193,7 @@ protected override Task<DenseTensor<float>> PrepareLatentsAsync(PromptOptions pr
/// <returns></returns>
protected static DenseTensor<double> CreateConditioningScaleTensor(float conditioningScale)
{
return TensorHelper.CreateTensor(new double[] { conditioningScale }, new int[] { 1 });
return new DenseTensor<double>(new double[] { conditioningScale }, new int[] { 1 });
}


Expand Down
Loading

0 comments on commit f1694e2

Please sign in to comment.