Update README.md
Browse files
README.md
CHANGED
@@ -24,6 +24,72 @@ This llama model was trained 2x faster with [Unsloth](https://github.com/unsloth
|
|
24 |
|
25 |
USE MODEL
|
26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
|
29 |
|
|
|
24 |
|
25 |
USE MODEL
|
26 |
|
27 |
+
# 推論用コード
|
28 |
+
Hugging Faceにアップロードしたモデルを用いてELYZA-tasks-100-TVの出力を得るためのコードです。
|
29 |
+
このコードはunslothライブラリを用いてモデルを読み込み、推論するためのコードとなります。
|
30 |
+
このコードで生成されたjsonlファイルは課題の成果として提出可能なフォーマットになっております。
|
31 |
+
"""
|
32 |
+
|
33 |
+
# Commented out IPython magic to ensure Python compatibility.
|
34 |
+
# %%capture
|
35 |
+
# !pip install unsloth
|
36 |
+
# !pip uninstall unsloth -y && pip install --upgrade --no-cache-dir "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
|
37 |
+
|
38 |
+
from unsloth import FastLanguageModel
|
39 |
+
import torch
|
40 |
+
import json
|
41 |
+
|
42 |
+
model_name = "zhulei777/llm-jp-3-13b-finetune-zhu6"
|
43 |
+
|
44 |
+
max_seq_length = 2048
|
45 |
+
dtype = None
|
46 |
+
load_in_4bit = True
|
47 |
+
|
48 |
+
model, tokenizer = FastLanguageModel.from_pretrained(
|
49 |
+
model_name = model_name,
|
50 |
+
max_seq_length = max_seq_length,
|
51 |
+
dtype = dtype,
|
52 |
+
load_in_4bit = load_in_4bit,
|
53 |
+
token = "your token",
|
54 |
+
)
|
55 |
+
FastLanguageModel.for_inference(model)
|
56 |
+
|
57 |
+
# データセットの読み込み。
|
58 |
+
# omnicampusの開発環境では、左にタスクのjsonlをドラッグアンドドロップしてから実行。
|
59 |
+
datasets = []
|
60 |
+
with open("./elyza-tasks-100-TV_0.jsonl", "r") as f:
|
61 |
+
item = ""
|
62 |
+
for line in f:
|
63 |
+
line = line.strip()
|
64 |
+
item += line
|
65 |
+
if item.endswith("}"):
|
66 |
+
try:
|
67 |
+
datasets.append(json.loads(item))
|
68 |
+
item = ""
|
69 |
+
except json.JSONDecodeError as e:
|
70 |
+
print(f"Error decoding JSON on line: {line}")
|
71 |
+
print(f"Error message: {e}")
|
72 |
+
|
73 |
+
from tqdm import tqdm
|
74 |
+
|
75 |
+
# 推論
|
76 |
+
results = []
|
77 |
+
for dt in tqdm(datasets):
|
78 |
+
input = dt["input"]
|
79 |
+
|
80 |
+
prompt = f"""### 指示\n{input}\n### 回答\n"""
|
81 |
+
|
82 |
+
inputs = tokenizer([prompt], return_tensors = "pt").to(model.device)
|
83 |
+
|
84 |
+
outputs = model.generate(**inputs, max_new_tokens = 512, use_cache = True, do_sample=False, repetition_penalty=1.2)
|
85 |
+
prediction = tokenizer.decode(outputs[0], skip_special_tokens=True).split('\n### 回答')[-1]
|
86 |
+
results.append({"task_id": dt["task_id"], "input": input, "output": prediction})
|
87 |
+
|
88 |
+
with open(f"./llm-jp-3-13b-finetune-zhu6_output.jsonl", 'w', encoding='utf-8') as f:
|
89 |
+
for result in results:
|
90 |
+
json.dump(result, f, ensure_ascii=False)
|
91 |
+
f.write('\n')
|
92 |
+
|
93 |
|
94 |
|
95 |
|