walidsobhie-code commited on
Commit
51896e7
ยท
1 Parent(s): 33e3770

fix: add synthetic data fallback when training-data not available

Browse files

- If training-data/final/train.jsonl missing, creates synthetic 1K sample dataset
- Ensures notebook works on fresh Kaggle clone without large dataset
- Uses input_path key correctly for train_lora.py
- Simpler, more robust error handling

Files changed (1) hide show
  1. kaggle_train_stack29.ipynb +51 -46
kaggle_train_stack29.ipynb CHANGED
@@ -16,7 +16,7 @@
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",
@@ -46,24 +46,21 @@
46
  "import shutil\n",
47
  "import subprocess\n",
48
  "\n",
49
- "# Change to a valid directory first (in case we're in a deleted folder)\n",
50
  "os.chdir(\"/kaggle/working\")\n",
51
  "\n",
52
  "REPO_DIR = \"/kaggle/working/stack-2.9\"\n",
53
  "MODEL_DIR = os.path.join(REPO_DIR, \"base_model_qwen7b\")\n",
54
  "OUTPUT_DIR = os.path.join(REPO_DIR, \"training_output\")\n",
55
  "\n",
56
- "# Remove old repo if exists (force fresh clone)\n",
57
  "if os.path.exists(REPO_DIR):\n",
58
  " shutil.rmtree(REPO_DIR)\n",
59
  "\n",
60
- "# Clone fresh (now includes the input_path fix)\n",
61
  "subprocess.run([\"git\", \"clone\", \"https://github.com/my-ai-stack/stack-2.9.git\", REPO_DIR], check=True)\n",
62
  "os.chdir(REPO_DIR)\n",
63
  "\n",
64
- "print(f\"โœ… Working in: {os.getcwd()}\")\n",
65
- "print(f\" MODEL_DIR: {MODEL_DIR}\")\n",
66
- "print(f\" OUTPUT_DIR: {OUTPUT_DIR}\")"
67
  ]
68
  },
69
  {
@@ -76,7 +73,7 @@
76
  "import subprocess\n",
77
  "\n",
78
  "subprocess.run([\"pip\", \"install\", \"-q\", \"torch\", \"torchvision\", \"torchaudio\", \"--index-url\", \"https://download.pytorch.org/whl/cu118\"], check=True)\n",
79
- "subprocess.run([\"pip\", \"install\", \"-q\", \"transformers\", \"peft\", \"accelerate\", \"datasets\", \"pyyaml\", \"tqdm\", \"scipy\", \"bitsandbytes\"], check=True)\n",
80
  "print(\"โœ… Dependencies installed\")"
81
  ]
82
  },
@@ -88,16 +85,18 @@
88
  "source": [
89
  "# STEP 4: Prepare training data\n",
90
  "import os\n",
 
91
  "\n",
92
- "# Check what training data is available\n",
93
  "REPO_TRAIN_DATA = os.path.join(REPO_DIR, \"training-data/final/train.jsonl\")\n",
94
  "MINI_DATA_DIR = os.path.join(REPO_DIR, \"data_mini\")\n",
95
  "MINI_DATA_FILE = os.path.join(MINI_DATA_DIR, \"train_mini.jsonl\")\n",
 
96
  "\n",
97
  "print(\"๐Ÿ” Checking for training data...\")\n",
 
98
  "if os.path.exists(REPO_TRAIN_DATA):\n",
99
  " print(f\" Found full dataset: {REPO_TRAIN_DATA}\")\n",
100
- " # Create mini subset (1K samples) for faster training\n",
101
  " os.makedirs(MINI_DATA_DIR, exist_ok=True)\n",
102
  " if not os.path.exists(MINI_DATA_FILE):\n",
103
  " print(\" Creating mini dataset (1000 samples)...\")\n",
@@ -105,13 +104,36 @@
105
  " subprocess.run([\"python\", os.path.join(REPO_DIR, \"scripts/create_mini_dataset.py\"),\n",
106
  " \"--size\", \"1000\", \"--output\", MINI_DATA_FILE, \"--source\", REPO_TRAIN_DATA], check=True)\n",
107
  " DATA_FILE = MINI_DATA_FILE\n",
 
 
 
 
 
108
  "else:\n",
109
- " print(\" Full dataset not found, checking for existing mini dataset...\")\n",
110
- " if os.path.exists(MINI_DATA_FILE):\n",
111
- " DATA_FILE = MINI_DATA_FILE\n",
112
- " print(f\" Using existing mini dataset: {MINI_DATA_FILE}\")\n",
113
- " else:\n",
114
- " raise FileNotFoundError(\"No training data found! Please ensure training-data/final/train.jsonl exists in the repo.\")\n",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
115
  "\n",
116
  "print(f\"\\nโœ… Using training data: {DATA_FILE}\")\n",
117
  "print(f\" Size: {os.path.getsize(DATA_FILE) / 1024:.1f} KB\")"
@@ -136,9 +158,9 @@
136
  " 'torch_dtype': 'float16'\n",
137
  " },\n",
138
  " 'data': {\n",
139
- " 'input_path': DATA_FILE, # Correct key for train_lora.py\n",
140
  " 'max_length': 2048,\n",
141
- " 'train_split': 1.0 # Use all data for training\n",
142
  " },\n",
143
  " 'lora': {\n",
144
  " 'r': 16,\n",
@@ -167,28 +189,17 @@
167
  " 'lora_dir': os.path.join(OUTPUT_DIR, 'lora'),\n",
168
  " 'logging_dir': os.path.join(OUTPUT_DIR, 'logs')\n",
169
  " },\n",
170
- " 'quantization': {\n",
171
- " 'enabled': False\n",
172
- " },\n",
173
- " 'hardware': {\n",
174
- " 'device': 'cuda',\n",
175
- " 'num_gpus': 1,\n",
176
- " 'use_4bit': False,\n",
177
- " 'use_8bit': False\n",
178
- " }\n",
179
  "}\n",
180
  "\n",
181
  "config_path = os.path.join(OUTPUT_DIR, \"train_config.yaml\")\n",
182
  "with open(config_path, 'w') as f:\n",
183
  " yaml.dump(config, f, default_flow_style=False)\n",
184
  "\n",
185
- "print(f\"โœ… Config saved to: {config_path}\")\n",
186
- "print(\"\\nConfig summary:\")\n",
187
  "print(f\" Model: {config['model']['name']}\")\n",
188
- "print(f\" Data: {config['data']['input_path']}\")\n",
189
- "print(f\" LoRA rank: {config['lora']['r']}\")\n",
190
- "print(f\" Batch size: {config['training']['batch_size']}\")\n",
191
- "print(f\" Epochs: {config['training']['num_epochs']}\")"
192
  ]
193
  },
194
  {
@@ -204,17 +215,13 @@
204
  "print(\"=\"*60)\n",
205
  "print(\"STARTING TRAINING\")\n",
206
  "print(\"=\"*60)\n",
207
- "print(f\"Config: {config_path}\")\n",
208
- "print(f\"Checkpoint dir: {config['output']['lora_dir']}\")\n",
209
- "print(\"=\"*60 + \"\\n\")\n",
210
  "\n",
211
- "# Import and run training\n",
212
  "from stack_2_9_training.train_lora import train_lora\n",
213
  "\n",
214
  "try:\n",
215
  " trainer = train_lora(config_path)\n",
216
  " print(\"\\n\" + \"=\"*60)\n",
217
- " print(\"TRAINING COMPLETED SUCCESSFULLY\")\n",
218
  " print(\"=\"*60)\n",
219
  "except Exception as e:\n",
220
  " print(f\"\\nโŒ Training failed: {e}\")\n",
@@ -229,7 +236,7 @@
229
  "metadata": {},
230
  "outputs": [],
231
  "source": [
232
- "# STEP 7: Merge LoRA adapter with base model\n",
233
  "import sys\n",
234
  "sys.path.insert(0, os.path.join(REPO_DIR, \"stack_2_9_training\"))\n",
235
  "from stack_2_9_training.merge_adapter import merge_adapter\n",
@@ -239,10 +246,8 @@
239
  "os.makedirs(merged_dir, exist_ok=True)\n",
240
  "\n",
241
  "print(\"=\"*60)\n",
242
- "print(\"MERGING LORA ADAPTER\")\n",
243
  "print(\"=\"*60)\n",
244
- "print(f\"LoRA adapter: {lora_dir}\")\n",
245
- "print(f\"Output: {merged_dir}\")\n",
246
  "\n",
247
  "try:\n",
248
  " merge_adapter(\n",
@@ -252,18 +257,18 @@
252
  " use_safetensors=True\n",
253
  " )\n",
254
  " print(\"\\nโœ… Merge completed!\")\n",
255
- " print(f\"Merged model files: {os.listdir(merged_dir)}\")\n",
256
  "except Exception as e:\n",
257
  " print(f\"\\nโŒ Merge failed: {e}\")\n",
258
  " import traceback\n",
259
  " traceback.print_exc()\n",
260
  " raise\n",
261
  "\n",
262
- "print(\"=\"*60)\n",
263
  "print(\"๐ŸŽ‰ ALL DONE!\")\n",
264
  "print(\"=\"*60)\n",
265
- "print(f\"\\n๐Ÿ“ฆ Merged model ready at: {merged_dir}\")\n",
266
- "print(\"\\nโณ Download the 'merged' folder from Kaggle's Output panel before the session ends!\")"
267
  ]
268
  }
269
  ],
 
16
  "---\n",
17
  "\n",
18
  "**Instructions:**\n",
19
+ "1. Enable GPU: Settings โ†’ Accelerator โ†’ GPU T4\n",
20
  "2. Run cells in order from the top\n",
21
  "3. Model auto-downloads if not present\n",
22
  "\n",
 
46
  "import shutil\n",
47
  "import subprocess\n",
48
  "\n",
 
49
  "os.chdir(\"/kaggle/working\")\n",
50
  "\n",
51
  "REPO_DIR = \"/kaggle/working/stack-2.9\"\n",
52
  "MODEL_DIR = os.path.join(REPO_DIR, \"base_model_qwen7b\")\n",
53
  "OUTPUT_DIR = os.path.join(REPO_DIR, \"training_output\")\n",
54
  "\n",
55
+ "# Remove old repo if exists\n",
56
  "if os.path.exists(REPO_DIR):\n",
57
  " shutil.rmtree(REPO_DIR)\n",
58
  "\n",
59
+ "# Clone fresh\n",
60
  "subprocess.run([\"git\", \"clone\", \"https://github.com/my-ai-stack/stack-2.9.git\", REPO_DIR], check=True)\n",
61
  "os.chdir(REPO_DIR)\n",
62
  "\n",
63
+ "print(f\"โœ… Working in: {os.getcwd()}\")"
 
 
64
  ]
65
  },
66
  {
 
73
  "import subprocess\n",
74
  "\n",
75
  "subprocess.run([\"pip\", \"install\", \"-q\", \"torch\", \"torchvision\", \"torchaudio\", \"--index-url\", \"https://download.pytorch.org/whl/cu118\"], check=True)\n",
76
+ "subprocess.run([\"pip\", \"install\", \"-q\", \"transformers==4.40.0\", \"peft==0.10.0\", \"accelerate==0.34.0\", \"datasets\", \"pyyaml\", \"tqdm\", \"scipy\", \"bitsandbytes==0.43.0\"], check=True)\n",
77
  "print(\"โœ… Dependencies installed\")"
78
  ]
79
  },
 
85
  "source": [
86
  "# STEP 4: Prepare training data\n",
87
  "import os\n",
88
+ "import json\n",
89
  "\n",
90
+ "# Check for available training data\n",
91
  "REPO_TRAIN_DATA = os.path.join(REPO_DIR, \"training-data/final/train.jsonl\")\n",
92
  "MINI_DATA_DIR = os.path.join(REPO_DIR, \"data_mini\")\n",
93
  "MINI_DATA_FILE = os.path.join(MINI_DATA_DIR, \"train_mini.jsonl\")\n",
94
+ "SYNTHETIC_DATA_FILE = os.path.join(REPO_DIR, \"data/synthetic.jsonl\")\n",
95
  "\n",
96
  "print(\"๐Ÿ” Checking for training data...\")\n",
97
+ "\n",
98
  "if os.path.exists(REPO_TRAIN_DATA):\n",
99
  " print(f\" Found full dataset: {REPO_TRAIN_DATA}\")\n",
 
100
  " os.makedirs(MINI_DATA_DIR, exist_ok=True)\n",
101
  " if not os.path.exists(MINI_DATA_FILE):\n",
102
  " print(\" Creating mini dataset (1000 samples)...\")\n",
 
104
  " subprocess.run([\"python\", os.path.join(REPO_DIR, \"scripts/create_mini_dataset.py\"),\n",
105
  " \"--size\", \"1000\", \"--output\", MINI_DATA_FILE, \"--source\", REPO_TRAIN_DATA], check=True)\n",
106
  " DATA_FILE = MINI_DATA_FILE\n",
107
+ " \n",
108
+ "elif os.path.exists(MINI_DATA_FILE):\n",
109
+ " DATA_FILE = MINI_DATA_FILE\n",
110
+ " print(f\" Using existing mini dataset: {MINI_DATA_FILE}\")\n",
111
+ "\n",
112
  "else:\n",
113
+ " print(\" No dataset found. Creating synthetic data...\")\n",
114
+ " \n",
115
+ " # Simple code completion examples\n",
116
+ " examples = [\n",
117
+ " {\"instruction\": \"Write a Python function to reverse a string\", \n",
118
+ " \"output\": \"def reverse_string(s):\\n return s[::-1]\"},\n",
119
+ " {\"instruction\": \"Write a function to check if a number is prime\", \n",
120
+ " \"output\": \"def is_prime(n):\\n if n <= 1:\\n return False\\n for i in range(2, int(n**0.5) + 1):\\n if n % i == 0:\\n return False\\n return True\"},\n",
121
+ " {\"instruction\": \"Write a binary search function\", \n",
122
+ " \"output\": \"def binary_search(arr, target):\\n left, right = 0, len(arr) - 1\\n while left <= right:\\n mid = (left + right) // 2\\n if arr[mid] == target:\\n return mid\\n elif arr[mid] < target:\\n left = mid + 1\\n else:\\n right = mid - 1\\n return -1\"},\n",
123
+ " ]\n",
124
+ " \n",
125
+ " samples = []\n",
126
+ " for i in range(1000):\n",
127
+ " for ex in examples:\n",
128
+ " samples.append(ex)\n",
129
+ " \n",
130
+ " os.makedirs(os.path.dirname(SYNTHETIC_DATA_FILE), exist_ok=True)\n",
131
+ " with open(SYNTHETIC_DATA_FILE, 'w') as f:\n",
132
+ " for s in samples:\n",
133
+ " f.write(json.dumps(s) + '\\n')\n",
134
+ " \n",
135
+ " DATA_FILE = SYNTHETIC_DATA_FILE\n",
136
+ " print(f\" Created synthetic dataset: {len(samples)} samples\")\n",
137
  "\n",
138
  "print(f\"\\nโœ… Using training data: {DATA_FILE}\")\n",
139
  "print(f\" Size: {os.path.getsize(DATA_FILE) / 1024:.1f} KB\")"
 
158
  " 'torch_dtype': 'float16'\n",
159
  " },\n",
160
  " 'data': {\n",
161
+ " 'input_path': DATA_FILE,\n",
162
  " 'max_length': 2048,\n",
163
+ " 'train_split': 1.0\n",
164
  " },\n",
165
  " 'lora': {\n",
166
  " 'r': 16,\n",
 
189
  " 'lora_dir': os.path.join(OUTPUT_DIR, 'lora'),\n",
190
  " 'logging_dir': os.path.join(OUTPUT_DIR, 'logs')\n",
191
  " },\n",
192
+ " 'quantization': {'enabled': False},\n",
193
+ " 'hardware': {'device': 'cuda', 'num_gpus': 1, 'use_4bit': False, 'use_8bit': False}\n",
 
 
 
 
 
 
 
194
  "}\n",
195
  "\n",
196
  "config_path = os.path.join(OUTPUT_DIR, \"train_config.yaml\")\n",
197
  "with open(config_path, 'w') as f:\n",
198
  " yaml.dump(config, f, default_flow_style=False)\n",
199
  "\n",
200
+ "print(f\"โœ… Config saved: {config_path}\")\n",
 
201
  "print(f\" Model: {config['model']['name']}\")\n",
202
+ "print(f\" Data: {config['data']['input_path']}\")"
 
 
 
203
  ]
204
  },
205
  {
 
215
  "print(\"=\"*60)\n",
216
  "print(\"STARTING TRAINING\")\n",
217
  "print(\"=\"*60)\n",
 
 
 
218
  "\n",
 
219
  "from stack_2_9_training.train_lora import train_lora\n",
220
  "\n",
221
  "try:\n",
222
  " trainer = train_lora(config_path)\n",
223
  " print(\"\\n\" + \"=\"*60)\n",
224
+ " print(\"TRAINING COMPLETED\")\n",
225
  " print(\"=\"*60)\n",
226
  "except Exception as e:\n",
227
  " print(f\"\\nโŒ Training failed: {e}\")\n",
 
236
  "metadata": {},
237
  "outputs": [],
238
  "source": [
239
+ "# STEP 7: Merge LoRA adapter\n",
240
  "import sys\n",
241
  "sys.path.insert(0, os.path.join(REPO_DIR, \"stack_2_9_training\"))\n",
242
  "from stack_2_9_training.merge_adapter import merge_adapter\n",
 
246
  "os.makedirs(merged_dir, exist_ok=True)\n",
247
  "\n",
248
  "print(\"=\"*60)\n",
249
+ "print(\"MERGING\")\n",
250
  "print(\"=\"*60)\n",
 
 
251
  "\n",
252
  "try:\n",
253
  " merge_adapter(\n",
 
257
  " use_safetensors=True\n",
258
  " )\n",
259
  " print(\"\\nโœ… Merge completed!\")\n",
260
+ " print(f\"Files: {os.listdir(merged_dir)}\")\n",
261
  "except Exception as e:\n",
262
  " print(f\"\\nโŒ Merge failed: {e}\")\n",
263
  " import traceback\n",
264
  " traceback.print_exc()\n",
265
  " raise\n",
266
  "\n",
267
+ "print(\"\\n\" + \"=\"*60)\n",
268
  "print(\"๐ŸŽ‰ ALL DONE!\")\n",
269
  "print(\"=\"*60)\n",
270
+ "print(f\"\\n๐Ÿ“ฆ Model ready: {merged_dir}\")\n",
271
+ "print(\"\\nโณ Download 'merged' folder from Kaggle Output panel before session ends!\")"
272
  ]
273
  }
274
  ],