MCPcopy Create free account
hub / github.com/YesianRohn/TextSSR / apply_forward_hook

Function apply_forward_hook

diffusers/src/diffusers/utils/accelerate_utils.py:27–48  ·  view source on GitHub ↗

Decorator that applies a registered CpuOffload hook to an arbitrary function rather than `forward`. This is useful for cases where a PyTorch module provides functions other than `forward` that should trigger a move to the appropriate acceleration device. This is the case for `encode` an

(method)

Source from the content-addressed store, hash-verified

25
26
27def apply_forward_hook(method):
28 """
29 Decorator that applies a registered CpuOffload hook to an arbitrary function rather than `forward`. This is useful
30 for cases where a PyTorch module provides functions other than `forward` that should trigger a move to the
31 appropriate acceleration device. This is the case for `encode` and `decode` in [`AutoencoderKL`].
32
33 This decorator looks inside the internal `_hf_hook` property to find a registered offload hook.
34
35 :param method: The method to decorate. This method should be a method of a PyTorch module.
36 """
37 if not is_accelerate_available():
38 return method
39 accelerate_version = version.parse(accelerate.__version__).base_version
40 if version.parse(accelerate_version) < version.parse("0.17.0"):
41 return method
42
43 def wrapper(self, *args, **kwargs):
44 if hasattr(self, "_hf_hook") and hasattr(self._hf_hook, "pre_forward"):
45 self._hf_hook.pre_forward(self)
46 return method(self, *args, **kwargs)
47
48 return wrapper

Callers

nothing calls this directly

Calls 1

is_accelerate_availableFunction · 0.85

Tested by

no test coverage detected