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

Function text2image_ldm_stable

src/ptp_utils.py:129–171  ·  view source on GitHub ↗
(
    model,
    prompt: List[str],
    controller,
    num_inference_steps: int = 50,
    guidance_scale: float = 7.5,
    generator: Optional[torch.Generator] = None,
    latent: Optional[torch.FloatTensor] = None,
    low_resource: bool = False,
)

Source from the content-addressed store, hash-verified

127
128@torch.no_grad()
129def text2image_ldm_stable(
130 model,
131 prompt: List[str],
132 controller,
133 num_inference_steps: int = 50,
134 guidance_scale: float = 7.5,
135 generator: Optional[torch.Generator] = None,
136 latent: Optional[torch.FloatTensor] = None,
137 low_resource: bool = False,
138):
139 register_attention_control(model, controller)
140 height = width = 512
141 batch_size = len(prompt)
142
143 text_input = model.tokenizer(
144 prompt,
145 padding="max_length",
146 max_length=model.tokenizer.model_max_length,
147 truncation=True,
148 return_tensors="pt",
149 )
150 text_embeddings = model.text_encoder(text_input.input_ids.to(model.device))[0]
151 max_length = text_input.input_ids.shape[-1]
152 uncond_input = model.tokenizer(
153 [""] * batch_size, padding="max_length", max_length=max_length, return_tensors="pt"
154 )
155 uncond_embeddings = model.text_encoder(uncond_input.input_ids.to(model.device))[0]
156
157 context = [uncond_embeddings, text_embeddings]
158 if not low_resource:
159 context = torch.cat(context)
160 latent, latents = init_latent(latent, model, height, width, generator, batch_size)
161
162 # set timesteps
163 # extra_set_kwargs = {"offset": 1}
164 # model.scheduler.set_timesteps(num_inference_steps, **extra_set_kwargs)
165 model.scheduler.set_timesteps(num_inference_steps)
166 for t in tqdm(model.scheduler.timesteps):
167 latents = diffusion_step(model, controller, latents, context, t, guidance_scale, low_resource)
168
169 image = latent2image(model.vae, latents)
170
171 return image, latent
172
173
174def register_attention_control(model, controller):

Callers

nothing calls this directly

Calls 4

init_latentFunction · 0.85
diffusion_stepFunction · 0.85
latent2imageFunction · 0.85

Tested by

no test coverage detected