AI, But Simple

Alignment In Transformers, Simply Explained

Free editionPRO edition

This week’s PRO issue is accompanied by a Colab notebook that includes a walkthrough of Alignment using a simple example and a pretrained model. As always, the code in this issue can be executed on a normal home PC and does not require a large amount of VRAM.

Transformer models are the most widely used architecture in AI at the moment. Some of its most popular uses are Natural Language Processing (NLP) tasks and multimodal tasks such as Image Captioning.

Transformers are one of the key architectures that tackle the problem of alignment.

Well, what exactly is alignment? In NLP, if we look at language translation, the difference in order and context of words forms a sentence’s meaning—we must figure out which input words correspond to which target words.

  • This process is essentially what alignment is: figuring out which part(s) of the input corresponds to which part(s) of the output.

In traditional models, alignment had to be solved as a separate task, but with the arrival of transformers and their “self-attention” mechanism, alignment became integrated within the model.

Because of this “self-attention,” transformers automatically learn the relationship between different parts of an input, such as the relationship between words in a sentence. Alignment is no longer separate but learned as part of training.

To better understand what alignment is and its math intuition, we’ll go through the basic mathematical framework of the alignment problem:

Given an input sequence X:

And a target sequence Y:

The alignment problem seeks to find a mapping that matches elements in the target sequence Y to elements in the input sequence X. This is typically done via an alignment mapping function:

This mapping function is learned by the model, whether that is a transformer, RNN, or other model. If we denote the mapping function as A, A(j) = i means that yi is aligned with xi.

Consider this simple English to French translation example:

A simple mapping function that makes logical sense in translation is A(1) = 1, A(2) = 2, and A(3) = 3.

The French translation of “I” is “Je,” the translation of “am” is “suis,” and the translation of “happy” is in fact “content.”

Here, it is important to remember one input token can be aligned with more than one target token, and vice versa.

This edition is for PRO members.

More in Safety

Safety 6
  1. #101 Safety PRO edition

    Reward Hacking, Simply Explained

  2. #79 Safety PRO edition

    LLM Security (Prompt Injection, Adversarial Attacks), Simply Explained

  3. #70 Safety PRO edition

    Subliminal Learning, Simply Explained