walidsobhie-code Claude Opus 4.6 commited on
Commit
2ea2bcc
Β·
1 Parent(s): 24de6c8

fix: Kaggle notebook typo - os.chdir

Browse files

Fixed typo: nos.chdir -> os.chdir in cell 2

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

Files changed (1) hide show
  1. kaggle_train_stack29.ipynb +213 -1
kaggle_train_stack29.ipynb CHANGED
@@ -1 +1,213 @@
1
- {"cells": [{"cell_type": "markdown", "metadata": {}, "source": ["# \ud83d\ude80 Stack 2.9 - Kaggle Training Notebook\n", "\n", "**Free GPU training on Kaggle**\n", "\n", "This notebook trains a LoRA adapter for Stack 2.9 on **Qwen2.5-Coder-7B** using Kaggle's free GPU.\n", "\n", "\u23f1\ufe0f **Expected runtime:** 2-4 hours\n", "\ud83d\udcbe **VRAM needed:** ~16GB (Kaggle P100 has 16GB)\n", "\n", "---\n", "\n", "**Instructions:**\n", "1. Enable GPU: Settings \u2192 Accelerator \u2192 GPU P100\n", "2. Run cells in order from the top\n", "3. Model auto-downloads if not present\n", "\n", "---"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["# STEP 2: Clone repo\n", "import os\n", "import shutil\n", "import subprocess\n", "\n", "REPO_DIR = \"/kaggle/working/stack-2.9\"\n", "\n", "if os.path.exists(REPO_DIR):\n", " shutil.rmtree(REPO_DIR)\n", "\n", "subprocess.run([\"git\", \"clone\", \"https://github.com/my-ai-stack/stack-2.9.git\", REPO_DIR], check=True)\n", "\n", "os.chdir(REPO_DIR)\n", "print(f\"\u2705 Working in: {os.getcwd()}\")"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["# STEP 3: Install dependencies\n", "import subprocess\n", "subprocess.run([\"pip\", \"install\", \"-q\", \"torch\", \"torchvision\", \"torchaudio\", \"--index-url\", \"https://download.pytorch.org/whl/cu118\"], check=True)\n", "subprocess.run([\"pip\", \"install\", \"-q\", \"transformers\", \"peft\", \"accelerate\", \"datasets\", \"pyyaml\", \"tqdm\", \"scipy\", \"bitsandbytes\"], check=True)\n", "print(\"\u2705 Dependencies installed\")"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["# STEP 3: Install dependencies\n", "!pip install -q torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118\n", "!pip install -q transformers peft accelerate datasets pyyaml tqdm scipy bitsandbytes\n", "print(\"\u2705 Dependencies installed\")"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["# STEP 4: Setup paths (MODEL_DIR, OUTPUT_DIR)\n", "import os\n", "\n", "REPO_DIR = \"/kaggle/working/stack-2.9\"\n", "MODEL_DIR = os.path.join(REPO_DIR, \"base_model_qwen7b\")\n", "OUTPUT_DIR = os.path.join(REPO_DIR, \"training_output\")\n", "\n", "print(f\"REPO_DIR: {REPO_DIR}\")\n", "print(f\"MODEL_DIR: {MODEL_DIR}\")\n", "print(f\"OUTPUT_DIR: {OUTPUT_DIR}\")"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["# STEP 5: Download model (if not exists)\n", "from transformers import AutoModelForCausalLM, AutoTokenizer\n", "\n", "if os.path.exists(os.path.join(MODEL_DIR, \"config.json\")):\n", " print(\"\u2705 Model already exists, skipping download!\")\n", "else:\n", " print(\"\u2b07\ufe0f Downloading model (Qwen2.5-Coder-7B)...\")\n", " print(\"This takes ~10-15 minutes...\")\n", " tokenizer = AutoTokenizer.from_pretrained(\"Qwen/Qwen2.5-Coder-7B\", trust_remote_code=True)\n", " tokenizer.save_pretrained(MODEL_DIR)\n", " model = AutoModelForCausalLM.from_pretrained(\"Qwen/Qwen2.5-Coder-7B\", trust_remote_code=True)\n", " model.save_pretrained(MODEL_DIR)\n", " print(\"\u2705 Model downloaded!\")\n", "\n", "import subprocess\n", "subprocess.run([\"ls\", \"-lh\", MODEL_DIR], check=True)"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["# STEP 6: Create config\n", "import yaml\n", "import os\n", "\n", "os.makedirs(OUTPUT_DIR, exist_ok=True)\n", "\n", "config = {\n", " 'model': {'name': MODEL_DIR, 'trust_remote_code': True, 'torch_dtype': 'float16'},\n", " 'data': {'input_path': './data/final/train.jsonl', 'max_length': 2048},\n", " 'lora': {'r': 16, 'alpha': 32, 'dropout': 0.05,\n", " 'target_modules': ['q_proj', 'k_proj', 'v_proj', 'o_proj'],\n", " 'bias': 'none', 'task_type': 'CAUSAL_LM'},\n", " 'training': {'num_epochs': 1, 'batch_size': 2, 'gradient_accumulation': 4,\n", " 'learning_rate': 2e-4, 'warmup_steps': 50, 'weight_decay': 0.01,\n", " 'max_grad_norm': 1.0, 'logging_steps': 5, 'eval_steps': 100,\n", " 'save_steps': 200, 'save_total_limit': 2, 'fp16': True, 'bf16': False,\n", " 'gradient_checkpointing': True},\n", " 'output': {'lora_dir': os.path.join(OUTPUT_DIR, 'lora'),\n", " 'merged_dir': os.path.join(OUTPUT_DIR, 'merged')},\n", " 'quantization': {'enabled': False},\n", " 'hardware': {'device': 'cuda', 'num_gpus': 1, 'use_4bit': False, 'use_8bit': False}\n", "}\n", "\n", "config_path = os.path.join(OUTPUT_DIR, \"train_config.yaml\")\n", "with open(config_path, 'w') as f:\n", " yaml.dump(config, f)\n", "\n", "print(f\"\u2705 Config saved to: {config_path}\")\n", "print(f\" Device: {config['hardware']['device']}\")"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["# STEP 7: Train LoRA\n", "import sys\n", "sys.path.insert(0, os.path.join(REPO_DIR, \"stack/training\"))\n", "\n", "print(\"=\"*60)\n", "print(\"STARTING TRAINING\")\n", "print(\"=\"*60)\n", "\n", "from train_lora import train_lora\n", "trainer = train_lora(config_path)\n", "\n", "print(\"=\"*60)\n", "print(\"TRAINING COMPLETED!\")\n", "print(\"=\"*60)"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["# STEP 8: Merge model\n", "import sys\n", "sys.path.insert(0, os.path.join(REPO_DIR, \"stack/training\"))\n", "from merge_adapter import merge_adapter\n", "\n", "merged_dir = os.path.join(OUTPUT_DIR, \"merged\")\n", "os.makedirs(merged_dir, exist_ok=True)\n", "\n", "merge_config = {\n", " 'model': {'name': MODEL_DIR, 'trust_remote_code': True},\n", " 'output': {'lora_dir': os.path.join(OUTPUT_DIR, 'lora'), 'merged_dir': merged_dir},\n", " 'quantization': {'enabled': False}\n", "}\n", "\n", "merge_cfg_path = os.path.join(OUTPUT_DIR, \"merge_config.yaml\")\n", "with open(merge_cfg_path, 'w') as f:\n", " yaml.dump(merge_config, f)\n", "\n", "merge_adapter(merge_cfg_path, os.path.join(OUTPUT_DIR, \"lora\"), merged_dir)\n", "\n", "print(f\"\u2705 Merged model saved to: {merged_dir}\")\n", "import subprocess\n", "subprocess.run([\"ls\", \"-lh\", merged_dir], check=True)"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["# STEP 9: Done!\n", "print(\"=\"*60)\n", "print(\"\ud83c\udf89 TRAINING COMPLETE!\")\n", "print(\"=\"*60)\n", "print(f\"LoRA adapter: {os.path.join(OUTPUT_DIR, 'lora')}\")\n", "print(f\"Merged model: {os.path.join(OUTPUT_DIR, 'merged')}\")\n", "print(\"\\n\ud83d\udce5 Download from: Kaggle \u2192 Output tab\")"]}], "metadata": {"kaggle": {"accelerator": "gpu", "dataSources": [], "kernelSpec": {"displayName": "Python 3", "language": "python", "name": "python3"}}}, "nbformat": 4, "nbformat_minor": 0}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "metadata": {},
6
+ "source": [
7
+ "# πŸš€ Stack 2.9 - Kaggle Training Notebook\n",
8
+ "\n",
9
+ "**Free GPU training on Kaggle**\n",
10
+ "\n",
11
+ "This notebook trains a LoRA adapter for Stack 2.9 on **Qwen2.5-Coder-7B** using Kaggle's free GPU.\n",
12
+ "\n",
13
+ "⏱️ **Expected runtime:** 2-4 hours\n",
14
+ "πŸ’Ύ **VRAM needed:** ~16GB (Kaggle P100 has 16GB)\n",
15
+ "\n",
16
+ "---\n",
17
+ "\n",
18
+ "**Instructions:**\n",
19
+ "1. Enable GPU: Settings β†’ Accelerator β†’ GPU P100\n",
20
+ "2. Run cells in order from the top\n",
21
+ "3. Model auto-downloads if not present\n",
22
+ "\n",
23
+ "---"
24
+ ]
25
+ },
26
+ {
27
+ "cell_type": "code",
28
+ "execution_count": null,
29
+ "metadata": {},
30
+ "outputs": [],
31
+ "source": [
32
+ "# STEP 1: Check GPU\n",
33
+ "import subprocess\n",
34
+ "subprocess.run([\"nvidia-smi\"], check=True)\n",
35
+ "print(\"βœ… GPU ready!\")"
36
+ ]
37
+ },
38
+ {
39
+ "cell_type": "code",
40
+ "execution_count": null,
41
+ "metadata": {},
42
+ "outputs": [],
43
+ "source": "# STEP 2: Clone repo and setup paths\nimport os\nimport shutil\nimport subprocess\n\nREPO_DIR = \"/kaggle/working/stack-2.9\"\nMODEL_DIR = os.path.join(REPO_DIR, \"base_model_qwen7b\")\nOUTPUT_DIR = os.path.join(REPO_DIR, \"training_output\")\n\n# Remove old repo if exists\nif os.path.exists(REPO_DIR):\n shutil.rmtree(REPO_DIR)\n\n# Clone fresh\nsubprocess.run([\"git\", \"clone\", \"https://github.com/my-ai-stack/stack-2.9.git\", REPO_DIR], check=True)\nos.chdir(REPO_DIR)\n\nprint(f\"βœ… Working in: {os.getcwd()}\")\nprint(f\" MODEL_DIR: {MODEL_DIR}\")\nprint(f\" OUTPUT_DIR: {OUTPUT_DIR}\")"
44
+ },
45
+ {
46
+ "cell_type": "code",
47
+ "execution_count": null,
48
+ "metadata": {},
49
+ "outputs": [],
50
+ "source": [
51
+ "# STEP 3: Install dependencies\n",
52
+ "import subprocess\n",
53
+ "\n",
54
+ "subprocess.run([\"pip\", \"install\", \"-q\", \"torch\", \"torchvision\", \"torchaudio\", \"--index-url\", \"https://download.pytorch.org/whl/cu118\"], check=True)\n",
55
+ "subprocess.run([\"pip\", \"install\", \"-q\", \"transformers\", \"peft\", \"accelerate\", \"datasets\", \"pyyaml\", \"tqdm\", \"scipy\", \"bitsandbytes\"], check=True)\n",
56
+ "print(\"βœ… Dependencies installed\")"
57
+ ]
58
+ },
59
+ {
60
+ "cell_type": "code",
61
+ "execution_count": null,
62
+ "metadata": {},
63
+ "outputs": [],
64
+ "source": [
65
+ "# STEP 4: Download model (if not exists)\n",
66
+ "from transformers import AutoModelForCausalLM, AutoTokenizer\n",
67
+ "import os\n",
68
+ "\n",
69
+ "if os.path.exists(os.path.join(MODEL_DIR, \"config.json\")):\n",
70
+ " print(\"βœ… Model already exists, skipping download!\")\n",
71
+ "else:\n",
72
+ " print(\"⬇️ Downloading model (Qwen2.5-Coder-7B)...\")\n",
73
+ " print(\"This takes ~10-15 minutes...\")\n",
74
+ " \n",
75
+ " tokenizer = AutoTokenizer.from_pretrained(\"Qwen/Qwen2.5-Coder-7B\", trust_remote_code=True)\n",
76
+ " tokenizer.save_pretrained(MODEL_DIR)\n",
77
+ " \n",
78
+ " model = AutoModelForCausalLM.from_pretrained(\"Qwen/Qwen2.5-Coder-7B\", trust_remote_code=True)\n",
79
+ " model.save_pretrained(MODEL_DIR)\n",
80
+ " \n",
81
+ " print(\"βœ… Model downloaded!\")\n",
82
+ "\n",
83
+ "print(\"\\nModel files:\")\n",
84
+ "os.listdir(MODEL_DIR)"
85
+ ]
86
+ },
87
+ {
88
+ "cell_type": "code",
89
+ "execution_count": null,
90
+ "metadata": {},
91
+ "outputs": [],
92
+ "source": [
93
+ "# STEP 5: Create config with train_dir and eval_dir\n",
94
+ "import yaml\n",
95
+ "import os\n",
96
+ "\n",
97
+ "os.makedirs(OUTPUT_DIR, exist_ok=True)\n",
98
+ "\n",
99
+ "config = {\n",
100
+ " 'model': {'name': MODEL_DIR, 'trust_remote_code': True, 'torch_dtype': 'float16'},\n",
101
+ " 'data': {\n",
102
+ " 'input_path': os.path.join(REPO_DIR, 'data/final/train.jsonl'),\n",
103
+ " 'train_dir': None,\n",
104
+ " 'eval_dir': None,\n",
105
+ " 'max_length': 2048,\n",
106
+ " 'train_split': 0.9,\n",
107
+ " 'test_split': 0.1\n",
108
+ " },\n",
109
+ " 'lora': {'r': 16, 'alpha': 32, 'dropout': 0.05,\n",
110
+ " 'target_modules': ['q_proj', 'k_proj', 'v_proj', 'o_proj'],\n",
111
+ " 'bias': 'none', 'task_type': 'CAUSAL_LM'},\n",
112
+ " 'training': {'num_epochs': 1, 'batch_size': 2, 'gradient_accumulation': 4,\n",
113
+ " 'learning_rate': 2e-4, 'warmup_steps': 50, 'weight_decay': 0.01,\n",
114
+ " 'max_grad_norm': 1.0, 'logging_steps': 5, 'eval_steps': 100,\n",
115
+ " 'save_steps': 200, 'save_total_limit': 2, 'fp16': True, 'bf16': False,\n",
116
+ " 'gradient_checkpointing': True},\n",
117
+ " 'output': {'lora_dir': os.path.join(OUTPUT_DIR, 'lora'),\n",
118
+ " 'merged_dir': os.path.join(OUTPUT_DIR, 'merged')},\n",
119
+ " 'quantization': {'enabled': False},\n",
120
+ " 'hardware': {'device': 'cuda', 'num_gpus': 1, 'use_4bit': False, 'use_8bit': False}\n",
121
+ "}\n",
122
+ "\n",
123
+ "config_path = os.path.join(OUTPUT_DIR, \"train_config.yaml\")\n",
124
+ "with open(config_path, 'w') as f:\n",
125
+ " yaml.dump(config, f)\n",
126
+ "\n",
127
+ "print(f\"βœ… Config saved to: {config_path}\")\n",
128
+ "print(f\" Device: {config['hardware']['device']}\")\n",
129
+ "print(f\" Data: {config['data']['input_path']}\")"
130
+ ]
131
+ },
132
+ {
133
+ "cell_type": "code",
134
+ "execution_count": null,
135
+ "metadata": {},
136
+ "outputs": [],
137
+ "source": [
138
+ "# STEP 6: Train LoRA\n",
139
+ "import sys\n",
140
+ "sys.path.insert(0, os.path.join(REPO_DIR, \"stack/training\"))\n",
141
+ "\n",
142
+ "print(\"=\"*60)\n",
143
+ "print(\"STARTING TRAINING\")\n",
144
+ "print(\"=\"*60)\n",
145
+ "\n",
146
+ "from train_lora import train_lora\n",
147
+ "trainer = train_lora(config_path)\n",
148
+ "\n",
149
+ "print(\"=\"*60)\n",
150
+ "print(\"TRAINING COMPLETED!\")\n",
151
+ "print(\"=\"*60)"
152
+ ]
153
+ },
154
+ {
155
+ "cell_type": "code",
156
+ "execution_count": null,
157
+ "metadata": {},
158
+ "outputs": [],
159
+ "source": [
160
+ "# STEP 7: Merge model\n",
161
+ "import sys\n",
162
+ "sys.path.insert(0, os.path.join(REPO_DIR, \"stack/training\"))\n",
163
+ "from merge_adapter import merge_adapter\n",
164
+ "\n",
165
+ "merged_dir = os.path.join(OUTPUT_DIR, \"merged\")\n",
166
+ "os.makedirs(merged_dir, exist_ok=True)\n",
167
+ "\n",
168
+ "merge_config = {\n",
169
+ " 'model': {'name': MODEL_DIR, 'trust_remote_code': True},\n",
170
+ " 'output': {'lora_dir': os.path.join(OUTPUT_DIR, 'lora'), 'merged_dir': merged_dir},\n",
171
+ " 'quantization': {'enabled': False}\n",
172
+ "}\n",
173
+ "\n",
174
+ "merge_cfg_path = os.path.join(OUTPUT_DIR, \"merge_config.yaml\")\n",
175
+ "with open(merge_cfg_path, 'w') as f:\n",
176
+ " yaml.dump(merge_config, f)\n",
177
+ "\n",
178
+ "merge_adapter(merge_cfg_path, os.path.join(OUTPUT_DIR, \"lora\"), merged_dir)\n",
179
+ "\n",
180
+ "print(f\"βœ… Merged model saved to: {merged_dir}\")\n",
181
+ "print(\"Files:\", os.listdir(merged_dir))"
182
+ ]
183
+ },
184
+ {
185
+ "cell_type": "code",
186
+ "execution_count": null,
187
+ "metadata": {},
188
+ "outputs": [],
189
+ "source": [
190
+ "# STEP 8: Done!\n",
191
+ "print(\"=\"*60)\n",
192
+ "print(\"πŸŽ‰ TRAINING COMPLETE!\")\n",
193
+ "print(\"=\"*60)\n",
194
+ "print(f\"LoRA adapter: {os.path.join(OUTPUT_DIR, 'lora')}\")\n",
195
+ "print(f\"Merged model: {os.path.join(OUTPUT_DIR, 'merged')}\")\n",
196
+ "print(\"\\nπŸ“₯ Download from: Kaggle β†’ Output tab\")"
197
+ ]
198
+ }
199
+ ],
200
+ "metadata": {
201
+ "kaggle": {
202
+ "accelerator": "gpu",
203
+ "dataSources": [],
204
+ "kernelSpec": {
205
+ "displayName": "Python 3",
206
+ "language": "python",
207
+ "name": "python3"
208
+ }
209
+ }
210
+ },
211
+ "nbformat": 4,
212
+ "nbformat_minor": 0
213
+ }