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
|
@@ -20,3 +21,160 @@ 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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
license: apache-2.0
|
10 |
language:
|
11 |
- en
|
12 |
+
library_name: peft
|
13 |
---
|
14 |
|
15 |
# Uploaded model
|
|
|
21 |
This llama model was trained 2x faster with [Unsloth](https://github.com/unslothai/unsloth) and Huggingface's TRL library.
|
22 |
|
23 |
[<img src="https://raw.githubusercontent.com/unslothai/unsloth/main/images/unsloth%20made%20with%20love.png" width="200"/>](https://github.com/unslothai/unsloth)
|
24 |
+
|
25 |
+
### Training Details
|
26 |
+
指示チューニングデータとして下記のものを利用しました。
|
27 |
+
* ichikara-instruction-003-001-1.json
|
28 |
+
* ichikara-instruction-003-002-1.json
|
29 |
+
* ichikara-instruction-003-003-1.json
|
30 |
+
|
31 |
+
以下について, 指示に対して複数の回答を持つデータは回答を1件のみ抽出して指示データをして利用しました。
|
32 |
+
* ichikara-instruction-003-001-5.1.json
|
33 |
+
* ichikara-instruction-003-001-5.2.json
|
34 |
+
* ichikara-instruction-003-001-2.1.json
|
35 |
+
* ichikara-instruction-003-001-2.2.json
|
36 |
+
|
37 |
+
回答を1件のみ抽出するために gpt-4o-mini で下記 script を利用しました。
|
38 |
+
|
39 |
+
```python
|
40 |
+
# gpt-4o-mini を使って最も適切そうな回答を選定
|
41 |
+
def select_best_response(file_path, output_dir):
|
42 |
+
with open(file_path, "r", encoding="utf-8") as file:
|
43 |
+
data = json.load(file)
|
44 |
+
|
45 |
+
grouped_data = {}
|
46 |
+
|
47 |
+
# データをデータ番号ごとに分類
|
48 |
+
for item in data:
|
49 |
+
data_number = item["ID"].split("-")[4]
|
50 |
+
grouped_data.setdefault(data_number, []).append(item)
|
51 |
+
|
52 |
+
filtered_data = []
|
53 |
+
for data_number, responses in grouped_data.items():
|
54 |
+
print(f"\n#### {data_number} ####")
|
55 |
+
|
56 |
+
# 指示と回答リストを生成
|
57 |
+
instructions = [r["text"] for r in responses]
|
58 |
+
outputs = [r["output"] for r in responses]
|
59 |
+
|
60 |
+
# GPT-4o-miniに最適な回答を選定させる
|
61 |
+
prompt = (
|
62 |
+
"以下の指示に対して最も適切な回答を選定してください。\n\n"
|
63 |
+
f"### 指示 ###\n```\n{instructions[0]}\n```\n\n"
|
64 |
+
"### 候補 ###\n" + "\n\n".join([f"{i+1}: \n```\n{output}\n```" for i, output in enumerate(outputs)]) +
|
65 |
+
"\n\n### 回答 ###\n番号(1 or 2)のみを回答してください。1つだけ選択してください。\n"
|
66 |
+
)
|
67 |
+
|
68 |
+
try:
|
69 |
+
response = openai.chat.completions.create(
|
70 |
+
model="gpt-4o-mini",
|
71 |
+
messages=[
|
72 |
+
{"role": "system", "content": "あなたはIQ140の論理的思考力の高い文章評価の専門家です。"},
|
73 |
+
{"role": "user", "content": prompt}
|
74 |
+
]
|
75 |
+
)
|
76 |
+
content = response.choices[0].message.content
|
77 |
+
print(content)
|
78 |
+
best_choice = int(content.strip()) - 1
|
79 |
+
except ValueError as e:
|
80 |
+
print(f"ValueError: 無効な応答がありました。データ番号: {data_number}")
|
81 |
+
print(f"応答内容: {response.choices[0].message['content']}")
|
82 |
+
best_choice = 1
|
83 |
+
except Exception as e:
|
84 |
+
print(f"予期しないエラーが発生しました。データ番号: {data_number}")
|
85 |
+
print(e)
|
86 |
+
best_choice = 1
|
87 |
+
|
88 |
+
filtered_data.append(responses[best_choice])
|
89 |
+
|
90 |
+
output_file_name = os.path.basename(file_path)
|
91 |
+
output_file_path = os.path.join(output_dir, output_file_name)
|
92 |
+
with open(output_file_path, "w", encoding="utf-8") as output_file:
|
93 |
+
json.dump(filtered_data, output_file, ensure_ascii=False, indent=2)
|
94 |
+
```
|
95 |
+
|
96 |
+
### ライセンス
|
97 |
+
* ichikara-instruction データセットのライセンスは cc-by-nc-sa になっております。
|
98 |
+
|
99 |
+
### SFTの概要
|
100 |
+
* 4bit量子化
|
101 |
+
* LoRAによるSFT
|
102 |
+
* learning_rate = 2e-4
|
103 |
+
* optim="adamw_torch_fused"
|
104 |
+
* lr_scheduler_type="cosine"
|
105 |
+
* weight_decay=0.01
|
106 |
+
|
107 |
+
## Bias, Risks, and Limitations
|
108 |
+
RLHF,DPOを実施していないため不適切な表現が出力される可能性があります。
|
109 |
+
|
110 |
+
# elyza-tasks-100-TV_0.jsonl の出力方法
|
111 |
+
|
112 |
+
elyza-tasks-100-TV_0.jsonl に記載されている指示に対する返答のサンプル出力コードは次のようになります。
|
113 |
+
|
114 |
+
```python
|
115 |
+
import torch
|
116 |
+
from transformers import (
|
117 |
+
AutoTokenizer,
|
118 |
+
AutoModelForCausalLM,
|
119 |
+
BitsAndBytesConfig,
|
120 |
+
)
|
121 |
+
from peft import LoraConfig, PeftModel
|
122 |
+
from datasets import load_dataset
|
123 |
+
|
124 |
+
|
125 |
+
BASE_MODEL = "llm-jp/llm-jp-3-13b"
|
126 |
+
PEFT_MODEL = "libkazz/llm-jp-3-13b-it"
|
127 |
+
|
128 |
+
tokenizer = AutoTokenizer.from_pretrained(PEFT_MODEL)
|
129 |
+
bnb_config = BitsAndBytesConfig(
|
130 |
+
load_in_4bit=True,
|
131 |
+
bnb_4bit_compute_dtype=torch.float16,
|
132 |
+
bnb_4bit_quant_type="nf4",
|
133 |
+
bnb_4bit_use_double_quant=False,
|
134 |
+
)
|
135 |
+
|
136 |
+
base_model = AutoModelForCausalLM.from_pretrained(
|
137 |
+
BASE_MODEL,
|
138 |
+
device_map="auto",
|
139 |
+
quantization_config=bnb_config,
|
140 |
+
torch_dtype="auto",
|
141 |
+
trust_remote_code=True,
|
142 |
+
)
|
143 |
+
|
144 |
+
model = PeftModel.from_pretrained(base_model, PEFT_MODEL)
|
145 |
+
|
146 |
+
# elyza-tasks-100-TV_0.jsonl データの読み込み
|
147 |
+
from datasets import load_dataset
|
148 |
+
|
149 |
+
dataset = load_dataset("json", data_files="./elyza-tasks-100-TV_0.jsonl", split="train")
|
150 |
+
|
151 |
+
results = []
|
152 |
+
for num in tqdm(range(100)):
|
153 |
+
instruction = dataset["input"][num]
|
154 |
+
|
155 |
+
prompt = f"次の指示に忠実に回答を作成しなさい。\n\n### 指示:\n{instruction}\n\n### 回答:\n"
|
156 |
+
|
157 |
+
model_input = tokenizer(prompt, return_tensors="pt").to(model.device)
|
158 |
+
input_ids = model_input["input_ids"]
|
159 |
+
|
160 |
+
with torch.no_grad():
|
161 |
+
outputs = model.generate(
|
162 |
+
input_ids,
|
163 |
+
max_new_tokens=300,
|
164 |
+
attention_mask = model_input.attention_mask,
|
165 |
+
pad_token_id=tokenizer.pad_token_id,
|
166 |
+
eos_token_id=tokenizer.eos_token_id,
|
167 |
+
do_sample=False,
|
168 |
+
repetition_penalty=1.02,
|
169 |
+
)[0]
|
170 |
+
output = tokenizer.decode(outputs[input_ids.size(1):], skip_special_tokens=True)
|
171 |
+
results.append({"task_id": num, "input": instruction, "output": output})
|
172 |
+
|
173 |
+
# ファイルに保存する
|
174 |
+
import json
|
175 |
+
with open("output.jsonl", "wt", encoding='utf-8') as f:
|
176 |
+
for result in results:
|
177 |
+
json.dump(result, f, ensure_ascii=False)
|
178 |
+
f.write('\n')
|
179 |
+
```
|
180 |
+
|