fix: correct 9 critical/high bugs found via multi-agent audit
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.backwardreturned 5 values for 3 inputs (forwardtakeslogits, target, position_mask). PyTorch autograd raisesRuntimeErroron every backward pass. -
specforge/distributed.pyβGather.backwardreturned 6 values for 5 inputs (one extra trailingNone). Crashes any distributed training run that uses sequence-parallel gather.
π΄ Silent Training Correctness Bugs
-
specforge/data/preprocessing.pyβ USPposition_idswere identical for all Ulysses ranks within the same ring group (ring_start = ring_rank * ring_chunkignoresulysses_rank). Withsp_ulysses_size > 1, all Ulysses peers claimed the same global position ID range, producing wrong attention patterns. Fixed by offsetting byulysses_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_intervalwas not scaled bydraft_accumulation_steps(unlikelog_intervalandeval_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 callingoptimizer.load_state_dict(resume_state)which restores both. -
specforge/lr_scheduler.pyβWarmupDelayerScheduler.step()passedepoch - warmup_epochsto the after-scheduler, missing thedelay_epochssubtraction. 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βHarmonyParserusedorinstead ofandin the first-message guard, making the condition alwaysTrue. Every conversation unconditionally received the reasoning effort prefix, including those that already had a system message. -
specforge/data/template.pyβTemplateRegistry.registerassert was logically inverted:assert (not override and ...)raisedAssertionErrorwheneveroverride=Truewas passed, making the override parameter non-functional.
π High Severity
-
specforge/core/eagle3.pyβ_compute_metric_accusedloss_mask.sum()as the denominator while the numerator was masked byposition_mask(which excludes out-of-vocabulary positions). This produced systematically deflated accuracy metrics throughout training. Fixed by usingposition_mask.sum()as denominator. -
specforge/modeling/target/eagle3_target_model.pyβpadding(target_out, left=False)was called unconditionally even whentarget_out is None(the case whenreturn_logits=False). This raisedAttributeErroronNone.__getitem__. Fixed with aNoneguard.
Test plan
- [ ] Verify
LogSoftmaxLossbackward runs withoutRuntimeErroron a minimal synthetic batch - [ ] Verify
Gather.backwardruns withoutRuntimeErrorin a 2-GPU USP setup - [ ] Verify
process_data_uspproduces distinctposition_idsfor each SP rank whensp_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
HarmonyParserdoes not prepend reasoning effort prefix when the first message is a system message
[!WARNING] You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!
HiοΌcan you update your PR? We have fix some bugs recently.
Updated the PR with the latest upstream/main and resolved the merge conflict. It is mergeable now.