| import torch |
| from diffusers import StableDiffusion3Pipeline |
|
|
| |
| pipe = StableDiffusion3Pipeline.from_pretrained( |
| "stabilityai/stable-diffusion-3-medium-diffusers", |
| torch_dtype=torch.float16 |
| ) |
|
|
| |
| |
| MMDIT_MODEL = pipe.components["unet"] |
|
|
| pipe.to("cuda") |
|
|
| |
| prompt = "A majestic castle on a floating island, photorealistic, 4k" |
| negative_prompt = "" |
|
|
| |
| guidance_scale = 7.0 |
| num_inference_steps = 20 |
| pipe.scheduler.set_timesteps(num_inference_steps, device=pipe.device) |
|
|
| |
| prompt_embeds, negative_prompt_embeds, pooled_prompt_embeds, negative_pooled_prompt_embeds = pipe.encode_prompt( |
| prompt=prompt, |
| prompt_2=prompt, |
| prompt_3=prompt, |
| negative_prompt=negative_prompt, |
| negative_prompt_2=negative_prompt, |
| negative_prompt_3=negative_prompt, |
| device=pipe.device, |
| do_classifier_free_guidance=True, |
| ) |
|
|
| |
| prompt_embeds = torch.cat([negative_prompt_embeds, prompt_embeds], dim=0) |
| pooled_prompt_embeds = torch.cat([negative_pooled_prompt_embeds, pooled_prompt_embeds], dim=0) |
|
|
| |
|
|
| |
| LATENT_CHANNELS = 16 |
| latents = torch.randn( |
| (1, LATENT_CHANNELS, 1024 // 8, 1024 // 8), |
| generator=None, |
| device=pipe.device, |
| dtype=pipe.dtype, |
| ) |
|
|
| print("开始定制采样循环...") |
| for i, t in enumerate(pipe.scheduler.timesteps): |
| |
| latent_model_input = torch.cat([latents] * 2) |
| |
| |
| |
| model_output = MMDIT_MODEL( |
| latent_model_input, |
| t, |
| encoder_hidden_states=prompt_embeds, |
| pooled_projections=pooled_prompt_embeds, |
| return_dict=False |
| )[0] |
|
|
| |
| v_uncond, v_cond = model_output.chunk(2) |
| |
| |
| v_guided = v_cond |
|
|
| |
| latents = pipe.scheduler.step(v_guided, t, latents, return_dict=False)[0] |
|
|
| |
| print("解码中...") |
| latents = 1 / pipe.vae.config.scaling_factor * latents |
| image = pipe.vae.decode(latents, return_dict=False)[0] |
| image = pipe.image_processor.postprocess(image.detach().cpu().float(), output_type="pil") |
| image.save("sd3_conditional_only_ode_result.png") |
|
|
| print("采样完成,结果已保存至 sd3_conditional_only_ode_result.png") |