fix: early_stop patience counts epochs not steps

This commit is contained in:
2026-07-16 06:09:11 -04:00
parent 25918a73ff
commit c44f6010eb
5 changed files with 48 additions and 87 deletions
+34 -70
View File
@@ -349,92 +349,56 @@ class TestBuildComparisonPairs:
# =========================================================================
def _write_manifest_with_best(tmp_path: Path, best_epoch: int) -> None:
"""写含 best.epoch 的 manifest,供 _should_early_stop 读 read_best。"""
manifest = {
"name": "test",
"store": ".",
"current": {"videos": "v", "questions": "q", "skills": "s/v1", "prompts": "p/v1"},
"best": {"epoch": best_epoch, "val_acc": 0.5},
"history": [],
}
(tmp_path / "manifest.json").write_text(json.dumps(manifest))
class TestShouldEarlyStop:
"""_should_early_stop 粒度 early stop。"""
"""_should_early_stop epoch 粒度 early stoppatience 以 epoch 计)"""
def test_improved_this_epoch_resets(self, tmp_path: Path) -> None:
"""本 epoch best 刷新时重置计数器。"""
# 写 manifest + best
manifest = {
"name": "test",
"store": ".",
"current": {"videos": "v", "questions": "q", "skills": "s/v1", "prompts": "p/v1"},
"best": {"epoch": 2, "val_acc": 0.9},
"history": [],
}
(tmp_path / "manifest.json").write_text(json.dumps(manifest))
_write_manifest_with_best(tmp_path, best_epoch=2)
state = MagicMock()
state.steps_since_best_improved = 10
state.epochs_since_best_improved = 3
result = _should_early_stop(tmp_path, epoch=2, steps_this_epoch=5, state=state, patience=20)
result = _should_early_stop(tmp_path, epoch=2, state=state, patience=2)
assert result is False
assert state.steps_since_best_improved == 0
def test_no_improvement_accumulates(self, tmp_path: Path) -> None:
"""未刷新时累加步数。"""
manifest = {
"name": "test",
"store": ".",
"current": {"videos": "v", "questions": "q", "skills": "s/v1", "prompts": "p/v1"},
"best": {"epoch": 1, "val_acc": 0.5},
"history": [],
}
(tmp_path / "manifest.json").write_text(json.dumps(manifest))
state = MagicMock()
state.steps_since_best_improved = 15
result = _should_early_stop(tmp_path, epoch=3, steps_this_epoch=5, state=state, patience=20)
assert result is True # 15 + 5 = 20 >= 20
assert state.steps_since_best_improved == 20
assert state.epochs_since_best_improved == 0
def test_below_patience_continues(self, tmp_path: Path) -> None:
"""累计步数未达阈值时继续。"""
manifest = {
"name": "test",
"store": ".",
"current": {"videos": "v", "questions": "q", "skills": "s/v1", "prompts": "p/v1"},
"best": {"epoch": 1, "val_acc": 0.5},
"history": [],
}
(tmp_path / "manifest.json").write_text(json.dumps(manifest))
"""未达 patience 个 epoch 无刷新时继续。"""
_write_manifest_with_best(tmp_path, best_epoch=1)
state = MagicMock()
state.steps_since_best_improved = 10
state.epochs_since_best_improved = 0
result = _should_early_stop(tmp_path, epoch=3, steps_this_epoch=5, state=state, patience=20)
result = _should_early_stop(tmp_path, epoch=2, state=state, patience=3)
assert result is False
assert state.steps_since_best_improved == 15
assert state.epochs_since_best_improved == 1
def test_step_granularity(self, tmp_path: Path) -> None:
"""步粒度而非 epoch 粒度"""
manifest = {
"name": "test",
"store": ".",
"current": {"videos": "v", "questions": "q", "skills": "s/v1", "prompts": "p/v1"},
"best": {"epoch": 1, "val_acc": 0.5},
"history": [],
}
(tmp_path / "manifest.json").write_text(json.dumps(manifest))
def test_early_stop_counts_epochs_not_steps(self, tmp_path: Path) -> None:
"""patience=2 表示连续 2 个 epoch 无 best 刷新才停(不是步数)"""
_write_manifest_with_best(tmp_path, best_epoch=1)
state = MagicMock()
# 连续 3 个 epoch,每个 3 步
state.steps_since_best_improved = 0
for ep in range(2, 5):
stopped = _should_early_stop(
tmp_path, epoch=ep, steps_this_epoch=3, state=state, patience=10
)
if ep < 4:
assert stopped is False
else:
# 3+3+3=9 < 10 但第三轮后 9+3=12>=10 在 ep=5 触发
# 实际:ep=2 → 3, ep=3 → 6, ep=4 → 9
assert stopped is False
stopped = _should_early_stop(
tmp_path, epoch=5, steps_this_epoch=3, state=state, patience=10
)
assert stopped is True # 9+3=12>=10
state.epochs_since_best_improved = 0
# epoch 2 无刷新 → 1 → 不停
assert _should_early_stop(tmp_path, epoch=2, state=state, patience=2) is False
assert state.epochs_since_best_improved == 1
# epoch 3 无刷新 → 2 → 停
assert _should_early_stop(tmp_path, epoch=3, state=state, patience=2) is True
assert state.epochs_since_best_improved == 2
# =========================================================================
@@ -667,7 +631,7 @@ class TestTrainState:
assert state.system_packs == []
assert state.tool_packs == []
assert state.changed_task_types_this_epoch == set()
assert state.steps_since_best_improved == 0
assert state.epochs_since_best_improved == 0
# =========================================================================