MCPcopy Create free account
hub / github.com/Monalissaa/DisenDiff / text2image_ldm

Function text2image_ldm

src/ptp_utils.py:98–125  ·  view source on GitHub ↗
(
    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,
)

Source from the content-addressed store, hash-verified

96
97@torch.no_grad()
98def 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()

Callers

nothing calls this directly

Calls 4

init_latentFunction · 0.85
diffusion_stepFunction · 0.85
latent2imageFunction · 0.85

Tested by

no test coverage detected