nishimura999
commited on
Commit
•
ef73ffc
1
Parent(s):
c35146a
Update README.md
Browse files
README.md
CHANGED
@@ -20,3 +20,92 @@ 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 |
+
|
26 |
+
# usage
|
27 |
+
## -import
|
28 |
+
```python
|
29 |
+
# 必要なライブラリを読み込み
|
30 |
+
from unsloth import FastLanguageModel
|
31 |
+
from peft import PeftModel
|
32 |
+
import torch
|
33 |
+
import json
|
34 |
+
from tqdm import tqdm
|
35 |
+
import re
|
36 |
+
```
|
37 |
+
|
38 |
+
## -setting
|
39 |
+
```python
|
40 |
+
# Hugging Faceで取得したToken
|
41 |
+
HF_TOKEN = "{Your hugging face token}"
|
42 |
+
|
43 |
+
# モデルのIDと、LoRAのアダプタ名
|
44 |
+
model_id = "llm-jp/llm-jp-3-13b"
|
45 |
+
adapter_id = "nishimura999/llm-jp-3-13b-it-v106_lora"
|
46 |
+
```
|
47 |
+
|
48 |
+
## -load
|
49 |
+
```python
|
50 |
+
# unslothのFastLanguageModelで元のモデルをロード。
|
51 |
+
dtype = None
|
52 |
+
load_in_4bit = True
|
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 |
+
# 元のモデルにLoRAのアダプタを統合。
|
60 |
+
model = PeftModel.from_pretrained(model, adapter_id, token = HF_TOKEN)
|
61 |
+
```
|
62 |
+
|
63 |
+
## -dataset
|
64 |
+
```python
|
65 |
+
# データセットの読み込み。
|
66 |
+
datasets = []
|
67 |
+
with open("./elyza-tasks-100-TV_0.jsonl", "r") as f:
|
68 |
+
item = ""
|
69 |
+
for line in f:
|
70 |
+
line = line.strip()
|
71 |
+
item += line
|
72 |
+
if item.endswith("}"):
|
73 |
+
datasets.append(json.loads(item))
|
74 |
+
item = ""
|
75 |
+
```
|
76 |
+
|
77 |
+
## -generate
|
78 |
+
```python
|
79 |
+
# モデルを用いてタスクの推論。
|
80 |
+
|
81 |
+
FastLanguageModel.for_inference(model)
|
82 |
+
|
83 |
+
results = []
|
84 |
+
for dt in tqdm(datasets):
|
85 |
+
input = dt["input"]
|
86 |
+
|
87 |
+
prompt = f"""### 指示\n{input}\n### 回答\n"""
|
88 |
+
|
89 |
+
inputs = tokenizer([prompt], return_tensors = "pt").to(model.device)
|
90 |
+
|
91 |
+
outputs = model.generate(**inputs, max_new_tokens = 1024, use_cache = True, do_sample=False, repetition_penalty=1.2)
|
92 |
+
prediction = tokenizer.decode(outputs[0], skip_special_tokens=True).split('\n### 回答')[-1]
|
93 |
+
|
94 |
+
results.append({"task_id": dt["task_id"], "input": input, "output": prediction})
|
95 |
+
```
|
96 |
+
|
97 |
+
## -output
|
98 |
+
```python
|
99 |
+
# 結果をjsonlで保存。
|
100 |
+
json_file_id = re.sub(".*/", "", adapter_id)
|
101 |
+
with open(f"./{json_file_id}_output.jsonl", 'w', encoding='utf-8') as f:
|
102 |
+
for result in results:
|
103 |
+
json.dump(result, f, ensure_ascii=False)
|
104 |
+
f.write('\n')
|
105 |
+
```
|
106 |
+
|
107 |
+
# ref
|
108 |
+
### 本モデルは下記のデータを使ってファインチューニングしております。ここでデータ提供者に感謝申し上げます。
|
109 |
+
(https://liat-aip.sakura.ne.jp/wp/llmのための日本語インストラクションデータ作成/llmのための日本語インストラクションデータ-公開/)
|
110 |
+
関根聡, 安藤まや, 後藤美知子, 鈴木久美, 河原大輔, 井之上直也, 乾健太郎.
|
111 |
+
ichikara-instruction: LLMのための日本語インストラクションデータの構築. 言語処理学会第30回年次大会(2024)
|