Skip to content

feat: add support for image embeddings #1580

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: concedo_experimental
Choose a base branch
from

Conversation

wbruna
Copy link

@wbruna wbruna commented Jun 3, 2025

This adds a new configuration parameter for the embeddings directory, and propagates it to sd.cpp.

I had to change sd.cpp to discard previous embeddings when preparing an inference, or the context would "remember" those for subsequent image generations. Since this can't really be properly tested without reusing the context, I believe it makes more sense to include it on Koboldcpp first.

(And because of leejet/stable-diffusion.cpp#656, it may be reasonable to either disable this for SDXL, or include stduhpf's fix locally too...)

@LostRuins
Copy link
Owner

Cool, I did see embeddings when I did my previous merge, but as I could not find a use case for them I didn't merge it. What would this be useful for? I believe we already have feature parity with A1111 like UIs

@wbruna
Copy link
Author

wbruna commented Jun 3, 2025

They help with specific concepts that may be underrepresented in the model's training data, or challenging to describe. The most popular are probably negative embeddings for guiding the generation away from low-quality images (for instance, "bad hands" embeddings are especially useful for SD 1.5).

@LostRuins
Copy link
Owner

Hmmm, but can they be created from the negative prompt? We do already have a negative prompt field.

I'm trying to avoid the clutter of having too many different file dependencies. An embedding file is unlikely to be used by 99% of people, and will likely be a confusing entry to most.

Especially the current implementation in sd.cpp reads files from a directory at runtime which is not suitable for kcpp, since the server should not be dynamically loading resources, otherwise it can crash on I/O issues - that's why I ditched the LoRA dir and only allow static LoRAs.

@wbruna
Copy link
Author

wbruna commented Jun 3, 2025

Hmmm, but can they be created from the negative prompt? We do already have a negative prompt field.

These are pre-existing embeddings, loaded and used pretty much like LoRAs. The loaded embedding vector replaces its trigger word at the prompt (or negative prompt).

An embedding file is unlikely to be used by 99% of people, and will likely be a confusing entry to most.

Actually, people used to SD 1.5 could expect those to work by default. For instance (just picking a random popular model): all Dreamshaper's example images on its Civitai homepage (https://civitai.com/models/4384/dreamshaper) have the "BadDream" embedding (https://civitai.com/models/72437?modelVersionId=77169 , by the model's author) on the negative prompt.

On the other hand...

Especially the current implementation in sd.cpp reads files from a directory at runtime which is not suitable for kcpp, since the server should not be dynamically loading resources, otherwise it can crash on I/O issues - that's why I ditched the LoRA dir and only allow static LoRAs.

... yeah, that's kind of unavoidable with the current code. It could be changed to preload a few embeddings beforehand (even the biggest ones have usually less than half a megabyte), and only apply the weights during inference, but that'd be a pretty big diff to keep outside sd.cpp :-(

@LostRuins
Copy link
Owner

Thanks but I think I will hold off on this PR for now, can keep it in mind for future.

@LostRuins LostRuins added the KIV for now Some issues prevent this from being merged label Jun 4, 2025
@LostRuins LostRuins force-pushed the concedo_experimental branch 2 times, most recently from fe14845 to 238be98 Compare June 14, 2025 04:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
KIV for now Some issues prevent this from being merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants