Skip to content

fix custom io reader/writer API - #130

Merged
andresy merged 1 commit into
mainfrom
fix-custom-io
Sep 14, 2026
Merged

andresy merged 1 commit into
mainfrom
fix-custom-io

Conversation

@andresy

@andresy andresy commented Aug 28, 2026

Copy link
Copy Markdown
Member

No description provided.

@davidkoski davidkoski left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, see ml-explore/mlx-swift#476

@andresy
andresy merged commit 2f8700a into main Sep 14, 2026
@andresy
andresy deleted the fix-custom-io branch September 14, 2026 22:15
aleroot added a commit to aleroot/mlx-swift that referenced this pull request Sep 15, 2026
ml-explore/mlx-c#130 changed `mlx_io_vtable` so that the callbacks report
whether they succeeded:

    int    (*seek)(void*, int64_t off, int whence);
    size_t (*read)(void*, char* data, size_t n);
    size_t (*read_at_offset)(void*, char* data, size_t n, size_t off);
    size_t (*write)(void*, const char* data, size_t n);

and `CReader`/`CWriter` now turn a negative seek or a short read/write into a
thrown `std::runtime_error`. Bump the mlx-c submodule to that commit (plus the
checked-in copies of `io_types.h` and the CMake `GIT_TAG`) and adapt the Swift
side:

- `FileIOState.seek` returns `0`/`-1` instead of silently ignoring a bad
  `whence` or a negative resulting offset.
- `FileIOState.read` returns the number of bytes actually read, so a file that
  is truncated after its header was parsed now fails instead of leaving the
  destination buffer uninitialized. The private implementation is renamed
  `readBytes` so it cannot be confused with the two public overloads.
- The in-memory reader reports an out-of-bounds read as `0` bytes rather than
  doing nothing, and its `seek` reports failure for an unknown `whence` or a
  negative offset.
- The file reader's `write` reports `0` bytes, so using it as a writer errors
  out instead of silently discarding the data.

This is the error-propagation path the load progress work was waiting on
(ml-explore/mlx#3742 is already in the vendored mlx v0.32.2), so the truncated
file test asserts a failure rather than skipping, and a new test truncates the
file *after* the header is parsed to cover the lazy read path:

    [mlx_io_reader] unable to read 65536 bytes (read 65504 instead) in file ...

Also fix the `withLoadProgressHandler` documentation links -- the
`-(_,()throws->R)` disambiguation does not resolve and `verify-docs.sh` builds
with `--warnings-as-errors` -- document why the reported progress is
approximate (loading is lazy, so it may stop short of the file size and it
counts bytes read rather than bytes covered), and add `LoadProgress` to the
MLX topics.
aleroot added a commit to aleroot/mlx-swift that referenced this pull request Oct 13, 2026
ml-explore/mlx-c#130 changed `mlx_io_vtable` so that the callbacks report
whether they succeeded:

    int    (*seek)(void*, int64_t off, int whence);
    size_t (*read)(void*, char* data, size_t n);
    size_t (*read_at_offset)(void*, char* data, size_t n, size_t off);
    size_t (*write)(void*, const char* data, size_t n);

and `CReader`/`CWriter` now turn a negative seek or a short read/write into a
thrown `std::runtime_error`. Bump the mlx-c submodule to that commit (plus the
checked-in copies of `io_types.h` and the CMake `GIT_TAG`) and adapt the Swift
side:

- `FileIOState.seek` returns `0`/`-1` instead of silently ignoring a bad
  `whence` or a negative resulting offset.
- `FileIOState.read` returns the number of bytes actually read, so a file that
  is truncated after its header was parsed now fails instead of leaving the
  destination buffer uninitialized. The private implementation is renamed
  `readBytes` so it cannot be confused with the two public overloads.
- The in-memory reader reports an out-of-bounds read as `0` bytes rather than
  doing nothing, and its `seek` reports failure for an unknown `whence` or a
  negative offset.
- The file reader's `write` reports `0` bytes, so using it as a writer errors
  out instead of silently discarding the data.

This is the error-propagation path the load progress work was waiting on
(ml-explore/mlx#3742 is already in the vendored mlx v0.32.2), so the truncated
file test asserts a failure rather than skipping, and a new test truncates the
file *after* the header is parsed to cover the lazy read path:

    [mlx_io_reader] unable to read 65536 bytes (read 65504 instead) in file ...

Also fix the `withLoadProgressHandler` documentation links -- the
`-(_,()throws->R)` disambiguation does not resolve and `verify-docs.sh` builds
with `--warnings-as-errors` -- document why the reported progress is
approximate (loading is lazy, so it may stop short of the file size and it
counts bytes read rather than bytes covered), and add `LoadProgress` to the
MLX topics.
davidkoski pushed a commit to ml-explore/mlx-swift that referenced this pull request Oct 21, 2026
* Add byte-progress reporting for safetensors file loading

Introduces LoadProgress and new loadArrays(url:stream:progressHandler:) and
loadArraysAndMetadata(url:stream:progressHandler:) overloads that report
bytes read as lazy arrays are evaluated. Uses a custom mlx_io_reader vtable
backed by pread() so progress callbacks can be invoked from MLX worker
threads. Includes a unit test verifying monotonic progress from 0 to 1.

* Add a scoped load-progress handler and fix memory reader SEEK_END

`loadWeights()` style helpers -- including the one in mlx-swift-lm -- call the
plain `loadArrays(url:)` / `loadArraysAndMetadata(url:)`, so a per-call
`progressHandler:` argument can never reach them without changing every caller
along the way.

Add `withLoadProgressHandler(_:_:)` (sync and async), a task local scoped
handler in the style of `withErrorHandler(_:_:)`. The plain file loading
functions report byte progress to it when one is installed, so an application
can drive a precise model loading progress bar around code it does not own:

    let container = try await withLoadProgressHandler({ tracker.update($0) }) {
        try await factory.loadContainer(from: directory, using: tokenizerLoader)
    }

`LoadProgress` gains the `url` of the file being read so progress can be
aggregated across the shards of a sharded model.

Also fix the SEEK_END case of the in-memory reader, which moved the offset
relative to the current position instead of the end of the data. mlx 0.32.1
seeks to the end of the stream to validate the tensor data offsets against the
size of the file, so `loadArrays(data:)` would fail there ("The JSON header is
N bytes long but the file is only 8 bytes").

Finally, restructure the truncated file test: a truncated file may now be
reported either eagerly, while the header is parsed, or lazily, when the arrays
are evaluated, and neither happens with the currently vendored mlx/mlx-c.

* Update for the mlx-c custom IO reader/writer API

ml-explore/mlx-c#130 changed `mlx_io_vtable` so that the callbacks report
whether they succeeded:

    int    (*seek)(void*, int64_t off, int whence);
    size_t (*read)(void*, char* data, size_t n);
    size_t (*read_at_offset)(void*, char* data, size_t n, size_t off);
    size_t (*write)(void*, const char* data, size_t n);

and `CReader`/`CWriter` now turn a negative seek or a short read/write into a
thrown `std::runtime_error`. Bump the mlx-c submodule to that commit (plus the
checked-in copies of `io_types.h` and the CMake `GIT_TAG`) and adapt the Swift
side:

- `FileIOState.seek` returns `0`/`-1` instead of silently ignoring a bad
  `whence` or a negative resulting offset.
- `FileIOState.read` returns the number of bytes actually read, so a file that
  is truncated after its header was parsed now fails instead of leaving the
  destination buffer uninitialized. The private implementation is renamed
  `readBytes` so it cannot be confused with the two public overloads.
- The in-memory reader reports an out-of-bounds read as `0` bytes rather than
  doing nothing, and its `seek` reports failure for an unknown `whence` or a
  negative offset.
- The file reader's `write` reports `0` bytes, so using it as a writer errors
  out instead of silently discarding the data.

This is the error-propagation path the load progress work was waiting on
(ml-explore/mlx#3742 is already in the vendored mlx v0.32.2), so the truncated
file test asserts a failure rather than skipping, and a new test truncates the
file *after* the header is parsed to cover the lazy read path:

    [mlx_io_reader] unable to read 65536 bytes (read 65504 instead) in file ...

Also fix the `withLoadProgressHandler` documentation links -- the
`-(_,()throws->R)` disambiguation does not resolve and `verify-docs.sh` builds
with `--warnings-as-errors` -- document why the reported progress is
approximate (loading is lazy, so it may stop short of the file size and it
counts bytes read rather than bytes covered), and add `LoadProgress` to the
MLX topics.

* Harden progress reads and cover lazy handler lifetime
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants