Large language model
A large language model (LLM) is a machine learning model implemented with a deep neural network containing billions or more parameters and trained on extensive text corpora. In this context, "large" denotes both the number of parameters and the size of the training dataset, which in modern systems can encompass several petabytes and trillions of tokens. LLMs are typically trained using self-supervised or semi-supervised approaches that involve predicting the next token in a sequence, enabling the model to learn statistical patterns in language. Increases in parameters, data volume, and computational steps have been shown to yield predictable improvements in performance, as demonstrated by scaling laws.
Since the introduction of BERT (2017) and particularly GPT-3 (2020), large language models have become the dominant approach in natural language processing. Contemporary models, including GPT-4o, Claude 3, Gemini 1.5, and LLaMA 3, are capable of generating text and program code, translating, summarizing, answering questions, and constructing chains of reasoning without specialized tuning. Multimodal variants can also process images, audio, and video. Adaptation to specific applications is achieved through fine-tuning or prompt engineering, also known as in-context learning. Despite these advancements, LLMs inherit biases and inaccuracies from their training data, are susceptible to "hallucinations," and demand substantial computational resources. As a result, ongoing research emphasizes behavioral alignment, corpus filtering, and the development of energy-efficient architectures.
Architecture
Modern LLMs almost exclusively use the Transformer architecture—a network featuring a self-attention mechanism. The Transformer model was first proposed in the 2017 paper "Attention is All You Need" by researchers at Google.
The Transformer architecture is a fundamental neural network design for processing sequences, comprising two main modules: an encoder (which encodes the input) and a decoder (which generates the output). An input sequence is received, its vector representation (embedding) is created, a positional encoding vector is added, and then the set of elements, regardless of their order in the sequence, is fed into the encoding component (parallel processing). The decoding component then receives part of this sequence and the output from the encoder. The result is a new output sequence.
The encoding component of a Transformer consists of several identical encoder layers; the decoding component is structured similarly. The Transformer itself is a sequence of attention models that transform the original sequence of vectors into a new sequence where each element considers the context of the others. The encoder forms hidden representations of the input data, preserving information about the relationships between elements. The decoder uses these hidden representations to create a new sequence of embeddings for the output tokens. These embeddings are then used by the language model to generate the final output elements.
Since Transformers were originally developed for tasks like machine translation, their architecture includes an encoder (processing the input text, such as a source sentence) and a decoder (generating the output, such as a translation). However, many language models use only the decoder part, operating in an autoregressive mode.
Transformers are used in three main configurations, each utilizing the encoder and decoder differently and tailored to its specific set of tasks:
- Encoder-only Transformers (bidirectional) are trained to restore intentionally hidden fragments of text, making them well-suited for "understanding" tasks such as classification, fact extraction, and semantic search.
- Decoder-only Transformers (autoregressive) are optimized to predict the next token and are used for tasks requiring sequential output, such as conversational agents, code completion, and creative text generation.
- Full encoder-decoder architectures combine both approaches: the encoder builds a representation of the entire input text, and the decoder generates the result step-by-step based on this representation. This configuration is most effective for machine translation, summarization, and question-answering systems.
Tokenization
Tokenization is a key initial step in text processing for large language models. In this stage, a continuous string of characters is broken down into individual units called tokens. Tokenization transforms a sequence of characters into a sequence of structured elements, enabling the neural network to process it efficiently.
From a linguistic perspective, tokenization could be seen as analogous to identifying the smallest units of language that carry independent meaning or functional weight, such as words, morphemes, or their fragments. However, a token is often just a statistically frequent sequence of characters, so it is important not to overestimate the linguistic meaningfulness of all tokens. Depending on the chosen scheme, a token can be a whole word, a subword, a single character, or a special marker (e.g., start and end of sequence markers).
Tokenization allows for:
- Limiting the vocabulary size to a manageable scale;
- Correctly handling rare and new words;
- Ensuring a one-to-one mapping of text to a sequence of numerical identifiers.
Subword algorithms are used for text segmentation, with the most common being Byte Pair Encoding (BPE), WordPiece, and UnigramLM. Each of these builds a vocabulary from the most frequent fragments in a corpus and uses it to segment any input text sequentially.
After the tokenization stage, each text unit—a token—is converted into a numerical representation that the neural network can understand. This process involves several sequential steps:
- Converting tokens to identifiers: Each token is mapped to a unique numerical index based on a pre-built token vocabulary. Textual tokens are replaced with their unique numerical identifiers (IDs). Each ID is the token's number in a pre-built dictionary, allowing the neural network to work with numbers instead of words.
- Converting identifiers to embeddings: For each token ID, a corresponding fixed-dimensional vector—an embedding—is retrieved or computed. This multidimensional numerical representation replaces the ID and already contains information about the token's meaning and contextual properties. All embeddings have the same length for ease of processing.
- Adding positional encodings: Since the Transformer architecture itself does not consider the order of elements, positional encodings are added to the embeddings to provide information about the token's position in the sequence. In other words, this allows the model to "know" which token comes first, second, third, and so on in a sentence.
- Forming input matrices: The output is a matrix of size
[sequence length × embedding dimension], which serves as the initial representation of the text and is passed to the input of the neural network, specifically to the self-attention blocks of the Transformer. Each row of this matrix corresponds to one token, and the vector it contains carries both its semantic meaning (embedding) and information about its position in the text (positional encoding).
Text → Tokens → IDs → Embeddings + Positional Encodings → Model Input
Attention Mechanism
The attention mechanism is a key component of the Transformer architecture that enables the model to account for dependencies between tokens regardless of the distance between them in a sequence. After the input text is converted into a sequence of vectors, this sequence is fed into the central element of the transformer—the attention block.
The attention mechanism is a way for the neural network to determine which parts of the input data to focus on more when processing each element of the sequence. It allows the vectors of individual text fragments to interact with each other, enriching them with information and updating their values based on the surrounding context. This enables the model to effectively capture both local and long-range dependencies between tokens, significantly enhancing its ability to interpret complex text structures.
In natural language, the meaning of a word or phrase is not determined in isolation; it depends on the context provided by other words and structures. In neural networks, text is encoded through vector representations—embeddings—that numerically reflect the lexical and syntactic properties of tokens. Without a direct attention mechanism (as in the Transformer), contextual information would either be lost over long distances (as in simpler models) or transmitted sequentially, which is less effective for capturing long-range relationships. However, in natural language, the meaning of a word or syntactic construction is dynamic, and its interpretation must adapt to the context. The attention mechanism contextualizes these vector representations, which means:
- Each token assesses its importance relative to other tokens in the sentence.
- During the update process, vector representations are enriched with mutual information, reflecting syntactic dependencies, semantic roles, and pragmatic context.
As a result of this information exchange, the updated vectors begin to encode not only the meaning of the token itself but also its grammatical relationships (syntax), its role in the described situation (semantics), and its overall meaning in context (pragmatics).
Input Token Vectors (with positions) → Attention Mechanism (Vector Interaction) → Contextualized Token Vectors (Vectors enriched with information about relationships with other tokens)
Technical Implementation of the Attention Mechanism
The internal workings of the attention mechanism involve several key computational steps and components. For each input vector, three vectors are generated: a Query, a Key, and a Value. Based on their interaction, attention weights are calculated, which are then used to obtain updated, contextualized vector representations. A common approach is to use a Multi-Head Attention architecture for parallel information processing.
Query, Key, and Value
At the core of the attention mechanism's computation is the transformation of each input vector (which is the sum of a token's embedding and its positional encoding) into three distinct vector representations: Query (Q), Key (K), and Value (V).
Conceptually, these three vectors perform the following roles in the attention mechanism:
- Query (Q): Represents the current token's vector, which initiates the process of finding relevant information in the sequence. It can be thought of as a "question" or "probe" used to assess the importance of other tokens relative to the current one.
- Key (K): Acts as an identifier or "label" describing an aspect of each token's content. The Query (Q) vector of the current token is compared with all Key (K) vectors in the sequence (including its own) to determine their degree of correspondence or relevance.
- Value (V): Contains the actual information or representation associated with each token that will be passed on. After calculating the attention weights based on the interaction of Queries and Keys, these weights are applied to the Value vectors to form the final weighted representation, which is the output of the attention mechanism for that token.
Training Large Language Models
LLM training primarily occurs in two stages:
- Pretraining: At this stage, the model is trained on large, unlabeled text corpora using self-supervised learning. The task is to predict the next token in a sequence (autoregression) or to restore masked fragments (masked language modeling). Pretraining allows the model to learn broad statistical patterns of language, grammar, facts about the world, and basic forms of reasoning.
- Fine-tuning: After pretraining, the model is further trained on specialized data to perform specific tasks, such as generating answers, classifying text, or following instructions. Modern approaches include:
- Supervised fine-tuning on labeled datasets.
- Reinforcement Learning from Human Feedback (RLHF) to adjust the model's behavior according to target metrics for quality, safety, and helpfulness.
Problems and Limitations
Despite impressive progress, modern Large Language Models (LLMs) have a number of problems and limitations:
I. Computational and Architectural Limitations
- High Computational Costs: Training and operating LLMs require significant computational power, time, and energy, leading to high economic and environmental costs. Additionally, the autoregressive nature of generation (sequential token creation) limits parallelization and slows down inference speed compared to non-autoregressive approaches.
- Context Length Limitation: The Transformer architecture has a quadratic dependency of computational costs and memory requirements on sequence length. This forces a fixed limit (context window) on the amount of text the model can process at one time, leading to the truncation of long documents and loss of information beyond the window.
II. Reliability and Accuracy Issues in Generation
- Hallucinations: The generation of factually incorrect but plausible-sounding information. This is due to the model's lack of true world understanding, reliance on statistical patterns in data, and inability to verify its generated statements.
- Error Propagation: A process where errors made in the early stages of generation are amplified, leading to an accumulation of inaccuracies in subsequent steps, which degrades the overall quality and coherence of the text.
- Limited Compositionality: Difficulties with tasks requiring multi-step logical reasoning or precise calculations (e.g., multiplying large numbers, solving puzzles). The accuracy of models in such tasks drops sharply as complexity increases due to the autoregressive nature of generation.
- Repetition: A tendency to excessively repeat words or phrases, which reduces the informativeness and readability of the text. This is related to training specifics and decoding algorithms (selection of the next token).
- Reversal Curse: The model's inability to automatically generalize knowledge in the reverse direction: after being trained on the statement "A is B," the model often cannot deduce that "B is A."
- Creativity-Accuracy Trade-off: The need to balance between generating diverse, original responses and maintaining factual accuracy. Enhancing one aspect often negatively affects the other; for example, high creativity can correlate with an increase in hallucinations.
III. Interaction and Controllability Issues:
- Poor Controllability: The difficulty of precisely controlling the style, tone, and content of generated text or ensuring adherence to complex instructions. Controllability is highly dependent on the quality of input instructions ("prompts") and fine-tuning methods.
- Sensitivity to Phrasing: Minor changes in the input prompt can lead to significantly different responses, even if the semantics of the query remain the same. This makes it difficult to obtain stable and predictable results.
IV. Ethical and Social Aspects:
- Bias and Fairness Issues: Models can reproduce and amplify existing social stereotypes, biases, or toxicity present in their training data. Ensuring fairness and safety in models is a complex challenge.
- The Alignment Problem (LLM Alignment): A broader problem that includes the previous point. It is the task of ensuring that a model's behavior aligns with human values, intentions, and ethical norms. It involves combating bias, hallucinations, the generation of harmful content, and improving controllability.
- Risks of Malicious Use: The potential for LLMs to be used for creating and mass-distributing disinformation, phishing, spam, malicious code, or generating convincing fake texts. This poses threats to information and personal security and undermines societal trust.
External links
- Large Language Model // Wikipedia (English version)
- Grand Modèle de Langage // Wikipédia (French version)
- Sprachmodell // Wikipedia (German version)
- Naveed H. et al. // A Comprehensive Overview of Large Language Models // arXiv:2307.06435, 2023
Literature
- Vaswani, A. et al. (2017). Attention Is All You Need. arXiv:1706.03762.
- Devlin, J. et al. (2019). BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding. arXiv:1810.04805.
- Brown, T. et al. (2020). Language Models Are Few-Shot Learners. arXiv:2005.14165.
- Kaplan, J. et al. (2020). Scaling Laws for Neural Language Models. arXiv:2001.08361.
- Hoffmann, J. et al. (2022). Training Compute-Optimal Large Language Models. arXiv:2203.15556.
- Ouyang, L. et al. (2022). Training Language Models to Follow Instructions with Human Feedback. arXiv:2203.02155.
- Bai, Y. et al. (2022). Constitutional AI: Harmlessness from AI Feedback. arXiv:2212.08073.
- Bubeck, S. et al. (2023). Sparks of Artificial General Intelligence: Early Experiments with GPT-4. arXiv:2303.12712.
- OpenAI. (2023). GPT-4 Technical Report. arXiv:2303.08774.
- Touvron, H. et al. (2024). The Llama 3 Herd of Models. arXiv:2407.21783.