Upload code/src/diffusion_model/Processor.py with huggingface_hub
Browse files
code/src/diffusion_model/Processor.py
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import math
|
| 3 |
+
import torch.nn.functional as F
|
| 4 |
+
DIFFUSION_LAYERS = [
|
| 5 |
+
'down_blocks[0].attentions[0].transformer_blocks[0].attn1', # 0
|
| 6 |
+
'down_blocks[0].attentions[0].transformer_blocks[0].attn2', # 1
|
| 7 |
+
'down_blocks[0].attentions[1].transformer_blocks[0].attn1', # 2
|
| 8 |
+
'down_blocks[0].attentions[1].transformer_blocks[0].attn2', # 3
|
| 9 |
+
'down_blocks[1].attentions[0].transformer_blocks[0].attn1', # 4
|
| 10 |
+
'down_blocks[1].attentions[0].transformer_blocks[0].attn2', # 5
|
| 11 |
+
'down_blocks[1].attentions[1].transformer_blocks[0].attn1', # 6
|
| 12 |
+
'down_blocks[1].attentions[1].transformer_blocks[0].attn2', # 7
|
| 13 |
+
'down_blocks[2].attentions[0].transformer_blocks[0].attn1', # 8
|
| 14 |
+
'down_blocks[2].attentions[0].transformer_blocks[0].attn2', # 9
|
| 15 |
+
'down_blocks[2].attentions[1].transformer_blocks[0].attn1', # 10
|
| 16 |
+
'down_blocks[2].attentions[1].transformer_blocks[0].attn2', # 11
|
| 17 |
+
|
| 18 |
+
'mid_block.attentions[0].transformer_blocks[0].attn1',
|
| 19 |
+
'mid_block.attentions[0].transformer_blocks[0].attn2',
|
| 20 |
+
|
| 21 |
+
'up_blocks[1].attentions[0].transformer_blocks[0].attn1', # -18
|
| 22 |
+
"up_blocks[1].attentions[0].transformer_blocks[0].attn2", # -17
|
| 23 |
+
'up_blocks[1].attentions[1].transformer_blocks[0].attn1', # -16
|
| 24 |
+
"up_blocks[1].attentions[1].transformer_blocks[0].attn2", # -15
|
| 25 |
+
'up_blocks[1].attentions[2].transformer_blocks[0].attn1', # -14
|
| 26 |
+
"up_blocks[1].attentions[2].transformer_blocks[0].attn2", # -13
|
| 27 |
+
'up_blocks[2].attentions[0].transformer_blocks[0].attn1', # -12
|
| 28 |
+
"up_blocks[2].attentions[0].transformer_blocks[0].attn2", # -11
|
| 29 |
+
'up_blocks[2].attentions[1].transformer_blocks[0].attn1', # -10
|
| 30 |
+
"up_blocks[2].attentions[1].transformer_blocks[0].attn2", # -9
|
| 31 |
+
'up_blocks[2].attentions[2].transformer_blocks[0].attn1', # -8
|
| 32 |
+
'up_blocks[2].attentions[2].transformer_blocks[0].attn2', # -7
|
| 33 |
+
"up_blocks[3].attentions[0].transformer_blocks[0].attn1", # -6
|
| 34 |
+
'up_blocks[3].attentions[0].transformer_blocks[0].attn2', # -5
|
| 35 |
+
"up_blocks[3].attentions[1].transformer_blocks[0].attn1", # -4
|
| 36 |
+
'up_blocks[3].attentions[1].transformer_blocks[0].attn2', # -3
|
| 37 |
+
"up_blocks[3].attentions[2].transformer_blocks[0].attn1", # -2
|
| 38 |
+
'up_blocks[3].attentions[2].transformer_blocks[0].attn2', # -1
|
| 39 |
+
]
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
class AttnProcessorForCallBack:
|
| 43 |
+
def __init__(self, model, layer):
|
| 44 |
+
self.model = model
|
| 45 |
+
self.layer = layer
|
| 46 |
+
|
| 47 |
+
def __call__(
|
| 48 |
+
self,
|
| 49 |
+
attn,
|
| 50 |
+
hidden_states: torch.Tensor,
|
| 51 |
+
encoder_hidden_states=None,
|
| 52 |
+
attention_mask=None,
|
| 53 |
+
temb=None,
|
| 54 |
+
*args,
|
| 55 |
+
**kwargs,
|
| 56 |
+
) -> torch.Tensor:
|
| 57 |
+
residual = hidden_states
|
| 58 |
+
|
| 59 |
+
if attn.spatial_norm is not None:
|
| 60 |
+
hidden_states = attn.spatial_norm(hidden_states, temb)
|
| 61 |
+
input_ndim = hidden_states.ndim
|
| 62 |
+
|
| 63 |
+
if input_ndim == 4:
|
| 64 |
+
batch_size, channel, height, width = hidden_states.shape
|
| 65 |
+
hidden_states = hidden_states.view(batch_size, channel, height * width).transpose(1, 2)
|
| 66 |
+
|
| 67 |
+
batch_size, sequence_length, _ = (
|
| 68 |
+
hidden_states.shape if encoder_hidden_states is None else encoder_hidden_states.shape
|
| 69 |
+
)
|
| 70 |
+
attention_mask = attn.prepare_attention_mask(attention_mask, sequence_length, batch_size)
|
| 71 |
+
|
| 72 |
+
if attn.group_norm is not None:
|
| 73 |
+
hidden_states = attn.group_norm(hidden_states.transpose(1, 2)).transpose(1, 2)
|
| 74 |
+
query = attn.to_q(hidden_states)
|
| 75 |
+
if encoder_hidden_states is None:
|
| 76 |
+
encoder_hidden_states = hidden_states
|
| 77 |
+
elif attn.norm_cross:
|
| 78 |
+
encoder_hidden_states = attn.norm_encoder_hidden_states(encoder_hidden_states)
|
| 79 |
+
|
| 80 |
+
key = attn.to_k(encoder_hidden_states)
|
| 81 |
+
value = attn.to_v(encoder_hidden_states)
|
| 82 |
+
|
| 83 |
+
# ! add code
|
| 84 |
+
h=w=int(math.sqrt(query.shape[1]))
|
| 85 |
+
low_res_query=query.clone().transpose(-2,-1).view(batch_size, -1, h,w)
|
| 86 |
+
low_res_key=key.clone().transpose(-2,-1).view(batch_size, -1,h,w)
|
| 87 |
+
low_res_query_ds = F.interpolate(low_res_query, size=(35, 35), mode='bilinear', align_corners=False)
|
| 88 |
+
low_res_key_ds = F.interpolate(low_res_key, size=(35, 35), mode='bilinear', align_corners=False)
|
| 89 |
+
low_res_query_ds=low_res_query_ds.flatten(start_dim=-2).transpose(-2,-1)
|
| 90 |
+
low_res_key_ds=low_res_key_ds.flatten(start_dim=-2).transpose(-2,-1)
|
| 91 |
+
low_res_query_ds = attn.head_to_batch_dim(low_res_query_ds)
|
| 92 |
+
low_res_key_ds = attn.head_to_batch_dim(low_res_key_ds)
|
| 93 |
+
low_res_attention_probs = attn.get_attention_scores(low_res_query_ds, low_res_key_ds, attention_mask)
|
| 94 |
+
# ! add code
|
| 95 |
+
query = attn.head_to_batch_dim(query)
|
| 96 |
+
key = attn.head_to_batch_dim(key)
|
| 97 |
+
value = attn.head_to_batch_dim(value)
|
| 98 |
+
|
| 99 |
+
attention_probs = attn.get_attention_scores(query, key, attention_mask)
|
| 100 |
+
hidden_states = torch.bmm(attention_probs, value)
|
| 101 |
+
hidden_states = attn.batch_to_head_dim(hidden_states)
|
| 102 |
+
|
| 103 |
+
# linear proj
|
| 104 |
+
hidden_states = attn.to_out[0](hidden_states)
|
| 105 |
+
# dropout
|
| 106 |
+
hidden_states = attn.to_out[1](hidden_states)
|
| 107 |
+
|
| 108 |
+
if input_ndim == 4:
|
| 109 |
+
hidden_states = hidden_states.transpose(-1, -2).reshape(batch_size, channel, height, width)
|
| 110 |
+
|
| 111 |
+
if attn.residual_connection:
|
| 112 |
+
hidden_states = hidden_states + residual
|
| 113 |
+
|
| 114 |
+
hidden_states = hidden_states / attn.rescale_output_factor
|
| 115 |
+
|
| 116 |
+
head_size = attn.heads
|
| 117 |
+
batch_size, q_len, v_len = attention_probs.shape
|
| 118 |
+
attention_probs = attention_probs.reshape(batch_size // head_size, head_size, q_len, v_len)
|
| 119 |
+
self.model.attention_maps[self.layer] = low_res_attention_probs
|
| 120 |
+
return hidden_states
|