jptv/LLM-grounded-diffusion
0
1# Original Stable Diffusion (1.4)2 3import torch4import models5from models import pipelines6from shared import model_dict, DEFAULT_OVERALL_NEGATIVE_PROMPT7import gc8 9vae, tokenizer, text_encoder, unet, scheduler, dtype = model_dict.vae, model_dict.tokenizer, model_dict.text_encoder, model_dict.unet, model_dict.scheduler, model_dict.dtype10 11torch.set_grad_enabled(False)12 13height = 512 # default height of Stable Diffusion14width = 512 # default width of Stable Diffusion15guidance_scale = 7.5 # Scale for classifier-free guidance16batch_size = 117 18# h, w19image_scale = (512, 512)20 21bg_negative = DEFAULT_OVERALL_NEGATIVE_PROMPT22 23# Using dpm scheduler by default24def run(prompt, scheduler_key='dpm_scheduler', bg_seed=1, num_inference_steps=20):25 print(f"prompt: {prompt}")26 generator = torch.manual_seed(bg_seed)27 28 prompts = [prompt]29 input_embeddings = models.encode_prompts(prompts=prompts, tokenizer=tokenizer, text_encoder=text_encoder, negative_prompt=bg_negative)30 31 latents = models.get_unscaled_latents(batch_size, unet.config.in_channels, height, width, generator, dtype)32 33 latents = latents * scheduler.init_noise_sigma34 35 pipelines.gligen_enable_fuser(model_dict['unet'], enabled=False)36 _, images = pipelines.generate(37 model_dict, latents, input_embeddings, num_inference_steps, 38 guidance_scale=guidance_scale, scheduler_key=scheduler_key39 )40 41 gc.collect()42 torch.cuda.empty_cache()43 44 return images[0]