11using Microsoft . ML . OnnxRuntime . Tensors ;
2+ using OnnxStack . Core ;
23using OnnxStack . Core . Services ;
34using OnnxStack . StableDiffusion . Common ;
45using OnnxStack . StableDiffusion . Config ;
89using SixLabors . ImageSharp ;
910using SixLabors . ImageSharp . PixelFormats ;
1011using System ;
12+ using System . Collections . Concurrent ;
1113using System . Collections . Generic ;
1214using System . IO ;
1315using 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