Add Optimum inference guide
Browse files
README.md
CHANGED
@@ -2,6 +2,7 @@
|
|
2 |
license: mit
|
3 |
datasets:
|
4 |
- Helsinki-NLP/tatoeba_mt
|
|
|
5 |
language:
|
6 |
- ja
|
7 |
- ko
|
@@ -23,9 +24,7 @@ This project is detailed on the [Github repo](https://github.com/sappho192/ffxiv
|
|
23 |
|
24 |
# Usage
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
## Inference
|
29 |
|
30 |
```Python
|
31 |
from transformers import(
|
@@ -58,6 +57,55 @@ def translate(text_src):
|
|
58 |
print(translate(text))
|
59 |
```
|
60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
## Training
|
62 |
|
63 |
Check the [training.ipynb](https://huggingface.co/sappho192/ffxiv-ja-ko-translator/blob/main/training.ipynb).
|
|
|
2 |
license: mit
|
3 |
datasets:
|
4 |
- Helsinki-NLP/tatoeba_mt
|
5 |
+
- sappho192/Tatoeba-Challenge-jpn-kor
|
6 |
language:
|
7 |
- ja
|
8 |
- ko
|
|
|
24 |
|
25 |
# Usage
|
26 |
|
27 |
+
## Inference (PyTorch)
|
|
|
|
|
28 |
|
29 |
```Python
|
30 |
from transformers import(
|
|
|
57 |
print(translate(text))
|
58 |
```
|
59 |
|
60 |
+
## Inference (Optimum.OnnxRuntime)
|
61 |
+
Note that current Optimum.OnnxRuntime still requires PyTorch for backend. [[Issue](https://github.com/huggingface/optimum/issues/526)]
|
62 |
+
You can use either [[ONNX](https://huggingface.co/sappho192/ffxiv-ja-ko-translator/tree/main/onnx)] or [[quantized ONNX](https://huggingface.co/sappho192/ffxiv-ja-ko-translator/tree/main/onnxq)] model.
|
63 |
+
|
64 |
+
```Python
|
65 |
+
from transformers import BertJapaneseTokenizer,PreTrainedTokenizerFast
|
66 |
+
from optimum.onnxruntime import ORTModelForSeq2SeqLM
|
67 |
+
from onnxruntime import SessionOptions
|
68 |
+
import torch
|
69 |
+
|
70 |
+
encoder_model_name = "cl-tohoku/bert-base-japanese-v2"
|
71 |
+
decoder_model_name = "skt/kogpt2-base-v2"
|
72 |
+
|
73 |
+
src_tokenizer = BertJapaneseTokenizer.from_pretrained(encoder_model_name)
|
74 |
+
trg_tokenizer = PreTrainedTokenizerFast.from_pretrained(decoder_model_name)
|
75 |
+
|
76 |
+
sess_options = SessionOptions()
|
77 |
+
sess_options.log_severity_level = 3 # mute warnings including CleanUnusedInitializersAndNodeArgs
|
78 |
+
# change subfolder to "onnxq" if you want to use the quantized model
|
79 |
+
model = ORTModelForSeq2SeqLM.from_pretrained("sappho192/ffxiv-ja-ko-translator",
|
80 |
+
sess_options=sess_options, subfolder="onnx")
|
81 |
+
|
82 |
+
texts = [
|
83 |
+
"้ใใ!", # Should be "๋๋ง์ณ!"
|
84 |
+
"ๅใใพใใฆ.", # "๋ฐ๊ฐ์์"
|
85 |
+
"ใใใใใ้กใใใพใ.", # "์ ๋ถํ๋๋ฆฝ๋๋ค."
|
86 |
+
"ใฎใซใฌใกใใทใฅ่จไผๆฆ", # "๊ธธ๊ฐ๋ฉ์ฌ ํ ๋ฒ์ "
|
87 |
+
"ใฎใซใฌใกใใทใฅ่จไผๆฆใซ่กใฃใฆใใพใใไธ็ทใซ่กใใพใใใใ๏ผ", # "๊ธธ๊ฐ๋ฉ์ฌ ํ ๋ฒ์ ์ ๊ฐ๋๋ค. ๊ฐ์ด ๊ฐ์ค๋์?"
|
88 |
+
"ๅคใซใชใใพใใ", # "๋ฐค์ด ๋์์ต๋๋ค"
|
89 |
+
"ใ้ฃฏใ้ฃในใพใใใ." # "์, ์ด์ ์์ฌ๋ ํด๋ณผ๊น์"
|
90 |
+
]
|
91 |
+
|
92 |
+
|
93 |
+
def translate(text_src):
|
94 |
+
embeddings = src_tokenizer(text_src, return_attention_mask=False, return_token_type_ids=False, return_tensors='pt')
|
95 |
+
print(f'Src tokens: {embeddings.data["input_ids"]}')
|
96 |
+
embeddings = {k: v for k, v in embeddings.items()}
|
97 |
+
|
98 |
+
output = model.generate(**embeddings, max_length=500)[0, 1:-1]
|
99 |
+
print(f'Trg tokens: {output}')
|
100 |
+
text_trg = trg_tokenizer.decode(output.cpu())
|
101 |
+
return text_trg
|
102 |
+
|
103 |
+
|
104 |
+
for text in texts:
|
105 |
+
print(translate(text))
|
106 |
+
print()
|
107 |
+
```
|
108 |
+
|
109 |
## Training
|
110 |
|
111 |
Check the [training.ipynb](https://huggingface.co/sappho192/ffxiv-ja-ko-translator/blob/main/training.ipynb).
|