Rishi-Jain-27 commited on
Commit
2b8e4a2
·
1 Parent(s): 5c99c3d

Added a kaggle notebook for training

Browse files
notebooks/qwen3_coder_codeflow_gguf_to_kaggle.ipynb ADDED
@@ -0,0 +1,205 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "metadata": {},
6
+ "source": [
7
+ "# Fine-tune Qwen3-Coder and Upload Q3_K_XL GGUF to Kaggle\n",
8
+ "\n",
9
+ "This notebook runs the repo's QLoRA fine-tune, exports `out/qwen3-coder-codeflow-Q3_K_XL.gguf`, and uploads it to Kaggle as a dataset.\n",
10
+ "\n",
11
+ "Use a high-memory GPU runtime. A free T4/L4 Colab runtime is unlikely to have enough VRAM/RAM/disk for the 30B merge/export step. A100/H100-class runtime with high system RAM is the safer target.\n",
12
+ "\n",
13
+ "Before running the Kaggle upload cell, add these Colab Secrets:\n",
14
+ "\n",
15
+ "- `KAGGLE_USERNAME`\n",
16
+ "- `KAGGLE_KEY`\n",
17
+ "\n",
18
+ "Do not paste your Kaggle API key into the notebook itself."
19
+ ]
20
+ },
21
+ {
22
+ "cell_type": "code",
23
+ "execution_count": null,
24
+ "metadata": {},
25
+ "outputs": [],
26
+ "source": [
27
+ "# Check GPU and disk before doing the expensive work.\n",
28
+ "!nvidia-smi\n",
29
+ "!df -h /content\n",
30
+ "!free -h"
31
+ ]
32
+ },
33
+ {
34
+ "cell_type": "code",
35
+ "execution_count": null,
36
+ "metadata": {},
37
+ "outputs": [],
38
+ "source": [
39
+ "# Clone the repo. If you uploaded this notebook inside an already-cloned repo,\n",
40
+ "# skip this cell and set REPO_DIR to that path instead.\n",
41
+ "REPO_URL = \"https://github.com/rishijain27/codeflow-qwen-3-finetuning.git\"\n",
42
+ "REPO_DIR = \"/content/codeflow-qwen-3-finetuning\"\n",
43
+ "\n",
44
+ "import os\n",
45
+ "\n",
46
+ "if not os.path.exists(REPO_DIR):\n",
47
+ " !git clone {REPO_URL} {REPO_DIR}\n",
48
+ "else:\n",
49
+ " print(f\"Repo already exists at {REPO_DIR}\")\n",
50
+ "\n",
51
+ "%cd {REPO_DIR}"
52
+ ]
53
+ },
54
+ {
55
+ "cell_type": "code",
56
+ "execution_count": null,
57
+ "metadata": {},
58
+ "outputs": [],
59
+ "source": [
60
+ "# System build tools for llama.cpp. CUDA is provided by the Colab runtime.\n",
61
+ "!apt-get update\n",
62
+ "!apt-get install -y git build-essential ninja-build"
63
+ ]
64
+ },
65
+ {
66
+ "cell_type": "code",
67
+ "execution_count": null,
68
+ "metadata": {},
69
+ "outputs": [],
70
+ "source": [
71
+ "# Create and populate the repo-local venv.\n",
72
+ "!python3 -m venv .venv\n",
73
+ "!.venv/bin/python -m pip install -U pip setuptools wheel\n",
74
+ "!.venv/bin/python -m pip install -r requirements.txt\n",
75
+ "!.venv/bin/python -m pip install kaggle"
76
+ ]
77
+ },
78
+ {
79
+ "cell_type": "code",
80
+ "execution_count": null,
81
+ "metadata": {},
82
+ "outputs": [],
83
+ "source": [
84
+ "# Optional dry run: validates tokenization and dataset formatting without loading the 30B model.\n",
85
+ "!.venv/bin/python finetune.py --dry-run"
86
+ ]
87
+ },
88
+ {
89
+ "cell_type": "code",
90
+ "execution_count": null,
91
+ "metadata": {},
92
+ "outputs": [],
93
+ "source": [
94
+ "# Fine-tune, merge, auto-build llama.cpp under .venv/llama.cpp if needed, and export Q3_K_XL GGUF.\n",
95
+ "# This is the long-running step.\n",
96
+ "!.venv/bin/python finetune.py \\\n",
97
+ " --model Qwen/Qwen3-Coder-30B-A3B-Instruct \\\n",
98
+ " --4bit \\\n",
99
+ " --epochs 3 \\\n",
100
+ " --batch-size 1 \\\n",
101
+ " --grad-accum 16 \\\n",
102
+ " --max-seq-len 2048 \\\n",
103
+ " --output-dir out/qwen-mermaid-lora \\\n",
104
+ " --merge-dir out/qwen-mermaid-merged \\\n",
105
+ " --merge-device-map cpu \\\n",
106
+ " --export-gguf \\\n",
107
+ " --gguf-out out/qwen3-coder-codeflow-Q3_K_XL.gguf \\\n",
108
+ " --gguf-quant Q3_K_XL"
109
+ ]
110
+ },
111
+ {
112
+ "cell_type": "code",
113
+ "execution_count": null,
114
+ "metadata": {},
115
+ "outputs": [],
116
+ "source": [
117
+ "# Verify the GGUF exists.\n",
118
+ "!ls -lh out/qwen3-coder-codeflow-Q3_K_XL.gguf"
119
+ ]
120
+ },
121
+ {
122
+ "cell_type": "code",
123
+ "execution_count": null,
124
+ "metadata": {},
125
+ "outputs": [],
126
+ "source": [
127
+ "# Configure Kaggle credentials from Colab Secrets.\n",
128
+ "from google.colab import userdata\n",
129
+ "import json\n",
130
+ "import os\n",
131
+ "from pathlib import Path\n",
132
+ "\n",
133
+ "kaggle_username = userdata.get(\"KAGGLE_USERNAME\")\n",
134
+ "kaggle_key = userdata.get(\"KAGGLE_KEY\")\n",
135
+ "if not kaggle_username or not kaggle_key:\n",
136
+ " raise RuntimeError(\"Add KAGGLE_USERNAME and KAGGLE_KEY in Colab Secrets before running this cell.\")\n",
137
+ "\n",
138
+ "kaggle_dir = Path.home() / \".kaggle\"\n",
139
+ "kaggle_dir.mkdir(parents=True, exist_ok=True)\n",
140
+ "kaggle_json = kaggle_dir / \"kaggle.json\"\n",
141
+ "kaggle_json.write_text(json.dumps({\"username\": kaggle_username, \"key\": kaggle_key}))\n",
142
+ "kaggle_json.chmod(0o600)\n",
143
+ "print(f\"Configured Kaggle credentials for {kaggle_username}\")"
144
+ ]
145
+ },
146
+ {
147
+ "cell_type": "code",
148
+ "execution_count": null,
149
+ "metadata": {},
150
+ "outputs": [],
151
+ "source": [
152
+ "# Stage the GGUF and Kaggle dataset metadata.\n",
153
+ "import json\n",
154
+ "import shutil\n",
155
+ "from pathlib import Path\n",
156
+ "\n",
157
+ "upload_dir = Path(\"kaggle_upload\")\n",
158
+ "upload_dir.mkdir(exist_ok=True)\n",
159
+ "gguf_src = Path(\"out/qwen3-coder-codeflow-Q3_K_XL.gguf\")\n",
160
+ "gguf_dst = upload_dir / gguf_src.name\n",
161
+ "if not gguf_src.exists():\n",
162
+ " raise FileNotFoundError(gguf_src)\n",
163
+ "if not gguf_dst.exists():\n",
164
+ " shutil.copy2(gguf_src, gguf_dst)\n",
165
+ "\n",
166
+ "metadata = {\n",
167
+ " \"title\": \"Qwen3 Coder CodeFlow Q3_K_XL GGUF\",\n",
168
+ " \"id\": \"rishijain27/qwen3-coder-codeflow-q3-k-xl-gguf\",\n",
169
+ " \"licenses\": [{\"name\": \"other\"}],\n",
170
+ "}\n",
171
+ "(upload_dir / \"dataset-metadata.json\").write_text(json.dumps(metadata, indent=2))\n",
172
+ "print(f\"Staged files in {upload_dir.resolve()}\")\n",
173
+ "!ls -lh kaggle_upload"
174
+ ]
175
+ },
176
+ {
177
+ "cell_type": "code",
178
+ "execution_count": null,
179
+ "metadata": {},
180
+ "outputs": [],
181
+ "source": [
182
+ "# Create the Kaggle dataset, or create a new version if it already exists.\n",
183
+ "!KAGGLE_CONFIG_DIR=$HOME/.kaggle .venv/bin/kaggle datasets create -p kaggle_upload --dir-mode skip || \\\n",
184
+ " KAGGLE_CONFIG_DIR=$HOME/.kaggle .venv/bin/kaggle datasets version -p kaggle_upload --dir-mode skip -m \"Update Qwen3 Coder CodeFlow Q3_K_XL GGUF\""
185
+ ]
186
+ }
187
+ ],
188
+ "metadata": {
189
+ "accelerator": "GPU",
190
+ "colab": {
191
+ "gpuType": "A100",
192
+ "provenance": []
193
+ },
194
+ "kernelspec": {
195
+ "display_name": "Python 3",
196
+ "language": "python",
197
+ "name": "python3"
198
+ },
199
+ "language_info": {
200
+ "name": "python"
201
+ }
202
+ },
203
+ "nbformat": 4,
204
+ "nbformat_minor": 0
205
+ }