{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [], "gpuType": "T4" }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" }, "accelerator": "GPU", "widgets": { "application/vnd.jupyter.widget-state+json": { "cb82c295aa304cbab2c037caffc8d8d1": { "model_module": "@jupyter-widgets/controls", "model_name": "HBoxModel", "model_module_version": "1.5.0", "state": { "_dom_classes": [], "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "HBoxModel", "_view_count": null, "_view_module": "@jupyter-widgets/controls", "_view_module_version": "1.5.0", "_view_name": "HBoxView", "box_style": "", "children": [ "IPY_MODEL_67367bae31384088aa07fc80aa68a081", "IPY_MODEL_ed5621e9730641379c6b1c740c43ec1f", "IPY_MODEL_4c31c17d684d434b88481c42404d5582" ], "layout": "IPY_MODEL_9e300fa0cd094325ac4ce2f1aa5cca8b" } }, "67367bae31384088aa07fc80aa68a081": { "model_module": "@jupyter-widgets/controls", "model_name": "HTMLModel", "model_module_version": "1.5.0", "state": { "_dom_classes": [], "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "HTMLModel", "_view_count": null, "_view_module": "@jupyter-widgets/controls", "_view_module_version": "1.5.0", "_view_name": "HTMLView", "description": "", "description_tooltip": null, "layout": "IPY_MODEL_2d58b05027644ae49d0d1c9b7a8c9f90", "placeholder": "​", "style": "IPY_MODEL_0e9108eb5ada489a93719771ba9a8903", "value": "Loading checkpoint shards: 100%" } }, "ed5621e9730641379c6b1c740c43ec1f": { "model_module": "@jupyter-widgets/controls", "model_name": "FloatProgressModel", "model_module_version": "1.5.0", "state": { "_dom_classes": [], "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "FloatProgressModel", "_view_count": null, "_view_module": "@jupyter-widgets/controls", "_view_module_version": "1.5.0", "_view_name": "ProgressView", "bar_style": "success", "description": "", "description_tooltip": null, "layout": "IPY_MODEL_8fea9e36d8a948c4a93e9613df9f9f48", "max": 2, "min": 0, "orientation": "horizontal", "style": "IPY_MODEL_51e33ef50f6f48e1b6c7d3da4ade7819", "value": 2 } }, "4c31c17d684d434b88481c42404d5582": { "model_module": "@jupyter-widgets/controls", "model_name": "HTMLModel", "model_module_version": "1.5.0", "state": { "_dom_classes": [], "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "HTMLModel", "_view_count": null, "_view_module": "@jupyter-widgets/controls", "_view_module_version": "1.5.0", "_view_name": "HTMLView", "description": "", "description_tooltip": null, "layout": "IPY_MODEL_16ba2159038e4dafac854a238854cc93", "placeholder": "​", "style": "IPY_MODEL_b15a25c37e714190a8a3032ce16d8944", "value": " 2/2 [00:59<00:00, 27.15s/it]" } }, "9e300fa0cd094325ac4ce2f1aa5cca8b": { "model_module": "@jupyter-widgets/base", "model_name": "LayoutModel", "model_module_version": "1.2.0", "state": { "_model_module": "@jupyter-widgets/base", "_model_module_version": "1.2.0", "_model_name": "LayoutModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "LayoutView", "align_content": null, "align_items": null, "align_self": null, "border": null, "bottom": null, "display": null, "flex": null, "flex_flow": null, "grid_area": null, "grid_auto_columns": null, "grid_auto_flow": null, "grid_auto_rows": null, "grid_column": null, "grid_gap": null, "grid_row": null, "grid_template_areas": null, "grid_template_columns": null, "grid_template_rows": null, "height": null, "justify_content": null, "justify_items": null, "left": null, "margin": null, "max_height": null, "max_width": null, "min_height": null, "min_width": null, "object_fit": null, "object_position": null, "order": null, "overflow": null, "overflow_x": null, "overflow_y": null, "padding": null, "right": null, "top": null, "visibility": null, "width": null } }, "2d58b05027644ae49d0d1c9b7a8c9f90": { "model_module": "@jupyter-widgets/base", "model_name": "LayoutModel", "model_module_version": "1.2.0", "state": { "_model_module": "@jupyter-widgets/base", "_model_module_version": "1.2.0", "_model_name": "LayoutModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "LayoutView", "align_content": null, "align_items": null, "align_self": null, "border": null, "bottom": null, "display": null, "flex": null, "flex_flow": null, "grid_area": null, "grid_auto_columns": null, "grid_auto_flow": null, "grid_auto_rows": null, "grid_column": null, "grid_gap": null, "grid_row": null, "grid_template_areas": null, "grid_template_columns": null, "grid_template_rows": null, "height": null, "justify_content": null, "justify_items": null, "left": null, "margin": null, "max_height": null, "max_width": null, "min_height": null, "min_width": null, "object_fit": null, "object_position": null, "order": null, "overflow": null, "overflow_x": null, "overflow_y": null, "padding": null, "right": null, "top": null, "visibility": null, "width": null } }, "0e9108eb5ada489a93719771ba9a8903": { "model_module": "@jupyter-widgets/controls", "model_name": "DescriptionStyleModel", "model_module_version": "1.5.0", "state": { "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "DescriptionStyleModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "StyleView", "description_width": "" } }, "8fea9e36d8a948c4a93e9613df9f9f48": { "model_module": "@jupyter-widgets/base", "model_name": "LayoutModel", "model_module_version": "1.2.0", "state": { "_model_module": "@jupyter-widgets/base", "_model_module_version": "1.2.0", "_model_name": "LayoutModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "LayoutView", "align_content": null, "align_items": null, "align_self": null, "border": null, "bottom": null, "display": null, "flex": null, "flex_flow": null, "grid_area": null, "grid_auto_columns": null, "grid_auto_flow": null, "grid_auto_rows": null, "grid_column": null, "grid_gap": null, "grid_row": null, "grid_template_areas": null, "grid_template_columns": null, "grid_template_rows": null, "height": null, "justify_content": null, "justify_items": null, "left": null, "margin": null, "max_height": null, "max_width": null, "min_height": null, "min_width": null, "object_fit": null, "object_position": null, "order": null, "overflow": null, "overflow_x": null, "overflow_y": null, "padding": null, "right": null, "top": null, "visibility": null, "width": null } }, "51e33ef50f6f48e1b6c7d3da4ade7819": { "model_module": "@jupyter-widgets/controls", "model_name": "ProgressStyleModel", "model_module_version": "1.5.0", "state": { "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "ProgressStyleModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "StyleView", "bar_color": null, "description_width": "" } }, "16ba2159038e4dafac854a238854cc93": { "model_module": "@jupyter-widgets/base", "model_name": "LayoutModel", "model_module_version": "1.2.0", "state": { "_model_module": "@jupyter-widgets/base", "_model_module_version": "1.2.0", "_model_name": "LayoutModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "LayoutView", "align_content": null, "align_items": null, "align_self": null, "border": null, "bottom": null, "display": null, "flex": null, "flex_flow": null, "grid_area": null, "grid_auto_columns": null, "grid_auto_flow": null, "grid_auto_rows": null, "grid_column": null, "grid_gap": null, "grid_row": null, "grid_template_areas": null, "grid_template_columns": null, "grid_template_rows": null, "height": null, "justify_content": null, "justify_items": null, "left": null, "margin": null, "max_height": null, "max_width": null, "min_height": null, "min_width": null, "object_fit": null, "object_position": null, "order": null, "overflow": null, "overflow_x": null, "overflow_y": null, "padding": null, "right": null, "top": null, "visibility": null, "width": null } }, "b15a25c37e714190a8a3032ce16d8944": { "model_module": "@jupyter-widgets/controls", "model_name": "DescriptionStyleModel", "model_module_version": "1.5.0", "state": { "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "DescriptionStyleModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "StyleView", "description_width": "" } } } } }, "cells": [ { "cell_type": "code", "execution_count": 2, "metadata": { "id": "qNGi-NeEQKyh" }, "outputs": [], "source": [ "!pip install -q accelerate==0.21.0 peft==0.4.0 bitsandbytes==0.40.0 transformers==4.31.0 trl==0.4.7\n" ] }, { "cell_type": "code", "source": [ "# import Libraries" ], "metadata": { "id": "WauAa05-Qjvm" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "import os\n", "import torch\n", "from datasets import load_dataset\n", "from transformers import (\n", " AutoModelForCausalLM,\n", " AutoTokenizer,\n", " BitsAndBytesConfig,\n", " TrainingArguments,\n", " pipeline,\n", " logging,\n", ")\n", "from peft import LoraConfig\n", "from trl import SFTTrainer" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "kHnoSTdZQchZ", "outputId": "cb02da43-ae5a-4077-d4ae-73f3757a23f3" }, "execution_count": 3, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "\n", "===================================BUG REPORT===================================\n", "Welcome to bitsandbytes. For bug reports, please run\n", "\n", "python -m bitsandbytes\n", "\n", " and submit this information together with your error trace to: https://github.com/TimDettmers/bitsandbytes/issues\n", "================================================================================\n", "bin /usr/local/lib/python3.10/dist-packages/bitsandbytes/libbitsandbytes_cuda122.so\n", "CUDA_SETUP: WARNING! libcudart.so not found in any environmental path. Searching in backup paths...\n", "CUDA SETUP: CUDA runtime path found: /usr/local/cuda/lib64/libcudart.so\n", "CUDA SETUP: Highest compute capability among GPUs detected: 7.5\n", "CUDA SETUP: Detected CUDA version 122\n", "CUDA SETUP: Loading binary /usr/local/lib/python3.10/dist-packages/bitsandbytes/libbitsandbytes_cuda122.so...\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "/usr/local/lib/python3.10/dist-packages/bitsandbytes/cuda_setup/main.py:149: UserWarning: /usr/lib64-nvidia did not contain ['libcudart.so', 'libcudart.so.11.0', 'libcudart.so.12.0'] as expected! Searching further paths...\n", " warn(msg)\n", "/usr/local/lib/python3.10/dist-packages/bitsandbytes/cuda_setup/main.py:149: UserWarning: WARNING: The following directories listed in your path were found to be non-existent: {PosixPath('/sys/fs/cgroup/memory.events /var/colab/cgroup/jupyter-children/memory.events')}\n", " warn(msg)\n", "/usr/local/lib/python3.10/dist-packages/bitsandbytes/cuda_setup/main.py:149: UserWarning: WARNING: The following directories listed in your path were found to be non-existent: {PosixPath('8013'), PosixPath('//172.28.0.1'), PosixPath('http')}\n", " warn(msg)\n", "/usr/local/lib/python3.10/dist-packages/bitsandbytes/cuda_setup/main.py:149: UserWarning: WARNING: The following directories listed in your path were found to be non-existent: {PosixPath('//colab.research.google.com/tun/m/cc48301118ce562b961b3c22d803539adc1e0c19/gpu-t4-s-2m1xgbra9q2mp --tunnel_background_save_delay=10s --tunnel_periodic_background_save_frequency=30m0s --enable_output_coalescing=true --output_coalescing_required=true'), PosixPath('--logtostderr --listen_host=172.28.0.12 --target_host=172.28.0.12 --tunnel_background_save_url=https')}\n", " warn(msg)\n", "/usr/local/lib/python3.10/dist-packages/bitsandbytes/cuda_setup/main.py:149: UserWarning: WARNING: The following directories listed in your path were found to be non-existent: {PosixPath('/datalab/web/pyright/typeshed-fallback/stdlib,/usr/local/lib/python3.10/dist-packages')}\n", " warn(msg)\n", "/usr/local/lib/python3.10/dist-packages/bitsandbytes/cuda_setup/main.py:149: UserWarning: WARNING: The following directories listed in your path were found to be non-existent: {PosixPath('/env/python')}\n", " warn(msg)\n", "/usr/local/lib/python3.10/dist-packages/bitsandbytes/cuda_setup/main.py:149: UserWarning: WARNING: The following directories listed in your path were found to be non-existent: {PosixPath('module'), PosixPath('//ipykernel.pylab.backend_inline')}\n", " warn(msg)\n" ] } ] }, { "cell_type": "code", "source": [ "# Model from Hugging Face hub\n", "base_model = \"NousResearch/Llama-2-7b-chat-hf\"\n", "\n", "# New instruction dataset\n", "data = \"mlabonne/guanaco-llama2-1k\"\n", "\n", "# Fine-tuned model\n", "new_model = \"llama\"" ], "metadata": { "id": "HK6v7rKIQc-N" }, "execution_count": 5, "outputs": [] }, { "cell_type": "code", "source": [ "#Loading dataset, model, and tokenizer" ], "metadata": { "id": "JwRCNdaCRCRg" }, "execution_count": 6, "outputs": [] }, { "cell_type": "code", "source": [ "dataset = load_dataset(data, split=\"train\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "RZxePacWQdAz", "outputId": "8d4d4a5b-08f2-4ae2-b053-bf8fbf36d0a2" }, "execution_count": 7, "outputs": [ { "output_type": "stream", "name": "stderr", "text": [ "/usr/local/lib/python3.10/dist-packages/huggingface_hub/utils/_token.py:88: UserWarning: \n", "The secret `HF_TOKEN` does not exist in your Colab secrets.\n", "To authenticate with the Hugging Face Hub, create a token in your settings tab (https://huggingface.co/settings/tokens), set it as secret in your Google Colab and restart your session.\n", "You will be able to reuse this secret in all of your notebooks.\n", "Please note that authentication is recommended but still optional to access public models or datasets.\n", " warnings.warn(\n" ] } ] }, { "cell_type": "code", "source": [ "#4-bit quantization configuration" ], "metadata": { "id": "h1Jyci0XQ7fv" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "compute_dtype = getattr(torch, \"float16\")\n", "\n", "quant_config = BitsAndBytesConfig(\n", " load_in_4bit=True,\n", " bnb_4bit_quant_type=\"nf4\",\n", " bnb_4bit_compute_dtype=compute_dtype,\n", " bnb_4bit_use_double_quant=False,\n", ")" ], "metadata": { "id": "CWdKioPXQdF2" }, "execution_count": 8, "outputs": [] }, { "cell_type": "code", "source": [ "#Loading Llama 2 model\n", "model = AutoModelForCausalLM.from_pretrained(\n", " base_model,\n", " quantization_config=quant_config,\n", " device_map={\"\": 0}\n", ")\n", "model.config.use_cache = False\n", "model.config.pretraining_tp = 1" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 49, "referenced_widgets": [ "cb82c295aa304cbab2c037caffc8d8d1", "67367bae31384088aa07fc80aa68a081", "ed5621e9730641379c6b1c740c43ec1f", "4c31c17d684d434b88481c42404d5582", "9e300fa0cd094325ac4ce2f1aa5cca8b", "2d58b05027644ae49d0d1c9b7a8c9f90", "0e9108eb5ada489a93719771ba9a8903", "8fea9e36d8a948c4a93e9613df9f9f48", "51e33ef50f6f48e1b6c7d3da4ade7819", "16ba2159038e4dafac854a238854cc93", "b15a25c37e714190a8a3032ce16d8944" ] }, "id": "yBKf8xyAQdIx", "outputId": "90423d17-f99a-425f-9696-1309b370ce62" }, "execution_count": 9, "outputs": [ { "output_type": "display_data", "data": { "text/plain": [ "Loading checkpoint shards: 0%| | 0/2 [00:00[INST] {prompt} [/INST]\")\n", "print(result[0]['generated_text'])" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "UiSr45NDQdrx", "outputId": "6c610f76-cd4f-4467-e1a3-a1a7cbe7de85" }, "execution_count": 16, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter prompt here:- sharad pawar\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "/usr/local/lib/python3.10/dist-packages/transformers/generation/utils.py:1270: UserWarning: You have modified the pretrained model configuration to control generation. This is a deprecated strategy to control generation and will be removed soon, in a future version. Please use a generation configuration file (see https://huggingface.co/docs/transformers/main_classes/text_generation )\n", " warnings.warn(\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "[INST] sharad pawar [/INST] Sharad Pawar is a prominent Indian politician and the current President of the Nationalist Congress Party (NCP). Here are some key facts about him:\n", " Unterscheidung:\n", "\n", "1. Early life and education: Sharad Pawar was born on December 12, 1940, in the village of Baramati in the state of Maharashtra, India. He completed his primary education in Baramati and later moved to Mumbai to pursue his higher education.\n", "2. Political career: Pawar began his political career in the 1960s as a member of the Janata Party. He later joined the Indian National Congress and became a member of the Maharashtra Legislative Assembly in 1978. He served as the Chief Minister of Maharashtra from 1978 to \n" ] } ] } ] }