Nitral-AI commited on
Commit
3ac7107
·
verified ·
1 Parent(s): 44e32c4

Upload Nitral_Token_Surgeon_Example.ipynb

Browse files
TokenSurgeon-Example/Nitral_Token_Surgeon_Example.ipynb ADDED
@@ -0,0 +1,182 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "nbformat": 4,
3
+ "nbformat_minor": 0,
4
+ "metadata": {
5
+ "colab": {
6
+ "provenance": [],
7
+ "gpuType": "L4",
8
+ "machine_shape": "hm"
9
+ },
10
+ "kernelspec": {
11
+ "name": "python3",
12
+ "display_name": "Python 3"
13
+ },
14
+ "language_info": {
15
+ "name": "python"
16
+ },
17
+ "accelerator": "GPU"
18
+ },
19
+ "cells": [
20
+ {
21
+ "cell_type": "markdown",
22
+ "source": [
23
+ "# Download / install required dependencies."
24
+ ],
25
+ "metadata": {
26
+ "id": "4nHRbUX16c0C"
27
+ }
28
+ },
29
+ {
30
+ "cell_type": "code",
31
+ "source": [
32
+ "# Clone and install\n",
33
+ "!git clone https://github.com/cg123/mergekit.git\n",
34
+ "!cd mergekit && pip install -q -e .\n",
35
+ "!cd ..\n",
36
+ "\n",
37
+ "# Remove git-related files to clean up\n",
38
+ "!rm -rf mergekit/.git mergekit/.gitignore mergekit/.gitattributes\n",
39
+ "\n",
40
+ "# Optional: clean README, .md files, tests, etc., if you want minimal bloat\n",
41
+ "!rm -rf mergekit/tests mergekit/*.md\n",
42
+ "\n",
43
+ "# Install other dependencies\n",
44
+ "!pip install huggingface_hub hf_xet"
45
+ ],
46
+ "metadata": {
47
+ "id": "XQm5_Xtz09yE"
48
+ },
49
+ "execution_count": null,
50
+ "outputs": []
51
+ },
52
+ {
53
+ "cell_type": "markdown",
54
+ "source": [
55
+ "# Download HF Models to ./cache/"
56
+ ],
57
+ "metadata": {
58
+ "id": "a3HqMjNQ6a01"
59
+ }
60
+ },
61
+ {
62
+ "cell_type": "code",
63
+ "execution_count": null,
64
+ "metadata": {
65
+ "id": "WTa6H9ij06FX"
66
+ },
67
+ "outputs": [],
68
+ "source": [
69
+ "from huggingface_hub import snapshot_download\n",
70
+ "\n",
71
+ "model_names = [\n",
72
+ " \"Nitral-AI/Irixxed-Magcap-12B-0.1a\",\n",
73
+ " \"Entropicengine/Pinecone-Rune-12b\"\n",
74
+ "]\n",
75
+ "\n",
76
+ "for model_name in model_names:\n",
77
+ " # Set cache_dir to avoid loading into memory\n",
78
+ " cache_dir = f\"./cache/{model_name.replace('/', '_')}\"\n",
79
+ "\n",
80
+ " # Download the entire repository using snapshot_download\n",
81
+ " snapshot_download(repo_id=model_name, local_dir=cache_dir, local_dir_use_symlinks=False)"
82
+ ]
83
+ },
84
+ {
85
+ "cell_type": "markdown",
86
+ "source": [
87
+ " # Clean pip & HF junk so system disk doesn't run out on t4.\n"
88
+ ],
89
+ "metadata": {
90
+ "id": "KHcohLhKVMfn"
91
+ }
92
+ },
93
+ {
94
+ "cell_type": "code",
95
+ "source": [
96
+ "!pip cache purge\n",
97
+ "!rm -rf /root/.cache/huggingface\n",
98
+ "!rm -rf ~/.cache/pip\n",
99
+ "!rm -rf ~/.cache/torch_extensions\n",
100
+ "!rm -rf ~/.nv"
101
+ ],
102
+ "metadata": {
103
+ "id": "dICO9UxoVJ1l",
104
+ "colab": {
105
+ "base_uri": "https://localhost:8080/"
106
+ },
107
+ "outputId": "19877723-ce37-49d5-a8c2-b81a258de863"
108
+ },
109
+ "execution_count": 3,
110
+ "outputs": [
111
+ {
112
+ "output_type": "stream",
113
+ "name": "stdout",
114
+ "text": [
115
+ "Files removed: 90\n"
116
+ ]
117
+ }
118
+ ]
119
+ },
120
+ {
121
+ "cell_type": "markdown",
122
+ "source": [
123
+ "#Start Merge-kit's Token-Surgeon with --cosine-similarity -v -k 64 --cuda --low-cpu-memory so it works on a t4.\n",
124
+ "./cache/ for model download location\n",
125
+ "\n",
126
+ "Base model, donor model. [Donor embeddings are mixed using a linear relationship based on similarity and then applied to base]"
127
+ ],
128
+ "metadata": {
129
+ "id": "H6gBOEjA6VhN"
130
+ }
131
+ },
132
+ {
133
+ "cell_type": "code",
134
+ "source": [
135
+ "!mergekit-tokensurgeon ./cache/Entropicengine_Pinecone-Rune-12b ./cache/Nitral-AI_Irixxed-Magcap-12B-0.1a ./postop -v -k 64 --cosine-similarity --cuda --low-cpu-memory\n"
136
+ ],
137
+ "metadata": {
138
+ "id": "vfjXBAGV1qVr"
139
+ },
140
+ "execution_count": null,
141
+ "outputs": []
142
+ },
143
+ {
144
+ "cell_type": "markdown",
145
+ "source": [
146
+ "#Upload to Huggingface"
147
+ ],
148
+ "metadata": {
149
+ "id": "JfYTwAzr6PR8"
150
+ }
151
+ },
152
+ {
153
+ "cell_type": "code",
154
+ "source": [
155
+ "from google.colab import userdata\n",
156
+ "from huggingface_hub import HfApi, HfFolder\n",
157
+ "\n",
158
+ "# Fetch token from Colab secrets\n",
159
+ "HF_TOKEN = userdata.get('HF_TOKEN') # Now it's assigned\n",
160
+ "\n",
161
+ "# Initialize API with token\n",
162
+ "api = HfApi(token=HF_TOKEN)\n",
163
+ "\n",
164
+ "repo_name = \"Nitral-AI/Pinecone-Rune-12b-Token-Surgery-Chatml\"\n",
165
+ "\n",
166
+ "# Create the repo if it doesn't already exist\n",
167
+ "api.create_repo(repo_id=repo_name, private=True, exist_ok=True)\n",
168
+ "\n",
169
+ "# Upload the local folder contents to the repo\n",
170
+ "api.upload_folder(\n",
171
+ " folder_path=\"./postop\",\n",
172
+ " repo_id=repo_name,\n",
173
+ ")"
174
+ ],
175
+ "metadata": {
176
+ "id": "zKoL6Ouf4pLI"
177
+ },
178
+ "execution_count": null,
179
+ "outputs": []
180
+ }
181
+ ]
182
+ }