Update README.md
Browse files
README.md
CHANGED
@@ -29,28 +29,22 @@ This llama model was trained 2x faster with [Unsloth](https://github.com/unsloth
|
|
29 |
'''
|
30 |
|
31 |
%%capture #結果を非表示にするセルマジックコマンド
|
32 |
-
# Google Colab の場合は上記の環境構築手順を行なわず、単にこのセルから実行していってください。
|
33 |
!pip uninstall unsloth -y
|
34 |
!pip install --upgrade --no-cache-dir "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
|
35 |
|
36 |
%%capture #結果を非表示にするセルマジックコマンド
|
37 |
-
# Google Colab のデフォルトで入っているパッケージをアップグレード(Moriyasu さんありがとうございます)
|
38 |
!pip install --upgrade torch
|
39 |
!pip install --upgrade xformers
|
40 |
|
41 |
%%capture #結果を非表示にするセルマジックコマンド
|
42 |
-
# notebookでインタラクティブな表示を可能とする(ただし、うまく動かない場合あり)
|
43 |
-
# Google Colabでは実行不要
|
44 |
!pip install ipywidgets --upgrade
|
45 |
|
46 |
-
# Install Flash Attention 2 for softcapping support
|
47 |
import torch
|
48 |
if torch.cuda.get_device_capability()[0] >= 8:
|
49 |
!pip install --no-deps packaging ninja einops "flash-attn>=2.6.3"
|
50 |
|
51 |
HF_TOKEN = "your token"
|
52 |
|
53 |
-
# llm-jp/llm-jp-3-13bを4bit量子化のqLoRA設定でロード。
|
54 |
|
55 |
from unsloth import FastLanguageModel
|
56 |
import torch
|
@@ -60,7 +54,7 @@ load_in_4bit = True # 今回は13Bモデルを扱うためTrue
|
|
60 |
|
61 |
model_id = "llm-jp/llm-jp-3-13b"
|
62 |
new_model_id = "llm-jp-3-13b-it" #Fine-Tuningしたモデルにつけたい名前、it: Instruction Tuning
|
63 |
-
|
64 |
model, tokenizer = FastLanguageModel.from_pretrained(
|
65 |
model_name=model_id,
|
66 |
dtype=dtype,
|
@@ -68,7 +62,6 @@ model, tokenizer = FastLanguageModel.from_pretrained(
|
|
68 |
trust_remote_code=True,
|
69 |
)
|
70 |
|
71 |
-
# SFT用のモデルを用意
|
72 |
model = FastLanguageModel.get_peft_model(
|
73 |
model,
|
74 |
r = 32,
|
@@ -84,15 +77,13 @@ model = FastLanguageModel.get_peft_model(
|
|
84 |
max_seq_length = max_seq_length,
|
85 |
)
|
86 |
|
87 |
-
# 学習に用いるデータセットの指定
|
88 |
from datasets import load_dataset
|
89 |
|
90 |
dataset = load_dataset("json", data_files="./ichikara-instruction-003-001-1.json")
|
91 |
|
92 |
-
# 学習時のプロンプトフォーマットの定義
|
93 |
prompt = """### 指示
|
94 |
{}
|
95 |
-
|
96 |
{}"""
|
97 |
|
98 |
|
@@ -107,7 +98,6 @@ def formatting_prompts_func(examples):
|
|
107 |
return { "formatted_text" : text, } # 新しいフィールド "formatted_text" を返す
|
108 |
pass
|
109 |
|
110 |
-
# # 各データにフォーマットを適用
|
111 |
dataset = dataset.map(
|
112 |
formatting_prompts_func,
|
113 |
num_proc= 4, # 並列処理数を指定
|
@@ -115,7 +105,6 @@ dataset = dataset.map(
|
|
115 |
|
116 |
dataset
|
117 |
|
118 |
-
# データを確認
|
119 |
print(dataset["train"]["formatted_text"][3])
|
120 |
|
121 |
"""
|
@@ -218,9 +207,7 @@ print(f"{start_gpu_memory} GB of memory reserved.")
|
|
218 |
#@title 学習実行
|
219 |
trainer_stats = trainer.train()
|
220 |
|
221 |
-
|
222 |
-
# データセットの読み込み。
|
223 |
-
# omnicampusの開発環境では、左にタスクのjsonlをドラッグアンドドロップしてから実行。
|
224 |
import json
|
225 |
datasets = []
|
226 |
with open("./elyza-tasks-100-TV_0.jsonl", "r") as f:
|
@@ -232,10 +219,8 @@ with open("./elyza-tasks-100-TV_0.jsonl", "r") as f:
|
|
232 |
datasets.append(json.loads(item))
|
233 |
item = ""
|
234 |
|
235 |
-
# 学習したモデルを用いてタスクを実行
|
236 |
from tqdm import tqdm
|
237 |
|
238 |
-
# 推論するためにモデルのモードを変更
|
239 |
FastLanguageModel.for_inference(model)
|
240 |
|
241 |
results = []
|
@@ -251,14 +236,11 @@ for dt in tqdm(datasets):
|
|
251 |
|
252 |
results.append({"task_id": dt["task_id"], "input": input, "output": prediction})
|
253 |
|
254 |
-
# jsonlで保存
|
255 |
with open(f"{new_model_id}_output.jsonl", 'w', encoding='utf-8') as f:
|
256 |
for result in results:
|
257 |
json.dump(result, f, ensure_ascii=False)
|
258 |
f.write('\n')
|
259 |
|
260 |
-
# LoRAアダプタだけ保存
|
261 |
-
# 書き込み可能なtoken
|
262 |
HF_TOKEN = "your token"
|
263 |
|
264 |
model.push_to_hub_merged(
|
@@ -274,14 +256,12 @@ model.push_to_hub_merged(
|
|
274 |
|
275 |
保存したモデルの使い方
|
276 |
'''
|
277 |
-
# 必要なライブラリをインストール
|
278 |
%%capture
|
279 |
!pip install unsloth
|
280 |
!pip uninstall unsloth -y && pip install --upgrade --no-cache-dir "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
|
281 |
!pip install -U torch
|
282 |
!pip install -U peft
|
283 |
|
284 |
-
# 必要なライブラリを読み込み
|
285 |
from unsloth import FastLanguageModel
|
286 |
from peft import PeftModel
|
287 |
import torch
|
@@ -289,13 +269,11 @@ import json
|
|
289 |
from tqdm import tqdm
|
290 |
import re
|
291 |
|
292 |
-
# ベースとなるモデルと学習したLoRAのアダプタ(Hugging FaceのIDを指定)。
|
293 |
model_id = "llm-jp/llm-jp-3-13b"
|
294 |
adapter_id = "okaba815/llm-jp-3-13b-it_lora"
|
295 |
|
296 |
HF_TOKEN = "your token"
|
297 |
|
298 |
-
# unslothのFastLanguageModelで元のモデルをロード。
|
299 |
dtype = None # Noneにしておけば自動で設定
|
300 |
load_in_4bit = True # 今回は13Bモデルを扱うためTrue
|
301 |
|
@@ -306,12 +284,9 @@ model, tokenizer = FastLanguageModel.from_pretrained(
|
|
306 |
trust_remote_code=True,
|
307 |
)
|
308 |
|
309 |
-
# 元のモデルにLoRAのアダプタを統合。
|
310 |
model = PeftModel.from_pretrained(model, adapter_id, token = HF_TOKEN)
|
311 |
|
312 |
|
313 |
-
# タスクとなるデータの読み込み。
|
314 |
-
# 事前にデータをアップロードしてください。
|
315 |
datasets = []
|
316 |
with open("./elyza-tasks-100-TV_0.jsonl", "r") as f:
|
317 |
item = ""
|
@@ -322,9 +297,7 @@ with open("./elyza-tasks-100-TV_0.jsonl", "r") as f:
|
|
322 |
datasets.append(json.loads(item))
|
323 |
item = ""
|
324 |
|
325 |
-
# モデルを用いてタスクの推論。
|
326 |
|
327 |
-
# 推論するためにモデルのモードを変更
|
328 |
FastLanguageModel.for_inference(model)
|
329 |
|
330 |
results = []
|
@@ -340,9 +313,7 @@ for dt in tqdm(datasets):
|
|
340 |
|
341 |
results.append({"task_id": dt["task_id"], "input": input, "output": prediction})
|
342 |
|
343 |
-
# 結果をjsonlで保存。
|
344 |
|
345 |
-
# ここではadapter_idを元にファイル名を決定しているが、ファイル名は任意で問題なし。
|
346 |
json_file_id = re.sub(".*/", "", adapter_id)
|
347 |
with open(f"/content/{json_file_id}_output.jsonl", 'w', encoding='utf-8') as f:
|
348 |
for result in results:
|
|
|
29 |
'''
|
30 |
|
31 |
%%capture #結果を非表示にするセルマジックコマンド
|
|
|
32 |
!pip uninstall unsloth -y
|
33 |
!pip install --upgrade --no-cache-dir "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
|
34 |
|
35 |
%%capture #結果を非表示にするセルマジックコマンド
|
|
|
36 |
!pip install --upgrade torch
|
37 |
!pip install --upgrade xformers
|
38 |
|
39 |
%%capture #結果を非表示にするセルマジックコマンド
|
|
|
|
|
40 |
!pip install ipywidgets --upgrade
|
41 |
|
|
|
42 |
import torch
|
43 |
if torch.cuda.get_device_capability()[0] >= 8:
|
44 |
!pip install --no-deps packaging ninja einops "flash-attn>=2.6.3"
|
45 |
|
46 |
HF_TOKEN = "your token"
|
47 |
|
|
|
48 |
|
49 |
from unsloth import FastLanguageModel
|
50 |
import torch
|
|
|
54 |
|
55 |
model_id = "llm-jp/llm-jp-3-13b"
|
56 |
new_model_id = "llm-jp-3-13b-it" #Fine-Tuningしたモデルにつけたい名前、it: Instruction Tuning
|
57 |
+
|
58 |
model, tokenizer = FastLanguageModel.from_pretrained(
|
59 |
model_name=model_id,
|
60 |
dtype=dtype,
|
|
|
62 |
trust_remote_code=True,
|
63 |
)
|
64 |
|
|
|
65 |
model = FastLanguageModel.get_peft_model(
|
66 |
model,
|
67 |
r = 32,
|
|
|
77 |
max_seq_length = max_seq_length,
|
78 |
)
|
79 |
|
|
|
80 |
from datasets import load_dataset
|
81 |
|
82 |
dataset = load_dataset("json", data_files="./ichikara-instruction-003-001-1.json")
|
83 |
|
|
|
84 |
prompt = """### 指示
|
85 |
{}
|
86 |
+
|
87 |
{}"""
|
88 |
|
89 |
|
|
|
98 |
return { "formatted_text" : text, } # 新しいフィールド "formatted_text" を返す
|
99 |
pass
|
100 |
|
|
|
101 |
dataset = dataset.map(
|
102 |
formatting_prompts_func,
|
103 |
num_proc= 4, # 並列処理数を指定
|
|
|
105 |
|
106 |
dataset
|
107 |
|
|
|
108 |
print(dataset["train"]["formatted_text"][3])
|
109 |
|
110 |
"""
|
|
|
207 |
#@title 学習実行
|
208 |
trainer_stats = trainer.train()
|
209 |
|
210 |
+
|
|
|
|
|
211 |
import json
|
212 |
datasets = []
|
213 |
with open("./elyza-tasks-100-TV_0.jsonl", "r") as f:
|
|
|
219 |
datasets.append(json.loads(item))
|
220 |
item = ""
|
221 |
|
|
|
222 |
from tqdm import tqdm
|
223 |
|
|
|
224 |
FastLanguageModel.for_inference(model)
|
225 |
|
226 |
results = []
|
|
|
236 |
|
237 |
results.append({"task_id": dt["task_id"], "input": input, "output": prediction})
|
238 |
|
|
|
239 |
with open(f"{new_model_id}_output.jsonl", 'w', encoding='utf-8') as f:
|
240 |
for result in results:
|
241 |
json.dump(result, f, ensure_ascii=False)
|
242 |
f.write('\n')
|
243 |
|
|
|
|
|
244 |
HF_TOKEN = "your token"
|
245 |
|
246 |
model.push_to_hub_merged(
|
|
|
256 |
|
257 |
保存したモデルの使い方
|
258 |
'''
|
|
|
259 |
%%capture
|
260 |
!pip install unsloth
|
261 |
!pip uninstall unsloth -y && pip install --upgrade --no-cache-dir "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
|
262 |
!pip install -U torch
|
263 |
!pip install -U peft
|
264 |
|
|
|
265 |
from unsloth import FastLanguageModel
|
266 |
from peft import PeftModel
|
267 |
import torch
|
|
|
269 |
from tqdm import tqdm
|
270 |
import re
|
271 |
|
|
|
272 |
model_id = "llm-jp/llm-jp-3-13b"
|
273 |
adapter_id = "okaba815/llm-jp-3-13b-it_lora"
|
274 |
|
275 |
HF_TOKEN = "your token"
|
276 |
|
|
|
277 |
dtype = None # Noneにしておけば自動で設定
|
278 |
load_in_4bit = True # 今回は13Bモデルを扱うためTrue
|
279 |
|
|
|
284 |
trust_remote_code=True,
|
285 |
)
|
286 |
|
|
|
287 |
model = PeftModel.from_pretrained(model, adapter_id, token = HF_TOKEN)
|
288 |
|
289 |
|
|
|
|
|
290 |
datasets = []
|
291 |
with open("./elyza-tasks-100-TV_0.jsonl", "r") as f:
|
292 |
item = ""
|
|
|
297 |
datasets.append(json.loads(item))
|
298 |
item = ""
|
299 |
|
|
|
300 |
|
|
|
301 |
FastLanguageModel.for_inference(model)
|
302 |
|
303 |
results = []
|
|
|
313 |
|
314 |
results.append({"task_id": dt["task_id"], "input": input, "output": prediction})
|
315 |
|
|
|
316 |
|
|
|
317 |
json_file_id = re.sub(".*/", "", adapter_id)
|
318 |
with open(f"/content/{json_file_id}_output.jsonl", 'w', encoding='utf-8') as f:
|
319 |
for result in results:
|