Virt-io commited on
Commit
eeb0016
1 Parent(s): f9a24f6

Upload Imat_AutoGGUF.ipynb

Browse files
Files changed (1) hide show
  1. Imat_AutoGGUF.ipynb +127 -0
Imat_AutoGGUF.ipynb ADDED
@@ -0,0 +1,127 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "nbformat": 4,
3
+ "nbformat_minor": 0,
4
+ "metadata": {
5
+ "colab": {
6
+ "provenance": [],
7
+ "gpuType": "T4"
8
+ },
9
+ "kernelspec": {
10
+ "name": "python3",
11
+ "display_name": "Python 3"
12
+ },
13
+ "language_info": {
14
+ "name": "python"
15
+ },
16
+ "accelerator": "GPU"
17
+ },
18
+ "cells": [
19
+ {
20
+ "cell_type": "code",
21
+ "source": [
22
+ "# @title # ⚡ Imat-AutoGGUF\n",
23
+ "\n",
24
+ "# @markdown Made by https://huggingface.co/Virt-io\n",
25
+ "\n",
26
+ "# @markdown Edited https://github.com/mlabonne/llm-course LazyMergekit to work with Imatrix\n",
27
+ "\n",
28
+ "# @markdown\n",
29
+ "\n",
30
+ "# @markdown The `token` corresponds to the name of the secret that stores your [Hugging Face access token](https://huggingface.co/settings/tokens) in Colab.\n",
31
+ "\n",
32
+ "# @markdown ---\n",
33
+ "\n",
34
+ "# @markdown ### ⚡ Quantization parameters\n",
35
+ "MODEL_ID = \"TinyLlama/TinyLlama-1.1B-Chat-v1.0\" # @param {type:\"string\"}\n",
36
+ "IMATRIX_OPTION = 'Imatrix' # @param [\"Imatrix\", \"Imatrix-RP\"]\n",
37
+ "if IMATRIX_OPTION == \"Imatrix\":\n",
38
+ " IMATRIX = f\"Google-Colab-Imatrix-GGUF/Imatrix/imatrix.txt\"\n",
39
+ "if IMATRIX_OPTION == \"Imatrix-RP\":\n",
40
+ " IMATRIX = f\"Google-Colab-Imatrix-GGUF/Imatrix/imatrix-with-rp-data.txt\"\n",
41
+ "print(IMATRIX)\n",
42
+ "QUANTIZATION_METHODS = \"IQ4_NL, Q8_0\" # @param {type:\"string\"}\n",
43
+ "QUANTIZATION_METHODS = QUANTIZATION_METHODS.replace(\" \", \"\").split(\",\")\n",
44
+ "\n",
45
+ "# @markdown ---\n",
46
+ "\n",
47
+ "# @markdown ### 🤗 Hugging Face Hub\n",
48
+ "username = \"Virt-io\" # @param {type:\"string\"}\n",
49
+ "token = \"HF_TOKEN\" # @param {type:\"string\"}\n",
50
+ "\n",
51
+ "MODEL_NAME = MODEL_ID.split('/')[-1]\n",
52
+ "\n",
53
+ "# Git clone llamacpp\n",
54
+ "!git clone https://github.com/ggerganov/llama.cpp\n",
55
+ "!cd llama.cpp && git pull\n",
56
+ "\n",
57
+ "# Download model\n",
58
+ "!git lfs install\n",
59
+ "!git clone https://huggingface.co/{MODEL_ID}\n",
60
+ "\n",
61
+ "# Download Imatrix\n",
62
+ "!git clone https://huggingface.co/Virt-io/Google-Colab-Imatrix-GGUF\n",
63
+ "\n",
64
+ "# Install python dependencies and reload instance\n",
65
+ "!pip install -r llama.cpp/requirements/requirements-convert.txt\n",
66
+ "\n",
67
+ "# Build llamacpp\n",
68
+ "!cd llama.cpp && make clean && LLAMA_CUBLAS=1 LLAMA_CUDA_FORCE_MMQ=1 LLAMA_LTO=1 LLAMA_CUDA_DMMV_X=64 LLAMA_CUDA_MMV_Y=4 LLAMA_CUDA_KQUANTS_ITER=2 LLAMA_CUDA_F16=1 LLAMA_CUDA_DMMV_F16=1 make -j16\n",
69
+ "\n",
70
+ "# Convert to fp16\n",
71
+ "fp16 = f\"{MODEL_NAME}/{MODEL_NAME.lower()}.fp16.gguf\"\n",
72
+ "!python llama.cpp/convert.py {MODEL_NAME} --outtype f16 --outfile {fp16}\n",
73
+ "\n",
74
+ "# Run imatrix\n",
75
+ "imat_dat = f\"{fp16}.{IMATRIX_OPTION}.dat\"\n",
76
+ "!./llama.cpp/imatrix -ngl 100 -c 512 -b 512 --model {fp16} -f {IMATRIX} -o {imat_dat}\n",
77
+ "\n",
78
+ "# Quantize the model for each method in the QUANTIZATION_METHODS list\n",
79
+ "for method in QUANTIZATION_METHODS:\n",
80
+ " qtype = f\"{MODEL_NAME}/{MODEL_NAME.lower()}.{method.upper()}.gguf\"\n",
81
+ " !./llama.cpp/quantize --imatrix {imat_dat} {fp16} {qtype} {method}"
82
+ ],
83
+ "metadata": {
84
+ "id": "fD24jJxq7t3k"
85
+ },
86
+ "execution_count": null,
87
+ "outputs": []
88
+ },
89
+ {
90
+ "cell_type": "code",
91
+ "source": [
92
+ "# @markdown Upload to HF\n",
93
+ "!pip install -q huggingface_hub\n",
94
+ "from huggingface_hub import create_repo, HfApi\n",
95
+ "from google.colab import userdata, runtime\n",
96
+ "\n",
97
+ "# Defined in the secrets tab in Google Colab\n",
98
+ "hf_token = userdata.get(token)\n",
99
+ "api = HfApi()\n",
100
+ "\n",
101
+ "# Create empty repo\n",
102
+ "create_repo(\n",
103
+ " repo_id = f\"{username}/{MODEL_NAME}-GGUF\",\n",
104
+ " repo_type=\"model\",\n",
105
+ " exist_ok=True,\n",
106
+ " token=hf_token\n",
107
+ ")\n",
108
+ "\n",
109
+ "# Upload gguf files\n",
110
+ "api.upload_folder(\n",
111
+ " folder_path=MODEL_NAME,\n",
112
+ " repo_id=f\"{username}/{MODEL_NAME}-GGUF\",\n",
113
+ " allow_patterns=[\"*.gguf\", \"*.fp16.gguf\", \"*.dat\", \"*.md\"],\n",
114
+ " token=hf_token\n",
115
+ ")\n",
116
+ "\n",
117
+ "# Kill runtime\n",
118
+ "# runtime.unassign()"
119
+ ],
120
+ "metadata": {
121
+ "id": "F7Q8_Y1_e3BX"
122
+ },
123
+ "execution_count": null,
124
+ "outputs": []
125
+ }
126
+ ]
127
+ }