Skip to content

[MLX] Handle empty contexts in GGUF quantized linear lowering - #25334

Merged
mergennachin merged 4 commits into
mainfrom
gh/mergennachin/47/head
Nov 21, 2026
Merged

mergennachin merged 4 commits into
mainfrom
gh/mergennachin/47/head

Conversation

@mergennachin

@mergennachin mergennachin commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

A dynamic GGUF linear exported with nonempty example inputs can receive zero activation rows at runtime. The fused Q4_K/Q5_K/Q6_K paths then try to compile a Metal kernel for an empty input and fail. Emit a runtime branch that reshapes the empty input to the correct output width and runs the existing quantized kernel only for nonempty inputs. Add it only when ExportedProgram range constraints permit a zero dimension; tracing ShapeEnv bounds can exclude 0/1 even when the exported ABI allows them.

This is a pre-existing bug: the same failing artifacts reproduce with both the old and updated MLX pins. The generated DFlash2 native test exposes it through draft_forward's supported zero-context ABI with cached prefixes. Ordinary batching generation normally supplies new target hidden states; a user-facing generation failure was not reproduced. The underlying GGUF path is shared by original DFlash and DFlash2.

Thirty new native configurations export at M=4 and run with M=0 in float32/float16/bfloat16 and M=1/8 in float16, across Q4_K/Q5_K/Q6_K and direct/default lowering. All pass, along with five existing Q6_K dynamic cases. Twelve additional positive-range cases assert the original serialized IfNode counts and pass; six direct/default Q4_K/Q5_K/Q6_K delegate payloads are byte-identical to the old lowering when the minimum sequence length is one. Restoring the old lowering fails the empty-context controls for all three fused formats on both MLX pins. Tests concatenate a sentinel row to exercise an empty internal projection without depending on the separate pre-existing standalone-empty-output limitation.

Tracked in #23011.

Authored with assistance from OpenAI Codex.

…ash window correction, and DFlash2 support with CI coverage.

[ghstack-poisoned]
…ash window correction, and DFlash2 support with CI coverage.

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/25334

Note: Links to docs will display an error until the docs builds have been completed.

❌ 3 Cancelled Jobs, 2 Pending, 1 Unrelated Failure

As of commit d96c545 with merge base 21c7c99 (image):

CANCELLED JOBS - The following jobs were cancelled. Please retry:

BROKEN TRUNK - The following job failed but were present on the merge base:

👉 Rebase onto the `viable/strict` branch to avoid these failures

This comment was automatically generated by Dr. CI and updates every 15 minutes.

…nd update the wheel ownership probe, and guard the MLX-only export test in builds without generated MLX files. Rebase the full stack onto fetched origin/main.

[ghstack-poisoned]
…nd update the wheel ownership probe, and guard the MLX-only export test in builds without generated MLX files. Rebase the full stack onto fetched origin/main.

[ghstack-poisoned]
Base automatically changed from gh/mergennachin/46/head to main November 21, 2026 04:27
@mergennachin
mergennachin merged commit 5554d29 into main Nov 21, 2026
@mergennachin
mergennachin deleted the gh/mergennachin/47/head branch November 21, 2026 04:28
mergennachin added a commit that referenced this pull request Nov 30, 2026
### Summary

Fixes #25602

Pin MLX to the stable v0.32.5 release
(`cfc675e9e59cb6ee29a647cf8fe87ef844eff879`), replacing the development
snapshot `14dc9c673bce9a51dca4e58f5b2ccae80736431d`. The release
includes an upstream integral-constant implementation that supersedes
our Metal address-space compatibility workaround, so remove that patch
and its CMake entry. Keep the platform SDK patch and update the recorded
metallib baseline comment; the size limits are unchanged.

The replacement is upstream
[ml-explore/mlx#4833](ml-explore/mlx#4833)
(`07e7bdae8c1e680ef8fe6bfbb89965712ba53fe0`), included in v0.32.5. It
fixes the same unavailable-trait error on older Metal environments by
deriving the type from the arithmetic expression and preserving `const`,
without using `metal::remove_addrspace_t` in this header.

Based on main at `aeeeb202cd3ac0f741b43dc1ba8b11ab17dc22b2`, including
#25333, #25334 and #25335. Authored with AI assistance using OpenAI
Codex.

### Test plan

Validated on an M5 Max Mac with macOS 27.2, Xcode 27.1 and main's Torch
2.15.0 pin. Built the MLX shared Python runtime with deployment target
macOS 14 and the native `mlx-release` runtime with deployment target
26.2. Built all Muse Glimmer runners, workers and native tests. All
three MLX artifacts remain below their existing CI size limits.

| Coverage | Result |
| --- | --- |
| MLX lowering/export/model Python tests | 239 unique tests passed |
| Native MLX CTests | 10/10 passed, including 50 threads × 100
predictions and speculative primitives in TF32/FP32 modes |
| Custom kernels | 197 passed across six modules, including GGUF
empty-context cases |
| Generic model suite | 37 passed, 5 skipped |
| Native operator sweep | 971 passed, 20 TF32 numerical failures;
baseline comparison described below |
| Generic operator suite | 403 passed, 64 skipped, 2 undefined
duplicate-index failures described below |
| Muse Glimmer native CTests | 3/3 passed, including serialized DFlash2
proposals 1–15, sliding windows, sampling and cache lifecycle |
| Full cached Muse Glimmer 30B DFlash2 model | Both 7- and 15-proposal
runs passed: 17 completed requests each, concurrency 4, plus an aborted
stream and recovery; exercised streaming, tool turns, prefix reuse and
session isolation |

The 20 native TF32 failures also occur with the previous MLX pin. Built
both pins against the same main and ran identical saved PTEs/inputs:
outputs were exactly identical between pins, and all 20 cases passed on
both pins with `MLX_ENABLE_TF32=0`. The generic operator failures are
the same `index_put` cases recorded in #25333: duplicate indices with
`accumulate=False`, whose result is undefined by PyTorch. No tolerances
or defaults were changed. The live smoke client skips reversed tool
results due to known issue #25299.

Representative reproduction commands, using the standard MLX development
dependencies:

```sh
cmake --preset mlx-release -DEXECUTORCH_BUILD_TESTS=ON -DEXECUTORCH_BUILD_EXTENSION_RUNNER_UTIL=ON -DEXECUTORCH_MLX_ENABLE_SANITIZERS=OFF
cmake --build cmake-out --parallel 12
python -m executorch.backends.mlx.test.run_all_tests -j4 --max-tasks-per-worker 10 --clean-after
python -m pytest -c /dev/null backends/test/suite/operators -m flow_mlx -n4 --timeout=180
python -m pytest -c /dev/null backends/test/suite/models -m flow_mlx -n2 --timeout=300
python backends/mlx/test/export_multi_thread_test_model.py /tmp/multi-thread.pte
ET_TESTING_MODEL_PATH=/tmp/multi-thread.pte ET_TESTING_NUM_THREADS=50 ET_PREDICTIONS_PER_THREAD=100 ctest --test-dir cmake-out/backends/mlx/test --output-on-failure --no-tests=error
```

The remaining Python/custom-kernel and Muse Glimmer fixture/build/CTest
commands follow `.github/workflows/mlx.yml`. For live testing, served
the cached `SocialLocalMobile/Muse-Glimmer-30B-ExecuTorch-PTE` DFlash2
model with the newly built batching worker, `--dflash --dflash-n-draft
7` and then `15`, and ran the Muse Glimmer repository's
`scripts/smoke_test.py --requests 8 --concurrency 4`. This validates the
existing full-model PTE on the new runtime; the serialized fixtures
separately validate fresh export.


Additional patch-removal check: compiled all 12 integral-constant binary
operators plus mixed-type/chained-value cases against both the old
patched header and the stable upstream header. Both passed Metal 3.1
targeting macOS 14, Metal 4.1 targeting macOS 26.2, and Metal 3.1
targeting iOS 17. These probes used Xcode 27.1; Xcode 15.4 itself is not
installed here and was not tested.

The remaining `mlx_metal_sdk_per_platform.patch` is still required:
unpatched v0.32.5 hard-codes `-sdk macosx` and `-mmacosx-version-min`
for both Metal compile and link commands. Compared generated commands
for macOS, iOS device and iOS simulator with both short SDK names and
absolute SDK paths. Unpatched upstream selects the wrong platform for
all iOS cases; the patch selects the correct SDK and deployment flag in
all six configurations. Also successfully compiled and linked the
bundled JIT-mode metallib for macOS 14, iOS 17 device and iOS 17
simulator using the patched CMake file (Xcode 27.1). This is Metal
kernel cross-compilation coverage, not on-device iOS runtime testing.

This branch was previously deployed

1 inactive deployment
cadence — d96c5451 Deployed Nov 20, 2026 by mergennachin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants