Skip to content
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

layernorm_decay_fix #35927

Merged
merged 9 commits into from
Feb 4, 2025
Merged

Conversation

Ryoo72
Copy link
Contributor

@Ryoo72 Ryoo72 commented Jan 28, 2025

What does this PR do?

This PR improves the weight decay parameter filtering mechanism to properly handle all normalization layers, especially RMSNorm variants.

Background and Motivation

Normalization layers (like LayerNorm and RMSNorm) should not have weight decay applied to their parameters. (See discussion)

The current implementation filters out instances of nn.LayerNorm through type checking. However, this approach has limitations:

  1. Many recent models use RMSNorm instead of LayerNorm
  2. These models often implement their own RMSNorm from scratch instead of using nn.RMSNorm

Simply adding nn.RMSNorm to ALL_LAYERNORM_LAYERS would not catch these custom implementations. Therefore, this PR adds name-based filtering alongside the existing type-based filtering.

Changes

  1. Added name-based parameter filtering to catch all normalization layers regardless of their implementation
  2. Updated documentation to reflect the enhanced filtering approach
  3. Made the filtering more robust by adding RMSNorm to excluded patterns

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines, and
    here are tips on formatting docstrings.
  • Did you write any new necessary tests?

Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

Sorry, something went wrong.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
@Rocketknight1
Copy link
Member

Agree that all normalization layers should not have weight decay applied to their parameters, so this is a good change, but I'll need a review from @SunMarc @muellerzr for the trainer changes!

Copy link
Member

@SunMarc SunMarc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes looks good to me. Can you add an additional test to check if rmsnorm is correctly removed, thanks !

@SunMarc
Copy link
Member

SunMarc commented Jan 28, 2025

please also fix the CI with make style

@Ryoo72
Copy link
Contributor Author

Ryoo72 commented Jan 28, 2025

please also fix the CI with make style

Thank you for the review comments!

I ran

ruff format --check src/transformers/trainer_pt_utils.py

and confirmed it shows 1 file already formatted.

I'll add the additional test for RMSNorm right away!

@Ryoo72
Copy link
Contributor Author

Ryoo72 commented Jan 28, 2025

The changes looks good to me. Can you add an additional test to check if rmsnorm is correctly removed, thanks !

Thank you for the review comments. I added test code to verify RMSNorm parameters are correctly excluded:

from transformers import AutoModelForCausalLM, AutoTokenizer
from transformers.trainer_pt_utils import get_parameter_names
from transformers.trainer import ALL_LAYERNORM_LAYERS

model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen2-7B-Instruct", device_map="auto")
forbidden_layer_names = ["bias", "layernorm", "rmsnorm"]

decay_parameters = get_parameter_names(model, ALL_LAYERNORM_LAYERS, forbidden_layer_names)

all_params = [name for name, _ in model.named_parameters()]

no_decay = set(all_params) - set(decay_parameters)

I confirmed that RMSNorm parameters are correctly filtered out to no_decay set. Here are some examples from no_decay:
'model.layers.17.input_layernorm.weight', 'model.layers.24.input_layernorm.weight', 'model.layers.21.self_attn.k_proj.bias', ...

These are the layers that were not filtered out in the previous code.

@SunMarc
Copy link
Member

SunMarc commented Jan 29, 2025

Nice ! Could you add a test in the PR with a smaller model like hf-internal-testing/tiny-random-LlamaForCausalLM or create a dummy models with rmsnorm layers and linear layers ? Thanks !

@SunMarc
Copy link
Member

SunMarc commented Jan 29, 2025

there is still an issue with the formatting, the ci is still red

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@Ryoo72
Copy link
Contributor Author

Ryoo72 commented Jan 30, 2025

Nice ! Could you add a test in the PR with a smaller model like hf-internal-testing/tiny-random-LlamaForCausalLM or create a dummy models with rmsnorm layers and linear layers ? Thanks !

Sorry for my confusion earlier, and thank you for your patience and clear guidance!

I've added a test using test_get_parameter_names_rmsnorm in tests/trainer/test_trainer_utils.py. The test verifies that both RMSNorm parameters and bias terms are correctly filtered out from weight decay parameters :)

Copy link
Member

@SunMarc SunMarc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, thanks for iterating !

@SunMarc SunMarc merged commit b1954fd into huggingface:main Feb 4, 2025
25 checks passed
elvircrn pushed a commit to elvircrn/transformers that referenced this pull request Feb 13, 2025
* layernorm_decay_fix

* W293 fix

* ruff format fix

* black format

* ruff format

* erase last layer

* add test_get_parameter_names_rmsnorm

* rmsnorm fix
sbucaille pushed a commit to sbucaille/transformers that referenced this pull request Feb 16, 2025
* layernorm_decay_fix

* W293 fix

* ruff format fix

* black format

* ruff format

* erase last layer

* add test_get_parameter_names_rmsnorm

* rmsnorm fix
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants