SpecForge icon indicating copy to clipboard operation
SpecForge copied to clipboard

fix: correct 9 critical/high bugs found via multi-agent audit

Open imitater-dou opened this issue 6 months ago β€’ 3 comments

Summary

This PR fixes 9 confirmed bugs discovered through a multi-agent static analysis of the codebase, all verified by direct source inspection before patching.

πŸ”΄ Runtime Crashes (will crash on first use)

  • specforge/core/loss.py β€” LogSoftmaxLoss.backward returned 5 values for 3 inputs (forward takes logits, target, position_mask). PyTorch autograd raises RuntimeError on every backward pass.

  • specforge/distributed.py β€” Gather.backward returned 6 values for 5 inputs (one extra trailing None). Crashes any distributed training run that uses sequence-parallel gather.

πŸ”΄ Silent Training Correctness Bugs

  • specforge/data/preprocessing.py β€” USP position_ids were identical for all Ulysses ranks within the same ring group (ring_start = ring_rank * ring_chunk ignores ulysses_rank). With sp_ulysses_size > 1, all Ulysses peers claimed the same global position ID range, producing wrong attention patterns. Fixed by offsetting by ulysses_rank * usp_chunk_size.

  • scripts/train_eagle3.py β€” draft_model.eval() was set at the start of the evaluation loop but never reversed. All training steps after the first eval checkpoint ran with dropout disabled. Also, save_interval was not scaled by draft_accumulation_steps (unlike log_interval and eval_interval), causing 4Γ— more frequent saves than intended.

  • scripts/train_dflash.py β€” On resume, only the LR scheduler state was restored (optimizer.scheduler.load_state_dict); the AdamW optimizer's first/second moment estimates (exp_avg, exp_avg_sq) were silently discarded, producing a loss spike at every resume. Fixed by calling optimizer.load_state_dict(resume_state) which restores both.

  • specforge/lr_scheduler.py β€” WarmupDelayerScheduler.step() passed epoch - warmup_epochs to the after-scheduler, missing the delay_epochs subtraction. The cosine schedule started at a non-zero offset, causing a discontinuous LR jump after the warmup+delay phase.

πŸ”΄ Logic Bugs in Data Pipeline

  • specforge/data/parse.py β€” HarmonyParser used or instead of and in the first-message guard, making the condition always True. Every conversation unconditionally received the reasoning effort prefix, including those that already had a system message.

  • specforge/data/template.py β€” TemplateRegistry.register assert was logically inverted: assert (not override and ...) raised AssertionError whenever override=True was passed, making the override parameter non-functional.

🟠 High Severity

  • specforge/core/eagle3.py β€” _compute_metric_acc used loss_mask.sum() as the denominator while the numerator was masked by position_mask (which excludes out-of-vocabulary positions). This produced systematically deflated accuracy metrics throughout training. Fixed by using position_mask.sum() as denominator.

  • specforge/modeling/target/eagle3_target_model.py β€” padding(target_out, left=False) was called unconditionally even when target_out is None (the case when return_logits=False). This raised AttributeError on None.__getitem__. Fixed with a None guard.

Test plan

  • [ ] Verify LogSoftmaxLoss backward runs without RuntimeError on a minimal synthetic batch
  • [ ] Verify Gather.backward runs without RuntimeError in a 2-GPU USP setup
  • [ ] Verify process_data_usp produces distinct position_ids for each SP rank when sp_ulysses_size > 1
  • [ ] Verify training resumes correctly (loss continues from pre-resume level, no spike)
  • [ ] Verify draft_model.train() mode is active during training steps after an eval checkpoint
  • [ ] Verify TemplateRegistry.register(name, tpl, override=True) no longer raises
  • [ ] Verify HarmonyParser does not prepend reasoning effort prefix when the first message is a system message

imitater-dou avatar May 26 '26 11:05 imitater-dou

[!WARNING] You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

gemini-code-assist avatar May 26 '26 11:05 gemini-code-assist

Hi,can you update your PR? We have fix some bugs recently.

jiapingW avatar Jun 11 '26 04:06 jiapingW

Updated the PR with the latest upstream/main and resolved the merge conflict. It is mergeable now.

imitater-dou avatar Jun 15 '26 09:06 imitater-dou