Repository navigation
Metal: opt-in sealed pipelines from shipped binary archives - #74
Open
jonathan308 wants to merge 2 commits into
Open
jonathan308 wants to merge 2 commits into
jonathan308 wants to merge 2 commits into
Conversation
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>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 (newComputePipelineStateon 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
metal::set_sealed_pipelines(mode, archive_paths, kernel_pack_path),sealed_pipelines(),sealed_pipelines_report(),start_pipeline_recording(),stop_pipeline_recording().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.enforcemodeMTLBinaryArchives withMTLPipelineOptionFailOnBinaryArchiveMiss/MTLFunctionOptionFailOnBinaryArchiveMiss.mlx.metallib(everyhost_nameis there) or be in the kernel pack: metallibs keyed by SHA-256 of the math mode plus the source.... 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_kernelworks only for sources in the kernel pack.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.reportmodeA 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;.mtlp-jsonpipelines script;<arch>.mtlarchiveper GPU architecture, built withmetal-tt;manifest.jsonwith SHA-256s.mlx.metallibto the recorded functions first (metal-objcopy --keep-symbol). Metal matches pipelines by function AIR, so archives still hit when MLX loads functions from the fullmlx.metallib.Tests
tests/standalone/sealed_pipelines_metal.cppruns one case per ctest process, because the mode is process-wide.fork-tests.ymlrunsctest -R '^sealed_pipelines_metal\.' -V.recordbuildmetal-ttis missing or can't build for the GPU)enforcecom.apple.MTLCompilerServiceinlaunchctl print pid/<self>; an unrecordederfpipeline and a new custom kernel both fail closedenforce-emptyreportlateLocal results (M5 Max
applegpu_g17s, macOS 27.0, Release):testsgives 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 unmodifiedmain56e12b0 on this machine, and pass withDEVICE=cpu.The second commit adds
AGENTS.mdto fork.yaml (the same lines as #73), because the fork-diff gate fails onmainsince 56e12b0.Not in this PR
🤖 Generated with Claude Code