WatariNAKANO
commited on
Commit
•
fa42f6e
1
Parent(s):
0f4a391
Update README.md
Browse files
README.md
CHANGED
@@ -9,6 +9,7 @@ tags:
|
|
9 |
license: apache-2.0
|
10 |
language:
|
11 |
- en
|
|
|
12 |
---
|
13 |
|
14 |
# Uploaded model
|
@@ -16,7 +17,185 @@ language:
|
|
16 |
- **Developed by:** WatariNAKANO
|
17 |
- **License:** apache-2.0
|
18 |
- **Finetuned from model :** llm-jp/llm-jp-3-13b
|
|
|
|
|
|
|
19 |
|
20 |
This llama model was trained 2x faster with [Unsloth](https://github.com/unslothai/unsloth) and Huggingface's TRL library.
|
21 |
|
22 |
[<img src="https://raw.githubusercontent.com/unslothai/unsloth/main/images/unsloth%20made%20with%20love.png" width="200"/>](https://github.com/unslothai/unsloth)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
license: apache-2.0
|
10 |
language:
|
11 |
- en
|
12 |
+
- ja
|
13 |
---
|
14 |
|
15 |
# Uploaded model
|
|
|
17 |
- **Developed by:** WatariNAKANO
|
18 |
- **License:** apache-2.0
|
19 |
- **Finetuned from model :** llm-jp/llm-jp-3-13b
|
20 |
+
- **使用したデータセット :** ichikara-instruction-003-001-1.json
|
21 |
+
- **ライセンス :** CC-BY-NC-SA
|
22 |
+
- **実行環境 :** Google Colab(L4)
|
23 |
|
24 |
This llama model was trained 2x faster with [Unsloth](https://github.com/unslothai/unsloth) and Huggingface's TRL library.
|
25 |
|
26 |
[<img src="https://raw.githubusercontent.com/unslothai/unsloth/main/images/unsloth%20made%20with%20love.png" width="200"/>](https://github.com/unslothai/unsloth)
|
27 |
+
|
28 |
+
# コードの説明
|
29 |
+
|
30 |
+
```python
|
31 |
+
|
32 |
+
# 必要なライブラリをインストール
|
33 |
+
!pip uninstall unsloth -y
|
34 |
+
!pip install --upgrade --no-cache-dir "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
|
35 |
+
!pip install --upgrade torch
|
36 |
+
!pip install --upgrade xformers
|
37 |
+
|
38 |
+
# Install Flash Attention 2 for softcapping support
|
39 |
+
import torch
|
40 |
+
if torch.cuda.get_device_capability()[0] >= 8:
|
41 |
+
!pip install --no-deps packaging ninja einops "flash-attn>=2.6.3"
|
42 |
+
|
43 |
+
# Hugging Face Token を指定
|
44 |
+
HF_TOKEN = "your-token" #@param {type:"string"}
|
45 |
+
|
46 |
+
# llm-jp/llm-jp-3-13bを4bit量子化のqLoRA設定でロード。
|
47 |
+
from unsloth import FastLanguageModel
|
48 |
+
import torch
|
49 |
+
max_seq_length = 768 # unslothではRoPEをサポートしているのでコンテキスト長は自由に設定可能
|
50 |
+
dtype = None # Noneにしておけば自動で設定
|
51 |
+
load_in_4bit = True # 今回は13Bモデルを扱うためTrue
|
52 |
+
|
53 |
+
model_id = "llm-jp/llm-jp-3-13b"
|
54 |
+
new_model_id = "llm-jp-3-13b-it-1217" #Fine-Tuningしたモデルにつけたい名前、it: Instruction Tuning
|
55 |
+
# FastLanguageModel インスタンスを作成
|
56 |
+
model, tokenizer = FastLanguageModel.from_pretrained(
|
57 |
+
model_name=model_id,
|
58 |
+
dtype=dtype,
|
59 |
+
load_in_4bit=load_in_4bit,
|
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",
|
72 |
+
use_gradient_checkpointing = "unsloth",
|
73 |
+
random_state = 3407,
|
74 |
+
use_rslora = False,
|
75 |
+
loftq_config = None,
|
76 |
+
max_seq_length = max_seq_length,
|
77 |
+
)
|
78 |
+
|
79 |
+
# 学習に用いるデータセットの指定
|
80 |
+
# CC-BY-NC-SAですのでモデルはライセンスを継承する前提でお使いください。
|
81 |
+
# https://liat-aip.sakura.ne.jp/wp/llmのための日本語インストラクションデータ作成/llmのための日本語インストラクションデータ-公開/
|
82 |
+
# 関根聡, 安藤まや, 後藤美知子, 鈴木久美, 河原大輔, 井之上直也, 乾健太郎. ichikara-instruction: LLMのための日本語インストラクションデータの構築. 言語処理学会第30回年次大会(2024)
|
83 |
+
|
84 |
+
from datasets import load_dataset
|
85 |
+
|
86 |
+
dataset = load_dataset("json", data_files="/content/ichikara-instruction-003-001-1.json")
|
87 |
+
|
88 |
+
# 学習時のプロンプトフォーマットの定義
|
89 |
+
prompt = """### 指示
|
90 |
+
{}
|
91 |
+
### 回答
|
92 |
+
{}"""
|
93 |
+
|
94 |
+
|
95 |
+
"""
|
96 |
+
formatting_prompts_func: 各データをプロンプトに合わせた形式に合わせる
|
97 |
+
"""
|
98 |
+
EOS_TOKEN = tokenizer.eos_token # トークナイザーのEOSトークン(文末トークン)
|
99 |
+
def formatting_prompts_func(examples):
|
100 |
+
input = examples["text"] # 入力データ
|
101 |
+
output = examples["output"] # 出力データ
|
102 |
+
text = prompt.format(input, output) + EOS_TOKEN # プロンプトの作成
|
103 |
+
return { "formatted_text" : text, } # 新しいフィールド "formatted_text" を返す
|
104 |
+
pass
|
105 |
+
|
106 |
+
# # 各データにフォーマットを適用
|
107 |
+
dataset = dataset.map(
|
108 |
+
formatting_prompts_func,
|
109 |
+
num_proc= 4, # 並列処理数を指定
|
110 |
+
)
|
111 |
+
|
112 |
+
dataset
|
113 |
+
|
114 |
+
|
115 |
+
# training_arguments: 学習の設定
|
116 |
+
from trl import SFTTrainer
|
117 |
+
from transformers import TrainingArguments
|
118 |
+
from unsloth import is_bfloat16_supported
|
119 |
+
|
120 |
+
trainer = SFTTrainer(
|
121 |
+
model = model,
|
122 |
+
tokenizer = tokenizer,
|
123 |
+
train_dataset=dataset["train"],
|
124 |
+
max_seq_length = max_seq_length,
|
125 |
+
dataset_text_field="formatted_text",
|
126 |
+
packing = False,
|
127 |
+
args = TrainingArguments(
|
128 |
+
per_device_train_batch_size = 2,
|
129 |
+
gradient_accumulation_steps = 4,
|
130 |
+
num_train_epochs = 1,
|
131 |
+
logging_steps = 10,
|
132 |
+
warmup_steps = 10,
|
133 |
+
save_steps=100,
|
134 |
+
save_total_limit=2,
|
135 |
+
max_steps=-1,
|
136 |
+
learning_rate = 2e-4,
|
137 |
+
fp16 = not is_bfloat16_supported(),
|
138 |
+
bf16 = is_bfloat16_supported(),
|
139 |
+
group_by_length=True,
|
140 |
+
seed = 3407,
|
141 |
+
output_dir = "outputs",
|
142 |
+
report_to = "none",
|
143 |
+
),
|
144 |
+
)
|
145 |
+
|
146 |
+
|
147 |
+
#@title 学習実行
|
148 |
+
trainer_stats = trainer.train()
|
149 |
+
|
150 |
+
|
151 |
+
# データセットの読み込み。
|
152 |
+
import json
|
153 |
+
datasets = []
|
154 |
+
with open("/content//elyza-tasks-100-TV_0.jsonl", "r") as f:
|
155 |
+
item = ""
|
156 |
+
for line in f:
|
157 |
+
line = line.strip()
|
158 |
+
item += line
|
159 |
+
if item.endswith("}"):
|
160 |
+
datasets.append(json.loads(item))
|
161 |
+
item = ""
|
162 |
+
|
163 |
+
|
164 |
+
# 学習したモデルを用いてタスクを実行
|
165 |
+
from tqdm import tqdm
|
166 |
+
|
167 |
+
# 推論するためにモデルのモードを変更
|
168 |
+
FastLanguageModel.for_inference(model)
|
169 |
+
|
170 |
+
results = []
|
171 |
+
for dt in tqdm(datasets):
|
172 |
+
input = dt["input"]
|
173 |
+
|
174 |
+
prompt = f"""### 指示\n{input}\n### 回答\n"""
|
175 |
+
|
176 |
+
inputs = tokenizer([prompt], return_tensors = "pt").to(model.device)
|
177 |
+
|
178 |
+
outputs = model.generate(**inputs, max_new_tokens = 1024, use_cache = True, do_sample=False, repetition_penalty=1.2)
|
179 |
+
prediction = tokenizer.decode(outputs[0], skip_special_tokens=True).split('\n### 回答')[-1]
|
180 |
+
|
181 |
+
results.append({"task_id": dt["task_id"], "input": input, "output": prediction})
|
182 |
+
|
183 |
+
|
184 |
+
# jsonlで保存
|
185 |
+
with open(f"{new_model_id}_output.jsonl", 'w', encoding='utf-8') as f:
|
186 |
+
for result in results:
|
187 |
+
json.dump(result, f, ensure_ascii=False)
|
188 |
+
f.write('\n')
|
189 |
+
|
190 |
+
|
191 |
+
# LoRAアダプタだけ保存
|
192 |
+
new_model_id = "WatariNAKANO/llm-jp-3-13b-it-1217" #Fine-Tuningしたモデルにつけたい名前
|
193 |
+
model.push_to_hub_merged(
|
194 |
+
new_model_id+"_lora",
|
195 |
+
tokenizer=tokenizer,
|
196 |
+
save_method="lora",
|
197 |
+
token=HF_TOKEN,
|
198 |
+
private=True
|
199 |
+
)
|
200 |
+
|
201 |
+
```
|