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

Refactor validate missing for LoRA + deprecate param utility #2321

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

RdoubleA
Copy link
Contributor

@RdoubleA RdoubleA commented Jan 31, 2025

Context

What is the purpose of this PR? Is it to

  • add a new feature
  • fix a bug
  • update tests and/or documentation
  • other (please add here)

validate_missing_and_unexpected_for_lora takes two sets of keys: base_missing and lora_missing. These are produced when loading the base model weights and adapter model weights separately with load state dict. When loading the base weights, the lora weights will be missing, and vice versa. It then checks against the lora config parameters to make sure everything was loaded correctly.

This is a bit rigid, because 1) it's tied to config params and not the actual model and 2) it checks for a hardcoded list of lora params (like w1, w2, w3 for MLP). When adding new lora parameters, such as for Mixture-of-Experts, this validation utility gets angry.

Since we have to deprecate existing parameters, I've also added a quick decorator similar to @deprecated to warn when a deprecated parameter is used. It also logs only once, only on rank zero! Hooray~

FSDP is enabled. Instantiating model and loading checkpoint on Rank 0 ...
NCCL version 2.21.5+cuda12.4
/data/users/rafiayub/torchtune-rafiayub/recipes/lora_finetune_distributed.py:513: FutureWarning: lora_attn_modules is deprecated for validate_missing_and_unexpected_for_lora and will be removed in future versions. Please use state_dict_keys instead.
  validate_missing_and_unexpected_for_lora(
/data/users/rafiayub/torchtune-rafiayub/torchtune/utils/_logging.py:143: FutureWarning: apply_lora_to_mlp is deprecated for validate_missing_and_unexpected_for_lora and will be removed in future versions. Please use state_dict_keys instead.
  return obj(*args, **kwargs)
/data/users/rafiayub/torchtune-rafiayub/torchtune/utils/_logging.py:143: FutureWarning: apply_lora_to_output is deprecated for validate_missing_and_unexpected_for_lora and will be removed in future versions. Please use state_dict_keys instead.
  return obj(*args, **kwargs)
Instantiating model and loading checkpoint took 2.85 secs
Memory stats after model init:
        GPU peak memory allocation: 1.24 GiB
        GPU peak memory reserved: 1.26 GiB
        GPU peak memory active: 1.24 GiB

Approach

Use set comparisons to deduce what is missing and check against the model's actual lora state dict (done by filtering by "lora" or "magnitude" in the param name. Still a bit hardcoded but more flexible). This does involve passing the model state dict to the function. Previously, the function assumed that querying model state dict incurred memory hits, hence the workarounds to determine lora keys in the model state dict. However, the model state dict is already loaded before you want to validate it anyway, so there is no memory hit. We can just pass in the model state dict directly to make the logic easier.

Changelog

What are the changes made in this PR?

  • Refactor validate_missing_and_unexpected_for_lora
  • Update unit test
  • Update callsites in all recipes

Test plan

Please make sure to do each of the following if applicable to your PR. If you're unsure about any one of these just ask and we will happily help. We also have a contributing page for some guidance on contributing.

  • run pre-commit hooks and linters (make sure you've first installed via pre-commit install)
  • add unit tests for any new functionality
  • update docstrings for any new or updated methods or classes
  • run unit tests via pytest tests
  • run recipe tests via pytest tests -m integration_test
  • manually run any new or modified recipes with sufficient proof of correctness
  • include relevant commands and any other artifacts in this summary (pastes of loss curves, eval results, etc.)

UX

If your function changed a public API, please add a dummy example of what the user experience will look like when calling it.
Here is a docstring example
and a tutorial example

  • I did not change any public API
  • I have added an example to docs or docstrings

Copy link

pytorch-bot bot commented Jan 31, 2025

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/torchtune/2321

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit ae07070 with merge base 6487029 (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jan 31, 2025
@codecov-commenter
Copy link

Codecov Report

Attention: Patch coverage is 0% with 18 lines in your changes missing coverage. Please review.

Project coverage is 23.71%. Comparing base (46d8153) to head (65e94ea).
Report is 5 commits behind head on main.

Files with missing lines Patch % Lines
torchtune/modules/peft/_utils.py 0.00% 17 Missing ⚠️
tests/torchtune/modules/peft/test_utils.py 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2321      +/-   ##
==========================================
- Coverage   23.87%   23.71%   -0.17%     
==========================================
  Files         359      361       +2     
  Lines       21260    21497     +237     
==========================================
+ Hits         5076     5098      +22     
- Misses      16184    16399     +215     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@RdoubleA RdoubleA changed the title Refactor validate missing for LoRA Refactor validate missing for LoRA + deprecate param utility Jan 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants