Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions src/diffusers/models/modeling_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -648,9 +648,6 @@ def set_attention_backend(self, backend: str) -> None:
continue
processor._attention_backend = backend

# Important to set the active backend so that it propagates gracefully throughout.
_AttentionBackendRegistry.set_active_backend(backend)

def reset_attention_backend(self) -> None:
"""
Resets the attention backend for the model. Following calls to `forward` will use the environment default, if
Expand Down
17 changes: 17 additions & 0 deletions tests/models/testing_utils/attention.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,23 @@ def teardown_method(self):
gc.collect()
backend_empty_cache(torch_device)

def test_set_attention_backend_does_not_change_registry(self):
model = self.model_class(**self.get_init_dict())
initial_backend, _ = _AttentionBackendRegistry.get_active_backend()
model_backend = (
AttentionBackendName.NATIVE
if initial_backend != AttentionBackendName.NATIVE
else AttentionBackendName._NATIVE_MATH
)

try:
model.set_attention_backend(model_backend.value)
active_backend, _ = _AttentionBackendRegistry.get_active_backend()
assert active_backend == initial_backend
finally:
model.reset_attention_backend()
_AttentionBackendRegistry.set_active_backend(initial_backend)

@torch.no_grad()
@pytest.mark.parametrize("backend", _ALL_BACKEND_PARAMS)
def test_set_attention_backend_matches_context_manager(self, backend):
Expand Down
Loading