Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 43 additions & 13 deletions flashdreams/flashdreams/api_v2/loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,15 @@ def _begin_run(
) -> _LoopRunResult:
"""Prepare one step or return a lifecycle request to the caller."""
self._run_message_batch()
return self._incorporate_user_events(events, generation)

@final
def _incorporate_user_events(
self,
events: UserInputEvents,
generation: int,
) -> _LoopRunResult:
"""Add newly available user events to the prepared run."""
self._pending_user_events.extend(events.get_events())
transition = _parse_lifecycle_events(self._pending_user_events)
if transition is not None:
Expand Down Expand Up @@ -408,19 +417,6 @@ def _run_model_loop(
received_ns = event_buffer.last_read_received_ns(reader_id)
if step_control is not None:
events, generation = step_control.inputs(events, generation)
if generation != unpublished_generation:
unpublished_step_elapsed_s = 0.0
unpublished_generation = generation
# The reset discarded the buffered output this input went into.
if unpublished_inputs:
self.profiler.input_dropped(
[
ns
for arrivals, _ in unpublished_inputs
for ns in arrivals
]
)
unpublished_inputs.clear()
result: list[StepResult] | None = None
step_completed = False
try:
Expand All @@ -442,6 +438,40 @@ def _run_model_loop(
break
elif stopping:
break
# Every rank finishes preparation before entering the fresh-input broadcast.
events, generation = event_buffer.read(reader_id)
received_ns.extend(event_buffer.last_read_received_ns(reader_id))
if step_control is not None:
events, generation = step_control.inputs(events, generation)
try:
run = self._incorporate_user_events(events, generation)
except BaseException:
if step_control is not None:
try:
step_control.ready(stopping=True, failed=True)
except BaseException:
# A departed peer must not replace the original failure.
pass
raise
stopping = run.step_index is None or self._shutdown_event.is_set()
if step_control is not None:
if not step_control.ready(stopping=stopping):
break
elif stopping:
break
if generation != unpublished_generation:
unpublished_step_elapsed_s = 0.0
unpublished_generation = generation
# The reset discarded the buffered output this input went into.
if unpublished_inputs:
self.profiler.input_dropped(
[
ns
for arrivals, _ in unpublished_inputs
for ns in arrivals
]
)
unpublished_inputs.clear()
# Admission commits every rank to this step. A later UI stop is
# handled at the next boundary, never by skipping a collective.
assert run.step_index is not None
Expand Down
54 changes: 50 additions & 4 deletions flashdreams/test_v2/test_session_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,10 @@ def step(
assert published[0][1] == pytest.approx(1.0)


@pytest.mark.parametrize("reset_during_pacing", [False, True])
def test_model_loop_discards_buffered_step_time_after_reset(
monkeypatch: pytest.MonkeyPatch,
reset_during_pacing: bool,
) -> None:
now = 0.0
calls = 0
Expand All @@ -387,9 +389,10 @@ def step(
calls += 1
now += 0.4 if calls == 1 else 0.6
if calls == 1:
event_buffer.append(
UserInputEvents([ResetUserInputEvent(timestamp=uint64(0))])
)
if not reset_during_pacing:
event_buffer.append(
UserInputEvents([ResetUserInputEvent(timestamp=uint64(0))])
)
return []
return [
StepResult(
Expand All @@ -408,10 +411,21 @@ def reset(self) -> None:
model_loop = ResetBufferedModelLoop()
model_loop.register_session_loop_objects(
state=None,
frequency=0,
frequency=1,
shutdown_event=threading.Event(),
failure_queue=failure_queue,
)

def cadence_wait(timeout: float) -> bool:
nonlocal now
now += timeout
if reset_during_pacing:
event_buffer.append(
UserInputEvents([ResetUserInputEvent(timestamp=uint64(0))])
)
return False

monkeypatch.setattr(model_loop._shutdown_event, "wait", cadence_wait)
published: list[tuple[int, list[StepResult], float]] = []

model_loop._run_model_loop(
Expand Down Expand Up @@ -756,6 +770,38 @@ def _lifecycle_event(event_type: type[UserInputEvent]) -> UserInputEvents:
return UserInputEvents([event_type(timestamp=uint64(0))])


def test_model_loop_collects_input_after_pacing(
monkeypatch: pytest.MonkeyPatch,
) -> None:
session = FakeSession(_session_desc(), CallLog())
session.init()
session.model_loop.frequency = 1
event_buffer = EventBuffer()
event_buffer.register(0)

def cadence_wait(timeout: float) -> bool:
assert timeout > 0
event_buffer.append(_key_event())
return False

monkeypatch.setattr(session.model_loop._shutdown_event, "wait", cadence_wait)
session.model_loop._run_model_loop(
event_buffer=event_buffer,
reader_id=0,
publish=lambda generation, results, elapsed: None,
max_steps=2,
)

assert session.observed_events[0].get_events() == []
observed = session.observed_events[1].get_events()
assert len(observed) == 1
assert isinstance(observed[0], KeyboardUserInputEvent)
assert (observed[0].key, observed[0].state) == (
"a",
KeyboardInputState.PRESSED,
)


def test_run_session_presents_every_step_in_order() -> None:
log = CallLog()
session = FakeSession(_session_desc(), log)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_step_agreement.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ def ready(agreement, **kwargs):
if (
scenario == "stop_after_admission"
and rank == 0
and ready_calls == 2
and ready_calls == 3
):
assert admitted
session._shutdown_event.set()
Expand Down