Skip to content

Latest commit

 

History

History
261 lines (176 loc) · 25.4 KB

File metadata and controls

261 lines (176 loc) · 25.4 KB

Introduction

Large Language Models (LLMs) have dramatically transformed how we process, generate, and interact with natural language in recent years. These models, which are part of the broader field of Natural Language Processing (NLP), represent a significant leap forward in the ability of machines to understand, manipulate, and generate human-like text. LLMs such as GPT-3, BERT, and T5 are widely recognized for their ability to perform a diverse range of language-related tasks, from summarization and text generation to machine translation and answering complex questions.

The power of LLMs stems not only from the sheer volume of data they are trained on but also from their underlying architecture—the Transformer model [1], which revolutionized the way sequence data is processed. These models often contain millions to hundreds of billions of parameters, with some like GPT-4 containing over 1 trillion parameters. These parameters encode relationships between tokens (sub-parts of words) and leverage next-word prediction and masked language modeling for effective learning. LLMs, as part of Generative AI, have emerged as the foundation for a wide range of intelligent applications.

Natural Language Processing (NLP)

Natural Language Processing (NLP) is a branch of Artificial Intelligence (AI) focused on enabling machines to understand, interpret, and generate human language. Traditional NLP approaches, such as n-grams and rule-based systems, have evolved into more sophisticated techniques involving deep learning and transformers, leading to better performance in tasks such as translation, text classification, and sentiment analysis.

Word2Vec

Introduced by Google in 2013, Word2Vec was one of the first deep learning models for generating word embeddings. It maps words into a continuous vector space based on their contextual usage, allowing words with similar meanings to have similar representations. Word2Vec introduced two training models: CBOW (Continuous Bag of Words) and Skip-gram, both of which allow models to predict word contexts from a given word or vice versa.

Recurrent Neural Networks (RNNs)

Before the rise of transformers, Recurrent Neural Networks (RNNs) were the dominant architecture for processing sequential data such as text. RNNs use loops to pass information from one time step to the next, allowing them to maintain a memory of previous inputs. However, they suffered from vanishing gradient problems, which made it hard to capture long-range dependencies in texts.

LSTM (Long Short-Term Memory)

LSTM networks, introduced by Hochreiter and Schmidhuber in 1997, solved many of the issues plaguing traditional RNNs by introducing gated mechanisms to control the flow of information through time steps. The input, output, and forget gates enable LSTM to remember long-term dependencies while preventing irrelevant information from affecting the prediction. LSTMs have been successfully used for various tasks, such as speech recognition and text generation.

Introduction to Generative AI

Generative AI is a subfield of AI focused on creating models that can generate new content. In the case of language models, this involves generating coherent and contextually appropriate text based on an input prompt. Generative models like GPT (Generative Pretrained Transformer) and T5 (Text-to-Text Transfer Transformer) excel at tasks such as code generation, creative writing, question answering, and more, all based on their ability to predict the next word or token in a sequence.

Introduction to Transformer Architecture

The Transformer architecture [1], introduced by Vaswani et al. in 2017, marked a paradigm shift in NLP by eliminating the need for recurrence or convolution to handle sequential data. The transformer model uses an attention mechanism to process all tokens in a sequence simultaneously, making it highly efficient at capturing dependencies across long sequences of text.

Transformers are built on two main blocks: the encoder and the decoder. The encoder processes the input sequence and generates a series of contextual embeddings (vector representations of tokens), while the decoder uses these embeddings to generate the output sequence. This architecture forms the backbone of many modern NLP models, including BERT and GPT.

https://jalammar.github.io/illustrated-transformer/

Encoder and Decoder

Encoder: The encoder takes the input sequence (text) and converts it into a series of vectors. Each vector represents the token and its position in the sequence. The encoder consists of multiple layers of multi-head self-attention and feed-forward networks. In tasks like text classification and named entity recognition, the encoder alone is used, as seen in BERT.

Decoder: The decoder generates text from the encoder's output, typically used in tasks like text generation or machine translation. It processes the embeddings and produces human-readable sequences of text. The decoder layers also have masked self-attention to ensure that each word generated is influenced only by previously generated words. This is the architecture used in GPT.

BERT - Bidirectional Encoder Representations from Transformers

BERT is a transformer-based model that only uses the encoder stack. It is trained to predict missing words in a sentence (called masked language modeling), making it suitable for tasks requiring a deep understanding of context, such as text classification, question answering, and language inference. Unlike previous models, BERT is bidirectional, meaning it considers both the left and right context of each word.

BERT-base: 12 encoder layers, 110 million parameters. BERT-large: 24 encoder layers, 340 million parameters. GPT - Generative Pretrained Transformer GPT models are auto-regressive transformers that only use the decoder part of the transformer architecture. They generate text one word at a time by predicting the next word in the sequence based on previous inputs. This form of self-supervised learning enables the model to handle diverse tasks such as text completion, summarization, and dialogue generation.

https://arxiv.org/abs/1810.04805

GPT Architecture

The GPT model is designed as an auto-regressive decoder, meaning it predicts one word at a time by incorporating previously generated words as inputs for future predictions. Its architecture is composed of multi-head attention layers and feed-forward layers, enabling it to learn complex dependencies across tokens.

GPT-3: Introduced in 2020, it consists of 96 transformer layers and 175 billion parameters, making it one of the largest LLMs at the time. It supports tasks such as zero-shot and few-shot learning, where the model can generalize from minimal examples.

GPT-4: A larger and more powerful successor with over 1 trillion parameters, GPT-4 expands its capabilities in generating more nuanced, contextually rich, and longer texts.

https://cdn.openai.com/research-covers/language-unsupervised/language_understanding_paper.pdf

Building an LLM

Building a large language model involves several steps, including data collection, preprocessing, architecture design, training, and fine-tuning.

Data Preparation

Data preparation involves gathering massive datasets, cleaning them, and ensuring high-quality text. Common datasets include Common Crawl, Wikipedia, and BookCorpus. Data must be tokenized and formatted in a way that the model can understand.

Tokenization

Tokenization involves breaking down text into individual units called tokens. These can represent words, subwords, or even individual characters. LLMs use subword tokenization to handle rare or unknown words effectively.

[BOS] - Beginning of Sequence: Marks the start of the text. [EOS] - End of Sequence: Marks the end of the text. GPT uses <|endoftext|>. [PAD] - Padding: Used to align texts to the same length for efficient batch processing. BPE - Byte Pair Encoding Byte Pair Encoding (BPE) is a tokenization technique that breaks words into smaller units. It is particularly useful for handling rare words and subword representations, allowing the model to generalize across a wider range of vocabulary.

Attention Mechanism

The attention mechanism allows the model to focus on specific parts of the input when making predictions. The core innovation of the transformer is the self-attention mechanism, which allows the model to compute the relevance of every word in the input to every other word, regardless of their distance in the sequence.

Self-attention: Calculates attention weights between all tokens in the input, allowing the model to capture relationships between distant tokens. Multi-head attention: Splits the attention mechanism into multiple "heads," allowing the model to attend to different parts of the input simultaneously.

https://arxiv.org/abs/1706.03762 https://nlp.seas.harvard.edu/2018/04/03/attention.html

LLM Architecture

LLMs typically consist of multiple layers of transformers, with each layer containing an attention mechanism and feed-forward networks. The size of the model (number of layers, attention heads, and parameters) affects its ability to capture language intricacies.

Pretraining

Pretraining involves training the model on large-scale datasets without explicit labels, using self-supervised learning objectives like next-word prediction or masked language modeling. Pretraining provides the model with a solid foundation of general knowledge, which can be adapted for specific tasks through fine-tuning.

Training Loop

The training loop iterates over the data, computing the loss function and updating the model's parameters using gradient descent. Modern frameworks such as PyTorch and TensorFlow are used to handle the computational complexity of training LLMs, often distributed across multiple GPUs or TPUs.

Model Evaluation

Evaluating LLMs involves assessing their performance on various NLP tasks, using metrics like perplexity, BLEU score (for machine translation), or ROUGE (for summarization). More advanced models are also tested on their ability to generalize across unseen tasks.

Finetuning an LLM

After pretraining a base model on large datasets, the next phase involves finetuning. Finetuning adapts the pretrained model to specific tasks or domains by continuing the training process on a narrower, task-specific dataset. This enables the model to excel in specialized applications such as sentiment analysis, machine translation, question-answering, or text classification.

Finetuning can either be done on general NLP tasks, where the model is trained for specific tasks, or domain-specific tasks, where the model is adjusted to particular fields like law, healthcare, or coding. During finetuning, the model learns to align its language understanding and generation abilities to specific task goals, often using supervised learning.

Instruction Finetuning

Instruction Finetuning involves teaching the LLM to follow human instructions and generate contextually relevant, goal-oriented outputs. Unlike traditional finetuning, which may only involve task-specific labeled data, instruction finetuning trains the model on data that includes a clear task prompt, followed by an expected response.

For example, in models like InstructGPT or Claude, instruction finetuning is used to make the model better at understanding user intents. This process uses datasets that include instructions (e.g., "Write a summary of this article," "Translate this text to French"). Models fine-tuned in this way are more effective at answering questions, writing essays, or completing tasks directly based on specific commands. Instruction finetuning often integrates Reinforcement Learning with Human Feedback (RLHF) to align the model's responses with human preferences, improving the quality and reliability of generated content.

Classification

In classification tasks, the goal is to predict predefined categories or labels for a given text input. During finetuning for classification, an LLM is trained on labeled datasets where each input corresponds to a specific class, such as sentiment (positive/negative), topic (sports/politics), or intent (question/command).

The model learns to map textual inputs to these predefined labels, often using a final softmax layer for multi-class problems. Finetuning a classifier requires task-specific datasets, like the IMDB movie reviews dataset for sentiment analysis, or AG News for topic classification. For binary classification tasks, the LLM uses datasets like Spam Detection (spam/ham emails) to predict a class with a confidence score.

Types of Finetuning

RLHF (Reinforcement Learning with Human Feedback)

Summary: RLHF is a method to align LLMs with human preferences by fine-tuning the model using human feedback. After generating responses, human evaluators rank them based on quality. The rankings are then used to train a reward model, which guides the LLM to produce preferred outputs using reinforcement learning. Applications: This method has been used in models like InstructGPT and ChatGPT to make them more aligned with user instructions and expectations. Paper: Learning to Summarize with Human Feedback (2020) by Ziegler et al. arXiv link: 2009.01325

DPO (Direct Preference Optimization)

Summary: DPO is an alternative to RLHF that simplifies the process by directly optimizing the model to align with human preferences without using a reward model. DPO modifies the standard supervised fine-tuning process by incorporating preference data where the model is trained to prefer responses with higher rankings based on human feedback. Applications: DPO reduces the complexity of RLHF while still achieving high alignment with human preferences. Paper: Direct Preference Optimization: Your Language Model is Secretly a Reward Model (2023) by Rafailov et al. arXiv link: 2305.18290

Memory-efficient Finetuning Techniques

LoRA (Low-Rank Adaptation of Large Language Models)

Summary: LoRA is a parameter-efficient fine-tuning method that reduces the computational cost of training large LLMs. It introduces low-rank updates to the model's weights during fine-tuning, allowing only a small subset of the model’s parameters to be updated. This approach significantly reduces memory usage and computation while retaining high performance. Applications: LoRA is widely used in scenarios requiring domain-specific or task-specific fine-tuning without the need to modify the entire model. Paper: LoRA: Low-Rank Adaptation of Large Language Models (2021) by Hu et al. arXiv link: 2106.09685

QLoRA (Quantized LoRA)

Summary: QLoRA extends LoRA by allowing efficient fine-tuning of quantized models. It uses 4-bit quantization to compress the model’s weights and then applies LoRA to perform fine-tuning. This method significantly reduces memory requirements, allowing LLMs with billions of parameters to be fine-tuned on a single GPU. Applications: QLoRA is ideal for developers who want to fine-tune very large models (e.g., GPT-3, LLaMA) on resource-constrained hardware while maintaining high accuracy. Paper: QLoRA: Efficient Finetuning of Quantized LLMs (2023) by Dettmers et al. arXiv link: 2305.14314

Retrieval Augmented Generation with LLM

Retrieval-Augmented Generation (RAG) combines the strengths of retrieval-based models and generative models. Traditional LLMs generate responses based solely on the data they were trained on. However, in retrieval-augmented systems, the model first retrieves relevant information from external sources (e.g., a document database, the web) and then incorporates that information into the generation process.

The retrieval mechanism helps LLMs provide more accurate, up-to-date, and contextually rich answers, especially for factual or knowledge-intensive queries. For instance, in applications like search engines, customer support bots, or knowledge assistants, the model retrieves related documents from a large corpus, processes them, and generates a context-aware answer.

Key components of RAG include:

Retriever: Searches for relevant documents or passages using methods like BM25 or dense retrieval. Generator: Generates human-like responses based on the retrieved documents. By leveraging RAG, models can access external knowledge, reducing the risks of hallucination (when a model generates inaccurate or invented information) and improving response reliability in real-world applications.

URL: https://arxiv.org/abs/2005.11401

Libraries that Were Used

Several key libraries were used in building and experimenting with LLMs. These libraries provide frameworks, utilities, and tools necessary for efficient training, deployment, and interaction with LLMs.

PyTorch: An open-source deep learning framework widely used in building LLMs. PyTorch provides tools for automatic differentiation, model parallelism, and GPU acceleration, making it essential for training models like GPT and BERT. It is highly flexible and allows researchers to prototype and iterate on models quickly. Hugging Face Transformers, a popular library for working with pretrained language models, is built on top of PyTorch.

LLamaIndex (GPT Index): LlamaIndex is a data framework that allows seamless integration between external knowledge bases and LLMs. It enhances the model’s ability to search and retrieve relevant information from structured datasets, which can then be used in applications like chatbots, Q&A systems, and research assistants. LlamaIndex helps enable retrieval-augmented generation, improving factual accuracy by using external knowledge sources.

Common Open-source LLM Weights

A wide variety of LLMs are available as open-source projects, making pretrained weights accessible to developers and researchers:

GPT-2: One of the earlier open-source LLMs released by OpenAI, GPT-2 has 1.5 billion parameters. It is widely used for tasks like text generation and summarization, and its weights are available via the Hugging Face Model Hub.

BERT: Google’s BERT is a bidirectional transformer designed for text classification, question answering, and named entity recognition. Weights for BERT-base (110M parameters) and BERT-large (340M parameters) are available for download.

GPT-J: A large language model with 6 billion parameters, developed by EleutherAI, GPT-J is an open-source alternative to GPT-3 and is useful for text generation tasks.

T5 (Text-to-Text Transfer Transformer): Developed by Google, T5 reformulates all NLP tasks as text-to-text tasks, meaning input and output are always text strings. Pretrained weights for T5 are available in multiple sizes, including small, base, and large.

BLOOM: A 176-billion-parameter open multilingual model developed by BigScience. It was trained to support text generation in over 40 languages and is one of the largest openly available models.

Publicly Available Datasets

Publicly available datasets are crucial for training and fine-tuning LLMs. Some key datasets include:

Dolma: A massive multilingual dataset designed for training LLMs. It consists of diverse data sources, including books, websites, research papers, and more. Dolma helps models learn across multiple languages and domains, making it an excellent resource for pretraining large-scale models.

Common Crawl: A dataset that provides snapshots of the web. It includes petabytes of web pages and is one of the most popular sources for pretraining models like GPT.

The Pile: Developed by EleutherAI, The Pile is an 800GB dataset that aggregates text from sources like PubMed, Wikipedia, arXiv, and books, designed for training LLMs.

Agentic AI

Agentic AI refers to AI systems that can operate autonomously and perform goal-oriented actions based on external stimuli and contextual information. These systems have some level of decision-making ability and can interact with users, environments, or other systems to achieve specific outcomes.

For example, in an Agentic AI-based personal assistant, the system might autonomously schedule meetings, send emails, and carry out other tasks based on user instructions. In the context of LLMs, agentic AI refers to integrating the model with real-time feedback loops and decision-making frameworks, allowing it to adapt and take actions without explicit user commands.

Publicly Available LLMs

Several publicly available LLMs can be accessed and utilized for research and development purposes:

GPT-2 and GPT-3: OpenAI has made various versions of GPT available via APIs and open-source repositories. GPT-2 is fully open-source, while GPT-3 can be accessed via the OpenAI API.

BLOOM: The 176-billion parameter open-access model, designed by BigScience for multilingual use, is one of the largest open LLMs available for research.

OPT: Developed by Meta AI, the OPT family of LLMs includes several open models ranging from 125M to 175B parameters.

FLAN-T5: Google’s FLAN-T5 fine-tuned T5 models are optimized for following instructions and achieving higher performance on a variety of NLP tasks.

Useful Repositories

Several repositories are central to the development, training, and deployment of LLMs:

Hugging Face Transformers: A central repository for pretrained transformer models, making it easy to download and fine-tune a wide range of LLMs. It includes models like BERT, GPT, and T5.

EleutherAI’s GPT-J Repository: Provides pretrained weights and code for training GPT-J, a 6-billion parameter LLM.

BigScience BLOOM: Contains code and pretrained weights for BLOOM, one of the largest open-access LLMs.

Resources for Further Study

To dive deeper into LLMs, transformers, and related technologies, the following books and papers are recommended:

Books

Papers

[1] Attention Is All You Need (2017)

Authors: Vaswani, Shazeer, Parmar, Uszkoreit, Jones, Gomez, Kaiser, and Polosukhin URL: https://arxiv.org/abs/1706.03762 Description: This foundational paper introduced the Transformer architecture, revolutionizing NLP with its attention mechanism and eliminating the need for recurrence.

[2] Improving Language Understanding by Generative Pre-Training (2018)

Authors: Radford, Narasimhan, Salimans, and Sutskever URL: https://cdn.openai.com/research-covers/language-unsupervised/language_understanding_paper.pdf Description: This paper introduced GPT (Generative Pretrained Transformer), focusing on pretraining a generative model for downstream tasks.

[3] BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding (2019)

Authors: Devlin, Chang, Lee, and Toutanova URL: https://arxiv.org/abs/1810.04805 Description: Introduces BERT (Bidirectional Encoder Representations from Transformers), a transformer-based model for natural language understanding with pretraining techniques like masked language modeling.

[4] Language Models are Few-Shot Learners (2020)

Authors: Brown, Mann, Ryder, Subbiah, Kaplan, Dhariwal, Neelakantan, Shyam, Sastry, Askell, Agarwal, Herbert-Voss, Krueger, Henighan, Child, Ramesh, Ziegler, Wu, Winter, Hesse, Chen, Sigler, Litwin, Gray, Chess, Clark, Berner, McCandlish, Radford, Sutskever, and Amodei URL: https://arxiv.org/abs/2005.14165 Description: Introduces GPT-3, a large-scale generative model with 175 billion parameters and the ability to perform few-shot learning.

[5] T5: Exploring the Limits of Transfer Learning with a Unified Text-to-Text Transformer (2020)

Authors: Raffel, Shazeer, Roberts, Lee, Narang, Matena, Zhou, Li, and Liu URL: https://arxiv.org/abs/1910.10683 Description: Introduces T5 (Text-to-Text Transfer Transformer), a model that reframes all NLP tasks into a text-to-text format.

[6] Exploring the Limits of Transfer Learning with GPT-2 (2019)

Authors: Radford, Wu, Child, Luan, Amodei, and Sutskever URL: https://cdn.openai.com/better-language-models/language_models_are_unsupervised_multitask_learners.pdf Description: Introduces GPT-2, demonstrating the effectiveness of large-scale language models in text generation tasks.

[7] Efficient Transformers: A Survey (2020)

Authors: Tay, Dehghani, Bahri, and Metzler URL: https://arxiv.org/abs/2009.06732 Description: A survey on various methods for improving the efficiency of transformers, covering techniques like sparse attention, low-rank factorization, and memory-efficient models.

[8] Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks (2020)

Authors: Lewis, Perez, Piktus, Petroni, Karpukhin, Goyal, Küttler, Lewis, Yih, Rocktäschel, Riedel, and Kiela URL: https://arxiv.org/abs/2005.11401 Description: Introduces Retrieval-Augmented Generation (RAG), a model that enhances generative transformers by incorporating external retrieval mechanisms.

[9] Scaling Laws for Neural Language Models (2020)

Authors: Kaplan, McCandlish, Henighan, Brown, Chess, Child, Gray, Radford, Wu, and Amodei URL: https://arxiv.org/abs/2001.08361 Description: Discusses the scaling behavior of neural language models, providing insight into how model performance improves with increased parameters, dataset size, and compute power.

[10] LLaMA: Open and Efficient Foundation Language Models (2023)

Authors: Touvron, Lavril, Izacard, Martinet, Lachaux, Lacroix, Rozière, Goyal, Hambro, Azhar, Rodriguez, Joulin, Grave, and Lample URL: https://arxiv.org/abs/2302.13971 Description: Introduces LLaMA (Large Language Model Meta AI), an open-source family of foundation language models.

[11] LoRA: Low-Rank Adaptation of Large Language Models (2021)

Authors: Hu, Shen, Wallis, Allen-Zhu, Li, Wang, Wang, and Chen: This paper introduces LoRA, a method for efficient fine-tuning of large language models using low-rank adaptation. URL: https://arxiv.org/abs/2106.09685

[12] QLoRA: Efficient Finetuning of Quantized LLMs (2023)

Authors: Dettmers, Lewis, Shleifer, Zettlemoyer: This paper introduces QLoRA, a technique that enables the fine-tuning of large-scale quantized language models on a single GPU. URL: https://arxiv.org/abs/2305.14314