An unconditional prompt is an empty text input representing the absence of a specific condition or descriptive information. In classifier-free guidance, it supplies a baseline alongside the conditional prompt. section 9.3.1 section 10.3
Use the unconditional prompt through this sequence:
- Set the prompt to an empty string, such as `uncond_prompt = ""`. section 9.3.1
- Tokenize it with the same tokenizer and fixed sequence length as the conditional prompt; padding fills the remaining positions. section 9.3.1 section 12.2.1
- Pass the tokens through the text encoder to create an unconditional text embedding. section 10.3
- Combine the unconditional and conditional predictions or embeddings using the CFG parameter to steer generation toward the supplied description. section 9.3.1 section 10.3
Image-generation example
For the conditional prompt “a banana riding a motorcycle, wearing sunglasses and a straw hat,” use `uncond_prompt = ""`. Encode both inputs, then use classifier-free guidance to move generation toward the described image while retaining an unconditional baseline. section 9.3.1
Listing 9.5 Tokenizing and encoding prompts
prompt = '''A banana riding a motorcycle, wearing sunglasses and a straw hat, highly detailed, ultra sharp, cinematic, 100mm lens, 8k resolution.''' uncond_prompt="" seed=42 with torch.no_grad(): generator=torch.Generator(device=device) generator.manual_seed(seed) cond_tokens=tokenizer.batch_encode_plus( [prompt], padding="max_length", max_length=77).input_ids cond_tokens=torch.tensor(cond_tokens, dtype=torch.long,device=device) #1 print("conditional tokens are\n", cond_tokens) cond_context = clip(cond_tokens).to(device) #2 uncond_tokens=tokenizer.batch_encode_plus( [uncond_prompt], padding="max_length", max_length=77).input_ids uncond_tokens=torch.tensor(uncond_tokens, dtype=torch.long,device=device) print("unconditional tokens are\n", uncond_tokens) uncond_context = clip(uncond_tokens).to(device) #3 clip.to("cpu") context=torch.cat([cond_context,uncond_context]) #4
Inspect the tokenization and encoding flow for the conditional prompt and the empty unconditional prompt. section 9.3.1
