Meismaxandmaxisme commited on
Commit
c15a748
·
verified ·
1 Parent(s): 38dd116

Upload 2 files

Browse files
src/backend/pipelines/lcm.py ADDED
@@ -0,0 +1,124 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from constants import LCM_DEFAULT_MODEL
2
+ from diffusers import (
3
+ DiffusionPipeline,
4
+ AutoencoderTiny,
5
+ UNet2DConditionModel,
6
+ LCMScheduler,
7
+ StableDiffusionPipeline,
8
+ )
9
+ import torch
10
+ from backend.tiny_autoencoder import get_tiny_autoencoder_repo_id
11
+ from typing import Any
12
+ from diffusers import (
13
+ LCMScheduler,
14
+ StableDiffusionImg2ImgPipeline,
15
+ StableDiffusionXLImg2ImgPipeline,
16
+ AutoPipelineForText2Image,
17
+ AutoPipelineForImage2Image,
18
+ StableDiffusionControlNetPipeline,
19
+ )
20
+ import pathlib
21
+
22
+
23
+ def _get_lcm_pipeline_from_base_model(
24
+ lcm_model_id: str,
25
+ base_model_id: str,
26
+ use_local_model: bool,
27
+ ):
28
+ pipeline = None
29
+ unet = UNet2DConditionModel.from_pretrained(
30
+ lcm_model_id,
31
+ torch_dtype=torch.float32,
32
+ local_files_only=use_local_model,
33
+ resume_download=True,
34
+ )
35
+ pipeline = DiffusionPipeline.from_pretrained(
36
+ base_model_id,
37
+ unet=unet,
38
+ torch_dtype=torch.float32,
39
+ local_files_only=use_local_model,
40
+ resume_download=True,
41
+ )
42
+ pipeline.scheduler = LCMScheduler.from_config(pipeline.scheduler.config)
43
+ return pipeline
44
+
45
+
46
+ def load_taesd(
47
+ pipeline: Any,
48
+ use_local_model: bool = False,
49
+ torch_data_type: torch.dtype = torch.float32,
50
+ ):
51
+ tiny_vae = get_tiny_autoencoder_repo_id(pipeline.__class__.__name__)
52
+ pipeline.vae = AutoencoderTiny.from_pretrained(
53
+ tiny_vae,
54
+ torch_dtype=torch_data_type,
55
+ local_files_only=use_local_model,
56
+ )
57
+
58
+
59
+ def get_lcm_model_pipeline(
60
+ model_id: str = LCM_DEFAULT_MODEL,
61
+ use_local_model: bool = False,
62
+ pipeline_args={},
63
+ ):
64
+ pipeline = None
65
+ if model_id == "latent-consistency/lcm-sdxl":
66
+ pipeline = _get_lcm_pipeline_from_base_model(
67
+ model_id,
68
+ "stabilityai/stable-diffusion-xl-base-1.0",
69
+ use_local_model,
70
+ )
71
+
72
+ elif model_id == "latent-consistency/lcm-ssd-1b":
73
+ pipeline = _get_lcm_pipeline_from_base_model(
74
+ model_id,
75
+ "segmind/SSD-1B",
76
+ use_local_model,
77
+ )
78
+ elif pathlib.Path(model_id).suffix == ".safetensors":
79
+ # When loading a .safetensors model, the pipeline has to be created
80
+ # with StableDiffusionPipeline() since it's the only class that
81
+ # defines the method from_single_file()
82
+ dummy_pipeline = StableDiffusionPipeline.from_single_file(
83
+ model_id,
84
+ safety_checker=None,
85
+ run_safety_checker=False,
86
+ load_safety_checker=False,
87
+ local_files_only=use_local_model,
88
+ use_safetensors=True,
89
+ )
90
+ if "lcm" in model_id.lower():
91
+ dummy_pipeline.scheduler = LCMScheduler.from_config(
92
+ dummy_pipeline.scheduler.config
93
+ )
94
+
95
+ pipeline = AutoPipelineForText2Image.from_pipe(
96
+ dummy_pipeline,
97
+ **pipeline_args,
98
+ )
99
+ del dummy_pipeline
100
+ else:
101
+ # pipeline = DiffusionPipeline.from_pretrained(
102
+ pipeline = AutoPipelineForText2Image.from_pretrained(
103
+ model_id,
104
+ local_files_only=use_local_model,
105
+ **pipeline_args,
106
+ )
107
+
108
+ return pipeline
109
+
110
+
111
+ def get_image_to_image_pipeline(pipeline: Any) -> Any:
112
+ components = pipeline.components
113
+ pipeline_class = pipeline.__class__.__name__
114
+ if (
115
+ pipeline_class == "LatentConsistencyModelPipeline"
116
+ or pipeline_class == "StableDiffusionPipeline"
117
+ ):
118
+ return StableDiffusionImg2ImgPipeline(**components)
119
+ elif pipeline_class == "StableDiffusionControlNetPipeline":
120
+ return AutoPipelineForImage2Image.from_pipe(pipeline)
121
+ elif pipeline_class == "StableDiffusionXLPipeline":
122
+ return StableDiffusionXLImg2ImgPipeline(**components)
123
+ else:
124
+ raise Exception(f"Unknown pipeline {pipeline_class}")
src/backend/pipelines/lcm_lora.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pathlib
2
+ from os import path
3
+
4
+ import torch
5
+ from diffusers import (
6
+ AutoPipelineForText2Image,
7
+ LCMScheduler,
8
+ StableDiffusionPipeline,
9
+ )
10
+
11
+
12
+ def load_lcm_weights(
13
+ pipeline,
14
+ use_local_model,
15
+ lcm_lora_id,
16
+ ):
17
+ kwargs = {
18
+ "local_files_only": use_local_model,
19
+ "weight_name": "pytorch_lora_weights.safetensors",
20
+ }
21
+ pipeline.load_lora_weights(
22
+ lcm_lora_id,
23
+ **kwargs,
24
+ adapter_name="lcm",
25
+ )
26
+
27
+
28
+ def get_lcm_lora_pipeline(
29
+ base_model_id: str,
30
+ lcm_lora_id: str,
31
+ use_local_model: bool,
32
+ torch_data_type: torch.dtype,
33
+ pipeline_args={},
34
+ ):
35
+ if pathlib.Path(base_model_id).suffix == ".safetensors":
36
+ # SD 1.5 models only
37
+ # When loading a .safetensors model, the pipeline has to be created
38
+ # with StableDiffusionPipeline() since it's the only class that
39
+ # defines the method from_single_file(); afterwards a new pipeline
40
+ # is created using AutoPipelineForText2Image() for ControlNet
41
+ # support, in case ControlNet is enabled
42
+ if not path.exists(base_model_id):
43
+ raise FileNotFoundError(
44
+ f"Model file not found,Please check your model path: {base_model_id}"
45
+ )
46
+ print("Using single file Safetensors model (Supported models - SD 1.5 models)")
47
+
48
+ dummy_pipeline = StableDiffusionPipeline.from_single_file(
49
+ base_model_id,
50
+ torch_dtype=torch_data_type,
51
+ safety_checker=None,
52
+ local_files_only=use_local_model,
53
+ use_safetensors=True,
54
+ )
55
+ pipeline = AutoPipelineForText2Image.from_pipe(
56
+ dummy_pipeline,
57
+ **pipeline_args,
58
+ )
59
+ del dummy_pipeline
60
+ else:
61
+ pipeline = AutoPipelineForText2Image.from_pretrained(
62
+ base_model_id,
63
+ torch_dtype=torch_data_type,
64
+ local_files_only=use_local_model,
65
+ **pipeline_args,
66
+ )
67
+
68
+ load_lcm_weights(
69
+ pipeline,
70
+ use_local_model,
71
+ lcm_lora_id,
72
+ )
73
+ # Always fuse LCM-LoRA
74
+ # pipeline.fuse_lora()
75
+
76
+ if "lcm" in lcm_lora_id.lower() or "hypersd" in lcm_lora_id.lower():
77
+ print("LCM LoRA model detected so using recommended LCMScheduler")
78
+ pipeline.scheduler = LCMScheduler.from_config(pipeline.scheduler.config)
79
+
80
+ # pipeline.unet.to(memory_format=torch.channels_last)
81
+ return pipeline