AhmedSohair commited on
Commit
e8a2934
Β·
verified Β·
1 Parent(s): eaae268

Speed optimizations: packing=True, SDPA attention, Liger kernel, larger batch size

Browse files

Changes:
1. attn_implementation: eager β†’ sdpa (~2Γ— speedup, no install needed)
2. packing: False β†’ True (~1.5-2Γ— for short sequences, uses position_ids for correct masking)
3. use_liger_kernel: True (fused RMSNorm/RoPE/CrossEntropy, +20-40% throughput)
4. Batch size: 4Γ—4 β†’ 8Γ—2 (same effective bs=16, better GPU utilization with packing)
5. dataloader_num_workers: 0 β†’ 2 (minor speedup for packing preprocessing)

Expected combined speedup: ~3-6Γ— over previous config.
Previous 6h timeout should now complete in ~1-2h on L40S."

Files changed (1) hide show
  1. train_synthpai.py +31 -16
train_synthpai.py CHANGED
@@ -125,11 +125,11 @@ def create_single_comment_examples(dataset):
125
 
126
  def main():
127
  print("=" * 60, flush=True)
128
- print("SynthPAI Attribute Inference Training", flush=True)
129
  print("=" * 60, flush=True)
130
 
131
  os.environ["TRACKIO_PROJECT"] = "synthpai-attribute-inference-7b"
132
- os.environ["TRACKIO_RUN_NAME"] = "qwen25-7b-lora-r64-ep1"
133
  os.makedirs(OUTPUT_DIR, exist_ok=True)
134
 
135
  # ── Load dataset ───────────────────────────────────────────────
@@ -198,24 +198,33 @@ def main():
198
  )
199
 
200
  # ── Training Config ────────────────────────────────────────────
201
- print("\n[5/7] Training config...", flush=True)
202
  use_bf16 = torch.cuda.is_available() and torch.cuda.is_bf16_supported()
203
  use_fp16 = torch.cuda.is_available() and not use_bf16
204
  if torch.cuda.is_available():
205
- print(f" GPU: {torch.cuda.get_device_name(0)} | VRAM: {torch.cuda.get_device_properties(0).total_memory / 1e9:.1f}GB | bf16: {use_bf16}", flush=True)
 
 
206
 
207
  training_args = SFTConfig(
208
  output_dir=OUTPUT_DIR,
209
  push_to_hub=True,
210
  hub_model_id=HUB_MODEL_ID,
211
  hub_strategy="every_save", # push checkpoints to Hub β€” protects against timeout
212
- # SFT
 
 
 
213
  max_length=1024,
214
- packing=False,
 
 
 
 
215
  # Hyperparams
216
- num_train_epochs=1, # 1 epoch β€” eval loss was increasing, avoid overfitting
217
- per_device_train_batch_size=4,
218
- gradient_accumulation_steps=4, # effective bs = 16
219
  learning_rate=2e-4,
220
  lr_scheduler_type="cosine",
221
  warmup_steps=100,
@@ -229,7 +238,7 @@ def main():
229
  # Eval & Save
230
  eval_strategy="steps",
231
  eval_steps=500,
232
- per_device_eval_batch_size=4,
233
  save_strategy="steps",
234
  save_steps=500,
235
  save_total_limit=3,
@@ -240,11 +249,20 @@ def main():
240
  logging_first_step=True,
241
  disable_tqdm=True,
242
  report_to="trackio",
243
- run_name="qwen25-7b-lora-r64-ep1",
244
  seed=42,
245
- dataloader_num_workers=0, # CRITICAL: multi-worker deadlocks in containers
 
 
246
  remove_unused_columns=True,
247
  )
 
 
 
 
 
 
 
248
  print(f" Epochs: {training_args.num_train_epochs}", flush=True)
249
  print(f" Effective batch size: {training_args.per_device_train_batch_size * training_args.gradient_accumulation_steps}", flush=True)
250
  print(f" Hub strategy: {training_args.hub_strategy} (checkpoints pushed to Hub)", flush=True)
@@ -252,9 +270,6 @@ def main():
252
  # ── Train ──────────────────────────────────────────────────────
253
  print("\n[6/7] Loading model and initializing trainer...", flush=True)
254
 
255
- # Force eager attention β€” SDPA can hang without flash-attn
256
- training_args.model_init_kwargs = {"attn_implementation": "eager"}
257
-
258
  trainer = SFTTrainer(
259
  model=MODEL_ID,
260
  args=training_args,
@@ -284,7 +299,7 @@ def main():
284
  print(f" {k}: {v}", flush=True)
285
 
286
  print(f"\nPushing final model to {HUB_MODEL_ID}...", flush=True)
287
- trainer.push_to_hub(commit_message="SynthPAI attribute inference - Qwen2.5-7B LoRA r64 (1 epoch)")
288
 
289
  from huggingface_hub import HfApi
290
  HfApi().upload_file(
 
125
 
126
  def main():
127
  print("=" * 60, flush=True)
128
+ print("SynthPAI Attribute Inference Training (OPTIMIZED)", flush=True)
129
  print("=" * 60, flush=True)
130
 
131
  os.environ["TRACKIO_PROJECT"] = "synthpai-attribute-inference-7b"
132
+ os.environ["TRACKIO_RUN_NAME"] = "qwen25-7b-lora-r64-ep1-optimized"
133
  os.makedirs(OUTPUT_DIR, exist_ok=True)
134
 
135
  # ── Load dataset ───────────────────────────────────────────────
 
198
  )
199
 
200
  # ── Training Config ────────────────────────────────────────────
201
+ print("\n[5/7] Training config (OPTIMIZED)...", flush=True)
202
  use_bf16 = torch.cuda.is_available() and torch.cuda.is_bf16_supported()
203
  use_fp16 = torch.cuda.is_available() and not use_bf16
204
  if torch.cuda.is_available():
205
+ print(f" GPU: {torch.cuda.get_device_name(0)} | VRAM: {torch.cuda.get_device_properties(0).total_mem / 1e9:.1f}GB | bf16: {use_bf16}", flush=True)
206
+
207
+ print(" SPEED OPTS: packing=True, attn=sdpa, liger_kernel=True, bs=8Γ—2", flush=True)
208
 
209
  training_args = SFTConfig(
210
  output_dir=OUTPUT_DIR,
211
  push_to_hub=True,
212
  hub_model_id=HUB_MODEL_ID,
213
  hub_strategy="every_save", # push checkpoints to Hub β€” protects against timeout
214
+ # ── SPEED: Sequence packing ──────────────────────────────
215
+ # Packs multiple short examples into 1024-token blocks.
216
+ # Eliminates padding waste β†’ ~1.5-2Γ— speedup for short sequences.
217
+ # TRL uses position_ids for correct per-example attention masking.
218
  max_length=1024,
219
+ packing=True,
220
+ # ── SPEED: Fused Triton kernels (Liger) ──────────────────
221
+ # Fuses RMSNorm (7Γ—), RoPE (8Γ—), CrossEntropy (3Γ—, critical for Qwen 152K vocab).
222
+ # +20-40% throughput, -55% memory. Zero code change.
223
+ use_liger_kernel=True,
224
  # Hyperparams
225
+ num_train_epochs=1,
226
+ per_device_train_batch_size=8, # ← was 4
227
+ gradient_accumulation_steps=2, # ← was 4 (effective bs still 16)
228
  learning_rate=2e-4,
229
  lr_scheduler_type="cosine",
230
  warmup_steps=100,
 
238
  # Eval & Save
239
  eval_strategy="steps",
240
  eval_steps=500,
241
+ per_device_eval_batch_size=8,
242
  save_strategy="steps",
243
  save_steps=500,
244
  save_total_limit=3,
 
249
  logging_first_step=True,
250
  disable_tqdm=True,
251
  report_to="trackio",
252
+ run_name="qwen25-7b-lora-r64-ep1-optimized",
253
  seed=42,
254
+ # ── SPEED: Dataloader ────────────────────────────────────
255
+ dataloader_num_workers=2, # ← was 0
256
+ dataloader_pin_memory=True,
257
  remove_unused_columns=True,
258
  )
259
+
260
+ # ── SPEED: SDPA attention ────────────────────────────────────
261
+ # ~2Γ— faster than eager, no install needed (built into PyTorch 2.1+).
262
+ # The old "SDPA hangs" issue was fixed in PyTorch 2.1+.
263
+ # Avoids the slow flash-attn compilation step in containers.
264
+ training_args.model_init_kwargs = {"attn_implementation": "sdpa"}
265
+
266
  print(f" Epochs: {training_args.num_train_epochs}", flush=True)
267
  print(f" Effective batch size: {training_args.per_device_train_batch_size * training_args.gradient_accumulation_steps}", flush=True)
268
  print(f" Hub strategy: {training_args.hub_strategy} (checkpoints pushed to Hub)", flush=True)
 
270
  # ── Train ──────────────────────────────────────────────────────
271
  print("\n[6/7] Loading model and initializing trainer...", flush=True)
272
 
 
 
 
273
  trainer = SFTTrainer(
274
  model=MODEL_ID,
275
  args=training_args,
 
299
  print(f" {k}: {v}", flush=True)
300
 
301
  print(f"\nPushing final model to {HUB_MODEL_ID}...", flush=True)
302
+ trainer.push_to_hub(commit_message="SynthPAI attribute inference - Qwen2.5-7B LoRA r64 (1 epoch, optimized)")
303
 
304
  from huggingface_hub import HfApi
305
  HfApi().upload_file(