(
model,
prompt: List[str],
controller,
num_inference_steps: int = 50,
guidance_scale: Optional[float] = 7.,
generator: Optional[torch.Generator] = None,
latent: Optional[torch.FloatTensor] = None,
)
| 96 | |
| 97 | @torch.no_grad() |
| 98 | def text2image_ldm( |
| 99 | model, |
| 100 | prompt: List[str], |
| 101 | controller, |
| 102 | num_inference_steps: int = 50, |
| 103 | guidance_scale: Optional[float] = 7., |
| 104 | generator: Optional[torch.Generator] = None, |
| 105 | latent: Optional[torch.FloatTensor] = None, |
| 106 | ): |
| 107 | register_attention_control(model, controller) |
| 108 | height = width = 256 |
| 109 | batch_size = len(prompt) |
| 110 | |
| 111 | uncond_input = model.tokenizer([""] * batch_size, padding="max_length", max_length=77, return_tensors="pt") |
| 112 | uncond_embeddings = model.bert(uncond_input.input_ids.to(model.device))[0] |
| 113 | |
| 114 | text_input = model.tokenizer(prompt, padding="max_length", max_length=77, return_tensors="pt") |
| 115 | text_embeddings = model.bert(text_input.input_ids.to(model.device))[0] |
| 116 | latent, latents = init_latent(latent, model, height, width, generator, batch_size) |
| 117 | context = torch.cat([uncond_embeddings, text_embeddings]) |
| 118 | |
| 119 | model.scheduler.set_timesteps(num_inference_steps) |
| 120 | for t in tqdm(model.scheduler.timesteps): |
| 121 | latents = diffusion_step(model, controller, latents, context, t, guidance_scale) |
| 122 | |
| 123 | image = latent2image(model.vqvae, latents) |
| 124 | |
| 125 | return image, latent |
| 126 | |
| 127 | |
| 128 | @torch.no_grad() |
nothing calls this directly
no test coverage detected