StableDiffusion.NET icon indicating copy to clipboard operation
StableDiffusion.NET copied to clipboard

External component has thrown an exception

Open SaymanDotNet opened this issue 8 months ago • 14 comments

Hello. I have simple code:

using HPPH;
using StableDiffusion.NET;

using DiffusionModel model = ModelBuilder.StableDiffusion(@"G:\path_to_checkpoints\jamJustAnotherMerge_v20bBakedvaePruned.safetensors")
										 .WithMultithreading()
										 .Build();


string prompt = "cat";
IImage<ColorRGB> image = model
						.TextToImage(prompt, model
							.GetDefaultParameter()
							.WithSteps(30)
							.WithCfg(8.0f)
							.WithSampler(Sampler.DPMPP2M)
							.WithSize(512, 800));

And i try to run and have error: ggml_cuda_init: GGML_CUDA_FORCE_MMQ: no ggml_cuda_init: GGML_CUDA_FORCE_CUBLAS: no ggml_cuda_init: found 1 CUDA devices: Device 0: NVIDIA GeForce RTX 3070, compute capability 8.6, VMM: yes |==================================================| 1130/1130 - 500.00it/s Unhandled exception. System.Runtime.InteropServices.SEHException (0x80004005): External component has thrown an exception. at StableDiffusion.NET.Native.g____PInvoke|10_0(sd_ctx_t* __sd_ctx_native, Byte* __prompt_native, Byte* __negative_prompt_native, Int32 __clip_skip_native, Single __cfg_scale_native, Single __guidance_native, Single __eta_native, Int32 __width_native, Int32 __height_native, Sampler __sample_method_native, Int32 __sample_steps_native, Int64 __seed_native, Int32 __batch_count_native, sd_image_t* __control_cond_native, Single __control_strength_native, Single __style_strength_native, SByte __normalize_input_native, Byte* __input_id_images_path_native, Int32* __skip_layers_native, Int32 __skip_layers_count_native, Single __slg_scale_native, Single __skip_layer_start_native, Single __skip_layer_end_native) at StableDiffusion.NET.Native.txt2img(sd_ctx_t* sd_ctx, String prompt, String negative_prompt, Int32 clip_skip, Single cfg_scale, Single guidance, Single eta, Int32 width, Int32 height, Sampler sample_method, Int32 sample_steps, Int64 seed, Int32 batch_count, sd_image_t* control_cond, Single control_strength, Single style_strength, Boolean normalize_input, String input_id_images_path, Int32[] skip_layers, Int32 skip_layers_count, Single slg_scale, Single skip_layer_start, Single skip_layer_end) at StableDiffusion.NET.DiffusionModel.TextToImage(String prompt, DiffusionParameter parameter) at Program.<Main>$(String[] args) in C:\Users\Sayman\source\repos\SD.NetT1\SD.Net\Program.cs:line 12

Downloaded from nuget. And downloaded some dlls from stable-diffusion.cpp (cuda dlls). Checkpoint file is sd1.5. What am I doing wrong?

SaymanDotNet avatar May 06 '25 18:05 SaymanDotNet

It looks like the problem is the lack of rtx30 support from stablediffusion.cpp

SaymanDotNet avatar May 06 '25 19:05 SaymanDotNet

what stable-diffusion-dlls are you using? does the exception also occur if you use one of the backend-packages?

DarthAffe avatar May 06 '25 19:05 DarthAffe

what stable-diffusion-dlls are you using? does the exception also occur if you use one of the backend-packages?

1.stable-diffusion-dlls: trying from packages (size ~550mb) and trying from git. cuda dlls from git (latest) 2. used StableDiffusion.NET.Backend.Cuda12.Windows

SaymanDotNet avatar May 07 '25 03:05 SaymanDotNet

Backends.CudaBackend.IsEnabled = true;
var cudaVersion = Backends.CudaBackend.CudaVersion;

cudaVersion shows -1.

SaymanDotNet avatar May 07 '25 03:05 SaymanDotNet

If it's reporting cuda-version as -1 it's not able to detect your cuda installation (or cuda is not installed at all). In that case it should not even try to do anything. I'll check what's wrong with loading a backend that is not available.

Do you have cuda installed? if yes check if the CUDA_PATH-environment-variable is set correctly.

DarthAffe avatar May 07 '25 18:05 DarthAffe

If you mean Cuda SDK, then it`s not installed.

SaymanDotNet avatar May 07 '25 19:05 SaymanDotNet

In that case I'd say that the exception is caused by a lack of some cuda-dlls. I've no idea how the setup works with the cuda-redistributables if it's not installed.

DarthAffe avatar May 07 '25 22:05 DarthAffe

In that case I'd say that the exception is caused by a lack of some cuda-dlls. I've no idea how the setup works with the cuda-redistributables if it's not installed.

Well, nevertheless, the sd.exe utility works fine. I'm not sure if you need to install any sdk to run stable duffusion on cuda.

SaymanDotNet avatar May 08 '25 07:05 SaymanDotNet

sd.exe from last release archive from stable-diffusion.cpp git. sd-master-10feacf-bin-win-cuda12-x64.zip

example with: sd.exe -m ../models/sd3_medium_incl_clips_t5xxlfp16.safetensors --cfg-scale 5 --steps 30 --sampling-method euler -H 1024 -W 1024 --seed 42 -p "fantasy medieval village world inside a glass sphere , high detail, fantasy, realistic, light effect, hyper detail, volumetric lighting, cinematic, macro, depth of field, blur, red light and clouds from the back, highly detailed epic cinematic concept art cg render made in maya, blender and photoshop, octane render, excellent composition, dynamic dramatic cinematic lighting, aesthetic, very inspirational, world inside a glass sphere by james gurney by artgerm with james jean, joe fenton and tristan eaton by ross tran, fine details, 4k resolution" works fine.

05.05.2025  13:28       113 712 640 cublas64_12.dll
05.05.2025  13:28       692 441 600 cublasLt64_12.dll
05.05.2025  13:28           573 952 cudart64_12.dll
09.03.2025  05:08           399 872 sd.exe
09.03.2025  18:27       548 001 792 stable-diffusion.dll

stable-diffusion.dll from nuget from this package. no cuda toolkid or sdk installed.

SaymanDotNet avatar May 08 '25 12:05 SaymanDotNet

Did you add all the cuda-dlls you had by your sd.exe too your c# program as well? The only thing I can imagine to cause that issue aside from some missing things is a problem with the settings you're using. Did you verify the exact same settings with sd.exe? (I know that some models are problematic with non-default size configurations)

DarthAffe avatar May 10 '25 18:05 DarthAffe

Same problem. And also RTX30. Apparently, this series is not supported?

If I understand correctly, to support rtx30 you need to add the -CMAKE_CUDA_ARCHITECTURES=86 switch when compiling Stable Diffusion, as specified here: https://github.com/leejet/stable-diffusion.cpp/blob/master/.github/workflows/build.yml#L166.

pro777s avatar Jul 16 '25 20:07 pro777s

I am having the same issue on a similar GPU, does stable-diffusion.cpp need to be rebuilt from source for this GPU?

ggml_cuda_init: GGML_CUDA_FORCE_MMQ: no ggml_cuda_init: GGML_CUDA_FORCE_CUBLAS: no ggml_cuda_init: found 1 CUDA devices: Device 0: NVIDIA GeForce RTX 3060 Laptop GPU, compute capability 8.6, VMM: yes

EDIT: Seems it may be unrelated as even when I uninstall the cuda backend and install the cpu backend I get the same error EDIT: Can confirm sd.exe is working fine, ableit slowly, with the avx512 binaries, but StableDiffusion.NET still encounters the System.Runtime.InteropServices.SEHException (0x80004005): External component has thrown an exception. at StableDiffusion.NET.Native.g____PInvoke

vugenti avatar Aug 10 '25 14:08 vugenti

This indicates you're using a sd.cpp build that had API changes and is not compatible (most likely to recent if you build yourself, the API has been compeltely reworked and I'm not done with the changey yet.) if you using backend nugets make sure the version fit the one of the main package

DarthAffe avatar Aug 10 '25 18:08 DarthAffe

This indicates you're using a sd.cpp build that had API changes and is not compatible (most likely to recent if you build yourself, the API has been compeltely reworked and I'm not done with the changey yet.) if you using backend nugets make sure the version fit the one of the main package

Thanks for the quick repsonse.

Right now I am testing with StableDiffusion.NET 3.6.0 and StableDiffusion.NET.Backends.CPU 3.6.0 / 3.6.2 so I believe these are matching.

How do I know what sd.cpp builds are compatible with the v3.6.0 of StableDiffusion.NET ?

vugenti avatar Aug 10 '25 19:08 vugenti