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

Safetensors offload #20321

Merged
merged 6 commits into from
Nov 28, 2022
Merged

Safetensors offload #20321

merged 6 commits into from
Nov 28, 2022

Conversation

sgugger
Copy link
Collaborator

@sgugger sgugger commented Nov 18, 2022

What does this PR do?

This PRs make offload to disk more efficient for models that have a checkpoint in safetensors format: instead of re-saving everything as Numpy memory-mapped array, it uses directly the fact we can access the tensor in the checkpoint without loading the rest.

Goes with huggingface/accelerate#873

@HuggingFaceDocBuilderDev
Copy link

HuggingFaceDocBuilderDev commented Nov 18, 2022

The documentation is not available anymore as the PR was closed or merged.

@@ -28,7 +28,7 @@

import torch
from packaging import version
from torch import Tensor, device, nn
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This import was dangerous with so many local variables named device. It was only used in a type annotation.

Comment on lines +2539 to +2543
offload_index = {
p: {"safetensors_file": os.path.join(folder, f), "weight_name": p, "dtype": str_dtype}
for p, f in sharded_metadata["weight_map"].items()
if param_device_map[p] == "disk"
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We build the offload_index here when the checkpoin is safetensors, this way we can skip loading all the shards and gain time.

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.

Great, very cool addition! Should make a lot of things simpler

@sgugger sgugger merged commit 3016392 into main Nov 28, 2022
@sgugger sgugger deleted the safetensors_offload branch November 28, 2022 15:35
amyeroberts pushed a commit to amyeroberts/transformers that referenced this pull request Dec 7, 2022
* INtegrate safetensos in weight offloading

* Use safetensors checkpoint for offload when available

* Make naming consistent

* Make load faster

* Quality

* Add default
mpierrau pushed a commit to mpierrau/transformers that referenced this pull request Dec 15, 2022
* INtegrate safetensos in weight offloading

* Use safetensors checkpoint for offload when available

* Make naming consistent

* Make load faster

* Quality

* Add default
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

3 participants