takeofuture
commited on
Update README.md
Browse files
README.md
CHANGED
@@ -72,6 +72,65 @@
|
|
72 |
prediction = tokenizer.decode(outputs[0], skip_special_tokens=True).split('\n### 回答')[-1]
|
73 |
print(prediction)
|
74 |
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
|
76 |
## **GOOGLE COLABORATORYでのelyza_100_tvでの推論方法**
|
77 |
以下のノートを参照してください
|
|
|
72 |
prediction = tokenizer.decode(outputs[0], skip_special_tokens=True).split('\n### 回答')[-1]
|
73 |
print(prediction)
|
74 |
```
|
75 |
+
- **ベンチマークの実施**
|
76 |
+
```
|
77 |
+
from unsloth import FastLanguageModel
|
78 |
+
from peft import PeftModel
|
79 |
+
import torch
|
80 |
+
import json
|
81 |
+
from tqdm import tqdm
|
82 |
+
import re
|
83 |
+
#ベースとなるモデルと学習したLoRAのアダプタ(Hugging FaceのIDを指定)。
|
84 |
+
#model_id = "llm-jp/llm-jp-3-13b" #HUGGINGFACEをよりダウンロードするときはこちらを使いください
|
85 |
+
local_model_dir = "./models/llm-jp/llm-jp-3-13b" # 事前にダウンロードしたモデルのローカルディレクトリ
|
86 |
+
adapter_id = "takeofuture/llm-jp-3-13b-finetune-22_lora"
|
87 |
+
HF_TOKEN = "HUGGINGFACEのTOKENを入れてください"
|
88 |
+
#unslothのFastLanguageModelで元のモデルをロード。
|
89 |
+
dtype = None # Noneにしておけば自動で設定
|
90 |
+
load_in_4bit = True # 今回は13Bモデルを扱うためTrue
|
91 |
+
model, tokenizer = FastLanguageModel.from_pretrained(
|
92 |
+
#model_name=model_id,
|
93 |
+
model_name=local_model_dir,
|
94 |
+
dtype=dtype,
|
95 |
+
load_in_4bit=load_in_4bit,
|
96 |
+
trust_remote_code=True,
|
97 |
+
)
|
98 |
+
model = PeftModel.from_pretrained(model, adapter_id, token = HF_TOKEN)
|
99 |
+
#タスクとなるデータの読み込み。
|
100 |
+
datasets = []
|
101 |
+
with open("./elyza-tasks-100-TV_0.jsonl", "r") as f:
|
102 |
+
item = ""
|
103 |
+
for line in f:
|
104 |
+
line = line.strip()
|
105 |
+
item += line
|
106 |
+
if item.endswith("}"):
|
107 |
+
datasets.append(json.loads(item))
|
108 |
+
item = ""
|
109 |
+
#推論モードに切り替え
|
110 |
+
FastLanguageModel.for_inference(model)
|
111 |
+
results = []
|
112 |
+
for dt in tqdm(datasets):
|
113 |
+
input = dt["input"]
|
114 |
+
print("\n\n=====================================================================================================================\n")
|
115 |
+
print("---指示---")
|
116 |
+
print(input)
|
117 |
+
prompt = f"""### 指示\n{input}\n### 回答\n"""
|
118 |
+
inputs = tokenizer([prompt], return_tensors = "pt").to(model.device)
|
119 |
+
if "token_type_ids" in inputs:
|
120 |
+
del inputs["token_type_ids"]
|
121 |
+
outputs = model.generate(**inputs, max_new_tokens = 512, use_cache = True, do_sample=False, repetition_penalty=1.2)
|
122 |
+
prediction = tokenizer.decode(outputs[0], skip_special_tokens=True).split('\n### 回答')[-1]
|
123 |
+
print("---回答---")
|
124 |
+
print(prediction)
|
125 |
+
results.append({"task_id": dt["task_id"], "input": input, "output": prediction})
|
126 |
+
|
127 |
+
#結果をjsonlで保存。
|
128 |
+
json_file_id = re.sub(".*/", "", adapter_id)
|
129 |
+
with open(f"./{json_file_id}_output.jsonl", 'w', encoding='utf-8') as f:
|
130 |
+
for result in results:
|
131 |
+
json.dump(result, f, ensure_ascii=False)
|
132 |
+
f.write('\n')
|
133 |
+
```
|
134 |
|
135 |
## **GOOGLE COLABORATORYでのelyza_100_tvでの推論方法**
|
136 |
以下のノートを参照してください
|