Emily.Bumblebee.FastKernels (emily v1.4.0)

Copy Markdown View Source

Rewrite RMSNorm, LayerNorm, RoPE, and SDPA Axon layers of a Bumblebee model so they call Emily.Fast.* instead of their stock defn implementations. When the rewritten model is then evaluated under Emily.Compiler, those Emily.Fast.* calls dispatch to fused MLX kernels via the :optional-node mechanism (see Emily.Fast's moduledoc). On any other backend the helpers fall back to defn composition and produce mathematically equivalent results, so applying the shim is safe even if the model is later evaluated on Nx.BinaryBackend or EXLA.

Optional dependency

This module depends on :axon and :bumblebee, which are declared as optional: true in Emily's mix.exs. Consumers who don't pull those deps into their own project get a clean build: the whole module definition is wrapped in Code.ensure_loaded?/1 and elides entirely when either dep is missing. To use the shim, add both to your own deps/0:

{:bumblebee, "~> 0.7"},
{:axon, "~> 0.8"}

Usage

{:ok, model_info} = Bumblebee.load_model({:hf, "openai/whisper-tiny"})

model_info =
  update_in(model_info.model, &Emily.Bumblebee.FastKernels.apply/1)

# or, to bound attention memory for long-context prefill:
#   update_in(model_info.model, &Emily.Bumblebee.FastKernels.apply(&1, force_fused_sdpa: true))

# then proceed with Bumblebee.Audio.speech_to_text_whisper/5
# (or Text.generation/4 / etc.) as usual.

Coverage

  • :rms_norm (Bumblebee's rms_norm/2).
  • :layer_norm (Axon's built-in normalization layer).
  • Bumblebee.Layers.apply_rotary_embedding/5 — supports the default schedule plus the :linear, :dynamic, :longrope, :llama3 scaling strategies, following Bumblebee's own schedules. Scaled inverse frequencies are precomputed at rewrite time and passed to mx::fast::rope via the freqs-override overload. The fused kernel runs for single-token calls (cached decode), with each row's position as its offset; multi-token calls such as prefill rotate by the explicit position_ids, so padded and batched inputs keep Bumblebee's per-token positions.
  • Bumblebee.Layers.attention_output_impl/3 — coalesced with its sibling attention_weights_impl/7 into a single mx::fast::scaled_dot_product_attention dispatch. Mask translation: plain and causal self-attention use MLX's built-in modes; anything else (key mask, bias, sliding window, or a cache offset) is collapsed into one additive array mask.

What's not rewritten

  • Norms with :channel_index other than -1 (uncommon outside vision-CNN heads — those don't fit the fused kernel's last-axis-only contract). The original layer is left in place.

Inference only

The rewrites target inference. Bumblebee copies a checkpoint's training-time attention_dropout_rate into its attention layers (0.1 for DistilBERT and other BERT-family models), but dropout does nothing under mode: :inference, so attention layers are fused whatever their dropout_rate. The fused layer applies no dropout, so train with the stock model, not a rewritten one.

Summary

Functions

Apply every available rewrite to model.

Functions

apply(model, opts \\ [])

@spec apply(
  Axon.t(),
  keyword()
) :: Axon.t()

Apply every available rewrite to model.

Options

  • :force_fused_sdpa - if true, the rewritten attention layers pass force_fused: true to Emily.Fast's SDPA helpers, so MLX always uses a fused attention kernel and raises when none supports the shape. This bounds attention memory for long prefill with head dims 192 and 256, at some cost in prefill throughput. See "Forcing the fused kernel" in Emily.Fast.scaled_dot_product_attention/4. Defaults to false.