{ "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 }