In 2017, the Attention Is All You Need paper introduced the transformer, a neural network architecture which has become the dominant type of machine learning model, especially in the area of large language models (LLMs).
This page and the accompanying Jupyter notebook describe an attempt at recreating the training setup described in the paper and recreating some of its results.
While LLMs are nowadays mostly "decoder-only" transformers, the original transformer was an encoder-decoder sequence-to-sequence model used primarily for machine translation. In other words, it was a translation machine. The following describes the process of building such a machine for English-German translation (in the paper they did this for English-German and English-French).
Attention Is All You Need was a contribution to the Conference on Neural Information Processing Systems (NIPS, called NeurIPS since 2018) in 2017 and used data of the machine translation task of the 2014 Workshop on Statistical Machine Translation (WMT14). The dataset for the English-German language pair consists of three parallel corpora (collections of sentences/text snippets in multiple languags):
The WMT14 task description page also lists the test set, "newstest2014" as well as the recommended dev/validation set "newstest2013" (the test set of WMT13).
When inspecting the training data, it becomes clear that the Common Crawl Parallel Corpus portion of the WMT14 dataset introduces noticeable noise. A manual review of random samples shows that some sentence pairs are not actual translations:
| Line | English | German | Actual meaning |
|---|---|---|---|
| 121 | The person in the image has tattoos all around the chest area. I've tried so many times to cover the tattoos and couldn't. | Der Noise Buster ist ein wirklich gutes Programm. | The Noise Buster is a really good program. |
| 200436 | Nearby 20 golf courses, horse riding, malt whisky trail, fishing. | In einigen der Gästezimmer können Sie sogar in Himmelbetten nächtigen. | In some of the guest rooms, you can even sleep in four-poster beds. |
| 1000144 | The initiative’s patron is President Horst Koehler. | Prämiert werden Orte, die zukunftsorientierte Ideen entwickeln und aktiv umsetzen. | What is being awarded are places that develop forward-looking ideas and actively implement them. |
In other cases, the entries are not even bilingual:
| Line | English | German |
|---|---|---|
| 8 | ACDSee 9 Photo Manager Organize your photos. Share your world. | Translator Internet is a Toolbar for MS Internet Explorer. |
| 100015 | The Beeches Hotel & Victorian Gardens is country-house accommodation just a few minutes’ walk from the centre of Norwich. | Stay at the Ramada Norwich and experience everything that you would expect from an international hotel company. |
| 1234002 | Sitges Hotel / Hotel San Sebastian Playa is at a privileged spot, washed by the Mediterranean and blessed with a microclimate... | Barcelona Hotel / The Amrey Diagonal is on the Diagonal Avenue, only 300 metres away from the Plaça de les Glòries Catalanes ... |
This is likely due to how the corpus was created. The dataset was generated automatically from the Common Crawl web archive using a large-scale alignment procedure: First, pages whose URLs look like they are available in multiple languages were selected, e.g. https://example.com/page_en.html and https://example.com/page_de.html. Then it was assumed there is a direct correspondence between the sentences on those pages, and they were matched. This rather crude algorithm leads to a lot of data, however the data is quite noisy, in other words a substantial part of the translations are completely off. However, it seems like giving the model more data to train on, even if the quality is not the best, is beneficial.
In a first step, a cleaning script is applied to all the input text file pairs to do the following:
All text gets tokenized using a vocabulary of 32768 tokens. Any sentence pairs where any of the two sequences exceeds 176 tokens is filtered out and the three training set corpora (Europarl, Common Crawl and News Commentary) get merged into one big dataset. This leaves about 4.45M sentences, about 70K (1.6%) of the original dataset have been filtered out by the cleaning and truncating procedures.
To reduce padding, the sentence pairs are sorted into buckets according to the length of the longest of the two sequences of each pair.
The model architecture follows the base configuration in the Attention Is All You Need paper: An encoder-decoder transformer with 6 encoder and 6 decoder layers, 512 model dimensions, 8 attention heads. The feed-forward layers have 2,048 dimensions. The vocabulary has a size of 32,768. The model has 77M parameters.
We train for 15 epochs on a machine with eight RTX 5090 GPUs. One epoch takes about three minutes. The rental cost for the machine is roughly $4 per hour, so one full training run takes about 45 minutes and costs approximately $3 (and consumes about 0.75h × 8 × 600W = 3.6kWh of energy). For comparison, the original Attention Is All You Need paper reported a training time of around 12 hours for a comparable number of tokens.
The cross-entropy loss goes down to about 2.4, or equivalently a perplexity of about 11. The Attention Is All You Need paper reached a perplexity of 4.92 for the base model.
On BLEU (Newstest 2014 testset), the model achieves a score of 19.1, while the value reported in the paper is 25.8.
So overall, the goal of matching the results of the paper has not fully been achieved (yet). However, the translation is somewhat working.
The following possible optimizations have not been implemented yet and might lead to improvements in the future: