-
Notifications
You must be signed in to change notification settings - Fork 873
Qualcomm AI Engine Direct - GLM1.5B #15691
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| # This source code is licensed under the BSD-style license found in the | ||
| # LICENSE file in the root directory of this source tree. | ||
|
|
||
| from executorch.examples.models.glm.convert_weights import convert_weights | ||
| from executorch.examples.models.llama.model import Llama2Model | ||
|
|
||
|
|
||
| class GLMModel(Llama2Model): | ||
| def __init__(self, **kwargs): | ||
| super().__init__(**kwargs) | ||
|
|
||
|
|
||
| __all__ = [ | ||
| "GLMModel", | ||
| "convert_weights", | ||
| ] |
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,17 @@ | ||
| { | ||
| "dim": 2048, | ||
| "ffn_dim_multiplier": 1, | ||
| "hidden_dim": 6144, | ||
| "n_heads": 16, | ||
| "head_dim": 128, | ||
| "n_kv_heads": 4, | ||
| "n_layers": 28, | ||
| "norm_eps": 1e-05, | ||
| "rope_theta": 10000.0, | ||
| "use_scaled_rope": false, | ||
| "vocab_size": 59264, | ||
| "use_hf_rope": true, | ||
| "attention_qkv_bias": false, | ||
| "use_qk_norm": false, | ||
| "model_architecture" : "GlmForCausalLM" | ||
| } | ||
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,79 @@ | ||
| import argparse | ||
| import os | ||
| from typing import Dict | ||
|
|
||
| import torch | ||
| from safetensors.torch import load_file | ||
| from torchtune.models.convert_weights import get_mapped_key | ||
|
|
||
| # Standard _FROM_META weight mapping of Meta weights to TorchTune + additional bias weight mappings. | ||
| _GLM_FROM_META = { | ||
| "tok_embeddings.weight": "model.embed_tokens.weight", | ||
| "norm.weight": "model.norm.weight", | ||
| "output.weight": "lm_head.weight", | ||
| "layers.{}.attention.wk.weight": "model.layers.{}.self_attn.k_proj.weight", | ||
| "layers.{}.attention.wq.weight": "model.layers.{}.self_attn.q_proj.weight", | ||
| "layers.{}.attention.wv.weight": "model.layers.{}.self_attn.v_proj.weight", | ||
| "layers.{}.attention.wo.weight": "model.layers.{}.self_attn.o_proj.weight", | ||
| "layers.{}.attention_norm.weight": "model.layers.{}.input_layernorm.weight", | ||
| "layers.{}.ffn_norm.weight": "model.layers.{}.post_attention_layernorm.weight", | ||
| "layers.{}.feed_forward.gate_up_proj.weight": "model.layers.{}.mlp.gate_up_proj.weight", | ||
| "layers.{}.feed_forward.down_proj.weight": "model.layers.{}.mlp.down_proj.weight", | ||
| } | ||
|
|
||
|
|
||
| def glm_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 _GLM_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 | ||
|
|
||
| if "lm_head.weight" not in state_dict: | ||
| converted_state_dict["output.weight"] = converted_state_dict[ | ||
| "tok_embeddings.weight" | ||
| ] | ||
|
|
||
| return converted_state_dict | ||
|
|
||
|
|
||
| def convert_weights(input_dir: str, output_file: str) -> None: | ||
| pt_path = os.path.join(input_dir, "model.safetensors") | ||
| print("Loading checkpoint from file...") | ||
| sd = load_file(pt_path) | ||
|
|
||
| print("Converting checkpoint...") | ||
| sd = glm_tune_to_meta(sd) | ||
|
|
||
| print("Saving checkpoint...") | ||
| torch.save(sd, output_file) | ||
| print("Done.") | ||
|
|
||
|
|
||
| def main(): | ||
| parser = argparse.ArgumentParser(description="Convert GLM 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() | ||
| convert_weights(args.input_dir, args.output) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
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 |
|---|---|---|
|
|
@@ -27,4 +27,5 @@ | |
| "smollm2_135m": "smollm2_135m", | ||
| "smollm3-3b": "smollm3", | ||
| "codegen2_1b": "codegen", | ||
| "glm-1_5b": "glm", | ||
| } | ||
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 |
|---|---|---|
|
|
@@ -41,6 +41,7 @@ enum DecoderModelVersion { | |
| kSmollm2_135m, | ||
| kSmollm3, | ||
| kCodegen, | ||
| kGlm, | ||
| }; | ||
|
|
||
| enum KvBitWidth { | ||
|
|
||
Oops, something went wrong.
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.
Do we have any existing variable that can be used for this?
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.
Thanks for the suggestion.
I was actually thinking of reusing
base_model_name_or_path. However, it seems like this variable is used in optimum for some other purpose, like referring to actual model path, so I created a new variable to prevent any conflict in future.Another reason of creating this config is that as we are enabling more models, we noticed minor differences among models. For example, GLM FeedForward is different from other model's FeedForward. We need some variables to differentiate GLM and other LLM models.