add the script
Browse files
fsmt-make-super-tiny-model.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python
|
| 2 |
+
# coding: utf-8
|
| 3 |
+
# Copyright 2020 The HuggingFace Team. All rights reserved.
|
| 4 |
+
#
|
| 5 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 6 |
+
# you may not use this file except in compliance with the License.
|
| 7 |
+
# You may obtain a copy of the License at
|
| 8 |
+
#
|
| 9 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 10 |
+
#
|
| 11 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 12 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 13 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 14 |
+
# See the License for the specific language governing permissions and
|
| 15 |
+
# limitations under the License.
|
| 16 |
+
|
| 17 |
+
# This script creates a super tiny model that is useful inside tests, when we just want to test that
|
| 18 |
+
# the machinery works, without needing to the check the quality of the outcomes.
|
| 19 |
+
#
|
| 20 |
+
# This version creates a tiny vocab first, and then a tiny model - so the outcome is truly tiny -
|
| 21 |
+
# all files ~60KB. As compared to taking a full-size model, reducing to the minimum its layers and
|
| 22 |
+
# emb dimensions, but keeping the full vocab + merges files, leading to ~3MB in total for all files.
|
| 23 |
+
# The latter is done by `fsmt-make-super-tiny-model.py`.
|
| 24 |
+
#
|
| 25 |
+
# It will be used then as "stas/tiny-wmt19-en-ru"
|
| 26 |
+
|
| 27 |
+
from pathlib import Path
|
| 28 |
+
import json
|
| 29 |
+
import tempfile
|
| 30 |
+
|
| 31 |
+
from transformers import FSMTTokenizer, FSMTConfig, FSMTForConditionalGeneration
|
| 32 |
+
from transformers.models.fsmt.tokenization_fsmt import VOCAB_FILES_NAMES
|
| 33 |
+
|
| 34 |
+
mname_tiny = "tiny-wmt19-en-ru"
|
| 35 |
+
|
| 36 |
+
# Build
|
| 37 |
+
|
| 38 |
+
# borrowed from a test
|
| 39 |
+
vocab = [ "l", "o", "w", "e", "r", "s", "t", "i", "d", "n", "w</w>", "r</w>", "t</w>", "lo", "low", "er</w>", "low</w>", "lowest</w>", "newer</w>", "wider</w>", "<unk>", ]
|
| 40 |
+
vocab_tokens = dict(zip(vocab, range(len(vocab))))
|
| 41 |
+
merges = ["l o 123", "lo w 1456", "e r</w> 1789", ""]
|
| 42 |
+
|
| 43 |
+
with tempfile.TemporaryDirectory() as tmpdirname:
|
| 44 |
+
build_dir = Path(tmpdirname)
|
| 45 |
+
src_vocab_file = build_dir / VOCAB_FILES_NAMES["src_vocab_file"]
|
| 46 |
+
tgt_vocab_file = build_dir / VOCAB_FILES_NAMES["tgt_vocab_file"]
|
| 47 |
+
merges_file = build_dir / VOCAB_FILES_NAMES["merges_file"]
|
| 48 |
+
with open(src_vocab_file, "w") as fp: fp.write(json.dumps(vocab_tokens))
|
| 49 |
+
with open(tgt_vocab_file, "w") as fp: fp.write(json.dumps(vocab_tokens))
|
| 50 |
+
with open(merges_file, "w") as fp : fp.write("\n".join(merges))
|
| 51 |
+
|
| 52 |
+
tokenizer = FSMTTokenizer(
|
| 53 |
+
langs=["en", "ru"],
|
| 54 |
+
src_vocab_size = len(vocab),
|
| 55 |
+
tgt_vocab_size = len(vocab),
|
| 56 |
+
src_vocab_file=src_vocab_file,
|
| 57 |
+
tgt_vocab_file=tgt_vocab_file,
|
| 58 |
+
merges_file=merges_file,
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
config = FSMTConfig(
|
| 62 |
+
langs=['ru', 'en'],
|
| 63 |
+
src_vocab_size=1000, tgt_vocab_size=1000,
|
| 64 |
+
d_model=4,
|
| 65 |
+
encoder_layers=1, decoder_layers=1,
|
| 66 |
+
encoder_ffn_dim=4, decoder_ffn_dim=4,
|
| 67 |
+
encoder_attention_heads=1, decoder_attention_heads=1,
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
tiny_model = FSMTForConditionalGeneration(config)
|
| 71 |
+
print(f"num of params {tiny_model.num_parameters()}")
|
| 72 |
+
|
| 73 |
+
# Test
|
| 74 |
+
batch = tokenizer(["Making tiny model"], return_tensors="pt")
|
| 75 |
+
outputs = tiny_model(**batch)
|
| 76 |
+
|
| 77 |
+
print("test output:", len(outputs.logits[0]))
|
| 78 |
+
|
| 79 |
+
# Save
|
| 80 |
+
tiny_model.half() # makes it smaller
|
| 81 |
+
tiny_model.save_pretrained(mname_tiny)
|
| 82 |
+
tokenizer.save_pretrained(mname_tiny)
|
| 83 |
+
|
| 84 |
+
print(f"Generated {mname_tiny}")
|
| 85 |
+
|
| 86 |
+
# Upload
|
| 87 |
+
# transformers-cli upload tiny-wmt19-en-ru
|