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

convert.py : Get rope scale from HuggingFace models #2772

Merged
merged 4 commits into from
Aug 25, 2023
Merged
Changes from 2 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
38 changes: 22 additions & 16 deletions convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ class Params:
f_norm_eps: float

f_rope_freq_base: Optional[float] = None
f_rope_scale: Optional[float] = None

ftype: Optional[GGMLFileType] = None

Expand Down Expand Up @@ -160,13 +161,14 @@ def guessed(model: 'LazyModel') -> 'Params':
def loadHFTransformerJson(model: 'LazyModel', config_path: 'Path') -> 'Params':
config = json.load(open(config_path))

n_vocab = config["vocab_size"]
n_embd = config["hidden_size"]
n_layer = config["num_hidden_layers"]
n_ff = config["intermediate_size"]
n_head = config["num_attention_heads"]
n_head_kv = config["num_key_value_heads"] if "num_key_value_heads" in config else n_head
f_norm_eps = config["rms_norm_eps"]
n_vocab = config["vocab_size"]
n_embd = config["hidden_size"]
n_layer = config["num_hidden_layers"]
n_ff = config["intermediate_size"]
n_head = config["num_attention_heads"]
n_head_kv = config["num_key_value_heads"] if "num_key_value_heads" in config else n_head
f_norm_eps = config["rms_norm_eps"]
f_rope_scale = config.get("rope_scaling", {}).get("factor", None) if config.get("rope_scaling", {}).get("type", "") == "linear" else None
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: dict.get returns None as a fallback by default. This would be more readable:

Suggested change
f_rope_scale = config.get("rope_scaling", {}).get("factor", None) if config.get("rope_scaling", {}).get("type", "") == "linear" else None
rope_scaling = config.get("rope_scaling", {})
f_rope_scale = rope_scaling.get("factor") if rope_scaling.get("type") == "linear" else None

Copy link
Collaborator

Choose a reason for hiding this comment

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

I would be happy with even a couple of plain if statements. But yeah, that one-liner a bit hard to read, anything that improves the readability would be good.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was overfitting a bit to the other implementations trying to match the style. I am not a fan of tertiary operators or long lines, so I would also prefer an if/else implementation (tested and committed now).


n_mult = Params.find_n_mult(n_ff, n_embd)

Expand All @@ -179,15 +181,16 @@ def loadHFTransformerJson(model: 'LazyModel', config_path: 'Path') -> 'Params':
"Suggestion: provide 'config.json' of the model in the same directory containing model files.")

return Params(
n_vocab = n_vocab,
n_embd = n_embd,
n_mult = n_mult,
n_layer = n_layer,
n_ctx = n_ctx,
n_ff = n_ff,
n_head = n_head,
n_head_kv = n_head_kv,
f_norm_eps = f_norm_eps,
n_vocab = n_vocab,
n_embd = n_embd,
n_mult = n_mult,
n_layer = n_layer,
n_ctx = n_ctx,
n_ff = n_ff,
n_head = n_head,
n_head_kv = n_head_kv,
f_norm_eps = f_norm_eps,
f_rope_scale = f_rope_scale,
)

# LLaMA v2 70B params.json
Expand Down Expand Up @@ -771,6 +774,9 @@ def add_meta_arch(self, params: Params) -> None:
if params.f_rope_freq_base:
self.gguf.add_rope_freq_base(params.f_rope_freq_base)

if params.f_rope_scale:
self.gguf.add_rope_scale_linear(params.f_rope_scale)

if params.ftype:
self.gguf.add_file_type(params.ftype)

Expand Down