-
Notifications
You must be signed in to change notification settings - Fork 926
/
Copy pathtest.py
57 lines (48 loc) · 1.91 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import argparse
from typing import List
import model
import numpy as np
from transformers import AutoModel, AutoTokenizer
def run_torch(bert_model: str, batch: List[str]):
tokenizer = AutoTokenizer.from_pretrained(bert_model)
torch_model = AutoModel.from_pretrained(bert_model)
torch_tokens = tokenizer(batch, return_tensors="pt", padding=True)
torch_forward = torch_model(**torch_tokens)
torch_output = torch_forward.last_hidden_state.detach().numpy()
torch_pooled = torch_forward.pooler_output.detach().numpy()
return torch_output, torch_pooled
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Run a BERT-like model for a batch of text."
)
parser.add_argument(
"--bert-model",
type=str,
default="bert-base-uncased",
help="The model identifier for a BERT-like model from Hugging Face Transformers.",
)
parser.add_argument(
"--mlx-model",
type=str,
default="weights/bert-base-uncased.npz",
help="The path of the stored MLX BERT weights (npz file).",
)
parser.add_argument(
"--text",
nargs="+",
default=["This is an example of BERT working in MLX."],
help="A batch of texts to process. Multiple texts should be separated by spaces.",
)
args = parser.parse_args()
torch_output, torch_pooled = run_torch(args.bert_model, args.text)
mlx_output, mlx_pooled = model.run(args.bert_model, args.mlx_model, args.text)
if torch_pooled is not None and mlx_pooled is not None:
assert np.allclose(
torch_output, mlx_output, rtol=1e-4, atol=1e-5
), "Model output is different"
assert np.allclose(
torch_pooled, mlx_pooled, rtol=1e-4, atol=1e-5
), "Model pooled output is different"
print("Tests pass :)")
else:
print("Pooled outputs were not compared due to one or both being None.")