MCPcopy Create free account
hub / github.com/Monalissaa/DisenDiff / aggregate_attention_all_step

Function aggregate_attention_all_step

sample.py:230–255  ·  view source on GitHub ↗
(prompts, attention_store: AttentionStore, res: int, from_where: List[str], is_cross: bool, select: int, len_tokens: int)

Source from the content-addressed store, hash-verified

228
229
230def aggregate_attention_all_step(prompts, attention_store: AttentionStore, res: int, from_where: List[str], is_cross: bool, select: int, len_tokens: int):
231 attention_all_step_maps = attention_store.all_step_attention
232
233 out_all_step = {}
234 for step, attention_maps in attention_all_step_maps.items():
235 out = []
236 for _, value in attention_maps.items():
237 for item in value:
238 res = int(math.sqrt(item.shape[1]))
239 cross_maps = item.reshape(len(prompts), -1, res , res, item.shape[-1])[select]
240 cross_maps_512 = torch.zeros((cross_maps.shape[0], 256, 256, 77))
241 for token in range(len_tokens):
242 token_cross_map = cross_maps[:, :, :, token].reshape(1, cross_maps.shape[0], res, res)
243 token_cross_map = F.interpolate(token_cross_map, size=(256, 256), mode='bicubic').reshape(cross_maps.shape[0], 256, 256)
244 cross_maps_512[:, :, :, token] = token_cross_map[:, :, :]
245 out.append(cross_maps_512)
246 print(step)
247 out = torch.cat(out, dim=0)
248 out = out.sum(0) / out.shape[0]
249
250 absolute = True
251 if not absolute:
252 out = (out - out.min()) / (out.max() - out.min() + 1e-8)
253 out_all_step[step] = out.cpu()
254
255 return out_all_step
256
257def show_cross_attention(prompts, tokenizer, save_path, base_count, attention_store: AttentionStore, res: int, from_where: List[str], select: int = 0):
258 tokens = tokenizer.encode(prompts[select])

Callers 1

show_cross_attentionFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected