Skip to content

Run the Python model classes under the memory watchdog by default - #21

Open
IonDen wants to merge 4 commits into
mainfrom
feature/0056-api-watchdog
Open

IonDen wants to merge 4 commits into
mainfrom
feature/0056-api-watchdog

Conversation

@IonDen

@IonDen IonDen commented Nov 24, 2026 •

Copy link
Copy Markdown
Owner

Why

Until now only mlx-dfloat generate stopped a run that went over the machine's memory. A script that called
generate_image on one of the model classes set memory caps but could still push a Mac into swap or a freeze. This
change gives the classes a watchdog of their own.

What changed

Every generate_image and encode call now runs under a watchdog. It has a hard line (the ceiling: physical memory
minus 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 DFloatWatchdogError at its next
checkpoint, 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 generate has no stop line and ends at the hard line (exit 70), or at its
time 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. DFloatWatchdogError is exported from mlx_dfloat and is
a MemoryError. WatchdogConfig is exported from mlx_dfloat.mflux and sets the lines (ceiling, stop_margin), an
optional time limit (wall_budget), the grace period and the folder for the record. Values are in bytes,
stop_margin must be above 0 and below ceiling, and a ceiling above physical memory minus 4 GiB is refused when the
call starts. Every model class takes the watchdog= keyword: the default is on, watchdog=None turns it off, a
WatchdogConfig changes 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_budget passes, the call raises at its next checkpoint, and if
none comes within grace seconds (10 by default) the process ends with exit 71. The same grace applies if the watchdog
cannot 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-dfloat folder under the temp directory, which is set to owner-only access; with artifact_dir= it goes in
the folder you name. The watchdog needs psutil, which the [mflux] extra installs. The README paragraph on the Python
classes and the changelog (a 0.2.3 entry) say the same.

How to verify

Hosted CI on this head gives 2390 passed and 691 skipped in the default run, with
coverage 89%. The default count includes the mflux tests. The integration lane on hosted CI gives
654 passed and 15 skipped. 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.

@IonDen IonDen self-assigned this Nov 24, 2026
@IonDen IonDen added the enhancement New feature or request label Nov 24, 2026
…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
IonDen force-pushed the feature/0056-api-watchdog branch from f9ec5b9 to 0e24ca8 Compare November 25, 2026 07:22
@IonDen IonDen added the documentation Improvements or additions to documentation label Nov 25, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant