Context manager to set the active attention backend.
(backend: str | AttentionBackendName = AttentionBackendName.NATIVE)
| 369 | |
| 370 | @contextlib.contextmanager |
| 371 | def attention_backend(backend: str | AttentionBackendName = AttentionBackendName.NATIVE): |
| 372 | """ |
| 373 | Context manager to set the active attention backend. |
| 374 | """ |
| 375 | if backend not in _AttentionBackendRegistry._backends: |
| 376 | raise ValueError(f"Backend {backend} is not registered.") |
| 377 | |
| 378 | backend = AttentionBackendName(backend) |
| 379 | _check_attention_backend_requirements(backend) |
| 380 | _maybe_download_kernel_for_backend(backend) |
| 381 | |
| 382 | old_backend = _AttentionBackendRegistry._active_backend |
| 383 | _AttentionBackendRegistry.set_active_backend(backend) |
| 384 | |
| 385 | try: |
| 386 | yield |
| 387 | finally: |
| 388 | _AttentionBackendRegistry.set_active_backend(old_backend) |
| 389 | |
| 390 | |
| 391 | def dispatch_attention_fn( |
nothing calls this directly
no test coverage detected
searching dependent graphs…