takeofuture
commited on
Commit
•
8f09d06
1
Parent(s):
596893e
Upload Model_Inference_Template_unsloth_20241127.ipynb
Browse files
Model_Inference_Template_unsloth_20241127.ipynb
ADDED
@@ -0,0 +1,188 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "markdown",
|
5 |
+
"metadata": {
|
6 |
+
"id": "MljifiTVCT0_"
|
7 |
+
},
|
8 |
+
"source": [
|
9 |
+
"# 推論用コード\n",
|
10 |
+
"本コードはunslothで学習したqLoRAのアダプタを用いてELYZA-tasks-100-TVの出力を得るためのコードです。 \n",
|
11 |
+
"Hugging Faceにアダプタをアップロードしてあることが前提となります。\n",
|
12 |
+
"このコードはunslothライブラリを用いてモデルを読み込み、推論するためのコードとなります。\n",
|
13 |
+
"このコードで生成されたjsonlファイルは課題の成果として提出可能なフォーマットになっております。\n",
|
14 |
+
"\n",
|
15 |
+
"※本コードはGoogle Colabでの動作を想定しており、Omnicampusでの動作を想定しておりません。\n",
|
16 |
+
"Omnicampus向けのコードは別途用意しております。"
|
17 |
+
]
|
18 |
+
},
|
19 |
+
{
|
20 |
+
"cell_type": "code",
|
21 |
+
"execution_count": null,
|
22 |
+
"metadata": {
|
23 |
+
"id": "I5B5MOHuBy8b"
|
24 |
+
},
|
25 |
+
"outputs": [],
|
26 |
+
"source": [
|
27 |
+
"# 必要なライブラリをインストール\n",
|
28 |
+
"%%capture\n",
|
29 |
+
"!pip install unsloth\n",
|
30 |
+
"!pip uninstall unsloth -y && pip install --upgrade --no-cache-dir \"unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git\"\n",
|
31 |
+
"!pip install -U torch\n",
|
32 |
+
"!pip install -U peft"
|
33 |
+
]
|
34 |
+
},
|
35 |
+
{
|
36 |
+
"cell_type": "code",
|
37 |
+
"execution_count": null,
|
38 |
+
"metadata": {
|
39 |
+
"id": "GM7SNRtACg9V"
|
40 |
+
},
|
41 |
+
"outputs": [],
|
42 |
+
"source": [
|
43 |
+
"# 必要なライブラリを読み込み\n",
|
44 |
+
"from unsloth import FastLanguageModel\n",
|
45 |
+
"from peft import PeftModel\n",
|
46 |
+
"import torch\n",
|
47 |
+
"import json\n",
|
48 |
+
"from tqdm import tqdm\n",
|
49 |
+
"import re"
|
50 |
+
]
|
51 |
+
},
|
52 |
+
{
|
53 |
+
"cell_type": "code",
|
54 |
+
"execution_count": null,
|
55 |
+
"metadata": {
|
56 |
+
"id": "JmdUATTVCtyr"
|
57 |
+
},
|
58 |
+
"outputs": [],
|
59 |
+
"source": [
|
60 |
+
"# ベースとなるモデルと学習したLoRAのアダプタ(Hugging FaceのIDを指定)。\n",
|
61 |
+
"model_id = \"llm-jp/llm-jp-3-13b\"\n",
|
62 |
+
"adapter_id = \"\""
|
63 |
+
]
|
64 |
+
},
|
65 |
+
{
|
66 |
+
"cell_type": "code",
|
67 |
+
"execution_count": null,
|
68 |
+
"metadata": {},
|
69 |
+
"outputs": [],
|
70 |
+
"source": [
|
71 |
+
"# Hugging Face Token を指定。\n",
|
72 |
+
"# 下記の URL から Hugging Face Token を取得できますので下記の HF_TOKEN に入れてください。\n",
|
73 |
+
"# https://huggingface.co/settings/tokens \n",
|
74 |
+
"HF_TOKEN = \"\" #@param {type:\"string\"}"
|
75 |
+
]
|
76 |
+
},
|
77 |
+
{
|
78 |
+
"cell_type": "code",
|
79 |
+
"execution_count": null,
|
80 |
+
"metadata": {
|
81 |
+
"id": "TB6Hzx-2B5g8"
|
82 |
+
},
|
83 |
+
"outputs": [],
|
84 |
+
"source": [
|
85 |
+
"# unslothのFastLanguageModelで元のモデルをロード。\n",
|
86 |
+
"dtype = None # Noneにしておけば自動で設定\n",
|
87 |
+
"load_in_4bit = True # 今回は13Bモデルを扱うためTrue\n",
|
88 |
+
"\n",
|
89 |
+
"model, tokenizer = FastLanguageModel.from_pretrained(\n",
|
90 |
+
" model_name=model_id,\n",
|
91 |
+
" dtype=dtype,\n",
|
92 |
+
" load_in_4bit=load_in_4bit,\n",
|
93 |
+
" trust_remote_code=True,\n",
|
94 |
+
")"
|
95 |
+
]
|
96 |
+
},
|
97 |
+
{
|
98 |
+
"cell_type": "code",
|
99 |
+
"execution_count": null,
|
100 |
+
"metadata": {},
|
101 |
+
"outputs": [],
|
102 |
+
"source": [
|
103 |
+
"# 元のモデルにLoRAのアダプタを統合。\n",
|
104 |
+
"model = PeftModel.from_pretrained(model, adapter_id, token = HF_TOKEN)"
|
105 |
+
]
|
106 |
+
},
|
107 |
+
{
|
108 |
+
"cell_type": "code",
|
109 |
+
"execution_count": null,
|
110 |
+
"metadata": {
|
111 |
+
"id": "fg_yURyiB8o6"
|
112 |
+
},
|
113 |
+
"outputs": [],
|
114 |
+
"source": [
|
115 |
+
"# タスクとなるデータの読み込み。\n",
|
116 |
+
"# 事前にデータをアップロードしてください。\n",
|
117 |
+
"datasets = []\n",
|
118 |
+
"with open(\"./elyza-tasks-100-TV_0.jsonl\", \"r\") as f:\n",
|
119 |
+
" item = \"\"\n",
|
120 |
+
" for line in f:\n",
|
121 |
+
" line = line.strip()\n",
|
122 |
+
" item += line\n",
|
123 |
+
" if item.endswith(\"}\"):\n",
|
124 |
+
" datasets.append(json.loads(item))\n",
|
125 |
+
" item = \"\""
|
126 |
+
]
|
127 |
+
},
|
128 |
+
{
|
129 |
+
"cell_type": "code",
|
130 |
+
"execution_count": null,
|
131 |
+
"metadata": {
|
132 |
+
"id": "TwfZEra1CEJo"
|
133 |
+
},
|
134 |
+
"outputs": [],
|
135 |
+
"source": [
|
136 |
+
"# モデルを用いてタスクの推論。\n",
|
137 |
+
"\n",
|
138 |
+
"# 推論するためにモデルのモードを変更\n",
|
139 |
+
"FastLanguageModel.for_inference(model)\n",
|
140 |
+
"\n",
|
141 |
+
"results = []\n",
|
142 |
+
"for dt in tqdm(datasets):\n",
|
143 |
+
" input = dt[\"input\"]\n",
|
144 |
+
"\n",
|
145 |
+
" prompt = f\"\"\"### 指示\\n{input}\\n### 回答\\n\"\"\"\n",
|
146 |
+
"\n",
|
147 |
+
" inputs = tokenizer([prompt], return_tensors = \"pt\").to(model.device)\n",
|
148 |
+
"\n",
|
149 |
+
" outputs = model.generate(**inputs, max_new_tokens = 512, use_cache = True, do_sample=False, repetition_penalty=1.2)\n",
|
150 |
+
" prediction = tokenizer.decode(outputs[0], skip_special_tokens=True).split('\\n### 回答')[-1]\n",
|
151 |
+
"\n",
|
152 |
+
" results.append({\"task_id\": dt[\"task_id\"], \"input\": input, \"output\": prediction})"
|
153 |
+
]
|
154 |
+
},
|
155 |
+
{
|
156 |
+
"cell_type": "code",
|
157 |
+
"execution_count": null,
|
158 |
+
"metadata": {
|
159 |
+
"id": "voAPnXp5CKRL"
|
160 |
+
},
|
161 |
+
"outputs": [],
|
162 |
+
"source": [
|
163 |
+
"# 結果をjsonlで保存。\n",
|
164 |
+
"\n",
|
165 |
+
"# ここではadapter_idを元にファイル名を決定しているが、ファイル名は任意で問題なし。\n",
|
166 |
+
"json_file_id = re.sub(\".*/\", \"\", adapter_id)\n",
|
167 |
+
"with open(f\"/content/{json_file_id}_output.jsonl\", 'w', encoding='utf-8') as f:\n",
|
168 |
+
" for result in results:\n",
|
169 |
+
" json.dump(result, f, ensure_ascii=False)\n",
|
170 |
+
" f.write('\\n')"
|
171 |
+
]
|
172 |
+
}
|
173 |
+
],
|
174 |
+
"metadata": {
|
175 |
+
"colab": {
|
176 |
+
"provenance": []
|
177 |
+
},
|
178 |
+
"kernelspec": {
|
179 |
+
"display_name": "Python 3",
|
180 |
+
"name": "python3"
|
181 |
+
},
|
182 |
+
"language_info": {
|
183 |
+
"name": "python"
|
184 |
+
}
|
185 |
+
},
|
186 |
+
"nbformat": 4,
|
187 |
+
"nbformat_minor": 0
|
188 |
+
}
|