File size: 9,907 Bytes
1e6d67a |
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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 |
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "jONLwzXgLg-I",
"metadata": {
"id": "jONLwzXgLg-I"
},
"outputs": [],
"source": [
"!pip install -q git+https://github.com/huggingface/transformers.git\n",
"!pip install -q git+https://github.com/huggingface/peft.git\n",
"!pip install -q git+https://github.com/huggingface/accelerate.git@main\n",
"!pip install huggingface_hub\n",
"!pip install bitsandbytes\n",
"!pip install SentencePiece"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "36460935",
"metadata": {
"id": "36460935"
},
"outputs": [],
"source": [
"import os\n",
"\n",
"os.environ[\"CUDA_VISIBLE_DEVICES\"] = \"0\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1351e04c",
"metadata": {
"id": "1351e04c"
},
"outputs": [],
"source": [
"from huggingface_hub import notebook_login\n",
"import torch\n",
"\n",
"notebook_login()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d85af699",
"metadata": {
"id": "d85af699"
},
"outputs": [],
"source": [
"from peft import PeftModel\n",
"from transformers import LlamaTokenizer, LlamaForCausalLM, GenerationConfig\n",
"\n",
"model_name = \"decapoda-research/llama-7b-hf\"\n",
"tokenizer = LlamaTokenizer.from_pretrained(model_name)\n",
"model = LlamaForCausalLM.from_pretrained(model_name, load_in_8bit=True, device_map=\"auto\", use_auth_token=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f0f515ed",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "f0f515ed",
"outputId": "312488a5-f4f8-48a4-8c63-7b4a59e80418"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 14.3 s, sys: 3.98 s, total: 18.3 s\n",
"Wall time: 19.3 s\n"
]
}
],
"source": [
"%%time\n",
"model = PeftModel.from_pretrained(model, \"tloen/alpaca-lora-7b\", adapter_name=\"eng_alpaca\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "67a0c121",
"metadata": {
"id": "67a0c121"
},
"outputs": [],
"source": [
"%%time\n",
"model.load_adapter(\"22h/cabrita-lora-v0-1\", adapter_name=\"portuguese_alpaca\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4b655fca",
"metadata": {
"id": "4b655fca"
},
"outputs": [],
"source": [
"model"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e9ebd572",
"metadata": {
"id": "e9ebd572"
},
"outputs": [],
"source": [
"model.to(\"cuda\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "138805b3",
"metadata": {
"id": "138805b3"
},
"outputs": [],
"source": [
"import torch\n",
"\n",
"device = \"cuda\"\n",
"\n",
"\n",
"def generate_prompt(instruction, input=None):\n",
" if input:\n",
" return f\"\"\"Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.\n",
"### Instruction:\n",
"{instruction}\n",
"### Input:\n",
"{input}\n",
"### Response:\"\"\"\n",
" else:\n",
" return f\"\"\"Below is an instruction that describes a task. Write a response that appropriately completes the request.\n",
"### Instruction:\n",
"{instruction}\n",
"### Response:\"\"\"\n",
"\n",
"\n",
"def evaluate(\n",
" instruction,\n",
" input=None,\n",
" temperature=0.1,\n",
" top_p=0.75,\n",
" top_k=40,\n",
" num_beams=4,\n",
" max_new_tokens=256,\n",
" **kwargs,\n",
"):\n",
" prompt = generate_prompt(instruction, input)\n",
" inputs = tokenizer(prompt, return_tensors=\"pt\")\n",
" input_ids = inputs[\"input_ids\"].to(device)\n",
" generation_config = GenerationConfig(\n",
" temperature=temperature,\n",
" top_p=top_p,\n",
" top_k=top_k,\n",
" num_beams=num_beams,\n",
" no_repeat_ngram_size=3,\n",
" **kwargs,\n",
" )\n",
"\n",
" with torch.no_grad():\n",
" generation_output = model.generate(\n",
" input_ids=input_ids,\n",
" generation_config=generation_config,\n",
" return_dict_in_generate=True,\n",
" output_scores=True,\n",
" max_new_tokens=max_new_tokens,\n",
" )\n",
" s = generation_output.sequences[0]\n",
" output = tokenizer.decode(s)\n",
" return output.split(\"### Response:\")[1].strip()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fd5e6b3b",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "fd5e6b3b",
"outputId": "ec72241b-c427-4258-b02f-2101df0d171a"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 4.98 ms, sys: 0 ns, total: 4.98 ms\n",
"Wall time: 5.19 ms\n"
]
}
],
"source": [
"%%time\n",
"model.set_adapter(\"eng_alpaca\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "33650851",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "33650851",
"outputId": "aae24052-0f09-4812-88c3-6fb53dec656c"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The alpaca (Vicugna pacos) is a domesticated species of South American camelid. It resembles a small llama in appearance, but unlike the llama, it is not used as a beast of burden. It is kept primarily for its fiber, which can be spun into yarn. Alpaca fiber is warmer, lighter, and softer than sheep's wool, and is highly valued in the textile industry. The fiber comes in a variety of natural colors, including white, beige, cream, and fawn. It can also be dyed in a wide range of colors.\n",
"Alpaca herds can be found in the highlands of Peru, Bolivia, Chile, Ecuador, and Colombia. They are also raised in the United States, Canada, Australia, New Zealand, and Europe. The animals graze on grasses, herbs, and shrubs, and can survive in temperatures as low as -30°F (-34°C). They are social animals, living in herds of up to 20 individuals.\n",
"The fiber of the alpaka is used to make clothing\n"
]
}
],
"source": [
"instruction = \"Tell me about alpacas.\"\n",
"\n",
"print(evaluate(instruction))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fdc7196e",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "fdc7196e",
"outputId": "44cb6742-066b-470e-f507-cbf21e5ae030"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 5.58 ms, sys: 97 µs, total: 5.68 ms\n",
"Wall time: 5.63 ms\n"
]
}
],
"source": [
"%%time\n",
"model.set_adapter(\"portuguese_alpaca\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "31997da3",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "31997da3",
"outputId": "8071de75-dc9d-4e89-e85f-674f1de22658"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\"Eu preciso ficar em casa para cuidar de meu gato.\"\n"
]
}
],
"source": [
"instruction = \"Invente uma desculpa criativa pra dizer que não preciso ir à festa.\"\n",
"\n",
"print(evaluate(instruction))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8b8e4e9a",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "8b8e4e9a",
"outputId": "84226223-e018-4feb-e189-969c344fd940"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"I'm sorry, but I can't go to the party. I'm sick. I have a cold. I don't feel well. I need to stay at home and rest.\n",
"I have a lot of homework to do. My dog ate my homework. My homework is too hard. I didn't have time to do it. It's too late. I forgot about it.\n",
"My parents won't let me go. My parents are out of town. They're on vacation. They have to work. They are sick. They need to take care of my brother.\n",
"They're not home. They went to the grocery store. They took the car to the mechanic. They had to go to a meeting. They were in a hurry. They forgot about me.\n",
"Their car broke down. Their car ran out of gas. They got a flat tire. They couldn't find a parking space. They didn' t have enough money. They lost their wallet.\n",
"It's raining. The roads are icy. There's a blizzard. There are too many cars on the road. There was an accident.\n"
]
}
],
"source": [
"with model.disable_adapter():\n",
" instruction = \"Invente uma desculpa criativa pra dizer que não preciso ir à festa.\"\n",
"\n",
" print(evaluate(instruction))"
]
}
],
"metadata": {
"accelerator": "GPU",
"colab": {
"provenance": []
},
"gpuClass": "standard",
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"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.10.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|