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

Commit 3363bbd

Browse files
committed
Merge branch 'master' into Docs
# Conflicts: # README.md
2 parents 76974a1 + 43a42ef commit 3363bbd

File tree

139 files changed

+3993
-8119
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

139 files changed

+3993
-8119
lines changed

OnnxStack.Console/Examples/StableDebug.cs

Lines changed: 5 additions & 3 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.Models)
53+
foreach (var model in _configuration.ModelSets)
5254
{
5355
OutputHelpers.WriteConsole($"Loading Model `{model.Name}`...", ConsoleColor.Green);
5456
await _stableDiffusionService.LoadModelAsync(model);
@@ -71,7 +73,7 @@ public async Task RunAsync()
7173
}
7274

7375

74-
private async Task<bool> GenerateImage(ModelOptions model, PromptOptions prompt, SchedulerOptions options)
76+
private async Task<bool> GenerateImage(StableDiffusionModelSet model, PromptOptions prompt, SchedulerOptions options)
7577
{
7678
var timestamp = Stopwatch.GetTimestamp();
7779
var outputFilename = Path.Combine(_outputDirectory, $"{model.Name}_{options.Seed}_{options.SchedulerType}.png");

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.Models)
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: 5 additions & 3 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.Models)
52+
foreach (var model in _configuration.ModelSets)
5153
{
5254
OutputHelpers.WriteConsole($"Loading Model `{model.Name}`...", ConsoleColor.Green);
5355
await _stableDiffusionService.LoadModelAsync(model);
@@ -65,7 +67,7 @@ public async Task RunAsync()
6567
}
6668
}
6769

68-
private async Task<bool> GenerateImage(ModelOptions model, PromptOptions prompt, SchedulerOptions options)
70+
private async Task<bool> GenerateImage(StableDiffusionModelSet model, PromptOptions prompt, SchedulerOptions options)
6971
{
7072
var outputFilename = Path.Combine(_outputDirectory, $"{options.Seed}_{options.SchedulerType}.png");
7173
var result = await _stableDiffusionService.GenerateAsImageAsync(model, prompt, options);

OnnxStack.Console/Examples/StableDiffusionGenerator.cs

Lines changed: 5 additions & 3 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.Models)
36+
foreach (var model in _configuration.ModelSets)
3537
{
3638
OutputHelpers.WriteConsole($"Loading Model `{model.Name}`...", ConsoleColor.Green);
3739
await _stableDiffusionService.LoadModelAsync(model);
@@ -62,7 +64,7 @@ public async Task RunAsync()
6264
OutputHelpers.ReadConsole(ConsoleColor.Gray);
6365
}
6466

65-
private async Task<bool> GenerateImage(ModelOptions model, PromptOptions prompt, SchedulerOptions options, string key)
67+
private async Task<bool> GenerateImage(StableDiffusionModelSet model, PromptOptions prompt, SchedulerOptions options, string key)
6668
{
6769
var outputFilename = Path.Combine(_outputDirectory, $"{options.Seed}_{options.SchedulerType}_{key}.png");
6870
var result = await _stableDiffusionService.GenerateAsImageAsync(model, prompt, options);

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.Models.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.Console/OnnxStack.Console.csproj

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,25 @@
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>disable</Nullable>
88
<PlatformTarget>x64</PlatformTarget>
9+
<Configurations>Debug;Release;Debug-DirectML;Debug-Cuda;Debug-TensorRT;Release-DirectML;Release-Cuda;Release-TensorRT</Configurations>
910
</PropertyGroup>
1011

1112
<ItemGroup>
1213
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
1314
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
14-
<PackageReference Include="Microsoft.ML.OnnxRuntime.DirectML" Version="1.16.2" />
1515
</ItemGroup>
1616

1717
<ItemGroup>
18-
<ProjectReference Include="..\OnnxStack.ImageUpscaler\OnnxStack.ImageUpscaler.csproj" />
19-
<ProjectReference Include="..\OnnxStack.StableDiffusion\OnnxStack.StableDiffusion.csproj" />
18+
<PackageReference Include="OnnxStack.StableDiffusion" Version="0.12.0" Condition=" '$(Configuration)' == 'Release' OR '$(Configuration)' == 'Release-DirectML' OR '$(Configuration)' == 'Release-Cuda' OR '$(Configuration)' == 'Release-TensorRT'" />
19+
<ProjectReference Include="..\OnnxStack.StableDiffusion\OnnxStack.StableDiffusion.csproj" Condition=" '$(Configuration)' == 'Debug' OR '$(Configuration)' == 'Debug-DirectML' OR '$(Configuration)' == 'Debug-Cuda' OR '$(Configuration)' == 'Debug-TensorRT'" />
20+
<ProjectReference Include="..\OnnxStack.ImageUpscaler\OnnxStack.ImageUpscaler.csproj" Condition=" '$(Configuration)' == 'Debug' OR '$(Configuration)' == 'Debug-DirectML' OR '$(Configuration)' == 'Debug-Cuda' OR '$(Configuration)' == 'Debug-TensorRT'" />
21+
</ItemGroup>
22+
23+
<ItemGroup>
24+
<PackageReference Include="Microsoft.ML.OnnxRuntime" Version="1.16.3" Condition=" '$(Configuration)' == 'Debug' OR '$(Configuration)' == 'Release' " />
25+
<PackageReference Include="Microsoft.ML.OnnxRuntime.Gpu" Version="1.16.3" Condition=" '$(Configuration)' == 'Debug-TensorRT' OR '$(Configuration)' == 'Release-TensorRT'" />
26+
<PackageReference Include="Microsoft.ML.OnnxRuntime.Gpu" Version="1.16.3" Condition=" '$(Configuration)' == 'Debug-Cuda' OR '$(Configuration)' == 'Release-Cuda'" />
27+
<PackageReference Include="Microsoft.ML.OnnxRuntime.DirectML" Version="1.16.3" Condition=" '$(Configuration)' == 'Debug-DirectML' OR '$(Configuration)' == 'Release-DirectML'" />
2028
</ItemGroup>
2129

2230
<ItemGroup>

OnnxStack.Console/appsettings.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
}
77
},
88
"AllowedHosts": "*",
9-
"OnnxStackConfig": {
10-
"OnnxModelSets": [
9+
"StableDiffusionConfig": {
10+
"ModelSets": [
1111
{
1212
"Name": "StableDiffusion 1.5",
1313
"IsEnabled": true,

OnnxStack.Core/Config/ExecutionProvider.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ public enum ExecutionProvider
55
DirectML = 0,
66
Cuda = 1,
77
Cpu = 2,
8-
CoreML = 3
8+
CoreML = 3,
9+
OpenVino = 4,
10+
TensorRT = 5
911
}
1012
}

OnnxStack.Core/Config/IOnnxModelSetConfig.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ public interface IOnnxModelSetConfig : IOnnxModel
1111
int IntraOpNumThreads { get; set; }
1212
ExecutionMode ExecutionMode { get; set; }
1313
ExecutionProvider ExecutionProvider { get; set; }
14-
List<OnnxModelSessionConfig> ModelConfigurations { get; set; }
14+
List<OnnxModelConfig> ModelConfigurations { get; set; }
1515
}
1616
}

OnnxStack.Core/Config/OnnxModelSessionConfig.cs renamed to OnnxStack.Core/Config/OnnxModelConfig.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace OnnxStack.Core.Config
55
{
6-
public class OnnxModelSessionConfig
6+
public class OnnxModelConfig
77
{
88
public OnnxModelType Type { get; set; }
99
public string OnnxModelPath { get; set; }
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System.Collections.Generic;
2+
3+
namespace OnnxStack.Core.Config
4+
{
5+
public class OnnxModelEqualityComparer : IEqualityComparer<IOnnxModel>
6+
{
7+
public bool Equals(IOnnxModel x, IOnnxModel y)
8+
{
9+
return x != null && y != null && x.Name == y.Name;
10+
}
11+
12+
public int GetHashCode(IOnnxModel obj)
13+
{
14+
return obj?.Name?.GetHashCode() ?? 0;
15+
}
16+
}
17+
}

OnnxStack.Core/Config/OnnxModelSetConfig.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ public class OnnxModelSetConfig : IOnnxModelSetConfig
1313
public int IntraOpNumThreads { get; set; }
1414
public ExecutionMode ExecutionMode { get; set; }
1515
public ExecutionProvider ExecutionProvider { get; set; }
16-
public List<OnnxModelSessionConfig> ModelConfigurations { get; set; }
16+
public List<OnnxModelConfig> ModelConfigurations { get; set; }
1717
}
1818
}

OnnxStack.Core/Config/OnnxModelType.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public enum OnnxModelType
88
TextEncoder = 20,
99
TextEncoder2 = 21,
1010
VaeEncoder = 30,
11-
VaeDecoder = 40
11+
VaeDecoder = 40,
12+
Upscaler = 1000
1213
}
1314
}
Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,11 @@
11
using OnnxStack.Common.Config;
2-
using System.Collections.Generic;
3-
using System.Linq;
42

53
namespace OnnxStack.Core.Config
64
{
75
public class OnnxStackConfig : IConfigSection
86
{
9-
public List<OnnxModelSetConfig> OnnxModelSets { get; set; } = new List<OnnxModelSetConfig>();
10-
117
public void Initialize()
128
{
13-
if (OnnxModelSets.IsNullOrEmpty())
14-
return;
15-
16-
foreach (var modelSet in OnnxModelSets)
17-
{
18-
modelSet.ApplyConfigurationOverrides();
19-
}
209
}
2110
}
2211
}

OnnxStack.Core/Extensions/Extensions.cs

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace OnnxStack.Core
1010
{
1111
public static class Extensions
1212
{
13-
public static SessionOptions GetSessionOptions(this OnnxModelSessionConfig configuration)
13+
public static SessionOptions GetSessionOptions(this OnnxModelConfig configuration)
1414
{
1515
var sessionOptions = new SessionOptions
1616
{
@@ -36,30 +36,27 @@ public static SessionOptions GetSessionOptions(this OnnxModelSessionConfig confi
3636
sessionOptions.AppendExecutionProvider_CPU();
3737
return sessionOptions;
3838
case ExecutionProvider.CoreML:
39-
sessionOptions.AppendExecutionProvider_CoreML(
40-
CoreMLFlags.COREML_FLAG_ONLY_ENABLE_DEVICE_WITH_ANE
41-
);
39+
sessionOptions.AppendExecutionProvider_CoreML(CoreMLFlags.COREML_FLAG_ONLY_ENABLE_DEVICE_WITH_ANE);
40+
return sessionOptions;
41+
case ExecutionProvider.OpenVino:
42+
var deviceId = configuration.DeviceId switch
43+
{
44+
0 => "CPU_FP32",
45+
1 => "GPU_FP32",
46+
2 => "GPU_FP16",
47+
3 => "MYRIAD_FP16",
48+
4 => "VAD-M_FP16",
49+
5 => "VAD-F_FP32",
50+
_ => string.Empty
51+
};
52+
sessionOptions.AppendExecutionProvider_OpenVINO(deviceId);
53+
return sessionOptions;
54+
case ExecutionProvider.TensorRT:
55+
sessionOptions.AppendExecutionProvider_Tensorrt(configuration.DeviceId.Value);
4256
return sessionOptions;
4357
}
4458
}
4559

46-
/// <summary>
47-
/// Applies the configuration overrides.
48-
/// </summary>
49-
public static void ApplyConfigurationOverrides(this IOnnxModelSetConfig innxModelSetConfig)
50-
{
51-
if (innxModelSetConfig.ModelConfigurations.IsNullOrEmpty())
52-
return;
53-
54-
foreach (var modelConfig in innxModelSetConfig.ModelConfigurations)
55-
{
56-
modelConfig.DeviceId ??= innxModelSetConfig.DeviceId;
57-
modelConfig.ExecutionMode ??= innxModelSetConfig.ExecutionMode;
58-
modelConfig.InterOpNumThreads ??= innxModelSetConfig.InterOpNumThreads;
59-
modelConfig.IntraOpNumThreads ??= innxModelSetConfig.IntraOpNumThreads;
60-
modelConfig.ExecutionProvider ??= innxModelSetConfig.ExecutionProvider;
61-
}
62-
}
6360

6461
/// <summary>
6562
/// Determines whether the the source sequence is null or empty

0 commit comments

Comments
 (0)