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

Commit 9ae2ae3

Browse files
committed
Remove unneeded dimensions arguments
1 parent 8119c01 commit 9ae2ae3

File tree

4 files changed

+8
-57
lines changed

4 files changed

+8
-57
lines changed

OnnxStack.StableDiffusion/Helpers/TensorHelper.cs

Lines changed: 3 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,6 @@ public static DenseTensor<T> CreateTensor<T>(T[] data, ReadOnlySpan<int> dimensi
2525
}
2626

2727

28-
/// <summary>
29-
/// Divides the tensor by float.
30-
/// </summary>
31-
/// <param name="tensor">The data.</param>
32-
/// <param name="value">The value.</param>
33-
/// <param name="dimensions">The dimensions.</param>
34-
/// <returns></returns>
35-
public static DenseTensor<float> DivideTensorByFloat(this DenseTensor<float> tensor, float value, ReadOnlySpan<int> dimensions)
36-
{
37-
var divTensor = new DenseTensor<float>(dimensions);
38-
for (int i = 0; i < tensor.Length; i++)
39-
{
40-
divTensor.SetValue(i, tensor.GetValue(i) / value);
41-
}
42-
return divTensor;
43-
}
44-
45-
4628
/// <summary>
4729
/// Divides the tensor by float.
4830
/// </summary>
@@ -131,29 +113,15 @@ public static DenseTensor<float> SumTensors(this DenseTensor<float>[] tensors, R
131113
}
132114

133115

134-
/// <summary>
135-
/// Duplicates the specified tensor.
136-
/// </summary>
137-
/// <param name="tensor">The data.</param>
138-
/// <param name="dimensions">The dimensions.</param>
139-
/// <returns></returns>
140-
public static DenseTensor<float> Duplicate(this DenseTensor<float> tensor, ReadOnlySpan<int> dimensions)
141-
{
142-
var dupTensor = tensor.Concat(tensor).ToArray();
143-
return CreateTensor(dupTensor, dimensions);
144-
}
145-
146-
147116
/// <summary>
148117
/// Subtracts the tensors.
149118
/// </summary>
150-
/// <param name="tensor">The tensor.</param>
119+
/// <param name="tensor">The sample.</param>
151120
/// <param name="subTensor">The sub tensor.</param>
152-
/// <param name="dimensions">The dimensions.</param>
153121
/// <returns></returns>
154-
public static DenseTensor<float> SubtractTensors(this DenseTensor<float> tensor, DenseTensor<float> subTensor, ReadOnlySpan<int> dimensions)
122+
public static DenseTensor<float> SubtractTensors(this DenseTensor<float> tensor, DenseTensor<float> subTensor)
155123
{
156-
var result = new DenseTensor<float>(dimensions);
124+
var result = new DenseTensor<float>(tensor.Dimensions);
157125
for (var i = 0; i < tensor.Length; i++)
158126
{
159127
result.SetValue(i, tensor.GetValue(i) - subTensor.GetValue(i));
@@ -162,18 +130,6 @@ public static DenseTensor<float> SubtractTensors(this DenseTensor<float> tensor,
162130
}
163131

164132

165-
/// <summary>
166-
/// Subtracts the tensors.
167-
/// </summary>
168-
/// <param name="tensor">The sample.</param>
169-
/// <param name="subTensor">The sub tensor.</param>
170-
/// <returns></returns>
171-
public static DenseTensor<float> SubtractTensors(this DenseTensor<float> tensor, DenseTensor<float> subTensor)
172-
{
173-
return tensor.SubtractTensors(subTensor, tensor.Dimensions);
174-
}
175-
176-
177133
/// <summary>
178134
/// Reorders the tensor.
179135
/// </summary>
@@ -196,7 +152,6 @@ public static DenseTensor<float> ReorderTensor(this DenseTensor<float> tensor, R
196152
}
197153

198154

199-
200155
/// <summary>
201156
/// Clips the specified Tensor valuse to the specified minimum/maximum.
202157
/// </summary>
@@ -215,10 +170,6 @@ public static DenseTensor<float> Clip(this DenseTensor<float> tensor, float minV
215170
}
216171

217172

218-
219-
220-
221-
222173
/// <summary>
223174
/// Computes the absolute values of the Tensor
224175
/// </summary>

OnnxStack.StableDiffusion/Schedulers/DDPMScheduler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ private DenseTensor<float> GetPredictedSample(DenseTensor<float> modelOutput, De
174174
{
175175
//pred_original_sample = (sample - beta_prod_t ** (0.5) * model_output) / alpha_prod_t ** (0.5)
176176
var sampleBeta = sample.SubtractTensors(modelOutput.MultipleTensorByFloat((float)Math.Sqrt(betaProdT)));
177-
predOriginalSample = sampleBeta.DivideTensorByFloat((float)Math.Sqrt(alphaProdT), sampleBeta.Dimensions);
177+
predOriginalSample = sampleBeta.DivideTensorByFloat((float)Math.Sqrt(alphaProdT));
178178
}
179179
else if (Options.PredictionType == PredictionType.Sample)
180180
{

OnnxStack.StableDiffusion/Schedulers/EulerAncestralScheduler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public override DenseTensor<float> ScaleInput(DenseTensor<float> sample, int tim
9191
sigma = (float)Math.Sqrt(Math.Pow(sigma, 2) + 1);
9292

9393
// Divide sample tensor shape {2,4,(H/8),(W/8)} by sigma
94-
return sample.DivideTensorByFloat(sigma, sample.Dimensions);
94+
return sample.DivideTensorByFloat(sigma);
9595
}
9696

9797

@@ -124,7 +124,7 @@ public override DenseTensor<float> Step(DenseTensor<float> modelOutput, int time
124124
// 2. Convert to an ODE derivative
125125
var derivative = sample
126126
.SubtractTensors(predOriginalSample)
127-
.DivideTensorByFloat(sigma, predOriginalSample.Dimensions);
127+
.DivideTensorByFloat(sigma);
128128

129129
var delta = sigmaDown - sigma;
130130
var prevSample = sample.AddTensors(derivative.MultipleTensorByFloat(delta));

OnnxStack.StableDiffusion/Schedulers/LMSScheduler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public override DenseTensor<float> ScaleInput(DenseTensor<float> sample, int tim
9595
sigma = (float)Math.Sqrt(Math.Pow(sigma, 2) + 1);
9696

9797
// Divide sample tensor shape {2,4,(H/8),(W/8)} by sigma
98-
return sample.DivideTensorByFloat(sigma, sample.Dimensions);
98+
return sample.DivideTensorByFloat(sigma);
9999
}
100100

101101

@@ -118,7 +118,7 @@ public override DenseTensor<float> Step(DenseTensor<float> modelOutput, int time
118118
// 2. Convert to an ODE derivative
119119
var derivativeSample = sample
120120
.SubtractTensors(predOriginalSample)
121-
.DivideTensorByFloat(sigma, sample.Dimensions);
121+
.DivideTensorByFloat(sigma);
122122

123123
_derivatives.Enqueue(derivativeSample);
124124
if (_derivatives.Count > order)

0 commit comments

Comments
 (0)