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

Commit d4aa01a

Browse files
committed
Support for LCM and other pipelines
1 parent 8f8af8b commit d4aa01a

File tree

10 files changed

+49
-9
lines changed

10 files changed

+49
-9
lines changed

OnnxStack.Console/appsettings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"TokenizerLimit": 77,
1818
"EmbeddingsLength": 768,
1919
"ScaleFactor": 0.18215,
20+
"PipelineType": "StableDiffusion",
2021
"Diffusers": [
2122
"TextToImage",
2223
"ImageToImage",

OnnxStack.Core/Extensions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.Collections.Concurrent;
55
using System.Collections.Generic;
66
using System.Linq;
7-
using System.Xml.Linq;
87

98
namespace OnnxStack.Core
109
{

OnnxStack.StableDiffusion/Common/IModelOptions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public interface IModelOptions : IOnnxModel
1414
int TokenizerLimit { get; set; }
1515
int InputTokenLimit { get; set; }
1616
int EmbeddingsLength { get; set; }
17+
DiffuserPipelineType PipelineType { get; set; }
1718
List<DiffuserType> Diffusers { get; set; }
1819
ImmutableArray<int> BlankTokenValueArray { get; set; }
1920
}

OnnxStack.StableDiffusion/Common/IScheduler.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
using Microsoft.ML.OnnxRuntime.Tensors;
2+
using OnnxStack.StableDiffusion.Enums;
23
using System;
34
using System.Collections.Generic;
45

56
namespace OnnxStack.StableDiffusion.Common
67
{
78
public interface IScheduler : IDisposable
89
{
10+
/// <summary>
11+
/// Gets the compatible pipeline
12+
/// </summary>
13+
DiffuserPipelineType PipelineType { get; }
14+
915
/// <summary>
1016
/// Gets the initial noise sigma.
1117
/// </summary>

OnnxStack.StableDiffusion/Config/ModelOptions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class ModelOptions : IModelOptions, IOnnxModelSetConfig
1919
public int TokenizerLimit { get; set; }
2020
public int EmbeddingsLength { get; set; }
2121
public float ScaleFactor { get; set; }
22+
public DiffuserPipelineType PipelineType { get; set; }
2223
public List<DiffuserType> Diffusers { get; set; } = new List<DiffuserType>();
2324

2425
public int DeviceId { get; set; }
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace OnnxStack.StableDiffusion.Enums
2+
{
3+
public enum DiffuserPipelineType
4+
{
5+
StableDiffusion = 0,
6+
LatentConsistency = 10
7+
}
8+
}

OnnxStack.StableDiffusion/Schedulers/SchedulerBase.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ public SchedulerBase(SchedulerOptions schedulerOptions)
5050
/// </summary>
5151
public IReadOnlyList<int> Timesteps => _timesteps;
5252

53+
/// <summary>
54+
/// Gets the compatible pipeline.
55+
/// </summary>
56+
public virtual DiffuserPipelineType PipelineType => DiffuserPipelineType.StableDiffusion;
57+
5358
/// <summary>
5459
/// Scales the input.
5560
/// </summary>

OnnxStack.StableDiffusion/Services/StableDiffusionService.cs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public sealed class StableDiffusionService : IStableDiffusionService
2525
{
2626
private readonly IOnnxModelService _onnxModelService;
2727
private readonly StableDiffusionConfig _configuration;
28-
private readonly IDictionary<DiffuserType, IDiffuser> _diffusers;
28+
private readonly IDictionary<DiffuserPipelineType, IDictionary<DiffuserType, IDiffuser>> _diffusers;
2929

3030
/// <summary>
3131
/// Initializes a new instance of the <see cref="StableDiffusionService"/> class.
@@ -35,11 +35,21 @@ public StableDiffusionService(StableDiffusionConfig configuration, IOnnxModelSer
3535
{
3636
_configuration = configuration;
3737
_onnxModelService = onnxModelService;
38-
_diffusers = new ConcurrentDictionary<DiffuserType, IDiffuser>();
39-
_diffusers.Add(DiffuserType.TextToImage, new TextDiffuser(onnxModelService, promptService));
40-
_diffusers.Add(DiffuserType.ImageToImage, new ImageDiffuser(onnxModelService, promptService));
41-
_diffusers.Add(DiffuserType.ImageInpaint, new InpaintDiffuser(onnxModelService, promptService));
42-
_diffusers.Add(DiffuserType.ImageInpaintLegacy, new InpaintLegacyDiffuser(onnxModelService, promptService));
38+
var stableDiffusionPipeline = new Dictionary<DiffuserType, IDiffuser>
39+
{
40+
{ DiffuserType.TextToImage, new TextDiffuser(onnxModelService, promptService) },
41+
{ DiffuserType.ImageToImage, new ImageDiffuser(onnxModelService, promptService) },
42+
{ DiffuserType.ImageInpaint, new InpaintDiffuser(onnxModelService, promptService) },
43+
{ DiffuserType.ImageInpaintLegacy, new InpaintLegacyDiffuser(onnxModelService, promptService) }
44+
};
45+
46+
var latentConsistancyPipeline = new Dictionary<DiffuserType, IDiffuser>
47+
{
48+
//TODO: TextToImage and ImageToImage is supported with LCM
49+
};
50+
51+
_diffusers.Add(DiffuserPipelineType.StableDiffusion, stableDiffusionPipeline);
52+
_diffusers.Add(DiffuserPipelineType.LatentConsistency, latentConsistancyPipeline);
4353
}
4454

4555

@@ -146,8 +156,15 @@ public async Task<Stream> GenerateAsStreamAsync(IModelOptions model, PromptOptio
146156

147157
private async Task<DenseTensor<float>> DiffuseAsync(IModelOptions modelOptions, PromptOptions promptOptions, SchedulerOptions schedulerOptions, Action<int, int> progress = null, CancellationToken cancellationToken = default)
148158
{
149-
return await _diffusers[promptOptions.DiffuserType]
150-
.DiffuseAsync(modelOptions, promptOptions, schedulerOptions, progress, cancellationToken);
159+
var pipeline = _diffusers[modelOptions.PipelineType];
160+
if (pipeline is null)
161+
throw new Exception("Pipeline not found or is unsupported");
162+
163+
var diffuser = pipeline[promptOptions.DiffuserType];
164+
if (diffuser is null)
165+
throw new Exception("Diffuser not found or is unsupported");
166+
167+
return await diffuser.DiffuseAsync(modelOptions, promptOptions, schedulerOptions, progress, cancellationToken);
151168
}
152169
}
153170
}

OnnxStack.UI/appsettings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"TokenizerLimit": 77,
1818
"EmbeddingsLength": 768,
1919
"ScaleFactor": 0.18215,
20+
"PipelineType": "StableDiffusion",
2021
"Diffusers": [
2122
"TextToImage",
2223
"ImageToImage",

OnnxStack.WebUI/appsettings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"TokenizerLimit": 77,
1818
"EmbeddingsLength": 768,
1919
"ScaleFactor": 0.18215,
20+
"PipelineType": "StableDiffusion",
2021
"Diffusers": [
2122
"TextToImage",
2223
"ImageToImage",

0 commit comments

Comments
 (0)