Update README.md
Browse files
README.md
CHANGED
@@ -20,3 +20,102 @@ language:
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
23 |
+
|
24 |
+
# 出力方法の流れ
|
25 |
+
1. 必要ライブラリのインストール
|
26 |
+
2. 必要ライブラリの読み込み
|
27 |
+
3. ベースモデルと学習したLoRAのアダプタの指定
|
28 |
+
4. Hugging Face Tokenの設定
|
29 |
+
5. unslothのFastLanguageModelで元のモデルのロード
|
30 |
+
6. 元のモデルへのLoRAのアダプタの統合
|
31 |
+
7. タスクとなるデータ(elyza-tasks-100-TV_0.jsonl)の読み込み
|
32 |
+
8. モデルを用いてタスクの推論
|
33 |
+
9. 結果をjsonlへ保存
|
34 |
+
|
35 |
+
# コード
|
36 |
+
# 1. 必要ライブラリのインストール
|
37 |
+
```
|
38 |
+
pip install unsloth
|
39 |
+
pip uninstall unsloth -y && pip install --upgrade --no-cache-dir "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
|
40 |
+
pip install -U torch
|
41 |
+
pip install -U peft
|
42 |
+
```
|
43 |
+
|
44 |
+
# 2. 必要ライブラリの読み込み
|
45 |
+
```
|
46 |
+
from unsloth import FastLanguageModel
|
47 |
+
from peft import PeftModel
|
48 |
+
import torch
|
49 |
+
import json
|
50 |
+
from tqdm import tqdm
|
51 |
+
import re
|
52 |
+
```
|
53 |
+
|
54 |
+
# 3. ベースモデルと学習したLoRAのアダプタの指定
|
55 |
+
```
|
56 |
+
model_id = "llm-jp/llm-jp-3-13b"
|
57 |
+
adapter_id = "sejeshin/llm-jp-3-13b-it2"
|
58 |
+
```
|
59 |
+
|
60 |
+
# 4. Hugging Face Tokenの設定
|
61 |
+
```
|
62 |
+
HF_TOKEN = "" #Hugging Faceのトークン
|
63 |
+
```
|
64 |
+
|
65 |
+
# 5. unslothのFastLanguageModelで元のモデルのロード
|
66 |
+
```
|
67 |
+
dtype = None # Noneにしておけば自動で設定
|
68 |
+
load_in_4bit = True # 今回は13Bモデルを扱うためTrue
|
69 |
+
|
70 |
+
model, tokenizer = FastLanguageModel.from_pretrained(
|
71 |
+
model_name=model_id,
|
72 |
+
dtype=dtype,
|
73 |
+
load_in_4bit=load_in_4bit,
|
74 |
+
trust_remote_code=True,
|
75 |
+
)
|
76 |
+
```
|
77 |
+
|
78 |
+
# 6. 元のモデルへのLoRAのアダプタの統合
|
79 |
+
```
|
80 |
+
model = PeftModel.from_pretrained(model, adapter_id, token = HF_TOKEN)
|
81 |
+
```
|
82 |
+
|
83 |
+
# 7. タスクとなるデータ(elyza-tasks-100-TV_0.jsonl)の読み込み
|
84 |
+
```
|
85 |
+
datasets = []
|
86 |
+
with open("./elyza-tasks-100-TV_0.jsonl", "r") as f:
|
87 |
+
item = ""
|
88 |
+
for line in f:
|
89 |
+
line = line.strip()
|
90 |
+
item += line
|
91 |
+
if item.endswith("}"):
|
92 |
+
datasets.append(json.loads(item))
|
93 |
+
item = ""
|
94 |
+
```
|
95 |
+
|
96 |
+
# 8. モデルを用いてタスクの推論
|
97 |
+
```
|
98 |
+
FastLanguageModel.for_inference(model)
|
99 |
+
|
100 |
+
results = []
|
101 |
+
for dt in tqdm(datasets):
|
102 |
+
input = dt["input"]
|
103 |
+
|
104 |
+
prompt = f"""### 指示\n{input}\n### 回答\n"""
|
105 |
+
|
106 |
+
inputs = tokenizer([prompt], return_tensors = "pt").to(model.device)
|
107 |
+
|
108 |
+
outputs = model.generate(**inputs, max_new_tokens = 512, use_cache = True, do_sample=False, repetition_penalty=1.2)
|
109 |
+
prediction = tokenizer.decode(outputs[0], skip_special_tokens=True).split('\n### 回答')[-1]
|
110 |
+
|
111 |
+
results.append({"task_id": dt["task_id"], "input": input, "output": prediction})
|
112 |
+
```
|
113 |
+
|
114 |
+
# 9. 結果をjsonlへ保存
|
115 |
+
```
|
116 |
+
json_file_id = re.sub(".*/", "", adapter_id)
|
117 |
+
with open(f"/content/{json_file_id}_output.jsonl", 'w', encoding='utf-8') as f:
|
118 |
+
for result in results:
|
119 |
+
json.dump(result, f, ensure_ascii=False)
|
120 |
+
f.write('\n')
|
121 |
+
```
|