Upstream defects¶
Defects of stock dependencies that gmlx guards against in its own code. Each entry gives the dependency and the versions seen, what goes wrong, the smallest repro, the guard and the test that fails without the guard. An entry stays until no supported version of the dependency has the defect.
mx.addmm misreads a first argument of one column on the GPU¶
- Dependency: mlx 0.32.2 and 0.32.3, GPU streams. The CPU stream is right.
- Affected:
mx.addmm(c, a, b)whose result has one column and more than one row, so c is [..., M, 1] with M above 1, when c is a view whose M values are not consecutive in memory: a column of a wider array, every second row of a taller one, reversed rows, a transposed row. float32, float16 and bfloat16, lazy or evaluated c, with or without batch dims. - What goes wrong: the op reads c at a fixed step from its first element that is not the row
stride of c. For a column of a wider array it returns the elements that follow c[0, 0] in its
row, one per output row. For a transposed row it reads past the buffer. No error is raised, and
a @ bis added to the wrong values. - Not affected: a c of two or more columns, or of one row, in any view; a dense c; a and b in
any view;
a @ b + c.
Smallest repro:
import mlx.core as mx
wide = mx.arange(21, dtype=mx.float32).reshape(3, 7)
c = wide[:, 3:4] # [3, 1], the values 3, 10, 17
a, b = mx.zeros((3, 2)), mx.zeros((2, 1))
print(mx.addmm(c, a, b).reshape(-1)) # GPU: [3, 4, 5]
print(mx.addmm(c, a, b, stream=mx.cpu).reshape(-1)) # [3, 10, 17]
print(mx.addmm(mx.contiguous(c), a, b).reshape(-1)) # GPU: [3, 10, 17]
- Where gmlx meets it: a LoRA layer adds its delta to the base output with
mx.addmm, the base output as c. That isLoRAKQuantLinearingmlx/load/modules.pyfor a text model and the pair placements ofgmlx/image/lora/install.pyfor an image model, where a pair on a band of one output column adds into a one-column slice of the base output. - Guard:
addmmingmlx/mlx_guards.py, which the three call sites use. A c of one column and more than one row goes in asc.reshape(-1).reshape(c.shape), which copies a view to dense memory, gives a transposed row the strides of its shape and costs a dense c nothing.mx.contiguous(c)is not enough: a transposed row is consecutive in memory, so it passes through unchanged and is still misread. A c of two or more columns, or of one row, goes in as it is, so the graph of a layer wider than one output is unchanged. - Test:
tests/test_mlx_guards.py. It holds the guard on each kind of view, a text LoRA layer of one output over a column of a wider array and an image LoRA band of one output column against a float64 sum, and it holds that a wider c reaches the stock op as the same array.
mx.pad misplaces its input past 2^31 output elements on the GPU¶
- Dependency: mlx 0.32.3, which mlx-kquant 0.5 pins, measured on GPU streams. 0.32.4 has the same code. The CPU stream is right. MLX main fixes it in afc03c5 (#4744), which no release carries yet.
- Affected:
mx.padin the constant mode, when the output holds more than 2^31 elements, the input holds at most INT32_MAX, and the input's last element lands more than INT32_MAX elements past its first one in the output. That takes a padded axis after a non-unit one, such as the token axis of [B, H, S, D] keys, or the width of an image of more than one row. Any dtype. - What goes wrong: the op fills the output and copies the input into a view of it that has the output's strides and the input's element count. The copy picks 32-bit offsets from that count, so each element whose offset from the start of the view passes INT32_MAX is written 2^32 elements below its place, outside the output, and its place keeps the fill. In two of gmlx's three measurements those writes landed on the op's own input, so a later read of the input is wrong too. No error is raised.
- Not affected: an output of at most 2^31 elements; an input of more than INT32_MAX elements, which
gets 64-bit offsets; a pad of the first non-unit axis only; the edge, reflect and symmetric modes,
which build the output from slice updates; a slice update into the output (
out[:, :16] = x), which offsets from the start of the whole output.
Smallest repro (a 2 GiB output):
import mlx.core as mx
rows, width = 65600, 32768
x = (mx.arange(rows) % 251).astype(mx.uint8)[:, None] + mx.zeros((1, 16), mx.uint8)
want = (mx.arange(rows) % 251).astype(mx.uint8)
out = mx.pad(x, [(0, 0), (0, width - 16)]) # [65600, 32768], 2.1e9 elements
print((out[:, 0] != want).sum()) # GPU: 64, the rows from 65536
out = mx.pad(x, [(0, 0), (0, width - 16)], stream=mx.cpu)
print((out[:, 0] != want).sum()) # 0
out = mx.zeros((rows, width), mx.uint8)
out[:, :16] = x # a slice update
print((out[:, 0] != want).sum()) # 0
- Where gmlx meets it:
BatchKVCache.extendingmlx/cache/kv_cache.pypads the keys and values of each side to the joint width when rows join a decoding batch ofgmlx serve. With 4 KV heads of 256, as on Qwen3.5-4B and 9B, a row of 293,377 tokens that joins 7 rows at 293,376 is the first that passes. That is past the trained context, which the server admits.BatchRotatingKVCache.extendin the same file pads the caches of sliding-window layers the same way. A joining row's buffer is its window plus its last prefill call, which a model that keeps only the last logits runs whole, so it widens with--prefill-step-size. On gemma-4-31B served text-only, with 16 KV heads of 256 in a window of 1024,GMLX_DECODE_BATCH=32and--prefill-step-size 16384, a prompt of 32,306 tokens that joins 31 decoding rows is the first that passes. At the default 8 rows gemma-4-26B-A4B passes with a step of 262,144 and a prompt of 152,502 tokens.--max-kv-sizedoes not build these caches ingmlx serve.BatchQuantizedKVCache.extendin the same file pads the packed keys and values and their scales and biases the same way under--kv-bitswith--kv-quant-scheme uniform. On gemma-4-26B-A4B at 8 bits, with 2 KV heads of 512 in its global layers andGMLX_DECODE_BATCH=64, a row of 129,537 tokens that joins 63 decoding rows is the first that passes.Downsampleingmlx/image/vae/flux1_ae.pypads the bottom and the right of the FLUX.1 VAE encoder's 128 channels. Up to 8192 a side, 38 canvases from 16,771,072 to 16,776,960 pixels pass, 4080x4112 and 4064x4128 among them.gmlx imagereaches them with an init image, which it cuts down to whole multiples of the stride.- Guard:
padingmlx/mlx_guards.py, which all four call. Past 2^31 output elements it builds the output as the fill with the input written in by a slice update. At or under 2^31 it is the stock op, so the graph of every smaller shape is unchanged. At the tested shapes it holds the stock op's memory peak. It stays until an MLX release carries afc03c5 and an A/B on these shapes shows the stock op right. - Test:
test_a_row_joining_a_batch_past_2_31_elements_keeps_every_key,test_a_row_joining_sliding_window_rows_past_2_31_elements_keeps_every_keyandtest_a_row_joining_a_quantized_batch_past_2_31_elements_keeps_every_keyintests/cache/test_kv_past_int32.py, andtest_a_vae_downsample_past_2_31_elements_keeps_every_pixelintests/image/test_image_past_int32.py. Each runs the caller at the first shape that passes, on a real Apple GPU, and checks every element of the output and of the inputs against a formula. With the stock op, the first leaves 12,058,624 keys of batch row 6, head 3 at the fill and writes as many over the 7 rows' own keys. The second leaves 48,896 keys of batch row 30, head 15 at the fill. The third leaves the 2,228,224 packed words of the keys of batch rows 61 and 62 at the fill. The fourth leaves 489,344 elements of the last image row at the fill and writes 472,960 of them over the start of x.test_pad_takes_a_slice_update_only_past_the_limitintests/test_mlx_guards.pyholds the routing on small arrays in every run, andtests/cache/test_kv_guard_calls.pyandtests/image/test_image_guard_calls.pyfail in every run when a caller calls the stock op instead. The past-limit tests are nightly.tests/past_int32.pyruns them one at a time, on a real Apple GPU with a recommended working set of at least 39 GiB, twice the largest test's footprint, sogate.py run --fullon such a Mac covers them.
mx.concatenate misplaces an input past 2^31 output elements on the GPU¶
- Dependency: mlx 0.32.3, which mlx-kquant 0.5 pins, measured on GPU streams. 0.32.4 has the same
code. The CPU stream is right. MLX main fixes it in df46f58 (#4758), which no release carries yet.
mx.stackon a later axis goes through concatenate. - Affected:
mx.concatenatewhen the output holds more than 2^31 elements, an input holds at most INT32_MAX, and that input's last element lands more than INT32_MAX elements past its first one in the output. That takes a joined axis after a non-unit one, such as the token axis of [B, H, S, D] keys or the last axis of [1, L, D] rows. Any dtype. - What goes wrong: the op copies each input into a view of the output that has the output's strides
and the input's element count, and picks 32-bit offsets from that count, as
mx.paddoes. Each element whose offset from the start of its view passes INT32_MAX is written 2^32 elements below its place, outside the output, and its place keeps what the memory held. In gmlx's measurements those writes landed on the op's own inputs, and when the copy read such an input afterwards, rows of the output under the limit came out wrong too. No error is raised. - Not affected: an output of at most 2^31 elements; the first non-unit axis; an input of more than INT32_MAX elements, which gets 64-bit offsets, though a smaller input beside it is still misplaced; a slice update into the output.
Smallest repro (a 2 GiB output):
import mlx.core as mx
rows, width = 65600, 32768
x = (mx.arange(rows) % 251).astype(mx.uint8)[:, None] + mx.zeros((1, 16), mx.uint8)
want = (mx.arange(rows) % 251).astype(mx.uint8)
out = mx.concatenate([x, mx.zeros((rows, width - 16), mx.uint8)], axis=1)
print((out[:, 0] != want).sum()) # GPU: 64, the rows from 65536
out = mx.concatenate([x, mx.zeros((rows, width - 16), mx.uint8)], axis=1, stream=mx.cpu)
print((out[:, 0] != want).sum()) # 0
- Where gmlx meets it:
growingmlx/cache/kv_append.pyregrows a KV cache by concatenating zeros on the token axis. With 8 rows decoding and 4 KV heads of 256, as on Qwen3.5-4B and 9B, the regrow from 260,608 to 268,800 tokens is the first that passes, inside their trained 262,144. gemma-4-26B-A4B passes at the same point. Plaingmlx servereaches it, and the keys of batch row 7, head 3 from token 55,808 are lost. Each later regrow writes its zeros 8 GiB below._pad_shared_kv_seqingmlx/spec/speculative.pypads the full-attention keys and values that a gemma-4 assistant drafter shares, when rows join an MTP batch. Two rows of gemma-4-E4B at 1,048,575 tokens that are padded to 1,048,577 are the first that pass, past the trained context.SingleBlock.outingmlx/image/families/flux2_klein/model.pyjoins a FLUX.2 klein block's attention and feed-forward rows on the last axis. 9B passes from 131,073 rows and 4B from 174,763, which the ComfyUI steps route reaches with several large references andgmlx imagewith a large canvas.pair_halvesingmlx/video/rope.pyreorders the 96 rotated dims of each 128-dim head of a MiniMax-H3 block's queries and keys and joins the other 32 back on the last axis. With 56 heads, H3 and FastH3 pass from 299,594 rows of the packed sequence. A 15 s clip at 2240x1280 has 299,600 video rows, andgmlx videoreaches it with H3 on a 96 GB Mac and with FastH3 on a 128 GB Mac.attendingmlx/video/families/h3/model.pyjoins H3's dense attention of query chunks of 4,096 rows on the token axis of [1, 56, L, 128]. It passes from 304,966 rows. A 15 s clip at 2304x1280 has 308,160 video rows, andgmlx videoreaches it on a 96 GB Mac. FastH3 runs video sparse attention instead.MemBlockingmlx/video/vae/tiny.pyjoins each frame's features and those of the frame before on the last axis in TAEH3, the decoder ofgmlx video --preview. The three blocks at 64 channels, over the 10 frames that a run of 5 latents grows to at a quarter of the canvas a side, pass at canvases from 26,844,160 to 53,686,272 pixels, 5184x5184 and 7200x3744 among them, on clips of 22 frames or more. Past that range each input gets 64-bit offsets. A 128 GB Mac reaches canvases up to 30,450,261 pixels and a 256 GB Mac the whole range. A run of 2 latents, the last of every clip, passes from 67,109,888 pixels and the blocks at 128 channels from 107,374,592, and a 512 GB Mac reaches both, up to 122,255,187 pixels.- Guard:
concatenateingmlx/mlx_guards.py, which all six call. Past 2^31 output elements it builds the output as zeros with each input written in by a slice update. At or under 2^31 it is the stock op, so the graph of every smaller shape is unchanged. At the tested shapes it holds the stock op's memory peak. It stays until an MLX release carries df46f58 and an A/B on these shapes shows the stock op right. - Test:
test_a_decode_regrow_past_2_31_elements_keeps_every_keyintests/cache/test_kv_past_int32.py,test_joining_rows_padded_past_2_31_elements_keep_every_keyintests/spec/test_shared_kv_past_int32.pyandtest_a_single_block_past_2_31_elements_keeps_every_rowintests/image/test_image_past_int32.py. Each runs the caller at the first shape that passes, on a real Apple GPU, and checks every element of the output and of the inputs against a formula. With the stock op the regrow leaves 52,428,800 keys unwritten and writes as many over the old keys, the shared-KV pad 1,024, and the single block its last row of 16,384, written over its attention rows.test_concatenate_takes_a_slice_update_only_past_the_limitintests/test_mlx_guards.pyholds the routing on small arrays in every run, andtests/cache/test_kv_guard_calls.py,test_pad_concatenates_through_the_guardintests/spec/test_shared_kv_inject_merge.py,tests/image/test_image_guard_calls.pyandtests/video/test_video_guard_calls.pyfail in every run when a caller calls the stock op instead. The past-limit tests run as the pad's do. The video sites have no past-limit test.
A gather misreads a strided view that spans more than INT32_MAX elements on the GPU¶
- Dependency: mlx 0.32.3, GPU streams. The CPU stream is right.
- Affected: a gather,
mx.take(x, indices, axis=...)or an index such asx[0]orx[idx], when x is a strided view of at most INT32_MAX elements whose elements span more than INT32_MAX elements of its buffer: one part of a split of a fused product with many rows, or a band of columns of a wide array. Any dtype. - What goes wrong: the gather picks 32-bit source offsets from the number of elements of x, not from the span of its strides, and reads each element past the 32-bit limit at a wrapped offset. The rows from the limit on come back wrong, and no error is raised.
- Not affected: a row-major x; a copy of the view, as
mx.contiguousmakes.
Smallest repro (a 2 GiB buffer):
import mlx.core as mx
rows, width = 65600, 32768
base = (mx.arange(rows) % 251).astype(mx.uint8)[:, None] + mx.zeros((1, width), mx.uint8)
view = base[:, :16] # [65600, 16], rows 32768 apart
want = (mx.arange(rows) % 251).astype(mx.uint8)
print((mx.take(view, mx.arange(rows), axis=0)[:, 0] != want).sum()) # GPU: 64, from row 65536
print((view[mx.arange(rows)][:, 0] != want).sum()) # GPU: 64
print((mx.take(view, mx.arange(rows), axis=0, stream=mx.cpu)[:, 0] != want).sum()) # 0
print((mx.take(mx.contiguous(view), mx.arange(rows), axis=0)[:, 0] != want).sum()) # 0
- Where gmlx meets it: a FastH3 block,
DiTBlock.attentioningmlx/video/families/h3/model.py, splits its fused qkv product, so v is a view with rows 3 x 56 x 128 apart. Its span passes INT32_MAX elements from 99,866 packed rows, which 1344x768 reaches at 345 frames.v[0]on that view is a gather. - Guard: the block hands
vsa_attentionq, k and v as views, throughsqueeze(0)and transposes, never through a gather. mlx-kquant'svsa_attentioncopies a view that spans past the limit to row-major before its own gathers (its docs/upstream-defects.md). - Test:
test_a_sparse_block_hands_the_call_views_not_gathersintests/video/test_video_h3_vsa.py. It stubs the call and checks that the graph of the q, k and v the call receives holds no gather. It needs no kernel, so it runs on any device.
CPU reductions lose digits over a long row¶
- Dependency: mlx 0.32.3, CPU streams. GPU streams are right.
- Affected:
mx.fast.layer_norm,mx.mean,mx.varandmx.sumover a long float32 axis on the CPU stream, which is the device of gmlx's CI and ofKQUANT_FORCE_CPU=1. - What goes wrong: a CPU reduction adds the row in float32 one element after another, so its error
grows with the row's length, and with its mean where the op takes the variance. Over 524,288
values
mx.fast.layer_normis 4e-6 relative off float64, and 2.5e-5 when the values sit near 16, against 5e-8 and 8e-7 on the GPU. No error is raised. - Not affected: float64 reductions on the CPU stream, which are exact to float64 rounding; short rows, such as a token's hidden size.
Smallest repro:
import mlx.core as mx
import numpy as np
x = np.random.default_rng(0).standard_normal((1, 1 << 19)).astype(np.float32)
want = (x - x.astype(np.float64).mean()) / x.astype(np.float64).std()
for s in (mx.cpu, mx.gpu):
y = np.asarray(mx.fast.layer_norm(mx.array(x), None, None, 0.0, stream=s), np.float64)
print(s, np.linalg.norm(y - want) / np.linalg.norm(want)) # cpu 2.9e-6, gpu 6e-8
- Where gmlx meets it: a VAE's GroupNorm normalizes each group over the whole image, so on the CPU a group of a large image reads a long row. The MiniMax-H3-Image encoder's fp32 self-test read 0.93 of its gate on the CPU against 0.53 on the GPU.
- Guard:
layer_normingmlx/mlx_guards.py, whichgroup_normingmlx/image/vae/group_norm.pyuses. On the CPU it takes the statistics in float64; on a GPU stream it is the stock op. - Test:
test_layer_norm_on_the_cpu_takes_its_statistics_in_float64intests/test_mlx_guards.py.
Integer floor division runs as eight GPU kernels¶
- Dependency: mlx 0.32.3, any stream.
- Affected:
a // kandmx.floor_divideon integer arrays, unsigned ones included. - What goes wrong: the op is built as a divide plus a sign fix of seven more ops (remainder, compares, a logical and, a cast, a subtract), each its own kernel. The values are right. For an unsigned array the fix never changes a value, so seven kernels a call do no work.
Smallest repro:
import io
import mlx.core as mx
f = io.StringIO()
mx.export_to_dot(f, mx.array(list(range(20)), dtype=mx.uint32) // 10)
print(f.getvalue().count("shape=rectangle")) # 14 ops, 8 of them kernels
- Where gmlx meets it: every sorted MoE forward takes the token row of each route as
order // k, once a MoE layer. - Guard:
floor_divideingmlx/mlx_guards.pycompiles an unsigned array's floor division into one kernel with the stock op's values; anything else is the stock op. The sorted expert paths ofgmlx/load/expert_store.py,gmlx/load/modules.pyandgmlx/lora_rows.pyuse it. - Test:
test_floor_divide_of_sorted_route_ids_is_one_kernelintests/test_mlx_guards.py.
The diffusers single-file loader leaves a BF16 tensor outside a linear layer undecoded¶
- Dependency: diffusers at e0abab83, the revision the image reference scripts under
scripts/image_oracle/pin. gmlx does not import diffusers: only those scripts do, to write the fixtures the image tests read. - Affected:
from_single_fileon a GGUF with a BF16 tensor that is not the weight of a linear layer. In the Qwen-Image 2.1 Q4_K_M file that istxt_in.text_norm.weight, the only BF16 vector of the file. - What goes wrong: the loader keeps the tensor in its GGUF bytes, and only a GGUF linear layer decodes them, in its call. Every other module reads the parameter as a plain tensor. On the bytes of a BF16 vector that is a tensor of twice the length, so the forward of the model stops at the text norm with a shape error.
- Guard:
load_modelinscripts/image_oracle/check3_qwen_image21.pydecodes each such parameter once, to the values the file holds, and refuses a codec other than BF16 there. It lists the names it decoded asresults.oracle.plain_decodedin the manifest of every fixture it loads the DiT for. - Test:
test_the_reference_loader_decoded_the_bf16_vectors_of_the_fileintests/image/test_image_oracle_harness.pyholds that list, in each committed manifest, to the BF16 vectors of the header inventory of the file. Without the guard no such fixture can be written.
pytest 9.1 hides a conftest's fixtures from a directory named twice¶
- Dependency: pytest 9.1.1, which the test job resolves. 9.0.3 is not affected. gmlx does not import pytest: only its test suite does.
- Affected: a command line whose test paths name one directory, then another, then the first
again, as in
tests/serve/a.py tests/b.py tests/serve/c.py. - What goes wrong: pytest collects the directory once for each run of consecutive paths in it,
and from 9.1 a conftest's fixtures are visible only below the directory node it was collected
for. The tests of the second node see no fixture of that conftest, its autouse fixtures
included, so a test that requests one errors with
fixture not foundand the tests that rely on an autouse restore fail. A run of the whole suite names each directory once and passes.
Smallest repro:
pytest tests/serve/test_admit_gate.py tests/test_envflags.py \
tests/serve/test_server_patches.py::test_the_seam_restore_lets_the_media_salt_install_again
- Guard:
pytest_configureintests/conftest.pyorders the test paths withgroupedintests/arg_order.py, so the paths of each directory are consecutive, in the order of first appearance at every level. - Test:
tests/test_arg_order.pyruns the repro in a child pytest and checks the grouping.
torch logs a redirect note on every macOS import of torch.distributed¶
- Dependency: torch 2.12.1 with transformers 5.22.0. gmlx does not depend on torch: the note shows only in an environment that has it installed.
- Affected: every gmlx command that imports a transformers processor. transformers imports
torch.distributed.checkpointwhen torch is present, which importstorch.distributed.elastic.multiprocessing.redirects. - What goes wrong: that module logs a warning at import on macOS, so the first line a command
prints is
NOTE: Redirects are currently not supported in MacOs.Nothing else is affected.
Smallest repro:
- Guard:
_umbrella_implingmlx/commands/cli.pyraises that logger's level to error before any command runs. - Test:
test_a_command_prints_no_torch_redirect_noteintests/commands/test_cli_quiet.pyruns a command in a child process where torch is installed and reads its output.