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

Function aggregate_attention_all

sample.py:202–227  ·  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

200 return out.cpu()
201
202def aggregate_attention_all(prompts, attention_store: AttentionStore, res: int, from_where: List[str], is_cross: bool, select: int, len_tokens: int):
203 attention_maps = attention_store.get_average_attention()
204
205 out = []
206 for _, value in attention_maps.items():
207
208
209 for item in value:
210 res = int(math.sqrt(item.shape[1]))
211 cross_maps = item.reshape(len(prompts), -1, res , res, item.shape[-1])[select]
212 if res!=16:
213 continue
214 cross_maps_512 = torch.zeros((cross_maps.shape[0], 256, 256, 77))
215 for token in range(len_tokens):
216 token_cross_map = cross_maps[:, :, :, token].reshape(1, cross_maps.shape[0], res, res)
217 token_cross_map = F.interpolate(token_cross_map, size=(256, 256), mode='bicubic').reshape(cross_maps.shape[0], 256, 256)
218 cross_maps_512[:, :, :, token] = token_cross_map[:, :, :]
219 out.append(cross_maps_512)
220 out = torch.cat(out, dim=0)
221 out = out.sum(0) / out.shape[0]
222
223 absolute = True
224 if not absolute:
225 out = (out - out.min()) / (out.max() - out.min() + 1e-8)
226
227 return out.cpu()
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):

Callers 1

show_cross_attentionFunction · 0.85

Calls 1

get_average_attentionMethod · 0.80

Tested by

no test coverage detected