From 9c6f82030f67ad1a65f5a2d957f47361660e60a4 Mon Sep 17 00:00:00 2001 From: Mihail Eric Date: Sat, 5 Oct 2024 00:35:47 -0700 Subject: [PATCH 1/6] log page contents in output dir --- benchmarks/retrieval/retrieve.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/benchmarks/retrieval/retrieve.py b/benchmarks/retrieval/retrieve.py index 7a809f5..cfb9f63 100644 --- a/benchmarks/retrieval/retrieve.py +++ b/benchmarks/retrieval/retrieve.py @@ -5,6 +5,7 @@ import json import logging import os +import pprint import time import configargparse @@ -81,13 +82,18 @@ def main(): retrieved = retriever.invoke(item[args.question_field]) item["retrieved"] = [] for doc_idx, doc in enumerate(retrieved): + pprint.pprint(doc) # The absolute value of the scores below does not affect the metrics; it merely determines the ranking of # the retrieved documents. The key of the score varies depending on the underlying retriever. If there's no # score, we use 1/(doc_idx+1) since it preserves the order of the documents. score = doc.metadata.get("score", doc.metadata.get("relevance_score", 1 / (doc_idx + 1))) retrieved_docs.append(ScoredDoc(query_id=query_id, doc_id=doc.metadata["file_path"], score=score)) # Update the output dictionary with the retrieved documents. - item["retrieved"].append({"file_path": doc.metadata["file_path"], "score": score}) + item["retrieved"].append({"file_path": doc.metadata["file_path"], + "score": score, + "page_content": doc.page_content, + "start_byte": doc.metadata.get("start_byte", None), + "end_byte": doc.metadata.get("end_byte", None)}) if "answer" in item: item.pop("answer") # Makes the output file harder to read. From 5cf208417916f121bb5b4f6bb0870ee5edae6cbd Mon Sep 17 00:00:00 2001 From: Mihail Eric Date: Mon, 7 Oct 2024 21:48:46 -0700 Subject: [PATCH 2/6] finetuning gpu --- sage/fine_tuning_code_llm_on_single_gpu.ipynb | 1657 +++++++++++++++++ 1 file changed, 1657 insertions(+) create mode 100644 sage/fine_tuning_code_llm_on_single_gpu.ipynb diff --git a/sage/fine_tuning_code_llm_on_single_gpu.ipynb b/sage/fine_tuning_code_llm_on_single_gpu.ipynb new file mode 100644 index 0000000..4f1d4ae --- /dev/null +++ b/sage/fine_tuning_code_llm_on_single_gpu.ipynb @@ -0,0 +1,1657 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "FNdZ-kD0l78P" + }, + "source": [ + "# Fine-tuning a Code LLM on Custom Code on a single GPU\n", + "\n", + "_Authored by: [Maria Khalusova](https://github.com/MKhalusova)_\n", + "\n", + "Publicly available code LLMs such as Codex, StarCoder, and Code Llama are great at generating code that adheres to general programming principles and syntax, but they may not align with an organization's internal conventions, or be aware of proprietary libraries.\n", + "\n", + "In this notebook, we'll see show how you can fine-tune a code LLM on private code bases to enhance its contextual awareness and improve a model's usefulness to your organization's needs. Since the code LLMs are quite large, fine-tuning them in a traditional manner can be resource-draining. Worry not! We will show how you can optimize fine-tuning to fit on a single GPU.\n", + "\n", + "\n", + "## Dataset\n", + "\n", + "For this example, we picked the top 10 Hugging Face public repositories on GitHub. We have excluded non-code files from the data, such as images, audio files, presentations, and so on. For Jupyter notebooks, we've kept only cells containing code. The resulting code is stored as a dataset that you can find on the Hugging Face Hub under [`smangrul/hf-stack-v1`](https://huggingface.co/datasets/smangrul/hf-stack-v1). It contains repo id, file path, and file content.\n", + "\n", + "\n", + "## Model\n", + "\n", + "We'll finetune [`bigcode/starcoderbase-1b`](https://huggingface.co/bigcode/starcoderbase-1b), which is a 1B parameter model trained on 80+ programming languages. This is a gated model, so if you plan to run this notebook with this exact model, you'll need to gain access to it on the model's page. Log in to your Hugging Face account to do so:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 331, + "referenced_widgets": [ + "65e4da277e6f4fc0bec087273ad6bce6", + "20f45ad1b5404dd1b35e2c81696e752d", + "fb6c47e03a37422ea81a09805d4742e4", + "6693fb7e2bc5433bba04c80b6eb40ad6", + "80305f2c23194f85ba0219856af7839a", + "8157516cdf984c1a9a09b05288df0b42", + "02ecdae95dcf47648b1898bddd68263d", + "e50a4cc3e2b14b0b8ca6cb0769a4ddb1", + "609e92a6fa5243c7ae35b07ef14f9cf5", + "e367a1e3164d4e13a4e5ad477e9ac625", + "a65cd74b61c2435ea23fc1cae68d7156", + "3e422885d1c24a38a5af8d50d9f2cbb7", + "83ec2e09b2954eb4a5561a1180ff2568", + "b111d185e6ed42c0beeaed4db567745c", + "982f0140aced4ac38f746f3f35f657d3", + "5770ef4c8d3f47d7b6e38d1c0e690cce", + "e82489e884464c8588447f74fd8fa97e" + ] + }, + "id": "bPlCJYDK6vrF", + "outputId": "f738649c-77dd-4daf-e19f-f09e44e2f4a6" + }, + "outputs": [ + { + "ename": "", + "evalue": "", + "output_type": "error", + "traceback": [ + "\u001b[1;31mRunning cells with 'sage-310 (Python 3.10.4)' requires the ipykernel package.\n", + "\u001b[1;31mRun the following command to install 'ipykernel' into the Python environment. \n", + "\u001b[1;31mCommand: 'conda install -n sage-310 ipykernel --update-deps --force-reinstall'" + ] + } + ], + "source": [ + "from huggingface_hub import notebook_login\n", + "\n", + "notebook_login()" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "WMVe_c8q43Qo" + }, + "source": [ + "To get started, let's install all the necessary libraries. As you can see, in addition to `transformers` and `datasets`, we'll be using `peft`, `bitsandbytes`, and `flash-attn` to optimize the training.\n", + "\n", + "By employing parameter-efficient training techniques, we can run this notebook on a single A100 High-RAM GPU." + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": { + "id": "Fp7i8WMCjKJG" + }, + "outputs": [], + "source": [ + "!pip install -q transformers datasets peft bitsandbytes flash-attn" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 36 + }, + "id": "Rhg5JgtC2WS-", + "outputId": "3628306f-5277-4e31-eb59-d12f9f48fa53" + }, + "outputs": [ + { + "data": { + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "string" + }, + "text/plain": [ + "'2.6.3'" + ] + }, + "execution_count": 39, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import flash_attn; flash_attn.__version__" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "16EdABzt3_Ig" + }, + "source": [ + "Let's define some variables now. Feel free to play with these." + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": { + "id": "hru3G-CLmqis" + }, + "outputs": [], + "source": [ + "MODEL=\"bigcode/starcoderbase-1b\" # Model checkpoint on the Hugging Face Hub\n", + "DATASET=\"smangrul/hf-stack-v1\" # Dataset on the Hugging Face Hub\n", + "DATA_COLUMN=\"content\" # Column name containing the code content\n", + "\n", + "SEQ_LENGTH=2048 # Sequence length\n", + "\n", + "# Training arguments\n", + "MAX_STEPS=2000 # max_steps\n", + "BATCH_SIZE=16 # batch_size\n", + "GR_ACC_STEPS=1 # gradient_accumulation_steps\n", + "LR=5e-4 # learning_rate\n", + "LR_SCHEDULER_TYPE=\"cosine\" # lr_scheduler_type\n", + "WEIGHT_DECAY=0.01 # weight_decay\n", + "NUM_WARMUP_STEPS=30 # num_warmup_steps\n", + "EVAL_FREQ=100 # eval_freq\n", + "SAVE_FREQ=100 # save_freq\n", + "LOG_FREQ=25 # log_freq\n", + "OUTPUT_DIR=\"peft-starcoder-lora-a100\" # output_dir\n", + "BF16=True # bf16\n", + "FP16=False # no_fp16\n", + "\n", + "# FIM trasformations arguments\n", + "FIM_RATE=0.5 # fim_rate\n", + "FIM_SPM_RATE=0.5 # fim_spm_rate\n", + "\n", + "# LORA\n", + "LORA_R=8 # lora_r\n", + "LORA_ALPHA=32 # lora_alpha\n", + "LORA_DROPOUT=0.0 # lora_dropout\n", + "LORA_TARGET_MODULES=\"c_proj,c_attn,q_attn,c_fc,c_proj\" # lora_target_modules\n", + "\n", + "# bitsandbytes config\n", + "USE_NESTED_QUANT=True # use_nested_quant\n", + "BNB_4BIT_COMPUTE_DTYPE=\"bfloat16\"# bnb_4bit_compute_dtype\n", + "\n", + "SEED=0" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": { + "id": "FyZSXTbJrcnC" + }, + "outputs": [], + "source": [ + "from transformers import (\n", + " AutoModelForCausalLM,\n", + " AutoTokenizer,\n", + " Trainer,\n", + " TrainingArguments,\n", + " logging,\n", + " set_seed,\n", + " BitsAndBytesConfig,\n", + ")\n", + "\n", + "set_seed(SEED)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "pO7F5L5AtKo1" + }, + "source": [ + "## Prepare the data" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "1LmrIZqP0oUE" + }, + "source": [ + "Begin by loading the data. As the dataset is likely to be quite large, make sure to enable the streaming mode. Streaming allows us to load the data progressively as we iterate over the dataset instead of downloading the whole dataset at once.\n", + "\n", + "We'll reserve the first 4000 examples as the validation set, and everything else will be the training data." + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": { + "id": "4oJZvZb-1J88" + }, + "outputs": [], + "source": [ + "from datasets import load_dataset\n", + "import torch\n", + "from tqdm import tqdm\n", + "\n", + "\n", + "dataset = load_dataset(\n", + " DATASET,\n", + " data_dir=\"data\",\n", + " split=\"train\",\n", + " streaming=True,\n", + ")\n", + "\n", + "valid_data = dataset.take(4000)\n", + "train_data = dataset.skip(4000)\n", + "train_data = train_data.shuffle(buffer_size=5000, seed=SEED)" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "rliIfosCs89r", + "outputId": "a245647b-f957-483c-b858-f661a0dd80da" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "DatasetInfo(description='', citation='', homepage='', license='', features={'repo_id': Value(dtype='string', id=None), 'file_path': Value(dtype='string', id=None), 'content': Value(dtype='string', id=None), '__index_level_0__': Value(dtype='int64', id=None)}, post_processed=None, supervised_keys=None, builder_name='parquet', dataset_name='hf-stack-v1', config_name='default', version=0.0.0, splits={'train': SplitInfo(name='train', num_bytes=91907731, num_examples=5905, shard_lengths=None, dataset_name=None)}, download_checksums=None, download_size=30589828, post_processing_size=None, dataset_size=91907731, size_in_bytes=None)" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "train_data.info" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "sLQ8t0LM2GR6" + }, + "source": [ + "At this step, the dataset still contains raw data with code of arbitraty length. For training, we need inputs of fixed length. Let's create an Iterable dataset that would return constant-length chunks of tokens from a stream of text files.\n", + "\n", + "First, let's estimate the average number of characters per token in the dataset, which will help us later estimate the number of tokens in the text buffer later. By default, we'll only take 400 examples (`nb_examples`) from the dataset. Using only a subset of the entire dataset will reduce computational cost while still providing a reasonable estimate of the overall character-to-token ratio." + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "KCiAvydztNsu", + "outputId": "e60f04c4-afc8-45d9-929c-f0ea193af93d" + }, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/usr/local/lib/python3.10/dist-packages/transformers/tokenization_utils_base.py:1601: FutureWarning: `clean_up_tokenization_spaces` was not set. It will be set to `True` by default. This behavior will be depracted in transformers v4.45, and will be then set to `False` by default. For more details check this issue: https://github.com/huggingface/transformers/issues/31884\n", + " warnings.warn(\n", + "100%|██████████| 400/400 [00:13<00:00, 29.38it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The character to token ratio of the dataset is: 2.43\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "\n" + ] + } + ], + "source": [ + "tokenizer = AutoTokenizer.from_pretrained(MODEL, trust_remote_code=True)\n", + "\n", + "def chars_token_ratio(dataset, tokenizer, data_column, nb_examples=400):\n", + " \"\"\"\n", + " Estimate the average number of characters per token in the dataset.\n", + " \"\"\"\n", + "\n", + " total_characters, total_tokens = 0, 0\n", + " for _, example in tqdm(zip(range(nb_examples), iter(dataset)), total=nb_examples):\n", + " total_characters += len(example[data_column])\n", + " total_tokens += len(tokenizer(example[data_column]).tokens())\n", + "\n", + " return total_characters / total_tokens\n", + "\n", + "\n", + "chars_per_token = chars_token_ratio(train_data, tokenizer, DATA_COLUMN)\n", + "print(f\"The character to token ratio of the dataset is: {chars_per_token:.2f}\")" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "6F13VGobB3Ma" + }, + "source": [ + "The character-to-token ratio can also be used as an indicator of the quality of text tokenization. For instance, a character-to-token ratio of 1.0 would mean that each character is represented with a token, which is not very meaningful. This would indicate poor tokenization. In standard English text, one token is typically equivalent to approximately four characters, meaning the character-to-token ratio is around 4.0. We can expect a lower ratio in the code dataset, but generally speaking, a number between 2.0 and 3.5 can be considered good enough." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "rcwYFRPpwxea" + }, + "source": [ + "**Optional FIM transformations**\n", + "\n", + "\n", + "Autoregressive language models typically generate sequences from left to right. By applying the FIM transformations, the model can also learn to infill text. Check out [\"Efficient Training of Language Models to Fill in the Middle\" paper](https://arxiv.org/pdf/2207.14255.pdf) to learn more about the technique.\n", + "We'll define the FIM transformations here and will use them when creating the Iterable Dataset. However, if you want to omit transformations, feel free to set `fim_rate` to 0." + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": { + "id": "zmejYvEKw1E-" + }, + "outputs": [], + "source": [ + "import functools\n", + "import numpy as np\n", + "\n", + "\n", + "# Helper function to get token ids of the special tokens for prefix, suffix and middle for FIM transformations.\n", + "@functools.lru_cache(maxsize=None)\n", + "def get_fim_token_ids(tokenizer):\n", + " try:\n", + " FIM_PREFIX, FIM_MIDDLE, FIM_SUFFIX, FIM_PAD = tokenizer.special_tokens_map[\"additional_special_tokens\"][1:5]\n", + " suffix_tok_id, prefix_tok_id, middle_tok_id, pad_tok_id = (\n", + " tokenizer.vocab[tok] for tok in [FIM_SUFFIX, FIM_PREFIX, FIM_MIDDLE, FIM_PAD]\n", + " )\n", + " except KeyError:\n", + " suffix_tok_id, prefix_tok_id, middle_tok_id, pad_tok_id = None, None, None, None\n", + " return suffix_tok_id, prefix_tok_id, middle_tok_id, pad_tok_id\n", + "\n", + "\n", + "## Adapted from https://github.com/bigcode-project/Megatron-LM/blob/6c4bf908df8fd86b4977f54bf5b8bd4b521003d1/megatron/data/gpt_dataset.py\n", + "def permute(\n", + " sample,\n", + " np_rng,\n", + " suffix_tok_id,\n", + " prefix_tok_id,\n", + " middle_tok_id,\n", + " pad_tok_id,\n", + " fim_rate=0.5,\n", + " fim_spm_rate=0.5,\n", + " truncate_or_pad=False,\n", + "):\n", + " \"\"\"\n", + " Take in a sample (list of tokens) and perform a FIM transformation on it with a probability of fim_rate, using two FIM modes:\n", + " PSM and SPM (with a probability of fim_spm_rate).\n", + " \"\"\"\n", + "\n", + " # The if condition will trigger with the probability of fim_rate\n", + " # This means FIM transformations will apply to samples with a probability of fim_rate\n", + " if np_rng.binomial(1, fim_rate):\n", + "\n", + " # Split the sample into prefix, middle, and suffix, based on randomly generated indices stored in the boundaries list.\n", + " boundaries = list(np_rng.randint(low=0, high=len(sample) + 1, size=2))\n", + " boundaries.sort()\n", + "\n", + " prefix = np.array(sample[: boundaries[0]], dtype=np.int64)\n", + " middle = np.array(sample[boundaries[0] : boundaries[1]], dtype=np.int64)\n", + " suffix = np.array(sample[boundaries[1] :], dtype=np.int64)\n", + "\n", + " if truncate_or_pad:\n", + " # calculate the new total length of the sample, taking into account tokens indicating prefix, middle, and suffix\n", + " new_length = suffix.shape[0] + prefix.shape[0] + middle.shape[0] + 3\n", + " diff = new_length - len(sample)\n", + "\n", + " # trancate or pad if there's a difference in length between the new length and the original\n", + " if diff > 0:\n", + " if suffix.shape[0] <= diff:\n", + " return sample, np_rng\n", + " suffix = suffix[: suffix.shape[0] - diff]\n", + " elif diff < 0:\n", + " suffix = np.concatenate([suffix, np.full((-1 * diff), pad_tok_id)])\n", + "\n", + " # With the probability of fim_spm_rateapply SPM variant of FIM transformations\n", + " # SPM: suffix, prefix, middle\n", + " if np_rng.binomial(1, fim_spm_rate):\n", + " new_sample = np.concatenate(\n", + " [\n", + " [prefix_tok_id, suffix_tok_id],\n", + " suffix,\n", + " [middle_tok_id],\n", + " prefix,\n", + " middle,\n", + " ]\n", + " )\n", + " # Otherwise, apply the PSM variant of FIM transformations\n", + " # PSM: prefix, suffix, middle\n", + " else:\n", + "\n", + " new_sample = np.concatenate(\n", + " [\n", + " [prefix_tok_id],\n", + " prefix,\n", + " [suffix_tok_id],\n", + " suffix,\n", + " [middle_tok_id],\n", + " middle,\n", + " ]\n", + " )\n", + " else:\n", + " # don't apply FIM transformations\n", + " new_sample = sample\n", + "\n", + " return list(new_sample), np_rng\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "AwW5FviD9xBH" + }, + "source": [ + "Let's define the `ConstantLengthDataset`, an Iterable dataset that will return constant-length chunks of tokens. To do so, we'll read a buffer of text from the original dataset until we hit the size limits and then apply tokenizer to convert the raw text into tokenized inputs. Optionally, we'll perform FIM transformations on some sequences (the proportion of sequences affected is controlled by `fim_rate`).\n", + "\n", + "Once defined, we can create instances of the `ConstantLengthDataset` from both training and validation data." + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": { + "id": "AgDW-692wzOl" + }, + "outputs": [], + "source": [ + "from torch.utils.data import IterableDataset\n", + "from torch.utils.data.dataloader import DataLoader\n", + "import random\n", + "\n", + "# Create an Iterable dataset that returns constant-length chunks of tokens from a stream of text files.\n", + "\n", + "class ConstantLengthDataset(IterableDataset):\n", + " \"\"\"\n", + " Iterable dataset that returns constant length chunks of tokens from stream of text files.\n", + " Args:\n", + " tokenizer (Tokenizer): The processor used for proccessing the data.\n", + " dataset (dataset.Dataset): Dataset with text files.\n", + " infinite (bool): If True the iterator is reset after dataset reaches end else stops.\n", + " seq_length (int): Length of token sequences to return.\n", + " num_of_sequences (int): Number of token sequences to keep in buffer.\n", + " chars_per_token (int): Number of characters per token used to estimate number of tokens in text buffer.\n", + " fim_rate (float): Rate (0.0 to 1.0) that sample will be permuted with FIM.\n", + " fim_spm_rate (float): Rate (0.0 to 1.0) of FIM permuations that will use SPM.\n", + " seed (int): Seed for random number generator.\n", + " \"\"\"\n", + "\n", + " def __init__(\n", + " self,\n", + " tokenizer,\n", + " dataset,\n", + " infinite=False,\n", + " seq_length=1024,\n", + " num_of_sequences=1024,\n", + " chars_per_token=3.6,\n", + " content_field=\"content\",\n", + " fim_rate=0.5,\n", + " fim_spm_rate=0.5,\n", + " seed=0,\n", + " ):\n", + " self.tokenizer = tokenizer\n", + " self.concat_token_id = tokenizer.eos_token_id\n", + " self.dataset = dataset\n", + " self.seq_length = seq_length\n", + " self.infinite = infinite\n", + " self.current_size = 0\n", + " self.max_buffer_size = seq_length * chars_per_token * num_of_sequences\n", + " self.content_field = content_field\n", + " self.fim_rate = fim_rate\n", + " self.fim_spm_rate = fim_spm_rate\n", + " self.seed = seed\n", + "\n", + " (\n", + " self.suffix_tok_id,\n", + " self.prefix_tok_id,\n", + " self.middle_tok_id,\n", + " self.pad_tok_id,\n", + " ) = get_fim_token_ids(self.tokenizer)\n", + " if not self.suffix_tok_id and self.fim_rate > 0:\n", + " print(\"FIM is not supported by tokenizer, disabling FIM\")\n", + " self.fim_rate = 0\n", + "\n", + " def __iter__(self):\n", + " iterator = iter(self.dataset)\n", + " more_examples = True\n", + " np_rng = np.random.RandomState(seed=self.seed)\n", + " while more_examples:\n", + " buffer, buffer_len = [], 0\n", + " while True:\n", + " if buffer_len >= self.max_buffer_size:\n", + " break\n", + " try:\n", + " buffer.append(next(iterator)[self.content_field])\n", + " buffer_len += len(buffer[-1])\n", + " except StopIteration:\n", + " if self.infinite:\n", + " iterator = iter(self.dataset)\n", + " else:\n", + " more_examples = False\n", + " break\n", + " tokenized_inputs = self.tokenizer(buffer, truncation=False)[\"input_ids\"]\n", + " all_token_ids = []\n", + "\n", + " for tokenized_input in tokenized_inputs:\n", + " # optionally do FIM permutations\n", + " if self.fim_rate > 0:\n", + " tokenized_input, np_rng = permute(\n", + " tokenized_input,\n", + " np_rng,\n", + " self.suffix_tok_id,\n", + " self.prefix_tok_id,\n", + " self.middle_tok_id,\n", + " self.pad_tok_id,\n", + " fim_rate=self.fim_rate,\n", + " fim_spm_rate=self.fim_spm_rate,\n", + " truncate_or_pad=False,\n", + " )\n", + "\n", + " all_token_ids.extend(tokenized_input + [self.concat_token_id])\n", + " examples = []\n", + " for i in range(0, len(all_token_ids), self.seq_length):\n", + " input_ids = all_token_ids[i : i + self.seq_length]\n", + " if len(input_ids) == self.seq_length:\n", + " examples.append(input_ids)\n", + " random.shuffle(examples)\n", + " for example in examples:\n", + " self.current_size += 1\n", + " yield {\n", + " \"input_ids\": torch.LongTensor(example),\n", + " \"labels\": torch.LongTensor(example),\n", + " }\n", + "\n", + "\n", + "train_dataset = ConstantLengthDataset(\n", + " tokenizer,\n", + " train_data,\n", + " infinite=True,\n", + " seq_length=SEQ_LENGTH,\n", + " chars_per_token=chars_per_token,\n", + " content_field=DATA_COLUMN,\n", + " fim_rate=FIM_RATE,\n", + " fim_spm_rate=FIM_SPM_RATE,\n", + " seed=SEED,\n", + ")\n", + "eval_dataset = ConstantLengthDataset(\n", + " tokenizer,\n", + " valid_data,\n", + " infinite=False,\n", + " seq_length=SEQ_LENGTH,\n", + " chars_per_token=chars_per_token,\n", + " content_field=DATA_COLUMN,\n", + " fim_rate=FIM_RATE,\n", + " fim_spm_rate=FIM_SPM_RATE,\n", + " seed=SEED,\n", + ")" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "rxev1sk6tRW9" + }, + "source": [ + "## Prepare the model" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "UCtWV-U42Eq_" + }, + "source": [ + "Now that the data is prepared, it's time to load the model! We're going to load the quantized version of the model.\n", + "\n", + "This will allow us to reduce memory usage, as quantization represents data with fewer bits. We'll use the `bitsandbytes` library to quantize the model, as it has a nice integration with `transformers`. All we need to do is define a `bitsandbytes` config, and then use it when loading the model.\n", + "\n", + "There are different variants of 4bit quantization, but generally, we recommend using NF4 quantization for better performance (`bnb_4bit_quant_type=\"nf4\"`).\n", + "\n", + "The `bnb_4bit_use_double_quant` option adds a second quantization after the first one to save an additional 0.4 bits per parameter.\n", + "\n", + "To learn more about quantization, check out the [\"Making LLMs even more accessible with bitsandbytes, 4-bit quantization and QLoRA\" blog post](https://huggingface.co/blog/4bit-transformers-bitsandbytes).\n", + "\n", + "Once defined, pass the config to the `from_pretrained` method to load the quantized version of the model." + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": { + "id": "XuwoX6U2DUvK" + }, + "outputs": [], + "source": [ + "from peft import LoraConfig, get_peft_model, prepare_model_for_kbit_training\n", + "from peft.tuners.lora import LoraLayer\n", + "\n", + "load_in_8bit = False\n", + "\n", + "# 4-bit quantization\n", + "compute_dtype = getattr(torch, BNB_4BIT_COMPUTE_DTYPE)\n", + "\n", + "bnb_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=USE_NESTED_QUANT,\n", + ")\n", + "\n", + "device_map = {\"\": 0}\n", + "\n", + "model = AutoModelForCausalLM.from_pretrained(\n", + " MODEL,\n", + " load_in_8bit=load_in_8bit,\n", + " quantization_config=bnb_config,\n", + " device_map=device_map,\n", + " use_cache=False, # We will be using gradient checkpointing\n", + " trust_remote_code=True,\n", + " use_flash_attention_2=True,\n", + ")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "bO9e2FV8D8ZF" + }, + "source": [ + "When using a quantized model for training, you need to call the `prepare_model_for_kbit_training()` function to preprocess the quantized model for training." + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": { + "id": "Qb_eB4xzEDBk" + }, + "outputs": [], + "source": [ + "model = prepare_model_for_kbit_training(model)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "lmnLjPZpDVtg" + }, + "source": [ + "Now that the quantized model is ready, we can set up a LoRA configuration. LoRA makes fine-tuning more efficient by drastically reducing the number of trainable parameters.\n", + "\n", + "To train a model using LoRA technique, we need to wrap the base model as a `PeftModel`. This involves definign LoRA configuration with `LoraConfig`, and wrapping the original model with `get_peft_model()` using the `LoraConfig`.\n", + "\n", + "To learn more about LoRA and its parameters, refer to [PEFT documentation](https://huggingface.co/docs/peft/main/en/conceptual_guides/lora)." + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "_pAUU2FR2Gey", + "outputId": "f25de148-955f-452d-d81e-fbd7e028da26" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "trainable params: 5,554,176 || all params: 1,142,761,472 || trainable%: 0.4860\n" + ] + } + ], + "source": [ + "# Set up lora\n", + "peft_config = LoraConfig(\n", + " lora_alpha=LORA_ALPHA,\n", + " lora_dropout=LORA_DROPOUT,\n", + " r=LORA_R,\n", + " bias=\"none\",\n", + " task_type=\"CAUSAL_LM\",\n", + " target_modules=LORA_TARGET_MODULES.split(\",\"),\n", + ")\n", + "\n", + "model = get_peft_model(model, peft_config)\n", + "model.print_trainable_parameters()" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "tHe7AElXzXVV" + }, + "source": [ + "As you can see, by applying LoRA technique we will now need to train less than 1% of the parameters." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "T_CqVydc40IM" + }, + "source": [ + "## Train the model" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Q_iN2khjrbD3" + }, + "source": [ + "Now that we have prepared the data, and optimized the model, we are ready to bring everything together to start the training.\n", + "\n", + "To instantiate a `Trainer`, you need to define the training configuration. The most important is the `TrainingArguments`, which is a class that contains all the attributes to configure the training.\n", + "\n", + "These are similar to any other kind of model training you may run, so we won't go into detail here." + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "65QHS8l1tKQe", + "outputId": "8e1a2c40-9470-4563-f217-3c734a106e03" + }, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/usr/local/lib/python3.10/dist-packages/transformers/training_args.py:1525: FutureWarning: `evaluation_strategy` is deprecated and will be removed in version 4.46 of 🤗 Transformers. Use `eval_strategy` instead\n", + " warnings.warn(\n" + ] + } + ], + "source": [ + "train_data.start_iteration = 0\n", + "\n", + "\n", + "training_args = TrainingArguments(\n", + " output_dir=f\"Your_HF_username/{OUTPUT_DIR}\",\n", + " dataloader_drop_last=True,\n", + " evaluation_strategy=\"steps\",\n", + " save_strategy=\"steps\",\n", + " max_steps=MAX_STEPS,\n", + " eval_steps=EVAL_FREQ,\n", + " save_steps=SAVE_FREQ,\n", + " logging_steps=LOG_FREQ,\n", + " per_device_train_batch_size=BATCH_SIZE,\n", + " per_device_eval_batch_size=BATCH_SIZE,\n", + " learning_rate=LR,\n", + " lr_scheduler_type=LR_SCHEDULER_TYPE,\n", + " warmup_steps=NUM_WARMUP_STEPS,\n", + " gradient_accumulation_steps=GR_ACC_STEPS,\n", + " gradient_checkpointing=True,\n", + " fp16=FP16,\n", + " bf16=BF16,\n", + " weight_decay=WEIGHT_DECAY,\n", + " push_to_hub=True,\n", + " include_tokens_per_second=True,\n", + ")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "WwiWGK430KM3", + "outputId": "c735b480-7c7f-4775-9f16-2c42d88e0790" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 34, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "model._supports_flash_attn_2" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "kB_fLRex09ut" + }, + "source": [ + "As a final step, instantiate the `Trainer` and call the `train` method. " + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 446 + }, + "id": "rS3nVwhUC69O", + "outputId": "bc5f6d1d-7260-4c68-f325-885742b0d284" + }, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "max_steps is given, it will override any value given in num_train_epochs\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Training...\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/eval_frame.py:600: UserWarning: torch.utils.checkpoint: the use_reentrant parameter should be passed explicitly. In version 2.4 we will raise an exception if use_reentrant is not passed. use_reentrant=False is recommended, but if you need to preserve the current default behavior, you can pass use_reentrant=True. Refer to docs for more details on the differences between the two variants.\n", + " return fn(*args, **kwargs)\n", + "The input hidden states seems to be silently casted in float32, this might be related to the fact you have upcasted embedding or layer norm layers in float32. We will cast back the input in torch.bfloat16.\n" + ] + }, + { + "ename": "RuntimeError", + "evalue": "FlashAttention only supports Ampere GPUs or newer.", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mRuntimeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Training...\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 6\u001b[0;31m \u001b[0mtrainer\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtrain\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/transformers/trainer.py\u001b[0m in \u001b[0;36mtrain\u001b[0;34m(self, resume_from_checkpoint, trial, ignore_keys_for_eval, **kwargs)\u001b[0m\n\u001b[1;32m 1927\u001b[0m \u001b[0;31m# Disable progress bars when uploading models during checkpoints to avoid polluting stdout\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1928\u001b[0m \u001b[0mhf_hub_utils\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdisable_progress_bars\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1929\u001b[0;31m return inner_training_loop(\n\u001b[0m\u001b[1;32m 1930\u001b[0m \u001b[0margs\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1931\u001b[0m \u001b[0mresume_from_checkpoint\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mresume_from_checkpoint\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/transformers/trainer.py\u001b[0m in \u001b[0;36m_inner_training_loop\u001b[0;34m(self, batch_size, args, resume_from_checkpoint, trial, ignore_keys_for_eval)\u001b[0m\n\u001b[1;32m 2277\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2278\u001b[0m \u001b[0;32mwith\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0maccelerator\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0maccumulate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmodel\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2279\u001b[0;31m \u001b[0mtr_loss_step\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtraining_step\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmodel\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minputs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2280\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2281\u001b[0m if (\n", + "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/transformers/trainer.py\u001b[0m in \u001b[0;36mtraining_step\u001b[0;34m(self, model, inputs)\u001b[0m\n\u001b[1;32m 3316\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3317\u001b[0m \u001b[0;32mwith\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcompute_loss_context_manager\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 3318\u001b[0;31m \u001b[0mloss\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcompute_loss\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmodel\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minputs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3319\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3320\u001b[0m \u001b[0;32mdel\u001b[0m \u001b[0minputs\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/transformers/trainer.py\u001b[0m in \u001b[0;36mcompute_loss\u001b[0;34m(self, model, inputs, return_outputs)\u001b[0m\n\u001b[1;32m 3361\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3362\u001b[0m \u001b[0mlabels\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 3363\u001b[0;31m \u001b[0moutputs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmodel\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m**\u001b[0m\u001b[0minputs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3364\u001b[0m \u001b[0;31m# Save past state if it exists\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3365\u001b[0m \u001b[0;31m# TODO: this needs to be fixed and made cleaner later.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py\u001b[0m in \u001b[0;36m_wrapped_call_impl\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 1551\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_compiled_call_impl\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# type: ignore[misc]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1552\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1553\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_call_impl\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1554\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1555\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m_call_impl\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py\u001b[0m in \u001b[0;36m_call_impl\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 1560\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0m_global_backward_pre_hooks\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0m_global_backward_hooks\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1561\u001b[0m or _global_forward_hooks or _global_forward_pre_hooks):\n\u001b[0;32m-> 1562\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mforward_call\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1563\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1564\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/accelerate/utils/operations.py\u001b[0m in \u001b[0;36mforward\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 818\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 819\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mforward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 820\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mmodel_forward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 821\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 822\u001b[0m \u001b[0;31m# To act like a decorator so that it can be popped when doing `extract_model_from_parallel`\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/accelerate/utils/operations.py\u001b[0m in \u001b[0;36m__call__\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 806\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 807\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m__call__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 808\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mconvert_to_fp32\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmodel_forward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 809\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 810\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m__getstate__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/torch/amp/autocast_mode.py\u001b[0m in \u001b[0;36mdecorate_autocast\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 41\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mdecorate_autocast\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 42\u001b[0m \u001b[0;32mwith\u001b[0m \u001b[0mautocast_instance\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 43\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mfunc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 44\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 45\u001b[0m \u001b[0mdecorate_autocast\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__script_unsupported\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"@autocast() decorator is not supported in script mode\"\u001b[0m \u001b[0;31m# type: ignore[attr-defined]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/peft/peft_model.py\u001b[0m in \u001b[0;36mforward\u001b[0;34m(self, input_ids, attention_mask, inputs_embeds, labels, output_attentions, output_hidden_states, return_dict, task_ids, **kwargs)\u001b[0m\n\u001b[1;32m 1642\u001b[0m \u001b[0;32mwith\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_enable_peft_forward_hooks\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1643\u001b[0m \u001b[0mkwargs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m{\u001b[0m\u001b[0mk\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mv\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mk\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mv\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mkwargs\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mitems\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mk\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mspecial_peft_forward_args\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1644\u001b[0;31m return self.base_model(\n\u001b[0m\u001b[1;32m 1645\u001b[0m \u001b[0minput_ids\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0minput_ids\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1646\u001b[0m \u001b[0mattention_mask\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mattention_mask\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py\u001b[0m in \u001b[0;36m_wrapped_call_impl\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 1551\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_compiled_call_impl\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# type: ignore[misc]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1552\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1553\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_call_impl\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1554\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1555\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m_call_impl\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py\u001b[0m in \u001b[0;36m_call_impl\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 1560\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0m_global_backward_pre_hooks\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0m_global_backward_hooks\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1561\u001b[0m or _global_forward_hooks or _global_forward_pre_hooks):\n\u001b[0;32m-> 1562\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mforward_call\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1563\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1564\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/peft/tuners/tuners_utils.py\u001b[0m in \u001b[0;36mforward\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 195\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 196\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mforward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mAny\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mAny\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 197\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmodel\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mforward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 198\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 199\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m_pre_injection_hook\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmodel\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mnn\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mModule\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mconfig\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mPeftConfig\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0madapter_name\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mstr\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m->\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/accelerate/hooks.py\u001b[0m in \u001b[0;36mnew_forward\u001b[0;34m(module, *args, **kwargs)\u001b[0m\n\u001b[1;32m 168\u001b[0m \u001b[0moutput\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmodule\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_old_forward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 169\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 170\u001b[0;31m \u001b[0moutput\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmodule\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_old_forward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 171\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mmodule\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_hf_hook\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpost_forward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmodule\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0moutput\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 172\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/transformers/models/gpt_bigcode/modeling_gpt_bigcode.py\u001b[0m in \u001b[0;36mforward\u001b[0;34m(self, input_ids, past_key_values, attention_mask, token_type_ids, position_ids, head_mask, inputs_embeds, encoder_hidden_states, encoder_attention_mask, labels, use_cache, output_attentions, output_hidden_states, return_dict)\u001b[0m\n\u001b[1;32m 1156\u001b[0m \u001b[0mreturn_dict\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mreturn_dict\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mreturn_dict\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32mNone\u001b[0m \u001b[0;32melse\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mconfig\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0muse_return_dict\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1157\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1158\u001b[0;31m transformer_outputs = self.transformer(\n\u001b[0m\u001b[1;32m 1159\u001b[0m \u001b[0minput_ids\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1160\u001b[0m \u001b[0mpast_key_values\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mpast_key_values\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py\u001b[0m in \u001b[0;36m_wrapped_call_impl\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 1551\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_compiled_call_impl\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# type: ignore[misc]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1552\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1553\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_call_impl\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1554\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1555\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m_call_impl\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py\u001b[0m in \u001b[0;36m_call_impl\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 1560\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0m_global_backward_pre_hooks\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0m_global_backward_hooks\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1561\u001b[0m or _global_forward_hooks or _global_forward_pre_hooks):\n\u001b[0;32m-> 1562\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mforward_call\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1563\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1564\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/accelerate/hooks.py\u001b[0m in \u001b[0;36mnew_forward\u001b[0;34m(module, *args, **kwargs)\u001b[0m\n\u001b[1;32m 168\u001b[0m \u001b[0moutput\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmodule\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_old_forward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 169\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 170\u001b[0;31m \u001b[0moutput\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmodule\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_old_forward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 171\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mmodule\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_hf_hook\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpost_forward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmodule\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0moutput\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 172\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/transformers/models/gpt_bigcode/modeling_gpt_bigcode.py\u001b[0m in \u001b[0;36mforward\u001b[0;34m(self, input_ids, past_key_values, attention_mask, token_type_ids, position_ids, head_mask, inputs_embeds, encoder_hidden_states, encoder_attention_mask, use_cache, output_attentions, output_hidden_states, return_dict)\u001b[0m\n\u001b[1;32m 979\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 980\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgradient_checkpointing\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtraining\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 981\u001b[0;31m outputs = self._gradient_checkpointing_func(\n\u001b[0m\u001b[1;32m 982\u001b[0m \u001b[0mblock\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__call__\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 983\u001b[0m \u001b[0mhidden_states\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/torch/_compile.py\u001b[0m in \u001b[0;36minner\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 29\u001b[0m \u001b[0mfn\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__dynamo_disable\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdisable_fn\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 30\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 31\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mdisable_fn\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 32\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 33\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0minner\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/torch/_dynamo/eval_frame.py\u001b[0m in \u001b[0;36m_fn\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 598\u001b[0m \u001b[0mprior\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mset_eval_frame\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcallback\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 599\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 600\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mfn\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 601\u001b[0m \u001b[0;32mfinally\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 602\u001b[0m \u001b[0mset_eval_frame\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mprior\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/torch/utils/checkpoint.py\u001b[0m in \u001b[0;36mcheckpoint\u001b[0;34m(function, use_reentrant, context_fn, determinism_check, debug, *args, **kwargs)\u001b[0m\n\u001b[1;32m 479\u001b[0m \u001b[0;34m\"use_reentrant=False.\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 480\u001b[0m )\n\u001b[0;32m--> 481\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mCheckpointFunction\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mapply\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfunction\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mpreserve\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 482\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 483\u001b[0m gen = _checkpoint_without_reentrant_generator(\n", + "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/torch/autograd/function.py\u001b[0m in \u001b[0;36mapply\u001b[0;34m(cls, *args, **kwargs)\u001b[0m\n\u001b[1;32m 572\u001b[0m \u001b[0;31m# See NOTE: [functorch vjp and autograd interaction]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 573\u001b[0m \u001b[0margs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0m_functorch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mutils\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0munwrap_dead_wrappers\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 574\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0msuper\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mapply\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# type: ignore[misc]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 575\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 576\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mis_setup_ctx_defined\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/torch/utils/checkpoint.py\u001b[0m in \u001b[0;36mforward\u001b[0;34m(ctx, run_function, preserve_rng_state, *args)\u001b[0m\n\u001b[1;32m 253\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 254\u001b[0m \u001b[0;32mwith\u001b[0m \u001b[0mtorch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mno_grad\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 255\u001b[0;31m \u001b[0moutputs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mrun_function\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 256\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0moutputs\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 257\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py\u001b[0m in \u001b[0;36m_wrapped_call_impl\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 1551\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_compiled_call_impl\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# type: ignore[misc]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1552\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1553\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_call_impl\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1554\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1555\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m_call_impl\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py\u001b[0m in \u001b[0;36m_call_impl\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 1560\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0m_global_backward_pre_hooks\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0m_global_backward_hooks\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1561\u001b[0m or _global_forward_hooks or _global_forward_pre_hooks):\n\u001b[0;32m-> 1562\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mforward_call\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1563\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1564\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/accelerate/hooks.py\u001b[0m in \u001b[0;36mnew_forward\u001b[0;34m(module, *args, **kwargs)\u001b[0m\n\u001b[1;32m 168\u001b[0m \u001b[0moutput\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmodule\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_old_forward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 169\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 170\u001b[0;31m \u001b[0moutput\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmodule\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_old_forward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 171\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mmodule\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_hf_hook\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpost_forward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmodule\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0moutput\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 172\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/transformers/models/gpt_bigcode/modeling_gpt_bigcode.py\u001b[0m in \u001b[0;36mforward\u001b[0;34m(self, hidden_states, layer_past, attention_mask, head_mask, encoder_hidden_states, encoder_attention_mask, use_cache, output_attentions, **kwargs)\u001b[0m\n\u001b[1;32m 606\u001b[0m \u001b[0mresidual\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mhidden_states\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 607\u001b[0m \u001b[0mhidden_states\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mln_1\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mhidden_states\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 608\u001b[0;31m attn_outputs = self.attn(\n\u001b[0m\u001b[1;32m 609\u001b[0m \u001b[0mhidden_states\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 610\u001b[0m \u001b[0mlayer_past\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mlayer_past\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py\u001b[0m in \u001b[0;36m_wrapped_call_impl\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 1551\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_compiled_call_impl\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# type: ignore[misc]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1552\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1553\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_call_impl\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1554\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1555\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m_call_impl\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py\u001b[0m in \u001b[0;36m_call_impl\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 1560\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0m_global_backward_pre_hooks\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0m_global_backward_hooks\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1561\u001b[0m or _global_forward_hooks or _global_forward_pre_hooks):\n\u001b[0;32m-> 1562\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mforward_call\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1563\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1564\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/accelerate/hooks.py\u001b[0m in \u001b[0;36mnew_forward\u001b[0;34m(module, *args, **kwargs)\u001b[0m\n\u001b[1;32m 168\u001b[0m \u001b[0moutput\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmodule\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_old_forward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 169\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 170\u001b[0;31m \u001b[0moutput\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmodule\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_old_forward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 171\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mmodule\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_hf_hook\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpost_forward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmodule\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0moutput\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 172\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/transformers/models/gpt_bigcode/modeling_gpt_bigcode.py\u001b[0m in \u001b[0;36mforward\u001b[0;34m(self, hidden_states, layer_past, attention_mask, head_mask, encoder_hidden_states, encoder_attention_mask, use_cache, output_attentions)\u001b[0m\n\u001b[1;32m 368\u001b[0m \u001b[0mvalue\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mvalue\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mto\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtarget_dtype\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 369\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 370\u001b[0;31m attn_output = _flash_attention_forward(\n\u001b[0m\u001b[1;32m 371\u001b[0m \u001b[0mquery\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 372\u001b[0m \u001b[0mkey\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/transformers/modeling_flash_attention_utils.py\u001b[0m in \u001b[0;36m_flash_attention_forward\u001b[0;34m(query_states, key_states, value_states, attention_mask, query_length, is_causal, dropout, position_ids, softmax_scale, sliding_window, use_top_left_mask, softcap, deterministic)\u001b[0m\n\u001b[1;32m 294\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 295\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 296\u001b[0;31m attn_output = flash_attn_func(\n\u001b[0m\u001b[1;32m 297\u001b[0m \u001b[0mquery_states\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkey_states\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mvalue_states\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdropout\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msoftmax_scale\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0msoftmax_scale\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcausal\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcausal\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mflash_kwargs\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 298\u001b[0m )\n", + "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/flash_attn/flash_attn_interface.py\u001b[0m in \u001b[0;36mflash_attn_func\u001b[0;34m(q, k, v, dropout_p, softmax_scale, causal, window_size, softcap, alibi_slopes, deterministic, return_attn_probs)\u001b[0m\n\u001b[1;32m 878\u001b[0m \u001b[0mpattern\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mnegative\u001b[0m \u001b[0mmeans\u001b[0m \u001b[0mthat\u001b[0m \u001b[0mlocation\u001b[0m \u001b[0mwas\u001b[0m \u001b[0mdropped\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnonnegative\u001b[0m \u001b[0mmeans\u001b[0m \u001b[0mit\u001b[0m \u001b[0mwas\u001b[0m \u001b[0mkept\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 879\u001b[0m \"\"\"\n\u001b[0;32m--> 880\u001b[0;31m return FlashAttnFunc.apply(\n\u001b[0m\u001b[1;32m 881\u001b[0m \u001b[0mq\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 882\u001b[0m \u001b[0mk\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/torch/autograd/function.py\u001b[0m in \u001b[0;36mapply\u001b[0;34m(cls, *args, **kwargs)\u001b[0m\n\u001b[1;32m 572\u001b[0m \u001b[0;31m# See NOTE: [functorch vjp and autograd interaction]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 573\u001b[0m \u001b[0margs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0m_functorch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mutils\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0munwrap_dead_wrappers\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 574\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0msuper\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mapply\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# type: ignore[misc]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 575\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 576\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mis_setup_ctx_defined\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/flash_attn/flash_attn_interface.py\u001b[0m in \u001b[0;36mforward\u001b[0;34m(ctx, q, k, v, dropout_p, softmax_scale, causal, window_size, softcap, alibi_slopes, deterministic, return_softmax)\u001b[0m\n\u001b[1;32m 544\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0msoftmax_scale\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 545\u001b[0m \u001b[0msoftmax_scale\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mq\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m**\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m0.5\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 546\u001b[0;31m out, q, k, v, out_padded, softmax_lse, S_dmask, rng_state = _flash_attn_forward(\n\u001b[0m\u001b[1;32m 547\u001b[0m \u001b[0mq\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 548\u001b[0m \u001b[0mk\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/flash_attn/flash_attn_interface.py\u001b[0m in \u001b[0;36m_flash_attn_forward\u001b[0;34m(q, k, v, dropout_p, softmax_scale, causal, window_size, softcap, alibi_slopes, return_softmax)\u001b[0m\n\u001b[1;32m 50\u001b[0m ):\n\u001b[1;32m 51\u001b[0m \u001b[0mq\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mk\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mv\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mmaybe_contiguous\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mx\u001b[0m \u001b[0;32min\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mq\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mk\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mv\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 52\u001b[0;31m out, q, k, v, out_padded, softmax_lse, S_dmask, rng_state = flash_attn_cuda.fwd(\n\u001b[0m\u001b[1;32m 53\u001b[0m \u001b[0mq\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 54\u001b[0m \u001b[0mk\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mRuntimeError\u001b[0m: FlashAttention only supports Ampere GPUs or newer." + ] + } + ], + "source": [ + "trainer = Trainer(\n", + " model=model, args=training_args, train_dataset=train_dataset, eval_dataset=eval_dataset\n", + ")\n", + "\n", + "print(\"Training...\")\n", + "trainer.train()\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "aAERlCnt1PEW" + }, + "source": [ + "Finally, you can push the fine-tuned model to your Hub repository to share with your team." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "1h7_AUTTDwE1" + }, + "outputs": [], + "source": [ + "trainer.push_to_hub()" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "KBVH7uFOM_UF" + }, + "source": [ + "## Inference\n", + "\n", + "Once the model is uploaded to Hub, we can use it for inference. To do so we first initialize the original base model and its tokenizer. Next, we need to merge the fine-duned weights with the base model." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "jtL37piINBFe" + }, + "outputs": [], + "source": [ + "from peft import PeftModel\n", + "import torch\n", + "\n", + "# load the original model first\n", + "tokenizer = AutoTokenizer.from_pretrained(MODEL, trust_remote_code=True)\n", + "base_model = AutoModelForCausalLM.from_pretrained(\n", + " MODEL,\n", + " quantization_config=None,\n", + " device_map=None,\n", + " trust_remote_code=True,\n", + " torch_dtype=torch.bfloat16,\n", + ").cuda()\n", + "\n", + "# merge fine-tuned weights with the base model\n", + "peft_model_id = f\"Your_HF_username/{OUTPUT_DIR}\"\n", + "model = PeftModel.from_pretrained(base_model, peft_model_id)\n", + "model.merge_and_unload()" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "3USQ2suvDi9M" + }, + "source": [ + "Now we can use the merged model for inference. For convenience, we'll define a `get_code_completion` - feel free to experiment with text generation parameters!" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "RoTGpNbjDeWI" + }, + "outputs": [], + "source": [ + "def get_code_completion(prefix, suffix):\n", + " text = prompt = f\"\"\"{prefix}{suffix}\"\"\"\n", + " model.eval()\n", + " outputs = model.generate(\n", + " input_ids=tokenizer(text, return_tensors=\"pt\").input_ids.cuda(),\n", + " max_new_tokens=128,\n", + " temperature=0.2,\n", + " top_k=50,\n", + " top_p=0.95,\n", + " do_sample=True,\n", + " repetition_penalty=1.0,\n", + " )\n", + " return tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "0kMJiGDfDrBf" + }, + "source": [ + "Now all we need to do to get code completion is call the `get_code_complete` function and pass the first few lines that we want to be completed as a prefix, and an empty string as a suffix." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "nXlco2_-YcvM" + }, + "outputs": [], + "source": [ + "prefix = \"\"\"from peft import LoraConfig, TaskType, get_peft_model\n", + "from transformers import AutoModelForCausalLM\n", + "peft_config = LoraConfig(\n", + "\"\"\"\n", + "suffix =\"\"\"\"\"\"\n", + "\n", + "print(get_code_completion(prefix, suffix))" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Ql2563kGlnmu" + }, + "source": [ + "As someone who has just used the PEFT library earlier in this notebook, you can see that the generated result for creating a `LoraConfig` is rather good!\n", + "\n", + "If you go back to the cell where we instantiate the model for inference, and comment out the lines where we merge the fine-tuned weights, you can see what the original model would've generated for the exact same prefix:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "29xxp1eHTgJ9" + }, + "outputs": [], + "source": [ + "prefix = \"\"\"from peft import LoraConfig, TaskType, get_peft_model\n", + "from transformers import AutoModelForCausalLM\n", + "peft_config = LoraConfig(\n", + "\"\"\"\n", + "suffix =\"\"\"\"\"\"\n", + "\n", + "print(get_code_completion(prefix, suffix))" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Pwy2ZC7U8Ema" + }, + "source": [ + "While it is Python syntax, you can see that the original model has no understanding of what a `LoraConfig` should be doing." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "CATYE8pp2drQ" + }, + "source": [ + "To learn how this kind of fine-tuning compares to full fine-tuning, and how to use a model like this as your copilot in VS Code via Inference Endpoints, or locally, check out the [\"Personal Copilot: Train Your Own Coding Assistant\" blog post](https://huggingface.co/blog/personal-copilot). This notebook complements the original blog post.\n" + ] + } + ], + "metadata": { + "accelerator": "GPU", + "colab": { + "gpuType": "T4", + "machine_shape": "hm", + "provenance": [] + }, + "kernelspec": { + "display_name": "Python 3", + "name": "python3" + }, + "language_info": { + "name": "python", + "version": "3.10.4" + }, + "widgets": { + "application/vnd.jupyter.widget-state+json": { + "02ecdae95dcf47648b1898bddd68263d": { + "model_module": "@jupyter-widgets/base", + "model_module_version": "1.2.0", + "model_name": "LayoutModel", + "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": "center", + "align_self": null, + "border": null, + "bottom": null, + "display": "flex", + "flex": null, + "flex_flow": "column", + "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": "50%" + } + }, + "20f45ad1b5404dd1b35e2c81696e752d": { + "model_module": "@jupyter-widgets/controls", + "model_module_version": "1.5.0", + "model_name": "HTMLModel", + "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_e50a4cc3e2b14b0b8ca6cb0769a4ddb1", + "placeholder": "​", + "style": "IPY_MODEL_609e92a6fa5243c7ae35b07ef14f9cf5", + "value": "

Copy a token from your Hugging Face\ntokens page and paste it below.
Immediately click login after copying\nyour token or it might be stored in plain text in this notebook file.
" + } + }, + "3e422885d1c24a38a5af8d50d9f2cbb7": { + "model_module": "@jupyter-widgets/base", + "model_module_version": "1.2.0", + "model_name": "LayoutModel", + "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 + } + }, + "5770ef4c8d3f47d7b6e38d1c0e690cce": { + "model_module": "@jupyter-widgets/base", + "model_module_version": "1.2.0", + "model_name": "LayoutModel", + "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 + } + }, + "609e92a6fa5243c7ae35b07ef14f9cf5": { + "model_module": "@jupyter-widgets/controls", + "model_module_version": "1.5.0", + "model_name": "DescriptionStyleModel", + "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": "" + } + }, + "65e4da277e6f4fc0bec087273ad6bce6": { + "model_module": "@jupyter-widgets/controls", + "model_module_version": "1.5.0", + "model_name": "VBoxModel", + "state": { + "_dom_classes": [], + "_model_module": "@jupyter-widgets/controls", + "_model_module_version": "1.5.0", + "_model_name": "VBoxModel", + "_view_count": null, + "_view_module": "@jupyter-widgets/controls", + "_view_module_version": "1.5.0", + "_view_name": "VBoxView", + "box_style": "", + "children": [ + "IPY_MODEL_20f45ad1b5404dd1b35e2c81696e752d", + "IPY_MODEL_fb6c47e03a37422ea81a09805d4742e4", + "IPY_MODEL_6693fb7e2bc5433bba04c80b6eb40ad6", + "IPY_MODEL_80305f2c23194f85ba0219856af7839a", + "IPY_MODEL_8157516cdf984c1a9a09b05288df0b42" + ], + "layout": "IPY_MODEL_02ecdae95dcf47648b1898bddd68263d" + } + }, + "6693fb7e2bc5433bba04c80b6eb40ad6": { + "model_module": "@jupyter-widgets/controls", + "model_module_version": "1.5.0", + "model_name": "CheckboxModel", + "state": { + "_dom_classes": [], + "_model_module": "@jupyter-widgets/controls", + "_model_module_version": "1.5.0", + "_model_name": "CheckboxModel", + "_view_count": null, + "_view_module": "@jupyter-widgets/controls", + "_view_module_version": "1.5.0", + "_view_name": "CheckboxView", + "description": "Add token as git credential?", + "description_tooltip": null, + "disabled": false, + "indent": true, + "layout": "IPY_MODEL_3e422885d1c24a38a5af8d50d9f2cbb7", + "style": "IPY_MODEL_83ec2e09b2954eb4a5561a1180ff2568", + "value": true + } + }, + "80305f2c23194f85ba0219856af7839a": { + "model_module": "@jupyter-widgets/controls", + "model_module_version": "1.5.0", + "model_name": "ButtonModel", + "state": { + "_dom_classes": [], + "_model_module": "@jupyter-widgets/controls", + "_model_module_version": "1.5.0", + "_model_name": "ButtonModel", + "_view_count": null, + "_view_module": "@jupyter-widgets/controls", + "_view_module_version": "1.5.0", + "_view_name": "ButtonView", + "button_style": "", + "description": "Login", + "disabled": false, + "icon": "", + "layout": "IPY_MODEL_b111d185e6ed42c0beeaed4db567745c", + "style": "IPY_MODEL_982f0140aced4ac38f746f3f35f657d3", + "tooltip": "" + } + }, + "8157516cdf984c1a9a09b05288df0b42": { + "model_module": "@jupyter-widgets/controls", + "model_module_version": "1.5.0", + "model_name": "HTMLModel", + "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_5770ef4c8d3f47d7b6e38d1c0e690cce", + "placeholder": "​", + "style": "IPY_MODEL_e82489e884464c8588447f74fd8fa97e", + "value": "\nPro Tip: If you don't already have one, you can create a dedicated\n'notebooks' token with 'write' access, that you can then easily reuse for all\nnotebooks. " + } + }, + "83ec2e09b2954eb4a5561a1180ff2568": { + "model_module": "@jupyter-widgets/controls", + "model_module_version": "1.5.0", + "model_name": "DescriptionStyleModel", + "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": "" + } + }, + "982f0140aced4ac38f746f3f35f657d3": { + "model_module": "@jupyter-widgets/controls", + "model_module_version": "1.5.0", + "model_name": "ButtonStyleModel", + "state": { + "_model_module": "@jupyter-widgets/controls", + "_model_module_version": "1.5.0", + "_model_name": "ButtonStyleModel", + "_view_count": null, + "_view_module": "@jupyter-widgets/base", + "_view_module_version": "1.2.0", + "_view_name": "StyleView", + "button_color": null, + "font_weight": "" + } + }, + "a65cd74b61c2435ea23fc1cae68d7156": { + "model_module": "@jupyter-widgets/controls", + "model_module_version": "1.5.0", + "model_name": "DescriptionStyleModel", + "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": "" + } + }, + "b111d185e6ed42c0beeaed4db567745c": { + "model_module": "@jupyter-widgets/base", + "model_module_version": "1.2.0", + "model_name": "LayoutModel", + "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 + } + }, + "e367a1e3164d4e13a4e5ad477e9ac625": { + "model_module": "@jupyter-widgets/base", + "model_module_version": "1.2.0", + "model_name": "LayoutModel", + "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 + } + }, + "e50a4cc3e2b14b0b8ca6cb0769a4ddb1": { + "model_module": "@jupyter-widgets/base", + "model_module_version": "1.2.0", + "model_name": "LayoutModel", + "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 + } + }, + "e82489e884464c8588447f74fd8fa97e": { + "model_module": "@jupyter-widgets/controls", + "model_module_version": "1.5.0", + "model_name": "DescriptionStyleModel", + "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": "" + } + }, + "fb6c47e03a37422ea81a09805d4742e4": { + "model_module": "@jupyter-widgets/controls", + "model_module_version": "1.5.0", + "model_name": "PasswordModel", + "state": { + "_dom_classes": [], + "_model_module": "@jupyter-widgets/controls", + "_model_module_version": "1.5.0", + "_model_name": "PasswordModel", + "_view_count": null, + "_view_module": "@jupyter-widgets/controls", + "_view_module_version": "1.5.0", + "_view_name": "PasswordView", + "continuous_update": true, + "description": "Token:", + "description_tooltip": null, + "disabled": false, + "layout": "IPY_MODEL_e367a1e3164d4e13a4e5ad477e9ac625", + "placeholder": "​", + "style": "IPY_MODEL_a65cd74b61c2435ea23fc1cae68d7156", + "value": "" + } + } + } + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} From 8dd62442699500c0e941a467ea232f65e1102222 Mon Sep 17 00:00:00 2001 From: Mihail Eric Date: Wed, 9 Oct 2024 02:02:58 +0000 Subject: [PATCH 3/6] partial refactor --- sage/fine_tuning_code_llm_on_single_gpu.py | 564 +++++++++++++++++++++ 1 file changed, 564 insertions(+) create mode 100644 sage/fine_tuning_code_llm_on_single_gpu.py diff --git a/sage/fine_tuning_code_llm_on_single_gpu.py b/sage/fine_tuning_code_llm_on_single_gpu.py new file mode 100644 index 0000000..07f9fcc --- /dev/null +++ b/sage/fine_tuning_code_llm_on_single_gpu.py @@ -0,0 +1,564 @@ +#!/usr/bin/env python +# coding: utf-8 + +# # Fine-tuning a Code LLM on Custom Code on a single GPU +# +# _Authored by: [Maria Khalusova](https://github.com/MKhalusova)_ +# +# Publicly available code LLMs such as Codex, StarCoder, and Code Llama are great at generating code that adheres to general programming principles and syntax, but they may not align with an organization's internal conventions, or be aware of proprietary libraries. +# +# In this notebook, we'll see show how you can fine-tune a code LLM on private code bases to enhance its contextual awareness and improve a model's usefulness to your organization's needs. Since the code LLMs are quite large, fine-tuning them in a traditional manner can be resource-draining. Worry not! We will show how you can optimize fine-tuning to fit on a single GPU. +# +# +# ## Dataset +# +# For this example, we picked the top 10 Hugging Face public repositories on GitHub. We have excluded non-code files from the data, such as images, audio files, presentations, and so on. For Jupyter notebooks, we've kept only cells containing code. The resulting code is stored as a dataset that you can find on the Hugging Face Hub under [`smangrul/hf-stack-v1`](https://huggingface.co/datasets/smangrul/hf-stack-v1). It contains repo id, file path, and file content. +# +# +# ## Model +# +# We'll finetune [`bigcode/starcoderbase-1b`](https://huggingface.co/bigcode/starcoderbase-1b), which is a 1B parameter model trained on 80+ programming languages. This is a gated model, so if you plan to run this notebook with this exact model, you'll need to gain access to it on the model's page. Log in to your Hugging Face account to do so: + + +import argparse +import os +import flash_attn +import argparse +import torch +import functools +import numpy as np +import random + +from datasets import load_dataset +from tqdm import tqdm +from huggingface_hub import HfApi, HfFolder +from huggingface_hub import notebook_login +from torch.utils.data import IterableDataset +from torch.utils.data.dataloader import DataLoader +from transformers import ( + AutoModelForCausalLM, + AutoTokenizer, + Trainer, + TrainingArguments, + logging, + set_seed, + BitsAndBytesConfig, +) + + +parser = argparse.ArgumentParser() +parser.add_argument("--model", type=str, default="bigcode/starcoderbase-1b", help="Model checkpoint on the Hugging Face Hub") +parser.add_argument("--dataset", type=str, default="smangrul/hf-stack-v1", help="Dataset on the Hugging Face Hub") +parser.add_argument("--data_column", type=str, default="content", help="Column name containing the code content") +parser.add_argument("--seq_length", type=int, default=2048, help="Sequence length") +parser.add_argument("--max_steps", type=int, default=2000, help="Max training steps") +parser.add_argument("--batch_size", type=int, default=16, help="Batch size") +parser.add_argument("--gr_acc_steps", type=int, default=1, help="Gradient accumulation steps") +parser.add_argument("--lr", type=float, default=5e-4, help="Learning rate") +parser.add_argument("--lr_scheduler_type", type=str, default="cosine", help="Learning rate scheduler type") +parser.add_argument("--weight_decay", type=float, default=0.01, help="Weight decay") +parser.add_argument("--num_warmup_steps", type=int, default=30, help="Number of warmup steps") +parser.add_argument("--eval_freq", type=int, default=10, help="Evaluation frequency") +parser.add_argument("--save_freq", type=int, default=100, help="Save frequency") +parser.add_argument("--log_freq", type=int, default=25, help="Logging frequency") +parser.add_argument("--output_dir", type=str, default="peft-starcoder-lora-a100", help="Output directory") +parser.add_argument("--bf16", action="store_true", help="Use bfloat16 precision") +parser.add_argument("--no_fp16", dest="fp16", action="store_false", help="Disable fp16 precision") +parser.add_argument("--fim_rate", type=float, default=0.5, help="FIM rate") +parser.add_argument("--fim_spm_rate", type=float, default=0.5, help="FIM SPM rate") +parser.add_argument("--lora_r", type=int, default=8, help="LoRA r") +parser.add_argument("--lora_alpha", type=int, default=32, help="LoRA alpha") +parser.add_argument("--lora_dropout", type=float, default=0.0, help="LoRA dropout") +parser.add_argument("--lora_target_modules", type=str, default="c_proj,c_attn,q_attn,c_fc,c_proj", help="LoRA target modules") +parser.add_argument("--use_nested_quant", action="store_true", help="Use nested quantization") +parser.add_argument("--bnb_4bit_compute_dtype", type=str, default="bfloat16", help="bitsandbytes 4bit compute dtype") +parser.add_argument("--seed", type=int, default=0, help="Random seed") +parser.add_argument('--wandb_key', type=str, required=True, help='wandb api key') +parser.add_argument('--hf_token', type=str, required=True, help='huggingface hub token') +parser.add_argument('--run_name', type=str, default='starcoderbase-1b-finetuned', help='run name for wandb') +args = parser.parse_args() + +run_name = args.run_name + + +def login_to_huggingface_hub(token): + """ + Log in to the Hugging Face Hub programmatically. + + :param token: Your Hugging Face API token + """ + # Set the token as an environment variable + os.environ["HUGGING_FACE_HUB_TOKEN"] = token + + # Set the token in the Hugging Face folder + HfFolder.save_token(token) + + # Initialize the Hugging Face API + api = HfApi() + + # Verify the token by trying to get user info + try: + user_info = api.whoami() + print(f"Successfully logged in as: {user_info['name']}") + except Exception as e: + print(f"Login failed: {str(e)}") + +def chars_token_ratio(dataset, tokenizer, data_column, nb_examples=400): + """ + Estimate the average number of characters per token in the dataset. + """ + total_characters, total_tokens = 0, 0 + for _, example in tqdm(zip(range(nb_examples), iter(dataset)), total=nb_examples): + total_characters += len(example[data_column]) + total_tokens += len(tokenizer(example[data_column]).tokens()) + + return total_characters / total_tokens + + +def get_code_completion(prefix, suffix): + text = prompt = f"""{prefix}{suffix}""" + model.eval() + outputs = model.generate( + input_ids=tokenizer(text, return_tensors="pt").input_ids.cuda(), + max_new_tokens=128, + temperature=0.2, + top_k=50, + top_p=0.95, + do_sample=True, + repetition_penalty=1.0, + ) + return tokenizer.batch_decode(outputs, skip_special_tokens=True)[0] + + +login_to_huggingface_hub(args.hf_token) +set_seed(SEED) + +# Wandb configuration +import wandb +wandb.login() + + +dataset = load_dataset( + DATASET, + data_dir="data", + split="train", + streaming=True, +) + +valid_data = dataset.take(400) +train_data = dataset.skip(400).take(400) +train_data = train_data.shuffle(buffer_size=5000, seed=SEED) + + + +train_data.info +tokenizer = AutoTokenizer.from_pretrained(MODEL, trust_remote_code=True) + + + +chars_per_token = chars_token_ratio(train_data, tokenizer, DATA_COLUMN) +# print(f"The character to token ratio of the dataset is: {chars_per_token:.2f}") + + + + + +# Helper function to get token ids of the special tokens for prefix, suffix and middle for FIM transformations. +@functools.lru_cache(maxsize=None) +def get_fim_token_ids(tokenizer): + try: + FIM_PREFIX, FIM_MIDDLE, FIM_SUFFIX, FIM_PAD = tokenizer.special_tokens_map["additional_special_tokens"][1:5] + suffix_tok_id, prefix_tok_id, middle_tok_id, pad_tok_id = ( + tokenizer.vocab[tok] for tok in [FIM_SUFFIX, FIM_PREFIX, FIM_MIDDLE, FIM_PAD] + ) + except KeyError: + suffix_tok_id, prefix_tok_id, middle_tok_id, pad_tok_id = None, None, None, None + return suffix_tok_id, prefix_tok_id, middle_tok_id, pad_tok_id + + +## Adapted from https://github.com/bigcode-project/Megatron-LM/blob/6c4bf908df8fd86b4977f54bf5b8bd4b521003d1/megatron/data/gpt_dataset.py +def permute( + sample, + np_rng, + suffix_tok_id, + prefix_tok_id, + middle_tok_id, + pad_tok_id, + fim_rate=0.5, + fim_spm_rate=0.5, + truncate_or_pad=False, +): + """ + Take in a sample (list of tokens) and perform a FIM transformation on it with a probability of fim_rate, using two FIM modes: + PSM and SPM (with a probability of fim_spm_rate). + """ + + # The if condition will trigger with the probability of fim_rate + # This means FIM transformations will apply to samples with a probability of fim_rate + if np_rng.binomial(1, fim_rate): + + # Split the sample into prefix, middle, and suffix, based on randomly generated indices stored in the boundaries list. + boundaries = list(np_rng.randint(low=0, high=len(sample) + 1, size=2)) + boundaries.sort() + + prefix = np.array(sample[: boundaries[0]], dtype=np.int64) + middle = np.array(sample[boundaries[0] : boundaries[1]], dtype=np.int64) + suffix = np.array(sample[boundaries[1] :], dtype=np.int64) + + if truncate_or_pad: + # calculate the new total length of the sample, taking into account tokens indicating prefix, middle, and suffix + new_length = suffix.shape[0] + prefix.shape[0] + middle.shape[0] + 3 + diff = new_length - len(sample) + + # trancate or pad if there's a difference in length between the new length and the original + if diff > 0: + if suffix.shape[0] <= diff: + return sample, np_rng + suffix = suffix[: suffix.shape[0] - diff] + elif diff < 0: + suffix = np.concatenate([suffix, np.full((-1 * diff), pad_tok_id)]) + + # With the probability of fim_spm_rateapply SPM variant of FIM transformations + # SPM: suffix, prefix, middle + if np_rng.binomial(1, fim_spm_rate): + new_sample = np.concatenate( + [ + [prefix_tok_id, suffix_tok_id], + suffix, + [middle_tok_id], + prefix, + middle, + ] + ) + # Otherwise, apply the PSM variant of FIM transformations + # PSM: prefix, suffix, middle + else: + + new_sample = np.concatenate( + [ + [prefix_tok_id], + prefix, + [suffix_tok_id], + suffix, + [middle_tok_id], + middle, + ] + ) + else: + # don't apply FIM transformations + new_sample = sample + + return list(new_sample), np_rng + + +# Let's define the `ConstantLengthDataset`, an Iterable dataset that will return constant-length chunks of tokens. To do so, we'll read a buffer of text from the original dataset until we hit the size limits and then apply tokenizer to convert the raw text into tokenized inputs. Optionally, we'll perform FIM transformations on some sequences (the proportion of sequences affected is controlled by `fim_rate`). +# Once defined, we can create instances of the `ConstantLengthDataset` from both training and validation data. + + + + + +# Create an Iterable dataset that returns constant-length chunks of tokens from a stream of text files. + +class ConstantLengthDataset(IterableDataset): + """ + Iterable dataset that returns constant length chunks of tokens from stream of text files. + Args: + tokenizer (Tokenizer): The processor used for proccessing the data. + dataset (dataset.Dataset): Dataset with text files. + infinite (bool): If True the iterator is reset after dataset reaches end else stops. + seq_length (int): Length of token sequences to return. + num_of_sequences (int): Number of token sequences to keep in buffer. + chars_per_token (int): Number of characters per token used to estimate number of tokens in text buffer. + fim_rate (float): Rate (0.0 to 1.0) that sample will be permuted with FIM. + fim_spm_rate (float): Rate (0.0 to 1.0) of FIM permuations that will use SPM. + seed (int): Seed for random number generator. + """ + + def __init__( + self, + tokenizer, + dataset, + infinite=False, + seq_length=1024, + num_of_sequences=1024, + chars_per_token=3.6, + content_field="content", + fim_rate=0.5, + fim_spm_rate=0.5, + seed=0, + ): + self.tokenizer = tokenizer + self.concat_token_id = tokenizer.eos_token_id + self.dataset = dataset + self.seq_length = seq_length + self.infinite = infinite + self.current_size = 0 + self.max_buffer_size = seq_length * chars_per_token * num_of_sequences + self.content_field = content_field + self.fim_rate = fim_rate + self.fim_spm_rate = fim_spm_rate + self.seed = seed + + ( + self.suffix_tok_id, + self.prefix_tok_id, + self.middle_tok_id, + self.pad_tok_id, + ) = get_fim_token_ids(self.tokenizer) + if not self.suffix_tok_id and self.fim_rate > 0: + print("FIM is not supported by tokenizer, disabling FIM") + self.fim_rate = 0 + + def __iter__(self): + iterator = iter(self.dataset) + more_examples = True + np_rng = np.random.RandomState(seed=self.seed) + while more_examples: + buffer, buffer_len = [], 0 + while True: + if buffer_len >= self.max_buffer_size: + break + try: + buffer.append(next(iterator)[self.content_field]) + buffer_len += len(buffer[-1]) + except StopIteration: + if self.infinite: + iterator = iter(self.dataset) + else: + more_examples = False + break + tokenized_inputs = self.tokenizer(buffer, truncation=False)["input_ids"] + all_token_ids = [] + + for tokenized_input in tokenized_inputs: + # optionally do FIM permutations + if self.fim_rate > 0: + tokenized_input, np_rng = permute( + tokenized_input, + np_rng, + self.suffix_tok_id, + self.prefix_tok_id, + self.middle_tok_id, + self.pad_tok_id, + fim_rate=self.fim_rate, + fim_spm_rate=self.fim_spm_rate, + truncate_or_pad=False, + ) + + all_token_ids.extend(tokenized_input + [self.concat_token_id]) + examples = [] + for i in range(0, len(all_token_ids), self.seq_length): + input_ids = all_token_ids[i : i + self.seq_length] + if len(input_ids) == self.seq_length: + examples.append(input_ids) + random.shuffle(examples) + for example in examples: + self.current_size += 1 + yield { + "input_ids": torch.LongTensor(example), + "labels": torch.LongTensor(example), + } + + +train_dataset = ConstantLengthDataset( + tokenizer, + train_data, + infinite=True, + seq_length=SEQ_LENGTH, + chars_per_token=chars_per_token, + content_field=DATA_COLUMN, + fim_rate=FIM_RATE, + fim_spm_rate=FIM_SPM_RATE, + seed=SEED, +) +eval_dataset = ConstantLengthDataset( + tokenizer, + valid_data, + infinite=False, + seq_length=SEQ_LENGTH, + chars_per_token=chars_per_token, + content_field=DATA_COLUMN, + fim_rate=FIM_RATE, + fim_spm_rate=FIM_SPM_RATE, + seed=SEED, +) + +# Dump dataset objs to disk +import pickle +with open('train_dataset.pkl', 'wb') as f: + pickle.dump(train_dataset, f) + +with open('eval_dataset.pkl', 'wb') as f: + pickle.dump(eval_dataset, f) + + +# ## Prepare the model + +# Now that the data is prepared, it's time to load the model! We're going to load the quantized version of the model. +# +# This will allow us to reduce memory usage, as quantization represents data with fewer bits. We'll use the `bitsandbytes` library to quantize the model, as it has a nice integration with `transformers`. All we need to do is define a `bitsandbytes` config, and then use it when loading the model. +# +# There are different variants of 4bit quantization, but generally, we recommend using NF4 quantization for better performance (`bnb_4bit_quant_type="nf4"`). +# +# The `bnb_4bit_use_double_quant` option adds a second quantization after the first one to save an additional 0.4 bits per parameter. +# +# To learn more about quantization, check out the ["Making LLMs even more accessible with bitsandbytes, 4-bit quantization and QLoRA" blog post](https://huggingface.co/blog/4bit-transformers-bitsandbytes). +# +# Once defined, pass the config to the `from_pretrained` method to load the quantized version of the model. + +# In[ ]: + + +from peft import LoraConfig, get_peft_model, prepare_model_for_kbit_training +from peft.tuners.lora import LoraLayer + +load_in_8bit = False + +# 4-bit quantization +compute_dtype = getattr(torch, BNB_4BIT_COMPUTE_DTYPE) + +bnb_config = BitsAndBytesConfig( + load_in_4bit=True, + bnb_4bit_quant_type="nf4", + bnb_4bit_compute_dtype=compute_dtype, + bnb_4bit_use_double_quant=USE_NESTED_QUANT, +) + +device_map = {"": 0} + +model = AutoModelForCausalLM.from_pretrained( + MODEL, + load_in_8bit=load_in_8bit, + quantization_config=bnb_config, + device_map=device_map, + use_cache=False, # We will be using gradient checkpointing + trust_remote_code=True, + use_flash_attention_2=True, +) + + +# When using a quantized model for training, you need to call the `prepare_model_for_kbit_training()` function to preprocess the quantized model for training. + + +model = prepare_model_for_kbit_training(model) + + +# Now that the quantized model is ready, we can set up a LoRA configuration. LoRA makes fine-tuning more efficient by drastically reducing the number of trainable parameters. +# +# To train a model using LoRA technique, we need to wrap the base model as a `PeftModel`. This involves definign LoRA configuration with `LoraConfig`, and wrapping the original model with `get_peft_model()` using the `LoraConfig`. +# +# To learn more about LoRA and its parameters, refer to [PEFT documentation](https://huggingface.co/docs/peft/main/en/conceptual_guides/lora). + + + +# Set up lora +peft_config = LoraConfig( + lora_alpha=LORA_ALPHA, + lora_dropout=LORA_DROPOUT, + r=LORA_R, + bias="none", + task_type="CAUSAL_LM", + target_modules=LORA_TARGET_MODULES.split(","), +) + +model = get_peft_model(model, peft_config) +model.print_trainable_parameters() + + +# As you can see, by applying LoRA technique we will now need to train less than 1% of the parameters. + +# ## Train the model + +# Now that we have prepared the data, and optimized the model, we are ready to bring everything together to start the training. +# +# To instantiate a `Trainer`, you need to define the training configuration. The most important is the `TrainingArguments`, which is a class that contains all the attributes to configure the training. +# +# These are similar to any other kind of model training you may run, so we won't go into detail here. + +# In[ ]: + + +train_data.start_iteration = 0 + + +training_args = TrainingArguments( + output_dir=f"runs/{run_name}/{OUTPUT_DIR}", + dataloader_drop_last=True, + evaluation_strategy="steps", + save_strategy="steps", + max_steps=MAX_STEPS, + eval_steps=EVAL_FREQ, + save_steps=SAVE_FREQ, + logging_steps=LOG_FREQ, + per_device_train_batch_size=BATCH_SIZE, + per_device_eval_batch_size=BATCH_SIZE, + learning_rate=LR, + lr_scheduler_type=LR_SCHEDULER_TYPE, + warmup_steps=NUM_WARMUP_STEPS, + gradient_accumulation_steps=GR_ACC_STEPS, + gradient_checkpointing=True, + fp16=FP16, + bf16=BF16, + weight_decay=WEIGHT_DECAY, + push_to_hub=True, + include_tokens_per_second=True, + run_name=f"runs-{run_name}", + report_to="wandb" +) + + +# As a final step, instantiate the `Trainer` and call the `train` method. + +trainer = Trainer( + model=model, args=training_args, train_dataset=train_dataset, eval_dataset=eval_dataset +) + +print("Training...") +trainer.train() + + +# Finally, you can push the fine-tuned model to your Hub repository to share with your team. + + +trainer.push_to_hub() + + +# ## Inference +# +# Once the model is uploaded to Hub, we can use it for inference. To do so we first initialize the original base model and its tokenizer. Next, we need to merge the fine-duned weights with the base model. + +# In[ ]: + + +from peft import PeftModel +import torch + +# load the original model first +tokenizer = AutoTokenizer.from_pretrained(MODEL, trust_remote_code=True) +base_model = AutoModelForCausalLM.from_pretrained( + MODEL, + quantization_config=None, + device_map=None, + trust_remote_code=True, + torch_dtype=torch.bfloat16, +).cuda() + +# merge fine-tuned weights with the base model +peft_model_id = f"runs/{run_name}/{OUTPUT_DIR}" +model = PeftModel.from_pretrained(base_model, peft_model_id) +model.merge_and_unload() + + +# Now we can use the merged model for inference. For convenience, we'll define a `get_code_completion` - feel free to experiment with text generation parameters! + + + + +# Now all we need to do to get code completion is call the `get_code_complete` function and pass the first few lines that we want to be completed as a prefix, and an empty string as a suffix. + + +# While it is Python syntax, you can see that the original model has no understanding of what a `LoraConfig` should be doing. + +# To learn how this kind of fine-tuning compares to full fine-tuning, and how to use a model like this as your copilot in VS Code via Inference Endpoints, or locally, check out the ["Personal Copilot: Train Your Own Coding Assistant" blog post](https://huggingface.co/blog/personal-copilot). This notebook complements the original blog post. +# From a5bb479b196c0e86a43228c06bae550aa34b0dd9 Mon Sep 17 00:00:00 2001 From: Mihail Eric Date: Wed, 9 Oct 2024 03:25:44 +0000 Subject: [PATCH 4/6] refactored training script --- sage/fine_tuning_code_llm_on_single_gpu.py | 441 ++++++++------------- 1 file changed, 171 insertions(+), 270 deletions(-) diff --git a/sage/fine_tuning_code_llm_on_single_gpu.py b/sage/fine_tuning_code_llm_on_single_gpu.py index 07f9fcc..8f8f291 100644 --- a/sage/fine_tuning_code_llm_on_single_gpu.py +++ b/sage/fine_tuning_code_llm_on_single_gpu.py @@ -1,33 +1,12 @@ -#!/usr/bin/env python -# coding: utf-8 - -# # Fine-tuning a Code LLM on Custom Code on a single GPU -# -# _Authored by: [Maria Khalusova](https://github.com/MKhalusova)_ -# -# Publicly available code LLMs such as Codex, StarCoder, and Code Llama are great at generating code that adheres to general programming principles and syntax, but they may not align with an organization's internal conventions, or be aware of proprietary libraries. -# -# In this notebook, we'll see show how you can fine-tune a code LLM on private code bases to enhance its contextual awareness and improve a model's usefulness to your organization's needs. Since the code LLMs are quite large, fine-tuning them in a traditional manner can be resource-draining. Worry not! We will show how you can optimize fine-tuning to fit on a single GPU. -# -# -# ## Dataset -# -# For this example, we picked the top 10 Hugging Face public repositories on GitHub. We have excluded non-code files from the data, such as images, audio files, presentations, and so on. For Jupyter notebooks, we've kept only cells containing code. The resulting code is stored as a dataset that you can find on the Hugging Face Hub under [`smangrul/hf-stack-v1`](https://huggingface.co/datasets/smangrul/hf-stack-v1). It contains repo id, file path, and file content. -# -# -# ## Model -# -# We'll finetune [`bigcode/starcoderbase-1b`](https://huggingface.co/bigcode/starcoderbase-1b), which is a 1B parameter model trained on 80+ programming languages. This is a gated model, so if you plan to run this notebook with this exact model, you'll need to gain access to it on the model's page. Log in to your Hugging Face account to do so: - - import argparse import os -import flash_attn -import argparse -import torch +import flash_attn import functools import numpy as np import random +import pickle +import torch +import wandb from datasets import load_dataset from tqdm import tqdm @@ -44,13 +23,16 @@ set_seed, BitsAndBytesConfig, ) - +from peft import LoraConfig, get_peft_model, prepare_model_for_kbit_training +from peft.tuners.lora import LoraLayer +from peft import PeftModel +from peft import LoraConfig, get_peft_model, prepare_model_for_kbit_training parser = argparse.ArgumentParser() parser.add_argument("--model", type=str, default="bigcode/starcoderbase-1b", help="Model checkpoint on the Hugging Face Hub") parser.add_argument("--dataset", type=str, default="smangrul/hf-stack-v1", help="Dataset on the Hugging Face Hub") parser.add_argument("--data_column", type=str, default="content", help="Column name containing the code content") -parser.add_argument("--seq_length", type=int, default=2048, help="Sequence length") +parser.add_argument("--seq_length", type=int, default=1024, help="Sequence length") parser.add_argument("--max_steps", type=int, default=2000, help="Max training steps") parser.add_argument("--batch_size", type=int, default=16, help="Batch size") parser.add_argument("--gr_acc_steps", type=int, default=1, help="Gradient accumulation steps") @@ -76,16 +58,13 @@ parser.add_argument('--wandb_key', type=str, required=True, help='wandb api key') parser.add_argument('--hf_token', type=str, required=True, help='huggingface hub token') parser.add_argument('--run_name', type=str, default='starcoderbase-1b-finetuned', help='run name for wandb') +parser.add_argument('--num_valid_datapoints', type=int, default=1000, help='number of datapoints to use for validation split') args = parser.parse_args() -run_name = args.run_name - def login_to_huggingface_hub(token): """ Log in to the Hugging Face Hub programmatically. - - :param token: Your Hugging Face API token """ # Set the token as an environment variable os.environ["HUGGING_FACE_HUB_TOKEN"] = token @@ -103,6 +82,7 @@ def login_to_huggingface_hub(token): except Exception as e: print(f"Login failed: {str(e)}") + def chars_token_ratio(dataset, tokenizer, data_column, nb_examples=400): """ Estimate the average number of characters per token in the dataset. @@ -130,40 +110,23 @@ def get_code_completion(prefix, suffix): return tokenizer.batch_decode(outputs, skip_special_tokens=True)[0] -login_to_huggingface_hub(args.hf_token) -set_seed(SEED) - -# Wandb configuration -import wandb -wandb.login() - - -dataset = load_dataset( - DATASET, - data_dir="data", - split="train", - streaming=True, -) - -valid_data = dataset.take(400) -train_data = dataset.skip(400).take(400) -train_data = train_data.shuffle(buffer_size=5000, seed=SEED) - - - -train_data.info -tokenizer = AutoTokenizer.from_pretrained(MODEL, trust_remote_code=True) - - - -chars_per_token = chars_token_ratio(train_data, tokenizer, DATA_COLUMN) -# print(f"The character to token ratio of the dataset is: {chars_per_token:.2f}") - - - - +def load_and_test_generation(): + # load the original model first + tokenizer = AutoTokenizer.from_pretrained(MODEL, trust_remote_code=True) + base_model = AutoModelForCausalLM.from_pretrained( + MODEL, + quantization_config=None, + device_map=None, + trust_remote_code=True, + torch_dtype=torch.bfloat16, + ).cuda() -# Helper function to get token ids of the special tokens for prefix, suffix and middle for FIM transformations. + # merge fine-tuned weights with the base model + peft_model_id = f"runs/{run_name}/{OUTPUT_DIR}" + model = PeftModel.from_pretrained(base_model, peft_model_id) + model.merge_and_unload() + + @functools.lru_cache(maxsize=None) def get_fim_token_ids(tokenizer): try: @@ -175,8 +138,6 @@ def get_fim_token_ids(tokenizer): suffix_tok_id, prefix_tok_id, middle_tok_id, pad_tok_id = None, None, None, None return suffix_tok_id, prefix_tok_id, middle_tok_id, pad_tok_id - -## Adapted from https://github.com/bigcode-project/Megatron-LM/blob/6c4bf908df8fd86b4977f54bf5b8bd4b521003d1/megatron/data/gpt_dataset.py def permute( sample, np_rng, @@ -250,16 +211,6 @@ def permute( return list(new_sample), np_rng - -# Let's define the `ConstantLengthDataset`, an Iterable dataset that will return constant-length chunks of tokens. To do so, we'll read a buffer of text from the original dataset until we hit the size limits and then apply tokenizer to convert the raw text into tokenized inputs. Optionally, we'll perform FIM transformations on some sequences (the proportion of sequences affected is controlled by `fim_rate`). -# Once defined, we can create instances of the `ConstantLengthDataset` from both training and validation data. - - - - - -# Create an Iterable dataset that returns constant-length chunks of tokens from a stream of text files. - class ConstantLengthDataset(IterableDataset): """ Iterable dataset that returns constant length chunks of tokens from stream of text files. @@ -280,25 +231,25 @@ def __init__( tokenizer, dataset, infinite=False, - seq_length=1024, + seq_length=args.seq_length, num_of_sequences=1024, chars_per_token=3.6, content_field="content", - fim_rate=0.5, - fim_spm_rate=0.5, - seed=0, + fim_rate=args.fim_rate, + fim_spm_rate=args.fim_spm_rate, + seed=args.seed, ): self.tokenizer = tokenizer self.concat_token_id = tokenizer.eos_token_id self.dataset = dataset - self.seq_length = seq_length + self.seq_length = args.seq_length self.infinite = infinite self.current_size = 0 - self.max_buffer_size = seq_length * chars_per_token * num_of_sequences + self.max_buffer_size = args.seq_length * chars_per_token * num_of_sequences self.content_field = content_field - self.fim_rate = fim_rate - self.fim_spm_rate = fim_spm_rate - self.seed = seed + self.fim_rate = args.fim_rate + self.fim_spm_rate = args.fim_spm_rate + self.seed = args.seed ( self.suffix_tok_id, @@ -360,205 +311,155 @@ def __iter__(self): "labels": torch.LongTensor(example), } +if __name__ == "__main__": + login_to_huggingface_hub(args.hf_token) + set_seed(args.seed) + run_name = args.run_name + wandb.login(key=args.wandb_key) + + dataset = load_dataset( + args.dataset, + data_dir="data", + split="train", + streaming=True, + ) -train_dataset = ConstantLengthDataset( - tokenizer, - train_data, - infinite=True, - seq_length=SEQ_LENGTH, - chars_per_token=chars_per_token, - content_field=DATA_COLUMN, - fim_rate=FIM_RATE, - fim_spm_rate=FIM_SPM_RATE, - seed=SEED, -) -eval_dataset = ConstantLengthDataset( - tokenizer, - valid_data, - infinite=False, - seq_length=SEQ_LENGTH, - chars_per_token=chars_per_token, - content_field=DATA_COLUMN, - fim_rate=FIM_RATE, - fim_spm_rate=FIM_SPM_RATE, - seed=SEED, -) - -# Dump dataset objs to disk -import pickle -with open('train_dataset.pkl', 'wb') as f: - pickle.dump(train_dataset, f) - -with open('eval_dataset.pkl', 'wb') as f: - pickle.dump(eval_dataset, f) - - -# ## Prepare the model - -# Now that the data is prepared, it's time to load the model! We're going to load the quantized version of the model. -# -# This will allow us to reduce memory usage, as quantization represents data with fewer bits. We'll use the `bitsandbytes` library to quantize the model, as it has a nice integration with `transformers`. All we need to do is define a `bitsandbytes` config, and then use it when loading the model. -# -# There are different variants of 4bit quantization, but generally, we recommend using NF4 quantization for better performance (`bnb_4bit_quant_type="nf4"`). -# -# The `bnb_4bit_use_double_quant` option adds a second quantization after the first one to save an additional 0.4 bits per parameter. -# -# To learn more about quantization, check out the ["Making LLMs even more accessible with bitsandbytes, 4-bit quantization and QLoRA" blog post](https://huggingface.co/blog/4bit-transformers-bitsandbytes). -# -# Once defined, pass the config to the `from_pretrained` method to load the quantized version of the model. - -# In[ ]: - - -from peft import LoraConfig, get_peft_model, prepare_model_for_kbit_training -from peft.tuners.lora import LoraLayer - -load_in_8bit = False - -# 4-bit quantization -compute_dtype = getattr(torch, BNB_4BIT_COMPUTE_DTYPE) - -bnb_config = BitsAndBytesConfig( - load_in_4bit=True, - bnb_4bit_quant_type="nf4", - bnb_4bit_compute_dtype=compute_dtype, - bnb_4bit_use_double_quant=USE_NESTED_QUANT, -) - -device_map = {"": 0} - -model = AutoModelForCausalLM.from_pretrained( - MODEL, - load_in_8bit=load_in_8bit, - quantization_config=bnb_config, - device_map=device_map, - use_cache=False, # We will be using gradient checkpointing - trust_remote_code=True, - use_flash_attention_2=True, -) - - -# When using a quantized model for training, you need to call the `prepare_model_for_kbit_training()` function to preprocess the quantized model for training. - - -model = prepare_model_for_kbit_training(model) - - -# Now that the quantized model is ready, we can set up a LoRA configuration. LoRA makes fine-tuning more efficient by drastically reducing the number of trainable parameters. -# -# To train a model using LoRA technique, we need to wrap the base model as a `PeftModel`. This involves definign LoRA configuration with `LoraConfig`, and wrapping the original model with `get_peft_model()` using the `LoraConfig`. -# -# To learn more about LoRA and its parameters, refer to [PEFT documentation](https://huggingface.co/docs/peft/main/en/conceptual_guides/lora). - - - -# Set up lora -peft_config = LoraConfig( - lora_alpha=LORA_ALPHA, - lora_dropout=LORA_DROPOUT, - r=LORA_R, - bias="none", - task_type="CAUSAL_LM", - target_modules=LORA_TARGET_MODULES.split(","), -) - -model = get_peft_model(model, peft_config) -model.print_trainable_parameters() - - -# As you can see, by applying LoRA technique we will now need to train less than 1% of the parameters. - -# ## Train the model - -# Now that we have prepared the data, and optimized the model, we are ready to bring everything together to start the training. -# -# To instantiate a `Trainer`, you need to define the training configuration. The most important is the `TrainingArguments`, which is a class that contains all the attributes to configure the training. -# -# These are similar to any other kind of model training you may run, so we won't go into detail here. - -# In[ ]: - - -train_data.start_iteration = 0 - - -training_args = TrainingArguments( - output_dir=f"runs/{run_name}/{OUTPUT_DIR}", - dataloader_drop_last=True, - evaluation_strategy="steps", - save_strategy="steps", - max_steps=MAX_STEPS, - eval_steps=EVAL_FREQ, - save_steps=SAVE_FREQ, - logging_steps=LOG_FREQ, - per_device_train_batch_size=BATCH_SIZE, - per_device_eval_batch_size=BATCH_SIZE, - learning_rate=LR, - lr_scheduler_type=LR_SCHEDULER_TYPE, - warmup_steps=NUM_WARMUP_STEPS, - gradient_accumulation_steps=GR_ACC_STEPS, - gradient_checkpointing=True, - fp16=FP16, - bf16=BF16, - weight_decay=WEIGHT_DECAY, - push_to_hub=True, - include_tokens_per_second=True, - run_name=f"runs-{run_name}", - report_to="wandb" -) - - -# As a final step, instantiate the `Trainer` and call the `train` method. - -trainer = Trainer( - model=model, args=training_args, train_dataset=train_dataset, eval_dataset=eval_dataset -) - -print("Training...") -trainer.train() - - -# Finally, you can push the fine-tuned model to your Hub repository to share with your team. - - -trainer.push_to_hub() + # Since this is an iterable dataset, we reserve a fixed # of datapoints for the valid split + valid_data = dataset.take(args.num_valid_datapoints) + train_data = dataset.skip(args.num_valid_datapoints) + + train_data = train_data.shuffle(buffer_size=5000, seed=args.seed) + tokenizer = AutoTokenizer.from_pretrained(args.model, trust_remote_code=True) + + chars_per_token = chars_token_ratio(train_data, tokenizer, args.data_column) + + train_dataset = ConstantLengthDataset( + tokenizer, + train_data, + infinite=True, + seq_length=args.seq_length, + chars_per_token=chars_per_token, + content_field=args.data_column, + fim_rate=args.fim_rate, + fim_spm_rate=args.fim_spm_rate, + seed=args.seed, + ) + eval_dataset = ConstantLengthDataset( + tokenizer, + valid_data, + infinite=False, + seq_length=args.seq_length, + chars_per_token=chars_per_token, + content_field=args.data_column, + fim_rate=args.fim_rate, + fim_spm_rate=args.fim_spm_rate, + seed=args.seed, + ) + with open('train_dataset.pkl', 'wb') as f: + pickle.dump(train_dataset, f) -# ## Inference -# -# Once the model is uploaded to Hub, we can use it for inference. To do so we first initialize the original base model and its tokenizer. Next, we need to merge the fine-duned weights with the base model. + with open('eval_dataset.pkl', 'wb') as f: + pickle.dump(eval_dataset, f) -# In[ ]: + # ## Prepare the model + # Now that the data is prepared, it's time to load the model! We're going to load the quantized version of the model. + # This will allow us to reduce memory usage, as quantization represents data with fewer bits. We'll use the `bitsandbytes` library to quantize the model, as it has a nice integration with `transformers`. All we need to do is define a `bitsandbytes` config, and then use it when loading the model. + # There are different variants of 4bit quantization, but generally, we recommend using NF4 quantization for better performance (`bnb_4bit_quant_type="nf4"`). + # The `bnb_4bit_use_double_quant` option adds a second quantization after the first one to save an additional 0.4 bits per parameter. + # To learn more about quantization, check out the ["Making LLMs even more accessible with bitsandbytes, 4-bit quantization and QLoRA" blog post](https://huggingface.co/blog/4bit-transformers-bitsandbytes). + # Once defined, pass the config to the `from_pretrained` method to load the quantized version of the model. -from peft import PeftModel -import torch -# load the original model first -tokenizer = AutoTokenizer.from_pretrained(MODEL, trust_remote_code=True) -base_model = AutoModelForCausalLM.from_pretrained( - MODEL, - quantization_config=None, - device_map=None, - trust_remote_code=True, - torch_dtype=torch.bfloat16, -).cuda() + # 4-bit quantization + compute_dtype = getattr(torch, args.bnb_4bit_compute_dtype) -# merge fine-tuned weights with the base model -peft_model_id = f"runs/{run_name}/{OUTPUT_DIR}" -model = PeftModel.from_pretrained(base_model, peft_model_id) -model.merge_and_unload() + bnb_config = BitsAndBytesConfig( + load_in_4bit=True, + bnb_4bit_quant_type="nf4", + bnb_4bit_compute_dtype=compute_dtype, + bnb_4bit_use_double_quant=args.use_nested_quant, + ) + device_map = {"": 0} -# Now we can use the merged model for inference. For convenience, we'll define a `get_code_completion` - feel free to experiment with text generation parameters! + model = AutoModelForCausalLM.from_pretrained( + args.model, + load_in_8bit=False, + quantization_config=bnb_config, + device_map=device_map, + use_cache=False, # We will be using gradient checkpointing + trust_remote_code=True, + use_flash_attention_2=True, + ) + # When using a quantized model for training, you need to call the `prepare_model_for_kbit_training()` function to preprocess the quantized model for training. + model = prepare_model_for_kbit_training(model) + + # Now that the quantized model is ready, we can set up a LoRA configuration. LoRA makes fine-tuning more efficient by drastically reducing the number of trainable parameters. + # To train a model using LoRA technique, we need to wrap the base model as a `PeftModel`. This involves definign LoRA configuration with `LoraConfig`, and wrapping the original model with `get_peft_model()` using the `LoraConfig`. + # To learn more about LoRA and its parameters, refer to [PEFT documentation](https://huggingface.co/docs/peft/main/en/conceptual_guides/lora). + + # Set up lora + peft_config = LoraConfig( + lora_alpha=args.lora_alpha, + lora_dropout=args.lora_dropout, + r=args.lora_r, + bias="none", + task_type="CAUSAL_LM", + target_modules=args.lora_target_modules.split(","), + ) + model = get_peft_model(model, peft_config) + model.print_trainable_parameters() + + # As you can see, by applying LoRA technique we will now need to train less than 1% of the parameters. + + # ## Train the model + # Now that we have prepared the data, and optimized the model, we are ready to bring everything together to start the training. + # + # To instantiate a `Trainer`, you need to define the training configuration. The most important is the `TrainingArguments`, which is a class that contains all the attributes to configure the training. + # + # These are similar to any other kind of model training you may run, so we won't go into detail here. + + + train_data.start_iteration = 0 + training_args = TrainingArguments( + output_dir=f"runs/{args.run_name}/{args.output_dir}", + dataloader_drop_last=True, + evaluation_strategy="steps", + save_strategy="steps", + max_steps=args.max_steps, + eval_steps=args.eval_freq, + save_steps=args.save_freq, + logging_steps=args.log_freq, + per_device_train_batch_size=args.batch_size, + per_device_eval_batch_size=args.batch_size, + learning_rate=args.lr, + lr_scheduler_type=args.lr_scheduler_type, + warmup_steps=args.num_warmup_steps, + gradient_accumulation_steps=args.gr_acc_steps, + gradient_checkpointing=True, + fp16=args.fp16, + bf16=args.bf16, + weight_decay=args.weight_decay, + push_to_hub=True, + include_tokens_per_second=True, + run_name=f"runs-{args.run_name}", + report_to="wandb" + ) -# Now all we need to do to get code completion is call the `get_code_complete` function and pass the first few lines that we want to be completed as a prefix, and an empty string as a suffix. + # As a final step, instantiate the `Trainer` and call the `train` method. + trainer = Trainer( + model=model, args=training_args, train_dataset=train_dataset, eval_dataset=eval_dataset + ) + print("Training...") + trainer.train() -# While it is Python syntax, you can see that the original model has no understanding of what a `LoraConfig` should be doing. -# To learn how this kind of fine-tuning compares to full fine-tuning, and how to use a model like this as your copilot in VS Code via Inference Endpoints, or locally, check out the ["Personal Copilot: Train Your Own Coding Assistant" blog post](https://huggingface.co/blog/personal-copilot). This notebook complements the original blog post. -# + # Finally, you can push the fine-tuned model to your Hub repository to share with your team. + trainer.push_to_hub() + wandb.finish() From 9a7b8e81c5ccf11d0ca529064f4c6a0ff9db1ec9 Mon Sep 17 00:00:00 2001 From: Mihail Eric Date: Wed, 9 Oct 2024 03:30:18 +0000 Subject: [PATCH 5/6] cleanup --- sage/fine_tuning_code_llm_on_single_gpu.ipynb | 1657 ----------------- sage/fine_tuning_code_llm_on_single_gpu.py | 20 +- 2 files changed, 6 insertions(+), 1671 deletions(-) delete mode 100644 sage/fine_tuning_code_llm_on_single_gpu.ipynb diff --git a/sage/fine_tuning_code_llm_on_single_gpu.ipynb b/sage/fine_tuning_code_llm_on_single_gpu.ipynb deleted file mode 100644 index 4f1d4ae..0000000 --- a/sage/fine_tuning_code_llm_on_single_gpu.ipynb +++ /dev/null @@ -1,1657 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "id": "FNdZ-kD0l78P" - }, - "source": [ - "# Fine-tuning a Code LLM on Custom Code on a single GPU\n", - "\n", - "_Authored by: [Maria Khalusova](https://github.com/MKhalusova)_\n", - "\n", - "Publicly available code LLMs such as Codex, StarCoder, and Code Llama are great at generating code that adheres to general programming principles and syntax, but they may not align with an organization's internal conventions, or be aware of proprietary libraries.\n", - "\n", - "In this notebook, we'll see show how you can fine-tune a code LLM on private code bases to enhance its contextual awareness and improve a model's usefulness to your organization's needs. Since the code LLMs are quite large, fine-tuning them in a traditional manner can be resource-draining. Worry not! We will show how you can optimize fine-tuning to fit on a single GPU.\n", - "\n", - "\n", - "## Dataset\n", - "\n", - "For this example, we picked the top 10 Hugging Face public repositories on GitHub. We have excluded non-code files from the data, such as images, audio files, presentations, and so on. For Jupyter notebooks, we've kept only cells containing code. The resulting code is stored as a dataset that you can find on the Hugging Face Hub under [`smangrul/hf-stack-v1`](https://huggingface.co/datasets/smangrul/hf-stack-v1). It contains repo id, file path, and file content.\n", - "\n", - "\n", - "## Model\n", - "\n", - "We'll finetune [`bigcode/starcoderbase-1b`](https://huggingface.co/bigcode/starcoderbase-1b), which is a 1B parameter model trained on 80+ programming languages. This is a gated model, so if you plan to run this notebook with this exact model, you'll need to gain access to it on the model's page. Log in to your Hugging Face account to do so:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 331, - "referenced_widgets": [ - "65e4da277e6f4fc0bec087273ad6bce6", - "20f45ad1b5404dd1b35e2c81696e752d", - "fb6c47e03a37422ea81a09805d4742e4", - "6693fb7e2bc5433bba04c80b6eb40ad6", - "80305f2c23194f85ba0219856af7839a", - "8157516cdf984c1a9a09b05288df0b42", - "02ecdae95dcf47648b1898bddd68263d", - "e50a4cc3e2b14b0b8ca6cb0769a4ddb1", - "609e92a6fa5243c7ae35b07ef14f9cf5", - "e367a1e3164d4e13a4e5ad477e9ac625", - "a65cd74b61c2435ea23fc1cae68d7156", - "3e422885d1c24a38a5af8d50d9f2cbb7", - "83ec2e09b2954eb4a5561a1180ff2568", - "b111d185e6ed42c0beeaed4db567745c", - "982f0140aced4ac38f746f3f35f657d3", - "5770ef4c8d3f47d7b6e38d1c0e690cce", - "e82489e884464c8588447f74fd8fa97e" - ] - }, - "id": "bPlCJYDK6vrF", - "outputId": "f738649c-77dd-4daf-e19f-f09e44e2f4a6" - }, - "outputs": [ - { - "ename": "", - "evalue": "", - "output_type": "error", - "traceback": [ - "\u001b[1;31mRunning cells with 'sage-310 (Python 3.10.4)' requires the ipykernel package.\n", - "\u001b[1;31mRun the following command to install 'ipykernel' into the Python environment. \n", - "\u001b[1;31mCommand: 'conda install -n sage-310 ipykernel --update-deps --force-reinstall'" - ] - } - ], - "source": [ - "from huggingface_hub import notebook_login\n", - "\n", - "notebook_login()" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "WMVe_c8q43Qo" - }, - "source": [ - "To get started, let's install all the necessary libraries. As you can see, in addition to `transformers` and `datasets`, we'll be using `peft`, `bitsandbytes`, and `flash-attn` to optimize the training.\n", - "\n", - "By employing parameter-efficient training techniques, we can run this notebook on a single A100 High-RAM GPU." - ] - }, - { - "cell_type": "code", - "execution_count": 37, - "metadata": { - "id": "Fp7i8WMCjKJG" - }, - "outputs": [], - "source": [ - "!pip install -q transformers datasets peft bitsandbytes flash-attn" - ] - }, - { - "cell_type": "code", - "execution_count": 39, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 36 - }, - "id": "Rhg5JgtC2WS-", - "outputId": "3628306f-5277-4e31-eb59-d12f9f48fa53" - }, - "outputs": [ - { - "data": { - "application/vnd.google.colaboratory.intrinsic+json": { - "type": "string" - }, - "text/plain": [ - "'2.6.3'" - ] - }, - "execution_count": 39, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "import flash_attn; flash_attn.__version__" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "16EdABzt3_Ig" - }, - "source": [ - "Let's define some variables now. Feel free to play with these." - ] - }, - { - "cell_type": "code", - "execution_count": 22, - "metadata": { - "id": "hru3G-CLmqis" - }, - "outputs": [], - "source": [ - "MODEL=\"bigcode/starcoderbase-1b\" # Model checkpoint on the Hugging Face Hub\n", - "DATASET=\"smangrul/hf-stack-v1\" # Dataset on the Hugging Face Hub\n", - "DATA_COLUMN=\"content\" # Column name containing the code content\n", - "\n", - "SEQ_LENGTH=2048 # Sequence length\n", - "\n", - "# Training arguments\n", - "MAX_STEPS=2000 # max_steps\n", - "BATCH_SIZE=16 # batch_size\n", - "GR_ACC_STEPS=1 # gradient_accumulation_steps\n", - "LR=5e-4 # learning_rate\n", - "LR_SCHEDULER_TYPE=\"cosine\" # lr_scheduler_type\n", - "WEIGHT_DECAY=0.01 # weight_decay\n", - "NUM_WARMUP_STEPS=30 # num_warmup_steps\n", - "EVAL_FREQ=100 # eval_freq\n", - "SAVE_FREQ=100 # save_freq\n", - "LOG_FREQ=25 # log_freq\n", - "OUTPUT_DIR=\"peft-starcoder-lora-a100\" # output_dir\n", - "BF16=True # bf16\n", - "FP16=False # no_fp16\n", - "\n", - "# FIM trasformations arguments\n", - "FIM_RATE=0.5 # fim_rate\n", - "FIM_SPM_RATE=0.5 # fim_spm_rate\n", - "\n", - "# LORA\n", - "LORA_R=8 # lora_r\n", - "LORA_ALPHA=32 # lora_alpha\n", - "LORA_DROPOUT=0.0 # lora_dropout\n", - "LORA_TARGET_MODULES=\"c_proj,c_attn,q_attn,c_fc,c_proj\" # lora_target_modules\n", - "\n", - "# bitsandbytes config\n", - "USE_NESTED_QUANT=True # use_nested_quant\n", - "BNB_4BIT_COMPUTE_DTYPE=\"bfloat16\"# bnb_4bit_compute_dtype\n", - "\n", - "SEED=0" - ] - }, - { - "cell_type": "code", - "execution_count": 23, - "metadata": { - "id": "FyZSXTbJrcnC" - }, - "outputs": [], - "source": [ - "from transformers import (\n", - " AutoModelForCausalLM,\n", - " AutoTokenizer,\n", - " Trainer,\n", - " TrainingArguments,\n", - " logging,\n", - " set_seed,\n", - " BitsAndBytesConfig,\n", - ")\n", - "\n", - "set_seed(SEED)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "pO7F5L5AtKo1" - }, - "source": [ - "## Prepare the data" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "1LmrIZqP0oUE" - }, - "source": [ - "Begin by loading the data. As the dataset is likely to be quite large, make sure to enable the streaming mode. Streaming allows us to load the data progressively as we iterate over the dataset instead of downloading the whole dataset at once.\n", - "\n", - "We'll reserve the first 4000 examples as the validation set, and everything else will be the training data." - ] - }, - { - "cell_type": "code", - "execution_count": 24, - "metadata": { - "id": "4oJZvZb-1J88" - }, - "outputs": [], - "source": [ - "from datasets import load_dataset\n", - "import torch\n", - "from tqdm import tqdm\n", - "\n", - "\n", - "dataset = load_dataset(\n", - " DATASET,\n", - " data_dir=\"data\",\n", - " split=\"train\",\n", - " streaming=True,\n", - ")\n", - "\n", - "valid_data = dataset.take(4000)\n", - "train_data = dataset.skip(4000)\n", - "train_data = train_data.shuffle(buffer_size=5000, seed=SEED)" - ] - }, - { - "cell_type": "code", - "execution_count": 25, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "rliIfosCs89r", - "outputId": "a245647b-f957-483c-b858-f661a0dd80da" - }, - "outputs": [ - { - "data": { - "text/plain": [ - "DatasetInfo(description='', citation='', homepage='', license='', features={'repo_id': Value(dtype='string', id=None), 'file_path': Value(dtype='string', id=None), 'content': Value(dtype='string', id=None), '__index_level_0__': Value(dtype='int64', id=None)}, post_processed=None, supervised_keys=None, builder_name='parquet', dataset_name='hf-stack-v1', config_name='default', version=0.0.0, splits={'train': SplitInfo(name='train', num_bytes=91907731, num_examples=5905, shard_lengths=None, dataset_name=None)}, download_checksums=None, download_size=30589828, post_processing_size=None, dataset_size=91907731, size_in_bytes=None)" - ] - }, - "execution_count": 25, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "train_data.info" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "sLQ8t0LM2GR6" - }, - "source": [ - "At this step, the dataset still contains raw data with code of arbitraty length. For training, we need inputs of fixed length. Let's create an Iterable dataset that would return constant-length chunks of tokens from a stream of text files.\n", - "\n", - "First, let's estimate the average number of characters per token in the dataset, which will help us later estimate the number of tokens in the text buffer later. By default, we'll only take 400 examples (`nb_examples`) from the dataset. Using only a subset of the entire dataset will reduce computational cost while still providing a reasonable estimate of the overall character-to-token ratio." - ] - }, - { - "cell_type": "code", - "execution_count": 26, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "KCiAvydztNsu", - "outputId": "e60f04c4-afc8-45d9-929c-f0ea193af93d" - }, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "/usr/local/lib/python3.10/dist-packages/transformers/tokenization_utils_base.py:1601: FutureWarning: `clean_up_tokenization_spaces` was not set. It will be set to `True` by default. This behavior will be depracted in transformers v4.45, and will be then set to `False` by default. For more details check this issue: https://github.com/huggingface/transformers/issues/31884\n", - " warnings.warn(\n", - "100%|██████████| 400/400 [00:13<00:00, 29.38it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "The character to token ratio of the dataset is: 2.43\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "\n" - ] - } - ], - "source": [ - "tokenizer = AutoTokenizer.from_pretrained(MODEL, trust_remote_code=True)\n", - "\n", - "def chars_token_ratio(dataset, tokenizer, data_column, nb_examples=400):\n", - " \"\"\"\n", - " Estimate the average number of characters per token in the dataset.\n", - " \"\"\"\n", - "\n", - " total_characters, total_tokens = 0, 0\n", - " for _, example in tqdm(zip(range(nb_examples), iter(dataset)), total=nb_examples):\n", - " total_characters += len(example[data_column])\n", - " total_tokens += len(tokenizer(example[data_column]).tokens())\n", - "\n", - " return total_characters / total_tokens\n", - "\n", - "\n", - "chars_per_token = chars_token_ratio(train_data, tokenizer, DATA_COLUMN)\n", - "print(f\"The character to token ratio of the dataset is: {chars_per_token:.2f}\")" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "6F13VGobB3Ma" - }, - "source": [ - "The character-to-token ratio can also be used as an indicator of the quality of text tokenization. For instance, a character-to-token ratio of 1.0 would mean that each character is represented with a token, which is not very meaningful. This would indicate poor tokenization. In standard English text, one token is typically equivalent to approximately four characters, meaning the character-to-token ratio is around 4.0. We can expect a lower ratio in the code dataset, but generally speaking, a number between 2.0 and 3.5 can be considered good enough." - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "rcwYFRPpwxea" - }, - "source": [ - "**Optional FIM transformations**\n", - "\n", - "\n", - "Autoregressive language models typically generate sequences from left to right. By applying the FIM transformations, the model can also learn to infill text. Check out [\"Efficient Training of Language Models to Fill in the Middle\" paper](https://arxiv.org/pdf/2207.14255.pdf) to learn more about the technique.\n", - "We'll define the FIM transformations here and will use them when creating the Iterable Dataset. However, if you want to omit transformations, feel free to set `fim_rate` to 0." - ] - }, - { - "cell_type": "code", - "execution_count": 27, - "metadata": { - "id": "zmejYvEKw1E-" - }, - "outputs": [], - "source": [ - "import functools\n", - "import numpy as np\n", - "\n", - "\n", - "# Helper function to get token ids of the special tokens for prefix, suffix and middle for FIM transformations.\n", - "@functools.lru_cache(maxsize=None)\n", - "def get_fim_token_ids(tokenizer):\n", - " try:\n", - " FIM_PREFIX, FIM_MIDDLE, FIM_SUFFIX, FIM_PAD = tokenizer.special_tokens_map[\"additional_special_tokens\"][1:5]\n", - " suffix_tok_id, prefix_tok_id, middle_tok_id, pad_tok_id = (\n", - " tokenizer.vocab[tok] for tok in [FIM_SUFFIX, FIM_PREFIX, FIM_MIDDLE, FIM_PAD]\n", - " )\n", - " except KeyError:\n", - " suffix_tok_id, prefix_tok_id, middle_tok_id, pad_tok_id = None, None, None, None\n", - " return suffix_tok_id, prefix_tok_id, middle_tok_id, pad_tok_id\n", - "\n", - "\n", - "## Adapted from https://github.com/bigcode-project/Megatron-LM/blob/6c4bf908df8fd86b4977f54bf5b8bd4b521003d1/megatron/data/gpt_dataset.py\n", - "def permute(\n", - " sample,\n", - " np_rng,\n", - " suffix_tok_id,\n", - " prefix_tok_id,\n", - " middle_tok_id,\n", - " pad_tok_id,\n", - " fim_rate=0.5,\n", - " fim_spm_rate=0.5,\n", - " truncate_or_pad=False,\n", - "):\n", - " \"\"\"\n", - " Take in a sample (list of tokens) and perform a FIM transformation on it with a probability of fim_rate, using two FIM modes:\n", - " PSM and SPM (with a probability of fim_spm_rate).\n", - " \"\"\"\n", - "\n", - " # The if condition will trigger with the probability of fim_rate\n", - " # This means FIM transformations will apply to samples with a probability of fim_rate\n", - " if np_rng.binomial(1, fim_rate):\n", - "\n", - " # Split the sample into prefix, middle, and suffix, based on randomly generated indices stored in the boundaries list.\n", - " boundaries = list(np_rng.randint(low=0, high=len(sample) + 1, size=2))\n", - " boundaries.sort()\n", - "\n", - " prefix = np.array(sample[: boundaries[0]], dtype=np.int64)\n", - " middle = np.array(sample[boundaries[0] : boundaries[1]], dtype=np.int64)\n", - " suffix = np.array(sample[boundaries[1] :], dtype=np.int64)\n", - "\n", - " if truncate_or_pad:\n", - " # calculate the new total length of the sample, taking into account tokens indicating prefix, middle, and suffix\n", - " new_length = suffix.shape[0] + prefix.shape[0] + middle.shape[0] + 3\n", - " diff = new_length - len(sample)\n", - "\n", - " # trancate or pad if there's a difference in length between the new length and the original\n", - " if diff > 0:\n", - " if suffix.shape[0] <= diff:\n", - " return sample, np_rng\n", - " suffix = suffix[: suffix.shape[0] - diff]\n", - " elif diff < 0:\n", - " suffix = np.concatenate([suffix, np.full((-1 * diff), pad_tok_id)])\n", - "\n", - " # With the probability of fim_spm_rateapply SPM variant of FIM transformations\n", - " # SPM: suffix, prefix, middle\n", - " if np_rng.binomial(1, fim_spm_rate):\n", - " new_sample = np.concatenate(\n", - " [\n", - " [prefix_tok_id, suffix_tok_id],\n", - " suffix,\n", - " [middle_tok_id],\n", - " prefix,\n", - " middle,\n", - " ]\n", - " )\n", - " # Otherwise, apply the PSM variant of FIM transformations\n", - " # PSM: prefix, suffix, middle\n", - " else:\n", - "\n", - " new_sample = np.concatenate(\n", - " [\n", - " [prefix_tok_id],\n", - " prefix,\n", - " [suffix_tok_id],\n", - " suffix,\n", - " [middle_tok_id],\n", - " middle,\n", - " ]\n", - " )\n", - " else:\n", - " # don't apply FIM transformations\n", - " new_sample = sample\n", - "\n", - " return list(new_sample), np_rng\n" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "AwW5FviD9xBH" - }, - "source": [ - "Let's define the `ConstantLengthDataset`, an Iterable dataset that will return constant-length chunks of tokens. To do so, we'll read a buffer of text from the original dataset until we hit the size limits and then apply tokenizer to convert the raw text into tokenized inputs. Optionally, we'll perform FIM transformations on some sequences (the proportion of sequences affected is controlled by `fim_rate`).\n", - "\n", - "Once defined, we can create instances of the `ConstantLengthDataset` from both training and validation data." - ] - }, - { - "cell_type": "code", - "execution_count": 28, - "metadata": { - "id": "AgDW-692wzOl" - }, - "outputs": [], - "source": [ - "from torch.utils.data import IterableDataset\n", - "from torch.utils.data.dataloader import DataLoader\n", - "import random\n", - "\n", - "# Create an Iterable dataset that returns constant-length chunks of tokens from a stream of text files.\n", - "\n", - "class ConstantLengthDataset(IterableDataset):\n", - " \"\"\"\n", - " Iterable dataset that returns constant length chunks of tokens from stream of text files.\n", - " Args:\n", - " tokenizer (Tokenizer): The processor used for proccessing the data.\n", - " dataset (dataset.Dataset): Dataset with text files.\n", - " infinite (bool): If True the iterator is reset after dataset reaches end else stops.\n", - " seq_length (int): Length of token sequences to return.\n", - " num_of_sequences (int): Number of token sequences to keep in buffer.\n", - " chars_per_token (int): Number of characters per token used to estimate number of tokens in text buffer.\n", - " fim_rate (float): Rate (0.0 to 1.0) that sample will be permuted with FIM.\n", - " fim_spm_rate (float): Rate (0.0 to 1.0) of FIM permuations that will use SPM.\n", - " seed (int): Seed for random number generator.\n", - " \"\"\"\n", - "\n", - " def __init__(\n", - " self,\n", - " tokenizer,\n", - " dataset,\n", - " infinite=False,\n", - " seq_length=1024,\n", - " num_of_sequences=1024,\n", - " chars_per_token=3.6,\n", - " content_field=\"content\",\n", - " fim_rate=0.5,\n", - " fim_spm_rate=0.5,\n", - " seed=0,\n", - " ):\n", - " self.tokenizer = tokenizer\n", - " self.concat_token_id = tokenizer.eos_token_id\n", - " self.dataset = dataset\n", - " self.seq_length = seq_length\n", - " self.infinite = infinite\n", - " self.current_size = 0\n", - " self.max_buffer_size = seq_length * chars_per_token * num_of_sequences\n", - " self.content_field = content_field\n", - " self.fim_rate = fim_rate\n", - " self.fim_spm_rate = fim_spm_rate\n", - " self.seed = seed\n", - "\n", - " (\n", - " self.suffix_tok_id,\n", - " self.prefix_tok_id,\n", - " self.middle_tok_id,\n", - " self.pad_tok_id,\n", - " ) = get_fim_token_ids(self.tokenizer)\n", - " if not self.suffix_tok_id and self.fim_rate > 0:\n", - " print(\"FIM is not supported by tokenizer, disabling FIM\")\n", - " self.fim_rate = 0\n", - "\n", - " def __iter__(self):\n", - " iterator = iter(self.dataset)\n", - " more_examples = True\n", - " np_rng = np.random.RandomState(seed=self.seed)\n", - " while more_examples:\n", - " buffer, buffer_len = [], 0\n", - " while True:\n", - " if buffer_len >= self.max_buffer_size:\n", - " break\n", - " try:\n", - " buffer.append(next(iterator)[self.content_field])\n", - " buffer_len += len(buffer[-1])\n", - " except StopIteration:\n", - " if self.infinite:\n", - " iterator = iter(self.dataset)\n", - " else:\n", - " more_examples = False\n", - " break\n", - " tokenized_inputs = self.tokenizer(buffer, truncation=False)[\"input_ids\"]\n", - " all_token_ids = []\n", - "\n", - " for tokenized_input in tokenized_inputs:\n", - " # optionally do FIM permutations\n", - " if self.fim_rate > 0:\n", - " tokenized_input, np_rng = permute(\n", - " tokenized_input,\n", - " np_rng,\n", - " self.suffix_tok_id,\n", - " self.prefix_tok_id,\n", - " self.middle_tok_id,\n", - " self.pad_tok_id,\n", - " fim_rate=self.fim_rate,\n", - " fim_spm_rate=self.fim_spm_rate,\n", - " truncate_or_pad=False,\n", - " )\n", - "\n", - " all_token_ids.extend(tokenized_input + [self.concat_token_id])\n", - " examples = []\n", - " for i in range(0, len(all_token_ids), self.seq_length):\n", - " input_ids = all_token_ids[i : i + self.seq_length]\n", - " if len(input_ids) == self.seq_length:\n", - " examples.append(input_ids)\n", - " random.shuffle(examples)\n", - " for example in examples:\n", - " self.current_size += 1\n", - " yield {\n", - " \"input_ids\": torch.LongTensor(example),\n", - " \"labels\": torch.LongTensor(example),\n", - " }\n", - "\n", - "\n", - "train_dataset = ConstantLengthDataset(\n", - " tokenizer,\n", - " train_data,\n", - " infinite=True,\n", - " seq_length=SEQ_LENGTH,\n", - " chars_per_token=chars_per_token,\n", - " content_field=DATA_COLUMN,\n", - " fim_rate=FIM_RATE,\n", - " fim_spm_rate=FIM_SPM_RATE,\n", - " seed=SEED,\n", - ")\n", - "eval_dataset = ConstantLengthDataset(\n", - " tokenizer,\n", - " valid_data,\n", - " infinite=False,\n", - " seq_length=SEQ_LENGTH,\n", - " chars_per_token=chars_per_token,\n", - " content_field=DATA_COLUMN,\n", - " fim_rate=FIM_RATE,\n", - " fim_spm_rate=FIM_SPM_RATE,\n", - " seed=SEED,\n", - ")" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "rxev1sk6tRW9" - }, - "source": [ - "## Prepare the model" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "UCtWV-U42Eq_" - }, - "source": [ - "Now that the data is prepared, it's time to load the model! We're going to load the quantized version of the model.\n", - "\n", - "This will allow us to reduce memory usage, as quantization represents data with fewer bits. We'll use the `bitsandbytes` library to quantize the model, as it has a nice integration with `transformers`. All we need to do is define a `bitsandbytes` config, and then use it when loading the model.\n", - "\n", - "There are different variants of 4bit quantization, but generally, we recommend using NF4 quantization for better performance (`bnb_4bit_quant_type=\"nf4\"`).\n", - "\n", - "The `bnb_4bit_use_double_quant` option adds a second quantization after the first one to save an additional 0.4 bits per parameter.\n", - "\n", - "To learn more about quantization, check out the [\"Making LLMs even more accessible with bitsandbytes, 4-bit quantization and QLoRA\" blog post](https://huggingface.co/blog/4bit-transformers-bitsandbytes).\n", - "\n", - "Once defined, pass the config to the `from_pretrained` method to load the quantized version of the model." - ] - }, - { - "cell_type": "code", - "execution_count": 29, - "metadata": { - "id": "XuwoX6U2DUvK" - }, - "outputs": [], - "source": [ - "from peft import LoraConfig, get_peft_model, prepare_model_for_kbit_training\n", - "from peft.tuners.lora import LoraLayer\n", - "\n", - "load_in_8bit = False\n", - "\n", - "# 4-bit quantization\n", - "compute_dtype = getattr(torch, BNB_4BIT_COMPUTE_DTYPE)\n", - "\n", - "bnb_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=USE_NESTED_QUANT,\n", - ")\n", - "\n", - "device_map = {\"\": 0}\n", - "\n", - "model = AutoModelForCausalLM.from_pretrained(\n", - " MODEL,\n", - " load_in_8bit=load_in_8bit,\n", - " quantization_config=bnb_config,\n", - " device_map=device_map,\n", - " use_cache=False, # We will be using gradient checkpointing\n", - " trust_remote_code=True,\n", - " use_flash_attention_2=True,\n", - ")\n" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "bO9e2FV8D8ZF" - }, - "source": [ - "When using a quantized model for training, you need to call the `prepare_model_for_kbit_training()` function to preprocess the quantized model for training." - ] - }, - { - "cell_type": "code", - "execution_count": 30, - "metadata": { - "id": "Qb_eB4xzEDBk" - }, - "outputs": [], - "source": [ - "model = prepare_model_for_kbit_training(model)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "lmnLjPZpDVtg" - }, - "source": [ - "Now that the quantized model is ready, we can set up a LoRA configuration. LoRA makes fine-tuning more efficient by drastically reducing the number of trainable parameters.\n", - "\n", - "To train a model using LoRA technique, we need to wrap the base model as a `PeftModel`. This involves definign LoRA configuration with `LoraConfig`, and wrapping the original model with `get_peft_model()` using the `LoraConfig`.\n", - "\n", - "To learn more about LoRA and its parameters, refer to [PEFT documentation](https://huggingface.co/docs/peft/main/en/conceptual_guides/lora)." - ] - }, - { - "cell_type": "code", - "execution_count": 31, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "_pAUU2FR2Gey", - "outputId": "f25de148-955f-452d-d81e-fbd7e028da26" - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "trainable params: 5,554,176 || all params: 1,142,761,472 || trainable%: 0.4860\n" - ] - } - ], - "source": [ - "# Set up lora\n", - "peft_config = LoraConfig(\n", - " lora_alpha=LORA_ALPHA,\n", - " lora_dropout=LORA_DROPOUT,\n", - " r=LORA_R,\n", - " bias=\"none\",\n", - " task_type=\"CAUSAL_LM\",\n", - " target_modules=LORA_TARGET_MODULES.split(\",\"),\n", - ")\n", - "\n", - "model = get_peft_model(model, peft_config)\n", - "model.print_trainable_parameters()" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "tHe7AElXzXVV" - }, - "source": [ - "As you can see, by applying LoRA technique we will now need to train less than 1% of the parameters." - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "T_CqVydc40IM" - }, - "source": [ - "## Train the model" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "Q_iN2khjrbD3" - }, - "source": [ - "Now that we have prepared the data, and optimized the model, we are ready to bring everything together to start the training.\n", - "\n", - "To instantiate a `Trainer`, you need to define the training configuration. The most important is the `TrainingArguments`, which is a class that contains all the attributes to configure the training.\n", - "\n", - "These are similar to any other kind of model training you may run, so we won't go into detail here." - ] - }, - { - "cell_type": "code", - "execution_count": 32, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "65QHS8l1tKQe", - "outputId": "8e1a2c40-9470-4563-f217-3c734a106e03" - }, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "/usr/local/lib/python3.10/dist-packages/transformers/training_args.py:1525: FutureWarning: `evaluation_strategy` is deprecated and will be removed in version 4.46 of 🤗 Transformers. Use `eval_strategy` instead\n", - " warnings.warn(\n" - ] - } - ], - "source": [ - "train_data.start_iteration = 0\n", - "\n", - "\n", - "training_args = TrainingArguments(\n", - " output_dir=f\"Your_HF_username/{OUTPUT_DIR}\",\n", - " dataloader_drop_last=True,\n", - " evaluation_strategy=\"steps\",\n", - " save_strategy=\"steps\",\n", - " max_steps=MAX_STEPS,\n", - " eval_steps=EVAL_FREQ,\n", - " save_steps=SAVE_FREQ,\n", - " logging_steps=LOG_FREQ,\n", - " per_device_train_batch_size=BATCH_SIZE,\n", - " per_device_eval_batch_size=BATCH_SIZE,\n", - " learning_rate=LR,\n", - " lr_scheduler_type=LR_SCHEDULER_TYPE,\n", - " warmup_steps=NUM_WARMUP_STEPS,\n", - " gradient_accumulation_steps=GR_ACC_STEPS,\n", - " gradient_checkpointing=True,\n", - " fp16=FP16,\n", - " bf16=BF16,\n", - " weight_decay=WEIGHT_DECAY,\n", - " push_to_hub=True,\n", - " include_tokens_per_second=True,\n", - ")\n" - ] - }, - { - "cell_type": "code", - "execution_count": 34, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "WwiWGK430KM3", - "outputId": "c735b480-7c7f-4775-9f16-2c42d88e0790" - }, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 34, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "model._supports_flash_attn_2" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "kB_fLRex09ut" - }, - "source": [ - "As a final step, instantiate the `Trainer` and call the `train` method. " - ] - }, - { - "cell_type": "code", - "execution_count": 33, - "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 446 - }, - "id": "rS3nVwhUC69O", - "outputId": "bc5f6d1d-7260-4c68-f325-885742b0d284" - }, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "max_steps is given, it will override any value given in num_train_epochs\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Training...\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/eval_frame.py:600: UserWarning: torch.utils.checkpoint: the use_reentrant parameter should be passed explicitly. In version 2.4 we will raise an exception if use_reentrant is not passed. use_reentrant=False is recommended, but if you need to preserve the current default behavior, you can pass use_reentrant=True. Refer to docs for more details on the differences between the two variants.\n", - " return fn(*args, **kwargs)\n", - "The input hidden states seems to be silently casted in float32, this might be related to the fact you have upcasted embedding or layer norm layers in float32. We will cast back the input in torch.bfloat16.\n" - ] - }, - { - "ename": "RuntimeError", - "evalue": "FlashAttention only supports Ampere GPUs or newer.", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mRuntimeError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Training...\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 6\u001b[0;31m \u001b[0mtrainer\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtrain\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/transformers/trainer.py\u001b[0m in \u001b[0;36mtrain\u001b[0;34m(self, resume_from_checkpoint, trial, ignore_keys_for_eval, **kwargs)\u001b[0m\n\u001b[1;32m 1927\u001b[0m \u001b[0;31m# Disable progress bars when uploading models during checkpoints to avoid polluting stdout\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1928\u001b[0m \u001b[0mhf_hub_utils\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdisable_progress_bars\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1929\u001b[0;31m return inner_training_loop(\n\u001b[0m\u001b[1;32m 1930\u001b[0m \u001b[0margs\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1931\u001b[0m \u001b[0mresume_from_checkpoint\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mresume_from_checkpoint\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/transformers/trainer.py\u001b[0m in \u001b[0;36m_inner_training_loop\u001b[0;34m(self, batch_size, args, resume_from_checkpoint, trial, ignore_keys_for_eval)\u001b[0m\n\u001b[1;32m 2277\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2278\u001b[0m \u001b[0;32mwith\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0maccelerator\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0maccumulate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmodel\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2279\u001b[0;31m \u001b[0mtr_loss_step\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtraining_step\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmodel\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minputs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2280\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2281\u001b[0m if (\n", - "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/transformers/trainer.py\u001b[0m in \u001b[0;36mtraining_step\u001b[0;34m(self, model, inputs)\u001b[0m\n\u001b[1;32m 3316\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3317\u001b[0m \u001b[0;32mwith\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcompute_loss_context_manager\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 3318\u001b[0;31m \u001b[0mloss\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcompute_loss\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmodel\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minputs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3319\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3320\u001b[0m \u001b[0;32mdel\u001b[0m \u001b[0minputs\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/transformers/trainer.py\u001b[0m in \u001b[0;36mcompute_loss\u001b[0;34m(self, model, inputs, return_outputs)\u001b[0m\n\u001b[1;32m 3361\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3362\u001b[0m \u001b[0mlabels\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 3363\u001b[0;31m \u001b[0moutputs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmodel\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m**\u001b[0m\u001b[0minputs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3364\u001b[0m \u001b[0;31m# Save past state if it exists\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3365\u001b[0m \u001b[0;31m# TODO: this needs to be fixed and made cleaner later.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py\u001b[0m in \u001b[0;36m_wrapped_call_impl\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 1551\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_compiled_call_impl\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# type: ignore[misc]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1552\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1553\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_call_impl\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1554\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1555\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m_call_impl\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py\u001b[0m in \u001b[0;36m_call_impl\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 1560\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0m_global_backward_pre_hooks\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0m_global_backward_hooks\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1561\u001b[0m or _global_forward_hooks or _global_forward_pre_hooks):\n\u001b[0;32m-> 1562\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mforward_call\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1563\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1564\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/accelerate/utils/operations.py\u001b[0m in \u001b[0;36mforward\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 818\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 819\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mforward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 820\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mmodel_forward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 821\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 822\u001b[0m \u001b[0;31m# To act like a decorator so that it can be popped when doing `extract_model_from_parallel`\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/accelerate/utils/operations.py\u001b[0m in \u001b[0;36m__call__\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 806\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 807\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m__call__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 808\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mconvert_to_fp32\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmodel_forward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 809\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 810\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m__getstate__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/torch/amp/autocast_mode.py\u001b[0m in \u001b[0;36mdecorate_autocast\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 41\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mdecorate_autocast\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 42\u001b[0m \u001b[0;32mwith\u001b[0m \u001b[0mautocast_instance\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 43\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mfunc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 44\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 45\u001b[0m \u001b[0mdecorate_autocast\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__script_unsupported\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"@autocast() decorator is not supported in script mode\"\u001b[0m \u001b[0;31m# type: ignore[attr-defined]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/peft/peft_model.py\u001b[0m in \u001b[0;36mforward\u001b[0;34m(self, input_ids, attention_mask, inputs_embeds, labels, output_attentions, output_hidden_states, return_dict, task_ids, **kwargs)\u001b[0m\n\u001b[1;32m 1642\u001b[0m \u001b[0;32mwith\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_enable_peft_forward_hooks\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1643\u001b[0m \u001b[0mkwargs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m{\u001b[0m\u001b[0mk\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mv\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mk\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mv\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mkwargs\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mitems\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mk\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mspecial_peft_forward_args\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1644\u001b[0;31m return self.base_model(\n\u001b[0m\u001b[1;32m 1645\u001b[0m \u001b[0minput_ids\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0minput_ids\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1646\u001b[0m \u001b[0mattention_mask\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mattention_mask\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py\u001b[0m in \u001b[0;36m_wrapped_call_impl\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 1551\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_compiled_call_impl\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# type: ignore[misc]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1552\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1553\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_call_impl\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1554\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1555\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m_call_impl\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py\u001b[0m in \u001b[0;36m_call_impl\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 1560\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0m_global_backward_pre_hooks\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0m_global_backward_hooks\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1561\u001b[0m or _global_forward_hooks or _global_forward_pre_hooks):\n\u001b[0;32m-> 1562\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mforward_call\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1563\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1564\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/peft/tuners/tuners_utils.py\u001b[0m in \u001b[0;36mforward\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 195\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 196\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mforward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mAny\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mAny\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 197\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmodel\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mforward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 198\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 199\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m_pre_injection_hook\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmodel\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mnn\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mModule\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mconfig\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mPeftConfig\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0madapter_name\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mstr\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m->\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/accelerate/hooks.py\u001b[0m in \u001b[0;36mnew_forward\u001b[0;34m(module, *args, **kwargs)\u001b[0m\n\u001b[1;32m 168\u001b[0m \u001b[0moutput\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmodule\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_old_forward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 169\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 170\u001b[0;31m \u001b[0moutput\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmodule\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_old_forward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 171\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mmodule\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_hf_hook\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpost_forward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmodule\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0moutput\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 172\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/transformers/models/gpt_bigcode/modeling_gpt_bigcode.py\u001b[0m in \u001b[0;36mforward\u001b[0;34m(self, input_ids, past_key_values, attention_mask, token_type_ids, position_ids, head_mask, inputs_embeds, encoder_hidden_states, encoder_attention_mask, labels, use_cache, output_attentions, output_hidden_states, return_dict)\u001b[0m\n\u001b[1;32m 1156\u001b[0m \u001b[0mreturn_dict\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mreturn_dict\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mreturn_dict\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32mNone\u001b[0m \u001b[0;32melse\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mconfig\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0muse_return_dict\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1157\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1158\u001b[0;31m transformer_outputs = self.transformer(\n\u001b[0m\u001b[1;32m 1159\u001b[0m \u001b[0minput_ids\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1160\u001b[0m \u001b[0mpast_key_values\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mpast_key_values\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py\u001b[0m in \u001b[0;36m_wrapped_call_impl\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 1551\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_compiled_call_impl\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# type: ignore[misc]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1552\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1553\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_call_impl\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1554\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1555\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m_call_impl\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py\u001b[0m in \u001b[0;36m_call_impl\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 1560\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0m_global_backward_pre_hooks\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0m_global_backward_hooks\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1561\u001b[0m or _global_forward_hooks or _global_forward_pre_hooks):\n\u001b[0;32m-> 1562\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mforward_call\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1563\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1564\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/accelerate/hooks.py\u001b[0m in \u001b[0;36mnew_forward\u001b[0;34m(module, *args, **kwargs)\u001b[0m\n\u001b[1;32m 168\u001b[0m \u001b[0moutput\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmodule\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_old_forward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 169\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 170\u001b[0;31m \u001b[0moutput\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmodule\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_old_forward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 171\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mmodule\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_hf_hook\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpost_forward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmodule\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0moutput\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 172\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/transformers/models/gpt_bigcode/modeling_gpt_bigcode.py\u001b[0m in \u001b[0;36mforward\u001b[0;34m(self, input_ids, past_key_values, attention_mask, token_type_ids, position_ids, head_mask, inputs_embeds, encoder_hidden_states, encoder_attention_mask, use_cache, output_attentions, output_hidden_states, return_dict)\u001b[0m\n\u001b[1;32m 979\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 980\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgradient_checkpointing\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtraining\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 981\u001b[0;31m outputs = self._gradient_checkpointing_func(\n\u001b[0m\u001b[1;32m 982\u001b[0m \u001b[0mblock\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__call__\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 983\u001b[0m \u001b[0mhidden_states\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/torch/_compile.py\u001b[0m in \u001b[0;36minner\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 29\u001b[0m \u001b[0mfn\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__dynamo_disable\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdisable_fn\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 30\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 31\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mdisable_fn\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 32\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 33\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0minner\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/torch/_dynamo/eval_frame.py\u001b[0m in \u001b[0;36m_fn\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 598\u001b[0m \u001b[0mprior\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mset_eval_frame\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcallback\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 599\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 600\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mfn\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 601\u001b[0m \u001b[0;32mfinally\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 602\u001b[0m \u001b[0mset_eval_frame\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mprior\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/torch/utils/checkpoint.py\u001b[0m in \u001b[0;36mcheckpoint\u001b[0;34m(function, use_reentrant, context_fn, determinism_check, debug, *args, **kwargs)\u001b[0m\n\u001b[1;32m 479\u001b[0m \u001b[0;34m\"use_reentrant=False.\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 480\u001b[0m )\n\u001b[0;32m--> 481\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mCheckpointFunction\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mapply\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfunction\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mpreserve\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 482\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 483\u001b[0m gen = _checkpoint_without_reentrant_generator(\n", - "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/torch/autograd/function.py\u001b[0m in \u001b[0;36mapply\u001b[0;34m(cls, *args, **kwargs)\u001b[0m\n\u001b[1;32m 572\u001b[0m \u001b[0;31m# See NOTE: [functorch vjp and autograd interaction]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 573\u001b[0m \u001b[0margs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0m_functorch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mutils\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0munwrap_dead_wrappers\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 574\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0msuper\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mapply\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# type: ignore[misc]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 575\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 576\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mis_setup_ctx_defined\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/torch/utils/checkpoint.py\u001b[0m in \u001b[0;36mforward\u001b[0;34m(ctx, run_function, preserve_rng_state, *args)\u001b[0m\n\u001b[1;32m 253\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 254\u001b[0m \u001b[0;32mwith\u001b[0m \u001b[0mtorch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mno_grad\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 255\u001b[0;31m \u001b[0moutputs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mrun_function\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 256\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0moutputs\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 257\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py\u001b[0m in \u001b[0;36m_wrapped_call_impl\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 1551\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_compiled_call_impl\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# type: ignore[misc]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1552\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1553\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_call_impl\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1554\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1555\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m_call_impl\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py\u001b[0m in \u001b[0;36m_call_impl\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 1560\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0m_global_backward_pre_hooks\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0m_global_backward_hooks\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1561\u001b[0m or _global_forward_hooks or _global_forward_pre_hooks):\n\u001b[0;32m-> 1562\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mforward_call\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1563\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1564\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/accelerate/hooks.py\u001b[0m in \u001b[0;36mnew_forward\u001b[0;34m(module, *args, **kwargs)\u001b[0m\n\u001b[1;32m 168\u001b[0m \u001b[0moutput\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmodule\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_old_forward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 169\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 170\u001b[0;31m \u001b[0moutput\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmodule\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_old_forward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 171\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mmodule\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_hf_hook\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpost_forward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmodule\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0moutput\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 172\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/transformers/models/gpt_bigcode/modeling_gpt_bigcode.py\u001b[0m in \u001b[0;36mforward\u001b[0;34m(self, hidden_states, layer_past, attention_mask, head_mask, encoder_hidden_states, encoder_attention_mask, use_cache, output_attentions, **kwargs)\u001b[0m\n\u001b[1;32m 606\u001b[0m \u001b[0mresidual\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mhidden_states\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 607\u001b[0m \u001b[0mhidden_states\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mln_1\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mhidden_states\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 608\u001b[0;31m attn_outputs = self.attn(\n\u001b[0m\u001b[1;32m 609\u001b[0m \u001b[0mhidden_states\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 610\u001b[0m \u001b[0mlayer_past\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mlayer_past\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py\u001b[0m in \u001b[0;36m_wrapped_call_impl\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 1551\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_compiled_call_impl\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# type: ignore[misc]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1552\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1553\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_call_impl\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1554\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1555\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m_call_impl\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py\u001b[0m in \u001b[0;36m_call_impl\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 1560\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0m_global_backward_pre_hooks\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0m_global_backward_hooks\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1561\u001b[0m or _global_forward_hooks or _global_forward_pre_hooks):\n\u001b[0;32m-> 1562\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mforward_call\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1563\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1564\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/accelerate/hooks.py\u001b[0m in \u001b[0;36mnew_forward\u001b[0;34m(module, *args, **kwargs)\u001b[0m\n\u001b[1;32m 168\u001b[0m \u001b[0moutput\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmodule\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_old_forward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 169\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 170\u001b[0;31m \u001b[0moutput\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmodule\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_old_forward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 171\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mmodule\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_hf_hook\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpost_forward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmodule\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0moutput\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 172\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/transformers/models/gpt_bigcode/modeling_gpt_bigcode.py\u001b[0m in \u001b[0;36mforward\u001b[0;34m(self, hidden_states, layer_past, attention_mask, head_mask, encoder_hidden_states, encoder_attention_mask, use_cache, output_attentions)\u001b[0m\n\u001b[1;32m 368\u001b[0m \u001b[0mvalue\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mvalue\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mto\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtarget_dtype\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 369\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 370\u001b[0;31m attn_output = _flash_attention_forward(\n\u001b[0m\u001b[1;32m 371\u001b[0m \u001b[0mquery\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 372\u001b[0m \u001b[0mkey\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/transformers/modeling_flash_attention_utils.py\u001b[0m in \u001b[0;36m_flash_attention_forward\u001b[0;34m(query_states, key_states, value_states, attention_mask, query_length, is_causal, dropout, position_ids, softmax_scale, sliding_window, use_top_left_mask, softcap, deterministic)\u001b[0m\n\u001b[1;32m 294\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 295\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 296\u001b[0;31m attn_output = flash_attn_func(\n\u001b[0m\u001b[1;32m 297\u001b[0m \u001b[0mquery_states\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkey_states\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mvalue_states\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdropout\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msoftmax_scale\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0msoftmax_scale\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcausal\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcausal\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mflash_kwargs\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 298\u001b[0m )\n", - "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/flash_attn/flash_attn_interface.py\u001b[0m in \u001b[0;36mflash_attn_func\u001b[0;34m(q, k, v, dropout_p, softmax_scale, causal, window_size, softcap, alibi_slopes, deterministic, return_attn_probs)\u001b[0m\n\u001b[1;32m 878\u001b[0m \u001b[0mpattern\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mnegative\u001b[0m \u001b[0mmeans\u001b[0m \u001b[0mthat\u001b[0m \u001b[0mlocation\u001b[0m \u001b[0mwas\u001b[0m \u001b[0mdropped\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnonnegative\u001b[0m \u001b[0mmeans\u001b[0m \u001b[0mit\u001b[0m \u001b[0mwas\u001b[0m \u001b[0mkept\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 879\u001b[0m \"\"\"\n\u001b[0;32m--> 880\u001b[0;31m return FlashAttnFunc.apply(\n\u001b[0m\u001b[1;32m 881\u001b[0m \u001b[0mq\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 882\u001b[0m \u001b[0mk\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/torch/autograd/function.py\u001b[0m in \u001b[0;36mapply\u001b[0;34m(cls, *args, **kwargs)\u001b[0m\n\u001b[1;32m 572\u001b[0m \u001b[0;31m# See NOTE: [functorch vjp and autograd interaction]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 573\u001b[0m \u001b[0margs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0m_functorch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mutils\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0munwrap_dead_wrappers\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 574\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0msuper\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mapply\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# type: ignore[misc]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 575\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 576\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mis_setup_ctx_defined\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/flash_attn/flash_attn_interface.py\u001b[0m in \u001b[0;36mforward\u001b[0;34m(ctx, q, k, v, dropout_p, softmax_scale, causal, window_size, softcap, alibi_slopes, deterministic, return_softmax)\u001b[0m\n\u001b[1;32m 544\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0msoftmax_scale\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 545\u001b[0m \u001b[0msoftmax_scale\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mq\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m**\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m0.5\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 546\u001b[0;31m out, q, k, v, out_padded, softmax_lse, S_dmask, rng_state = _flash_attn_forward(\n\u001b[0m\u001b[1;32m 547\u001b[0m \u001b[0mq\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 548\u001b[0m \u001b[0mk\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/flash_attn/flash_attn_interface.py\u001b[0m in \u001b[0;36m_flash_attn_forward\u001b[0;34m(q, k, v, dropout_p, softmax_scale, causal, window_size, softcap, alibi_slopes, return_softmax)\u001b[0m\n\u001b[1;32m 50\u001b[0m ):\n\u001b[1;32m 51\u001b[0m \u001b[0mq\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mk\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mv\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mmaybe_contiguous\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mx\u001b[0m \u001b[0;32min\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mq\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mk\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mv\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 52\u001b[0;31m out, q, k, v, out_padded, softmax_lse, S_dmask, rng_state = flash_attn_cuda.fwd(\n\u001b[0m\u001b[1;32m 53\u001b[0m \u001b[0mq\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 54\u001b[0m \u001b[0mk\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;31mRuntimeError\u001b[0m: FlashAttention only supports Ampere GPUs or newer." - ] - } - ], - "source": [ - "trainer = Trainer(\n", - " model=model, args=training_args, train_dataset=train_dataset, eval_dataset=eval_dataset\n", - ")\n", - "\n", - "print(\"Training...\")\n", - "trainer.train()\n" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "aAERlCnt1PEW" - }, - "source": [ - "Finally, you can push the fine-tuned model to your Hub repository to share with your team." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "1h7_AUTTDwE1" - }, - "outputs": [], - "source": [ - "trainer.push_to_hub()" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "KBVH7uFOM_UF" - }, - "source": [ - "## Inference\n", - "\n", - "Once the model is uploaded to Hub, we can use it for inference. To do so we first initialize the original base model and its tokenizer. Next, we need to merge the fine-duned weights with the base model." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "jtL37piINBFe" - }, - "outputs": [], - "source": [ - "from peft import PeftModel\n", - "import torch\n", - "\n", - "# load the original model first\n", - "tokenizer = AutoTokenizer.from_pretrained(MODEL, trust_remote_code=True)\n", - "base_model = AutoModelForCausalLM.from_pretrained(\n", - " MODEL,\n", - " quantization_config=None,\n", - " device_map=None,\n", - " trust_remote_code=True,\n", - " torch_dtype=torch.bfloat16,\n", - ").cuda()\n", - "\n", - "# merge fine-tuned weights with the base model\n", - "peft_model_id = f\"Your_HF_username/{OUTPUT_DIR}\"\n", - "model = PeftModel.from_pretrained(base_model, peft_model_id)\n", - "model.merge_and_unload()" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "3USQ2suvDi9M" - }, - "source": [ - "Now we can use the merged model for inference. For convenience, we'll define a `get_code_completion` - feel free to experiment with text generation parameters!" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "RoTGpNbjDeWI" - }, - "outputs": [], - "source": [ - "def get_code_completion(prefix, suffix):\n", - " text = prompt = f\"\"\"{prefix}{suffix}\"\"\"\n", - " model.eval()\n", - " outputs = model.generate(\n", - " input_ids=tokenizer(text, return_tensors=\"pt\").input_ids.cuda(),\n", - " max_new_tokens=128,\n", - " temperature=0.2,\n", - " top_k=50,\n", - " top_p=0.95,\n", - " do_sample=True,\n", - " repetition_penalty=1.0,\n", - " )\n", - " return tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "0kMJiGDfDrBf" - }, - "source": [ - "Now all we need to do to get code completion is call the `get_code_complete` function and pass the first few lines that we want to be completed as a prefix, and an empty string as a suffix." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "nXlco2_-YcvM" - }, - "outputs": [], - "source": [ - "prefix = \"\"\"from peft import LoraConfig, TaskType, get_peft_model\n", - "from transformers import AutoModelForCausalLM\n", - "peft_config = LoraConfig(\n", - "\"\"\"\n", - "suffix =\"\"\"\"\"\"\n", - "\n", - "print(get_code_completion(prefix, suffix))" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "Ql2563kGlnmu" - }, - "source": [ - "As someone who has just used the PEFT library earlier in this notebook, you can see that the generated result for creating a `LoraConfig` is rather good!\n", - "\n", - "If you go back to the cell where we instantiate the model for inference, and comment out the lines where we merge the fine-tuned weights, you can see what the original model would've generated for the exact same prefix:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "29xxp1eHTgJ9" - }, - "outputs": [], - "source": [ - "prefix = \"\"\"from peft import LoraConfig, TaskType, get_peft_model\n", - "from transformers import AutoModelForCausalLM\n", - "peft_config = LoraConfig(\n", - "\"\"\"\n", - "suffix =\"\"\"\"\"\"\n", - "\n", - "print(get_code_completion(prefix, suffix))" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "Pwy2ZC7U8Ema" - }, - "source": [ - "While it is Python syntax, you can see that the original model has no understanding of what a `LoraConfig` should be doing." - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "CATYE8pp2drQ" - }, - "source": [ - "To learn how this kind of fine-tuning compares to full fine-tuning, and how to use a model like this as your copilot in VS Code via Inference Endpoints, or locally, check out the [\"Personal Copilot: Train Your Own Coding Assistant\" blog post](https://huggingface.co/blog/personal-copilot). This notebook complements the original blog post.\n" - ] - } - ], - "metadata": { - "accelerator": "GPU", - "colab": { - "gpuType": "T4", - "machine_shape": "hm", - "provenance": [] - }, - "kernelspec": { - "display_name": "Python 3", - "name": "python3" - }, - "language_info": { - "name": "python", - "version": "3.10.4" - }, - "widgets": { - "application/vnd.jupyter.widget-state+json": { - "02ecdae95dcf47648b1898bddd68263d": { - "model_module": "@jupyter-widgets/base", - "model_module_version": "1.2.0", - "model_name": "LayoutModel", - "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": "center", - "align_self": null, - "border": null, - "bottom": null, - "display": "flex", - "flex": null, - "flex_flow": "column", - "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": "50%" - } - }, - "20f45ad1b5404dd1b35e2c81696e752d": { - "model_module": "@jupyter-widgets/controls", - "model_module_version": "1.5.0", - "model_name": "HTMLModel", - "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_e50a4cc3e2b14b0b8ca6cb0769a4ddb1", - "placeholder": "​", - "style": "IPY_MODEL_609e92a6fa5243c7ae35b07ef14f9cf5", - "value": "

Copy a token from your Hugging Face\ntokens page and paste it below.
Immediately click login after copying\nyour token or it might be stored in plain text in this notebook file.
" - } - }, - "3e422885d1c24a38a5af8d50d9f2cbb7": { - "model_module": "@jupyter-widgets/base", - "model_module_version": "1.2.0", - "model_name": "LayoutModel", - "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 - } - }, - "5770ef4c8d3f47d7b6e38d1c0e690cce": { - "model_module": "@jupyter-widgets/base", - "model_module_version": "1.2.0", - "model_name": "LayoutModel", - "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 - } - }, - "609e92a6fa5243c7ae35b07ef14f9cf5": { - "model_module": "@jupyter-widgets/controls", - "model_module_version": "1.5.0", - "model_name": "DescriptionStyleModel", - "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": "" - } - }, - "65e4da277e6f4fc0bec087273ad6bce6": { - "model_module": "@jupyter-widgets/controls", - "model_module_version": "1.5.0", - "model_name": "VBoxModel", - "state": { - "_dom_classes": [], - "_model_module": "@jupyter-widgets/controls", - "_model_module_version": "1.5.0", - "_model_name": "VBoxModel", - "_view_count": null, - "_view_module": "@jupyter-widgets/controls", - "_view_module_version": "1.5.0", - "_view_name": "VBoxView", - "box_style": "", - "children": [ - "IPY_MODEL_20f45ad1b5404dd1b35e2c81696e752d", - "IPY_MODEL_fb6c47e03a37422ea81a09805d4742e4", - "IPY_MODEL_6693fb7e2bc5433bba04c80b6eb40ad6", - "IPY_MODEL_80305f2c23194f85ba0219856af7839a", - "IPY_MODEL_8157516cdf984c1a9a09b05288df0b42" - ], - "layout": "IPY_MODEL_02ecdae95dcf47648b1898bddd68263d" - } - }, - "6693fb7e2bc5433bba04c80b6eb40ad6": { - "model_module": "@jupyter-widgets/controls", - "model_module_version": "1.5.0", - "model_name": "CheckboxModel", - "state": { - "_dom_classes": [], - "_model_module": "@jupyter-widgets/controls", - "_model_module_version": "1.5.0", - "_model_name": "CheckboxModel", - "_view_count": null, - "_view_module": "@jupyter-widgets/controls", - "_view_module_version": "1.5.0", - "_view_name": "CheckboxView", - "description": "Add token as git credential?", - "description_tooltip": null, - "disabled": false, - "indent": true, - "layout": "IPY_MODEL_3e422885d1c24a38a5af8d50d9f2cbb7", - "style": "IPY_MODEL_83ec2e09b2954eb4a5561a1180ff2568", - "value": true - } - }, - "80305f2c23194f85ba0219856af7839a": { - "model_module": "@jupyter-widgets/controls", - "model_module_version": "1.5.0", - "model_name": "ButtonModel", - "state": { - "_dom_classes": [], - "_model_module": "@jupyter-widgets/controls", - "_model_module_version": "1.5.0", - "_model_name": "ButtonModel", - "_view_count": null, - "_view_module": "@jupyter-widgets/controls", - "_view_module_version": "1.5.0", - "_view_name": "ButtonView", - "button_style": "", - "description": "Login", - "disabled": false, - "icon": "", - "layout": "IPY_MODEL_b111d185e6ed42c0beeaed4db567745c", - "style": "IPY_MODEL_982f0140aced4ac38f746f3f35f657d3", - "tooltip": "" - } - }, - "8157516cdf984c1a9a09b05288df0b42": { - "model_module": "@jupyter-widgets/controls", - "model_module_version": "1.5.0", - "model_name": "HTMLModel", - "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_5770ef4c8d3f47d7b6e38d1c0e690cce", - "placeholder": "​", - "style": "IPY_MODEL_e82489e884464c8588447f74fd8fa97e", - "value": "\nPro Tip: If you don't already have one, you can create a dedicated\n'notebooks' token with 'write' access, that you can then easily reuse for all\nnotebooks. " - } - }, - "83ec2e09b2954eb4a5561a1180ff2568": { - "model_module": "@jupyter-widgets/controls", - "model_module_version": "1.5.0", - "model_name": "DescriptionStyleModel", - "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": "" - } - }, - "982f0140aced4ac38f746f3f35f657d3": { - "model_module": "@jupyter-widgets/controls", - "model_module_version": "1.5.0", - "model_name": "ButtonStyleModel", - "state": { - "_model_module": "@jupyter-widgets/controls", - "_model_module_version": "1.5.0", - "_model_name": "ButtonStyleModel", - "_view_count": null, - "_view_module": "@jupyter-widgets/base", - "_view_module_version": "1.2.0", - "_view_name": "StyleView", - "button_color": null, - "font_weight": "" - } - }, - "a65cd74b61c2435ea23fc1cae68d7156": { - "model_module": "@jupyter-widgets/controls", - "model_module_version": "1.5.0", - "model_name": "DescriptionStyleModel", - "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": "" - } - }, - "b111d185e6ed42c0beeaed4db567745c": { - "model_module": "@jupyter-widgets/base", - "model_module_version": "1.2.0", - "model_name": "LayoutModel", - "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 - } - }, - "e367a1e3164d4e13a4e5ad477e9ac625": { - "model_module": "@jupyter-widgets/base", - "model_module_version": "1.2.0", - "model_name": "LayoutModel", - "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 - } - }, - "e50a4cc3e2b14b0b8ca6cb0769a4ddb1": { - "model_module": "@jupyter-widgets/base", - "model_module_version": "1.2.0", - "model_name": "LayoutModel", - "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 - } - }, - "e82489e884464c8588447f74fd8fa97e": { - "model_module": "@jupyter-widgets/controls", - "model_module_version": "1.5.0", - "model_name": "DescriptionStyleModel", - "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": "" - } - }, - "fb6c47e03a37422ea81a09805d4742e4": { - "model_module": "@jupyter-widgets/controls", - "model_module_version": "1.5.0", - "model_name": "PasswordModel", - "state": { - "_dom_classes": [], - "_model_module": "@jupyter-widgets/controls", - "_model_module_version": "1.5.0", - "_model_name": "PasswordModel", - "_view_count": null, - "_view_module": "@jupyter-widgets/controls", - "_view_module_version": "1.5.0", - "_view_name": "PasswordView", - "continuous_update": true, - "description": "Token:", - "description_tooltip": null, - "disabled": false, - "layout": "IPY_MODEL_e367a1e3164d4e13a4e5ad477e9ac625", - "placeholder": "​", - "style": "IPY_MODEL_a65cd74b61c2435ea23fc1cae68d7156", - "value": "" - } - } - } - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} diff --git a/sage/fine_tuning_code_llm_on_single_gpu.py b/sage/fine_tuning_code_llm_on_single_gpu.py index 8f8f291..8c07773 100644 --- a/sage/fine_tuning_code_llm_on_single_gpu.py +++ b/sage/fine_tuning_code_llm_on_single_gpu.py @@ -28,6 +28,10 @@ from peft import PeftModel from peft import LoraConfig, get_peft_model, prepare_model_for_kbit_training +""" +Adapted from https://huggingface.co/learn/cookbook/en/fine_tuning_code_llm_on_single_gpu +""" + parser = argparse.ArgumentParser() parser.add_argument("--model", type=str, default="bigcode/starcoderbase-1b", help="Model checkpoint on the Hugging Face Hub") parser.add_argument("--dataset", type=str, default="smangrul/hf-stack-v1", help="Dataset on the Hugging Face Hub") @@ -362,16 +366,6 @@ def __iter__(self): with open('eval_dataset.pkl', 'wb') as f: pickle.dump(eval_dataset, f) - - # ## Prepare the model - # Now that the data is prepared, it's time to load the model! We're going to load the quantized version of the model. - # This will allow us to reduce memory usage, as quantization represents data with fewer bits. We'll use the `bitsandbytes` library to quantize the model, as it has a nice integration with `transformers`. All we need to do is define a `bitsandbytes` config, and then use it when loading the model. - # There are different variants of 4bit quantization, but generally, we recommend using NF4 quantization for better performance (`bnb_4bit_quant_type="nf4"`). - # The `bnb_4bit_use_double_quant` option adds a second quantization after the first one to save an additional 0.4 bits per parameter. - # To learn more about quantization, check out the ["Making LLMs even more accessible with bitsandbytes, 4-bit quantization and QLoRA" blog post](https://huggingface.co/blog/4bit-transformers-bitsandbytes). - # Once defined, pass the config to the `from_pretrained` method to load the quantized version of the model. - - # 4-bit quantization compute_dtype = getattr(torch, args.bnb_4bit_compute_dtype) @@ -397,10 +391,6 @@ def __iter__(self): # When using a quantized model for training, you need to call the `prepare_model_for_kbit_training()` function to preprocess the quantized model for training. model = prepare_model_for_kbit_training(model) - # Now that the quantized model is ready, we can set up a LoRA configuration. LoRA makes fine-tuning more efficient by drastically reducing the number of trainable parameters. - # To train a model using LoRA technique, we need to wrap the base model as a `PeftModel`. This involves definign LoRA configuration with `LoraConfig`, and wrapping the original model with `get_peft_model()` using the `LoraConfig`. - # To learn more about LoRA and its parameters, refer to [PEFT documentation](https://huggingface.co/docs/peft/main/en/conceptual_guides/lora). - # Set up lora peft_config = LoraConfig( lora_alpha=args.lora_alpha, @@ -463,3 +453,5 @@ def __iter__(self): # Finally, you can push the fine-tuned model to your Hub repository to share with your team. trainer.push_to_hub() wandb.finish() + + # TODO (mihail): Add appropriate eval metrics From e09c0186c03ee0dfc57a25cac636480b39f413af Mon Sep 17 00:00:00 2001 From: Mihail Eric Date: Wed, 9 Oct 2024 03:32:34 +0000 Subject: [PATCH 6/6] remove comment --- sage/fine_tuning_code_llm_on_single_gpu.py | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/sage/fine_tuning_code_llm_on_single_gpu.py b/sage/fine_tuning_code_llm_on_single_gpu.py index 8c07773..98451f8 100644 --- a/sage/fine_tuning_code_llm_on_single_gpu.py +++ b/sage/fine_tuning_code_llm_on_single_gpu.py @@ -404,16 +404,6 @@ def __iter__(self): model = get_peft_model(model, peft_config) model.print_trainable_parameters() - # As you can see, by applying LoRA technique we will now need to train less than 1% of the parameters. - - # ## Train the model - # Now that we have prepared the data, and optimized the model, we are ready to bring everything together to start the training. - # - # To instantiate a `Trainer`, you need to define the training configuration. The most important is the `TrainingArguments`, which is a class that contains all the attributes to configure the training. - # - # These are similar to any other kind of model training you may run, so we won't go into detail here. - - train_data.start_iteration = 0 training_args = TrainingArguments( output_dir=f"runs/{args.run_name}/{args.output_dir}",