Compare commits

..

No commits in common. "master" and "4.0.0" have entirely different histories.

21 changed files with 123 additions and 301 deletions

View File

@ -111,6 +111,52 @@ jobs:
name: windows-cuda12
path: .\build\bin\Release\stable-diffusion.dll
windows-sycl:
runs-on: windows-latest
defaults:
run:
shell: bash
env:
WINDOWS_BASEKIT_URL: https://registrationcenter-download.intel.com/akdlm/IRC_NAS/b380d914-366b-4b77-a74a-05e3c38b3514/intel-oneapi-base-toolkit-2025.0.0.882_offline.exe
WINDOWS_DPCPP_MKL: intel.oneapi.win.cpp-dpcpp-common:intel.oneapi.win.mkl.devel:intel.oneapi.win.dnnl:intel.oneapi.win.tbb.devel
ONEAPI_ROOT: "C:/Program Files (x86)/Intel/oneAPI"
steps:
- name: Checkout
id: checkout
uses: actions/checkout@v4.2.2
with:
repository: 'leejet/stable-diffusion.cpp'
ref: '${{ github.event.inputs.commit }}'
submodules: recursive
- name: Checkout Install Script
id: checkoutInstallScript
uses: actions/checkout@v4.2.2
with:
path: scripts
sparse-checkout: |
Backends/install-oneapi.bat
Backends/win-build-sycl.bat
sparse-checkout-cone-mode: false
- name: Install Sycl tools
id: installSyclCompiler
run: scripts/Backends/install-oneapi.bat $WINDOWS_BASEKIT_URL $WINDOWS_DPCPP_MKL
- name: Build
id: cmake_build
run: scripts/Backends/win-build-sycl.bat
- name: Upload artifact
id: upload_artifact
uses: actions/upload-artifact@v4.6.1
with:
name: windows-sycl
path: .\build\bin\stable-diffusion.dll
windows-hip:
runs-on: windows-latest
@ -120,20 +166,14 @@ jobs:
run: |
$ErrorActionPreference = "Stop"
write-host "Downloading AMD HIP SDK Installer"
Invoke-WebRequest -Uri "https://download.amd.com/developer/eula/rocm-hub/AMD-Software-PRO-Edition-25.Q3-WinSvr2022-For-HIP.exe" -OutFile "${env:RUNNER_TEMP}\rocm-install.exe"
Invoke-WebRequest -Uri "https://download.amd.com/developer/eula/rocm-hub/AMD-Software-PRO-Edition-24.Q4-WinSvr2022-For-HIP.exe" -OutFile "${env:RUNNER_TEMP}\rocm-install.exe"
write-host "Installing AMD HIP SDK"
Start-Process "${env:RUNNER_TEMP}\rocm-install.exe" -ArgumentList '-install' -NoNewWindow -Wait
write-host "Completed AMD HIP SDK installation"
- name: Verify ROCm
id: verify
run: |
# Find and test ROCm installation
$clangPath = Get-ChildItem 'C:\Program Files\AMD\ROCm\*\bin\clang.exe' | Select-Object -First 1
if (-not $clangPath) {
Write-Error "ROCm installation not found"
exit 1
}
& $clangPath.FullName --version
& 'C:\Program Files\AMD\ROCm\*\bin\clang.exe' --version
- name: Checkout
id: checkout
@ -164,7 +204,7 @@ jobs:
runs-on: windows-latest
env:
VULKAN_VERSION: 1.4.328.1
VULKAN_VERSION: 1.3.261.1
steps:
- name: Checkout
@ -178,7 +218,7 @@ jobs:
- name: Install Vulkan SDK
id: get_vulkan
run: |
curl.exe -o $env:RUNNER_TEMP/VulkanSDK-Installer.exe -L "https://sdk.lunarg.com/sdk/download/${env:VULKAN_VERSION}/windows/vulkansdk-windows-X64-${env:VULKAN_VERSION}.exe"
curl.exe -o $env:RUNNER_TEMP/VulkanSDK-Installer.exe -L "https://sdk.lunarg.com/sdk/download/${env:VULKAN_VERSION}/windows/VulkanSDK-${env:VULKAN_VERSION}-Installer.exe"
& "$env:RUNNER_TEMP\VulkanSDK-Installer.exe" --accept-licenses --default-answer --confirm-command install
Add-Content $env:GITHUB_ENV "VULKAN_SDK=C:\VulkanSDK\${env:VULKAN_VERSION}"
Add-Content $env:GITHUB_PATH "C:\VulkanSDK\${env:VULKAN_VERSION}\bin"
@ -314,7 +354,7 @@ jobs:
linux-hip:
runs-on: ubuntu-22.04
container: rocm/dev-ubuntu-22.04:6.4.4
container: rocm/dev-ubuntu-22.04:6.0.2
steps:
- name: Dependencies

View File

@ -19,10 +19,9 @@ jobs:
fetch-depth: 0
- name: Install .NET Core
uses: actions/setup-dotnet@v5
uses: actions/setup-dotnet@v4
with:
dotnet-version: |
10.0.x
9.0.x
8.0.x
@ -50,7 +49,7 @@ jobs:
with:
tag_name: ${{ github.event.inputs.version }}
generate_release_notes: true
files: StableDiffusion.NET/bin/Release/net10.0/StableDiffusion.NET.dll
files: StableDiffusion.NET/bin/Release/net9.0/StableDiffusion.NET.dll
- name: Nuget Push
id: nuget_push

View File

@ -31,46 +31,46 @@ extern "C" {
enum rng_type_t {
STD_DEFAULT_RNG,
CUDA_RNG,
CPU_RNG,
RNG_TYPE_COUNT
};
enum sample_method_t {
EULER_SAMPLE_METHOD,
EULER_A_SAMPLE_METHOD,
HEUN_SAMPLE_METHOD,
DPM2_SAMPLE_METHOD,
DPMPP2S_A_SAMPLE_METHOD,
DPMPP2M_SAMPLE_METHOD,
DPMPP2Mv2_SAMPLE_METHOD,
IPNDM_SAMPLE_METHOD,
IPNDM_V_SAMPLE_METHOD,
LCM_SAMPLE_METHOD,
DDIM_TRAILING_SAMPLE_METHOD,
TCD_SAMPLE_METHOD,
SAMPLE_METHOD_DEFAULT,
EULER,
HEUN,
DPM2,
DPMPP2S_A,
DPMPP2M,
DPMPP2Mv2,
IPNDM,
IPNDM_V,
LCM,
DDIM_TRAILING,
TCD,
EULER_A,
SAMPLE_METHOD_COUNT
};
enum scheduler_t {
DISCRETE_SCHEDULER,
KARRAS_SCHEDULER,
EXPONENTIAL_SCHEDULER,
AYS_SCHEDULER,
GITS_SCHEDULER,
SGM_UNIFORM_SCHEDULER,
SIMPLE_SCHEDULER,
SMOOTHSTEP_SCHEDULER,
LCM_SCHEDULER,
SCHEDULER_COUNT
DEFAULT,
DISCRETE,
KARRAS,
EXPONENTIAL,
AYS,
GITS,
SGM_UNIFORM,
SIMPLE,
SMOOTHSTEP,
SCHEDULE_COUNT
};
enum prediction_t {
DEFAULT_PRED,
EPS_PRED,
V_PRED,
EDM_V_PRED,
FLOW_PRED,
SD3_FLOW_PRED,
FLUX_FLOW_PRED,
FLUX2_FLOW_PRED,
PREDICTION_COUNT
};
@ -126,21 +126,6 @@ enum sd_log_level_t {
SD_LOG_ERROR
};
enum preview_t {
PREVIEW_NONE,
PREVIEW_PROJ,
PREVIEW_TAE,
PREVIEW_VAE,
PREVIEW_COUNT
};
enum lora_apply_mode_t {
LORA_APPLY_AUTO,
LORA_APPLY_IMMEDIATELY,
LORA_APPLY_AT_RUNTIME,
LORA_APPLY_MODE_COUNT,
};
typedef struct {
bool enabled;
int tile_size_x;
@ -156,8 +141,8 @@ typedef struct {
const char* clip_g_path;
const char* clip_vision_path;
const char* t5xxl_path;
const char* llm_path;
const char* llm_vision_path;
const char* qwen2vl_path;
const char* qwen2vl_vision_path;
const char* diffusion_model_path;
const char* high_noise_diffusion_model_path;
const char* vae_path;
@ -166,21 +151,17 @@ typedef struct {
const char* lora_model_dir;
const char* embedding_dir;
const char* photo_maker_path;
const char* tensor_type_rules;
bool vae_decode_only;
bool free_params_immediately;
int n_threads;
enum sd_type_t wtype;
enum rng_type_t rng_type;
enum rng_type_t sampler_rng_type;
enum prediction_t prediction;
enum lora_apply_mode_t lora_apply_mode;
bool offload_params_to_cpu;
bool keep_clip_on_cpu;
bool keep_control_net_on_cpu;
bool keep_vae_on_cpu;
bool diffusion_flash_attn;
bool tae_preview_only;
bool diffusion_conv_direct;
bool vae_conv_direct;
bool force_sdxl_vae_conv_scale;
@ -228,13 +209,6 @@ typedef struct {
float style_strength;
} sd_pm_params_t; // photo maker
typedef struct {
bool enabled;
float reuse_threshold;
float start_percent;
float end_percent;
} sd_easycache_params_t;
typedef struct {
const char* prompt;
const char* negative_prompt;
@ -255,7 +229,6 @@ typedef struct {
float control_strength;
sd_pm_params_t pm_params;
sd_tiling_params_t vae_tiling_params;
sd_easycache_params_t easycache;
} sd_img_gen_params_t;
typedef struct {
@ -275,19 +248,16 @@ typedef struct {
int64_t seed;
int video_frames;
float vace_strength;
sd_easycache_params_t easycache;
} sd_vid_gen_params_t;
typedef struct sd_ctx_t sd_ctx_t;
typedef void (*sd_log_cb_t)(enum sd_log_level_t level, const char* text, void* data);
typedef void (*sd_progress_cb_t)(int step, int steps, float time, void* data);
typedef void (*sd_preview_cb_t)(int step, int frame_count, sd_image_t* frames, bool is_noisy, void* data);
SD_API void sd_set_log_callback(sd_log_cb_t sd_log_cb, void* data);
SD_API void sd_set_progress_callback(sd_progress_cb_t cb, void* data);
SD_API void sd_set_preview_callback(sd_preview_cb_t cb, enum preview_t mode, int interval, bool denoised, bool noisy, void* data);
SD_API int32_t sd_get_num_physical_cores();
SD_API int32_t get_num_physical_cores();
SD_API const char* sd_get_system_info();
SD_API const char* sd_type_name(enum sd_type_t type);
@ -296,29 +266,21 @@ SD_API const char* sd_rng_type_name(enum rng_type_t rng_type);
SD_API enum rng_type_t str_to_rng_type(const char* str);
SD_API const char* sd_sample_method_name(enum sample_method_t sample_method);
SD_API enum sample_method_t str_to_sample_method(const char* str);
SD_API const char* sd_scheduler_name(enum scheduler_t scheduler);
SD_API enum scheduler_t str_to_scheduler(const char* str);
SD_API const char* sd_schedule_name(enum scheduler_t scheduler);
SD_API enum scheduler_t str_to_schedule(const char* str);
SD_API const char* sd_prediction_name(enum prediction_t prediction);
SD_API enum prediction_t str_to_prediction(const char* str);
SD_API const char* sd_preview_name(enum preview_t preview);
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_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);
SD_API sd_ctx_t* new_sd_ctx(const sd_ctx_params_t* sd_ctx_params);
SD_API void free_sd_ctx(sd_ctx_t* sd_ctx);
SD_API enum sample_method_t sd_get_default_sample_method(const sd_ctx_t* sd_ctx);
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 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);
SD_API sd_image_t* generate_image(sd_ctx_t* sd_ctx, const sd_img_gen_params_t* sd_img_gen_params);
@ -357,4 +319,4 @@ SD_API bool preprocess_canny(sd_image_t image,
}
#endif
#endif // __STABLE_DIFFUSION_H__
#endif // __STABLE_DIFFUSION_H__

View File

@ -1,8 +0,0 @@
namespace StableDiffusion.NET;
public enum LoraApplyMode
{
Auto,
Immediately,
AtRuntime
}

View File

@ -2,11 +2,10 @@
public enum Prediction
{
Default,
EPS,
V,
EDM_V,
Flow,
FluxFlow,
Flux2Flow,
Default
SD3Flow,
FluxFlow
}

View File

@ -1,9 +0,0 @@
namespace StableDiffusion.NET;
public enum Preview
{
None,
Proj,
TAE,
VAE
}

View File

@ -3,6 +3,5 @@
public enum RngType
{
Standard,
Cuda,
Cpu
Cuda
}

View File

@ -2,8 +2,8 @@
public enum Sampler
{
Default,
Euler,
Euler_A,
Heun,
DPM2,
DPMPP2SA,
@ -14,5 +14,5 @@ public enum Sampler
LCM,
DDIM_Trailing,
TCD,
Default
Euler_A,
}

View File

@ -2,6 +2,7 @@
public enum Scheduler
{
Default,
Discrete,
Karras,
Exponential,
@ -9,7 +10,5 @@ public enum Scheduler
GITS,
SGM_Uniform,
Simple,
Smoothstep,
LCM,
Default
Smoothstep
}

View File

@ -1,15 +0,0 @@
using System;
using HPPH;
namespace StableDiffusion.NET;
public sealed class StableDiffusionPreviewEventArgs(int step, bool isNoisy, Image<ColorRGB> image) : EventArgs
{
#region Properties & Fields
public int Step { get; } = step;
public bool IsNoisy { get; } = isNoisy;
public Image<ColorRGB> Image { get; } = image;
#endregion
}

View File

@ -1,5 +1,4 @@
using System;
using JetBrains.Annotations;
using JetBrains.Annotations;
namespace StableDiffusion.NET;
@ -73,8 +72,6 @@ public sealed class DiffusionModelParameter
/// </summary>
public bool FlashAttention { get; set; } = false;
public bool TaePreviewOnly { get; set; } = false;
/// <summary>
/// use Conv2d direct in the diffusion model
/// This might crash if it is not supported by the backend.
@ -92,12 +89,8 @@ public sealed class DiffusionModelParameter
/// </summary>
public RngType RngType { get; set; } = RngType.Standard;
public RngType SamplerRngType { get; set; } = RngType.Standard;
public Prediction Prediction { get; set; } = Prediction.Default;
public LoraApplyMode LoraApplyMode { get; set; } = LoraApplyMode.Auto;
/// <summary>
/// quantizes on load
/// not really useful in most cases
@ -113,8 +106,6 @@ public sealed class DiffusionModelParameter
/// </summary>
public string StackedIdEmbeddingsDirectory { get; set; } = string.Empty;
public string TensorTypeRules { get; set; } = string.Empty;
/// <summary>
/// path to full model
/// </summary>
@ -140,13 +131,9 @@ public sealed class DiffusionModelParameter
/// </summary>
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;
public string Qwen2VLPath { 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 Qwen2VLVisionPath { get; set; } = string.Empty;
public string ClipVisionPath { get; set; } = string.Empty;
public string HighNoiseDiffusionModelPath { get; set; } = string.Empty;

View File

@ -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() { }
}

View File

@ -236,20 +236,16 @@ 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)
public static DiffusionModelParameter WithQwen2VLPath(this DiffusionModelParameter parameter, string qwen2VLPath)
{
parameter.LLMPath = llmPath;
parameter.Qwen2VLPath = qwen2VLPath;
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)
public static DiffusionModelParameter WithQwen2VLVisionPath(this DiffusionModelParameter parameter, string qwen2VLVisionPath)
{
parameter.LLMVisionPath = llmVisionPath;
parameter.Qwen2VLVisionPath = qwen2VLVisionPath;
return parameter;
}

View File

@ -59,8 +59,6 @@ public sealed class ImageGenerationParameter
public TilingParameter VaeTiling { get; } = new();
public EasyCache EasyCache { get; } = new();
#endregion
public static ImageGenerationParameter Create() => new();

View File

@ -38,8 +38,6 @@ public sealed class VideoGenerationParameter
public float VaceStrength { get; set; }
public EasyCache EasyCache { get; } = new();
#endregion
public static VideoGenerationParameter Create() => new();

View File

@ -14,8 +14,8 @@ internal static unsafe class DiffusionModelParameterMarshaller
clip_g_path = AnsiStringMarshaller.ConvertToUnmanaged(managed.ClipGPath),
clip_vision_path = AnsiStringMarshaller.ConvertToUnmanaged(managed.ClipVisionPath),
t5xxl_path = AnsiStringMarshaller.ConvertToUnmanaged(managed.T5xxlPath),
llm_path = AnsiStringMarshaller.ConvertToUnmanaged(managed.LLMPath),
llm_vision_path = AnsiStringMarshaller.ConvertToUnmanaged(managed.LLMVisionPath),
qwen2vl_path = AnsiStringMarshaller.ConvertToUnmanaged(managed.Qwen2VLPath),
qwen2vl_vision_path = AnsiStringMarshaller.ConvertToUnmanaged(managed.Qwen2VLVisionPath),
diffusion_model_path = AnsiStringMarshaller.ConvertToUnmanaged(managed.DiffusionModelPath),
high_noise_diffusion_model_path = AnsiStringMarshaller.ConvertToUnmanaged(managed.HighNoiseDiffusionModelPath),
vae_path = AnsiStringMarshaller.ConvertToUnmanaged(managed.VaePath),
@ -24,21 +24,17 @@ internal static unsafe class DiffusionModelParameterMarshaller
lora_model_dir = AnsiStringMarshaller.ConvertToUnmanaged(managed.LoraModelDirectory),
embedding_dir = AnsiStringMarshaller.ConvertToUnmanaged(managed.EmbeddingsDirectory),
photo_maker_path = AnsiStringMarshaller.ConvertToUnmanaged(managed.StackedIdEmbeddingsDirectory),
tensor_type_rules = AnsiStringMarshaller.ConvertToUnmanaged(managed.TensorTypeRules),
vae_decode_only = (sbyte)(managed.VaeDecodeOnly ? 1 : 0),
free_params_immediately = (sbyte)(managed.FreeParamsImmediately ? 1 : 0),
n_threads = managed.ThreadCount,
wtype = managed.Quantization,
rng_type = managed.RngType,
sampler_rng_type = managed.SamplerRngType,
prediction = managed.Prediction,
lora_apply_mode = managed.LoraApplyMode,
offload_params_to_cpu = (sbyte)(managed.OffloadParamsToCPU ? 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),
diffusion_flash_attn = (sbyte)(managed.FlashAttention ? 1 : 0),
tae_preview_only = (sbyte)(managed.TaePreviewOnly ? 1 : 0),
diffusion_conv_direct = (sbyte)(managed.DiffusionConvDirect ? 1 : 0),
vae_conv_direct = (sbyte)(managed.VaeConvDirect ? 1 : 0),
force_sdxl_vae_conv_scale = (sbyte)(managed.ForceSdxlVaeConvScale ? 1 : 0),
@ -56,8 +52,8 @@ internal static unsafe class DiffusionModelParameterMarshaller
ClipGPath = AnsiStringMarshaller.ConvertToManaged(unmanaged.clip_g_path) ?? string.Empty,
ClipVisionPath = AnsiStringMarshaller.ConvertToManaged(unmanaged.clip_vision_path) ?? string.Empty,
T5xxlPath = AnsiStringMarshaller.ConvertToManaged(unmanaged.t5xxl_path) ?? string.Empty,
LLMPath = AnsiStringMarshaller.ConvertToManaged(unmanaged.llm_path) ?? string.Empty,
LLMVisionPath = AnsiStringMarshaller.ConvertToManaged(unmanaged.llm_vision_path) ?? string.Empty,
Qwen2VLPath = AnsiStringMarshaller.ConvertToManaged(unmanaged.qwen2vl_path) ?? string.Empty,
Qwen2VLVisionPath = AnsiStringMarshaller.ConvertToManaged(unmanaged.qwen2vl_vision_path) ?? string.Empty,
DiffusionModelPath = AnsiStringMarshaller.ConvertToManaged(unmanaged.diffusion_model_path) ?? string.Empty,
HighNoiseDiffusionModelPath = AnsiStringMarshaller.ConvertToManaged(unmanaged.high_noise_diffusion_model_path) ?? string.Empty,
VaePath = AnsiStringMarshaller.ConvertToManaged(unmanaged.vae_path) ?? string.Empty,
@ -66,21 +62,17 @@ internal static unsafe class DiffusionModelParameterMarshaller
LoraModelDirectory = AnsiStringMarshaller.ConvertToManaged(unmanaged.lora_model_dir) ?? string.Empty,
EmbeddingsDirectory = AnsiStringMarshaller.ConvertToManaged(unmanaged.embedding_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,
FreeParamsImmediately = unmanaged.free_params_immediately == 1,
ThreadCount = unmanaged.n_threads,
Quantization = unmanaged.wtype,
RngType = unmanaged.rng_type,
SamplerRngType = unmanaged.sampler_rng_type,
Prediction = unmanaged.prediction,
LoraApplyMode = unmanaged.lora_apply_mode,
OffloadParamsToCPU = unmanaged.offload_params_to_cpu == 1,
KeepClipOnCPU = unmanaged.keep_clip_on_cpu == 1,
KeepControlNetOnCPU = unmanaged.keep_control_net_on_cpu == 1,
KeepVaeOnCPU = unmanaged.keep_vae_on_cpu == 1,
FlashAttention = unmanaged.diffusion_flash_attn == 1,
TaePreviewOnly = unmanaged.tae_preview_only == 1,
DiffusionConvDirect = unmanaged.diffusion_conv_direct == 1,
VaeConvDirect = unmanaged.vae_conv_direct == 1,
ForceSdxlVaeConvScale = unmanaged.force_sdxl_vae_conv_scale == 1,
@ -96,8 +88,8 @@ internal static unsafe class DiffusionModelParameterMarshaller
AnsiStringMarshaller.Free(unmanaged.clip_l_path);
AnsiStringMarshaller.Free(unmanaged.clip_g_path);
AnsiStringMarshaller.Free(unmanaged.t5xxl_path);
AnsiStringMarshaller.Free(unmanaged.llm_path);
AnsiStringMarshaller.Free(unmanaged.llm_vision_path);
AnsiStringMarshaller.Free(unmanaged.qwen2vl_path);
AnsiStringMarshaller.Free(unmanaged.qwen2vl_vision_path);
AnsiStringMarshaller.Free(unmanaged.diffusion_model_path);
AnsiStringMarshaller.Free(unmanaged.vae_path);
AnsiStringMarshaller.Free(unmanaged.taesd_path);
@ -105,6 +97,5 @@ internal static unsafe class DiffusionModelParameterMarshaller
AnsiStringMarshaller.Free(unmanaged.lora_model_dir);
AnsiStringMarshaller.Free(unmanaged.embedding_dir);
AnsiStringMarshaller.Free(unmanaged.photo_maker_path);
AnsiStringMarshaller.Free(unmanaged.tensor_type_rules);
}
}

View File

@ -46,13 +46,6 @@ internal static class ImageGenerationParameterMarshaller
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
}
};
@ -133,14 +126,6 @@ internal static class ImageGenerationParameterMarshaller
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),
@ -160,8 +145,7 @@ internal static class ImageGenerationParameterMarshaller
control_image = _controlNetImage,
control_strength = managed.ControlNet.Strength,
pm_params = photoMakerParams,
vae_tiling_params = tilingParams,
easycache = easyCache
vae_tiling_params = tilingParams
};
}

View File

@ -27,14 +27,7 @@ internal static class VideoGenerationParameterMarshaller
Strength = unmanaged.strength,
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
}
VaceStrength = unmanaged.vace_strength
};
return parameter;
@ -75,14 +68,6 @@ internal static class VideoGenerationParameterMarshaller
_initImage = managed.InitImage?.ToSdImage() ?? new Native.Types.sd_image_t();
_endImage = managed.EndImage?.ToSdImage() ?? new Native.Types.sd_image_t();
_controlFrames = managed.ControlFrames == null ? null : managed.ControlFrames.ToSdImage();
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
{
@ -102,7 +87,6 @@ internal static class VideoGenerationParameterMarshaller
seed = managed.Seed,
video_frames = managed.FrameCount,
vace_strength = managed.VaceStrength,
easycache = easyCache,
};
}

View File

@ -23,9 +23,6 @@ using sd_img_gen_params_t = ImageGenerationParameter;
using sd_log_level_t = LogLevel;
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;
@ -60,8 +57,8 @@ internal unsafe partial class Native
public byte* clip_g_path;
public byte* clip_vision_path;
public byte* t5xxl_path;
public byte* llm_path;
public byte* llm_vision_path;
public byte* qwen2vl_path;
public byte* qwen2vl_vision_path;
public byte* diffusion_model_path;
public byte* high_noise_diffusion_model_path;
public byte* vae_path;
@ -70,21 +67,17 @@ internal unsafe partial class Native
public byte* lora_model_dir;
public byte* embedding_dir;
public byte* photo_maker_path;
public byte* tensor_type_rules;
public sbyte vae_decode_only;
public sbyte free_params_immediately;
public int n_threads;
public sd_type_t wtype;
public rng_type_t rng_type;
public rng_type_t sampler_rng_type;
public prediction_t prediction;
public lora_apply_mode_t lora_apply_mode;
public sbyte offload_params_to_cpu;
public sbyte keep_clip_on_cpu;
public sbyte keep_control_net_on_cpu;
public sbyte keep_vae_on_cpu;
public sbyte diffusion_flash_attn;
public sbyte tae_preview_only;
public sbyte diffusion_conv_direct;
public sbyte vae_conv_direct;
public sbyte force_sdxl_vae_conv_scale;
@ -134,7 +127,6 @@ internal unsafe partial class Native
public int shifted_timestep;
}
[StructLayout(LayoutKind.Sequential)]
internal struct sd_pm_params_t
{
public sd_image_t* id_images;
@ -143,15 +135,6 @@ internal unsafe partial class Native
public float style_strength;
}
[StructLayout(LayoutKind.Sequential)]
internal struct sd_easycache_params_t
{
public sbyte enabled;
public float reuse_threshold;
public float start_percent;
public float end_percent;
}
[StructLayout(LayoutKind.Sequential)]
internal struct sd_img_gen_params_t
{
@ -174,7 +157,6 @@ 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;
}
[StructLayout(LayoutKind.Sequential)]
@ -196,7 +178,6 @@ internal unsafe partial class Native
public int64_t seed;
public int video_frames;
public float vace_strength;
public sd_easycache_params_t easycache;
}
internal struct sd_ctx_t;
@ -207,7 +188,6 @@ internal unsafe partial class Native
internal delegate void sd_log_cb_t(sd_log_level_t level, [MarshalAs(UnmanagedType.LPStr)] string text, void* data);
internal delegate void sd_progress_cb_t(int step, int steps, float time, void* data);
internal delegate void sd_preview_cb_t(int step, int frame_count, sd_image_t* frames, bool is_noisy, void* data);
#endregion
@ -219,11 +199,8 @@ internal unsafe partial class Native
[LibraryImport(LIB_NAME, EntryPoint = "sd_set_progress_callback")]
internal static partial void sd_set_progress_callback(sd_progress_cb_t cb, void* data);
[LibraryImport(LIB_NAME, EntryPoint = "sd_set_preview_callback")]
internal static partial void sd_set_preview_callback(sd_preview_cb_t? cb, preview_t mode, int interval, [MarshalAs(UnmanagedType.I1)] bool denoised, [MarshalAs(UnmanagedType.I1)] bool noisy, void* data);
[LibraryImport(LIB_NAME, EntryPoint = "sd_get_num_physical_cores")]
internal static partial int32_t sd_get_num_physical_cores();
[LibraryImport(LIB_NAME, EntryPoint = "get_num_physical_cores")]
internal static partial int32_t get_num_physical_cores();
[LibraryImport(LIB_NAME, EntryPoint = "sd_get_system_info")]
[return: MarshalAs(UnmanagedType.LPStr)]
@ -252,12 +229,12 @@ internal unsafe partial class Native
[LibraryImport(LIB_NAME, EntryPoint = "str_to_sample_method")]
internal static partial sample_method_t str_to_sample_method([MarshalAs(UnmanagedType.LPStr)] string str);
[LibraryImport(LIB_NAME, EntryPoint = "sd_scheduler_name")]
[LibraryImport(LIB_NAME, EntryPoint = "sd_schedule_name")]
[return: MarshalAs(UnmanagedType.LPStr)]
internal static partial string sd_scheduler_name(scheduler_t schedule);
internal static partial string sd_schedule_name(scheduler_t schedule);
[LibraryImport(LIB_NAME, EntryPoint = "str_to_scheduler")]
internal static partial scheduler_t str_to_scheduler([MarshalAs(UnmanagedType.LPStr)] string str);
[LibraryImport(LIB_NAME, EntryPoint = "str_to_schedule")]
internal static partial scheduler_t str_to_schedule([MarshalAs(UnmanagedType.LPStr)] string str);
[LibraryImport(LIB_NAME, EntryPoint = "sd_prediction_name")]
[return: MarshalAs(UnmanagedType.LPStr)]
@ -266,23 +243,6 @@ internal unsafe partial class Native
[LibraryImport(LIB_NAME, EntryPoint = "str_to_prediction")]
internal static partial prediction_t str_to_prediction([MarshalAs(UnmanagedType.LPStr)] string str);
[LibraryImport(LIB_NAME, EntryPoint = "sd_preview_name")]
[return: MarshalAs(UnmanagedType.LPStr)]
internal static partial string sd_preview_name(preview_t preview);
[LibraryImport(LIB_NAME, EntryPoint = "str_to_preview")]
internal static partial preview_t str_to_preview([MarshalAs(UnmanagedType.LPStr)] string str);
[LibraryImport(LIB_NAME, EntryPoint = "sd_lora_apply_mode_name")]
[return: MarshalAs(UnmanagedType.LPStr)]
internal static partial string sd_lora_apply_mode_name(lora_apply_mode_t mode);
[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_ctx_params_init")]
@ -300,6 +260,9 @@ internal unsafe partial class Native
[LibraryImport(LIB_NAME, EntryPoint = "free_sd_ctx")]
internal static partial void free_sd_ctx(sd_ctx_t* sd_ctx);
[LibraryImport(LIB_NAME, EntryPoint = "sd_get_default_sample_method")]
internal static partial sample_method_t sd_get_default_sample_method(sd_ctx_t* sd_ctx);
//
[LibraryImport(LIB_NAME, EntryPoint = "sd_sample_params_init")]
@ -316,12 +279,6 @@ internal unsafe partial class Native
[return: MarshalAs(UnmanagedType.LPStr)]
internal static partial string sd_img_gen_params_to_str([MarshalUsing(typeof(ImageGenerationParameterMarshaller))] in sd_img_gen_params_t sd_img_gen_params);
[LibraryImport(LIB_NAME, EntryPoint = "sd_get_default_sample_method")]
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);
[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);

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net10.0;net9.0;net8.0</TargetFrameworks>
<TargetFrameworks>net9.0;net8.0</TargetFrameworks>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>

View File

@ -1,6 +1,6 @@
using HPPH;
using System;
using HPPH;
using JetBrains.Annotations;
using System;
namespace StableDiffusion.NET;
@ -12,7 +12,6 @@ public static unsafe class StableDiffusionCpp
// ReSharper disable NotAccessedField.Local - They are important, the delegate can be collected if it's not stored!
private static Native.sd_log_cb_t? _logCallback;
private static Native.sd_progress_cb_t? _progressCallback;
private static Native.sd_preview_cb_t? _previewCallback;
// ReSharper restore NotAccessedField.Local
#endregion
@ -21,7 +20,6 @@ public static unsafe class StableDiffusionCpp
public static event EventHandler<StableDiffusionLogEventArgs>? Log;
public static event EventHandler<StableDiffusionProgressEventArgs>? Progress;
public static event EventHandler<StableDiffusionPreviewEventArgs>? Preview;
#endregion
@ -35,19 +33,6 @@ public static unsafe class StableDiffusionCpp
Native.sd_set_progress_callback(_progressCallback = OnNativeProgress, null);
}
public static void EnablePreview(Preview mode, int interval, bool denoised, bool noisy)
{
ArgumentOutOfRangeException.ThrowIfNegative(interval);
if (mode == NET.Preview.None)
_previewCallback = null;
else if (_previewCallback == null)
_previewCallback = OnPreview;
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 = "")
{
ArgumentException.ThrowIfNullOrWhiteSpace(nameof(modelPath));
@ -60,7 +45,7 @@ public static unsafe class StableDiffusionCpp
public static string GetSystemInfo() => Native.sd_get_system_info();
public static int GetNumPhysicalCores() => Native.sd_get_num_physical_cores();
public static int GetNumPhysicalCores() => Native.get_num_physical_cores();
public static Image<ColorRGB> PreprocessCanny(CannyParameter parameter)
{
@ -104,18 +89,5 @@ public static unsafe class StableDiffusionCpp
catch { /**/ }
}
private static void OnPreview(int step, int frameCount, Native.Types.sd_image_t* frames, bool isNoisy, void* data)
{
try
{
if (frameCount <= 0 || frames == null) return;
Image<ColorRGB> image = ImageHelper.GetImage(frames, 0);
Preview?.Invoke(null, new StableDiffusionPreviewEventArgs(step, isNoisy, image));
}
catch { /**/ }
}
#endregion
}