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)
| 25 | |
| 26 | |
| 27 | def 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 |
nothing calls this directly
no test coverage detected