forked fromleejet/stable-diffusion.cpp
- Notifications
You must be signed in to change notification settings - Fork0
Stable Diffusion in pure C/C++
License
NotificationsYou must be signed in to change notification settings
sangohan/stable-diffusion.cpp
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Inference ofStable Diffusion in pure C/C++
- Plain C/C++ implementation based onggml, working in the same way asllama.cpp
- 16-bit, 32-bit float support
- 4-bit, 5-bit and 8-bit integer quantization support
- Accelerated memory-efficient CPU inference
- Only requires ~2.3GB when using txt2img with fp16 precision to generate a 512x512 image
- AVX, AVX2 and AVX512 support for x86 architectures
- Original
txt2imgandimg2imgmode - Negative prompt
- Sampling method
Euler A
- Supported platforms
- Linux
- Mac OS
- Windows
- More sampling methods
- GPU support
- Make inference faster
- The current implementation of ggml_conv_2d is slow and has high memory usage
- Continuing to reduce memory usage (quantizing the weights of ggml_conv_2d)
- stable-diffusion-webui style tokenizer (eg: token weighting, ...)
- LoRA support
- k-quants support
- Cross-platform reproducibility (perhaps ensuring consistency with the original SD)
git clone --recursive https://github.com/leejet/stable-diffusion.cppcd stable-diffusion.cpp- If you have already cloned the repository, you can use the following command to update the repository to the latest code.
cd stable-diffusion.cppgit pull origin mastergit submodule updatedownload original weights(.ckpt or .safetensors). For example
- Stable Diffusion v1.4 fromhttps://huggingface.co/CompVis/stable-diffusion-v-1-4-original
- Stable Diffusion v1.5 fromhttps://huggingface.co/runwayml/stable-diffusion-v1-5
curl -L -O https://huggingface.co/CompVis/stable-diffusion-v-1-4-original/resolve/main/sd-v1-4.ckpt# curl -L -O https://huggingface.co/runwayml/stable-diffusion-v1-5/resolve/main/v1-5-pruned-emaonly.safetensorsconvert weights to ggml model format
cd modelspip install -r requirements.txtpython convert.py [path to weights] --out_type [output precision]# For example, python convert.py sd-v1-4.ckpt --out_type f16
You can specify the output model format using the --out_type parameter
f16for 16-bit floating-pointf32for 32-bit floating-pointq8_0for 8-bit integer quantizationq5_0orq5_1for 5-bit integer quantizationq4_0orq4_1for 4-bit integer quantization
mkdir buildcd buildcmake ..cmake --build. --config Release
cmake .. -DGGML_OPENBLAS=ONcmake --build . --config Releaseusage: ./bin/sd [arguments]arguments: -h, --help show this help message and exit -M, --mode [txt2img or img2img] generation mode (default: txt2img) -t, --threads N number of threads to use during computation (default: -1). If threads <= 0, then threads will be set to the number of CPU physical cores -m, --model [MODEL] path to model -i, --init-img [IMAGE] path to the input image, required by img2img -o, --output OUTPUT path to write result image to (default: .\output.png) -p, --prompt [PROMPT] the prompt to render -n, --negative-prompt PROMPT the negative prompt (default: "") --cfg-scale SCALE unconditional guidance scale: (default: 7.0) --strength STRENGTH strength for noising/unnoising (default: 0.75) 1.0 corresponds to full destruction of information in init image -H, --height H image height, in pixel space (default: 512) -W, --width W image width, in pixel space (default: 512) --sample-method SAMPLE_METHOD sample method (default: "eular a") --steps STEPS number of sample steps (default: 20) -s SEED, --seed SEED RNG seed (default: 42, use random seed for < 0) -v, --verbose print extra info./bin/sd -m ../models/sd-v1-4-ggml-model-f16.bin -p "a lovely cat"Using formats of different precisions will yield results of varying quality.
| f32 | f16 | q8_0 | q5_0 | q5_1 | q4_0 | q4_1 |
|---|---|---|---|---|---|---|
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
./output.pngis the image generated from the above txt2img pipeline
./bin/sd --mode img2img -m ../models/sd-v1-4-ggml-model-f16.bin -p "cat with blue eyes" -i ./output.png -o ./img2img_output.png --strength 0.4| precision | f32 | f16 | q8_0 | q5_0 | q5_1 | q4_0 | q4_1 |
|---|---|---|---|---|---|---|---|
| Disk | 2.7G | 2.0G | 1.7G | 1.6G | 1.6G | 1.5G | 1.5G |
| Memory(txt2img - 512 x 512) | ~2.8G | ~2.3G | ~2.1G | ~2.0G | ~2.0G | ~2.0G | ~2.0G |
About
Stable Diffusion in pure C/C++
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
No releases published
Packages0
No packages published
Languages
- C++93.3%
- Python6.0%
- CMake0.7%








