Compel is an active Python library, currently at version 2.3.1, designed to enhance prompting for transformers-type text embedding systems. It provides a flexible and intuitive syntax for sophisticated prompt weighting, blending, and concatenation, commonly used with Hugging Face `diffusers` pipelines. The library aims to give users granular control over how text encoders interpret complex prompt strings, and it maintains a regular release cadence with ongoing development.
pip install compelVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to use `CompelForSDXL` with a Hugging Face `diffusers` pipeline to apply weighting to both positive and negative prompts. It showcases the `++` and explicit number weighting syntax, and how to retrieve and pass the generated conditioning tensors (embeds and pooled_embeds) to the SDXL pipeline for image generation. Ensure you have `diffusers` and `torch` installed and a suitable Hugging Face model loaded.
Update `Compel` initialization to use `returned_embeddings_type=ReturnedEmbeddingsType.PENULTIMATE_HIDDEN_STATES_NON_NORMALIZED` for SDXL, or `ReturnedEmbeddingsType.LAST_HIDDEN_STATES_NORMALIZED` for SD<=2.1.
If you rely on the legacy downweighting behavior (token removal), initialize `Compel` with `downweight_mode=DownweightMode.REMOVE`. Otherwise, no change is needed as masking is the default and recommended.
Initialize `Compel` or `CompelForSDXL` with `device='cuda'` (or your desired GPU device) explicitly: `compel = CompelForSDXL(pipeline, device='cuda')`.
Always pass both positive and negative conditioning tensors through `compel.pad_conditioning_tensors_to_same_length([positive_embeds, negative_embeds])` before passing them to the diffusion pipeline.
Wrap your `compel` calls and subsequent pipeline inference within `with torch.no_grad():`.
For SDXL, ensure `CompelForSDXL` is correctly initialized with both tokenizers and text encoders (`compel = CompelForSDXL(pipeline)`). If the issue persists with special characters like '!', a workaround is to initialize `Compel` with a duplicated tokenizer for both: `compel = Compel(tokenizer=[pipeline.tokenizer, pipeline.tokenizer], text_encoder=[pipeline.text_encoder, pipeline.text_encoder_2], ...)` as a temporary fix, along with `truncate_long_prompts=False` and `pad_conditioning_tensors_to_same_length()`.
If you intend to use long prompts, ensure `truncate_long_prompts=False` is set in `Compel` initialization and always use `compel.pad_conditioning_tensors_to_same_length()` for all conditioning tensors. If truncation is desired, ensure `truncate_long_prompts=True` (which is the default behavior in `Compel`).