feat(cycle): Cycle and Recurrence return early on Stop with the last finished state

Open
#383 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
56/100
Issue type
Feature
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
backend

Research direction

Read packages/pipekit-cycle/src/pipekit_cycle/cycles/_cycle.py and the P8 roadmap entry, then review the existing Cycle and Recurrence loops and their tests. Implement the specified stop handling, state and history behavior, and attributes for both classes; update their docstrings and the roadmap. Done means the listed stop, repeat-call, exception, history, and hash checks pass, along with make test, lint, and typecheck.

Written by the indexing model from the issue text.

Description

area:code priority:p2 type:feature

Problem / Request

pipekit_cycle.Cycle and Recurrence cannot end early and return where they were.
This issue makes both catch pipekit_ops.Stop (added by P8 (pipekit-ops, #382)), record it as self.stop, and return the stop's carrier with the state of the last step that finished.
It is the pipekit-cycle half of phase P8 of packages/pipekit-ops/docs/design/roadmap.md.

User Story

As the author of a chunked model run on Cycle, I want a monitor inside the step to end the run cleanly at a chunk edge, so that I get the last good state back and can write a checkpoint from it.

Motivation

sesmax-experiment runs a model in chunks as Cycle(step_op=chunk, n_steps=timeline.n_chunks)(first, 0), where chunk is a Sequential of an AdvanceChunk (a StatefulOperator whose state is the chunk index), the monitors, and the snapshot and checkpoint stages (run/chunks.py @ 12f169f6bf).
When a monitor wants the run to end (a wall-clock budget, a SIGTERM, a steady state), it raises sesmax's own RunStopped, which escapes Cycle.
To know the last frame for teardown, sesmax adds Tap(lambda frame: last.__setitem__(0, frame)) to the step.
With this issue the monitor raises Stop, Cycle returns, and both workarounds go.

Cycle._apply today (pipekit_cycle/cycles/_cycle.py @ be1b64a900):

def _apply(self, carrier, state):
    self.history = []
    for i in range(self.n_steps):
        carrier, state = self.step_op(carrier, state)
        if self.save_history and (i % self.history_stride == 0):
            self.history.append((carrier, state))
    return carrier, state

Recurrence._apply loops step_op until condition_op(state) is truthy or max_iters is reached, and records self.iters and self.converged.

Proposed API

# pipekit_cycle/cycles/_cycle.py  (proposed, this issue)
class Cycle(StatefulOperator):
    stop: Stop | None      # the Stop that ended the last call, or None
    steps_done: int        # the steps that finished in the last call

class Recurrence(StatefulOperator):
    stop: Stop | None      # as above; converged stays False when a Stop ends the loop

Neither constructor changes, and neither does get_config, so pipeline_hash stays the same.

Design Snapshot

from pipekit_ops import Stop
from pipekit_cycle import Cycle

# within the loop
try:
    carrier, state = self.step_op(carrier, state)
except Stop as stop:
    self.stop = stop
    return stop.carrier, state          # state: the last step that finished

The returned state is that of the last step that finished, because the step that raised did not return one.
The carrier is the stop's, so it can be one step ahead of the returned state when the operator that raised runs after the model advanced in the same step; sesmax's frames record their own chunk index, so the caller can tell.
History keeps only finished steps.
Any other exception propagates as today.
EnsembleCycle and WindowedCycle are out of scope; they can follow when a user needs them.

References & Existing Code

  • Roadmap: packages/pipekit-ops/docs/design/roadmap.md, phase P8 (merged in #374).
  • Cycle, Recurrence in packages/pipekit-cycle/src/pipekit_cycle/cycles/_cycle.py @ be1b64a900.
  • Stop from P8 (pipekit-ops, #382): Stop(reason, carrier=None, *, resumable=False, complete=False), a BaseException exported by pipekit_ops.
  • pipekit-cycle depends on pipekit-ops>=0.4.0 today (packages/pipekit-cycle/pyproject.toml @ be1b64a900).

Implementation Steps

  • Raise the floor in packages/pipekit-cycle/pyproject.toml to the pipekit-ops release that holds Stop.
  • In Cycle._apply, reset self.stop = None and self.steps_done = 0, catch Stop around self.step_op(...), and return (stop.carrier, state).
  • Do the same in Recurrence._apply, leaving converged false.
  • Update the docstrings of both classes with the new attributes and an example.
  • Mark the pipekit-cycle part of P8 as built in the roadmap.

Definition of Done

  • Code lands at the path above
  • Tests pass: make test
  • Lint and typecheck pass: make lint && make typecheck
  • Google-style docstrings describe stop and steps_done

Testing

  • G8-1: a Cycle of 10 steps whose step raises Stop at step 4 returns the stop's carrier and the state after step 3; cycle.stop.reason is the reason given and cycle.steps_done == 3.
  • G8-3: a second call of the same Cycle that runs to the end has cycle.stop is None and steps_done == 10.
  • G8-4: a ValueError raised at step 4 propagates out of Cycle and Recurrence unchanged.
  • G8-6: pipeline_hash of Cycle and Recurrence is unchanged (pinned in a test).
  • With save_history=True, a stop at step 4 leaves the history of steps 1 to 3 only.
  • Recurrence whose step raises Stop at iteration 2 returns its carrier with converged false and iters == 1.

Documentation

  • The pipekit-cycle API page lists stop and steps_done.

Relationships

  • Parent (theme epic): #379
  • Blocked by: #382, and a pipekit-ops release that holds Stop
  • Blocks: #385 (its gate G10-5 runs a guard inside a Cycle)
  • Related: #230 (pipekit-cycle roadmap)
Dominant language
Python
Stars
0
Forks
0
Avg merge
3h 50m
Merged PRs (30d)
143

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from jejjohnson/pipekit

All issues in jejjohnson/pipekit

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.