Mammoth Multilingual Translation Model
A multilingual transformer translation model (6 encoder + 6 decoder layers, dimension 1024) covering 37 source and 37 target languages, trained on 313 translation directions (tasks).
The model is stored as separate components — one per language embedding, per encoder/decoder layer stack, and so on — each stored once even when many directions share it. When you choose a direction, only the components it uses are downloaded and assembled into a model.
Setup
No Mammoth installation is needed. Create an environment and fetch the inference script from this repo:
python -m venv .venv && source .venv/bin/activate
pip install transformers torch huggingface_hub safetensors
hf download chao0524/big-4p-d-Lenc-Ldec-160k inference.py --local-dir .
inference.py downloads the model files it needs on first use: into the Hugging Face cache by default, or into a folder of your choice with --local-dir (see Offline Use).
Translate
# A trained direction
python inference.py --model-dir chao0524/big-4p-d-Lenc-Ldec-160k --task eng-bos --sentences "Helsinki is the capital of Finland."
# A direction that was never trained (zero-shot): combine a source and a target language
python inference.py --model-dir chao0524/big-4p-d-Lenc-Ldec-160k --src bos --tgt bul --input-file input.bos.txt
Options
| Option | Meaning |
|---|---|
--sentences "..." "..." | Sentences to translate (instead of --input-file) |
--input-file <path> | Text file, one source sentence per line |
--output-file <path> | Write translations here, one per line (default: print to the terminal) |
--device cpu|cuda|mps | Where to run the model (default: cpu) |
--num-beams N | Beam size (default: 4) |
--max-new-tokens N | Maximum output length in tokens (default: 128) |
--batch-size N | Sentences per batch (default: 32) |
--local-dir <path> | Download the model files into this folder instead of the Hugging Face cache |
Choosing a Direction
Trained directions — --task <source>-<target>. These 313 directions were trained directly and are listed in the table below.
Zero-shot directions — --src <source> --tgt <target>. Any source language can be combined with any target language, including pairs that were never trained together (1056 more directions). The source language brings its encoder and the target language its decoder. Quality of untrained pairs varies; check the output before relying on it.
Languages
| Language | As source: trained targets |
|---|---|
bos | bos deu eng fra spa |
bul | bul deu eng fra spa |
cat | cat deu eng fra spa |
ces | ces deu eng fra spa |
dan | dan deu eng fra spa |
deu | all 37 target languages |
ell | deu ell eng fra spa |
eng | all 37 target languages |
est | deu eng est fra spa |
eus | deu eng eus fra spa |
fin | deu eng fin fra spa |
fra | all 37 target languages |
gle | deu eng fra gle spa |
glg | deu eng fra glg spa |
hrv | deu eng fra hrv spa |
hun | deu eng fra hun spa |
isl | deu eng fra isl spa |
ita | deu eng fra ita spa |
kat | deu eng fra kat spa |
lav | deu eng fra lav spa |
lit | deu eng fra lit spa |
mkd | deu eng fra mkd spa |
mlt | deu eng fra mlt spa |
nld | deu eng fra nld spa |
nno | deu eng fra nno spa |
nob | deu eng fra nob spa |
pol | deu eng fra pol spa |
por | deu eng fra por spa |
ron | deu eng fra ron spa |
slk | deu eng fra slk spa |
slv | deu eng fra slv spa |
spa | all 37 target languages |
sqi | deu eng fra spa sqi |
srp_Cyrl | deu eng fra spa srp_Cyrl |
swe | deu eng fra spa swe |
tur | deu eng fra spa tur |
ukr | deu eng fra spa ukr |
Using the Model from Python
inference.py is also a module (it must be in the current directory or on sys.path):
from inference import load_model, translate
src_tok, tgt_tok, model = load_model("chao0524/big-4p-d-Lenc-Ldec-160k", task="eng-bos")
print(translate(src_tok, tgt_tok, model, ["Helsinki is the capital of Finland."]))
load_model also takes src= / tgt= (and encoder= / decoder=), like the command line. translate() takes num_beams and max_new_tokens. model is a standard transformers model, so model.generate() also works if you need more control.
Offline Use
By default the model files go into the Hugging Face cache, which is hard to browse. Add --local-dir to download them into a folder of your choice instead (only the files needed for that task are downloaded):
python inference.py --model-dir chao0524/big-4p-d-Lenc-Ldec-160k --task eng-bos --local-dir ./eng-bos --input-file in.txt
The folder contains everything needed, including inference.py. On later runs, pass the folder as --model-dir (no internet connection needed):
python ./eng-bos/inference.py --model-dir ./eng-bos --task eng-bos --input-file in.txt
From Python: load_model("chao0524/big-4p-d-Lenc-Ldec-160k", ..., local_dir="./eng-bos"), then load_model("./eng-bos", ...) later.
To download without translating, use the shipped model_downloader.py:
hf download chao0524/big-4p-d-Lenc-Ldec-160k model_downloader.py --local-dir .
# Everything
python model_downloader.py --repo-id chao0524/big-4p-d-Lenc-Ldec-160k --local-dir ./model
# Only the files needed for one task
python model_downloader.py --repo-id chao0524/big-4p-d-Lenc-Ldec-160k --local-dir ./eng-bos --task eng-bos
Notes
- Mammoth uses a separate vocabulary per language, so each direction has a source tokenizer (for the input) and a target tokenizer (for the output).
inference.pyandload_model()pick the right pair for you. - The model code is shipped in this repo (
modeling_mammoth.py,native_transformer.py, ...), which is why loading usestrust_remote_code=True. - Trained with MAMMOTH.