-
Notifications
You must be signed in to change notification settings - Fork 239
Feat (brevitas_examples/llm): better RMSNorm replacement #1436
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
Open
Giuseppe5
wants to merge
8
commits into
Xilinx:dev
Choose a base branch
from
Giuseppe5:gemma_rmsnorm
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+129
−74
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7cc065e
Fix (brevitas_examples/llm): support for Gemma RMSNorm
Giuseppe5 ebeb93d
Feat (brevitas_examples/llm): best RMSNorm replacement
Giuseppe5 030d735
New and improved
Giuseppe5 9a9d9df
typo
Giuseppe5 655c2ab
review
Giuseppe5 aebb155
fix
Giuseppe5 30025f1
Review
Giuseppe5 df6cde2
review 2
Giuseppe5 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
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 |
|---|---|---|
|
|
@@ -3,36 +3,67 @@ | |
| SPDX-License-Identifier: MIT | ||
| """ | ||
|
|
||
| from inspect import signature | ||
|
|
||
| from packaging import version | ||
| import torch | ||
| from torch import nn | ||
|
|
||
| from brevitas import torch_version | ||
| from brevitas.graph.base import ModuleToModuleByClass | ||
| from brevitas.graph import ModuleInstanceToModuleInstance | ||
| from brevitas.graph import ModuleToModuleByClass | ||
| from brevitas.graph.equalize import _is_scale_invariant_module | ||
| from brevitas.graph.equalize import LayerNormToRMS | ||
| from brevitas.graph.equalize import MergeLnAffine | ||
| from brevitas.graph.utils import get_module | ||
|
|
||
|
|
||
| def replace_rmsnorm_with_torch(model, config): | ||
| assert torch_version >= version.parse('2.4'), "torch.nn.RMSNorm requires torch 2.4 or greater" | ||
| set_of_layers = set(type(x) for x in model.modules() if 'RMS' in type(x).__name__) | ||
| dtype = next(model.parameters()).dtype | ||
| device = next(model.parameters()).device | ||
| rewriters = [ | ||
| ModuleToModuleByClass( | ||
| rms_cls, | ||
| torch.nn.RMSNorm, | ||
| normalized_shape=lambda module: module.weight.shape[0], | ||
| eps=config.rms_norm_eps, | ||
| dtype=dtype, | ||
| device=device) for rms_cls in set_of_layers] | ||
| dtype = next(iter(model.parameters())).dtype | ||
| for r in rewriters: | ||
| model = r.apply(model) | ||
| model = model.to(dtype) | ||
| return model | ||
| class rmsnorm_patch: | ||
|
|
||
| def __init__(self, model, config, enabled=True): | ||
| self.model = model | ||
| self.config = config | ||
| if enabled: | ||
| self.rmsnorm_classes = tuple( | ||
| set(type(x) for x in model.modules() if 'RMS' in type(x).__name__)) | ||
| else: | ||
| self.rmsnorm_classes = tuple() | ||
| self.mapping = dict() | ||
|
|
||
| def __enter__(self): | ||
| assert torch_version >= version.parse('2.4'), "torch.nn.RMSNorm requires torch 2.4 or greater" | ||
|
|
||
| dtype = next(self.model.parameters()).dtype | ||
| device = next(self.model.parameters()).device | ||
|
|
||
| rewriters = [ | ||
| ModuleToModuleByClass( | ||
| rms_cls, | ||
| torch.nn.RMSNorm, | ||
| normalized_shape=lambda module: module.weight.shape[0], | ||
| eps=self.config.rms_norm_eps, | ||
| dtype=dtype, | ||
| device=device) for rms_cls in self.rmsnorm_classes] | ||
|
|
||
| for r in rewriters: | ||
| self.model = r.apply(self.model) | ||
| self.mapping.update(r.old_new_module_dict) | ||
|
|
||
| self.model = self.model.to(dtype) | ||
| return self | ||
|
|
||
| def __exit__(self, *args, **kwargs): | ||
| rewriters = [] | ||
| dtype = next(self.model.parameters()).dtype | ||
|
|
||
| for old_module, new_module in self.mapping.items(): | ||
|
Collaborator
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. No need to iterate twice. It can be done as: |
||
| rewriter = ModuleInstanceToModuleInstance(old_module, new_module) | ||
| rewriters.append(rewriter) | ||
|
|
||
| for r in rewriters: | ||
| self.model = r.apply(self.model) | ||
|
|
||
| self.model = self.model.to(dtype) | ||
Giuseppe5 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| def replace_bias(next_module, new_bias): | ||
|
|
@@ -106,8 +137,8 @@ def merge_layernorm_affine_params(graph_model): | |
|
|
||
|
|
||
| @torch.no_grad() | ||
| def apply_layernorm_affine_merge(graph_model): | ||
| eq = MergeLnAffine() | ||
| def apply_layernorm_affine_merge(graph_model, rmsnorm_classes): | ||
| eq = MergeLnAffine(extra_state_kwargs={'scale_invariant_layers': rmsnorm_classes}) | ||
| graph_model = eq.apply(graph_model) | ||
| return graph_model | ||
|
|
||
|
|
||
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.