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

Esm checkpointing #26454

Merged
merged 2 commits into from
Sep 28, 2023
Merged

Conversation

Amelie-Schreiber
Copy link
Contributor

What does this PR do?

Title: Resolve In-Place Operation Error in ESM Embeddings

Description:

This pull request addresses a critical issue concerning in-place operations within the ESM embeddings module which was causing a RuntimeError during the backward pass when training with certain configurations. The error message being encountered was:

RuntimeError: Output 0 of MatMul4BitBackward is a view and is being modified inplace.

The modifications in this pull request ensure that in-place operations are replaced with out-of-place operations to comply with the PyTorch autograd's requirement, which disallows in-place operations on tensor views to ensure correct gradient computation.

Changes:

  1. Replaced in-place operations in modeling_esm.py with their out-of-place counterparts.
  2. Ensured that all tensors being operated upon are not views to avoid the aforementioned RuntimeError.

These changes have been tested and verified to resolve the error during the training phase, thus improving the robustness of the ESM module for a wider variety of training configurations.

This resolution is crucial for researchers and practitioners working with ESM models, ensuring smooth training and utilization of the models provided within the Transformers library.

It's important to note that while resolving the in-place operation error is crucial for correct functionality, the switch from in-place to out-of-place operations may have a slight impact on computational efficiency and memory usage. In-place operations are usually more memory-efficient as they don't require additional memory allocation for storing the results; they directly update the values in the original tensors. On the other hand, out-of-place operations create new tensors to store the results, which can increase the memory footprint of the model.

However, due to the fact that the ESM-2 models are now compatible with QLoRA, this seems negligible and a good compromise.

Fixes # (issue)

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.

Copy link
Member

@Rocketknight1 Rocketknight1 left a comment

Choose a reason for hiding this comment

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

I think this is a good change! The effect on memory during training should be negligible or zero - intermediate tensors must be retained for autograd/backpropagation anyway.

The only benefit to in-place ops would be during inference, but even during inference the old buffer can be immediately freed as soon as the new buffer is created, so the memory overhead is very small and very transient.

One question, though: What conditions cause this error to occur? Does it only occur when using gradient checkpointing or heavy quantization?

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint.

Copy link
Member

@LysandreJik LysandreJik left a comment

Choose a reason for hiding this comment

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

Thank you, @Amelie-Schreiber

@Rocketknight1 Rocketknight1 merged commit 4e931a8 into huggingface:main Sep 28, 2023
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.

5 participants