Update
Browse files
README.md
CHANGED
@@ -20,3 +20,141 @@ 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 |
+
#Sample Use
|
25 |
+
!pip install -U bitsandbytes
|
26 |
+
!pip install -U transformers
|
27 |
+
!pip install -U accelerate
|
28 |
+
!pip install -U datasets
|
29 |
+
|
30 |
+
!pip install ipywidgets --upgrade
|
31 |
+
|
32 |
+
from transformers import (
|
33 |
+
AutoModelForCausalLM,
|
34 |
+
AutoTokenizer,
|
35 |
+
BitsAndBytesConfig,
|
36 |
+
)
|
37 |
+
import torch
|
38 |
+
from tqdm import tqdm
|
39 |
+
import json
|
40 |
+
|
41 |
+
# Hugging Faceで取得したTokenをこちらに貼る。
|
42 |
+
HF_TOKEN = "your-token"
|
43 |
+
|
44 |
+
# 自分の作成したモデルのIDをこちらに貼る。
|
45 |
+
model_name = "yusuke0505/llm-jp-3-13b-it"
|
46 |
+
|
47 |
+
# QLoRA config
|
48 |
+
bnb_config = BitsAndBytesConfig(
|
49 |
+
load_in_4bit=True,
|
50 |
+
bnb_4bit_quant_type="nf4",
|
51 |
+
bnb_4bit_compute_dtype=torch.bfloat16,
|
52 |
+
bnb_4bit_use_double_quant=False,
|
53 |
+
)
|
54 |
+
|
55 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
56 |
+
|
57 |
+
# モデル名を指定
|
58 |
+
model_name = "yusuke0505/llm-jp-3-13b-it"
|
59 |
+
|
60 |
+
# 量子化設定(例として4ビット量子化)
|
61 |
+
bnb_config = {
|
62 |
+
"load_in_4bit": True # 必要に応じて変更
|
63 |
+
}
|
64 |
+
|
65 |
+
# モデルをロード
|
66 |
+
model = AutoModelForCausalLM.from_pretrained(
|
67 |
+
model_name,
|
68 |
+
quantization_config=bnb_config,
|
69 |
+
device_map="auto",
|
70 |
+
use_auth_token="your-token" # Hugging Faceトークンを指定(必要な場合)
|
71 |
+
)
|
72 |
+
|
73 |
+
# トークナイザをロード
|
74 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
75 |
+
model_name,
|
76 |
+
use_auth_token="your-token"
|
77 |
+
)
|
78 |
+
|
79 |
+
import json # 必要なモジュールをインポート
|
80 |
+
|
81 |
+
# データセットの読み込み。
|
82 |
+
# omnicampusの開発環境では、左にタスクのjsonlをドラッグアンドドロップしてから実行。
|
83 |
+
datasets = []
|
84 |
+
with open("./elyza-tasks-100-TV_0.jsonl", "r") as f:
|
85 |
+
item = ""
|
86 |
+
for line in f:
|
87 |
+
line = line.strip()
|
88 |
+
item += line
|
89 |
+
if item.endswith("}"):
|
90 |
+
datasets.append(json.loads(item))
|
91 |
+
item = ""
|
92 |
+
|
93 |
+
from tqdm import tqdm
|
94 |
+
|
95 |
+
# gemma
|
96 |
+
results = []
|
97 |
+
for data in tqdm(datasets):
|
98 |
+
|
99 |
+
input_text = data["input"]
|
100 |
+
prompt = f"""### 指示
|
101 |
+
{input_text}
|
102 |
+
### 回答:
|
103 |
+
"""
|
104 |
+
|
105 |
+
# トークナイズ処理
|
106 |
+
input_ids = tokenizer(prompt, return_tensors="pt").to(model.device)
|
107 |
+
input_ids.pop("token_type_ids", None) # GPT系モデルでは不要な "token_type_ids" を削除
|
108 |
+
|
109 |
+
# モデルでテキスト生成
|
110 |
+
outputs = model.generate(
|
111 |
+
**input_ids,
|
112 |
+
max_new_tokens=512,
|
113 |
+
do_sample=False,
|
114 |
+
repetition_penalty=1.2,
|
115 |
+
)
|
116 |
+
|
117 |
+
# 出力をデコード
|
118 |
+
output = tokenizer.decode(
|
119 |
+
outputs[0][input_ids["input_ids"].size(1):], skip_special_tokens=True
|
120 |
+
)
|
121 |
+
|
122 |
+
# 結果をリストに追加
|
123 |
+
results.append({"task_id": data["task_id"], "input": input_text, "output": output})
|
124 |
+
|
125 |
+
# 結果を確認(オプション)
|
126 |
+
print(results)
|
127 |
+
|
128 |
+
import torch # torchをインポート
|
129 |
+
from tqdm import tqdm # tqdmをインポート(プログレスバー用)
|
130 |
+
|
131 |
+
# llmjp
|
132 |
+
results = []
|
133 |
+
for data in tqdm(datasets):
|
134 |
+
|
135 |
+
input = data["input"]
|
136 |
+
|
137 |
+
prompt = f"""### 指示
|
138 |
+
{input}
|
139 |
+
### 回答:
|
140 |
+
"""
|
141 |
+
|
142 |
+
tokenized_input = tokenizer.encode(prompt, add_special_tokens=False, return_tensors="pt").to(model.device)
|
143 |
+
with torch.no_grad():
|
144 |
+
outputs = model.generate(
|
145 |
+
tokenized_input,
|
146 |
+
max_new_tokens=100,
|
147 |
+
do_sample=False,
|
148 |
+
repetition_penalty=1.2
|
149 |
+
)[0]
|
150 |
+
output = tokenizer.decode(outputs[tokenized_input.size(1):], skip_special_tokens=True)
|
151 |
+
|
152 |
+
results.append({"task_id": data["task_id"], "input": input, "output": output})
|
153 |
+
|
154 |
+
# jsolの生成
|
155 |
+
import re
|
156 |
+
model_name = re.sub(".*/", "", model_name)
|
157 |
+
with open(f"./{model_name}-outputs.jsonl", 'w', encoding='utf-8') as f:
|
158 |
+
for result in results:
|
159 |
+
json.dump(result, f, ensure_ascii=False) # ensure_ascii=False for handling non-ASCII characters
|
160 |
+
f.write('\n')
|