Choosing Sequence Lengths
The filtertoolong transform drops training pairs whose source is longer than src_seq_length_max tokens or whose target is longer than tgt_seq_length_max tokens. Set the limits too low and you throw away good data; set them too high and a few very long pairs make batches large and slow. optimize_seq_lengths tokenizes your data with your tokenizers and suggests limits that keep a chosen share of it (99% by default).
python -m mammoth.scripts.optimize_seq_lengths config.yaml
Unlike the memory profiler, this tool reads and tokenizes the data files, so it needs the data and the tokenizers on disk. On a large config it can take a while. Tokenization runs in batches on all available CPU cores, so on LUMI or Roihu run it as a CPU job with several cores (see the CSC quickstart), and use --max-samples to profile only the first lines of each task.
How it works
For each task in
tasks, it readspath_srcandpath_tgt(andpath_valid_src/path_valid_tgtif present).It finds the tokenizer of each file from its language. The language is the last extension of the file name (
train.bg→bg,eng-spa.eng.gz→eng), looked up insrc_vocab/tgt_vocab. Tasks whose tokenizer cannot be found are skipped and listed in the report.It counts the tokens of every line on both sides.
Over all tasks together, it takes the 99th percentile of the source lengths and of the target lengths, and rounds each up to a multiple of 64.
Reading the report
The report has four parts:
Current settings: the values in the config now.
Training set per-task statistics: for each task, the number of pairs, the median (P50) and 99th percentile (P99) length of each side, and the P99 of the longer side of each pair. A task with much higher numbers than the others has unusually long sentences, which is worth a look.
Validation set per-task statistics: the same for the validation files, if the tasks have them.
Recommended settings: the suggested
src_seq_length_maxandtgt_seq_length_max, next to the current values, and the share of training (and validation) pairs that the suggestion keeps.
Copy the suggested src_seq_length_max and tgt_seq_length_max into your config. The report also suggests max_length and valid_max_length. Those limit how many tokens are generated when translating (and when translating the validation set), so they do not belong in the training config.
Things to keep in mind
The tool counts the tokens that the tokenizer produces. With Hugging Face tokenizers, MAMMOTH adds a beginning and an end of sequence token itself, and
filtertoolonglowers both limits by 2 to make room for them. The share of data kept in training is therefore slightly below the reported percentage.The percentile is global: a small task with long sentences can lose more than 1% of its data while the total loses 1%. Check the per-task P99 column for that.
Only the
filtertoolongtransform usessrc_seq_length_max/tgt_seq_length_max. Tasks without it in theirtransformsare not filtered.
Options
Option |
Effect |
|---|---|
|
The training YAML (positional). |
|
Keep this percentage of the data (default |
|
Profile only the first N pairs of each task (default: all). |