torobi 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +71 -0
- data/Cargo.lock +539 -0
- data/Cargo.toml +6 -0
- data/LICENSE +21 -0
- data/README.md +259 -0
- data/config/ops.yml +230 -0
- data/docs/plan.md +3982 -0
- data/docs/vendoring.md +656 -0
- data/engine/Cargo.toml +35 -0
- data/engine/build.rs +253 -0
- data/engine/src/checkpoint.rs +513 -0
- data/engine/src/executor.rs +257 -0
- data/engine/src/export.rs +431 -0
- data/engine/src/fixtures.rs +518 -0
- data/engine/src/graph.rs +115 -0
- data/engine/src/init.rs +65 -0
- data/engine/src/interp.rs +985 -0
- data/engine/src/lib.rs +62 -0
- data/engine/src/main.rs +97 -0
- data/engine/src/memory.rs +109 -0
- data/engine/src/mlxc/array.rs +454 -0
- data/engine/src/mlxc/error.rs +205 -0
- data/engine/src/mlxc/fast.rs +46 -0
- data/engine/src/mlxc/handle.rs +155 -0
- data/engine/src/mlxc/io.rs +268 -0
- data/engine/src/mlxc/memory.rs +56 -0
- data/engine/src/mlxc/metal.rs +24 -0
- data/engine/src/mlxc/mod.rs +55 -0
- data/engine/src/mlxc/nn.rs +38 -0
- data/engine/src/mlxc/ops.rs +477 -0
- data/engine/src/mlxc/quantized.rs +405 -0
- data/engine/src/mlxc/random.rs +161 -0
- data/engine/src/mlxc/transforms.rs +439 -0
- data/engine/src/op.rs +745 -0
- data/engine/src/optimizer.rs +650 -0
- data/engine/src/plan.rs +1638 -0
- data/engine/src/runtime.rs +377 -0
- data/engine/src/session.rs +1523 -0
- data/engine/src/state.rs +1007 -0
- data/engine/src/tensor.rs +331 -0
- data/ext/torobi/Cargo.toml +21 -0
- data/ext/torobi/extconf.rb +42 -0
- data/ext/torobi/src/gvl.rs +182 -0
- data/ext/torobi/src/lib.rs +1037 -0
- data/lib/torobi/adapter_file.rb +184 -0
- data/lib/torobi/batch.rb +55 -0
- data/lib/torobi/checkpoint.rb +60 -0
- data/lib/torobi/dsl/builder.rb +414 -0
- data/lib/torobi/dsl/handle.rb +156 -0
- data/lib/torobi/dsl/layers.rb +82 -0
- data/lib/torobi/dsl.rb +35 -0
- data/lib/torobi/errors.rb +88 -0
- data/lib/torobi/export.rb +534 -0
- data/lib/torobi/freeze.rb +24 -0
- data/lib/torobi/freshness.rb +57 -0
- data/lib/torobi/grad_cache.rb +143 -0
- data/lib/torobi/graph_config.rb +301 -0
- data/lib/torobi/hooks.rb +439 -0
- data/lib/torobi/ir/dimensions.rb +26 -0
- data/lib/torobi/ir/dtype.rb +28 -0
- data/lib/torobi/ir/graph/check.rb +116 -0
- data/lib/torobi/ir/graph.rb +52 -0
- data/lib/torobi/ir/input_spec.rb +39 -0
- data/lib/torobi/ir/json.rb +45 -0
- data/lib/torobi/ir/node_spec.rb +52 -0
- data/lib/torobi/ir/parameter_spec.rb +67 -0
- data/lib/torobi/ir/ref.rb +52 -0
- data/lib/torobi/ir/source.rb +65 -0
- data/lib/torobi/ir.rb +23 -0
- data/lib/torobi/journal.rb +310 -0
- data/lib/torobi/lora.rb +190 -0
- data/lib/torobi/memory.rb +50 -0
- data/lib/torobi/metallib.rb +50 -0
- data/lib/torobi/mlx_prebuilt.json +24 -0
- data/lib/torobi/mlx_prebuilt.rb +378 -0
- data/lib/torobi/models/build.rb +37 -0
- data/lib/torobi/models/description.rb +59 -0
- data/lib/torobi/models/gemma3.rb +273 -0
- data/lib/torobi/models/llama.rb +381 -0
- data/lib/torobi/models/modern_bert.rb +513 -0
- data/lib/torobi/models/padding.rb +27 -0
- data/lib/torobi/models/windows.rb +53 -0
- data/lib/torobi/ops.rb +81 -0
- data/lib/torobi/preflight.rb +136 -0
- data/lib/torobi/provenance.rb +75 -0
- data/lib/torobi/quantization.rb +151 -0
- data/lib/torobi/replay.rb +229 -0
- data/lib/torobi/runner.rb +419 -0
- data/lib/torobi/session.rb +855 -0
- data/lib/torobi/shape.rb +438 -0
- data/lib/torobi/tensor_data.rb +178 -0
- data/lib/torobi/version.rb +5 -0
- data/lib/torobi/weights.rb +108 -0
- data/lib/torobi.rb +67 -0
- data/licenses/MLX.txt +21 -0
- data/licenses/README.md +16 -0
- data/licenses/gguflib.txt +21 -0
- data/licenses/mlx-c.txt +21 -0
- data/licenses/mlx-rs.txt +27 -0
- metadata +229 -0
data/docs/vendoring.md
ADDED
|
@@ -0,0 +1,656 @@
|
|
|
1
|
+
# Dependency ledger
|
|
2
|
+
|
|
3
|
+
What the engine is built against, and how that was decided.
|
|
4
|
+
|
|
5
|
+
## History
|
|
6
|
+
|
|
7
|
+
The dependency moved on 2026-09-21. Why it was where it was is kept
|
|
8
|
+
below rather than deleted: the older sections are true of the older
|
|
9
|
+
arrangement, and the reason they were true is what makes the move's
|
|
10
|
+
conditions checkable.
|
|
11
|
+
|
|
12
|
+
**2026-09-03 - OminiX-MLX adopted.** `mlx-rs` / `mlx-sys` were taken as a
|
|
13
|
+
git dependency on `OminiX-ai/OminiX-MLX` (commit `4988a3f`). Upstream's
|
|
14
|
+
`mlx-rs` was a generation behind (MLX 0.25 against a 0.30 pre-built
|
|
15
|
+
archive), and its `mlx-sys` had no way to build without Xcode: it ran
|
|
16
|
+
cmake on its mlx-c from source every time. OminiX's fork tracked MLX 0.32
|
|
17
|
+
and fell back to a pre-built MLX when no Metal compiler was present, which
|
|
18
|
+
is what let the gem install on a machine without the toolchain.
|
|
19
|
+
|
|
20
|
+
**2026-09-21 - upstream mlx-rs 0.32, with a system MLX.** The reasons for
|
|
21
|
+
the fork are gone. Upstream `mlx-rs 0.32.0` / `mlx-sys 0.6.0` are the same
|
|
22
|
+
generation as each other and pin mlx-c `c74db530` over **MLX 0.32.2**, and
|
|
23
|
+
`mlx-c` carries `MLX_C_USE_SYSTEM_MLX`, which finds an installed MLX
|
|
24
|
+
instead of fetching one. So the pre-built MLX is now handed over through
|
|
25
|
+
that option, driven from a generated CMake toolchain file, and the fork is
|
|
26
|
+
no longer needed. The cost is the same one the exit always named: it is a
|
|
27
|
+
number (the MLX/mlx-c pair), not a fork of `mlx-sys`.
|
|
28
|
+
|
|
29
|
+
**2026-09-22 - a patch on top, so that a Linux build can exist.** MLX's
|
|
30
|
+
CUDA backend is how a graph written on this Mac is trained on a rented
|
|
31
|
+
Linux box with an NVIDIA card, and `mlx-rs` could not be built for such a
|
|
32
|
+
machine at all: four separate failures, only two of them about CUDA. The
|
|
33
|
+
answer is a `[patch.crates-io]` onto a fork, which is a bridge and not a
|
|
34
|
+
destination - "The Linux patch, and when it goes" below says what has to
|
|
35
|
+
become true for it to be deleted.
|
|
36
|
+
|
|
37
|
+
**2026-09-23 - mlx-c bound here, and mlx-rs gone.** The patch
|
|
38
|
+
above got the build scripts to Linux, and the Rust underneath still did
|
|
39
|
+
not compile there: `mlx/c/half.h` declares the four half-precision
|
|
40
|
+
accessors only on ARM, and `mlx-rs` calls them unconditionally. Fixing
|
|
41
|
+
that in `mlx-rs` would make part of its public API exist on one platform
|
|
42
|
+
and not another, which is not a change to ask of somebody else's crate.
|
|
43
|
+
The engine uses two or three percent of `mlx-rs`, so it binds mlx-c
|
|
44
|
+
itself instead - "mlx-c, bound here" below. The patch went with `mlx-rs`:
|
|
45
|
+
its exit came by removal rather than by a release, and no fork is
|
|
46
|
+
carried any more.
|
|
47
|
+
|
|
48
|
+
The parts below headed "Which one is upstream", "Is the fork's mlx-rs the
|
|
49
|
+
same", "What Apple publishes" and "Which mlx-c, and which MLX under it"
|
|
50
|
+
are the investigation that led here, kept for its evidence.
|
|
51
|
+
|
|
52
|
+
## Which mlx-rs
|
|
53
|
+
|
|
54
|
+
There are three repositories with a claim to the name, and the manifests
|
|
55
|
+
do not make it obvious which one is built. In order, top to bottom:
|
|
56
|
+
|
|
57
|
+
| | what it is | where |
|
|
58
|
+
|---|---|---|
|
|
59
|
+
| **mlx-rs (upstream)** | the unofficial Rust bindings, by Minghua Wu and David Chavez, and what crates.io publishes. **What Torobi built from 2026-09-21 to 2026-09-23**, version `0.32.0`, whose `mlx-sys 0.6.0` pins mlx-c and MLX 0.32.2 | `github.com/oxiglade/mlx-rs`, formerly `github.com/oxideai/mlx-rs`. The old name redirects |
|
|
60
|
+
| **OminiX-MLX** | **the same repository, continued**, and what Torobi built from 2026-09-03 to 2026-09-21. Not a rewrite and not a vendored copy: the history is mlx-rs's own (534 commits, `init commit` at the bottom, 302 of them by upstream's main author), and on 2026-01-25 `753d289 refactor: Move original mlx-rs components into mlx-rs directory` moved it into a subdirectory to make room for model crates | `github.com/OminiX-ai/OminiX-MLX`, subtree `mlx-rs/` |
|
|
61
|
+
| **mlx-c** | Apple's C API for MLX, a git submodule of `mlx-sys`. bindgen reads its headers | `github.com/ml-explore/mlx-c` |
|
|
62
|
+
| **MLX** | the library itself. Not built here: a pre-built binary is downloaded at build time (below) | `github.com/ml-explore/mlx` |
|
|
63
|
+
|
|
64
|
+
## Which one is upstream, and why nothing says so
|
|
65
|
+
|
|
66
|
+
`oxiglade/mlx-rs` is. GitHub's API settles it rather than the pages do:
|
|
67
|
+
|
|
68
|
+
| | `fork` | created | stars |
|
|
69
|
+
|---|---|---|---|
|
|
70
|
+
| `oxiglade/mlx-rs` | **false**, no parent | 2023-12-23 | 371 |
|
|
71
|
+
| `OminiX-ai/OminiX-MLX` | **false**, no parent | **2026-01-26** | |
|
|
72
|
+
|
|
73
|
+
The first is the original: not a fork, created when mlx-rs began, holding
|
|
74
|
+
the stars and the docs, and published to crates.io by minghuaw and dcvz,
|
|
75
|
+
who are the authors of most of the history in both. `oxideai/mlx-rs`
|
|
76
|
+
redirects to it, so that was a rename and not a move to a fork.
|
|
77
|
+
|
|
78
|
+
The second is not a GitHub fork of anything. It was created on 2026-01-26,
|
|
79
|
+
**the day after** `753d289 Move original mlx-rs components into mlx-rs
|
|
80
|
+
directory` in its own history: a restructured clone, pushed as a new
|
|
81
|
+
repository rather than forked.
|
|
82
|
+
|
|
83
|
+
**That is why this is confusing, and the reason is worth stating.** The
|
|
84
|
+
descent is real in git and invisible to GitHub: no parent link, no fork
|
|
85
|
+
banner, and crate metadata that names a third URL. Nothing you can click
|
|
86
|
+
on tells you what was compiled. Only the git history and `Cargo.lock` do.
|
|
87
|
+
|
|
88
|
+
## Is the fork's mlx-rs the same as the one on crates.io
|
|
89
|
+
|
|
90
|
+
Same origin, still converging, not the same code.
|
|
91
|
+
|
|
92
|
+
**Still converging**: upstream's work keeps arriving. The history carries
|
|
93
|
+
upstream pull requests (#289, #305, #311, #313, #314, #321, #323) and a
|
|
94
|
+
commit that says how they get there, `b6c36f3 fix: remove duplicate
|
|
95
|
+
gather_mm and Float64 pattern after upstream rebase`. The Rust API is
|
|
96
|
+
upstream's, which is what makes the exit below realistic.
|
|
97
|
+
|
|
98
|
+
**Not the same code**: twelve commits by OminiX-era authors touch
|
|
99
|
+
`mlx-rs/` and upstream does not have them. Sorted by what they are, and
|
|
100
|
+
by whether Torobi is standing on them:
|
|
101
|
+
|
|
102
|
+
| | commits | reaches Torobi |
|
|
103
|
+
|---|---|---|
|
|
104
|
+
| build machinery | `100f155` auto-download pre-built MLX when Xcode is unavailable, `b024efa` deployment-target override | **`100f155` yes, and it is the whole reason this fork is here.** Without it `gem install` needs Xcode |
|
|
105
|
+
| the library | `e53aa1b` contiguity check in `try_as_slice` / `contiguous` (a breaking change: a non-contiguous array is an error now rather than the wrong bytes), `c13ee0e` Float64 in safetensors, `284916e` IO extensions, `d145d5b` requires MLX 0.32.0 | `e53aa1b` is in a function the engine calls (`tensor.rs` copies through `contiguous()`), but the engine calls it *to make* the array contiguous, so it stands on its own rather than on the check. The other three it does not use |
|
|
106
|
+
| structure | `753d289` the move into a subdirectory, `e5aed65` the version renumbering, `b6c36f3` cleanup after an upstream rebase | no |
|
|
107
|
+
| fixes elsewhere | `6d11748` RoPE reshape that broke multi-head attention, `d8495fd` and `e4beb9b` async pipelining | no: the engine implements RoPE itself (`interp.rs`, host-built angles and a rotation), so `nn::Rope` is never called |
|
|
108
|
+
|
|
109
|
+
So the answer to "could we build against upstream instead" is: the Rust
|
|
110
|
+
side, yes, checked function by function; the build without Xcode, no.
|
|
111
|
+
That is the exit below, and this is the evidence for how narrow it is.
|
|
112
|
+
|
|
113
|
+
**The version numbers do not compare.** The fork renumbered to 1.0.0 in
|
|
114
|
+
`e5aed65 feat: v1.0.0 - version alignment with OminiX-API` and is 1.2.0
|
|
115
|
+
now; crates.io's newest is 0.25.3, and upstream's own README still tells
|
|
116
|
+
you to install 0.21.0. Three numbers, no relation between any two of
|
|
117
|
+
them. `Cargo.lock`'s commit is the only version that means anything
|
|
118
|
+
here.
|
|
119
|
+
|
|
120
|
+
**The crate metadata points at the wrong one.** OminiX's workspace still
|
|
121
|
+
carries upstream's `repository = "https://github.com/oxideai/mlx-rs"`, so
|
|
122
|
+
`cargo tree`, docs.rs links and anything reading crate metadata lead to
|
|
123
|
+
upstream rather than to what was compiled. Read `engine/Cargo.toml` and
|
|
124
|
+
`Cargo.lock` for the truth; they name the fork and its commit.
|
|
125
|
+
|
|
126
|
+
## What Apple publishes, and why it is not a drop-in
|
|
127
|
+
|
|
128
|
+
`mlx-metal` on PyPI is official (`mlx@group.apple.com`, owners awni /
|
|
129
|
+
katharas / mlx-dev), versioned per MLX release, one wheel per macOS
|
|
130
|
+
version, **with a SHA-256 for every file**. At 0.32.2 a wheel holds:
|
|
131
|
+
|
|
132
|
+
| | |
|
|
133
|
+
|---|---|
|
|
134
|
+
| `mlx/lib/mlx.metallib` | 129.55 MB, the compiled Metal kernels |
|
|
135
|
+
| `mlx/lib/libmlx.dylib` | 20.86 MB, **dynamic** |
|
|
136
|
+
| `mlx/lib/libjaccl.dylib` | 1.48 MB |
|
|
137
|
+
| `mlx/include/**` | MLX's headers, 405 entries |
|
|
138
|
+
|
|
139
|
+
What `mlx-sys` wants from `MLX_PREBUILT_PATH` is `libmlx.a`, `libmlxc.a`,
|
|
140
|
+
`libgguflib.a` and `mlx.metallib`: **static** libraries, and `libmlxc.a`
|
|
141
|
+
is mlx-c, a separate project that is in no MLX wheel. So the official
|
|
142
|
+
artifact is not a substitute for OminiX's tarball as things stand.
|
|
143
|
+
|
|
144
|
+
What it *is* good for is the expensive half: the metallib needs the Metal
|
|
145
|
+
compiler and is 130 MB, and Apple publishes it, versioned and hashed.
|
|
146
|
+
|
|
147
|
+
**But a prebuilt metallib does not buy a build without the toolchain**,
|
|
148
|
+
which was worth checking before planning around it. MLX asks for the
|
|
149
|
+
Metal compiler twice, and `MLX_METAL_PATH` is not a way past either:
|
|
150
|
+
|
|
151
|
+
```cmake
|
|
152
|
+
# CMakeLists.txt, when MLX_BUILD_METAL=ON on Darwin: fatal at configure
|
|
153
|
+
execute_process(COMMAND zsh -c "echo __METAL_VERSION__ | xcrun -sdk macosx metal ..."
|
|
154
|
+
COMMAND_ERROR_IS_FATAL ANY)
|
|
155
|
+
|
|
156
|
+
# mlx/backend/metal/kernels/CMakeLists.txt: every .metal compiled and linked
|
|
157
|
+
add_custom_command(OUTPUT ${MLX_METAL_PATH}/mlx.metallib
|
|
158
|
+
COMMAND xcrun -sdk macosx metal ...)
|
|
159
|
+
add_dependencies(mlx mlx-metallib) # the mlx target depends on it
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
`MLX_METAL_PATH` moves where the metallib is written and looked for; it
|
|
163
|
+
does not skip building it, and `add_subdirectory(kernels)` is
|
|
164
|
+
unconditional. So anyone building MLX with Metal needs the toolchain
|
|
165
|
+
(`xcodebuild -downloadComponent MetalToolchain`), which is a thing a
|
|
166
|
+
GitHub macOS runner can install and this machine cannot.
|
|
167
|
+
|
|
168
|
+
## Which one to point at
|
|
169
|
+
|
|
170
|
+
Three different questions, three different answers, and they are not in
|
|
171
|
+
conflict:
|
|
172
|
+
|
|
173
|
+
| asking | look at |
|
|
174
|
+
|---|---|
|
|
175
|
+
| what does this API do | **upstream**, `oxiglade.github.io/mlx-rs`. The API was always upstream's, and OminiX published no documentation of its own for it |
|
|
176
|
+
| what is actually compiled | **the pinned version**, through `engine/Cargo.toml` and `Cargo.lock` |
|
|
177
|
+
| what may be redistributed, and on whose terms | **upstream mlx-rs**, which is dual-licensed MIT or Apache-2.0 |
|
|
178
|
+
|
|
179
|
+
The dependency is the crates.io crate now. It installs without Xcode
|
|
180
|
+
because the MLX it is built against is provided as a system package
|
|
181
|
+
(below), not because a fork reaches for one.
|
|
182
|
+
|
|
183
|
+
## The Linux patch, and how it went
|
|
184
|
+
|
|
185
|
+
**Gone on 2026-09-23**, with `mlx-rs` itself: the engine binds mlx-c
|
|
186
|
+
(below), so there is no crate left to patch, and `[patch.crates-io]` came
|
|
187
|
+
out of the workspace manifest. The exit written here was "a published
|
|
188
|
+
release carries the four fixes"; what happened instead was that a fifth
|
|
189
|
+
blocker turned up underneath them (`mlx/c/half.h`, "mlx-c, bound here")
|
|
190
|
+
which `mlx-rs` could only fix by changing its public API. The section is
|
|
191
|
+
kept as the record of what stopped a Linux build and why.
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
The crates on crates.io cannot be built for anything that is not Apple's.
|
|
195
|
+
This matters because MLX itself can: `ml-explore/mlx` carries a CUDA
|
|
196
|
+
backend, builds and tests it on every pull request against CUDA 12.6,
|
|
197
|
+
12.9 and 13.0, and publishes `mlx-cuda-12` / `mlx-cuda-13` to PyPI in step
|
|
198
|
+
with the Metal wheels. The backend is there; the bindings do not reach it.
|
|
199
|
+
|
|
200
|
+
Four things stop them, and it is worth saying that only two are about
|
|
201
|
+
CUDA. The other two would stop a CPU-only Linux build just as dead:
|
|
202
|
+
|
|
203
|
+
| | where | what |
|
|
204
|
+
|---|---|---|
|
|
205
|
+
| 1 | `mlx-sys/build.rs` | `dylib=objc` and `framework=Foundation` are linked for every target, cmake is handed `/usr/bin/cc` for the macOS SDK, and `clang_rt.osx` is looked up through `xcrun` |
|
|
206
|
+
| 2 | `mlx-sys` | no way to ask for the CUDA backend: `MLX_BUILD_CUDA` is never set, and because MLX is linked as a **static** archive its CUDA dependencies (cuBLASLt, cuFFT, NVRTC, the driver API, cuDNN) arrive at the final link unresolved and must be named there too |
|
|
207
|
+
| 3 | `mlx-rs/src/random.rs` | `RandomState::new` seeds itself from `mach_approximate_time`, Mach's clock, reached from a `thread_local!`. Any use of `mlx_rs::random` therefore fails to link off Apple - and the engine uses it in ten places (`init`, dropout in `interp`, `executor`, `plan`, `state`). The patch does not port the clock, it drops it: the value only ever reaches `key()`, so `std::hash::RandomState` answers the same question with no dependency and no `cfg`, and is the better seed besides - a clock is shared by two runs started close enough together, which on one training box is a thing that happens |
|
|
208
|
+
| 4 | `mlx-rs/Cargo.toml` | `mlx-sys` is taken with its defaults on, so `default-features = false` on `mlx-rs` turns off this crate's `metal` and leaves the sys crate linking Metal regardless. No feature combination produces a build that is not Apple's |
|
|
209
|
+
|
|
210
|
+
**The exit condition, which is the point of writing this down.** The
|
|
211
|
+
patch is deleted - not edited, deleted - when a published `mlx-sys` and
|
|
212
|
+
`mlx-rs` carry these four. Concretely: when a crates.io release builds for
|
|
213
|
+
`x86_64-unknown-linux-gnu` and offers a `cuda` feature, `[patch.crates-io]`
|
|
214
|
+
comes out of the workspace manifest and `engine/Cargo.toml` goes back to
|
|
215
|
+
naming versions alone. Nothing else in the tree depends on the fork, which
|
|
216
|
+
is what keeps that a deletion rather than a migration.
|
|
217
|
+
|
|
218
|
+
Until it went, the fork was `takahashim/mlx-rs`, branch `linux-cuda`,
|
|
219
|
+
pinned by revision rather than by branch: a branch is a name that moves,
|
|
220
|
+
and what is compiled here should be a thing that does not. The four commits are one
|
|
221
|
+
per row of the table above, and each leaves macOS behaviour alone - which
|
|
222
|
+
is checkable, and was checked, by building and testing this project
|
|
223
|
+
against the patch on a Mac before anything else was done with it.
|
|
224
|
+
|
|
225
|
+
This is the second time a fork has been carried here, and the first one
|
|
226
|
+
(OminiX-MLX, above) is the reason the exit is written before the entrance.
|
|
227
|
+
|
|
228
|
+
## mlx-c, bound here
|
|
229
|
+
|
|
230
|
+
The engine reaches MLX through `engine/src/mlxc/`, which binds mlx-c
|
|
231
|
+
directly: `engine/build.rs` runs bindgen over the headers in the prefix
|
|
232
|
+
`TOROBI_MLX_PREFIX` names, and links the three static archives beside
|
|
233
|
+
them. Nothing is compiled, and no mlx-c source is fetched. The headers
|
|
234
|
+
and the archives come out of the same prefix, so what is declared is what
|
|
235
|
+
is linked (notes/plan-mlxc-migration.md has the plan and its progress).
|
|
236
|
+
|
|
237
|
+
**Why here rather than through `mlx-rs`.** `mlx/c/half.h` defines
|
|
238
|
+
`float16_t` and `bfloat16_t` only when compiling for ARM, and `array.h`
|
|
239
|
+
declares `mlx_array_item_float16`, `mlx_array_item_bfloat16`,
|
|
240
|
+
`mlx_array_data_float16` and `mlx_array_data_bfloat16` under the same
|
|
241
|
+
condition, so on x86_64 they do not exist. `mlx-rs` uses them without a
|
|
242
|
+
condition (its `ArrayElement` for `f16` and `bf16`), so it cannot compile
|
|
243
|
+
there. What they do is read half precision into a host variable; nothing
|
|
244
|
+
about computing in half precision is missing, and the engine converts to
|
|
245
|
+
f32 on the device before reading anything. bindgen is told to leave those
|
|
246
|
+
two types and four functions out, and what remains is the same on both
|
|
247
|
+
platforms.
|
|
248
|
+
|
|
249
|
+
**Why not simply fix `mlx-rs`.** The fix changes its public API per
|
|
250
|
+
platform, which is a design decision for its maintainers rather than a
|
|
251
|
+
patch to send them. And the engine does not need most of it: models
|
|
252
|
+
arrive from Ruby as a graph the engine interprets, so layers, optimizers
|
|
253
|
+
and parameter management are the engine's own.
|
|
254
|
+
|
|
255
|
+
**The shape is `mlx-rs`'s.** The same names, argument order and module
|
|
256
|
+
paths, so that moving the engine is a change of `use` lines. `mlx-rs` is
|
|
257
|
+
the reference for which mlx-c variant each method means. The reference
|
|
258
|
+
is **`mlx-rs 0.32.0` from crates.io, source commit `9c2fd72e`**
|
|
259
|
+
(`oxiglade/mlx-rs`, the `.cargo_vcs_info.json` of the published crate);
|
|
260
|
+
the patched build this project compiled at the time differs from it only
|
|
261
|
+
in build scripts, manifests and the RNG's seed clock, none of which is copied. It
|
|
262
|
+
is MIT OR Apache-2.0, so reading it and taking from it are both fine;
|
|
263
|
+
what the shim takes beyond the API's shape is small (how an exception
|
|
264
|
+
prints, the wording of the safetensors path refusals), and "Licences"
|
|
265
|
+
below says what that asks of a redistributor.
|
|
266
|
+
|
|
267
|
+
While both were linked, a test called the shim and `mlx-rs` on the same
|
|
268
|
+
inputs and compared the results to the bit: the free functions, gelu in
|
|
269
|
+
f32 and bf16, attention under each kind of mask and its gradient, and
|
|
270
|
+
safetensors written by one and read by the other. It went with `mlx-rs`.
|
|
271
|
+
What outlives it is the draws from seed 42, pinned as constants recorded
|
|
272
|
+
through `mlx-rs` (exact on Metal, where they were recorded, and within f32
|
|
273
|
+
rounding on CUDA, whose sampler is the same algorithm in another
|
|
274
|
+
arithmetic), and a before-and-after comparison on ruri-v3-130m (the
|
|
275
|
+
same SHA-256 for a forward, three losses and the weights they leave)
|
|
276
|
+
recorded in docs/plan.md section 15.75.
|
|
277
|
+
|
|
278
|
+
**Where the shim deliberately differs**, each for a stated reason:
|
|
279
|
+
|
|
280
|
+
| what | `mlx-rs 0.32.0` | here |
|
|
281
|
+
|---|---|---|
|
|
282
|
+
| error handler | installed lazily on first use | installed once, by the runtime when it is created and wherever a handle that can fail is made (`engine/src/mlxc/error.rs` says where); tested in a child process, so an exit would fail the test rather than end the run |
|
|
283
|
+
| a constructor mlx-c could not serve | an `Array` holding an empty handle, which fails later in MLX's words | an error at the constructor, in MLX's words: constructors return `Result`, and an `Array` always holds an array |
|
|
284
|
+
| data that does not fill the shape | a panic | an error |
|
|
285
|
+
| reading a value (`item`, `as_slice`) | panics on failure (`item_cast`, `as_slice`) | returns `Result`; an empty array's slice is empty rather than a panic |
|
|
286
|
+
| a closure panicking inside `value_and_grad` | caught, then resumed after the call | the same |
|
|
287
|
+
| the closure's output vector | written into mlx-c's | the same, with mlx-c's own empty vector filled rather than overwritten |
|
|
288
|
+
|
|
289
|
+
## mlx-c, call by call
|
|
290
|
+
|
|
291
|
+
What each method the engine calls maps to. **Ownership** is the same for
|
|
292
|
+
every row and so is said once: inputs are borrowed (mlx-c takes a new
|
|
293
|
+
reference where it keeps one), and the
|
|
294
|
+
output is written into an `mlx_array` the shim owns before the call, so a
|
|
295
|
+
failing call frees it. Every op runs on `Stream::current`, the default
|
|
296
|
+
device's default stream, which is what `mlx-rs` used when no thread-local
|
|
297
|
+
stream was set.
|
|
298
|
+
|
|
299
|
+
| method | mlx-c | optional and edge arguments |
|
|
300
|
+
|---|---|---|
|
|
301
|
+
| `abs` `negative` `sqrt` `rsqrt` `square` `exp` `log` `cos` `sin` | `mlx_<same name>` | none |
|
|
302
|
+
| `add` `subtract` `multiply` `divide` `matmul` | `mlx_<same name>` | the other operand is `impl AsRef<Array>`; broadcasting is MLX's |
|
|
303
|
+
| `sum` `mean` `max` `min` (`keep_dims`) | `mlx_sum` `mlx_mean` `mlx_max` `mlx_min`: over every axis | `keep_dims: None` is `false` |
|
|
304
|
+
| `sum_axes` `mean_axes` `max_axes` (`axes`, `keep_dims`) | `mlx_sum_axes` `mlx_mean_axes` `mlx_max_axes` | negative axes count from the end; an **empty list reduces nothing** (MLX's reading, and `mlx-rs` passes it through); `keep_dims: None` is `false` |
|
|
305
|
+
| `reshape` | `mlx_reshape` | one `-1` is inferred |
|
|
306
|
+
| `transpose_axes` `expand_dims_axes` `squeeze_axes` | `mlx_transpose_axes` `mlx_expand_dims_axes` `mlx_squeeze_axes` | negative axes count from the end |
|
|
307
|
+
| `take_axis` | `mlx_take_axis` | negative axis counts from the end |
|
|
308
|
+
| `contiguous` | `mlx_contiguous` with `allow_col_major = false` | `mlx-rs`'s default |
|
|
309
|
+
| `as_dtype` | `mlx_astype` | |
|
|
310
|
+
| `zeros::<T>` `ones::<T>` | `mlx_zeros` `mlx_ones` with `T`'s dtype | |
|
|
311
|
+
| `from_slice` | `mlx_array_new_data` (copies) | refused when the data does not fill the shape, before mlx-c reads it |
|
|
312
|
+
| `from_f32` `from_i32` | `mlx_array_new_float32` `mlx_array_new_int` | a constructor that returns an empty handle is an error, with what MLX said |
|
|
313
|
+
| `clone` | `mlx_array_set` into a new handle | another reference, not a copy of the data |
|
|
314
|
+
| `item::<T>` (mlx-rs's `item_cast`) | `mlx_array_eval`, then `mlx_astype` when the dtype differs, then `mlx_array_item_<T>` | an error on more than one value |
|
|
315
|
+
| `as_slice::<T>` | `mlx_array_eval`, `_mlx_array_is_row_contiguous`, `mlx_array_data_<T>` | an error on another dtype or a non-row-major layout; `T` is `f32`, `i32`, `u32` or `bool` |
|
|
316
|
+
| `shape` `ndim` `size` `dtype` | `mlx_array_shape` `mlx_array_ndim` `mlx_array_size` `mlx_array_dtype` | a scalar's shape is empty |
|
|
317
|
+
| `ops::tanh` `ops::sigmoid` `ops::erf` `stop_gradient` | `mlx_tanh` `mlx_sigmoid` `mlx_erf` `mlx_stop_gradient` | none |
|
|
318
|
+
| `ops::maximum` | `mlx_maximum` | |
|
|
319
|
+
| `ops::zeros_like` | `mlx_zeros` with the input's shape and dtype, **not** `mlx_zeros_like` | as `mlx-rs`: the input is read for its shape and does not enter the graph |
|
|
320
|
+
| `ops::softmax_axis` | `mlx_softmax_axis` | `precise: None` is `false` |
|
|
321
|
+
| `ops::logsumexp_axes` | `mlx_logsumexp_axes` | `keep_dims: None` is `false` |
|
|
322
|
+
| `ops::concatenate` `ops::stack` | `mlx_concatenate_axis` `mlx_stack_axis` | the arrays go in an `mlx_vector_array` the shim owns and frees |
|
|
323
|
+
| `ops::indexing::take_along_axis` | `mlx_take_along_axis` | `axis: None` flattens first (`mlx_reshape` to `[-1]`, axis 0), as `mlx-rs` does |
|
|
324
|
+
| `power` | `mlx_power` | used by `gelu_approximate` |
|
|
325
|
+
| `nn::gelu_approximate` | `0.5·x·(1 + tanh(√(2/π)·(x + 0.044715·x³)))`, op for op as `mlx-rs` writes it: f32 constants, an i32 exponent | **not compiled**, where `mlx-rs` wraps it in `compile`; equal to the bit in f32 and bf16 on the parity test's inputs |
|
|
326
|
+
| `fast::scaled_dot_product_attention` | `mlx_fast_scaled_dot_product_attention` with `force_fused = false` | no mask and an array mask are mode `""`, causal is `"causal"` with an empty array; no sinks is an empty array. The empty arrays are owned and freed, where `mlx-rs` leaked them |
|
|
327
|
+
| `quantized::quantize` | `mlx_quantize` | `group_size` and `bits` as `mlx_optional_int` with `has_value`; `mode` a C string (`"affine"` only); `global_scale` an empty array (it is `nvfp4`'s). Returns an `mlx_vector_array` the shim owns: three arrays for affine (weight, scales, biases), two for the block floating-point modes, read from the back. Not in `mlx-rs` |
|
|
328
|
+
| `quantized::dequantize` | `mlx_dequantize` | the same optionals; no biases is an empty array; `dtype: None` is `mlx_optional_dtype` without a value, which MLX reads as the scales' dtype |
|
|
329
|
+
| `quantized::quantized_matmul` | `mlx_quantized_matmul` | the same optionals; `transpose` is always `true` where the engine calls it (a linear's `[d_out, d_in]`). MLX's VJP is defined for `x`, the scales and the biases, and refuses the packed weight (pinned in `quantized.rs`'s tests) |
|
|
330
|
+
| `random::key` | `mlx_random_key` | |
|
|
331
|
+
| `random::split(key, n)` | `mlx_random_split_num`, then `take_axis` of index 0 and 1 on axis 0 | returns the first two of `n`, as `mlx-rs` does |
|
|
332
|
+
| `random::normal::<T>` `random::uniform::<T>` `random::bernoulli` | `mlx_random_normal` `mlx_random_uniform` `mlx_random_bernoulli` | **the key is required by type** (`&Array`), where `mlx-rs` took an `Option` and drew from a clock-seeded global without one; `loc`, `scale` and the bounds are plain `f32`, and `p` is an array. Seed 42's draws are the ones `mlx-rs` drew, pinned in a test |
|
|
333
|
+
| `transforms::eval` | `mlx_eval` over one vector | |
|
|
334
|
+
| `transforms::value_and_grad_with_argnums` | `mlx_closure_new_func_payload`, `mlx_value_and_grad`, `mlx_closure_value_and_grad_apply` | the closure's own error wins over MLX's "non-zero value"; a panic resumes on the Rust side |
|
|
335
|
+
| `Array::load_safetensors` | `mlx_load_safetensors` on the CPU stream, then `mlx_map_string_to_array_iterator_*` | refuses a missing file or another extension before MLX sees it, in `mlx-rs`'s words; the header's metadata is read and dropped |
|
|
336
|
+
| `Array::save_safetensors` | `mlx_map_string_to_array_insert`, `mlx_map_string_to_string_insert`, `mlx_save_safetensors` | the maps are owned and freed on every path; a name with a NUL is an error |
|
|
337
|
+
| `memory::active` `cache` `peak` `limit` `clear_cache` `reset_peak` `set_limit` | `mlx_get_active_memory` and its siblings | not in `mlx-rs`: the engine called `mlx-sys` for these, and now goes through the shim's handler |
|
|
338
|
+
|
|
339
|
+
## The ledger
|
|
340
|
+
|
|
341
|
+
| what | state |
|
|
342
|
+
|---|---|
|
|
343
|
+
| mlx-rs / mlx-sys | **not dependencies.** The engine binds mlx-c itself ("mlx-c, bound here"). `mlx-rs 0.32.0` (source commit `9c2fd72e`) is the reference the binding was written and checked against, and nothing of it is compiled. `Cargo.lock` holds no `mlx` crate |
|
|
344
|
+
| mlx-c | **built into the pre-built archive** at commit `c74db5307cc8`, and bound by `engine/build.rs`: bindgen reads the headers in the prefix and the build links `libmlxc.a` from the same prefix, so what is declared is what is linked. Nothing compiles or fetches mlx-c here |
|
|
345
|
+
| MLX core | **not built from source here**: there is no Metal compiler on this machine, so a pre-built archive is used instead. `takahashim/mlx-prebuilt`, built from stated inputs on a runner with the toolchain rather than taken from a third party's release. It must carry the generation the binding was written against - **MLX 0.32.2 over mlx-c `c74db5307cc8`** - and `mlx_prebuilt.json`'s `requires` is what holds it to that. It names the mlx-c **commit**, not `v0.6.0`: the tag pins MLX 0.31.1 and seven later commits pin 0.32.2, and those commits change headers under `mlx/c/`, so a build from the tag is the wrong generation even though it says 0.6.0. The pin names **`takahashim/mlx-prebuilt` `v0.6.0.2`** (MLX 0.32.2 / mlx-c `c74db530`), which is what a build fetches: `macos-arm64` (digest `290331d6…`) and `linux-x86_64`, the `linux-x86_64-cuda12` archive (CUDA 12.9, sm 80/86/89/90a, digest `060f2757…`). The release also carries the Metal kernels as an asset of their own, `<stem>.metallib.tar.gz` (digest `20d3b7e1…`), which a platform gem fetches on first use. `rake mlx:pin` checks the bytes of the archive it records, and CI's `linux` job repeats the fetch, verify, bind and link for the Linux one |
|
|
346
|
+
| mlx.metallib | 129 MB. MLX locates it through `dladdr`, i.e. **beside whichever library holds the MLX symbols**: the installed bundle, or `lib/torobi/` for a checkout. It comes from the prefix at `<prefix>/lib/mlx.metallib`; `ext/torobi/extconf.rb` installs it beside the bundle, `rake metallib` copies it into the checkout, and `engine/build.rs` links it beside the engine's own binaries. A source install keeps that: it is beside the bundle and MLX finds it. The macOS platform gem cannot ship it and may not be able to write beside the installed bundle, so on first use it is fetched into `~/.cache/torobi/` and MLX is told where it is (`mlx_metal_set_metallib_path`, mlx-swift issue #415), rather than into the gem's own directory |
|
|
347
|
+
| Linux | the same binding, the same bindgen blocklist, and a link against the CUDA toolkit (cudart, cuBLASLt, cuFFT, NVRTC, the driver), cuDNN and **OpenBLAS**, which MLX's CPU backend calls for BLAS and LAPACK and which its own CMake package names. Checked in an amd64 container with CUDA 12.9 and cuDNN 9 against the archive mlx-prebuilt's CI built: 650 functions bound, none of them half-precision, and the engine's test binary and command line link and start. A machine that runs it needs `libopenblas0` as well as the driver. **At run time MLX compiles the CUDA kernels it did not build ahead of time through NVRTC, which reads NVIDIA's headers (`include/cccl`, `include/cute`, `include/cutlass`) from one directory above the extension; the Linux platform gem ships them in `lib/include`, and a checkout gets them from the prefix; the engine refuses before a step when they are not there.** The `linux` job in CI fetches and checks the pinned archive and repeats the bind and link on a GitHub runner, with no GPU; it first passed on 2026-09-23 |
|
|
348
|
+
| the prefix, how it is handed over | one variable, `TOROBI_MLX_PREFIX`, which `rake` and `extconf.rb` set to the checked prefix and `engine/build.rs` reads for headers, archives, `MANIFEST.txt` and the metallib. A caller who sets it themselves is left alone |
|
|
349
|
+
|
|
350
|
+
## Licences
|
|
351
|
+
|
|
352
|
+
| | licence | holder |
|
|
353
|
+
|---|---|---|
|
|
354
|
+
| Torobi | MIT | this project |
|
|
355
|
+
| mlx-rs (upstream, oxiglade): the reference the binding follows, not a dependency | MIT **or** Apache-2.0, at the user's choice | its authors |
|
|
356
|
+
| mlx-c | MIT | ml-explore |
|
|
357
|
+
| MLX | MIT | ml-explore |
|
|
358
|
+
| gguflib (the GGUF library MLX vendors) | MIT | Georgi Gerganov (antirez/gguf-tools) |
|
|
359
|
+
| CCCL (the CUDA headers MLX JITs against on Linux) | Apache-2.0 with LLVM exception | NVIDIA |
|
|
360
|
+
| CUTLASS (the same) | BSD-3-Clause | NVIDIA |
|
|
361
|
+
|
|
362
|
+
(OminiX-MLX, used from 2026-09-03 to 2026-09-21, was under the same
|
|
363
|
+
MIT-or-Apache-2.0 terms; nothing of it is in the current build.)
|
|
364
|
+
|
|
365
|
+
Everything in the chain is permissive, and MIT and Apache-2.0 both ask the
|
|
366
|
+
same thing of a redistributor: carry the notice.
|
|
367
|
+
|
|
368
|
+
**A source gem carries none of it, and does not have to.** What it holds
|
|
369
|
+
is `spec.files`: Ruby, the engine's own Rust, two manifests and the docs.
|
|
370
|
+
No line of MLX or mlx-c is in it, and `mlx-rs` is neither in it nor
|
|
371
|
+
fetched: the binding follows its API, and the few small pieces taken from
|
|
372
|
+
it (how an exception prints, two refusal messages) are in the engine's
|
|
373
|
+
source under its own MIT notice. `lib/torobi/mlx_prebuilt.rb` fetches our
|
|
374
|
+
own pre-built MLX, mlx-c included, from `takahashim/mlx-prebuilt`; it
|
|
375
|
+
arrives at the user's machine from its own authors, under their own
|
|
376
|
+
licences. Torobi points, it does not ship.
|
|
377
|
+
|
|
378
|
+
**The platform gem does ship it, since 2026-09-24** (docs/plan.md
|
|
379
|
+
section 11.4). Nothing is built at install, so the package contains
|
|
380
|
+
mlx-c's compiled code and its dependencies' (MLX, gguflib), and Torobi is
|
|
381
|
+
a redistributor. The notices travel with it, in `licenses/` - MLX, mlx-c
|
|
382
|
+
and gguflib for the code it links, mlx-rs for the binding - and the README
|
|
383
|
+
says what is inside the binary and under what terms. The prebuilt ships
|
|
384
|
+
`LICENSE.mlx`, `LICENSE.mlx-c` and `LICENSE.gguflib`, which are what
|
|
385
|
+
`licenses/` holds; the mlx-rs notice names the crate's authors, since its
|
|
386
|
+
`LICENSE-MIT` carries the text without a copyright line.
|
|
387
|
+
|
|
388
|
+
The `mlx.metallib` (129 MB) is not bundled: it is fetched on first use
|
|
389
|
+
beside the installed bundle, so the package stays a few megabytes. What
|
|
390
|
+
the redistribution does not cost is a new obligation: the prebuilt
|
|
391
|
+
(`takahashim/mlx-prebuilt`) already redistributes MLX's compiled code.
|
|
392
|
+
|
|
393
|
+
Not hard, and not discovered afterwards: it is written here so that the
|
|
394
|
+
decision was made with it in view.
|
|
395
|
+
|
|
396
|
+
## Why a pinned dependency rather than a vendored copy
|
|
397
|
+
|
|
398
|
+
The plan (docs/plan.md section 5) first imagined selective vendoring:
|
|
399
|
+
copying array / ops / fast / transforms / io into this tree and pruning the
|
|
400
|
+
rest. A pinned dependency does the same job for less:
|
|
401
|
+
|
|
402
|
+
- it costs no source tree of ours to carry or to re-sync
|
|
403
|
+
- cargo records the exact version and checksum in Cargo.lock, so the pin is
|
|
404
|
+
enforced rather than described
|
|
405
|
+
- it builds anywhere, which is the whole point (below)
|
|
406
|
+
|
|
407
|
+
What it gives up is local pruning and local patching. If a patch becomes
|
|
408
|
+
necessary, the answer is a fork with its own pin, not a vendored copy.
|
|
409
|
+
|
|
410
|
+
This was a pinned **git dependency on OminiX's fork** until 2026-09-21,
|
|
411
|
+
because that fork tracked MLX 0.32 and reached for a pre-built MLX, and
|
|
412
|
+
then the crates.io crate until 2026-09-23. Since then there is no Rust
|
|
413
|
+
dependency on MLX to pin at all: the binding is the engine's own source,
|
|
414
|
+
and what is pinned is the pre-built archive (`mlx_prebuilt.json`).
|
|
415
|
+
|
|
416
|
+
## How MLX is provided, and what OminiX did
|
|
417
|
+
|
|
418
|
+
### Now: the prefix, read by `engine/build.rs`
|
|
419
|
+
|
|
420
|
+
The pre-built archive is an MLX install prefix with mlx-c already built
|
|
421
|
+
into it: headers under `include/`, `libmlx.a`, `libmlxc.a` and
|
|
422
|
+
`libgguflib.a` under `lib/`, and `MANIFEST.txt` beside it naming the MLX
|
|
423
|
+
release and the mlx-c commit. `engine/build.rs` runs bindgen over those
|
|
424
|
+
headers, links those archives, reads the manifest into `build_info`, and
|
|
425
|
+
on Apple links `lib/mlx.metallib` beside the test binaries. Nothing runs
|
|
426
|
+
cmake, and the one thing a build is told is where the prefix is
|
|
427
|
+
(`TOROBI_MLX_PREFIX`).
|
|
428
|
+
|
|
429
|
+
### 2026-09-21 to 2026-09-23: a system MLX, found through a toolchain file
|
|
430
|
+
|
|
431
|
+
Upstream `mlx-sys 0.6.0` always runs cmake on its vendored mlx-c. It has
|
|
432
|
+
no branch for "use this MLX already on the machine", but mlx-c does:
|
|
433
|
+
`MLX_C_USE_SYSTEM_MLX=ON` turns its `FetchContent(mlx)` into
|
|
434
|
+
`find_package(MLX REQUIRED)`, and MLX installs a real CMake package, so
|
|
435
|
+
nothing of MLX is fetched or compiled. The `cmake` crate reads
|
|
436
|
+
`CMAKE_TOOLCHAIN_FILE` from the environment, which is how those two
|
|
437
|
+
variables reach that cmake run without touching `mlx-sys`:
|
|
438
|
+
|
|
439
|
+
```cmake
|
|
440
|
+
set(MLX_C_USE_SYSTEM_MLX ON CACHE BOOL "" FORCE)
|
|
441
|
+
set(CMAKE_PREFIX_PATH "<prefix>" CACHE STRING "" FORCE)
|
|
442
|
+
set(MLX_C_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
|
|
443
|
+
```
|
|
444
|
+
|
|
445
|
+
The third is not optional. `mlx-c` builds its examples by default, and
|
|
446
|
+
`example-gguf` / `example-safe-tensors` link MLX's vendored `gguflib`,
|
|
447
|
+
which exists in the build tree only when MLX is built there too; with a
|
|
448
|
+
system MLX they fail to link (`_gguf_*` undefined) and stop the build.
|
|
449
|
+
The library does not need them.
|
|
450
|
+
|
|
451
|
+
`lib/torobi/mlx_prebuilt.rb` writes that file (into the cache, so no
|
|
452
|
+
machine-specific path is committed) and fetches and checks the prefix that
|
|
453
|
+
it names. `extconf.rb` and the Rakefile set `CMAKE_TOOLCHAIN_FILE` for
|
|
454
|
+
cargo. Two further variables come from the same place: `MLX_RS_METAL_PATH`
|
|
455
|
+
tells `mlx-sys` where the metallib is (the prefix's `lib/`, so it is found
|
|
456
|
+
rather than warned about), and `RUSTFLAGS=-L native=<prefix>/lib` adds the
|
|
457
|
+
one link path upstream does not emit when MLX is a system package, since
|
|
458
|
+
`mlx-sys` looks for `libmlx.a` / `libgguflib.a` in its own build tree.
|
|
459
|
+
|
|
460
|
+
### Then: OminiX's `build.rs`
|
|
461
|
+
|
|
462
|
+
Kept because it is what the exit above was measured against.
|
|
463
|
+
`mlx-sys/build.rs` decided in three ways:
|
|
464
|
+
|
|
465
|
+
| condition | what it does |
|
|
466
|
+
|---|---|
|
|
467
|
+
| `MLX_PREBUILT_PATH` is set | links `libmlx.a` / `libmlxc.a` / `mlx.metallib` from that directory. **This is the branch Torobi takes**, below |
|
|
468
|
+
| `xcrun -sdk macosx metal --version` works | builds MLX from source through cmake, patching `device.cpp` / `device.h` to disable NAX for older Metal |
|
|
469
|
+
| no Metal compiler (this machine) | downloads `mlx-prebuilt-v0.1.0-macos-arm64.tar.gz` from OminiX's releases |
|
|
470
|
+
|
|
471
|
+
This machine takes the third: `xcrun -sdk macosx metal --version` answers
|
|
472
|
+
`cannot execute tool 'metal' due to missing Metal Toolchain`.
|
|
473
|
+
|
|
474
|
+
**The download is `curl -L -f` and nothing else.** No checksum, no
|
|
475
|
+
signature, nothing in `build.rs` that verifies what arrived beyond TLS
|
|
476
|
+
(`grep -n 'sha256|checksum|verify|digest'` finds nothing). It runs during
|
|
477
|
+
`gem install`, on the machine of whoever installs, and announces itself
|
|
478
|
+
as `cargo:warning=Downloading pre-built MLX from: ...`. Whoever wants
|
|
479
|
+
that guarantee sets `MLX_PREBUILT_PATH` to a directory they trust; that
|
|
480
|
+
is the first branch, and it exists for exactly this.
|
|
481
|
+
|
|
482
|
+
**Where it lands, and who moves it afterwards.** `build.rs` extracts into
|
|
483
|
+
`OUT_DIR/mlx-prebuilt/` (reused on later builds if the three files are
|
|
484
|
+
there) and then copies the metallib to `target/<profile>/mlx.metallib`.
|
|
485
|
+
Everything past that point is Torobi's own: `extconf.rb` appends a
|
|
486
|
+
Makefile rule that puts it beside the installed bundle, `rake metallib`
|
|
487
|
+
puts it in `lib/torobi/` for a checkout, and the command line finds it
|
|
488
|
+
beside itself in `target/`. `Torobi::Preflight` and `runtime.rs` refuse
|
|
489
|
+
when it is missing, which is the end of the chain and the reason a
|
|
490
|
+
missing file is an error rather than an abort.
|
|
491
|
+
|
|
492
|
+
bindgen runs in every mode, from the mlx-c headers in the submodule.
|
|
493
|
+
|
|
494
|
+
**Torobi takes the first branch on purpose.** `lib/torobi/mlx_prebuilt.rb`
|
|
495
|
+
fetches the archive itself, refuses anything whose SHA-256 is not the
|
|
496
|
+
recorded one, unpacks it into `~/.cache/torobi/`, and hands that directory
|
|
497
|
+
to cargo through the environment (`extconf.rb` exports it into the
|
|
498
|
+
generated Makefile; the Rakefile sets it for the engine's own builds). So
|
|
499
|
+
the third branch never runs, one checked copy serves every build on the
|
|
500
|
+
machine, and a replaced release asset breaks against the digest instead of
|
|
501
|
+
being linked. That digest was computed from the bytes here and agrees with
|
|
502
|
+
the one GitHub publishes for the asset.
|
|
503
|
+
|
|
504
|
+
## Which mlx-c, and which MLX under it
|
|
505
|
+
|
|
506
|
+
**The generation is now stated by the dependency itself.** `mlx-sys 0.6.0`
|
|
507
|
+
pins mlx-c `c74db530` (v0.6.0-7) over **MLX 0.32.2**, its submodule and its
|
|
508
|
+
`CHANGELOG.md` both say so, and `lib/torobi/mlx_prebuilt.json`'s `requires`
|
|
509
|
+
refuses a pre-built archive that reports anything else. The rest of this
|
|
510
|
+
section is the investigation that made that the deciding question; the
|
|
511
|
+
`v0.4.1` / `v0.30.1` pair below is what was current before the move.
|
|
512
|
+
|
|
513
|
+
`mlx-sys` generates its bindings from mlx-c's headers, so what the archive
|
|
514
|
+
has to match is mlx-c, not MLX. OminiX's copy of mlx-c differed from
|
|
515
|
+
upstream's `v0.4.1` by exactly one line, and it is not a header:
|
|
516
|
+
|
|
517
|
+
```diff
|
|
518
|
+
- GIT_TAG v0.30.1)
|
|
519
|
+
+ GIT_TAG v0.32.0)
|
|
520
|
+
```
|
|
521
|
+
|
|
522
|
+
That line decides which MLX the from-source path compiles; `mlx/c/ops.h`
|
|
523
|
+
and the rest of the headers are identical to the tag. So the bindings are
|
|
524
|
+
mlx-c v0.4.1's either way, and an archive of **mlx-c v0.4.1 with MLX
|
|
525
|
+
v0.30.1** is the pairing upstream tagged and the one those headers were
|
|
526
|
+
written against. OminiX's from-source path is the asymmetric one: mlx-c
|
|
527
|
+
0.4.1 over an MLX two minor versions newer than it was written for.
|
|
528
|
+
|
|
529
|
+
This was worth checking rather than assuming, and the assumption made
|
|
530
|
+
first here was wrong: an earlier note in this ledger said the headers
|
|
531
|
+
expected 0.32.0 and the binary was behind them. The headers expect mlx-c,
|
|
532
|
+
and mlx-c is what they got.
|
|
533
|
+
|
|
534
|
+
Two things follow.
|
|
535
|
+
|
|
536
|
+
**The unknown MLX revision is findable.** The build script says the
|
|
537
|
+
pre-built form "is what ships in the official `mlx-metal` wheel, which is
|
|
538
|
+
the only way to obtain a build of MLX's Metal kernels without the Metal
|
|
539
|
+
compiler". So the tarball is a repackaged wheel, and the version that
|
|
540
|
+
wheel names is the MLX revision this ledger cannot currently state.
|
|
541
|
+
|
|
542
|
+
**There is an exit, and it is narrow.** What Torobi depends on OminiX for
|
|
543
|
+
is not the Rust API - the engine touches about twelve functions
|
|
544
|
+
(`matmul`, the four arithmetic ops, `square`, `mean`, `transpose_axes`,
|
|
545
|
+
`contiguous`, `stop_gradient`, `value_and_grad_with_argnums`, `eval`,
|
|
546
|
+
`Array::from_slice`, `Exception`) - but the build machinery: building
|
|
547
|
+
without Xcode. That is replaceable:
|
|
548
|
+
|
|
549
|
+
1. obtain the official `mlx-metal` wheel (pip download, unpack), and
|
|
550
|
+
2. point `MLX_PREBUILT_PATH` at it.
|
|
551
|
+
|
|
552
|
+
**That exit is narrower than it was written.** Upstream's `mlx-sys` has no
|
|
553
|
+
prebuilt branch at all: `build_and_link_mlx_c` runs cmake on its mlx-c
|
|
554
|
+
submodule every time, with `MLX_BUILD_METAL=ON` under the `metal` feature,
|
|
555
|
+
and MLX refuses to configure that without the Metal toolchain. The
|
|
556
|
+
`MLX_RS_METAL_PATH` it does read only chooses where the metallib it builds
|
|
557
|
+
is cached; there is nowhere to hand it one.
|
|
558
|
+
|
|
559
|
+
So `MLX_PREBUILT_PATH` is OminiX's, and it is the whole of what Torobi
|
|
560
|
+
cannot get upstream:
|
|
561
|
+
|
|
562
|
+
| | upstream mlx-rs | OminiX-MLX |
|
|
563
|
+
|---|---|---|
|
|
564
|
+
| with the Metal toolchain | works, building MLX itself | works |
|
|
565
|
+
| without it (this machine, and most installs) | **cannot build** | works |
|
|
566
|
+
|
|
567
|
+
**But there is a way through that needs no branch and no patch**, found
|
|
568
|
+
by reading rather than assuming, and tried rather than argued:
|
|
569
|
+
|
|
570
|
+
1. `mlx-c` has an option for exactly this. Its CMakeLists says
|
|
571
|
+
`if(MLX_C_USE_SYSTEM_MLX) find_package(MLX REQUIRED) else() FetchContent ...`,
|
|
572
|
+
so a pre-installed MLX means MLX is never fetched and never compiled.
|
|
573
|
+
2. MLX's install exports a real package (`MLXTargets.cmake`,
|
|
574
|
+
`MLXConfig.cmake` into `share/cmake/MLX`), so `find_package` has
|
|
575
|
+
something to find.
|
|
576
|
+
3. The `cmake` crate reads `CMAKE_TOOLCHAIN_FILE` from the environment
|
|
577
|
+
(`cmake-0.1.58/src/lib.rs:450`), so variables can be put into upstream
|
|
578
|
+
mlx-sys's cmake run the same way `MLX_PREBUILT_PATH` is put into
|
|
579
|
+
OminiX's: an exported path, no fork.
|
|
580
|
+
|
|
581
|
+
Tried, with crates.io's `mlx-rs = "0.25"` unmodified and a toolchain file
|
|
582
|
+
setting `MLX_C_USE_SYSTEM_MLX=ON` and `CMAKE_PREFIX_PATH`. **`metal` is a
|
|
583
|
+
default feature of both crates, and MLX was still never built**: cmake
|
|
584
|
+
configured, skipped the fetch, and went on to compile mlx-c's own C++
|
|
585
|
+
against the MLX it was given. The Metal toolchain was never asked for.
|
|
586
|
+
|
|
587
|
+
It stopped there, on the version pair rather than the mechanism:
|
|
588
|
+
|
|
589
|
+
```
|
|
590
|
+
mlx-prefix/include/mlx/fast.h:52: std::optional<array> mask_arr = {},
|
|
591
|
+
CMakeFiles/mlxc.dir/mlx/c/fast.cpp.o] Error 1
|
|
592
|
+
```
|
|
593
|
+
|
|
594
|
+
`mlx-sys 0.2.0`'s vendored mlx-c is written against **MLX v0.25.1**, and
|
|
595
|
+
it was handed v0.30.1's headers. So the price of the published crate,
|
|
596
|
+
today, is running MLX five minor versions back. What the archive must
|
|
597
|
+
carry also changes: an install tree (lib, include, `share/cmake/MLX`)
|
|
598
|
+
rather than four loose files.
|
|
599
|
+
|
|
600
|
+
`mlx-prebuilt` installs a prefix rather than gathering four files, so one
|
|
601
|
+
archive serves both ways in: the tree is what `find_package(MLX)` follows,
|
|
602
|
+
and `lib/` holds what a Rust build links. It is nested under a name
|
|
603
|
+
because `lib` is cmake's word for that directory and a bare `lib/` reads
|
|
604
|
+
like a gem's; renaming it is not available, since the exported package
|
|
605
|
+
records paths relative to the prefix. Torobi searches the unpacked archive
|
|
606
|
+
for the prefix rather than knowing where it is, so the shape stays the
|
|
607
|
+
archive's business.
|
|
608
|
+
|
|
609
|
+
**The exit was taken on 2026-09-21**, once upstream had moved to the
|
|
610
|
+
0.32 generation and the version pair stopped being the obstacle. Its cost
|
|
611
|
+
turned out to be a number (the MLX/mlx-c pair the archive must carry)
|
|
612
|
+
rather than a fork or a patch, which is what made taking it cheap. What
|
|
613
|
+
remained was the distribution question (docs/plan.md 11.4), now decided
|
|
614
|
+
there: a platform gem, which builds nothing at install and so leans the
|
|
615
|
+
whole of it on the pre-built path. The dependency and the distribution
|
|
616
|
+
question were two faces of one decision.
|
|
617
|
+
|
|
618
|
+
## Updating the pin
|
|
619
|
+
|
|
620
|
+
One thing moves, the MLX/mlx-c generation, and it moves in two places
|
|
621
|
+
that are one decision: the archive the pin names, and the binding that
|
|
622
|
+
was written against it. One version at a time, as its own change:
|
|
623
|
+
|
|
624
|
+
1. pick the mlx-c commit, and read what MLX it pins and what changed
|
|
625
|
+
under `mlx/c/` since `c74db530`;
|
|
626
|
+
2. build and release a `takahashim/mlx-prebuilt` archive for it (bump
|
|
627
|
+
`MLX_C_REF` in its `build.sh`), set `requires` in
|
|
628
|
+
`mlx_prebuilt.json` to that generation, and `rake mlx:pin[v<release>]`;
|
|
629
|
+
3. rebuild. A header change that renames or removes a function fails at
|
|
630
|
+
compile time; one that keeps a name and changes what it means does not,
|
|
631
|
+
so read the header diff against "mlx-c, call by call" above and fix the
|
|
632
|
+
table with the code;
|
|
633
|
+
4. run the differential, convergence, memory and installed-gem tests
|
|
634
|
+
(docs/plan.md section 12), and the seed-42 draws in
|
|
635
|
+
`engine/src/mlxc/random.rs`, which change if MLX's RNG does.
|
|
636
|
+
|
|
637
|
+
`Torobi::Native.build_info` reports what a build was made from: `mlx` and
|
|
638
|
+
`mlx_c` are the MLX release and the mlx-c commit from the archive's
|
|
639
|
+
`MANIFEST.txt`, read at build time by `engine/build.rs`.
|
|
640
|
+
|
|
641
|
+
## What the installed-gem smoke test found
|
|
642
|
+
|
|
643
|
+
`ruby test/installed_gem_smoke.rb` builds the gem, installs it into an
|
|
644
|
+
empty GEM_HOME, and runs one step from outside the checkout.
|
|
645
|
+
|
|
646
|
+
**2026-09-03, with the path dependency**: the mechanics worked (a 56 KB
|
|
647
|
+
source gem built the extension, the metallib landed where dladdr looks, a
|
|
648
|
+
step ran), but the gem only built on this machine: `engine/Cargo.toml`
|
|
649
|
+
named `/Users/maki/git/OminiX-MLX/mlx-rs` by absolute path, and that path
|
|
650
|
+
shipped inside the package. Moving the checkout aside and installing again
|
|
651
|
+
failed with `make: *** [target/release/libtorobi.dylib] Error 101`.
|
|
652
|
+
|
|
653
|
+
**2026-09-03, with the git dependency**: the same test **with the local
|
|
654
|
+
checkout renamed away** builds, installs and runs a step (loss 2.5). The
|
|
655
|
+
package no longer depends on anything outside itself and its pinned
|
|
656
|
+
remotes. That is the finding this ledger existed to reach.
|