Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions .github/workflows/fork-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ name: Fork Tests
# build-cpp builds the C++ library and tests, and runs the C++ tests
# with the GPU and then with the CPU as the default device,
# and the JACCL tests with simulated verbs (tests/jaccl).
# Then it runs the standalone allocation cap tests, one
# process for each case, and the bounded Metal fence chains.
# Then it runs the standalone allocation cap tests and the
# sealed pipeline tests, one process for each case, and the
# bounded Metal fence chains.
# python-gpu runs the Python tests with the GPU as the default device.
# python-cpu runs the Python tests with the CPU as the default device.
# metal-build-and-test
Expand Down Expand Up @@ -182,6 +183,20 @@ jobs:
ctest --test-dir build -R '^process_allocation_cap' -V \
--no-tests=error

- name: Run the sealed Metal pipeline tests
# The sealed pipeline mode is set once in each process, before the
# first GPU kernel. ctest runs each case in a fresh process: record,
# build the archive with metal-tt, enforce, and three cases without
# an archive (tests/CMakeLists.txt). The build and enforce cases skip
# when the runner has no metal-tt or metal-tt cannot build for its
# GPU; -V prints why.
if: ${{ !cancelled() && steps.build.outcome == 'success' }}
env:
LLVM_PROFILE_FILE: ${{ runner.temp }}/profiles/sealed-%p-%m.profraw
run: |
ctest --test-dir build -R '^sealed_pipelines_metal\.' -V \
--no-tests=error

- name: Run the bounded Metal fence chains
# metal_fence_chain runs exact GPU -> CPU -> GPU chains with fast
# synchronization at three command buffer sizes, one ctest entry for
Expand Down
60 changes: 59 additions & 1 deletion fork.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,61 @@ def:
do. The seed search ran in PR #46 (first commit).
globs:
- "python/tests/mlx_tests.py"
- title: "Sealed Metal pipelines (opt-in)"
description: |
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. Sealed pipelines let a caller serve only GPU code
that was built offline and shipped. The mode is off by default.
`metal::set_sealed_pipelines(mode, archive_paths, kernel_pack_path)` (C:
`mlx_metal_set_sealed_pipelines`) sets it once, before the first GPU kernel; a later
call fails and changes nothing.

In `enforce` mode, each pipeline and each specialized function comes from a shipped
`MTLBinaryArchive` with the fail-on-miss options. Metal ignores those options when no
archive is given, so MLX treats an empty archive set as a miss. A generated kernel
source must be in the precompiled metallib (each `host_name` is in it) or in the
kernel pack, a file of metallibs keyed by the SHA-256 of the source and the math
mode. A miss throws an error that names the code and says "Runtime Metal compilation
is refused"; MLX builds nothing. `compile` runs the function unfused
(`gpu::can_build_kernels()` is false), also for a function that was compiled before.
`fast::metal_kernel` works only for sources in the kernel pack. MLX uses the non-NAX
kernels, because the offline translator (`metal-tt`, Metal toolchain 27.1) fails on
NAX kernels (Metal 4 cooperative tensors). In `report` mode a miss builds the code as
before and is counted, and `sealed_pipelines_report()` lists it.

`start_pipeline_recording()` / `stop_pipeline_recording()` record the pipelines
(library, function, specialized name, constant values) and the generated sources of a
workload as JSON. `scripts/sealed_pipelines.py build` turns recordings into the
kernel pack, a Metal pipelines script and one archive for each GPU architecture with
`metal-tt`. It gives `metal-tt` a copy of `mlx.metallib` with only the recorded
functions (`metal-objcopy --keep-symbol`), because an archive holds the AIR of the
libraries it is built from. An archive loads only on the macOS major it was built
for. `tests/standalone/sealed_pipelines_metal.cpp` runs one case in each ctest process:
record, build, enforce (no miss, the same values as the CPU, no compiler service in
`launchctl print`, unrecorded code fails closed), enforce with no archive, report, and
a late call. The build and enforce cases skip (77) when `metal-tt` is missing or
cannot build for the GPU.
globs:
- "mlx/backend/common/sealed_pipelines.h"
- "mlx/backend/metal/sealed_pipelines.h"
- "mlx/backend/metal/sealed_pipelines.cpp"
- "mlx/backend/metal/metal.h"
- "mlx/backend/metal/no_metal.cpp"
- "mlx/backend/metal/device.h"
- "mlx/backend/metal/device.cpp"
- "mlx/backend/metal/device_info.cpp"
- "mlx/backend/metal/CMakeLists.txt"
- "mlx/backend/gpu/device_info.h"
- "mlx/backend/cuda/device_info.cpp"
- "mlx/backend/no_gpu/device_info.cpp"
- "mlx/backend/cpu/compiled.cpp"
- "mlx/backend/no_cpu/compiled.cpp"
- "mlx/compile.cpp"
- "scripts/sealed_pipelines.py"
- "tests/standalone/sealed_pipelines_metal.cpp"
- "tests/CMakeLists.txt"
- ".github/workflows/fork-tests.yml"
- title: "Fork tooling"
description: |
The fork deletes the upstream workflows that cannot run here: the PyPI release build,
Expand All @@ -735,7 +790,7 @@ def:
wheel. The other job builds the C++ library and tests, and runs the C++ tests with the
GPU and then with the CPU as the default device, and builds and runs the JACCL tests
with simulated verbs (`tests/jaccl`). Then it runs the standalone allocation
cap tests with ctest, one process for each case. The Python tests run in shard jobs:
cap tests and the sealed pipeline tests with ctest, one process for each case. The Python tests run in shard jobs:
one set with the GPU as the default device, and one set with the CPU.
`.github/scripts/fork-tests/python-shards.json` gives the shards.
`python_shards.py` fails a job when a test file is in no shard or in two shards, and
Expand Down Expand Up @@ -772,6 +827,8 @@ def:
under PRoot (`.github/scripts/fedora-proot/`); the Fedora x86_64 job and
the protected Pages publisher stay on GitHub-hosted runners.
`FORKDIFF.md` describes the qualification and watchdog time bounds.
`AGENTS.md` adds the "Working in our MLX forks" section permitting agent-assisted
contributions in Layr-Labs forks (`56e12b00` on `main`).
`darkbloom-watchdog.yml` remains unchanged;
a rerun keeps these static labels. On a Darkbloom Sandbox that has
no Darkbloom cache, `fork-build-cache` sets `CI_ACTIONS_CACHE=off` and every
Expand Down Expand Up @@ -825,6 +882,7 @@ def:
- ".github/scripts/test-ci-resource-sampler.py"
- ".github/scripts/test-runner-sizes.py"
- "scripts/check_forkdiff.py"
- "AGENTS.md"
- "FORKDIFF.md"
- "docs/ci-codeql-native.md"
- "README.md"
Expand Down
71 changes: 71 additions & 0 deletions mlx/backend/common/sealed_pipelines.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright © 2026 Apple Inc.

#pragma once

#include <stddef.h>
#include <stdint.h>

#include "mlx/api.h"

#if defined(__APPLE__)
#ifdef __cplusplus
extern "C" {
#endif

/// Modes of sealed Metal pipelines.
/// - OFF: the default. MLX builds GPU code at run time as needed.
/// - REPORT: MLX uses the shipped GPU code. A miss builds the code at run
/// time and is counted.
/// - ENFORCE: MLX uses only the shipped GPU code. A miss is an error.
/// In REPORT and ENFORCE, `compile` runs functions unfused and MLX uses the
/// non-NAX kernels.
enum {
MLX_METAL_SEALED_PIPELINES_OFF = 0,
MLX_METAL_SEALED_PIPELINES_REPORT = 1,
MLX_METAL_SEALED_PIPELINES_ENFORCE = 2,
};

/// Sets the sealed pipeline mode, the Metal binary archives (`archive_count`
/// paths) and the kernel pack (NULL for none). Call it before the first GPU
/// kernel; a later call fails. Returns 0, or -1 and a message in `error`
/// (at most `error_size` bytes with the terminating NUL).
MLX_API int mlx_metal_set_sealed_pipelines(
int mode,
const char* const* archive_paths,
size_t archive_count,
const char* kernel_pack_path,
char* error,
size_t error_size);

/// The current sealed pipeline mode.
MLX_API int mlx_metal_sealed_pipelines_mode(void);

/// Counts since the process started. A library hit is a generated kernel
/// source found in the precompiled metallib or in the kernel pack.
typedef struct mlx_metal_sealed_pipelines_counts {
uint64_t archive_hits;
uint64_t archive_misses;
uint64_t library_hits;
uint64_t library_misses;
} mlx_metal_sealed_pipelines_counts;

MLX_API void mlx_metal_get_sealed_pipelines_counts(
mlx_metal_sealed_pipelines_counts* counts);

/// The misses (at most 1024), one per line. The caller frees the string.
MLX_API char* mlx_metal_sealed_pipelines_misses(void);

/// Starts to record the pipelines (function and constant values) and the
/// generated kernel sources that the GPU builds. Start it before the first
/// GPU kernel to record a whole workload. Record in REPORT mode, so that MLX
/// selects the same kernels as in ENFORCE mode.
MLX_API void mlx_metal_start_pipeline_recording(void);

/// Stops the recording and returns it as JSON (schema
/// "mlx-metal-pipelines/1"), or NULL on error. The caller frees the string.
MLX_API char* mlx_metal_stop_pipeline_recording(void);

#ifdef __cplusplus
}
#endif
#endif
6 changes: 5 additions & 1 deletion mlx/backend/cpu/compiled.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "mlx/backend/common/compiled.h"
#include "mlx/backend/cpu/encoder.h"
#include "mlx/backend/cpu/jit_compiler.h"
#include "mlx/backend/gpu/device_info.h"
#include "mlx/device.h"
#include "mlx/graph_utils.h"
#include "mlx/version.h"
Expand Down Expand Up @@ -54,7 +55,10 @@ static CompilerCache& cache() {
// usable C++ compiler (MSVC or clang-cl on Windows).
namespace detail {
bool compile_available_for_device(const Device& device) {
return device == Device::gpu || JitCompiler::available();
if (device == Device::gpu) {
return gpu::can_build_kernels();
}
return JitCompiler::available();
}

} // namespace detail
Expand Down
4 changes: 4 additions & 0 deletions mlx/backend/cuda/device_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,10 @@ bool is_available() {
return true;
}

bool can_build_kernels() {
return true;
}

int device_count() {
int count = 0;
cudaGetDeviceCount(&count);
Expand Down
6 changes: 6 additions & 0 deletions mlx/backend/gpu/device_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ namespace mlx::core::gpu {

MLX_API bool is_available();

/**
* Whether the GPU backend can build kernels at run time. When it cannot
* (Metal sealed pipelines), `compile` runs functions unfused.
*/
MLX_API bool can_build_kernels();

/**
* Get the number of available GPU devices.
*/
Expand Down
1 change: 1 addition & 0 deletions mlx/backend/metal/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ target_sources(
${CMAKE_CURRENT_SOURCE_DIR}/ternary.cpp
${CMAKE_CURRENT_SOURCE_DIR}/unary.cpp
${CMAKE_CURRENT_SOURCE_DIR}/resident.cpp
${CMAKE_CURRENT_SOURCE_DIR}/sealed_pipelines.cpp
${CMAKE_CURRENT_SOURCE_DIR}/utils.cpp)

if(NOT MLX_METAL_PATH)
Expand Down
Loading
Loading