Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ struct PagedAttentionKernelKey: Hashable {
var partitionTokens: Int
/// Pass A only: fuse the decode KV write into the kernel.
var hasWrite: Bool = false
/// Pass A only: merge-record heads per round of the side-by-side form
/// (`paged_attention_part_side_impl`); 0 is the plain form.
var sideHeadsPerRound: Int = 0

var kernelName: String {
let d: String
Expand All @@ -72,6 +75,7 @@ struct PagedAttentionKernelKey: Hashable {
+ "_n\(simdgroups)_t\(partitionTokens)"
+ "_sink\(hasSinks ? 1 : 0)_cap\(hasSoftcap ? 1 : 0)"
+ (hasWrite ? "_w1" : "")
+ (sideHeadsPerRound > 0 ? "_side\(sideHeadsPerRound)" : "")
}
}

Expand Down Expand Up @@ -126,6 +130,45 @@ enum PagedAttentionMSL {
thread_index_in_simdgroup);
"""

/// Side-by-side pass A (`paged_attention_part_side_impl`): one
/// threadgroup holds every head group of a kv head. Same inputs and
/// outputs as `partBody` / `partBodyNoWrite`.
static let partBodySide: String = """
const int kvh = kcache_shape[1];
const int maxp = tables_shape[1];
const int maxpart = threadgroups_per_grid.z;
if (thread_position_in_grid.x == 0 && thread_position_in_grid.y == 0
&& thread_position_in_grid.z == 0) {
fence[0] = wfence[0] + 1;
}
threadgroup float q_smem[GQA * D];
threadgroup float red_smem[NSG * HRED * (D + 2)];
const cbv2::PagedSlabAccessor<T> cache{kcache, vcache, (size_t)kvh * S * D};
cbv2::paged_attention_part_side_impl<T, D, S, GQA, HPT, NSG, PTOK, HRED, true, HAS_SOFTCAP>(
q, knew, vnew, cache, tables, seqinfo, params,
kvh, maxp, maxpart, q_smem, red_smem, partials, meta,
threadgroup_position_in_grid,
thread_position_in_threadgroup,
simdgroup_index_in_threadgroup,
thread_index_in_simdgroup);
"""

static let partBodySideNoWrite: String = """
const int kvh = kcache_shape[1];
const int maxp = tables_shape[1];
const int maxpart = threadgroups_per_grid.z;
threadgroup float q_smem[GQA * D];
threadgroup float red_smem[NSG * HRED * (D + 2)];
const cbv2::PagedSlabAccessor<T> cache{kcache, vcache, (size_t)kvh * S * D};
cbv2::paged_attention_part_side_impl<T, D, S, GQA, HPT, NSG, PTOK, HRED, false, HAS_SOFTCAP>(
q, q, q, cache, tables, seqinfo, params,
kvh, maxp, maxpart, q_smem, red_smem, partials, meta,
threadgroup_position_in_grid,
thread_position_in_threadgroup,
simdgroup_index_in_threadgroup,
thread_index_in_simdgroup);
"""

/// Bulk in-place KV write: scatters `[KVH, N, D]` tiles into the slabs
/// and emits the next fence in the group's write chain.
static let writeBody: String = """
Expand Down Expand Up @@ -462,6 +505,68 @@ public enum PagedAttentionKernel {
return nil
}

/// Threads of one side-by-side threadgroup at most: the measured shapes
/// (27B full attention, Gemma 4 global layers) run 512.
static let sideBySideMaxThreads = 512

/// Exact threadgroup bytes of the side-by-side pass A: every query head
/// of the kv head staged, plus NSG merge records of `headsPerRound` heads.
public static func sideBySideThreadgroupBytes(
headDim: Int, gqa: Int, simdgroups: Int, headsPerRound: Int
) -> Int {
let qSmem = gqa * headDim
let redSmem = simdgroups * headsPerRound * (headDim + mergeRecordMetaFloats)
return (qSmem + redSmem) * MemoryLayout<Float32>.size
}

/// Merge-record heads per round (the kernel's HRED) of the side-by-side
/// pass A for one shape: the largest divisor of `headsPerThreadgroup`
/// whose buffers fit the threadgroup-memory cap. Nil when a kv head's
/// query heads already share one threadgroup, or the form does not fit.
public static func sideBySideHeadsPerRound(headDim: Int, gqa: Int) -> Int? {
guard ineligibilityReason(headDim: headDim, gqa: gqa) == nil,
let nsg = simdgroupsPerThreadgroup(headDim: headDim, gqa: gqa)
else { return nil }
let hpt = headsPerThreadgroup(headDim: headDim, gqa: gqa)
let splits = gqa / hpt
guard splits > 1, 32 * nsg * splits <= sideBySideMaxThreads else { return nil }
return stride(from: hpt, through: 1, by: -1).first {
hpt % $0 == 0
&& sideBySideThreadgroupBytes(
headDim: headDim, gqa: gqa, simdgroups: nsg, headsPerRound: $0)
<= threadgroupMemoryLimit
}
}

/// Attended length from which decode takes the side-by-side pass A; 0 is
/// off, the default. Both forms give the same bits; the side-by-side form
/// reads a partition's keys and values once per kv head instead of once
/// per head group. Per attention layer it was faster at long context on an
/// M5 Ultra (1.08x for the 27B shape and 1.22x for Gemma 4 global layers at
/// 65,536 tokens, slower for Gemma below 32,768), but whole decode steps on
/// an M5 Max were not faster, so it stays opt-in until the engine is timed:
/// `DARKBLOOM_CBV2_PAGED_SIDE_BY_SIDE_TOKENS=49152` takes it from that length.
public static let sideBySideMinimumTokens = sideBySideMinimum(
environment: ProcessInfo.processInfo.environment)
static let sideBySideEnvironmentKey = "DARKBLOOM_CBV2_PAGED_SIDE_BY_SIDE_TOKENS"
static let sideBySideDefaultTokens = 0

static func sideBySideMinimum(environment: [String: String]) -> Int {
guard let raw = environment[sideBySideEnvironmentKey],
let value = Int(raw.trimmingCharacters(in: .whitespaces)), value >= 0
else { return sideBySideDefaultTokens }
return value
}

/// HRED of the side-by-side pass A for one decode dispatch, or nil for
/// the plain form.
static func sideBySideForDispatch(
headDim: Int, gqa: Int, maxAttendLength: Int, minimumTokens: Int
) -> Int? {
guard minimumTokens > 0, maxAttendLength >= minimumTokens else { return nil }
return sideBySideHeadsPerRound(headDim: headDim, gqa: gqa)
}

private final class KernelCache: @unchecked Sendable {
private var kernels: [PagedAttentionKernelKey: MLXFast.MLXFastKernel] = [:]
private let lock = NSLock()
Expand All @@ -487,7 +592,8 @@ public enum PagedAttentionKernel {
"params", "wfence",
],
outputNames: ["partials", "meta", "fence"],
source: PagedAttentionMSL.partBody,
source: key.sideHeadsPerRound > 0
? PagedAttentionMSL.partBodySide : PagedAttentionMSL.partBody,
header: source,
ensureRowContiguous: true,
mutableInputs: ["kcache", "vcache"]
Expand All @@ -499,7 +605,8 @@ public enum PagedAttentionKernel {
"q", "kcache", "vcache", "tables", "seqinfo", "params", "wfence",
],
outputNames: ["partials", "meta"],
source: PagedAttentionMSL.partBodyNoWrite,
source: key.sideHeadsPerRound > 0
? PagedAttentionMSL.partBodySideNoWrite : PagedAttentionMSL.partBodyNoWrite,
header: source,
ensureRowContiguous: true
)
Expand Down Expand Up @@ -648,7 +755,8 @@ public enum PagedAttentionKernel {
pageSize: Int,
writeFence: MLXArray,
kernelSource: String,
stream: StreamOrDevice = .default
stream: StreamOrDevice = .default,
sideBySideMinimumTokens: Int = PagedAttentionKernel.sideBySideMinimumTokens
) throws(CBv2PagedKVWriteError) -> (out: MLXArray, nextWriteFence: MLXArray?) {
var q = queries
if q.ndim == 4 {
Expand Down Expand Up @@ -714,29 +822,36 @@ public enum PagedAttentionKernel {
// `ceilingDivide`, not `(a + b - 1) / b`: `maxAttendLength` is
// caller-supplied, and this is the site that motivated the helper.
let maxParts = ceilingDivide(maxAttendLength, ptok)
let side = sideBySideForDispatch(
headDim: headDim, gqa: gqa, maxAttendLength: maxAttendLength,
minimumTokens: sideBySideMinimumTokens)
let partKey = PagedAttentionKernelKey(
pass: .part, dtype: dtype, headDim: headDim, pageSize: pageSize, gqa: gqa,
simdgroups: nsg, hasSinks: false, hasSoftcap: softcap,
partitionTokens: ptok, hasWrite: hasWrite)
partitionTokens: ptok, hasWrite: hasWrite, sideHeadsPerRound: side ?? 0)
let tg = 32 * nsg
var partTemplate: [(String, any KernelTemplateArg)] = [
("T", dtype),
("D", headDim),
("S", pageSize),
("GQA", gqa),
("HPT", hpt),
("NSG", nsg),
("PTOK", ptok),
("HAS_SOFTCAP", softcap),
]
if let side { partTemplate.append(("HRED", side)) }
// MLX passes arrays smaller than 8 elements in Metal's constant address
// space; the merge implementation expects a device pointer. Keep even
// a one-head/one-partition probe on the same legal interface.
let metaShape = [max(8, b * queryHeads * maxParts * 2)]
// Same grid either way: the side-by-side form's threadgroup holds all
// `splits` head groups of a kv head.
let partOut = kernel(for: partKey, source: kernelSource)(
partInputs,
template: [
("T", dtype),
("D", headDim),
("S", pageSize),
("GQA", gqa),
("HPT", hpt),
("NSG", nsg),
("PTOK", ptok),
("HAS_SOFTCAP", softcap),
],
template: partTemplate,
grid: (kvHeads * splits * tg, b, maxParts),
threadGroup: (tg, 1, 1),
threadGroup: (side == nil ? tg : tg * splits, 1, 1),
outputShapes: hasWrite
? [[b, queryHeads, maxParts, headDim], metaShape, [1]]
: [[b, queryHeads, maxParts, headDim], metaShape],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,28 @@ enum PagedSegmentAttention {
"""
}

/// `body` with the side-by-side pass A: one threadgroup holds every head
/// group of a kv head (`PagedAttentionKernel.sideBySideHeadsPerRound`).
static func sideBody(bindings: Int) -> String {
let pointers = (0 ..< bindings).map { "segment\($0)" }.joined(separator: ", ")
return """
const device int32_t* record = records + threadgroup_position_in_grid.y * STRIDE;
const cbv2::PagedSegmentAccessor<T, SEGMENTS> cache{
{\(pointers)}, value_offsets, record, (size_t)KVH * S * D};
const uint3 position(threadgroup_position_in_grid.x, record[0], record[1]);
if (thread_position_in_grid.x == 0 && thread_position_in_grid.y == 0) {
fence[0] = previous[0] + 1;
}
threadgroup float q_smem[GQA * D];
threadgroup float red_smem[NSG * HRED * (D + 2)];
cbv2::paged_attention_part_side_impl<T, D, S, GQA, HPT, NSG, PTOK, HRED, HAS_WRITE, HAS_SOFTCAP>(
q, knew, vnew, cache, seqinfo, seqinfo, params, KVH, 0, record[6],
q_smem, red_smem, partials, meta,
position, thread_position_in_threadgroup, simdgroup_index_in_threadgroup,
thread_index_in_simdgroup);
"""
}

private static func kernel(
key: PagedAttentionKernelKey, bindings: Int, kvHeads: Int,
source: String, allocatesScratch: Bool = false
Expand All @@ -50,7 +72,9 @@ enum PagedSegmentAttention {
name: name,
inputNames: inputNames(bindings: bindings, allocatesScratch: allocatesScratch),
outputNames: ["fence"] + (allocatesScratch ? ["partials", "meta"] : []),
source: body(bindings: bindings), header: source, ensureRowContiguous: true,
source: key.sideHeadsPerRound > 0
? sideBody(bindings: bindings) : body(bindings: bindings),
header: source, ensureRowContiguous: true,
mutableInputs: (allocatesScratch ? [] : ["partials", "meta"])
+ (key.hasWrite ? (0 ..< bindings).map { "segment\($0)" } : []))
kernels[name] = made
Expand Down Expand Up @@ -83,7 +107,8 @@ enum PagedSegmentAttention {
sinks: MLXArray?, params: MLXArray, softcap: Bool, source: String,
dispatchCache: PagedSegmentDispatchCache? = nil,
zeroFilledScratch: Bool = false,
seqinfoCache: PagedSegmentSeqinfoCache? = nil
seqinfoCache: PagedSegmentSeqinfoCache? = nil,
sideBySideMinimumTokens: Int = PagedAttentionKernel.sideBySideMinimumTokens
) -> MLXArray {
var q = queries.ndim == 4 ? queries.squeezed(axis: 2) : queries
guard !group.writeValidation.isFaulted else { return q }
Expand Down Expand Up @@ -138,10 +163,14 @@ enum PagedSegmentAttention {
var partials: MLXArray? =
zeroFilledScratch ? MLXArray.zeros(partialsShape, dtype: .float32) : nil
var meta: MLXArray? = zeroFilledScratch ? MLXArray.zeros([metaCount], dtype: .float32) : nil
let side = PagedAttentionKernel.sideBySideForDispatch(
headDim: d, gqa: gqa, maxAttendLength: maxLength,
minimumTokens: sideBySideMinimumTokens)
let partKey = PagedAttentionKernelKey(
pass: .part, dtype: dtype, headDim: d, pageSize: group.pageSize, gqa: gqa,
simdgroups: nsg, hasSinks: false, hasSoftcap: softcap,
partitionTokens: ptok, hasWrite: newKeys != nil)
partitionTokens: ptok, hasWrite: newKeys != nil, sideHeadsPerRound: side ?? 0)
let threads = side == nil ? 32 * nsg : 32 * nsg * splits
for (bucket, metadata) in zip(plan.buckets, prepared.metadata) {
let segments = bucket.segmentIDs.map { group.segments[$0]! }
let bindingCount = bucket.bindingClass
Expand All @@ -154,26 +183,27 @@ enum PagedSegmentAttention {
[q, k, v] + backing + [
seqinfo, params, group.writeFence, metadata.valueOffsets, metadata.records,
]
let template: [(String, any KernelTemplateArg)] = [
var template: [(String, any KernelTemplateArg)] = [
("T", dtype), ("D", d), ("S", group.pageSize),
("KVH", kvh), ("GQA", gqa), ("HPT", hpt), ("NSG", nsg),
("PTOK", ptok), ("HAS_WRITE", newKeys != nil),
("HAS_SOFTCAP", softcap), ("SEGMENTS", bindingCount),
("STRIDE", PagedSegmentDispatchPlan.recordStride),
]
if let side { template.append(("HRED", side)) }
let grid = (kvh * splits * 32 * nsg, bucket.workCount, 1)
if let partials, let meta {
group.writeFence =
kernel(
key: partKey, bindings: bindingCount, kvHeads: kvh, source: source)(
inputs + [partials, meta], template: template, grid: grid,
threadGroup: (32 * nsg, 1, 1), outputShapes: [[1]], outputDTypes: [.int32])[
threadGroup: (threads, 1, 1), outputShapes: [[1]], outputDTypes: [.int32])[
0]
} else {
let outputs = kernel(
key: partKey, bindings: bindingCount, kvHeads: kvh, source: source,
allocatesScratch: true)(
inputs, template: template, grid: grid, threadGroup: (32 * nsg, 1, 1),
inputs, template: template, grid: grid, threadGroup: (threads, 1, 1),
outputShapes: [[1], partialsShape, [metaCount]],
outputDTypes: [.int32, .float32, .float32])
group.writeFence = outputs[0]
Expand Down
Loading
Loading