Skip to content

Commit f324960

Browse files
authored
Merge pull request #68 from DarthAffe/sd.cpp_update
Updated sd.cpp to 43a70e8
2 parents 98784a0 + d77cebe commit f324960

16 files changed

Lines changed: 378 additions & 123 deletions

Header/stable-diffusion.h

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,11 @@ typedef struct {
150150
float rel_size_y;
151151
} sd_tiling_params_t;
152152

153+
typedef struct {
154+
const char* name;
155+
const char* path;
156+
} sd_embedding_t;
157+
153158
typedef struct {
154159
const char* model_path;
155160
const char* clip_l_path;
@@ -164,7 +169,8 @@ typedef struct {
164169
const char* taesd_path;
165170
const char* control_net_path;
166171
const char* lora_model_dir;
167-
const char* embedding_dir;
172+
const sd_embedding_t* embeddings;
173+
uint32_t embedding_count;
168174
const char* photo_maker_path;
169175
const char* tensor_type_rules;
170176
bool vae_decode_only;
@@ -219,6 +225,8 @@ typedef struct {
219225
int sample_steps;
220226
float eta;
221227
int shifted_timestep;
228+
float* custom_sigmas;
229+
int custom_sigmas_count;
222230
} sd_sample_params_t;
223231

224232
typedef struct {
@@ -236,6 +244,14 @@ typedef struct {
236244
} sd_easycache_params_t;
237245

238246
typedef struct {
247+
bool is_high_noise;
248+
float multiplier;
249+
const char* path;
250+
} sd_lora_t;
251+
252+
typedef struct {
253+
const sd_lora_t* loras;
254+
uint32_t lora_count;
239255
const char* prompt;
240256
const char* negative_prompt;
241257
int clip_skip;
@@ -259,6 +275,8 @@ typedef struct {
259275
} sd_img_gen_params_t;
260276

261277
typedef struct {
278+
const sd_lora_t* loras;
279+
uint32_t lora_count;
262280
const char* prompt;
263281
const char* negative_prompt;
264282
int clip_skip;
@@ -331,7 +349,8 @@ typedef struct upscaler_ctx_t upscaler_ctx_t;
331349
SD_API upscaler_ctx_t* new_upscaler_ctx(const char* esrgan_path,
332350
bool offload_params_to_cpu,
333351
bool direct,
334-
int n_threads);
352+
int n_threads,
353+
int tile_size);
335354
SD_API void free_upscaler_ctx(upscaler_ctx_t* upscaler_ctx);
336355

337356
SD_API sd_image_t upscale(upscaler_ctx_t* upscaler_ctx,
@@ -353,8 +372,11 @@ SD_API bool preprocess_canny(sd_image_t image,
353372
float strong,
354373
bool inverse);
355374

375+
SD_API const char* sd_commit(void);
376+
SD_API const char* sd_version(void);
377+
356378
#ifdef __cplusplus
357379
}
358380
#endif
359381

360-
#endif // __STABLE_DIFFUSION_H__
382+
#endif // __STABLE_DIFFUSION_H__

StableDiffusion.NET/Models/Parameter/DiffusionModelParameter.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using JetBrains.Annotations;
34

45
namespace StableDiffusion.NET;
@@ -24,8 +25,11 @@ public sealed class DiffusionModelParameter
2425
/// <summary>
2526
/// path to embeddings
2627
/// </summary>
28+
[Obsolete("Use Embeddings instead")]
2729
public string EmbeddingsDirectory { get; set; } = string.Empty;
2830

31+
public List<Embedding> Embeddings { get; } = [];
32+
2933
/// <summary>
3034
/// path to control net model
3135
/// </summary>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System.Diagnostics.CodeAnalysis;
2+
3+
namespace StableDiffusion.NET;
4+
5+
public sealed class Embedding
6+
{
7+
public required string Name { get; init; }
8+
public required string Path { get; init; }
9+
10+
public Embedding() { }
11+
12+
[SetsRequiredMembers]
13+
public Embedding(string name, string path)
14+
{
15+
this.Name = name;
16+
this.Path = path;
17+
}
18+
}

StableDiffusion.NET/Models/Parameter/Extensions/DiffusionModelBuilderExtension.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public static DiffusionModelParameter WithLoraSupport(this DiffusionModelParamet
3737
return parameter;
3838
}
3939

40+
[Obsolete("Use WithEmbedding instead")]
4041
public static DiffusionModelParameter WithEmbeddingSupport(this DiffusionModelParameter parameter, string embeddingsDirectory)
4142
{
4243
ArgumentNullException.ThrowIfNull(embeddingsDirectory);
@@ -46,6 +47,15 @@ public static DiffusionModelParameter WithEmbeddingSupport(this DiffusionModelPa
4647
return parameter;
4748
}
4849

50+
public static DiffusionModelParameter WithEmbedding(this DiffusionModelParameter parameter, Embedding embedding)
51+
{
52+
ArgumentNullException.ThrowIfNull(embedding);
53+
54+
parameter.Embeddings.Add(embedding);
55+
56+
return parameter;
57+
}
58+
4959
public static DiffusionModelParameter WithControlNet(this DiffusionModelParameter parameter, string controlNetPath)
5060
{
5161
ArgumentNullException.ThrowIfNull(controlNetPath);

StableDiffusion.NET/Models/Parameter/ImageGenerationParameter.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using HPPH;
1+
using System.Collections.Generic;
2+
using HPPH;
23
using JetBrains.Annotations;
34

45
namespace StableDiffusion.NET;
@@ -61,6 +62,8 @@ public sealed class ImageGenerationParameter
6162

6263
public EasyCache EasyCache { get; } = new();
6364

65+
public List<Lora> Loras { get; } = [];
66+
6467
#endregion
6568

6669
public static ImageGenerationParameter Create() => new();
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System.Diagnostics.CodeAnalysis;
2+
3+
namespace StableDiffusion.NET;
4+
5+
public sealed class Lora
6+
{
7+
public bool IsHighNoise { get; set; } = false;
8+
public float Multiplier { get; set; } = 1f;
9+
public required string Path { get; init; }
10+
11+
public Lora() { }
12+
13+
[SetsRequiredMembers]
14+
public Lora(string path)
15+
{
16+
this.Path = path;
17+
}
18+
}

StableDiffusion.NET/Models/Parameter/SampleParameter.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,7 @@ public sealed class SampleParameter
2626

2727
public int ShiftedTimestep { get; set; } = 0;
2828

29+
public float[] CustomSigmas { get; set; } = [];
30+
2931
internal SampleParameter() { }
3032
}

StableDiffusion.NET/Models/Parameter/UpscaleModelParameter.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,7 @@ public sealed class UpscaleModelParameter
2424
/// </summary>
2525
public bool ConvDirect { get; set; } = false;
2626

27+
public int TileSize { get; set; } = 128;
28+
2729
public static UpscaleModelParameter Create() => new();
2830
}

StableDiffusion.NET/Models/Parameter/VideoGenerationParameter.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using HPPH;
22
using JetBrains.Annotations;
3+
using System.Collections.Generic;
34

45
namespace StableDiffusion.NET;
56

@@ -40,6 +41,8 @@ public sealed class VideoGenerationParameter
4041

4142
public EasyCache EasyCache { get; } = new();
4243

44+
public List<Lora> Loras { get; } = [];
45+
4346
#endregion
4447

4548
public static VideoGenerationParameter Create() => new();

StableDiffusion.NET/Models/UpscaleModel.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ private void Initialize()
4141
_ctx = Native.new_upscaler_ctx(ModelParameter.ModelPath,
4242
ModelParameter.OffloadParamsToCPU,
4343
ModelParameter.ConvDirect,
44-
ModelParameter.ThreadCount);
44+
ModelParameter.ThreadCount,
45+
ModelParameter.TileSize);
4546

4647
if (_ctx == null) throw new NullReferenceException("Failed to initialize upscale-model.");
4748
}

0 commit comments

Comments
 (0)