Skip to content

Metal: opt-in sealed pipelines from shipped binary archives - #74

Open
jonathan308 wants to merge 2 commits into
mainfrom
dfense/sealed-metal-pipelines
Open

jonathan308 wants to merge 2 commits into
mainfrom
dfense/sealed-metal-pipelines

Conversation

@jonathan308

Copy link
Copy Markdown

Summary

MLX builds GPU code at run time: generated kernel sources (get_library(name, builder)), compile fusion, fast::metal_kernel, and the lowering of every pipeline from AIR (newComputePipelineState on a shader-cache miss). All of it goes through the process's Metal compiler service. This PR adds an opt-in sealed-pipelines mode so a caller can serve only GPU code that was built offline and shipped. Off by default; with the mode off, behavior is unchanged.

API

  • C++: metal::set_sealed_pipelines(mode, archive_paths, kernel_pack_path), sealed_pipelines(), sealed_pipelines_report(), start_pipeline_recording(), stop_pipeline_recording().
  • C (mlx/backend/common/sealed_pipelines.h): mlx_metal_set_sealed_pipelines, mlx_metal_sealed_pipelines_mode, mlx_metal_get_sealed_pipelines_counts, mlx_metal_sealed_pipelines_misses, mlx_metal_start_pipeline_recording, mlx_metal_stop_pipeline_recording.
  • Set the mode once, before the first GPU kernel. A later call fails and changes nothing.

enforce mode

  • Every pipeline and specialized function comes from the shipped MTLBinaryArchives with MTLPipelineOptionFailOnBinaryArchiveMiss / MTLFunctionOptionFailOnBinaryArchiveMiss.
  • Metal ignores those options when no archive is given, so MLX treats an empty archive set as a miss.
  • A generated kernel source must either be in mlx.metallib (every host_name is there) or be in the kernel pack: metallibs keyed by SHA-256 of the math mode plus the source.
  • A miss throws ... is not in the shipped GPU code. Runtime Metal compilation is refused. MLX builds nothing.
  • compile() runs the unfused graph, including functions compiled before the mode was set (gpu::can_build_kernels()).
  • fast::metal_kernel works only for sources in the kernel pack.
  • Sealed mode uses the non-NAX kernels. metal-tt (Metal toolchain 27.1.266.1) cannot translate NAX kernels (Metal 4 cooperative tensors): applegpu-nt: error: unsupported deferred-static-alloca-size function body.

report mode

A miss builds the code as before. It is counted and listed by sealed_pipelines_report().

Recording and offline build

  • start/stop_pipeline_recording() returns JSON (mlx-metal-pipelines/1): generated sources, plus each pipeline's library, function, specialized name and function-constant values.
  • scripts/sealed_pipelines.py build --metallib mlx.metallib --arch applegpu_g17s --macos-version 27.0 --out DIR rec.json... writes:
    • kernels.mlxpack;
    • a .mtlp-json pipelines script;
    • one <arch>.mtlarchive per GPU architecture, built with metal-tt;
    • manifest.json with SHA-256s.
  • Archive size: an archive embeds the AIR of the libraries it was built from. The tool therefore prunes mlx.metallib to the recorded functions first (metal-objcopy --keep-symbol). Metal matches pipelines by function AIR, so archives still hit when MLX loads functions from the full mlx.metallib.
    • The test workload's archive is 204 KB, instead of 223 MB.
  • An archive loads only on the macOS major it was built for.

Tests

tests/standalone/sealed_pipelines_metal.cpp runs one case per ctest process, because the mode is process-wide. fork-tests.yml runs ctest -R '^sealed_pipelines_metal\.' -V.

case checks
record report mode + recording; schema, function constants, custom kernel present
build runs the build tool for the device's architecture and macOS major (skips with 77 if metal-tt is missing or can't build for the GPU)
enforce the workload (matmul with function constants, gather, compile, custom kernel) runs sealed: 0 archive misses, 0 library misses, same values as the CPU, no com.apple.MTLCompilerService in launchctl print pid/<self>; an unrecorded erf pipeline and a new custom kernel both fail closed
enforce-empty no archives: the first kernel fails closed
report misses run and are counted
late setting the mode after a kernel returns -1 and leaves the mode off

Local results (M5 Max applegpu_g17s, macOS 27.0, Release):

  • all 6 sealed cases pass;
  • tests gives 317/323 passed. The 6 failures are linalg cases that compare GPU matmul with tight tolerances (test matrix inversion, lu, QR, cholesky, eigh, pseudo-inverse). They fail identically on unmodified main 56e12b0 on this machine, and pass with DEVICE=cpu.

The second commit adds AGENTS.md to fork.yaml (the same lines as #73), because the fork-diff gate fails on main since 56e12b0.

Not in this PR

  • Exposing the switch in mlx-swift, and the d-inference wiring (archive build in the release lane, gating). Those are separate PRs.
  • Throughput cost of sealed mode (no fusion, no NAX): measured in the d-inference PR.

🤖 Generated with Claude Code

jonathan308 and others added 2 commits December 4, 2026 09:50
MLX builds GPU code at run time: generated kernel sources, compile
fusion, custom kernels, and the lowering of each pipeline from AIR. All
of it goes through the Metal compiler service of the process. A caller
that must serve only GPU code built offline can now turn on sealed
pipelines. The mode is off by default and nothing changes when it is
off.

- metal::set_sealed_pipelines(mode, archive_paths, kernel_pack_path)
  (C: mlx_metal_set_sealed_pipelines) sets the mode once, before the
  first GPU kernel. A later call fails and changes nothing.
- enforce: pipelines and specialized functions come only from the
  shipped MTLBinaryArchives with the fail-on-miss options. An empty
  archive set is a miss (Metal ignores the options without archives).
  A generated source must be in mlx.metallib or in the kernel pack. A
  miss throws "Runtime Metal compilation is refused".
- compile() runs functions unfused, also for functions compiled before
  the mode was set (gpu::can_build_kernels()).
- Sealed mode uses the non-NAX kernels: metal-tt (Metal toolchain
  27.1) cannot translate the NAX kernels.
- report: a miss builds the code as before and is counted.
- start/stop_pipeline_recording() record the pipelines (function,
  specialized name, constant values) and generated sources as JSON.
- scripts/sealed_pipelines.py builds the kernel pack and one archive
  per GPU architecture with metal-tt. It prunes mlx.metallib to the
  recorded functions first, so an archive is about 200 KB, not a copy
  of the 223 MB metallib.
- tests/standalone/sealed_pipelines_metal.cpp: record, build, enforce
  (same values as the CPU, no miss, no compiler service, unrecorded code
  fails closed), enforce with no archive, report, and a late call. Each
  case runs in its own ctest process; fork-tests.yml runs them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The fork-diff gate fails on main since 56e12b0 because no section
describes AGENTS.md. Same lines as in #73.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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.

1 participant