Spaces:
Running
Running
Add support for SMALL100 and update examples
Browse files- examples/Alpaca-Lora.sh +0 -2
- examples/FlanT5-large.sh +0 -2
- examples/LLaMA.sh +0 -2
- examples/README.md +2 -0
- examples/Vicuna.sh +0 -2
- examples/m2m100-1.2B.sh +0 -2
- examples/m2m100-1.2B_2GPUS.sh +0 -2
- examples/m2m100-12B_8bit.sh +0 -2
- examples/m2m100-12B_fp16.sh +0 -2
- examples/mbart.sh +1 -3
- examples/nllb200-moe-54B_1GPU_4bits.sh +0 -3
- examples/nllb200-moe-54B_1GPU_8bits.sh +0 -2
- examples/nllb200_3B_8bit.sh +0 -2
- examples/nllb200_3B_fp16.sh +0 -2
- examples/opusMT.sh +2 -4
- examples/small100.sh +0 -2
- model.py +6 -1
- sample_text/en2es.m2m100_1.2B.json +24 -24
- sample_text/en2es.mbart50_m2m.json +60 -0
- sample_text/en2es.nllb-200_3B.json +60 -0
- sample_text/en2es.opusMT.json +60 -0
- sample_text/en2es.small100.json +60 -0
- sample_text/en2es.translation.m2m100_1.2B.txt +2 -2
- sample_text/en2es.translation.mbart50_m2m.txt +0 -0
- sample_text/en2es.translation.nllb-200_3B.txt +0 -0
- sample_text/en2es.translation.opusMT.txt +0 -0
- sample_text/en2es.translation.small100.txt +0 -0
- translate.py +5 -1
- utils/tokenization_small100.py +393 -0
examples/Alpaca-Lora.sh
CHANGED
@@ -1,8 +1,6 @@
|
|
1 |
# Run Alpaca-Lora (A LoRA model) model on sample text using prompting
|
2 |
# We need to set the base model with --model_name and the LoRA weights with --lora_weights_name_or_path
|
3 |
|
4 |
-
cd ..
|
5 |
-
|
6 |
python3 translate.py \
|
7 |
--sentences_path sample_text/en.txt \
|
8 |
--output_path sample_text/en2es.AlpacaLora.translation.txt \
|
|
|
1 |
# Run Alpaca-Lora (A LoRA model) model on sample text using prompting
|
2 |
# We need to set the base model with --model_name and the LoRA weights with --lora_weights_name_or_path
|
3 |
|
|
|
|
|
4 |
python3 translate.py \
|
5 |
--sentences_path sample_text/en.txt \
|
6 |
--output_path sample_text/en2es.AlpacaLora.translation.txt \
|
examples/FlanT5-large.sh
CHANGED
@@ -1,7 +1,5 @@
|
|
1 |
# Run Flan-T5 model on sample text using promting
|
2 |
|
3 |
-
cd ..
|
4 |
-
|
5 |
python3 translate.py \
|
6 |
--sentences_path sample_text/en.txt \
|
7 |
--output_path sample_text/en2es.FlanT5.translation.txt \
|
|
|
1 |
# Run Flan-T5 model on sample text using promting
|
2 |
|
|
|
|
|
3 |
python3 translate.py \
|
4 |
--sentences_path sample_text/en.txt \
|
5 |
--output_path sample_text/en2es.FlanT5.translation.txt \
|
examples/LLaMA.sh
CHANGED
@@ -1,7 +1,5 @@
|
|
1 |
# Run LLaMA65B model on sample text using prompting
|
2 |
|
3 |
-
cd ..
|
4 |
-
|
5 |
python3 translate.py \
|
6 |
--sentences_path sample_text/en.txt \
|
7 |
--output_path sample_text/en2es.LLaMA.translation.txt \
|
|
|
1 |
# Run LLaMA65B model on sample text using prompting
|
2 |
|
|
|
|
|
3 |
python3 translate.py \
|
4 |
--sentences_path sample_text/en.txt \
|
5 |
--output_path sample_text/en2es.LLaMA.translation.txt \
|
examples/README.md
CHANGED
@@ -3,6 +3,8 @@
|
|
3 |
This folder contains examples of how to use Easy-Translate with different models and configurations.
|
4 |
You can adapt these examples to your own use cases. If you have any questions, please feel free to open an issue.
|
5 |
|
|
|
|
|
6 |
### MT Models
|
7 |
|
8 |
```bash
|
|
|
3 |
This folder contains examples of how to use Easy-Translate with different models and configurations.
|
4 |
You can adapt these examples to your own use cases. If you have any questions, please feel free to open an issue.
|
5 |
|
6 |
+
> Run the examples from the root of the Easy-Translate repository as follows: `sh examples/<example>.sh`
|
7 |
+
|
8 |
### MT Models
|
9 |
|
10 |
```bash
|
examples/Vicuna.sh
CHANGED
@@ -4,8 +4,6 @@
|
|
4 |
# lmsys/vicuna-13b-v1.3
|
5 |
# lmsys/vicuna-7b-v1.3
|
6 |
|
7 |
-
cd ..
|
8 |
-
|
9 |
python3 translate.py \
|
10 |
--sentences_path sample_text/en.txt \
|
11 |
--output_path sample_text/en2es.Vicuna33B.translation.txt \
|
|
|
4 |
# lmsys/vicuna-13b-v1.3
|
5 |
# lmsys/vicuna-7b-v1.3
|
6 |
|
|
|
|
|
7 |
python3 translate.py \
|
8 |
--sentences_path sample_text/en.txt \
|
9 |
--output_path sample_text/en2es.Vicuna33B.translation.txt \
|
examples/m2m100-1.2B.sh
CHANGED
@@ -1,7 +1,5 @@
|
|
1 |
# Run M2M100-1.2B model on sample text. One GPU, default precision.
|
2 |
|
3 |
-
cd ..
|
4 |
-
|
5 |
python3 translate.py \
|
6 |
--sentences_path sample_text/en.txt \
|
7 |
--output_path sample_text/en2es.translation.m2m100_1.2B.txt \
|
|
|
1 |
# Run M2M100-1.2B model on sample text. One GPU, default precision.
|
2 |
|
|
|
|
|
3 |
python3 translate.py \
|
4 |
--sentences_path sample_text/en.txt \
|
5 |
--output_path sample_text/en2es.translation.m2m100_1.2B.txt \
|
examples/m2m100-1.2B_2GPUS.sh
CHANGED
@@ -1,7 +1,5 @@
|
|
1 |
# Run M2M100-1.2B model on sample text. Multi GPU, default precision.
|
2 |
|
3 |
-
cd ..
|
4 |
-
|
5 |
accelerate launch --multi_gpu --num_processes 2 --num_machines 1 translate.py \
|
6 |
--sentences_path sample_text/en.txt \
|
7 |
--output_path sample_text/en2es.translation.m2m100_1.2B.txt \
|
|
|
1 |
# Run M2M100-1.2B model on sample text. Multi GPU, default precision.
|
2 |
|
|
|
|
|
3 |
accelerate launch --multi_gpu --num_processes 2 --num_machines 1 translate.py \
|
4 |
--sentences_path sample_text/en.txt \
|
5 |
--output_path sample_text/en2es.translation.m2m100_1.2B.txt \
|
examples/m2m100-12B_8bit.sh
CHANGED
@@ -3,8 +3,6 @@
|
|
3 |
# with a lot of RAM, running the model in FP16 should be faster and produce sighly better results,
|
4 |
# see examples/m2m100-12B_fp16.sh
|
5 |
|
6 |
-
cd ..
|
7 |
-
|
8 |
python3 translate.py \
|
9 |
--sentences_path sample_text/en.txt \
|
10 |
--output_path sample_text/en2es.translation.m2m100_12B.txt \
|
|
|
3 |
# with a lot of RAM, running the model in FP16 should be faster and produce sighly better results,
|
4 |
# see examples/m2m100-12B_fp16.sh
|
5 |
|
|
|
|
|
6 |
python3 translate.py \
|
7 |
--sentences_path sample_text/en.txt \
|
8 |
--output_path sample_text/en2es.translation.m2m100_12B.txt \
|
examples/m2m100-12B_fp16.sh
CHANGED
@@ -1,8 +1,6 @@
|
|
1 |
# Run M2M100-1.2B model on sample text. We use FP16 precision, which requires a GPU with a lot of VRAM (i.e NVIDIA A100)
|
2 |
# For running this model in customer grade GPUs, use 8-bit quantization, see examples/m2m100-12B_8bit.sh
|
3 |
|
4 |
-
cd ..
|
5 |
-
|
6 |
python3 translate.py \
|
7 |
--sentences_path sample_text/en.txt \
|
8 |
--output_path sample_text/en2es.translation.m2m100_12B.txt \
|
|
|
1 |
# Run M2M100-1.2B model on sample text. We use FP16 precision, which requires a GPU with a lot of VRAM (i.e NVIDIA A100)
|
2 |
# For running this model in customer grade GPUs, use 8-bit quantization, see examples/m2m100-12B_8bit.sh
|
3 |
|
|
|
|
|
4 |
python3 translate.py \
|
5 |
--sentences_path sample_text/en.txt \
|
6 |
--output_path sample_text/en2es.translation.m2m100_12B.txt \
|
examples/mbart.sh
CHANGED
@@ -1,10 +1,8 @@
|
|
1 |
# Run Mbart-many-to-many model on sample text.
|
2 |
|
3 |
-
cd ..
|
4 |
-
|
5 |
python3 translate.py \
|
6 |
--sentences_path sample_text/en.txt \
|
7 |
-
--output_path sample_text/en2es.translation.
|
8 |
--source_lang en_XX \
|
9 |
--target_lang es_XX \
|
10 |
--model_name facebook/mbart-large-50-many-to-many-mmt
|
|
|
1 |
# Run Mbart-many-to-many model on sample text.
|
2 |
|
|
|
|
|
3 |
python3 translate.py \
|
4 |
--sentences_path sample_text/en.txt \
|
5 |
+
--output_path sample_text/en2es.translation.mbart50_m2m.txt \
|
6 |
--source_lang en_XX \
|
7 |
--target_lang es_XX \
|
8 |
--model_name facebook/mbart-large-50-many-to-many-mmt
|
examples/nllb200-moe-54B_1GPU_4bits.sh
CHANGED
@@ -2,9 +2,6 @@
|
|
2 |
# 4-bit quantization to reduce the required VRAM. Still it might not fit on a single GPU, so we also use
|
3 |
# the --force_auto_device_map flag that will offload the model parameters that don't fit on the GPU to the CPU.
|
4 |
|
5 |
-
|
6 |
-
cd ..
|
7 |
-
|
8 |
python3 translate.py \
|
9 |
--sentences_path sample_text/en.txt \
|
10 |
--output_path sample_text/en2es.translation.nllb200-moe-54B.txt \
|
|
|
2 |
# 4-bit quantization to reduce the required VRAM. Still it might not fit on a single GPU, so we also use
|
3 |
# the --force_auto_device_map flag that will offload the model parameters that don't fit on the GPU to the CPU.
|
4 |
|
|
|
|
|
|
|
5 |
python3 translate.py \
|
6 |
--sentences_path sample_text/en.txt \
|
7 |
--output_path sample_text/en2es.translation.nllb200-moe-54B.txt \
|
examples/nllb200-moe-54B_1GPU_8bits.sh
CHANGED
@@ -3,8 +3,6 @@
|
|
3 |
# the --force_auto_device_map flag that will offload the model parameters that don't fit on the GPU to the CPU.
|
4 |
# If 8-bit quantization is not enough, you can use 4-bit quantization, see examples/nllb200-moe-54B_1GPU_4bits.sh
|
5 |
|
6 |
-
cd ..
|
7 |
-
|
8 |
python3 translate.py \
|
9 |
--sentences_path sample_text/en.txt \
|
10 |
--output_path sample_text/en2es.translation.nllb200-moe-54B.txt \
|
|
|
3 |
# the --force_auto_device_map flag that will offload the model parameters that don't fit on the GPU to the CPU.
|
4 |
# If 8-bit quantization is not enough, you can use 4-bit quantization, see examples/nllb200-moe-54B_1GPU_4bits.sh
|
5 |
|
|
|
|
|
6 |
python3 translate.py \
|
7 |
--sentences_path sample_text/en.txt \
|
8 |
--output_path sample_text/en2es.translation.nllb200-moe-54B.txt \
|
examples/nllb200_3B_8bit.sh
CHANGED
@@ -1,8 +1,6 @@
|
|
1 |
# Run NLLB200-3B model on sample text. We use FP16 precision, which requires a GPU with a lot of VRAM
|
2 |
# For running this model in GPUs with less VRAM, use 8-bit quantization, see examples/nllb200_3B_8bit.sh
|
3 |
|
4 |
-
cd ..
|
5 |
-
|
6 |
python3 translate.py \
|
7 |
--sentences_path sample_text/en.txt \
|
8 |
--output_path sample_text/en2es.translation.nllb-200_3B.txt \
|
|
|
1 |
# Run NLLB200-3B model on sample text. We use FP16 precision, which requires a GPU with a lot of VRAM
|
2 |
# For running this model in GPUs with less VRAM, use 8-bit quantization, see examples/nllb200_3B_8bit.sh
|
3 |
|
|
|
|
|
4 |
python3 translate.py \
|
5 |
--sentences_path sample_text/en.txt \
|
6 |
--output_path sample_text/en2es.translation.nllb-200_3B.txt \
|
examples/nllb200_3B_fp16.sh
CHANGED
@@ -1,8 +1,6 @@
|
|
1 |
# Run NLLB200-3B model on sample text. We use FP16 precision, which requires a GPU with a lot of VRAM
|
2 |
# For running this model in GPUs with less VRAM, use 8-bit quantization, see examples/nllb200_3B_8bit.sh
|
3 |
|
4 |
-
cd ..
|
5 |
-
|
6 |
python3 translate.py \
|
7 |
--sentences_path sample_text/en.txt \
|
8 |
--output_path sample_text/en2es.translation.nllb-200_3B.txt \
|
|
|
1 |
# Run NLLB200-3B model on sample text. We use FP16 precision, which requires a GPU with a lot of VRAM
|
2 |
# For running this model in GPUs with less VRAM, use 8-bit quantization, see examples/nllb200_3B_8bit.sh
|
3 |
|
|
|
|
|
4 |
python3 translate.py \
|
5 |
--sentences_path sample_text/en.txt \
|
6 |
--output_path sample_text/en2es.translation.nllb-200_3B.txt \
|
examples/opusMT.sh
CHANGED
@@ -1,8 +1,6 @@
|
|
1 |
# Run OpusMT model on sample text.
|
2 |
|
3 |
-
cd ..
|
4 |
-
|
5 |
python3 translate.py \
|
6 |
--sentences_path sample_text/en.txt \
|
7 |
-
--output_path sample_text/en2es.translation.
|
8 |
-
--model_name Helsinki-NLP/opus-mt-es
|
|
|
1 |
# Run OpusMT model on sample text.
|
2 |
|
|
|
|
|
3 |
python3 translate.py \
|
4 |
--sentences_path sample_text/en.txt \
|
5 |
+
--output_path sample_text/en2es.translation.opusMT.txt \
|
6 |
+
--model_name Helsinki-NLP/opus-mt-en-es
|
examples/small100.sh
CHANGED
@@ -1,7 +1,5 @@
|
|
1 |
# Run SMALL100 model on sample text.
|
2 |
|
3 |
-
cd ..
|
4 |
-
|
5 |
python3 translate.py \
|
6 |
--sentences_path sample_text/en.txt \
|
7 |
--output_path sample_text/en2es.translation.small100.txt \
|
|
|
1 |
# Run SMALL100 model on sample text.
|
2 |
|
|
|
|
|
3 |
python3 translate.py \
|
4 |
--sentences_path sample_text/en.txt \
|
5 |
--output_path sample_text/en2es.translation.small100.txt \
|
model.py
CHANGED
@@ -2,7 +2,6 @@ from transformers import (
|
|
2 |
AutoConfig,
|
3 |
AutoModelForCausalLM,
|
4 |
AutoModelForSeq2SeqLM,
|
5 |
-
AutoTokenizer,
|
6 |
BitsAndBytesConfig,
|
7 |
PreTrainedModel,
|
8 |
PreTrainedTokenizerBase,
|
@@ -84,6 +83,12 @@ def load_model_for_inference(
|
|
84 |
torch_dtype if torch_dtype in ["auto", None] else getattr(torch, torch_dtype)
|
85 |
)
|
86 |
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
tokenizer: PreTrainedTokenizerBase = AutoTokenizer.from_pretrained(
|
88 |
weights_path,
|
89 |
add_eos_token=True,
|
|
|
2 |
AutoConfig,
|
3 |
AutoModelForCausalLM,
|
4 |
AutoModelForSeq2SeqLM,
|
|
|
5 |
BitsAndBytesConfig,
|
6 |
PreTrainedModel,
|
7 |
PreTrainedTokenizerBase,
|
|
|
83 |
torch_dtype if torch_dtype in ["auto", None] else getattr(torch, torch_dtype)
|
84 |
)
|
85 |
|
86 |
+
if "small100" in weights_path:
|
87 |
+
print(f"Loading custom small100 tokenizer for utils.tokenization_small100")
|
88 |
+
from utils.tokenization_small100 import SMALL100Tokenizer as AutoTokenizer
|
89 |
+
else:
|
90 |
+
from transformers import AutoTokenizer
|
91 |
+
|
92 |
tokenizer: PreTrainedTokenizerBase = AutoTokenizer.from_pretrained(
|
93 |
weights_path,
|
94 |
add_eos_token=True,
|
sample_text/en2es.m2m100_1.2B.json
CHANGED
@@ -1,12 +1,12 @@
|
|
1 |
{
|
2 |
"path": "sample_text/en2es.translation.m2m100_1.2B.txt",
|
3 |
"sacrebleu": {
|
4 |
-
"score": 32.
|
5 |
"counts": [
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
],
|
11 |
"totals": [
|
12 |
30102,
|
@@ -15,28 +15,28 @@
|
|
15 |
27106
|
16 |
],
|
17 |
"precisions": [
|
18 |
-
63.
|
19 |
-
39.
|
20 |
-
26.
|
21 |
-
19.
|
22 |
],
|
23 |
"bp": 0.9553495084330463,
|
24 |
"sys_len": 30102,
|
25 |
"ref_len": 31477
|
26 |
},
|
27 |
"rouge": {
|
28 |
-
"rouge1": 0.
|
29 |
-
"rouge2": 0.
|
30 |
-
"rougeL": 0.
|
31 |
-
"rougeLsum": 0.
|
32 |
},
|
33 |
"bleu": {
|
34 |
-
"bleu": 0.
|
35 |
"precisions": [
|
36 |
-
0.
|
37 |
-
0.
|
38 |
-
0.
|
39 |
-
0.
|
40 |
],
|
41 |
"brevity_penalty": 0.9553495084330461,
|
42 |
"length_ratio": 0.9563173110525145,
|
@@ -44,17 +44,17 @@
|
|
44 |
"reference_length": 31477
|
45 |
},
|
46 |
"meteor": {
|
47 |
-
"meteor": 0.
|
48 |
},
|
49 |
"ter": {
|
50 |
-
"score": 56.
|
51 |
-
"num_edits":
|
52 |
"ref_length": 28314.0
|
53 |
},
|
54 |
"bert_score": {
|
55 |
-
"precision": 0.
|
56 |
-
"recall": 0.
|
57 |
-
"f1": 0.
|
58 |
"hashcode": "microsoft/deberta-xlarge-mnli_L40_no-idf_version=0.3.12(hug_trans=4.30.0.dev0)_fast-tokenizer"
|
59 |
}
|
60 |
}
|
|
|
1 |
{
|
2 |
"path": "sample_text/en2es.translation.m2m100_1.2B.txt",
|
3 |
"sacrebleu": {
|
4 |
+
"score": 32.15019121336132,
|
5 |
"counts": [
|
6 |
+
19161,
|
7 |
+
11393,
|
8 |
+
7559,
|
9 |
+
5187
|
10 |
],
|
11 |
"totals": [
|
12 |
30102,
|
|
|
15 |
27106
|
16 |
],
|
17 |
"precisions": [
|
18 |
+
63.653577835359776,
|
19 |
+
39.148512129750536,
|
20 |
+
26.896527184742386,
|
21 |
+
19.13598465284439
|
22 |
],
|
23 |
"bp": 0.9553495084330463,
|
24 |
"sys_len": 30102,
|
25 |
"ref_len": 31477
|
26 |
},
|
27 |
"rouge": {
|
28 |
+
"rouge1": 0.602850264212381,
|
29 |
+
"rouge2": 0.38933908070548573,
|
30 |
+
"rougeL": 0.5520611983269869,
|
31 |
+
"rougeLsum": 0.5520263175872622
|
32 |
},
|
33 |
"bleu": {
|
34 |
+
"bleu": 0.32150191213361295,
|
35 |
"precisions": [
|
36 |
+
0.6365357783535978,
|
37 |
+
0.39148512129750535,
|
38 |
+
0.26896527184742386,
|
39 |
+
0.1913598465284439
|
40 |
],
|
41 |
"brevity_penalty": 0.9553495084330461,
|
42 |
"length_ratio": 0.9563173110525145,
|
|
|
44 |
"reference_length": 31477
|
45 |
},
|
46 |
"meteor": {
|
47 |
+
"meteor": 0.559313673095486
|
48 |
},
|
49 |
"ter": {
|
50 |
+
"score": 56.87292505474324,
|
51 |
+
"num_edits": 16103,
|
52 |
"ref_length": 28314.0
|
53 |
},
|
54 |
"bert_score": {
|
55 |
+
"precision": 0.8262975588142872,
|
56 |
+
"recall": 0.8192616358399392,
|
57 |
+
"f1": 0.8223584635257721,
|
58 |
"hashcode": "microsoft/deberta-xlarge-mnli_L40_no-idf_version=0.3.12(hug_trans=4.30.0.dev0)_fast-tokenizer"
|
59 |
}
|
60 |
}
|
sample_text/en2es.mbart50_m2m.json
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"path": "sample_text/en2es.translation.mbart50_m2m.txt",
|
3 |
+
"sacrebleu": {
|
4 |
+
"score": 34.312443769569654,
|
5 |
+
"counts": [
|
6 |
+
19844,
|
7 |
+
12131,
|
8 |
+
8189,
|
9 |
+
5651
|
10 |
+
],
|
11 |
+
"totals": [
|
12 |
+
31140,
|
13 |
+
30140,
|
14 |
+
29140,
|
15 |
+
28140
|
16 |
+
],
|
17 |
+
"precisions": [
|
18 |
+
63.72511239563263,
|
19 |
+
40.24883875248839,
|
20 |
+
28.10226492793411,
|
21 |
+
20.081734186211797
|
22 |
+
],
|
23 |
+
"bp": 0.9892362544138555,
|
24 |
+
"sys_len": 31140,
|
25 |
+
"ref_len": 31477
|
26 |
+
},
|
27 |
+
"rouge": {
|
28 |
+
"rouge1": 0.6159604817857698,
|
29 |
+
"rouge2": 0.4073511703525804,
|
30 |
+
"rougeL": 0.5660722931147588,
|
31 |
+
"rougeLsum": 0.5660998611201875
|
32 |
+
},
|
33 |
+
"bleu": {
|
34 |
+
"bleu": 0.3431244376956965,
|
35 |
+
"precisions": [
|
36 |
+
0.6372511239563262,
|
37 |
+
0.40248838752488386,
|
38 |
+
0.2810226492793411,
|
39 |
+
0.20081734186211797
|
40 |
+
],
|
41 |
+
"brevity_penalty": 0.9892362544138555,
|
42 |
+
"length_ratio": 0.9892937700543254,
|
43 |
+
"translation_length": 31140,
|
44 |
+
"reference_length": 31477
|
45 |
+
},
|
46 |
+
"meteor": {
|
47 |
+
"meteor": 0.5778973538427097
|
48 |
+
},
|
49 |
+
"ter": {
|
50 |
+
"score": 56.014692378328746,
|
51 |
+
"num_edits": 15860,
|
52 |
+
"ref_length": 28314.0
|
53 |
+
},
|
54 |
+
"bert_score": {
|
55 |
+
"precision": 0.8280336964726448,
|
56 |
+
"recall": 0.8266492820978165,
|
57 |
+
"f1": 0.8269554697275162,
|
58 |
+
"hashcode": "microsoft/deberta-xlarge-mnli_L40_no-idf_version=0.3.12(hug_trans=4.30.0.dev0)_fast-tokenizer"
|
59 |
+
}
|
60 |
+
}
|
sample_text/en2es.nllb-200_3B.json
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"path": "sample_text/en2es.translation.nllb-200_3B.txt",
|
3 |
+
"sacrebleu": {
|
4 |
+
"score": 36.1806558580766,
|
5 |
+
"counts": [
|
6 |
+
20331,
|
7 |
+
12688,
|
8 |
+
8717,
|
9 |
+
6126
|
10 |
+
],
|
11 |
+
"totals": [
|
12 |
+
31189,
|
13 |
+
30189,
|
14 |
+
29189,
|
15 |
+
28189
|
16 |
+
],
|
17 |
+
"precisions": [
|
18 |
+
65.18644393856809,
|
19 |
+
42.02855344661963,
|
20 |
+
29.86398985919353,
|
21 |
+
21.731881230267124
|
22 |
+
],
|
23 |
+
"bp": 0.9908084778664968,
|
24 |
+
"sys_len": 31189,
|
25 |
+
"ref_len": 31477
|
26 |
+
},
|
27 |
+
"rouge": {
|
28 |
+
"rouge1": 0.6296302993105114,
|
29 |
+
"rouge2": 0.4251646068510136,
|
30 |
+
"rougeL": 0.581729073803074,
|
31 |
+
"rougeLsum": 0.5819048504270989
|
32 |
+
},
|
33 |
+
"bleu": {
|
34 |
+
"bleu": 0.361806558580766,
|
35 |
+
"precisions": [
|
36 |
+
0.6518644393856808,
|
37 |
+
0.4202855344661963,
|
38 |
+
0.2986398985919353,
|
39 |
+
0.21731881230267125
|
40 |
+
],
|
41 |
+
"brevity_penalty": 0.9908084778664968,
|
42 |
+
"length_ratio": 0.9908504622422721,
|
43 |
+
"translation_length": 31189,
|
44 |
+
"reference_length": 31477
|
45 |
+
},
|
46 |
+
"meteor": {
|
47 |
+
"meteor": 0.5972263887277424
|
48 |
+
},
|
49 |
+
"ter": {
|
50 |
+
"score": 54.04040404040404,
|
51 |
+
"num_edits": 15301,
|
52 |
+
"ref_length": 28314.0
|
53 |
+
},
|
54 |
+
"bert_score": {
|
55 |
+
"precision": 0.8332746320664882,
|
56 |
+
"recall": 0.8329256139397622,
|
57 |
+
"f1": 0.8327405597269535,
|
58 |
+
"hashcode": "microsoft/deberta-xlarge-mnli_L40_no-idf_version=0.3.12(hug_trans=4.30.0.dev0)_fast-tokenizer"
|
59 |
+
}
|
60 |
+
}
|
sample_text/en2es.opusMT.json
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"path": "sample_text/en2es.translation.opusMT.txt",
|
3 |
+
"sacrebleu": {
|
4 |
+
"score": 36.43661473091549,
|
5 |
+
"counts": [
|
6 |
+
20325,
|
7 |
+
12744,
|
8 |
+
8795,
|
9 |
+
6218
|
10 |
+
],
|
11 |
+
"totals": [
|
12 |
+
31138,
|
13 |
+
30138,
|
14 |
+
29138,
|
15 |
+
28138
|
16 |
+
],
|
17 |
+
"precisions": [
|
18 |
+
65.27394180743785,
|
19 |
+
42.28548676089986,
|
20 |
+
30.183952227332007,
|
21 |
+
22.098230151396688
|
22 |
+
],
|
23 |
+
"bp": 0.989172030031952,
|
24 |
+
"sys_len": 31138,
|
25 |
+
"ref_len": 31477
|
26 |
+
},
|
27 |
+
"rouge": {
|
28 |
+
"rouge1": 0.635226089622703,
|
29 |
+
"rouge2": 0.43519113055921854,
|
30 |
+
"rougeL": 0.5866361740266497,
|
31 |
+
"rougeLsum": 0.5868857224203005
|
32 |
+
},
|
33 |
+
"bleu": {
|
34 |
+
"bleu": 0.3643661473091548,
|
35 |
+
"precisions": [
|
36 |
+
0.6527394180743785,
|
37 |
+
0.4228548676089986,
|
38 |
+
0.30183952227332006,
|
39 |
+
0.2209823015139669
|
40 |
+
],
|
41 |
+
"brevity_penalty": 0.989172030031952,
|
42 |
+
"length_ratio": 0.9892302315976745,
|
43 |
+
"translation_length": 31138,
|
44 |
+
"reference_length": 31477
|
45 |
+
},
|
46 |
+
"meteor": {
|
47 |
+
"meteor": 0.5999046031429729
|
48 |
+
},
|
49 |
+
"ter": {
|
50 |
+
"score": 53.69075369075369,
|
51 |
+
"num_edits": 15202,
|
52 |
+
"ref_length": 28314.0
|
53 |
+
},
|
54 |
+
"bert_score": {
|
55 |
+
"precision": 0.8365356454253197,
|
56 |
+
"recall": 0.8347951875030994,
|
57 |
+
"f1": 0.8353308997601271,
|
58 |
+
"hashcode": "microsoft/deberta-xlarge-mnli_L40_no-idf_version=0.3.12(hug_trans=4.30.0.dev0)_fast-tokenizer"
|
59 |
+
}
|
60 |
+
}
|
sample_text/en2es.small100.json
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"path": "sample_text/en2es.translation.small100.txt",
|
3 |
+
"sacrebleu": {
|
4 |
+
"score": 30.053582066546088,
|
5 |
+
"counts": [
|
6 |
+
18913,
|
7 |
+
10845,
|
8 |
+
6942,
|
9 |
+
4595
|
10 |
+
],
|
11 |
+
"totals": [
|
12 |
+
30637,
|
13 |
+
29637,
|
14 |
+
28638,
|
15 |
+
27639
|
16 |
+
],
|
17 |
+
"precisions": [
|
18 |
+
61.73254561477952,
|
19 |
+
36.59277254782873,
|
20 |
+
24.240519589356797,
|
21 |
+
16.625058793733494
|
22 |
+
],
|
23 |
+
"bp": 0.9729546287833154,
|
24 |
+
"sys_len": 30637,
|
25 |
+
"ref_len": 31477
|
26 |
+
},
|
27 |
+
"rouge": {
|
28 |
+
"rouge1": 0.5870210774800202,
|
29 |
+
"rouge2": 0.3676688593672182,
|
30 |
+
"rougeL": 0.5358577025466011,
|
31 |
+
"rougeLsum": 0.5354834368959198
|
32 |
+
},
|
33 |
+
"bleu": {
|
34 |
+
"bleu": 0.30053582066546075,
|
35 |
+
"precisions": [
|
36 |
+
0.6173254561477951,
|
37 |
+
0.3659277254782873,
|
38 |
+
0.242405195893568,
|
39 |
+
0.16625058793733494
|
40 |
+
],
|
41 |
+
"brevity_penalty": 0.9729546287833154,
|
42 |
+
"length_ratio": 0.973313848206627,
|
43 |
+
"translation_length": 30637,
|
44 |
+
"reference_length": 31477
|
45 |
+
},
|
46 |
+
"meteor": {
|
47 |
+
"meteor": 0.5421271958584509
|
48 |
+
},
|
49 |
+
"ter": {
|
50 |
+
"score": 58.49049939959031,
|
51 |
+
"num_edits": 16561,
|
52 |
+
"ref_length": 28314.0
|
53 |
+
},
|
54 |
+
"bert_score": {
|
55 |
+
"precision": 0.8210419535636901,
|
56 |
+
"recall": 0.8172258196473121,
|
57 |
+
"f1": 0.8188223859369754,
|
58 |
+
"hashcode": "microsoft/deberta-xlarge-mnli_L40_no-idf_version=0.3.12(hug_trans=4.30.0.dev0)_fast-tokenizer"
|
59 |
+
}
|
60 |
+
}
|
sample_text/en2es.translation.m2m100_1.2B.txt
CHANGED
@@ -87,7 +87,7 @@ Señor Presidente, quisiera hacer muy claro que, sobre todo, la Comisión tiene
|
|
87 |
Por lo tanto, respetamos lo que el Parlamento pueda decidir.
|
88 |
Pero también quisiera hacer muy claro que el presidente Prodi se ha comprometido con este Parlamento a introducir un nuevo debate, como nos ha recordado el señor Barón Crespo, que sería, además del debate anual sobre el programa legislativo de la Comisión, sobre los amplios ámbitos de acción para los próximos cinco años, es decir, para esta legislatura.
|
89 |
El acuerdo alcanzado en septiembre distinguió este debate de la presentación anual del programa legislativo de la Comisión.
|
90 |
-
También me gustaría decir que la Comisión está preparada y preparada para llevar a cabo este debate cuando sea conveniente y que estábamos listos para hacerlo esta semana como habíamos acordado originalmente, sobre la base de que
|
91 |
Por lo tanto, señora Presidenta, quisiera repetir que la Comisión ha debatido el plan de acción para los próximos cinco años y, cuando el Parlamento decida, - esta semana si esa es la decisión - estamos listos para venir y explicar el programa para los próximos cinco años y, el próximo mes, el programa para el año 2000, en lo que estábamos plenamente de acuerdo.
|
92 |
Propongo que votemos a petición del Grupo del Partido Socialista Europeo para que se restablezca la declaración de la Comisión sobre sus objetivos estratégicos.
|
93 |
(El Parlamento rechazó la solicitud) Presidente.
|
@@ -997,4 +997,4 @@ Quiero felicitarle, lamentablemente en su ausencia, por la forma exhaustiva y ri
|
|
997 |
Él mencionó anteriormente que el informe se llevó a cabo con una mayoría significativa, pero no con mi apoyo.
|
998 |
Por lo tanto, aunque no comparto sus conclusiones, creo que él ha ilustrado en su informe muchas de las cuestiones que la Comisión debe abordar.
|
999 |
La primera es la posibilidad de renacentización de la política de competencia.
|
1000 |
-
Sé que la Comisión se opone a esto, pero el potencial existe.
|
|
|
87 |
Por lo tanto, respetamos lo que el Parlamento pueda decidir.
|
88 |
Pero también quisiera hacer muy claro que el presidente Prodi se ha comprometido con este Parlamento a introducir un nuevo debate, como nos ha recordado el señor Barón Crespo, que sería, además del debate anual sobre el programa legislativo de la Comisión, sobre los amplios ámbitos de acción para los próximos cinco años, es decir, para esta legislatura.
|
89 |
El acuerdo alcanzado en septiembre distinguió este debate de la presentación anual del programa legislativo de la Comisión.
|
90 |
+
También me gustaría decir que la Comisión está preparada y preparada para llevar a cabo este debate cuando sea conveniente y que estábamos listos para hacerlo esta semana como habíamos acordado originalmente, sobre la base de que se presentaría el día anterior en un discurso a los grupos parlamentarios.
|
91 |
Por lo tanto, señora Presidenta, quisiera repetir que la Comisión ha debatido el plan de acción para los próximos cinco años y, cuando el Parlamento decida, - esta semana si esa es la decisión - estamos listos para venir y explicar el programa para los próximos cinco años y, el próximo mes, el programa para el año 2000, en lo que estábamos plenamente de acuerdo.
|
92 |
Propongo que votemos a petición del Grupo del Partido Socialista Europeo para que se restablezca la declaración de la Comisión sobre sus objetivos estratégicos.
|
93 |
(El Parlamento rechazó la solicitud) Presidente.
|
|
|
997 |
Él mencionó anteriormente que el informe se llevó a cabo con una mayoría significativa, pero no con mi apoyo.
|
998 |
Por lo tanto, aunque no comparto sus conclusiones, creo que él ha ilustrado en su informe muchas de las cuestiones que la Comisión debe abordar.
|
999 |
La primera es la posibilidad de renacentización de la política de competencia.
|
1000 |
+
Sé que la Comisión se opone a esto, pero el potencial existe.
|
sample_text/en2es.translation.mbart50_m2m.txt
ADDED
The diff for this file is too large to render.
See raw diff
|
|
sample_text/en2es.translation.nllb-200_3B.txt
ADDED
The diff for this file is too large to render.
See raw diff
|
|
sample_text/en2es.translation.opusMT.txt
ADDED
The diff for this file is too large to render.
See raw diff
|
|
sample_text/en2es.translation.small100.txt
ADDED
The diff for this file is too large to render.
See raw diff
|
|
translate.py
CHANGED
@@ -164,6 +164,10 @@ def main(
|
|
164 |
raise KeyError(
|
165 |
f"Language {target_lang} not found in tokenizer. Available languages: {tokenizer.lang_code_to_id.keys()}"
|
166 |
)
|
|
|
|
|
|
|
|
|
167 |
|
168 |
gen_kwargs = {
|
169 |
"max_length": max_length,
|
@@ -187,7 +191,7 @@ def main(
|
|
187 |
f"Output file: {output_path}\n"
|
188 |
f"Source language: {source_lang}\n"
|
189 |
f"Target language: {target_lang}\n"
|
190 |
-
f"
|
191 |
f"Prompt: {prompt}\n"
|
192 |
f"Starting batch size: {starting_batch_size}\n"
|
193 |
f"Device: {str(accelerator.device).split(':')[0]}\n"
|
|
|
164 |
raise KeyError(
|
165 |
f"Language {target_lang} not found in tokenizer. Available languages: {tokenizer.lang_code_to_id.keys()}"
|
166 |
)
|
167 |
+
if "small100" in model_name:
|
168 |
+
tokenizer.tgt_lang = target_lang
|
169 |
+
# We don't need to force the BOS token, so we set is_translation_model to False
|
170 |
+
is_translation_model = False
|
171 |
|
172 |
gen_kwargs = {
|
173 |
"max_length": max_length,
|
|
|
191 |
f"Output file: {output_path}\n"
|
192 |
f"Source language: {source_lang}\n"
|
193 |
f"Target language: {target_lang}\n"
|
194 |
+
f"Force target lang as BOS token: {is_translation_model}\n"
|
195 |
f"Prompt: {prompt}\n"
|
196 |
f"Starting batch size: {starting_batch_size}\n"
|
197 |
f"Device: {str(accelerator.device).split(':')[0]}\n"
|
utils/tokenization_small100.py
ADDED
@@ -0,0 +1,393 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) 2022 Idiap Research Institute, http://www.idiap.ch/
|
2 |
+
# Written by Alireza Mohammadshahi <alireza.mohammadshahi@idiap.ch>
|
3 |
+
# This is a modified version of https://github.com/huggingface/transformers/blob/main/src/transformers/models/m2m_100/tokenization_m2m_100.py
|
4 |
+
# which owns by Fariseq Authors and The HuggingFace Inc. team.
|
5 |
+
#
|
6 |
+
#
|
7 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
8 |
+
# you may not use this file except in compliance with the License.
|
9 |
+
# You may obtain a copy of the License at
|
10 |
+
#
|
11 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
12 |
+
#
|
13 |
+
# Unless required by applicable law or agreed to in writing, software
|
14 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
15 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
16 |
+
# See the License for the specific language governing permissions and
|
17 |
+
# limitations under the License.
|
18 |
+
"""Tokenization classes for SMALL100."""
|
19 |
+
import json
|
20 |
+
import os
|
21 |
+
from pathlib import Path
|
22 |
+
from shutil import copyfile
|
23 |
+
from typing import Any, Dict, List, Optional, Tuple, Union
|
24 |
+
|
25 |
+
import sentencepiece
|
26 |
+
|
27 |
+
from transformers.tokenization_utils import BatchEncoding, PreTrainedTokenizer
|
28 |
+
from transformers.utils import logging
|
29 |
+
|
30 |
+
|
31 |
+
logger = logging.get_logger(__name__)
|
32 |
+
|
33 |
+
SPIECE_UNDERLINE = "▁"
|
34 |
+
|
35 |
+
VOCAB_FILES_NAMES = {
|
36 |
+
"vocab_file": "vocab.json",
|
37 |
+
"spm_file": "sentencepiece.bpe.model",
|
38 |
+
"tokenizer_config_file": "tokenizer_config.json",
|
39 |
+
}
|
40 |
+
|
41 |
+
PRETRAINED_VOCAB_FILES_MAP = {
|
42 |
+
"vocab_file": {
|
43 |
+
"alirezamsh/small100": "https://huggingface.co/alirezamsh/small100/resolve/main/vocab.json",
|
44 |
+
},
|
45 |
+
"spm_file": {
|
46 |
+
"alirezamsh/small100": "https://huggingface.co/alirezamsh/small100/resolve/main/sentencepiece.bpe.model",
|
47 |
+
},
|
48 |
+
"tokenizer_config_file": {
|
49 |
+
"alirezamsh/small100": "https://huggingface.co/alirezamsh/small100/resolve/main/tokenizer_config.json",
|
50 |
+
},
|
51 |
+
}
|
52 |
+
|
53 |
+
PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES = {
|
54 |
+
"alirezamsh/small100": 1024,
|
55 |
+
}
|
56 |
+
|
57 |
+
# fmt: off
|
58 |
+
FAIRSEQ_LANGUAGE_CODES = {
|
59 |
+
"m2m100": ["af", "am", "ar", "ast", "az", "ba", "be", "bg", "bn", "br", "bs", "ca", "ceb", "cs", "cy", "da", "de", "el", "en", "es", "et", "fa", "ff", "fi", "fr", "fy", "ga", "gd", "gl", "gu", "ha", "he", "hi", "hr", "ht", "hu", "hy", "id", "ig", "ilo", "is", "it", "ja", "jv", "ka", "kk", "km", "kn", "ko", "lb", "lg", "ln", "lo", "lt", "lv", "mg", "mk", "ml", "mn", "mr", "ms", "my", "ne", "nl", "no", "ns", "oc", "or", "pa", "pl", "ps", "pt", "ro", "ru", "sd", "si", "sk", "sl", "so", "sq", "sr", "ss", "su", "sv", "sw", "ta", "th", "tl", "tn", "tr", "uk", "ur", "uz", "vi", "wo", "xh", "yi", "yo", "zh", "zu"]
|
60 |
+
}
|
61 |
+
# fmt: on
|
62 |
+
|
63 |
+
|
64 |
+
class SMALL100Tokenizer(PreTrainedTokenizer):
|
65 |
+
"""
|
66 |
+
Construct an SMALL100 tokenizer. Based on [SentencePiece](https://github.com/google/sentencepiece).
|
67 |
+
This tokenizer inherits from [`PreTrainedTokenizer`] which contains most of the main methods. Users should refer to
|
68 |
+
this superclass for more information regarding those methods.
|
69 |
+
Args:
|
70 |
+
vocab_file (`str`):
|
71 |
+
Path to the vocabulary file.
|
72 |
+
spm_file (`str`):
|
73 |
+
Path to [SentencePiece](https://github.com/google/sentencepiece) file (generally has a .spm extension) that
|
74 |
+
contains the vocabulary.
|
75 |
+
tgt_lang (`str`, *optional*):
|
76 |
+
A string representing the target language.
|
77 |
+
eos_token (`str`, *optional*, defaults to `"</s>"`):
|
78 |
+
The end of sequence token.
|
79 |
+
sep_token (`str`, *optional*, defaults to `"</s>"`):
|
80 |
+
The separator token, which is used when building a sequence from multiple sequences, e.g. two sequences for
|
81 |
+
sequence classification or for a text and a question for question answering. It is also used as the last
|
82 |
+
token of a sequence built with special tokens.
|
83 |
+
unk_token (`str`, *optional*, defaults to `"<unk>"`):
|
84 |
+
The unknown token. A token that is not in the vocabulary cannot be converted to an ID and is set to be this
|
85 |
+
token instead.
|
86 |
+
pad_token (`str`, *optional*, defaults to `"<pad>"`):
|
87 |
+
The token used for padding, for example when batching sequences of different lengths.
|
88 |
+
language_codes (`str`, *optional*):
|
89 |
+
What language codes to use. Should be `"m2m100"`.
|
90 |
+
sp_model_kwargs (`dict`, *optional*):
|
91 |
+
Will be passed to the `SentencePieceProcessor.__init__()` method. The [Python wrapper for
|
92 |
+
SentencePiece](https://github.com/google/sentencepiece/tree/master/python) can be used, among other things,
|
93 |
+
to set:
|
94 |
+
- `enable_sampling`: Enable subword regularization.
|
95 |
+
- `nbest_size`: Sampling parameters for unigram. Invalid for BPE-Dropout.
|
96 |
+
- `nbest_size = {0,1}`: No sampling is performed.
|
97 |
+
- `nbest_size > 1`: samples from the nbest_size results.
|
98 |
+
- `nbest_size < 0`: assuming that nbest_size is infinite and samples from the all hypothesis (lattice)
|
99 |
+
using forward-filtering-and-backward-sampling algorithm.
|
100 |
+
- `alpha`: Smoothing parameter for unigram sampling, and dropout probability of merge operations for
|
101 |
+
BPE-dropout.
|
102 |
+
Examples:
|
103 |
+
```python
|
104 |
+
\>>> from tokenization_small100 import SMALL100Tokenizer
|
105 |
+
\>>> tokenizer = SMALL100Tokenizer.from_pretrained("alirezamsh/small100", tgt_lang="ro")
|
106 |
+
\>>> src_text = " UN Chief Says There Is No Military Solution in Syria"
|
107 |
+
\>>> tgt_text = "Şeful ONU declară că nu există o soluţie militară în Siria"
|
108 |
+
\>>> model_inputs = tokenizer(src_text, text_target=tgt_text, return_tensors="pt")
|
109 |
+
\>>> model(**model_inputs) # should work
|
110 |
+
```
|
111 |
+
"""
|
112 |
+
|
113 |
+
vocab_files_names = VOCAB_FILES_NAMES
|
114 |
+
max_model_input_sizes = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
|
115 |
+
pretrained_vocab_files_map = PRETRAINED_VOCAB_FILES_MAP
|
116 |
+
model_input_names = ["input_ids", "attention_mask"]
|
117 |
+
|
118 |
+
prefix_tokens: List[int] = []
|
119 |
+
suffix_tokens: List[int] = []
|
120 |
+
|
121 |
+
def __init__(
|
122 |
+
self,
|
123 |
+
vocab_file,
|
124 |
+
spm_file,
|
125 |
+
tgt_lang=None,
|
126 |
+
bos_token="<s>",
|
127 |
+
eos_token="</s>",
|
128 |
+
sep_token="</s>",
|
129 |
+
pad_token="<pad>",
|
130 |
+
unk_token="<unk>",
|
131 |
+
language_codes="m2m100",
|
132 |
+
sp_model_kwargs: Optional[Dict[str, Any]] = None,
|
133 |
+
num_madeup_words=8,
|
134 |
+
**kwargs,
|
135 |
+
) -> None:
|
136 |
+
self.sp_model_kwargs = {} if sp_model_kwargs is None else sp_model_kwargs
|
137 |
+
|
138 |
+
self.language_codes = language_codes
|
139 |
+
fairseq_language_code = FAIRSEQ_LANGUAGE_CODES[language_codes]
|
140 |
+
self.lang_code_to_token = {
|
141 |
+
lang_code: f"__{lang_code}__" for lang_code in fairseq_language_code
|
142 |
+
}
|
143 |
+
|
144 |
+
kwargs["additional_special_tokens"] = kwargs.get(
|
145 |
+
"additional_special_tokens", []
|
146 |
+
)
|
147 |
+
kwargs["additional_special_tokens"] += [
|
148 |
+
self.get_lang_token(lang_code)
|
149 |
+
for lang_code in fairseq_language_code
|
150 |
+
if self.get_lang_token(lang_code) not in kwargs["additional_special_tokens"]
|
151 |
+
]
|
152 |
+
|
153 |
+
super().__init__(
|
154 |
+
tgt_lang=tgt_lang,
|
155 |
+
bos_token=bos_token,
|
156 |
+
eos_token=eos_token,
|
157 |
+
sep_token=sep_token,
|
158 |
+
unk_token=unk_token,
|
159 |
+
pad_token=pad_token,
|
160 |
+
language_codes=language_codes,
|
161 |
+
sp_model_kwargs=self.sp_model_kwargs,
|
162 |
+
num_madeup_words=num_madeup_words,
|
163 |
+
**kwargs,
|
164 |
+
)
|
165 |
+
|
166 |
+
self.vocab_file = vocab_file
|
167 |
+
self.encoder = load_json(vocab_file)
|
168 |
+
self.decoder = {v: k for k, v in self.encoder.items()}
|
169 |
+
self.spm_file = spm_file
|
170 |
+
self.sp_model = load_spm(spm_file, self.sp_model_kwargs)
|
171 |
+
|
172 |
+
self.encoder_size = len(self.encoder)
|
173 |
+
|
174 |
+
self.lang_token_to_id = {
|
175 |
+
self.get_lang_token(lang_code): self.encoder_size + i
|
176 |
+
for i, lang_code in enumerate(fairseq_language_code)
|
177 |
+
}
|
178 |
+
self.lang_code_to_id = {
|
179 |
+
lang_code: self.encoder_size + i
|
180 |
+
for i, lang_code in enumerate(fairseq_language_code)
|
181 |
+
}
|
182 |
+
self.id_to_lang_token = {v: k for k, v in self.lang_token_to_id.items()}
|
183 |
+
|
184 |
+
self._tgt_lang = tgt_lang if tgt_lang is not None else "en"
|
185 |
+
self.cur_lang_id = self.get_lang_id(self._tgt_lang)
|
186 |
+
self.set_lang_special_tokens(self._tgt_lang)
|
187 |
+
|
188 |
+
self.num_madeup_words = num_madeup_words
|
189 |
+
|
190 |
+
@property
|
191 |
+
def vocab_size(self) -> int:
|
192 |
+
return len(self.encoder) + len(self.lang_token_to_id) + self.num_madeup_words
|
193 |
+
|
194 |
+
@property
|
195 |
+
def tgt_lang(self) -> str:
|
196 |
+
return self._tgt_lang
|
197 |
+
|
198 |
+
@tgt_lang.setter
|
199 |
+
def tgt_lang(self, new_tgt_lang: str) -> None:
|
200 |
+
self._tgt_lang = new_tgt_lang
|
201 |
+
self.set_lang_special_tokens(self._tgt_lang)
|
202 |
+
|
203 |
+
def _tokenize(self, text: str) -> List[str]:
|
204 |
+
return self.sp_model.encode(text, out_type=str)
|
205 |
+
|
206 |
+
def _convert_token_to_id(self, token):
|
207 |
+
if token in self.lang_token_to_id:
|
208 |
+
return self.lang_token_to_id[token]
|
209 |
+
return self.encoder.get(token, self.encoder[self.unk_token])
|
210 |
+
|
211 |
+
def _convert_id_to_token(self, index: int) -> str:
|
212 |
+
"""Converts an index (integer) in a token (str) using the decoder."""
|
213 |
+
if index in self.id_to_lang_token:
|
214 |
+
return self.id_to_lang_token[index]
|
215 |
+
return self.decoder.get(index, self.unk_token)
|
216 |
+
|
217 |
+
def convert_tokens_to_string(self, tokens: List[str]) -> str:
|
218 |
+
"""Converts a sequence of tokens (strings for sub-words) in a single string."""
|
219 |
+
return self.sp_model.decode(tokens)
|
220 |
+
|
221 |
+
def get_special_tokens_mask(
|
222 |
+
self,
|
223 |
+
token_ids_0: List[int],
|
224 |
+
token_ids_1: Optional[List[int]] = None,
|
225 |
+
already_has_special_tokens: bool = False,
|
226 |
+
) -> List[int]:
|
227 |
+
"""
|
228 |
+
Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding
|
229 |
+
special tokens using the tokenizer `prepare_for_model` method.
|
230 |
+
Args:
|
231 |
+
token_ids_0 (`List[int]`):
|
232 |
+
List of IDs.
|
233 |
+
token_ids_1 (`List[int]`, *optional*):
|
234 |
+
Optional second list of IDs for sequence pairs.
|
235 |
+
already_has_special_tokens (`bool`, *optional*, defaults to `False`):
|
236 |
+
Whether or not the token list is already formatted with special tokens for the model.
|
237 |
+
Returns:
|
238 |
+
`List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token.
|
239 |
+
"""
|
240 |
+
|
241 |
+
if already_has_special_tokens:
|
242 |
+
return super().get_special_tokens_mask(
|
243 |
+
token_ids_0=token_ids_0,
|
244 |
+
token_ids_1=token_ids_1,
|
245 |
+
already_has_special_tokens=True,
|
246 |
+
)
|
247 |
+
|
248 |
+
prefix_ones = [1] * len(self.prefix_tokens)
|
249 |
+
suffix_ones = [1] * len(self.suffix_tokens)
|
250 |
+
if token_ids_1 is None:
|
251 |
+
return prefix_ones + ([0] * len(token_ids_0)) + suffix_ones
|
252 |
+
return (
|
253 |
+
prefix_ones
|
254 |
+
+ ([0] * len(token_ids_0))
|
255 |
+
+ ([0] * len(token_ids_1))
|
256 |
+
+ suffix_ones
|
257 |
+
)
|
258 |
+
|
259 |
+
def build_inputs_with_special_tokens(
|
260 |
+
self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None
|
261 |
+
) -> List[int]:
|
262 |
+
"""
|
263 |
+
Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and
|
264 |
+
adding special tokens. An MBART sequence has the following format, where `X` represents the sequence:
|
265 |
+
- `input_ids` (for encoder) `X [eos, src_lang_code]`
|
266 |
+
- `decoder_input_ids`: (for decoder) `X [eos, tgt_lang_code]`
|
267 |
+
BOS is never used. Pairs of sequences are not the expected use case, but they will be handled without a
|
268 |
+
separator.
|
269 |
+
Args:
|
270 |
+
token_ids_0 (`List[int]`):
|
271 |
+
List of IDs to which the special tokens will be added.
|
272 |
+
token_ids_1 (`List[int]`, *optional*):
|
273 |
+
Optional second list of IDs for sequence pairs.
|
274 |
+
Returns:
|
275 |
+
`List[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens.
|
276 |
+
"""
|
277 |
+
if token_ids_1 is None:
|
278 |
+
if self.prefix_tokens is None:
|
279 |
+
return token_ids_0 + self.suffix_tokens
|
280 |
+
else:
|
281 |
+
return self.prefix_tokens + token_ids_0 + self.suffix_tokens
|
282 |
+
# We don't expect to process pairs, but leave the pair logic for API consistency
|
283 |
+
if self.prefix_tokens is None:
|
284 |
+
return token_ids_0 + token_ids_1 + self.suffix_tokens
|
285 |
+
else:
|
286 |
+
return self.prefix_tokens + token_ids_0 + token_ids_1 + self.suffix_tokens
|
287 |
+
|
288 |
+
def get_vocab(self) -> Dict:
|
289 |
+
vocab = {self.convert_ids_to_tokens(i): i for i in range(self.vocab_size)}
|
290 |
+
vocab.update(self.added_tokens_encoder)
|
291 |
+
return vocab
|
292 |
+
|
293 |
+
def __getstate__(self) -> Dict:
|
294 |
+
state = self.__dict__.copy()
|
295 |
+
state["sp_model"] = None
|
296 |
+
return state
|
297 |
+
|
298 |
+
def __setstate__(self, d: Dict) -> None:
|
299 |
+
self.__dict__ = d
|
300 |
+
|
301 |
+
# for backward compatibility
|
302 |
+
if not hasattr(self, "sp_model_kwargs"):
|
303 |
+
self.sp_model_kwargs = {}
|
304 |
+
|
305 |
+
self.sp_model = load_spm(self.spm_file, self.sp_model_kwargs)
|
306 |
+
|
307 |
+
def save_vocabulary(
|
308 |
+
self, save_directory: str, filename_prefix: Optional[str] = None
|
309 |
+
) -> Tuple[str]:
|
310 |
+
save_dir = Path(save_directory)
|
311 |
+
if not save_dir.is_dir():
|
312 |
+
raise OSError(f"{save_directory} should be a directory")
|
313 |
+
vocab_save_path = save_dir / (
|
314 |
+
(filename_prefix + "-" if filename_prefix else "")
|
315 |
+
+ self.vocab_files_names["vocab_file"]
|
316 |
+
)
|
317 |
+
spm_save_path = save_dir / (
|
318 |
+
(filename_prefix + "-" if filename_prefix else "")
|
319 |
+
+ self.vocab_files_names["spm_file"]
|
320 |
+
)
|
321 |
+
|
322 |
+
save_json(self.encoder, vocab_save_path)
|
323 |
+
|
324 |
+
if os.path.abspath(self.spm_file) != os.path.abspath(
|
325 |
+
spm_save_path
|
326 |
+
) and os.path.isfile(self.spm_file):
|
327 |
+
copyfile(self.spm_file, spm_save_path)
|
328 |
+
elif not os.path.isfile(self.spm_file):
|
329 |
+
with open(spm_save_path, "wb") as fi:
|
330 |
+
content_spiece_model = self.sp_model.serialized_model_proto()
|
331 |
+
fi.write(content_spiece_model)
|
332 |
+
|
333 |
+
return (str(vocab_save_path), str(spm_save_path))
|
334 |
+
|
335 |
+
def prepare_seq2seq_batch(
|
336 |
+
self,
|
337 |
+
src_texts: List[str],
|
338 |
+
tgt_texts: Optional[List[str]] = None,
|
339 |
+
tgt_lang: str = "ro",
|
340 |
+
**kwargs,
|
341 |
+
) -> BatchEncoding:
|
342 |
+
self.tgt_lang = tgt_lang
|
343 |
+
self.set_lang_special_tokens(self.tgt_lang)
|
344 |
+
return super().prepare_seq2seq_batch(src_texts, tgt_texts, **kwargs)
|
345 |
+
|
346 |
+
def _build_translation_inputs(
|
347 |
+
self, raw_inputs, tgt_lang: Optional[str], **extra_kwargs
|
348 |
+
):
|
349 |
+
"""Used by translation pipeline, to prepare inputs for the generate function"""
|
350 |
+
if tgt_lang is None:
|
351 |
+
raise ValueError("Translation requires a `tgt_lang` for this model")
|
352 |
+
self.tgt_lang = tgt_lang
|
353 |
+
inputs = self(raw_inputs, add_special_tokens=True, **extra_kwargs)
|
354 |
+
return inputs
|
355 |
+
|
356 |
+
def _switch_to_input_mode(self):
|
357 |
+
self.set_lang_special_tokens(self.tgt_lang)
|
358 |
+
|
359 |
+
def _switch_to_target_mode(self):
|
360 |
+
self.prefix_tokens = None
|
361 |
+
self.suffix_tokens = [self.eos_token_id]
|
362 |
+
|
363 |
+
def set_lang_special_tokens(self, src_lang: str) -> None:
|
364 |
+
"""Reset the special tokens to the tgt lang setting. No prefix and suffix=[eos, tgt_lang_code]."""
|
365 |
+
lang_token = self.get_lang_token(src_lang)
|
366 |
+
self.cur_lang_id = self.lang_token_to_id[lang_token]
|
367 |
+
self.prefix_tokens = [self.cur_lang_id]
|
368 |
+
self.suffix_tokens = [self.eos_token_id]
|
369 |
+
|
370 |
+
def get_lang_token(self, lang: str) -> str:
|
371 |
+
return self.lang_code_to_token[lang]
|
372 |
+
|
373 |
+
def get_lang_id(self, lang: str) -> int:
|
374 |
+
lang_token = self.get_lang_token(lang)
|
375 |
+
return self.lang_token_to_id[lang_token]
|
376 |
+
|
377 |
+
|
378 |
+
def load_spm(
|
379 |
+
path: str, sp_model_kwargs: Dict[str, Any]
|
380 |
+
) -> sentencepiece.SentencePieceProcessor:
|
381 |
+
spm = sentencepiece.SentencePieceProcessor(**sp_model_kwargs)
|
382 |
+
spm.Load(str(path))
|
383 |
+
return spm
|
384 |
+
|
385 |
+
|
386 |
+
def load_json(path: str) -> Union[Dict, List]:
|
387 |
+
with open(path, "r") as f:
|
388 |
+
return json.load(f)
|
389 |
+
|
390 |
+
|
391 |
+
def save_json(data, path: str) -> None:
|
392 |
+
with open(path, "w") as f:
|
393 |
+
json.dump(data, f, indent=2)
|