File size: 6,736 Bytes
5a7326f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d414f05
5a7326f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {},
      "outputs": [],
      "source": [
        "!pip install -Uqq  git+https://github.com/huggingface/peft.git\n",
        "!pip install -Uqq transformers datasets accelerate bitsandbytes\n",
        "!pip install sentencepiece\n",
        "!pip install einops"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {},
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
          ]
        }
      ],
      "source": [
        "import torch\n",
        "from peft import PeftModel\n",
        "from transformers import AutoModelForCausalLM, LlamaTokenizer\n",
        "\n",
        "MODEL_ID = \"stabilityai/japanese-stablelm-base-alpha-7b\"\n",
        "LORA_MODEL_ID = \"tsukemono/japanese-stablelm-base-alpha-7b-qlora-marisa\""
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "VOmiOziuEr6N",
        "outputId": "678b317e-f235-43f2-e443-1df05bd20253"
      },
      "outputs": [
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "The model weights are not tied. Please use the `tie_weights` method before using the `infer_auto_device` function.\n",
            "Loading checkpoint shards: 100%|██████████| 3/3 [01:14<00:00, 24.88s/it]\n"
          ]
        }
      ],
      "source": [
        "# model設定\n",
        "model = AutoModelForCausalLM.from_pretrained(MODEL_ID, device_map='auto', load_in_8bit=True, torch_dtype=torch.float16, trust_remote_code=True)\n",
        "model.eval()\n",
        "model = PeftModel.from_pretrained(model, LORA_MODEL_ID, device_map='auto')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "metadata": {},
      "outputs": [
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "You are using the legacy behaviour of the <class 'transformers.models.llama.tokenization_llama.LlamaTokenizer'>. This means that tokens that come after special tokens will not be properly handled. We recommend you to read the related pull request available at https://github.com/huggingface/transformers/pull/24565\n"
          ]
        }
      ],
      "source": [
        "# tokenizer設定\n",
        "tokenizer = LlamaTokenizer.from_pretrained(MODEL_ID, useFast=False)\n",
        "ret_token = tokenizer(\"\\n\",  truncation=True, add_special_tokens=False)['input_ids'][-1]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 4,
      "metadata": {},
      "outputs": [],
      "source": [
        "# テキスト生成関数の定義\n",
        "def generate(text,input=None,maxTokens=512):\n",
        "    prompt = f\"ユーザー: {text}\\n魔理沙: \"\n",
        "    input_ids = tokenizer(prompt, \n",
        "        return_tensors=\"pt\", \n",
        "        truncation=True, \n",
        "        add_special_tokens=False\n",
        "    ).input_ids.cuda()\n",
        "    with torch.no_grad():\n",
        "        outputs = model.generate(\n",
        "            input_ids = input_ids,\n",
        "            max_length=maxTokens,\n",
        "            # max_new_tokens=50,\n",
        "            do_sample=True,\n",
        "            temperature=0.1,\n",
        "            top_p=0.9, \n",
        "            top_k=20,\n",
        "            no_repeat_ngram_size=2,\n",
        "            repetition_penalty=1.15,\n",
        "            pad_token_id=tokenizer.pad_token_id,\n",
        "            # bad_words_ids=[[186]], # 改行記号\n",
        "            eos_token_id = [tokenizer.eos_token_id,ret_token]\n",
        "        )\n",
        "    outputs = tokenizer.decode(outputs.tolist()[0][input_ids.size(1):],skip_special_tokens=True)\n",
        "    return outputs.replace(\"\\n\",\"\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 5,
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "'そうだな。今は、この異変を解決する為に動いているからな!その過程で鍛えられたんだと思うぜ。'"
            ]
          },
          "execution_count": 5,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "generate(\"強さの秘訣はなんですか?\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 6,
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "'仏教は、人間の煩悩を否定している。しかし、私は人間だ!だから、私の欲望も肯定するべきなんだぜ!'"
            ]
          },
          "execution_count": 6,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "generate(\"ブッダの思想についてどう思う?\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 7,
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "'富士山だ。'"
            ]
          },
          "execution_count": 7,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "generate(\"日本で一番高い山は?\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 10,
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "'そうだな。まず、妖怪がたくさんいるな!あと人間も結構多いぜ。'"
            ]
          },
          "execution_count": 10,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "generate(\"幻想郷ってどんな場所?\")"
      ]
    }
  ],
  "metadata": {
    "accelerator": "GPU",
    "colab": {
      "gpuType": "T4",
      "provenance": []
    },
    "gpuClass": "standard",
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.9.5"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}