MCPcopy Create free account
hub / github.com/huggingface/diffusers / attention_backend

Function attention_backend

src/diffusers/models/attention_dispatch.py:371–388  ·  view source on GitHub ↗

Context manager to set the active attention backend.

(backend: str | AttentionBackendName = AttentionBackendName.NATIVE)

Source from the content-addressed store, hash-verified

369
370@contextlib.contextmanager
371def 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
391def dispatch_attention_fn(

Callers

nothing calls this directly

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…