OotoroMS commited on
Commit
391257f
·
verified ·
1 Parent(s): 16139a1

Upload Model_Inference_Template_unsloth.ipynb

Browse files
Model_Inference_Template_unsloth.ipynb ADDED
@@ -0,0 +1,182 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "metadata": {
6
+ "id": "MljifiTVCT0_"
7
+ },
8
+ "source": [
9
+ "# 推論用コード\n",
10
+ "Hugging Faceにアップロードしたモデルを用いてELYZA-tasks-100-TVの出力を得るためのコードです。 \n",
11
+ "このコードはunslothライブラリを用いてモデルを読み込み、推論するためのコードとなります。\n",
12
+ "このコードはサンプルの推論用コードとほぼ同一であり、実行すればそのまま提出用のjsonlファイルが作成されます。\n",
13
+ "\n",
14
+ "なお、HuggingFaceのトークンの部分は実行者に合わせ適宜変更してください。"
15
+ ]
16
+ },
17
+ {
18
+ "cell_type": "code",
19
+ "execution_count": null,
20
+ "metadata": {
21
+ "id": "I5B5MOHuBy8b"
22
+ },
23
+ "outputs": [],
24
+ "source": [
25
+ "%%capture\n",
26
+ "!pip install unsloth\n",
27
+ "!pip uninstall unsloth -y && pip install --upgrade --no-cache-dir \"unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git\""
28
+ ]
29
+ },
30
+ {
31
+ "cell_type": "code",
32
+ "execution_count": null,
33
+ "metadata": {
34
+ "id": "GM7SNRtACg9V",
35
+ "colab": {
36
+ "base_uri": "https://localhost:8080/"
37
+ },
38
+ "outputId": "607ad0ee-6e25-4e07-8c45-33ef0161e656"
39
+ },
40
+ "outputs": [
41
+ {
42
+ "output_type": "stream",
43
+ "name": "stdout",
44
+ "text": [
45
+ "🦥 Unsloth: Will patch your computer to enable 2x faster free finetuning.\n",
46
+ "🦥 Unsloth Zoo will now patch everything to make training faster!\n"
47
+ ]
48
+ }
49
+ ],
50
+ "source": [
51
+ "from unsloth import FastLanguageModel\n",
52
+ "import torch\n",
53
+ "import json"
54
+ ]
55
+ },
56
+ {
57
+ "cell_type": "code",
58
+ "execution_count": null,
59
+ "metadata": {
60
+ "id": "JmdUATTVCtyr"
61
+ },
62
+ "outputs": [],
63
+ "source": [
64
+ "user_name = \"OotoroMS\"\n",
65
+ "model_name = \"llm-jp-3-13b-it_lora\"\n",
66
+ "\n",
67
+ "model_address = f\"{user_name}/{model_name}\""
68
+ ]
69
+ },
70
+ {
71
+ "cell_type": "code",
72
+ "execution_count": null,
73
+ "metadata": {
74
+ "id": "TB6Hzx-2B5g8"
75
+ },
76
+ "outputs": [],
77
+ "source": [
78
+ "max_seq_length = 2048\n",
79
+ "dtype = None\n",
80
+ "load_in_4bit = True\n",
81
+ "\n",
82
+ "model, tokenizer = FastLanguageModel.from_pretrained(\n",
83
+ " model_name = model_address,\n",
84
+ " max_seq_length = max_seq_length,\n",
85
+ " dtype = dtype,\n",
86
+ " load_in_4bit = load_in_4bit,\n",
87
+ " token = \"HF_TOKEN\",\n",
88
+ ")\n",
89
+ "FastLanguageModel.for_inference(model)"
90
+ ]
91
+ },
92
+ {
93
+ "cell_type": "code",
94
+ "execution_count": null,
95
+ "metadata": {
96
+ "id": "fg_yURyiB8o6"
97
+ },
98
+ "outputs": [],
99
+ "source": [
100
+ "# データセットの読み込み。\n",
101
+ "# omnicampusの開発環境では、左にタスクのjsonlをドラッグアンドドロップしてから実行。\n",
102
+ "datasets = []\n",
103
+ "with open(\"./elyza-tasks-100-TV_0.jsonl\", \"r\") as f:\n",
104
+ " item = \"\"\n",
105
+ " for line in f:\n",
106
+ " line = line.strip()\n",
107
+ " item += line\n",
108
+ " if item.endswith(\"}\"):\n",
109
+ " datasets.append(json.loads(item))\n",
110
+ " item = \"\""
111
+ ]
112
+ },
113
+ {
114
+ "cell_type": "code",
115
+ "execution_count": null,
116
+ "metadata": {
117
+ "id": "TwfZEra1CEJo",
118
+ "colab": {
119
+ "base_uri": "https://localhost:8080/"
120
+ },
121
+ "outputId": "bca86293-c1f2-45bf-f19c-9910d4e0118f"
122
+ },
123
+ "outputs": [
124
+ {
125
+ "output_type": "stream",
126
+ "name": "stderr",
127
+ "text": [
128
+ "100%|██████████| 100/100 [10:31<00:00, 6.31s/it]\n"
129
+ ]
130
+ }
131
+ ],
132
+ "source": [
133
+ "from tqdm import tqdm\n",
134
+ "\n",
135
+ "# 推論\n",
136
+ "results = []\n",
137
+ "for dt in tqdm(datasets):\n",
138
+ " input = dt[\"input\"]\n",
139
+ "\n",
140
+ " prompt = f\"\"\"### 指示\\n{input}\\n### 回答\\n\"\"\"\n",
141
+ "\n",
142
+ " inputs = tokenizer([prompt], return_tensors = \"pt\").to(model.device)\n",
143
+ "\n",
144
+ " outputs = model.generate(**inputs, max_new_tokens = 512, use_cache = True, do_sample=False, repetition_penalty=1.2)\n",
145
+ " prediction = tokenizer.decode(outputs[0], skip_special_tokens=True).split('\\n### 回答')[-1]\n",
146
+ "\n",
147
+ " results.append({\"task_id\": dt[\"task_id\"], \"input\": input, \"output\": prediction})"
148
+ ]
149
+ },
150
+ {
151
+ "cell_type": "code",
152
+ "execution_count": null,
153
+ "metadata": {
154
+ "id": "voAPnXp5CKRL"
155
+ },
156
+ "outputs": [],
157
+ "source": [
158
+ "with open(f\"./{model_name}_output.jsonl\", 'w', encoding='utf-8') as f:\n",
159
+ " for result in results:\n",
160
+ " json.dump(result, f, ensure_ascii=False)\n",
161
+ " f.write('\\n')"
162
+ ]
163
+ }
164
+ ],
165
+ "metadata": {
166
+ "colab": {
167
+ "provenance": [],
168
+ "gpuType": "L4",
169
+ "machine_shape": "hm"
170
+ },
171
+ "kernelspec": {
172
+ "display_name": "Python 3",
173
+ "name": "python3"
174
+ },
175
+ "language_info": {
176
+ "name": "python"
177
+ },
178
+ "accelerator": "GPU"
179
+ },
180
+ "nbformat": 4,
181
+ "nbformat_minor": 0
182
+ }