def aggregate_attention(
    attention_store: AttentionStore, res: int, from_where: List[str], is_cross: bool, select: int
) -> torch.Tensor:
    """Aggregates the attention across the different layers and heads at the specified resolution."""
    out = []
    attention_maps = attention_store.get_average_attention()

    # for k, v in attention_maps.items():
    #     for vv in v:
    #         print(vv.shape)
    # exit()

    num_pixels = res**2
    for location in from_where:
        for item in attention_maps[f"{location}_{'cross' if is_cross else 'self'}"]:
            if item.shape[1] == num_pixels:
                cross_maps = item.reshape(1, -1, res, res, item.shape[-1])[select]
                out.append(cross_maps)
    out = torch.cat(out, dim=0)
    out = out.sum(0) / out.shape[0]
    return out
Aggregates the attention across the different layers and heads at the specified resolution.
examples/community/pipeline_stable_diffusion_boxdiff.py