-
Notifications
You must be signed in to change notification settings - Fork 686
Add Phi-4-mini-instruct #8856
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
Add Phi-4-mini-instruct #8856
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
617d811
Add phi4 mini
jackzhxng a12912a
Partial rotary embeddings
jackzhxng 147805a
Lint
jackzhxng 8e0fc8c
Convert script uses correct ckpt
jackzhxng ef717db
Merge branch 'main' into jz/add-phi4
jackzhxng bb60a20
Merge branch 'main' into jz/add-phi4
jackzhxng 859d3a4
Fix test_model.sh
jackzhxng a8231d8
Remove phi4_mini test from pull target
jackzhxng File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"dim": 3072, | ||
"ffn_dim_multiplier": 1, | ||
"hidden_dim": 8192, | ||
"n_heads": 24, | ||
"n_kv_heads": 8, | ||
"n_layers": 32, | ||
"norm_eps": 1e-05, | ||
"rope_theta": 10000.0, | ||
"use_scaled_rope": false, | ||
"vocab_size": 200064, | ||
"use_hf_rope": true, | ||
"partial_rotary_factor": 0.75, | ||
"attention_qkv_bias": false | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import argparse | ||
from typing import Dict | ||
|
||
import torch | ||
|
||
from torchtune.models.convert_weights import get_mapped_key | ||
|
||
from torchtune.training import FullModelHFCheckpointer | ||
|
||
|
||
# Standard _FROM_META weight mapping of Meta weights to TorchTune. | ||
_PHI_4_FROM_META = { | ||
"tok_embeddings.weight": "tok_embeddings.weight", | ||
"norm.weight": "norm.scale", | ||
"layers.{}.attention.wk.weight": "layers.{}.attn.k_proj.weight", | ||
"layers.{}.attention.wq.weight": "layers.{}.attn.q_proj.weight", | ||
"layers.{}.attention.wv.weight": "layers.{}.attn.v_proj.weight", | ||
"layers.{}.attention.wo.weight": "layers.{}.attn.output_proj.weight", | ||
"layers.{}.attention_norm.weight": "layers.{}.sa_norm.scale", | ||
"layers.{}.ffn_norm.weight": "layers.{}.mlp_norm.scale", | ||
"layers.{}.feed_forward.w1.weight": "layers.{}.mlp.w1.weight", | ||
"layers.{}.feed_forward.w2.weight": "layers.{}.mlp.w2.weight", | ||
"layers.{}.feed_forward.w3.weight": "layers.{}.mlp.w3.weight", | ||
} | ||
|
||
|
||
def phi_4_tune_to_meta(state_dict: Dict[str, torch.Tensor]) -> Dict[str, torch.Tensor]: | ||
""" | ||
Convert a state dict from torchtune's format to Meta's format. This function | ||
doesn't handle any sharding or splitting of state dicts. It follows the | ||
state_dict IN -> state_dict OUT pattern. | ||
|
||
Args: | ||
state_dict (Dict[str, torch.Tensor]): State dict in torchtune's format. | ||
|
||
Returns: | ||
Dict[str, torch.Tensor]: State dict in Meta's format. | ||
""" | ||
converted_state_dict = {} | ||
inverted_mapping_dict = {v: k for k, v in _PHI_4_FROM_META.items()} | ||
|
||
for key, value in state_dict.items(): | ||
new_key = get_mapped_key(key, inverted_mapping_dict) | ||
converted_state_dict[new_key] = value | ||
|
||
# Input and output embeddings are tied. | ||
converted_state_dict["output.weight"] = converted_state_dict[ | ||
"tok_embeddings.weight" | ||
] | ||
|
||
return converted_state_dict | ||
|
||
|
||
def main(): | ||
parser = argparse.ArgumentParser( | ||
description="Convert Phi-4-mini weights to Meta format." | ||
) | ||
parser.add_argument( | ||
"input_dir", | ||
type=str, | ||
help="Path to directory containing checkpoint files", | ||
) | ||
parser.add_argument("output", type=str, help="Path to the output checkpoint") | ||
|
||
args = parser.parse_args() | ||
|
||
checkpointer = FullModelHFCheckpointer( | ||
checkpoint_dir=args.input_dir, | ||
checkpoint_files=[ | ||
"model-00001-of-00003.safetensors", | ||
"model-00002-of-00003.safetensors", | ||
"model-00003-of-00003.safetensors", | ||
], | ||
output_dir=".", | ||
model_type="PHI4_MINI", | ||
) | ||
|
||
print("Loading checkpoint...") | ||
sd = checkpointer.load_checkpoint() | ||
|
||
print("Converting checkpoint...") | ||
sd = phi_4_tune_to_meta(sd["model"]) | ||
|
||
torch.save(sd, args.output) | ||
print(f"Checkpoint saved to {args.output}") | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason to remove it, probably it's mostly covered by llama tests?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh it's just too large to run on every pull request, we only run the small ones on pull