Update README.md
Browse files
README.md
CHANGED
@@ -23,7 +23,72 @@ This llama model was trained 2x faster with [Unsloth](https://github.com/unsloth
|
|
23 |
|
24 |
# How to conduct inference
|
25 |
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
|
29 |
|
|
|
23 |
|
24 |
# How to conduct inference
|
25 |
|
26 |
+
```
|
27 |
+
from unsloth import FastLanguageModel
|
28 |
+
from peft import PeftModel
|
29 |
+
import torch
|
30 |
+
import json
|
31 |
+
from tqdm import tqdm
|
32 |
+
import re
|
33 |
+
|
34 |
+
# Base model id and LoRA adapter ID
|
35 |
+
base_model_id = "llm-jp/llm-jp-3-13b"
|
36 |
+
adapter_id = "Rumi/llm-jp_SFT_rn_2024-12-14_06"
|
37 |
+
|
38 |
+
# Log in with your Hugging Face token
|
39 |
+
HF_TOKEN = "hogehoge"
|
40 |
+
from huggingface_hub import login
|
41 |
+
login(HF_TOKEN)
|
42 |
+
|
43 |
+
# Download the original model
|
44 |
+
dtype = None
|
45 |
+
load_in_4bit = True
|
46 |
+
|
47 |
+
base_model, tokenizer = FastLanguageModel.from_pretrained(
|
48 |
+
model_name=base_model_id,
|
49 |
+
dtype=dtype,
|
50 |
+
load_in_4bit=load_in_4bit,
|
51 |
+
trust_remote_code=True,
|
52 |
+
)
|
53 |
+
|
54 |
+
# Merge adapter to the base model
|
55 |
+
model = PeftModel.from_pretrained(base_model, adapter_id, token = HF_TOKEN)
|
56 |
+
|
57 |
+
# Read evaluation dataset
|
58 |
+
datasets = []
|
59 |
+
with open("./elyza-tasks-100-TV_0.jsonl", "r") as f:
|
60 |
+
item = ""
|
61 |
+
for line in f:
|
62 |
+
line = line.strip()
|
63 |
+
item += line
|
64 |
+
if item.endswith("}"):
|
65 |
+
datasets.append(json.loads(item))
|
66 |
+
item = ""
|
67 |
+
|
68 |
+
# Change the format and conduct the evaluation
|
69 |
+
FastLanguageModel.for_inference(model)
|
70 |
+
|
71 |
+
results = []
|
72 |
+
for dt in tqdm(datasets):
|
73 |
+
input = dt["input"]
|
74 |
+
|
75 |
+
prompt = f"""### 指示\n{input}\n### 回答\n"""
|
76 |
+
|
77 |
+
inputs = tokenizer([prompt], return_tensors = "pt").to(model.device)
|
78 |
+
|
79 |
+
outputs = model.generate(**inputs, max_new_tokens = 512, use_cache = True, do_sample=False, repetition_penalty=1.2)
|
80 |
+
prediction = tokenizer.decode(outputs[0], skip_special_tokens=True).split('\n### 回答')[-1]
|
81 |
+
|
82 |
+
results.append({"task_id": dt["task_id"], "input": input, "output": prediction})
|
83 |
+
|
84 |
+
# Save result in the jsonl format
|
85 |
+
json_file_id = re.sub(".*/", "", adapter_id)
|
86 |
+
with open(f"/content/{json_file_id}_output.jsonl", 'w', encoding='utf-8') as f:
|
87 |
+
for result in results:
|
88 |
+
json.dump(result, f, ensure_ascii=False)
|
89 |
+
f.write('\n')
|
90 |
+
|
91 |
+
```
|
92 |
|
93 |
|
94 |
|