Update README.md
Browse files
README.md
CHANGED
@@ -6,17 +6,193 @@ tags:
|
|
6 |
- unsloth
|
7 |
- llama
|
8 |
- trl
|
9 |
-
license:
|
10 |
language:
|
11 |
- en
|
12 |
---
|
13 |
|
14 |
-
#
|
15 |
|
16 |
- **Developed by:** tabachain
|
17 |
-
- **License:**
|
18 |
- **Finetuned from model :** llm-jp/llm-jp-3-13b
|
19 |
|
20 |
-
|
|
|
21 |
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
- unsloth
|
7 |
- llama
|
8 |
- trl
|
9 |
+
license: cc-by-nc-3.0
|
10 |
language:
|
11 |
- en
|
12 |
---
|
13 |
|
14 |
+
# How to use
|
15 |
|
16 |
- **Developed by:** tabachain
|
17 |
+
- **License:** cc-by-nc-3.0
|
18 |
- **Finetuned from model :** llm-jp/llm-jp-3-13b
|
19 |
|
20 |
+
Colab L4での動作確認方法とOmnicampusでの動作確認方法をそれぞれ載せます。
|
21 |
+
(動さ確認はColab L4中心で行っているため、可能な限りColab L4での利用を推奨します)
|
22 |
|
23 |
+
|
24 |
+
### Colab L4での動作確認方法
|
25 |
+
```python
|
26 |
+
# 必要なライブラリをインストール
|
27 |
+
%%capture
|
28 |
+
!pip install unsloth
|
29 |
+
!pip uninstall unsloth -y && pip install --upgrade --no-cache-dir "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
|
30 |
+
!pip install -U torch
|
31 |
+
!pip install -U peft
|
32 |
+
|
33 |
+
# 必要なライブラリを読み込み
|
34 |
+
from unsloth import FastLanguageModel
|
35 |
+
from peft import PeftModel
|
36 |
+
import torch
|
37 |
+
import json
|
38 |
+
from tqdm import tqdm
|
39 |
+
import re
|
40 |
+
|
41 |
+
# ベースとなるモデルと学習したLoRAのアダプタ(Hugging FaceのIDを指定)。
|
42 |
+
model_id = "llm-jp/llm-jp-3-13b"
|
43 |
+
adapter_id = "tabachain/llm-jp-3-13b-it_output_r64"
|
44 |
+
|
45 |
+
# colabのシークレットにHF_TOKENを入れておき、それを読み込む
|
46 |
+
from google.colab import userdata
|
47 |
+
HF_TOKEN=userdata.get('HF_TOKEN')
|
48 |
+
|
49 |
+
# unslothのFastLanguageModelで元のモデルをロード。
|
50 |
+
dtype = None # Noneにしておけば自動で設定
|
51 |
+
load_in_4bit = True # 今回は13Bモデルを扱うためTrue
|
52 |
+
|
53 |
+
model, tokenizer = FastLanguageModel.from_pretrained(
|
54 |
+
model_name=model_id,
|
55 |
+
dtype=dtype,
|
56 |
+
load_in_4bit=load_in_4bit,
|
57 |
+
trust_remote_code=True,
|
58 |
+
)
|
59 |
+
|
60 |
+
# 元のモデルにLoRAのアダプタを統合。
|
61 |
+
model = PeftModel.from_pretrained(model, adapter_id, token = HF_TOKEN)
|
62 |
+
|
63 |
+
# タスクとなるデータの読み込み。
|
64 |
+
# 事前にデータをアップロードしてください。
|
65 |
+
datasets = []
|
66 |
+
with open("./elyza-tasks-100-TV_0.jsonl", "r") as f:
|
67 |
+
item = ""
|
68 |
+
for line in f:
|
69 |
+
line = line.strip()
|
70 |
+
item += line
|
71 |
+
if item.endswith("}"):
|
72 |
+
datasets.append(json.loads(item))
|
73 |
+
item = ""
|
74 |
+
|
75 |
+
# モデルを用いてタスクの推論。
|
76 |
+
|
77 |
+
# 推論するためにモデルのモードを変更
|
78 |
+
FastLanguageModel.for_inference(model)
|
79 |
+
|
80 |
+
results = []
|
81 |
+
for dt in tqdm(datasets):
|
82 |
+
input = dt["input"]
|
83 |
+
|
84 |
+
prompt = f"""### 指示\n{input} 簡潔に回答して \n### 回答\n"""
|
85 |
+
|
86 |
+
inputs = tokenizer([prompt], return_tensors = "pt").to(model.device)
|
87 |
+
outputs = model.generate(**inputs, max_new_tokens = 512, use_cache = True, do_sample=False, repetition_penalty=1.2)
|
88 |
+
prediction = tokenizer.decode(outputs[0], skip_special_tokens=True).split('\n### 回答')[-1]
|
89 |
+
results.append({"task_id": dt["task_id"], "input": input, "output": prediction})
|
90 |
+
|
91 |
+
# 結果をjsonlで保存。
|
92 |
+
|
93 |
+
with open(f"/content/output.jsonl", 'w', encoding='utf-8') as f:
|
94 |
+
for result in results:
|
95 |
+
json.dump(result, f, ensure_ascii=False)
|
96 |
+
f.write('\n')
|
97 |
+
```
|
98 |
+
|
99 |
+
### Omnicampusでの動作確認方法
|
100 |
+
```python
|
101 |
+
!pip install -U bitsandbytes
|
102 |
+
!pip install -U transformers
|
103 |
+
!pip install -U accelerate
|
104 |
+
!pip install -U datasets
|
105 |
+
!pip install -U peft
|
106 |
+
|
107 |
+
# notebookでインタラクティブな表示を可能とする(ただし、うまく動かない場合あり)
|
108 |
+
!pip install ipywidgets --upgrade
|
109 |
+
|
110 |
+
|
111 |
+
from transformers import (
|
112 |
+
AutoModelForCausalLM,
|
113 |
+
AutoTokenizer,
|
114 |
+
BitsAndBytesConfig,
|
115 |
+
)
|
116 |
+
from peft import PeftModel
|
117 |
+
import torch
|
118 |
+
from tqdm import tqdm
|
119 |
+
import json
|
120 |
+
|
121 |
+
|
122 |
+
# Hugging Faceで取得したTokenをこちらに貼る。
|
123 |
+
HF_TOKEN = ""
|
124 |
+
|
125 |
+
# ベースとなるモデルと学習したLoRAのアダプタ。
|
126 |
+
# model_idの値はomnicampusの環境におけるモデルのパスを表しており、それ以外の環境で実行する場合は変更の必要があります。
|
127 |
+
model_id = "models/models--llm-jp--llm-jp-3-13b/snapshots/cd3823f4c1fcbb0ad2e2af46036ab1b0ca13192a"
|
128 |
+
adapter_id = "tabachain/llm-jp-3-13b-it_output_r64"
|
129 |
+
|
130 |
+
# QLoRA config
|
131 |
+
bnb_config = BitsAndBytesConfig(
|
132 |
+
load_in_4bit=True,
|
133 |
+
bnb_4bit_quant_type="nf4",
|
134 |
+
bnb_4bit_compute_dtype=torch.bfloat16,
|
135 |
+
)
|
136 |
+
|
137 |
+
# Load model
|
138 |
+
model = AutoModelForCausalLM.from_pretrained(
|
139 |
+
model_id,
|
140 |
+
quantization_config=bnb_config,
|
141 |
+
device_map="auto",
|
142 |
+
token = HF_TOKEN
|
143 |
+
)
|
144 |
+
|
145 |
+
# Load tokenizer
|
146 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True, token = HF_TOKEN)
|
147 |
+
|
148 |
+
# 元のモデルにLoRAのアダプタを統合。
|
149 |
+
model = PeftModel.from_pretrained(model, adapter_id, token = HF_TOKEN)
|
150 |
+
|
151 |
+
# データセットの読み込み。
|
152 |
+
# omnicampusの開発環境では、左にタスクのjsonlをドラッグアンドドロップしてから実行。
|
153 |
+
datasets = []
|
154 |
+
with open("./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 |
+
# llmjp
|
164 |
+
results = []
|
165 |
+
for data in tqdm(datasets):
|
166 |
+
|
167 |
+
input = data["input"]
|
168 |
+
|
169 |
+
prompt = f"""### 指示
|
170 |
+
{input} 簡潔に回答して
|
171 |
+
### 回答
|
172 |
+
"""
|
173 |
+
print("\n" + input)
|
174 |
+
tokenized_input = tokenizer.encode(prompt, add_special_tokens=False, return_tensors="pt").to(model.device)
|
175 |
+
with torch.no_grad():
|
176 |
+
outputs = model.generate(
|
177 |
+
tokenized_input,
|
178 |
+
max_new_tokens=512,
|
179 |
+
use_cache = True,
|
180 |
+
do_sample=False,
|
181 |
+
repetition_penalty=1.2,
|
182 |
+
pad_token_id=tokenizer.eos_token_id
|
183 |
+
)[0]
|
184 |
+
output = tokenizer.decode(outputs[tokenized_input.size(1):], skip_special_tokens=True)
|
185 |
+
print("\n" + output)
|
186 |
+
|
187 |
+
results.append({"task_id": data["task_id"], "input": input, "output": output})
|
188 |
+
|
189 |
+
# こちらで生成されたjsonlを提出してください。
|
190 |
+
# 本コードではinputとeval_aspectも含んでいますが、なくても問題ありません。
|
191 |
+
# 必須なのはtask_idとoutputとなります。
|
192 |
+
import re
|
193 |
+
jsonl_id = re.sub(".*/", "", adapter_id)
|
194 |
+
with open(f"./{jsonl_id}-outputs.jsonl", 'w', encoding='utf-8') as f:
|
195 |
+
for result in results:
|
196 |
+
json.dump(result, f, ensure_ascii=False) # ensure_ascii=False for handling non-ASCII characters
|
197 |
+
f.write('\n')
|
198 |
+
```
|