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

Commit 8f65278

Browse files
committed
Support Inpaint or InpaintLegacy based on Model configuration
1 parent b88ab19 commit 8f65278

File tree

16 files changed

+152
-98
lines changed

16 files changed

+152
-98
lines changed

OnnxStack.Console/appsettings.json

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,29 @@
1313
"IsEnabled": true,
1414
"PadTokenId": 49407,
1515
"BlankTokenId": 49407,
16-
"InputTokenLimit": 512,
16+
"InputTokenLimit": 2048,
1717
"TokenizerLimit": 77,
1818
"EmbeddingsLength": 768,
1919
"ScaleFactor": 0.18215,
20+
"Diffusers": [
21+
"TextToImage",
22+
"ImageToImage",
23+
"ImageInpaintLegacy"
24+
],
2025
"DeviceId": 0,
2126
"InterOpNumThreads": 0,
2227
"IntraOpNumThreads": 0,
23-
"ExecutionMode": "ORT_PARALLEL",
28+
"ExecutionMode": "ORT_SEQUENTIAL",
2429
"ExecutionProvider": "DirectML",
2530
"ModelConfigurations": [
26-
{
27-
"Type": "Unet",
28-
"OnnxModelPath": "D:\\Repositories\\stable-diffusion-v1-5\\unet\\model.onnx"
29-
},
3031
{
3132
"Type": "Tokenizer",
3233
"OnnxModelPath": "D:\\Repositories\\stable-diffusion-v1-5\\cliptokenizer.onnx"
3334
},
35+
{
36+
"Type": "Unet",
37+
"OnnxModelPath": "D:\\Repositories\\stable-diffusion-v1-5\\unet\\model.onnx"
38+
},
3439
{
3540
"Type": "TextEncoder",
3641
"OnnxModelPath": "D:\\Repositories\\stable-diffusion-v1-5\\text_encoder\\model.onnx"
@@ -42,14 +47,9 @@
4247
{
4348
"Type": "VaeDecoder",
4449
"OnnxModelPath": "D:\\Repositories\\stable-diffusion-v1-5\\vae_decoder\\model.onnx"
45-
},
46-
{
47-
"IsEnabled": false,
48-
"Type": "SafetyChecker",
49-
"OnnxModelPath": "D:\\Repositories\\stable-diffusion-v1-5\\safety_checker\\model.onnx"
5050
}
5151
]
5252
}
5353
]
5454
}
55-
}
55+
}

OnnxStack.Core/Config/ConfigManager.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,12 @@ public static void SaveConfiguration(OnnxStackConfig configuration)
9393
SaveConfiguration<OnnxStackConfig>(configuration);
9494
}
9595

96-
public static void SaveConfiguration<T>(T configuration)
96+
public static void SaveConfiguration<T>(T configuration) where T : class, IConfigSection
97+
{
98+
SaveConfiguration<T>(typeof(T).Name, configuration);
99+
}
100+
101+
public static void SaveConfiguration<T>(string sectionName, T configuration) where T : class, IConfigSection
97102
{
98103

99104
string appsettingStreamFile = GetAppSettingsFile();
@@ -104,7 +109,7 @@ public static void SaveConfiguration<T>(T configuration)
104109
appSettings = JsonSerializer.Deserialize<Dictionary<string, object>>(appsettingReadStream, GetSerializerOptions());
105110

106111
// Set Object
107-
appSettings[typeof(T).Name] = configuration;
112+
appSettings[sectionName] = configuration;
108113

109114
// Write out file
110115
var serializerOptions = GetSerializerOptions();

OnnxStack.Core/Config/IOnnxModelSetConfig.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ public interface IOnnxModelSetConfig : IOnnxModel
77
{
88
bool IsEnabled { get; set; }
99
int DeviceId { get; set; }
10-
string OnnxModelPath { get; set; }
1110
int InterOpNumThreads { get; set; }
1211
int IntraOpNumThreads { get; set; }
1312
ExecutionMode ExecutionMode { get; set; }
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.ML.OnnxRuntime;
2+
using System.Text.Json.Serialization;
23

34
namespace OnnxStack.Core.Config
45
{
@@ -7,12 +8,19 @@ public class OnnxModelSessionConfig
78
public OnnxModelType Type { get; set; }
89
public string OnnxModelPath { get; set; }
910

10-
public bool? IsEnabled { get; set; }
11+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
1112
public int? DeviceId { get; set; }
13+
14+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
1215
public int? InterOpNumThreads { get; set; }
16+
17+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
1318
public int? IntraOpNumThreads { get; set; }
19+
20+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
1421
public ExecutionMode? ExecutionMode { get; set; }
22+
23+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
1524
public ExecutionProvider? ExecutionProvider { get; set; }
16-
1725
}
1826
}

OnnxStack.Core/Extensions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ public static void ApplyConfigurationOverrides(this IOnnxModelSetConfig innxMode
5353

5454
foreach (var modelConfig in innxModelSetConfig.ModelConfigurations)
5555
{
56-
modelConfig.IsEnabled = modelConfig.IsEnabled != false;
5756
modelConfig.DeviceId ??= innxModelSetConfig.DeviceId;
5857
modelConfig.ExecutionMode ??= innxModelSetConfig.ExecutionMode;
5958
modelConfig.InterOpNumThreads ??= innxModelSetConfig.InterOpNumThreads;

OnnxStack.Core/Model/OnnxModelSet.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ public OnnxModelSet(IOnnxModelSetConfig configuration)
2020
{
2121
_configuration = configuration;
2222
_prePackedWeightsContainer = new PrePackedWeightsContainer();
23-
_modelSessions = configuration.ModelConfigurations
24-
.Where(x => x.IsEnabled == true)
25-
.ToImmutableDictionary(
23+
_modelSessions = configuration.ModelConfigurations.ToImmutableDictionary(
2624
modelConfig => modelConfig.Type,
2725
modelConfig => new OnnxModelSession(modelConfig, _prePackedWeightsContainer));
2826
}

OnnxStack.Core/Services/IOnnxModelService.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ public interface IOnnxModelService : IDisposable
3333
/// </returns>
3434
bool IsModelLoaded(IOnnxModel model);
3535

36+
37+
/// <summary>
38+
/// Updates the model set.
39+
/// </summary>
40+
/// <param name="modelSet">The model set.</param>
41+
/// <returns></returns>
42+
bool UpdateModelSet(IOnnxModelSetConfig modelSet);
43+
3644
/// <summary>
3745
/// Determines whether the specified model type is enabled.
3846
/// </summary>

OnnxStack.Core/Services/OnnxModelService.cs

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

2222
/// <summary>
2323
/// Initializes a new instance of the <see cref="OnnxModelService"/> class.
@@ -27,7 +27,19 @@ public OnnxModelService(OnnxStackConfig configuration)
2727
{
2828
_configuration = configuration;
2929
_onnxModelSets = new ConcurrentDictionary<string, OnnxModelSet>();
30-
_onnxModelSetConfigs = _configuration.OnnxModelSets.ToConcurrentDictionary(x => x.Name, x => x);
30+
_onnxModelSetConfigs = _configuration.OnnxModelSets.ToConcurrentDictionary(x => x.Name, x => x as IOnnxModelSetConfig);
31+
}
32+
33+
34+
/// <summary>
35+
/// Updates the model set.
36+
/// </summary>
37+
/// <param name="modelSet">The model set.</param>
38+
/// <returns></returns>
39+
public bool UpdateModelSet(IOnnxModelSetConfig modelSet)
40+
{
41+
_onnxModelSetConfigs.TryRemove(modelSet.Name, out var existing);
42+
return _onnxModelSetConfigs.TryAdd(modelSet.Name, modelSet);
3143
}
3244

3345

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
using OnnxStack.Core.Config;
2+
using OnnxStack.StableDiffusion.Config;
3+
using OnnxStack.StableDiffusion.Enums;
4+
using System.Collections.Generic;
25
using System.Collections.Immutable;
36

47
namespace OnnxStack.StableDiffusion.Common
58
{
69
public interface IModelOptions : IOnnxModel
710
{
11+
bool IsEnabled { get; set; }
812
int PadTokenId { get; set; }
913
int BlankTokenId { get; set; }
1014
float ScaleFactor { get; set; }
1115
int TokenizerLimit { get; set; }
1216
int InputTokenLimit { get; set; }
1317
int EmbeddingsLength { get; set; }
18+
List<ProcessType> Diffusers { get; set; }
1419
ImmutableArray<int> BlankTokenValueArray { get; set; }
1520
}
1621
}

OnnxStack.StableDiffusion/Common/IStableDiffusionService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.ML.OnnxRuntime.Tensors;
2+
using OnnxStack.Core.Config;
23
using OnnxStack.Core.Model;
34
using OnnxStack.StableDiffusion.Config;
45
using SixLabors.ImageSharp;

0 commit comments

Comments
 (0)