Commit cab31f1 for stable-diffusion.cpp

commit cab31f12b54cb6ca2541b7858dff43041b96c85b
Author: Zihao <Louis20120127@gmail.com>
Date:   Sat Nov 28 21:10:50 2026 +1100

    docs: document textual inversion usage (#2339)

diff --git a/README.md b/README.md
index b5ddd2a..f36a435 100644
--- a/README.md
+++ b/README.md
@@ -100,6 +100,7 @@ API and command-line option may change frequently.***
   - Control Net support with SD 1.5
   - [ADetailer](./docs/models/adetailer.md)
   - LoRA support, same as [stable-diffusion-webui](https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/Features#lora)
+  - [Textual inversion embeddings](./docs/embeddings.md) for SD 1.x/2.x/SDXL via `--embd-dir` and `<embedding:...>` tags
   - Latent Consistency Models support (LCM/LCM-LoRA)
   - Faster and memory efficient latent decoding with [TAESD](./docs/models/taesd.md)
   - Upscale images generated with [ESRGAN](./docs/models/esrgan.md)
diff --git a/docs/embeddings.md b/docs/embeddings.md
new file mode 100644
index 0000000..49c4fe6
--- /dev/null
+++ b/docs/embeddings.md
@@ -0,0 +1,108 @@
+# Textual Inversion Embeddings
+
+Textual inversion embeddings add a new trigger word (or a few vectors) to the
+CLIP text encoder. They are supported for **SD 1.x, SD 2.x and SDXL**
+checkpoints, which use `FrozenCLIPEmbedderWithCustomWords`. Other families
+(SD3, FLUX, Qwen Image, ...) do not use this code path and ignore `--embd-dir`.
+
+Supported weight files are `.safetensors`, `.pt`, `.ckpt` and `.gguf`.
+
+## Directory layout
+
+Point `--embd-dir` at the folder holding the embedding files. Only the top
+level of the directory is scanned; subdirectory files are not discovered:
+
+```sh
+./bin/sd-cli -m ../models/v1-5-pruned-emaonly.safetensors \
+  --embd-dir ../models/embeddings \
+  -p "easynegative, girl playing the guitar" \
+  --steps 20 -o output.png
+```
+
+The trigger word defaults to the file name without its extension. For
+`embeddings/easynegative.safetensors` the trigger word is `easynegative`.
+
+Run with `-v` to list the discovered embeddings at startup:
+
+```text
+embedding_dir: "/path/to/embeddings",
+embeddings: {
+  "easynegative": "/path/to/embeddings/easynegative.safetensors",
+  ...
+}
+```
+
+The same `--embd-dir` startup option applies to `sd-server`; prompts sent to
+the server resolve the same way.
+
+## Using embeddings in prompts
+
+The CLI and the server both accept the two forms below in `prompt` and
+`negative_prompt`. Prompt weights, token chunks and SDXL dual-CLIP
+zero-padding still apply.
+
+### 1. Bare trigger word via `--embd-dir`
+
+Type the trigger word like any other word:
+
+```text
+easynegative, girl playing the guitar
+```
+
+Before generation the prompt is scanned for `--embd-dir` file stems.
+Matching is case-insensitive, longest-stem-first and word-boundary aware, and
+existing `<...>` tags are left untouched. A match is rewritten to an explicit
+`<embedding:...>` tag with the stored file path, so the vectors replace token
+embeddings before CLIP adds positional embeddings.
+
+Normal prompt weighting applies, so `(easynegative:1.2)` boosts it and
+`[easynegative]` weakens it.
+
+### 2. Explicit `<embedding:...>` tag
+
+Reference a file directly by path:
+
+```text
+a lovely cat, <embedding:embeddings/badhandV4.pt>
+```
+
+Rules:
+
+- The content is a file path, not a trigger word. Paths resolve from the
+  process working directory and are canonicalized before loading. The server
+  also requires the path to pass its model-directory allowlist.
+- `<embedding:...>` follows the same path and escaping rules as
+  `<image:...>`. Use `\<embedding:...>` for literal text. A bare filename
+  without `--embd-dir` is ordinary text in DGL `encode_prompt`; use the tag.
+- The tag requires an SD 1.x, SD 2.x or SDXL CLIP encoder. Other encoders
+  reject it, and a missing file fails generation.
+- A path may not contain `>`.
+- Replacing the file invalidates the cached condition without rebuilding the
+  text encoder.
+
+Weight follows the normal attention syntax around the tag:
+
+```text
+a lovely cat, (<embedding:embeddings/badhandV4.pt>:0.8)
+```
+
+### DGL
+
+```dgl
+text = load_text_encoder(model_path)
+negative = encode_prompt(text, "<embedding:embeddings/badhandV4.pt>, blurry")
+```
+
+No tokenizer aliases or loader `embeddings` argument are used.
+
+## Troubleshooting
+
+- `cannot inspect textual inversion '...'`: the path is wrong, the file is
+  unreadable, or file access (`SD_DGL_FILE_READ_MODEL` and the server
+  allowlist) rejected it.
+- No visible effect: make sure the checkpoint is SD 1.x, SD 2.x or SDXL.
+  Other models reject embedding markers.
+- Bare word does nothing: the file is not directly under `--embd-dir`, has an
+  unsupported extension, collided with another stem, or was split by prompt
+  weighting punctuation. Check the verbose startup listing for the exact key,
+  and prefer unique stems.
diff --git a/docs/models/sd.md b/docs/models/sd.md
index ae0fded..e453127 100644
--- a/docs/models/sd.md
+++ b/docs/models/sd.md
@@ -3,6 +3,8 @@
 Stable Diffusion 1.x, 2.x, and SDXL support text-to-image and image-to-image
 generation from single-file checkpoints.

+See [Textual Inversion Embeddings](../embeddings.md) for `--embd-dir` and `<embedding:...>` usage with SD 1.x/2.x/SDXL.
+
 ## Model files

 | Component | Weights and downloads | CLI option | Requirement |
diff --git a/examples/common/common.cpp b/examples/common/common.cpp
index 7c37fdd..c66502f 100644
--- a/examples/common/common.cpp
+++ b/examples/common/common.cpp
@@ -485,7 +485,7 @@ ArgOptions SDContextParams::get_options() {
          &control_net_path},
         {"",
          "--embd-dir",
-         "embeddings directory",
+         "textual inversion embeddings directory (SD 1.x/2.x/SDXL only, see docs/embeddings.md)",
          0,
          &embedding_dir},
         {"",
diff --git a/examples/server/README.md b/examples/server/README.md
index b56a1b6..a42f2d0 100644
--- a/examples/server/README.md
+++ b/examples/server/README.md
@@ -383,7 +383,7 @@ steps/CFG for the chosen weights.
 Metadata inspection, conversion, standalone upscaling and ADetailer are CLI
 workflows rather than model-loading recipes. ESRGAN and LTX latent upscalers
 are selected through generation upscaling settings with `--hires-upscalers-dir`;
-textual inversion uses the startup `--embd-dir` option.
+textual inversion uses the startup `--embd-dir` option (see `../../docs/embeddings.md` for trigger words and `<embedding:...>` tags).

 SVD is not available through the current server pipeline.