OMOGANEYA Technical Academy 〜 OMOGANEYA Technical School 〜

Technical Academy

Computer Science & Engineering | LLM — Transformer Vol. 2: Input Token Embedding and Positional Encoding

To process text with a Transformer, the text must first be divided into tokens, and each token must then be converted into a vector composed of numerical values.

However, converting tokens into vectors alone does not represent where each token occurs within the token sequence.

Therefore, in a Transformer, a positional encoding representing the position within the token sequence is added to the input token embedding representing the token, and the resulting vector is provided to the model.

This article explains how the vectors provided to a Transformer are constructed by examining input token embeddings and positional encodings separately.

Transformer Configurations and the Scope of This Article

Transformers may use an encoder-only architecture, an encoder-decoder architecture, or a decoder-only architecture.

Figure 2-1 illustrates how a token sequence provided to an encoder is converted into a sequence of input vectors using input token embeddings and positional encodings, and then processed by multiple encoder blocks.

In an encoder-decoder architecture, the source-side token sequence is provided to the encoder, while the target-side token sequence is provided to the decoder.

On both the encoder and decoder sides, the token embeddings are multiplied by D\sqrt{D}, after which positional encodings are added.
However, the target-side token sequence provided to the decoder is shifted by one position so that the model can predict the next token at each position.

The decoder also receives the representations produced by the encoder and generates its output while referring to the source-side token sequence.

In a decoder-only architecture, no encoder is used.
The input token sequence is converted into token embeddings, positional information is added, and the decoder blocks then predict the next tokens sequentially.

Methods for providing positional information are not identical across all Transformers.
The sinusoidal positional encoding described in this article is one of the methods used in Transformer models.

Some models use other methods, such as learned positional embeddings whose values are updated during training.

Fig. 2-1 Input Token Embeddings and Positional Encodings in the Transformer Encoder

Figure 2-1 Input Token Embeddings and Positional Encodings in a Transformer Encoder

Vectors and Dimensions

Both input token embeddings and positional encodings are represented as vectors consisting of multiple numerical values.

For example, the following vector consists of four numerical values and is therefore a four-dimensional vector.

[0.20.70.10.4](2-1)\begin{bmatrix} 0.2 & 0.7 & -0.1 & 0.4 \end{bmatrix} \tag{2-1}


A Transformer uses vectors such as this to represent individual tokens.

In this article, the number of values forming a vector is denoted by DD. Therefore, a DD-dimensional vector contains DD numerical values.

Input Token Embeddings

In a Transformer, a DD-dimensional input token embedding is prepared for each token in the vocabulary VV.

The input token embedding corresponding to token ω\omega is expressed as follows.

𝒆ωD\boldsymbol{e}_{\omega} \in \mathbb{R}^{D}



D\mathbb{R}^{D} indicates a DD-dimensional vector whose elements are real numbers.

An input token sequence of length nn is expressed as follows.

(ω0,ω1,,ωn1)(2-3)\left( \omega_0, \omega_1, \ldots, \omega_{n-1} \right) \tag{2-3}


Here, ωi\omega_i represents the token at position ii in the token sequence.


Through the embedding layer, each token is converted into its corresponding DD-dimensional input token embedding.

ωi𝒆ωiD(2-4)\omega_i \longmapsto \boldsymbol{e}_{\omega_i} \in \mathbb{R}^{D} \tag{2-4}



Therefore, the sequence of input token embeddings corresponding to the input token sequence is expressed as follows.

(𝒆ω0,𝒆ω1,,𝒆ωn1)(2-5)\left( \boldsymbol{e}_{\omega_0}, \boldsymbol{e}_{\omega_1}, \ldots, \boldsymbol{e}_{\omega_{n-1}} \right) \tag{2-5}



The numerical values forming the input token embeddings are not manually assigned to individual tokens.
In a Transformer, the weights of the embedding layer are also learned during model training.

At this stage, each input token embedding is a vector corresponding to the token itself, and information representing its position within the token sequence has not yet been added.

Input Token Embedding Matrix

All input token embeddings held by the model can be represented collectively as the input token embedding matrix 𝑬\boldsymbol{E}.


𝑬=[𝒆1T𝒆2T𝒆|V|T]|V|×D(2-6)\boldsymbol{E} = \begin{bmatrix} \boldsymbol{e}_{1}^{\mathrm{T}}\\ \boldsymbol{e}_{2}^{\mathrm{T}}\\ \vdots\\ \boldsymbol{e}_{|V|}^{\mathrm{T}} \end{bmatrix} \in \mathbb{R}^{|V|\times D} \tag{2-6}



Here, |V||V| is the total number of tokens in the vocabulary VV, and DD is the dimensionality of each input token embedding.

Each row of the input token embedding matrix corresponds to one token.

Therefore, if the vocabulary contains |V||V| tokens and each token is represented by a DD-dimensional vector, the input token embedding matrix has dimensions |V|×D|V|\times D.

Why Positional Information Is Necessary

The input token embedding 𝒆ωi\boldsymbol{e}_{\omega_i} is a vector corresponding to token ωi\omega_i; it does not itself represent position ii in the token sequence.

A Transformer also processes relationships between tokens primarily through attention mechanisms, without using recurrent or convolutional neural networks.

Consequently, if only input token embeddings were used, the order of the tokens within the sequence could not be explicitly provided to the model.

Rearranging the tokens in the input token sequence shown in Equation (2-3) produces an ordering different from the original sequence.

However, the input token embeddings themselves contain no information indicating where their corresponding tokens occur within the token sequence.

Therefore, positional encodings are added to the respective input token embeddings to explicitly provide the token order to the Transformer.

Positional Encoding

Positional encoding is a method for representing the position of each token within a token sequence as a vector.

In sinusoidal positional encoding, the vector corresponding to each position is constructed by combining sine and cosine functions with different periods.

Sine and Cosine Functions

The sine function sin\sin and cosine function cos\cos vary repeatedly with a fixed period according to their input values.

For both functions, the output ranges from -1 to 1.

1sin(i)1,1cos(i)1(2-7)\begin{aligned} -1 &\leq \sin(i) \leq 1,\\ -1 &\leq \cos(i) \leq 1 \end{aligned} \tag{2-7}



Positional encoding combines multiple sine and cosine functions with different periods to construct a DD-dimensional vector corresponding to each position.

Calculating Positional Encodings

Let the DD-dimensional positional encoding corresponding to position ii in the token sequence be 𝒑i\boldsymbol{p}_i.

Each element of 𝒑i\boldsymbol{p}_i is calculated using the following equations.

pi,2k=sin(i100002k/D)(2-8)p_{i,2k} = \sin \left( \frac{i}{10000^{2k/D}} \right) \tag{2-8}


pi,2k+1=cos(i100002k/D)(2-9)p_{i,2k+1} = \cos \left( \frac{i}{10000^{2k/D}} \right) \tag{2-9}

where k=0,1,,D21k=0,1,\ldots,\frac{D}{2}-1.

The symbols represent the following quantities.


ii: Position within the token sequence

kk: Integer specifying a pair of sine and cosine functions

DD: Dimensionality of the positional encoding and input token embedding


In the positional encoding, sine functions are used for the even-numbered elements, while cosine functions are used for the odd-numbered elements.
Because the sine and cosine functions are used in pairs, DD is assumed to be even in the equations above.

The positional encoding 𝒑i\boldsymbol{p}_i corresponding to position ii is expressed as follows.

𝒑i=[sin(i)cos(i)sin(i100002/D)cos(i100002/D)sin(i10000(D2)/D)cos(i10000(D2)/D)](2-10)\boldsymbol{p}_i = \begin{bmatrix} \sin(i)\\ \cos(i)\\ \sin\left(\dfrac{i}{10000^{2/D}}\right)\\ \cos\left(\dfrac{i}{10000^{2/D}}\right)\\ \vdots\\ \sin\left(\dfrac{i}{10000^{(D-2)/D}}\right)\\ \cos\left(\dfrac{i}{10000^{(D-2)/D}}\right) \end{bmatrix} \tag{2-10}



Positional Encoding at Position 0

When the position is i=0i=0, the input to every sine and cosine function is 0.

sin(0)=0,cos(0)=1(2-11)\begin{aligned} \sin(0) &= 0,\\ \cos(0) &= 1 \end{aligned} \tag{2-11}



Therefore, the positional encoding corresponding to position 0 is a vector in which 0 and 1 alternate, as shown below.

𝒑0=[010101](2-12)\boldsymbol{p}_0 = \begin{bmatrix} 0\\ 1\\ 0\\ 1\\ \vdots\\ 0\\ 1 \end{bmatrix} \tag{2-12}


As position ii changes, the values of the respective sine and cosine functions also change, so the positional encoding corresponding to each position changes as well.


Wavelengths of Positional Encodings

In positional encoding, the wavelengths of the sine and cosine functions differ according to the vector dimension.

The wavelengths of the sine and cosine functions used in Equations (2-8) and (2-9) increase geometrically from 2π2\pi to 2π10000(D2)/D2\pi\cdot10000^{(D-2)/D}.

When DD is large, the maximum wavelength approaches 100002π10000\cdot2\pi.

Therefore, a single positional encoding contains values from multiple sine and cosine functions having different wavelengths.

Relative Positional Relationships

Sinusoidal positional encodings have the property that a fixed positional offset can be represented by a linear transformation.

For any fixed positional offset mm, the positional encoding 𝒑i+m\boldsymbol{p}_{i+m} at position i+mi+m can be expressed as a linear function of the positional encoding 𝒑i\boldsymbol{p}_i at position ii.

This property is expected to make it easier for the model to learn to attend based on relative positions.


Adding Input Token Embeddings and Positional Encodings

Both the input token embedding and positional encoding are DD-dimensional vectors.

Therefore, the input token embedding and positional encoding can be added element by element.

Let the token at position ii be ωi\omega_i, its input token embedding be 𝒆ωi\boldsymbol{e}_{\omega_i}, and the positional encoding corresponding to the same position be 𝒑i\boldsymbol{p}_i.

The vector 𝒙i\boldsymbol{x}_i provided to the Transformer is calculated using the following equation.

𝒙i=D𝒆ωi+𝒑i(2-13)\boldsymbol{x}_i = \sqrt{D}\boldsymbol{e}_{\omega_i} + \boldsymbol{p}_i \tag{2-13}



The input token embedding can be considered to be multiplied by D\sqrt{D} to bring the scale of the input token embeddings into balance with that of the positional encodings.

Calculating this for every position in the token sequence produces the following sequence of input vectors.

(𝒙0,𝒙1,,𝒙n1)(2-14)\left( \boldsymbol{x}_0, \boldsymbol{x}_1, \ldots, \boldsymbol{x}_{n-1} \right) \tag{2-14}



Why Sinusoidal Positional Encoding Was Adopted

In the original Transformer research, fixed sinusoidal positional encodings were compared experimentally with learned positional embeddings whose values are updated during training.

In these experiments, the fixed positional encodings and learned positional embeddings produced nearly identical results.

However, sinusoidal positional encodings may also be applicable to token sequences longer than those encountered during training.

Taking this possibility into consideration, fixed positional encodings were adopted.


Summary

In a Transformer, each token in the vocabulary is converted into a DD-dimensional input token embedding.

However, input token embeddings alone cannot represent the positions or order of tokens within a token sequence.

Therefore, positional encodings calculated using sine and cosine functions are added to the input token embeddings.

The input vector corresponding to token ωi\omega_i at position ii is expressed by the following equation.


𝒙i=D𝒆ωi+𝒑i\boldsymbol{x}_i = \sqrt{D}\boldsymbol{e}_{\omega_i} + \boldsymbol{p}_i



By arranging the input vectors calculated for each position as rows, an n×Dn\times D input matrix is constructed.

This input matrix is processed by the Transformer blocks and transformed through self-attention and other mechanisms into contextualized representations that reflect the relationships between tokens.

About This Article

References
Introduction to Large Language Models, supervised and written by Ikuo Yamada; written by Masatoshi Suzuki, Kosuke Yamada, and Linghan Li
・Ashish Vaswani et al., “Attention Is All You Need,” Advances in Neural Information Processing Systems 30, 2017.
・Alec Radford et al., “Improving Language Understanding by Generative Pre-Training,” OpenAI, 2018.


※This article was prepared with reference to the sources listed above and organized based on the author’s understanding.


Written by

Company
Lightcone Technology Inc.
Business
Planning, Development, and Operation of LLM-Powered AI Services
Planning, Development, and Operation of Web and Mobile Applications
Website Planning and Development
Research and Development of AI Foundation Technologies
URL
https://lc-techno.com/
Contact
info@lc-techno.com
View Company Page
contact

Feel free to contact us about listing your products or services, questions on existing listings, or procurement, sales, and trade support through OMOGANEYA.