(in_channels, attn_type="vanilla", attn_kwargs=None)
| 231 | |
| 232 | |
| 233 | def make_attn(in_channels, attn_type="vanilla", attn_kwargs=None): |
| 234 | assert attn_type in [ |
| 235 | "vanilla", |
| 236 | "vanilla-xformers", |
| 237 | "memory-efficient-cross-attn", |
| 238 | "linear", |
| 239 | "none", |
| 240 | ], f"attn_type {attn_type} unknown" |
| 241 | if version.parse(torch.__version__) < version.parse("2.0.0") and attn_type != "none": |
| 242 | assert XFORMERS_IS_AVAILABLE, ( |
| 243 | f"We do not support vanilla attention in {torch.__version__} anymore, " |
| 244 | f"as it is too expensive. Please install xformers via e.g. 'pip install xformers==0.0.16'" |
| 245 | ) |
| 246 | attn_type = "vanilla-xformers" |
| 247 | print(f"making attention of type '{attn_type}' with {in_channels} in_channels") |
| 248 | if attn_type == "vanilla": |
| 249 | assert attn_kwargs is None |
| 250 | return AttnBlock(in_channels) |
| 251 | elif attn_type == "vanilla-xformers": |
| 252 | print(f"building MemoryEfficientAttnBlock with {in_channels} in_channels...") |
| 253 | return MemoryEfficientAttnBlock(in_channels) |
| 254 | elif type == "memory-efficient-cross-attn": |
| 255 | attn_kwargs["query_dim"] = in_channels |
| 256 | return MemoryEfficientCrossAttentionWrapper(**attn_kwargs) |
| 257 | elif attn_type == "none": |
| 258 | return nn.Identity(in_channels) |
| 259 | else: |
| 260 | return LinAttnBlock(in_channels) |
| 261 | |
| 262 | |
| 263 | class Model(nn.Module): |
no test coverage detected