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: 3 additions & 0 deletions src/diffusers/utils/accelerate_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
Accelerate utilities: Utilities related to accelerate
"""

import functools

from packaging import version

from .import_utils import is_accelerate_available
Expand All @@ -40,6 +42,7 @@ def apply_forward_hook(method):
if version.parse(accelerate_version) < version.parse("0.17.0"):
return method

@functools.wraps(method)
def wrapper(self, *args, **kwargs):
if hasattr(self, "_hf_hook") and hasattr(self._hf_hook, "pre_forward"):
self._hf_hook.pre_forward(self)
Expand Down
21 changes: 21 additions & 0 deletions tests/others/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,27 @@ def _capture(target_device):
)


class ApplyForwardHookTester(unittest.TestCase):
"""Tests for :func:`diffusers.utils.accelerate_utils.apply_forward_hook`."""

def test_preserves_wrapped_function_metadata(self):
import inspect

from diffusers.utils.accelerate_utils import apply_forward_hook

@apply_forward_hook
def example(self, x: int, y: int = 0) -> int:
"""Example method docstring."""
return x + y

assert example.__name__ == "example"
assert example.__doc__ == "Example method docstring."
assert hasattr(example, "__wrapped__")
signature = inspect.signature(example)
assert list(signature.parameters) == ["self", "x", "y"]
assert signature.parameters["y"].default == 0


# Copied from https://github.com/huggingface/transformers/blob/main/tests/utils/test_expectations.py
class ExpectationsTester(unittest.TestCase):
def test_expectations(self):
Expand Down
Loading