Skip to content

fix: make conv2d/conv1d fallible at the FFI boundary so conv shape faults don't abort the server #427

Closed

Description

@inureyes

Summary

conv2d (and conv1d) are declared in the cxx bridge as returning UniquePtr<MlxArray>, not Result. When MLX throws inside them (for example a shape mismatch), cxx converts the uncaught C++ exception into std::terminate, which aborts the whole server process instead of failing the one request. This is the same crash class that #384 fixed for the audio-synthesis matmul path. This issue extends that fix to the convolution ops so a conv fault on the audio (or vision) path degrades to a recoverable per-request error.

Background

The Gemma 4 audio conv crash surfaced as:

libc++abi: terminating due to uncaught exception of type std::invalid_argument: [conv] Expect the input channels in the input and weight array to match but got shapes - input: (1,12,130,1) and weight: (128,3,1,3)

The shape mismatch itself is a weight-layout bug fixed separately (the conv-weight layout-detection issue). This issue is the defense-in-depth half: even with correct layouts, any future data-dependent conv shape fault should not be able to take down the server.

Mechanism

  • src/lib/mlxcel-core/src/lib.rs:953 declares fn conv2d(...) -> UniquePtr<MlxArray> in the cxx bridge (non-Result). The C++ side (src/lib/mlxcel-core/cpp/mlx_cxx_bridge.cpp:2375) calls mlx::core::conv2d directly.
  • Per the fix(mlxcel-core): make audio-path MLX ops fallible at the FFI boundary #384 commit (09e5c3d) rationale: "An MLX C++ exception thrown from a non-Result cxx op aborts the process via std::terminate, which the catch_unwind backstop in the audio worker cannot intercept regardless of the release panic setting." So a throw from conv2d aborts the process; the audio worker's catch_unwind cannot catch a C++ std::terminate.
  • MLX validates conv shapes eagerly at op construction, so the throw happens at graph build, exactly where a Result-returning cxx wrapper can catch it.

Scope

  • Add try_conv2d (and try_conv1d) declared -> Result<...> in the cxx bridge, mirroring try_matmul from fix(mlxcel-core): make audio-path MLX ops fallible at the FFI boundary #384, so cxx catches an MLX throw at the FFI boundary and returns a recoverable Err.
  • Route the audio encoder's conv calls through the fallible variants: SSCPConvBlock conv2d (src/audio/encoder.rs:125) and ConformerLightConv1d depthwise conv1d (src/audio/encoder.rs:374), since their input shapes are data-dependent (derived from the runtime audio length).
  • Follow fix(mlxcel-core): make audio-path MLX ops fallible at the FFI boundary #384's scoping rationale: leave conv calls on hot paths with statically fixed shapes unchanged to avoid per-op overhead; convert the data-dependent audio-path calls. Vision-encoder conv calls can be evaluated for inclusion but are lower priority (their patch-embed shapes are fixed by config).
  • Map a returned Err to the existing audio worker error handling so the request fails cleanly with a message instead of aborting.

Tests

Acceptance criteria

  • A conv shape mismatch on the audio path produces a per-request Err (surfaced as a request error), not a process abort.
  • An FFI test demonstrates try_conv2d returns Err on a channel mismatch rather than terminating.
  • No behavior change on the happy path; no measurable per-op overhead added to statically-shaped conv hot paths.

Related

Companion to the conv-weight layout-detection fix (which removes the specific Gemma 4 audio trigger). This issue ensures the broader conv path is crash-isolated. References #384 (09e5c3d), which established the try_* fallible-op pattern for the audio path.

Activity

  1. added
    type:enhancement
    status:ready
    priority:medium
    area:core
    on Jun 24, 2026
  2. inureyes commented on Jun 24, 2026

    @inureyes
    MemberAuthor

    Companion to #428, the layout-detection fix that removes the specific Gemma 4 audio trigger. This issue is the defense-in-depth half (crash isolation for the broader conv path).

  3. added
    status:in-progress
    status:review
    and removed
    status:ready
    status:in-progress
    on Jun 25, 2026
  4. added
    status:done
    and removed
    status:review
    on Jun 25, 2026
  5. self-assigned this
    on Jun 25, 2026
  6. added a commit that references this issue on Jul 30, 2026
    194159e
  7. added 2 commits that reference this issue on Oct 27, 2026
    d434e3e
    ad2ec54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area:corepriority:mediumstatus:donetype:enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions