AIO2023 Module9 Extra LLMs Instruction Finetuning 130424
AIO2023 Module9 Extra LLMs Instruction Finetuning 130424
All-in-One Course
(TA Session)
Instruction Tuning
Extra Class: LLMs
Dinh-Thang Duong – TA
Nguyen-Thuan Duong – TA
Year 2024
AI VIETNAM
All-in-One Course
(TA Session) Objectives
2
AI VIETNAM
All-in-One Course
(TA Session)
Outline
Ø Introduction
Ø In-context Learning
Ø Instruction Tuning
Ø Parameter Efficient Fine-Tuning
Ø Evaluation
Ø Practices
Ø Question 3
AI VIETNAM
All-in-One Course
(TA Session)
Introduction
4
AI VIETNAM
All-in-One Course
(TA Session) Introduction
v Getting Started
6
AI VIETNAM
All-in-One Course
(TA Session) Introduction
v What are Large Language Models (LLMs)?
ChatGPT App:
7
AI VIETNAM
All-in-One Course
(TA Session) Introduction
v What are Large Language Models (LLMs)?
LLMs (Large Language Models): AI models (language models) that were trained on a very large corpus of text. This
made them capable of performing various NLP tasks with high precision.
8
AI VIETNAM
All-in-One Course
(TA Session) Introduction
v What are Large Language Models (LLMs)?
9
AI VIETNAM
All-in-One Course
(TA Session) Introduction
v LLMs I/O
Output Text
Input Text
10
AI VIETNAM
All-in-One Course
(TA Session) Introduction
v Generative AI
11
https://www.jonstokes.com/p/getting-started-with-stable-diffusion
AI VIETNAM
All-in-One Course
(TA Session) Introduction
v Generative AI Prompting
12
https://medium.com/m/global-identity-2?redirectUrl=https%3A%2F%2Ftowardsdatascience.com%2Fpractical-prompt-engineering-74e96130abc4
AI VIETNAM
All-in-One Course
(TA Session) Introduction
v Prompting in LLMs
With prompting, we can make LLMs do any task with just natural language (zero-shot capability)
13
AI VIETNAM
All-in-One Course
(TA Session) Introduction
v LLMs problem
14
AI VIETNAM
All-in-One Course
(TA Session) Introduction
v How to improve LLMs on specific tasks?
In-context learning
Augmenting
15
AI VIETNAM
All-in-One Course
(TA Session) Introduction
v How to improve LLMs on specific tasks?
In-context learning
Augmenting
16
AI VIETNAM
All-in-One Course
(TA Session)
In-context Learning
17
AI VIETNAM
All-in-One Course
(TA Session) In-context learning
v Introduction
18
https://towardsdatascience.com/in-context-learning-approaches-in-large-language-models-9c0c53b116a1
AI VIETNAM
All-in-One Course
(TA Session) In-context learning
v Example
Zero/One/Few-shot
learning
20
AI VIETNAM
All-in-One Course
(TA Session) In-context learning
v Zero-shot learning
21
https://www.hopsworks.ai/dictionary/in-context-learning-icl
AI VIETNAM
All-in-One Course
(TA Session) In-context learning
v One-shot learning
22
https://www.hopsworks.ai/dictionary/in-context-learning-icl
AI VIETNAM
All-in-One Course
(TA Session) In-context learning
v Few-shot learning
23
https://www.hopsworks.ai/dictionary/in-context-learning-icl
AI VIETNAM
All-in-One Course
(TA Session) In-context learning
v Chain-of-Thought Prompting
24
AI VIETNAM
All-in-One Course
(TA Session) In-context learning
v Zero-shot learning with CoT
25
https://www.promptingguide.ai/techniques/cot
AI VIETNAM
All-in-One Course
(TA Session) In-context learning
v One-shot learning with CoT
26
https://www.promptingguide.ai/techniques/cot
AI VIETNAM
All-in-One Course
(TA Session) In-context learning
v CoT variations
27
https://cobusgreyling.medium.com/the-anatomy-of-chain-of-thought-prompting-cot-b7489c925402
AI VIETNAM
All-in-One Course
(TA Session)
Instruction Tuning
28
AI VIETNAM
All-in-One Course
(TA Session) Instruction Tuning
v Training model in Machine Learning
Start with a randomized
1. Training weights and train the model
on a new task.
Fine-tuning
Machine
Training Learning Model
Dataset
Pre-trained Update
Weights Weights
Initialize (Knowledge)
31
AI VIETNAM
All-in-One Course
(TA Session) Instruction Tuning
v Introduction
32
AI VIETNAM
All-in-One Course
(TA Session) Instruction Tuning
v Training LLMs
33
https://www.lesswrong.com/posts/9asGWZ9vjmNDc4TeN/proposal-align-systems-earlier-in-training
AI VIETNAM
All-in-One Course
(TA Session) Instruction Tuning
v Applications
34
https://intuitivetutorial.com/2023/06/18/large-language-models-in-deep-learning/
AI VIETNAM
All-in-One Course
(TA Session) Instruction Tuning
v Applications
35
AI VIETNAM
All-in-One Course
(TA Session) Instruction Tuning
v Use case: Multiple choice math question solver
Problem statement: Build a model that can choose the best answer (A, B, C or D) given a multiple choice math
question.
36
AI VIETNAM
All-in-One Course
(TA Session) Instruction Tuning
v Use case: Multiple choice math question solver
37
AI VIETNAM
All-in-One Course
(TA Session)
38
AI VIETNAM
All-in-One Course
(TA Session) Parameter Efficient Fine-tuning
v Problem Description
39
AI VIETNAM
All-in-One Course
(TA Session) Parameter Efficient Fine-tuning
v PEFT type
40
https://www.leewayhertz.com/parameter-efficient-fine-tuning/
AI VIETNAM
All-in-One Course
(TA Session) Parameter Efficient Fine-tuning
v PEFT type
41
AI VIETNAM
All-in-One Course
(TA Session) Parameter Efficient Fine-tuning
v LoRA: Low Rank Adaptation 𝑟 𝑘
𝑟 A
LoRA training 𝑑 B
ℎ = 𝑊𝑥 = 𝑊! 𝑥 + ∆𝑊𝑥
ℎ = 𝑊! 𝑥 + 𝐵𝐴𝑥
𝑘
𝐵 ∈ ℝ"×$ , 𝐴 ∈ ℝ$×%
𝑟 ≪ {𝑑, 𝑘}
𝑑
42
AI VIETNAM
All-in-One Course
(TA Session) Parameter Efficient Fine-tuning
v LoRA: Low Rank Adaptation
*
Pre-trained
Task A
weight
Frozen
*
Pre-trained
Task B
weight
Frozen
43
AI VIETNAM
All-in-One Course
(TA Session) Parameter Efficient Fine-tuning
v QLoRA
Double Quantization
44
AI VIETNAM
All-in-One Course
(TA Session) Parameter Efficient Fine-tuning
v Performance of LoRA
45
AI VIETNAM
All-in-One Course
(TA Session)
LLM Metrics
46
AI VIETNAM
All-in-One Course
(TA Session) LLM Metrics
v How to evaluate LLMs?
48
https://datasciencedojo.com/blog/evaluating-large-language-models-llms/
AI VIETNAM
All-in-One Course
(TA Session) LLM Metrics
v BLEU score
BLEU (Bilingual Evaluation Understudy) is an algorithm for evaluating the quality of text which has been
machine-translated.
1+1+1+1+1+1
𝑠𝑐𝑜𝑟𝑒 = =1
𝑙𝑒𝑛(𝑐𝑎𝑛𝑑𝑖𝑑𝑎𝑡𝑒)
Candidate I I a student student of
+1 +1 +1 +1 +1 +1
1+1+1+1
𝑠𝑐𝑜𝑟𝑒 = = 0.66
𝑙𝑒𝑛(𝑐𝑎𝑛𝑑𝑖𝑑𝑎𝑡𝑒)
+1 +0 +1 +1 +0 +1
Cons:
- don't consider semantic meaning
- don't consider sentence structure
50
AI VIETNAM
All-in-One Course
(TA Session) LLM Metrics
v ROUGE score
ROUGE (Recall-Oriented Understudy for Gisting Evaluation) is a set of metrics for evaluating text generation
models (summarization or machine translation).
+1 +0 +1 +0 +1 1+1+1
𝑠𝑐𝑜𝑟𝑒2 = = 0.42
𝑙𝑒𝑛(𝑟𝑒𝑓𝑒𝑟𝑒𝑛𝑐𝑒)
Reference 2 I am a member of this university
51
AI VIETNAM
All-in-One Course
(TA Session) LLM Metrics
v Other metrics?
52
https://msandbu.org/benchmarking-llms-and-what-is-the-best-llm/
AI VIETNAM
All-in-One Course
(TA Session)
Quiz
53
AI VIETNAM
All-in-One Course
(TA Session)
Practices
54
AI VIETNAM
All-in-One Course
(TA Session) Practices
v Problem Description
Description: Build a Vietnamese Chatbot that can handle math problem using Large Language Models (LLMs).
55
AI VIETNAM
All-in-One Course
(TA Session) Practices
v Problem Description
Description: Build a Vietnamese Chatbot that can handle math problem using Large Language Models (LLMs).
56
AI VIETNAM
All-in-One Course
(TA Session) Practices
v Coding Step 1: Install libraries
57
AI VIETNAM
All-in-One Course
(TA Session) Practices
v Coding Step 2: Import libraries/modules
58
AI VIETNAM
All-in-One Course
(TA Session) Practices
v Coding Step 3: Load pre-trained model
59
AI VIETNAM
All-in-One Course
(TA Session) Practices
v Coding Step 3: Load pre-trained model
60
AI VIETNAM
All-in-One Course
(TA Session) Practices
v Coding Step 4: Configurate LLMs
61
AI VIETNAM
All-in-One Course
(TA Session) Practices
v Fine-tuning LLMs
Fine-tuning
LLMs
Training
Dataset
Pre-trained Update
Weights Weights
Initialize (Knowledge)
62
AI VIETNAM
All-in-One Course
(TA Session) Practices
v Coding Step 5: Download dataset
Fine-tune VinaLLaMA on
hllj/vi_grade_school_math_mc
q
(https://huggingface.co/datasets
/hllj/vi_grade_school_math_mcq)
, a Vietnamese elementary math
dataset.
63
AI VIETNAM
All-in-One Course
(TA Session) Practices
v Coding Step 5: Download dataset
64
AI VIETNAM
All-in-One Course
(TA Session) Practices
v Prompting format
<|im_start|>system
Bạn là một chuyên gia về toán. Bạn sẽ nhận câu hỏi trắc nghiệm kèm theo các lựa
chọn, hãy giải step by step nếu có và chọn phương án đúng.
<|im_start|>user
### Câu hỏi:
{question}
### Các lựa chọn:
{choices}
### Câu trả lời:
<|im_start|>assistant
{explanation}
65
AI VIETNAM
All-in-One Course
(TA Session) Practices
v Coding Step 6: Create generate prompt function
66
AI VIETNAM
All-in-One Course
(TA Session) Practices
v Coding Step 7: Create training samples
67
AI VIETNAM
All-in-One Course
(TA Session) Practices
v Coding Step 7: Create training samples
1. Choices
2. Question
3. Explanation
68
AI VIETNAM
All-in-One Course
(TA Session) Practices
v Coding Step 7: Create training samples
Extract question,
Call generate
Take a dictionary choices,
prompt function
explanation
No
Problems Yes
End of list ?
column
69
AI VIETNAM
All-in-One Course
(TA Session) Practices
v Coding Step 7: Create training samples
70
AI VIETNAM
All-in-One Course
(TA Session) Practices
v Coding Step 8: Training
71
AI VIETNAM
All-in-One Course
(TA Session) Practices
v Fine-tuning results
Before fine-tuning
72
AI VIETNAM
All-in-One Course
(TA Session) Practices
v Fine-tuning results
After fine-tuning
73
AI VIETNAM
All-in-One Course
(TA Session) Summary
In this lecture, we have discussed:
1. Training type
1. Pre-training
2. Fine-tuning
2. How to make LLMs adapt to a task?
1. Prompting (In-context learning)
1. One-shot learning: Prompting with 1 example.
2. Few-shot learning: Prompting with more than 1 example.
3. Chain-of-Thought: Prompting with reasoning.
2. Instruction Tuning: By supervised learning LLMs with instruction data.
3. How to train LLMs on a single (small) GPU?
1. Parameter Efficient Fine-Tuning
4. Apply Instruction Tuning for multiple choice math question solver task.
74
AI VIETNAM
All-in-One Course
(TA Session) Question
?
75
76