Skip to content

validate checkpoint is consistent with meta_to_tune flag #2736

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all 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
16 changes: 16 additions & 0 deletions torchtune/models/convert_weights.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,22 @@ def get_mapped_key(key: str, mapping_dict: dict[str, str]) -> str:
return new_key


def is_in_meta_format(state_dict: dict[str, torch.Tensor]) -> bool:
"""
Check whether the state dict is in Meta's format by checking the presence
of unique keys only available in META format.
"""
unique_meta_keys = {k for k, v in _FROM_META.items() if k != v}
for key in state_dict.keys():
if key not in ["rope.freqs"]: # Skip loading the position embeddings
if any(k.isdigit() for k in key.split(".")):
# Replace layer number with "{}" to create key for lookup
key = re.sub(r"(\.\d+)", ".{}", key)
if key in unique_meta_keys:
return True
return False


def meta_to_tune(state_dict: dict[str, torch.Tensor]) -> dict[str, torch.Tensor]:
"""
Convert a state dict from Meta's format to torchtune's format. State dicts
Expand Down
Loading