Skip to content

feat: Support MLXCEL_CCCL_DIR env var and binary-adjacent CCCL lookup for CUDA NVRTC JIT #265

Closed

Description

@inureyes

Problem / Background

The MLX CUDA backend JIT-compiles a core set of kernels at runtime via NVRTC. gather/indexing kernels return precompiled=false and are always NVRTC-compiled from embedded sources (see mlx/backend/cuda/indexing.cpp and jit_module.cpp). gather is used for token-embedding lookup, so any model generate triggers NVRTC on first use. There is no global MLX_CUDA_JIT=OFF build switch (unlike the Metal backend), so the JIT cannot be compiled away; the kernels include <cuda/std/tuple> and therefore need the CCCL (libcu++) headers present at runtime.

jit_module.cpp::include_path_args() resolves the CCCL include path in this order:

  1. <dir-of-executable>/../include/cccl (MLX's cmake --install layout).
  2. Fallback to the compile-time MLX_CCCL_DIR define.

mlxcel's src/lib/mlx-cpp/CMakeLists.txt sets MLX_CCCL_DIR to the build tree's _deps/cccl-src/include (because the statically linked binary is not cmake installed). That absolute path only exists on the build machine. So a released binary run on any other host fails the first JIT with catastrophic error: cannot open source file "cuda/std/tuple" unless the CCCL headers are shipped in MLX's native <prefix>/include/cccl layout (which requires placing the binaries under bin/).

PR #262 addresses the release artifact by bundling the CCCL headers (~8.8 MB / ~953 files: include/cuda + include/nv) in that native layout. This issue is the complementary code-level change so the header location is overridable and a flat binary layout is supported, which matters when another application (Backend.AI:GO) embeds mlxcel and controls where the headers live.

Proposed Solution

Add an MLXCEL_CCCL_DIR environment variable (and a binary-adjacent cccl/ lookup) so the CUDA runtime NVRTC JIT can locate the CCCL (libcu++) headers at an operator-controlled path, instead of relying on either the build-time-baked MLX_CCCL_DIR (which points at the build machine) or MLX's fixed <binary_parent>/include/cccl install layout. This makes mlxcel portable as an embedded CUDA inference engine (e.g. the Backend.AI:GO engine) without forcing a specific on-disk artifact layout.

Patch the vendored MLX via src/lib/mlx-cpp/patches/ (so it survives MLX pin bumps and is attributed per the project's third-party attribution rules in CLAUDE.md). Extend the CCCL include-path resolution in mlx/backend/cuda/jit_module.cpp::include_path_args() to try, in priority order:

  1. MLXCEL_CCCL_DIR env var, if set and exists (highest priority).
  2. <dir-of-executable>/cccl (binary-adjacent, same directory as the executable — supports a flat artifact layout).
  3. Existing <dir-of-executable>/../include/cccl (MLX native install layout).
  4. Existing compile-time MLX_CCCL_DIR fallback.

The second NVRTC include path (CUDA runtime headers) already honors CUDA_HOME / CUDA_PATH; document that alongside.

Acceptance Criteria

  • With MLXCEL_CCCL_DIR=/path/to/cccl set, mlxcel runs a CUDA generate on a host whose CUDA install does not provide a compatible CCCL, with a flat binary layout (binary and mlxcel-server not under bin/), and the first-run NVRTC JIT succeeds.
  • A binary-adjacent ./cccl/ directory is found without any env var.
  • When neither is present, behavior is unchanged (still falls back to MLX's native layout / baked MLX_CCCL_DIR).
  • The change is implemented as an mlx-cpp patch with the required per-file derivation note and NOTICE/README attribution updates, and survives an MLX pin bump.
  • Document the variable in docs/installation.md (and the environment-variables reference) next to the CUDA JIT notes.

Technical Considerations

Activity

  1. added
    type:enhancement
    priority:medium
    status:ready
    on Jun 13, 2026
  2. added
    priority:high
    and removed
    priority:medium
    on Jun 13, 2026
  3. inureyes commented on Jun 13, 2026

    @inureyes
    MemberAuthor

    Confirmed blocking: relative-invocation breaks the bundled-CCCL layout

    Verified on a Backend.AI GPU node with the PR #262 artifact (bin/ + include/cccl/, bundle present and correct).

    Root cause confirmed. MLX resolves the CCCL include dir via mlx/backend/common/utils.cpp::current_binary_dir(), which uses dladdr(&current_binary_dir, &info) and returns std::filesystem::path(info.dli_fname).parent_path(). When the binary is launched by a relative path (./mlxcel from inside bin/), glibc returns a relative dli_fname (./mlxcel), so:

    • current_binary_dir() = parent_path("./mlxcel") = .
    • jit_module.cpp then does root_dir.parent_path() = "" (empty)
    • CCCL path = "" / "include" / "cccl" = include/cccl, resolved against the cwd (bin/) -> bin/include/cccl, which does not exist (the bundle is at ../include/cccl)
    • the compiled-in MLX_CCCL_DIR fallback points at the build machine and also does not exist
    • result: no CCCL --include-path is added -> gather_*_int32_1.cu NVRTC compile fails with cannot open source file "cuda/std/tuple"

    Proof. Same artifact, same node: cd ~ && ~/mlxcel-test/mlxcel-linux-x86_64-cuda13/bin/mlxcel run (absolute path) succeeds; cd bin && ./mlxcel run (relative) fails. The only difference is the invocation path feeding dladdr.

    So the bin/ + include/cccl/ bundling from PR #262 is necessary but not sufficient: MLX's path resolution must be made invocation-independent. Two parts:

    1. Honor MLXCEL_CCCL_DIR (and optionally a binary-adjacent cccl/) FIRST, before the dladdr-derived path. An absolute env value bypasses the whole problem and is the clean path for Backend.AI:GO embedding.
    2. Resolve the executable directory from /proc/self/exe (canonical absolute) rather than relying on dladdr's possibly-relative dli_fname, so <exe-dir>/../include/cccl works regardless of how the process was launched.

    Workaround until this lands: invoke mlxcel/mlxcel-server via an absolute path (which a service manager / Backend.AI:GO subprocess spawn already does).

  4. added this to the 0.3 milestone on Jun 21, 2026
  5. self-assigned this
    on Jun 21, 2026
  6. added
    status:done
    and removed
    status:ready
    on Sep 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

priority:highstatus:donetype:enhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions