最終課題jsonlファイルの出力方法を記す
Browse files
README.md
CHANGED
@@ -14,20 +14,73 @@ licence: license
|
|
14 |
This model is a fine-tuned version of [google/gemma-2-2b](https://huggingface.co/google/gemma-2-2b).
|
15 |
It has been trained using [TRL](https://github.com/huggingface/trl).
|
16 |
|
17 |
-
|
|
|
|
|
18 |
|
19 |
```python
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
-
|
23 |
-
generator = pipeline("text-generation", model="ftnext/gemma-2-2b-elyza-tasks-sft", device="cuda")
|
24 |
-
output = generator([{"role": "user", "content": question}], max_new_tokens=128, return_full_text=False)[0]
|
25 |
-
print(output["generated_text"])
|
26 |
```
|
27 |
|
28 |
## Training procedure
|
29 |
|
30 |
-
|
31 |
|
32 |
|
33 |
This model was trained with SFT.
|
|
|
14 |
This model is a fine-tuned version of [google/gemma-2-2b](https://huggingface.co/google/gemma-2-2b).
|
15 |
It has been trained using [TRL](https://github.com/huggingface/trl).
|
16 |
|
17 |
+
松尾研LLM講座2024 最終課題で作ったモデル
|
18 |
+
|
19 |
+
## How to inference
|
20 |
|
21 |
```python
|
22 |
+
# /// script
|
23 |
+
# requires-python = "3.10"
|
24 |
+
# dependencies = [
|
25 |
+
# "transformers[torch]",
|
26 |
+
# "datasets",
|
27 |
+
# "peft",
|
28 |
+
# "bitsandbytes<0.44",
|
29 |
+
# ]
|
30 |
+
# ///
|
31 |
+
|
32 |
+
# import os
|
33 |
+
# from google.colab import userdata
|
34 |
+
# os.environ["HF_TOKEN"] = userdata.get("HF_TOKEN")
|
35 |
+
|
36 |
+
import torch
|
37 |
+
from datasets import load_dataset
|
38 |
+
from peft import AutoPeftModelForCausalLM
|
39 |
+
from transformers import AutoTokenizer, BitsAndBytesConfig
|
40 |
+
|
41 |
+
bnb_config = BitsAndBytesConfig(
|
42 |
+
load_in_4bit=True,
|
43 |
+
bnb_4bit_quant_type="nf4",
|
44 |
+
bnb_4bit_compute_dtype=torch.bfloat16,
|
45 |
+
)
|
46 |
+
|
47 |
+
model_id = "ftnext/gemma-2-2b-elyza-tasks-sft"
|
48 |
+
|
49 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
50 |
+
tokenizer.pad_token = tokenizer.eos_token
|
51 |
+
|
52 |
+
peft_model = AutoPeftModelForCausalLM.from_pretrained(
|
53 |
+
model_id,
|
54 |
+
quantization_config=bnb_config,
|
55 |
+
device_map={"": 0},
|
56 |
+
)
|
57 |
+
|
58 |
+
dataset = load_dataset("json", data_files="./elyza-tasks-100-TV_0.jsonl", split="train")
|
59 |
+
|
60 |
+
response_format = "### 応答:\n"
|
61 |
+
|
62 |
+
|
63 |
+
def format_prompt(input):
|
64 |
+
return f"以下は、タスクを説明する指示です。要求を適切に満たす応答を書きなさい。\n\n### 指示:\n{input}\n\n{response_format}"
|
65 |
+
|
66 |
+
|
67 |
+
@torch.no_grad
|
68 |
+
def infer(example):
|
69 |
+
prompt = format_prompt(example["input"])
|
70 |
+
inputs = tokenizer(prompt, return_tensors="pt").to("cuda:0")
|
71 |
+
model_output = peft_model.generate(**inputs, max_new_tokens=150)
|
72 |
+
output = tokenizer.decode(model_output[0], skip_special_tokens=True)
|
73 |
+
return {**example, "output": output[len(prompt) :]}
|
74 |
+
|
75 |
+
|
76 |
+
inferred_ds = dataset.map(infer)
|
77 |
|
78 |
+
inferred_ds.to_json("submission.jsonl", force_ascii=False)
|
|
|
|
|
|
|
79 |
```
|
80 |
|
81 |
## Training procedure
|
82 |
|
83 |
+
See https://github.com/ftnext/practice-dl-nlp/blob/552dda69387b53f825bd3b560f4d2e6252cc43b0/llmjp/fine_tuning/gemma_2_2b_elyza_tasks_sft.ipynb
|
84 |
|
85 |
|
86 |
This model was trained with SFT.
|