Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
45 changes: 44 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,48 @@ All notable changes to this project are documented here. The format follows

## [Unreleased]

## [0.2.3] - 2026-11-24

0.2.3 makes the Python model classes stop a run that goes over the memory limit, as `mlx-dfloat generate` already did.

### Added

- Every `generate_image` and `encode` call runs under a memory watchdog by default. 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 is exported from `mlx_dfloat`, is a `MemoryError` and carries the path of the record when one was
written. Your process keeps running.
- `mlx-dfloat generate` has no stop line and is unchanged: it ends at the hard line (exit 70) or at its time limit
(exit 71).
- `WatchdogConfig`, exported from `mlx_dfloat.mflux`, sets the lines (`ceiling`, `stop_margin`), an optional time limit
(`wall_budget`), the `grace` period and the folder for the record (`artifact_dir`, a string or a path). Values are in
bytes. `ceiling` can only be lower than physical memory minus 4 GiB; a higher one is refused when the call starts.
`stop_margin` must be above 0 and below `ceiling`.
- A `watchdog=` keyword on every model class. It takes a `WatchdogConfig` or `None`, and `None` turns the watchdog off.
Anything else is refused when the model is built.
- 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.

### Behaviour of a trip

- A memory trip has no grace period. The process ends only if memory reaches the hard line (exit 70), as the command
does.
- Memory the process already holds before the call counts toward both lines. A call that starts at or above the stop
line raises at once, before it does 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 by default. Set `wall_budget` in `WatchdogConfig` to add one. When it passes, the call raises at
the 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.

### Not yet checked

- The abort has not yet been run on real weights on a Mac. The tests use small models and fake memory readings.

## [0.2.2] - 2026-11-13

0.2.2 moves `mlx-dfloat[mflux]` to mflux 0.24 or newer, so it installs next to current mflux and the other mlx-* libraries. If
Expand Down Expand Up @@ -424,7 +466,8 @@ The first release.
- CI's mflux lane gets the Hub token for its test step only, so the three tokenizer tests against the gated FLUX.1
bases run there instead of skipping.

[Unreleased]: https://github.com/IonDen/mlx-dfloat/compare/v0.2.2...HEAD
[Unreleased]: https://github.com/IonDen/mlx-dfloat/compare/v0.2.3...HEAD
[0.2.3]: https://github.com/IonDen/mlx-dfloat/compare/v0.2.2...v0.2.3
[0.2.2]: https://github.com/IonDen/mlx-dfloat/compare/v0.2.1...v0.2.2
[0.2.1]: https://github.com/IonDen/mlx-dfloat/compare/v0.2.0...v0.2.1
[0.2.0]: https://github.com/IonDen/mlx-dfloat/compare/v0.1.0...v0.2.0
Expand Down
37 changes: 34 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,40 @@ image.save("lighthouse.png")
FLUX.1-dev and FLUX.1-Krea-dev want more steps; the measured runs below used 20 steps and `guidance=3.5`.
`base_path=` takes the `--base` value, and `fit_check=False` replaces `--no-fit-check` (see below). From Python, each
`generate_image` call runs under the same memory caps as `mlx-dfloat generate` and puts MLX's limits back when it
returns; a wired limit your process already set is left alone. Only the `mlx-dfloat generate` command stops a run that
goes over the memory limit; the Python classes set memory caps but do not stop the process. Prefer the command on a Mac
near its memory limit.
returns; a wired limit your process already set is left alone.

From 0.2.3, each call also runs under a memory 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 raises `DFloatWatchdogError` at its next checkpoint. A checkpoint is a place where
the call can stop cleanly, such as between transformer blocks. The call frees what it loaded before it raises. The error
carries the path of the record when one was written, and your process keeps running.

`mlx-dfloat generate` has no stop line. It ends at the hard line (exit code 70) or at its time limit (exit code 71). The
model classes use a watchdog built like the command's that also raises at the stop line.

A memory trip has no grace period. The process ends only if memory reaches the hard line, with exit code 70 as the
command does. Memory the process already holds before the call counts toward both lines. A call that starts at or above
the stop line raises at once, before it does 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.
If memory jumps past both lines inside one of them, the process ends 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 you set `wall_budget=` in `WatchdogConfig`. If you do and the limit passes, the call raises
at its next checkpoint. If none comes within `grace` seconds (10 by default), the process ends with exit code 71. The
same grace applies when the watchdog cannot read memory: the process ends with exit code 70 rather than run unwatched.

These exits are immediate. Open files are not flushed and no cleanup handlers run.

To turn the watchdog off, pass `watchdog=None` to the model class. To move the lines, pass
`watchdog=WatchdogConfig(ceiling=..., stop_margin=...)`, with `WatchdogConfig` imported from `mlx_dfloat.mflux`. The
`watchdog=` keyword takes a `WatchdogConfig` or `None`, and the model class refuses anything else when you build it.
Values are in bytes. `ceiling` can only be lower than physical memory minus 4 GiB; a higher one is refused when the call
starts. `stop_margin` must be above 0 and below `ceiling`. `artifact_dir` takes a string or a path.

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.

Measured on an M1 Max at 1024², one image per model, with the commands under "Measured numbers": FLUX.1-schnell (4
steps, macOS 27.0.1, mflux 0.23.0) peaked at 19.77 GiB, FLUX.1-dev (20 steps, guidance 3.5) at 19.96 GiB and
Expand Down
7 changes: 6 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,11 @@ omit = [
]

[tool.coverage.report]
exclude_lines = ["pragma: no cover", "if TYPE_CHECKING:", "@overload", "raise NotImplementedError"]
exclude_lines = [
"pragma: no cover", "if TYPE_CHECKING:", "@overload", "raise NotImplementedError",
# scripts/prove_api_watchdog.py: the one function that builds a FLUX.2 Klein model on the GPU, never
# run by a test; the orchestrator around it is tested through a fake unit runner.
"^def run_unit\\(",
]
show_missing = true
fail_under = 85
Loading
Loading