radames commited on
Commit
e85a61d
1 Parent(s): cac33a7
Files changed (3) hide show
  1. README.md +1 -1
  2. app.py +38 -33
  3. requirements.txt +3 -2
README.md CHANGED
@@ -1,6 +1,6 @@
1
  ---
2
  title: Real-Time Text-to-Image SDXL Lightning
3
- emoji: 🏆
4
  colorFrom: yellow
5
  colorTo: purple
6
  sdk: gradio
 
1
  ---
2
  title: Real-Time Text-to-Image SDXL Lightning
3
+ emoji: ⚡️⚡️⚡️⚡️
4
  colorFrom: yellow
5
  colorTo: purple
6
  sdk: gradio
app.py CHANGED
@@ -1,18 +1,18 @@
1
- from diffusers import StableDiffusionXLPipeline, EulerDiscreteScheduler
 
 
 
 
2
  import torch
3
  import os
4
  from huggingface_hub import hf_hub_download
5
 
6
- try:
7
- import intel_extension_for_pytorch as ipex
8
- except:
9
- pass
10
 
11
  from PIL import Image
12
  import gradio as gr
13
  import time
14
  from safetensors.torch import load_file
15
-
16
 
17
  # Constants
18
  BASE = "stabilityai/stable-diffusion-xl-base-1.0"
@@ -30,37 +30,45 @@ CHECKPOINT = "sdxl_lightning_1step_unet_x0.safetensors"
30
 
31
  TORCH_COMPILE = os.environ.get("TORCH_COMPILE", "0") == "1"
32
  # check if MPS is available OSX only M1/M2/M3 chips
33
- mps_available = hasattr(torch.backends, "mps") and torch.backends.mps.is_available()
34
- xpu_available = hasattr(torch, "xpu") and torch.xpu.is_available()
35
- device = torch.device(
36
- "cuda" if torch.cuda.is_available() else "xpu" if xpu_available else "cpu"
37
- )
38
  torch_device = device
39
  torch_dtype = torch.float16
40
 
41
  print(f"TORCH_COMPILE: {TORCH_COMPILE}")
42
  print(f"device: {device}")
43
 
44
- if mps_available:
45
- device = torch.device("mps")
46
- torch_device = "cpu"
47
- torch_dtype = torch.float32
48
-
49
-
50
  pipe = StableDiffusionXLPipeline.from_pretrained(
51
- BASE, torch_dtype=torch.float16, variant="fp16"
52
- )
53
 
 
54
  pipe.scheduler = EulerDiscreteScheduler.from_config(
55
  pipe.scheduler.config, timestep_spacing="trailing", prediction_type="sample"
56
  )
 
 
 
 
57
 
58
- pipe.unet.load_state_dict(
59
- torch.load(load_file(hf_hub_download(REPO, CHECKPOINT)), map_location="cuda")
60
- )
 
 
61
 
62
- pipe.to(device=torch_device, dtype=torch_dtype).to(device)
63
- pipe.set_progress_bar_config(disable=True)
 
 
 
 
 
 
 
64
 
65
 
66
  def predict(prompt, seed=1231231):
@@ -71,8 +79,6 @@ def predict(prompt, seed=1231231):
71
  generator=generator,
72
  num_inference_steps=1,
73
  guidance_scale=0.0,
74
- width=512,
75
- height=512,
76
  # original_inference_steps=params.lcm_steps,
77
  output_type="pil",
78
  )
@@ -102,10 +108,10 @@ css = """
102
  with gr.Blocks(css=css) as demo:
103
  with gr.Column(elem_id="container"):
104
  gr.Markdown(
105
- """# SDXL Turbo - Text To Image
106
  ## Unofficial Demo
107
- SDXL Turbo model can generate high quality images in a single pass read more on [stability.ai post](https://stability.ai/news/stability-ai-sdxl-turbo).
108
- **Model**: https://huggingface.co/stabilityai/sdxl-turbo
109
  """,
110
  elem_id="intro",
111
  )
@@ -123,8 +129,8 @@ with gr.Blocks(css=css) as demo:
123
  )
124
  with gr.Accordion("Run with diffusers"):
125
  gr.Markdown(
126
- """## Running SDXL Turbo with `diffusers`
127
- ```py
128
  import torch
129
  from diffusers import (
130
  StableDiffusionXLPipeline,
@@ -156,8 +162,7 @@ pipe.scheduler = EulerDiscreteScheduler.from_config(
156
  pipe("A girl smiling", num_inference_steps=1, guidance_scale=0).images[0].save(
157
  "output.png"
158
  )
159
-
160
- ```
161
  """
162
  )
163
 
 
1
+ from diffusers import (
2
+ StableDiffusionXLPipeline,
3
+ EulerDiscreteScheduler,
4
+ UNet2DConditionModel,
5
+ )
6
  import torch
7
  import os
8
  from huggingface_hub import hf_hub_download
9
 
 
 
 
 
10
 
11
  from PIL import Image
12
  import gradio as gr
13
  import time
14
  from safetensors.torch import load_file
15
+ from sfast.compilers.diffusion_pipeline_compiler import compile, CompilationConfig
16
 
17
  # Constants
18
  BASE = "stabilityai/stable-diffusion-xl-base-1.0"
 
30
 
31
  TORCH_COMPILE = os.environ.get("TORCH_COMPILE", "0") == "1"
32
  # check if MPS is available OSX only M1/M2/M3 chips
33
+
34
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 
 
 
35
  torch_device = device
36
  torch_dtype = torch.float16
37
 
38
  print(f"TORCH_COMPILE: {TORCH_COMPILE}")
39
  print(f"device: {device}")
40
 
41
+ # Load model.
42
+ unet = UNet2DConditionModel.from_config(BASE, subfolder="unet").to(device, torch_dtype)
43
+ unet.load_state_dict(load_file(hf_hub_download(REPO, CHECKPOINT), device="cuda"))
 
 
 
44
  pipe = StableDiffusionXLPipeline.from_pretrained(
45
+ BASE, unet=unet, torch_dtype=torch_dtype, variant="fp16"
46
+ ).to(device)
47
 
48
+ # Ensure sampler uses "trailing" timesteps and "sample" prediction type.
49
  pipe.scheduler = EulerDiscreteScheduler.from_config(
50
  pipe.scheduler.config, timestep_spacing="trailing", prediction_type="sample"
51
  )
52
+ pipe.set_progress_bar_config(disable=True)
53
+ config = CompilationConfig.Default()
54
+ try:
55
+ import xformers
56
 
57
+ config.enable_xformers = True
58
+ except ImportError:
59
+ print("xformers not installed, skip")
60
+ try:
61
+ import triton
62
 
63
+ config.enable_triton = True
64
+ except ImportError:
65
+ print("Triton not installed, skip")
66
+ # CUDA Graph is suggested for small batch sizes and small resolutions to reduce CPU overhead.
67
+ # But it can increase the amount of GPU memory used.
68
+ # For StableVideoDiffusionPipeline it is not needed.
69
+ config.enable_cuda_graph = True
70
+
71
+ pipe = compile(pipe, config)
72
 
73
 
74
  def predict(prompt, seed=1231231):
 
79
  generator=generator,
80
  num_inference_steps=1,
81
  guidance_scale=0.0,
 
 
82
  # original_inference_steps=params.lcm_steps,
83
  output_type="pil",
84
  )
 
108
  with gr.Blocks(css=css) as demo:
109
  with gr.Column(elem_id="container"):
110
  gr.Markdown(
111
+ """# SDXL-Lightning- Text To Image 1-Step
112
  ## Unofficial Demo
113
+ SDXL-Lightining https://huggingface.co/ByteDance/SDXL-Lightning
114
+ **Model**: https://huggingface.co/ByteDance/SDXL-Lightning
115
  """,
116
  elem_id="intro",
117
  )
 
129
  )
130
  with gr.Accordion("Run with diffusers"):
131
  gr.Markdown(
132
+ """## Running SDXL-Lightning with `diffusers`
133
+ ```py
134
  import torch
135
  from diffusers import (
136
  StableDiffusionXLPipeline,
 
162
  pipe("A girl smiling", num_inference_steps=1, guidance_scale=0).images[0].save(
163
  "output.png"
164
  )
165
+ ```
 
166
  """
167
  )
168
 
requirements.txt CHANGED
@@ -1,7 +1,7 @@
1
  diffusers==0.26.3
2
  transformers
3
  gradio==4.19.1
4
- torch==2.2.0
5
  fastapi==0.104.0
6
  uvicorn==0.23.2
7
  Pillow==10.1.0
@@ -12,4 +12,5 @@ peft==0.6.0
12
  xformers
13
  hf_transfer
14
  huggingface_hub
15
- safetensors
 
 
1
  diffusers==0.26.3
2
  transformers
3
  gradio==4.19.1
4
+ torch==2.1.0
5
  fastapi==0.104.0
6
  uvicorn==0.23.2
7
  Pillow==10.1.0
 
12
  xformers
13
  hf_transfer
14
  huggingface_hub
15
+ safetensors
16
+ stable_fast @ https://github.com/chengzeyi/stable-fast/releases/download/v1.0.2/stable_fast-1.0.2+torch211cu121-cp310-cp310-manylinux2014_x86_64.whl; sys_platform != 'darwin' or platform_machine != 'arm64'