Below is the minimal pattern that most people use when “cross-fading” two prompts with the SDXL/flux.1-dev text-to-image pipeline in 🤗 diffusers.
Everything that follows assumes you already have a working FluxStableDiffusionPipeline (or the current StableDiffusionXLPipeline) on GPU and you know your way around the library.
import torch
from diffusers import FluxStableDiffusionPipeline # or StableDiffusionXLPipeline
from diffusers.utils.torch_utils import randn_tensor
pipe = FluxStableDiffusionPipeline.from_pretrained(
"stabilityai/flux.1-dev",
torch_dtype=torch.float16
).to("cuda")
pipe.enable_xformers_memory_efficient_attention() # optional
pipe.scheduler.set_timesteps(40) # whatever you like
height, width = 768, 768
batch = 1
────────────────────────────────────────────────────────
- Encode each prompt once (both text encoders are handled for you)
────────────────────────────────────────────────────────
prompt_a = "a hyper-realistic close-up photograph of a cat in a tuxedo, studio light"
prompt_b = "an impressionist oil painting of New York skyline at dusk, thick brush strokes"
# encode_prompt returns:
# prompt_embeds – token-level embeddings (text-enc1 ⊕ text-enc2) (B, 77, 2304)
# negative_prompt_embeds – we ignore for now
# pooled_prompt_embeds – pooled CLS embeddings (enc1 ⊕ enc2) (B, 2304)
with torch.no_grad():
e_a, _, p_a = pipe.encode_prompt(prompt_a, device=pipe.device)
e_b, _, p_b = pipe.encode_prompt(prompt_b, device=pipe.device)
• The helper already runs both SDXL text encoders and concatenates their outputs, so you do not have to worry about the “two encoders” detail.
────────────────────────────────────────────────────────
2. Prepare the latent noise for both prompts
────────────────────────────────────────────────────────
flux.1-dev uses the same latent shape as SDXL (C = 4, spatial = H / 8).
Using the scheduler’s own init_noise_sigma keeps the variance correct.
shape = (batch, pipe.unet.config.in_channels, height // 8, width // 8)
rng = torch.Generator(device="cuda").manual_seed(42)
noise_a = randn_tensor(shape, generator=rng, device="cuda")
rng = torch.Generator(device="cuda").manual_seed(666)
noise_b = randn_tensor(shape, generator=rng, device="cuda")
────────────────────────────────────────────────────────
3. Interpolate (LERP or SLERP) embeddings and noise
────────────────────────────────────────────────────────
Linear blend is good enough for embeddings; for noise a SLERP keeps the norm stable:
def slerp(alpha, x0, x1):
# x0, x1 (…, N)
dot = (x0 * x1).sum(dim=-1, keepdim=True)
omega = torch.arccos(torch.clamp(dot, -1., 1.))
so = torch.sin(omega)
return (torch.sin((1.0 - alpha) * omega) / so) * x0 + (torch.sin(alpha * omega) / so) * x1
alpha = 0.35 # 0 = prompt_a / 1 = prompt_b
latents = slerp(alpha, noise_a, noise_b) * pipe.scheduler.init_noise_sigma
embeds = (1 - alpha) * e_a + alpha * e_b # (batch, 77, 2304)
p_embeds = (1 - alpha) * p_a + alpha * p_b # (batch, 2304)
────────────────────────────────────────────────────────
4. Run the denoising loop with custom conditioning
────────────────────────────────────────────────────────
We bypass the internal tokenizer by feeding the things we just built.
If you still want CFG (classifier-free guidance) give the negative counterparts; the quickest hack is to reuse the interpolated embeddings and zero them out:
neg_embeds = torch.zeros_like(embeds)
neg_pooled = torch.zeros_like(p_embeds)
out = pipe(
prompt=None, # skip tokenization
prompt_embeds=embeds,
pooled_prompt_embeds=p_embeds,
negative_prompt_embeds=neg_embeds,
negative_pooled_prompt_embeds=neg_pooled,
latents=latents,
num_inference_steps=40,
guidance_scale=7.5,
output_type="pil"
)
img = out.images[0]
img.save("interpolated.png")
That’s it. The denoiser sees a single latent tensor and a single set of prompt embeddings that sit somewhere on the straight line (or great circle) between A and B, so the resulting image naturally drifts from prompt A to prompt B depending on alpha.
────────────────────────────────────────────────────────
Quick summary / FAQ
────────────────────────────────────────────────────────
• Do I need to touch the two individual text encoders?
→ No. encode_prompt already concatenates hidden states from both encoders (token-level and pooled CLS). Interpolate the concatenated tensors directly.
• Should I blend pooled_prompt_embeds as well?
→ Yes. SDXL/flux uses them inside the UNet through an extra adapter MLP; leaving them un-blended biases the output toward one prompt.
• Why slerp for noise but not for embeddings?
→ token_embeds store semantic direction; preserving vector norm is less critical. Noise is length-sensitive, so slerp avoids contrast loss.
• Can I create a video morph?
→ Generate a list of alphas (e.g. np.linspace(0,1,60)), loop the block above, and stitch the frames.
Happy blending!