# LLM Training

**These are my notes from the very recommended book** [**https://www.manning.com/books/build-a-large-language-model-from-scratch**](https://www.manning.com/books/build-a-large-language-model-from-scratch) **with some extra information.**

## Basic Information

You should start by reading this post for some basic concepts you should know about:

{% content-ref url="llm-training-data-preparation/0.-basic-llm-concepts" %}
[0.-basic-llm-concepts](llm-training-data-preparation/0.-basic-llm-concepts)
{% endcontent-ref %}

## 1. Tokenization

{% hint style="success" %}
The goal of this initial phase is very simple: **Divide the input in tokens (ids) in some way that makes sense**.
{% endhint %}

{% content-ref url="llm-training-data-preparation/1.-tokenizing" %}
[1.-tokenizing](llm-training-data-preparation/1.-tokenizing)
{% endcontent-ref %}

## 2. Data Sampling

{% hint style="success" %}
The goal of this second phase is very simple: **Sample the input data and prepare it for the training phase usually by separating the dataset into sentences of a specific length and generating also the expected response.**
{% endhint %}

{% content-ref url="llm-training-data-preparation/2.-data-sampling" %}
[2.-data-sampling](llm-training-data-preparation/2.-data-sampling)
{% endcontent-ref %}

## 3. Token Embeddings

{% hint style="success" %}
The goal of this third phase is very simple: **Assign each of the previous tokens in the vocabulary a vector of the desired dimensions to train the model.** Each word in the vocabulary will a point in a space of X dimensions.\
Note that initially the position of each word in the space is just initialised "randomly" and these positions are trainable parameters (will be improved during the training).

Moreover, during the token embedding **another layer of embeddings is created** which represents (in this case) the **absolute possition of the word in the training sentence**. This way a word in different positions in the sentence will have a different representation (meaning).
{% endhint %}

{% content-ref url="llm-training-data-preparation/3.-token-embeddings" %}
[3.-token-embeddings](llm-training-data-preparation/3.-token-embeddings)
{% endcontent-ref %}

## 4. Attention Mechanisms

{% hint style="success" %}
The goal of this fourth phase is very simple: **Apply some attetion mechanisms**. These are going to be a lot of **repeated layers** that are going to **capture the relation of a word in the vocabulary with its neighbours in the current sentence being used to train the LLM**.\
A lot of layers are used for this, so a lot of trainable parameters are going to be capturing this information.
{% endhint %}

{% content-ref url="llm-training-data-preparation/4.-attention-mechanisms" %}
[4.-attention-mechanisms](llm-training-data-preparation/4.-attention-mechanisms)
{% endcontent-ref %}

## 5. LLM Architecture

{% hint style="success" %}
The goal of this fifth phase is very simple: **Develop the architecture of the full LLM**. Put everything together, apply all the layers and create all the functions to generate text or transform text to IDs and backwards.

This architecture will be used for both, training and predicting text after it was trained.
{% endhint %}

{% content-ref url="llm-training-data-preparation/5.-llm-architecture" %}
[5.-llm-architecture](llm-training-data-preparation/5.-llm-architecture)
{% endcontent-ref %}

## 6. Pre-training & Loading models

{% hint style="success" %}
The goal of this sixth phase is very simple: **Train the model from scratch**. For this the previous LLM architecture will be used with some loops going over the data sets using the defined loss functions and optimizer to train all the parameters of the model.
{% endhint %}

{% content-ref url="llm-training-data-preparation/6.-pre-training-and-loading-models" %}
[6.-pre-training-and-loading-models](llm-training-data-preparation/6.-pre-training-and-loading-models)
{% endcontent-ref %}

## 7.0. LoRA Improvements in fine-tuning

{% hint style="success" %}
The use of **LoRA reduce a lot the computation** needed to **fine tune** already trained models.
{% endhint %}

{% content-ref url="llm-training-data-preparation/7.0.-lora-improvements-in-fine-tuning" %}
[7.0.-lora-improvements-in-fine-tuning](llm-training-data-preparation/7.0.-lora-improvements-in-fine-tuning)
{% endcontent-ref %}

## 7.1. Fine-Tuning for Classification

{% hint style="success" %}
The goal of this section is to show how to fine-tune an already pre-trained model so instead of generating new text the LLM will select give the **probabilities of the given text being categorized in each of the given categories** (like if a text is spam or not).
{% endhint %}

{% content-ref url="llm-training-data-preparation/7.1.-fine-tuning-for-classification" %}
[7.1.-fine-tuning-for-classification](llm-training-data-preparation/7.1.-fine-tuning-for-classification)
{% endcontent-ref %}

## 7.2. Fine-Tuning to follow instructions

{% hint style="success" %}
The goal of this section is to show how to **fine-tune an already pre-trained model to follow instructions** rather than just generating text, for example, responding to tasks as a chat bot.
{% endhint %}

{% content-ref url="llm-training-data-preparation/7.2.-fine-tuning-to-follow-instructions" %}
[7.2.-fine-tuning-to-follow-instructions](llm-training-data-preparation/7.2.-fine-tuning-to-follow-instructions)
{% endcontent-ref %}
