Skip to content

Commit 9fd65e1

Browse files
authored
Merge pull request #70 from DarthAffe/v5
Updated sd.cpp to b87fe13; removed deprecated APIs; changed defaults to be closer to sd.cpp
2 parents f324960 + 0c0ddb2 commit 9fd65e1

25 files changed

+337
-227
lines changed

Header/stable-diffusion.h

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ enum scheduler_t {
6060
SGM_UNIFORM_SCHEDULER,
6161
SIMPLE_SCHEDULER,
6262
SMOOTHSTEP_SCHEDULER,
63+
KL_OPTIMAL_SCHEDULER,
6364
LCM_SCHEDULER,
6465
SCHEDULER_COUNT
6566
};
@@ -168,7 +169,6 @@ typedef struct {
168169
const char* vae_path;
169170
const char* taesd_path;
170171
const char* control_net_path;
171-
const char* lora_model_dir;
172172
const sd_embedding_t* embeddings;
173173
uint32_t embedding_count;
174174
const char* photo_maker_path;
@@ -182,17 +182,21 @@ typedef struct {
182182
enum prediction_t prediction;
183183
enum lora_apply_mode_t lora_apply_mode;
184184
bool offload_params_to_cpu;
185+
bool enable_mmap;
185186
bool keep_clip_on_cpu;
186187
bool keep_control_net_on_cpu;
187188
bool keep_vae_on_cpu;
188189
bool diffusion_flash_attn;
189190
bool tae_preview_only;
190191
bool diffusion_conv_direct;
191192
bool vae_conv_direct;
193+
bool circular_x;
194+
bool circular_y;
192195
bool force_sdxl_vae_conv_scale;
193196
bool chroma_use_dit_mask;
194197
bool chroma_use_t5_mask;
195198
int chroma_t5_mask_pad;
199+
bool qwen_image_zero_cond_t;
196200
float flow_shift;
197201
} sd_ctx_params_t;
198202

@@ -236,12 +240,34 @@ typedef struct {
236240
float style_strength;
237241
} sd_pm_params_t; // photo maker
238242

243+
enum sd_cache_mode_t {
244+
SD_CACHE_DISABLED = 0,
245+
SD_CACHE_EASYCACHE,
246+
SD_CACHE_UCACHE,
247+
SD_CACHE_DBCACHE,
248+
SD_CACHE_TAYLORSEER,
249+
SD_CACHE_CACHE_DIT,
250+
};
251+
239252
typedef struct {
240-
bool enabled;
253+
enum sd_cache_mode_t mode;
241254
float reuse_threshold;
242255
float start_percent;
243256
float end_percent;
244-
} sd_easycache_params_t;
257+
float error_decay_rate;
258+
bool use_relative_threshold;
259+
bool reset_error_on_compute;
260+
int Fn_compute_blocks;
261+
int Bn_compute_blocks;
262+
float residual_diff_threshold;
263+
int max_warmup_steps;
264+
int max_cached_steps;
265+
int max_continuous_cached_steps;
266+
int taylorseer_n_derivatives;
267+
int taylorseer_skip_interval;
268+
const char* scm_mask;
269+
bool scm_policy_dynamic;
270+
} sd_cache_params_t;
245271

246272
typedef struct {
247273
bool is_high_noise;
@@ -271,7 +297,7 @@ typedef struct {
271297
float control_strength;
272298
sd_pm_params_t pm_params;
273299
sd_tiling_params_t vae_tiling_params;
274-
sd_easycache_params_t easycache;
300+
sd_cache_params_t cache;
275301
} sd_img_gen_params_t;
276302

277303
typedef struct {
@@ -293,7 +319,8 @@ typedef struct {
293319
int64_t seed;
294320
int video_frames;
295321
float vace_strength;
296-
sd_easycache_params_t easycache;
322+
sd_tiling_params_t vae_tiling_params;
323+
sd_cache_params_t cache;
297324
} sd_vid_gen_params_t;
298325

299326
typedef struct sd_ctx_t sd_ctx_t;
@@ -323,7 +350,7 @@ SD_API enum preview_t str_to_preview(const char* str);
323350
SD_API const char* sd_lora_apply_mode_name(enum lora_apply_mode_t mode);
324351
SD_API enum lora_apply_mode_t str_to_lora_apply_mode(const char* str);
325352

326-
SD_API void sd_easycache_params_init(sd_easycache_params_t* easycache_params);
353+
SD_API void sd_cache_params_init(sd_cache_params_t* cache_params);
327354

328355
SD_API void sd_ctx_params_init(sd_ctx_params_t* sd_ctx_params);
329356
SD_API char* sd_ctx_params_to_str(const sd_ctx_params_t* sd_ctx_params);
@@ -335,7 +362,7 @@ SD_API void sd_sample_params_init(sd_sample_params_t* sample_params);
335362
SD_API char* sd_sample_params_to_str(const sd_sample_params_t* sample_params);
336363

337364
SD_API enum sample_method_t sd_get_default_sample_method(const sd_ctx_t* sd_ctx);
338-
SD_API enum scheduler_t sd_get_default_scheduler(const sd_ctx_t* sd_ctx);
365+
SD_API enum scheduler_t sd_get_default_scheduler(const sd_ctx_t* sd_ctx, enum sample_method_t sample_method);
339366

340367
SD_API void sd_img_gen_params_init(sd_img_gen_params_t* sd_img_gen_params);
341368
SD_API char* sd_img_gen_params_to_str(const sd_img_gen_params_t* sd_img_gen_params);
@@ -363,7 +390,8 @@ SD_API bool convert(const char* input_path,
363390
const char* vae_path,
364391
const char* output_path,
365392
enum sd_type_t output_type,
366-
const char* tensor_type_rules);
393+
const char* tensor_type_rules,
394+
bool convert_name);
367395

368396
SD_API bool preprocess_canny(sd_image_t image,
369397
float high_threshold,
@@ -379,4 +407,4 @@ SD_API const char* sd_version(void);
379407
}
380408
#endif
381409

382-
#endif // __STABLE_DIFFUSION_H__
410+
#endif // __STABLE_DIFFUSION_H__
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace StableDiffusion.NET;
2+
3+
public enum CacheMode
4+
{
5+
Disabled = 0,
6+
EasyCache,
7+
UCache,
8+
DBCache,
9+
Taylorseer,
10+
CacheDit,
11+
}

StableDiffusion.NET/Enums/Schedule.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public enum Scheduler
1010
SGM_Uniform,
1111
Simple,
1212
Smoothstep,
13+
KlOptimal,
1314
LCM,
1415
Default
1516
}

StableDiffusion.NET/Extensions/ParameterExtension.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ public static void Validate(this DiffusionModelParameter parameter)
2020
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(parameter.ThreadCount, nameof(DiffusionModelParameter.ThreadCount));
2121
ArgumentNullException.ThrowIfNull(parameter.VaePath, nameof(DiffusionModelParameter.VaePath));
2222
ArgumentNullException.ThrowIfNull(parameter.TaesdPath, nameof(DiffusionModelParameter.TaesdPath));
23-
ArgumentNullException.ThrowIfNull(parameter.LoraModelDirectory, nameof(DiffusionModelParameter.LoraModelDirectory));
2423
ArgumentNullException.ThrowIfNull(parameter.ControlNetPath, nameof(DiffusionModelParameter.ControlNetPath));
25-
ArgumentNullException.ThrowIfNull(parameter.EmbeddingsDirectory, nameof(DiffusionModelParameter.EmbeddingsDirectory));
2624
ArgumentNullException.ThrowIfNull(parameter.StackedIdEmbeddingsDirectory, nameof(DiffusionModelParameter.StackedIdEmbeddingsDirectory));
2725

2826
if (!string.IsNullOrWhiteSpace(parameter.VaePath) && !string.IsNullOrWhiteSpace(parameter.TaesdPath)) throw new ArgumentException("VAE and TAESD are mutually exclusive.");
@@ -93,7 +91,6 @@ public static void Validate(this GuidanceParameter parameter)
9391

9492
ArgumentOutOfRangeException.ThrowIfNegative(parameter.ImgCfg);
9593
ArgumentOutOfRangeException.ThrowIfNegative(parameter.DistilledGuidance);
96-
ArgumentOutOfRangeException.ThrowIfNegative(parameter.MinCfg);
9794
ArgumentOutOfRangeException.ThrowIfNegative(parameter.TxtCfg);
9895

9996
parameter.Slg.Validate();
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using JetBrains.Annotations;
2+
3+
namespace StableDiffusion.NET;
4+
5+
[PublicAPI]
6+
public sealed class CacheParameter
7+
{
8+
public CacheMode Mode { get; set; } = CacheMode.Disabled;
9+
public float ReuseThreshold { get; set; } = 1.0f;
10+
public float StartPercent { get; set; } = 0.15f;
11+
public float EndPercent { get; set; } = 0.95f;
12+
public float ErrorDecayRate { get; set; } = 1.0f;
13+
public bool UseRelativeThreshold { get; set; } = true;
14+
public bool ResetErrorOnCompute { get; set; } = true;
15+
public int FnComputeBlocks { get; set; } = 8;
16+
public int BnComputeBlocks { get; set; } = 0;
17+
public float ResidualDiffThreshold { get; set; } = 0.08f;
18+
public int MaxWarmupSteps { get; set; } = 8;
19+
public int MaxCachedSteps { get; set; } = -1;
20+
public int MaxContinuousCachedSteps { get; set; } = -1;
21+
public int TaylorseerNDerivatives { get; set; } = 1;
22+
public int TaylorseerSkipInterval { get; set; } = 1;
23+
public string? ScmMask { get; set; } = null;
24+
public bool ScmPolicyDynamic { get; set; } = true;
25+
26+
internal CacheParameter() { }
27+
}

StableDiffusion.NET/Models/Parameter/CannyParameter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@ public sealed class CannyParameter
3535
///
3636
/// </summary>
3737
public bool Inverse { get; set; } = false;
38-
38+
3939
public static CannyParameter Create() => new();
4040
}

StableDiffusion.NET/Models/Parameter/DiffusionModelParameter.cs

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
1+
using System.Collections.Generic;
32
using JetBrains.Annotations;
43

54
namespace StableDiffusion.NET;
@@ -17,17 +16,6 @@ public sealed class DiffusionModelParameter
1716
/// </summary>
1817
public string TaesdPath { get; set; } = string.Empty;
1918

20-
/// <summary>
21-
/// lora model directory
22-
/// </summary>
23-
public string LoraModelDirectory { get; set; } = string.Empty;
24-
25-
/// <summary>
26-
/// path to embeddings
27-
/// </summary>
28-
[Obsolete("Use Embeddings instead")]
29-
public string EmbeddingsDirectory { get; set; } = string.Empty;
30-
3119
public List<Embedding> Embeddings { get; } = [];
3220

3321
/// <summary>
@@ -39,7 +27,7 @@ public sealed class DiffusionModelParameter
3927
/// number of threads to use during computation (default: -1)
4028
/// If threads = -1, then threads will be set to the number of CPU physical cores
4129
/// </summary>
42-
public int ThreadCount { get; set; } = 1;
30+
public int ThreadCount { get; set; } = -1;
4331

4432
/// <summary>
4533
///
@@ -55,6 +43,8 @@ public sealed class DiffusionModelParameter
5543

5644
public bool OffloadParamsToCPU { get; set; } = false;
5745

46+
public bool EnableMmap { get; set; } = false;
47+
5848
/// <summary>
5949
/// keep clip in cpu (for low vram)
6050
/// </summary>
@@ -91,6 +81,9 @@ public sealed class DiffusionModelParameter
9181
/// </summary>
9282
public bool VaeConvDirect { get; set; } = false;
9383

84+
public bool CircularX { get; set; } = false;
85+
public bool CircularY { get; set; } = false;
86+
9487
/// <summary>
9588
/// RNG (default: Standard)
9689
/// </summary>
@@ -108,7 +101,7 @@ public sealed class DiffusionModelParameter
108101
/// </summary>
109102
public Quantization Quantization { get; set; } = Quantization.Unspecified;
110103

111-
public float FlowShift { get; set; } = 0;
104+
public float FlowShift { get; set; } = float.PositiveInfinity;
112105

113106
public bool ForceSdxlVaeConvScale { get; set; } = false;
114107

@@ -144,12 +137,8 @@ public sealed class DiffusionModelParameter
144137
/// </summary>
145138
public string T5xxlPath { get; set; } = string.Empty;
146139

147-
[Obsolete("Use LLMPath instead")]
148-
public string Qwen2VLPath { get => LLMPath; set => LLMPath = value; }
149140
public string LLMPath { get; set; } = string.Empty;
150141

151-
[Obsolete("Use LLMVisionPath instead")]
152-
public string Qwen2VLVisionPath { get => LLMVisionPath; set => LLMVisionPath = value; }
153142
public string LLMVisionPath { get; set; } = string.Empty;
154143

155144
public string ClipVisionPath { get; set; } = string.Empty;
@@ -159,5 +148,7 @@ public sealed class DiffusionModelParameter
159148
public bool ChromaEnableT5Map { get; set; } = false;
160149
public int ChromaT5MaskPad { get; set; } = 1;
161150

151+
public bool QwenImageZeroCondT { get; set; } = false;
152+
162153
public static DiffusionModelParameter Create() => new();
163154
}

StableDiffusion.NET/Models/Parameter/EasyCache.cs

Lines changed: 0 additions & 11 deletions
This file was deleted.

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

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,6 @@ public static DiffusionModelParameter WithTaesd(this DiffusionModelParameter par
2828
return parameter;
2929
}
3030

31-
public static DiffusionModelParameter WithLoraSupport(this DiffusionModelParameter parameter, string loraModelDirectory)
32-
{
33-
ArgumentNullException.ThrowIfNull(loraModelDirectory);
34-
35-
parameter.LoraModelDirectory = loraModelDirectory;
36-
37-
return parameter;
38-
}
39-
40-
[Obsolete("Use WithEmbedding instead")]
41-
public static DiffusionModelParameter WithEmbeddingSupport(this DiffusionModelParameter parameter, string embeddingsDirectory)
42-
{
43-
ArgumentNullException.ThrowIfNull(embeddingsDirectory);
44-
45-
parameter.EmbeddingsDirectory = embeddingsDirectory;
46-
47-
return parameter;
48-
}
49-
5031
public static DiffusionModelParameter WithEmbedding(this DiffusionModelParameter parameter, Embedding embedding)
5132
{
5233
ArgumentNullException.ThrowIfNull(embedding);
@@ -246,17 +227,13 @@ public static DiffusionModelParameter WithT5xxlPath(this DiffusionModelParameter
246227
return parameter;
247228
}
248229

249-
[Obsolete("Use WithLLMPath instead")]
250-
public static DiffusionModelParameter WithQwen2VLPath(this DiffusionModelParameter parameter, string qwen2VLPath) => parameter.WithLLMPath(qwen2VLPath);
251230
public static DiffusionModelParameter WithLLMPath(this DiffusionModelParameter parameter, string llmPath)
252231
{
253232
parameter.LLMPath = llmPath;
254233

255234
return parameter;
256235
}
257236

258-
[Obsolete("Use WithLLMVisionPath instead")]
259-
public static DiffusionModelParameter WithQwen2VLVisionPath(this DiffusionModelParameter parameter, string qwen2VLVisionPath) => parameter.WithLLMVisionPath(qwen2VLVisionPath);
260237
public static DiffusionModelParameter WithLLMVisionPath(this DiffusionModelParameter parameter, string llmVisionPath)
261238
{
262239
parameter.LLMVisionPath = llmVisionPath;

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,6 @@ public static ImageGenerationParameter WithImgCfg(this ImageGenerationParameter
9999
return parameter;
100100
}
101101

102-
public static ImageGenerationParameter WithMinCfg(this ImageGenerationParameter parameter, float minCfg)
103-
{
104-
parameter.SampleParameter.Guidance.MinCfg = minCfg;
105-
106-
return parameter;
107-
}
108-
109102
public static ImageGenerationParameter WithGuidance(this ImageGenerationParameter parameter, float guidance)
110103
{
111104
parameter.SampleParameter.Guidance.DistilledGuidance = guidance;

0 commit comments

Comments
 (0)