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

Load model faster for checkpoint conversion #5

Merged
merged 7 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions periflow/sdk/resource/checkpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,7 @@ def convert(

"""
try:
import accelerate # type: ignore[import] # pylint: disable=import-outside-toplevel
seungahdev marked this conversation as resolved.
Show resolved Hide resolved
import torch # type: ignore[import] # pylint: disable=import-outside-toplevel
except ImportError as exc:
raise CheckpointConversionError(
Expand Down Expand Up @@ -900,12 +901,14 @@ def convert(
"Start loading Hugging Face checkpoint(%s) for conversion...",
model_name_or_path,
)
state_dict = hf_factory.from_pretrained(
model_name_or_path,
torch_dtype=torch.float32,
cache_dir=cache_dir,
trust_remote_code=True,
).state_dict()
with accelerate.init_empty_weights():
seungahdev marked this conversation as resolved.
Show resolved Hide resolved
state_dict = hf_factory.from_pretrained(
model_name_or_path,
torch_dtype=torch.float32,
cache_dir=cache_dir,
trust_remote_code=True,
device_map="cpu",
).state_dict()
logger.info(
"Hugging Face checkpoint(%s) is successfully loaded!",
model_name_or_path,
Expand Down
Loading