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

Commit f2751fe

Browse files
committed
Move Diffusers to new folder structure
1 parent 87bcdb8 commit f2751fe

File tree

9 files changed

+44
-34
lines changed

9 files changed

+44
-34
lines changed

OnnxStack.StableDiffusion/Diffusers/DiffuserBase.cs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public virtual async Task<DenseTensor<float>> DiffuseAsync(IModelOptions modelOp
123123
/// <param name="options">The options.</param>
124124
/// <param name="latents">The latents.</param>
125125
/// <returns></returns>
126-
protected async Task<DenseTensor<float>> DecodeLatents(IModelOptions model, PromptOptions prompt, SchedulerOptions options, DenseTensor<float> latents)
126+
protected virtual async Task<DenseTensor<float>> DecodeLatents(IModelOptions model, PromptOptions prompt, SchedulerOptions options, DenseTensor<float> latents)
127127
{
128128
// Scale and decode the image latents with vae.
129129
latents = latents.MultiplyBy(1.0f / model.ScaleFactor);
@@ -169,7 +169,7 @@ protected async Task<DenseTensor<float>> DecodeLatents(IModelOptions model, Prom
169169
/// <param name="noisePredText">The noise pred text.</param>
170170
/// <param name="guidanceScale">The guidance scale.</param>
171171
/// <returns></returns>
172-
protected DenseTensor<float> PerformGuidance(DenseTensor<float> noisePrediction, float guidanceScale)
172+
protected virtual DenseTensor<float> PerformGuidance(DenseTensor<float> noisePrediction, float guidanceScale)
173173
{
174174
// Split Prompt and Negative Prompt predictions
175175
var dimensions = noisePrediction.Dimensions.ToArray();
@@ -203,6 +203,27 @@ protected virtual IReadOnlyList<NamedOnnxValue> CreateUnetInputParams(IModelOpti
203203
}
204204

205205

206+
/// <summary>
207+
/// Gets the scheduler.
208+
/// </summary>
209+
/// <param name="options">The options.</param>
210+
/// <param name="schedulerConfig">The scheduler configuration.</param>
211+
/// <returns></returns>
212+
protected virtual IScheduler GetScheduler(PromptOptions prompt, SchedulerOptions options)
213+
{
214+
return prompt.SchedulerType switch
215+
{
216+
SchedulerType.LMS => new LMSScheduler(options),
217+
SchedulerType.Euler => new EulerScheduler(options),
218+
SchedulerType.EulerAncestral => new EulerAncestralScheduler(options),
219+
SchedulerType.DDPM => new DDPMScheduler(options),
220+
SchedulerType.DDIM => new DDIMScheduler(options),
221+
SchedulerType.KDPM2 => new KDPM2Scheduler(options),
222+
_ => default
223+
};
224+
}
225+
226+
206227
/// <summary>
207228
/// Determines whether the specified result image is not NSFW.
208229
/// </summary>
@@ -267,27 +288,6 @@ protected static DenseTensor<float> ClipImageFeatureExtractor(SchedulerOptions o
267288
}
268289

269290

270-
271-
/// <summary>
272-
/// Gets the scheduler.
273-
/// </summary>
274-
/// <param name="options">The options.</param>
275-
/// <param name="schedulerConfig">The scheduler configuration.</param>
276-
/// <returns></returns>
277-
protected static IScheduler GetScheduler(PromptOptions prompt, SchedulerOptions options)
278-
{
279-
return prompt.SchedulerType switch
280-
{
281-
SchedulerType.LMS => new LMSScheduler(options),
282-
SchedulerType.Euler => new EulerScheduler(options),
283-
SchedulerType.EulerAncestral => new EulerAncestralScheduler(options),
284-
SchedulerType.DDPM => new DDPMScheduler(options),
285-
SchedulerType.DDIM => new DDIMScheduler(options),
286-
SchedulerType.KDPM2 => new KDPM2Scheduler(options),
287-
_ => default
288-
};
289-
}
290-
291291
/// <summary>
292292
/// Helper for creating the input parameters.
293293
/// </summary>

OnnxStack.StableDiffusion/Diffusers/AnimateDiffuser.cs renamed to OnnxStack.StableDiffusion/Diffusers/StableDiffusion/AnimateDiffuser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
using System.Text;
99
using System.Threading.Tasks;
1010

11-
namespace OnnxStack.StableDiffusion.Diffusers
11+
namespace OnnxStack.StableDiffusion.Diffusers.StableDiffusion
1212
{
1313
public class AnimateDiffuser : DiffuserBase
1414
{

OnnxStack.StableDiffusion/Diffusers/ImageDiffuser.cs renamed to OnnxStack.StableDiffusion/Diffusers/StableDiffusion/ImageDiffuser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
using System.Linq;
1313

1414

15-
namespace OnnxStack.StableDiffusion.Services
15+
namespace OnnxStack.StableDiffusion.Diffusers.StableDiffusion
1616
{
1717
public sealed class ImageDiffuser : DiffuserBase
1818
{

OnnxStack.StableDiffusion/Diffusers/InpaintDiffuser.cs renamed to OnnxStack.StableDiffusion/Diffusers/StableDiffusion/InpaintDiffuser.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
using System.Threading.Tasks;
1515

1616

17-
namespace OnnxStack.StableDiffusion.Services
17+
namespace OnnxStack.StableDiffusion.Diffusers.StableDiffusion
1818
{
1919
public sealed class InpaintDiffuser : DiffuserBase
2020
{
@@ -171,9 +171,9 @@ private DenseTensor<float> PrepareImageMask(IModelOptions modelOptions, PromptOp
171171
for (int y = 0; y < height; y++)
172172
{
173173
var pixelSpan = img.GetRowSpan(y);
174-
imageTensor[0, 0, y, x] = (pixelSpan[x].R / 127.5f) - 1f;
175-
imageTensor[0, 1, y, x] = (pixelSpan[x].G / 127.5f) - 1f;
176-
imageTensor[0, 2, y, x] = (pixelSpan[x].B / 127.5f) - 1f;
174+
imageTensor[0, 0, y, x] = pixelSpan[x].R / 127.5f - 1f;
175+
imageTensor[0, 1, y, x] = pixelSpan[x].G / 127.5f - 1f;
176+
imageTensor[0, 2, y, x] = pixelSpan[x].B / 127.5f - 1f;
177177
}
178178
}
179179
});

OnnxStack.StableDiffusion/Diffusers/InpaintLegacyDiffuser.cs renamed to OnnxStack.StableDiffusion/Diffusers/StableDiffusion/InpaintLegacyDiffuser.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
using System.Threading;
1515
using System.Threading.Tasks;
1616

17-
namespace OnnxStack.StableDiffusion.Services
17+
namespace OnnxStack.StableDiffusion.Diffusers.StableDiffusion
1818
{
1919
public sealed class InpaintLegacyDiffuser : DiffuserBase
2020
{
@@ -167,7 +167,7 @@ private DenseTensor<float> PrepareMask(IModelOptions modelOptions, PromptOptions
167167
for (int y = 0; y < height; y++)
168168
{
169169
var pixelSpan = img.GetRowSpan(y);
170-
var value = (float)pixelSpan[x].A / 255.0f;
170+
var value = pixelSpan[x].A / 255.0f;
171171
maskTensor[0, 0, y, x] = 1f - value;
172172
maskTensor[0, 1, y, x] = 0f; // Needed for shape only
173173
maskTensor[0, 2, y, x] = 0f; // Needed for shape only
@@ -207,7 +207,7 @@ private DenseTensor<float> ApplyMaskedLatents(DenseTensor<float> latents, DenseT
207207
float initLatentsProperValue = initLatentsProper[batch, channel, height, width];
208208

209209
//Apply the logic to compute the result based on the mask
210-
float newValue = (initLatentsProperValue * maskValue) + (latentsValue * (1f - maskValue));
210+
float newValue = initLatentsProperValue * maskValue + latentsValue * (1f - maskValue);
211211
result[batch, channel, height, width] = newValue;
212212
}
213213
}

OnnxStack.StableDiffusion/Diffusers/TextDiffuser.cs renamed to OnnxStack.StableDiffusion/Diffusers/StableDiffusion/TextDiffuser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
using System.Collections.Generic;
77

88

9-
namespace OnnxStack.StableDiffusion.Services
9+
namespace OnnxStack.StableDiffusion.Diffusers.StableDiffusion
1010
{
1111
public sealed class TextDiffuser : DiffuserBase
1212
{

OnnxStack.StableDiffusion/OnnxStack.StableDiffusion.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,8 @@
6161
</None>
6262
</ItemGroup>
6363

64+
<ItemGroup>
65+
<Folder Include="Diffusers\LatentConsistency\" />
66+
</ItemGroup>
67+
6468
</Project>

OnnxStack.StableDiffusion/Pipelines/StableDiffusion.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
using OnnxStack.Core.Services;
22
using OnnxStack.StableDiffusion.Common;
33
using OnnxStack.StableDiffusion.Diffusers;
4+
using OnnxStack.StableDiffusion.Diffusers.StableDiffusion;
45
using OnnxStack.StableDiffusion.Enums;
5-
using OnnxStack.StableDiffusion.Services;
66
using System.Collections.Concurrent;
77
using System.Collections.Generic;
88

OnnxStack.StableDiffusion/Schedulers/LCMScheduler.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ public LCMScheduler() : this(new SchedulerOptions()) { }
2828
public LCMScheduler(SchedulerOptions options) : base(options) { }
2929

3030

31+
/// <summary>
32+
/// Gets the compatible pipeline.
33+
/// </summary>
34+
public override DiffuserPipelineType PipelineType => DiffuserPipelineType.LatentConsistency;
35+
36+
3137
/// <summary>
3238
/// Initializes this instance.
3339
/// </summary>

0 commit comments

Comments
 (0)