usrbinenv python coding utf8 2020 the huggingface team licensed under the apache license version 2 0 the license you may not use this file except in compliance with the license you may obtain a copy of the license at http www apache orglicenseslicense2 0 unless required by applicable law or agreed to in writing software distributed under the license is distributed on an as is basis without warranties or conditions of any kind either express or implied see the license for the specific language governing permissions and limitations under the license this script creates a super tiny model that is useful inside tests when we just want to test that the machinery works without needing to the check the quality of the outcomes this version creates a tiny model through reduction of a normal pretrained model but keeping the full vocab merges file and thus also resulting in a larger model due to a large vocab size this gives 3mb in total for all files if you want a 50 times smaller than this see fsmtmakesupertinymodel py which is slightly more complicated it will be used then as stastinywmt19ende build get the correct vocab sizes etc from the master model test save upload transformerscli upload tinywmt19ende usr bin env python coding utf 8 2020 the huggingface team licensed under the apache license version 2 0 the license you may not use this file except in compliance with the license you may obtain a copy of the license at http www apache org licenses license 2 0 unless required by applicable law or agreed to in writing software distributed under the license is distributed on an as is basis without warranties or conditions of any kind either express or implied see the license for the specific language governing permissions and limitations under the license this script creates a super tiny model that is useful inside tests when we just want to test that the machinery works without needing to the check the quality of the outcomes this version creates a tiny model through reduction of a normal pre trained model but keeping the full vocab merges file and thus also resulting in a larger model due to a large vocab size this gives 3mb in total for all files if you want a 50 times smaller than this see fsmt make super tiny model py which is slightly more complicated it will be used then as stas tiny wmt19 en de build get the correct vocab sizes etc from the master model test save makes it smaller upload transformers cli upload tiny wmt19 en de
from transformers import FSMTConfig, FSMTForConditionalGeneration, FSMTTokenizer


mname = "facebook/wmt19-en-de"
tokenizer = FSMTTokenizer.from_pretrained(mname)

config = FSMTConfig.from_pretrained(mname)
config.update({
    "d_model": 4,
    "encoder_layers": 1, "decoder_layers": 1,
    "encoder_ffn_dim": 4, "decoder_ffn_dim": 4,
    "encoder_attention_heads": 1, "decoder_attention_heads": 1})

tiny_model = FSMTForConditionalGeneration(config)
print(f"num of params {tiny_model.num_parameters()}")


batch = tokenizer(["Making tiny model"], return_tensors="pt")
outputs = tiny_model(**batch)

print("test output:", len(outputs.logits[0]))


mname_tiny = "tiny-wmt19-en-de"
tiny_model.half() 
tiny_model.save_pretrained(mname_tiny)
tokenizer.save_pretrained(mname_tiny)

print(f"Generated {mname_tiny}")