From 3b0d7e9cf511189300cf56d872ba276eac57794e Mon Sep 17 00:00:00 2001 From: Darth Affe Date: Thu, 24 Oct 2024 21:47:02 +0200 Subject: [PATCH 1/2] Updated for new SD3.5 support --- .../Models/Builder/ModelBuilder.cs | 1 + .../Builder/StableDiffusion3_5ModelBuilder.cs | 37 +++++++++++++++++++ StableDiffusion.NET/Models/DiffusionModel.cs | 1 + .../Parameter/DiffusionModelParameter.cs | 8 +++- .../Models/Parameter/DiffusionParameter.cs | 1 + StableDiffusion.NET/Native/Native.cs | 1 + 6 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 StableDiffusion.NET/Models/Builder/StableDiffusion3_5ModelBuilder.cs diff --git a/StableDiffusion.NET/Models/Builder/ModelBuilder.cs b/StableDiffusion.NET/Models/Builder/ModelBuilder.cs index d7189bf..3d91c0b 100644 --- a/StableDiffusion.NET/Models/Builder/ModelBuilder.cs +++ b/StableDiffusion.NET/Models/Builder/ModelBuilder.cs @@ -6,6 +6,7 @@ namespace StableDiffusion.NET; public static class ModelBuilder { public static StableDiffusionModelBuilder StableDiffusion(string modelPath) => new(modelPath); + public static StableDiffusion3_5ModelBuilder StableDiffusion3_5(string modelPath, string clipLPath, string clipGPath, string t5xxlPath) => new(modelPath, clipLPath, clipGPath, t5xxlPath); public static FluxModelBuilder Flux(string diffusionModelPath, string clipLPath, string t5xxlPath, string vaePath) => new(diffusionModelPath, clipLPath, t5xxlPath, vaePath); public static ESRGANModelBuilder ESRGAN(string modelPath) => new(modelPath); } \ No newline at end of file diff --git a/StableDiffusion.NET/Models/Builder/StableDiffusion3_5ModelBuilder.cs b/StableDiffusion.NET/Models/Builder/StableDiffusion3_5ModelBuilder.cs new file mode 100644 index 0000000..66ac5b1 --- /dev/null +++ b/StableDiffusion.NET/Models/Builder/StableDiffusion3_5ModelBuilder.cs @@ -0,0 +1,37 @@ +using JetBrains.Annotations; + +namespace StableDiffusion.NET; + +[PublicAPI] +public sealed class StableDiffusion3_5ModelBuilder : IDiffusionModelBuilder, IQuantizedModelBuilder +{ + #region Properties & Fields + + public DiffusionModelParameter Parameter { get; } + IDiffusionModelParameter IDiffusionModelBuilder.Parameter => Parameter; + IQuantizedModelParameter IQuantizedModelBuilder.Parameter => Parameter; + + #endregion + + #region Constructors + + public StableDiffusion3_5ModelBuilder(string modelPath, string clipLPath, string clipGPath, string t5xxlPath) + { + Parameter = new DiffusionModelParameter + { + DiffusionModelType = DiffusionModelType.StableDiffusion, + ModelPath = modelPath, + ClipLPath = clipLPath, + ClipGPath = clipGPath, + T5xxlPath = t5xxlPath, + }; + } + + #endregion + + #region Methods + + public DiffusionModel Build() => new(Parameter); + + #endregion +} \ No newline at end of file diff --git a/StableDiffusion.NET/Models/DiffusionModel.cs b/StableDiffusion.NET/Models/DiffusionModel.cs index 1f90b70..cbb2e7d 100644 --- a/StableDiffusion.NET/Models/DiffusionModel.cs +++ b/StableDiffusion.NET/Models/DiffusionModel.cs @@ -41,6 +41,7 @@ private void Initialize() { _ctx = Native.new_sd_ctx(ModelParameter.ModelPath, ModelParameter.ClipLPath, + ModelParameter.ClipGPath, ModelParameter.T5xxlPath, ModelParameter.DiffusionModelPath, ModelParameter.VaePath, diff --git a/StableDiffusion.NET/Models/Parameter/DiffusionModelParameter.cs b/StableDiffusion.NET/Models/Parameter/DiffusionModelParameter.cs index 379750a..8a54a79 100644 --- a/StableDiffusion.NET/Models/Parameter/DiffusionModelParameter.cs +++ b/StableDiffusion.NET/Models/Parameter/DiffusionModelParameter.cs @@ -24,12 +24,16 @@ public sealed class DiffusionModelParameter : IDiffusionModelParameter, IQuantiz public Quantization Quantization { get; set; } = Quantization.Unspecified; - // Stable Diffusion only + // SD <= 3 only public string ModelPath { get; set; } = string.Empty; public string StackedIdEmbeddingsDirectory { get; set; } = string.Empty; - // Flux only + // Flux & SD3.5 only public string DiffusionModelPath { get; set; } = string.Empty; public string ClipLPath { get; set; } = string.Empty; public string T5xxlPath { get; set; } = string.Empty; + + + // SD3.5 only + public string ClipGPath { get; set; } = string.Empty; } \ No newline at end of file diff --git a/StableDiffusion.NET/Models/Parameter/DiffusionParameter.cs b/StableDiffusion.NET/Models/Parameter/DiffusionParameter.cs index 3210254..c6809e5 100644 --- a/StableDiffusion.NET/Models/Parameter/DiffusionParameter.cs +++ b/StableDiffusion.NET/Models/Parameter/DiffusionParameter.cs @@ -9,6 +9,7 @@ public sealed class DiffusionParameter public static DiffusionParameter SD1Default => new() { Width = 512, Height = 512, CfgScale = 7.5f, Guidance = 1f, SampleSteps = 25, SampleMethod = Sampler.Euler_A }; public static DiffusionParameter SDXLDefault => new() { Width = 1024, Height = 1024, CfgScale = 7f, Guidance = 1f, SampleSteps = 30, SampleMethod = Sampler.Euler_A }; + public static DiffusionParameter SD3_5Default => new() { Width = 1024, Height = 1024, CfgScale = 4.5f, Guidance = 1f, SampleSteps = 20, SampleMethod = Sampler.Euler }; public static DiffusionParameter FluxDefault => new() { Width = 1024, Height = 1024, CfgScale = 1, Guidance = 3.5f, SampleSteps = 20, SampleMethod = Sampler.Euler }; public string NegativePrompt { get; set; } = string.Empty; diff --git a/StableDiffusion.NET/Native/Native.cs b/StableDiffusion.NET/Native/Native.cs index 8d27155..66f55ab 100644 --- a/StableDiffusion.NET/Native/Native.cs +++ b/StableDiffusion.NET/Native/Native.cs @@ -51,6 +51,7 @@ internal struct sd_image_t [LibraryImport(LIB_NAME, EntryPoint = "new_sd_ctx")] internal static partial sd_ctx_t* new_sd_ctx([MarshalAs(UnmanagedType.LPStr)] string model_path, [MarshalAs(UnmanagedType.LPStr)] string clip_l_path, + [MarshalAs(UnmanagedType.LPStr)] string clip_g_path, [MarshalAs(UnmanagedType.LPStr)] string t5xxl_path, [MarshalAs(UnmanagedType.LPStr)] string diffusion_model_path, [MarshalAs(UnmanagedType.LPStr)] string vae_path, From 5ed63e52be038471b9184cebc0944d728e282187 Mon Sep 17 00:00:00 2001 From: Darth Affe Date: Thu, 24 Oct 2024 21:47:26 +0200 Subject: [PATCH 2/2] Updated example to 3.2 backends --- Examples/ImageCreationUI/ImageCreationUI.csproj | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Examples/ImageCreationUI/ImageCreationUI.csproj b/Examples/ImageCreationUI/ImageCreationUI.csproj index ab86463..1a68fce 100644 --- a/Examples/ImageCreationUI/ImageCreationUI.csproj +++ b/Examples/ImageCreationUI/ImageCreationUI.csproj @@ -11,9 +11,9 @@ - - - + + +