File size: 9,004 Bytes
d303ab6 fa0da4d d303ab6 fa0da4d d303ab6 aa3ba77 d303ab6 fa0da4d d303ab6 fa0da4d d303ab6 fa0da4d d303ab6 fa0da4d d303ab6 fa0da4d d303ab6 aa3ba77 d303ab6 fa0da4d d303ab6 fa0da4d d303ab6 fa0da4d d303ab6 fa0da4d d303ab6 fa0da4d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "Ku0ezvyD42ng"
},
"source": [
"#Quantizing huggingface models to exl2\n",
"This version of my exl2 quantize colab creates a single quantizaion to upload privatly.\\\n",
"To calculate an estimate for VRAM size use: [NyxKrage/LLM-Model-VRAM-Calculator](https://huggingface.co/spaces/NyxKrage/LLM-Model-VRAM-Calculator)\\\n",
"Not all models and architectures are compatible with exl2."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cellView": "form",
"id": "G7zSk2LWHtPU"
},
"outputs": [],
"source": [
"#@title Download and install environment\n",
"!git clone https://github.com/turboderp/exllamav2\n",
"%cd exllamav2\n",
"print(\"Installing pip dependencies\")\n",
"!pip install -q -r requirements.txt\n",
"!pip install -q huggingface_hub requests tqdm\n",
"!pip install . -q\n",
"#@markdown Uses [download-model.py](https://github.com/oobabooga/text-generation-webui/blob/main/download-model.py) by [oobabooga](https://github.com/oobabooga)\n",
"!wget https://raw.githubusercontent.com/oobabooga/text-generation-webui/main/download-model.py\n",
"model = \"none\"\n",
"dsd = 'false'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cellView": "form",
"id": "8Hl3fQmRLybp"
},
"outputs": [],
"source": [
"#@title Login to HF (Required to upload files)\n",
"#@markdown From my Colab/Kaggle login script on [Anthonyg5005/hf-scripts](https://huggingface.co/Anthonyg5005/hf-scripts/blob/main/HF%20Login%20Snippet%20Kaggle.py)\n",
"#import required functions\n",
"import os\n",
"from huggingface_hub import login, get_token, whoami\n",
"\n",
"#get token\n",
"if os.environ.get('KAGGLE_KERNEL_RUN_TYPE', None) is not None: #check if user in kaggle\n",
" from kaggle_secrets import UserSecretsClient\n",
" from kaggle_web_client import BackendError\n",
" try:\n",
" login(UserSecretsClient().get_secret(\"HF_TOKEN\")) #login if token secret found\n",
" except BackendError:\n",
" print('''\n",
" When using Kaggle, make sure to use the secret key HF_TOKEN with a 'WRITE' token.\n",
" This will prevent the need to login every time you run the script.\n",
" Set your secrets with the secrets add-on on the top of the screen.\n",
" ''')\n",
"if get_token() is not None:\n",
" #if the token is found then log in:\n",
" login(get_token())\n",
"else:\n",
" #if the token is not found then prompt user to provide it:\n",
" login(input(\"API token not detected. Enter your HuggingFace (WRITE) token: \"))\n",
"\n",
"#if the token is read only then prompt user to provide a write token (Only required if user needs a WRITE token, remove if READ is enough):\n",
"while True:\n",
" if whoami().get('auth', {}).get('accessToken', {}).get('role', None) != 'write':\n",
" if os.environ.get('HF_TOKEN', None) is not None: #if environ finds HF_TOKEN as read-only then display following text and exit:\n",
" print('''\n",
" You have the environment variable HF_TOKEN set.\n",
" You cannot log in.\n",
" Either set the environment variable to a 'WRITE' token or remove it.\n",
" ''')\n",
" input(\"Press enter to continue.\")\n",
" exit()\n",
" if os.environ.get('COLAB_BACKEND_VERSION', None) is not None:\n",
" print('''\n",
" Your Colab secret key is read-only\n",
" Please switch your key to 'write' or disable notebook access on the left.\n",
" For now, you are stuck in a loop\n",
" ''')\n",
" elif os.environ.get('KAGGLE_KERNEL_RUN_TYPE', None) is not None:\n",
" print('''\n",
" Your Kaggle secret key is read-only\n",
" Please switch your key to 'write' or unattach from notebook in add-ons at the top.\n",
" Having a read-only key attched will require login every time.\n",
" ''')\n",
" print(\"You do not have write access to this repository. Please use a valid token with (WRITE) access.\")\n",
" login(input(\"Enter your HuggingFace (WRITE) token: \"))\n",
" continue\n",
" break"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cellView": "form",
"id": "NI1LUMD7H-Zx"
},
"outputs": [],
"source": [
"#@title ##Choose HF model to download\n",
"#@markdown ###Repo should be formatted as user/repo\n",
"#@markdown Weights must be stored in safetensors\n",
"if model != \"none\":\n",
" !rm {model}-{BPW}bpw.zip\n",
" !rm -r {model}-exl2-{BPW}bpw\n",
"repo_url = \"mistralai/Mistral-7B-Instruct-v0.2\" # @param {type:\"string\"}\n",
"model = repo_url.replace(\"/\", \"_\")\n",
"!python download-model.py {repo_url}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cellView": "form",
"id": "8anbEbGyNmBI"
},
"outputs": [],
"source": [
"#@title Quantize the model\n",
"#@markdown ###Quantization time will last based on model size\n",
"#@markdown Target bits per weight:\n",
"BPW = \"4.125\" # @param {type:\"string\"}\n",
"!mkdir {model}-exl2-{BPW}bpw-WD\n",
"!mkdir {model}-exl2-{BPW}bpw\n",
"!cp models/{model}/config.json {model}-exl2-{BPW}bpw-WD\n",
"#@markdown Calibrate with dataset, may improve model output (optional):\n",
"Calibrate = True # @param {type:\"boolean\"}\n",
"#@markdown Calibration dataset, enable calibrate above (must be parquet file):\n",
"if Calibrate == True:\n",
" dataset_url = \"https://huggingface.co/datasets/wikitext/resolve/refs%2Fconvert%2Fparquet/wikitext-103-v1/test/0000.parquet?download=true\" # @param {type:\"string\"}\n",
" dataset_url = dataset_url.replace(\"?download=true\", \"\")\n",
" if dsd == 'false':\n",
" !wget {dataset_url}\n",
" dsd = 'true'\n",
" dataset = dataset_url.split(\"/\")[-1]\n",
"#@markdown To use a calibration dataset, enter the huggingface resolve url. Right click the download button and copy the link. Afterwards, paste the link into dataset_url.\n",
"#@markdown ![Example Image](https://huggingface.co/Anthonyg5005/hf-scripts/resolve/main/ipynb/dataset-example.jpg \"Copy from download button\")\n",
"if Calibrate == True:\n",
" quant = f\"convert.py -i models/{model} -o {model}-exl2-{BPW}bpw-WD -cf {model}-exl2-{BPW}bpw -c {dataset} -b {BPW}\"\n",
"else:\n",
" quant = f\"convert.py -i models/{model} -o {model}-exl2-{BPW}bpw-WD -cf {model}-exl2-{BPW}bpw -b {BPW}\"\n",
"!python {quant}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cellView": "form",
"id": "XORLS2uPrbma"
},
"outputs": [],
"source": [
"#@title Upload to huggingface privately\n",
"#@markdown You may also set it to public but I'd recommend waiting for my next ipynb that will create mutliple quants and place them all into individual branches.\n",
"!rm -r {model}-exl2-{BPW}bpw-WD\n",
"!rm -r models/{model}\n",
"print(\"Uploading to Huggingface. May take a while\")\n",
"from huggingface_hub import HfApi, whoami, create_repo\n",
"create_repo(f\"{whoami().get('name', None)}/{model}-exl2-{BPW}bpw\", private=True)\n",
"HfApi().upload_folder(folder_path=f\"{model}-exl2-{BPW}bpw\", repo_id=f\"{whoami().get('name', None)}/{model}-exl2-{BPW}bpw\", repo_type=\"model\", commit_message=\"Upload from Colab automation\")\n",
"print(f\"uploaded to {whoami().get('name', None)}/{model}-exl2-{BPW}bpw\")"
]
}
],
"metadata": {
"accelerator": "GPU",
"colab": {
"gpuType": "T4",
"provenance": []
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
},
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
|