Update
Browse files
README.md
CHANGED
@@ -15,38 +15,28 @@ language:
|
|
15 |
LLMでの出力方法
|
16 |
|
17 |
```python
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
import torch
|
24 |
-
from tqdm import tqdm
|
25 |
import json
|
26 |
|
27 |
-
HF_TOKEN = "HF_TOKEN"
|
28 |
model_name = "tatsurou/llm-jp-3-13b-ft-20241123"
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
model = AutoModelForCausalLM.from_pretrained(
|
40 |
-
model_name,
|
41 |
-
quantization_config=bnb_config,
|
42 |
-
device_map="auto",
|
43 |
-
token = HF_TOKEN
|
44 |
)
|
|
|
45 |
|
46 |
-
# Load tokenizer
|
47 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True, token = HF_TOKEN)
|
48 |
-
|
49 |
-
# データセットの読み込み。
|
50 |
datasets = []
|
51 |
with open("./elyza-tasks-100-TV_0.jsonl", "r") as f:
|
52 |
item = ""
|
@@ -56,35 +46,27 @@ with open("./elyza-tasks-100-TV_0.jsonl", "r") as f:
|
|
56 |
if item.endswith("}"):
|
57 |
datasets.append(json.loads(item))
|
58 |
item = ""
|
|
|
|
|
|
|
|
|
59 |
results = []
|
60 |
-
for
|
|
|
61 |
|
62 |
-
|
63 |
|
64 |
-
prompt =
|
65 |
-
{input}
|
66 |
-
### 回答:
|
67 |
-
"""
|
68 |
|
69 |
-
|
70 |
-
|
71 |
-
outputs = model.generate(
|
72 |
-
tokenized_input,
|
73 |
-
max_new_tokens=300,
|
74 |
-
do_sample=False,
|
75 |
-
repetition_penalty=1.2
|
76 |
-
)[0]
|
77 |
-
output = tokenizer.decode(outputs[tokenized_input.size(1):], skip_special_tokens=True)
|
78 |
|
79 |
results.append({"task_id": data["task_id"], "input": input, "output": output})
|
80 |
|
81 |
-
|
82 |
-
model_name = re.sub(".*/", "", model_name)
|
83 |
-
with open(f"./{model_name}-my-original-outputs.jsonl", 'w', encoding='utf-8') as f:
|
84 |
for result in results:
|
85 |
-
json.dump(result, f, ensure_ascii=False)
|
86 |
f.write('\n')
|
87 |
-
|
88 |
```
|
89 |
|
90 |
|
|
|
15 |
LLMでの出力方法
|
16 |
|
17 |
```python
|
18 |
+
%%capture
|
19 |
+
!pip install unsloth
|
20 |
+
!pip uninstall unsloth -y && pip install --upgrade --no-cache-dir "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
|
21 |
+
|
22 |
+
from unsloth import FastLanguageModel
|
23 |
import torch
|
|
|
24 |
import json
|
25 |
|
|
|
26 |
model_name = "tatsurou/llm-jp-3-13b-ft-20241123"
|
27 |
+
max_seq_length = 2048
|
28 |
+
dtype = None
|
29 |
+
load_in_4bit = True
|
30 |
+
|
31 |
+
model, tokenizer = FastLanguageModel.from_pretrained(
|
32 |
+
model_name = model_name,
|
33 |
+
max_seq_length = max_seq_length,
|
34 |
+
dtype = dtype,
|
35 |
+
load_in_4bit = load_in_4bit,
|
36 |
+
token = "HF token",
|
|
|
|
|
|
|
|
|
|
|
37 |
)
|
38 |
+
FastLanguageModel.for_inference(model)
|
39 |
|
|
|
|
|
|
|
|
|
40 |
datasets = []
|
41 |
with open("./elyza-tasks-100-TV_0.jsonl", "r") as f:
|
42 |
item = ""
|
|
|
46 |
if item.endswith("}"):
|
47 |
datasets.append(json.loads(item))
|
48 |
item = ""
|
49 |
+
|
50 |
+
from tqdm import tqdm
|
51 |
+
|
52 |
+
# 推論
|
53 |
results = []
|
54 |
+
for dt in tqdm(data):
|
55 |
+
input = dt["input"]
|
56 |
|
57 |
+
prompt = f"""### 指示\n{input}\n### 回答\n"""
|
58 |
|
59 |
+
inputs = tokenizer([prompt], return_tensors = "pt").to(model.device)
|
|
|
|
|
|
|
60 |
|
61 |
+
outputs = model.generate(**inputs, max_new_tokens = 512, use_cache = True, do_sample=False, repetition_penalty=1.2)
|
62 |
+
prediction = tokenizer.decode(outputs[0], skip_special_tokens=True).split('\n### 回答')[-1]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
|
64 |
results.append({"task_id": data["task_id"], "input": input, "output": output})
|
65 |
|
66 |
+
with open(f"/content/{model_name}_output.jsonl", 'w', encoding='utf-8') as f:
|
|
|
|
|
67 |
for result in results:
|
68 |
+
json.dump(result, f, ensure_ascii=False)
|
69 |
f.write('\n')
|
|
|
70 |
```
|
71 |
|
72 |
|