Repository navigation
Conversation
…ints A watchdog in raise mode marks a trip at the stop line below its ceiling and leaves the call to stop at its next checkpoint; it still ends the process at the ceiling. Adds DFloatWatchdogError and WatchdogConfig.
generate_image and encode on each model class arm a raise-mode watchdog (watchdog=None turns it off), stop at block, load and encode checkpoints, free what they loaded and raise DFloatWatchdogError. The command and verify_image keep their own watchdog.
IonDen
force-pushed
the
feature/0056-api-watchdog
branch
from
November 25, 2026 07:22
f9ec5b9 to
0e24ca8
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
Until now only
mlx-dfloat generatestopped a run that went over the machine's memory. A script that calledgenerate_imageon one of the model classes set memory caps but could still push a Mac into swap or a freeze. Thischange gives the classes a watchdog of their own.
What changed
Every
generate_imageandencodecall now runs under a watchdog. It has a hard line (the ceiling: physical memoryminus 4 GiB unless you lower it) and a stop line 1 GiB below the hard line (physical memory minus 5 GiB by default).
Once the process passes the stop line, the call frees what it loaded and raises
DFloatWatchdogErrorat its nextcheckpoint, a place where the call can stop cleanly, such as between transformer blocks. The error carries the path of
the record when one was written, and the process stays alive. A later call reloads the weights and runs.
The command itself is unchanged.
mlx-dfloat generatehas no stop line and ends at the hard line (exit 70), or at itstime limit (exit 71). The classes use a watchdog built like the command's that also raises at the stop line.
There are two new public names, plus the
watchdog=keyword.DFloatWatchdogErroris exported frommlx_dfloatand isa
MemoryError.WatchdogConfigis exported frommlx_dfloat.mfluxand sets the lines (ceiling,stop_margin), anoptional time limit (
wall_budget), thegraceperiod and the folder for the record. Values are in bytes,stop_marginmust be above 0 and belowceiling, and aceilingabove physical memory minus 4 GiB is refused when thecall starts. Every model class takes the
watchdog=keyword: the default is on,watchdog=Noneturns it off, aWatchdogConfigchanges it, and anything else is refused when the model is built.A memory trip has no grace period. The process ends only if memory reaches the hard line (exit 70). Memory the process
already holds before the call counts, and a call that starts at or above the stop line raises at once, before any work.
Two stretches have no checkpoint: loading the text encoder together with the first prompt encoding (for ERNIE-Image
with guidance, the prompt and the negative prompt are encoded together), and decoding the image. A memory jump past both lines inside one of them ends the process at the hard line. If the call crossed the stop
line during the decode, the finished image is discarded and the call raises instead of returning it.
There is no time limit unless one is set. When a
wall_budgetpasses, the call raises at its next checkpoint, and ifnone comes within
graceseconds (10 by default) the process ends with exit 71. The same grace applies if the watchdogcannot read memory: the process ends with exit 70 rather than run unwatched. These exits are immediate: open files are
not flushed and no cleanup handlers run. The record is a JSON file written with mode 0600. By default it goes in a
private
mlx-dfloatfolder under the temp directory, which is set to owner-only access; withartifact_dir=it goes inthe folder you name. The watchdog needs
psutil, which the[mflux]extra installs. The README paragraph on the Pythonclasses and the changelog (a 0.2.3 entry) say the same.
How to verify
Hosted CI on this head gives
2390passed and691skipped in the default run, withcoverage
89%. The default count includes the mflux tests. The integration lane on hosted CI gives654passed and15skipped. The tests cover each phase of a call (prompt encoding,weight loading, denoising, image decode), nested calls, the state of the model after an abort, and the opt-out. They use
small models and fake memory readings.
Notes
The abort has not yet been run on real weights on a Mac. A proof script is in the branch. It has to run on a Mac with
the real weights before the 0.2.3 tag. This PR should not be merged before that check.Nothing in this change makes a speed or memory-saving claim.