# Foundations of LLMs Lesson 1

**Learning objectives:**

* Have a good mental model of when to train or fine-tune LLMs
* Understand high level the key pieces to make it work successfully
* Understand why and how to participate in the Neurips LLM Efficiency Challenge

### Introduction&#x20;

<figure><img src="https://748712899-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZcqmeHkGECPaayJZTNEt%2Fuploads%2FtX9b8mvJceo9EdDHskS0%2Fimage.png?alt=media&#x26;token=ea1eb35f-b003-477f-b6d4-41f7eecd7600" alt="" width="375"><figcaption><p>LLM keyphrase usage in arxiv papers from 2020</p></figcaption></figure>

<figure><img src="https://748712899-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZcqmeHkGECPaayJZTNEt%2Fuploads%2Fd4KSqDKKdqUFBiys0jMF%2Fimage.png?alt=media&#x26;token=37b40e2f-5bd1-4052-8f54-0b1422d5e7ec" alt=""><figcaption><p>Many are using LLMs in </p></figcaption></figure>

LLMs are decoder-only models. Trained to predict the next word in the sequence.&#x20;

### When to Train or fine-tune?&#x20;

<figure><img src="https://748712899-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZcqmeHkGECPaayJZTNEt%2Fuploads%2FOJlJdt9BJLyXjL3zzdif%2Fimage.png?alt=media&#x26;token=905f6a88-900b-4f26-add3-0cc22148c09d" alt=""><figcaption><p>Slide on resons for finetuning LLMs</p></figcaption></figure>

* &#x20;Pre-train/ train the LLM when you want to have full control over the training data.&#x20;
* Finetune the open-source LLM when you have to have more control but want to have cheaper inference.&#x20;
* Use commercial APIs when you have to reduce the time to market.&#x20;

#### Finetuning the model --Types:&#x20;

* **Task specific**: When we have a task specified we can finetune the model to do that task based on a certain prompt or instruction. ![](https://748712899-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZcqmeHkGECPaayJZTNEt%2Fuploads%2FdREk25WbTfvzd87EhWNn%2Fimage.png?alt=media\&token=9a05ba22-bea0-44e4-8440-044d6cc45a37)
* **Instruction tuning and RLHF** \
  instruction tuning: We give text as the instruction and expect the model to follow that instruction.&#x20;
  * **Supervised finetuning:** We provide the model with instructions and responses and train the model to behave in a particular way.&#x20;
  * **RLHF Reinforcement learning with human feedback:** We have two model techniques. involves training the reward model where we teach that reward model to what humans prefer. We train the LLM with reinforcement learning to align with human preferences. \
    ![](https://748712899-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZcqmeHkGECPaayJZTNEt%2Fuploads%2FoGaL4xvtOZEkxbkDpNfm%2Fimage.png?alt=media\&token=e90a29d9-236d-49cb-a7dc-6f9f89dc22a2)

#### Pretraining vs finetuning&#x20;

Chatgpt started as the code model trained to help with programming Then in further iterations it was trained with RLHF (technique above discussed) for aligning more with human responses.&#x20;

<figure><img src="https://748712899-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZcqmeHkGECPaayJZTNEt%2Fuploads%2FKNFaE96u9L6LEqjI089j%2Fimage.png?alt=media&#x26;token=0c7edb33-a6aa-4436-bae4-52ea8404eebc" alt=""><figcaption><p>pretraining vs finetuning slide. </p></figcaption></figure>

#### Some of the open-source models released which we can finetune today!&#x20;

<figure><img src="https://748712899-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZcqmeHkGECPaayJZTNEt%2Fuploads%2Fp62JnHG8GrzK8VbK1cpv%2Fimage.png?alt=media&#x26;token=957188a5-4bbf-4dca-8f14-2dc63c4a8f36" alt=""><figcaption></figcaption></figure>

#### What do we need for finetuning?&#x20;

1. &#x20;What is the goal and what are the evaluation criteria?&#x20;
2. Choose model architecture and foundation model
3. Create the right dataset.&#x20;
4. Efficient training and finetuning of the model on data.&#x20;

### Introduction to Neurips LLM Efficiency Challenge

<figure><img src="https://748712899-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZcqmeHkGECPaayJZTNEt%2Fuploads%2FN1zcaMyXYithqBvKkDdR%2Fimage.png?alt=media&#x26;token=fa5ca9c6-a729-493e-be0a-d6e3a872c27e" alt=""><figcaption></figcaption></figure>

<img src="https://748712899-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZcqmeHkGECPaayJZTNEt%2Fuploads%2F0fJXZdIvyQ2mMTYVb3S7%2Fimage.png?alt=media&#x26;token=e00653ca-b7c1-4a67-913b-f298f8b14551" alt="" data-size="original">![](https://748712899-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZcqmeHkGECPaayJZTNEt%2Fuploads%2FGjAOhxK4nZJbK5L8VWPt%2Fimage.png?alt=media\&token=fe3e050b-2758-4185-a535-e806d2134390)

#### Finetuning LLMs

* Quantization ( to fit the model into  limited memory )&#x20;
* PEFT ( instead of training the whole model we train some part or append model with new parameters)  ( see images below)&#x20;
  * Techniques by model structure
    * adapters&#x20;
    * LORA Low-rank matrix adaptation
    * QLORA Quantized low-rank matrix adaptation(using lower precision for finetuning the LLM)&#x20;
  * Techniques by feeding data in different types&#x20;
    * Prompt tuning
    * Prefix tuning&#x20;
    * P-tuning&#x20;

<img src="https://748712899-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZcqmeHkGECPaayJZTNEt%2Fuploads%2FtA3cAZB5WlwZZ5BP8qE0%2Fimage.png?alt=media&#x26;token=d994eaff-21b9-4169-b77d-c60ccc5955d4" alt="" data-size="original"><img src="https://748712899-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZcqmeHkGECPaayJZTNEt%2Fuploads%2FXU9pckxSIqEOz38skwhY%2Fimage.png?alt=media&#x26;token=ac342846-be64-4311-9749-69b5f8c16590" alt="" data-size="original">![](https://748712899-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZcqmeHkGECPaayJZTNEt%2Fuploads%2FXez7Svwyayxb5X9KYhH9%2Fimage.png?alt=media\&token=f1cd8af6-535c-4fb0-bb1b-118fdc8e22e5)

#### Data curation&#x20;

![](https://748712899-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZcqmeHkGECPaayJZTNEt%2Fuploads%2FR3aXkfbnPt2o7u3XBk2N%2Fimage.png?alt=media\&token=612d3ee1-f47d-4dd2-9ddf-100bc6c65f09)

![](https://748712899-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZcqmeHkGECPaayJZTNEt%2Fuploads%2FthioGTwJfL8AN9NjsNQU%2Fimage.png?alt=media\&token=88097245-566f-4a9d-81b7-57f8cf929ebc)![](https://748712899-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZcqmeHkGECPaayJZTNEt%2Fuploads%2FZB3YFYaHZLye3Z6L3CEH%2Fimage.png?alt=media\&token=0cbe1bbd-dc3d-4e15-98e6-dd892788cb4e)

![](https://748712899-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZcqmeHkGECPaayJZTNEt%2Fuploads%2FgrU38wwYEjiLCRcYOIUQ%2Fimage.png?alt=media\&token=dc5a7252-b01d-4e9b-af06-790667ff5fd2)

### Code to start experimenting

{% embed url="<https://github.com/ayulockin/neurips-llm-efficiency-challenge>" %}

{% embed url="<https://youtu.be/2QRlvKSzyVw>" %}
Link to youtube recording
{% endembed %}

{% embed url="<https://llm-course.wandb.events/agenda/session/1191608>" %}
Course page link
{% endembed %}
