Skip to content
Open
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
39 changes: 39 additions & 0 deletions docs/receipts/inkling2-mini-metal-dspark-units/pack-long-128k.json

Large diffs are not rendered by default.

142 changes: 142 additions & 0 deletions docs/receipts/inkling2-mini-metal-dspark-units/perf-receipt.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
{
"schema": 1,
"pack_id": "tf-receipt-long-128k-v1",
"pack_sha256": "9e3cbcbf9b9fd507f82f45aba18b74da4a0e0365f361ba3cd3aad1522632e998",
"generated_at_utc": "2026-12-05T20:17:04Z",
"claim": "Drafted tokens equal main at 128K context, where the drafter hold falls from 10.55 to 5.63 ms, so drafted decode runs faster than main",
"git": {
"base_sha": "0ca16774654ed7ca0635055cbad0f5f2466a40d8",
"head_sha": "9abef9e218073851a7b85d35323b9d6a193e5394"
},
"environment": {
"chip": "apple-m5-ultra",
"os": "darwin",
"arch": "arm64",
"checkpoint_id": "thinkingmachines/Inkling-2-Mini@fcd43a4241eb71e9f56f5f4d4bb125438cc613d9",
"draft": true,
"base_binary_sha256": "8510e88c6dd0a3a94d93969326d38328e4ffc9b380625002ec10bd087ec588b5",
"head_binary_sha256": "d53ac155defec989b27d1c2b688e174460726b6ae7c15816f85f413969aae8c8"
},
"control": {
"prompt_id": "long_seeded",
"seed_a": 424242,
"seed_b": 989898,
"base_token_sha256": "3d5a4e2989bdc7ad2263ac29d9a4ddf64d1b8bc68f3bc00fe20f2e64bdad0b3c",
"alt_token_sha256": "bd2d7e60dff236122e18d67c998cb4aed0f69448a9a797237790714010486556",
"diverged": true
},
"draft_check": {
"prompt_id": "long_greedy",
"plain_token_sha256": "2282fcdd1d08251251e28073ee0ad3466b138bc4ae458561e14de0caddc7c21e",
"draft_token_sha256": "2282fcdd1d08251251e28073ee0ad3466b138bc4ae458561e14de0caddc7c21e",
"equal": true
},
"order": [
"base",
"head",
"head",
"base",
"base",
"head",
"head",
"base"
],
"rounds": [
{
"index": 0,
"arm": "base",
"agg_tok_s": 38.579
},
{
"index": 1,
"arm": "head",
"agg_tok_s": 41.728
},
{
"index": 2,
"arm": "head",
"agg_tok_s": 41.218
},
{
"index": 3,
"arm": "base",
"agg_tok_s": 39.105
},
{
"index": 4,
"arm": "base",
"agg_tok_s": 39.12
},
{
"index": 5,
"arm": "head",
"agg_tok_s": 41.75
},
{
"index": 6,
"arm": "head",
"agg_tok_s": 41.691
},
{
"index": 7,
"arm": "base",
"agg_tok_s": 39.132
}
],
"summary": {
"mean_base_tok_s": 38.984,
"mean_head_tok_s": 41.597,
"gain_pct": 6.702,
"base_noise_pct": 1.433
},
"prompts": {
"long_greedy": {
"base": {
"tokens": 256,
"prompt_tokens": 131155,
"cached_tokens": 0,
"complete": true,
"token_sha256": "2282fcdd1d08251251e28073ee0ad3466b138bc4ae458561e14de0caddc7c21e",
"output_sha256": "8311dab6bf4f4e914ce16a30ea6f48311d1024824a8f11ce4c3277f60404f223",
"tok_s": 33.693,
"ttft_s": 155.2858
},
"head": {
"tokens": 256,
"prompt_tokens": 131155,
"cached_tokens": 0,
"complete": true,
"token_sha256": "2282fcdd1d08251251e28073ee0ad3466b138bc4ae458561e14de0caddc7c21e",
"output_sha256": "8311dab6bf4f4e914ce16a30ea6f48311d1024824a8f11ce4c3277f60404f223",
"tok_s": 35.612,
"ttft_s": 155.6704
},
"token_equal": true
},
"long_seeded": {
"base": {
"tokens": 256,
"prompt_tokens": 131154,
"cached_tokens": 0,
"complete": true,
"token_sha256": "3d5a4e2989bdc7ad2263ac29d9a4ddf64d1b8bc68f3bc00fe20f2e64bdad0b3c",
"output_sha256": "68adc24a8bbbe4c26b090558caea0415c5a7e8835d7dc28fedca2b572b0ec0d4",
"tok_s": 46.665,
"ttft_s": 155.279
},
"head": {
"tokens": 256,
"prompt_tokens": 131154,
"cached_tokens": 0,
"complete": true,
"token_sha256": "3d5a4e2989bdc7ad2263ac29d9a4ddf64d1b8bc68f3bc00fe20f2e64bdad0b3c",
"output_sha256": "68adc24a8bbbe4c26b090558caea0415c5a7e8835d7dc28fedca2b572b0ec0d4",
"tok_s": 50.272,
"ttft_s": 155.2639
},
"token_equal": true
}
},
"exactness_sha256": "196f5695666ad6befa0d2bc1607d1ecb9330c1e35aa28eb6c1086bb7d384d359",
"receipt_sha256": "74d6b4f9f5cadc7d3527fa74900b04a3870f93a26b2cbc310c3409bab707f114"
}
1 change: 1 addition & 0 deletions zig/kernels/metal/inkling2/dspark_sources.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ const guard = @import("../threadgroup_source.zig");

pub const dspark = guard.normalize(@embedFile("dspark.metal"));
pub const dspark_pre_m5 = guard.normalize("#define TF_SIMD_FRAGS 1\n" ++ dspark);
pub const dspark_units = guard.normalize(@embedFile("../nax.h") ++ @embedFile("dspark_units.metal"));
142 changes: 142 additions & 0 deletions zig/kernels/metal/inkling2/dspark_units.metal
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
//! Mini DSpark's block attention on the tensor units; dspark_sources prepends the tensor-fragment helpers.
#include <metal_stdlib>
using namespace metal;
#pragma METAL fp contract(off)

constant constexpr uint DSU_D = 128, DSU_KV = 1024, DSU_KEYS = 64, DSU_PAIRS = 24, DSU_GROUP = 3;
constant constexpr float DSU_SCALE = 0x1.6a09e6p-4f, DSU_LOG2E = 1.4426950408889634f;
struct DsparkUnitsArgs { uint committed, ring, first, chunk, chunks; };

// The 16x16 block at key row r, column c of a tile in a context ring; a key outside [lo, hi) reads as zero.
inline void dsu_tile_get(thread tfp::frag<bfloat>& f, const device bfloat* tile, int r, int c, short2 home, int lo, int hi) {
const device bfloat* p = tile + (r + home.y) * int(DSU_KV) + (c + home.x);
const bfloat none = bfloat(0);
const bfloat4 zero = bfloat4(none, none, none, none);
const int x = r + home.y, y = x + 8;
f = tfp::frag<bfloat>(x >= lo && x < hi ? bfloat4(*(const device bfloat4*)p) : zero,
y >= lo && y < hi ? bfloat4(*(const device bfloat4*)(p + 8 * int(DSU_KV))) : zero);
}

inline void dsu_rows(thread const tfp::frag<float>* s, thread float (&r)[2], bool maximum) {
for (uint f = 0; f < 4; ++f) for (uint h = 0; h < 2; ++h) {
const uint b = 4 * h;
float v = maximum ? max(max(s[f][b], s[f][b + 1]), max(s[f][b + 2], s[f][b + 3]))
: (s[f][b] + s[f][b + 1]) + (s[f][b + 2] + s[f][b + 3]);
const float x = simd_shuffle_xor(v, ushort(1));
v = maximum ? max(v, x) : v + x;
const float y = simd_shuffle_xor(v, ushort(8));
v = maximum ? max(v, y) : v + y;
r[h] = maximum ? max(r[h], v) : r[h] + v;
}
}

// A key head's 24 (row, head) pairs and one chunk of the retained context a 64-thread group; a simdgroup owns 16 pairs.
[[max_total_threads_per_threadgroup(64)]]
kernel void tf_ink2_dspark_attn_units(const device bfloat* q [[buffer(0)]], const device bfloat* kc [[buffer(1)]],
const device bfloat* vc [[buffer(2)]], device float* part_o [[buffer(3)]], device float2* part_ml [[buffer(4)]],
constant DsparkUnitsArgs& a [[buffer(5)]], uint3 tg [[threadgroup_position_in_grid]],
uint sg [[simdgroup_index_in_threadgroup]], uint lane [[thread_index_in_simdgroup]]) {
const uint kvh = tg.y, b_lo = (a.first / a.chunk + tg.x) * a.chunk, k_hi = min(b_lo + a.chunk, a.committed) - 1;
const uint m = 16 * sg;
const short2 home = tfp::frag_home(ushort(lane));
const uint row_a = m + uint(home.y), row_b = row_a + 8;
const uint pair_a = min(row_a, DSU_PAIRS - 1), pair_b = min(row_b, DSU_PAIRS - 1);
const device bfloat* qb0 = q + ((pair_a / DSU_GROUP) * DSU_PAIRS + kvh * DSU_GROUP + pair_a % DSU_GROUP) * DSU_D;
const device bfloat* qb1 = q + ((pair_b / DSU_GROUP) * DSU_PAIRS + kvh * DSU_GROUP + pair_b % DSU_GROUP) * DSU_D;
tfp::frag<float> acc[8];
for (uint d = 0; d < 8; ++d) acc[d] = tfp::frag<float>(0);
float top[2] = {-INFINITY, -INFINITY}, mass[2] = {0.0f, 0.0f};
for (uint kt = b_lo; kt <= k_hi; kt += DSU_KEYS) {
// A tile is 64 keys from a multiple of 64 and a ring is a multiple of 64 rows, so a tile never wraps its ring.
const device bfloat* tile_k = kc + size_t(kt % a.ring) * DSU_KV + kvh * DSU_D;
const device bfloat* tile_v = vc + size_t(kt % a.ring) * DSU_KV + kvh * DSU_D;
const int lo = a.first > kt ? int(a.first - kt) : 0, hi = int(min(kt + DSU_KEYS, a.committed) - kt);
if (lo >= hi) continue;
tfp::frag<float> score[4] = {tfp::frag<float>(0), tfp::frag<float>(0), tfp::frag<float>(0), tfp::frag<float>(0)};
for (uint d = 0; d < 8; ++d) {
tfp::frag<bfloat> qa, kb0, kb1, kb2, kb3;
tfp::frag_get_rows(qa, qb0, qb1, 16 * d, home, row_a < DSU_PAIRS, row_b < DSU_PAIRS);
dsu_tile_get(kb0, tile_k, 0, 16 * d, home, lo, hi);
dsu_tile_get(kb1, tile_k, 16, 16 * d, home, lo, hi);
dsu_tile_get(kb2, tile_k, 32, 16 * d, home, lo, hi);
dsu_tile_get(kb3, tile_k, 48, 16 * d, home, lo, hi);
tfp::mma_16x32<false, true>(score[0], score[1], qa, kb0, kb1);
tfp::mma_16x32<false, true>(score[2], score[3], qa, kb2, kb3);
}
float peak[2] = {top[0], top[1]};
for (uint f = 0; f < 4; ++f) for (uint e = 0; e < 8; ++e) {
const uint row = m + uint(home.y) + (e >> 2) * 8;
const int key = int(16 * f) + home.x + int(TF_COL(e));
const bool live = row < DSU_PAIRS && key >= lo && key < hi;
score[f][e] = live ? score[f][e] * DSU_SCALE * DSU_LOG2E : -INFINITY;
}
dsu_rows(score, peak, true);
float keep[2];
for (uint h = 0; h < 2; ++h) {
const float mu = peak[h] == -INFINITY ? 0.0f : peak[h];
keep[h] = precise::exp2(top[h] - mu);
top[h] = peak[h]; mass[h] *= keep[h];
}
tfp::frag<bfloat> p[4];
for (uint f = 0; f < 4; ++f) for (uint e = 0; e < 8; ++e) {
const float mu = peak[e >> 2] == -INFINITY ? 0.0f : peak[e >> 2];
p[f][e] = bfloat(precise::exp2(score[f][e] - mu));
}
// Reduce the rounded P, not its pre-bf16 exponential.
tfp::frag<float> rounded[4];
for (uint f = 0; f < 4; ++f) for (uint e = 0; e < 8; ++e) rounded[f][e] = float(p[f][e]);
dsu_rows(rounded, mass, false);
for (uint d = 0; d < 8; ++d) for (uint e = 0; e < 8; ++e) acc[d][e] *= keep[e >> 2];
for (uint f = 0; f < 4; ++f) for (uint d = 0; d < 8; d += 2) {
tfp::frag<bfloat> v0, v1;
dsu_tile_get(v0, tile_v, 16 * f, 16 * d, home, lo, hi);
dsu_tile_get(v1, tile_v, 16 * f, 16 * d + 16, home, lo, hi);
tfp::mma_16x32<false, false>(acc[d], acc[d + 1], p[f], v0, v1);
}
}
for (uint d = 0; d < 8; ++d) for (uint e = 0; e < 8; ++e) {
const uint row = m + uint(home.y) + (e >> 2) * 8, col = uint(home.x) + TF_COL(e);
if (row >= DSU_PAIRS) continue;
const size_t slot = size_t((row / DSU_GROUP) * DSU_PAIRS + kvh * DSU_GROUP + row % DSU_GROUP) * a.chunks + tg.x;
part_o[slot * DSU_D + 16 * d + col] = acc[d][e];
}
if (home.x == 0) for (uint h = 0; h < 2; ++h) {
const uint row = m + uint(home.y) + 8 * h;
if (row >= DSU_PAIRS) continue;
const size_t slot = size_t((row / DSU_GROUP) * DSU_PAIRS + kvh * DSU_GROUP + row % DSU_GROUP) * a.chunks + tg.x;
part_ml[slot] = float2(top[h], mass[h]);
}
}

// A (row, head) a 128-thread group: the block's eight keys, then the context's chunks in order, rounded once to bf16.
[[max_total_threads_per_threadgroup(128)]]
kernel void tf_ink2_dspark_attn_merge(const device bfloat* q [[buffer(0)]], const device bfloat* block_k [[buffer(1)]],
const device bfloat* block_v [[buffer(2)]], const device float* part_o [[buffer(3)]],
const device float2* part_ml [[buffer(4)]], device bfloat* out [[buffer(5)]], constant DsparkUnitsArgs& a [[buffer(6)]],
uint group [[threadgroup_position_in_grid]], uint d [[thread_index_in_threadgroup]]) {
const uint kv = (group % DSU_PAIRS) / DSU_GROUP, q_at = group * DSU_D;
threadgroup float bs[8];
if (d < 8) {
float dot = 0.0f;
for (uint i = 0; i < DSU_D; ++i) dot += float(q[q_at + i]) * float(block_k[d * DSU_KV + kv * DSU_D + i]);
bs[d] = dot * DSU_SCALE * DSU_LOG2E;
}
threadgroup_barrier(mem_flags::mem_threadgroup);
float mm = bs[0];
for (uint j = 1; j < 8; ++j) mm = max(mm, bs[j]);
float ll = 0.0f, o = 0.0f;
for (uint j = 0; j < 8; ++j) {
const float w = float(bfloat(precise::exp2(bs[j] - mm)));
ll += w;
o += w * float(block_v[j * DSU_KV + kv * DSU_D + d]);
}
for (uint c = 0; c < a.chunks; ++c) {
const size_t at = size_t(group) * a.chunks + c;
const float2 ml = part_ml[at];
const float mn = max(mm, ml.x), x = precise::exp2(mm - mn), y = precise::exp2(ml.x - mn);
ll = ll * x + ml.y * y;
o = o * x + part_o[at * DSU_D + d] * y;
mm = mn;
}
out[q_at + d] = bfloat(o / ll);
}
1 change: 1 addition & 0 deletions zig/kernels/metal/inkling2_facade.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub const tree = owner.tree;
pub const small_ops = owner.small_ops;
pub const inkling2_dspark = owner.inkling2_dspark;
pub const inkling2_dspark_pre_m5 = owner.inkling2_dspark_pre_m5;
pub const inkling2_dspark_units = owner.inkling2_dspark_units;
pub const vision = owner.vision;
pub const inkling2_dflash2 = owner.inkling2_dflash2;
pub const sample = owner.sample;
1 change: 1 addition & 0 deletions zig/kernels/metal/inkling2_sources.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub const tree = @import("inkling2/tree_sources.zig").tree;
pub const small_ops = @import("inkling2/small_ops_sources.zig").small_ops;
pub const inkling2_dspark = @import("inkling2/dspark_sources.zig").dspark;
pub const inkling2_dspark_pre_m5 = @import("inkling2/dspark_sources.zig").dspark_pre_m5;
pub const inkling2_dspark_units = @import("inkling2/dspark_sources.zig").dspark_units;
pub const vision = @import("inkling2/vision_sources.zig").vision;
pub const inkling2_dflash2 = @import("inkling2/dflash2_sources.zig").dflash2;
pub const sample = @import("inkling2/sample_sources.zig").sample;
1 change: 1 addition & 0 deletions zig/runtime_sources/completion.zig
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,5 @@ fn inkling2(s: *Set) !void {
try s.fragments("inkling2", "vision", src.vision);
try s.add("inkling2", "inkling2_dflash2", src.inkling2_dflash2, precise());
try s.raw("inkling2", if (s.units) "inkling2_dspark" else "inkling2_dspark_pre_m5", if (s.units) src.inkling2_dspark else src.inkling2_dspark_pre_m5);
if (s.units) try s.raw("inkling2", "inkling2_dspark_units", src.inkling2_dspark_units);
}
18 changes: 18 additions & 0 deletions zig/src/core/contract/inkling2_dspark_units_metal_records.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//! Mini DSpark's tensor-unit attention launches (zig/tests/contract/metal_inkling2_dspark_units.zig).
const std = @import("std");
const M = @import("limits.zig").MetalLaunch;

pub const records = [_]M{
.{ .name = "metal inkling2 dspark_units tf_ink2_dspark_attn_units", .threads = 64, .tg_memory = 0, .reserved = 64, .offline = false },
.{ .name = "metal inkling2 dspark_units tf_ink2_dspark_attn_merge", .threads = 128, .tg_memory = 32, .reserved = 128, .offline = false },
};

test "the tensor-unit attention's launches reserve their threads and carry no M1/M2 compile receipts" {
for (records) |record| {
try std.testing.expectEqual(@as(u32, 0), record.m1);
try std.testing.expectEqual(@as(u32, 0), record.m2);
try std.testing.expectEqual(record.threads, record.reserved);
try std.testing.expect(!record.offline);
try std.testing.expect(std.mem.startsWith(u8, record.name, "metal inkling2 dspark_units "));
}
}
2 changes: 2 additions & 0 deletions zig/src/core/contract/registry_joins.zig
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ pub const all_metal = @import("penalties_records.zig").records ++
@import("inkling2_runtime_metal_records.zig").records ++
@import("inkling2_tree_metal_records.zig").records ++
@import("inkling2_dspark_metal_records.zig").records ++
@import("inkling2_dspark_units_metal_records.zig").records ++
@import("inkling2_vision_metal_records.zig").records ++
@import("inkling2_dflash2_metal_records.zig").records ++
@import("inkling2_sample_metal_records.zig").records ++
Expand Down Expand Up @@ -207,6 +208,7 @@ test {
_ = @import("inkling2_runtime_metal_records.zig");
_ = @import("inkling2_tree_metal_records.zig");
_ = @import("inkling2_dspark_metal_records.zig");
_ = @import("inkling2_dspark_units_metal_records.zig");
_ = @import("inkling2_sample_metal_records.zig");
_ = @import("h3_image_cuda_records.zig");
_ = @import("inkling2_vision_metal_records.zig");
Expand Down
6 changes: 3 additions & 3 deletions zig/src/families/inkling2/metal/admit.zig
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ pub const Demand = struct {
}
};

/// A serve's demand at `window` tokens beside `fixed` bytes of weights, dense layers and scratch.
pub fn demandAt(fixed: u64, window: usize, drafts: bool) Demand {
/// A serve's demand at `window` tokens beside `fixed` bytes; `units` when the drafter holds the tensor-unit store.
pub fn demandAt(fixed: u64, window: usize, drafts: bool, units: bool) Demand {
return .{
.fixed = fixed,
.shared = if (drafts) dspark.Drafter.scoreBytes(window) - dspark.Drafter.scoreBytes(0) else 0,
.shared = if (drafts) dspark.Drafter.scoreBytes(window, units) - dspark.Drafter.scoreBytes(0, units) else 0,
.stream = st.cacheBytes(window) + if (drafts) dspark.Drafter.streamBytes(window) else 0,
};
}
Loading
Loading