mirror of
https://github.com/sous-labs/songe.git
synced 2026-12-01 12:47:14 -10:00
Training: a sample whose recompute differs is passed over, not the Dream (#235)
On a mixture-of-experts model a routing tie can send a token to another expert when a checkpointed layer is recomputed for the backward pass; torch then raises CheckpointError and the whole Deep Dream failed. Nothing is applied before that error, so the one sample is now skipped and training goes on. Fixes #232 Claude-Session: https://claude.ai/code/session_01RCtP4BBuHZ8UY83feh62qM Co-authored-by: Claude <[email protected]>
This commit is contained in:
1 parent
8aa3fdd15e
commit
c22031e776
2 files changed
+60
-6
No files matched your search
@@ -110,7 +110,7 @@ def train(backend, pool, anchors, work, progress):
|
||||
|
||||
optimizer = backend.optimizer(schedule)
|
||||
weights = sample_weights(pool)
|
||||
losses, learned = [], set()
|
||||
losses, learned, skipped = [], set(), 0
|
||||
is_torch = backend.kind == "torch"
|
||||
if not is_torch:
|
||||
from mlx.utils import tree_flatten, tree_map, tree_unflatten
|
||||
@@ -199,13 +199,26 @@ def train(backend, pool, anchors, work, progress):
|
||||
# A step boundary: the slice deadline may stop here, never midway.
|
||||
getattr(backend, "boundary", backend.check)()
|
||||
step_started = time.monotonic()
|
||||
gradients, loss, pending = None, 0.0, set()
|
||||
gradients, loss, pending, used = None, 0.0, set(), 0
|
||||
batch = plan[cursor : cursor + accumulation]
|
||||
for micro, (is_anchor, index) in enumerate(batch):
|
||||
step = anchors[index] if is_anchor else pool[index]
|
||||
value, grad, count = backend.train_step(
|
||||
step, optimizer, seed=7613 + cursor + micro
|
||||
)
|
||||
try:
|
||||
value, grad, count = backend.train_step(
|
||||
step, optimizer, seed=7613 + cursor + micro
|
||||
)
|
||||
except Exception as error:
|
||||
# A mixture-of-experts model can route a token to another
|
||||
# expert when a checkpointed layer is recomputed for the
|
||||
# backward pass (Inkling 2 Mini: a routing tie), and torch
|
||||
# then stops the step. Nothing was applied yet, so that
|
||||
# one sample is passed over instead of ending the Dream.
|
||||
if type(error).__name__ != "CheckpointError":
|
||||
raise
|
||||
skipped += 1
|
||||
getattr(backend, "release", lambda: None)()
|
||||
continue
|
||||
used += 1
|
||||
if not math.isfinite(value):
|
||||
raise ValueError("Non-finite training loss")
|
||||
if count < 1:
|
||||
@@ -242,7 +255,11 @@ def train(backend, pool, anchors, work, progress):
|
||||
# otherwise accumulation grows the allocator cache by each shape.
|
||||
getattr(backend, "release", lambda: None)()
|
||||
loss += value * weight
|
||||
size = len(batch)
|
||||
if gradients is None:
|
||||
# Every sample of this step was passed over.
|
||||
cursor += len(batch)
|
||||
continue
|
||||
size = used
|
||||
if is_torch:
|
||||
gradients = {n: _inplace(g, "div_", size) for n, g in gradients.items()}
|
||||
else:
|
||||
@@ -281,6 +298,8 @@ def train(backend, pool, anchors, work, progress):
|
||||
return {
|
||||
"iterations": iteration,
|
||||
"samples_trained": cursor,
|
||||
# In this slice; a resumed run counts from its resume.
|
||||
"samples_skipped": skipped,
|
||||
"steps_learned": len(learned),
|
||||
"trained_revisions": {
|
||||
s["id"]: revision_id(s) for s in pool if s["id"] in learned
|
||||
|
||||
@@ -126,6 +126,41 @@ def test_the_slice_deadline_stops_only_between_steps(config, tmp_path):
|
||||
assert saved["iteration"] == 2 and saved["sample_cursor"] == 4
|
||||
|
||||
|
||||
def test_a_sample_whose_recompute_differs_is_passed_over(config, tmp_path):
|
||||
"""Live (Inkling 2 Mini): a mixture-of-experts routing tie made the
|
||||
recomputed forward differ, torch raised CheckpointError and the whole
|
||||
Dream failed after 1.5 h. That sample is now passed over."""
|
||||
|
||||
class CheckpointError(RuntimeError):
|
||||
pass
|
||||
|
||||
config.accumulation, config.anchor_fraction = 2, 0
|
||||
pool, clock = steps(6), [0]
|
||||
backend = ScalarBackend(config, clock)
|
||||
plain = backend.train_step
|
||||
|
||||
def train_step(step, optimizer, seed):
|
||||
if step["id"] in ("2", "3", "4"):
|
||||
raise CheckpointError("recomputed tensor shapes differ")
|
||||
return plain(step, optimizer, seed)
|
||||
|
||||
backend.train_step = train_step
|
||||
result = train(backend, pool, [], private_dir(tmp_path / "work"), lambda *_: None)
|
||||
# Step 1 averages over its one good sample; step 2 had none and is not applied.
|
||||
assert backend.gradients == [1.0, 1.0]
|
||||
assert result["iterations"] == 2 and result["samples_skipped"] == 3
|
||||
assert sorted(result["trained_revisions"]) == ["1", "5", "6"]
|
||||
|
||||
# Any other error still stops the run.
|
||||
def broken(step, optimizer, seed):
|
||||
raise RuntimeError("out of memory")
|
||||
|
||||
other = ScalarBackend(config, clock)
|
||||
other.train_step = broken
|
||||
with pytest.raises(RuntimeError):
|
||||
train(other, steps(2, 50), [], private_dir(tmp_path / "other"), lambda *_: None)
|
||||
|
||||
|
||||
def test_resume_keeps_frozen_batch_cursor_and_rehearsal(config, tmp_path):
|
||||
config.accumulation, config.anchor_fraction = 3, 0.25
|
||||
pool, anchors, clock = steps(7), steps(3, 100), [0]
|
||||
|
||||
Reference in new issue
Block a user