Skip to content

Commit

Permalink
Updated stable-diffusion.cpp to ec82d52
Browse files Browse the repository at this point in the history
  • Loading branch information
DarthAffe committed Apr 14, 2024
1 parent 9c7daf6 commit 78a21e4
Show file tree
Hide file tree
Showing 13 changed files with 139 additions and 22 deletions.
4 changes: 3 additions & 1 deletion StableDiffusion.NET/Backends/Backends.cs
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 3 additions & 1 deletion StableDiffusion.NET/Backends/CpuBackend.cs
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 2 additions & 0 deletions StableDiffusion.NET/Backends/CudaBackend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
using System.Collections;
using System.Linq;
using System.Text.RegularExpressions;
using JetBrains.Annotations;

namespace StableDiffusion.NET;

[PublicAPI]
public partial class CudaBackend : IBackend
{
#region Constants
Expand Down
5 changes: 4 additions & 1 deletion StableDiffusion.NET/Backends/IBackend.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace StableDiffusion.NET;
using JetBrains.Annotations;

namespace StableDiffusion.NET;

[PublicAPI]
public interface IBackend
{
bool IsEnabled { get; set; }
Expand Down
2 changes: 2 additions & 0 deletions StableDiffusion.NET/Backends/RocmBackend.cs
Original file line number Diff line number Diff line change
@@ -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
Expand Down
5 changes: 4 additions & 1 deletion StableDiffusion.NET/ModelParameter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace StableDiffusion.NET;
using JetBrains.Annotations;

namespace StableDiffusion.NET;

[PublicAPI]
public class ModelParameter
{
#region Properties & Fields
Expand Down
7 changes: 6 additions & 1 deletion StableDiffusion.NET/Native/Native.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,12 @@ internal struct sd_image_t
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,
Expand Down
4 changes: 4 additions & 0 deletions StableDiffusion.NET/StableDiffusion.NET.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,8 @@
<Content Include="sd_net.png" Link="sd_net.png" Pack="true" PackagePath="\" />
<None Include="..\README.md" Pack="true" PackagePath="\" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2023.3.0" />
</ItemGroup>
</Project>
4 changes: 3 additions & 1 deletion StableDiffusion.NET/StableDiffusionImage.cs
Original file line number Diff line number Diff line change
@@ -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
Expand Down
110 changes: 97 additions & 13 deletions StableDiffusion.NET/StableDiffusionModel.cs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -206,20 +208,102 @@ private StableDiffusionImage ImageToImage(string prompt, Native.sd_image_t image
{
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);
}
Expand Down
7 changes: 6 additions & 1 deletion StableDiffusion.NET/StableDiffusionParameter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace StableDiffusion.NET;
using JetBrains.Annotations;

namespace StableDiffusion.NET;

[PublicAPI]
public sealed class StableDiffusionParameter
{
#region Properties & Fields
Expand All @@ -20,6 +23,7 @@ public sealed class StableDiffusionParameter
#endregion
}

[PublicAPI]
public sealed class StableDiffusionControlNetParameter
{
public bool IsEnabled => Image?.Length > 0;
Expand All @@ -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;
Expand Down
5 changes: 4 additions & 1 deletion StableDiffusion.NET/UpscalerModelParameter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace StableDiffusion.NET;
using JetBrains.Annotations;

namespace StableDiffusion.NET;

[PublicAPI]
public class UpscalerModelParameter
{
#region Properties & Fields
Expand Down
2 changes: 1 addition & 1 deletion build.bat
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 78a21e4

Please sign in to comment.