File size: 5,598 Bytes
d4c15a1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# 1. ๊ฐ๋ฐ ํ๊ฒฝ ์ค์ ยถ
# 1.1 ํ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ค์นํ๊ธฐยถ
In [ ]:
!pip3 install -q -U transformers==4.38.2
!pip3 install -q -U datasets==2.18.0
!pip3 install -q -U bitsandbytes==0.42.0
!pip3 install -q -U peft==0.9.0
!pip3 install -q -U trl==0.7.11
!pip3 install -q -U accelerate==0.27.2
# 1.2 Import modulesยถ
In [ ]:
import torch
from datasets import Dataset, load_dataset
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig, pipeline, TrainingArguments
from peft import LoraConfig, PeftModel
from trl import SFTTrainer
# 1.3 Huggingface ๋ก๊ทธ์ธยถ
In [ ]:
from huggingface_hub import notebook_login
notebook_login()
# 2. Dataset ์์ฑ ๋ฐ ์ค๋นยถ
# 2.1 ๋ฐ์ดํฐ์
๋ก๋ยถ
In [ ]:
from datasets import load_dataset
dataset = load_dataset("daekeun-ml/naver-news-summarization-ko")
# 2.2 ๋ฐ์ดํฐ์
ํ์ยถ
In [ ]:
dataset
# 2.3 ๋ฐ์ดํฐ์
์์ยถ
In [ ]:
dataset['train'][0]
# 3. Gemma ๋ชจ๋ธ์ ํ๊ตญ์ด ์์ฝ ํ
์คํธยถ
# 3.1 ๋ชจ๋ธ ๋ก๋ยถ
In [ ]:
BASE_MODEL = "google/gemma-2b-it"
model = AutoModelForCausalLM.from_pretrained(BASE_MODEL, device_map={"":0})
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL, add_special_tokens=True)
# 3.2 Gemma-it์ ํ๋กฌํํธ ํ์ยถ
In [ ]:
doc = dataset['train']['document'][0]
In [ ]:
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, max_new_tokens=512)
In [ ]:
messages = [
{
"role": "user",
"content": "๋ค์ ๊ธ์ ์์ฝํด์ฃผ์ธ์ :\n\n{}".format(doc)
}
]
prompt = pipe.tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
In [ ]:
prompt
# 3.3 Gemma-it ์ถ๋ก ยถ
In [ ]:
outputs = pipe(
prompt,
do_sample=True,
temperature=0.2,
top_k=50,
top_p=0.95,
add_special_tokens=True
)
In [ ]:
print(outputs[0]["generated_text"][len(prompt):])
# 4. Gemma ํ์ธํ๋ยถ
์ฃผ์: Colab GPU ๋ฉ๋ชจ๋ฆฌ ํ๊ณ๋ก ์ด์ ์ฅ ์ถ๋ก ์์ ์ฌ์ฉํ๋ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ๋น์ ์ค์ผ ํ์ธํ๋์ ์งํ ํ ์ ์์ต๋๋ค.
notebook ๋ฐํ์ ์ธ์
์ ์ฌ์์ ํ ํ 1๋ฒ๊ณผ 2๋ฒ์ 2.1 ํญ๋ชฉ๊น์ง ๋ค์ ์คํํ์ฌ ๋ก๋ ํ ํ ์๋ ๊ณผ์ ์ ์งํํฉ๋๋ค
In [ ]:
!nvidia-smi
# 4.1 ํ์ต์ฉ ํ๋กฌํํธ ์กฐ์ ยถ
In [ ]:
def generate_prompt(example):
prompt_list = []
for i in range(len(example['document'])):
prompt_list.append(r"""<bos><start_of_turn>user
๋ค์ ๊ธ์ ์์ฝํด์ฃผ์ธ์:
{}<end_of_turn>
<start_of_turn>model
{}<end_of_turn><eos>""".format(example['document'][i], example['summary'][i]))
return prompt_list
In [ ]:
train_data = dataset['train']
print(generate_prompt(train_data[:1])[0])
# 4.2 QLoRA ์ค์ ยถ
In [ ]:
lora_config = LoraConfig(
r=6,
target_modules=["q_proj", "o_proj", "k_proj", "v_proj", "gate_proj", "up_proj", "down_proj"],
task_type="CAUSAL_LM",
)
bnb_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_compute_dtype=torch.float16
)
In [ ]:
BASE_MODEL = "google/gemma-2b-it"
model = AutoModelForCausalLM.from_pretrained(BASE_MODEL, device_map="auto", quantization_config=bnb_config)
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL, add_special_tokens=True)
tokenizer.padding_side = 'right'
# 4.3 Trainer ์คํยถ
In [ ]:
trainer = SFTTrainer(
model=model,
train_dataset=train_data,
max_seq_length=512,
args=TrainingArguments(
output_dir="outputs",
# num_train_epochs = 1,
max_steps=3000,
per_device_train_batch_size=1,
gradient_accumulation_steps=4,
optim="paged_adamw_8bit",
warmup_steps=0.03,
learning_rate=2e-4,
fp16=True,
logging_steps=100,
push_to_hub=False,
report_to='none',
),
peft_config=lora_config,
formatting_func=generate_prompt,
)
In [ ]:
trainer.train()
# 4.4 Finetuned Model ์ ์ฅยถ
In [ ]:
ADAPTER_MODEL = "lora_adapter"
trainer.model.save_pretrained(ADAPTER_MODEL)
In [ ]:
!ls -alh lora_adapter
In [ ]:
model = AutoModelForCausalLM.from_pretrained(BASE_MODEL, device_map='auto', torch_dtype=torch.float16)
model = PeftModel.from_pretrained(model, ADAPTER_MODEL, device_map='auto', torch_dtype=torch.float16)
model = model.merge_and_unload()
model.save_pretrained('gemma-2b-it-sum-ko')
In [ ]:
!ls -alh ./gemma-2b-it-sum-ko
# 5. Gemma ํ๊ตญ์ด ์์ฝ ๋ชจ๋ธ ์ถ๋ก ยถ
์ฃผ์: ๋ง์ฐฌ๊ฐ์ง๋ก Colab GPU ๋ฉ๋ชจ๋ฆฌ ํ๊ณ๋ก ํ์ต ์ ์ฌ์ฉํ๋ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ๋น์ ์ค์ผ ํ์ธํ๋์ ์งํ ํ ์ ์์ต๋๋ค.
notebook ๋ฐํ์ ์ธ์
์ ์ฌ์์ ํ ํ 1๋ฒ๊ณผ 2๋ฒ์ 2.1 ํญ๋ชฉ๊น์ง ๋ค์ ์คํํ์ฌ ๋ก๋ ํ ํ ์๋ ๊ณผ์ ์ ์งํํฉ๋๋ค
In [ ]:
!nvidia-smi
# 5.1 Fine-tuned ๋ชจ๋ธ ๋ก๋ยถ
In [ ]:
BASE_MODEL = "google/gemma-2b-it"
FINETUNE_MODEL = "./gemma-2b-it-sum-ko"
finetune_model = AutoModelForCausalLM.from_pretrained(FINETUNE_MODEL, device_map={"":0})
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL, add_special_tokens=True)
# 5.2 Fine-tuned ๋ชจ๋ธ ์ถ๋ก ยถ
In [ ]:
pipe_finetuned = pipeline("text-generation", model=finetune_model, tokenizer=tokenizer, max_new_tokens=512)
In [ ]:
doc = dataset['test']['document'][10]
In [ ]:
messages = [
{
"role": "user",
"content": "๋ค์ ๊ธ์ ์์ฝํด์ฃผ์ธ์:\n\n{}".format(doc)
}
]
prompt = pipe_finetuned.tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
In [ ]:
outputs = pipe_finetuned(
prompt,
do_sample=True,
temperature=0.2,
top_k=50,
top_p=0.95,
add_special_tokens=True
)
print(outputs[0]["generated_text"][len(prompt):])
In [ ]:
|