From 0c0ddb23290d143186b385b440ba998f71bf431b Mon Sep 17 00:00:00 2001 From: DarthAffe Date: Tue, 20 Jan 2026 23:48:40 +0100 Subject: [PATCH] Updated sd.cpp to b87fe13; removed deprecated APIs; changed defaults to be closer to sd.cpp --- Header/stable-diffusion.h | 46 ++++++++-- StableDiffusion.NET/Enums/CacheMode.cs | 11 +++ StableDiffusion.NET/Enums/Schedule.cs | 1 + .../Extensions/ParameterExtension.cs | 3 - .../Models/Parameter/CacheParameter.cs | 27 ++++++ .../Models/Parameter/CannyParameter.cs | 2 +- .../Parameter/DiffusionModelParameter.cs | 29 +++--- .../Models/Parameter/EasyCache.cs | 11 --- .../DiffusionModelBuilderExtension.cs | 23 ----- .../ImageGenerationParameterExtension.cs | 7 -- .../VideoGenerationParameterExtension.cs | 14 --- .../Models/Parameter/GuidanceParameter.cs | 3 +- .../Parameter/ImageGenerationParameter.cs | 8 +- .../Models/Parameter/SampleParameter.cs | 8 +- .../Models/Parameter/SlgParameter.cs | 4 +- .../Models/Parameter/TilingParameter.cs | 12 +-- .../Parameter/VideoGenerationParameter.cs | 20 +++-- .../Marshaller/CacheParameterMarshaller.cs | 89 +++++++++++++++++++ .../DiffusionModelParameterMarshaller.cs | 61 ++++--------- .../ImageGenerationParameterMarshaller.cs | 48 +++------- .../Marshaller/SampleParameterMarshaller.cs | 2 - .../Marshaller/TilingParameterMarshaller.cs | 63 +++++++++++++ .../VideoGenerationParameterMarshaller.cs | 26 +++--- StableDiffusion.NET/Native/Native.cs | 40 ++++++--- StableDiffusion.NET/StableDiffusionCpp.cs | 6 +- 25 files changed, 337 insertions(+), 227 deletions(-) create mode 100644 StableDiffusion.NET/Enums/CacheMode.cs create mode 100644 StableDiffusion.NET/Models/Parameter/CacheParameter.cs delete mode 100644 StableDiffusion.NET/Models/Parameter/EasyCache.cs create mode 100644 StableDiffusion.NET/Native/Marshaller/CacheParameterMarshaller.cs create mode 100644 StableDiffusion.NET/Native/Marshaller/TilingParameterMarshaller.cs diff --git a/Header/stable-diffusion.h b/Header/stable-diffusion.h index d54376b..8f040d2 100644 --- a/Header/stable-diffusion.h +++ b/Header/stable-diffusion.h @@ -60,6 +60,7 @@ enum scheduler_t { SGM_UNIFORM_SCHEDULER, SIMPLE_SCHEDULER, SMOOTHSTEP_SCHEDULER, + KL_OPTIMAL_SCHEDULER, LCM_SCHEDULER, SCHEDULER_COUNT }; @@ -168,7 +169,6 @@ typedef struct { const char* vae_path; const char* taesd_path; const char* control_net_path; - const char* lora_model_dir; const sd_embedding_t* embeddings; uint32_t embedding_count; const char* photo_maker_path; @@ -182,6 +182,7 @@ typedef struct { enum prediction_t prediction; enum lora_apply_mode_t lora_apply_mode; bool offload_params_to_cpu; + bool enable_mmap; bool keep_clip_on_cpu; bool keep_control_net_on_cpu; bool keep_vae_on_cpu; @@ -189,10 +190,13 @@ typedef struct { bool tae_preview_only; bool diffusion_conv_direct; bool vae_conv_direct; + bool circular_x; + bool circular_y; bool force_sdxl_vae_conv_scale; bool chroma_use_dit_mask; bool chroma_use_t5_mask; int chroma_t5_mask_pad; + bool qwen_image_zero_cond_t; float flow_shift; } sd_ctx_params_t; @@ -236,12 +240,34 @@ typedef struct { float style_strength; } sd_pm_params_t; // photo maker +enum sd_cache_mode_t { + SD_CACHE_DISABLED = 0, + SD_CACHE_EASYCACHE, + SD_CACHE_UCACHE, + SD_CACHE_DBCACHE, + SD_CACHE_TAYLORSEER, + SD_CACHE_CACHE_DIT, +}; + typedef struct { - bool enabled; + enum sd_cache_mode_t mode; float reuse_threshold; float start_percent; float end_percent; -} sd_easycache_params_t; + float error_decay_rate; + bool use_relative_threshold; + bool reset_error_on_compute; + int Fn_compute_blocks; + int Bn_compute_blocks; + float residual_diff_threshold; + int max_warmup_steps; + int max_cached_steps; + int max_continuous_cached_steps; + int taylorseer_n_derivatives; + int taylorseer_skip_interval; + const char* scm_mask; + bool scm_policy_dynamic; +} sd_cache_params_t; typedef struct { bool is_high_noise; @@ -271,7 +297,7 @@ typedef struct { float control_strength; sd_pm_params_t pm_params; sd_tiling_params_t vae_tiling_params; - sd_easycache_params_t easycache; + sd_cache_params_t cache; } sd_img_gen_params_t; typedef struct { @@ -293,7 +319,8 @@ typedef struct { int64_t seed; int video_frames; float vace_strength; - sd_easycache_params_t easycache; + sd_tiling_params_t vae_tiling_params; + sd_cache_params_t cache; } sd_vid_gen_params_t; typedef struct sd_ctx_t sd_ctx_t; @@ -323,7 +350,7 @@ SD_API enum preview_t str_to_preview(const char* str); SD_API const char* sd_lora_apply_mode_name(enum lora_apply_mode_t mode); SD_API enum lora_apply_mode_t str_to_lora_apply_mode(const char* str); -SD_API void sd_easycache_params_init(sd_easycache_params_t* easycache_params); +SD_API void sd_cache_params_init(sd_cache_params_t* cache_params); SD_API void sd_ctx_params_init(sd_ctx_params_t* sd_ctx_params); 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); SD_API char* sd_sample_params_to_str(const sd_sample_params_t* sample_params); SD_API enum sample_method_t sd_get_default_sample_method(const sd_ctx_t* sd_ctx); -SD_API enum scheduler_t sd_get_default_scheduler(const sd_ctx_t* sd_ctx); +SD_API enum scheduler_t sd_get_default_scheduler(const sd_ctx_t* sd_ctx, enum sample_method_t sample_method); SD_API void sd_img_gen_params_init(sd_img_gen_params_t* sd_img_gen_params); 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, const char* vae_path, const char* output_path, enum sd_type_t output_type, - const char* tensor_type_rules); + const char* tensor_type_rules, + bool convert_name); SD_API bool preprocess_canny(sd_image_t image, float high_threshold, @@ -379,4 +407,4 @@ SD_API const char* sd_version(void); } #endif -#endif // __STABLE_DIFFUSION_H__ \ No newline at end of file +#endif // __STABLE_DIFFUSION_H__ diff --git a/StableDiffusion.NET/Enums/CacheMode.cs b/StableDiffusion.NET/Enums/CacheMode.cs new file mode 100644 index 0000000..dbde434 --- /dev/null +++ b/StableDiffusion.NET/Enums/CacheMode.cs @@ -0,0 +1,11 @@ +namespace StableDiffusion.NET; + +public enum CacheMode +{ + Disabled = 0, + EasyCache, + UCache, + DBCache, + Taylorseer, + CacheDit, +} \ No newline at end of file diff --git a/StableDiffusion.NET/Enums/Schedule.cs b/StableDiffusion.NET/Enums/Schedule.cs index 32a470b..ae05041 100644 --- a/StableDiffusion.NET/Enums/Schedule.cs +++ b/StableDiffusion.NET/Enums/Schedule.cs @@ -10,6 +10,7 @@ public enum Scheduler SGM_Uniform, Simple, Smoothstep, + KlOptimal, LCM, Default } \ No newline at end of file diff --git a/StableDiffusion.NET/Extensions/ParameterExtension.cs b/StableDiffusion.NET/Extensions/ParameterExtension.cs index 8aff3a5..c0901f5 100644 --- a/StableDiffusion.NET/Extensions/ParameterExtension.cs +++ b/StableDiffusion.NET/Extensions/ParameterExtension.cs @@ -20,9 +20,7 @@ public static class ParameterExtension ArgumentOutOfRangeException.ThrowIfNegativeOrZero(parameter.ThreadCount, nameof(DiffusionModelParameter.ThreadCount)); ArgumentNullException.ThrowIfNull(parameter.VaePath, nameof(DiffusionModelParameter.VaePath)); ArgumentNullException.ThrowIfNull(parameter.TaesdPath, nameof(DiffusionModelParameter.TaesdPath)); - ArgumentNullException.ThrowIfNull(parameter.LoraModelDirectory, nameof(DiffusionModelParameter.LoraModelDirectory)); ArgumentNullException.ThrowIfNull(parameter.ControlNetPath, nameof(DiffusionModelParameter.ControlNetPath)); - ArgumentNullException.ThrowIfNull(parameter.EmbeddingsDirectory, nameof(DiffusionModelParameter.EmbeddingsDirectory)); ArgumentNullException.ThrowIfNull(parameter.StackedIdEmbeddingsDirectory, nameof(DiffusionModelParameter.StackedIdEmbeddingsDirectory)); if (!string.IsNullOrWhiteSpace(parameter.VaePath) && !string.IsNullOrWhiteSpace(parameter.TaesdPath)) throw new ArgumentException("VAE and TAESD are mutually exclusive."); @@ -93,7 +91,6 @@ public static class ParameterExtension ArgumentOutOfRangeException.ThrowIfNegative(parameter.ImgCfg); ArgumentOutOfRangeException.ThrowIfNegative(parameter.DistilledGuidance); - ArgumentOutOfRangeException.ThrowIfNegative(parameter.MinCfg); ArgumentOutOfRangeException.ThrowIfNegative(parameter.TxtCfg); parameter.Slg.Validate(); diff --git a/StableDiffusion.NET/Models/Parameter/CacheParameter.cs b/StableDiffusion.NET/Models/Parameter/CacheParameter.cs new file mode 100644 index 0000000..114c73d --- /dev/null +++ b/StableDiffusion.NET/Models/Parameter/CacheParameter.cs @@ -0,0 +1,27 @@ +using JetBrains.Annotations; + +namespace StableDiffusion.NET; + +[PublicAPI] +public sealed class CacheParameter +{ + public CacheMode Mode { get; set; } = CacheMode.Disabled; + public float ReuseThreshold { get; set; } = 1.0f; + public float StartPercent { get; set; } = 0.15f; + public float EndPercent { get; set; } = 0.95f; + public float ErrorDecayRate { get; set; } = 1.0f; + public bool UseRelativeThreshold { get; set; } = true; + public bool ResetErrorOnCompute { get; set; } = true; + public int FnComputeBlocks { get; set; } = 8; + public int BnComputeBlocks { get; set; } = 0; + public float ResidualDiffThreshold { get; set; } = 0.08f; + public int MaxWarmupSteps { get; set; } = 8; + public int MaxCachedSteps { get; set; } = -1; + public int MaxContinuousCachedSteps { get; set; } = -1; + public int TaylorseerNDerivatives { get; set; } = 1; + public int TaylorseerSkipInterval { get; set; } = 1; + public string? ScmMask { get; set; } = null; + public bool ScmPolicyDynamic { get; set; } = true; + + internal CacheParameter() { } +} diff --git a/StableDiffusion.NET/Models/Parameter/CannyParameter.cs b/StableDiffusion.NET/Models/Parameter/CannyParameter.cs index fcaa56a..d95a7bb 100644 --- a/StableDiffusion.NET/Models/Parameter/CannyParameter.cs +++ b/StableDiffusion.NET/Models/Parameter/CannyParameter.cs @@ -35,6 +35,6 @@ public sealed class CannyParameter /// /// public bool Inverse { get; set; } = false; - + public static CannyParameter Create() => new(); } \ No newline at end of file diff --git a/StableDiffusion.NET/Models/Parameter/DiffusionModelParameter.cs b/StableDiffusion.NET/Models/Parameter/DiffusionModelParameter.cs index 4a1af18..a8b69ec 100644 --- a/StableDiffusion.NET/Models/Parameter/DiffusionModelParameter.cs +++ b/StableDiffusion.NET/Models/Parameter/DiffusionModelParameter.cs @@ -1,5 +1,4 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using JetBrains.Annotations; namespace StableDiffusion.NET; @@ -17,17 +16,6 @@ public sealed class DiffusionModelParameter /// public string TaesdPath { get; set; } = string.Empty; - /// - /// lora model directory - /// - public string LoraModelDirectory { get; set; } = string.Empty; - - /// - /// path to embeddings - /// - [Obsolete("Use Embeddings instead")] - public string EmbeddingsDirectory { get; set; } = string.Empty; - public List Embeddings { get; } = []; /// @@ -39,7 +27,7 @@ public sealed class DiffusionModelParameter /// number of threads to use during computation (default: -1) /// If threads = -1, then threads will be set to the number of CPU physical cores /// - public int ThreadCount { get; set; } = 1; + public int ThreadCount { get; set; } = -1; /// /// @@ -55,6 +43,8 @@ public sealed class DiffusionModelParameter public bool OffloadParamsToCPU { get; set; } = false; + public bool EnableMmap { get; set; } = false; + /// /// keep clip in cpu (for low vram) /// @@ -91,6 +81,9 @@ public sealed class DiffusionModelParameter /// public bool VaeConvDirect { get; set; } = false; + public bool CircularX { get; set; } = false; + public bool CircularY { get; set; } = false; + /// /// RNG (default: Standard) /// @@ -108,7 +101,7 @@ public sealed class DiffusionModelParameter /// public Quantization Quantization { get; set; } = Quantization.Unspecified; - public float FlowShift { get; set; } = 0; + public float FlowShift { get; set; } = float.PositiveInfinity; public bool ForceSdxlVaeConvScale { get; set; } = false; @@ -144,12 +137,8 @@ public sealed class DiffusionModelParameter /// public string T5xxlPath { get; set; } = string.Empty; - [Obsolete("Use LLMPath instead")] - public string Qwen2VLPath { get => LLMPath; set => LLMPath = value; } public string LLMPath { get; set; } = string.Empty; - [Obsolete("Use LLMVisionPath instead")] - public string Qwen2VLVisionPath { get => LLMVisionPath; set => LLMVisionPath = value; } public string LLMVisionPath { get; set; } = string.Empty; public string ClipVisionPath { get; set; } = string.Empty; @@ -159,5 +148,7 @@ public sealed class DiffusionModelParameter public bool ChromaEnableT5Map { get; set; } = false; public int ChromaT5MaskPad { get; set; } = 1; + public bool QwenImageZeroCondT { get; set; } = false; + public static DiffusionModelParameter Create() => new(); } \ No newline at end of file diff --git a/StableDiffusion.NET/Models/Parameter/EasyCache.cs b/StableDiffusion.NET/Models/Parameter/EasyCache.cs deleted file mode 100644 index ad0455d..0000000 --- a/StableDiffusion.NET/Models/Parameter/EasyCache.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace StableDiffusion.NET; - -public sealed class EasyCache -{ - public bool IsEnabled { get; set; } - public float ReuseThreshold { get; set; } - public float StartPercent { get; set; } - public float EndPercent { get; set; } - - internal EasyCache() { } -} \ No newline at end of file diff --git a/StableDiffusion.NET/Models/Parameter/Extensions/DiffusionModelBuilderExtension.cs b/StableDiffusion.NET/Models/Parameter/Extensions/DiffusionModelBuilderExtension.cs index aa53e75..cc27c48 100644 --- a/StableDiffusion.NET/Models/Parameter/Extensions/DiffusionModelBuilderExtension.cs +++ b/StableDiffusion.NET/Models/Parameter/Extensions/DiffusionModelBuilderExtension.cs @@ -28,25 +28,6 @@ public static class DiffusionModelBuilderExtension return parameter; } - public static DiffusionModelParameter WithLoraSupport(this DiffusionModelParameter parameter, string loraModelDirectory) - { - ArgumentNullException.ThrowIfNull(loraModelDirectory); - - parameter.LoraModelDirectory = loraModelDirectory; - - return parameter; - } - - [Obsolete("Use WithEmbedding instead")] - public static DiffusionModelParameter WithEmbeddingSupport(this DiffusionModelParameter parameter, string embeddingsDirectory) - { - ArgumentNullException.ThrowIfNull(embeddingsDirectory); - - parameter.EmbeddingsDirectory = embeddingsDirectory; - - return parameter; - } - public static DiffusionModelParameter WithEmbedding(this DiffusionModelParameter parameter, Embedding embedding) { ArgumentNullException.ThrowIfNull(embedding); @@ -246,8 +227,6 @@ public static class DiffusionModelBuilderExtension return parameter; } - [Obsolete("Use WithLLMPath instead")] - public static DiffusionModelParameter WithQwen2VLPath(this DiffusionModelParameter parameter, string qwen2VLPath) => parameter.WithLLMPath(qwen2VLPath); public static DiffusionModelParameter WithLLMPath(this DiffusionModelParameter parameter, string llmPath) { parameter.LLMPath = llmPath; @@ -255,8 +234,6 @@ public static class DiffusionModelBuilderExtension return parameter; } - [Obsolete("Use WithLLMVisionPath instead")] - public static DiffusionModelParameter WithQwen2VLVisionPath(this DiffusionModelParameter parameter, string qwen2VLVisionPath) => parameter.WithLLMVisionPath(qwen2VLVisionPath); public static DiffusionModelParameter WithLLMVisionPath(this DiffusionModelParameter parameter, string llmVisionPath) { parameter.LLMVisionPath = llmVisionPath; diff --git a/StableDiffusion.NET/Models/Parameter/Extensions/ImageGenerationParameterExtension.cs b/StableDiffusion.NET/Models/Parameter/Extensions/ImageGenerationParameterExtension.cs index 9459e81..7467016 100644 --- a/StableDiffusion.NET/Models/Parameter/Extensions/ImageGenerationParameterExtension.cs +++ b/StableDiffusion.NET/Models/Parameter/Extensions/ImageGenerationParameterExtension.cs @@ -99,13 +99,6 @@ public static class ImageGenerationParameterExtension return parameter; } - public static ImageGenerationParameter WithMinCfg(this ImageGenerationParameter parameter, float minCfg) - { - parameter.SampleParameter.Guidance.MinCfg = minCfg; - - return parameter; - } - public static ImageGenerationParameter WithGuidance(this ImageGenerationParameter parameter, float guidance) { parameter.SampleParameter.Guidance.DistilledGuidance = guidance; diff --git a/StableDiffusion.NET/Models/Parameter/Extensions/VideoGenerationParameterExtension.cs b/StableDiffusion.NET/Models/Parameter/Extensions/VideoGenerationParameterExtension.cs index 12ecd9b..41ada26 100644 --- a/StableDiffusion.NET/Models/Parameter/Extensions/VideoGenerationParameterExtension.cs +++ b/StableDiffusion.NET/Models/Parameter/Extensions/VideoGenerationParameterExtension.cs @@ -85,13 +85,6 @@ public static class VideoGenerationParameterExtension return parameter; } - public static VideoGenerationParameter WithMinCfg(this VideoGenerationParameter parameter, float minCfg) - { - parameter.SampleParameter.Guidance.MinCfg = minCfg; - - return parameter; - } - public static VideoGenerationParameter WithGuidance(this VideoGenerationParameter parameter, float guidance) { parameter.SampleParameter.Guidance.DistilledGuidance = guidance; @@ -196,13 +189,6 @@ public static class VideoGenerationParameterExtension return parameter; } - public static VideoGenerationParameter WithHighNoiseMinCfg(this VideoGenerationParameter parameter, float minCfg) - { - parameter.HighNoiseSampleParameter.Guidance.MinCfg = minCfg; - - return parameter; - } - public static VideoGenerationParameter WithHighNoiseGuidance(this VideoGenerationParameter parameter, float guidance) { parameter.HighNoiseSampleParameter.Guidance.DistilledGuidance = guidance; diff --git a/StableDiffusion.NET/Models/Parameter/GuidanceParameter.cs b/StableDiffusion.NET/Models/Parameter/GuidanceParameter.cs index 25b91e7..85f0fbf 100644 --- a/StableDiffusion.NET/Models/Parameter/GuidanceParameter.cs +++ b/StableDiffusion.NET/Models/Parameter/GuidanceParameter.cs @@ -3,8 +3,7 @@ public sealed class GuidanceParameter { public float TxtCfg { get; set; } = 7.0f; - public float ImgCfg { get; set; } = 7.0f; - public float MinCfg { get; set; } = 1.0f; + public float ImgCfg { get; set; } = float.PositiveInfinity; public float DistilledGuidance { get; set; } = 3.5f; public SlgParameter Slg { get; } = new(); diff --git a/StableDiffusion.NET/Models/Parameter/ImageGenerationParameter.cs b/StableDiffusion.NET/Models/Parameter/ImageGenerationParameter.cs index 4e67566..f6d5736 100644 --- a/StableDiffusion.NET/Models/Parameter/ImageGenerationParameter.cs +++ b/StableDiffusion.NET/Models/Parameter/ImageGenerationParameter.cs @@ -45,9 +45,9 @@ public sealed class ImageGenerationParameter public SampleParameter SampleParameter { get; internal init; } = new(); /// - /// strength for noising/unnoising (default: 0.7) + /// strength for noising/unnoising (default: 0.75) /// - public float Strength { get; set; } = 0.7f; + public float Strength { get; set; } = 0.75f; /// /// RNG seed. use -1 for a random seed (default: -1) @@ -58,9 +58,9 @@ public sealed class ImageGenerationParameter public PhotoMakerParameter PhotoMaker { get; } = new(); - public TilingParameter VaeTiling { get; } = new(); + public TilingParameter VaeTiling { get; internal init; } = new(); - public EasyCache EasyCache { get; } = new(); + public CacheParameter Cache { get; internal init; } = new(); public List Loras { get; } = []; diff --git a/StableDiffusion.NET/Models/Parameter/SampleParameter.cs b/StableDiffusion.NET/Models/Parameter/SampleParameter.cs index 83da2fb..e00714d 100644 --- a/StableDiffusion.NET/Models/Parameter/SampleParameter.cs +++ b/StableDiffusion.NET/Models/Parameter/SampleParameter.cs @@ -10,14 +10,14 @@ public sealed class SampleParameter public Scheduler Scheduler { get; set; } = Scheduler.Default; /// - /// sampling method (default: Euler_A) + /// sampling method (default: Default) /// - public Sampler SampleMethod { get; set; } = Sampler.Euler_A; + public Sampler SampleMethod { get; set; } = Sampler.Default; /// - /// number of sample steps (default: 25) + /// number of sample steps (default: 20) /// - public int SampleSteps { get; set; } = 25; + public int SampleSteps { get; set; } = 20; /// /// eta in DDIM, only for DDIM and TCD (default: 0) diff --git a/StableDiffusion.NET/Models/Parameter/SlgParameter.cs b/StableDiffusion.NET/Models/Parameter/SlgParameter.cs index 090df64..3b36cb6 100644 --- a/StableDiffusion.NET/Models/Parameter/SlgParameter.cs +++ b/StableDiffusion.NET/Models/Parameter/SlgParameter.cs @@ -3,9 +3,9 @@ public sealed class SlgParameter { /// - /// Layers to skip for SLG steps: (default: [7,8,9]) + /// Layers to skip for SLG steps /// - public int[] Layers { get; set; } = [7, 8, 9]; + public int[] Layers { get; set; } = []; /// /// SLG enabling point: (default: 0.01) diff --git a/StableDiffusion.NET/Models/Parameter/TilingParameter.cs b/StableDiffusion.NET/Models/Parameter/TilingParameter.cs index 6f9934e..1e869c4 100644 --- a/StableDiffusion.NET/Models/Parameter/TilingParameter.cs +++ b/StableDiffusion.NET/Models/Parameter/TilingParameter.cs @@ -2,13 +2,13 @@ public sealed class TilingParameter { - public bool IsEnabled { get; set; } + public bool IsEnabled { get; set; } = false; - public int TileSizeX { get; set; } - public int TileSizeY { get; set; } - public float TargetOverlap { get; set; } - public float RelSizeX { get; set; } - public float RelSizeY { get; set; } + public int TileSizeX { get; set; } = 0; + public int TileSizeY { get; set; } = 0; + public float TargetOverlap { get; set; } = 0.5f; + public float RelSizeX { get; set; } = 0; + public float RelSizeY { get; set; } = 0; internal TilingParameter() { } } \ No newline at end of file diff --git a/StableDiffusion.NET/Models/Parameter/VideoGenerationParameter.cs b/StableDiffusion.NET/Models/Parameter/VideoGenerationParameter.cs index 19549b7..082fe0d 100644 --- a/StableDiffusion.NET/Models/Parameter/VideoGenerationParameter.cs +++ b/StableDiffusion.NET/Models/Parameter/VideoGenerationParameter.cs @@ -21,25 +21,27 @@ public sealed class VideoGenerationParameter public IImage[]? ControlFrames { get; set; } - public int Width { get; set; } + public int Width { get; set; } = 512; - public int Height { get; set; } + public int Height { get; set; } = 512; public SampleParameter SampleParameter { get; internal init; } = new(); - public SampleParameter HighNoiseSampleParameter { get; internal init; } = new(); + public SampleParameter HighNoiseSampleParameter { get; internal init; } = new() { SampleSteps = -1 }; - public float MoeBoundry { get; set; } + public float MoeBoundry { get; set; } = 0.875f; - public float Strength { get; set; } + public float Strength { get; set; } = 0.75f; - public long Seed { get; set; } + public long Seed { get; set; } = -1; - public int FrameCount { get; set; } + public int FrameCount { get; set; } = 6; - public float VaceStrength { get; set; } + public float VaceStrength { get; set; } = 1.0f; - public EasyCache EasyCache { get; } = new(); + public TilingParameter VaeTiling { get; internal init; } = new(); + + public CacheParameter Cache { get; internal init; } = new(); public List Loras { get; } = []; diff --git a/StableDiffusion.NET/Native/Marshaller/CacheParameterMarshaller.cs b/StableDiffusion.NET/Native/Marshaller/CacheParameterMarshaller.cs new file mode 100644 index 0000000..e8bbb61 --- /dev/null +++ b/StableDiffusion.NET/Native/Marshaller/CacheParameterMarshaller.cs @@ -0,0 +1,89 @@ +// ReSharper disable MemberCanBeMadeStatic.Global + +using System; +using System.Runtime.InteropServices.Marshalling; + +namespace StableDiffusion.NET; + +[CustomMarshaller(typeof(CacheParameter), MarshalMode.ManagedToUnmanagedIn, typeof(CacheParameterMarshallerIn))] +[CustomMarshaller(typeof(CacheParameter), MarshalMode.ManagedToUnmanagedOut, typeof(CacheParameterMarshaller))] +[CustomMarshaller(typeof(CacheParameter), MarshalMode.ManagedToUnmanagedRef, typeof(CacheParameterMarshallerRef))] +internal static unsafe class CacheParameterMarshaller +{ + public static CacheParameter ConvertToManaged(Native.Types.sd_cache_params_t unmanaged) + { + CacheParameter parameter = new() + { + Mode = unmanaged.mode, + ReuseThreshold = unmanaged.reuse_threshold, + StartPercent = unmanaged.start_percent, + EndPercent = unmanaged.end_percent, + ErrorDecayRate = unmanaged.error_decay_rate, + UseRelativeThreshold = unmanaged.use_relative_threshold == 1, + ResetErrorOnCompute = unmanaged.reset_error_on_compute == 1, + FnComputeBlocks = unmanaged.Fn_compute_blocks, + BnComputeBlocks = unmanaged.Bn_compute_blocks, + ResidualDiffThreshold = unmanaged.residual_diff_threshold, + MaxWarmupSteps = unmanaged.max_warmup_steps, + MaxCachedSteps = unmanaged.max_cached_steps, + MaxContinuousCachedSteps = unmanaged.max_continuous_cached_steps, + TaylorseerNDerivatives = unmanaged.taylorseer_n_derivatives, + TaylorseerSkipInterval = unmanaged.taylorseer_skip_interval, + ScmMask = AnsiStringMarshaller.ConvertToManaged(unmanaged.scm_mask), + ScmPolicyDynamic = unmanaged.scm_policy_dynamic == 1 + }; + + return parameter; + } + + internal ref struct CacheParameterMarshallerIn + { + private Native.Types.sd_cache_params_t _cacheParams; + + public void FromManaged(CacheParameter managed) + { + + _cacheParams = new Native.Types.sd_cache_params_t + { + mode = managed.Mode, + reuse_threshold = managed.ReuseThreshold, + start_percent = managed.StartPercent, + end_percent = managed.EndPercent, + error_decay_rate = managed.ErrorDecayRate, + use_relative_threshold = (sbyte)(managed.UseRelativeThreshold ? 1 : 0), + reset_error_on_compute = (sbyte)(managed.ResetErrorOnCompute ? 1 : 0), + Fn_compute_blocks = managed.FnComputeBlocks, + Bn_compute_blocks = managed.BnComputeBlocks, + residual_diff_threshold = managed.ResidualDiffThreshold, + max_warmup_steps = managed.MaxWarmupSteps, + max_cached_steps = managed.MaxCachedSteps, + max_continuous_cached_steps = managed.MaxContinuousCachedSteps, + taylorseer_n_derivatives = managed.TaylorseerNDerivatives, + taylorseer_skip_interval = managed.TaylorseerSkipInterval, + scm_mask = AnsiStringMarshaller.ConvertToUnmanaged(managed.ScmMask), + scm_policy_dynamic = (sbyte)(managed.ScmPolicyDynamic ? 1 : 0) + }; + } + + public Native.Types.sd_cache_params_t ToUnmanaged() => _cacheParams; + + public void Free() + { + AnsiStringMarshaller.Free(_cacheParams.scm_mask); + } + } + + internal ref struct CacheParameterMarshallerRef() + { + private CacheParameterMarshallerIn _inMarshaller = new(); + private CacheParameter? _parameter; + + public void FromManaged(CacheParameter managed) => _inMarshaller.FromManaged(managed); + public Native.Types.sd_cache_params_t ToUnmanaged() => _inMarshaller.ToUnmanaged(); + + public void FromUnmanaged(Native.Types.sd_cache_params_t unmanaged) => _parameter = ConvertToManaged(unmanaged); + public CacheParameter ToManaged() => _parameter ?? throw new NullReferenceException($"{nameof(FromUnmanaged)} needs to be called before ToManaged."); + + public void Free() => _inMarshaller.Free(); + } +} \ No newline at end of file diff --git a/StableDiffusion.NET/Native/Marshaller/DiffusionModelParameterMarshaller.cs b/StableDiffusion.NET/Native/Marshaller/DiffusionModelParameterMarshaller.cs index ba10023..590ae8a 100644 --- a/StableDiffusion.NET/Native/Marshaller/DiffusionModelParameterMarshaller.cs +++ b/StableDiffusion.NET/Native/Marshaller/DiffusionModelParameterMarshaller.cs @@ -1,6 +1,4 @@ -using System.Collections.Generic; -using System.IO; -using System.Runtime.InteropServices; +using System.Runtime.InteropServices; using System.Runtime.InteropServices.Marshalling; namespace StableDiffusion.NET; @@ -26,7 +24,6 @@ internal static unsafe class DiffusionModelParameterMarshaller VaePath = AnsiStringMarshaller.ConvertToManaged(unmanaged.vae_path) ?? string.Empty, TaesdPath = AnsiStringMarshaller.ConvertToManaged(unmanaged.taesd_path) ?? string.Empty, ControlNetPath = AnsiStringMarshaller.ConvertToManaged(unmanaged.control_net_path) ?? string.Empty, - LoraModelDirectory = AnsiStringMarshaller.ConvertToManaged(unmanaged.lora_model_dir) ?? string.Empty, StackedIdEmbeddingsDirectory = AnsiStringMarshaller.ConvertToManaged(unmanaged.photo_maker_path) ?? string.Empty, TensorTypeRules = AnsiStringMarshaller.ConvertToManaged(unmanaged.tensor_type_rules) ?? string.Empty, VaeDecodeOnly = unmanaged.vae_decode_only == 1, @@ -38,6 +35,7 @@ internal static unsafe class DiffusionModelParameterMarshaller Prediction = unmanaged.prediction, LoraApplyMode = unmanaged.lora_apply_mode, OffloadParamsToCPU = unmanaged.offload_params_to_cpu == 1, + EnableMmap = unmanaged.enable_mmap == 1, KeepClipOnCPU = unmanaged.keep_clip_on_cpu == 1, KeepControlNetOnCPU = unmanaged.keep_control_net_on_cpu == 1, KeepVaeOnCPU = unmanaged.keep_vae_on_cpu == 1, @@ -45,10 +43,13 @@ internal static unsafe class DiffusionModelParameterMarshaller TaePreviewOnly = unmanaged.tae_preview_only == 1, DiffusionConvDirect = unmanaged.diffusion_conv_direct == 1, VaeConvDirect = unmanaged.vae_conv_direct == 1, + CircularX = unmanaged.circular_x == 1, + CircularY = unmanaged.circular_y == 1, ForceSdxlVaeConvScale = unmanaged.force_sdxl_vae_conv_scale == 1, ChromaUseDitMap = unmanaged.chroma_use_dit_mask == 1, ChromaEnableT5Map = unmanaged.chroma_use_t5_mask == 1, ChromaT5MaskPad = unmanaged.chroma_t5_mask_pad, + QwenImageZeroCondT = unmanaged.qwen_image_zero_cond_t == 1, FlowShift = unmanaged.flow_shift }; @@ -75,46 +76,17 @@ internal static unsafe class DiffusionModelParameterMarshaller public void FromManaged(DiffusionModelParameter managed) { - //_embeddings = (Native.Types.sd_embedding_t*)NativeMemory.Alloc((nuint)managed.Embeddings.Count, (nuint)Marshal.SizeOf()); + _embeddings = (Native.Types.sd_embedding_t*)NativeMemory.Alloc((nuint)managed.Embeddings.Count, (nuint)Marshal.SizeOf()); - //for (int i = 0; i < managed.Embeddings.Count; i++) - //{ - // Embedding embedding = managed.Embeddings[i]; - - // _embeddings[i] = new Native.Types.sd_embedding_t - // { - // name = AnsiStringMarshaller.ConvertToUnmanaged(embedding.Name), - // path = AnsiStringMarshaller.ConvertToUnmanaged(embedding.Path), - // }; - //} - - //HACK DarthAffe 25.12.2025 Workaround to support EmbeddingsDir till the next major release - List embeddings = []; + for (int i = 0; i < managed.Embeddings.Count; i++) { - embeddings.AddRange(managed.Embeddings); + Embedding embedding = managed.Embeddings[i]; - try + _embeddings[i] = new Native.Types.sd_embedding_t { - if (!string.IsNullOrWhiteSpace(managed.EmbeddingsDirectory) && Directory.Exists(managed.EmbeddingsDirectory)) - { - foreach (string file in Directory.GetFiles(managed.EmbeddingsDirectory)) - embeddings.Add(new Embedding(Path.GetFileNameWithoutExtension(file), file)); - } - } - catch { /**/ } - - _embeddings = (Native.Types.sd_embedding_t*)NativeMemory.Alloc((nuint)embeddings.Count, (nuint)Marshal.SizeOf()); - - for (int i = 0; i < embeddings.Count; i++) - { - Embedding embedding = embeddings[i]; - - _embeddings[i] = new Native.Types.sd_embedding_t - { - name = AnsiStringMarshaller.ConvertToUnmanaged(embedding.Name), - path = AnsiStringMarshaller.ConvertToUnmanaged(embedding.Path), - }; - } + name = AnsiStringMarshaller.ConvertToUnmanaged(embedding.Name), + path = AnsiStringMarshaller.ConvertToUnmanaged(embedding.Path), + }; } _ctxParams = new Native.Types.sd_ctx_params_t @@ -131,9 +103,8 @@ internal static unsafe class DiffusionModelParameterMarshaller vae_path = AnsiStringMarshaller.ConvertToUnmanaged(managed.VaePath), taesd_path = AnsiStringMarshaller.ConvertToUnmanaged(managed.TaesdPath), control_net_path = AnsiStringMarshaller.ConvertToUnmanaged(managed.ControlNetPath), - lora_model_dir = AnsiStringMarshaller.ConvertToUnmanaged(managed.LoraModelDirectory), embeddings = _embeddings, - embedding_count = (uint)embeddings.Count, + embedding_count = (uint)managed.Embeddings.Count, photo_maker_path = AnsiStringMarshaller.ConvertToUnmanaged(managed.StackedIdEmbeddingsDirectory), tensor_type_rules = AnsiStringMarshaller.ConvertToUnmanaged(managed.TensorTypeRules), vae_decode_only = (sbyte)(managed.VaeDecodeOnly ? 1 : 0), @@ -145,6 +116,7 @@ internal static unsafe class DiffusionModelParameterMarshaller prediction = managed.Prediction, lora_apply_mode = managed.LoraApplyMode, offload_params_to_cpu = (sbyte)(managed.OffloadParamsToCPU ? 1 : 0), + enable_mmap = (sbyte)(managed.EnableMmap ? 1 : 0), keep_clip_on_cpu = (sbyte)(managed.KeepClipOnCPU ? 1 : 0), keep_control_net_on_cpu = (sbyte)(managed.KeepControlNetOnCPU ? 1 : 0), keep_vae_on_cpu = (sbyte)(managed.KeepVaeOnCPU ? 1 : 0), @@ -152,10 +124,13 @@ internal static unsafe class DiffusionModelParameterMarshaller tae_preview_only = (sbyte)(managed.TaePreviewOnly ? 1 : 0), diffusion_conv_direct = (sbyte)(managed.DiffusionConvDirect ? 1 : 0), vae_conv_direct = (sbyte)(managed.VaeConvDirect ? 1 : 0), + circular_x = (sbyte)(managed.CircularX ? 1 : 0), + circular_y = (sbyte)(managed.CircularY ? 1 : 0), force_sdxl_vae_conv_scale = (sbyte)(managed.ForceSdxlVaeConvScale ? 1 : 0), chroma_use_dit_mask = (sbyte)(managed.ChromaUseDitMap ? 1 : 0), chroma_use_t5_mask = (sbyte)(managed.ChromaEnableT5Map ? 1 : 0), chroma_t5_mask_pad = managed.ChromaT5MaskPad, + qwen_image_zero_cond_t = (sbyte)(managed.QwenImageZeroCondT ? 1 : 0), flow_shift = managed.FlowShift }; } @@ -164,7 +139,6 @@ internal static unsafe class DiffusionModelParameterMarshaller public void Free() { - AnsiStringMarshaller.Free(_ctxParams.model_path); AnsiStringMarshaller.Free(_ctxParams.clip_l_path); AnsiStringMarshaller.Free(_ctxParams.clip_g_path); @@ -175,7 +149,6 @@ internal static unsafe class DiffusionModelParameterMarshaller AnsiStringMarshaller.Free(_ctxParams.vae_path); AnsiStringMarshaller.Free(_ctxParams.taesd_path); AnsiStringMarshaller.Free(_ctxParams.control_net_path); - AnsiStringMarshaller.Free(_ctxParams.lora_model_dir); AnsiStringMarshaller.Free(_ctxParams.photo_maker_path); AnsiStringMarshaller.Free(_ctxParams.tensor_type_rules); diff --git a/StableDiffusion.NET/Native/Marshaller/ImageGenerationParameterMarshaller.cs b/StableDiffusion.NET/Native/Marshaller/ImageGenerationParameterMarshaller.cs index b7e2242..7bf6674 100644 --- a/StableDiffusion.NET/Native/Marshaller/ImageGenerationParameterMarshaller.cs +++ b/StableDiffusion.NET/Native/Marshaller/ImageGenerationParameterMarshaller.cs @@ -38,22 +38,8 @@ internal static unsafe class ImageGenerationParameterMarshaller IdEmbedPath = AnsiStringMarshaller.ConvertToManaged(unmanaged.pm_params.id_embed_path) ?? string.Empty, StyleStrength = unmanaged.pm_params.style_strength, }, - VaeTiling = - { - IsEnabled = unmanaged.vae_tiling_params.enabled == 1, - TileSizeX = unmanaged.vae_tiling_params.tile_size_x, - TileSizeY = unmanaged.vae_tiling_params.tile_size_y, - TargetOverlap = unmanaged.vae_tiling_params.target_overlap, - RelSizeX = unmanaged.vae_tiling_params.rel_size_x, - RelSizeY = unmanaged.vae_tiling_params.rel_size_y - }, - EasyCache = - { - IsEnabled = unmanaged.easycache.enabled == 1, - ReuseThreshold = unmanaged.easycache.reuse_threshold, - StartPercent = unmanaged.easycache.start_percent, - EndPercent = unmanaged.easycache.end_percent - } + VaeTiling = TilingParameterMarshaller.ConvertToManaged(unmanaged.vae_tiling_params), + Cache = CacheParameterMarshaller.ConvertToManaged(unmanaged.cache) }; for (int i = 0; i < unmanaged.lora_count; i++) @@ -73,6 +59,8 @@ internal static unsafe class ImageGenerationParameterMarshaller internal ref struct ImageGenerationParameterMarshallerIn { private SampleParameterMarshaller.SampleParameterMarshallerIn _sampleParameterMarshaller = new(); + private TilingParameterMarshaller.TilingParameterMarshallerIn _tilingParameterMarshaller = new(); + private CacheParameterMarshaller.CacheParameterMarshallerIn _cacheParameterMarshaller = new(); private Native.Types.sd_img_gen_params_t _imgGenParams; private Native.Types.sd_image_t _initImage; @@ -87,6 +75,8 @@ internal static unsafe class ImageGenerationParameterMarshaller public void FromManaged(ImageGenerationParameter managed) { _sampleParameterMarshaller.FromManaged(managed.SampleParameter); + _tilingParameterMarshaller.FromManaged(managed.VaeTiling); + _cacheParameterMarshaller.FromManaged(managed.Cache); _initImage = managed.InitImage?.ToSdImage() ?? new Native.Types.sd_image_t(); _controlNetImage = managed.ControlNet.Image?.ToSdImage() ?? new Native.Types.sd_image_t(); @@ -129,24 +119,6 @@ internal static unsafe class ImageGenerationParameterMarshaller style_strength = managed.PhotoMaker.StyleStrength }; - Native.Types.sd_tiling_params_t tilingParams = new() - { - enabled = (sbyte)(managed.VaeTiling.IsEnabled ? 1 : 0), - tile_size_x = managed.VaeTiling.TileSizeX, - tile_size_y = managed.VaeTiling.TileSizeY, - target_overlap = managed.VaeTiling.TargetOverlap, - rel_size_x = managed.VaeTiling.RelSizeX, - rel_size_y = managed.VaeTiling.RelSizeY - }; - - Native.Types.sd_easycache_params_t easyCache = new() - { - enabled = (sbyte)(managed.EasyCache.IsEnabled ? 1 : 0), - reuse_threshold = managed.EasyCache.ReuseThreshold, - start_percent = managed.EasyCache.StartPercent, - end_percent = managed.EasyCache.EndPercent, - }; - _imgGenParams = new Native.Types.sd_img_gen_params_t { prompt = AnsiStringMarshaller.ConvertToUnmanaged(managed.Prompt), @@ -166,10 +138,10 @@ internal static unsafe class ImageGenerationParameterMarshaller control_image = _controlNetImage, control_strength = managed.ControlNet.Strength, pm_params = photoMakerParams, - vae_tiling_params = tilingParams, - easycache = easyCache, + vae_tiling_params = _tilingParameterMarshaller.ToUnmanaged(), + cache = _cacheParameterMarshaller.ToUnmanaged(), loras = _loras, - lora_count = (uint)managed.Loras.Count + lora_count = (uint)managed.Loras.Count, }; } @@ -192,6 +164,8 @@ internal static unsafe class ImageGenerationParameterMarshaller ImageHelper.Free(_pmIdImages, _imgGenParams.pm_params.id_images_count); _sampleParameterMarshaller.Free(); + _tilingParameterMarshaller.Free(); + _cacheParameterMarshaller.Free(); for (int i = 0; i < _imgGenParams.lora_count; i++) AnsiStringMarshaller.Free(_imgGenParams.loras[i].path); diff --git a/StableDiffusion.NET/Native/Marshaller/SampleParameterMarshaller.cs b/StableDiffusion.NET/Native/Marshaller/SampleParameterMarshaller.cs index 54de39b..6670d7c 100644 --- a/StableDiffusion.NET/Native/Marshaller/SampleParameterMarshaller.cs +++ b/StableDiffusion.NET/Native/Marshaller/SampleParameterMarshaller.cs @@ -19,7 +19,6 @@ internal static unsafe class SampleParameterMarshaller { TxtCfg = unmanaged.guidance.txt_cfg, ImgCfg = unmanaged.guidance.img_cfg, - MinCfg = unmanaged.guidance.min_cfg, DistilledGuidance = unmanaged.guidance.distilled_guidance, Slg = { @@ -74,7 +73,6 @@ internal static unsafe class SampleParameterMarshaller { txt_cfg = managed.Guidance.TxtCfg, img_cfg = managed.Guidance.ImgCfg, - min_cfg = managed.Guidance.MinCfg, distilled_guidance = managed.Guidance.DistilledGuidance, slg = slg }; diff --git a/StableDiffusion.NET/Native/Marshaller/TilingParameterMarshaller.cs b/StableDiffusion.NET/Native/Marshaller/TilingParameterMarshaller.cs new file mode 100644 index 0000000..c534ed1 --- /dev/null +++ b/StableDiffusion.NET/Native/Marshaller/TilingParameterMarshaller.cs @@ -0,0 +1,63 @@ +// ReSharper disable MemberCanBeMadeStatic.Global + +using System; +using System.Runtime.InteropServices.Marshalling; + +namespace StableDiffusion.NET; + +[CustomMarshaller(typeof(TilingParameter), MarshalMode.ManagedToUnmanagedIn, typeof(TilingParameterMarshallerIn))] +[CustomMarshaller(typeof(TilingParameter), MarshalMode.ManagedToUnmanagedOut, typeof(TilingParameterMarshaller))] +[CustomMarshaller(typeof(TilingParameter), MarshalMode.ManagedToUnmanagedRef, typeof(TilingParameterMarshallerRef))] +internal static unsafe class TilingParameterMarshaller +{ + public static TilingParameter ConvertToManaged(Native.Types.sd_tiling_params_t unmanaged) + { + TilingParameter parameter = new() + { + IsEnabled = unmanaged.enabled == 1, + TileSizeX = unmanaged.tile_size_x, + TileSizeY = unmanaged.tile_size_y, + TargetOverlap = unmanaged.target_overlap, + RelSizeX = unmanaged.rel_size_x, + RelSizeY = unmanaged.rel_size_y + }; + + return parameter; + } + + internal ref struct TilingParameterMarshallerIn + { + private Native.Types.sd_tiling_params_t _tilingParams; + + public void FromManaged(TilingParameter managed) + { + _tilingParams = new Native.Types.sd_tiling_params_t + { + enabled = (sbyte)(managed.IsEnabled ? 1 : 0), + tile_size_x = managed.TileSizeX, + tile_size_y = managed.TileSizeY, + target_overlap = managed.TargetOverlap, + rel_size_x = managed.RelSizeX, + rel_size_y = managed.RelSizeY + }; + } + + public Native.Types.sd_tiling_params_t ToUnmanaged() => _tilingParams; + + public void Free() { } + } + + internal ref struct TilingParameterMarshallerRef() + { + private TilingParameterMarshallerIn _inMarshaller = new(); + private TilingParameter? _parameter; + + public void FromManaged(TilingParameter managed) => _inMarshaller.FromManaged(managed); + public Native.Types.sd_tiling_params_t ToUnmanaged() => _inMarshaller.ToUnmanaged(); + + public void FromUnmanaged(Native.Types.sd_tiling_params_t unmanaged) => _parameter = ConvertToManaged(unmanaged); + public TilingParameter ToManaged() => _parameter ?? throw new NullReferenceException($"{nameof(FromUnmanaged)} needs to be called before ToManaged."); + + public void Free() => _inMarshaller.Free(); + } +} \ No newline at end of file diff --git a/StableDiffusion.NET/Native/Marshaller/VideoGenerationParameterMarshaller.cs b/StableDiffusion.NET/Native/Marshaller/VideoGenerationParameterMarshaller.cs index 02a10e0..0ec77b8 100644 --- a/StableDiffusion.NET/Native/Marshaller/VideoGenerationParameterMarshaller.cs +++ b/StableDiffusion.NET/Native/Marshaller/VideoGenerationParameterMarshaller.cs @@ -29,13 +29,8 @@ internal static unsafe class VideoGenerationParameterMarshaller Seed = unmanaged.seed, FrameCount = unmanaged.video_frames, VaceStrength = unmanaged.vace_strength, - EasyCache = - { - IsEnabled = unmanaged.easycache.enabled == 1, - ReuseThreshold = unmanaged.easycache.reuse_threshold, - StartPercent = unmanaged.easycache.start_percent, - EndPercent = unmanaged.easycache.end_percent - } + VaeTiling = TilingParameterMarshaller.ConvertToManaged(unmanaged.vae_tiling_params), + Cache = CacheParameterMarshaller.ConvertToManaged(unmanaged.cache), }; for (int i = 0; i < unmanaged.lora_count; i++) @@ -55,7 +50,9 @@ internal static unsafe class VideoGenerationParameterMarshaller internal ref struct VideoGenerationParameterMarshallerIn { private SampleParameterMarshaller.SampleParameterMarshallerIn _sampleParameterMarshaller = new(); + private TilingParameterMarshaller.TilingParameterMarshallerIn _tilingParameterMarshaller = new(); private SampleParameterMarshaller.SampleParameterMarshallerIn _highNoiseSampleParameterMarshaller = new(); + private CacheParameterMarshaller.CacheParameterMarshallerIn _cacheParameterMarshaller = new(); private Native.Types.sd_vid_gen_params_t _vidGenParams; private Native.Types.sd_image_t _initImage; @@ -68,7 +65,9 @@ internal static unsafe class VideoGenerationParameterMarshaller public void FromManaged(VideoGenerationParameter managed) { _sampleParameterMarshaller.FromManaged(managed.SampleParameter); + _tilingParameterMarshaller.FromManaged(managed.VaeTiling); _highNoiseSampleParameterMarshaller.FromManaged(managed.HighNoiseSampleParameter); + _cacheParameterMarshaller.FromManaged(managed.Cache); _initImage = managed.InitImage?.ToSdImage() ?? new Native.Types.sd_image_t(); _endImage = managed.EndImage?.ToSdImage() ?? new Native.Types.sd_image_t(); @@ -86,14 +85,6 @@ internal static unsafe class VideoGenerationParameterMarshaller }; } - Native.Types.sd_easycache_params_t easyCache = new() - { - enabled = (sbyte)(managed.EasyCache.IsEnabled ? 1 : 0), - reuse_threshold = managed.EasyCache.ReuseThreshold, - start_percent = managed.EasyCache.StartPercent, - end_percent = managed.EasyCache.EndPercent, - }; - _vidGenParams = new Native.Types.sd_vid_gen_params_t { prompt = AnsiStringMarshaller.ConvertToUnmanaged(managed.Prompt), @@ -112,7 +103,8 @@ internal static unsafe class VideoGenerationParameterMarshaller seed = managed.Seed, video_frames = managed.FrameCount, vace_strength = managed.VaceStrength, - easycache = easyCache, + vae_tiling_params = _tilingParameterMarshaller.ToUnmanaged(), + cache = _cacheParameterMarshaller.ToUnmanaged(), loras = _loras, lora_count = (uint)managed.Loras.Count }; @@ -132,7 +124,9 @@ internal static unsafe class VideoGenerationParameterMarshaller ImageHelper.Free(_controlFrames, _vidGenParams.control_frames_size); _sampleParameterMarshaller.Free(); + _tilingParameterMarshaller.Free(); _highNoiseSampleParameterMarshaller.Free(); + _cacheParameterMarshaller.Free(); for (int i = 0; i < _vidGenParams.lora_count; i++) AnsiStringMarshaller.Free(_vidGenParams.loras[i].path); diff --git a/StableDiffusion.NET/Native/Native.cs b/StableDiffusion.NET/Native/Native.cs index a6c0875..f1236ac 100644 --- a/StableDiffusion.NET/Native/Native.cs +++ b/StableDiffusion.NET/Native/Native.cs @@ -15,6 +15,8 @@ using rng_type_t = RngType; using sample_method_t = Sampler; using scheduler_t = Scheduler; using prediction_t = Prediction; +using sd_cache_mode_t = CacheMode; +using sd_cache_params_t = CacheParameter; using sd_ctx_params_t = DiffusionModelParameter; using sd_ctx_t = Native.Types.sd_ctx_t; using sd_image_t = Native.Types.sd_image_t; @@ -25,7 +27,6 @@ using sd_type_t = Quantization; using sd_vid_gen_params_t = VideoGenerationParameter; using lora_apply_mode_t = LoraApplyMode; using preview_t = Preview; -using sd_easycache_params_t = Native.Types.sd_easycache_params_t; using size_t = nuint; using uint32_t = uint; using uint8_t = byte; @@ -74,7 +75,6 @@ internal unsafe partial class Native public byte* vae_path; public byte* taesd_path; public byte* control_net_path; - public byte* lora_model_dir; public sd_embedding_t* embeddings; public uint32_t embedding_count; public byte* photo_maker_path; @@ -88,6 +88,7 @@ internal unsafe partial class Native public prediction_t prediction; public lora_apply_mode_t lora_apply_mode; public sbyte offload_params_to_cpu; + public sbyte enable_mmap; public sbyte keep_clip_on_cpu; public sbyte keep_control_net_on_cpu; public sbyte keep_vae_on_cpu; @@ -95,10 +96,13 @@ internal unsafe partial class Native public sbyte tae_preview_only; public sbyte diffusion_conv_direct; public sbyte vae_conv_direct; + public sbyte circular_x; + public sbyte circular_y; public sbyte force_sdxl_vae_conv_scale; public sbyte chroma_use_dit_mask; public sbyte chroma_use_t5_mask; public int chroma_t5_mask_pad; + public sbyte qwen_image_zero_cond_t; public float flow_shift; } @@ -126,7 +130,6 @@ internal unsafe partial class Native { public float txt_cfg; public float img_cfg; - public float min_cfg; public float distilled_guidance; public sd_slg_params_t slg; } @@ -154,12 +157,25 @@ internal unsafe partial class Native } [StructLayout(LayoutKind.Sequential)] - internal struct sd_easycache_params_t + internal struct sd_cache_params_t { - public sbyte enabled; + public sd_cache_mode_t mode; public float reuse_threshold; public float start_percent; public float end_percent; + public float error_decay_rate; + public sbyte use_relative_threshold; + public sbyte reset_error_on_compute; + public int Fn_compute_blocks; + public int Bn_compute_blocks; + public float residual_diff_threshold; + public int max_warmup_steps; + public int max_cached_steps; + public int max_continuous_cached_steps; + public int taylorseer_n_derivatives; + public int taylorseer_skip_interval; + public byte* scm_mask; + public sbyte scm_policy_dynamic; } [StructLayout(LayoutKind.Sequential)] @@ -194,7 +210,7 @@ internal unsafe partial class Native public float control_strength; public sd_pm_params_t pm_params; public sd_tiling_params_t vae_tiling_params; - public sd_easycache_params_t easycache; + public sd_cache_params_t cache; } [StructLayout(LayoutKind.Sequential)] @@ -218,7 +234,8 @@ internal unsafe partial class Native public int64_t seed; public int video_frames; public float vace_strength; - public sd_easycache_params_t easycache; + public sd_tiling_params_t vae_tiling_params; + public sd_cache_params_t cache; } internal struct sd_ctx_t; @@ -302,8 +319,8 @@ internal unsafe partial class Native [LibraryImport(LIB_NAME, EntryPoint = "str_to_lora_apply_mode")] internal static partial lora_apply_mode_t str_to_lora_apply_mode([MarshalAs(UnmanagedType.LPStr)] string str); - [LibraryImport(LIB_NAME, EntryPoint = "sd_easycache_params_init")] - internal static partial void sd_easycache_params_init(ref sd_easycache_params_t easycache_params); + [LibraryImport(LIB_NAME, EntryPoint = "sd_cache_params_init")] + internal static partial void sd_cache_params_init([MarshalUsing(typeof(CacheParameterMarshaller))] ref sd_cache_params_t cache_params); // @@ -343,7 +360,7 @@ internal unsafe partial class Native internal static partial sample_method_t sd_get_default_sample_method(sd_ctx_t* sd_ctx); [LibraryImport(LIB_NAME, EntryPoint = "sd_get_default_scheduler")] - internal static partial scheduler_t sd_get_default_scheduler(sd_ctx_t* sd_ctx); + internal static partial scheduler_t sd_get_default_scheduler(sd_ctx_t* sd_ctx, sample_method_t sample_method); [LibraryImport(LIB_NAME, EntryPoint = "generate_image")] internal static partial sd_image_t* generate_image(sd_ctx_t* sd_ctx, [MarshalUsing(typeof(ImageGenerationParameterMarshaller))] in sd_img_gen_params_t sd_img_gen_params); @@ -385,7 +402,8 @@ internal unsafe partial class Native [MarshalAs(UnmanagedType.LPStr)] string vae_path, [MarshalAs(UnmanagedType.LPStr)] string output_path, sd_type_t output_type, - [MarshalAs(UnmanagedType.LPStr)] string tensor_type_rules); + [MarshalAs(UnmanagedType.LPStr)] string tensor_type_rules, + [MarshalAs(UnmanagedType.I1)] bool convert_name); // diff --git a/StableDiffusion.NET/StableDiffusionCpp.cs b/StableDiffusion.NET/StableDiffusionCpp.cs index 3b5114c..af4562d 100644 --- a/StableDiffusion.NET/StableDiffusionCpp.cs +++ b/StableDiffusion.NET/StableDiffusionCpp.cs @@ -16,7 +16,7 @@ public static unsafe class StableDiffusionCpp private static Native.sd_preview_cb_t? _previewCallback; // ReSharper restore NotAccessedField.Local - public static string ExpectedSDCommit => "43a70e8"; + public static string ExpectedSDCommit => "b87fe13"; #endregion @@ -51,14 +51,14 @@ public static unsafe class StableDiffusionCpp Native.sd_set_preview_callback(_previewCallback, mode, interval, denoised, noisy, null); } - public static void Convert(string modelPath, string vaePath, Quantization quantization, string outputPath, string tensorTypeRules = "") + public static void Convert(string modelPath, string vaePath, Quantization quantization, string outputPath, bool convertName = false, string tensorTypeRules = "") { ArgumentException.ThrowIfNullOrWhiteSpace(nameof(modelPath)); ArgumentException.ThrowIfNullOrWhiteSpace(nameof(outputPath)); ArgumentNullException.ThrowIfNull(vaePath); if (!Enum.IsDefined(quantization)) throw new ArgumentOutOfRangeException(nameof(quantization)); - Native.convert(modelPath, vaePath, outputPath, quantization, tensorTypeRules); + Native.convert(modelPath, vaePath, outputPath, quantization, tensorTypeRules, convertName); } public static string GetSystemInfo() => Native.sd_get_system_info();