feat(cycle): Cycle and Recurrence return early on Stop with the last finished state
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 56/100
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
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,Recurrenceinpackages/pipekit-cycle/src/pipekit_cycle/cycles/_cycle.py@be1b64a900.Stopfrom P8 (pipekit-ops, #382):Stop(reason, carrier=None, *, resumable=False, complete=False), aBaseExceptionexported bypipekit_ops.- pipekit-cycle depends on
pipekit-ops>=0.4.0today (packages/pipekit-cycle/pyproject.toml@be1b64a900).
Implementation Steps
- Raise the floor in
packages/pipekit-cycle/pyproject.tomlto the pipekit-ops release that holdsStop. - In
Cycle._apply, resetself.stop = Noneandself.steps_done = 0, catchStoparoundself.step_op(...), and return(stop.carrier, state). - Do the same in
Recurrence._apply, leavingconvergedfalse. - 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
stopandsteps_done
Testing
- G8-1: a
Cycleof 10 steps whose step raisesStopat step 4 returns the stop's carrier and the state after step 3;cycle.stop.reasonis the reason given andcycle.steps_done == 3. - G8-3: a second call of the same
Cyclethat runs to the end hascycle.stop is Noneandsteps_done == 10. - G8-4: a
ValueErrorraised at step 4 propagates out ofCycleandRecurrenceunchanged. - G8-6:
pipeline_hashofCycleandRecurrenceis unchanged (pinned in a test). - With
save_history=True, a stop at step 4 leaves the history of steps 1 to 3 only. -
Recurrencewhose step raisesStopat iteration 2 returns its carrier withconvergedfalse anditers == 1.
Documentation
- The pipekit-cycle API page lists
stopandsteps_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
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from jejjohnson/pipekit
-
area:code layer:0-primitives priority:p2 type:feature
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
jejjohnson/pipekit#296 ·
Maintainers usually reply within 1 day
-
area:code type:feature
Difficulty 3/5 1-2 days Newbie friendliness 57/100
jejjohnson/pipekit#411 ·
Maintainers usually reply within 1 day
-
area:code type:feature
Difficulty 4/5 3-5 days Newbie friendliness 48/100
jejjohnson/pipekit#410 ·
Maintainers usually reply within 1 day
-
area:code type:feature
Difficulty 5/5 Over a week Newbie friendliness 38/100
jejjohnson/pipekit#409 ·
Maintainers usually reply within 1 day
-
area:code type:feature
Difficulty 3/5 1-2 days Newbie friendliness 75/100
jejjohnson/pipekit#408 ·
Maintainers usually reply within 1 day
All issues in jejjohnson/pipekit
Similar issues
-
bug(operator): VolterraOperator built inside jax.jit with a concrete decay raises TracerBoolConversionErrorPossibly taken @jejjohnson claimed this today. Opentype:bug
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
jejjohnson/integrix#144 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
finos/ai-governance-framework#459 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
autowarefoundation/autoware_carla_scenario#2963 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
freelawproject/litigant-portal#1307 ·
Maintainers usually reply within 2 days
-
type/bug
Difficulty 1/5 Under an hour Newbie friendliness 86/100
h2oai/h2o-llmstudio#1163 ·
Maintainers usually reply within 1 day