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

Commit 7d60312

Browse files
committed
make the pipeline a bit more abstract
1 parent f6b6688 commit 7d60312

File tree

5 files changed

+141
-22
lines changed

5 files changed

+141
-22
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using OnnxStack.StableDiffusion.Diffusers;
2+
using OnnxStack.StableDiffusion.Enums;
3+
using System.Collections.Concurrent;
4+
5+
namespace OnnxStack.StableDiffusion.Common
6+
{
7+
public interface IPipeline
8+
{
9+
10+
/// <summary>
11+
/// Gets the type of the pipeline.
12+
/// </summary>
13+
DiffuserPipelineType PipelineType { get; }
14+
15+
16+
/// <summary>
17+
/// The pipelines diffuser set.
18+
/// </summary>
19+
ConcurrentDictionary<DiffuserType, IDiffuser> Diffusers { get; }
20+
21+
22+
/// <summary>
23+
/// Gets the diffuser.
24+
/// </summary>
25+
/// <param name="diffuserType">Type of the diffuser.</param>
26+
/// <returns></returns>
27+
IDiffuser GetDiffuser(DiffuserType diffuserType);
28+
}
29+
}

OnnxStack.StableDiffusion/Enums/SchedulerType.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ public enum SchedulerType
2020
DDIM = 4,
2121

2222
[Display(Name = "KDPM2")]
23-
KDPM2 = 5
23+
KDPM2 = 5,
24+
25+
[Display(Name = "LCM")]
26+
LCM = 20
2427
}
2528
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using OnnxStack.Core.Services;
2+
using OnnxStack.StableDiffusion.Common;
3+
using OnnxStack.StableDiffusion.Diffusers;
4+
using OnnxStack.StableDiffusion.Enums;
5+
using System.Collections.Concurrent;
6+
using System.Collections.Generic;
7+
8+
namespace OnnxStack.StableDiffusion.Pipelines
9+
{
10+
public sealed class LatentConsistencyPipeline : IPipeline
11+
{
12+
private readonly DiffuserPipelineType _pipelineType;
13+
private readonly ConcurrentDictionary<DiffuserType, IDiffuser> _diffusers;
14+
15+
public LatentConsistencyPipeline(IOnnxModelService onnxModelService, IPromptService promptService)
16+
{
17+
var diffusers = new Dictionary<DiffuserType, IDiffuser>
18+
{
19+
//TODO: TextToImage and ImageToImage is supported with LCM
20+
};
21+
_pipelineType = DiffuserPipelineType.LatentConsistency;
22+
_diffusers = new ConcurrentDictionary<DiffuserType, IDiffuser>(diffusers);
23+
}
24+
25+
26+
/// <summary>
27+
/// Gets the type of the pipeline.
28+
/// </summary>
29+
public DiffuserPipelineType PipelineType => _pipelineType;
30+
31+
32+
/// <summary>
33+
/// Gets the diffusers.
34+
/// </summary>
35+
public ConcurrentDictionary<DiffuserType, IDiffuser> Diffusers => _diffusers;
36+
37+
38+
/// <summary>
39+
/// Gets the diffuser.
40+
/// </summary>
41+
/// <param name="diffuserType">Type of the diffuser.</param>
42+
/// <returns></returns>
43+
public IDiffuser GetDiffuser(DiffuserType diffuserType)
44+
{
45+
_diffusers.TryGetValue(diffuserType, out var diffuser);
46+
return diffuser;
47+
}
48+
}
49+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using OnnxStack.Core.Services;
2+
using OnnxStack.StableDiffusion.Common;
3+
using OnnxStack.StableDiffusion.Diffusers;
4+
using OnnxStack.StableDiffusion.Enums;
5+
using OnnxStack.StableDiffusion.Services;
6+
using System.Collections.Concurrent;
7+
using System.Collections.Generic;
8+
9+
namespace OnnxStack.StableDiffusion.Pipelines
10+
{
11+
public sealed class StableDiffusionPipeline : IPipeline
12+
{
13+
private readonly DiffuserPipelineType _pipelineType;
14+
private readonly ConcurrentDictionary<DiffuserType, IDiffuser> _diffusers;
15+
16+
public StableDiffusionPipeline(IOnnxModelService onnxModelService, IPromptService promptService)
17+
{
18+
var diffusers = new Dictionary<DiffuserType, IDiffuser>
19+
{
20+
{ DiffuserType.TextToImage, new TextDiffuser(onnxModelService, promptService) },
21+
{ DiffuserType.ImageToImage, new ImageDiffuser(onnxModelService, promptService) },
22+
{ DiffuserType.ImageInpaint, new InpaintDiffuser(onnxModelService, promptService) },
23+
{ DiffuserType.ImageInpaintLegacy, new InpaintLegacyDiffuser(onnxModelService, promptService) }
24+
};
25+
_pipelineType = DiffuserPipelineType.StableDiffusion;
26+
_diffusers = new ConcurrentDictionary<DiffuserType, IDiffuser>(diffusers);
27+
}
28+
29+
30+
/// <summary>
31+
/// Gets the type of the pipeline.
32+
/// </summary>
33+
public DiffuserPipelineType PipelineType => _pipelineType;
34+
35+
36+
/// <summary>
37+
/// Gets the diffusers.
38+
/// </summary>
39+
public ConcurrentDictionary<DiffuserType, IDiffuser> Diffusers => _diffusers;
40+
41+
42+
/// <summary>
43+
/// Gets the diffuser.
44+
/// </summary>
45+
/// <param name="diffuserType">Type of the diffuser.</param>
46+
/// <returns></returns>
47+
public IDiffuser GetDiffuser(DiffuserType diffuserType)
48+
{
49+
_diffusers.TryGetValue(diffuserType, out var diffuser);
50+
return diffuser;
51+
}
52+
}
53+
}

OnnxStack.StableDiffusion/Services/StableDiffusionService.cs

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@
22
using OnnxStack.Core.Services;
33
using OnnxStack.StableDiffusion.Common;
44
using OnnxStack.StableDiffusion.Config;
5-
using OnnxStack.StableDiffusion.Diffusers;
65
using OnnxStack.StableDiffusion.Enums;
76
using OnnxStack.StableDiffusion.Helpers;
7+
using OnnxStack.StableDiffusion.Pipelines;
88
using SixLabors.ImageSharp;
99
using SixLabors.ImageSharp.PixelFormats;
1010
using System;
11-
using System.Collections.Concurrent;
1211
using System.Collections.Generic;
13-
using System.Collections.Immutable;
1412
using System.IO;
1513
using System.Threading;
1614
using System.Threading.Tasks;
@@ -25,7 +23,7 @@ public sealed class StableDiffusionService : IStableDiffusionService
2523
{
2624
private readonly IOnnxModelService _onnxModelService;
2725
private readonly StableDiffusionConfig _configuration;
28-
private readonly IDictionary<DiffuserPipelineType, IDictionary<DiffuserType, IDiffuser>> _diffusers;
26+
private readonly IDictionary<DiffuserPipelineType, IPipeline> _pipelines;
2927

3028
/// <summary>
3129
/// Initializes a new instance of the <see cref="StableDiffusionService"/> class.
@@ -35,21 +33,8 @@ public StableDiffusionService(StableDiffusionConfig configuration, IOnnxModelSer
3533
{
3634
_configuration = configuration;
3735
_onnxModelService = onnxModelService;
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);
36+
_pipelines.Add(DiffuserPipelineType.StableDiffusion, new StableDiffusionPipeline(onnxModelService, promptService));
37+
_pipelines.Add(DiffuserPipelineType.LatentConsistency, new LatentConsistencyPipeline(onnxModelService, promptService));
5338
}
5439

5540

@@ -156,11 +141,11 @@ public async Task<Stream> GenerateAsStreamAsync(IModelOptions model, PromptOptio
156141

157142
private async Task<DenseTensor<float>> DiffuseAsync(IModelOptions modelOptions, PromptOptions promptOptions, SchedulerOptions schedulerOptions, Action<int, int> progress = null, CancellationToken cancellationToken = default)
158143
{
159-
var pipeline = _diffusers[modelOptions.PipelineType];
144+
var pipeline = _pipelines[modelOptions.PipelineType];
160145
if (pipeline is null)
161146
throw new Exception("Pipeline not found or is unsupported");
162147

163-
var diffuser = pipeline[promptOptions.DiffuserType];
148+
var diffuser = pipeline.GetDiffuser(promptOptions.DiffuserType);
164149
if (diffuser is null)
165150
throw new Exception("Diffuser not found or is unsupported");
166151

0 commit comments

Comments
 (0)