root commited on
Commit
40e68f7
1 Parent(s): 0827931
app.py ADDED
@@ -0,0 +1,275 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Adapted from https://github.com/guoyww/AnimateDiff/blob/main/app.py
2
+
3
+ # import spaces
4
+ import os
5
+ import json
6
+ import torch
7
+ import random
8
+
9
+ import gradio as gr
10
+ from glob import glob
11
+ from omegaconf import OmegaConf
12
+ from datetime import datetime
13
+ from safetensors import safe_open
14
+
15
+ from PIL import Image
16
+
17
+ from unet2d_custom import UNet2DConditionModel
18
+ import torch
19
+ from pipeline_stable_diffusion_custom import StableDiffusionPipeline
20
+
21
+ from diffusers import DDIMScheduler
22
+ from pnp_utils import *
23
+ import torchvision.transforms as T
24
+ from preprocess import get_timesteps
25
+ from preprocess import Preprocess
26
+
27
+
28
+ from pnp import PNP
29
+
30
+ sample_idx = 0
31
+
32
+ css = """
33
+ .toolbutton {
34
+ margin-buttom: 0em 0em 0em 0em;
35
+ max-width: 1.5em;
36
+ min-width: 1.5em !important;
37
+ height: 1.5em;
38
+ }
39
+ """
40
+
41
+ class AnimateController:
42
+ def __init__(self):
43
+ self.sr = 44100
44
+ self.save_steps = 50
45
+ self.device = 'cuda'
46
+ self.seed = 42
47
+ self.extract_reverse = False
48
+ self.save_dir = 'latents'
49
+ self.steps = 50
50
+ self.inversion_prompt = ''
51
+
52
+
53
+ self.seed = 42
54
+ seed_everything(self.seed)
55
+
56
+ self.pnp = PNP(sd_version="1.4")
57
+
58
+ self.pnp.unet.to(self.device)
59
+ self.pnp.audio_projector.to(self.device)
60
+
61
+
62
+
63
+ # audio_projector_path = "ckpts/audio_projector_landscape.pth"
64
+ # gate_dict_path = "ckpts/landscape.pt"
65
+ # self.pnp.set_audio_projector(gate_dict_path, audio_projector_path)
66
+
67
+
68
+
69
+
70
+ #@spaces.GPU
71
+ def preprocess(self, image=None):
72
+
73
+ model_key = "CompVis/stable-diffusion-v1-4"
74
+ toy_scheduler = DDIMScheduler.from_pretrained(model_key, subfolder="scheduler")
75
+ toy_scheduler.set_timesteps(self.save_steps)
76
+ timesteps_to_save, num_inference_steps = get_timesteps(toy_scheduler, num_inference_steps=self.save_steps,
77
+ strength=1.0,
78
+ device=self.device)
79
+
80
+ save_path = os.path.join(self.save_dir + "_forward")
81
+ os.makedirs(save_path, exist_ok=True)
82
+ model = Preprocess(self.device, sd_version='1.4', hf_key=None)
83
+ recon_image = model.extract_latents(data_path=image,
84
+ num_steps=self.steps,
85
+ save_path=save_path,
86
+ timesteps_to_save=timesteps_to_save,
87
+ inversion_prompt=self.inversion_prompt,
88
+ extract_reverse=False)
89
+
90
+ T.ToPILImage()(recon_image[0]).save(os.path.join(save_path, f'recon.jpg'))
91
+
92
+ #@spaces.GPU
93
+ def generate(self, file=None, audio=None, prompt=None,
94
+ cfg_scale=5, image_path=None,
95
+ pnp_f_t=0.8, pnp_attn_t=0.8,):
96
+
97
+ image = self.pnp.run_pnp(
98
+ n_timesteps=50,
99
+ pnp_f_t=pnp_f_t, pnp_attn_t=pnp_attn_t,
100
+ prompt=prompt,
101
+ negative_prompt="",
102
+ audio_path=audio,
103
+ image_path=image_path,
104
+ cfg_scale=cfg_scale,
105
+ )
106
+
107
+ return image
108
+
109
+ # @spaces.GPU
110
+ # def update_audio_model(self, audio_model_update):
111
+
112
+ # print(f"changing ckpts audio model {audio_model_update}")
113
+
114
+ # if audio_model_update == "Landscape Model":
115
+ # audio_projector_path = "ckpts/audio_projector_landscape.pth"
116
+ # gate_dict_path = "ckpts/landscape.pt"
117
+ # else:
118
+ # audio_projector_path = "ckpts/audio_projector_gh.pth"
119
+ # gate_dict_path = "ckpts/greatest_hits.pt"
120
+
121
+ # self.pnp.set_audio_projector(gate_dict_path, audio_projector_path)
122
+ # self.pnp.changed_model = True
123
+
124
+ # # gate_dict = torch.load(gate_dict_path)
125
+
126
+ # # for name, param in self.pnp.unet.named_parameters():
127
+ # # if "adapter" in name:
128
+ # # param.data = gate_dict[name]
129
+
130
+ # # self.pnp.audio_projector.load_state_dict(torch.load(audio_projector_path))
131
+ # # self.pnp.unet.to(self.device)
132
+ # # self.pnp.audio_projector.to(self.device)
133
+
134
+ # return gr.Dropdown()
135
+
136
+ controller = AnimateController()
137
+
138
+
139
+ def ui():
140
+ with gr.Blocks(css=css) as demo:
141
+ gr.Markdown(
142
+ """
143
+ # [SonicDiffusion: Audio-Driven Image Generation and Editing with Pretrained Diffusion Models]
144
+ """
145
+ )
146
+ with gr.Row():
147
+ audio_input = gr.Audio(sources="upload", type="filepath")
148
+ prompt_textbox = gr.Textbox(label="Prompt", lines=2)
149
+
150
+ with gr.Row():
151
+ with gr.Column():
152
+ pnp_f_t = gr.Slider(label="PNP Residual Injection", step=0.1, value=0.8, minimum=0.0, maximum=1.0)
153
+ pnp_attn_t = gr.Slider(label="PNP Attention Injection", step=0.1, value=0.8, minimum=0.0, maximum=1.0)
154
+
155
+ with gr.Column():
156
+ audio_model_dropdown = gr.Dropdown(
157
+ label="Select SonicDiffusion model",
158
+ value="Landscape Model",
159
+ choices=["Landscape Model", "Greatest Hits Model"],
160
+ interactive=True,
161
+ )
162
+
163
+ # audio_model_dropdown.change(fn=controller.update_audio_model, inputs=[audio_model_dropdown], outputs=[audio_model_dropdown])
164
+ cfg_scale_slider = gr.Slider(label="CFG Scale", step=0.5, value=7.5, minimum=0, maximum=20)
165
+
166
+
167
+ with gr.Row():
168
+ preprocess_button = gr.Button(value="Preprocess", variant='primary')
169
+ generate_button = gr.Button(value="Generate", variant='primary')
170
+
171
+
172
+ with gr.Row():
173
+ with gr.Column():
174
+ image_input = gr.Image(label="Input Image Component", sources="upload", type="filepath")
175
+
176
+ with gr.Column():
177
+ output = gr.Image(label="Output Image Component",
178
+ height=512, width=512)
179
+
180
+ with gr.Row():
181
+
182
+ examples_img_1 = [
183
+ [Image.open("assets/corridor.png")],
184
+ [Image.open("assets/desert.png")],
185
+ [Image.open("assets/forest.png")],
186
+ [Image.open("assets/forest_painting.png")],
187
+ [Image.open("assets/golf_field.png")],
188
+ [Image.open("assets/human.png")],
189
+ [Image.open("assets/wood.png")],
190
+ [Image.open("assets/house.png")],
191
+
192
+ [Image.open("assets/apple.png")],
193
+ [Image.open("assets/chair.png")],
194
+ [Image.open("assets/hands.png")],
195
+ [Image.open("assets/pineapple.png")],
196
+ [Image.open("assets/table.png")],
197
+ ]
198
+ gr.Examples(examples=examples_img_1,inputs=[image_input], label="Images")
199
+
200
+
201
+ # examples_img_2 = [
202
+ # [Image.open("assets/apple.png")],
203
+ # [Image.open("assets/chair.png")],
204
+ # [Image.open("assets/hands.png")],
205
+ # [Image.open("assets/pineapple.png")],
206
+ # [Image.open("assets/table.png")],
207
+ # ]
208
+ # gr.Examples(examples=examples,inputs=[image_input], label="Greatest Hits Images")
209
+
210
+ examples2 = [
211
+ ['./assets/fire_crackling.wav'],
212
+ ['./assets/forest_birds.wav'],
213
+ ['./assets/forest_stepping_on_branches.wav'],
214
+ ['./assets/howling_wind.wav'],
215
+ ['./assets/rain.wav'],
216
+ ['./assets/splashing_water.wav'],
217
+ ['./assets/splashing_water_soft.wav'],
218
+ ['./assets/steps_on_snow.wav'],
219
+ ['./assets/thunder.wav'],
220
+ ['./assets/underwater.wav'],
221
+ ['./assets/waterfall_burble.wav'],
222
+ ['./assets/wind_noise_birds.wav'],
223
+ ]
224
+
225
+ gr.Examples(examples=examples2,inputs=[audio_input], label="Landscape Audios")
226
+
227
+
228
+ examples3 = [
229
+ ['./assets/cardboard.wav'],
230
+ ['./assets/carpet.wav'],
231
+ ['./assets/ceramic.wav'],
232
+ ['./assets/cloth.wav'],
233
+ ['./assets/gravel.wav'],
234
+ ['./assets/leaf.wav'],
235
+ ['./assets/metal.wav'],
236
+ ['./assets/plastic_bag.wav'],
237
+ ['./assets/plastic.wav'],
238
+ ['./assets/rock.wav'],
239
+ ['./assets/wood.wav'],
240
+ ]
241
+ gr.Examples(examples=examples3,inputs=[audio_input], label="Greatest Hits Audios")
242
+
243
+
244
+ preprocess_button.click(
245
+ fn=controller.preprocess,
246
+ inputs=[
247
+ image_input
248
+ ],
249
+ outputs=output
250
+ )
251
+
252
+
253
+ generate_button.click(
254
+ fn=controller.generate,
255
+ inputs=[
256
+ audio_model_dropdown,
257
+ audio_input,
258
+ prompt_textbox,
259
+ cfg_scale_slider,
260
+ image_input,
261
+ pnp_f_t,
262
+ pnp_attn_t,
263
+ ],
264
+ outputs=output
265
+ )
266
+
267
+
268
+ return demo
269
+
270
+
271
+
272
+ if __name__ == "__main__":
273
+ demo = ui()
274
+ demo.launch(share=True)
275
+
attention_custom.py ADDED
@@ -0,0 +1,681 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Adapted from https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/attention.py
2
+
3
+ from typing import Any, Dict, Optional
4
+
5
+ import torch
6
+ import torch.nn.functional as F
7
+ from torch import nn
8
+
9
+ from diffusers.utils import deprecate, logging
10
+ from diffusers.utils.torch_utils import maybe_allow_in_graph
11
+ from diffusers.models.activations import GEGLU, GELU, ApproximateGELU
12
+ from diffusers.models.attention_processor import Attention
13
+ from diffusers.models.embeddings import SinusoidalPositionalEmbedding
14
+ from diffusers.models.normalization import AdaLayerNorm, AdaLayerNormContinuous, AdaLayerNormZero, RMSNorm
15
+
16
+
17
+ logger = logging.get_logger(__name__)
18
+
19
+
20
+ def _chunked_feed_forward(ff: nn.Module, hidden_states: torch.Tensor, chunk_dim: int, chunk_size: int):
21
+ # "feed_forward_chunk_size" can be used to save memory
22
+ if hidden_states.shape[chunk_dim] % chunk_size != 0:
23
+ raise ValueError(
24
+ f"`hidden_states` dimension to be chunked: {hidden_states.shape[chunk_dim]} has to be divisible by chunk size: {chunk_size}. Make sure to set an appropriate `chunk_size` when calling `unet.enable_forward_chunking`."
25
+ )
26
+
27
+ num_chunks = hidden_states.shape[chunk_dim] // chunk_size
28
+ ff_output = torch.cat(
29
+ [ff(hid_slice) for hid_slice in hidden_states.chunk(num_chunks, dim=chunk_dim)],
30
+ dim=chunk_dim,
31
+ )
32
+ return ff_output
33
+
34
+
35
+ @maybe_allow_in_graph
36
+ class GatedSelfAttentionDense(nn.Module):
37
+ r"""
38
+ A gated self-attention dense layer that combines visual features and object features.
39
+
40
+ Parameters:
41
+ query_dim (`int`): The number of channels in the query.
42
+ context_dim (`int`): The number of channels in the context.
43
+ n_heads (`int`): The number of heads to use for attention.
44
+ d_head (`int`): The number of channels in each head.
45
+ """
46
+
47
+ def __init__(self, query_dim: int, context_dim: int, n_heads: int, d_head: int):
48
+ super().__init__()
49
+
50
+ # we need a linear projection since we need cat visual feature and obj feature
51
+ self.linear = nn.Linear(context_dim, query_dim)
52
+
53
+ self.attn = Attention(query_dim=query_dim, heads=n_heads, dim_head=d_head)
54
+ self.ff = FeedForward(query_dim, activation_fn="geglu")
55
+
56
+ self.norm1 = nn.LayerNorm(query_dim)
57
+ self.norm2 = nn.LayerNorm(query_dim)
58
+
59
+ self.register_parameter("alpha_attn", nn.Parameter(torch.tensor(0.0)))
60
+ self.register_parameter("alpha_dense", nn.Parameter(torch.tensor(0.0)))
61
+
62
+ self.enabled = True
63
+
64
+ def forward(self, x: torch.Tensor, objs: torch.Tensor) -> torch.Tensor:
65
+ if not self.enabled:
66
+ return x
67
+
68
+ n_visual = x.shape[1]
69
+ objs = self.linear(objs)
70
+
71
+ x = x + self.alpha_attn.tanh() * self.attn(self.norm1(torch.cat([x, objs], dim=1)))[:, :n_visual, :]
72
+ x = x + self.alpha_dense.tanh() * self.ff(self.norm2(x))
73
+
74
+ return x
75
+
76
+
77
+ @maybe_allow_in_graph
78
+ class BasicTransformerBlock(nn.Module):
79
+ r"""
80
+ A basic Transformer block.
81
+
82
+ Parameters:
83
+ dim (`int`): The number of channels in the input and output.
84
+ num_attention_heads (`int`): The number of heads to use for multi-head attention.
85
+ attention_head_dim (`int`): The number of channels in each head.
86
+ dropout (`float`, *optional*, defaults to 0.0): The dropout probability to use.
87
+ cross_attention_dim (`int`, *optional*): The size of the encoder_hidden_states vector for cross attention.
88
+ activation_fn (`str`, *optional*, defaults to `"geglu"`): Activation function to be used in feed-forward.
89
+ num_embeds_ada_norm (:
90
+ obj: `int`, *optional*): The number of diffusion steps used during training. See `Transformer2DModel`.
91
+ attention_bias (:
92
+ obj: `bool`, *optional*, defaults to `False`): Configure if the attentions should contain a bias parameter.
93
+ only_cross_attention (`bool`, *optional*):
94
+ Whether to use only cross-attention layers. In this case two cross attention layers are used.
95
+ double_self_attention (`bool`, *optional*):
96
+ Whether to use two self-attention layers. In this case no cross attention layers are used.
97
+ upcast_attention (`bool`, *optional*):
98
+ Whether to upcast the attention computation to float32. This is useful for mixed precision training.
99
+ norm_elementwise_affine (`bool`, *optional*, defaults to `True`):
100
+ Whether to use learnable elementwise affine parameters for normalization.
101
+ norm_type (`str`, *optional*, defaults to `"layer_norm"`):
102
+ The normalization layer to use. Can be `"layer_norm"`, `"ada_norm"` or `"ada_norm_zero"`.
103
+ final_dropout (`bool` *optional*, defaults to False):
104
+ Whether to apply a final dropout after the last feed-forward layer.
105
+ attention_type (`str`, *optional*, defaults to `"default"`):
106
+ The type of attention to use. Can be `"default"` or `"gated"` or `"gated-text-image"`.
107
+ positional_embeddings (`str`, *optional*, defaults to `None`):
108
+ The type of positional embeddings to apply to.
109
+ num_positional_embeddings (`int`, *optional*, defaults to `None`):
110
+ The maximum number of positional embeddings to apply.
111
+ """
112
+
113
+ def __init__(
114
+ self,
115
+ dim: int,
116
+ num_attention_heads: int,
117
+ attention_head_dim: int,
118
+ dropout=0.0,
119
+ cross_attention_dim: Optional[int] = None,
120
+ activation_fn: str = "geglu",
121
+ num_embeds_ada_norm: Optional[int] = None,
122
+ attention_bias: bool = False,
123
+ only_cross_attention: bool = False,
124
+ double_self_attention: bool = False,
125
+ upcast_attention: bool = False,
126
+ norm_elementwise_affine: bool = True,
127
+ norm_type: str = "layer_norm", # 'layer_norm', 'ada_norm', 'ada_norm_zero', 'ada_norm_single', 'ada_norm_continuous', 'layer_norm_i2vgen'
128
+ norm_eps: float = 1e-5,
129
+ final_dropout: bool = False,
130
+ attention_type: str = "default",
131
+ positional_embeddings: Optional[str] = None,
132
+ num_positional_embeddings: Optional[int] = None,
133
+ ada_norm_continous_conditioning_embedding_dim: Optional[int] = None,
134
+ ada_norm_bias: Optional[int] = None,
135
+ ff_inner_dim: Optional[int] = None,
136
+ ff_bias: bool = True,
137
+ attention_out_bias: bool = True,
138
+ use_adapter: bool = False,
139
+ ):
140
+ super().__init__()
141
+ self.only_cross_attention = only_cross_attention
142
+
143
+ # We keep these boolean flags for backward-compatibility.
144
+ self.use_ada_layer_norm_zero = (num_embeds_ada_norm is not None) and norm_type == "ada_norm_zero"
145
+ self.use_ada_layer_norm = (num_embeds_ada_norm is not None) and norm_type == "ada_norm"
146
+ self.use_ada_layer_norm_single = norm_type == "ada_norm_single"
147
+ self.use_layer_norm = norm_type == "layer_norm"
148
+ self.use_ada_layer_norm_continuous = norm_type == "ada_norm_continuous"
149
+
150
+ self.use_adapter = use_adapter
151
+
152
+ if norm_type in ("ada_norm", "ada_norm_zero") and num_embeds_ada_norm is None:
153
+ raise ValueError(
154
+ f"`norm_type` is set to {norm_type}, but `num_embeds_ada_norm` is not defined. Please make sure to"
155
+ f" define `num_embeds_ada_norm` if setting `norm_type` to {norm_type}."
156
+ )
157
+
158
+ self.norm_type = norm_type
159
+ self.num_embeds_ada_norm = num_embeds_ada_norm
160
+
161
+ if positional_embeddings and (num_positional_embeddings is None):
162
+ raise ValueError(
163
+ "If `positional_embedding` type is defined, `num_positition_embeddings` must also be defined."
164
+ )
165
+
166
+ if positional_embeddings == "sinusoidal":
167
+ self.pos_embed = SinusoidalPositionalEmbedding(dim, max_seq_length=num_positional_embeddings)
168
+ else:
169
+ self.pos_embed = None
170
+
171
+ # Define 3 blocks. Each block has its own normalization layer.
172
+ # 1. Self-Attn
173
+ if norm_type == "ada_norm":
174
+ self.norm1 = AdaLayerNorm(dim, num_embeds_ada_norm)
175
+ elif norm_type == "ada_norm_zero":
176
+ self.norm1 = AdaLayerNormZero(dim, num_embeds_ada_norm)
177
+ elif norm_type == "ada_norm_continuous":
178
+ self.norm1 = AdaLayerNormContinuous(
179
+ dim,
180
+ ada_norm_continous_conditioning_embedding_dim,
181
+ norm_elementwise_affine,
182
+ norm_eps,
183
+ ada_norm_bias,
184
+ "rms_norm",
185
+ )
186
+ else:
187
+ self.norm1 = nn.LayerNorm(dim, elementwise_affine=norm_elementwise_affine, eps=norm_eps)
188
+
189
+ self.attn1 = Attention(
190
+ query_dim=dim,
191
+ heads=num_attention_heads,
192
+ dim_head=attention_head_dim,
193
+ dropout=dropout,
194
+ bias=attention_bias,
195
+ cross_attention_dim=cross_attention_dim if only_cross_attention else None,
196
+ upcast_attention=upcast_attention,
197
+ out_bias=attention_out_bias,
198
+ )
199
+
200
+ # 2. Cross-Attn
201
+ if cross_attention_dim is not None or double_self_attention:
202
+ # We currently only use AdaLayerNormZero for self attention where there will only be one attention block.
203
+ # I.e. the number of returned modulation chunks from AdaLayerZero would not make sense if returned during
204
+ # the second cross attention block.
205
+ if norm_type == "ada_norm":
206
+ self.norm2 = AdaLayerNorm(dim, num_embeds_ada_norm)
207
+ elif norm_type == "ada_norm_continuous":
208
+ self.norm2 = AdaLayerNormContinuous(
209
+ dim,
210
+ ada_norm_continous_conditioning_embedding_dim,
211
+ norm_elementwise_affine,
212
+ norm_eps,
213
+ ada_norm_bias,
214
+ "rms_norm",
215
+ )
216
+ else:
217
+ self.norm2 = nn.LayerNorm(dim, norm_eps, norm_elementwise_affine)
218
+
219
+ self.attn2 = Attention(
220
+ query_dim=dim,
221
+ cross_attention_dim=cross_attention_dim if not double_self_attention else None,
222
+ heads=num_attention_heads,
223
+ dim_head=attention_head_dim,
224
+ dropout=dropout,
225
+ bias=attention_bias,
226
+ upcast_attention=upcast_attention,
227
+ out_bias=attention_out_bias,
228
+ ) # is self-attn if encoder_hidden_states is none
229
+ else:
230
+ self.norm2 = None
231
+ self.attn2 = None
232
+
233
+ if use_adapter:
234
+ self.attn_adapter = Attention(
235
+ query_dim=dim,
236
+ cross_attention_dim=cross_attention_dim if not double_self_attention else None,
237
+ heads=num_attention_heads,
238
+ dim_head=attention_head_dim,
239
+ dropout=dropout,
240
+ bias=attention_bias,
241
+ upcast_attention=upcast_attention,
242
+ out_bias=attention_out_bias,
243
+ )
244
+ self.norm_adapter = nn.LayerNorm(dim)
245
+ self.gate_adapter = nn.Parameter(torch.tensor([0.1]))
246
+
247
+
248
+ # 3. Feed-forward
249
+ if norm_type == "ada_norm_continuous":
250
+ self.norm3 = AdaLayerNormContinuous(
251
+ dim,
252
+ ada_norm_continous_conditioning_embedding_dim,
253
+ norm_elementwise_affine,
254
+ norm_eps,
255
+ ada_norm_bias,
256
+ "layer_norm",
257
+ )
258
+
259
+ elif norm_type in ["ada_norm_zero", "ada_norm", "layer_norm", "ada_norm_continuous"]:
260
+ self.norm3 = nn.LayerNorm(dim, norm_eps, norm_elementwise_affine)
261
+ elif norm_type == "layer_norm_i2vgen":
262
+ self.norm3 = None
263
+
264
+ self.ff = FeedForward(
265
+ dim,
266
+ dropout=dropout,
267
+ activation_fn=activation_fn,
268
+ final_dropout=final_dropout,
269
+ inner_dim=ff_inner_dim,
270
+ bias=ff_bias,
271
+ )
272
+
273
+ # 4. Fuser
274
+ if attention_type == "gated" or attention_type == "gated-text-image":
275
+ self.fuser = GatedSelfAttentionDense(dim, cross_attention_dim, num_attention_heads, attention_head_dim)
276
+
277
+ # 5. Scale-shift for PixArt-Alpha.
278
+ if norm_type == "ada_norm_single":
279
+ self.scale_shift_table = nn.Parameter(torch.randn(6, dim) / dim**0.5)
280
+
281
+ # let chunk size default to None
282
+ self._chunk_size = None
283
+ self._chunk_dim = 0
284
+
285
+ def set_chunk_feed_forward(self, chunk_size: Optional[int], dim: int = 0):
286
+ # Sets chunk feed-forward
287
+ self._chunk_size = chunk_size
288
+ self._chunk_dim = dim
289
+
290
+ def forward(
291
+ self,
292
+ hidden_states: torch.FloatTensor,
293
+ attention_mask: Optional[torch.FloatTensor] = None,
294
+ encoder_hidden_states: Optional[torch.FloatTensor] = None,
295
+ encoder_attention_mask: Optional[torch.FloatTensor] = None,
296
+ timestep: Optional[torch.LongTensor] = None,
297
+ cross_attention_kwargs: Dict[str, Any] = None,
298
+ class_labels: Optional[torch.LongTensor] = None,
299
+ added_cond_kwargs: Optional[Dict[str, torch.Tensor]] = None,
300
+ audio_context: Optional[torch.FloatTensor] = None,
301
+ f_multiplier: Optional[float] = 1.0,
302
+ ) -> torch.FloatTensor:
303
+ if cross_attention_kwargs is not None:
304
+ if cross_attention_kwargs.get("scale", None) is not None:
305
+ logger.warning("Passing `scale` to `cross_attention_kwargs` is depcrecated. `scale` will be ignored.")
306
+
307
+ # Notice that normalization is always applied before the real computation in the following blocks.
308
+ # 0. Self-Attention
309
+ batch_size = hidden_states.shape[0]
310
+
311
+ if self.norm_type == "ada_norm":
312
+ norm_hidden_states = self.norm1(hidden_states, timestep)
313
+ elif self.norm_type == "ada_norm_zero":
314
+ norm_hidden_states, gate_msa, shift_mlp, scale_mlp, gate_mlp = self.norm1(
315
+ hidden_states, timestep, class_labels, hidden_dtype=hidden_states.dtype
316
+ )
317
+ elif self.norm_type in ["layer_norm", "layer_norm_i2vgen"]:
318
+ norm_hidden_states = self.norm1(hidden_states)
319
+ elif self.norm_type == "ada_norm_continuous":
320
+ norm_hidden_states = self.norm1(hidden_states, added_cond_kwargs["pooled_text_emb"])
321
+ elif self.norm_type == "ada_norm_single":
322
+ shift_msa, scale_msa, gate_msa, shift_mlp, scale_mlp, gate_mlp = (
323
+ self.scale_shift_table[None] + timestep.reshape(batch_size, 6, -1)
324
+ ).chunk(6, dim=1)
325
+ norm_hidden_states = self.norm1(hidden_states)
326
+ norm_hidden_states = norm_hidden_states * (1 + scale_msa) + shift_msa
327
+ norm_hidden_states = norm_hidden_states.squeeze(1)
328
+ else:
329
+ raise ValueError("Incorrect norm used")
330
+
331
+ if self.pos_embed is not None:
332
+ norm_hidden_states = self.pos_embed(norm_hidden_states)
333
+
334
+ # 1. Prepare GLIGEN inputs
335
+ cross_attention_kwargs = cross_attention_kwargs.copy() if cross_attention_kwargs is not None else {}
336
+ gligen_kwargs = cross_attention_kwargs.pop("gligen", None)
337
+
338
+ attn_output = self.attn1(
339
+ norm_hidden_states,
340
+ encoder_hidden_states=encoder_hidden_states if self.only_cross_attention else None,
341
+ attention_mask=attention_mask,
342
+ **cross_attention_kwargs,
343
+ )
344
+ if self.norm_type == "ada_norm_zero":
345
+ attn_output = gate_msa.unsqueeze(1) * attn_output
346
+ elif self.norm_type == "ada_norm_single":
347
+ attn_output = gate_msa * attn_output
348
+
349
+ hidden_states = attn_output + hidden_states
350
+ if hidden_states.ndim == 4:
351
+ hidden_states = hidden_states.squeeze(1)
352
+
353
+ # 1.2 GLIGEN Control
354
+ if gligen_kwargs is not None:
355
+ hidden_states = self.fuser(hidden_states, gligen_kwargs["objs"])
356
+
357
+ # 3. Cross-Attention
358
+ if self.attn2 is not None:
359
+ if self.norm_type == "ada_norm":
360
+ norm_hidden_states = self.norm2(hidden_states, timestep)
361
+ elif self.norm_type in ["ada_norm_zero", "layer_norm", "layer_norm_i2vgen"]:
362
+ norm_hidden_states = self.norm2(hidden_states)
363
+ elif self.norm_type == "ada_norm_single":
364
+ # For PixArt norm2 isn't applied here:
365
+ # https://github.com/PixArt-alpha/PixArt-alpha/blob/0f55e922376d8b797edd44d25d0e7464b260dcab/diffusion/model/nets/PixArtMS.py#L70C1-L76C103
366
+ norm_hidden_states = hidden_states
367
+ elif self.norm_type == "ada_norm_continuous":
368
+ norm_hidden_states = self.norm2(hidden_states, added_cond_kwargs["pooled_text_emb"])
369
+ else:
370
+ raise ValueError("Incorrect norm")
371
+
372
+ if self.pos_embed is not None and self.norm_type != "ada_norm_single":
373
+ norm_hidden_states = self.pos_embed(norm_hidden_states)
374
+
375
+ attn_output = self.attn2(
376
+ norm_hidden_states,
377
+ encoder_hidden_states=encoder_hidden_states,
378
+ attention_mask=encoder_attention_mask,
379
+ **cross_attention_kwargs,
380
+ )
381
+ hidden_states = attn_output + hidden_states
382
+
383
+
384
+ if self.use_adapter and audio_context is not None:
385
+ norm_hidden_states = self.norm_adapter(hidden_states)
386
+ attn_output = self.attn_adapter(norm_hidden_states, encoder_hidden_states=audio_context)*self.gate_adapter.tanh()
387
+ hidden_states = attn_output*f_multiplier + hidden_states
388
+
389
+
390
+ # 4. Feed-forward
391
+ # i2vgen doesn't have this norm 🤷‍♂️
392
+ if self.norm_type == "ada_norm_continuous":
393
+ norm_hidden_states = self.norm3(hidden_states, added_cond_kwargs["pooled_text_emb"])
394
+ elif not self.norm_type == "ada_norm_single":
395
+ norm_hidden_states = self.norm3(hidden_states)
396
+
397
+ if self.norm_type == "ada_norm_zero":
398
+ norm_hidden_states = norm_hidden_states * (1 + scale_mlp[:, None]) + shift_mlp[:, None]
399
+
400
+ if self.norm_type == "ada_norm_single":
401
+ norm_hidden_states = self.norm2(hidden_states)
402
+ norm_hidden_states = norm_hidden_states * (1 + scale_mlp) + shift_mlp
403
+
404
+ if self._chunk_size is not None:
405
+ # "feed_forward_chunk_size" can be used to save memory
406
+ ff_output = _chunked_feed_forward(self.ff, norm_hidden_states, self._chunk_dim, self._chunk_size)
407
+ else:
408
+ ff_output = self.ff(norm_hidden_states)
409
+
410
+ if self.norm_type == "ada_norm_zero":
411
+ ff_output = gate_mlp.unsqueeze(1) * ff_output
412
+ elif self.norm_type == "ada_norm_single":
413
+ ff_output = gate_mlp * ff_output
414
+
415
+ hidden_states = ff_output + hidden_states
416
+ if hidden_states.ndim == 4:
417
+ hidden_states = hidden_states.squeeze(1)
418
+
419
+ return hidden_states
420
+
421
+
422
+ @maybe_allow_in_graph
423
+ class TemporalBasicTransformerBlock(nn.Module):
424
+ r"""
425
+ A basic Transformer block for video like data.
426
+
427
+ Parameters:
428
+ dim (`int`): The number of channels in the input and output.
429
+ time_mix_inner_dim (`int`): The number of channels for temporal attention.
430
+ num_attention_heads (`int`): The number of heads to use for multi-head attention.
431
+ attention_head_dim (`int`): The number of channels in each head.
432
+ cross_attention_dim (`int`, *optional*): The size of the encoder_hidden_states vector for cross attention.
433
+ """
434
+
435
+ def __init__(
436
+ self,
437
+ dim: int,
438
+ time_mix_inner_dim: int,
439
+ num_attention_heads: int,
440
+ attention_head_dim: int,
441
+ cross_attention_dim: Optional[int] = None,
442
+ ):
443
+ super().__init__()
444
+ self.is_res = dim == time_mix_inner_dim
445
+
446
+ self.norm_in = nn.LayerNorm(dim)
447
+
448
+ # Define 3 blocks. Each block has its own normalization layer.
449
+ # 1. Self-Attn
450
+ self.ff_in = FeedForward(
451
+ dim,
452
+ dim_out=time_mix_inner_dim,
453
+ activation_fn="geglu",
454
+ )
455
+
456
+ self.norm1 = nn.LayerNorm(time_mix_inner_dim)
457
+ self.attn1 = Attention(
458
+ query_dim=time_mix_inner_dim,
459
+ heads=num_attention_heads,
460
+ dim_head=attention_head_dim,
461
+ cross_attention_dim=None,
462
+ )
463
+
464
+ # 2. Cross-Attn
465
+ if cross_attention_dim is not None:
466
+ # We currently only use AdaLayerNormZero for self attention where there will only be one attention block.
467
+ # I.e. the number of returned modulation chunks from AdaLayerZero would not make sense if returned during
468
+ # the second cross attention block.
469
+ self.norm2 = nn.LayerNorm(time_mix_inner_dim)
470
+ self.attn2 = Attention(
471
+ query_dim=time_mix_inner_dim,
472
+ cross_attention_dim=cross_attention_dim,
473
+ heads=num_attention_heads,
474
+ dim_head=attention_head_dim,
475
+ ) # is self-attn if encoder_hidden_states is none
476
+ else:
477
+ self.norm2 = None
478
+ self.attn2 = None
479
+
480
+ # 3. Feed-forward
481
+ self.norm3 = nn.LayerNorm(time_mix_inner_dim)
482
+ self.ff = FeedForward(time_mix_inner_dim, activation_fn="geglu")
483
+
484
+ # let chunk size default to None
485
+ self._chunk_size = None
486
+ self._chunk_dim = None
487
+
488
+ def set_chunk_feed_forward(self, chunk_size: Optional[int], **kwargs):
489
+ # Sets chunk feed-forward
490
+ self._chunk_size = chunk_size
491
+ # chunk dim should be hardcoded to 1 to have better speed vs. memory trade-off
492
+ self._chunk_dim = 1
493
+
494
+ def forward(
495
+ self,
496
+ hidden_states: torch.FloatTensor,
497
+ num_frames: int,
498
+ encoder_hidden_states: Optional[torch.FloatTensor] = None,
499
+ ) -> torch.FloatTensor:
500
+ # Notice that normalization is always applied before the real computation in the following blocks.
501
+ # 0. Self-Attention
502
+ batch_size = hidden_states.shape[0]
503
+
504
+ batch_frames, seq_length, channels = hidden_states.shape
505
+ batch_size = batch_frames // num_frames
506
+
507
+ hidden_states = hidden_states[None, :].reshape(batch_size, num_frames, seq_length, channels)
508
+ hidden_states = hidden_states.permute(0, 2, 1, 3)
509
+ hidden_states = hidden_states.reshape(batch_size * seq_length, num_frames, channels)
510
+
511
+ residual = hidden_states
512
+ hidden_states = self.norm_in(hidden_states)
513
+
514
+ if self._chunk_size is not None:
515
+ hidden_states = _chunked_feed_forward(self.ff_in, hidden_states, self._chunk_dim, self._chunk_size)
516
+ else:
517
+ hidden_states = self.ff_in(hidden_states)
518
+
519
+ if self.is_res:
520
+ hidden_states = hidden_states + residual
521
+
522
+ norm_hidden_states = self.norm1(hidden_states)
523
+ attn_output = self.attn1(norm_hidden_states, encoder_hidden_states=None)
524
+ hidden_states = attn_output + hidden_states
525
+
526
+ # 3. Cross-Attention
527
+ if self.attn2 is not None:
528
+ norm_hidden_states = self.norm2(hidden_states)
529
+ attn_output = self.attn2(norm_hidden_states, encoder_hidden_states=encoder_hidden_states)
530
+ hidden_states = attn_output + hidden_states
531
+
532
+ # 4. Feed-forward
533
+ norm_hidden_states = self.norm3(hidden_states)
534
+
535
+ if self._chunk_size is not None:
536
+ ff_output = _chunked_feed_forward(self.ff, norm_hidden_states, self._chunk_dim, self._chunk_size)
537
+ else:
538
+ ff_output = self.ff(norm_hidden_states)
539
+
540
+ if self.is_res:
541
+ hidden_states = ff_output + hidden_states
542
+ else:
543
+ hidden_states = ff_output
544
+
545
+ hidden_states = hidden_states[None, :].reshape(batch_size, seq_length, num_frames, channels)
546
+ hidden_states = hidden_states.permute(0, 2, 1, 3)
547
+ hidden_states = hidden_states.reshape(batch_size * num_frames, seq_length, channels)
548
+
549
+ return hidden_states
550
+
551
+
552
+ class SkipFFTransformerBlock(nn.Module):
553
+ def __init__(
554
+ self,
555
+ dim: int,
556
+ num_attention_heads: int,
557
+ attention_head_dim: int,
558
+ kv_input_dim: int,
559
+ kv_input_dim_proj_use_bias: bool,
560
+ dropout=0.0,
561
+ cross_attention_dim: Optional[int] = None,
562
+ attention_bias: bool = False,
563
+ attention_out_bias: bool = True,
564
+ ):
565
+ super().__init__()
566
+ if kv_input_dim != dim:
567
+ self.kv_mapper = nn.Linear(kv_input_dim, dim, kv_input_dim_proj_use_bias)
568
+ else:
569
+ self.kv_mapper = None
570
+
571
+ self.norm1 = RMSNorm(dim, 1e-06)
572
+
573
+ self.attn1 = Attention(
574
+ query_dim=dim,
575
+ heads=num_attention_heads,
576
+ dim_head=attention_head_dim,
577
+ dropout=dropout,
578
+ bias=attention_bias,
579
+ cross_attention_dim=cross_attention_dim,
580
+ out_bias=attention_out_bias,
581
+ )
582
+
583
+ self.norm2 = RMSNorm(dim, 1e-06)
584
+
585
+ self.attn2 = Attention(
586
+ query_dim=dim,
587
+ cross_attention_dim=cross_attention_dim,
588
+ heads=num_attention_heads,
589
+ dim_head=attention_head_dim,
590
+ dropout=dropout,
591
+ bias=attention_bias,
592
+ out_bias=attention_out_bias,
593
+ )
594
+
595
+ def forward(self, hidden_states, encoder_hidden_states, cross_attention_kwargs):
596
+ cross_attention_kwargs = cross_attention_kwargs.copy() if cross_attention_kwargs is not None else {}
597
+
598
+ if self.kv_mapper is not None:
599
+ encoder_hidden_states = self.kv_mapper(F.silu(encoder_hidden_states))
600
+
601
+ norm_hidden_states = self.norm1(hidden_states)
602
+
603
+ attn_output = self.attn1(
604
+ norm_hidden_states,
605
+ encoder_hidden_states=encoder_hidden_states,
606
+ **cross_attention_kwargs,
607
+ )
608
+
609
+ hidden_states = attn_output + hidden_states
610
+
611
+ norm_hidden_states = self.norm2(hidden_states)
612
+
613
+ attn_output = self.attn2(
614
+ norm_hidden_states,
615
+ encoder_hidden_states=encoder_hidden_states,
616
+ **cross_attention_kwargs,
617
+ )
618
+
619
+ hidden_states = attn_output + hidden_states
620
+
621
+ return hidden_states
622
+
623
+
624
+ class FeedForward(nn.Module):
625
+ r"""
626
+ A feed-forward layer.
627
+
628
+ Parameters:
629
+ dim (`int`): The number of channels in the input.
630
+ dim_out (`int`, *optional*): The number of channels in the output. If not given, defaults to `dim`.
631
+ mult (`int`, *optional*, defaults to 4): The multiplier to use for the hidden dimension.
632
+ dropout (`float`, *optional*, defaults to 0.0): The dropout probability to use.
633
+ activation_fn (`str`, *optional*, defaults to `"geglu"`): Activation function to be used in feed-forward.
634
+ final_dropout (`bool` *optional*, defaults to False): Apply a final dropout.
635
+ bias (`bool`, defaults to True): Whether to use a bias in the linear layer.
636
+ """
637
+
638
+ def __init__(
639
+ self,
640
+ dim: int,
641
+ dim_out: Optional[int] = None,
642
+ mult: int = 4,
643
+ dropout: float = 0.0,
644
+ activation_fn: str = "geglu",
645
+ final_dropout: bool = False,
646
+ inner_dim=None,
647
+ bias: bool = True,
648
+ ):
649
+ super().__init__()
650
+ if inner_dim is None:
651
+ inner_dim = int(dim * mult)
652
+ dim_out = dim_out if dim_out is not None else dim
653
+ linear_cls = nn.Linear
654
+
655
+ if activation_fn == "gelu":
656
+ act_fn = GELU(dim, inner_dim, bias=bias)
657
+ if activation_fn == "gelu-approximate":
658
+ act_fn = GELU(dim, inner_dim, approximate="tanh", bias=bias)
659
+ elif activation_fn == "geglu":
660
+ act_fn = GEGLU(dim, inner_dim, bias=bias)
661
+ elif activation_fn == "geglu-approximate":
662
+ act_fn = ApproximateGELU(dim, inner_dim, bias=bias)
663
+
664
+ self.net = nn.ModuleList([])
665
+ # project in
666
+ self.net.append(act_fn)
667
+ # project dropout
668
+ self.net.append(nn.Dropout(dropout))
669
+ # project out
670
+ self.net.append(linear_cls(inner_dim, dim_out, bias=bias))
671
+ # FF as used in Vision Transformer, MLP-Mixer, etc. have a final dropout
672
+ if final_dropout:
673
+ self.net.append(nn.Dropout(dropout))
674
+
675
+ def forward(self, hidden_states: torch.Tensor, *args, **kwargs) -> torch.Tensor:
676
+ if len(args) > 0 or kwargs.get("scale", None) is not None:
677
+ deprecation_message = "The `scale` argument is deprecated and will be ignored. Please remove it, as passing it will raise an error in the future. `scale` should directly be passed while calling the underlying pipeline component i.e., via `cross_attention_kwargs`."
678
+ deprecate("scale", "1.0.0", deprecation_message)
679
+ for module in self.net:
680
+ hidden_states = module(hidden_states)
681
+ return hidden_states
attention_processor_custom.py ADDED
The diff for this file is too large to render. See raw diff
 
pipeline_stable_diffusion_custom.py ADDED
@@ -0,0 +1,1024 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Adapted from https://github.com/huggingface/diffusers/blob/main/src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion.py
2
+
3
+ import inspect
4
+ from typing import Any, Callable, Dict, List, Optional, Union
5
+
6
+ import torch
7
+ from packaging import version
8
+ from transformers import CLIPImageProcessor, CLIPTextModel, CLIPTokenizer, CLIPVisionModelWithProjection
9
+
10
+ from diffusers.configuration_utils import FrozenDict
11
+ from diffusers.image_processor import PipelineImageInput, VaeImageProcessor
12
+
13
+ from diffusers.loaders import IPAdapterMixin, LoraLoaderMixin, TextualInversionLoaderMixin, FromSingleFileMixin
14
+
15
+ from diffusers.models import AutoencoderKL, ImageProjection, UNet2DConditionModel
16
+ from diffusers.models.lora import adjust_lora_scale_text_encoder
17
+ from diffusers.schedulers import KarrasDiffusionSchedulers
18
+ from diffusers.utils import (
19
+ USE_PEFT_BACKEND,
20
+ deprecate,
21
+ logging,
22
+ replace_example_docstring,
23
+ scale_lora_layers,
24
+ unscale_lora_layers,
25
+ )
26
+ from diffusers.utils.torch_utils import randn_tensor
27
+ from diffusers.pipelines.pipeline_utils import DiffusionPipeline, StableDiffusionMixin
28
+ from diffusers.pipelines.stable_diffusion.pipeline_output import StableDiffusionPipelineOutput
29
+ from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker
30
+
31
+
32
+ logger = logging.get_logger(__name__) # pylint: disable=invalid-name
33
+
34
+ EXAMPLE_DOC_STRING = """
35
+ Examples:
36
+ ```py
37
+ >>> import torch
38
+ >>> from diffusers import StableDiffusionPipeline
39
+
40
+ >>> pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16)
41
+ >>> pipe = pipe.to("cuda")
42
+
43
+ >>> prompt = "a photo of an astronaut riding a horse on mars"
44
+ >>> image = pipe(prompt).images[0]
45
+ ```
46
+ """
47
+
48
+
49
+ def rescale_noise_cfg(noise_cfg, noise_pred_text, guidance_rescale=0.0):
50
+ """
51
+ Rescale `noise_cfg` according to `guidance_rescale`. Based on findings of [Common Diffusion Noise Schedules and
52
+ Sample Steps are Flawed](https://arxiv.org/pdf/2305.08891.pdf). See Section 3.4
53
+ """
54
+ std_text = noise_pred_text.std(dim=list(range(1, noise_pred_text.ndim)), keepdim=True)
55
+ std_cfg = noise_cfg.std(dim=list(range(1, noise_cfg.ndim)), keepdim=True)
56
+ # rescale the results from guidance (fixes overexposure)
57
+ noise_pred_rescaled = noise_cfg * (std_text / std_cfg)
58
+ # mix with the original results from guidance by factor guidance_rescale to avoid "plain looking" images
59
+ noise_cfg = guidance_rescale * noise_pred_rescaled + (1 - guidance_rescale) * noise_cfg
60
+ return noise_cfg
61
+
62
+
63
+ def retrieve_timesteps(
64
+ scheduler,
65
+ num_inference_steps: Optional[int] = None,
66
+ device: Optional[Union[str, torch.device]] = None,
67
+ timesteps: Optional[List[int]] = None,
68
+ **kwargs,
69
+ ):
70
+ """
71
+ Calls the scheduler's `set_timesteps` method and retrieves timesteps from the scheduler after the call. Handles
72
+ custom timesteps. Any kwargs will be supplied to `scheduler.set_timesteps`.
73
+
74
+ Args:
75
+ scheduler (`SchedulerMixin`):
76
+ The scheduler to get timesteps from.
77
+ num_inference_steps (`int`):
78
+ The number of diffusion steps used when generating samples with a pre-trained model. If used,
79
+ `timesteps` must be `None`.
80
+ device (`str` or `torch.device`, *optional*):
81
+ The device to which the timesteps should be moved to. If `None`, the timesteps are not moved.
82
+ timesteps (`List[int]`, *optional*):
83
+ Custom timesteps used to support arbitrary spacing between timesteps. If `None`, then the default
84
+ timestep spacing strategy of the scheduler is used. If `timesteps` is passed, `num_inference_steps`
85
+ must be `None`.
86
+
87
+ Returns:
88
+ `Tuple[torch.Tensor, int]`: A tuple where the first element is the timestep schedule from the scheduler and the
89
+ second element is the number of inference steps.
90
+ """
91
+ if timesteps is not None:
92
+ accepts_timesteps = "timesteps" in set(inspect.signature(scheduler.set_timesteps).parameters.keys())
93
+ if not accepts_timesteps:
94
+ raise ValueError(
95
+ f"The current scheduler class {scheduler.__class__}'s `set_timesteps` does not support custom"
96
+ f" timestep schedules. Please check whether you are using the correct scheduler."
97
+ )
98
+ scheduler.set_timesteps(timesteps=timesteps, device=device, **kwargs)
99
+ timesteps = scheduler.timesteps
100
+ num_inference_steps = len(timesteps)
101
+ else:
102
+ scheduler.set_timesteps(num_inference_steps, device=device, **kwargs)
103
+ timesteps = scheduler.timesteps
104
+ return timesteps, num_inference_steps
105
+
106
+
107
+ class StableDiffusionPipeline(
108
+ DiffusionPipeline,
109
+ StableDiffusionMixin,
110
+ TextualInversionLoaderMixin,
111
+ LoraLoaderMixin,
112
+ IPAdapterMixin,
113
+ FromSingleFileMixin,
114
+ ):
115
+ r"""
116
+ Pipeline for text-to-image generation using Stable Diffusion.
117
+
118
+ This model inherits from [`DiffusionPipeline`]. Check the superclass documentation for the generic methods
119
+ implemented for all pipelines (downloading, saving, running on a particular device, etc.).
120
+
121
+ The pipeline also inherits the following loading methods:
122
+ - [`~loaders.TextualInversionLoaderMixin.load_textual_inversion`] for loading textual inversion embeddings
123
+ - [`~loaders.LoraLoaderMixin.load_lora_weights`] for loading LoRA weights
124
+ - [`~loaders.LoraLoaderMixin.save_lora_weights`] for saving LoRA weights
125
+ - [`~loaders.FromSingleFileMixin.from_single_file`] for loading `.ckpt` files
126
+ - [`~loaders.IPAdapterMixin.load_ip_adapter`] for loading IP Adapters
127
+
128
+ Args:
129
+ vae ([`AutoencoderKL`]):
130
+ Variational Auto-Encoder (VAE) model to encode and decode images to and from latent representations.
131
+ text_encoder ([`~transformers.CLIPTextModel`]):
132
+ Frozen text-encoder ([clip-vit-large-patch14](https://huggingface.co/openai/clip-vit-large-patch14)).
133
+ tokenizer ([`~transformers.CLIPTokenizer`]):
134
+ A `CLIPTokenizer` to tokenize text.
135
+ unet ([`UNet2DConditionModel`]):
136
+ A `UNet2DConditionModel` to denoise the encoded image latents.
137
+ scheduler ([`SchedulerMixin`]):
138
+ A scheduler to be used in combination with `unet` to denoise the encoded image latents. Can be one of
139
+ [`DDIMScheduler`], [`LMSDiscreteScheduler`], or [`PNDMScheduler`].
140
+ safety_checker ([`StableDiffusionSafetyChecker`]):
141
+ Classification module that estimates whether generated images could be considered offensive or harmful.
142
+ Please refer to the [model card](https://huggingface.co/runwayml/stable-diffusion-v1-5) for more details
143
+ about a model's potential harms.
144
+ feature_extractor ([`~transformers.CLIPImageProcessor`]):
145
+ A `CLIPImageProcessor` to extract features from generated images; used as inputs to the `safety_checker`.
146
+ """
147
+
148
+ model_cpu_offload_seq = "text_encoder->image_encoder->unet->vae"
149
+ _optional_components = ["safety_checker", "feature_extractor", "image_encoder"]
150
+ _exclude_from_cpu_offload = ["safety_checker"]
151
+ _callback_tensor_inputs = ["latents", "prompt_embeds", "negative_prompt_embeds"]
152
+
153
+ def __init__(
154
+ self,
155
+ vae: AutoencoderKL,
156
+ text_encoder: CLIPTextModel,
157
+ tokenizer: CLIPTokenizer,
158
+ unet: UNet2DConditionModel,
159
+ scheduler: KarrasDiffusionSchedulers,
160
+ safety_checker: StableDiffusionSafetyChecker,
161
+ feature_extractor: CLIPImageProcessor,
162
+ image_encoder: CLIPVisionModelWithProjection = None,
163
+ requires_safety_checker: bool = True,
164
+ ):
165
+ super().__init__()
166
+
167
+ if hasattr(scheduler.config, "steps_offset") and scheduler.config.steps_offset != 1:
168
+ deprecation_message = (
169
+ f"The configuration file of this scheduler: {scheduler} is outdated. `steps_offset`"
170
+ f" should be set to 1 instead of {scheduler.config.steps_offset}. Please make sure "
171
+ "to update the config accordingly as leaving `steps_offset` might led to incorrect results"
172
+ " in future versions. If you have downloaded this checkpoint from the Hugging Face Hub,"
173
+ " it would be very nice if you could open a Pull request for the `scheduler/scheduler_config.json`"
174
+ " file"
175
+ )
176
+ deprecate("steps_offset!=1", "1.0.0", deprecation_message, standard_warn=False)
177
+ new_config = dict(scheduler.config)
178
+ new_config["steps_offset"] = 1
179
+ scheduler._internal_dict = FrozenDict(new_config)
180
+
181
+ if hasattr(scheduler.config, "clip_sample") and scheduler.config.clip_sample is True:
182
+ deprecation_message = (
183
+ f"The configuration file of this scheduler: {scheduler} has not set the configuration `clip_sample`."
184
+ " `clip_sample` should be set to False in the configuration file. Please make sure to update the"
185
+ " config accordingly as not setting `clip_sample` in the config might lead to incorrect results in"
186
+ " future versions. If you have downloaded this checkpoint from the Hugging Face Hub, it would be very"
187
+ " nice if you could open a Pull request for the `scheduler/scheduler_config.json` file"
188
+ )
189
+ deprecate("clip_sample not set", "1.0.0", deprecation_message, standard_warn=False)
190
+ new_config = dict(scheduler.config)
191
+ new_config["clip_sample"] = False
192
+ scheduler._internal_dict = FrozenDict(new_config)
193
+
194
+ if safety_checker is None and requires_safety_checker:
195
+ logger.warning(
196
+ f"You have disabled the safety checker for {self.__class__} by passing `safety_checker=None`. Ensure"
197
+ " that you abide to the conditions of the Stable Diffusion license and do not expose unfiltered"
198
+ " results in services or applications open to the public. Both the diffusers team and Hugging Face"
199
+ " strongly recommend to keep the safety filter enabled in all public facing circumstances, disabling"
200
+ " it only for use-cases that involve analyzing network behavior or auditing its results. For more"
201
+ " information, please have a look at https://github.com/huggingface/diffusers/pull/254 ."
202
+ )
203
+
204
+ if safety_checker is not None and feature_extractor is None:
205
+ raise ValueError(
206
+ "Make sure to define a feature extractor when loading {self.__class__} if you want to use the safety"
207
+ " checker. If you do not want to use the safety checker, you can pass `'safety_checker=None'` instead."
208
+ )
209
+
210
+ is_unet_version_less_0_9_0 = hasattr(unet.config, "_diffusers_version") and version.parse(
211
+ version.parse(unet.config._diffusers_version).base_version
212
+ ) < version.parse("0.9.0.dev0")
213
+ is_unet_sample_size_less_64 = hasattr(unet.config, "sample_size") and unet.config.sample_size < 64
214
+ if is_unet_version_less_0_9_0 and is_unet_sample_size_less_64:
215
+ deprecation_message = (
216
+ "The configuration file of the unet has set the default `sample_size` to smaller than"
217
+ " 64 which seems highly unlikely. If your checkpoint is a fine-tuned version of any of the"
218
+ " following: \n- CompVis/stable-diffusion-v1-4 \n- CompVis/stable-diffusion-v1-3 \n-"
219
+ " CompVis/stable-diffusion-v1-2 \n- CompVis/stable-diffusion-v1-1 \n- runwayml/stable-diffusion-v1-5"
220
+ " \n- runwayml/stable-diffusion-inpainting \n you should change 'sample_size' to 64 in the"
221
+ " configuration file. Please make sure to update the config accordingly as leaving `sample_size=32`"
222
+ " in the config might lead to incorrect results in future versions. If you have downloaded this"
223
+ " checkpoint from the Hugging Face Hub, it would be very nice if you could open a Pull request for"
224
+ " the `unet/config.json` file"
225
+ )
226
+ deprecate("sample_size<64", "1.0.0", deprecation_message, standard_warn=False)
227
+ new_config = dict(unet.config)
228
+ new_config["sample_size"] = 64
229
+ unet._internal_dict = FrozenDict(new_config)
230
+
231
+ self.register_modules(
232
+ vae=vae,
233
+ text_encoder=text_encoder,
234
+ tokenizer=tokenizer,
235
+ unet=unet,
236
+ scheduler=scheduler,
237
+ safety_checker=safety_checker,
238
+ feature_extractor=feature_extractor,
239
+ image_encoder=image_encoder,
240
+ )
241
+ self.vae_scale_factor = 2 ** (len(self.vae.config.block_out_channels) - 1)
242
+ self.image_processor = VaeImageProcessor(vae_scale_factor=self.vae_scale_factor)
243
+ self.register_to_config(requires_safety_checker=requires_safety_checker)
244
+
245
+ def _encode_prompt(
246
+ self,
247
+ prompt,
248
+ device,
249
+ num_images_per_prompt,
250
+ do_classifier_free_guidance,
251
+ negative_prompt=None,
252
+ prompt_embeds: Optional[torch.FloatTensor] = None,
253
+ negative_prompt_embeds: Optional[torch.FloatTensor] = None,
254
+ lora_scale: Optional[float] = None,
255
+ **kwargs,
256
+ ):
257
+ deprecation_message = "`_encode_prompt()` is deprecated and it will be removed in a future version. Use `encode_prompt()` instead. Also, be aware that the output format changed from a concatenated tensor to a tuple."
258
+ deprecate("_encode_prompt()", "1.0.0", deprecation_message, standard_warn=False)
259
+
260
+ prompt_embeds_tuple = self.encode_prompt(
261
+ prompt=prompt,
262
+ device=device,
263
+ num_images_per_prompt=num_images_per_prompt,
264
+ do_classifier_free_guidance=do_classifier_free_guidance,
265
+ negative_prompt=negative_prompt,
266
+ prompt_embeds=prompt_embeds,
267
+ negative_prompt_embeds=negative_prompt_embeds,
268
+ lora_scale=lora_scale,
269
+ **kwargs,
270
+ )
271
+
272
+ # concatenate for backwards comp
273
+ prompt_embeds = torch.cat([prompt_embeds_tuple[1], prompt_embeds_tuple[0]])
274
+
275
+ return prompt_embeds
276
+
277
+ def encode_prompt(
278
+ self,
279
+ prompt,
280
+ device,
281
+ num_images_per_prompt,
282
+ do_classifier_free_guidance,
283
+ negative_prompt=None,
284
+ prompt_embeds: Optional[torch.FloatTensor] = None,
285
+ negative_prompt_embeds: Optional[torch.FloatTensor] = None,
286
+ lora_scale: Optional[float] = None,
287
+ clip_skip: Optional[int] = None,
288
+ ):
289
+ r"""
290
+ Encodes the prompt into text encoder hidden states.
291
+
292
+ Args:
293
+ prompt (`str` or `List[str]`, *optional*):
294
+ prompt to be encoded
295
+ device: (`torch.device`):
296
+ torch device
297
+ num_images_per_prompt (`int`):
298
+ number of images that should be generated per prompt
299
+ do_classifier_free_guidance (`bool`):
300
+ whether to use classifier free guidance or not
301
+ negative_prompt (`str` or `List[str]`, *optional*):
302
+ The prompt or prompts not to guide the image generation. If not defined, one has to pass
303
+ `negative_prompt_embeds` instead. Ignored when not using guidance (i.e., ignored if `guidance_scale` is
304
+ less than `1`).
305
+ prompt_embeds (`torch.FloatTensor`, *optional*):
306
+ Pre-generated text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not
307
+ provided, text embeddings will be generated from `prompt` input argument.
308
+ negative_prompt_embeds (`torch.FloatTensor`, *optional*):
309
+ Pre-generated negative text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt
310
+ weighting. If not provided, negative_prompt_embeds will be generated from `negative_prompt` input
311
+ argument.
312
+ lora_scale (`float`, *optional*):
313
+ A LoRA scale that will be applied to all LoRA layers of the text encoder if LoRA layers are loaded.
314
+ clip_skip (`int`, *optional*):
315
+ Number of layers to be skipped from CLIP while computing the prompt embeddings. A value of 1 means that
316
+ the output of the pre-final layer will be used for computing the prompt embeddings.
317
+ """
318
+ # set lora scale so that monkey patched LoRA
319
+ # function of text encoder can correctly access it
320
+ if lora_scale is not None and isinstance(self, LoraLoaderMixin):
321
+ self._lora_scale = lora_scale
322
+
323
+ # dynamically adjust the LoRA scale
324
+ if not USE_PEFT_BACKEND:
325
+ adjust_lora_scale_text_encoder(self.text_encoder, lora_scale)
326
+ else:
327
+ scale_lora_layers(self.text_encoder, lora_scale)
328
+
329
+ if prompt is not None and isinstance(prompt, str):
330
+ batch_size = 1
331
+ elif prompt is not None and isinstance(prompt, list):
332
+ batch_size = len(prompt)
333
+ else:
334
+ batch_size = prompt_embeds.shape[0]
335
+
336
+ if prompt_embeds is None:
337
+ # textual inversion: process multi-vector tokens if necessary
338
+ if isinstance(self, TextualInversionLoaderMixin):
339
+ prompt = self.maybe_convert_prompt(prompt, self.tokenizer)
340
+
341
+ text_inputs = self.tokenizer(
342
+ prompt,
343
+ padding="max_length",
344
+ max_length=self.tokenizer.model_max_length,
345
+ truncation=True,
346
+ return_tensors="pt",
347
+ )
348
+ text_input_ids = text_inputs.input_ids
349
+ untruncated_ids = self.tokenizer(prompt, padding="longest", return_tensors="pt").input_ids
350
+
351
+ if untruncated_ids.shape[-1] >= text_input_ids.shape[-1] and not torch.equal(
352
+ text_input_ids, untruncated_ids
353
+ ):
354
+ removed_text = self.tokenizer.batch_decode(
355
+ untruncated_ids[:, self.tokenizer.model_max_length - 1 : -1]
356
+ )
357
+ logger.warning(
358
+ "The following part of your input was truncated because CLIP can only handle sequences up to"
359
+ f" {self.tokenizer.model_max_length} tokens: {removed_text}"
360
+ )
361
+
362
+ if hasattr(self.text_encoder.config, "use_attention_mask") and self.text_encoder.config.use_attention_mask:
363
+ attention_mask = text_inputs.attention_mask.to(device)
364
+ else:
365
+ attention_mask = None
366
+
367
+ if clip_skip is None:
368
+ prompt_embeds = self.text_encoder(text_input_ids.to(device), attention_mask=attention_mask)
369
+ prompt_embeds = prompt_embeds[0]
370
+ else:
371
+ prompt_embeds = self.text_encoder(
372
+ text_input_ids.to(device), attention_mask=attention_mask, output_hidden_states=True
373
+ )
374
+ # Access the `hidden_states` first, that contains a tuple of
375
+ # all the hidden states from the encoder layers. Then index into
376
+ # the tuple to access the hidden states from the desired layer.
377
+ prompt_embeds = prompt_embeds[-1][-(clip_skip + 1)]
378
+ # We also need to apply the final LayerNorm here to not mess with the
379
+ # representations. The `last_hidden_states` that we typically use for
380
+ # obtaining the final prompt representations passes through the LayerNorm
381
+ # layer.
382
+ prompt_embeds = self.text_encoder.text_model.final_layer_norm(prompt_embeds)
383
+
384
+ if self.text_encoder is not None:
385
+ prompt_embeds_dtype = self.text_encoder.dtype
386
+ elif self.unet is not None:
387
+ prompt_embeds_dtype = self.unet.dtype
388
+ else:
389
+ prompt_embeds_dtype = prompt_embeds.dtype
390
+
391
+ prompt_embeds = prompt_embeds.to(dtype=prompt_embeds_dtype, device=device)
392
+
393
+ bs_embed, seq_len, _ = prompt_embeds.shape
394
+ # duplicate text embeddings for each generation per prompt, using mps friendly method
395
+ prompt_embeds = prompt_embeds.repeat(1, num_images_per_prompt, 1)
396
+ prompt_embeds = prompt_embeds.view(bs_embed * num_images_per_prompt, seq_len, -1)
397
+
398
+ # get unconditional embeddings for classifier free guidance
399
+ if do_classifier_free_guidance and negative_prompt_embeds is None:
400
+ uncond_tokens: List[str]
401
+ if negative_prompt is None:
402
+ uncond_tokens = [""] * batch_size
403
+ elif prompt is not None and type(prompt) is not type(negative_prompt):
404
+ raise TypeError(
405
+ f"`negative_prompt` should be the same type to `prompt`, but got {type(negative_prompt)} !="
406
+ f" {type(prompt)}."
407
+ )
408
+ elif isinstance(negative_prompt, str):
409
+ uncond_tokens = [negative_prompt]
410
+ elif batch_size != len(negative_prompt):
411
+ raise ValueError(
412
+ f"`negative_prompt`: {negative_prompt} has batch size {len(negative_prompt)}, but `prompt`:"
413
+ f" {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches"
414
+ " the batch size of `prompt`."
415
+ )
416
+ else:
417
+ uncond_tokens = negative_prompt
418
+
419
+ # textual inversion: process multi-vector tokens if necessary
420
+ if isinstance(self, TextualInversionLoaderMixin):
421
+ uncond_tokens = self.maybe_convert_prompt(uncond_tokens, self.tokenizer)
422
+
423
+ max_length = prompt_embeds.shape[1]
424
+ uncond_input = self.tokenizer(
425
+ uncond_tokens,
426
+ padding="max_length",
427
+ max_length=max_length,
428
+ truncation=True,
429
+ return_tensors="pt",
430
+ )
431
+
432
+ if hasattr(self.text_encoder.config, "use_attention_mask") and self.text_encoder.config.use_attention_mask:
433
+ attention_mask = uncond_input.attention_mask.to(device)
434
+ else:
435
+ attention_mask = None
436
+
437
+ negative_prompt_embeds = self.text_encoder(
438
+ uncond_input.input_ids.to(device),
439
+ attention_mask=attention_mask,
440
+ )
441
+ negative_prompt_embeds = negative_prompt_embeds[0]
442
+
443
+ if do_classifier_free_guidance:
444
+ # duplicate unconditional embeddings for each generation per prompt, using mps friendly method
445
+ seq_len = negative_prompt_embeds.shape[1]
446
+
447
+ negative_prompt_embeds = negative_prompt_embeds.to(dtype=prompt_embeds_dtype, device=device)
448
+
449
+ negative_prompt_embeds = negative_prompt_embeds.repeat(1, num_images_per_prompt, 1)
450
+ negative_prompt_embeds = negative_prompt_embeds.view(batch_size * num_images_per_prompt, seq_len, -1)
451
+
452
+ if isinstance(self, LoraLoaderMixin) and USE_PEFT_BACKEND:
453
+ # Retrieve the original scale by scaling back the LoRA layers
454
+ unscale_lora_layers(self.text_encoder, lora_scale)
455
+
456
+ return prompt_embeds, negative_prompt_embeds
457
+
458
+ def encode_image(self, image, device, num_images_per_prompt, output_hidden_states=None):
459
+ dtype = next(self.image_encoder.parameters()).dtype
460
+
461
+ if not isinstance(image, torch.Tensor):
462
+ image = self.feature_extractor(image, return_tensors="pt").pixel_values
463
+
464
+ image = image.to(device=device, dtype=dtype)
465
+ if output_hidden_states:
466
+ image_enc_hidden_states = self.image_encoder(image, output_hidden_states=True).hidden_states[-2]
467
+ image_enc_hidden_states = image_enc_hidden_states.repeat_interleave(num_images_per_prompt, dim=0)
468
+ uncond_image_enc_hidden_states = self.image_encoder(
469
+ torch.zeros_like(image), output_hidden_states=True
470
+ ).hidden_states[-2]
471
+ uncond_image_enc_hidden_states = uncond_image_enc_hidden_states.repeat_interleave(
472
+ num_images_per_prompt, dim=0
473
+ )
474
+ return image_enc_hidden_states, uncond_image_enc_hidden_states
475
+ else:
476
+ image_embeds = self.image_encoder(image).image_embeds
477
+ image_embeds = image_embeds.repeat_interleave(num_images_per_prompt, dim=0)
478
+ uncond_image_embeds = torch.zeros_like(image_embeds)
479
+
480
+ return image_embeds, uncond_image_embeds
481
+
482
+ def prepare_ip_adapter_image_embeds(
483
+ self, ip_adapter_image, ip_adapter_image_embeds, device, num_images_per_prompt, do_classifier_free_guidance
484
+ ):
485
+ if ip_adapter_image_embeds is None:
486
+ if not isinstance(ip_adapter_image, list):
487
+ ip_adapter_image = [ip_adapter_image]
488
+
489
+ if len(ip_adapter_image) != len(self.unet.encoder_hid_proj.image_projection_layers):
490
+ raise ValueError(
491
+ f"`ip_adapter_image` must have same length as the number of IP Adapters. Got {len(ip_adapter_image)} images and {len(self.unet.encoder_hid_proj.image_projection_layers)} IP Adapters."
492
+ )
493
+
494
+ image_embeds = []
495
+ for single_ip_adapter_image, image_proj_layer in zip(
496
+ ip_adapter_image, self.unet.encoder_hid_proj.image_projection_layers
497
+ ):
498
+ output_hidden_state = not isinstance(image_proj_layer, ImageProjection)
499
+ single_image_embeds, single_negative_image_embeds = self.encode_image(
500
+ single_ip_adapter_image, device, 1, output_hidden_state
501
+ )
502
+ single_image_embeds = torch.stack([single_image_embeds] * num_images_per_prompt, dim=0)
503
+ single_negative_image_embeds = torch.stack(
504
+ [single_negative_image_embeds] * num_images_per_prompt, dim=0
505
+ )
506
+
507
+ if do_classifier_free_guidance:
508
+ single_image_embeds = torch.cat([single_negative_image_embeds, single_image_embeds])
509
+ single_image_embeds = single_image_embeds.to(device)
510
+
511
+ image_embeds.append(single_image_embeds)
512
+ else:
513
+ repeat_dims = [1]
514
+ image_embeds = []
515
+ for single_image_embeds in ip_adapter_image_embeds:
516
+ if do_classifier_free_guidance:
517
+ single_negative_image_embeds, single_image_embeds = single_image_embeds.chunk(2)
518
+ single_image_embeds = single_image_embeds.repeat(
519
+ num_images_per_prompt, *(repeat_dims * len(single_image_embeds.shape[1:]))
520
+ )
521
+ single_negative_image_embeds = single_negative_image_embeds.repeat(
522
+ num_images_per_prompt, *(repeat_dims * len(single_negative_image_embeds.shape[1:]))
523
+ )
524
+ single_image_embeds = torch.cat([single_negative_image_embeds, single_image_embeds])
525
+ else:
526
+ single_image_embeds = single_image_embeds.repeat(
527
+ num_images_per_prompt, *(repeat_dims * len(single_image_embeds.shape[1:]))
528
+ )
529
+ image_embeds.append(single_image_embeds)
530
+
531
+ return image_embeds
532
+
533
+ def run_safety_checker(self, image, device, dtype):
534
+ if self.safety_checker is None:
535
+ has_nsfw_concept = None
536
+ else:
537
+ if torch.is_tensor(image):
538
+ feature_extractor_input = self.image_processor.postprocess(image, output_type="pil")
539
+ else:
540
+ feature_extractor_input = self.image_processor.numpy_to_pil(image)
541
+ safety_checker_input = self.feature_extractor(feature_extractor_input, return_tensors="pt").to(device)
542
+ image, has_nsfw_concept = self.safety_checker(
543
+ images=image, clip_input=safety_checker_input.pixel_values.to(dtype)
544
+ )
545
+ return image, has_nsfw_concept
546
+
547
+ def decode_latents(self, latents):
548
+ deprecation_message = "The decode_latents method is deprecated and will be removed in 1.0.0. Please use VaeImageProcessor.postprocess(...) instead"
549
+ deprecate("decode_latents", "1.0.0", deprecation_message, standard_warn=False)
550
+
551
+ latents = 1 / self.vae.config.scaling_factor * latents
552
+ image = self.vae.decode(latents, return_dict=False)[0]
553
+ image = (image / 2 + 0.5).clamp(0, 1)
554
+ # we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16
555
+ image = image.cpu().permute(0, 2, 3, 1).float().numpy()
556
+ return image
557
+
558
+ def prepare_extra_step_kwargs(self, generator, eta):
559
+ # prepare extra kwargs for the scheduler step, since not all schedulers have the same signature
560
+ # eta (η) is only used with the DDIMScheduler, it will be ignored for other schedulers.
561
+ # eta corresponds to η in DDIM paper: https://arxiv.org/abs/2010.02502
562
+ # and should be between [0, 1]
563
+
564
+ accepts_eta = "eta" in set(inspect.signature(self.scheduler.step).parameters.keys())
565
+ extra_step_kwargs = {}
566
+ if accepts_eta:
567
+ extra_step_kwargs["eta"] = eta
568
+
569
+ # check if the scheduler accepts generator
570
+ accepts_generator = "generator" in set(inspect.signature(self.scheduler.step).parameters.keys())
571
+ if accepts_generator:
572
+ extra_step_kwargs["generator"] = generator
573
+ return extra_step_kwargs
574
+
575
+ def check_inputs(
576
+ self,
577
+ prompt,
578
+ height,
579
+ width,
580
+ callback_steps,
581
+ negative_prompt=None,
582
+ prompt_embeds=None,
583
+ negative_prompt_embeds=None,
584
+ ip_adapter_image=None,
585
+ ip_adapter_image_embeds=None,
586
+ callback_on_step_end_tensor_inputs=None,
587
+ ):
588
+ if height % 8 != 0 or width % 8 != 0:
589
+ raise ValueError(f"`height` and `width` have to be divisible by 8 but are {height} and {width}.")
590
+
591
+ if callback_steps is not None and (not isinstance(callback_steps, int) or callback_steps <= 0):
592
+ raise ValueError(
593
+ f"`callback_steps` has to be a positive integer but is {callback_steps} of type"
594
+ f" {type(callback_steps)}."
595
+ )
596
+ if callback_on_step_end_tensor_inputs is not None and not all(
597
+ k in self._callback_tensor_inputs for k in callback_on_step_end_tensor_inputs
598
+ ):
599
+ raise ValueError(
600
+ f"`callback_on_step_end_tensor_inputs` has to be in {self._callback_tensor_inputs}, but found {[k for k in callback_on_step_end_tensor_inputs if k not in self._callback_tensor_inputs]}"
601
+ )
602
+
603
+ if prompt is not None and prompt_embeds is not None:
604
+ raise ValueError(
605
+ f"Cannot forward both `prompt`: {prompt} and `prompt_embeds`: {prompt_embeds}. Please make sure to"
606
+ " only forward one of the two."
607
+ )
608
+ elif prompt is None and prompt_embeds is None:
609
+ raise ValueError(
610
+ "Provide either `prompt` or `prompt_embeds`. Cannot leave both `prompt` and `prompt_embeds` undefined."
611
+ )
612
+ elif prompt is not None and (not isinstance(prompt, str) and not isinstance(prompt, list)):
613
+ raise ValueError(f"`prompt` has to be of type `str` or `list` but is {type(prompt)}")
614
+
615
+ if negative_prompt is not None and negative_prompt_embeds is not None:
616
+ raise ValueError(
617
+ f"Cannot forward both `negative_prompt`: {negative_prompt} and `negative_prompt_embeds`:"
618
+ f" {negative_prompt_embeds}. Please make sure to only forward one of the two."
619
+ )
620
+
621
+ if prompt_embeds is not None and negative_prompt_embeds is not None:
622
+ if prompt_embeds.shape != negative_prompt_embeds.shape:
623
+ raise ValueError(
624
+ "`prompt_embeds` and `negative_prompt_embeds` must have the same shape when passed directly, but"
625
+ f" got: `prompt_embeds` {prompt_embeds.shape} != `negative_prompt_embeds`"
626
+ f" {negative_prompt_embeds.shape}."
627
+ )
628
+
629
+ if ip_adapter_image is not None and ip_adapter_image_embeds is not None:
630
+ raise ValueError(
631
+ "Provide either `ip_adapter_image` or `ip_adapter_image_embeds`. Cannot leave both `ip_adapter_image` and `ip_adapter_image_embeds` defined."
632
+ )
633
+
634
+ if ip_adapter_image_embeds is not None:
635
+ if not isinstance(ip_adapter_image_embeds, list):
636
+ raise ValueError(
637
+ f"`ip_adapter_image_embeds` has to be of type `list` but is {type(ip_adapter_image_embeds)}"
638
+ )
639
+ elif ip_adapter_image_embeds[0].ndim not in [3, 4]:
640
+ raise ValueError(
641
+ f"`ip_adapter_image_embeds` has to be a list of 3D or 4D tensors but is {ip_adapter_image_embeds[0].ndim}D"
642
+ )
643
+
644
+ def prepare_latents(self, batch_size, num_channels_latents, height, width, dtype, device, generator, latents=None):
645
+ shape = (batch_size, num_channels_latents, height // self.vae_scale_factor, width // self.vae_scale_factor)
646
+ if isinstance(generator, list) and len(generator) != batch_size:
647
+ raise ValueError(
648
+ f"You have passed a list of generators of length {len(generator)}, but requested an effective batch"
649
+ f" size of {batch_size}. Make sure the batch size matches the length of the generators."
650
+ )
651
+
652
+ if latents is None:
653
+ latents = randn_tensor(shape, generator=generator, device=device, dtype=dtype)
654
+ else:
655
+ latents = latents.to(device)
656
+
657
+ # scale the initial noise by the standard deviation required by the scheduler
658
+ latents = latents * self.scheduler.init_noise_sigma
659
+ return latents
660
+
661
+ # Copied from diffusers.pipelines.latent_consistency_models.pipeline_latent_consistency_text2img.LatentConsistencyModelPipeline.get_guidance_scale_embedding
662
+ def get_guidance_scale_embedding(self, w, embedding_dim=512, dtype=torch.float32):
663
+ """
664
+ See https://github.com/google-research/vdm/blob/dc27b98a554f65cdc654b800da5aa1846545d41b/model_vdm.py#L298
665
+
666
+ Args:
667
+ timesteps (`torch.Tensor`):
668
+ generate embedding vectors at these timesteps
669
+ embedding_dim (`int`, *optional*, defaults to 512):
670
+ dimension of the embeddings to generate
671
+ dtype:
672
+ data type of the generated embeddings
673
+
674
+ Returns:
675
+ `torch.FloatTensor`: Embedding vectors with shape `(len(timesteps), embedding_dim)`
676
+ """
677
+ assert len(w.shape) == 1
678
+ w = w * 1000.0
679
+
680
+ half_dim = embedding_dim // 2
681
+ emb = torch.log(torch.tensor(10000.0)) / (half_dim - 1)
682
+ emb = torch.exp(torch.arange(half_dim, dtype=dtype) * -emb)
683
+ emb = w.to(dtype)[:, None] * emb[None, :]
684
+ emb = torch.cat([torch.sin(emb), torch.cos(emb)], dim=1)
685
+ if embedding_dim % 2 == 1: # zero pad
686
+ emb = torch.nn.functional.pad(emb, (0, 1))
687
+ assert emb.shape == (w.shape[0], embedding_dim)
688
+ return emb
689
+
690
+ @property
691
+ def guidance_scale(self):
692
+ return self._guidance_scale
693
+
694
+ @property
695
+ def guidance_rescale(self):
696
+ return self._guidance_rescale
697
+
698
+ @property
699
+ def clip_skip(self):
700
+ return self._clip_skip
701
+
702
+ # here `guidance_scale` is defined analog to the guidance weight `w` of equation (2)
703
+ # of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1`
704
+ # corresponds to doing no classifier free guidance.
705
+ @property
706
+ def do_classifier_free_guidance(self):
707
+ return self._guidance_scale > 1 and self.unet.config.time_cond_proj_dim is None
708
+
709
+ @property
710
+ def cross_attention_kwargs(self):
711
+ return self._cross_attention_kwargs
712
+
713
+ @property
714
+ def num_timesteps(self):
715
+ return self._num_timesteps
716
+
717
+ @property
718
+ def interrupt(self):
719
+ return self._interrupt
720
+
721
+ @torch.no_grad()
722
+ @replace_example_docstring(EXAMPLE_DOC_STRING)
723
+ def __call__(
724
+ self,
725
+ prompt: Union[str, List[str]] = None,
726
+ height: Optional[int] = None,
727
+ width: Optional[int] = None,
728
+ num_inference_steps: int = 50,
729
+ timesteps: List[int] = None,
730
+ guidance_scale: float = 7.5,
731
+ negative_prompt: Optional[Union[str, List[str]]] = None,
732
+ num_images_per_prompt: Optional[int] = 1,
733
+ eta: float = 0.0,
734
+ generator: Optional[Union[torch.Generator, List[torch.Generator]]] = None,
735
+ latents: Optional[torch.FloatTensor] = None,
736
+ prompt_embeds: Optional[torch.FloatTensor] = None,
737
+ negative_prompt_embeds: Optional[torch.FloatTensor] = None,
738
+ ip_adapter_image: Optional[PipelineImageInput] = None,
739
+ ip_adapter_image_embeds: Optional[List[torch.FloatTensor]] = None,
740
+ output_type: Optional[str] = "pil",
741
+ return_dict: bool = True,
742
+ cross_attention_kwargs: Optional[Dict[str, Any]] = None,
743
+ guidance_rescale: float = 0.0,
744
+ clip_skip: Optional[int] = None,
745
+ callback_on_step_end: Optional[Callable[[int, int, Dict], None]] = None,
746
+ callback_on_step_end_tensor_inputs: List[str] = ["latents"],
747
+ audio_context: Optional[torch.FloatTensor] = None,
748
+ **kwargs,
749
+ ):
750
+ r"""
751
+ The call function to the pipeline for generation.
752
+
753
+ Args:
754
+ prompt (`str` or `List[str]`, *optional*):
755
+ The prompt or prompts to guide image generation. If not defined, you need to pass `prompt_embeds`.
756
+ height (`int`, *optional*, defaults to `self.unet.config.sample_size * self.vae_scale_factor`):
757
+ The height in pixels of the generated image.
758
+ width (`int`, *optional*, defaults to `self.unet.config.sample_size * self.vae_scale_factor`):
759
+ The width in pixels of the generated image.
760
+ num_inference_steps (`int`, *optional*, defaults to 50):
761
+ The number of denoising steps. More denoising steps usually lead to a higher quality image at the
762
+ expense of slower inference.
763
+ timesteps (`List[int]`, *optional*):
764
+ Custom timesteps to use for the denoising process with schedulers which support a `timesteps` argument
765
+ in their `set_timesteps` method. If not defined, the default behavior when `num_inference_steps` is
766
+ passed will be used. Must be in descending order.
767
+ guidance_scale (`float`, *optional*, defaults to 7.5):
768
+ A higher guidance scale value encourages the model to generate images closely linked to the text
769
+ `prompt` at the expense of lower image quality. Guidance scale is enabled when `guidance_scale > 1`.
770
+ negative_prompt (`str` or `List[str]`, *optional*):
771
+ The prompt or prompts to guide what to not include in image generation. If not defined, you need to
772
+ pass `negative_prompt_embeds` instead. Ignored when not using guidance (`guidance_scale < 1`).
773
+ num_images_per_prompt (`int`, *optional*, defaults to 1):
774
+ The number of images to generate per prompt.
775
+ eta (`float`, *optional*, defaults to 0.0):
776
+ Corresponds to parameter eta (η) from the [DDIM](https://arxiv.org/abs/2010.02502) paper. Only applies
777
+ to the [`~schedulers.DDIMScheduler`], and is ignored in other schedulers.
778
+ generator (`torch.Generator` or `List[torch.Generator]`, *optional*):
779
+ A [`torch.Generator`](https://pytorch.org/docs/stable/generated/torch.Generator.html) to make
780
+ generation deterministic.
781
+ latents (`torch.FloatTensor`, *optional*):
782
+ Pre-generated noisy latents sampled from a Gaussian distribution, to be used as inputs for image
783
+ generation. Can be used to tweak the same generation with different prompts. If not provided, a latents
784
+ tensor is generated by sampling using the supplied random `generator`.
785
+ prompt_embeds (`torch.FloatTensor`, *optional*):
786
+ Pre-generated text embeddings. Can be used to easily tweak text inputs (prompt weighting). If not
787
+ provided, text embeddings are generated from the `prompt` input argument.
788
+ negative_prompt_embeds (`torch.FloatTensor`, *optional*):
789
+ Pre-generated negative text embeddings. Can be used to easily tweak text inputs (prompt weighting). If
790
+ not provided, `negative_prompt_embeds` are generated from the `negative_prompt` input argument.
791
+ ip_adapter_image: (`PipelineImageInput`, *optional*): Optional image input to work with IP Adapters.
792
+ ip_adapter_image_embeds (`List[torch.FloatTensor]`, *optional*):
793
+ Pre-generated image embeddings for IP-Adapter. It should be a list of length same as number of IP-adapters.
794
+ Each element should be a tensor of shape `(batch_size, num_images, emb_dim)`. It should contain the negative image embedding
795
+ if `do_classifier_free_guidance` is set to `True`.
796
+ If not provided, embeddings are computed from the `ip_adapter_image` input argument.
797
+ output_type (`str`, *optional*, defaults to `"pil"`):
798
+ The output format of the generated image. Choose between `PIL.Image` or `np.array`.
799
+ return_dict (`bool`, *optional*, defaults to `True`):
800
+ Whether or not to return a [`~pipelines.stable_diffusion.StableDiffusionPipelineOutput`] instead of a
801
+ plain tuple.
802
+ cross_attention_kwargs (`dict`, *optional*):
803
+ A kwargs dictionary that if specified is passed along to the [`AttentionProcessor`] as defined in
804
+ [`self.processor`](https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/attention_processor.py).
805
+ guidance_rescale (`float`, *optional*, defaults to 0.0):
806
+ Guidance rescale factor from [Common Diffusion Noise Schedules and Sample Steps are
807
+ Flawed](https://arxiv.org/pdf/2305.08891.pdf). Guidance rescale factor should fix overexposure when
808
+ using zero terminal SNR.
809
+ clip_skip (`int`, *optional*):
810
+ Number of layers to be skipped from CLIP while computing the prompt embeddings. A value of 1 means that
811
+ the output of the pre-final layer will be used for computing the prompt embeddings.
812
+ callback_on_step_end (`Callable`, *optional*):
813
+ A function that calls at the end of each denoising steps during the inference. The function is called
814
+ with the following arguments: `callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int,
815
+ callback_kwargs: Dict)`. `callback_kwargs` will include a list of all tensors as specified by
816
+ `callback_on_step_end_tensor_inputs`.
817
+ callback_on_step_end_tensor_inputs (`List`, *optional*):
818
+ The list of tensor inputs for the `callback_on_step_end` function. The tensors specified in the list
819
+ will be passed as `callback_kwargs` argument. You will only be able to include variables listed in the
820
+ `._callback_tensor_inputs` attribute of your pipeline class.
821
+
822
+ Examples:
823
+
824
+ Returns:
825
+ [`~pipelines.stable_diffusion.StableDiffusionPipelineOutput`] or `tuple`:
826
+ If `return_dict` is `True`, [`~pipelines.stable_diffusion.StableDiffusionPipelineOutput`] is returned,
827
+ otherwise a `tuple` is returned where the first element is a list with the generated images and the
828
+ second element is a list of `bool`s indicating whether the corresponding generated image contains
829
+ "not-safe-for-work" (nsfw) content.
830
+ """
831
+
832
+ callback = kwargs.pop("callback", None)
833
+ callback_steps = kwargs.pop("callback_steps", None)
834
+
835
+ if callback is not None:
836
+ deprecate(
837
+ "callback",
838
+ "1.0.0",
839
+ "Passing `callback` as an input argument to `__call__` is deprecated, consider using `callback_on_step_end`",
840
+ )
841
+ if callback_steps is not None:
842
+ deprecate(
843
+ "callback_steps",
844
+ "1.0.0",
845
+ "Passing `callback_steps` as an input argument to `__call__` is deprecated, consider using `callback_on_step_end`",
846
+ )
847
+
848
+ # 0. Default height and width to unet
849
+ height = height or self.unet.config.sample_size * self.vae_scale_factor
850
+ width = width or self.unet.config.sample_size * self.vae_scale_factor
851
+ # to deal with lora scaling and other possible forward hooks
852
+
853
+ # 1. Check inputs. Raise error if not correct
854
+ self.check_inputs(
855
+ prompt,
856
+ height,
857
+ width,
858
+ callback_steps,
859
+ negative_prompt,
860
+ prompt_embeds,
861
+ negative_prompt_embeds,
862
+ ip_adapter_image,
863
+ ip_adapter_image_embeds,
864
+ callback_on_step_end_tensor_inputs,
865
+ )
866
+
867
+ self._guidance_scale = guidance_scale
868
+ self._guidance_rescale = guidance_rescale
869
+ self._clip_skip = clip_skip
870
+ self._cross_attention_kwargs = cross_attention_kwargs
871
+ self._interrupt = False
872
+
873
+ # 2. Define call parameters
874
+ if prompt is not None and isinstance(prompt, str):
875
+ batch_size = 1
876
+ elif prompt is not None and isinstance(prompt, list):
877
+ batch_size = len(prompt)
878
+ else:
879
+ batch_size = prompt_embeds.shape[0]
880
+
881
+ device = self._execution_device
882
+
883
+ # 3. Encode input prompt
884
+ lora_scale = (
885
+ self.cross_attention_kwargs.get("scale", None) if self.cross_attention_kwargs is not None else None
886
+ )
887
+
888
+ prompt_embeds, negative_prompt_embeds = self.encode_prompt(
889
+ prompt,
890
+ device,
891
+ num_images_per_prompt,
892
+ self.do_classifier_free_guidance,
893
+ negative_prompt,
894
+ prompt_embeds=prompt_embeds,
895
+ negative_prompt_embeds=negative_prompt_embeds,
896
+ lora_scale=lora_scale,
897
+ clip_skip=self.clip_skip,
898
+ )
899
+
900
+ # For classifier free guidance, we need to do two forward passes.
901
+ # Here we concatenate the unconditional and text embeddings into a single batch
902
+ # to avoid doing two forward passes
903
+ if self.do_classifier_free_guidance:
904
+ prompt_embeds = torch.cat([negative_prompt_embeds, prompt_embeds])
905
+
906
+ if ip_adapter_image is not None or ip_adapter_image_embeds is not None:
907
+ image_embeds = self.prepare_ip_adapter_image_embeds(
908
+ ip_adapter_image,
909
+ ip_adapter_image_embeds,
910
+ device,
911
+ batch_size * num_images_per_prompt,
912
+ self.do_classifier_free_guidance,
913
+ )
914
+
915
+ # 4. Prepare timesteps
916
+ timesteps, num_inference_steps = retrieve_timesteps(self.scheduler, num_inference_steps, device, timesteps)
917
+
918
+ # 5. Prepare latent variables
919
+ num_channels_latents = self.unet.config.in_channels
920
+ latents = self.prepare_latents(
921
+ batch_size * num_images_per_prompt,
922
+ num_channels_latents,
923
+ height,
924
+ width,
925
+ prompt_embeds.dtype,
926
+ device,
927
+ generator,
928
+ latents,
929
+ )
930
+
931
+ # 6. Prepare extra step kwargs. TODO: Logic should ideally just be moved out of the pipeline
932
+ extra_step_kwargs = self.prepare_extra_step_kwargs(generator, eta)
933
+
934
+ # 6.1 Add image embeds for IP-Adapter
935
+ added_cond_kwargs = (
936
+ {"image_embeds": image_embeds}
937
+ if (ip_adapter_image is not None or ip_adapter_image_embeds is not None)
938
+ else None
939
+ )
940
+
941
+ # 6.2 Optionally get Guidance Scale Embedding
942
+ timestep_cond = None
943
+ if self.unet.config.time_cond_proj_dim is not None:
944
+ guidance_scale_tensor = torch.tensor(self.guidance_scale - 1).repeat(batch_size * num_images_per_prompt)
945
+ timestep_cond = self.get_guidance_scale_embedding(
946
+ guidance_scale_tensor, embedding_dim=self.unet.config.time_cond_proj_dim
947
+ ).to(device=device, dtype=latents.dtype)
948
+
949
+ # 7. Denoising loop
950
+ num_warmup_steps = len(timesteps) - num_inference_steps * self.scheduler.order
951
+ self._num_timesteps = len(timesteps)
952
+ with self.progress_bar(total=num_inference_steps) as progress_bar:
953
+ for i, t in enumerate(timesteps):
954
+ if self.interrupt:
955
+ continue
956
+
957
+ # expand the latents if we are doing classifier free guidance
958
+ latent_model_input = torch.cat([latents] * 2) if self.do_classifier_free_guidance else latents
959
+ latent_model_input = self.scheduler.scale_model_input(latent_model_input, t)
960
+
961
+ # predict the noise residual
962
+ noise_pred = self.unet(
963
+ latent_model_input,
964
+ t,
965
+ encoder_hidden_states=prompt_embeds,
966
+ timestep_cond=timestep_cond,
967
+ cross_attention_kwargs=self.cross_attention_kwargs,
968
+ added_cond_kwargs=added_cond_kwargs,
969
+ return_dict=False,
970
+ audio_context=audio_context,
971
+ )[0]
972
+
973
+ # perform guidance
974
+ if self.do_classifier_free_guidance:
975
+ noise_pred_uncond, noise_pred_text = noise_pred.chunk(2)
976
+ noise_pred = noise_pred_uncond + self.guidance_scale * (noise_pred_text - noise_pred_uncond)
977
+
978
+ if self.do_classifier_free_guidance and self.guidance_rescale > 0.0:
979
+ # Based on 3.4. in https://arxiv.org/pdf/2305.08891.pdf
980
+ noise_pred = rescale_noise_cfg(noise_pred, noise_pred_text, guidance_rescale=self.guidance_rescale)
981
+
982
+ # compute the previous noisy sample x_t -> x_t-1
983
+ latents = self.scheduler.step(noise_pred, t, latents, **extra_step_kwargs, return_dict=False)[0]
984
+
985
+ if callback_on_step_end is not None:
986
+ callback_kwargs = {}
987
+ for k in callback_on_step_end_tensor_inputs:
988
+ callback_kwargs[k] = locals()[k]
989
+ callback_outputs = callback_on_step_end(self, i, t, callback_kwargs)
990
+
991
+ latents = callback_outputs.pop("latents", latents)
992
+ prompt_embeds = callback_outputs.pop("prompt_embeds", prompt_embeds)
993
+ negative_prompt_embeds = callback_outputs.pop("negative_prompt_embeds", negative_prompt_embeds)
994
+
995
+ # call the callback, if provided
996
+ if i == len(timesteps) - 1 or ((i + 1) > num_warmup_steps and (i + 1) % self.scheduler.order == 0):
997
+ progress_bar.update()
998
+ if callback is not None and i % callback_steps == 0:
999
+ step_idx = i // getattr(self.scheduler, "order", 1)
1000
+ callback(step_idx, t, latents)
1001
+
1002
+ if not output_type == "latent":
1003
+ image = self.vae.decode(latents / self.vae.config.scaling_factor, return_dict=False, generator=generator)[
1004
+ 0
1005
+ ]
1006
+ image, has_nsfw_concept = self.run_safety_checker(image, device, prompt_embeds.dtype)
1007
+ else:
1008
+ image = latents
1009
+ has_nsfw_concept = None
1010
+
1011
+ if has_nsfw_concept is None:
1012
+ do_denormalize = [True] * image.shape[0]
1013
+ else:
1014
+ do_denormalize = [not has_nsfw for has_nsfw in has_nsfw_concept]
1015
+
1016
+ image = self.image_processor.postprocess(image, output_type=output_type, do_denormalize=do_denormalize)
1017
+
1018
+ # Offload all models
1019
+ self.maybe_free_model_hooks()
1020
+
1021
+ if not return_dict:
1022
+ return (image, has_nsfw_concept)
1023
+
1024
+ return StableDiffusionPipelineOutput(images=image, nsfw_content_detected=has_nsfw_concept)
pnp.py ADDED
@@ -0,0 +1,279 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Adapted from https://github.com/MichalGeyer/pnp-diffusers/blob/main/pnp.py
2
+
3
+ import spaces
4
+ import glob
5
+ import os
6
+ from pathlib import Path
7
+ import torch
8
+ import torch.nn as nn
9
+ import torchvision.transforms as T
10
+ import argparse
11
+ from PIL import Image
12
+ import yaml
13
+ from tqdm import tqdm
14
+ from transformers import logging
15
+ from diffusers import DDIMScheduler, StableDiffusionPipeline
16
+
17
+ from pnp_utils import *
18
+
19
+ from unet2d_custom import UNet2DConditionModel
20
+ from pipeline_stable_diffusion_custom import StableDiffusionPipeline
21
+ from ldm.modules.encoders.audio_projector_res import Adapter
22
+
23
+ # suppress partial model loading warning
24
+ logging.set_verbosity_error()
25
+
26
+ from diffusers import logging
27
+ logging.set_verbosity_error()
28
+
29
+
30
+
31
+ class PNP(nn.Module):
32
+ def __init__(self, sd_version="1.4", n_timesteps=50, audio_projector_ckpt_path="ckpts/audio_projector_gh.pth",
33
+ adapter_ckpt_path="ckpts/greatest_hits.pt", device="cuda",
34
+ clap_path="CLAP/msclap",
35
+ clap_weights = "ckpts/CLAP_weights_2022.pth",
36
+
37
+ ):
38
+ super().__init__()
39
+
40
+ self.device = device
41
+
42
+ if sd_version == '2.1':
43
+ model_key = "stabilityai/stable-diffusion-2-1-base"
44
+ elif sd_version == '2.0':
45
+ model_key = "stabilityai/stable-diffusion-2-base"
46
+ elif sd_version == '1.5':
47
+ model_key = "runwayml/stable-diffusion-v1-5"
48
+ elif sd_version == '1.4':
49
+ model_key = "CompVis/stable-diffusion-v1-4"
50
+ print(f"model key is {model_key}")
51
+ else:
52
+ raise ValueError(f'Stable-diffusion version {sd_version} not supported.')
53
+
54
+ # Create SD models
55
+ print('Loading SD model')
56
+
57
+
58
+ pipe = StableDiffusionPipeline.from_pretrained(model_key, torch_dtype=torch.float16).to("cuda")
59
+
60
+ model_id = "CompVis/stable-diffusion-v1-4"
61
+ self.unet = UNet2DConditionModel.from_pretrained(
62
+ model_id,
63
+ subfolder="unet",
64
+ use_adapter_list=[False, True, True],
65
+ low_cpu_mem_usage=False,
66
+ device_map=None
67
+ ).to("cuda")
68
+
69
+
70
+ # gate_dict = torch.load(adapter_ckpt_path)
71
+
72
+ # for name, param in self.unet.named_parameters():
73
+ # if "adapter" in name:
74
+ # param.data = gate_dict[name]
75
+ #unet.to(self.device);
76
+
77
+ #pipe.unet = unet.to(self.device);
78
+
79
+ self.vae = pipe.vae
80
+ self.tokenizer = pipe.tokenizer
81
+ self.text_encoder = pipe.text_encoder
82
+ # self.unet = unet.to(self.device);
83
+ #pipe.unet
84
+
85
+ self.scheduler = DDIMScheduler.from_pretrained(model_key, subfolder="scheduler")
86
+ self.scheduler.set_timesteps(n_timesteps, device=self.device)
87
+
88
+ self.latents_path = "latents_forward"
89
+ self.output_path = "PNP-results/home"
90
+
91
+ import os
92
+ os.makedirs(self.output_path, exist_ok=True)
93
+
94
+ import sys
95
+ sys.path.append(clap_path)
96
+ from CLAPWrapper import CLAPWrapper
97
+ self.audio_encoder = CLAPWrapper(clap_weights, use_cuda=True)
98
+
99
+ self.audio_projector = Adapter(audio_token_count=77, transformer_layer_count=4).cuda()
100
+ #self.audio_projector_ckpt_path = audio_projector_ckpt_path
101
+ self.sr = 44100
102
+ # self.set_audio_projector(adapter_ckpt_path, audio_projector_ckpt_path)
103
+ self.text_encoder = self.text_encoder.cuda()
104
+
105
+ # self.audio_projector.load_state_dict(torch.load(audio_projector_ckpt_path))
106
+
107
+ self.audio_projector_ckpt_path = audio_projector_ckpt_path
108
+ self.adapter_ckpt_path = adapter_ckpt_path
109
+ self.changed_model = False
110
+
111
+ @spaces.GPU
112
+ def set_audio_projector(self, adapter_ckpt_path, audio_projector_ckpt_path):
113
+
114
+ print(f"SETTING MODEL TO {adapter_ckpt_path}")
115
+ gate_dict = torch.load(adapter_ckpt_path)
116
+ for name, param in self.unet.named_parameters():
117
+ if "adapter" in name:
118
+ param.data = gate_dict[name]
119
+
120
+ self.unet.eval()
121
+ self.unet = self.unet.cuda()
122
+
123
+ self.audio_projector.load_state_dict(torch.load(audio_projector_ckpt_path))
124
+ self.audio_projector.eval()
125
+ self.audio_projector = self.audio_projector.cuda()
126
+
127
+ @spaces.GPU
128
+ def set_text_embeds(self, prompt, negative_prompt=""):
129
+ self.text_encoder = self.text_encoder.cuda()
130
+ self.text_embeds = self.get_text_embeds(prompt, negative_prompt)
131
+ self.pnp_guidance_embeds = self.get_text_embeds("", "").chunk(2)[0]
132
+
133
+ @spaces.GPU
134
+ def set_audio_context(self, audio_path):
135
+
136
+ self.audio_projector = self.audio_projector.cuda()
137
+ self.audio_encoder.clap.audio_encoder = self.audio_encoder.clap.audio_encoder.to("cuda")
138
+ audio_emb, _ = self.audio_encoder.get_audio_embeddings([audio_path], resample = self.sr)
139
+
140
+ dtpye_w = self.audio_projector.audio_emb_projection[0].weight.dtype
141
+ device_w = self.audio_projector.audio_emb_projection[0].weight.device
142
+
143
+ audio_emb = audio_emb.cuda()
144
+ audio_proj = self.audio_projector(audio_emb.unsqueeze(1))
145
+
146
+ audio_emb = torch.zeros(1, 1024).cuda()
147
+ audio_uc = self.audio_projector(audio_emb.unsqueeze(1))
148
+
149
+ self.audio_context = torch.cat([audio_uc, audio_uc, audio_proj]).cuda()
150
+
151
+
152
+ @torch.no_grad()
153
+ @spaces.GPU
154
+ def get_text_embeds(self, prompt, negative_prompt, batch_size=1):
155
+ # Tokenize text and get embeddings
156
+ text_input = self.tokenizer(prompt, padding='max_length', max_length=self.tokenizer.model_max_length,
157
+ truncation=True, return_tensors='pt')
158
+ input_ids = text_input.input_ids.to("cuda")
159
+ text_embeddings = self.text_encoder(input_ids)[0]
160
+
161
+ # Do the same for unconditional embeddings
162
+ uncond_input = self.tokenizer(negative_prompt, padding='max_length', max_length=self.tokenizer.model_max_length,
163
+ return_tensors='pt')
164
+
165
+ uncond_embeddings = self.text_encoder(uncond_input.input_ids.to(self.device))[0]
166
+
167
+ # Cat for final embeddings
168
+ text_embeddings = torch.cat([uncond_embeddings] * batch_size + [text_embeddings] * batch_size)
169
+ return text_embeddings
170
+
171
+ @torch.no_grad()
172
+ @spaces.GPU
173
+ def decode_latent(self, latent):
174
+ self.vae = self.vae.cuda()
175
+ with torch.autocast(device_type='cuda', dtype=torch.float32):
176
+ latent = 1 / 0.18215 * latent
177
+ img = self.vae.decode(latent).sample
178
+ img = (img / 2 + 0.5).clamp(0, 1)
179
+ return img
180
+
181
+ #@torch.autocast(device_type='cuda', dtype=torch.float32)
182
+ @spaces.GPU
183
+ def get_data(self, image_path):
184
+ self.image_path = image_path
185
+ # load image
186
+ image = Image.open(image_path).convert('RGB')
187
+ image = image.resize((512, 512), resample=Image.Resampling.LANCZOS)
188
+ image = T.ToTensor()(image).to(self.device)
189
+
190
+ # get noise
191
+ latents_path = os.path.join(self.latents_path, f'noisy_latents_{self.scheduler.timesteps[0]}.pt')
192
+ noisy_latent = torch.load(latents_path).to(self.device)
193
+ return image, noisy_latent
194
+
195
+ @torch.no_grad()
196
+ @spaces.GPU
197
+ def denoise_step(self, x, t, guidance_scale):
198
+ # register the time step and features in pnp injection modules
199
+ source_latents = load_source_latents_t(t, os.path.join(self.latents_path))
200
+ latent_model_input = torch.cat([source_latents] + ([x] * 2))
201
+
202
+ register_time(self, t.item())
203
+
204
+ # compute text embeddings
205
+ text_embed_input = torch.cat([self.pnp_guidance_embeds, self.text_embeds], dim=0)
206
+
207
+ # apply the denoising network
208
+ noise_pred = self.unet(latent_model_input, t,
209
+ encoder_hidden_states=text_embed_input,
210
+ audio_context=self.audio_context)['sample']
211
+
212
+ # perform guidance
213
+ _, noise_pred_uncond, noise_pred_cond = noise_pred.chunk(3)
214
+ noise_pred = noise_pred_uncond + guidance_scale * (noise_pred_cond - noise_pred_uncond)
215
+
216
+ # compute the denoising step with the reference model
217
+ denoised_latent = self.scheduler.step(noise_pred, t, x)['prev_sample']
218
+ return denoised_latent
219
+
220
+ @spaces.GPU
221
+ def init_pnp(self, conv_injection_t, qk_injection_t):
222
+ self.qk_injection_timesteps = self.scheduler.timesteps[:qk_injection_t] if qk_injection_t >= 0 else []
223
+ self.conv_injection_timesteps = self.scheduler.timesteps[:conv_injection_t] if conv_injection_t >= 0 else []
224
+ register_attention_control_efficient(self, self.qk_injection_timesteps)
225
+ register_conv_control_efficient(self, self.conv_injection_timesteps)
226
+
227
+ @spaces.GPU
228
+ def run_pnp(self, n_timesteps=50, pnp_f_t=0.5, pnp_attn_t=0.5,
229
+ prompt="", negative_prompt="",
230
+ audio_path="", image_path="",
231
+ cfg_scale=5):
232
+
233
+ # if not self.changed_model:
234
+ # self.set_audio_projector(self.adapter_ckpt_path, self.audio_projector_ckpt_path)
235
+
236
+ self.audio_projector = self.audio_projector.cuda()
237
+
238
+ self.set_text_embeds(prompt)
239
+ self.set_audio_context(audio_path=audio_path)
240
+ self.image, self.eps = self.get_data(image_path=image_path)
241
+
242
+ self.unet = self.unet.cuda()
243
+
244
+ pnp_f_t = int(n_timesteps * pnp_f_t)
245
+ pnp_attn_t = int(n_timesteps * pnp_attn_t)
246
+ self.init_pnp(conv_injection_t=pnp_f_t, qk_injection_t=pnp_attn_t)
247
+
248
+ edited_img = self.sample_loop(self.eps, cfg_scale=cfg_scale)
249
+
250
+ return T.ToPILImage()(edited_img[0])
251
+
252
+ @spaces.GPU
253
+ def sample_loop(self, x, cfg_scale):
254
+ with torch.autocast(device_type='cuda', dtype=torch.float32):
255
+ for i, t in enumerate(tqdm(self.scheduler.timesteps, desc="Sampling")):
256
+ x = self.denoise_step(x, t, cfg_scale)
257
+
258
+ decoded_latent = self.decode_latent(x)
259
+ T.ToPILImage()(decoded_latent[0]).save(f'{self.output_path}/output.png')
260
+
261
+ return decoded_latent
262
+
263
+
264
+ if __name__ == '__main__':
265
+ parser = argparse.ArgumentParser()
266
+ parser.add_argument('--config_path', type=str, default='config_pnp.yaml')
267
+
268
+ opt = parser.parse_args()
269
+ with open(opt.config_path, "r") as f:
270
+ config = yaml.safe_load(f)
271
+ os.makedirs(config["output_path"], exist_ok=True)
272
+
273
+ with open(os.path.join(config["output_path"], "config.yaml"), "w") as f:
274
+ yaml.dump(config, f)
275
+
276
+ seed_everything(config["seed"])
277
+ print(config)
278
+ pnp = PNP(config)
279
+ temp = pnp.run_pnp()
pnp_utils.py ADDED
@@ -0,0 +1,169 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Adapted from https://github.com/MichalGeyer/pnp-diffusers/blob/main/pnp_utils.py
2
+
3
+ import torch
4
+ import os
5
+ import random
6
+ import numpy as np
7
+
8
+ def seed_everything(seed):
9
+ torch.manual_seed(seed)
10
+ torch.cuda.manual_seed(seed)
11
+ random.seed(seed)
12
+ np.random.seed(seed)
13
+
14
+ def register_time(model, t):
15
+
16
+
17
+ conv_res_dict = {1: [0, 1, 2], 2: [0, 1, 2], 3: [0, 1, 2]}
18
+ for res in conv_res_dict:
19
+ for block in conv_res_dict[res]:
20
+ conv_module = model.unet.up_blocks[res].resnets[block]
21
+ setattr(conv_module, 't', t)
22
+
23
+ down_res_dict = {0: [0, 1], 1: [0, 1], 2: [0, 1]}
24
+ up_res_dict = {1: [0, 1, 2], 2: [0, 1, 2], 3: [0, 1, 2]}
25
+ for res in up_res_dict:
26
+ for block in up_res_dict[res]:
27
+ module = model.unet.up_blocks[res].attentions[block].transformer_blocks[0].attn1
28
+ setattr(module, 't', t)
29
+ for res in down_res_dict:
30
+ for block in down_res_dict[res]:
31
+ module = model.unet.down_blocks[res].attentions[block].transformer_blocks[0].attn1
32
+ setattr(module, 't', t)
33
+ module = model.unet.mid_block.attentions[0].transformer_blocks[0].attn1
34
+ setattr(module, 't', t)
35
+
36
+
37
+ def load_source_latents_t(t, latents_path):
38
+ latents_t_path = os.path.join(latents_path, f'noisy_latents_{t}.pt')
39
+ assert os.path.exists(latents_t_path), f'Missing latents at t {t} path {latents_t_path}'
40
+ latents = torch.load(latents_t_path)
41
+ return latents
42
+
43
+ def register_attention_control_efficient(model, injection_schedule):
44
+ def sa_forward(self):
45
+ to_out = self.to_out
46
+ if type(to_out) is torch.nn.modules.container.ModuleList:
47
+ to_out = self.to_out[0]
48
+ else:
49
+ to_out = self.to_out
50
+
51
+ def forward(x, encoder_hidden_states=None, attention_mask=None):
52
+ batch_size, sequence_length, dim = x.shape
53
+ h = self.heads
54
+
55
+ is_cross = encoder_hidden_states is not None
56
+ encoder_hidden_states = encoder_hidden_states if is_cross else x
57
+ if not is_cross and self.injection_schedule is not None and (
58
+ self.t in self.injection_schedule or self.t == 1000):
59
+ q = self.to_q(x)
60
+ k = self.to_k(encoder_hidden_states)
61
+
62
+ source_batch_size = int(q.shape[0] // 3)
63
+ # inject unconditional
64
+ q[source_batch_size:2 * source_batch_size] = q[:source_batch_size]
65
+ k[source_batch_size:2 * source_batch_size] = k[:source_batch_size]
66
+ # inject conditional
67
+ q[2 * source_batch_size:] = q[:source_batch_size]
68
+ k[2 * source_batch_size:] = k[:source_batch_size]
69
+
70
+ q = self.head_to_batch_dim(q)
71
+ k = self.head_to_batch_dim(k)
72
+ else:
73
+ q = self.to_q(x)
74
+ k = self.to_k(encoder_hidden_states)
75
+ q = self.head_to_batch_dim(q)
76
+ k = self.head_to_batch_dim(k)
77
+
78
+ v = self.to_v(encoder_hidden_states)
79
+ v = self.head_to_batch_dim(v)
80
+
81
+ sim = torch.einsum("b i d, b j d -> b i j", q, k) * self.scale
82
+
83
+ if attention_mask is not None:
84
+ attention_mask = attention_mask.reshape(batch_size, -1)
85
+ max_neg_value = -torch.finfo(sim.dtype).max
86
+ attention_mask = attention_mask[:, None, :].repeat(h, 1, 1)
87
+ sim.masked_fill_(~attention_mask, max_neg_value)
88
+
89
+ # attention, what we cannot get enough of
90
+ attn = sim.softmax(dim=-1)
91
+ out = torch.einsum("b i j, b j d -> b i d", attn, v)
92
+ out = self.batch_to_head_dim(out)
93
+
94
+ return to_out(out)
95
+
96
+ return forward
97
+
98
+
99
+ res_dict = {1: [1, 2], 2: [0, 1, 2], 3: [0, 1, 2]} # we are injecting attention in blocks 4 - 11 of the decoder, so not in the first block of the lowest resolution
100
+ for res in res_dict:
101
+ for block in res_dict[res]:
102
+ module = model.unet.up_blocks[res].attentions[block].transformer_blocks[0].attn1
103
+ module.forward = sa_forward(module)
104
+ setattr(module, 'injection_schedule', injection_schedule)
105
+
106
+
107
+ def register_conv_control_efficient(model, injection_schedule):
108
+ def conv_forward(self):
109
+ def forward(input_tensor, temb):
110
+ hidden_states = input_tensor
111
+
112
+ hidden_states = self.norm1(hidden_states)
113
+ hidden_states = self.nonlinearity(hidden_states)
114
+
115
+ if self.upsample is not None:
116
+ # upsample_nearest_nhwc fails with large batch sizes. see https://github.com/huggingface/diffusers/issues/984
117
+ if hidden_states.shape[0] >= 64:
118
+ input_tensor = input_tensor.contiguous()
119
+ hidden_states = hidden_states.contiguous()
120
+ input_tensor = self.upsample(input_tensor)
121
+ hidden_states = self.upsample(hidden_states)
122
+ elif self.downsample is not None:
123
+ input_tensor = self.downsample(input_tensor)
124
+ hidden_states = self.downsample(hidden_states)
125
+
126
+ hidden_states = self.conv1(hidden_states)
127
+
128
+ if temb is not None:
129
+ temb = self.time_emb_proj(self.nonlinearity(temb))[:, :, None, None]
130
+
131
+ if temb is not None and self.time_embedding_norm == "default":
132
+ hidden_states = hidden_states + temb
133
+
134
+ hidden_states = self.norm2(hidden_states)
135
+
136
+ if temb is not None and self.time_embedding_norm == "scale_shift":
137
+ scale, shift = torch.chunk(temb, 2, dim=1)
138
+ hidden_states = hidden_states * (1 + scale) + shift
139
+
140
+ hidden_states = self.nonlinearity(hidden_states)
141
+
142
+ hidden_states = self.dropout(hidden_states)
143
+ hidden_states = self.conv2(hidden_states)
144
+ if self.injection_schedule is not None and (self.t in self.injection_schedule or self.t == 1000):
145
+ source_batch_size = int(hidden_states.shape[0] // 3)
146
+ # inject unconditional
147
+ hidden_states[source_batch_size:2 * source_batch_size] = hidden_states[:source_batch_size]
148
+ # inject conditional
149
+ hidden_states[2 * source_batch_size:] = hidden_states[:source_batch_size]
150
+
151
+ if self.conv_shortcut is not None:
152
+ input_tensor = self.conv_shortcut(input_tensor)
153
+
154
+ output_tensor = (input_tensor + hidden_states) / self.output_scale_factor
155
+
156
+ return output_tensor
157
+
158
+ return forward
159
+
160
+ conv_res_dict = {1: [1, 2]}
161
+
162
+ for res in conv_res_dict:
163
+ for block in conv_res_dict[res]:
164
+ conv_module = model.unet.up_blocks[res].resnets[block]
165
+ conv_module.forward = conv_forward(conv_module)
166
+ setattr(conv_module, 'injection_schedule', injection_schedule)
167
+
168
+
169
+
preprocess.py ADDED
@@ -0,0 +1,228 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Adapted from https://github.com/MichalGeyer/pnp-diffusers/blob/main/preprocess.py
2
+
3
+ from transformers import CLIPTextModel, CLIPTokenizer, logging
4
+ from diffusers import AutoencoderKL, UNet2DConditionModel, DDIMScheduler
5
+
6
+ # suppress partial model loading warning
7
+ logging.set_verbosity_error()
8
+
9
+ import os
10
+ from PIL import Image
11
+ from tqdm import tqdm, trange
12
+ import torch
13
+ import torch.nn as nn
14
+ import argparse
15
+ from pathlib import Path
16
+ from pnp_utils import *
17
+ import torchvision.transforms as T
18
+
19
+
20
+ def get_timesteps(scheduler, num_inference_steps, strength, device):
21
+ # get the original timestep using init_timestep
22
+ init_timestep = min(int(num_inference_steps * strength), num_inference_steps)
23
+
24
+ t_start = max(num_inference_steps - init_timestep, 0)
25
+ timesteps = scheduler.timesteps[t_start:]
26
+
27
+ return timesteps, num_inference_steps - t_start
28
+
29
+
30
+ class Preprocess(nn.Module):
31
+ def __init__(self, device, sd_version='2.0', hf_key=None):
32
+ super().__init__()
33
+
34
+ self.device = device
35
+ self.sd_version = sd_version
36
+ self.use_depth = False
37
+
38
+ print(f'[INFO] loading stable diffusion...')
39
+ if hf_key is not None:
40
+ print(f'[INFO] using hugging face custom model key: {hf_key}')
41
+ model_key = hf_key
42
+ elif self.sd_version == '2.1':
43
+ model_key = "stabilityai/stable-diffusion-2-1-base"
44
+ elif self.sd_version == '2.0':
45
+ model_key = "stabilityai/stable-diffusion-2-base"
46
+ elif self.sd_version == '1.5':
47
+ model_key = "runwayml/stable-diffusion-v1-5"
48
+ elif self.sd_version == 'depth':
49
+ model_key = "stabilityai/stable-diffusion-2-depth"
50
+ self.use_depth = True
51
+ elif self.sd_version == '1.4':
52
+ model_key = "CompVis/stable-diffusion-v1-4"
53
+
54
+ else:
55
+ raise ValueError(f'Stable-diffusion version {self.sd_version} not supported.')
56
+
57
+ # Create model
58
+ self.vae = AutoencoderKL.from_pretrained(model_key, subfolder="vae", revision="fp16",
59
+ torch_dtype=torch.float16).to(self.device)
60
+ self.tokenizer = CLIPTokenizer.from_pretrained(model_key, subfolder="tokenizer")
61
+ self.text_encoder = CLIPTextModel.from_pretrained(model_key, subfolder="text_encoder", revision="fp16",
62
+ torch_dtype=torch.float16).to(self.device)
63
+ self.unet = UNet2DConditionModel.from_pretrained(model_key, subfolder="unet", revision="fp16",
64
+ torch_dtype=torch.float16).to(self.device)
65
+ self.scheduler = DDIMScheduler.from_pretrained(model_key, subfolder="scheduler")
66
+ print(f'[INFO] loaded stable diffusion!')
67
+
68
+ self.inversion_func = self.ddim_inversion
69
+
70
+ @torch.no_grad()
71
+ def get_text_embeds(self, prompt, negative_prompt, device="cuda"):
72
+ text_input = self.tokenizer(prompt, padding='max_length', max_length=self.tokenizer.model_max_length,
73
+ truncation=True, return_tensors='pt')
74
+ text_embeddings = self.text_encoder(text_input.input_ids.to(device))[0]
75
+ uncond_input = self.tokenizer(negative_prompt, padding='max_length', max_length=self.tokenizer.model_max_length,
76
+ return_tensors='pt')
77
+ uncond_embeddings = self.text_encoder(uncond_input.input_ids.to(device))[0]
78
+ text_embeddings = torch.cat([uncond_embeddings, text_embeddings])
79
+ return text_embeddings
80
+
81
+ @torch.no_grad()
82
+ def decode_latents(self, latents):
83
+ with torch.autocast(device_type='cuda', dtype=torch.float32):
84
+ latents = 1 / 0.18215 * latents
85
+ imgs = self.vae.decode(latents).sample
86
+ imgs = (imgs / 2 + 0.5).clamp(0, 1)
87
+ return imgs
88
+
89
+ def load_img(self, image_path):
90
+ image_pil = T.Resize(512)(Image.open(image_path).convert("RGB"))
91
+ image = T.ToTensor()(image_pil).unsqueeze(0).to(self.device)
92
+ return image
93
+
94
+ @torch.no_grad()
95
+ def encode_imgs(self, imgs):
96
+ with torch.autocast(device_type='cuda', dtype=torch.float32):
97
+ imgs = 2 * imgs - 1
98
+ posterior = self.vae.encode(imgs).latent_dist
99
+ latents = posterior.mean * 0.18215
100
+ return latents
101
+
102
+ @torch.no_grad()
103
+ def ddim_inversion(self, cond, latent, save_path, save_latents=True,
104
+ timesteps_to_save=None):
105
+ timesteps = reversed(self.scheduler.timesteps)
106
+ with torch.autocast(device_type='cuda', dtype=torch.float32):
107
+ for i, t in enumerate(tqdm(timesteps)):
108
+ cond_batch = cond.repeat(latent.shape[0], 1, 1)
109
+
110
+ alpha_prod_t = self.scheduler.alphas_cumprod[t]
111
+ alpha_prod_t_prev = (
112
+ self.scheduler.alphas_cumprod[timesteps[i - 1]]
113
+ if i > 0 else self.scheduler.final_alpha_cumprod
114
+ )
115
+
116
+ mu = alpha_prod_t ** 0.5
117
+ mu_prev = alpha_prod_t_prev ** 0.5
118
+ sigma = (1 - alpha_prod_t) ** 0.5
119
+ sigma_prev = (1 - alpha_prod_t_prev) ** 0.5
120
+
121
+ eps = self.unet(latent, t, encoder_hidden_states=cond_batch).sample
122
+
123
+ pred_x0 = (latent - sigma_prev * eps) / mu_prev
124
+ latent = mu * pred_x0 + sigma * eps
125
+ if save_latents:
126
+ torch.save(latent, os.path.join(save_path, f'noisy_latents_{t}.pt'))
127
+ torch.save(latent, os.path.join(save_path, f'noisy_latents_{t}.pt'))
128
+ return latent
129
+
130
+ @torch.no_grad()
131
+ def ddim_sample(self, x, cond, save_path, save_latents=False, timesteps_to_save=None):
132
+ timesteps = self.scheduler.timesteps
133
+ with torch.autocast(device_type='cuda', dtype=torch.float32):
134
+ for i, t in enumerate(tqdm(timesteps)):
135
+ cond_batch = cond.repeat(x.shape[0], 1, 1)
136
+ alpha_prod_t = self.scheduler.alphas_cumprod[t]
137
+ alpha_prod_t_prev = (
138
+ self.scheduler.alphas_cumprod[timesteps[i + 1]]
139
+ if i < len(timesteps) - 1
140
+ else self.scheduler.final_alpha_cumprod
141
+ )
142
+ mu = alpha_prod_t ** 0.5
143
+ sigma = (1 - alpha_prod_t) ** 0.5
144
+ mu_prev = alpha_prod_t_prev ** 0.5
145
+ sigma_prev = (1 - alpha_prod_t_prev) ** 0.5
146
+
147
+ eps = self.unet(x, t, encoder_hidden_states=cond_batch).sample
148
+
149
+ pred_x0 = (x - sigma * eps) / mu
150
+ x = mu_prev * pred_x0 + sigma_prev * eps
151
+
152
+ if save_latents:
153
+ torch.save(x, os.path.join(save_path, f'noisy_latents_{t}.pt'))
154
+ return x
155
+
156
+ @torch.no_grad()
157
+ def extract_latents(self, num_steps, data_path, save_path, timesteps_to_save,
158
+ inversion_prompt='', extract_reverse=False):
159
+ self.scheduler.set_timesteps(num_steps)
160
+
161
+ cond = self.get_text_embeds(inversion_prompt, "")[1].unsqueeze(0)
162
+ image = self.load_img(data_path)
163
+ latent = self.encode_imgs(image)
164
+
165
+ inverted_x = self.inversion_func(cond, latent, save_path, save_latents=not extract_reverse,
166
+ timesteps_to_save=timesteps_to_save)
167
+ latent_reconstruction = self.ddim_sample(inverted_x, cond, save_path, save_latents=extract_reverse,
168
+ timesteps_to_save=timesteps_to_save)
169
+ rgb_reconstruction = self.decode_latents(latent_reconstruction)
170
+
171
+ return rgb_reconstruction # , latent_reconstruction
172
+
173
+
174
+ def run(opt):
175
+ device = 'cuda'
176
+ # timesteps to save
177
+ if opt.sd_version == '2.1':
178
+ model_key = "stabilityai/stable-diffusion-2-1-base"
179
+ elif opt.sd_version == '2.0':
180
+ model_key = "stabilityai/stable-diffusion-2-base"
181
+ elif opt.sd_version == '1.5':
182
+ model_key = "runwayml/stable-diffusion-v1-5"
183
+ elif opt.sd_version == 'depth':
184
+ model_key = "stabilityai/stable-diffusion-2-depth"
185
+ elif opt.sd_version == '1.4':
186
+ model_key = "CompVis/stable-diffusion-v1-4"
187
+
188
+ toy_scheduler = DDIMScheduler.from_pretrained(model_key, subfolder="scheduler")
189
+ toy_scheduler.set_timesteps(opt.save_steps)
190
+ timesteps_to_save, num_inference_steps = get_timesteps(toy_scheduler, num_inference_steps=opt.save_steps,
191
+ strength=1.0,
192
+ device=device)
193
+
194
+ seed_everything(opt.seed)
195
+
196
+ extraction_path_prefix = "_reverse" if opt.extract_reverse else "_forward"
197
+ save_path = os.path.join(opt.save_dir + extraction_path_prefix, os.path.splitext(os.path.basename(opt.data_path))[0])
198
+ os.makedirs(save_path, exist_ok=True)
199
+
200
+ model = Preprocess(device, sd_version=opt.sd_version, hf_key=None)
201
+ recon_image = model.extract_latents(data_path=opt.data_path,
202
+ num_steps=opt.steps,
203
+ save_path=save_path,
204
+ timesteps_to_save=timesteps_to_save,
205
+ inversion_prompt=opt.inversion_prompt,
206
+ extract_reverse=opt.extract_reverse)
207
+
208
+ T.ToPILImage()(recon_image[0]).save(os.path.join(save_path, f'recon.jpg'))
209
+
210
+
211
+ if __name__ == "__main__":
212
+ device = 'cuda'
213
+ parser = argparse.ArgumentParser()
214
+ parser.add_argument('--data_path', type=str,
215
+ default='data/source_2.png')
216
+ parser.add_argument('--save_dir', type=str, default='latents')
217
+ parser.add_argument('--sd_version', type=str, default='1.4', choices=['1.5', '2.0', '2.1', '1.4'],
218
+ help="stable diffusion version")
219
+
220
+ parser.add_argument('--seed', type=int, default=1)
221
+ parser.add_argument('--steps', type=int, default=50)
222
+ parser.add_argument('--save-steps', type=int, default=1000)
223
+ parser.add_argument('--inversion_prompt', type=str, default='')
224
+ parser.add_argument('--extract-reverse', default=False, action='store_true', help="extract features during the denoising process")
225
+
226
+ opt = parser.parse_args()
227
+ run(opt)
228
+
transformer_2d_custom.py ADDED
@@ -0,0 +1,453 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Adapted from https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/transformer_2d.py
2
+
3
+ from dataclasses import dataclass
4
+ from typing import Any, Dict, Optional
5
+
6
+ import torch
7
+ import torch.nn.functional as F
8
+ from torch import nn
9
+
10
+ from diffusers.configuration_utils import ConfigMixin, register_to_config
11
+ from diffusers.utils import BaseOutput, deprecate, is_torch_version, logging
12
+ from attention_custom import BasicTransformerBlock
13
+
14
+ from diffusers.models.embeddings import ImagePositionalEmbeddings, PatchEmbed, PixArtAlphaTextProjection
15
+ from diffusers.models.modeling_utils import ModelMixin
16
+ from diffusers.models.normalization import AdaLayerNormSingle
17
+
18
+
19
+ logger = logging.get_logger(__name__) # pylint: disable=invalid-name
20
+
21
+ @dataclass
22
+ class Transformer2DModelOutput(BaseOutput):
23
+ """
24
+ The output of [`Transformer2DModel`].
25
+
26
+ Args:
27
+ sample (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)` or `(batch size, num_vector_embeds - 1, num_latent_pixels)` if [`Transformer2DModel`] is discrete):
28
+ The hidden states output conditioned on the `encoder_hidden_states` input. If discrete, returns probability
29
+ distributions for the unnoised latent pixels.
30
+ """
31
+
32
+ sample: torch.FloatTensor
33
+
34
+
35
+ class Transformer2DModel(ModelMixin, ConfigMixin):
36
+ """
37
+ A 2D Transformer model for image-like data.
38
+
39
+ Parameters:
40
+ num_attention_heads (`int`, *optional*, defaults to 16): The number of heads to use for multi-head attention.
41
+ attention_head_dim (`int`, *optional*, defaults to 88): The number of channels in each head.
42
+ in_channels (`int`, *optional*):
43
+ The number of channels in the input and output (specify if the input is **continuous**).
44
+ num_layers (`int`, *optional*, defaults to 1): The number of layers of Transformer blocks to use.
45
+ dropout (`float`, *optional*, defaults to 0.0): The dropout probability to use.
46
+ cross_attention_dim (`int`, *optional*): The number of `encoder_hidden_states` dimensions to use.
47
+ sample_size (`int`, *optional*): The width of the latent images (specify if the input is **discrete**).
48
+ This is fixed during training since it is used to learn a number of position embeddings.
49
+ num_vector_embeds (`int`, *optional*):
50
+ The number of classes of the vector embeddings of the latent pixels (specify if the input is **discrete**).
51
+ Includes the class for the masked latent pixel.
52
+ activation_fn (`str`, *optional*, defaults to `"geglu"`): Activation function to use in feed-forward.
53
+ num_embeds_ada_norm ( `int`, *optional*):
54
+ The number of diffusion steps used during training. Pass if at least one of the norm_layers is
55
+ `AdaLayerNorm`. This is fixed during training since it is used to learn a number of embeddings that are
56
+ added to the hidden states.
57
+
58
+ During inference, you can denoise for up to but not more steps than `num_embeds_ada_norm`.
59
+ attention_bias (`bool`, *optional*):
60
+ Configure if the `TransformerBlocks` attention should contain a bias parameter.
61
+ """
62
+
63
+ _supports_gradient_checkpointing = True
64
+
65
+ @register_to_config
66
+ def __init__(
67
+ self,
68
+ num_attention_heads: int = 16,
69
+ attention_head_dim: int = 88,
70
+ in_channels: Optional[int] = None,
71
+ out_channels: Optional[int] = None,
72
+ num_layers: int = 1,
73
+ dropout: float = 0.0,
74
+ norm_num_groups: int = 32,
75
+ cross_attention_dim: Optional[int] = None,
76
+ attention_bias: bool = False,
77
+ sample_size: Optional[int] = None,
78
+ num_vector_embeds: Optional[int] = None,
79
+ patch_size: Optional[int] = None,
80
+ activation_fn: str = "geglu",
81
+ num_embeds_ada_norm: Optional[int] = None,
82
+ use_linear_projection: bool = False,
83
+ only_cross_attention: bool = False,
84
+ double_self_attention: bool = False,
85
+ upcast_attention: bool = False,
86
+ norm_type: str = "layer_norm", # 'layer_norm', 'ada_norm', 'ada_norm_zero', 'ada_norm_single', 'ada_norm_continuous', 'layer_norm_i2vgen'
87
+ norm_elementwise_affine: bool = True,
88
+ norm_eps: float = 1e-5,
89
+ attention_type: str = "default",
90
+ caption_channels: int = None,
91
+ interpolation_scale: float = None,
92
+ use_adapter: bool = False,
93
+ ):
94
+ super().__init__()
95
+ if patch_size is not None:
96
+ if norm_type not in ["ada_norm", "ada_norm_zero", "ada_norm_single"]:
97
+ raise NotImplementedError(
98
+ f"Forward pass is not implemented when `patch_size` is not None and `norm_type` is '{norm_type}'."
99
+ )
100
+ elif norm_type in ["ada_norm", "ada_norm_zero"] and num_embeds_ada_norm is None:
101
+ raise ValueError(
102
+ f"When using a `patch_size` and this `norm_type` ({norm_type}), `num_embeds_ada_norm` cannot be None."
103
+ )
104
+
105
+ self.use_linear_projection = use_linear_projection
106
+ self.num_attention_heads = num_attention_heads
107
+ self.attention_head_dim = attention_head_dim
108
+ inner_dim = num_attention_heads * attention_head_dim
109
+
110
+ conv_cls = nn.Conv2d
111
+ linear_cls = nn.Linear
112
+
113
+ # 1. Transformer2DModel can process both standard continuous images of shape `(batch_size, num_channels, width, height)` as well as quantized image embeddings of shape `(batch_size, num_image_vectors)`
114
+ # Define whether input is continuous or discrete depending on configuration
115
+ self.is_input_continuous = (in_channels is not None) and (patch_size is None)
116
+ self.is_input_vectorized = num_vector_embeds is not None
117
+ self.is_input_patches = in_channels is not None and patch_size is not None
118
+
119
+ if norm_type == "layer_norm" and num_embeds_ada_norm is not None:
120
+ deprecation_message = (
121
+ f"The configuration file of this model: {self.__class__} is outdated. `norm_type` is either not set or"
122
+ " incorrectly set to `'layer_norm'`.Make sure to set `norm_type` to `'ada_norm'` in the config."
123
+ " Please make sure to update the config accordingly as leaving `norm_type` might led to incorrect"
124
+ " results in future versions. If you have downloaded this checkpoint from the Hugging Face Hub, it"
125
+ " would be very nice if you could open a Pull request for the `transformer/config.json` file"
126
+ )
127
+ deprecate("norm_type!=num_embeds_ada_norm", "1.0.0", deprecation_message, standard_warn=False)
128
+ norm_type = "ada_norm"
129
+
130
+ if self.is_input_continuous and self.is_input_vectorized:
131
+ raise ValueError(
132
+ f"Cannot define both `in_channels`: {in_channels} and `num_vector_embeds`: {num_vector_embeds}. Make"
133
+ " sure that either `in_channels` or `num_vector_embeds` is None."
134
+ )
135
+ elif self.is_input_vectorized and self.is_input_patches:
136
+ raise ValueError(
137
+ f"Cannot define both `num_vector_embeds`: {num_vector_embeds} and `patch_size`: {patch_size}. Make"
138
+ " sure that either `num_vector_embeds` or `num_patches` is None."
139
+ )
140
+ elif not self.is_input_continuous and not self.is_input_vectorized and not self.is_input_patches:
141
+ raise ValueError(
142
+ f"Has to define `in_channels`: {in_channels}, `num_vector_embeds`: {num_vector_embeds}, or patch_size:"
143
+ f" {patch_size}. Make sure that `in_channels`, `num_vector_embeds` or `num_patches` is not None."
144
+ )
145
+
146
+ # 2. Define input layers
147
+ if self.is_input_continuous:
148
+ self.in_channels = in_channels
149
+
150
+ self.norm = torch.nn.GroupNorm(num_groups=norm_num_groups, num_channels=in_channels, eps=1e-6, affine=True)
151
+ if use_linear_projection:
152
+ self.proj_in = linear_cls(in_channels, inner_dim)
153
+ else:
154
+ self.proj_in = conv_cls(in_channels, inner_dim, kernel_size=1, stride=1, padding=0)
155
+ elif self.is_input_vectorized:
156
+ assert sample_size is not None, "Transformer2DModel over discrete input must provide sample_size"
157
+ assert num_vector_embeds is not None, "Transformer2DModel over discrete input must provide num_embed"
158
+
159
+ self.height = sample_size
160
+ self.width = sample_size
161
+ self.num_vector_embeds = num_vector_embeds
162
+ self.num_latent_pixels = self.height * self.width
163
+
164
+ self.latent_image_embedding = ImagePositionalEmbeddings(
165
+ num_embed=num_vector_embeds, embed_dim=inner_dim, height=self.height, width=self.width
166
+ )
167
+ elif self.is_input_patches:
168
+ assert sample_size is not None, "Transformer2DModel over patched input must provide sample_size"
169
+
170
+ self.height = sample_size
171
+ self.width = sample_size
172
+
173
+ self.patch_size = patch_size
174
+ interpolation_scale = (
175
+ interpolation_scale if interpolation_scale is not None else max(self.config.sample_size // 64, 1)
176
+ )
177
+ self.pos_embed = PatchEmbed(
178
+ height=sample_size,
179
+ width=sample_size,
180
+ patch_size=patch_size,
181
+ in_channels=in_channels,
182
+ embed_dim=inner_dim,
183
+ interpolation_scale=interpolation_scale,
184
+ )
185
+
186
+ # 3. Define transformers blocks
187
+ self.transformer_blocks = nn.ModuleList(
188
+ [
189
+ BasicTransformerBlock(
190
+ inner_dim,
191
+ num_attention_heads,
192
+ attention_head_dim,
193
+ dropout=dropout,
194
+ cross_attention_dim=cross_attention_dim,
195
+ activation_fn=activation_fn,
196
+ num_embeds_ada_norm=num_embeds_ada_norm,
197
+ attention_bias=attention_bias,
198
+ only_cross_attention=only_cross_attention,
199
+ double_self_attention=double_self_attention,
200
+ upcast_attention=upcast_attention,
201
+ norm_type=norm_type,
202
+ norm_elementwise_affine=norm_elementwise_affine,
203
+ norm_eps=norm_eps,
204
+ attention_type=attention_type,
205
+ use_adapter=use_adapter,
206
+ )
207
+ for d in range(num_layers)
208
+ ]
209
+ )
210
+
211
+ # 4. Define output layers
212
+ self.out_channels = in_channels if out_channels is None else out_channels
213
+ if self.is_input_continuous:
214
+ # TODO: should use out_channels for continuous projections
215
+ if use_linear_projection:
216
+ self.proj_out = linear_cls(inner_dim, in_channels)
217
+ else:
218
+ self.proj_out = conv_cls(inner_dim, in_channels, kernel_size=1, stride=1, padding=0)
219
+ elif self.is_input_vectorized:
220
+ self.norm_out = nn.LayerNorm(inner_dim)
221
+ self.out = nn.Linear(inner_dim, self.num_vector_embeds - 1)
222
+ elif self.is_input_patches and norm_type != "ada_norm_single":
223
+ self.norm_out = nn.LayerNorm(inner_dim, elementwise_affine=False, eps=1e-6)
224
+ self.proj_out_1 = nn.Linear(inner_dim, 2 * inner_dim)
225
+ self.proj_out_2 = nn.Linear(inner_dim, patch_size * patch_size * self.out_channels)
226
+ elif self.is_input_patches and norm_type == "ada_norm_single":
227
+ self.norm_out = nn.LayerNorm(inner_dim, elementwise_affine=False, eps=1e-6)
228
+ self.scale_shift_table = nn.Parameter(torch.randn(2, inner_dim) / inner_dim**0.5)
229
+ self.proj_out = nn.Linear(inner_dim, patch_size * patch_size * self.out_channels)
230
+
231
+ # 5. PixArt-Alpha blocks.
232
+ self.adaln_single = None
233
+ self.use_additional_conditions = False
234
+ if norm_type == "ada_norm_single":
235
+ self.use_additional_conditions = self.config.sample_size == 128
236
+ # TODO(Sayak, PVP) clean this, for now we use sample size to determine whether to use
237
+ # additional conditions until we find better name
238
+ self.adaln_single = AdaLayerNormSingle(inner_dim, use_additional_conditions=self.use_additional_conditions)
239
+
240
+ self.caption_projection = None
241
+ if caption_channels is not None:
242
+ self.caption_projection = PixArtAlphaTextProjection(in_features=caption_channels, hidden_size=inner_dim)
243
+
244
+ self.gradient_checkpointing = False
245
+
246
+ def _set_gradient_checkpointing(self, module, value=False):
247
+ if hasattr(module, "gradient_checkpointing"):
248
+ module.gradient_checkpointing = value
249
+
250
+ def forward(
251
+ self,
252
+ hidden_states: torch.Tensor,
253
+ encoder_hidden_states: Optional[torch.Tensor] = None,
254
+ timestep: Optional[torch.LongTensor] = None,
255
+ added_cond_kwargs: Dict[str, torch.Tensor] = None,
256
+ class_labels: Optional[torch.LongTensor] = None,
257
+ cross_attention_kwargs: Dict[str, Any] = None,
258
+ attention_mask: Optional[torch.Tensor] = None,
259
+ encoder_attention_mask: Optional[torch.Tensor] = None,
260
+ return_dict: bool = True,
261
+ audio_context: Optional[torch.Tensor] = None,
262
+ f_multiplier: Optional[float] = 1.0
263
+ ):
264
+ """
265
+ The [`Transformer2DModel`] forward method.
266
+
267
+ Args:
268
+ hidden_states (`torch.LongTensor` of shape `(batch size, num latent pixels)` if discrete, `torch.FloatTensor` of shape `(batch size, channel, height, width)` if continuous):
269
+ Input `hidden_states`.
270
+ encoder_hidden_states ( `torch.FloatTensor` of shape `(batch size, sequence len, embed dims)`, *optional*):
271
+ Conditional embeddings for cross attention layer. If not given, cross-attention defaults to
272
+ self-attention.
273
+ timestep ( `torch.LongTensor`, *optional*):
274
+ Used to indicate denoising step. Optional timestep to be applied as an embedding in `AdaLayerNorm`.
275
+ class_labels ( `torch.LongTensor` of shape `(batch size, num classes)`, *optional*):
276
+ Used to indicate class labels conditioning. Optional class labels to be applied as an embedding in
277
+ `AdaLayerZeroNorm`.
278
+ cross_attention_kwargs ( `Dict[str, Any]`, *optional*):
279
+ A kwargs dictionary that if specified is passed along to the `AttentionProcessor` as defined under
280
+ `self.processor` in
281
+ [diffusers.models.attention_processor](https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/attention_processor.py).
282
+ attention_mask ( `torch.Tensor`, *optional*):
283
+ An attention mask of shape `(batch, key_tokens)` is applied to `encoder_hidden_states`. If `1` the mask
284
+ is kept, otherwise if `0` it is discarded. Mask will be converted into a bias, which adds large
285
+ negative values to the attention scores corresponding to "discard" tokens.
286
+ encoder_attention_mask ( `torch.Tensor`, *optional*):
287
+ Cross-attention mask applied to `encoder_hidden_states`. Two formats supported:
288
+
289
+ * Mask `(batch, sequence_length)` True = keep, False = discard.
290
+ * Bias `(batch, 1, sequence_length)` 0 = keep, -10000 = discard.
291
+
292
+ If `ndim == 2`: will be interpreted as a mask, then converted into a bias consistent with the format
293
+ above. This bias will be added to the cross-attention scores.
294
+ return_dict (`bool`, *optional*, defaults to `True`):
295
+ Whether or not to return a [`~models.unets.unet_2d_condition.UNet2DConditionOutput`] instead of a plain
296
+ tuple.
297
+
298
+ Returns:
299
+ If `return_dict` is True, an [`~models.transformer_2d.Transformer2DModelOutput`] is returned, otherwise a
300
+ `tuple` where the first element is the sample tensor.
301
+ """
302
+ if cross_attention_kwargs is not None:
303
+ if cross_attention_kwargs.get("scale", None) is not None:
304
+ logger.warning("Passing `scale` to `cross_attention_kwargs` is depcrecated. `scale` will be ignored.")
305
+ # ensure attention_mask is a bias, and give it a singleton query_tokens dimension.
306
+ # we may have done this conversion already, e.g. if we came here via UNet2DConditionModel#forward.
307
+ # we can tell by counting dims; if ndim == 2: it's a mask rather than a bias.
308
+ # expects mask of shape:
309
+ # [batch, key_tokens]
310
+ # adds singleton query_tokens dimension:
311
+ # [batch, 1, key_tokens]
312
+ # this helps to broadcast it as a bias over attention scores, which will be in one of the following shapes:
313
+ # [batch, heads, query_tokens, key_tokens] (e.g. torch sdp attn)
314
+ # [batch * heads, query_tokens, key_tokens] (e.g. xformers or classic attn)
315
+ if attention_mask is not None and attention_mask.ndim == 2:
316
+ # assume that mask is expressed as:
317
+ # (1 = keep, 0 = discard)
318
+ # convert mask into a bias that can be added to attention scores:
319
+ # (keep = +0, discard = -10000.0)
320
+ attention_mask = (1 - attention_mask.to(hidden_states.dtype)) * -10000.0
321
+ attention_mask = attention_mask.unsqueeze(1)
322
+
323
+ # convert encoder_attention_mask to a bias the same way we do for attention_mask
324
+ if encoder_attention_mask is not None and encoder_attention_mask.ndim == 2:
325
+ encoder_attention_mask = (1 - encoder_attention_mask.to(hidden_states.dtype)) * -10000.0
326
+ encoder_attention_mask = encoder_attention_mask.unsqueeze(1)
327
+
328
+ # 1. Input
329
+ if self.is_input_continuous:
330
+ batch, _, height, width = hidden_states.shape
331
+ residual = hidden_states
332
+
333
+ hidden_states = self.norm(hidden_states)
334
+ if not self.use_linear_projection:
335
+ hidden_states = self.proj_in(hidden_states)
336
+ inner_dim = hidden_states.shape[1]
337
+ hidden_states = hidden_states.permute(0, 2, 3, 1).reshape(batch, height * width, inner_dim)
338
+ else:
339
+ inner_dim = hidden_states.shape[1]
340
+ hidden_states = hidden_states.permute(0, 2, 3, 1).reshape(batch, height * width, inner_dim)
341
+ hidden_states = self.proj_in(hidden_states)
342
+
343
+ elif self.is_input_vectorized:
344
+ hidden_states = self.latent_image_embedding(hidden_states)
345
+ elif self.is_input_patches:
346
+ height, width = hidden_states.shape[-2] // self.patch_size, hidden_states.shape[-1] // self.patch_size
347
+ hidden_states = self.pos_embed(hidden_states)
348
+
349
+ if self.adaln_single is not None:
350
+ if self.use_additional_conditions and added_cond_kwargs is None:
351
+ raise ValueError(
352
+ "`added_cond_kwargs` cannot be None when using additional conditions for `adaln_single`."
353
+ )
354
+ batch_size = hidden_states.shape[0]
355
+ timestep, embedded_timestep = self.adaln_single(
356
+ timestep, added_cond_kwargs, batch_size=batch_size, hidden_dtype=hidden_states.dtype
357
+ )
358
+
359
+ # 2. Blocks
360
+ if self.caption_projection is not None:
361
+ batch_size = hidden_states.shape[0]
362
+ encoder_hidden_states = self.caption_projection(encoder_hidden_states)
363
+ encoder_hidden_states = encoder_hidden_states.view(batch_size, -1, hidden_states.shape[-1])
364
+
365
+ for block in self.transformer_blocks:
366
+ if self.training and self.gradient_checkpointing:
367
+
368
+ def create_custom_forward(module, return_dict=None):
369
+ def custom_forward(*inputs):
370
+ if return_dict is not None:
371
+ return module(*inputs, return_dict=return_dict)
372
+ else:
373
+ return module(*inputs)
374
+
375
+ return custom_forward
376
+
377
+ ckpt_kwargs: Dict[str, Any] = {"use_reentrant": False} if is_torch_version(">=", "1.11.0") else {}
378
+ hidden_states = torch.utils.checkpoint.checkpoint(
379
+ create_custom_forward(block),
380
+ hidden_states,
381
+ attention_mask,
382
+ encoder_hidden_states,
383
+ encoder_attention_mask,
384
+ timestep,
385
+ cross_attention_kwargs,
386
+ class_labels,
387
+ audio_context,
388
+ f_multiplier,
389
+ **ckpt_kwargs,
390
+ )
391
+ else:
392
+ hidden_states = block(
393
+ hidden_states,
394
+ attention_mask=attention_mask,
395
+ encoder_hidden_states=encoder_hidden_states,
396
+ encoder_attention_mask=encoder_attention_mask,
397
+ timestep=timestep,
398
+ cross_attention_kwargs=cross_attention_kwargs,
399
+ class_labels=class_labels,
400
+ audio_context=audio_context,
401
+ f_multiplier=f_multiplier,
402
+ )
403
+
404
+ # 3. Output
405
+ if self.is_input_continuous:
406
+ if not self.use_linear_projection:
407
+ hidden_states = hidden_states.reshape(batch, height, width, inner_dim).permute(0, 3, 1, 2).contiguous()
408
+ hidden_states = self.proj_out(hidden_states)
409
+ else:
410
+ hidden_states = self.proj_out(hidden_states)
411
+ hidden_states = hidden_states.reshape(batch, height, width, inner_dim).permute(0, 3, 1, 2).contiguous()
412
+
413
+ output = hidden_states + residual
414
+ elif self.is_input_vectorized:
415
+ hidden_states = self.norm_out(hidden_states)
416
+ logits = self.out(hidden_states)
417
+ # (batch, self.num_vector_embeds - 1, self.num_latent_pixels)
418
+ logits = logits.permute(0, 2, 1)
419
+
420
+ # log(p(x_0))
421
+ output = F.log_softmax(logits.double(), dim=1).float()
422
+
423
+ if self.is_input_patches:
424
+ if self.config.norm_type != "ada_norm_single":
425
+ conditioning = self.transformer_blocks[0].norm1.emb(
426
+ timestep, class_labels, hidden_dtype=hidden_states.dtype
427
+ )
428
+ shift, scale = self.proj_out_1(F.silu(conditioning)).chunk(2, dim=1)
429
+ hidden_states = self.norm_out(hidden_states) * (1 + scale[:, None]) + shift[:, None]
430
+ hidden_states = self.proj_out_2(hidden_states)
431
+ elif self.config.norm_type == "ada_norm_single":
432
+ shift, scale = (self.scale_shift_table[None] + embedded_timestep[:, None]).chunk(2, dim=1)
433
+ hidden_states = self.norm_out(hidden_states)
434
+ # Modulation
435
+ hidden_states = hidden_states * (1 + scale) + shift
436
+ hidden_states = self.proj_out(hidden_states)
437
+ hidden_states = hidden_states.squeeze(1)
438
+
439
+ # unpatchify
440
+ if self.adaln_single is None:
441
+ height = width = int(hidden_states.shape[1] ** 0.5)
442
+ hidden_states = hidden_states.reshape(
443
+ shape=(-1, height, width, self.patch_size, self.patch_size, self.out_channels)
444
+ )
445
+ hidden_states = torch.einsum("nhwpqc->nchpwq", hidden_states)
446
+ output = hidden_states.reshape(
447
+ shape=(-1, self.out_channels, height * self.patch_size, width * self.patch_size)
448
+ )
449
+
450
+ if not return_dict:
451
+ return (output,)
452
+
453
+ return Transformer2DModelOutput(sample=output)
unet2d_custom.py ADDED
@@ -0,0 +1,1314 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Adapted from https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/unet_2d_blocks.py
2
+
3
+ from dataclasses import dataclass
4
+ from typing import Any, Dict, List, Optional, Tuple, Union
5
+
6
+ import torch
7
+ import torch.nn as nn
8
+ import torch.utils.checkpoint
9
+
10
+ from diffusers.configuration_utils import ConfigMixin, register_to_config
11
+ from diffusers.loaders import PeftAdapterMixin, UNet2DConditionLoadersMixin
12
+ #from diffusers.loaders import UNet2DConditionLoadersMixin
13
+
14
+ from diffusers.utils import USE_PEFT_BACKEND, BaseOutput, deprecate, logging, scale_lora_layers, unscale_lora_layers
15
+ from diffusers.models.activations import get_activation
16
+
17
+ from diffusers.models.attention_processor import (
18
+ ADDED_KV_ATTENTION_PROCESSORS,
19
+ CROSS_ATTENTION_PROCESSORS,
20
+ Attention,
21
+ AttentionProcessor,
22
+ AttnAddedKVProcessor,
23
+ AttnProcessor,
24
+ )
25
+ from diffusers.models.embeddings import (
26
+ GaussianFourierProjection,
27
+ #GLIGENTextBoundingboxProjection,
28
+ ImageHintTimeEmbedding,
29
+ ImageProjection,
30
+ ImageTimeEmbedding,
31
+ TextImageProjection,
32
+ TextImageTimeEmbedding,
33
+ TextTimeEmbedding,
34
+ TimestepEmbedding,
35
+ Timesteps,
36
+ )
37
+ from diffusers.models.modeling_utils import ModelMixin
38
+
39
+ from unet_2d_blocks_custom import (
40
+ get_down_block,
41
+ get_mid_block,
42
+ get_up_block,
43
+ )
44
+
45
+ logger = logging.get_logger(__name__) # pylint: disable=invalid-name
46
+
47
+
48
+
49
+
50
+ @dataclass
51
+ class UNet2DConditionOutput(BaseOutput):
52
+ """
53
+ The output of [`UNet2DConditionModel`].
54
+
55
+ Args:
56
+ sample (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):
57
+ The hidden states output conditioned on `encoder_hidden_states` input. Output of last layer of model.
58
+ """
59
+
60
+ sample: torch.FloatTensor = None
61
+
62
+
63
+ class UNet2DConditionModel(ModelMixin, ConfigMixin, UNet2DConditionLoadersMixin, PeftAdapterMixin):
64
+ r"""
65
+ A conditional 2D UNet model that takes a noisy sample, conditional state, and a timestep and returns a sample
66
+ shaped output.
67
+
68
+ This model inherits from [`ModelMixin`]. Check the superclass documentation for it's generic methods implemented
69
+ for all models (such as downloading or saving).
70
+
71
+ Parameters:
72
+ sample_size (`int` or `Tuple[int, int]`, *optional*, defaults to `None`):
73
+ Height and width of input/output sample.
74
+ in_channels (`int`, *optional*, defaults to 4): Number of channels in the input sample.
75
+ out_channels (`int`, *optional*, defaults to 4): Number of channels in the output.
76
+ center_input_sample (`bool`, *optional*, defaults to `False`): Whether to center the input sample.
77
+ flip_sin_to_cos (`bool`, *optional*, defaults to `True`):
78
+ Whether to flip the sin to cos in the time embedding.
79
+ freq_shift (`int`, *optional*, defaults to 0): The frequency shift to apply to the time embedding.
80
+ down_block_types (`Tuple[str]`, *optional*, defaults to `("CrossAttnDownBlock2D", "CrossAttnDownBlock2D", "CrossAttnDownBlock2D", "DownBlock2D")`):
81
+ The tuple of downsample blocks to use.
82
+ mid_block_type (`str`, *optional*, defaults to `"UNetMidBlock2DCrossAttn"`):
83
+ Block type for middle of UNet, it can be one of `UNetMidBlock2DCrossAttn`, `UNetMidBlock2D`, or
84
+ `UNetMidBlock2DSimpleCrossAttn`. If `None`, the mid block layer is skipped.
85
+ up_block_types (`Tuple[str]`, *optional*, defaults to `("UpBlock2D", "CrossAttnUpBlock2D", "CrossAttnUpBlock2D", "CrossAttnUpBlock2D")`):
86
+ The tuple of upsample blocks to use.
87
+ only_cross_attention(`bool` or `Tuple[bool]`, *optional*, default to `False`):
88
+ Whether to include self-attention in the basic transformer blocks, see
89
+ [`~models.attention.BasicTransformerBlock`].
90
+ block_out_channels (`Tuple[int]`, *optional*, defaults to `(320, 640, 1280, 1280)`):
91
+ The tuple of output channels for each block.
92
+ layers_per_block (`int`, *optional*, defaults to 2): The number of layers per block.
93
+ downsample_padding (`int`, *optional*, defaults to 1): The padding to use for the downsampling convolution.
94
+ mid_block_scale_factor (`float`, *optional*, defaults to 1.0): The scale factor to use for the mid block.
95
+ dropout (`float`, *optional*, defaults to 0.0): The dropout probability to use.
96
+ act_fn (`str`, *optional*, defaults to `"silu"`): The activation function to use.
97
+ norm_num_groups (`int`, *optional*, defaults to 32): The number of groups to use for the normalization.
98
+ If `None`, normalization and activation layers is skipped in post-processing.
99
+ norm_eps (`float`, *optional*, defaults to 1e-5): The epsilon to use for the normalization.
100
+ cross_attention_dim (`int` or `Tuple[int]`, *optional*, defaults to 1280):
101
+ The dimension of the cross attention features.
102
+ transformer_layers_per_block (`int`, `Tuple[int]`, or `Tuple[Tuple]` , *optional*, defaults to 1):
103
+ The number of transformer blocks of type [`~models.attention.BasicTransformerBlock`]. Only relevant for
104
+ [`~models.unet_2d_blocks.CrossAttnDownBlock2D`], [`~models.unet_2d_blocks.CrossAttnUpBlock2D`],
105
+ [`~models.unet_2d_blocks.UNetMidBlock2DCrossAttn`].
106
+ reverse_transformer_layers_per_block : (`Tuple[Tuple]`, *optional*, defaults to None):
107
+ The number of transformer blocks of type [`~models.attention.BasicTransformerBlock`], in the upsampling
108
+ blocks of the U-Net. Only relevant if `transformer_layers_per_block` is of type `Tuple[Tuple]` and for
109
+ [`~models.unet_2d_blocks.CrossAttnDownBlock2D`], [`~models.unet_2d_blocks.CrossAttnUpBlock2D`],
110
+ [`~models.unet_2d_blocks.UNetMidBlock2DCrossAttn`].
111
+ encoder_hid_dim (`int`, *optional*, defaults to None):
112
+ If `encoder_hid_dim_type` is defined, `encoder_hidden_states` will be projected from `encoder_hid_dim`
113
+ dimension to `cross_attention_dim`.
114
+ encoder_hid_dim_type (`str`, *optional*, defaults to `None`):
115
+ If given, the `encoder_hidden_states` and potentially other embeddings are down-projected to text
116
+ embeddings of dimension `cross_attention` according to `encoder_hid_dim_type`.
117
+ attention_head_dim (`int`, *optional*, defaults to 8): The dimension of the attention heads.
118
+ num_attention_heads (`int`, *optional*):
119
+ The number of attention heads. If not defined, defaults to `attention_head_dim`
120
+ resnet_time_scale_shift (`str`, *optional*, defaults to `"default"`): Time scale shift config
121
+ for ResNet blocks (see [`~models.resnet.ResnetBlock2D`]). Choose from `default` or `scale_shift`.
122
+ class_embed_type (`str`, *optional*, defaults to `None`):
123
+ The type of class embedding to use which is ultimately summed with the time embeddings. Choose from `None`,
124
+ `"timestep"`, `"identity"`, `"projection"`, or `"simple_projection"`.
125
+ addition_embed_type (`str`, *optional*, defaults to `None`):
126
+ Configures an optional embedding which will be summed with the time embeddings. Choose from `None` or
127
+ "text". "text" will use the `TextTimeEmbedding` layer.
128
+ addition_time_embed_dim: (`int`, *optional*, defaults to `None`):
129
+ Dimension for the timestep embeddings.
130
+ num_class_embeds (`int`, *optional*, defaults to `None`):
131
+ Input dimension of the learnable embedding matrix to be projected to `time_embed_dim`, when performing
132
+ class conditioning with `class_embed_type` equal to `None`.
133
+ time_embedding_type (`str`, *optional*, defaults to `positional`):
134
+ The type of position embedding to use for timesteps. Choose from `positional` or `fourier`.
135
+ time_embedding_dim (`int`, *optional*, defaults to `None`):
136
+ An optional override for the dimension of the projected time embedding.
137
+ time_embedding_act_fn (`str`, *optional*, defaults to `None`):
138
+ Optional activation function to use only once on the time embeddings before they are passed to the rest of
139
+ the UNet. Choose from `silu`, `mish`, `gelu`, and `swish`.
140
+ timestep_post_act (`str`, *optional*, defaults to `None`):
141
+ The second activation function to use in timestep embedding. Choose from `silu`, `mish` and `gelu`.
142
+ time_cond_proj_dim (`int`, *optional*, defaults to `None`):
143
+ The dimension of `cond_proj` layer in the timestep embedding.
144
+ conv_in_kernel (`int`, *optional*, default to `3`): The kernel size of `conv_in` layer.
145
+ conv_out_kernel (`int`, *optional*, default to `3`): The kernel size of `conv_out` layer.
146
+ projection_class_embeddings_input_dim (`int`, *optional*): The dimension of the `class_labels` input when
147
+ `class_embed_type="projection"`. Required when `class_embed_type="projection"`.
148
+ class_embeddings_concat (`bool`, *optional*, defaults to `False`): Whether to concatenate the time
149
+ embeddings with the class embeddings.
150
+ mid_block_only_cross_attention (`bool`, *optional*, defaults to `None`):
151
+ Whether to use cross attention with the mid block when using the `UNetMidBlock2DSimpleCrossAttn`. If
152
+ `only_cross_attention` is given as a single boolean and `mid_block_only_cross_attention` is `None`, the
153
+ `only_cross_attention` value is used as the value for `mid_block_only_cross_attention`. Default to `False`
154
+ otherwise.
155
+ """
156
+
157
+ _supports_gradient_checkpointing = True
158
+
159
+ @register_to_config
160
+ def __init__(
161
+ self,
162
+ sample_size: Optional[int] = None,
163
+ in_channels: int = 4,
164
+ out_channels: int = 4,
165
+ center_input_sample: bool = False,
166
+ flip_sin_to_cos: bool = True,
167
+ freq_shift: int = 0,
168
+ down_block_types: Tuple[str] = (
169
+ "CrossAttnDownBlock2D",
170
+ "CrossAttnDownBlock2D",
171
+ "CrossAttnDownBlock2D",
172
+ "DownBlock2D",
173
+ ),
174
+ mid_block_type: Optional[str] = "UNetMidBlock2DCrossAttn",
175
+ up_block_types: Tuple[str] = ("UpBlock2D", "CrossAttnUpBlock2D", "CrossAttnUpBlock2D", "CrossAttnUpBlock2D"),
176
+ only_cross_attention: Union[bool, Tuple[bool]] = False,
177
+ block_out_channels: Tuple[int] = (320, 640, 1280, 1280),
178
+ layers_per_block: Union[int, Tuple[int]] = 2,
179
+ downsample_padding: int = 1,
180
+ mid_block_scale_factor: float = 1,
181
+ dropout: float = 0.0,
182
+ act_fn: str = "silu",
183
+ norm_num_groups: Optional[int] = 32,
184
+ norm_eps: float = 1e-5,
185
+ cross_attention_dim: Union[int, Tuple[int]] = 1280,
186
+ transformer_layers_per_block: Union[int, Tuple[int], Tuple[Tuple]] = 1,
187
+ reverse_transformer_layers_per_block: Optional[Tuple[Tuple[int]]] = None,
188
+ encoder_hid_dim: Optional[int] = None,
189
+ encoder_hid_dim_type: Optional[str] = None,
190
+ attention_head_dim: Union[int, Tuple[int]] = 8,
191
+ num_attention_heads: Optional[Union[int, Tuple[int]]] = None,
192
+ dual_cross_attention: bool = False,
193
+ use_linear_projection: bool = False,
194
+ class_embed_type: Optional[str] = None,
195
+ addition_embed_type: Optional[str] = None,
196
+ addition_time_embed_dim: Optional[int] = None,
197
+ num_class_embeds: Optional[int] = None,
198
+ upcast_attention: bool = False,
199
+ resnet_time_scale_shift: str = "default",
200
+ resnet_skip_time_act: bool = False,
201
+ resnet_out_scale_factor: float = 1.0,
202
+ time_embedding_type: str = "positional",
203
+ time_embedding_dim: Optional[int] = None,
204
+ time_embedding_act_fn: Optional[str] = None,
205
+ timestep_post_act: Optional[str] = None,
206
+ time_cond_proj_dim: Optional[int] = None,
207
+ conv_in_kernel: int = 3,
208
+ conv_out_kernel: int = 3,
209
+ projection_class_embeddings_input_dim: Optional[int] = None,
210
+ attention_type: str = "default",
211
+ class_embeddings_concat: bool = False,
212
+ mid_block_only_cross_attention: Optional[bool] = None,
213
+ cross_attention_norm: Optional[str] = None,
214
+ addition_embed_type_num_heads: int = 64,
215
+ use_adapter_list: list = [False, False, False],
216
+ ):
217
+ super().__init__()
218
+
219
+ self.sample_size = sample_size
220
+
221
+ if num_attention_heads is not None:
222
+ raise ValueError(
223
+ "At the moment it is not possible to define the number of attention heads via `num_attention_heads` because of a naming issue as described in https://github.com/huggingface/diffusers/issues/2011#issuecomment-1547958131. Passing `num_attention_heads` will only be supported in diffusers v0.19."
224
+ )
225
+
226
+ # If `num_attention_heads` is not defined (which is the case for most models)
227
+ # it will default to `attention_head_dim`. This looks weird upon first reading it and it is.
228
+ # The reason for this behavior is to correct for incorrectly named variables that were introduced
229
+ # when this library was created. The incorrect naming was only discovered much later in https://github.com/huggingface/diffusers/issues/2011#issuecomment-1547958131
230
+ # Changing `attention_head_dim` to `num_attention_heads` for 40,000+ configurations is too backwards breaking
231
+ # which is why we correct for the naming here.
232
+ num_attention_heads = num_attention_heads or attention_head_dim
233
+
234
+ # Check inputs
235
+ self._check_config(
236
+ down_block_types=down_block_types,
237
+ up_block_types=up_block_types,
238
+ only_cross_attention=only_cross_attention,
239
+ block_out_channels=block_out_channels,
240
+ layers_per_block=layers_per_block,
241
+ cross_attention_dim=cross_attention_dim,
242
+ transformer_layers_per_block=transformer_layers_per_block,
243
+ reverse_transformer_layers_per_block=reverse_transformer_layers_per_block,
244
+ attention_head_dim=attention_head_dim,
245
+ num_attention_heads=num_attention_heads,
246
+ )
247
+
248
+ # input
249
+ conv_in_padding = (conv_in_kernel - 1) // 2
250
+ self.conv_in = nn.Conv2d(
251
+ in_channels, block_out_channels[0], kernel_size=conv_in_kernel, padding=conv_in_padding
252
+ )
253
+
254
+ # time
255
+ time_embed_dim, timestep_input_dim = self._set_time_proj(
256
+ time_embedding_type,
257
+ block_out_channels=block_out_channels,
258
+ flip_sin_to_cos=flip_sin_to_cos,
259
+ freq_shift=freq_shift,
260
+ time_embedding_dim=time_embedding_dim,
261
+ )
262
+
263
+ self.time_embedding = TimestepEmbedding(
264
+ timestep_input_dim,
265
+ time_embed_dim,
266
+ act_fn=act_fn,
267
+ post_act_fn=timestep_post_act,
268
+ cond_proj_dim=time_cond_proj_dim,
269
+ )
270
+
271
+ self._set_encoder_hid_proj(
272
+ encoder_hid_dim_type,
273
+ cross_attention_dim=cross_attention_dim,
274
+ encoder_hid_dim=encoder_hid_dim,
275
+ )
276
+
277
+ # class embedding
278
+ self._set_class_embedding(
279
+ class_embed_type,
280
+ act_fn=act_fn,
281
+ num_class_embeds=num_class_embeds,
282
+ projection_class_embeddings_input_dim=projection_class_embeddings_input_dim,
283
+ time_embed_dim=time_embed_dim,
284
+ timestep_input_dim=timestep_input_dim,
285
+ )
286
+
287
+ self._set_add_embedding(
288
+ addition_embed_type,
289
+ addition_embed_type_num_heads=addition_embed_type_num_heads,
290
+ addition_time_embed_dim=addition_time_embed_dim,
291
+ cross_attention_dim=cross_attention_dim,
292
+ encoder_hid_dim=encoder_hid_dim,
293
+ flip_sin_to_cos=flip_sin_to_cos,
294
+ freq_shift=freq_shift,
295
+ projection_class_embeddings_input_dim=projection_class_embeddings_input_dim,
296
+ time_embed_dim=time_embed_dim,
297
+ )
298
+
299
+ if time_embedding_act_fn is None:
300
+ self.time_embed_act = None
301
+ else:
302
+ self.time_embed_act = get_activation(time_embedding_act_fn)
303
+
304
+ self.down_blocks = nn.ModuleList([])
305
+ self.up_blocks = nn.ModuleList([])
306
+
307
+ if isinstance(only_cross_attention, bool):
308
+ if mid_block_only_cross_attention is None:
309
+ mid_block_only_cross_attention = only_cross_attention
310
+
311
+ only_cross_attention = [only_cross_attention] * len(down_block_types)
312
+
313
+ if mid_block_only_cross_attention is None:
314
+ mid_block_only_cross_attention = False
315
+
316
+ if isinstance(num_attention_heads, int):
317
+ num_attention_heads = (num_attention_heads,) * len(down_block_types)
318
+
319
+ if isinstance(attention_head_dim, int):
320
+ attention_head_dim = (attention_head_dim,) * len(down_block_types)
321
+
322
+ if isinstance(cross_attention_dim, int):
323
+ cross_attention_dim = (cross_attention_dim,) * len(down_block_types)
324
+
325
+ if isinstance(layers_per_block, int):
326
+ layers_per_block = [layers_per_block] * len(down_block_types)
327
+
328
+ if isinstance(transformer_layers_per_block, int):
329
+ transformer_layers_per_block = [transformer_layers_per_block] * len(down_block_types)
330
+
331
+ if class_embeddings_concat:
332
+ # The time embeddings are concatenated with the class embeddings. The dimension of the
333
+ # time embeddings passed to the down, middle, and up blocks is twice the dimension of the
334
+ # regular time embeddings
335
+ blocks_time_embed_dim = time_embed_dim * 2
336
+ else:
337
+ blocks_time_embed_dim = time_embed_dim
338
+
339
+ # down
340
+ output_channel = block_out_channels[0]
341
+ for i, down_block_type in enumerate(down_block_types):
342
+ input_channel = output_channel
343
+ output_channel = block_out_channels[i]
344
+ is_final_block = i == len(block_out_channels) - 1
345
+
346
+ down_block = get_down_block(
347
+ down_block_type,
348
+ num_layers=layers_per_block[i],
349
+ transformer_layers_per_block=transformer_layers_per_block[i],
350
+ in_channels=input_channel,
351
+ out_channels=output_channel,
352
+ temb_channels=blocks_time_embed_dim,
353
+ add_downsample=not is_final_block,
354
+ resnet_eps=norm_eps,
355
+ resnet_act_fn=act_fn,
356
+ resnet_groups=norm_num_groups,
357
+ cross_attention_dim=cross_attention_dim[i],
358
+ num_attention_heads=num_attention_heads[i],
359
+ downsample_padding=downsample_padding,
360
+ dual_cross_attention=dual_cross_attention,
361
+ use_linear_projection=use_linear_projection,
362
+ only_cross_attention=only_cross_attention[i],
363
+ upcast_attention=upcast_attention,
364
+ resnet_time_scale_shift=resnet_time_scale_shift,
365
+ attention_type=attention_type,
366
+ resnet_skip_time_act=resnet_skip_time_act,
367
+ resnet_out_scale_factor=resnet_out_scale_factor,
368
+ cross_attention_norm=cross_attention_norm,
369
+ attention_head_dim=attention_head_dim[i] if attention_head_dim[i] is not None else output_channel,
370
+ dropout=dropout,
371
+ use_adapter=use_adapter_list[0],
372
+ )
373
+ self.down_blocks.append(down_block)
374
+
375
+ # mid
376
+ self.mid_block = get_mid_block(
377
+ mid_block_type,
378
+ temb_channels=blocks_time_embed_dim,
379
+ in_channels=block_out_channels[-1],
380
+ resnet_eps=norm_eps,
381
+ resnet_act_fn=act_fn,
382
+ resnet_groups=norm_num_groups,
383
+ output_scale_factor=mid_block_scale_factor,
384
+ transformer_layers_per_block=transformer_layers_per_block[-1],
385
+ num_attention_heads=num_attention_heads[-1],
386
+ cross_attention_dim=cross_attention_dim[-1],
387
+ dual_cross_attention=dual_cross_attention,
388
+ use_linear_projection=use_linear_projection,
389
+ mid_block_only_cross_attention=mid_block_only_cross_attention,
390
+ upcast_attention=upcast_attention,
391
+ resnet_time_scale_shift=resnet_time_scale_shift,
392
+ attention_type=attention_type,
393
+ resnet_skip_time_act=resnet_skip_time_act,
394
+ cross_attention_norm=cross_attention_norm,
395
+ attention_head_dim=attention_head_dim[-1],
396
+ dropout=dropout,
397
+ use_adapter=use_adapter_list[1],
398
+ )
399
+
400
+ # count how many layers upsample the images
401
+ self.num_upsamplers = 0
402
+
403
+ # up
404
+ reversed_block_out_channels = list(reversed(block_out_channels))
405
+ reversed_num_attention_heads = list(reversed(num_attention_heads))
406
+ reversed_layers_per_block = list(reversed(layers_per_block))
407
+ reversed_cross_attention_dim = list(reversed(cross_attention_dim))
408
+ reversed_transformer_layers_per_block = (
409
+ list(reversed(transformer_layers_per_block))
410
+ if reverse_transformer_layers_per_block is None
411
+ else reverse_transformer_layers_per_block
412
+ )
413
+ only_cross_attention = list(reversed(only_cross_attention))
414
+
415
+ output_channel = reversed_block_out_channels[0]
416
+ for i, up_block_type in enumerate(up_block_types):
417
+ is_final_block = i == len(block_out_channels) - 1
418
+
419
+ prev_output_channel = output_channel
420
+ output_channel = reversed_block_out_channels[i]
421
+ input_channel = reversed_block_out_channels[min(i + 1, len(block_out_channels) - 1)]
422
+
423
+ # add upsample block for all BUT final layer
424
+ if not is_final_block:
425
+ add_upsample = True
426
+ self.num_upsamplers += 1
427
+ else:
428
+ add_upsample = False
429
+
430
+ up_block = get_up_block(
431
+ up_block_type,
432
+ num_layers=reversed_layers_per_block[i] + 1,
433
+ transformer_layers_per_block=reversed_transformer_layers_per_block[i],
434
+ in_channels=input_channel,
435
+ out_channels=output_channel,
436
+ prev_output_channel=prev_output_channel,
437
+ temb_channels=blocks_time_embed_dim,
438
+ add_upsample=add_upsample,
439
+ resnet_eps=norm_eps,
440
+ resnet_act_fn=act_fn,
441
+ resolution_idx=i,
442
+ resnet_groups=norm_num_groups,
443
+ cross_attention_dim=reversed_cross_attention_dim[i],
444
+ num_attention_heads=reversed_num_attention_heads[i],
445
+ dual_cross_attention=dual_cross_attention,
446
+ use_linear_projection=use_linear_projection,
447
+ only_cross_attention=only_cross_attention[i],
448
+ upcast_attention=upcast_attention,
449
+ resnet_time_scale_shift=resnet_time_scale_shift,
450
+ attention_type=attention_type,
451
+ resnet_skip_time_act=resnet_skip_time_act,
452
+ resnet_out_scale_factor=resnet_out_scale_factor,
453
+ cross_attention_norm=cross_attention_norm,
454
+ attention_head_dim=attention_head_dim[i] if attention_head_dim[i] is not None else output_channel,
455
+ dropout=dropout,
456
+ use_adapter=use_adapter_list[2],
457
+ )
458
+ self.up_blocks.append(up_block)
459
+ prev_output_channel = output_channel
460
+
461
+ # out
462
+ if norm_num_groups is not None:
463
+ self.conv_norm_out = nn.GroupNorm(
464
+ num_channels=block_out_channels[0], num_groups=norm_num_groups, eps=norm_eps
465
+ )
466
+
467
+ self.conv_act = get_activation(act_fn)
468
+
469
+ else:
470
+ self.conv_norm_out = None
471
+ self.conv_act = None
472
+
473
+ conv_out_padding = (conv_out_kernel - 1) // 2
474
+ self.conv_out = nn.Conv2d(
475
+ block_out_channels[0], out_channels, kernel_size=conv_out_kernel, padding=conv_out_padding
476
+ )
477
+
478
+ self._set_pos_net_if_use_gligen(attention_type=attention_type, cross_attention_dim=cross_attention_dim)
479
+
480
+ def _check_config(
481
+ self,
482
+ down_block_types: Tuple[str],
483
+ up_block_types: Tuple[str],
484
+ only_cross_attention: Union[bool, Tuple[bool]],
485
+ block_out_channels: Tuple[int],
486
+ layers_per_block: Union[int, Tuple[int]],
487
+ cross_attention_dim: Union[int, Tuple[int]],
488
+ transformer_layers_per_block: Union[int, Tuple[int], Tuple[Tuple[int]]],
489
+ reverse_transformer_layers_per_block: bool,
490
+ attention_head_dim: int,
491
+ num_attention_heads: Optional[Union[int, Tuple[int]]],
492
+ ):
493
+ if len(down_block_types) != len(up_block_types):
494
+ raise ValueError(
495
+ f"Must provide the same number of `down_block_types` as `up_block_types`. `down_block_types`: {down_block_types}. `up_block_types`: {up_block_types}."
496
+ )
497
+
498
+ if len(block_out_channels) != len(down_block_types):
499
+ raise ValueError(
500
+ f"Must provide the same number of `block_out_channels` as `down_block_types`. `block_out_channels`: {block_out_channels}. `down_block_types`: {down_block_types}."
501
+ )
502
+
503
+ if not isinstance(only_cross_attention, bool) and len(only_cross_attention) != len(down_block_types):
504
+ raise ValueError(
505
+ f"Must provide the same number of `only_cross_attention` as `down_block_types`. `only_cross_attention`: {only_cross_attention}. `down_block_types`: {down_block_types}."
506
+ )
507
+
508
+ if not isinstance(num_attention_heads, int) and len(num_attention_heads) != len(down_block_types):
509
+ raise ValueError(
510
+ f"Must provide the same number of `num_attention_heads` as `down_block_types`. `num_attention_heads`: {num_attention_heads}. `down_block_types`: {down_block_types}."
511
+ )
512
+
513
+ if not isinstance(attention_head_dim, int) and len(attention_head_dim) != len(down_block_types):
514
+ raise ValueError(
515
+ f"Must provide the same number of `attention_head_dim` as `down_block_types`. `attention_head_dim`: {attention_head_dim}. `down_block_types`: {down_block_types}."
516
+ )
517
+
518
+ if isinstance(cross_attention_dim, list) and len(cross_attention_dim) != len(down_block_types):
519
+ raise ValueError(
520
+ f"Must provide the same number of `cross_attention_dim` as `down_block_types`. `cross_attention_dim`: {cross_attention_dim}. `down_block_types`: {down_block_types}."
521
+ )
522
+
523
+ if not isinstance(layers_per_block, int) and len(layers_per_block) != len(down_block_types):
524
+ raise ValueError(
525
+ f"Must provide the same number of `layers_per_block` as `down_block_types`. `layers_per_block`: {layers_per_block}. `down_block_types`: {down_block_types}."
526
+ )
527
+ if isinstance(transformer_layers_per_block, list) and reverse_transformer_layers_per_block is None:
528
+ for layer_number_per_block in transformer_layers_per_block:
529
+ if isinstance(layer_number_per_block, list):
530
+ raise ValueError("Must provide 'reverse_transformer_layers_per_block` if using asymmetrical UNet.")
531
+
532
+ def _set_time_proj(
533
+ self,
534
+ time_embedding_type: str,
535
+ block_out_channels: int,
536
+ flip_sin_to_cos: bool,
537
+ freq_shift: float,
538
+ time_embedding_dim: int,
539
+ ) -> Tuple[int, int]:
540
+ if time_embedding_type == "fourier":
541
+ time_embed_dim = time_embedding_dim or block_out_channels[0] * 2
542
+ if time_embed_dim % 2 != 0:
543
+ raise ValueError(f"`time_embed_dim` should be divisible by 2, but is {time_embed_dim}.")
544
+ self.time_proj = GaussianFourierProjection(
545
+ time_embed_dim // 2, set_W_to_weight=False, log=False, flip_sin_to_cos=flip_sin_to_cos
546
+ )
547
+ timestep_input_dim = time_embed_dim
548
+ elif time_embedding_type == "positional":
549
+ time_embed_dim = time_embedding_dim or block_out_channels[0] * 4
550
+
551
+ self.time_proj = Timesteps(block_out_channels[0], flip_sin_to_cos, freq_shift)
552
+ timestep_input_dim = block_out_channels[0]
553
+ else:
554
+ raise ValueError(
555
+ f"{time_embedding_type} does not exist. Please make sure to use one of `fourier` or `positional`."
556
+ )
557
+
558
+ return time_embed_dim, timestep_input_dim
559
+
560
+ def _set_encoder_hid_proj(
561
+ self,
562
+ encoder_hid_dim_type: Optional[str],
563
+ cross_attention_dim: Union[int, Tuple[int]],
564
+ encoder_hid_dim: Optional[int],
565
+ ):
566
+ if encoder_hid_dim_type is None and encoder_hid_dim is not None:
567
+ encoder_hid_dim_type = "text_proj"
568
+ self.register_to_config(encoder_hid_dim_type=encoder_hid_dim_type)
569
+ logger.info("encoder_hid_dim_type defaults to 'text_proj' as `encoder_hid_dim` is defined.")
570
+
571
+ if encoder_hid_dim is None and encoder_hid_dim_type is not None:
572
+ raise ValueError(
573
+ f"`encoder_hid_dim` has to be defined when `encoder_hid_dim_type` is set to {encoder_hid_dim_type}."
574
+ )
575
+
576
+ if encoder_hid_dim_type == "text_proj":
577
+ self.encoder_hid_proj = nn.Linear(encoder_hid_dim, cross_attention_dim)
578
+ elif encoder_hid_dim_type == "text_image_proj":
579
+ # image_embed_dim DOESN'T have to be `cross_attention_dim`. To not clutter the __init__ too much
580
+ # they are set to `cross_attention_dim` here as this is exactly the required dimension for the currently only use
581
+ # case when `addition_embed_type == "text_image_proj"` (Kadinsky 2.1)`
582
+ self.encoder_hid_proj = TextImageProjection(
583
+ text_embed_dim=encoder_hid_dim,
584
+ image_embed_dim=cross_attention_dim,
585
+ cross_attention_dim=cross_attention_dim,
586
+ )
587
+ elif encoder_hid_dim_type == "image_proj":
588
+ # Kandinsky 2.2
589
+ self.encoder_hid_proj = ImageProjection(
590
+ image_embed_dim=encoder_hid_dim,
591
+ cross_attention_dim=cross_attention_dim,
592
+ )
593
+ elif encoder_hid_dim_type is not None:
594
+ raise ValueError(
595
+ f"encoder_hid_dim_type: {encoder_hid_dim_type} must be None, 'text_proj' or 'text_image_proj'."
596
+ )
597
+ else:
598
+ self.encoder_hid_proj = None
599
+
600
+ def _set_class_embedding(
601
+ self,
602
+ class_embed_type: Optional[str],
603
+ act_fn: str,
604
+ num_class_embeds: Optional[int],
605
+ projection_class_embeddings_input_dim: Optional[int],
606
+ time_embed_dim: int,
607
+ timestep_input_dim: int,
608
+ ):
609
+ if class_embed_type is None and num_class_embeds is not None:
610
+ self.class_embedding = nn.Embedding(num_class_embeds, time_embed_dim)
611
+ elif class_embed_type == "timestep":
612
+ self.class_embedding = TimestepEmbedding(timestep_input_dim, time_embed_dim, act_fn=act_fn)
613
+ elif class_embed_type == "identity":
614
+ self.class_embedding = nn.Identity(time_embed_dim, time_embed_dim)
615
+ elif class_embed_type == "projection":
616
+ if projection_class_embeddings_input_dim is None:
617
+ raise ValueError(
618
+ "`class_embed_type`: 'projection' requires `projection_class_embeddings_input_dim` be set"
619
+ )
620
+ # The projection `class_embed_type` is the same as the timestep `class_embed_type` except
621
+ # 1. the `class_labels` inputs are not first converted to sinusoidal embeddings
622
+ # 2. it projects from an arbitrary input dimension.
623
+ #
624
+ # Note that `TimestepEmbedding` is quite general, being mainly linear layers and activations.
625
+ # When used for embedding actual timesteps, the timesteps are first converted to sinusoidal embeddings.
626
+ # As a result, `TimestepEmbedding` can be passed arbitrary vectors.
627
+ self.class_embedding = TimestepEmbedding(projection_class_embeddings_input_dim, time_embed_dim)
628
+ elif class_embed_type == "simple_projection":
629
+ if projection_class_embeddings_input_dim is None:
630
+ raise ValueError(
631
+ "`class_embed_type`: 'simple_projection' requires `projection_class_embeddings_input_dim` be set"
632
+ )
633
+ self.class_embedding = nn.Linear(projection_class_embeddings_input_dim, time_embed_dim)
634
+ else:
635
+ self.class_embedding = None
636
+
637
+ def _set_add_embedding(
638
+ self,
639
+ addition_embed_type: str,
640
+ addition_embed_type_num_heads: int,
641
+ addition_time_embed_dim: Optional[int],
642
+ flip_sin_to_cos: bool,
643
+ freq_shift: float,
644
+ cross_attention_dim: Optional[int],
645
+ encoder_hid_dim: Optional[int],
646
+ projection_class_embeddings_input_dim: Optional[int],
647
+ time_embed_dim: int,
648
+ ):
649
+ if addition_embed_type == "text":
650
+ if encoder_hid_dim is not None:
651
+ text_time_embedding_from_dim = encoder_hid_dim
652
+ else:
653
+ text_time_embedding_from_dim = cross_attention_dim
654
+
655
+ self.add_embedding = TextTimeEmbedding(
656
+ text_time_embedding_from_dim, time_embed_dim, num_heads=addition_embed_type_num_heads
657
+ )
658
+ elif addition_embed_type == "text_image":
659
+ # text_embed_dim and image_embed_dim DON'T have to be `cross_attention_dim`. To not clutter the __init__ too much
660
+ # they are set to `cross_attention_dim` here as this is exactly the required dimension for the currently only use
661
+ # case when `addition_embed_type == "text_image"` (Kadinsky 2.1)`
662
+ self.add_embedding = TextImageTimeEmbedding(
663
+ text_embed_dim=cross_attention_dim, image_embed_dim=cross_attention_dim, time_embed_dim=time_embed_dim
664
+ )
665
+ elif addition_embed_type == "text_time":
666
+ self.add_time_proj = Timesteps(addition_time_embed_dim, flip_sin_to_cos, freq_shift)
667
+ self.add_embedding = TimestepEmbedding(projection_class_embeddings_input_dim, time_embed_dim)
668
+ elif addition_embed_type == "image":
669
+ # Kandinsky 2.2
670
+ self.add_embedding = ImageTimeEmbedding(image_embed_dim=encoder_hid_dim, time_embed_dim=time_embed_dim)
671
+ elif addition_embed_type == "image_hint":
672
+ # Kandinsky 2.2 ControlNet
673
+ self.add_embedding = ImageHintTimeEmbedding(image_embed_dim=encoder_hid_dim, time_embed_dim=time_embed_dim)
674
+ elif addition_embed_type is not None:
675
+ raise ValueError(f"addition_embed_type: {addition_embed_type} must be None, 'text' or 'text_image'.")
676
+
677
+ def _set_pos_net_if_use_gligen(self, attention_type: str, cross_attention_dim: int):
678
+ if attention_type in ["gated", "gated-text-image"]:
679
+ positive_len = 768
680
+ if isinstance(cross_attention_dim, int):
681
+ positive_len = cross_attention_dim
682
+ elif isinstance(cross_attention_dim, tuple) or isinstance(cross_attention_dim, list):
683
+ positive_len = cross_attention_dim[0]
684
+
685
+ feature_type = "text-only" if attention_type == "gated" else "text-image"
686
+ self.position_net = GLIGENTextBoundingboxProjection(
687
+ positive_len=positive_len, out_dim=cross_attention_dim, feature_type=feature_type
688
+ )
689
+
690
+ @property
691
+ def attn_processors(self) -> Dict[str, AttentionProcessor]:
692
+ r"""
693
+ Returns:
694
+ `dict` of attention processors: A dictionary containing all attention processors used in the model with
695
+ indexed by its weight name.
696
+ """
697
+ # set recursively
698
+ processors = {}
699
+
700
+ def fn_recursive_add_processors(name: str, module: torch.nn.Module, processors: Dict[str, AttentionProcessor]):
701
+ if hasattr(module, "get_processor"):
702
+ processors[f"{name}.processor"] = module.get_processor(return_deprecated_lora=True)
703
+
704
+ for sub_name, child in module.named_children():
705
+ fn_recursive_add_processors(f"{name}.{sub_name}", child, processors)
706
+
707
+ return processors
708
+
709
+ for name, module in self.named_children():
710
+ fn_recursive_add_processors(name, module, processors)
711
+
712
+ return processors
713
+
714
+ def set_attn_processor(self, processor: Union[AttentionProcessor, Dict[str, AttentionProcessor]]):
715
+ r"""
716
+ Sets the attention processor to use to compute attention.
717
+
718
+ Parameters:
719
+ processor (`dict` of `AttentionProcessor` or only `AttentionProcessor`):
720
+ The instantiated processor class or a dictionary of processor classes that will be set as the processor
721
+ for **all** `Attention` layers.
722
+
723
+ If `processor` is a dict, the key needs to define the path to the corresponding cross attention
724
+ processor. This is strongly recommended when setting trainable attention processors.
725
+
726
+ """
727
+ count = len(self.attn_processors.keys())
728
+
729
+ if isinstance(processor, dict) and len(processor) != count:
730
+ raise ValueError(
731
+ f"A dict of processors was passed, but the number of processors {len(processor)} does not match the"
732
+ f" number of attention layers: {count}. Please make sure to pass {count} processor classes."
733
+ )
734
+
735
+ def fn_recursive_attn_processor(name: str, module: torch.nn.Module, processor):
736
+ if hasattr(module, "set_processor"):
737
+ if not isinstance(processor, dict):
738
+ module.set_processor(processor)
739
+ else:
740
+ module.set_processor(processor.pop(f"{name}.processor"))
741
+
742
+ for sub_name, child in module.named_children():
743
+ fn_recursive_attn_processor(f"{name}.{sub_name}", child, processor)
744
+
745
+ for name, module in self.named_children():
746
+ fn_recursive_attn_processor(name, module, processor)
747
+
748
+ def set_default_attn_processor(self):
749
+ """
750
+ Disables custom attention processors and sets the default attention implementation.
751
+ """
752
+ if all(proc.__class__ in ADDED_KV_ATTENTION_PROCESSORS for proc in self.attn_processors.values()):
753
+ processor = AttnAddedKVProcessor()
754
+ elif all(proc.__class__ in CROSS_ATTENTION_PROCESSORS for proc in self.attn_processors.values()):
755
+ processor = AttnProcessor()
756
+ else:
757
+ raise ValueError(
758
+ f"Cannot call `set_default_attn_processor` when attention processors are of type {next(iter(self.attn_processors.values()))}"
759
+ )
760
+
761
+ self.set_attn_processor(processor)
762
+
763
+ def set_attention_slice(self, slice_size: Union[str, int, List[int]] = "auto"):
764
+ r"""
765
+ Enable sliced attention computation.
766
+
767
+ When this option is enabled, the attention module splits the input tensor in slices to compute attention in
768
+ several steps. This is useful for saving some memory in exchange for a small decrease in speed.
769
+
770
+ Args:
771
+ slice_size (`str` or `int` or `list(int)`, *optional*, defaults to `"auto"`):
772
+ When `"auto"`, input to the attention heads is halved, so attention is computed in two steps. If
773
+ `"max"`, maximum amount of memory is saved by running only one slice at a time. If a number is
774
+ provided, uses as many slices as `attention_head_dim // slice_size`. In this case, `attention_head_dim`
775
+ must be a multiple of `slice_size`.
776
+ """
777
+ sliceable_head_dims = []
778
+
779
+ def fn_recursive_retrieve_sliceable_dims(module: torch.nn.Module):
780
+ if hasattr(module, "set_attention_slice"):
781
+ sliceable_head_dims.append(module.sliceable_head_dim)
782
+
783
+ for child in module.children():
784
+ fn_recursive_retrieve_sliceable_dims(child)
785
+
786
+ # retrieve number of attention layers
787
+ for module in self.children():
788
+ fn_recursive_retrieve_sliceable_dims(module)
789
+
790
+ num_sliceable_layers = len(sliceable_head_dims)
791
+
792
+ if slice_size == "auto":
793
+ # half the attention head size is usually a good trade-off between
794
+ # speed and memory
795
+ slice_size = [dim // 2 for dim in sliceable_head_dims]
796
+ elif slice_size == "max":
797
+ # make smallest slice possible
798
+ slice_size = num_sliceable_layers * [1]
799
+
800
+ slice_size = num_sliceable_layers * [slice_size] if not isinstance(slice_size, list) else slice_size
801
+
802
+ if len(slice_size) != len(sliceable_head_dims):
803
+ raise ValueError(
804
+ f"You have provided {len(slice_size)}, but {self.config} has {len(sliceable_head_dims)} different"
805
+ f" attention layers. Make sure to match `len(slice_size)` to be {len(sliceable_head_dims)}."
806
+ )
807
+
808
+ for i in range(len(slice_size)):
809
+ size = slice_size[i]
810
+ dim = sliceable_head_dims[i]
811
+ if size is not None and size > dim:
812
+ raise ValueError(f"size {size} has to be smaller or equal to {dim}.")
813
+
814
+ # Recursively walk through all the children.
815
+ # Any children which exposes the set_attention_slice method
816
+ # gets the message
817
+ def fn_recursive_set_attention_slice(module: torch.nn.Module, slice_size: List[int]):
818
+ if hasattr(module, "set_attention_slice"):
819
+ module.set_attention_slice(slice_size.pop())
820
+
821
+ for child in module.children():
822
+ fn_recursive_set_attention_slice(child, slice_size)
823
+
824
+ reversed_slice_size = list(reversed(slice_size))
825
+ for module in self.children():
826
+ fn_recursive_set_attention_slice(module, reversed_slice_size)
827
+
828
+ def _set_gradient_checkpointing(self, module, value=False):
829
+ if hasattr(module, "gradient_checkpointing"):
830
+ module.gradient_checkpointing = value
831
+
832
+ def enable_freeu(self, s1: float, s2: float, b1: float, b2: float):
833
+ r"""Enables the FreeU mechanism from https://arxiv.org/abs/2309.11497.
834
+
835
+ The suffixes after the scaling factors represent the stage blocks where they are being applied.
836
+
837
+ Please refer to the [official repository](https://github.com/ChenyangSi/FreeU) for combinations of values that
838
+ are known to work well for different pipelines such as Stable Diffusion v1, v2, and Stable Diffusion XL.
839
+
840
+ Args:
841
+ s1 (`float`):
842
+ Scaling factor for stage 1 to attenuate the contributions of the skip features. This is done to
843
+ mitigate the "oversmoothing effect" in the enhanced denoising process.
844
+ s2 (`float`):
845
+ Scaling factor for stage 2 to attenuate the contributions of the skip features. This is done to
846
+ mitigate the "oversmoothing effect" in the enhanced denoising process.
847
+ b1 (`float`): Scaling factor for stage 1 to amplify the contributions of backbone features.
848
+ b2 (`float`): Scaling factor for stage 2 to amplify the contributions of backbone features.
849
+ """
850
+ for i, upsample_block in enumerate(self.up_blocks):
851
+ setattr(upsample_block, "s1", s1)
852
+ setattr(upsample_block, "s2", s2)
853
+ setattr(upsample_block, "b1", b1)
854
+ setattr(upsample_block, "b2", b2)
855
+
856
+ def disable_freeu(self):
857
+ """Disables the FreeU mechanism."""
858
+ freeu_keys = {"s1", "s2", "b1", "b2"}
859
+ for i, upsample_block in enumerate(self.up_blocks):
860
+ for k in freeu_keys:
861
+ if hasattr(upsample_block, k) or getattr(upsample_block, k, None) is not None:
862
+ setattr(upsample_block, k, None)
863
+
864
+ def fuse_qkv_projections(self):
865
+ """
866
+ Enables fused QKV projections. For self-attention modules, all projection matrices (i.e., query,
867
+ key, value) are fused. For cross-attention modules, key and value projection matrices are fused.
868
+
869
+ <Tip warning={true}>
870
+
871
+ This API is 🧪 experimental.
872
+
873
+ </Tip>
874
+ """
875
+ self.original_attn_processors = None
876
+
877
+ for _, attn_processor in self.attn_processors.items():
878
+ if "Added" in str(attn_processor.__class__.__name__):
879
+ raise ValueError("`fuse_qkv_projections()` is not supported for models having added KV projections.")
880
+
881
+ self.original_attn_processors = self.attn_processors
882
+
883
+ for module in self.modules():
884
+ if isinstance(module, Attention):
885
+ module.fuse_projections(fuse=True)
886
+
887
+ def unfuse_qkv_projections(self):
888
+ """Disables the fused QKV projection if enabled.
889
+
890
+ <Tip warning={true}>
891
+
892
+ This API is 🧪 experimental.
893
+
894
+ </Tip>
895
+
896
+ """
897
+ if self.original_attn_processors is not None:
898
+ self.set_attn_processor(self.original_attn_processors)
899
+
900
+ def unload_lora(self):
901
+ """Unloads LoRA weights."""
902
+ deprecate(
903
+ "unload_lora",
904
+ "0.28.0",
905
+ "Calling `unload_lora()` is deprecated and will be removed in a future version. Please install `peft` and then call `disable_adapters().",
906
+ )
907
+ for module in self.modules():
908
+ if hasattr(module, "set_lora_layer"):
909
+ module.set_lora_layer(None)
910
+
911
+ def get_time_embed(
912
+ self, sample: torch.Tensor, timestep: Union[torch.Tensor, float, int]
913
+ ) -> Optional[torch.Tensor]:
914
+ timesteps = timestep
915
+ if not torch.is_tensor(timesteps):
916
+ # TODO: this requires sync between CPU and GPU. So try to pass timesteps as tensors if you can
917
+ # This would be a good case for the `match` statement (Python 3.10+)
918
+ is_mps = sample.device.type == "mps"
919
+ if isinstance(timestep, float):
920
+ dtype = torch.float32 if is_mps else torch.float64
921
+ else:
922
+ dtype = torch.int32 if is_mps else torch.int64
923
+ timesteps = torch.tensor([timesteps], dtype=dtype, device=sample.device)
924
+ elif len(timesteps.shape) == 0:
925
+ timesteps = timesteps[None].to(sample.device)
926
+
927
+ # broadcast to batch dimension in a way that's compatible with ONNX/Core ML
928
+ timesteps = timesteps.expand(sample.shape[0])
929
+
930
+ t_emb = self.time_proj(timesteps)
931
+ # `Timesteps` does not contain any weights and will always return f32 tensors
932
+ # but time_embedding might actually be running in fp16. so we need to cast here.
933
+ # there might be better ways to encapsulate this.
934
+ t_emb = t_emb.to(dtype=sample.dtype)
935
+ return t_emb
936
+
937
+ def get_class_embed(self, sample: torch.Tensor, class_labels: Optional[torch.Tensor]) -> Optional[torch.Tensor]:
938
+ class_emb = None
939
+ if self.class_embedding is not None:
940
+ if class_labels is None:
941
+ raise ValueError("class_labels should be provided when num_class_embeds > 0")
942
+
943
+ if self.config.class_embed_type == "timestep":
944
+ class_labels = self.time_proj(class_labels)
945
+
946
+ # `Timesteps` does not contain any weights and will always return f32 tensors
947
+ # there might be better ways to encapsulate this.
948
+ class_labels = class_labels.to(dtype=sample.dtype)
949
+
950
+ class_emb = self.class_embedding(class_labels).to(dtype=sample.dtype)
951
+ return class_emb
952
+
953
+ def get_aug_embed(
954
+ self, emb: torch.Tensor, encoder_hidden_states: torch.Tensor, added_cond_kwargs: Dict[str, Any]
955
+ ) -> Optional[torch.Tensor]:
956
+ aug_emb = None
957
+ if self.config.addition_embed_type == "text":
958
+ aug_emb = self.add_embedding(encoder_hidden_states)
959
+ elif self.config.addition_embed_type == "text_image":
960
+ # Kandinsky 2.1 - style
961
+ if "image_embeds" not in added_cond_kwargs:
962
+ raise ValueError(
963
+ f"{self.__class__} has the config param `addition_embed_type` set to 'text_image' which requires the keyword argument `image_embeds` to be passed in `added_cond_kwargs`"
964
+ )
965
+
966
+ image_embs = added_cond_kwargs.get("image_embeds")
967
+ text_embs = added_cond_kwargs.get("text_embeds", encoder_hidden_states)
968
+ aug_emb = self.add_embedding(text_embs, image_embs)
969
+ elif self.config.addition_embed_type == "text_time":
970
+ # SDXL - style
971
+ if "text_embeds" not in added_cond_kwargs:
972
+ raise ValueError(
973
+ f"{self.__class__} has the config param `addition_embed_type` set to 'text_time' which requires the keyword argument `text_embeds` to be passed in `added_cond_kwargs`"
974
+ )
975
+ text_embeds = added_cond_kwargs.get("text_embeds")
976
+ if "time_ids" not in added_cond_kwargs:
977
+ raise ValueError(
978
+ f"{self.__class__} has the config param `addition_embed_type` set to 'text_time' which requires the keyword argument `time_ids` to be passed in `added_cond_kwargs`"
979
+ )
980
+ time_ids = added_cond_kwargs.get("time_ids")
981
+ time_embeds = self.add_time_proj(time_ids.flatten())
982
+ time_embeds = time_embeds.reshape((text_embeds.shape[0], -1))
983
+ add_embeds = torch.concat([text_embeds, time_embeds], dim=-1)
984
+ add_embeds = add_embeds.to(emb.dtype)
985
+ aug_emb = self.add_embedding(add_embeds)
986
+ elif self.config.addition_embed_type == "image":
987
+ # Kandinsky 2.2 - style
988
+ if "image_embeds" not in added_cond_kwargs:
989
+ raise ValueError(
990
+ f"{self.__class__} has the config param `addition_embed_type` set to 'image' which requires the keyword argument `image_embeds` to be passed in `added_cond_kwargs`"
991
+ )
992
+ image_embs = added_cond_kwargs.get("image_embeds")
993
+ aug_emb = self.add_embedding(image_embs)
994
+ elif self.config.addition_embed_type == "image_hint":
995
+ # Kandinsky 2.2 - style
996
+ if "image_embeds" not in added_cond_kwargs or "hint" not in added_cond_kwargs:
997
+ raise ValueError(
998
+ f"{self.__class__} has the config param `addition_embed_type` set to 'image_hint' which requires the keyword arguments `image_embeds` and `hint` to be passed in `added_cond_kwargs`"
999
+ )
1000
+ image_embs = added_cond_kwargs.get("image_embeds")
1001
+ hint = added_cond_kwargs.get("hint")
1002
+ aug_emb = self.add_embedding(image_embs, hint)
1003
+ return aug_emb
1004
+
1005
+ def process_encoder_hidden_states(
1006
+ self, encoder_hidden_states: torch.Tensor, added_cond_kwargs: Dict[str, Any]
1007
+ ) -> torch.Tensor:
1008
+ if self.encoder_hid_proj is not None and self.config.encoder_hid_dim_type == "text_proj":
1009
+ encoder_hidden_states = self.encoder_hid_proj(encoder_hidden_states)
1010
+ elif self.encoder_hid_proj is not None and self.config.encoder_hid_dim_type == "text_image_proj":
1011
+ # Kadinsky 2.1 - style
1012
+ if "image_embeds" not in added_cond_kwargs:
1013
+ raise ValueError(
1014
+ f"{self.__class__} has the config param `encoder_hid_dim_type` set to 'text_image_proj' which requires the keyword argument `image_embeds` to be passed in `added_conditions`"
1015
+ )
1016
+
1017
+ image_embeds = added_cond_kwargs.get("image_embeds")
1018
+ encoder_hidden_states = self.encoder_hid_proj(encoder_hidden_states, image_embeds)
1019
+ elif self.encoder_hid_proj is not None and self.config.encoder_hid_dim_type == "image_proj":
1020
+ # Kandinsky 2.2 - style
1021
+ if "image_embeds" not in added_cond_kwargs:
1022
+ raise ValueError(
1023
+ f"{self.__class__} has the config param `encoder_hid_dim_type` set to 'image_proj' which requires the keyword argument `image_embeds` to be passed in `added_conditions`"
1024
+ )
1025
+ image_embeds = added_cond_kwargs.get("image_embeds")
1026
+ encoder_hidden_states = self.encoder_hid_proj(image_embeds)
1027
+ elif self.encoder_hid_proj is not None and self.config.encoder_hid_dim_type == "ip_image_proj":
1028
+ if "image_embeds" not in added_cond_kwargs:
1029
+ raise ValueError(
1030
+ f"{self.__class__} has the config param `encoder_hid_dim_type` set to 'ip_image_proj' which requires the keyword argument `image_embeds` to be passed in `added_conditions`"
1031
+ )
1032
+ image_embeds = added_cond_kwargs.get("image_embeds")
1033
+ image_embeds = self.encoder_hid_proj(image_embeds)
1034
+ encoder_hidden_states = (encoder_hidden_states, image_embeds)
1035
+ return encoder_hidden_states
1036
+
1037
+ def forward(
1038
+ self,
1039
+ sample: torch.FloatTensor,
1040
+ timestep: Union[torch.Tensor, float, int],
1041
+ encoder_hidden_states: torch.Tensor,
1042
+ class_labels: Optional[torch.Tensor] = None,
1043
+ timestep_cond: Optional[torch.Tensor] = None,
1044
+ attention_mask: Optional[torch.Tensor] = None,
1045
+ cross_attention_kwargs: Optional[Dict[str, Any]] = None,
1046
+ added_cond_kwargs: Optional[Dict[str, torch.Tensor]] = None,
1047
+ down_block_additional_residuals: Optional[Tuple[torch.Tensor]] = None,
1048
+ mid_block_additional_residual: Optional[torch.Tensor] = None,
1049
+ down_intrablock_additional_residuals: Optional[Tuple[torch.Tensor]] = None,
1050
+ encoder_attention_mask: Optional[torch.Tensor] = None,
1051
+ return_dict: bool = True,
1052
+ audio_context: Optional[torch.Tensor] = None,
1053
+ ) -> Union[UNet2DConditionOutput, Tuple]:
1054
+ r"""
1055
+ The [`UNet2DConditionModel`] forward method.
1056
+
1057
+ Args:
1058
+ sample (`torch.FloatTensor`):
1059
+ The noisy input tensor with the following shape `(batch, channel, height, width)`.
1060
+ timestep (`torch.FloatTensor` or `float` or `int`): The number of timesteps to denoise an input.
1061
+ encoder_hidden_states (`torch.FloatTensor`):
1062
+ The encoder hidden states with shape `(batch, sequence_length, feature_dim)`.
1063
+ class_labels (`torch.Tensor`, *optional*, defaults to `None`):
1064
+ Optional class labels for conditioning. Their embeddings will be summed with the timestep embeddings.
1065
+ timestep_cond: (`torch.Tensor`, *optional*, defaults to `None`):
1066
+ Conditional embeddings for timestep. If provided, the embeddings will be summed with the samples passed
1067
+ through the `self.time_embedding` layer to obtain the timestep embeddings.
1068
+ attention_mask (`torch.Tensor`, *optional*, defaults to `None`):
1069
+ An attention mask of shape `(batch, key_tokens)` is applied to `encoder_hidden_states`. If `1` the mask
1070
+ is kept, otherwise if `0` it is discarded. Mask will be converted into a bias, which adds large
1071
+ negative values to the attention scores corresponding to "discard" tokens.
1072
+ cross_attention_kwargs (`dict`, *optional*):
1073
+ A kwargs dictionary that if specified is passed along to the `AttentionProcessor` as defined under
1074
+ `self.processor` in
1075
+ [diffusers.models.attention_processor](https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/attention_processor.py).
1076
+ added_cond_kwargs: (`dict`, *optional*):
1077
+ A kwargs dictionary containing additional embeddings that if specified are added to the embeddings that
1078
+ are passed along to the UNet blocks.
1079
+ down_block_additional_residuals: (`tuple` of `torch.Tensor`, *optional*):
1080
+ A tuple of tensors that if specified are added to the residuals of down unet blocks.
1081
+ mid_block_additional_residual: (`torch.Tensor`, *optional*):
1082
+ A tensor that if specified is added to the residual of the middle unet block.
1083
+ down_intrablock_additional_residuals (`tuple` of `torch.Tensor`, *optional*):
1084
+ additional residuals to be added within UNet down blocks, for example from T2I-Adapter side model(s)
1085
+ encoder_attention_mask (`torch.Tensor`):
1086
+ A cross-attention mask of shape `(batch, sequence_length)` is applied to `encoder_hidden_states`. If
1087
+ `True` the mask is kept, otherwise if `False` it is discarded. Mask will be converted into a bias,
1088
+ which adds large negative values to the attention scores corresponding to "discard" tokens.
1089
+ return_dict (`bool`, *optional*, defaults to `True`):
1090
+ Whether or not to return a [`~models.unets.unet_2d_condition.UNet2DConditionOutput`] instead of a plain
1091
+ tuple.
1092
+
1093
+ Returns:
1094
+ [`~models.unets.unet_2d_condition.UNet2DConditionOutput`] or `tuple`:
1095
+ If `return_dict` is True, an [`~models.unets.unet_2d_condition.UNet2DConditionOutput`] is returned, otherwise
1096
+ a `tuple` is returned where the first element is the sample tensor.
1097
+ """
1098
+ # By default samples have to be AT least a multiple of the overall upsampling factor.
1099
+ # The overall upsampling factor is equal to 2 ** (# num of upsampling layers).
1100
+ # However, the upsampling interpolation output size can be forced to fit any upsampling size
1101
+ # on the fly if necessary.
1102
+ default_overall_up_factor = 2**self.num_upsamplers
1103
+
1104
+ # upsample size should be forwarded when sample is not a multiple of `default_overall_up_factor`
1105
+ forward_upsample_size = False
1106
+ upsample_size = None
1107
+
1108
+ for dim in sample.shape[-2:]:
1109
+ if dim % default_overall_up_factor != 0:
1110
+ # Forward upsample size to force interpolation output size.
1111
+ forward_upsample_size = True
1112
+ break
1113
+
1114
+ # ensure attention_mask is a bias, and give it a singleton query_tokens dimension
1115
+ # expects mask of shape:
1116
+ # [batch, key_tokens]
1117
+ # adds singleton query_tokens dimension:
1118
+ # [batch, 1, key_tokens]
1119
+ # this helps to broadcast it as a bias over attention scores, which will be in one of the following shapes:
1120
+ # [batch, heads, query_tokens, key_tokens] (e.g. torch sdp attn)
1121
+ # [batch * heads, query_tokens, key_tokens] (e.g. xformers or classic attn)
1122
+ if attention_mask is not None:
1123
+ # assume that mask is expressed as:
1124
+ # (1 = keep, 0 = discard)
1125
+ # convert mask into a bias that can be added to attention scores:
1126
+ # (keep = +0, discard = -10000.0)
1127
+ attention_mask = (1 - attention_mask.to(sample.dtype)) * -10000.0
1128
+ attention_mask = attention_mask.unsqueeze(1)
1129
+
1130
+ # convert encoder_attention_mask to a bias the same way we do for attention_mask
1131
+ if encoder_attention_mask is not None:
1132
+ encoder_attention_mask = (1 - encoder_attention_mask.to(sample.dtype)) * -10000.0
1133
+ encoder_attention_mask = encoder_attention_mask.unsqueeze(1)
1134
+
1135
+ # 0. center input if necessary
1136
+ if self.config.center_input_sample:
1137
+ sample = 2 * sample - 1.0
1138
+
1139
+ # 1. time
1140
+ t_emb = self.get_time_embed(sample=sample, timestep=timestep)
1141
+ emb = self.time_embedding(t_emb, timestep_cond)
1142
+ aug_emb = None
1143
+
1144
+ class_emb = self.get_class_embed(sample=sample, class_labels=class_labels)
1145
+ if class_emb is not None:
1146
+ if self.config.class_embeddings_concat:
1147
+ emb = torch.cat([emb, class_emb], dim=-1)
1148
+ else:
1149
+ emb = emb + class_emb
1150
+
1151
+ aug_emb = self.get_aug_embed(
1152
+ emb=emb, encoder_hidden_states=encoder_hidden_states, added_cond_kwargs=added_cond_kwargs
1153
+ )
1154
+ if self.config.addition_embed_type == "image_hint":
1155
+ aug_emb, hint = aug_emb
1156
+ sample = torch.cat([sample, hint], dim=1)
1157
+
1158
+ emb = emb + aug_emb if aug_emb is not None else emb
1159
+
1160
+ if self.time_embed_act is not None:
1161
+ emb = self.time_embed_act(emb)
1162
+
1163
+ encoder_hidden_states = self.process_encoder_hidden_states(
1164
+ encoder_hidden_states=encoder_hidden_states, added_cond_kwargs=added_cond_kwargs
1165
+ )
1166
+
1167
+ # 2. pre-process
1168
+ sample = self.conv_in(sample)
1169
+
1170
+ # 2.5 GLIGEN position net
1171
+ if cross_attention_kwargs is not None and cross_attention_kwargs.get("gligen", None) is not None:
1172
+ cross_attention_kwargs = cross_attention_kwargs.copy()
1173
+ gligen_args = cross_attention_kwargs.pop("gligen")
1174
+ cross_attention_kwargs["gligen"] = {"objs": self.position_net(**gligen_args)}
1175
+
1176
+ # 3. down
1177
+ # we're popping the `scale` instead of getting it because otherwise `scale` will be propagated
1178
+ # to the internal blocks and will raise deprecation warnings. this will be confusing for our users.
1179
+ if cross_attention_kwargs is not None:
1180
+ cross_attention_kwargs = cross_attention_kwargs.copy()
1181
+ lora_scale = cross_attention_kwargs.pop("scale", 1.0)
1182
+ else:
1183
+ lora_scale = 1.0
1184
+
1185
+ if USE_PEFT_BACKEND:
1186
+ # weight the lora layers by setting `lora_scale` for each PEFT layer
1187
+ scale_lora_layers(self, lora_scale)
1188
+
1189
+ is_controlnet = mid_block_additional_residual is not None and down_block_additional_residuals is not None
1190
+ # using new arg down_intrablock_additional_residuals for T2I-Adapters, to distinguish from controlnets
1191
+ is_adapter = down_intrablock_additional_residuals is not None
1192
+ # maintain backward compatibility for legacy usage, where
1193
+ # T2I-Adapter and ControlNet both use down_block_additional_residuals arg
1194
+ # but can only use one or the other
1195
+ if not is_adapter and mid_block_additional_residual is None and down_block_additional_residuals is not None:
1196
+ deprecate(
1197
+ "T2I should not use down_block_additional_residuals",
1198
+ "1.3.0",
1199
+ "Passing intrablock residual connections with `down_block_additional_residuals` is deprecated \
1200
+ and will be removed in diffusers 1.3.0. `down_block_additional_residuals` should only be used \
1201
+ for ControlNet. Please make sure use `down_intrablock_additional_residuals` instead. ",
1202
+ standard_warn=False,
1203
+ )
1204
+ down_intrablock_additional_residuals = down_block_additional_residuals
1205
+ is_adapter = True
1206
+
1207
+ down_block_res_samples = (sample,)
1208
+ for downsample_block in self.down_blocks:
1209
+ if hasattr(downsample_block, "has_cross_attention") and downsample_block.has_cross_attention:
1210
+ # For t2i-adapter CrossAttnDownBlock2D
1211
+ additional_residuals = {}
1212
+ if is_adapter and len(down_intrablock_additional_residuals) > 0:
1213
+ additional_residuals["additional_residuals"] = down_intrablock_additional_residuals.pop(0)
1214
+
1215
+ sample, res_samples = downsample_block(
1216
+ hidden_states=sample,
1217
+ temb=emb,
1218
+ encoder_hidden_states=encoder_hidden_states,
1219
+ attention_mask=attention_mask,
1220
+ cross_attention_kwargs=cross_attention_kwargs,
1221
+ encoder_attention_mask=encoder_attention_mask,
1222
+ audio_context=audio_context,
1223
+ **additional_residuals,
1224
+ )
1225
+ else:
1226
+ sample, res_samples = downsample_block(hidden_states=sample, temb=emb)
1227
+ if is_adapter and len(down_intrablock_additional_residuals) > 0:
1228
+ sample += down_intrablock_additional_residuals.pop(0)
1229
+
1230
+ down_block_res_samples += res_samples
1231
+
1232
+ if is_controlnet:
1233
+ new_down_block_res_samples = ()
1234
+
1235
+ for down_block_res_sample, down_block_additional_residual in zip(
1236
+ down_block_res_samples, down_block_additional_residuals
1237
+ ):
1238
+ down_block_res_sample = down_block_res_sample + down_block_additional_residual
1239
+ new_down_block_res_samples = new_down_block_res_samples + (down_block_res_sample,)
1240
+
1241
+ down_block_res_samples = new_down_block_res_samples
1242
+
1243
+ # 4. mid
1244
+ if self.mid_block is not None:
1245
+ if hasattr(self.mid_block, "has_cross_attention") and self.mid_block.has_cross_attention:
1246
+ sample = self.mid_block(
1247
+ sample,
1248
+ emb,
1249
+ encoder_hidden_states=encoder_hidden_states,
1250
+ attention_mask=attention_mask,
1251
+ cross_attention_kwargs=cross_attention_kwargs,
1252
+ encoder_attention_mask=encoder_attention_mask,
1253
+ audio_context=audio_context,
1254
+ )
1255
+ else:
1256
+ sample = self.mid_block(sample, emb)
1257
+
1258
+ # To support T2I-Adapter-XL
1259
+ if (
1260
+ is_adapter
1261
+ and len(down_intrablock_additional_residuals) > 0
1262
+ and sample.shape == down_intrablock_additional_residuals[0].shape
1263
+ ):
1264
+ sample += down_intrablock_additional_residuals.pop(0)
1265
+
1266
+ if is_controlnet:
1267
+ sample = sample + mid_block_additional_residual
1268
+
1269
+ # 5. up
1270
+ for i, upsample_block in enumerate(self.up_blocks):
1271
+ is_final_block = i == len(self.up_blocks) - 1
1272
+
1273
+ res_samples = down_block_res_samples[-len(upsample_block.resnets) :]
1274
+ down_block_res_samples = down_block_res_samples[: -len(upsample_block.resnets)]
1275
+
1276
+ # if we have not reached the final block and need to forward the
1277
+ # upsample size, we do it here
1278
+ if not is_final_block and forward_upsample_size:
1279
+ upsample_size = down_block_res_samples[-1].shape[2:]
1280
+
1281
+ if hasattr(upsample_block, "has_cross_attention") and upsample_block.has_cross_attention:
1282
+ sample = upsample_block(
1283
+ hidden_states=sample,
1284
+ temb=emb,
1285
+ res_hidden_states_tuple=res_samples,
1286
+ encoder_hidden_states=encoder_hidden_states,
1287
+ cross_attention_kwargs=cross_attention_kwargs,
1288
+ upsample_size=upsample_size,
1289
+ attention_mask=attention_mask,
1290
+ encoder_attention_mask=encoder_attention_mask,
1291
+ audio_context=audio_context,
1292
+ )
1293
+ else:
1294
+ sample = upsample_block(
1295
+ hidden_states=sample,
1296
+ temb=emb,
1297
+ res_hidden_states_tuple=res_samples,
1298
+ upsample_size=upsample_size,
1299
+ )
1300
+
1301
+ # 6. post-process
1302
+ if self.conv_norm_out:
1303
+ sample = self.conv_norm_out(sample)
1304
+ sample = self.conv_act(sample)
1305
+ sample = self.conv_out(sample)
1306
+
1307
+ if USE_PEFT_BACKEND:
1308
+ # remove `lora_scale` from each PEFT layer
1309
+ unscale_lora_layers(self, lora_scale)
1310
+
1311
+ if not return_dict:
1312
+ return (sample,)
1313
+
1314
+ return UNet2DConditionOutput(sample=sample)
unet_2d_blocks_custom.py ADDED
The diff for this file is too large to render. See raw diff