LLM fine tunning - HiroSung/Study GitHub Wiki
LLM finetunning (24.09.02~03) / ๊นํ์ฑ ๊ฐ์ฌ
LLM finetuning-v4.pdf colab-20240902T061043Z-001.zip
1. Revisit LLMs
- ๋๋ถ๋ถ์ llm์ Transformer ๊ธฐ๋ฐ ์ํคํ ์ฒ (2017๋ ๋ถํฐ~. ๊ตฌ๊ธ ๊ฐ๋ฐ. ๋ฒ์ญ๋ชจ๋ธ ๊ฐ๋ฐํ๊ธฐ ์ํด์. RNL๊ณ์ด . sequence to sequence)
- GPT ๊ณ์ด(๋ฌธ์ฅ ์์ฑ), BERT ๊ณ์ด (๋ฌธ์ฅ ๋งฅ๋ฝ ์ดํด. ๋ถ๋ฅ. ์ ๋ ฅ๋ ๋จ์ด์ embedding ๋ฐฉ์)
2. BERT
2.1 Tokenization & Embedding
- Tockenization : ํ ์คํธ๋ฅผ ์๋ฏธ์๋ ํ ํฐ ๋จ์๋ก ๋๋๊ณ ๊ณ ์ ํ ์ธ๋ฑ์ค๋ฒํธ๋ก ๋ณํ [CLS] this is a input . [SEP] ์คํ์ค ํ ํฐ ... ์ธ๋ฑ์ค ๋ฒํธ ๋จ์ด์ฌ์ ์ ๊ท๋ชจ๋ 5๋ง๊ฐ ์ด์์. ==> ์ ์์ฝ๋ฉ. ์ธ๋ฑ์ค ์ฝ๋ฉ.
- Embedding : Input Layer
3. GPT
- ์ฌ์ ํ์ต์ ํ์ง๋ง Decoder๊ธฐ๋ฐ ์ธ์ด ๋ชจ๋ธ
- ๊ฐ๊ฐ์ time step ์ผ๋ก ๋ ์ด๋ธ ํ์ฌ ์์ธก์ ๋จ์ด๋ฅผ ๋ถ๋ฅํ์ฌ Vocab ์ฌ์ ์์ ๋จ์ด๋ฅผ ์ ํํ๊ฒ ๋จ.
4. Transformer
4.1 ์ฃผ์ ํน์ง
- ๋ค์ํ ๋ชจ๋ธ ์ง์: Transformers ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ ์์ญ ์ข ์ Transformer ๊ธฐ๋ฐ ๋ชจ๋ธ์ ์ง์ํฉ๋๋ค. ์ด ๋ชจ๋ธ๋ค์ ๋ค์ํ ์ธ์ด ๋ฐ ์์ ์ ๋ํด ์ฌ์ ํ๋ จ๋์ด ์ ๊ณต๋ฉ๋๋ค.
- ์ฌ์ด ๋ชจ๋ธ ์ฌ์ฉ: ์ฌ์ ํ๋ จ๋ ๋ชจ๋ธ์ ๋ช ์ค์ ์ฝ๋๋ก ๋ถ๋ฌ์ ์ฌ์ฉํ ์ ์์ต๋๋ค. ์ด๋ฅผ ํตํด ๋ณต์กํ ๋ชจ๋ธ ์ํคํ ์ฒ๋ฅผ ์ดํดํ์ง ์์๋ ๊ณ ์ฑ๋ฅ์ NLP ๋ชจ๋ธ์ ์ ์ฉํ ์ ์์ต๋๋ค.
- ์ฌ์ฉ์ ์นํ์ : PyTorch์ TensorFlow ๋ชจ๋๋ฅผ ์ง์ํ๋ฉฐ, ์ฌ์ด API๋ฅผ ํตํด ๋ ํ๋ ์์ํฌ์์ ๋ชจ๋ ์์ฝ๊ฒ ์ฌ์ฉํ ์ ์์ต๋๋ค. ๋ํ, ์ง๊ด์ ์ธ ๋ฌธ์์ ๋ค์ํ ํํ ๋ฆฌ์ผ์ด ์ ๊ณต๋์ด ์ฌ์ฉ์๊ฐ ๋น ๋ฅด๊ฒ ํ์ตํ๊ณ ์ ์ฉํ ์ ์๋๋ก ๋์์ค๋๋ค.
- ํ์ฅ์ฑ: ์ฌ์ฉ์๊ฐ ์ง์ ๋ชจ๋ธ์ ์์ ํ๊ฑฐ๋ ์๋ก์ด ํํ์ Transformer ๋ชจ๋ธ์ ์ฝ๊ฒ ์ถ๊ฐํ ์ ์๋ ๊ตฌ์กฐ๋ฅผ ๊ฐ์ถ๊ณ ์์ต๋๋ค.
- ์ปค๋ฎค๋ํฐ ๋ฐ ์์: Hugging Face๋ ํ๋ฐํ ์ปค๋ฎค๋ํฐ๋ฅผ ๋ณด์ ํ๊ณ ์์ผ๋ฉฐ, ๋ชจ๋ธ ํ๋ธ๋ฅผ ํตํด ์ฌ์ฉ์๊ฐ ๊ฐ๋ฐํ ๋ชจ๋ธ์ ๊ณต์ ํ๊ฑฐ๋ ๋ค๋ฅธ ์ฌ์ฉ์์ ๋ชจ๋ธ์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
4.2 Pipeline
4.2.1 Pipeline ์ข ๋ฅ
๊ฐ์ ๋ถ์
zero-shot text classification
text generation(Completion)
- generator = pipeline("text-generation", model="kykim/gpt3-kor-small_based_on_gpt2")
Question answering
Summarization
NER
์ค์ต
4.2.2 Model
- Transformer์๋ ๋ค์ํ ์ํคํ ์ฒ๊ฐ ์์ผ๋ฉฐ, ๊ฐ๊ฐ์ ํน์ ์์ ์ ํด๊ฒฐํ๊ธฐ ์ํด ์ค๊ณ๋์์ต๋๋ค. ForCausalLM - Text ์์ฑ. ํ์ต method๊ฐ ์ ์๋์ด ์์. ForMaskedLM - Bert ๋ชจ๋ธ ์ฌ์ ํ์ต์ ์ฌ์ฉ ForMultipleChoice ForQuestionAnswering ForSequenceClassification - CLS๋ฅผ ๋ถ๋ฅ๊ธฐ๋ก ๋ฝ์์ ์ฌ์ฉ ForTokenClassification - ๊ฐ ํ ํฐ๋ณ๋ก CLS๊ฐ ํ์ํ ๊ฒฝ์ฐ ์ฌ์ฉ. ์) ๋ฌธ๋ฒ ๋ถ์๊ธฐ?
Training Language Model
Fine-tunning
- 2๊ฐ์ง ๋ฐฉ๋ฒ์ด ์์.
- Scratch vs Transfer Learning
Scratch
- Training from scratch .. ํ์ฌ๋ ๊ฑฐ์ ์ฌ์ฉํ์ง ์์
Transfer Learning
- ์ฌ์ ํ์ต ๋ชจ๋ธ.
- PLM (Pretrained Language Modeling)
Fine-tune ์ ๋ต
- Pre-Tran > SFT (Supervised Fine Tunnig) > RLHF (๊ฐํํ์ต) > Fine-tunning & in-context learning
- ์ค์ต (PytorcH, Transformer Trainer API๋ฅผ ์ฌ์ฉํ ํ์ธํ๋) 5.Fine_tuning_a_model.ipynb
- chatgpt๋ 3๋จ๊ณ๋ก ์ ์ฉ์ด ๋๊ฒ์
SFT (Supervised Fine Tunnig)
RLHF (Reinforcement Learning from Human Feedback)
Question answering model
-
ํ ํฐ ๋จ์๋ก ๋ ์ด๋ธ๋ง์ ์ค์ ํจ 0/1
-
AutoModel ์ค์ต (https://colab.research.google.com/drive/1BcOBXSYZDu25XUpZvgV79CCH9IU4Xdg9
PEFT (Parameter-Efficient Fine-Tuning)
Foundation Model
- ํ์ต์ํฌ ์๋ฃ๊ฐ ์ปค์ง์๋ก ๋ฆฌ์์ค ์ฌ์ฉ์ด ์ปค์ง๊ฒ๋จ.
- ๊ฑฐ๋ ๋ชจ๋ธ์ ํ๋๊น์ง ํ ์ ์์๊น? ๋ฉ๋ชจ๋ฆฌ ํจ์จ์ ์ฌ์ฉ์ํด์ ์ฌ์ฉ๋ ๋ชจ๋ธ์ด PEFT ๋ชจ๋ธ์
์ ์ฉ๋ฐฉ๋ฒ
- Prompt-based method
- Adater modules
prefix-tuning
- BERT ๋ชจ๋ธ. ์๋น์คํ๊ณ ์ ํ๋ ๊ตฌ์ฑ์ ๋ฐ๋ผ ๋ชจ๋ธ์ด ์์ฑ๋ ๊ฒ์ธ๋ฐ, attention layer ์ ๊ฐ๋ณ์ Prefix ์ ํด๋นํ๋ ํ ํฐ๋ค๋ง ํ์ต์ ์ํด
prompt tuning
- prefix-tunig์ ๋ฐ์ ์ํจ๊ฒ.
- ์ ๋ ฅ ํ ํฐ๋ง ํ์ต
LoRA
- ๊ฐ์ฅ ์ธ๊ธฐ ์๋ ๋ฐฉ๋ฒ
- attention layer์ Query / Key / Value ๋ก ์ธ๋ฑ์ฑ ํ๋ ํ๋ ฌ์ด ๋ฐ๋ ๊ฒฝ์ฐ Transformer ์ญํ ์ด ์ปค์ง๊ฒ ๋์ด. ํด๋น ๋ถ๋ถ์ LoRA๋ฅผ ์ ์ฉํจ.
- ์ฑ๋ฅ๋ ์ข๊ณ , ์ ๋ฐ์ดํธ ํ๋ ํ๋ผ๋ฏธํฐ ์๋ ์ ์ด์ ์ฐ์ฐ์ ํ์ํ ๋ฉ๋ชจ๋ฆฌ๊ฐ ์ ๊ฒ ์ฌ์ฉ๋จ
- LoRA ์ค์ต https://colab.research.google.com/drive/1lr0nTEEaq5gEL8jghNCdFldiNImZ0iLO?usp=sharing
Instruction tuning
huggingface
- accesstocken - write / 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
- mistralai/Mistral-7B-v0.1 / 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
Instruction tuning
- ๋ต๋ณ ์์ฑ์ชฝ์ ํ๋์ด ๋์ด ์๋ค๋ฉด ์ํ๋ ๋ต๋ณ์ ํ๊ธฐ ์ํด์ fine tuning ํ๊ฒ ๋จ.
- SFT (์ง๋์ ๊ฐํํ์ต) ์ ํ๋์ ๋ฐฉ๋ฒ
ํ์ต๋ฐฉ๋ฒ์?
-
gpt ๋ชจ๋ธ ํ์ต์ ๊ธฐ์กด์ ์๊ณ ๋ฆฌ์ฆ๊ณผ ๋์ผํ๊ณ data-set๋ง ๋ฌ๋ผ์ง
-
step1. Dataset ๊ตฌ์ถ > step2. ํ๋ ๊ตฌ์ถ .
-
์์ฆ์ instruction๋ AI๋ฅผ ํตํด์ ์์ฑ. ChatGPT4, Gemini ...
-
Alpaca Dataset ( GPT-3.5 (text-davinci-003) https://crfm.stanford.edu/2023/03/13/alpaca.html)
-
Instruction tuning with GTP4 / https://github.com/Instruction-Tuning-with-GPT-4/GPT-4-LLM#how-good-is-the-data
-
wandb ๋ฅ๋ฌ๋ ์ฌ์ฉํ๋ ์ฌ์ฉ์๊ฐ ๋ง์ด ์ฌ์ฉํจ
Alpaca ๋ฐ์ดํฐ์ ์ ์ฌ์ฉํ Llama ๋ชจ๋ธ instruction tuning
want db
- https://wandb.ai/manchiro-sk ([email protected])
- api key / 4a3adb7003907aacaebb23a7bc8db5489631351a
- wandb.Artifact View project at https://wandb.ai/manchiro-sk/alpaca_ft View run at https://wandb.ai/manchiro-sk/alpaca_ft/runs/zvrceil5
RLHF
- (Reinforcement Learning from Human Feedback) / ๊ฐํํ์ต
- InstructGPT . PPO(๋ณด์๋ชจ๋ธ์ ๋ณด์์น๋ฅผ ๋์) / DPO (๋ณด์๋ชจ๋ธ์์ด ์ฌ๋์ ์ ํธ๋๋ก ํ์ตํ๋ ์๊ณ ๋ฆฌ์ฆ. ์ ํธ.๋น์ ํธ ๋ฌธ์๋ฅผ ์ฃผ๊ณ ๋ฐ๋ก ํ์ต)
- PPO ๋ฐฉ์์ ๋ ๊ฐ๊ฑดํ๊ณ ํ์ต์๊ณ ๋ฆฌ์ฆ ์ ์ฉ์ ์ํ ๋ฐฉ๋ฒ์ด ๊ฐ๋ฐ๋์ด์ผ ํจ.
- ์ ์ฉ๋ฐฉ๋ฒ . 3๋จ๊ณ๋ก ์งํ
- ์ ์ฑ ๋ชจ๋ธ . ์ํฉ์ ์ฌ๋ฐ๋ฅธ ๊ฒฐ์ ์ ๋ด๋ฆฌ๋๋ก ํ๋๊ฒ.
- DPO ์ค์ต https://colab.research.google.com/drive/1uugTDKSJvpoLYfz5ntT6qL7uEFRpt-U_?usp=sharing
์์๋ฌธ์
[๋ฌธ์ ] ffn์ ๋ฃ์ด์ฃผ๋ ์ด์ - ๋น์ ํ์ฑ์ ๋ฃ์ด์ฃผ๊ธฐ ์ํด์์
์ฐธ๊ณ ์๋ฃ
Huggingface ํ์ ํ๋ก์ ํธ
Transformer
https://huggingface.co/docs/transformers/index
PEFT
https://huggingface.co/docs/peft/v0.10.0/en/index
https://github.com/huggingface/peft/blob/main/README.md
์ ์ฉํ ์ํฐํด
The Ultimate Guide to Fine-Tune LLaMA 2, With LLM Evaluations
https://www.confident-ai.com/blog/the-ultimate-guide-to-fine-tune-llama-2-with-llm-evaluations
How to fine-tune an LLM part1 : Preparing Dataset for Instruction Tuning
https://newsletter.ruder.io/p/instruction-tuning-vol-1
https://newsletter.ruder.io/p/instruction-tuning-vol-2
https://medium.com/aiguys/reinforcement-learning-from-human-feedback-instructgpt-and-chatgpt-693d00cb9c58
Generating a Clinical Instruction Dataset in Portuguese with Langchain and GPT-4
https://solano-todeschini.medium.com/generating-a-clinical-instruction-dataset-in-portuguese-with-langchain-and-gpt-4-6ee9abfa41ae
How to Generate Instruction Datasets from Any Documents for LLM Fine-Tuning
https://towardsdatascience.com/how-to-generate-instruction-datasets-from-any-documents-for-llm-fine-tuning-abb319a05d91)
Github source
https://github.com/mlabonne/llm-course
https://github.com/ashishpatel26/LLM-Finetuning
https://github.com/tatsu-lab/stanford_alpaca
https://github.com/Instruction-Tuning-with-GPT-4/GPT-4-LLM?tab=readme-ov-file#how-good-is-the-data
https://github.com/Eladlev/AutoPrompt