-
Notifications
You must be signed in to change notification settings - Fork 573
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #184 from kyutai-labs/helium-mlx-import
Helium inference for MLX
- Loading branch information
Showing
5 changed files
with
184 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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 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 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,62 @@ | ||
# Copyright (c) Kyutai, all rights reserved. | ||
# This source code is licensed under the license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
import argparse | ||
import sentencepiece | ||
import huggingface_hub | ||
import mlx.core as mx | ||
from moshi_mlx import models, utils | ||
|
||
|
||
def main(): | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("--tokenizer", type=str) | ||
parser.add_argument("--weights", type=str) | ||
parser.add_argument("--nsteps", type=int, default=20) | ||
parser.add_argument("--hf-repo", type=str, default="kyutai/helium-1-preview-2b-mlx") | ||
parser.add_argument("--prompt", type=str, default="Aujourd'hui, il est temps") | ||
parser.add_argument("--verbose", action="store_true") | ||
args = parser.parse_args() | ||
|
||
weights = args.weights | ||
if weights is None: | ||
weights = huggingface_hub.hf_hub_download( | ||
args.hf_repo, "helium-1-preview-2b-bf16.safetensors" | ||
) | ||
tokenizer = args.tokenizer | ||
if tokenizer is None: | ||
tokenizer = huggingface_hub.hf_hub_download( | ||
args.hf_repo, "tokenizer_spm_48k_multi6_2.model" | ||
) | ||
|
||
mx.random.seed(299792458) | ||
lm_config = models.config_helium_1_preview_2b() | ||
model = models.Lm(lm_config) | ||
model.set_dtype(mx.bfloat16) | ||
model.load_weights(weights, strict=True) | ||
sampler = utils.Sampler() | ||
tokenizer = sentencepiece.SentencePieceProcessor(tokenizer) # type: ignore | ||
if args.verbose: | ||
print("prompt", args.prompt) | ||
else: | ||
print(args.prompt, end="", flush=True) | ||
prompt_tokens = tokenizer.encode(args.prompt) # type: ignore | ||
token = mx.array([[1] + prompt_tokens]) | ||
for step_idx in range(args.nsteps): | ||
logits = model(token) | ||
token, _ = sampler(logits[:, -1]) | ||
text_token = token.item() | ||
_text = tokenizer.id_to_piece(text_token) # type: ignore | ||
_text = _text.replace("▁", " ") | ||
_text = _text.replace("<0x0A>", "\n") | ||
if args.verbose: | ||
print(step_idx, token, _text) | ||
else: | ||
print(_text, end="", flush=True) | ||
token = token[None] | ||
print() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains 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 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,73 @@ | ||
# Copyright (c) Kyutai, all rights reserved. | ||
# This source code is licensed under the license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
import argparse | ||
import torch | ||
from pathlib import Path | ||
from safetensors import safe_open | ||
from safetensors.torch import save_file | ||
from huggingface_hub import hf_hub_download | ||
|
||
|
||
def import_model(in_path: Path, out_path: Path, silent: bool = False) -> None: | ||
with safe_open(in_path, framework="pt", device="cpu") as f: | ||
tensors = { key: f.get_tensor(key) for key in f.keys() } | ||
model = { | ||
"text_emb.weight": tensors["model.embed_tokens.weight"], | ||
"text_linear.weight": tensors["lm_head.weight"], | ||
"out_norm.weight": tensors["model.norm.weight"], | ||
} | ||
n_layers = -1 | ||
for key in tensors.keys(): | ||
if key.startswith("model.layers."): | ||
layer_idx = int(key.split(".")[2]) | ||
n_layers = max(layer_idx, n_layers) | ||
n_layers += 1 | ||
if not silent: | ||
print(f"found {n_layers} layers") | ||
for layer_idx in range(n_layers): | ||
dst_prefix = f"transformer.layers.{layer_idx}." | ||
src_prefix = f"model.layers.{layer_idx}." | ||
_model = { | ||
"norm1.weight": "input_layernorm.weight", | ||
"norm2.weight": "post_attention_layernorm.weight", | ||
"self_attn.out_proj.weight": "self_attn.o_proj.weight", | ||
"gating.linear_out.weight": "mlp.down_proj.weight", | ||
} | ||
for dst, src in _model.items(): | ||
model[dst_prefix + dst] = tensors[src_prefix + src] | ||
gate_proj = tensors[src_prefix + "mlp.gate_proj.weight"] | ||
up_proj = tensors[src_prefix + "mlp.up_proj.weight"] | ||
linear_in = torch.cat([gate_proj, up_proj], dim=0) | ||
model[dst_prefix + "gating.linear_in.weight"] = linear_in | ||
q = tensors[src_prefix + "self_attn.q_proj.weight"] | ||
k = tensors[src_prefix + "self_attn.k_proj.weight"] | ||
v = tensors[src_prefix + "self_attn.v_proj.weight"] | ||
in_proj = torch.cat([q, k, v], dim=0) | ||
model[dst_prefix + "self_attn.in_proj.weight"] = in_proj | ||
|
||
save_file(model, out_path) | ||
|
||
|
||
def main(): | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("--checkpoint", type=str, default="kyutai/helium-1-preview-2b", help="the transformers checkpoint to import") | ||
parser.add_argument("--out", type=str, help="the mlx safetensors file to generate") | ||
parser.add_argument( | ||
"-s", "--silent", action="store_true", help="Only prints the checkpoint name" | ||
) | ||
args = parser.parse_args() | ||
|
||
ckpt_path = Path(args.checkpoint) | ||
if not ckpt_path.exists(): | ||
ckpt_path = hf_hub_download(repo_id=args.checkpoint, filename="model.safetensors") | ||
out_path = Path(args.out) | ||
if not out_path.exists(): | ||
import_model(ckpt_path, out_path, silent=args.silent) | ||
print(out_path) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() | ||
|