Skip to content

refactor(mlxcelverse): organize MLX-side overlays and custom kernels under src/lib/mlxcelverse - #2305

Merged
inureyes merged 2 commits into
mainfrom
refactor/issue-1816-mlxcelverse-layout
Oct 14, 2026
Merged

inureyes merged 2 commits into
mainfrom
refactor/issue-1816-mlxcelverse-layout

Conversation

@inureyes

Copy link
Copy Markdown
Member

Summary

Reorganize src/lib/mlx-cpp/ into src/lib/mlxcelverse/ so that mlxcel's C++ MLX layer has a clear, self-documenting two-tier boundary between backend source overlays (overlays/{cpu,metal,cuda,rocm}/mlx/...) and mlxcel-owned fused custom kernels (kernels/).

Changes

  • Top-level layout (src/lib/mlxcelverse/):
    • Moved src/lib/mlx-cpp/CMakeLists.txt to src/lib/mlxcelverse/CMakeLists.txt and added src/lib/mlxcelverse/README.md.
    • Consolidated patches/mlx/backend/cpu into overlays/cpu/mlx/backend/cpu, patches/mlx/backend/metal into overlays/metal/mlx/backend/metal, patches/mlx/backend/cuda + patches-cuda/*.cpp into overlays/cuda/mlx/, and patches-rocm into overlays/rocm.
    • Moved src/lib/mlx-cpp/turbo/ to src/lib/mlxcelverse/kernels/.
    • Added per-backend README.md files under overlays/{cpu,metal,cuda}/ explaining each overlay's purpose, mechanics, and base-commit provenance.
  • Build & C++ bridge (src/lib/mlxcel-core/):
    • Updated build.rs, Cargo.toml, and build_support/mlx_pin.rs to point to src/lib/mlxcelverse/ (overlays/{cpu,metal,cuda,rocm} and kernels).
    • Updated src/lib/mlxcel-core/cpp/*.{cpp,h} include paths from "../../mlx-cpp/turbo/..." to "../../mlxcelverse/kernels/...".
  • Tooling, CI, docs, and tests:
    • Updated .github/workflows/{ci.yml,release.yml}, Makefile, scripts/ci/{mlx_pinned_commit.sh,check_cuda_arch_lists.py,check_kernel_dtype_keys.py,check_kernel_port_dispatch.py}, scripts/mlxcelverse/{rocm_overlay.py,sync_from_fork.sh}, and scripts/bench_decode.sh.
    • Updated CONTRIBUTING.md, NOTICE, docs/*, src/lib/mlxcel-mlx-pin/tests/cross_parser.rs, src/lib/mlxcel-core/src/cache/paged_pool_offset_tests.rs, and src/execution/memory_estimate_inflight_tests.rs.

Verification

  • Verified exact file-for-file and content-for-content equivalence of mlx_apply_source_overlays before vs. after across Metal/CPU (29 files), CUDA (32 files), and ROCm (153 files).
  • bash scripts/ci/mlx_pinned_commit_test.sh and cargo test -p mlxcel-mlx-pin (27/27 passed).
  • make verify-versions verify-kernel-dtype-keys verify-kernel-port-dispatch verify-llama-compat verify-rocm-overlay verify-python-tooling verify-fmt (123/123 Python tooling tests passed).
  • bash scripts/mlxcelverse/rocm_overlay_test.sh (29/29 passed) and python3 scripts/mlxcelverse/rocm_overlay.py drift --no-fetch.
  • python3 scripts/insert_apache_header.py --check and python3 scripts/check_cross_repo_refs.py.
  • Guarded ROCm hardware smoke test (scripts/ci/rocm_smoke.sh on Qwen3-0.6B-4bit, gfx1151): 32 tokens in 0.13s = 255.73 tok/s.

Closes #1816

…under src/lib/mlxcelverse

Reorganize `src/lib/mlx-cpp/` into `src/lib/mlxcelverse/` so that mlxcel's C++ MLX layer has a clean, self-documenting two-tier boundary between backend source overlays (`overlays/{cpu,metal,cuda,rocm}/mlx/...`) and mlxcel-owned fused custom kernels (`kernels/`).

- Move `src/lib/mlx-cpp/CMakeLists.txt` to `src/lib/mlxcelverse/CMakeLists.txt` and update `mlx_apply_source_overlays` to apply `overlays/cpu` and `overlays/metal` unconditionally, `overlays/cuda` when `MLX_BUILD_CUDA` is enabled (consolidating the previously split `patches/mlx/backend/cuda/` and `patches-cuda/` directories into a single `overlays/cuda/mlx/` root), and `overlays/rocm` when `MLX_BUILD_ROCM` is enabled.
- Move `src/lib/mlx-cpp/turbo/` to `src/lib/mlxcelverse/kernels/` and update `src/lib/mlxcel-core/build.rs`, `src/lib/mlxcel-core/Cargo.toml`, and `src/lib/mlxcel-core/cpp/*.{cpp,h}` include paths (`"../../mlxcelverse/kernels/..."`).
- Add `README.md` files for `src/lib/mlxcelverse/` and `src/lib/mlxcelverse/overlays/{cpu,metal,cuda}/` explaining the layer split, overlay mechanics, base-commit provenance headers, and per-backend scope.
- Update CI workflows, pin and lint gates (`mlx_pinned_commit.sh`, `check_cuda_arch_lists.py`, `check_kernel_dtype_keys.py`, `check_kernel_port_dispatch.py`), ROCm overlay tooling (`scripts/mlxcelverse/rocm_overlay.py`, `sync_from_fork.sh`), documentation (`CONTRIBUTING.md`, `NOTICE`, `docs/*`), and test fixtures (`mlxcel-mlx-pin`, `paged_pool_offset_tests`, `memory_estimate_inflight_tests`) to reference `src/lib/mlxcelverse/`.

Closes #1816
@inureyes inureyes added status:review Under review type:refactor Code restructuring without changing functionality priority:medium Medium priority labels Oct 14, 2026
@inureyes

Copy link
Copy Markdown
Member Author

Implementation Review Summary

Intent

Reorganize mlxcel's MLX-side C++ layer from src/lib/mlx-cpp/ into src/lib/mlxcelverse/ with a clear two-tier structure separating per-backend whole-file source overlays (overlays/) from mlxcel's custom fused kernels (kernels/).

Findings Addressed

  • Complete directory restructuring: src/lib/mlx-cpp/ moved to src/lib/mlxcelverse/ with explicit overlays/{cpu,metal,cuda,rocm} and kernels/ tiers (N/A)
  • Accurate bilingual technical reports committed under TECHNICAL_REPORTS/ per repository .keep-reports convention (LOW)

Remaining Items

None

Verification

  • All stated requirements implemented
  • No placeholder/mock code remaining
  • Integrated into project code flow
  • Project conventions followed
  • Existing modules reused where applicable
  • No unintended structural changes
  • Tests pass

@inureyes

Copy link
Copy Markdown
Member Author

Security and Performance Review Summary

Threat Boundaries & Scope

This review evaluates the architectural re-organization of mlxcel's MLX-side C++ layer from src/lib/mlx-cpp/ to src/lib/mlxcelverse/ across security, safety, build performance, and runtime execution criteria. The codebase operates as an in-process native acceleration and build-time overlay framework. All paths, scripts, and build artifacts were analyzed for command injection, path traversal, unexpected source divergence, cache pollution, compilation regressions, and runtime kernel overhead.

Security Assessment

  • Injection & Process Execution: All maintenance and verification scripts (scripts/mlxcelverse/rocm_overlay.py, scripts/ci/*.py) invoke commands via sanitized argument lists with standard library tools (subprocess.run(["git", ...]), avoiding shell interpolation (shell=True).
  • Path Traversal & Safe Sandboxing: CMake configure_file(COPYONLY) targets explicit destination trees under ${CMAKE_BINARY_DIR}/_deps/mlx-src. Cache purges in src/lib/mlxcel-core/build.rs (purge_stale_mlx_cache) operate strictly within Cargo's isolated OUT_DIR/build/_deps, preventing directory escape.
  • Data Integrity & License Boundaries: All 31 kernel launcher and source files retain valid Apache-2.0 license headers (scripts/insert_apache_header.py --check passed). Third-party upstream MLX files in overlays/ preserve their original copyright notices and declared base-commit provenance SHAs. No credentials or secrets are present.
  • Concurrency & State Sharing: Thread safety and concurrency guarantees around SmallSizePool allocator pools and process-wide device mapping in overlays/rocm/ remain intact as guarded by existing concurrency suites (tests/rocm_scalar_pool_concurrency.rs, tests/rocm_device_map_concurrency.rs).

Performance Assessment

  • Zero Runtime Overhead: Verified that all source overlay implementations for CPU, Metal, CUDA, and ROCm remain byte-for-byte identical in executable code before and after the refactoring (only comment citations of moved files updated). Fused kernel launchers route through mlxcel::select_kernel_port without additional dispatch latency. Guarded hardware smoke test on AMD Radeon 8060S (gfx1151) confirms 255.73 tok/s decode performance.
  • Build System & Incremental Compilation: configure_file(COPYONLY) avoids updating file modification timestamps when contents are unchanged, preventing spurious recompilation of unchanged MLX objects across builds. Cargo rerun-if-changed triggers in src/lib/mlxcel-core/build.rs cleanly track CMakeLists.txt, overlays/{cpu,metal,cuda,rocm}, and individual kernel files without over-invalidation.
  • CI & Gate Efficiency: Filter patterns in .github/workflows/ci.yml accurately partition backend overlays (overlays/rocm/**, overlays/metal/**, overlays/cuda/**), ensuring heavy CUDA/ROCm jobs on self-hosted runners are triggered only by relevant changes.

Findings

  • CRITICAL: 0
  • HIGH: 0
  • MEDIUM: 0
  • LOW: 0

Verification

  • Exact source and overlay equivalence verified across Metal, CPU, CUDA, and ROCm trees
  • MLX pin validation and cross-parser consistency (cargo test -p mlxcel-mlx-pin, scripts/ci/mlx_pinned_commit_test.sh: 27/27 passed)
  • Static contract gates (make verify-versions verify-kernel-dtype-keys verify-kernel-port-dispatch verify-rocm-overlay verify-fmt: all passed)
  • ROCm overlay drift validation (scripts/mlxcelverse/rocm_overlay.py drift --no-fetch: drift OK, 0 differences)
  • ROCm overlay synthetic suite (scripts/mlxcelverse/rocm_overlay_test.sh: 29/29 checks passed)
  • CUDA architecture lists audit (scripts/ci/check_cuda_arch_lists.py: 7 lists checked OK)
  • Cross-repository reference check (scripts/ci/check_cross_repo_refs.py: passed)

@inureyes

Copy link
Copy Markdown
Member Author

PR Finalization Complete

Summary

  • Tests: Verified exact byte equivalence across Metal, CPU, CUDA, and ROCm source overlays; verified unit and contract test suites (cargo test -p mlxcel-mlx-pin, mlx_pinned_commit_test.sh, check_cross_repo_refs_test.sh, rocm_overlay_test.sh, and verify-python-tooling covering 123 tests); verified guarded ROCm hardware smoke on gfx1151 (Qwen3-0.6B-4bit at 255.73 tok/s).
  • Documentation: Layout and maintenance documentation added (src/lib/mlxcelverse/README.md and src/lib/mlxcelverse/overlays/{cpu,metal,cuda}/README.md); bilingual technical reports committed (TECHNICAL_REPORTS/2305-mlxcelverse-layout-20261014.{en,ko}.md); path citations updated across CONTRIBUTING.md, NOTICE, and docs/* with historical logs preserved.
  • Lint/Format: Verified cargo fmt --all -- --check, Apache-2.0 license headers, ROCm overlay drift records (0 drift), CUDA architecture lists, and kernel port dispatch checkers.

All narrow gates pass cleanly. Ready for merge.

@inureyes inureyes added status:done Completed and removed status:review Under review labels Oct 14, 2026
@inureyes
inureyes merged commit 096152c into main Oct 14, 2026
27 checks passed
@inureyes
inureyes deleted the refactor/issue-1816-mlxcelverse-layout branch October 14, 2026 02:17
inureyes added a commit that referenced this pull request Oct 15, 2026
…2318)

## Summary

Classify the #1712 failure as far as the evidence allows, make the next recurrence self-classifying, and fix a coverage gap found on the way: the "steel envelope" test has not exercised the steel kernel since May.

## Findings

**The test never reached the steel kernel.** `update_and_turbo4_delegated_attention` tries the dequant-first SDPA route first, and that route has been default-on since f77e98b (2026-05-08, two days after the test landed in 09d9cc6). Under the default env the wrapper returns before `attention_turbo4_delegated_steel`, and nothing else calls it. So the test, and the launcher CONTRIBUTING.md names for post-pin-bump re-validation, compared dequant-first SDPA against the cold-only composition. The `4.6824e-1` came from that pairing, not from `turbo4_delegated_steel_sdpa`.

**The wrong value is "cold V contribution dropped".** I rebuilt step 0 (cold_offset=8, hot_offset=1, same seeds) on CUDA and measured candidate wrong outputs against the graph reference. Attention with the cold V rows zeroed sits at RMS 4.6820e-1, 4e-5 from the reported 4.6824e-1 (the two correct paths differ by about 1.4e-4). The next closest candidates are far off: K read at full buffer capacity 4.03e-1, all-zero output 5.12e-1, hot V dropped 5.15e-2, missing inverse rotation 7.85e-1. So on one side the cold contribution was all zeros: either the bulk rotated-dequant output (dequant-SDPA route), the cold weighted-sum output (cold-only route), or a zero `v_rescale`. A never-written fresh Metal buffer reads as zeros, which explains why the value repeated to five digits while the failure itself was rare. The value alone cannot tell which side or which of these.

**It has not recurred.** Nightly verify since 2026-09-08: 48 runs. 44 ran the test and all 44 passed (0accedd through 537ebe1, four Mac minis). One run failed before the test step (38430123889) and one was cancelled. Six of the passes ran on MLX 243bdf09 (PR #2304 branch and main after the merge). Every nightly passes `--test-threads=1`, while both failures came from multi-threaded workspace gates. That fits the shared-device hazard of #163/#1008 but does not prove it. In this window the delegated kernel source changed only by #2029 (dispatch through `select_kernel_port`, kernel bodies untouched), the #2305 move, and the #1796 header update. A code read of the three launchers found no unwritten output region, out-of-bounds index, or missing barrier.

## Changes

- New test-only `cache/delegated_parity_diag.rs`. On failure it reports the route gates, a comparison of the two caches' K, packed V, rescale and hot V, each observed output against the graph reference (with the max-abs location), the dropped-contribution signatures, and an in-process re-run of every route on both caches. Thresholds are unchanged.
- Both steel tests now also call `attention_turbo4_delegated_steel` directly, and still check the wrapper's output, so the pairing that failed keeps its coverage.

## Test plan

- [x] CUDA (GB10): new `cache::delegated_parity_diag_tests` (2 tests) pass and run the full report path. The step-0 candidate table above comes from a throwaway test on the same binary.
- [x] The two macOS-gated tests compile on Linux with their gate lifted (clippy `-D warnings`, `--lib --tests`). Gate restored before commit.
- [ ] Metal: Nightly verify dispatched on this branch, run [38777513435](https://github.com/lablup/mlxcel/actions/runs/38777513435), pending. The steel kernel has not been tested since May, so a red result here is a real finding, not noise.

Refs #1712
inureyes added a commit that referenced this pull request Oct 30, 2026
Refreshes parts of the ROCm documentation that later work made stale. No code change.

- `docs/installation.md`, Linux with AMD ROCm: the decode throughput paragraph quoted Qwen3-30B-A3B-4bit at about 55 tok/s (fused MoE off) and gpt-oss-20b-MXFP4-Q4 at about 8 tok/s (generic gather kernel), both from the 2026-09-30 baseline. Current figures are about 62 tok/s with the fused MoE HIP port and 63.62 tok/s with the mxfp4 warp-shared gather kernel (`LOCAL_FIXES.md` item 41), and the paragraph now links both benchmark pages.
- Same section, limits table: "Windows, multiple GPUs, distributed inference: Not supported" contradicted the page's own Windows with AMD ROCm section. Windows now links there; multi-GPU and distributed stay unsupported.
- Wave64 row: "Not run" while the same row cites a gfx942 run (item 52). Now "Not validated", pointing at #2699.
- `tests/rocm_hip_grid_clamp.rs`: a doc comment cited the pre-#2305 `patches-rocm/` path.

Historical technical reports, benchmark pages and logit-trace metadata keep the paths and numbers of their time and are not edited.

Validation: `make verify-env-var-docs` passes; every number traces to the linked benchmark pages.
@inureyes inureyes added this to the 0.8 milestone Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

priority:medium Medium priority status:done Completed type:refactor Code restructuring without changing functionality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

refactor(mlx-cpp): organize mlxcel's MLX-side layer as mlxcelverse (backend overlays and mlxcel kernels)

1 participant