https://github.com/leejet/stable-diffusion.cpp Skip to content Toggle navigation Sign up * Product + Actions Automate any workflow + Packages Host and manage packages + Security Find and fix vulnerabilities + Codespaces Instant dev environments + Copilot Write better code with AI + Code review Manage code changes + Issues Plan and track work + Discussions Collaborate outside of code Explore + All features + Documentation + GitHub Skills + Blog * Solutions For + Enterprise + Teams + Startups + Education By Solution + CI/CD & Automation + DevOps + DevSecOps Resources + Customer Stories + White papers, Ebooks, Webinars + Partners * Open Source + GitHub Sponsors Fund open source developers + The ReadME Project GitHub community articles Repositories + Topics + Trending + Collections * Pricing Search or jump to... Search code, repositories, users, issues, pull requests... Search [ ] Clear Search syntax tips Provide feedback We read every piece of feedback, and take your input very seriously. [ ] [ ] Include my email address so I can be contacted Cancel Submit feedback Saved searches Use saved searches to filter your results more quickly Name [ ] Query [ ] To see all available qualifiers, see our documentation. Cancel Create saved search Sign in Sign up You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session. {{ message }} leejet / stable-diffusion.cpp Public * Notifications * Fork 26 * Star 636 Stable Diffusion in pure C/C++ License MIT license 636 stars 26 forks Activity Star Notifications * Code * Issues 8 * Pull requests 1 * Actions * Projects 0 * Security * Insights More * Code * Issues * Pull requests * Actions * Projects * Security * Insights leejet/stable-diffusion.cpp This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository. master Switch branches/tags [ ] Branches Tags Could not load branches Nothing to show {{ refName }} default View all branches Could not load tags Nothing to show {{ refName }} default View all tags Name already in use A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch? Cancel Create 1 branch 0 tags Code * Local * Codespaces * Clone HTTPS GitHub CLI [https://github.com/l] Use Git or checkout with SVN using the web URL. [gh repo clone leejet] Work fast with our official CLI. Learn more about the CLI. * Open with GitHub Desktop * Download ZIP Sign In Required Please sign in to use Codespaces. Launching GitHub Desktop If nothing happens, download GitHub Desktop and try again. Launching GitHub Desktop If nothing happens, download GitHub Desktop and try again. Launching Xcode If nothing happens, download Xcode and try again. Launching Visual Studio Code Your codespace will open once ready. There was a problem preparing your codespace, please try again. Latest commit @leejet leejet docs: update sd path ... 7132027 Aug 17, 2023 docs: update sd path 7132027 Git stats * 13 commits Files Permalink Failed to load latest commit information. Type Name Latest commit message Commit time .github/workflows feat: cmake improvements and simple ci (#9) August 17, 2023 21:09 assets feat: add img2img mode (#5) August 16, 2023 01:48 examples feat: cmake improvements and simple ci (#9) August 17, 2023 21:09 ggml @ ed522bb perf: sync ggml August 16, 2023 22:20 models Initial commit August 13, 2023 16:00 .gitignore feat: cmake improvements and simple ci (#9) August 17, 2023 21:09 .gitmodules Initial commit August 13, 2023 16:00 CMakeLists.txt feat: cmake improvements and simple ci (#9) August 17, 2023 21:09 LICENSE Initial commit August 13, 2023 16:00 README.md docs: update sd path August 17, 2023 23:44 stable-diffusion.cpp perf: free unused params immediately to reduce memory usage August 17, 2023 00:55 stable-diffusion.h perf: free unused params immediately to reduce memory usage August 17, 2023 00:55 View code [ ] stable-diffusion.cpp Features TODO Usage Get the Code Convert weights Quantization Build Using OpenBLAS Run txt2img example img2img example Memory/Disk Requirements References README.md [a] stable-diffusion.cpp Inference of Stable Diffusion in pure C/C++ Features * Plain C/C++ implementation based on ggml, working in the same way as llama.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 txt2img and img2img mode * Negative prompt * Sampling method + Euler A * Supported platforms + Linux + Mac OS + Windows TODO * [ ] 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) Usage Get the Code git clone --recursive https://github.com/leejet/stable-diffusion.cpp cd 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.cpp git pull origin master git submodule update Convert weights * download original weights(.ckpt or .safetensors). For example + Stable Diffusion v1.4 from https://huggingface.co/CompVis/ stable-diffusion-v-1-4-original + Stable Diffusion v1.5 from https://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.safetensors * convert weights to ggml model format cd models pip install -r requirements.txt python convert.py [path to weights] --out_type [output precision] # For example, python convert.py sd-v1-4.ckpt --out_type f16 Quantization You can specify the output model format using the --out_type parameter * f16 for 16-bit floating-point * f32 for 32-bit floating-point * q8_0 for 8-bit integer quantization * q5_0 or q5_1 for 5-bit integer quantization * q4_0 or q4_1 for 4-bit integer quantization Build mkdir build cd build cmake .. cmake --build . --config Release Using OpenBLAS cmake .. -DGGML_OPENBLAS=ON cmake --build . --config Release Run usage: ./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 txt2img example ./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 [f32] [f16] [q8_0] [q5_0] [q5_1] [q4_0] [q4_1] img2img example * ./output.png is 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 [img2img_output] Memory/Disk Requirements 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 References * ggml * stable-diffusion * stable-diffusion-webui * k-diffusion About Stable Diffusion in pure C/C++ Topics ai cplusplus image-generation diffusion text2image txt2img latent-diffusion stable-diffusion ggml Resources Readme License MIT license Activity Stars 636 stars Watchers 17 watching Forks 26 forks Report repository Releases No releases published Packages 0 No packages published Contributors 3 * @leejet leejet leejet * @ggerganov ggerganov Georgi Gerganov * @Green-Sky Green-Sky Erik Scholz Languages * C++ 93.3% * Python 6.0% * CMake 0.7% Footer (c) 2023 GitHub, Inc. Footer navigation * Terms * Privacy * Security * Status * Docs * Contact GitHub * Pricing * API * Training * Blog * About You can't perform that action at this time.