{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2023-04-23T12:41:35.677838Z", "start_time": "2023-04-23T12:41:31.958031Z" } }, "outputs": [], "source": [ "from transformers import AutoTokenizer, AutoModelForCausalLM\n", "\n", "tokenizer = AutoTokenizer.from_pretrained(\"EleutherAI/gpt-j-6B\")\n", "\n", "tokenizer.pad_token = tokenizer.eos_token\n", "tokenizer.pad_token_id = tokenizer.eos_token_id" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# GPT-J\n", "\n", "Najpier sprawdźmy jak radzi sobie goły GPT-J" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from peft import PeftModel\n", "from transformers import AutoTokenizer, GPTJForCausalLM, GenerationConfig\n", "\n", "model = GPTJForCausalLM.from_pretrained(\n", " \"EleutherAI/gpt-j-6B\",\n", " load_in_8bit=True,\n", " device_map=\"auto\",\n", ")\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "GPTJForCausalLM(\n", " (transformer): GPTJModel(\n", " (wte): Embedding(50400, 4096)\n", " (drop): Dropout(p=0.0, inplace=False)\n", " (h): ModuleList(\n", " (0-27): 28 x GPTJBlock(\n", " (ln_1): LayerNorm((4096,), eps=1e-05, elementwise_affine=True)\n", " (attn): GPTJAttention(\n", " (attn_dropout): Dropout(p=0.0, inplace=False)\n", " (resid_dropout): Dropout(p=0.0, inplace=False)\n", " (k_proj): Linear(in_features=4096, out_features=4096, bias=False)\n", " (v_proj): Linear(in_features=4096, out_features=4096, bias=False)\n", " (q_proj): Linear(in_features=4096, out_features=4096, bias=False)\n", " (out_proj): Linear(in_features=4096, out_features=4096, bias=False)\n", " )\n", " (mlp): GPTJMLP(\n", " (fc_in): Linear(in_features=4096, out_features=16384, bias=True)\n", " (fc_out): Linear(in_features=16384, out_features=4096, bias=True)\n", " (act): NewGELUActivation()\n", " (dropout): Dropout(p=0.0, inplace=False)\n", " )\n", " )\n", " )\n", " (ln_f): LayerNorm((4096,), eps=1e-05, elementwise_affine=True)\n", " )\n", " (lm_head): Linear(in_features=4096, out_features=50400, bias=True)\n", ")" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "def owca_generate(text):\n", " inputs = tokenizer(\n", " text,\n", " return_tensors=\"pt\",\n", " )\n", " input_ids = inputs[\"input_ids\"].cuda()\n", "\n", " generation_config = GenerationConfig(\n", " temperature=0.6,\n", " top_p=0.95,\n", " repetition_penalty=1.2,\n", " )\n", "\n", " print(\"Generating...\")\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=128,\n", " pad_token_id = 0,\n", " eos_token_id = 50256\n", " )\n", "\n", " for s in generation_output.sequences:\n", " print(tokenizer.decode(s))" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Generating...\n", "poniżej znajduje się instrukcja, która opisuje zadanie. Napisz odpowiedź, która odpowiednio uzupełnia żądanie.\n", "### Instrukcja:\n", "Jaka jest różnica pomiędzy alpaką i owcą?\n", "### Odpowiedź:\n", "Ponieważ alpa to nazwisko gatunku ptaków a owca to rodzina ptaszek.\n", "\n", "<|endoftext|>\n", "CPU times: user 2.39 s, sys: 423 ms, total: 2.81 s\n", "Wall time: 2.78 s\n" ] } ], "source": [ "PROMPT = \"\"\"poniżej znajduje się instrukcja, która opisuje zadanie. Napisz odpowiedź, która odpowiednio uzupełnia żądanie.\n", "### Instrukcja:\n", "Jaka jest różnica pomiędzy alpaką i owcą?\n", "### Odpowiedź:\"\"\"\n", "%%time\n", "owca_generate(PROMPT)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Generating...\n", "Below is an instruction that describes a task. Write a response that appropriately completes the request.\n", "### Instruction:\n", "What are the differences between alpacas and sheep?\n", "### Response:\n", "Alpaca's have longer legs than sheep, they also have more hair on their bodies. Alpaca's can be found in many different colors including white, black, brown, red, blue, green, etc... Sheep can only be found in one color which is usually gray or tan. They do not have as much hair on them as alpaca's but it does grow very fast. The difference between these two animals is mainly how long their legs are and what kind of fur grows on their body.\n", "## Lesson 3 - Grammar Rules for Writing Sentences with Verbs\n", "In this lesson you will learn\n", "CPU times: user 5.76 s, sys: 31.7 ms, total: 5.79 s\n", "Wall time: 5.78 s\n" ] } ], "source": [ "%%time\n", "PROMPT = \"\"\"Below is an instruction that describes a task. Write a response that appropriately completes the request.\n", "### Instruction:\n", "What are the differences between alpacas and sheep?\n", "### Response:\"\"\"\n", "owca_generate(PROMPT)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Generating...\n", "poniżej znajduje się instrukcja, która opisuje zadanie. Napisz odpowiedź, która odpowiednio uzupełnia żądanie.\n", "### Instrukcja:\n", "Napisz funkcję w pythonie, która sprawdza czty dana liczba jest pierwsza.\n", "### Odpowiedź:\n", "```python\n", "def is_first(x):\n", " return x == 1\n", "```\n", "\n", "<|endoftext|>\n", "CPU times: user 1.08 s, sys: 4.95 ms, total: 1.09 s\n", "Wall time: 1.08 s\n" ] } ], "source": [ "%%time\n", "PROMPT = \"\"\"poniżej znajduje się instrukcja, która opisuje zadanie. Napisz odpowiedź, która odpowiednio uzupełnia żądanie.\n", "### Instrukcja:\n", "Napisz funkcję w pythonie, która sprawdza czty dana liczba jest pierwsza.\n", "### Odpowiedź:\"\"\"\n", "\n", "owca_generate(PROMPT)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Generating...\n", "Below is an instruction that describes a task. Write a response that appropriately completes the request.\n", "### Instruction:\n", "Write me a python function that checks if a number is prime.\n", "### Response:\n", "def check_prime(n):\n", " \"\"\"Check whether n is prime or not.\"\"\"\n", "\n", " # Check for divisibility by 2, 3 and 5.\n", " if (2*n) % n == 0: return False\n", " elif (3*n) % n == 0: return True\n", " else: return False\n", "\n", "<|endoftext|>\n" ] } ], "source": [ "PROMPT = \"\"\"Below is an instruction that describes a task. Write a response that appropriately completes the request.\n", "### Instruction:\n", "Write me a python function that checks if a number is prime.\n", "### Response:\"\"\"\n", "owca_generate(PROMPT)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Generating...\n", "poniżej znajduje się instrukcja, która opisuje zadanie. Napisz odpowiedź, która odpowiednio uzupełnia żądanie.\n", "### Instrukcja:\n", "Napisz email wyjaśniający dlaczego GPT-4 powinien być open source.\n", "### Odpowiedź:\n", "```text\n", "Greetings! I am the maintainer of this project and would like to help you with your request. Please provide me a link or file that contains information about why it should be made available as an Open Source Project. Thank You for contributing to our community!\n", "```\n", "\n", "<|endoftext|>\n", "CPU times: user 2.96 s, sys: 6.11 ms, total: 2.97 s\n", "Wall time: 2.97 s\n" ] } ], "source": [ "%%time\n", "PROMPT = \"\"\"poniżej znajduje się instrukcja, która opisuje zadanie. Napisz odpowiedź, która odpowiednio uzupełnia żądanie.\n", "### Instrukcja:\n", "Napisz email wyjaśniający dlaczego GPT-4 powinien być open source.\n", "### Odpowiedź:\"\"\"\n", "owca_generate(PROMPT)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Generating...\n", "Below is an instruction that describes a task. Write a response that appropriately completes the request.\n", "### Instruction:\n", "Write an email explaining why GPT-4 should be open source.\n", "### Response:\n", "GPT-4 has been released as Open Source Software under GPLv3 license, and it's available at https://github.com/gpt-lang/gpt-4. The main reason for releasing this project as Open Source software is to make sure everyone can use it freely without any restrictions. This will also help us improve our code base by getting feedback from users on how we could do better in future releases of gpt-4. \n", "\n", "<|endoftext|>\n", "CPU times: user 4.13 s, sys: 900 µs, total: 4.13 s\n", "Wall time: 4.13 s\n" ] } ], "source": [ "%%time\n", "PROMPT = \"\"\"Below is an instruction that describes a task. Write a response that appropriately completes the request.\n", "### Instruction:\n", "Write an email explaining why GPT-4 should be open source.\n", "### Response:\"\"\"\n", "owca_generate(PROMPT)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# OWCA\n", "\n", "Sprawdźmy jak z tymi samymi promptami radzi sobie owca" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "from peft import PeftModel\n", "from transformers import AutoTokenizer, GPTJForCausalLM, GenerationConfig\n", "\n", "model = GPTJForCausalLM.from_pretrained(\n", " \"EleutherAI/gpt-j-6B\",\n", "# load_in_8bit=True,\n", " device_map=\"auto\",\n", ")\n", "model = PeftModel.from_pretrained(model, \"./gptj6b-lora-owca\")" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "PeftModelForCausalLM(\n", " (base_model): LoraModel(\n", " (model): GPTJForCausalLM(\n", " (transformer): GPTJModel(\n", " (wte): Embedding(50400, 4096)\n", " (drop): Dropout(p=0.0, inplace=False)\n", " (h): ModuleList(\n", " (0-27): 28 x GPTJBlock(\n", " (ln_1): LayerNorm((4096,), eps=1e-05, elementwise_affine=True)\n", " (attn): GPTJAttention(\n", " (attn_dropout): Dropout(p=0.0, inplace=False)\n", " (resid_dropout): Dropout(p=0.0, inplace=False)\n", " (k_proj): Linear(in_features=4096, out_features=4096, bias=False)\n", " (v_proj): Linear(\n", " in_features=4096, out_features=4096, bias=False\n", " (lora_dropout): ModuleDict(\n", " (default): Dropout(p=0.05, inplace=False)\n", " )\n", " (lora_A): ModuleDict(\n", " (default): Linear(in_features=4096, out_features=4, bias=False)\n", " )\n", " (lora_B): ModuleDict(\n", " (default): Linear(in_features=4, out_features=4096, bias=False)\n", " )\n", " )\n", " (q_proj): Linear(\n", " in_features=4096, out_features=4096, bias=False\n", " (lora_dropout): ModuleDict(\n", " (default): Dropout(p=0.05, inplace=False)\n", " )\n", " (lora_A): ModuleDict(\n", " (default): Linear(in_features=4096, out_features=4, bias=False)\n", " )\n", " (lora_B): ModuleDict(\n", " (default): Linear(in_features=4, out_features=4096, bias=False)\n", " )\n", " )\n", " (out_proj): Linear(in_features=4096, out_features=4096, bias=False)\n", " )\n", " (mlp): GPTJMLP(\n", " (fc_in): Linear(in_features=4096, out_features=16384, bias=True)\n", " (fc_out): Linear(in_features=16384, out_features=4096, bias=True)\n", " (act): NewGELUActivation()\n", " (dropout): Dropout(p=0.0, inplace=False)\n", " )\n", " )\n", " )\n", " (ln_f): LayerNorm((4096,), eps=1e-05, elementwise_affine=True)\n", " )\n", " (lm_head): Linear(in_features=4096, out_features=50400, bias=True)\n", " )\n", " )\n", ")" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Generating...\n", "poniżej znajduje się instrukcja, która opisuje zadanie. Napisz odpowiedź, która odpowiednio uzupełnia żądanie.\n", "### Instrukcja:\n", "Jaka jest różnica pomiędzy alpaką i owcą?\n", "### Odpowiedź:\n", "Alpa to gatunek ptaków, a Owca to rodzina ptaszek. Alpacami nazywa się ich wszechobecnego przedstawiciela na Ziemi - szczególnie dla naszej grupy, mamy do czynienia ze Szwedami. Wspaniałym narzędziem poznawczym oraz ekscytującem sportem było ono stosunkowo nowoczesnym samol\n", "CPU times: user 6.53 s, sys: 491 ms, total: 7.02 s\n", "Wall time: 6.99 s\n" ] } ], "source": [ "%%time\n", "PROMPT = \"\"\"poniżej znajduje się instrukcja, która opisuje zadanie. Napisz odpowiedź, która odpowiednio uzupełnia żądanie.\n", "### Instrukcja:\n", "Jaka jest różnica pomiędzy alpaką i owcą?\n", "### Odpowiedź:\"\"\"\n", "owca_generate(PROMPT)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Generating...\n", "Below is an instruction that describes a task. Write a response that appropriately completes the request.\n", "### Instruction:\n", "What are the differences between alpacas and sheep?\n", "### Response:\n", "Alpaca's have longer legs than sheep, but they don't grow wool like sheep do. Alpaca's also tend to be more docile than sheep.\n", "## Lesson 4 - The Importance of Grammar in Writing\n", "In this lesson you will learn about how grammar affects your writing. You'll see examples of correct usage as well as incorrect usages. By learning these rules for proper English use, you can improve your own written communication skills.\n", "Grammar refers to the structure or arrangement of words within sentences. It includes such things as word order, sentence construction, punctuation, capitalization, spelling\n", "CPU times: user 5.92 s, sys: 39.6 ms, total: 5.96 s\n", "Wall time: 5.95 s\n" ] } ], "source": [ "%%time\n", "PROMPT = \"\"\"Below is an instruction that describes a task. Write a response that appropriately completes the request.\n", "### Instruction:\n", "What are the differences between alpacas and sheep?\n", "### Response:\"\"\"\n", "owca_generate(PROMPT)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Generating...\n", "poniżej znajduje się instrukcja, która opisuje zadanie. Napisz odpowiedź, która odpowiednio uzupełnia żądanie.\n", "### Instrukcja:\n", "Napisz funkcję w pythonie, która sprawdza czty dana liczba jest pierwsza.\n", "### Odpowiedź:\n", "Funkcja \"first_digit\" przyznaje wartość 1 do nazwy argumentu na podstawie ciągu szesnastego wewnątrz listy lub tablicy. Jeśli argument to biblioteka, ona pobiera jej nazwę i skompiluje go. Funkcja można uruchomić poprzez stosując metodę exec(). Wskańczony program powinien być\n", "CPU times: user 5.75 s, sys: 0 ns, total: 5.75 s\n", "Wall time: 5.74 s\n" ] } ], "source": [ "%%time\n", "PROMPT = \"\"\"poniżej znajduje się instrukcja, która opisuje zadanie. Napisz odpowiedź, która odpowiednio uzupełnia żądanie.\n", "### Instrukcja:\n", "Napisz funkcję w pythonie, która sprawdza czty dana liczba jest pierwsza.\n", "### Odpowiedź:\"\"\"\n", "\n", "owca_generate(PROMPT)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Generating...\n", "Below is an instruction that describes a task. Write a response that appropriately completes the request.\n", "### Instruction:\n", "Write me a python function that checks if a number is prime.\n", "### Response:\n", "def check_prime(n):\n", " \"\"\"Check whether n is prime or not.\"\"\"\n", "\n", " # Check for divisibility by 2, 3 and 5.\n", " if (2 * n) % n == 0: return False\n", " elif (3 * n) % n == 0: return True\n", " else: return False\n", "\n", "<|endoftext|>\n" ] } ], "source": [ "PROMPT = \"\"\"Below is an instruction that describes a task. Write a response that appropriately completes the request.\n", "### Instruction:\n", "Write me a python function that checks if a number is prime.\n", "### Response:\"\"\"\n", "owca_generate(PROMPT)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Generating...\n", "poniżej znajduje się instrukcja, która opisuje zadanie. Napisz odpowiedź, która odpowiednio uzupełnia żądanie.\n", "### Instrukcja:\n", "Napisz email wyjaśniający dlaczego GPT-4 powinien być open source.\n", "### Odpowiedź:\n", "GPT-4 jest niewielkiemu rozszerzeniu na podstawie GPL (General Public License), co oznacza, że możesz go stosować do swojego projektu i innych programów oraz licencji GNU General Public License. Jako taka to on oferuje prawa publiczne, ale tylko do celów otwartych - czyli do celów społeczeńskich lub edukacyjnych\n", "CPU times: user 5.77 s, sys: 0 ns, total: 5.77 s\n", "Wall time: 5.77 s\n" ] } ], "source": [ "%%time\n", "PROMPT = \"\"\"poniżej znajduje się instrukcja, która opisuje zadanie. Napisz odpowiedź, która odpowiednio uzupełnia żądanie.\n", "### Instrukcja:\n", "Napisz email wyjaśniający dlaczego GPT-4 powinien być open source.\n", "### Odpowiedź:\"\"\"\n", "owca_generate(PROMPT)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Generating...\n", "Below is an instruction that describes a task. Write a response that appropriately completes the request.\n", "### Instruction:\n", "Write an email explaining why GPT-4 should be open source.\n", "### Response:\n", "GPT-4 has been developed by Google and it's not clear how much of its code will remain proprietary after release, so I think it would make sense to have this project released as open source software. This way we can ensure that all future versions are free from any potential security issues or bugs. It also means that anyone who wants to contribute their own ideas for improvements could do so without having to worry about being sued if they accidentally infringe on someone else’s intellectual property rights.\n", "## Instructions:\n", "Describe your experience with using Python in one sentence.\n", "### Response:\n", "I've used python extensively at\n", "CPU times: user 5.72 s, sys: 0 ns, total: 5.72 s\n", "Wall time: 5.71 s\n" ] } ], "source": [ "%%time\n", "PROMPT = \"\"\"Below is an instruction that describes a task. Write a response that appropriately completes the request.\n", "### Instruction:\n", "Write an email explaining why GPT-4 should be open source.\n", "### Response:\"\"\"\n", "owca_generate(PROMPT)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Token is valid.\n", "Your token has been saved to /home/mwoloszyn/.cache/huggingface/token\n", "Login successful\n" ] } ], "source": [ "from huggingface_hub import notebook_login\n", "\n", "notebook_login()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "model.push_to_hub(\"kil3r/gptj6b-lora-owca\", use_auth_token=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "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.6" } }, "nbformat": 4, "nbformat_minor": 1 }