From 78a21e41ba010b7035381d7c6668e12fcc7e6a4b Mon Sep 17 00:00:00 2001 From: Darth Affe Date: Sun, 14 Apr 2024 11:48:41 +0200 Subject: [PATCH] Updated stable-diffusion.cpp to ec82d52 --- StableDiffusion.NET/Backends/Backends.cs | 4 +- StableDiffusion.NET/Backends/CpuBackend.cs | 4 +- StableDiffusion.NET/Backends/CudaBackend.cs | 2 + StableDiffusion.NET/Backends/IBackend.cs | 5 +- StableDiffusion.NET/Backends/RocmBackend.cs | 2 + StableDiffusion.NET/ModelParameter.cs | 5 +- StableDiffusion.NET/Native/Native.cs | 7 +- .../StableDiffusion.NET.csproj | 4 + StableDiffusion.NET/StableDiffusionImage.cs | 4 +- StableDiffusion.NET/StableDiffusionModel.cs | 110 +++++++++++++++--- .../StableDiffusionParameter.cs | 7 +- StableDiffusion.NET/UpscalerModelParameter.cs | 5 +- build.bat | 2 +- 13 files changed, 139 insertions(+), 22 deletions(-) diff --git a/StableDiffusion.NET/Backends/Backends.cs b/StableDiffusion.NET/Backends/Backends.cs index d345f04..150befb 100644 --- a/StableDiffusion.NET/Backends/Backends.cs +++ b/StableDiffusion.NET/Backends/Backends.cs @@ -1,10 +1,12 @@ -using System; +using JetBrains.Annotations; +using System; using System.Collections.Generic; using System.IO; using System.Linq; namespace StableDiffusion.NET; +[PublicAPI] public static class Backends { #region Properties & Fields diff --git a/StableDiffusion.NET/Backends/CpuBackend.cs b/StableDiffusion.NET/Backends/CpuBackend.cs index b3d05fa..ae8ed0e 100644 --- a/StableDiffusion.NET/Backends/CpuBackend.cs +++ b/StableDiffusion.NET/Backends/CpuBackend.cs @@ -1,10 +1,12 @@ -using System; +using JetBrains.Annotations; +using System; using System.Collections.Generic; using System.ComponentModel; using System.Runtime.InteropServices; namespace StableDiffusion.NET; +[PublicAPI] public class CpuBackend : IBackend { #region Properties & Fields diff --git a/StableDiffusion.NET/Backends/CudaBackend.cs b/StableDiffusion.NET/Backends/CudaBackend.cs index 68edde2..c0e97c9 100644 --- a/StableDiffusion.NET/Backends/CudaBackend.cs +++ b/StableDiffusion.NET/Backends/CudaBackend.cs @@ -5,9 +5,11 @@ using System; using System.Collections; using System.Linq; using System.Text.RegularExpressions; +using JetBrains.Annotations; namespace StableDiffusion.NET; +[PublicAPI] public partial class CudaBackend : IBackend { #region Constants diff --git a/StableDiffusion.NET/Backends/IBackend.cs b/StableDiffusion.NET/Backends/IBackend.cs index 75c43b2..e881a9c 100644 --- a/StableDiffusion.NET/Backends/IBackend.cs +++ b/StableDiffusion.NET/Backends/IBackend.cs @@ -1,5 +1,8 @@ -namespace StableDiffusion.NET; +using JetBrains.Annotations; +namespace StableDiffusion.NET; + +[PublicAPI] public interface IBackend { bool IsEnabled { get; set; } diff --git a/StableDiffusion.NET/Backends/RocmBackend.cs b/StableDiffusion.NET/Backends/RocmBackend.cs index 87a7730..64c83b0 100644 --- a/StableDiffusion.NET/Backends/RocmBackend.cs +++ b/StableDiffusion.NET/Backends/RocmBackend.cs @@ -1,10 +1,12 @@ using System; using System.Runtime.InteropServices; using System.Text.RegularExpressions; +using JetBrains.Annotations; using StableDiffusion.NET.Helper; namespace StableDiffusion.NET; +[PublicAPI] public partial class RocmBackend : IBackend { #region Properties & Fields diff --git a/StableDiffusion.NET/ModelParameter.cs b/StableDiffusion.NET/ModelParameter.cs index cfd013f..598c080 100644 --- a/StableDiffusion.NET/ModelParameter.cs +++ b/StableDiffusion.NET/ModelParameter.cs @@ -1,5 +1,8 @@ -namespace StableDiffusion.NET; +using JetBrains.Annotations; +namespace StableDiffusion.NET; + +[PublicAPI] public class ModelParameter { #region Properties & Fields diff --git a/StableDiffusion.NET/Native/Native.cs b/StableDiffusion.NET/Native/Native.cs index d95ecff..fff17d9 100644 --- a/StableDiffusion.NET/Native/Native.cs +++ b/StableDiffusion.NET/Native/Native.cs @@ -101,7 +101,12 @@ internal unsafe partial class Native int sample_steps, float strength, long seed, - int batch_count); + int batch_count, + sd_image_t* control_cond, + float control_strength, + float style_strength, + [MarshalAs(UnmanagedType.I1)] bool normalize_input, + [MarshalAs(UnmanagedType.LPStr)] string input_id_images_path); [LibraryImport(LIB_NAME, EntryPoint = "img2vid")] internal static partial sd_image_t* img2vid(sd_ctx_t* sd_ctx, diff --git a/StableDiffusion.NET/StableDiffusion.NET.csproj b/StableDiffusion.NET/StableDiffusion.NET.csproj index 8fc51ff..5e15481 100644 --- a/StableDiffusion.NET/StableDiffusion.NET.csproj +++ b/StableDiffusion.NET/StableDiffusion.NET.csproj @@ -53,4 +53,8 @@ + + + + diff --git a/StableDiffusion.NET/StableDiffusionImage.cs b/StableDiffusion.NET/StableDiffusionImage.cs index 70dc6b1..8aade19 100644 --- a/StableDiffusion.NET/StableDiffusionImage.cs +++ b/StableDiffusion.NET/StableDiffusionImage.cs @@ -1,8 +1,10 @@ -using System; +using JetBrains.Annotations; +using System; using System.Runtime.InteropServices; namespace StableDiffusion.NET; +[PublicAPI] public sealed unsafe class StableDiffusionImage : IDisposable { #region Properties & Fields diff --git a/StableDiffusion.NET/StableDiffusionModel.cs b/StableDiffusion.NET/StableDiffusionModel.cs index 2cc4e83..a0f0bdb 100644 --- a/StableDiffusion.NET/StableDiffusionModel.cs +++ b/StableDiffusion.NET/StableDiffusionModel.cs @@ -1,8 +1,10 @@ using System; using System.Runtime.InteropServices; +using JetBrains.Annotations; namespace StableDiffusion.NET; +[PublicAPI] public sealed unsafe class StableDiffusionModel : IDisposable { #region Properties & Fields @@ -206,20 +208,102 @@ public sealed unsafe class StableDiffusionModel : IDisposable { ObjectDisposedException.ThrowIf(_disposed, this); - Native.sd_image_t* result = Native.img2img(_ctx, - image, - prompt, - parameter.NegativePrompt, - parameter.ClipSkip, - parameter.CfgScale, - parameter.Width, - parameter.Height, - parameter.SampleMethod, - parameter.SampleSteps, - parameter.Strength, - parameter.Seed, - 1); + Native.sd_image_t* result; + if (parameter.ControlNet.IsEnabled) + { + fixed (byte* imagePtr = parameter.ControlNet.Image) + { + if (parameter.ControlNet.CannyPreprocess) + { + Native.sd_image_t controlNetImage = new() + { + width = (uint)parameter.Width, + height = (uint)parameter.Height, + channel = 3, + data = Native.preprocess_canny(imagePtr, + parameter.Width, + parameter.Height, + parameter.ControlNet.CannyHighThreshold, + parameter.ControlNet.CannyLowThreshold, + parameter.ControlNet.CannyWeak, + parameter.ControlNet.CannyStrong, + parameter.ControlNet.CannyInverse) + }; + + result = Native.img2img(_ctx, + image, + prompt, + parameter.NegativePrompt, + parameter.ClipSkip, + parameter.CfgScale, + parameter.Width, + parameter.Height, + parameter.SampleMethod, + parameter.SampleSteps, + parameter.Strength, + parameter.Seed, + 1, + &controlNetImage, + parameter.ControlNet.Strength, + parameter.PhotoMaker.StyleRatio, + parameter.PhotoMaker.NormalizeInput, + parameter.PhotoMaker.InputIdImageDirectory); + + Marshal.FreeHGlobal((nint)controlNetImage.data); + } + else + { + Native.sd_image_t controlNetImage = new() + { + width = (uint)parameter.Width, + height = (uint)parameter.Height, + channel = 3, + data = imagePtr + }; + + result = Native.img2img(_ctx, + image, + prompt, + parameter.NegativePrompt, + parameter.ClipSkip, + parameter.CfgScale, + parameter.Width, + parameter.Height, + parameter.SampleMethod, + parameter.SampleSteps, + parameter.Strength, + parameter.Seed, + 1, + &controlNetImage, + parameter.ControlNet.Strength, + parameter.PhotoMaker.StyleRatio, + parameter.PhotoMaker.NormalizeInput, + parameter.PhotoMaker.InputIdImageDirectory); + } + } + } + else + { + result = Native.img2img(_ctx, + image, + prompt, + parameter.NegativePrompt, + parameter.ClipSkip, + parameter.CfgScale, + parameter.Width, + parameter.Height, + parameter.SampleMethod, + parameter.SampleSteps, + parameter.Strength, + parameter.Seed, + 1, + null, + 0, + parameter.PhotoMaker.StyleRatio, + parameter.PhotoMaker.NormalizeInput, + parameter.PhotoMaker.InputIdImageDirectory); + } return new StableDiffusionImage(result); } diff --git a/StableDiffusion.NET/StableDiffusionParameter.cs b/StableDiffusion.NET/StableDiffusionParameter.cs index eeb1680..032b087 100644 --- a/StableDiffusion.NET/StableDiffusionParameter.cs +++ b/StableDiffusion.NET/StableDiffusionParameter.cs @@ -1,5 +1,8 @@ -namespace StableDiffusion.NET; +using JetBrains.Annotations; +namespace StableDiffusion.NET; + +[PublicAPI] public sealed class StableDiffusionParameter { #region Properties & Fields @@ -20,6 +23,7 @@ public sealed class StableDiffusionParameter #endregion } +[PublicAPI] public sealed class StableDiffusionControlNetParameter { public bool IsEnabled => Image?.Length > 0; @@ -34,6 +38,7 @@ public sealed class StableDiffusionControlNetParameter public bool CannyInverse { get; set; } = false; } +[PublicAPI] public sealed class PhotoMakerParameter { public string InputIdImageDirectory { get; set; } = string.Empty; diff --git a/StableDiffusion.NET/UpscalerModelParameter.cs b/StableDiffusion.NET/UpscalerModelParameter.cs index 577751d..264f694 100644 --- a/StableDiffusion.NET/UpscalerModelParameter.cs +++ b/StableDiffusion.NET/UpscalerModelParameter.cs @@ -1,5 +1,8 @@ -namespace StableDiffusion.NET; +using JetBrains.Annotations; +namespace StableDiffusion.NET; + +[PublicAPI] public class UpscalerModelParameter { #region Properties & Fields diff --git a/build.bat b/build.bat index c74984d..246bf67 100644 --- a/build.bat +++ b/build.bat @@ -4,7 +4,7 @@ if not exist stable-diffusion.cpp ( cd stable-diffusion.cpp git fetch -git checkout 48bcce493f45a11d9d5a4c69943d03ff919d748f +git checkout ec82d5279ab7d3b20d95bf1e803c78306030e6b1 git submodule init git submodule update