Update README.md
Browse files
README.md
CHANGED
@@ -25,20 +25,25 @@ This llama model was trained 2x faster with [Unsloth](https://github.com/unsloth
|
|
25 |
|
26 |
|
27 |
# Implementation
|
28 |
-
|
29 |
-
'''
|
30 |
!pip uninstall unsloth -y
|
31 |
!pip install --upgrade --no-cache-dir "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
|
|
|
|
|
32 |
!pip install --upgrade torch
|
33 |
!pip install --upgrade xformers
|
34 |
|
|
|
35 |
import torch
|
36 |
if torch.cuda.get_device_capability()[0] >= 8:
|
37 |
!pip install --no-deps packaging ninja einops "flash-attn>=2.6.3"
|
38 |
|
|
|
|
|
39 |
from google.colab import userdata
|
40 |
HF_TOKEN=userdata.get('HF_TOKEN')
|
41 |
|
|
|
42 |
from unsloth import FastLanguageModel
|
43 |
import torch
|
44 |
max_seq_length = 2048 # unslothではRoPEをサポートしているのでコンテキスト長は自由に設定可能
|
@@ -47,6 +52,7 @@ load_in_4bit = True # 今回は13Bモデルを扱うためTrue
|
|
47 |
|
48 |
model_id = "llm-jp/llm-jp-3-13b"
|
49 |
new_model_id = "llm-jp-3-13b-it-ctx2048" #Fine-Tuningしたモデルにつけたい名前、it: Instruction Tuning
|
|
|
50 |
model, tokenizer = FastLanguageModel.from_pretrained(
|
51 |
model_name=model_id,
|
52 |
dtype=dtype,
|
@@ -54,10 +60,12 @@ model, tokenizer = FastLanguageModel.from_pretrained(
|
|
54 |
trust_remote_code=True,
|
55 |
)
|
56 |
|
|
|
57 |
model = FastLanguageModel.get_peft_model(
|
58 |
model,
|
59 |
r = 32,
|
60 |
-
target_modules = ["q_proj", "k_proj", "v_proj", "o_proj",
|
|
|
61 |
lora_alpha = 32,
|
62 |
lora_dropout = 0.05,
|
63 |
bias = "none",
|
@@ -68,15 +76,21 @@ model = FastLanguageModel.get_peft_model(
|
|
68 |
max_seq_length = max_seq_length,
|
69 |
)
|
70 |
|
|
|
71 |
from datasets import load_dataset
|
72 |
dataset = load_dataset("json", data_files="/content/ichikara-instruction-003-001-1.json")
|
73 |
|
|
|
74 |
prompt = """### 指示
|
75 |
{}
|
76 |
|
77 |
### 回答
|
78 |
{}"""
|
79 |
|
|
|
|
|
|
|
|
|
80 |
EOS_TOKEN = tokenizer.eos_token # トークナイザーのEOSトークン(文末トークン)
|
81 |
def formatting_prompts_func(examples):
|
82 |
input = examples["text"] # 入力データ
|
@@ -85,11 +99,77 @@ def formatting_prompts_func(examples):
|
|
85 |
return { "formatted_text" : text, } # 新しいフィールド "formatted_text" を返す
|
86 |
pass
|
87 |
|
|
|
88 |
dataset = dataset.map(
|
89 |
formatting_prompts_func,
|
90 |
num_proc= 4, # 並列処理数を指定
|
91 |
)
|
92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
from trl import SFTTrainer
|
94 |
from transformers import TrainingArguments
|
95 |
from unsloth import is_bfloat16_supported
|
@@ -120,8 +200,12 @@ trainer = SFTTrainer(
|
|
120 |
),
|
121 |
)
|
122 |
|
|
|
123 |
trainer_stats = trainer.train()
|
124 |
|
|
|
|
|
|
|
125 |
import json
|
126 |
datasets = []
|
127 |
with open("/content/elyza-tasks-100-TV_0.jsonl", "r") as f:
|
@@ -133,7 +217,10 @@ with open("/content/elyza-tasks-100-TV_0.jsonl", "r") as f:
|
|
133 |
datasets.append(json.loads(item))
|
134 |
item = ""
|
135 |
|
|
|
136 |
from tqdm import tqdm
|
|
|
|
|
137 |
FastLanguageModel.for_inference(model)
|
138 |
|
139 |
results = []
|
@@ -149,11 +236,13 @@ for dt in tqdm(datasets):
|
|
149 |
|
150 |
results.append({"task_id": dt["task_id"], "input": input, "output": prediction})
|
151 |
|
|
|
152 |
with open(f"./{new_model_id}_output.jsonl", 'w', encoding='utf-8') as f:
|
153 |
for result in results:
|
154 |
json.dump(result, f, ensure_ascii=False)
|
155 |
f.write('\n')
|
156 |
|
|
|
157 |
model.push_to_hub(new_model_id, token=HF_TOKEN, private=True)
|
158 |
tokenizer.push_to_hub(new_model_id, token=HF_TOKEN, private=True)
|
159 |
-
|
|
|
25 |
|
26 |
|
27 |
# Implementation
|
28 |
+
```python
|
|
|
29 |
!pip uninstall unsloth -y
|
30 |
!pip install --upgrade --no-cache-dir "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
|
31 |
+
|
32 |
+
# Google Colab のデフォルトで入っているパッケージをアップグレード
|
33 |
!pip install --upgrade torch
|
34 |
!pip install --upgrade xformers
|
35 |
|
36 |
+
# Install Flash Attention 2 for softcapping support
|
37 |
import torch
|
38 |
if torch.cuda.get_device_capability()[0] >= 8:
|
39 |
!pip install --no-deps packaging ninja einops "flash-attn>=2.6.3"
|
40 |
|
41 |
+
"""## モデルのロード"""
|
42 |
+
|
43 |
from google.colab import userdata
|
44 |
HF_TOKEN=userdata.get('HF_TOKEN')
|
45 |
|
46 |
+
# llm-jp/llm-jp-3-13bを4bit量子化のqLoRA設定でロード
|
47 |
from unsloth import FastLanguageModel
|
48 |
import torch
|
49 |
max_seq_length = 2048 # unslothではRoPEをサポートしているのでコンテキスト長は自由に設定可能
|
|
|
52 |
|
53 |
model_id = "llm-jp/llm-jp-3-13b"
|
54 |
new_model_id = "llm-jp-3-13b-it-ctx2048" #Fine-Tuningしたモデルにつけたい名前、it: Instruction Tuning
|
55 |
+
# FastLanguageModel インスタンスを作成
|
56 |
model, tokenizer = FastLanguageModel.from_pretrained(
|
57 |
model_name=model_id,
|
58 |
dtype=dtype,
|
|
|
60 |
trust_remote_code=True,
|
61 |
)
|
62 |
|
63 |
+
# SFT用のモデルを用意
|
64 |
model = FastLanguageModel.get_peft_model(
|
65 |
model,
|
66 |
r = 32,
|
67 |
+
target_modules = ["q_proj", "k_proj", "v_proj", "o_proj",
|
68 |
+
"gate_proj", "up_proj", "down_proj",],
|
69 |
lora_alpha = 32,
|
70 |
lora_dropout = 0.05,
|
71 |
bias = "none",
|
|
|
76 |
max_seq_length = max_seq_length,
|
77 |
)
|
78 |
|
79 |
+
# 学習に用いるデータセットの指定
|
80 |
from datasets import load_dataset
|
81 |
dataset = load_dataset("json", data_files="/content/ichikara-instruction-003-001-1.json")
|
82 |
|
83 |
+
# 学習時のプロンプトフォーマット
|
84 |
prompt = """### 指示
|
85 |
{}
|
86 |
|
87 |
### 回答
|
88 |
{}"""
|
89 |
|
90 |
+
|
91 |
+
"""
|
92 |
+
formatting_prompts_func: 各データをプロンプトに合わせた形式に合わせる
|
93 |
+
"""
|
94 |
EOS_TOKEN = tokenizer.eos_token # トークナイザーのEOSトークン(文末トークン)
|
95 |
def formatting_prompts_func(examples):
|
96 |
input = examples["text"] # 入力データ
|
|
|
99 |
return { "formatted_text" : text, } # 新しいフィールド "formatted_text" を返す
|
100 |
pass
|
101 |
|
102 |
+
# # 各データにフォーマットを適用
|
103 |
dataset = dataset.map(
|
104 |
formatting_prompts_func,
|
105 |
num_proc= 4, # 並列処理数を指定
|
106 |
)
|
107 |
|
108 |
+
dataset
|
109 |
+
|
110 |
+
# データを確認
|
111 |
+
print(dataset["train"]["formatted_text"][3])
|
112 |
+
|
113 |
+
"""
|
114 |
+
training_arguments: 学習の設定
|
115 |
+
|
116 |
+
- output_dir:
|
117 |
+
-トレーニング後のモデルを保存するディレクトリ
|
118 |
+
|
119 |
+
- per_device_train_batch_size:
|
120 |
+
- デバイスごとのトレーニングバッチサイズ
|
121 |
+
|
122 |
+
- per_device_eval_batch_size:
|
123 |
+
- デバイスごとの評価バッチサイズ
|
124 |
+
|
125 |
+
- gradient_accumulation_steps:
|
126 |
+
- 勾配を更新する前にステップを積み重ねる回数
|
127 |
+
|
128 |
+
- optim:
|
129 |
+
- オプティマイザの設定
|
130 |
+
|
131 |
+
- num_train_epochs:
|
132 |
+
- エポック数
|
133 |
+
|
134 |
+
- eval_strategy:
|
135 |
+
- 評価の戦略 ("no"/"steps"/"epoch")
|
136 |
+
|
137 |
+
- eval_steps:
|
138 |
+
- eval_strategyが"steps"のとき、評価を行うstep間隔
|
139 |
+
|
140 |
+
- logging_strategy:
|
141 |
+
- ログ記録の戦略
|
142 |
+
|
143 |
+
- logging_steps:
|
144 |
+
- ログを出力するステップ間隔
|
145 |
+
|
146 |
+
- warmup_steps:
|
147 |
+
- 学習率のウォームアップステップ数
|
148 |
+
|
149 |
+
- save_steps:
|
150 |
+
- モデルを保存するステップ間隔
|
151 |
+
|
152 |
+
- save_total_limit:
|
153 |
+
- 保存しておくcheckpointの数
|
154 |
+
|
155 |
+
- max_steps:
|
156 |
+
- トレーニングの最大ステップ数
|
157 |
+
|
158 |
+
- learning_rate:
|
159 |
+
- 学習率
|
160 |
+
|
161 |
+
- fp16:
|
162 |
+
- 16bit浮動小数点の使用設定(第8回演習を参考にすると良いです)
|
163 |
+
|
164 |
+
- bf16:
|
165 |
+
- BFloat16の使用設定
|
166 |
+
|
167 |
+
- group_by_length:
|
168 |
+
- 入力シーケンスの長さによりバッチをグループ化 (トレーニングの効率化)
|
169 |
+
|
170 |
+
- report_to:
|
171 |
+
- ログの送信先 ("wandb"/"tensorboard"など)
|
172 |
+
"""
|
173 |
from trl import SFTTrainer
|
174 |
from transformers import TrainingArguments
|
175 |
from unsloth import is_bfloat16_supported
|
|
|
200 |
),
|
201 |
)
|
202 |
|
203 |
+
#@title 学習実行
|
204 |
trainer_stats = trainer.train()
|
205 |
|
206 |
+
# ELYZA-tasks-100-TVの読み込み。事前にファイルをアップロードしてください
|
207 |
+
# データセットの読み込み。
|
208 |
+
# omnicampusの開発環境では、左にタスクのjsonlをドラッグアンドドロップしてから実行。
|
209 |
import json
|
210 |
datasets = []
|
211 |
with open("/content/elyza-tasks-100-TV_0.jsonl", "r") as f:
|
|
|
217 |
datasets.append(json.loads(item))
|
218 |
item = ""
|
219 |
|
220 |
+
# 学習したモデルを用いてタスクを実行
|
221 |
from tqdm import tqdm
|
222 |
+
|
223 |
+
# 推論するためにモデルのモードを変更
|
224 |
FastLanguageModel.for_inference(model)
|
225 |
|
226 |
results = []
|
|
|
236 |
|
237 |
results.append({"task_id": dt["task_id"], "input": input, "output": prediction})
|
238 |
|
239 |
+
# jsonlで保存
|
240 |
with open(f"./{new_model_id}_output.jsonl", 'w', encoding='utf-8') as f:
|
241 |
for result in results:
|
242 |
json.dump(result, f, ensure_ascii=False)
|
243 |
f.write('\n')
|
244 |
|
245 |
+
# モデルとトークナイザーをHugging Faceにアップロード
|
246 |
model.push_to_hub(new_model_id, token=HF_TOKEN, private=True)
|
247 |
tokenizer.push_to_hub(new_model_id, token=HF_TOKEN, private=True)
|
248 |
+
```
|