Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
1758260
Apply a per-row mask to each row of Gemma 2's grouped-query scores
ronaldmannak Nov 5, 2026
944909b
Add a release decode benchmark
ronaldmannak Nov 5, 2026
53911f9
Keep the Gemma 2 mask fix focused on its existing test suite
ronaldmannak Nov 5, 2026
3b5c028
Use per-row rotary positions and attention scaling in LLM and VLM models
ronaldmannak Nov 6, 2026
2ee53b1
Test each model's per-row positions with the row-offset cache
ronaldmannak Nov 6, 2026
a0b4dbe
Add full-attention batch caches and checked cache row operations
ronaldmannak Nov 6, 2026
7484860
Test rows that share a position behind a wider buffer
ronaldmannak Nov 6, 2026
593267b
Use the tiny Qwen VL helpers from RoPEApplicationTests
ronaldmannak Nov 6, 2026
4ab48eb
Bound BatchKVCache.filter's padding drop by the buffer, and cite mlx-vlm
ronaldmannak Nov 6, 2026
496a4a3
Fold QwenVLBatchedPositionTests.swift into RoPEApplicationTests.swift
ronaldmannak Nov 6, 2026
9c6ef21
Refuse batched cache rounds that cannot rewind padding
ronaldmannak Nov 6, 2026
652c276
Move batch row operations onto BatchableKVCache
ronaldmannak Nov 7, 2026
1c399b0
Validate merged rows and extract padding-only rows
ronaldmannak Nov 7, 2026
311ea97
Restore minimum fallback for cache progress inference
ronaldmannak Nov 10, 2026
ddd577c
Report the longest row's position as BatchKVCache.offset
ronaldmannak Nov 12, 2026
97ddd50
Say when BatchKVCache.offset excludes padding
ronaldmannak Nov 12, 2026
a2a96b0
Keep full-attention batches out of the shared cache kind
codex Nov 12, 2026
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
5 changes: 5 additions & 0 deletions Libraries/MLXLLM/Models/Gemma2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ class Gemma2Attention: Module {
var scores = matmul(queries, keys.swappedAxes(-1, -2))
scores = tanh(scores / logitSoftCap) * logitSoftCap

// A per-row `[B, 1, L, S]` mask needs the head-group axis of grouped-query scores.
var mask = mask
if repeats > 1, case .array(let array) = mask, array.ndim == 4 {
mask = .array(expandedDimensions(array, axis: -3))
}
scores = applyAttentionMask(scores: scores, mask: mask)
scores = softmax(scores, axis: -1, precise: true)
var output = matmul(scores, values)
Expand Down
16 changes: 16 additions & 0 deletions Libraries/MLXLLM/Models/Llama4AttentionScaling.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright © 2026 Apple Inc.

import MLX

/// Per-row query scales for attention shaped `[B, H, L, D]`.
func batchedLlama4AttentionScale(
offsets: MLXArray, length: Int, beta: Float, maxPositionEmbeddings: Int
) -> MLXArray {
let positions = offsets[0..., .newAxis] + MLXArray.arange(length)[.newAxis]
// Negative positions belong to left padding and must not produce log(0).
let nonnegative = MLX.maximum(positions, MLXArray(Int32(0))).asType(.float32)
let scaling =
1 + beta
* MLX.log(1 + MLX.floor(nonnegative / Float(maxPositionEmbeddings)))
return scaling[0..., .newAxis, 0..., .newAxis]
}
32 changes: 18 additions & 14 deletions Libraries/MLXLLM/Models/Mistral3Text.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ import MLXNN
private func getLlama4AttentionScale(
start: Int, stop: Int, beta: Float, maxPositionEmbeddings: Int
) -> MLXArray {
let positions = MLXArray(Int32(start) ..< Int32(stop))
var positions = MLXArray(Int32(start) ..< Int32(stop))
if start < 0 {
positions = MLX.maximum(positions, MLXArray(Int32(0)))
}
let scaling =
1 + beta
* MLX.log(
Expand Down Expand Up @@ -224,13 +227,6 @@ public class Mistral3TextModelInner: Module {
h = embedTokens(inputs)
}

let offset: Int
if let cache {
offset = cache[0].offset
} else {
offset = 0
}

// Create full attention mask
let faMask = createAttentionMask(h: h, cache: cache?[faIdx])

Expand All @@ -249,12 +245,20 @@ public class Mistral3TextModelInner: Module {
let llama4ScalingBeta = ropeParams["llama_4_scaling_beta"]?.asFloat(),
let originalMaxPosEmbed = ropeParams["original_max_position_embeddings"]?.asInt()
{
attnScale = getLlama4AttentionScale(
start: offset,
stop: offset + inputs.dim(1),
beta: llama4ScalingBeta,
maxPositionEmbeddings: originalMaxPosEmbed
).asType(h.dtype)
switch cache?.first?.ropeOffset ?? .scalar(0) {
case .scalar(let offset):
attnScale = getLlama4AttentionScale(
start: offset,
stop: offset + inputs.dim(1),
beta: llama4ScalingBeta,
maxPositionEmbeddings: originalMaxPosEmbed
).asType(h.dtype)
case .batch(let offsets):
attnScale = batchedLlama4AttentionScale(
offsets: offsets, length: inputs.dim(1), beta: llama4ScalingBeta,
maxPositionEmbeddings: originalMaxPosEmbed
).asType(h.dtype)
}
} else {
attnScale = MLXArray.ones([inputs.dim(1), 1]).asType(h.dtype)
}
Expand Down
31 changes: 18 additions & 13 deletions Libraries/MLXLLM/Models/NemotronLabsDiffusion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,14 @@ import MLXLMCommon
import MLXNN

// MARK: - Llama-4 Attention Scaling
//
// Identical to Mistral3Text.swift's helper but kept private here to keep the
// file self-contained — the existing helper there is `private` so it cannot be
// reused.

private func nlsLlama4AttentionScale(
start: Int, stop: Int, beta: Float, maxPositionEmbeddings: Int
) -> MLXArray {
let positions = arange(start, stop)
var positions = arange(start, stop)
if start < 0 {
positions = MLX.maximum(positions, MLXArray(Int32(0)))
}
let scaling =
1 + beta
* MLX.log(
Expand Down Expand Up @@ -207,8 +206,6 @@ public class NemotronLabsDiffusionEncoder: Module {
) -> MLXArray {
var h = embedTokens(inputs)

let offset = cache?[0].offset ?? 0

let mask: MLXFast.ScaledDotProductAttentionMaskMode
if bidirectional {
// Diffusion mode: full attention.
Expand All @@ -223,12 +220,20 @@ public class NemotronLabsDiffusionEncoder: Module {
let beta = ropeParams["llama_4_scaling_beta"]?.asFloat(),
let origMax = ropeParams["original_max_position_embeddings"]?.asInt()
{
attnScale = nlsLlama4AttentionScale(
start: offset,
stop: offset + inputs.dim(1),
beta: beta,
maxPositionEmbeddings: origMax
).asType(h.dtype)
switch cache?.first?.ropeOffset ?? .scalar(0) {
case .scalar(let offset):
attnScale = nlsLlama4AttentionScale(
start: offset,
stop: offset + inputs.dim(1),
beta: beta,
maxPositionEmbeddings: origMax
).asType(h.dtype)
case .batch(let offsets):
attnScale = batchedLlama4AttentionScale(
offsets: offsets, length: inputs.dim(1), beta: beta,
maxPositionEmbeddings: origMax
).asType(h.dtype)
}
} else {
attnScale = MLXArray.ones([inputs.dim(1), 1]).asType(h.dtype)
}
Expand Down
Loading
Loading