walidsobhie-code commited on
Commit
732bef9
·
1 Parent(s): 4ce3e59

revert bitsandbytes: CUDA 13 runtime missing on Kaggle

Browse files

bitsandbytes 4-bit quantization requires libnvJitLink.so.13 which
is not available in the Kaggle CUDA 11.8 environment.

Switching back to bfloat16-only training (no quantization):
- train_simple_nobnb.py: bfloat16 by default, 4-bit as opt-in flag
- notebook: use_4bit=False, no bitsandbytes pip install
- merge_simple.py: also uses bfloat16 for consistency

VRAM usage with bf16 LoRA on T4 should still be manageable with
batch_size=1 + gradient_accumulation=8

kaggle_train_stack29_v5.ipynb CHANGED
@@ -8,17 +8,12 @@
8
  "\n",
9
  "Free GPU training on Kaggle using Qwen2.5-Coder-7B.\n",
10
  "\n",
11
- "⏱️ **Runtime:** 2-4 hours | 💾 **VRAM:** ~12GB (with 4-bit quantization)\n",
12
  "\n",
13
  "**Setup:**\n",
14
  "1. Settings → Accelerator → GPU **T4**\n",
15
  "2. Run all cells in order\n",
16
- "3. Download merged model from Output tab when done\n",
17
- "\n",
18
- "**Key fixes in v5:**\n",
19
- "- ✅ 4-bit NF4 quantization (saves ~10GB VRAM)\n",
20
- "- ✅ bfloat16 training (instead of float16)\n",
21
- "- ✅ bitsandbytes installed automatically"
22
  ]
23
  },
24
  {
@@ -70,8 +65,8 @@
70
  "metadata": {},
71
  "outputs": [],
72
  "source": [
73
- "# Install dependencies (including bitsandbytes for 4-bit quantization)\n",
74
- "!pip install -q transformers==4.40.0 peft==0.10.0 accelerate==0.34.0 datasets==3.0.0 pyyaml tqdm scipy bitsandbytes\n",
75
  "print('✅ Dependencies ready')"
76
  ]
77
  },
@@ -127,7 +122,7 @@
127
  "outputs": [],
128
  "source": [
129
  "# Generate training configuration\n",
130
- "# Key changes: use_4bit=True, bf16=True, fp16=False\n",
131
  "import yaml\n",
132
  "\n",
133
  "os.makedirs(OUTPUT_DIR, exist_ok=True)\n",
@@ -138,8 +133,8 @@
138
  " 'lora': {'r': 16, 'alpha': 32, 'dropout': 0.05, 'target_modules': ['q_proj', 'k_proj', 'v_proj', 'o_proj', 'gate_proj', 'up_proj', 'down_proj'], 'bias': 'none', 'task_type': 'CAUSAL_LM'},\n",
139
  " 'training': {'num_epochs': 1, 'batch_size': 1, 'gradient_accumulation': 8, 'learning_rate': 2e-4, 'warmup_steps': 50, 'weight_decay': 0.01, 'max_grad_norm': 1.0, 'logging_steps': 10, 'save_steps': 100, 'save_total_limit': 2, 'fp16': False, 'bf16': True, 'gradient_checkpointing': True},\n",
140
  " 'output': {'lora_dir': os.path.join(OUTPUT_DIR, 'lora'), 'logging_dir': os.path.join(OUTPUT_DIR, 'logs')},\n",
141
- " 'quantization': {'enabled': True},\n",
142
- " 'hardware': {'device': 'cuda', 'num_gpus': 1, 'use_4bit': True, 'use_8bit': False}\n",
143
  "}\n",
144
  "\n",
145
  "config_path = os.path.join(OUTPUT_DIR, 'train_config.yaml')\n",
@@ -149,7 +144,6 @@
149
  "print(f'✅ Config: {config_path}')\n",
150
  "print(f\" Model: {config['model']['name']}\")\n",
151
  "print(f\" Data: {config['data']['input_path']}\")\n",
152
- "print(f\" Quantization: 4-bit={config['hardware']['use_4bit']}\")\n",
153
  "print(f\" bf16={config['training']['bf16']}, fp16={config['training']['fp16']}\")"
154
  ]
155
  },
@@ -159,9 +153,9 @@
159
  "metadata": {},
160
  "outputs": [],
161
  "source": [
162
- "# Train (using standalone train_simple_nobnb.py - now with 4-bit support)\n",
163
  "print('='*60)\n",
164
- "print('STARTING TRAINING (4-bit NF4 quantized)')\n",
165
  "print('='*60)\n",
166
  "\n",
167
  "!cd {REPO_DIR} && python train_simple_nobnb.py --config {config_path}\n",
 
8
  "\n",
9
  "Free GPU training on Kaggle using Qwen2.5-Coder-7B.\n",
10
  "\n",
11
+ "⏱️ **Runtime:** 2-4 hours | 💾 **VRAM:** ~14GB (bfloat16, no bitsandbytes)\n",
12
  "\n",
13
  "**Setup:**\n",
14
  "1. Settings → Accelerator → GPU **T4**\n",
15
  "2. Run all cells in order\n",
16
+ "3. Download merged model from Output tab when done"
 
 
 
 
 
17
  ]
18
  },
19
  {
 
65
  "metadata": {},
66
  "outputs": [],
67
  "source": [
68
+ "# Install other dependencies (NO bitsandbytes bfloat16 only)\n",
69
+ "!pip install -q transformers==4.40.0 peft==0.10.0 accelerate==0.34.0 datasets==3.0.0 pyyaml tqdm scipy\n",
70
  "print('✅ Dependencies ready')"
71
  ]
72
  },
 
122
  "outputs": [],
123
  "source": [
124
  "# Generate training configuration\n",
125
+ "# Uses bfloat16 only (NO bitsandbytes — avoids CUDA 13 dependency issues)\n",
126
  "import yaml\n",
127
  "\n",
128
  "os.makedirs(OUTPUT_DIR, exist_ok=True)\n",
 
133
  " 'lora': {'r': 16, 'alpha': 32, 'dropout': 0.05, 'target_modules': ['q_proj', 'k_proj', 'v_proj', 'o_proj', 'gate_proj', 'up_proj', 'down_proj'], 'bias': 'none', 'task_type': 'CAUSAL_LM'},\n",
134
  " 'training': {'num_epochs': 1, 'batch_size': 1, 'gradient_accumulation': 8, 'learning_rate': 2e-4, 'warmup_steps': 50, 'weight_decay': 0.01, 'max_grad_norm': 1.0, 'logging_steps': 10, 'save_steps': 100, 'save_total_limit': 2, 'fp16': False, 'bf16': True, 'gradient_checkpointing': True},\n",
135
  " 'output': {'lora_dir': os.path.join(OUTPUT_DIR, 'lora'), 'logging_dir': os.path.join(OUTPUT_DIR, 'logs')},\n",
136
+ " 'quantization': {'enabled': False},\n",
137
+ " 'hardware': {'device': 'cuda', 'num_gpus': 1, 'use_4bit': False, 'use_8bit': False}\n",
138
  "}\n",
139
  "\n",
140
  "config_path = os.path.join(OUTPUT_DIR, 'train_config.yaml')\n",
 
144
  "print(f'✅ Config: {config_path}')\n",
145
  "print(f\" Model: {config['model']['name']}\")\n",
146
  "print(f\" Data: {config['data']['input_path']}\")\n",
 
147
  "print(f\" bf16={config['training']['bf16']}, fp16={config['training']['fp16']}\")"
148
  ]
149
  },
 
153
  "metadata": {},
154
  "outputs": [],
155
  "source": [
156
+ "# Train (using standalone train_simple_nobnb.py - bfloat16, no quantization)\n",
157
  "print('='*60)\n",
158
+ "print('STARTING TRAINING (bfloat16, no quantization)')\n",
159
  "print('='*60)\n",
160
  "\n",
161
  "!cd {REPO_DIR} && python train_simple_nobnb.py --config {config_path}\n",
train_simple_nobnb.py CHANGED
@@ -1,7 +1,7 @@
1
  #!/usr/bin/env python3
2
  """
3
  Simple standalone training script for Stack 2.9.
4
- Uses 4-bit quantization when use_4bit=True, otherwise bfloat16.
5
  """
6
 
7
  import argparse
@@ -16,7 +16,6 @@ from transformers import (
16
  TrainingArguments,
17
  Trainer,
18
  DataCollatorForLanguageModeling,
19
- BitsAndBytesConfig,
20
  )
21
  from peft import LoraConfig, get_peft_model, TaskType
22
  import torch
@@ -32,11 +31,13 @@ def load_model_and_tokenizer(
32
  trust_remote_code: bool = True,
33
  use_4bit: bool = False,
34
  ):
35
- """Load base model with optional 4-bit quantization."""
36
  tokenizer = AutoTokenizer.from_pretrained(
37
  model_name, trust_remote_code=trust_remote_code
38
  )
 
39
  if use_4bit:
 
40
  bnb_config = BitsAndBytesConfig(
41
  load_in_4bit=True,
42
  bnb_4bit_quant_type="nf4",
@@ -56,6 +57,7 @@ def load_model_and_tokenizer(
56
  trust_remote_code=trust_remote_code,
57
  device_map={"": torch.device("cuda")},
58
  )
 
59
  return model, tokenizer
60
 
61
 
@@ -103,17 +105,13 @@ def load_data(
103
  # Handle train_split logic
104
  total_samples = len(tokenized_dataset)
105
  if train_split >= 1.0:
106
- # Absolute number of training samples
107
  n_train = int(train_split)
108
  if n_train >= total_samples:
109
- # Use all data for training, no eval set
110
  return tokenized_dataset, None
111
  else:
112
- # Split with exact number
113
  split = tokenized_dataset.train_test_split(train_size=n_train)
114
  return split["train"], split["test"]
115
  else:
116
- # Fractional split
117
  split = tokenized_dataset.train_test_split(train_size=train_split)
118
  return split["train"], split["test"]
119
 
@@ -126,9 +124,8 @@ def train(config: dict):
126
  training_config = config["training"]
127
  output_config = config["output"]
128
  hardware_config = config.get("hardware", {})
129
- quantization_config = config.get("quantization", {})
130
 
131
- use_4bit = hardware_config.get("use_4bit", False) or quantization_config.get("enabled", False)
132
 
133
  # Load model and tokenizer
134
  print(f"Loading model: {model_config['name']} (4bit={use_4bit})")
@@ -164,8 +161,7 @@ def train(config: dict):
164
  model = get_peft_model(model, peft_config)
165
  model.print_trainable_parameters()
166
 
167
- # Enable gradient checkpointing with explicit use_reentrant=False
168
- # (required for PyTorch 2.9+ compatibility)
169
  if training_config.get("gradient_checkpointing", True):
170
  model.gradient_checkpointing_enable(
171
  gradient_checkpointing_kwargs={"use_reentrant": False}
@@ -177,7 +173,7 @@ def train(config: dict):
177
  gradient_checkpointing_kwargs={"use_reentrant": False}
178
  )
179
 
180
- # Training arguments
181
  output_dir = output_config["lora_dir"]
182
  os.makedirs(output_dir, exist_ok=True)
183
 
@@ -193,7 +189,7 @@ def train(config: dict):
193
  logging_steps=training_config.get("logging_steps", 10),
194
  save_steps=training_config.get("save_steps", 100),
195
  save_total_limit=training_config.get("save_total_limit", 2),
196
- bf16=True, # always use bf16 (works for both full and quantized)
197
  fp16=False,
198
  gradient_checkpointing=training_config.get("gradient_checkpointing", True),
199
  evaluation_strategy="steps" if eval_dataset else "no",
 
1
  #!/usr/bin/env python3
2
  """
3
  Simple standalone training script for Stack 2.9.
4
+ Uses bfloat16 with optional 4-bit quantization via bitsandbytes.
5
  """
6
 
7
  import argparse
 
16
  TrainingArguments,
17
  Trainer,
18
  DataCollatorForLanguageModeling,
 
19
  )
20
  from peft import LoraConfig, get_peft_model, TaskType
21
  import torch
 
31
  trust_remote_code: bool = True,
32
  use_4bit: bool = False,
33
  ):
34
+ """Load base model in bfloat16, with optional 4-bit quantization."""
35
  tokenizer = AutoTokenizer.from_pretrained(
36
  model_name, trust_remote_code=trust_remote_code
37
  )
38
+
39
  if use_4bit:
40
+ from transformers import BitsAndBytesConfig
41
  bnb_config = BitsAndBytesConfig(
42
  load_in_4bit=True,
43
  bnb_4bit_quant_type="nf4",
 
57
  trust_remote_code=trust_remote_code,
58
  device_map={"": torch.device("cuda")},
59
  )
60
+
61
  return model, tokenizer
62
 
63
 
 
105
  # Handle train_split logic
106
  total_samples = len(tokenized_dataset)
107
  if train_split >= 1.0:
 
108
  n_train = int(train_split)
109
  if n_train >= total_samples:
 
110
  return tokenized_dataset, None
111
  else:
 
112
  split = tokenized_dataset.train_test_split(train_size=n_train)
113
  return split["train"], split["test"]
114
  else:
 
115
  split = tokenized_dataset.train_test_split(train_size=train_split)
116
  return split["train"], split["test"]
117
 
 
124
  training_config = config["training"]
125
  output_config = config["output"]
126
  hardware_config = config.get("hardware", {})
 
127
 
128
+ use_4bit = hardware_config.get("use_4bit", False)
129
 
130
  # Load model and tokenizer
131
  print(f"Loading model: {model_config['name']} (4bit={use_4bit})")
 
161
  model = get_peft_model(model, peft_config)
162
  model.print_trainable_parameters()
163
 
164
+ # Enable gradient checkpointing with use_reentrant=False (required for PyTorch 2.9+)
 
165
  if training_config.get("gradient_checkpointing", True):
166
  model.gradient_checkpointing_enable(
167
  gradient_checkpointing_kwargs={"use_reentrant": False}
 
173
  gradient_checkpointing_kwargs={"use_reentrant": False}
174
  )
175
 
176
+ # Training arguments — bf16 is safe for both full and quantized models
177
  output_dir = output_config["lora_dir"]
178
  os.makedirs(output_dir, exist_ok=True)
179
 
 
189
  logging_steps=training_config.get("logging_steps", 10),
190
  save_steps=training_config.get("save_steps", 100),
191
  save_total_limit=training_config.get("save_total_limit", 2),
192
+ bf16=True,
193
  fp16=False,
194
  gradient_checkpointing=training_config.get("gradient_checkpointing", True),
195
  evaluation_strategy="steps" if eval_dataset else "no",