patrickvonplaten commited on
Commit
e990e13
1 Parent(s): 941d256

improve sd xl

Browse files
Files changed (7) hide show
  1. load_ckpt.py +57 -0
  2. run_local.py +20 -23
  3. run_local_xl.py +7 -4
  4. run_safety.py +1 -0
  5. run_shape.py +58 -0
  6. run_video.py +5 -2
  7. speed_up_scheduler.py +33 -0
load_ckpt.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ from diffusers import StableDiffusionPipeline, KDPM2DiscreteScheduler, StableDiffusionImg2ImgPipeline, HeunDiscreteScheduler, KDPM2AncestralDiscreteScheduler, DDIMScheduler
3
+ import time
4
+ import os
5
+ from huggingface_hub import HfApi
6
+ # from compel import Compel
7
+ import torch
8
+ import sys
9
+ from pathlib import Path
10
+ import requests
11
+ from PIL import Image
12
+ from io import BytesIO
13
+
14
+ path = sys.argv[1]
15
+
16
+ api = HfApi()
17
+ start_time = time.time()
18
+ pipe = StableDiffusionPipeline.from_ckpt(path, torch_dtype=torch.float16)
19
+ import ipdb; ipdb.set_trace()
20
+
21
+ pipe = pipe.to("cuda")
22
+
23
+ prompt = "A lion in galaxies, spirals, nebulae, stars, smoke, iridescent, intricate detail, octane render, 8k"
24
+
25
+ # rompts = ["a cat playing with a ball++ in the forest", "a cat playing with a ball++ in the forest", "a cat playing with a ball-- in the forest"]
26
+
27
+ # prompt_embeds = torch.cat([compel.build_conditioning_tensor(prompt) for prompt in prompts])
28
+
29
+ # generator = [torch.Generator(device="cuda").manual_seed(0) for _ in range(prompt_embeds.shape[0])]
30
+ #
31
+ # url = "https://raw.githubusercontent.com/CompVis/stable-diffusion/main/assets/stable-samples/img2img/sketch-mountains-input.jpg"
32
+ #
33
+ # response = requests.get(url)
34
+ # image = Image.open(BytesIO(response.content)).convert("RGB")
35
+ # image.thumbnail((768, 768))
36
+ #
37
+
38
+ for TIMESTEP_TYPE in ["trailing", "leading"]:
39
+ for RESCALE_BETAS_ZEROS_SNR in [True, False]:
40
+ for GUIDANCE_RESCALE in [0,0, 0.7]:
41
+
42
+ pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config, timestep_spacing=TIMESTEP_TYPE, rescale_betas_zero_snr=RESCALE_BETAS_ZEROS_SNR)
43
+ generator = torch.Generator(device="cpu").manual_seed(0)
44
+ images = pipe(prompt=prompt, generator=generator, num_images_per_prompt=4, num_inference_steps=40, guidance_rescale=GUIDANCE_RESCALE).images
45
+
46
+ for i, image in enumerate(images):
47
+ file_name = f"bb_{i}_{TIMESTEP_TYPE}_{str(int(RESCALE_BETAS_ZEROS_SNR))}_{GUIDANCE_RESCALE}"
48
+ path = os.path.join(Path.home(), "images", f"{file_name}.png")
49
+ image.save(path)
50
+
51
+ api.upload_file(
52
+ path_or_fileobj=path,
53
+ path_in_repo=path.split("/")[-1],
54
+ repo_id="patrickvonplaten/images",
55
+ repo_type="dataset",
56
+ )
57
+ print(f"https://huggingface.co/datasets/patrickvonplaten/images/blob/main/{file_name}.png")
run_local.py CHANGED
@@ -11,16 +11,17 @@ import requests
11
  from PIL import Image
12
  from io import BytesIO
13
 
14
- # path = sys.argv[1]
15
- path = "runwayml/stable-diffusion-v1-5"
16
- path = "ptx0/pseudo-journey-v2"
17
  # path = "stabilityai/stable-diffusion-2-1"
18
 
19
  api = HfApi()
20
  start_time = time.time()
21
  pipe = StableDiffusionPipeline.from_pretrained(path, torch_dtype=torch.float16)
 
 
 
22
  # pipe = StableDiffusionImg2ImgPipeline.from_pretrained(path, torch_dtype=torch.float16, safety_checker=None)
23
- # pipe.scheduler = KDPM2AncestralDiscreteScheduler.from_config(pipe.scheduler.config)
24
 
25
  # compel = Compel(tokenizer=pipe.tokenizer, text_encoder=pipe.text_encoder)
26
 
@@ -36,29 +37,25 @@ prompt = "A lion in galaxies, spirals, nebulae, stars, smoke, iridescent, intric
36
  # generator = [torch.Generator(device="cuda").manual_seed(0) for _ in range(prompt_embeds.shape[0])]
37
  #
38
  # url = "https://raw.githubusercontent.com/CompVis/stable-diffusion/main/assets/stable-samples/img2img/sketch-mountains-input.jpg"
39
- #
40
  # response = requests.get(url)
41
  # image = Image.open(BytesIO(response.content)).convert("RGB")
42
  # image.thumbnail((768, 768))
43
- #
44
 
45
- for TIMESTEP_TYPE in ["trailing", "leading"]:
46
- for RESCALE_BETAS_ZEROS_SNR in [True, False]:
47
- for GUIDANCE_RESCALE in [0,0, 0.7]:
48
 
49
- pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config, timestep_spacing=TIMESTEP_TYPE, rescale_betas_zero_snr=RESCALE_BETAS_ZEROS_SNR)
50
- generator = torch.Generator(device="cpu").manual_seed(0)
51
- images = pipe(prompt=prompt, generator=generator, num_images_per_prompt=4, num_inference_steps=40, guidance_rescale=GUIDANCE_RESCALE).images
52
 
53
- for i, image in enumerate(images):
54
- file_name = f"bb_{i}_{TIMESTEP_TYPE}_{str(int(RESCALE_BETAS_ZEROS_SNR))}_{GUIDANCE_RESCALE}"
55
- path = os.path.join(Path.home(), "images", f"{file_name}.png")
56
- image.save(path)
57
 
58
- api.upload_file(
59
- path_or_fileobj=path,
60
- path_in_repo=path.split("/")[-1],
61
- repo_id="patrickvonplaten/images",
62
- repo_type="dataset",
63
- )
64
- print(f"https://huggingface.co/datasets/patrickvonplaten/images/blob/main/{file_name}.png")
 
11
  from PIL import Image
12
  from io import BytesIO
13
 
14
+ path = sys.argv[1]
15
+ # path = "ptx0/pseudo-journey-v2"
 
16
  # path = "stabilityai/stable-diffusion-2-1"
17
 
18
  api = HfApi()
19
  start_time = time.time()
20
  pipe = StableDiffusionPipeline.from_pretrained(path, torch_dtype=torch.float16)
21
+
22
+ pipe.unet = torch.compile(pipe.unet)
23
+
24
  # pipe = StableDiffusionImg2ImgPipeline.from_pretrained(path, torch_dtype=torch.float16, safety_checker=None)
 
25
 
26
  # compel = Compel(tokenizer=pipe.tokenizer, text_encoder=pipe.text_encoder)
27
 
 
37
  # generator = [torch.Generator(device="cuda").manual_seed(0) for _ in range(prompt_embeds.shape[0])]
38
  #
39
  # url = "https://raw.githubusercontent.com/CompVis/stable-diffusion/main/assets/stable-samples/img2img/sketch-mountains-input.jpg"
40
+
41
  # response = requests.get(url)
42
  # image = Image.open(BytesIO(response.content)).convert("RGB")
43
  # image.thumbnail((768, 768))
 
44
 
 
 
 
45
 
46
+ generator = torch.Generator(device="cpu").manual_seed(0)
47
+ # images = pipe(prompt=prompt, image=image, generator=generator, num_images_per_prompt=4, num_inference_steps=25).images
48
+ images = pipe(prompt=prompt, generator=generator, num_images_per_prompt=4, num_inference_steps=25).images
49
 
50
+ for i, image in enumerate(images):
51
+ file_name = f"bb_1_{i}"
52
+ path = os.path.join(Path.home(), "images", f"{file_name}.png")
53
+ image.save(path)
54
 
55
+ api.upload_file(
56
+ path_or_fileobj=path,
57
+ path_in_repo=path.split("/")[-1],
58
+ repo_id="patrickvonplaten/images",
59
+ repo_type="dataset",
60
+ )
61
+ print(f"https://huggingface.co/datasets/patrickvonplaten/images/blob/main/{file_name}.png")
run_local_xl.py CHANGED
@@ -1,5 +1,6 @@
1
  #!/usr/bin/env python3
2
  from diffusers import DiffusionPipeline, EulerDiscreteScheduler, StableDiffusionPipeline, KDPM2DiscreteScheduler, StableDiffusionImg2ImgPipeline, HeunDiscreteScheduler, KDPM2AncestralDiscreteScheduler, DDIMScheduler
 
3
  import time
4
  from pytorch_lightning import seed_everything
5
  import os
@@ -17,20 +18,22 @@ start_time = time.time()
17
 
18
  use_refiner = bool(int(sys.argv[1]))
19
 
20
- pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-0.9", torch_dtype=torch.float16, variant="fp16", use_safetensors=True)
 
21
  pipe.to("cuda")
22
  # pipe.enable_model_cpu_offload()
23
 
24
  if use_refiner:
25
- refiner = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-refiner-0.9", torch_dtype=torch.float16)
26
- refiner.enable_model_cpu_offload()
27
 
28
  prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k"
29
  seed_everything(0)
30
  image = pipe(prompt=prompt, output_type="latent" if use_refiner else "pil").images[0]
 
31
 
32
  if use_refiner:
33
- image = refiner(prompt=prompt, image=image[None, :]).imagas[0]
34
 
35
  # pipe.unet.to(memory_format=torch.channels_last)
36
  # pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True)
 
1
  #!/usr/bin/env python3
2
  from diffusers import DiffusionPipeline, EulerDiscreteScheduler, StableDiffusionPipeline, KDPM2DiscreteScheduler, StableDiffusionImg2ImgPipeline, HeunDiscreteScheduler, KDPM2AncestralDiscreteScheduler, DDIMScheduler
3
+ from diffusers import StableDiffusionXLPipeline, StableDiffusionXLImg2ImgPipeline
4
  import time
5
  from pytorch_lightning import seed_everything
6
  import os
 
18
 
19
  use_refiner = bool(int(sys.argv[1]))
20
 
21
+ # pipe_1 = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-0.9", torch_dtype=torch.float16, variant="fp16", use_safetensors=True)
22
+ pipe = StableDiffusionXLPipeline.from_single_file("https://huggingface.co/nichijoufan777/stable-diffusion-xl-base-0.9/blob/main/sd_xl_base_0.9.safetensors", torch_dtype=torch.float16, use_safetensors=True)
23
  pipe.to("cuda")
24
  # pipe.enable_model_cpu_offload()
25
 
26
  if use_refiner:
27
+ refiner = StableDiffusionXLImg2ImgPipeline.from_single_file("https://huggingface.co/nichijoufan777/stable-diffusion-xl-refiner-0.9/blob/main/sd_xl_refiner_0.9.safetensors", torch_dtype=torch.float16)
28
+ refiner.to("cuda")
29
 
30
  prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k"
31
  seed_everything(0)
32
  image = pipe(prompt=prompt, output_type="latent" if use_refiner else "pil").images[0]
33
+ # image = pipe(prompt=prompt, output_type="latent" if use_refiner else "pil").images[0]
34
 
35
  if use_refiner:
36
+ image = refiner(prompt=prompt, image=image[None, :]).images[0]
37
 
38
  # pipe.unet.to(memory_format=torch.channels_last)
39
  # pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True)
run_safety.py ADDED
@@ -0,0 +1 @@
 
 
1
+ #!/usr/bin/env python3
run_shape.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ import torch
3
+ import numpy as np
4
+ from huggingface_hub import HfApi
5
+
6
+ from diffusers import ShapEPipeline
7
+ from diffusers.utils import export_to_gif
8
+
9
+ api = HfApi()
10
+
11
+ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
12
+
13
+ batch_size = 1
14
+ guidance_scale = 15.0
15
+ prompt = "a red table"
16
+ prompt = "A chair that looks like an avocado"
17
+ torch.manual_seed(0)
18
+
19
+ repo = "openai/shap-e"
20
+ pipe = ShapEPipeline.from_pretrained(repo)
21
+ pipe = pipe.to(device)
22
+
23
+ generator = torch.Generator(device="cuda").manual_seed(0)
24
+
25
+ prompts = [
26
+ "A chair that looks like an avocado",
27
+ "An airplane that looks like a banana",
28
+ "A spaceship",
29
+ "A birthday cupcake",
30
+ "A chair that looks like a tree",
31
+ "A green boot",
32
+ "A penguin",
33
+ "Ube ice cream cone",
34
+ "A bowl of vegetables",
35
+ ]
36
+
37
+ for prompt in prompts:
38
+ images = pipe(
39
+ prompt,
40
+ num_images_per_prompt=batch_size,
41
+ generator=generator,
42
+ guidance_scale=guidance_scale,
43
+ num_inference_steps=64,
44
+ frame_size=256,
45
+ output_type='pil'
46
+ ).images
47
+
48
+ path = f"/home/patrick/images/{'_'.join(prompt.split())}.gif"
49
+ export_to_gif(images[0], path)
50
+
51
+
52
+ api.upload_file(
53
+ path_or_fileobj=path,
54
+ path_in_repo=path.split("/")[-1],
55
+ repo_id="patrickvonplaten/images",
56
+ repo_type="dataset",
57
+ )
58
+ print(f"https://huggingface.co/datasets/patrickvonplaten/images/blob/main/{path.split('/')[-1]}")
run_video.py CHANGED
@@ -5,13 +5,16 @@ from diffusers.utils import export_to_video
5
  from PIL import Image
6
 
7
  # Make sure CUDA has < 13GB VRAM
8
- torch.cuda.set_per_process_memory_fraction(0.5)
9
 
10
  pipe = DiffusionPipeline.from_pretrained("cerspense/zeroscope_v2_576w", torch_dtype=torch.float16)
11
  pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
12
- pipe.enable_model_cpu_offload()
 
13
  pipe.enable_vae_slicing()
14
 
 
 
15
  prompt = "Darth Vader is surfing on waves"
16
  video_frames = pipe(prompt, num_inference_steps=40, height=320, width=576, num_frames=36).frames
17
  video_path = export_to_video(video_frames, output_video_path="/home/patrick/videos/video_576_darth_vader_36.mp4")
 
5
  from PIL import Image
6
 
7
  # Make sure CUDA has < 13GB VRAM
8
+ # torch.cuda.set_per_process_memory_fraction(0.5)
9
 
10
  pipe = DiffusionPipeline.from_pretrained("cerspense/zeroscope_v2_576w", torch_dtype=torch.float16)
11
  pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
12
+ # pipe.enable_model_cpu_offload()
13
+ pipe.to("cuda")
14
  pipe.enable_vae_slicing()
15
 
16
+ pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True)
17
+
18
  prompt = "Darth Vader is surfing on waves"
19
  video_frames = pipe(prompt, num_inference_steps=40, height=320, width=576, num_frames=36).frames
20
  video_path = export_to_video(video_frames, output_video_path="/home/patrick/videos/video_576_darth_vader_36.mp4")
speed_up_scheduler.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ import torch
3
+ from diffusers import DiffusionPipeline
4
+ from diffusers import EulerAncestralDiscreteScheduler
5
+
6
+ import cProfile
7
+ import pstats
8
+ import io
9
+ from pstats import SortKey
10
+
11
+ path = 'stabilityai/stable-diffusion-2-1-base'
12
+ prompt = "Women standing on a mountain top"
13
+
14
+ torch.set_grad_enabled(False)
15
+ torch.backends.cudnn.benchmark = True
16
+
17
+ with torch.inference_mode():
18
+ pipe = DiffusionPipeline.from_pretrained(path, torch_dtype=torch.float16, safety_checker=None, requires_safety_checker=False)
19
+ pipe.to('cuda')
20
+ pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
21
+ pipe.unet.to(device='cuda', dtype=torch.float16, memory_format=torch.channels_last)
22
+
23
+ for bi in range(7):
24
+ if bi == 2: # Start profiler on 3rd image
25
+ ob = cProfile.Profile()
26
+ ob.enable()
27
+ images = pipe(prompt=prompt, width=512, height=512, num_inference_steps=20, num_images_per_prompt=1).images
28
+ ob.disable()
29
+ sec = io.StringIO()
30
+ sortby = SortKey.TIME
31
+ ps = pstats.Stats(ob, stream=sec).sort_stats(sortby)
32
+ ps.print_stats()
33
+ print(sec.getvalue()[0:1000])