Update README.md
Browse files
README.md
CHANGED
@@ -196,4 +196,109 @@ Carbon emissions can be estimated using the [Machine Learning Impact calculator]
|
|
196 |
|
197 |
## Model Card Contact
|
198 |
|
199 |
-
[More Information Needed]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
196 |
|
197 |
## Model Card Contact
|
198 |
|
199 |
+
[More Information Needed]
|
200 |
+
|
201 |
+
|
202 |
+
Sample Use
|
203 |
+
|
204 |
+
推論用コード
|
205 |
+
Hugging Faceにアップロードしたモデルを用いてELYZA-tasks-100-TVの出力を得るためのコードです。 こちらはLoRA_template このコードで生成されたjsonlファイルは課題の成果として提出可能なフォーマットになっております。
|
206 |
+
|
207 |
+
from transformers import (
|
208 |
+
AutoModelForCausalLM,
|
209 |
+
AutoTokenizer,
|
210 |
+
BitsAndBytesConfig,
|
211 |
+
)
|
212 |
+
from peft import PeftModel
|
213 |
+
import torch
|
214 |
+
from tqdm import tqdm
|
215 |
+
import json
|
216 |
+
|
217 |
+
# Hugging Faceで取得したTokenをこちらに貼る。
|
218 |
+
HF_TOKEN = "Hugging Face Token"
|
219 |
+
|
220 |
+
# QLoRA config
|
221 |
+
bnb_config = BitsAndBytesConfig(
|
222 |
+
load_in_4bit=True,
|
223 |
+
bnb_4bit_quant_type="nf4",
|
224 |
+
bnb_4bit_compute_dtype=torch.bfloat16,
|
225 |
+
)
|
226 |
+
|
227 |
+
# Load model
|
228 |
+
model = AutoModelForCausalLM.from_pretrained(
|
229 |
+
model_id,
|
230 |
+
quantization_config=bnb_config,
|
231 |
+
device_map="auto",
|
232 |
+
token = HF_TOKEN
|
233 |
+
)
|
234 |
+
|
235 |
+
# Load tokenizer
|
236 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True, token = HF_TOKEN)
|
237 |
+
|
238 |
+
# 元のモデルにLoRAのアダプタを統合。
|
239 |
+
model = PeftModel.from_pretrained(model, adapter_id, token = HF_TOKEN)
|
240 |
+
|
241 |
+
# データセットの読み込み。
|
242 |
+
# omnicampusの開発環境では、左にタスクのjsonlをドラッグアンドドロップしてから実行。
|
243 |
+
datasets = []
|
244 |
+
with open("./elyza-tasks-100-TV_0.jsonl", "r") as f:
|
245 |
+
item = ""
|
246 |
+
for line in f:
|
247 |
+
line = line.strip()
|
248 |
+
item += line
|
249 |
+
if item.endswith("}"):
|
250 |
+
datasets.append(json.loads(item))
|
251 |
+
item = ""
|
252 |
+
|
253 |
+
# gemma
|
254 |
+
results = []
|
255 |
+
for data in tqdm(datasets):
|
256 |
+
|
257 |
+
input = data["input"]
|
258 |
+
prompt = f"""### 指示
|
259 |
+
{input}
|
260 |
+
### 回答
|
261 |
+
"""
|
262 |
+
|
263 |
+
input_ids = tokenizer(prompt, return_tensors="pt").to(model.device)
|
264 |
+
outputs = model.generate(**input_ids, max_new_tokens=512, do_sample=False, repetition_penalty=1.2,)
|
265 |
+
output = tokenizer.decode(outputs[0][input_ids.input_ids.size(1):], skip_special_tokens=True)
|
266 |
+
|
267 |
+
results.append({"task_id": data["task_id"], "input": input, "output": output})
|
268 |
+
|
269 |
+
|
270 |
+
# llmjp
|
271 |
+
results = []
|
272 |
+
for data in tqdm(datasets):
|
273 |
+
|
274 |
+
input = data["input"]
|
275 |
+
|
276 |
+
prompt = f"""### 指示
|
277 |
+
{input}
|
278 |
+
### 回答
|
279 |
+
"""
|
280 |
+
|
281 |
+
tokenized_input = tokenizer.encode(prompt, add_special_tokens=False, return_tensors="pt").to(model.device)
|
282 |
+
attention_mask = torch.ones_like(tokenized_input)
|
283 |
+
with torch.no_grad():
|
284 |
+
outputs = model.generate(
|
285 |
+
tokenized_input,
|
286 |
+
attention_mask=attention_mask,
|
287 |
+
max_new_tokens=100,
|
288 |
+
do_sample=False,
|
289 |
+
repetition_penalty=1.2,
|
290 |
+
pad_token_id=tokenizer.eos_token_id
|
291 |
+
)[0]
|
292 |
+
output = tokenizer.decode(outputs[tokenized_input.size(1):], skip_special_tokens=True)
|
293 |
+
|
294 |
+
results.append({"task_id": data["task_id"], "input": input, "output": output})
|
295 |
+
|
296 |
+
# こちらで生成されたjsolを提出してください。
|
297 |
+
# 本コードではinputとeval_aspectも含んでいますが、なくても問題ありません。
|
298 |
+
# 必須なのはtask_idとoutputとなります。
|
299 |
+
import re
|
300 |
+
jsonl_id = re.sub(".*/", "", adapter_id)
|
301 |
+
with open(f"./{jsonl_id}-outputs.jsonl", 'w', encoding='utf-8') as f:
|
302 |
+
for result in results:
|
303 |
+
json.dump(result, f, ensure_ascii=False) # ensure_ascii=False for handling non-ASCII characters
|
304 |
+
f.write('\n')
|