Skip to content

Update get_merged_lora_ckpt for dist checkpoints #2834

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 8 commits into from
Jun 23, 2025

Conversation

ankitageorge
Copy link
Contributor

@ankitageorge ankitageorge commented Jun 18, 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)

Please link to any issues this PR addresses.

Changelog

What are the changes made in this PR?

  • There was a bug where the get_merged_lora_ckpt method doesn't work in distributed recipes when async checkpointing was enabled, because the existing method expects the data to be all on rank-0. This PR adds a new method get_merged_lora_dist_ckpt to be used for dist checkpoints and appropriately calls it when needed
  • note that this doesn't work for nf4 tensors, I've added a comment in the code as well
  • When testing on 70B model with the lora_finetune_distributed recipe, it took ~20s, compared to 63s for gathering and ~250s for saving (~310s total), saving almost 5 minutes
  • since we save the adapter weights separately, change the order to save that first, so that the trainer is unblocked faster. This because dist checkpointer blocks on itself for two separate calls, so if we save adapter checkpoint after normal, then we are blocking on the normal checkpoint saving time, which is longer than adapter.

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.

  • [ x] 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
  • [x ] run unit tests via pytest tests
  • [x ] run recipe tests via pytest tests -m integration_test
  • [x ] 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

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

Copy link

pytorch-bot bot commented Jun 18, 2025

🔗 Helpful Links

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

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

⏳ No Failures, 3 Pending

As of commit 9a0a6eb with merge base 9d91fe3 (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 Jun 18, 2025
@ankitageorge ankitageorge marked this pull request as ready for review June 18, 2025 19:02
@ankitageorge ankitageorge changed the title dist merge Update get_merged_lora_ckpt for dist checkpoints Jun 18, 2025
@ankitageorge ankitageorge requested a review from joecummings June 18, 2025 19:37
lora_moe_modules = _get_lora_moe_modules(state_dict)

# Create a simple module for matrix multiplication
class MatMulModule(torch.nn.Module):
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we need this instead of just calling matmul directly?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

these operations don't work properly on d-tensors. it's what was causing the hangs

Copy link
Contributor

Choose a reason for hiding this comment

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

"doesn't work properly" - can you expand on that?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok actually, I think it's not needed. Good catch. I thought it was causing problems, but I just re-tested without it and it still works. I'll get rid of them and just add barriers to the existing method.

lora_b_weight = state_dict[f"{module}.lora_{param}_b"]

# Create a simple module for transpose operation
class TransposeModule(torch.nn.Module):
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here: why does this need to be a transpose module?

@ankitageorge ankitageorge force-pushed the fix-dist-merged-weights branch from e5ed645 to 9a0a6eb Compare June 23, 2025 20:25
@ankitageorge ankitageorge merged commit 5b2e881 into pytorch:main Jun 23, 2025
14 checks passed
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