Updated stable-diffusion.cpp to ec82d52

This commit is contained in:
Darth Affe 2024-04-14 11:48:41 +02:00
parent 9c7daf6e53
commit 78a21e41ba
13 changed files with 139 additions and 22 deletions

View File

@ -1,10 +1,12 @@
using System; using JetBrains.Annotations;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
namespace StableDiffusion.NET; namespace StableDiffusion.NET;
[PublicAPI]
public static class Backends public static class Backends
{ {
#region Properties & Fields #region Properties & Fields

View File

@ -1,10 +1,12 @@
using System; using JetBrains.Annotations;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
namespace StableDiffusion.NET; namespace StableDiffusion.NET;
[PublicAPI]
public class CpuBackend : IBackend public class CpuBackend : IBackend
{ {
#region Properties & Fields #region Properties & Fields

View File

@ -5,9 +5,11 @@ using System;
using System.Collections; using System.Collections;
using System.Linq; using System.Linq;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using JetBrains.Annotations;
namespace StableDiffusion.NET; namespace StableDiffusion.NET;
[PublicAPI]
public partial class CudaBackend : IBackend public partial class CudaBackend : IBackend
{ {
#region Constants #region Constants

View File

@ -1,5 +1,8 @@
namespace StableDiffusion.NET; using JetBrains.Annotations;
namespace StableDiffusion.NET;
[PublicAPI]
public interface IBackend public interface IBackend
{ {
bool IsEnabled { get; set; } bool IsEnabled { get; set; }

View File

@ -1,10 +1,12 @@
using System; using System;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using JetBrains.Annotations;
using StableDiffusion.NET.Helper; using StableDiffusion.NET.Helper;
namespace StableDiffusion.NET; namespace StableDiffusion.NET;
[PublicAPI]
public partial class RocmBackend : IBackend public partial class RocmBackend : IBackend
{ {
#region Properties & Fields #region Properties & Fields

View File

@ -1,5 +1,8 @@
namespace StableDiffusion.NET; using JetBrains.Annotations;
namespace StableDiffusion.NET;
[PublicAPI]
public class ModelParameter public class ModelParameter
{ {
#region Properties & Fields #region Properties & Fields

View File

@ -101,7 +101,12 @@ internal unsafe partial class Native
int sample_steps, int sample_steps,
float strength, float strength,
long seed, 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")] [LibraryImport(LIB_NAME, EntryPoint = "img2vid")]
internal static partial sd_image_t* img2vid(sd_ctx_t* sd_ctx, internal static partial sd_image_t* img2vid(sd_ctx_t* sd_ctx,

View File

@ -53,4 +53,8 @@
<Content Include="sd_net.png" Link="sd_net.png" Pack="true" PackagePath="\" /> <Content Include="sd_net.png" Link="sd_net.png" Pack="true" PackagePath="\" />
<None Include="..\README.md" Pack="true" PackagePath="\" /> <None Include="..\README.md" Pack="true" PackagePath="\" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2023.3.0" />
</ItemGroup>
</Project> </Project>

View File

@ -1,8 +1,10 @@
using System; using JetBrains.Annotations;
using System;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
namespace StableDiffusion.NET; namespace StableDiffusion.NET;
[PublicAPI]
public sealed unsafe class StableDiffusionImage : IDisposable public sealed unsafe class StableDiffusionImage : IDisposable
{ {
#region Properties & Fields #region Properties & Fields

View File

@ -1,8 +1,10 @@
using System; using System;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using JetBrains.Annotations;
namespace StableDiffusion.NET; namespace StableDiffusion.NET;
[PublicAPI]
public sealed unsafe class StableDiffusionModel : IDisposable public sealed unsafe class StableDiffusionModel : IDisposable
{ {
#region Properties & Fields #region Properties & Fields
@ -206,20 +208,102 @@ public sealed unsafe class StableDiffusionModel : IDisposable
{ {
ObjectDisposedException.ThrowIf(_disposed, this); ObjectDisposedException.ThrowIf(_disposed, this);
Native.sd_image_t* result = Native.img2img(_ctx, Native.sd_image_t* result;
image, if (parameter.ControlNet.IsEnabled)
prompt, {
parameter.NegativePrompt, fixed (byte* imagePtr = parameter.ControlNet.Image)
parameter.ClipSkip, {
parameter.CfgScale,
parameter.Width,
parameter.Height,
parameter.SampleMethod,
parameter.SampleSteps,
parameter.Strength,
parameter.Seed,
1);
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); return new StableDiffusionImage(result);
} }

View File

@ -1,5 +1,8 @@
namespace StableDiffusion.NET; using JetBrains.Annotations;
namespace StableDiffusion.NET;
[PublicAPI]
public sealed class StableDiffusionParameter public sealed class StableDiffusionParameter
{ {
#region Properties & Fields #region Properties & Fields
@ -20,6 +23,7 @@ public sealed class StableDiffusionParameter
#endregion #endregion
} }
[PublicAPI]
public sealed class StableDiffusionControlNetParameter public sealed class StableDiffusionControlNetParameter
{ {
public bool IsEnabled => Image?.Length > 0; public bool IsEnabled => Image?.Length > 0;
@ -34,6 +38,7 @@ public sealed class StableDiffusionControlNetParameter
public bool CannyInverse { get; set; } = false; public bool CannyInverse { get; set; } = false;
} }
[PublicAPI]
public sealed class PhotoMakerParameter public sealed class PhotoMakerParameter
{ {
public string InputIdImageDirectory { get; set; } = string.Empty; public string InputIdImageDirectory { get; set; } = string.Empty;

View File

@ -1,5 +1,8 @@
namespace StableDiffusion.NET; using JetBrains.Annotations;
namespace StableDiffusion.NET;
[PublicAPI]
public class UpscalerModelParameter public class UpscalerModelParameter
{ {
#region Properties & Fields #region Properties & Fields

View File

@ -4,7 +4,7 @@ if not exist stable-diffusion.cpp (
cd stable-diffusion.cpp cd stable-diffusion.cpp
git fetch git fetch
git checkout 48bcce493f45a11d9d5a4c69943d03ff919d748f git checkout ec82d5279ab7d3b20d95bf1e803c78306030e6b1
git submodule init git submodule init
git submodule update git submodule update