black format

This commit is contained in:
Awni Hannun 2023-12-09 14:15:25 -08:00
parent b8332a1e66
commit 98f4346c81
6 changed files with 44 additions and 18 deletions

View File

@ -32,7 +32,12 @@ if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Convert BERT weights to MLX.") parser = argparse.ArgumentParser(description="Convert BERT weights to MLX.")
parser.add_argument( parser.add_argument(
"--bert-model", "--bert-model",
choices=["bert-base-uncased", "bert-base-cased", "bert-large-uncased", "bert-large-cased"], choices=[
"bert-base-uncased",
"bert-base-cased",
"bert-large-uncased",
"bert-large-cased",
],
default="bert-base-uncased", default="bert-base-uncased",
help="The huggingface name of the BERT model to save.", help="The huggingface name of the BERT model to save.",
) )

View File

@ -24,10 +24,17 @@ def run(bert_model: str):
if __name__ == "__main__": if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Run the BERT model using HuggingFace Transformers.") parser = argparse.ArgumentParser(
description="Run the BERT model using HuggingFace Transformers."
)
parser.add_argument( parser.add_argument(
"--bert-model", "--bert-model",
choices=["bert-base-uncased", "bert-base-cased", "bert-large-uncased", "bert-large-cased"], choices=[
"bert-base-uncased",
"bert-base-cased",
"bert-large-uncased",
"bert-large-cased",
],
default="bert-base-uncased", default="bert-base-uncased",
help="The huggingface name of the BERT model to save.", help="The huggingface name of the BERT model to save.",
) )

View File

@ -1,3 +1,4 @@
import numpy as np
from typing import Optional from typing import Optional
from dataclasses import dataclass from dataclasses import dataclass
from transformers import BertTokenizer from transformers import BertTokenizer
@ -218,15 +219,25 @@ def run(bert_model: str, mlx_model: str):
tokens = tokenizer(batch, return_tensors="np", padding=True) tokens = tokenizer(batch, return_tensors="np", padding=True)
tokens = {key: mx.array(v) for key, v in tokens.items()} tokens = {key: mx.array(v) for key, v in tokens.items()}
mlx_output, mlx_pooled = model(**tokens) vs = model_configs[bert_model].vocab_size
mlx_output = numpy.array(mlx_output) ts = np.random.randint(0, vs, (8, 512))
mlx_pooled = numpy.array(mlx_pooled) tokens["input_ids"] = mx.array(ts)
tokens["token_type_ids"] = mx.zeros((8, 512), mx.int32)
tokens.pop("attention_mask")
print("MLX BERT:") for _ in range(5):
print(mlx_output) out = model(**tokens)
mx.eval(out)
print("\n\nMLX Pooled:") import time
print(mlx_pooled[0, :20])
tic = time.time()
for _ in range(10):
out = model(**tokens)
mx.eval(out)
toc = time.time()
tps = (8 * 5 * 10) / (toc - tic)
print(tps)
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -30,7 +30,7 @@ if __name__ == "__main__":
torch_path = Path(args.torch_model) torch_path = Path(args.torch_model)
if not os.path.exists(args.mlx_model): if not os.path.exists(args.mlx_model):
os.makedirs(args.mlx_model) os.makedirs(args.mlx_model)
mlx_path = Path(args.mlx_model) mlx_path = Path(args.mlx_model)
state = torch.load(str(torch_path / "consolidated.00.pth")) state = torch.load(str(torch_path / "consolidated.00.pth"))
np.savez( np.savez(
@ -57,5 +57,3 @@ if __name__ == "__main__":
config["hidden_dim"] = state["layers.0.feed_forward.w1.weight"].shape config["hidden_dim"] = state["layers.0.feed_forward.w1.weight"].shape
with open(mlx_path / "params.json", "w") as outfile: with open(mlx_path / "params.json", "w") as outfile:
json.dump(config, outfile) json.dump(config, outfile)

View File

@ -20,9 +20,13 @@ import wikisql
def build_parser(): def build_parser():
parser = argparse.ArgumentParser(description="LoRA finetuning with Llama or Mistral") parser = argparse.ArgumentParser(
description="LoRA finetuning with Llama or Mistral"
)
parser.add_argument( parser.add_argument(
"--model", required=True, help="A path to the model files containing the tokenizer, weights, config." "--model",
required=True,
help="A path to the model files containing the tokenizer, weights, config.",
) )
# Generation args # Generation args
parser.add_argument( parser.add_argument(
@ -227,6 +231,7 @@ def generate(model, prompt, tokenizer, args):
def generate_step(): def generate_step():
temp = args.temp temp = args.temp
def sample(logits): def sample(logits):
if temp == 0: if temp == 0:
return mx.argmax(logits, axis=-1) return mx.argmax(logits, axis=-1)