From 31562c131dafc0759ec06206b7dc5034b7ebef7e Mon Sep 17 00:00:00 2001 From: Joe Dluzen Date: Thu, 9 Nov 2023 21:15:03 -0500 Subject: [PATCH] Vectorize most of TensorHelper --- .../Helpers/TensorHelper.cs | 169 ++++++++++++------ .../LatentConsistency/LCMScheduler.cs | 18 +- .../Schedulers/SchedulerBase.cs | 4 +- .../StableDiffusion/DDIMScheduler.cs | 24 +-- .../StableDiffusion/DDPMScheduler.cs | 20 +-- .../EulerAncestralScheduler.cs | 6 +- .../StableDiffusion/EulerScheduler.cs | 10 +- .../StableDiffusion/KDPM2Scheduler.cs | 8 +- .../StableDiffusion/LMSScheduler.cs | 4 +- 9 files changed, 158 insertions(+), 105 deletions(-) diff --git a/OnnxStack.StableDiffusion/Helpers/TensorHelper.cs b/OnnxStack.StableDiffusion/Helpers/TensorHelper.cs index a58fbaeb..741170ca 100644 --- a/OnnxStack.StableDiffusion/Helpers/TensorHelper.cs +++ b/OnnxStack.StableDiffusion/Helpers/TensorHelper.cs @@ -2,16 +2,18 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Numerics; namespace OnnxStack.StableDiffusion.Helpers { /// - /// TODO: Optimization, all functions in here are tensor copy, but not all need to be + /// TODO: Optimization, some functions in here are tensor copy, but not all need to be /// probably some good mem/cpu gains here if a set of mutate and non-mutate functions were created /// public static class TensorHelper { + private static readonly int vectorSize = Vector.Count; /// /// Creates a new tensor. /// @@ -31,29 +33,26 @@ public static DenseTensor CreateTensor(T[] data, ReadOnlySpan dimensi /// The data. /// The value. /// - public static DenseTensor DivideTensorByFloat(this DenseTensor tensor, float value) - { - var divTensor = new DenseTensor(tensor.Dimensions); - for (int i = 0; i < tensor.Length; i++) - { - divTensor.SetValue(i, tensor.GetValue(i) / value); - } - return divTensor; - } + public static DenseTensor DivideTensorByFloat(this DenseTensor tensor, float value) => tensor.MultiplyTensorByFloat(1 / value); /// - /// Multiples the tensor by float. + /// Multiplies the tensor by float. /// /// The data. /// The value. /// - public static DenseTensor MultipleTensorByFloat(this DenseTensor tensor, float value) + public static DenseTensor MultiplyTensorByFloat(this DenseTensor tensor, float value) { var mullTensor = new DenseTensor(tensor.Dimensions); - for (int i = 0; i < tensor.Length; i++) + for (int i = 0; i < tensor.Length / vectorSize; i++) + { + (new Vector(tensor.Buffer.Span.Slice(i * vectorSize, vectorSize)) * value) + .CopyTo(mullTensor.Buffer.Span.Slice(i * vectorSize, vectorSize)); + } + for (int i = (int)(tensor.Length - tensor.Length % vectorSize); i < tensor.Length; i++) { - mullTensor.SetValue(i, tensor.GetValue(i) * value); + mullTensor.Buffer.Span[i] = tensor.Buffer.Span[i] * value; } return mullTensor; } @@ -68,9 +67,15 @@ public static DenseTensor MultipleTensorByFloat(this DenseTensor t public static DenseTensor SubtractFloat(this DenseTensor tensor, float value) { var subTensor = new DenseTensor(tensor.Dimensions); - for (int i = 0; i < tensor.Length; i++) + Vector vectorValue = new Vector(value); + for (int i = 0; i < tensor.Length / vectorSize; i++) + { + Vector.Subtract(new Vector(tensor.Buffer.Span.Slice(i * vectorSize, vectorSize)), vectorValue) + .CopyTo(subTensor.Buffer.Span.Slice(i * vectorSize, vectorSize)); + } + for (int i = (int)(tensor.Length - tensor.Length % vectorSize); i < tensor.Length; i++) { - subTensor.SetValue(i, tensor.GetValue(i) - value); + subTensor.Buffer.Span[i] = tensor.Buffer.Span[i] - value; } return subTensor; } @@ -84,9 +89,15 @@ public static DenseTensor SubtractFloat(this DenseTensor tensor, f public static DenseTensor AddTensors(this DenseTensor tensor, DenseTensor sumTensor) { var addTensor = new DenseTensor(tensor.Dimensions); - for (var i = 0; i < tensor.Length; i++) + for (int i = 0; i < tensor.Length / vectorSize; i++) + { + (new Vector(tensor.Buffer.Span.Slice(i * vectorSize, vectorSize)) + + new Vector(sumTensor.Buffer.Span.Slice(i * vectorSize, vectorSize))) + .CopyTo(addTensor.Buffer.Span.Slice(i * vectorSize, vectorSize)); + } + for (int i = (int)(tensor.Length - tensor.Length % vectorSize); i < tensor.Length; i++) { - addTensor.SetValue(i, tensor.GetValue(i) + sumTensor.GetValue(i)); + addTensor.Buffer.Span[i] = tensor.Buffer.Span[i] + sumTensor.Buffer.Span[i]; } return addTensor; } @@ -103,11 +114,7 @@ public static DenseTensor SumTensors(this DenseTensor[] tensors, R var sumTensor = new DenseTensor(dimensions); for (int m = 0; m < tensors.Length; m++) { - var tensorToSum = tensors[m]; - for (var i = 0; i < tensorToSum.Length; i++) - { - sumTensor.SetValue(i, sumTensor.GetValue(i) + tensorToSum.GetValue(i)); - } + sumTensor.Add(tensors[m]); } return sumTensor; } @@ -122,9 +129,15 @@ public static DenseTensor SumTensors(this DenseTensor[] tensors, R public static DenseTensor SubtractTensors(this DenseTensor tensor, DenseTensor subTensor) { var result = new DenseTensor(tensor.Dimensions); - for (var i = 0; i < tensor.Length; i++) + for (int i = 0; i < tensor.Length / vectorSize; i++) { - result.SetValue(i, tensor.GetValue(i) - subTensor.GetValue(i)); + (new Vector(tensor.Buffer.Span.Slice(i * vectorSize, vectorSize)) + - new Vector(subTensor.Buffer.Span.Slice(i * vectorSize, vectorSize))) + .CopyTo(result.Buffer.Span.Slice(i * vectorSize, vectorSize)); + } + for (int i = (int)(tensor.Length - tensor.Length % vectorSize); i < tensor.Length; i++) + { + result.Buffer.Span[i] = tensor.Buffer.Span[i] - subTensor.Buffer.Span[i]; } return result; } @@ -161,12 +174,22 @@ public static DenseTensor ReorderTensor(this DenseTensor tensor, R /// public static DenseTensor Clip(this DenseTensor tensor, float minValue, float maxValue) { + Vector min = new Vector(minValue); + Vector max = new Vector(maxValue); var clipTensor = new DenseTensor(tensor.Dimensions); - for (int i = 0; i < tensor.Length; i++) + for (int i = 0; i < tensor.Length / vectorSize; i++) + { + Vector.Min(min, + Vector.Max(max, + new Vector(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.SetValue(i, Math.Clamp(tensor.GetValue(i), minValue, maxValue)); + clipTensor.Buffer.Span[i] = Math.Clamp(tensor.Buffer.Span[i], minValue, maxValue); } return clipTensor; + } @@ -177,9 +200,15 @@ public static DenseTensor Clip(this DenseTensor tensor, float minV /// public static DenseTensor Abs(this DenseTensor tensor) { - for (int i = 0; i < tensor.Length; i++) + for (int i = 0; i < tensor.Length / vectorSize; i++) + { + Span buffer = tensor.Buffer.Span.Slice(i * vectorSize, vectorSize); + Vector.Abs(new Vector(buffer)) + .CopyTo(buffer); + } + for (int i = (int)(tensor.Length - tensor.Length % vectorSize); i < tensor.Length; i++) { - tensor.SetValue(i, Math.Abs(tensor.GetValue(i))); + tensor.Buffer.Span[i] = Math.Abs(tensor.Buffer.Span[i]); } return tensor; } @@ -193,9 +222,15 @@ public static DenseTensor Abs(this DenseTensor tensor) /// public static DenseTensor Multiply(this DenseTensor tensor, DenseTensor mulTensor) { - for (int i = 0; i < tensor.Length; i++) + for (int i = 0; i < tensor.Length / vectorSize; i++) + { + (new Vector(tensor.Buffer.Span.Slice(i * vectorSize, vectorSize)) + * new Vector(mulTensor.Buffer.Span.Slice(i * vectorSize, vectorSize))) + .CopyTo(tensor.Buffer.Span.Slice(i * vectorSize, vectorSize)); + } + for (int i = (int)(tensor.Length - tensor.Length % vectorSize); i < tensor.Length; i++) { - tensor.SetValue(i, tensor.GetValue(i) * mulTensor.GetValue(i)); + tensor.Buffer.Span[i] = tensor.Buffer.Span[i] * mulTensor.Buffer.Span[i]; } return tensor; } @@ -209,9 +244,15 @@ public static DenseTensor Multiply(this DenseTensor tensor, DenseT /// public static DenseTensor Divide(this DenseTensor tensor, DenseTensor divTensor) { - for (int i = 0; i < tensor.Length; i++) + for (int i = 0; i < tensor.Length / vectorSize; i++) { - tensor.SetValue(i, tensor.GetValue(i) / divTensor.GetValue(i)); + (new Vector(tensor.Buffer.Span.Slice(i * vectorSize, vectorSize)) + / new Vector(divTensor.Buffer.Span.Slice(i * vectorSize, vectorSize))) + .CopyTo(tensor.Buffer.Span.Slice(i * vectorSize, vectorSize)); + } + for (int i = (int)(tensor.Length - tensor.Length % vectorSize); i < tensor.Length; i++) + { + tensor.Buffer.Span[i] = tensor.Buffer.Span[i] / divTensor.Buffer.Span[i]; } return tensor; } @@ -280,12 +321,12 @@ public static DenseTensor GetRandomTensor(Random random, ReadOnlySpan Join(this IList> tensors, in /// - /// Adds the tensors. + /// Adds the tensors, mutates the original /// /// The tensor to mutate. /// The tensor values to add to tensor. /// public static DenseTensor Add(this DenseTensor tensor, DenseTensor addTensor) { - for (var i = 0; i < tensor.Length; i++) + for (int i = 0; i < tensor.Length / vectorSize; i++) + { + (new Vector(tensor.Buffer.Span.Slice(i * vectorSize, vectorSize)) + + new Vector(addTensor.Buffer.Span.Slice(i * vectorSize, vectorSize))) + .CopyTo(tensor.Buffer.Span.Slice(i * vectorSize, vectorSize)); + } + for (int i = (int)(tensor.Length - tensor.Length % vectorSize); i < tensor.Length; i++) { - tensor.SetValue(i, tensor.GetValue(i) + addTensor.GetValue(i)); + tensor.Buffer.Span[i] = tensor.Buffer.Span[i] + addTensor.Buffer.Span[i]; } return tensor; } /// - /// Subtracts the tensors. + /// Subtracts the tensors, mutates the original /// /// The tensor to mutate. /// The tensor to subtract from tensor. @@ -370,43 +417,49 @@ public static DenseTensor Add(this DenseTensor tensor, DenseTensor /// public static DenseTensor Subtract(this DenseTensor tensor, DenseTensor subTensor) { - for (var i = 0; i < tensor.Length; i++) + for (int i = 0; i < tensor.Length / vectorSize; i++) + { + (new Vector(tensor.Buffer.Span.Slice(i * vectorSize, vectorSize)) + - new Vector(subTensor.Buffer.Span.Slice(i * vectorSize, vectorSize))) + .CopyTo(tensor.Buffer.Span.Slice(i * vectorSize, vectorSize)); + } + for (int i = (int)(tensor.Length - tensor.Length % vectorSize); i < tensor.Length; i++) { - tensor.SetValue(i, tensor.GetValue(i) - subTensor.GetValue(i)); + tensor.Buffer.Span[i] = tensor.Buffer.Span[i] - subTensor.Buffer.Span[i]; } return tensor; } /// - /// Divides the tensor by float. + /// Divides the tensor by float, mutates the original /// /// The tensor to mutate. /// The value to divide by. /// public static DenseTensor DivideBy(this DenseTensor tensor, float value) { - for (int i = 0; i < tensor.Length; i++) + value = 1 / value; + var mullTensor = new DenseTensor(tensor.Dimensions); + for (int i = 0; i < tensor.Length / vectorSize; i++) { - tensor.SetValue(i, tensor.GetValue(i) / value); + (new Vector(tensor.Buffer.Span.Slice(i * vectorSize, vectorSize)) * value) + .CopyTo(mullTensor.Buffer.Span.Slice(i * vectorSize, vectorSize)); } - return tensor; + for (int i = (int)(tensor.Length - tensor.Length % vectorSize); i < tensor.Length; i++) + { + mullTensor.Buffer.Span[i] = tensor.Buffer.Span[i] * value; + } + return mullTensor; } /// - /// Multiples the tensor by float. + /// Multiples the tensor by float, mutates the original /// /// The tensor to mutate. /// The value to multiply by. /// - public static DenseTensor MultiplyBy(this DenseTensor tensor, float value) - { - for (int i = 0; i < tensor.Length; i++) - { - tensor.SetValue(i, tensor.GetValue(i) * value); - } - return tensor; - } + public static DenseTensor MultiplyBy(this DenseTensor tensor, float value) => DivideBy(tensor, 1 / value); } } diff --git a/OnnxStack.StableDiffusion/Schedulers/LatentConsistency/LCMScheduler.cs b/OnnxStack.StableDiffusion/Schedulers/LatentConsistency/LCMScheduler.cs index 2c23bb11..de281a9c 100644 --- a/OnnxStack.StableDiffusion/Schedulers/LatentConsistency/LCMScheduler.cs +++ b/OnnxStack.StableDiffusion/Schedulers/LatentConsistency/LCMScheduler.cs @@ -137,7 +137,7 @@ public override SchedulerStepResult Step(DenseTensor modelOutput, int tim if (Options.PredictionType == PredictionType.Epsilon) { predOriginalSample = sample - .SubtractTensors(modelOutput.MultipleTensorByFloat(betaSqrt)) + .SubtractTensors(modelOutput.MultiplyTensorByFloat(betaSqrt)) .DivideTensorByFloat(alphaSqrt); } else if (Options.PredictionType == PredictionType.Sample) @@ -147,8 +147,8 @@ public override SchedulerStepResult Step(DenseTensor modelOutput, int tim else if (Options.PredictionType == PredictionType.VariablePrediction) { predOriginalSample = sample - .MultipleTensorByFloat(alphaSqrt) - .SubtractTensors(modelOutput.MultipleTensorByFloat(betaSqrt)); + .MultiplyTensorByFloat(alphaSqrt) + .SubtractTensors(modelOutput.MultiplyTensorByFloat(betaSqrt)); } @@ -158,15 +158,15 @@ public override SchedulerStepResult Step(DenseTensor modelOutput, int tim //# 6. Denoise model output using boundary conditions var denoised = sample - .MultipleTensorByFloat(cSkip) - .AddTensors(predOriginalSample.MultipleTensorByFloat(cOut)); + .MultiplyTensorByFloat(cSkip) + .AddTensors(predOriginalSample.MultiplyTensorByFloat(cOut)); //# 7. Sample and inject noise z ~ N(0, I) for MultiStep Inference var prevSample = Timesteps.Count > 1 ? CreateRandomSample(modelOutput.Dimensions) - .MultipleTensorByFloat(betaProdTPrevSqrt) - .AddTensors(denoised.MultipleTensorByFloat(alphaProdTPrevSqrt)) + .MultiplyTensorByFloat(betaProdTPrevSqrt) + .AddTensors(denoised.MultiplyTensorByFloat(alphaProdTPrevSqrt)) : denoised; return new SchedulerStepResult(prevSample, denoised); @@ -189,8 +189,8 @@ public override DenseTensor AddNoise(DenseTensor originalSamples, float sqrtOneMinusAlpha = MathF.Sqrt(1.0f - alphaProd); return noise - .MultipleTensorByFloat(sqrtOneMinusAlpha) - .AddTensors(originalSamples.MultipleTensorByFloat(sqrtAlpha)); + .MultiplyTensorByFloat(sqrtOneMinusAlpha) + .AddTensors(originalSamples.MultiplyTensorByFloat(sqrtAlpha)); } diff --git a/OnnxStack.StableDiffusion/Schedulers/SchedulerBase.cs b/OnnxStack.StableDiffusion/Schedulers/SchedulerBase.cs index f14fc1ea..d1c81193 100644 --- a/OnnxStack.StableDiffusion/Schedulers/SchedulerBase.cs +++ b/OnnxStack.StableDiffusion/Schedulers/SchedulerBase.cs @@ -205,13 +205,13 @@ protected virtual DenseTensor GetPredictedSample(DenseTensor model DenseTensor predOriginalSample = null; if (Options.PredictionType == PredictionType.Epsilon) { - predOriginalSample = sample.SubtractTensors(modelOutput.MultipleTensorByFloat(sigma)); + predOriginalSample = sample.SubtractTensors(modelOutput.MultiplyTensorByFloat(sigma)); } else if (Options.PredictionType == PredictionType.VariablePrediction) { var sigmaSqrt = (float)Math.Sqrt(sigma * sigma + 1); predOriginalSample = sample.DivideTensorByFloat(sigmaSqrt) - .AddTensors(modelOutput.MultipleTensorByFloat(-sigma / sigmaSqrt)); + .AddTensors(modelOutput.MultiplyTensorByFloat(-sigma / sigmaSqrt)); } else if (Options.PredictionType == PredictionType.Sample) { diff --git a/OnnxStack.StableDiffusion/Schedulers/StableDiffusion/DDIMScheduler.cs b/OnnxStack.StableDiffusion/Schedulers/StableDiffusion/DDIMScheduler.cs index 51ad46e0..935de3c6 100644 --- a/OnnxStack.StableDiffusion/Schedulers/StableDiffusion/DDIMScheduler.cs +++ b/OnnxStack.StableDiffusion/Schedulers/StableDiffusion/DDIMScheduler.cs @@ -114,7 +114,7 @@ public override SchedulerStepResult Step(DenseTensor modelOutput, int tim DenseTensor predOriginalSample = null; if (Options.PredictionType == PredictionType.Epsilon) { - var sampleBeta = sample.SubtractTensors(modelOutput.MultipleTensorByFloat((float)Math.Sqrt(betaProdT))); + var sampleBeta = sample.SubtractTensors(modelOutput.MultiplyTensorByFloat((float)Math.Sqrt(betaProdT))); predOriginalSample = sampleBeta.DivideTensorByFloat((float)Math.Sqrt(alphaProdT)); predEpsilon = modelOutput; } @@ -122,7 +122,7 @@ public override SchedulerStepResult Step(DenseTensor modelOutput, int tim { predOriginalSample = modelOutput; predEpsilon = sample.SubtractTensors(predOriginalSample - .MultipleTensorByFloat((float)Math.Sqrt(alphaProdT))) + .MultiplyTensorByFloat((float)Math.Sqrt(alphaProdT))) .DivideTensorByFloat((float)Math.Sqrt(betaProdT)); } else if (Options.PredictionType == PredictionType.VariablePrediction) @@ -130,11 +130,11 @@ public override SchedulerStepResult Step(DenseTensor modelOutput, int tim var alphaSqrt = (float)Math.Sqrt(alphaProdT); var betaSqrt = (float)Math.Sqrt(betaProdT); predOriginalSample = sample - .MultipleTensorByFloat(alphaSqrt) - .SubtractTensors(modelOutput.MultipleTensorByFloat(betaSqrt)); + .MultiplyTensorByFloat(alphaSqrt) + .SubtractTensors(modelOutput.MultiplyTensorByFloat(betaSqrt)); predEpsilon = modelOutput - .MultipleTensorByFloat(alphaSqrt) - .AddTensors(sample.MultipleTensorByFloat(betaSqrt)); + .MultiplyTensorByFloat(alphaSqrt) + .AddTensors(sample.MultiplyTensorByFloat(betaSqrt)); } @@ -161,20 +161,20 @@ public override SchedulerStepResult Step(DenseTensor modelOutput, int tim { //# the pred_epsilon is always re-derived from the clipped x_0 in Glide predEpsilon = sample - .SubtractTensors(predOriginalSample.MultipleTensorByFloat((float)Math.Sqrt(alphaProdT))) + .SubtractTensors(predOriginalSample.MultiplyTensorByFloat((float)Math.Sqrt(alphaProdT))) .DivideTensorByFloat((float)Math.Sqrt(betaProdT)); } //# 5. compute "direction pointing to x_t" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf - var predSampleDirection = predEpsilon.MultipleTensorByFloat((float)Math.Sqrt(1f - alphaProdTPrev - Math.Pow(stdDevT, 2f))); + var predSampleDirection = predEpsilon.MultiplyTensorByFloat((float)Math.Sqrt(1f - alphaProdTPrev - Math.Pow(stdDevT, 2f))); //# 6. compute x_t without "random noise" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf - var prevSample = predSampleDirection.AddTensors(predOriginalSample.MultipleTensorByFloat((float)Math.Sqrt(alphaProdTPrev))); + var prevSample = predSampleDirection.AddTensors(predOriginalSample.MultiplyTensorByFloat((float)Math.Sqrt(alphaProdTPrev))); if (eta > 0) - prevSample = prevSample.AddTensors(CreateRandomSample(modelOutput.Dimensions).MultipleTensorByFloat(stdDevT)); + prevSample = prevSample.AddTensors(CreateRandomSample(modelOutput.Dimensions).MultiplyTensorByFloat(stdDevT)); return new SchedulerStepResult(prevSample); } @@ -196,8 +196,8 @@ public override DenseTensor AddNoise(DenseTensor originalSamples, float sqrtOneMinusAlpha = (float)Math.Sqrt(1.0f - alphaProd); return noise - .MultipleTensorByFloat(sqrtOneMinusAlpha) - .AddTensors(originalSamples.MultipleTensorByFloat(sqrtAlpha)); + .MultiplyTensorByFloat(sqrtOneMinusAlpha) + .AddTensors(originalSamples.MultiplyTensorByFloat(sqrtAlpha)); } diff --git a/OnnxStack.StableDiffusion/Schedulers/StableDiffusion/DDPMScheduler.cs b/OnnxStack.StableDiffusion/Schedulers/StableDiffusion/DDPMScheduler.cs index f141406f..e7bbaf9c 100644 --- a/OnnxStack.StableDiffusion/Schedulers/StableDiffusion/DDPMScheduler.cs +++ b/OnnxStack.StableDiffusion/Schedulers/StableDiffusion/DDPMScheduler.cs @@ -132,8 +132,8 @@ public override SchedulerStepResult Step(DenseTensor modelOutput, int tim //# See formula (7) from https://arxiv.org/pdf/2006.11239.pdf //pred_prev_sample = pred_original_sample_coeff * pred_original_sample + current_sample_coeff * sample var predPrevSample = sample - .MultipleTensorByFloat(currentSampleCoeff) - .AddTensors(predOriginalSample.MultipleTensorByFloat(predOriginalSampleCoeff)); + .MultiplyTensorByFloat(currentSampleCoeff) + .AddTensors(predOriginalSample.MultiplyTensorByFloat(predOriginalSampleCoeff)); //# 6. Add noise @@ -144,17 +144,17 @@ public override SchedulerStepResult Step(DenseTensor modelOutput, int tim if (Options.VarianceType == VarianceType.FixedSmallLog) { var v = GetVariance(currentTimestep, predictedVariance); - variance = varianceNoise.MultipleTensorByFloat(v); + variance = varianceNoise.MultiplyTensorByFloat(v); } else if (Options.VarianceType == VarianceType.LearnedRange) { var v = (float)Math.Exp(0.5 * GetVariance(currentTimestep, predictedVariance)); - variance = varianceNoise.MultipleTensorByFloat(v); + variance = varianceNoise.MultiplyTensorByFloat(v); } else { var v = (float)Math.Sqrt(GetVariance(currentTimestep, predictedVariance)); - variance = varianceNoise.MultipleTensorByFloat(v); + variance = varianceNoise.MultiplyTensorByFloat(v); } predPrevSample = predPrevSample.AddTensors(variance); } @@ -169,7 +169,7 @@ private DenseTensor GetPredictedSample(DenseTensor modelOutput, De if (Options.PredictionType == PredictionType.Epsilon) { //pred_original_sample = (sample - beta_prod_t ** (0.5) * model_output) / alpha_prod_t ** (0.5) - var sampleBeta = sample.SubtractTensors(modelOutput.MultipleTensorByFloat((float)Math.Sqrt(betaProdT))); + var sampleBeta = sample.SubtractTensors(modelOutput.MultiplyTensorByFloat((float)Math.Sqrt(betaProdT))); predOriginalSample = sampleBeta.DivideTensorByFloat((float)Math.Sqrt(alphaProdT)); } else if (Options.PredictionType == PredictionType.Sample) @@ -182,8 +182,8 @@ private DenseTensor GetPredictedSample(DenseTensor modelOutput, De var alphaSqrt = (float)Math.Sqrt(alphaProdT); var betaSqrt = (float)Math.Sqrt(betaProdT); predOriginalSample = sample - .MultipleTensorByFloat(alphaSqrt) - .SubtractTensors(modelOutput.MultipleTensorByFloat(betaSqrt)); + .MultiplyTensorByFloat(alphaSqrt) + .SubtractTensors(modelOutput.MultiplyTensorByFloat(betaSqrt)); } return predOriginalSample; } @@ -204,8 +204,8 @@ public override DenseTensor AddNoise(DenseTensor originalSamples, float sqrtOneMinusAlpha = (float)Math.Sqrt(1.0f - alphaProd); return noise - .MultipleTensorByFloat(sqrtOneMinusAlpha) - .AddTensors(originalSamples.MultipleTensorByFloat(sqrtAlpha)); + .MultiplyTensorByFloat(sqrtOneMinusAlpha) + .AddTensors(originalSamples.MultiplyTensorByFloat(sqrtAlpha)); } /// diff --git a/OnnxStack.StableDiffusion/Schedulers/StableDiffusion/EulerAncestralScheduler.cs b/OnnxStack.StableDiffusion/Schedulers/StableDiffusion/EulerAncestralScheduler.cs index bc4c8d0f..1d863a11 100644 --- a/OnnxStack.StableDiffusion/Schedulers/StableDiffusion/EulerAncestralScheduler.cs +++ b/OnnxStack.StableDiffusion/Schedulers/StableDiffusion/EulerAncestralScheduler.cs @@ -127,9 +127,9 @@ public override SchedulerStepResult Step(DenseTensor modelOutput, int tim .DivideTensorByFloat(sigma); var delta = sigmaDown - sigma; - var prevSample = sample.AddTensors(derivative.MultipleTensorByFloat(delta)); + var prevSample = sample.AddTensors(derivative.MultiplyTensorByFloat(delta)); var noise = CreateRandomSample(prevSample.Dimensions); - prevSample = prevSample.AddTensors(noise.MultipleTensorByFloat(sigmaUp)); + prevSample = prevSample.AddTensors(noise.MultiplyTensorByFloat(sigmaUp)); return new SchedulerStepResult(prevSample); } @@ -152,7 +152,7 @@ public override DenseTensor AddNoise(DenseTensor originalSamples, .Max(); return noise - .MultipleTensorByFloat(sigma) + .MultiplyTensorByFloat(sigma) .AddTensors(originalSamples); } diff --git a/OnnxStack.StableDiffusion/Schedulers/StableDiffusion/EulerScheduler.cs b/OnnxStack.StableDiffusion/Schedulers/StableDiffusion/EulerScheduler.cs index 652db12d..9696111c 100644 --- a/OnnxStack.StableDiffusion/Schedulers/StableDiffusion/EulerScheduler.cs +++ b/OnnxStack.StableDiffusion/Schedulers/StableDiffusion/EulerScheduler.cs @@ -121,17 +121,17 @@ public override SchedulerStepResult Step(DenseTensor modelOutput, int tim float gamma = s_tmin <= sigma && sigma <= s_tmax ? (float)Math.Min(s_churn / (_sigmas.Length - 1f), Math.Sqrt(2.0f) - 1.0f) : 0f; var noise = CreateRandomSample(modelOutput.Dimensions); - var epsilon = noise.MultipleTensorByFloat(s_noise); + var epsilon = noise.MultiplyTensorByFloat(s_noise); float sigmaHat = sigma * (1.0f + gamma); if (gamma > 0) - sample = sample.AddTensors(epsilon.MultipleTensorByFloat((float)Math.Sqrt(Math.Pow(sigmaHat, 2f) - Math.Pow(sigma, 2f)))); + sample = sample.AddTensors(epsilon.MultiplyTensorByFloat((float)Math.Sqrt(Math.Pow(sigmaHat, 2f) - Math.Pow(sigma, 2f)))); // 1. compute predicted original sample (x_0) from sigma-scaled predicted noise var predOriginalSample = Options.PredictionType != PredictionType.Epsilon ? GetPredictedSample(modelOutput, sample, sigma) - : sample.SubtractTensors(modelOutput.MultipleTensorByFloat(sigmaHat)); + : sample.SubtractTensors(modelOutput.MultiplyTensorByFloat(sigmaHat)); // 2. Convert to an ODE derivative @@ -140,7 +140,7 @@ public override SchedulerStepResult Step(DenseTensor modelOutput, int tim .DivideTensorByFloat(sigmaHat); var delta = _sigmas[stepIndex + 1] - sigmaHat; - return new SchedulerStepResult(sample.AddTensors(derivative.MultipleTensorByFloat(delta))); + return new SchedulerStepResult(sample.AddTensors(derivative.MultiplyTensorByFloat(delta))); } @@ -160,7 +160,7 @@ public override DenseTensor AddNoise(DenseTensor originalSamples, .Max(); return noise - .MultipleTensorByFloat(sigma) + .MultiplyTensorByFloat(sigma) .AddTensors(originalSamples); } diff --git a/OnnxStack.StableDiffusion/Schedulers/StableDiffusion/KDPM2Scheduler.cs b/OnnxStack.StableDiffusion/Schedulers/StableDiffusion/KDPM2Scheduler.cs index 15405974..fbc348e4 100644 --- a/OnnxStack.StableDiffusion/Schedulers/StableDiffusion/KDPM2Scheduler.cs +++ b/OnnxStack.StableDiffusion/Schedulers/StableDiffusion/KDPM2Scheduler.cs @@ -142,13 +142,13 @@ public override SchedulerStepResult Step(DenseTensor modelOutput, int tim DenseTensor predOriginalSample; if (Options.PredictionType == PredictionType.Epsilon) { - predOriginalSample = sample.SubtractTensors(modelOutput.MultipleTensorByFloat(sigmaInput)); + predOriginalSample = sample.SubtractTensors(modelOutput.MultiplyTensorByFloat(sigmaInput)); } else if (Options.PredictionType == PredictionType.VariablePrediction) { var sigmaSqrt = (float)Math.Sqrt(sigmaInput * sigmaInput + 1f); predOriginalSample = sample.DivideTensorByFloat(sigmaSqrt) - .AddTensors(modelOutput.MultipleTensorByFloat(-sigmaInput / sigmaSqrt)); + .AddTensors(modelOutput.MultiplyTensorByFloat(-sigmaInput / sigmaSqrt)); } else { @@ -177,7 +177,7 @@ public override SchedulerStepResult Step(DenseTensor modelOutput, int tim } _stepIndex += 1; - return new SchedulerStepResult(sample.AddTensors(derivative.MultipleTensorByFloat(dt))); + return new SchedulerStepResult(sample.AddTensors(derivative.MultiplyTensorByFloat(dt))); } @@ -192,7 +192,7 @@ public override DenseTensor AddNoise(DenseTensor originalSamples, { var sigma = _sigmas[_stepIndex]; return noise - .MultipleTensorByFloat(sigma) + .MultiplyTensorByFloat(sigma) .AddTensors(originalSamples); } diff --git a/OnnxStack.StableDiffusion/Schedulers/StableDiffusion/LMSScheduler.cs b/OnnxStack.StableDiffusion/Schedulers/StableDiffusion/LMSScheduler.cs index e4265167..93756c31 100644 --- a/OnnxStack.StableDiffusion/Schedulers/StableDiffusion/LMSScheduler.cs +++ b/OnnxStack.StableDiffusion/Schedulers/StableDiffusion/LMSScheduler.cs @@ -144,7 +144,7 @@ public override SchedulerStepResult Step(DenseTensor modelOutput, int tim { // Multiply to coeff by each derivatives to create the new tensors var (lmsCoeff, derivative) = lmsCoeffsAndDerivatives[i]; - lmsDerProduct[i] = derivative.MultipleTensorByFloat((float)lmsCoeff); + lmsDerProduct[i] = derivative.MultiplyTensorByFloat((float)lmsCoeff); } // Add the sumed tensor to the sample @@ -168,7 +168,7 @@ public override DenseTensor AddNoise(DenseTensor originalSamples, .Max(); return noise - .MultipleTensorByFloat(sigma) + .MultiplyTensorByFloat(sigma) .AddTensors(originalSamples); }