sappho192 commited on
Commit
3a27485
ยท
verified ยท
1 Parent(s): 9247483

Add Optimum inference guide

Browse files
Files changed (1) hide show
  1. README.md +51 -3
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
- Check the [test_eval.ipynb](https://huggingface.co/sappho192/ffxiv-ja-ko-translator/blob/main/test_eval.ipynb) or below section.
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).