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

Commit d2a90ca

Browse files
committed
Remove ModelSetConfig caching from Core
1 parent 102ce5f commit d2a90ca

File tree

14 files changed

+35
-301
lines changed

14 files changed

+35
-301
lines changed

OnnxStack.Console/Examples/StableDebug.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ namespace OnnxStack.Console.Runner
1010
public sealed class StableDebug : IExampleRunner
1111
{
1212
private readonly string _outputDirectory;
13+
private readonly StableDiffusionConfig _configuration;
1314
private readonly IStableDiffusionService _stableDiffusionService;
1415

15-
public StableDebug(IStableDiffusionService stableDiffusionService)
16+
public StableDebug(StableDiffusionConfig configuration, IStableDiffusionService stableDiffusionService)
1617
{
18+
_configuration = configuration;
1719
_stableDiffusionService = stableDiffusionService;
1820
_outputDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Examples", nameof(StableDebug));
1921
}
@@ -48,7 +50,7 @@ public async Task RunAsync()
4850
Strength = 0.6f
4951
};
5052

51-
foreach (var model in _stableDiffusionService.ModelSets)
53+
foreach (var model in _configuration.ModelSets)
5254
{
5355
OutputHelpers.WriteConsole($"Loading Model `{model.Name}`...", ConsoleColor.Green);
5456
await _stableDiffusionService.LoadModelAsync(model);

OnnxStack.Console/Examples/StableDiffusionBatch.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
using OnnxStack.StableDiffusion.Common;
22
using OnnxStack.StableDiffusion.Config;
33
using OnnxStack.StableDiffusion.Enums;
4-
using OnnxStack.StableDiffusion;
5-
using SixLabors.ImageSharp;
64
using OnnxStack.StableDiffusion.Helpers;
5+
using SixLabors.ImageSharp;
76

87
namespace OnnxStack.Console.Runner
98
{
109
public sealed class StableDiffusionBatch : IExampleRunner
1110
{
1211
private readonly string _outputDirectory;
12+
private readonly StableDiffusionConfig _configuration;
1313
private readonly IStableDiffusionService _stableDiffusionService;
1414

15-
public StableDiffusionBatch(IStableDiffusionService stableDiffusionService)
15+
public StableDiffusionBatch(StableDiffusionConfig configuration, IStableDiffusionService stableDiffusionService)
1616
{
17+
_configuration = configuration;
1718
_stableDiffusionService = stableDiffusionService;
1819
_outputDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Examples", nameof(StableDiffusionBatch));
1920
}
@@ -51,7 +52,7 @@ public async Task RunAsync()
5152
BatchType = BatchOptionType.Scheduler
5253
};
5354

54-
foreach (var model in _stableDiffusionService.ModelSets)
55+
foreach (var model in _configuration.ModelSets)
5556
{
5657
OutputHelpers.WriteConsole($"Loading Model `{model.Name}`...", ConsoleColor.Green);
5758
await _stableDiffusionService.LoadModelAsync(model);

OnnxStack.Console/Examples/StableDiffusionExample.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ namespace OnnxStack.Console.Runner
88
public sealed class StableDiffusionExample : IExampleRunner
99
{
1010
private readonly string _outputDirectory;
11+
private readonly StableDiffusionConfig _configuration;
1112
private readonly IStableDiffusionService _stableDiffusionService;
1213

13-
public StableDiffusionExample(IStableDiffusionService stableDiffusionService)
14+
public StableDiffusionExample(StableDiffusionConfig configuration, IStableDiffusionService stableDiffusionService)
1415
{
16+
_configuration = configuration;
1517
_stableDiffusionService = stableDiffusionService;
1618
_outputDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Examples", nameof(StableDiffusionExample));
1719
}
@@ -47,7 +49,7 @@ public async Task RunAsync()
4749
Seed = Random.Shared.Next()
4850
};
4951

50-
foreach (var model in _stableDiffusionService.ModelSets)
52+
foreach (var model in _configuration.ModelSets)
5153
{
5254
OutputHelpers.WriteConsole($"Loading Model `{model.Name}`...", ConsoleColor.Green);
5355
await _stableDiffusionService.LoadModelAsync(model);

OnnxStack.Console/Examples/StableDiffusionGenerator.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ namespace OnnxStack.Console.Runner
99
public sealed class StableDiffusionGenerator : IExampleRunner
1010
{
1111
private readonly string _outputDirectory;
12+
private readonly StableDiffusionConfig _configuration;
1213
private readonly IStableDiffusionService _stableDiffusionService;
1314
private readonly ReadOnlyDictionary<string, string> _generationPrompts;
1415

15-
public StableDiffusionGenerator(IStableDiffusionService stableDiffusionService)
16+
public StableDiffusionGenerator(StableDiffusionConfig configuration, IStableDiffusionService stableDiffusionService)
1617
{
18+
_configuration = configuration;
1719
_stableDiffusionService = stableDiffusionService;
1820
_generationPrompts = GeneratePrompts();
1921
_outputDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Examples", nameof(StableDiffusionGenerator));
@@ -31,7 +33,7 @@ public async Task RunAsync()
3133
Directory.CreateDirectory(_outputDirectory);
3234

3335
var seed = Random.Shared.Next();
34-
foreach (var model in _stableDiffusionService.ModelSets)
36+
foreach (var model in _configuration.ModelSets)
3537
{
3638
OutputHelpers.WriteConsole($"Loading Model `{model.Name}`...", ConsoleColor.Green);
3739
await _stableDiffusionService.LoadModelAsync(model);

OnnxStack.Console/Examples/StableDiffusionGif.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
11
using OnnxStack.Core.Image;
2-
using OnnxStack.StableDiffusion;
32
using OnnxStack.StableDiffusion.Common;
43
using OnnxStack.StableDiffusion.Config;
54
using OnnxStack.StableDiffusion.Enums;
6-
using OnnxStack.StableDiffusion.Helpers;
7-
using OnnxStack.StableDiffusion.Models;
85
using SixLabors.ImageSharp;
9-
using SixLabors.ImageSharp.Formats.Gif;
106
using SixLabors.ImageSharp.PixelFormats;
117
using SixLabors.ImageSharp.Processing;
12-
using System.Diagnostics;
138

149
namespace OnnxStack.Console.Runner
1510
{
1611
public sealed class StableDiffusionGif : IExampleRunner
1712
{
1813
private readonly string _outputDirectory;
14+
private readonly StableDiffusionConfig _configuration;
1915
private readonly IStableDiffusionService _stableDiffusionService;
2016

21-
public StableDiffusionGif(IStableDiffusionService stableDiffusionService)
17+
public StableDiffusionGif(StableDiffusionConfig configuration, IStableDiffusionService stableDiffusionService)
2218
{
19+
_configuration = configuration;
2320
_stableDiffusionService = stableDiffusionService;
2421
_outputDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Examples", nameof(StableDiffusionGif));
2522
Directory.CreateDirectory(_outputDirectory);
@@ -54,7 +51,7 @@ public async Task RunAsync()
5451
};
5552

5653
// Choose Model
57-
var model = _stableDiffusionService.ModelSets.FirstOrDefault(x => x.Name == "LCM-Dreamshaper-V7");
54+
var model = _configuration.ModelSets.FirstOrDefault(x => x.Name == "LCM-Dreamshaper-V7");
5855
OutputHelpers.WriteConsole($"Loading Model `{model.Name}`...", ConsoleColor.Green);
5956
await _stableDiffusionService.LoadModelAsync(model);
6057

OnnxStack.Console/Examples/UpscaleExample.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using OnnxStack.Core.Image;
2+
using OnnxStack.ImageUpscaler.Config;
23
using OnnxStack.ImageUpscaler.Services;
34
using SixLabors.ImageSharp;
45
using SixLabors.ImageSharp.PixelFormats;
@@ -8,11 +9,13 @@ namespace OnnxStack.Console.Runner
89
public sealed class UpscaleExample : IExampleRunner
910
{
1011
private readonly string _outputDirectory;
12+
private readonly ImageUpscalerConfig _configuration;
1113
private readonly IUpscaleService _imageUpscaleService;
1214

1315

14-
public UpscaleExample(IUpscaleService imageUpscaleService)
16+
public UpscaleExample(ImageUpscalerConfig configuration, IUpscaleService imageUpscaleService)
1517
{
18+
_configuration = configuration;
1619
_imageUpscaleService = imageUpscaleService;
1720
_outputDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Examples", nameof(UpscaleExample));
1821
Directory.CreateDirectory(_outputDirectory);
@@ -24,7 +27,7 @@ public UpscaleExample(IUpscaleService imageUpscaleService)
2427

2528
public async Task RunAsync()
2629
{
27-
var modelSet = _imageUpscaleService.ModelSets.FirstOrDefault(x => x.Name == "RealSR BSRGAN x4");
30+
var modelSet = _configuration.ModelSets.FirstOrDefault(x => x.Name == "RealSR BSRGAN x4");
2831

2932

3033

OnnxStack.Core/Services/IOnnxModelService.cs

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,44 +14,12 @@ public interface IOnnxModelService : IDisposable
1414
/// </summary>
1515
IEnumerable<OnnxModelSet> ModelSets { get; }
1616

17-
/// <summary>
18-
/// Gets the ModelSet configs.
19-
/// </summary>
20-
IEnumerable<IOnnxModelSetConfig> ModelSetConfigs { get; }
21-
22-
/// <summary>
23-
/// Adds a ModelSet
24-
/// </summary>
25-
/// <param name="modelSet">The model set.</param>
26-
/// <returns></returns>
27-
Task<bool> AddModelSet(IOnnxModelSetConfig modelSet);
28-
29-
/// <summary>
30-
/// Adds a collection of ModelSet
31-
/// </summary>
32-
/// <param name="modelSets">The model sets.</param>
33-
Task AddModelSet(IEnumerable<IOnnxModelSetConfig> modelSets);
34-
35-
/// <summary>
36-
/// Removes a model set.
37-
/// </summary>
38-
/// <param name="modelSet">The model set.</param>
39-
/// <returns></returns>
40-
Task<bool> RemoveModelSet(IOnnxModelSetConfig modelSet);
41-
42-
/// <summary>
43-
/// Updates the model set.
44-
/// </summary>
45-
/// <param name="modelSet">The model set.</param>
46-
/// <returns></returns>
47-
Task<bool> UpdateModelSet(IOnnxModelSetConfig modelSet);
48-
4917
/// <summary>
5018
/// Loads the model.
5119
/// </summary>
5220
/// <param name="model">The model.</param>
5321
/// <returns></returns>
54-
Task<OnnxModelSet> LoadModelAsync(IOnnxModel model);
22+
Task<OnnxModelSet> LoadModelAsync(IOnnxModelSetConfig model);
5523

5624
/// <summary>
5725
/// Unloads the model.

OnnxStack.Core/Services/OnnxModelService.cs

Lines changed: 7 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ public sealed class OnnxModelService : IOnnxModelService
1717
{
1818
private readonly OnnxStackConfig _configuration;
1919
private readonly ConcurrentDictionary<IOnnxModel, OnnxModelSet> _onnxModelSets;
20-
private readonly ConcurrentDictionary<IOnnxModel, IOnnxModelSetConfig> _onnxModelSetConfigs;
2120

2221
/// <summary>
2322
/// Initializes a new instance of the <see cref="OnnxModelService"/> class.
@@ -27,7 +26,6 @@ public OnnxModelService(OnnxStackConfig configuration)
2726
{
2827
_configuration = configuration;
2928
_onnxModelSets = new ConcurrentDictionary<IOnnxModel, OnnxModelSet>(new OnnxModelEqualityComparer());
30-
_onnxModelSetConfigs = new ConcurrentDictionary<IOnnxModel, IOnnxModelSetConfig>(new OnnxModelEqualityComparer());
3129
}
3230

3331

@@ -37,65 +35,12 @@ public OnnxModelService(OnnxStackConfig configuration)
3735
public IEnumerable<OnnxModelSet> ModelSets => _onnxModelSets.Values;
3836

3937

40-
/// <summary>
41-
/// Gets the ModelSet configs.
42-
/// </summary>
43-
public IEnumerable<IOnnxModelSetConfig> ModelSetConfigs => _onnxModelSetConfigs.Values;
44-
45-
46-
/// <summary>
47-
/// Adds a model set.
48-
/// </summary>
49-
/// <param name="modelSet">The model set.</param>
50-
/// <returns></returns>
51-
public Task<bool> AddModelSet(IOnnxModelSetConfig modelSet)
52-
{
53-
return Task.FromResult(_onnxModelSetConfigs.TryAdd(modelSet, modelSet));
54-
}
55-
56-
/// <summary>
57-
/// Adds a modelsets.
58-
/// </summary>
59-
/// <param name="modelSets">The model sets.</param>
60-
public Task AddModelSet(IEnumerable<IOnnxModelSetConfig> modelSets)
61-
{
62-
foreach (var modelSet in modelSets)
63-
{
64-
AddModelSet(modelSet);
65-
}
66-
return Task.CompletedTask;
67-
}
68-
69-
70-
/// <summary>
71-
/// Removes the model set.
72-
/// </summary>
73-
/// <param name="modelSet">The model set.</param>
74-
/// <returns></returns>
75-
public Task<bool> RemoveModelSet(IOnnxModelSetConfig modelSet)
76-
{
77-
return Task.FromResult(_onnxModelSetConfigs.TryRemove(modelSet, out _));
78-
}
79-
80-
81-
/// <summary>
82-
/// Updates an existing model set.
83-
/// </summary>
84-
/// <param name="modelSet">The model set.</param>
85-
/// <returns></returns>
86-
public Task<bool> UpdateModelSet(IOnnxModelSetConfig modelSet)
87-
{
88-
_onnxModelSetConfigs.TryRemove(modelSet, out _);
89-
return Task.FromResult(_onnxModelSetConfigs.TryAdd(modelSet, modelSet));
90-
}
91-
92-
9338
/// <summary>
9439
/// Loads the model.
9540
/// </summary>
9641
/// <param name="model">The model.</param>
9742
/// <returns></returns>
98-
public async Task<OnnxModelSet> LoadModelAsync(IOnnxModel model)
43+
public async Task<OnnxModelSet> LoadModelAsync(IOnnxModelSetConfig model)
9944
{
10045
return await Task.Run(() => LoadModelSet(model)).ConfigureAwait(false);
10146
}
@@ -264,19 +209,16 @@ private OnnxModelSet GetModelSet(IOnnxModel model)
264209
/// <param name="model">The model.</param>
265210
/// <returns></returns>
266211
/// <exception cref="System.Exception">Model {model.Name} not found in configuration</exception>
267-
private OnnxModelSet LoadModelSet(IOnnxModel model)
212+
private OnnxModelSet LoadModelSet(IOnnxModelSetConfig modelSetConfig)
268213
{
269-
if (_onnxModelSets.ContainsKey(model))
270-
return _onnxModelSets[model];
271-
272-
if (!_onnxModelSetConfigs.TryGetValue(model, out var modelSetConfig))
273-
throw new Exception($"Model {model.Name} not found");
274-
214+
if (_onnxModelSets.ContainsKey(modelSetConfig))
215+
return _onnxModelSets[modelSetConfig];
216+
275217
if (!modelSetConfig.IsEnabled)
276-
throw new Exception($"Model {model.Name} is not enabled");
218+
throw new Exception($"Model {modelSetConfig.Name} is not enabled");
277219

278220
var modelSet = new OnnxModelSet(modelSetConfig);
279-
_onnxModelSets.TryAdd(model, modelSet);
221+
_onnxModelSets.TryAdd(modelSetConfig, modelSet);
280222
return modelSet;
281223
}
282224

OnnxStack.ImageUpscaler/Services/IUpscaleService.cs

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
using Microsoft.ML.OnnxRuntime.Tensors;
22
using OnnxStack.Core.Image;
3-
using OnnxStack.ImageUpscaler.Config;
43
using OnnxStack.StableDiffusion.Config;
54
using SixLabors.ImageSharp;
65
using SixLabors.ImageSharp.PixelFormats;
7-
using System.Collections.Generic;
86
using System.IO;
97
using System.Threading.Tasks;
108

@@ -13,32 +11,6 @@ namespace OnnxStack.ImageUpscaler.Services
1311
public interface IUpscaleService
1412
{
1513

16-
/// <summary>
17-
/// Gets the model sets.
18-
/// </summary>
19-
IReadOnlyCollection<UpscaleModelSet> ModelSets { get; }
20-
21-
/// <summary>
22-
/// Adds the model.
23-
/// </summary>
24-
/// <param name="model">The model.</param>
25-
/// <returns></returns>
26-
Task<bool> AddModelAsync(UpscaleModelSet model);
27-
28-
/// <summary>
29-
/// Removes the model.
30-
/// </summary>
31-
/// <param name="model">The model.</param>
32-
/// <returns></returns>
33-
Task<bool> RemoveModelAsync(UpscaleModelSet model);
34-
35-
/// <summary>
36-
/// Updates the model.
37-
/// </summary>
38-
/// <param name="model">The model.</param>
39-
/// <returns></returns>
40-
Task<bool> UpdateModelAsync(UpscaleModelSet model);
41-
4214
/// <summary>
4315
/// Loads the model.
4416
/// </summary>

0 commit comments

Comments
 (0)