Skip to content

Commit 78a21e4

Browse files
committed
Updated stable-diffusion.cpp to ec82d52
1 parent 9c7daf6 commit 78a21e4

13 files changed

+139
-22
lines changed

StableDiffusion.NET/Backends/Backends.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
using System;
1+
using JetBrains.Annotations;
2+
using System;
23
using System.Collections.Generic;
34
using System.IO;
45
using System.Linq;
56

67
namespace StableDiffusion.NET;
78

9+
[PublicAPI]
810
public static class Backends
911
{
1012
#region Properties & Fields

StableDiffusion.NET/Backends/CpuBackend.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
using System;
1+
using JetBrains.Annotations;
2+
using System;
23
using System.Collections.Generic;
34
using System.ComponentModel;
45
using System.Runtime.InteropServices;
56

67
namespace StableDiffusion.NET;
78

9+
[PublicAPI]
810
public class CpuBackend : IBackend
911
{
1012
#region Properties & Fields

StableDiffusion.NET/Backends/CudaBackend.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
using System.Collections;
66
using System.Linq;
77
using System.Text.RegularExpressions;
8+
using JetBrains.Annotations;
89

910
namespace StableDiffusion.NET;
1011

12+
[PublicAPI]
1113
public partial class CudaBackend : IBackend
1214
{
1315
#region Constants

StableDiffusion.NET/Backends/IBackend.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
namespace StableDiffusion.NET;
1+
using JetBrains.Annotations;
22

3+
namespace StableDiffusion.NET;
4+
5+
[PublicAPI]
36
public interface IBackend
47
{
58
bool IsEnabled { get; set; }

StableDiffusion.NET/Backends/RocmBackend.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
using System;
22
using System.Runtime.InteropServices;
33
using System.Text.RegularExpressions;
4+
using JetBrains.Annotations;
45
using StableDiffusion.NET.Helper;
56

67
namespace StableDiffusion.NET;
78

9+
[PublicAPI]
810
public partial class RocmBackend : IBackend
911
{
1012
#region Properties & Fields

StableDiffusion.NET/ModelParameter.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
namespace StableDiffusion.NET;
1+
using JetBrains.Annotations;
22

3+
namespace StableDiffusion.NET;
4+
5+
[PublicAPI]
36
public class ModelParameter
47
{
58
#region Properties & Fields

StableDiffusion.NET/Native/Native.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,12 @@ internal struct sd_image_t
101101
int sample_steps,
102102
float strength,
103103
long seed,
104-
int batch_count);
104+
int batch_count,
105+
sd_image_t* control_cond,
106+
float control_strength,
107+
float style_strength,
108+
[MarshalAs(UnmanagedType.I1)] bool normalize_input,
109+
[MarshalAs(UnmanagedType.LPStr)] string input_id_images_path);
105110

106111
[LibraryImport(LIB_NAME, EntryPoint = "img2vid")]
107112
internal static partial sd_image_t* img2vid(sd_ctx_t* sd_ctx,

StableDiffusion.NET/StableDiffusion.NET.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,8 @@
5353
<Content Include="sd_net.png" Link="sd_net.png" Pack="true" PackagePath="\" />
5454
<None Include="..\README.md" Pack="true" PackagePath="\" />
5555
</ItemGroup>
56+
57+
<ItemGroup>
58+
<PackageReference Include="JetBrains.Annotations" Version="2023.3.0" />
59+
</ItemGroup>
5660
</Project>

StableDiffusion.NET/StableDiffusionImage.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
using System;
1+
using JetBrains.Annotations;
2+
using System;
23
using System.Runtime.InteropServices;
34

45
namespace StableDiffusion.NET;
56

7+
[PublicAPI]
68
public sealed unsafe class StableDiffusionImage : IDisposable
79
{
810
#region Properties & Fields

StableDiffusion.NET/StableDiffusionModel.cs

Lines changed: 97 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using System;
22
using System.Runtime.InteropServices;
3+
using JetBrains.Annotations;
34

45
namespace StableDiffusion.NET;
56

7+
[PublicAPI]
68
public sealed unsafe class StableDiffusionModel : IDisposable
79
{
810
#region Properties & Fields
@@ -206,20 +208,102 @@ private StableDiffusionImage ImageToImage(string prompt, Native.sd_image_t image
206208
{
207209
ObjectDisposedException.ThrowIf(_disposed, this);
208210

209-
Native.sd_image_t* result = Native.img2img(_ctx,
210-
image,
211-
prompt,
212-
parameter.NegativePrompt,
213-
parameter.ClipSkip,
214-
parameter.CfgScale,
215-
parameter.Width,
216-
parameter.Height,
217-
parameter.SampleMethod,
218-
parameter.SampleSteps,
219-
parameter.Strength,
220-
parameter.Seed,
221-
1);
211+
Native.sd_image_t* result;
212+
if (parameter.ControlNet.IsEnabled)
213+
{
214+
fixed (byte* imagePtr = parameter.ControlNet.Image)
215+
{
216+
217+
if (parameter.ControlNet.CannyPreprocess)
218+
{
219+
Native.sd_image_t controlNetImage = new()
220+
{
221+
width = (uint)parameter.Width,
222+
height = (uint)parameter.Height,
223+
channel = 3,
224+
data = Native.preprocess_canny(imagePtr,
225+
parameter.Width,
226+
parameter.Height,
227+
parameter.ControlNet.CannyHighThreshold,
228+
parameter.ControlNet.CannyLowThreshold,
229+
parameter.ControlNet.CannyWeak,
230+
parameter.ControlNet.CannyStrong,
231+
parameter.ControlNet.CannyInverse)
232+
};
233+
234+
result = Native.img2img(_ctx,
235+
image,
236+
prompt,
237+
parameter.NegativePrompt,
238+
parameter.ClipSkip,
239+
parameter.CfgScale,
240+
parameter.Width,
241+
parameter.Height,
242+
parameter.SampleMethod,
243+
parameter.SampleSteps,
244+
parameter.Strength,
245+
parameter.Seed,
246+
1,
247+
&controlNetImage,
248+
parameter.ControlNet.Strength,
249+
parameter.PhotoMaker.StyleRatio,
250+
parameter.PhotoMaker.NormalizeInput,
251+
parameter.PhotoMaker.InputIdImageDirectory);
252+
253+
Marshal.FreeHGlobal((nint)controlNetImage.data);
254+
}
255+
else
256+
{
257+
Native.sd_image_t controlNetImage = new()
258+
{
259+
width = (uint)parameter.Width,
260+
height = (uint)parameter.Height,
261+
channel = 3,
262+
data = imagePtr
263+
};
222264

265+
result = Native.img2img(_ctx,
266+
image,
267+
prompt,
268+
parameter.NegativePrompt,
269+
parameter.ClipSkip,
270+
parameter.CfgScale,
271+
parameter.Width,
272+
parameter.Height,
273+
parameter.SampleMethod,
274+
parameter.SampleSteps,
275+
parameter.Strength,
276+
parameter.Seed,
277+
1,
278+
&controlNetImage,
279+
parameter.ControlNet.Strength,
280+
parameter.PhotoMaker.StyleRatio,
281+
parameter.PhotoMaker.NormalizeInput,
282+
parameter.PhotoMaker.InputIdImageDirectory);
283+
}
284+
}
285+
}
286+
else
287+
{
288+
result = Native.img2img(_ctx,
289+
image,
290+
prompt,
291+
parameter.NegativePrompt,
292+
parameter.ClipSkip,
293+
parameter.CfgScale,
294+
parameter.Width,
295+
parameter.Height,
296+
parameter.SampleMethod,
297+
parameter.SampleSteps,
298+
parameter.Strength,
299+
parameter.Seed,
300+
1,
301+
null,
302+
0,
303+
parameter.PhotoMaker.StyleRatio,
304+
parameter.PhotoMaker.NormalizeInput,
305+
parameter.PhotoMaker.InputIdImageDirectory);
306+
}
223307

224308
return new StableDiffusionImage(result);
225309
}

StableDiffusion.NET/StableDiffusionParameter.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
namespace StableDiffusion.NET;
1+
using JetBrains.Annotations;
22

3+
namespace StableDiffusion.NET;
4+
5+
[PublicAPI]
36
public sealed class StableDiffusionParameter
47
{
58
#region Properties & Fields
@@ -20,6 +23,7 @@ public sealed class StableDiffusionParameter
2023
#endregion
2124
}
2225

26+
[PublicAPI]
2327
public sealed class StableDiffusionControlNetParameter
2428
{
2529
public bool IsEnabled => Image?.Length > 0;
@@ -34,6 +38,7 @@ public sealed class StableDiffusionControlNetParameter
3438
public bool CannyInverse { get; set; } = false;
3539
}
3640

41+
[PublicAPI]
3742
public sealed class PhotoMakerParameter
3843
{
3944
public string InputIdImageDirectory { get; set; } = string.Empty;

StableDiffusion.NET/UpscalerModelParameter.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
namespace StableDiffusion.NET;
1+
using JetBrains.Annotations;
22

3+
namespace StableDiffusion.NET;
4+
5+
[PublicAPI]
36
public class UpscalerModelParameter
47
{
58
#region Properties & Fields

build.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ if not exist stable-diffusion.cpp (
44

55
cd stable-diffusion.cpp
66
git fetch
7-
git checkout 48bcce493f45a11d9d5a4c69943d03ff919d748f
7+
git checkout ec82d5279ab7d3b20d95bf1e803c78306030e6b1
88
git submodule init
99
git submodule update
1010

0 commit comments

Comments
 (0)