wilsonHuggingFace commited on
Commit
27053ae
·
verified ·
1 Parent(s): a778715

Update SDXL_Lighting.py

Browse files
Files changed (1) hide show
  1. SDXL_Lighting.py +26 -17
SDXL_Lighting.py CHANGED
@@ -1,3 +1,10 @@
 
 
 
 
 
 
 
1
  import torch
2
  from diffusers import StableDiffusionXLPipeline, UNet2DConditionModel, EulerDiscreteScheduler
3
  from huggingface_hub import hf_hub_download
@@ -7,24 +14,26 @@ base = "stabilityai/stable-diffusion-xl-base-1.0"
7
  repo = "ByteDance/SDXL-Lightning"
8
  ckpt = "sdxl_lightning_4step_unet.safetensors" # Use the correct ckpt for your step setting!
9
 
10
- ## download
11
- # git clone https://hf-mirror.com/stabilityai/stable-diffusion-xl-base-1.0
12
- # wget -c hf-mirror.com/stabilityai/stable-diffusion-xl-base-1.0/resolve/main/unet/diffusion_pytorch_model.fp16.safetensors
13
- # wget -c https://hf-mirror.com/ByteDance/SDXL-Lightning/resolve/main/sdxl_lightning_4step_unet.safetensors
14
- # base = xxx/stable-diffusion-xl-base-1.0
15
- # ckpt = SDXL-Lightning/sdxl_lightning_4step_unet.safetensors
16
 
17
- # Load model.
18
- # unet = UNet2DConditionModel.from_config(base, subfolder="unet").to("cuda", torch.float16)
19
- # unet.load_state_dict(load_file(hf_hub_download(repo, ckpt), device="cuda"))
20
- # pipe = StableDiffusionXLPipeline.from_pretrained(base, unet=unet, torch_dtype=torch.float16, variant="fp16").to("cuda")
 
 
 
 
 
21
 
22
- unet = UNet2DConditionModel.from_config(base, subfolder="unet").to("cuda", torch.float16)
23
- unet.load_state_dict(load_file(ckpt), device="cuda"))
24
- pipe = StableDiffusionXLPipeline.from_pretrained(base, unet=unet, torch_dtype=torch.float16, variant="fp16").to("cuda")
25
 
26
- # Ensure sampler uses "trailing" timesteps.
27
- pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config, timestep_spacing="trailing")
 
28
 
29
- # Ensure using the same inference steps as the loaded model and CFG set to 0.
30
- pipe("A girl smiling", num_inference_steps=4, guidance_scale=0).images[0].save("output.png")
 
 
 
1
+ ## download
2
+ # git clone https://hf-mirror.com/stabilityai/stable-diffusion-xl-base-1.0
3
+ # wget -c hf-mirror.com/stabilityai/stable-diffusion-xl-base-1.0/resolve/main/unet/diffusion_pytorch_model.fp16.safetensors
4
+ # wget -c https://hf-mirror.com/ByteDance/SDXL-Lightning/resolve/main/sdxl_lightning_4step_unet.safetensors
5
+ # base = xxx/stable-diffusion-xl-base-1.0
6
+ # ckpt = SDXL-Lightning/sdxl_lightning_4step_unet.safetensors
7
+
8
  import torch
9
  from diffusers import StableDiffusionXLPipeline, UNet2DConditionModel, EulerDiscreteScheduler
10
  from huggingface_hub import hf_hub_download
 
14
  repo = "ByteDance/SDXL-Lightning"
15
  ckpt = "sdxl_lightning_4step_unet.safetensors" # Use the correct ckpt for your step setting!
16
 
17
+ def t2i(prompt):
 
 
 
 
 
18
 
19
+ # Load model.
20
+
21
+ # unet = UNet2DConditionModel.from_config(base, subfolder="unet").to("cuda", torch.float16)
22
+ # unet.load_state_dict(load_file(hf_hub_download(repo, ckpt), device="cuda"))
23
+ # pipe = StableDiffusionXLPipeline.from_pretrained(base, unet=unet, torch_dtype=torch.float16, variant="fp16").to("cuda")
24
+
25
+ unet = UNet2DConditionModel.from_config(base, subfolder="unet").to("cuda", torch.float16)
26
+ unet.load_state_dict(load_file(ckpt), device="cuda"))
27
+ pipe = StableDiffusionXLPipeline.from_pretrained(base, unet=unet, torch_dtype=torch.float16, variant="fp16").to("cuda")
28
 
29
+ # Ensure sampler uses "trailing" timesteps.
30
+ pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config, timestep_spacing="trailing")
 
31
 
32
+ # Ensure using the same inference steps as the loaded model and CFG set to 0.
33
+ image = pipe(prompt, num_inference_steps=4, guidance_scale=0).images[0]
34
+ return image
35
 
36
+ if __name__ == "__main__":
37
+ prompt = "a girl in beijing"
38
+ image = t2i(prompt)
39
+ image.save("sdxl_lighting_output.png")