mirror of
https://github.com/DarthAffe/StableDiffusion.NET.git
synced 2025-12-13 05:48:40 +00:00
Compare commits
11 Commits
07a65ead6d
...
dd01e98caa
| Author | SHA1 | Date | |
|---|---|---|---|
| dd01e98caa | |||
| 68064e282d | |||
| 4348323abc | |||
| e6297d485f | |||
| 305d0a6f3a | |||
| e25d8ad5c9 | |||
| c214f0c5a1 | |||
| 8834ab9543 | |||
| dd8e41cca1 | |||
| 1db7ae59fb | |||
| 849845b0a2 |
44
.github/workflows/backends.yml
vendored
44
.github/workflows/backends.yml
vendored
@ -370,42 +370,7 @@ jobs:
|
||||
with:
|
||||
name: linux-cuda12
|
||||
path: ./build/bin/libstable-diffusion.so
|
||||
|
||||
linux-cuda11:
|
||||
runs-on: ubuntu-latest
|
||||
container: nvidia/cuda:11.8.0-devel-ubuntu22.04
|
||||
|
||||
steps:
|
||||
- name: Install dependencies
|
||||
env:
|
||||
DEBIAN_FRONTEND: noninteractive
|
||||
run: |
|
||||
apt update
|
||||
apt install -y cmake build-essential ninja-build libgomp1 git
|
||||
|
||||
- name: Checkout
|
||||
id: checkout
|
||||
uses: actions/checkout@v4.2.2
|
||||
with:
|
||||
repository: 'leejet/stable-diffusion.cpp'
|
||||
ref: '${{ github.event.inputs.commit }}'
|
||||
submodules: recursive
|
||||
|
||||
- name: Build
|
||||
id: cmake_build
|
||||
run: |
|
||||
mkdir build
|
||||
cd build
|
||||
cmake .. -DSD_CUDA=ON -DCMAKE_CUDA_ARCHITECTURES="61;75;86;89" -DSD_BUILD_SHARED_LIBS=ON
|
||||
cmake --build . --config Release
|
||||
|
||||
- name: Upload artifact
|
||||
id: upload_artifact
|
||||
uses: actions/upload-artifact@v4.6.1
|
||||
with:
|
||||
name: linux-cuda11
|
||||
path: ./build/bin/libstable-diffusion.so
|
||||
|
||||
|
||||
linux-sycl:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
@ -526,13 +491,11 @@ jobs:
|
||||
needs:
|
||||
- windows-cpu
|
||||
- windows-cuda12
|
||||
- windows-cuda11
|
||||
# - windows-sycl
|
||||
- windows-hip
|
||||
- windows-vulkan
|
||||
- linux-cpu
|
||||
- linux-cuda12
|
||||
- linux-cuda11
|
||||
- linux-sycl
|
||||
- linux-hip
|
||||
- osx-cpu
|
||||
@ -559,13 +522,10 @@ jobs:
|
||||
id: pack
|
||||
run: |
|
||||
nuget pack ./Backends/StableDiffusion.NET.Backend.Cpu.nuspec -version ${{ github.event.inputs.version }}
|
||||
nuget pack ./Backends/StableDiffusion.NET.Backend.Cuda11.Windows.nuspec -version ${{ github.event.inputs.version }}
|
||||
nuget pack ./Backends/StableDiffusion.NET.Backend.Cuda12.Windows.nuspec -version ${{ github.event.inputs.version }}
|
||||
nuget pack ./Backends/StableDiffusion.NET.Backend.Cuda11.Linux.nuspec -version ${{ github.event.inputs.version }}
|
||||
nuget pack ./Backends/StableDiffusion.NET.Backend.Cuda12.Linux.nuspec -version ${{ github.event.inputs.version }}
|
||||
nuget pack ./Backends/StableDiffusion.NET.Backend.Cuda.nuspec -version ${{ github.event.inputs.version }}
|
||||
nuget pack ./Backends/StableDiffusion.NET.Backend.Rocm.nuspec -version ${{ github.event.inputs.version }}
|
||||
nuget pack ./Backends/StableDiffusion.NET.Backend.Sycl.nuspec -version ${{ github.event.inputs.version }}
|
||||
nuget pack ./Backends/StableDiffusion.NET.Backend.Vulkan.nuspec -version ${{ github.event.inputs.version }}
|
||||
|
||||
- name: Upload artifacts
|
||||
@ -577,4 +537,4 @@ jobs:
|
||||
|
||||
- name: Nuget Push
|
||||
id: nuget_push
|
||||
run: dotnet nuget push **\*.nupkg --skip-duplicate --api-key ${{ secrets.NUGET_TOKEN }} --source https://api.nuget.org/v3/index.json
|
||||
run: dotnet nuget push **\*.nupkg --skip-duplicate --api-key ${{ secrets.NUGET_TOKEN }} --source https://api.nuget.org/v3/index.json
|
||||
@ -9,15 +9,13 @@
|
||||
<license type="expression">MIT</license>
|
||||
<icon>sd_net_cuda.png</icon>
|
||||
<projectUrl>https://github.com/DarthAffe/StableDiffusion.NET</projectUrl>
|
||||
<description>CUDA-Backend (11 and 12) for StableDiffusion.NET.</description>
|
||||
<description>CUDA-Backend (12) for StableDiffusion.NET.</description>
|
||||
<releaseNotes></releaseNotes>
|
||||
<copyright>Copyright © Darth Affe 2024</copyright>
|
||||
<readme>readme.md</readme>
|
||||
|
||||
<dependencies>
|
||||
<dependency id="StableDiffusion.NET.Backend.Cuda11.Windows" version="$version$" />
|
||||
<dependency id="StableDiffusion.NET.Backend.Cuda12.Windows" version="$version$" />
|
||||
<dependency id="StableDiffusion.NET.Backend.Cuda11.Linux" version="$version$" />
|
||||
<dependency id="StableDiffusion.NET.Backend.Cuda12.Linux" version="$version$" />
|
||||
</dependencies>
|
||||
</metadata>
|
||||
|
||||
@ -1,28 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<package>
|
||||
<metadata>
|
||||
<id>StableDiffusion.NET.Backend.Cuda11.Linux</id>
|
||||
<version>$version$</version>
|
||||
<title>StableDiffusion.NET.Backend.Cuda11.Linux</title>
|
||||
<authors>Darth Affe & stable-diffusion.cpp Authors</authors>
|
||||
<requireLicenseAcceptance>false</requireLicenseAcceptance>
|
||||
<license type="expression">MIT</license>
|
||||
<icon>sd_net_cuda.png</icon>
|
||||
<projectUrl>https://github.com/DarthAffe/StableDiffusion.NET</projectUrl>
|
||||
<description>CUDA 11 Linux Backend for StableDiffusion.NET.</description>
|
||||
<releaseNotes></releaseNotes>
|
||||
<copyright>Copyright © Darth Affe 2024</copyright>
|
||||
<readme>readme.md</readme>
|
||||
</metadata>
|
||||
|
||||
<files>
|
||||
<file src="StableDiffusion.NET.Backend.props" target="build/net8.0/StableDiffusion.NET.Backend.Cuda.props" />
|
||||
|
||||
<file src="linux-cuda11/libstable-diffusion.so" target="runtimes\linux-x64\native\cuda11\libstable-diffusion.so" />
|
||||
|
||||
<file src="sd_net_cuda.png" target="sd_net_cuda.png" />
|
||||
<file src="readme.md" target="readme.md" />
|
||||
<file src="ggml.txt" target="ggml.txt" />
|
||||
<file src="stable-diffusion.cpp.txt" target="stable-diffusion.cpp.txt" />
|
||||
</files>
|
||||
</package>
|
||||
@ -1,28 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<package>
|
||||
<metadata>
|
||||
<id>StableDiffusion.NET.Backend.Cuda11.Windows</id>
|
||||
<version>$version$</version>
|
||||
<title>StableDiffusion.NET.Backend.Cuda11.Windows</title>
|
||||
<authors>Darth Affe & stable-diffusion.cpp Authors</authors>
|
||||
<requireLicenseAcceptance>false</requireLicenseAcceptance>
|
||||
<license type="expression">MIT</license>
|
||||
<icon>sd_net_cuda.png</icon>
|
||||
<projectUrl>https://github.com/DarthAffe/StableDiffusion.NET</projectUrl>
|
||||
<description>CUDA 11 Windows Backend for StableDiffusion.NET.</description>
|
||||
<releaseNotes></releaseNotes>
|
||||
<copyright>Copyright © Darth Affe 2024</copyright>
|
||||
<readme>readme.md</readme>
|
||||
</metadata>
|
||||
|
||||
<files>
|
||||
<file src="StableDiffusion.NET.Backend.props" target="build/net8.0/StableDiffusion.NET.Backend.Cuda.props" />
|
||||
|
||||
<file src="windows-cuda11/stable-diffusion.dll" target="runtimes\win-x64\native\cuda11\stable-diffusion.dll" />
|
||||
|
||||
<file src="sd_net_cuda.png" target="sd_net_cuda.png" />
|
||||
<file src="readme.md" target="readme.md" />
|
||||
<file src="ggml.txt" target="ggml.txt" />
|
||||
<file src="stable-diffusion.cpp.txt" target="stable-diffusion.cpp.txt" />
|
||||
</files>
|
||||
</package>
|
||||
37
README.md
37
README.md
@ -27,11 +27,11 @@ StableDiffusionCpp.Progress += (_, args) => Console.WriteLine($"PROGRESS {args.S
|
||||
|
||||
Image<ColorRGB>? treeWithTiger;
|
||||
// Load a StableDiffusion model in a using block to unload it again after the two images are created
|
||||
using (DiffusionModel sd = ModelBuilder.StableDiffusion(@"<path to model")
|
||||
// .WithVae(@"<optional path to vae>")
|
||||
.WithMultithreading()
|
||||
.WithFlashAttention()
|
||||
.Build())
|
||||
using (DiffusionModel sd = new(DiffusionModelParameter.Create()
|
||||
.WithModelPath(@"N:\StableDiffusion\stable-diffusion-webui\models\Stable-diffusion\animagineXLV3_v30.safetensors")
|
||||
// .WithVae(@"<optional path to vae>")
|
||||
.WithMultithreading()
|
||||
.WithFlashAttention()))
|
||||
{
|
||||
// Create a image from a prompt
|
||||
Image<ColorRGB>? tree = sd.GenerateImage(ImageGenerationParameter.TextToImage("A beautiful tree standing on a small hill").WithSDXLDefaults());
|
||||
@ -43,17 +43,24 @@ using (DiffusionModel sd = ModelBuilder.StableDiffusion(@"<path to model")
|
||||
File.WriteAllBytes("image2.png", treeWithTiger.ToPng());
|
||||
}
|
||||
|
||||
// Load a flux kontext model
|
||||
using DiffusionModel flux = ModelBuilder.Flux(@"<path to flux-model.gguf>",
|
||||
@"<path to clip_l.safetensors>",
|
||||
@"<path to t5xxl_fp16.safetensors>",
|
||||
@"<path to ae.safetensors>")
|
||||
.WithMultithreading()
|
||||
.WithFlashAttention()
|
||||
.Build();
|
||||
// Load the qwen image edit model
|
||||
using DiffusionModel qwenContext = new(DiffusionModelParameter.Create()
|
||||
.WithDiffusionModelPath(@"<Qwen-Image-Edit-2509-path>")
|
||||
.WithQwen2VLPath(@"<Qwen2.5-VL-7B-Instruct-path>")
|
||||
.WithQwen2VLVisionPath(@"<Qwen2.5-VL-7B-Instruct.mmproj-path>")
|
||||
.WithVae(@"<qwen_image_vae-path>")
|
||||
.WithMultithreading()
|
||||
.WithFlashAttention()
|
||||
.WithFlowShift(3)
|
||||
.WithOffloadedParamsToCPU()
|
||||
.WithImmediatelyFreedParams());
|
||||
|
||||
// Perform an edit on the previosly created image
|
||||
Image<ColorRGB>? tigerOnMoon = flux.GenerateImage(ImageGenerationParameter.TextToImage("Remove the hill with the grass and place the tree with the tiger on the moon").WithFluxDefaults().WithRefImages(treeWithTiger));
|
||||
// Perform an edit on the previously created image
|
||||
Image<ColorRGB>? tigerOnMoon = qwenContext.GenerateImage(ImageGenerationParameter.TextToImage("Remove the background and place the tree and the tiger on the moon.")
|
||||
.WithSize(1024, 1024)
|
||||
.WithCfg(2.5f)
|
||||
.WithSampler(Sampler.Euler)
|
||||
.WithRefImages(treeWithTiger));
|
||||
File.WriteAllBytes("image3.png", tigerOnMoon.ToPng());
|
||||
```
|
||||
|
||||
|
||||
@ -82,14 +82,6 @@ internal static class ImageHelper
|
||||
}
|
||||
}
|
||||
|
||||
public static unsafe Native.Types.sd_image_t* ToSdImagePtr(this IImage image, bool monochrome = false)
|
||||
{
|
||||
Native.Types.sd_image_t* imagePtr = (Native.Types.sd_image_t*)NativeMemory.Alloc((nuint)Marshal.SizeOf<Native.Types.sd_image_t>());
|
||||
imagePtr[0] = image.ToSdImage(monochrome);
|
||||
|
||||
return imagePtr;
|
||||
}
|
||||
|
||||
public static unsafe void Free(this Native.Types.sd_image_t sdImage)
|
||||
{
|
||||
if (sdImage.data == null) return;
|
||||
@ -127,7 +119,7 @@ internal static class ImageHelper
|
||||
{
|
||||
int count = images.Length;
|
||||
|
||||
Native.Types.sd_image_t* imagePtr = (Native.Types.sd_image_t*)NativeMemory.Alloc((nuint)(count * Marshal.SizeOf<Native.Types.sd_image_t>()));
|
||||
Native.Types.sd_image_t* imagePtr = (Native.Types.sd_image_t*)NativeMemory.Alloc((nuint)count, (nuint)Marshal.SizeOf<Native.Types.sd_image_t>());
|
||||
|
||||
for (int i = 0; i < count; i++)
|
||||
imagePtr[i] = images[i].ToSdImage(monochrome);
|
||||
|
||||
@ -41,6 +41,8 @@ public sealed class DiffusionModelParameter
|
||||
/// </summary>
|
||||
public bool VaeDecodeOnly { get; set; } = false;
|
||||
|
||||
public bool FreeParamsImmediately { get; set; } = false;
|
||||
|
||||
/// <summary>
|
||||
/// process vae in tiles to reduce memory usage
|
||||
/// </summary>
|
||||
@ -103,7 +105,7 @@ public sealed class DiffusionModelParameter
|
||||
/// path to PHOTOMAKER stacked id embeddings
|
||||
/// </summary>
|
||||
public string StackedIdEmbeddingsDirectory { get; set; } = string.Empty;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// path to full model
|
||||
/// </summary>
|
||||
|
||||
@ -80,6 +80,13 @@ public static class DiffusionModelBuilderExtension
|
||||
return parameter;
|
||||
}
|
||||
|
||||
public static DiffusionModelParameter WithImmediatelyFreedParams(this DiffusionModelParameter parameter, bool immediatelyFreedParams = true)
|
||||
{
|
||||
parameter.FreeParamsImmediately = immediatelyFreedParams;
|
||||
|
||||
return parameter;
|
||||
}
|
||||
|
||||
public static DiffusionModelParameter WithVaeTiling(this DiffusionModelParameter parameter, bool vaeTiling = true)
|
||||
{
|
||||
parameter.VaeTiling = vaeTiling;
|
||||
|
||||
@ -25,7 +25,7 @@ internal static unsafe class DiffusionModelParameterMarshaller
|
||||
embedding_dir = AnsiStringMarshaller.ConvertToUnmanaged(managed.EmbeddingsDirectory),
|
||||
photo_maker_path = AnsiStringMarshaller.ConvertToUnmanaged(managed.StackedIdEmbeddingsDirectory),
|
||||
vae_decode_only = (sbyte)(managed.VaeDecodeOnly ? 1 : 0),
|
||||
free_params_immediately = 0, // DarthAffe 06.08.2025: Static value
|
||||
free_params_immediately = (sbyte)(managed.FreeParamsImmediately ? 1 : 0),
|
||||
n_threads = managed.ThreadCount,
|
||||
wtype = managed.Quantization,
|
||||
rng_type = managed.RngType,
|
||||
@ -63,6 +63,7 @@ internal static unsafe class DiffusionModelParameterMarshaller
|
||||
EmbeddingsDirectory = AnsiStringMarshaller.ConvertToManaged(unmanaged.embedding_dir) ?? string.Empty,
|
||||
StackedIdEmbeddingsDirectory = AnsiStringMarshaller.ConvertToManaged(unmanaged.photo_maker_path) ?? 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,
|
||||
|
||||
@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using HPPH;
|
||||
using JetBrains.Annotations;
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user