Upload 3 files
Browse files- all-mpnet-base-v2.py +47 -0
- tokenizer.json +0 -0
- tokenizer_config.json +1 -0
all-mpnet-base-v2.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from transformers import AutoTokenizer, AutoModel
|
| 3 |
+
|
| 4 |
+
from executorch.backends.xnnpack.partition.xnnpack_partitioner import XnnpackPartitioner
|
| 5 |
+
from executorch.exir import to_edge_transform_and_lower
|
| 6 |
+
from torch.export import export, Dim
|
| 7 |
+
from executorch.runtime import Runtime
|
| 8 |
+
|
| 9 |
+
model_name = f"all-mpnet-base-v2.pte"
|
| 10 |
+
tokenizer = AutoTokenizer.from_pretrained("sentence-transformers/all-mpnet-base-v2")
|
| 11 |
+
model = AutoModel.from_pretrained("sentence-transformers/all-mpnet-base-v2")
|
| 12 |
+
model.eval()
|
| 13 |
+
|
| 14 |
+
sentences = ["This is an example sentence"]
|
| 15 |
+
encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors="pt")
|
| 16 |
+
executorch_input = (encoded_input["input_ids"], encoded_input["attention_mask"])
|
| 17 |
+
|
| 18 |
+
# Export the model
|
| 19 |
+
executorch_program = to_edge_transform_and_lower(
|
| 20 |
+
export(
|
| 21 |
+
model,
|
| 22 |
+
executorch_input,
|
| 23 |
+
dynamic_shapes={
|
| 24 |
+
"input_ids": {
|
| 25 |
+
1: Dim("tokens", min=1, max=384),
|
| 26 |
+
},
|
| 27 |
+
"attention_mask": {
|
| 28 |
+
1: Dim("tokens", min=1, max=384),
|
| 29 |
+
},
|
| 30 |
+
},
|
| 31 |
+
),
|
| 32 |
+
partitioner=[XnnpackPartitioner()],
|
| 33 |
+
).to_executorch()
|
| 34 |
+
|
| 35 |
+
# Save the model
|
| 36 |
+
with open(model_name, "wb") as file:
|
| 37 |
+
file.write(executorch_program.buffer)
|
| 38 |
+
|
| 39 |
+
# Check the model output
|
| 40 |
+
eager_reference_output = model(**encoded_input)
|
| 41 |
+
|
| 42 |
+
runtime = Runtime.get()
|
| 43 |
+
program = runtime.load_program(model_name)
|
| 44 |
+
method = program.load_method("forward")
|
| 45 |
+
output = method.execute(executorch_input)
|
| 46 |
+
|
| 47 |
+
print(torch.allclose(output[0], eager_reference_output[0], rtol=1e-3, atol=1e-5))
|
tokenizer.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
tokenizer_config.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
{"do_lower_case": true, "bos_token": "<s>", "eos_token": "</s>", "sep_token": "</s>", "cls_token": "<s>", "unk_token": "[UNK]", "pad_token": "<pad>", "mask_token": "<mask>", "tokenize_chinese_chars": true, "strip_accents": null, "model_max_length": 512, "special_tokens_map_file": null, "name_or_path": "microsoft/mpnet-base", "tokenizer_class": "MPNetTokenizer"}
|