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

Commit 0ca1152

Browse files
committed
Make pipeline collection thread-safe (WebUI)
1 parent 7d60312 commit 0ca1152

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

OnnxStack.StableDiffusion/Services/StableDiffusionService.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.ML.OnnxRuntime.Tensors;
2+
using OnnxStack.Core;
23
using OnnxStack.Core.Services;
34
using OnnxStack.StableDiffusion.Common;
45
using OnnxStack.StableDiffusion.Config;
@@ -8,6 +9,7 @@
89
using SixLabors.ImageSharp;
910
using SixLabors.ImageSharp.PixelFormats;
1011
using System;
12+
using System.Collections.Concurrent;
1113
using System.Collections.Generic;
1214
using System.IO;
1315
using System.Threading;
@@ -23,7 +25,7 @@ public sealed class StableDiffusionService : IStableDiffusionService
2325
{
2426
private readonly IOnnxModelService _onnxModelService;
2527
private readonly StableDiffusionConfig _configuration;
26-
private readonly IDictionary<DiffuserPipelineType, IPipeline> _pipelines;
28+
private readonly ConcurrentDictionary<DiffuserPipelineType, IPipeline> _pipelines;
2729

2830
/// <summary>
2931
/// Initializes a new instance of the <see cref="StableDiffusionService"/> class.
@@ -33,8 +35,9 @@ public StableDiffusionService(StableDiffusionConfig configuration, IOnnxModelSer
3335
{
3436
_configuration = configuration;
3537
_onnxModelService = onnxModelService;
36-
_pipelines.Add(DiffuserPipelineType.StableDiffusion, new StableDiffusionPipeline(onnxModelService, promptService));
37-
_pipelines.Add(DiffuserPipelineType.LatentConsistency, new LatentConsistencyPipeline(onnxModelService, promptService));
38+
_pipelines = new ConcurrentDictionary<DiffuserPipelineType, IPipeline>();
39+
_pipelines.TryAdd(DiffuserPipelineType.StableDiffusion, new StableDiffusionPipeline(onnxModelService, promptService));
40+
_pipelines.TryAdd(DiffuserPipelineType.LatentConsistency, new LatentConsistencyPipeline(onnxModelService, promptService));
3841
}
3942

4043

@@ -141,8 +144,7 @@ public async Task<Stream> GenerateAsStreamAsync(IModelOptions model, PromptOptio
141144

142145
private async Task<DenseTensor<float>> DiffuseAsync(IModelOptions modelOptions, PromptOptions promptOptions, SchedulerOptions schedulerOptions, Action<int, int> progress = null, CancellationToken cancellationToken = default)
143146
{
144-
var pipeline = _pipelines[modelOptions.PipelineType];
145-
if (pipeline is null)
147+
if (!_pipelines.TryGetValue(modelOptions.PipelineType, out var pipeline))
146148
throw new Exception("Pipeline not found or is unsupported");
147149

148150
var diffuser = pipeline.GetDiffuser(promptOptions.DiffuserType);

0 commit comments

Comments
 (0)