sasakipeter
commited on
Commit
•
6bd9ea8
1
Parent(s):
2118cda
update inference example
Browse files
README.md
CHANGED
@@ -54,11 +54,15 @@ from transformers import (
|
|
54 |
)
|
55 |
from peft import PeftModel
|
56 |
import torch
|
|
|
|
|
|
|
57 |
|
58 |
# Hugging Face Token (recommended to set via environment variable)
|
59 |
HF_TOKEN = "YOUR_HF_ACCESS_TOKEN"
|
60 |
|
61 |
# Model and adapter IDs
|
|
|
62 |
base_model_id = "llm-jp/llm-jp-3-13b" # Base model
|
63 |
adapter_id = "sasakipeter/llm-jp-3-13b-finetune"
|
64 |
|
@@ -90,39 +94,55 @@ tokenizer = AutoTokenizer.from_pretrained(
|
|
90 |
|
91 |
# Integrate LoRA adapter into the base model
|
92 |
model = PeftModel.from_pretrained(model, adapter_id, token=HF_TOKEN)
|
|
|
93 |
```
|
94 |
|
95 |
-
### 4. Perform Inference
|
96 |
|
97 |
```python
|
98 |
-
#
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
""
|
108 |
-
|
109 |
-
#
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
)
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
126 |
```
|
127 |
|
128 |
---
|
|
|
54 |
)
|
55 |
from peft import PeftModel
|
56 |
import torch
|
57 |
+
from tqdm import tqdm
|
58 |
+
import json
|
59 |
+
import re
|
60 |
|
61 |
# Hugging Face Token (recommended to set via environment variable)
|
62 |
HF_TOKEN = "YOUR_HF_ACCESS_TOKEN"
|
63 |
|
64 |
# Model and adapter IDs
|
65 |
+
# base_model_id = "models/models--llm-jp--llm-jp-3-13b/snapshots/cd3823f4c1fcbb0ad2e2af46036ab1b0ca13192a"
|
66 |
base_model_id = "llm-jp/llm-jp-3-13b" # Base model
|
67 |
adapter_id = "sasakipeter/llm-jp-3-13b-finetune"
|
68 |
|
|
|
94 |
|
95 |
# Integrate LoRA adapter into the base model
|
96 |
model = PeftModel.from_pretrained(model, adapter_id, token=HF_TOKEN)
|
97 |
+
model.config.use_cache = False
|
98 |
```
|
99 |
|
100 |
+
### 4. Perform Inference on `[elyza-tasks-100](https://huggingface.co/datasets/elyza/ELYZA-tasks-100)`
|
101 |
|
102 |
```python
|
103 |
+
# loading dataset
|
104 |
+
datasets = []
|
105 |
+
with open("./elyza-tasks-100-TV_0.jsonl", "r") as f:
|
106 |
+
item = ""
|
107 |
+
for line in f:
|
108 |
+
line = line.strip()
|
109 |
+
item += line
|
110 |
+
if item.endswith("}"):
|
111 |
+
datasets.append(json.loads(item))
|
112 |
+
item = ""
|
113 |
+
|
114 |
+
# execute inference
|
115 |
+
results = []
|
116 |
+
for data in tqdm(datasets):
|
117 |
+
|
118 |
+
input_text = data["input"]
|
119 |
+
|
120 |
+
prompt = f"""### 指示
|
121 |
+
{input_text}
|
122 |
+
### 回答
|
123 |
+
"""
|
124 |
+
|
125 |
+
tokenized_input = tokenizer.encode(prompt, add_special_tokens=False, return_tensors="pt").to(model.device)
|
126 |
+
attention_mask = torch.ones_like(tokenized_input)
|
127 |
+
|
128 |
+
with torch.no_grad():
|
129 |
+
outputs = model.generate(
|
130 |
+
tokenized_input,
|
131 |
+
attention_mask=attention_mask,
|
132 |
+
max_new_tokens=100,
|
133 |
+
do_sample=False,
|
134 |
+
repetition_penalty=1.2,
|
135 |
+
pad_token_id=tokenizer.eos_token_id
|
136 |
+
)[0]
|
137 |
+
output = tokenizer.decode(outputs[tokenized_input.size(1):], skip_special_tokens=True)
|
138 |
+
|
139 |
+
results.append({"task_id": data["task_id"], "input": input, "output": output})
|
140 |
+
|
141 |
+
jsonl_id = re.sub(".*/", "", new_model_id)
|
142 |
+
with open(f"./{jsonl_id}-outputs-validation.jsonl", 'w', encoding='utf-8') as f:
|
143 |
+
for result in results:
|
144 |
+
json.dump(result, f, ensure_ascii=False) # ensure_ascii=False for handling non-ASCII characters
|
145 |
+
f.write('\n')
|
146 |
```
|
147 |
|
148 |
---
|