-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Disable PEFT input autocast when using fp8 layerwise casting #10685
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
fe1a9a4
disable peft input autocast
a-r-r-o-w aa6282e
Merge branch 'main' into peft/disable-input-autocast
a-r-r-o-w e8f78ce
use new peft method name; only disable peft input autocast if submodu…
a-r-r-o-w 020e374
Merge branch 'main' into peft/disable-input-autocast
a-r-r-o-w fb8ad79
add test; reference PeftInputAutocastDisableHook in peft docs
a-r-r-o-w 448ca9d
add load_lora_weights test
a-r-r-o-w c2b1ec5
Merge branch 'main' into peft/disable-input-autocast
a-r-r-o-w acba7b7
casted -> cast
a-r-r-o-w e0c45c2
Update tests/lora/utils.py
a-r-r-o-w File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,14 +17,16 @@ | |
|
|
||
| import torch | ||
|
|
||
| from ..utils import get_logger | ||
| from ..utils import get_logger, is_peft_available, is_peft_version | ||
| from .hooks import HookRegistry, ModelHook | ||
|
|
||
|
|
||
| logger = get_logger(__name__) # pylint: disable=invalid-name | ||
|
|
||
|
|
||
| # fmt: off | ||
| _LAYERWISE_CASTING_HOOK = "layerwise_casting" | ||
| _PEFT_AUTOCAST_DISABLE_HOOK = "peft_autocast_disable" | ||
| SUPPORTED_PYTORCH_LAYERS = ( | ||
| torch.nn.Conv1d, torch.nn.Conv2d, torch.nn.Conv3d, | ||
| torch.nn.ConvTranspose1d, torch.nn.ConvTranspose2d, torch.nn.ConvTranspose3d, | ||
|
|
@@ -34,6 +36,11 @@ | |
| DEFAULT_SKIP_MODULES_PATTERN = ("pos_embed", "patch_embed", "norm", "^proj_in$", "^proj_out$") | ||
| # fmt: on | ||
|
|
||
| _SHOULD_DISABLE_PEFT_INPUT_AUTOCAST = is_peft_available() and is_peft_version(">", "0.14.0") | ||
| if _SHOULD_DISABLE_PEFT_INPUT_AUTOCAST: | ||
| from peft.helpers import disable_input_dtype_casting | ||
| from peft.tuners.tuners_utils import BaseTunerLayer | ||
|
|
||
|
|
||
| class LayerwiseCastingHook(ModelHook): | ||
| r""" | ||
|
|
@@ -70,6 +77,32 @@ def post_forward(self, module: torch.nn.Module, output): | |
| return output | ||
|
|
||
|
|
||
| class PeftInputAutocastDisableHook(ModelHook): | ||
| r""" | ||
| A hook that disables the casting of inputs to the module weight dtype during the forward pass. By default, PEFT | ||
| casts the inputs to the weight dtype of the module, which can lead to precision loss. | ||
|
|
||
| The reasons for needing this are: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks a LOT for writing this! Really, thanks! |
||
| - If we don't add PEFT layers' weight names to `skip_modules_pattern` when applying layerwise casting, the | ||
| inputs will be casted to the, possibly lower precision, storage dtype. Reference: | ||
| https://github.com/huggingface/peft/blob/0facdebf6208139cbd8f3586875acb378813dd97/src/peft/tuners/lora/layer.py#L706 | ||
| - We can, on our end, use something like accelerate's `send_to_device` but for dtypes. This way, we can ensure | ||
| that the inputs are casted to the computation dtype correctly always. However, there are two goals we are | ||
| hoping to achieve: | ||
| 1. Making forward implementations independent of device/dtype casting operations as much as possible. | ||
| 2. Peforming inference without losing information from casting to different precisions. With the current | ||
| PEFT implementation (as linked in the reference above), and assuming running layerwise casting inference | ||
| with storage_dtype=torch.float8_e4m3fn and compute_dtype=torch.bfloat16, inputs are cast to | ||
| torch.float8_e4m3fn in the lora layer. We will then upcast back to torch.bfloat16 when we continue the | ||
| forward pass in PEFT linear forward or Diffusers layer forward, with a `send_to_dtype` operation from | ||
| LayerwiseCastingHook. This will be a lossy operation and result in poorer generation quality. | ||
| """ | ||
|
|
||
| def new_forward(self, module: torch.nn.Module, *args, **kwargs): | ||
| with disable_input_dtype_casting(module): | ||
| return self.fn_ref.original_forward(*args, **kwargs) | ||
|
|
||
|
|
||
| def apply_layerwise_casting( | ||
| module: torch.nn.Module, | ||
| storage_dtype: torch.dtype, | ||
|
|
@@ -134,6 +167,7 @@ def apply_layerwise_casting( | |
| skip_modules_classes, | ||
| non_blocking, | ||
| ) | ||
| _disable_peft_input_autocast(module) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The method is already version-guarded. So, no worries. (For other reviewers). |
||
|
|
||
|
|
||
| def _apply_layerwise_casting( | ||
|
|
@@ -188,4 +222,24 @@ def apply_layerwise_casting_hook( | |
| """ | ||
| registry = HookRegistry.check_if_exists_or_initialize(module) | ||
| hook = LayerwiseCastingHook(storage_dtype, compute_dtype, non_blocking) | ||
| registry.register_hook(hook, "layerwise_casting") | ||
| registry.register_hook(hook, _LAYERWISE_CASTING_HOOK) | ||
|
|
||
|
|
||
| def _is_layerwise_casting_active(module: torch.nn.Module) -> bool: | ||
| for submodule in module.modules(): | ||
| if ( | ||
| hasattr(submodule, "_diffusers_hook") | ||
| and submodule._diffusers_hook.get_hook(_LAYERWISE_CASTING_HOOK) is not None | ||
| ): | ||
| return True | ||
| return False | ||
|
|
||
|
|
||
| def _disable_peft_input_autocast(module: torch.nn.Module) -> None: | ||
| if not _SHOULD_DISABLE_PEFT_INPUT_AUTOCAST: | ||
| return | ||
| for submodule in module.modules(): | ||
| if isinstance(submodule, BaseTunerLayer) and _is_layerwise_casting_active(submodule): | ||
| registry = HookRegistry.check_if_exists_or_initialize(submodule) | ||
| hook = PeftInputAutocastDisableHook() | ||
| registry.register_hook(hook, _PEFT_AUTOCAST_DISABLE_HOOK) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.