add the script
Browse files- fsmt-make-tiny-model.py +59 -0
fsmt-make-tiny-model.py
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 model through reduction of a normal pre-trained model, but keeping the
|
21 |
+
# full vocab, merges file, and thus also resulting in a larger model due to a large vocab size.
|
22 |
+
# This gives ~3MB in total for all files.
|
23 |
+
#
|
24 |
+
# If you want a 50 times smaller than this see `fsmt-make-super-tiny-model.py`, which is slightly more complicated
|
25 |
+
#
|
26 |
+
#
|
27 |
+
# It will be used then as "stas/tiny-wmt19-en-de"
|
28 |
+
|
29 |
+
# Build
|
30 |
+
from transformers import FSMTTokenizer, FSMTConfig, FSMTForConditionalGeneration
|
31 |
+
mname = "facebook/wmt19-en-de"
|
32 |
+
tokenizer = FSMTTokenizer.from_pretrained(mname)
|
33 |
+
# get the correct vocab sizes, etc. from the master model
|
34 |
+
config = FSMTConfig.from_pretrained(mname)
|
35 |
+
config.update(dict(
|
36 |
+
d_model=4,
|
37 |
+
encoder_layers=1, decoder_layers=1,
|
38 |
+
encoder_ffn_dim=4, decoder_ffn_dim=4,
|
39 |
+
encoder_attention_heads=1, decoder_attention_heads=1))
|
40 |
+
|
41 |
+
tiny_model = FSMTForConditionalGeneration(config)
|
42 |
+
print(f"num of params {tiny_model.num_parameters()}")
|
43 |
+
|
44 |
+
# Test
|
45 |
+
batch = tokenizer(["Making tiny model"], return_tensors="pt")
|
46 |
+
outputs = tiny_model(**batch)
|
47 |
+
|
48 |
+
print("test output:", len(outputs.logits[0]))
|
49 |
+
|
50 |
+
# Save
|
51 |
+
mname_tiny = "tiny-wmt19-en-de"
|
52 |
+
tiny_model.half() # makes it smaller
|
53 |
+
tiny_model.save_pretrained(mname_tiny)
|
54 |
+
tokenizer.save_pretrained(mname_tiny)
|
55 |
+
|
56 |
+
print(f"Generated {mname_tiny}")
|
57 |
+
|
58 |
+
# Upload
|
59 |
+
# transformers-cli upload tiny-wmt19-en-de
|