Skip to content
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Each release's page on GitHub has its notes and measurements. See [the 1.0.0 rel

## Unreleased

- HIP kernels: Inkling 2's attention at each layer's own bias extent (256 and 128 rows beside Mini's 1,024), zero-padded, with exact extent checks; for tiny-inkling2-mini (#5499, part of #4265). Thanks to @AbrahamCete.

- cuda: select MT2 row-tile decode attention on 170-SM sm_120 cards too (RTX 5090) (#5733). Thanks to @jojomondag.

- Hexagon: a Gemma embedding-scale kernel for Qualcomm Hexagon V75 with a host fixture test and an on-DSP contract runner (`test-hexagon-scale-host`, `hexagon-kernel-contract`); not used by serving. Thanks @tgerighty.
Expand Down
24 changes: 19 additions & 5 deletions docs/kernel-contract.md
Original file line number Diff line number Diff line change
Expand Up @@ -561,11 +561,12 @@ HIP also checks DeepSeek-V4.1's indexer top-512 (`deepseek_v41/topk512.hip`, Met
the CPU's stable sort. Four controls, as on Metal: NaN ranked above +inf (`nonfinite`), ties going to the higher index
and the last visible score dropped (`shapes`), and a write into its input (`repeat`).

HIP also checks Inkling 2's attention at Mini's 24 query and 8 key heads (`inkling2/attention.hip`,
`contract_inkling2*.zig`, records in `hip_records_inkling2.zig`; `tf-hip-kernel-contract image inkling2`). It does not
match #4125's CUDA bits; it keeps CUDA's rounding points and is held to the same float64 reference
(`core.contract.inkling2_attn_ref`, with HIP's scalar fp32 chains): `tf_ink2_prep` (sliding and global, tau at n_floor
8,192), `tf_ink2_rel_table`, and the chunked attention and its merge on sliding layers (`tf_ink2_attn`) and global ones
HIP also checks Inkling 2's attention at Mini's 24 query and 8 key heads and at tiny-inkling2-mini's 4 and 2
(`inkling2/attention.hip`, `contract_inkling2*.zig`, records in `hip_records_inkling2.zig`;
`tf-hip-kernel-contract image inkling2`). It does not match #4125's CUDA bits; it keeps CUDA's rounding points and is
held to the same float64 reference (`core.contract.inkling2_attn_ref`, with HIP's scalar fp32 chains and each call's
bias extent): `tf_ink2_prep` (sliding and global, tau at n_floor 8,192), the bias table (`tf_ink2_rel_table` and its
extents below), and the chunked attention and its merge on sliding layers (`tf_ink2_attn`) and global ones
(`tf_ink2_attn_fold`) to 1M keys on a ring that wraps. Every P is rounded to bf16 before its sum and P V, the sink enters
last as a raw logit and the merge takes the raw gate's sigmoid, rounding once. The split launch (a block a 1,024-key
sub-chunk) and the prompt launch (`tf_ink2_attn_prompt`: 8 pairs a block share one LDS stage while the next tile loads
Expand All @@ -581,6 +582,19 @@ call's rows less one, a call with no streams (only one stream takes the prompt k
fewer than rows x heads x stride partials. It does not refuse a global position past the ring, since the contract's
4,096-slot ring over 1M keys wraps on purpose: the engine glue keeps a global layer's positions inside its ring.

The bias table follows each layer's extent from the checkpoint config (`config.Text.extent`: the window on local
layers, `rel_extent` on global ones). `tf_ink2_rel_table` is built at Mini's 1,024, `tf_ink2_rel_table_e256` and
`tf_ink2_rel_table_e128` at tiny-inkling2-mini's 256 (global) and 128 (local). Each reads `rel_logits_proj` as
[16][extent] and writes a row of 1,024 distances, zeros past its extent, so the attention kernels read one layout at
every extent and keep their code. `relTable()` refuses any other extent (`UnsupportedRelExtent`), and `layerExtent()`
says which layer, its extent and the three built. The float64 reference takes the extent per call (`Call.extent`,
1,024 by default, so CUDA's cases keep their bits). Tiny-mini's registrations run each path at its 4/2 heads: the two
new tables, prep, sliding rows at window 128 (edges at 127 to 129 keys) and across a 256-key chunk, global fold, split
and prompt rows past extent 256 (edges at 255 to 257 keys) and across 1,024-key sub-chunks and 16,384-key chunks, three
streams in one launch bit-equal to each alone, the split and prompt bit-equal to the fold, and the exact-P rows; gate
4's values cancel over its 128-key window. Four more controls go red at `shapes`: each new table read a column late,
global rows given bias past extent 256, and a window one key past 128.

HIP also checks Inkling 2's loop kernels (`inkling2/loop.hip`, `contract_inkling2_loop*.zig`, records in
`hip_records_inkling2_loop.zig`; `tf-hip-kernel-contract image inkling2_loop`), in their own group built with the strict
flags. They are built at two compile-time hidden widths: Inkling 2 Mini's 2,304, whose symbols carry no suffix, and
Expand Down
35 changes: 22 additions & 13 deletions zig/kernels/hip/inkling2/attention.hip
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,29 @@ extern "C" __global__ void __launch_bounds__(32)
for (int i = 0; i < 4; ++i) dst[lane * 4 + i] = tobf(x[i]);
}

// tab[row, head, d] = bf16(rel[row, head, :] . proj[:, d]) for d < 1024, fp32 sums in tap order (rel_logits_proj).
extern "C" __global__ void __launch_bounds__(256)
tf_ink2_rel_table(const uint16_t* __restrict__ rel, const uint16_t* __restrict__ proj,
uint16_t* __restrict__ tab) {
using namespace ink2;
const size_t rh = blockIdx.x;
float r[kRel];
for (int j = 0; j < kRel; ++j) r[j] = bf(rel[rh * kRel + j]);
for (int d = threadIdx.x; d < kExtent; d += blockDim.x) {
float acc = 0.0f;
for (int j = 0; j < kRel; ++j) acc += r[j] * bf(proj[j * kExtent + d]);
tab[rh * kExtent + d] = tobf(acc);
// tab[row, head, d] = bf16(rel[row, head, :] . proj[:, d]) for d < E (proj [16][E], fp32 sums in tap order), else 0.
#define INK2_REL_TABLE(name, E) \
extern "C" __global__ void __launch_bounds__(256) \
name(const uint16_t* __restrict__ rel, const uint16_t* __restrict__ proj, uint16_t* __restrict__ tab) { \
using namespace ink2; \
static_assert(E > 0 && E <= kTabRow); \
const size_t rh = blockIdx.x; \
float r[kRel]; \
for (int j = 0; j < kRel; ++j) r[j] = bf(rel[rh * kRel + j]); \
for (int d = threadIdx.x; d < E; d += blockDim.x) { \
float acc = 0.0f; \
for (int j = 0; j < kRel; ++j) acc += r[j] * bf(proj[j * E + d]); \
tab[rh * kTabRow + d] = tobf(acc); \
} \
if constexpr (E < kTabRow) { \
for (int d = E + static_cast<int>(threadIdx.x); d < kTabRow; d += blockDim.x) tab[rh * kTabRow + d] = 0; \
} \
}
}

// Inkling 2 Mini's extent 1,024 (no suffix) and tiny-inkling2-mini's 256 and 128; hip_attention.zig refuses others.
INK2_REL_TABLE(tf_ink2_rel_table, 1024)
INK2_REL_TABLE(tf_ink2_rel_table_e256, 256)
INK2_REL_TABLE(tf_ink2_rel_table_e128, 128)

// Sliding layers (a chunk a block) and split global calls (a sub-chunk a block, `chunks` partials a row and head).
extern "C" __global__ void __launch_bounds__(ink2::kWaves * 32)
Expand Down
6 changes: 3 additions & 3 deletions zig/kernels/hip/inkling2/tile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace ink2 {

constexpr int kDim = 128;
constexpr int kRel = 16;
constexpr int kExtent = 1024;
constexpr int kTabRow = 1024; // distances a bias-table row holds: a layer's extent fills its first, zeros the rest
constexpr int kKeys = 64;
constexpr int kSub = 1024;
constexpr int kWaves = 4; // pairs a block on the decode paths (tiles of 4 pairs)
Expand Down Expand Up @@ -120,7 +120,7 @@ __device__ __forceinline__ float score(float dot, int kp, int live_lo, int live_
float scale) {
if (kp < live_lo || kp > live_hi) return -INFINITY;
const int d = pos - kp;
const float bias = d < kExtent ? bf(trow[d]) : 0.0f;
const float bias = d < kTabRow ? bf(trow[d]) : 0.0f;
return (dot * scale + bias) * kLog2e;
}

Expand Down Expand Up @@ -191,7 +191,7 @@ __device__ __forceinline__ void attnTile(Ptrs p, const Seg s, int m0, int kvh, i
const size_t prow = static_cast<size_t>(s.row0 + lr) * g.heads + kvh * group + (ok ? mr % group : 0);
float* qw = qs + w * kDim;
for (int i = 0; i < 4; ++i) qw[lane * 4 + i] = ok ? bf(p.q[prow * kDim + lane * 4 + i]) : 0.0f;
const uint16_t* trow = p.tab + prow * kExtent;
const uint16_t* trow = p.tab + prow * kTabRow;
const size_t head_base = s.off + static_cast<size_t>(kvh) * g.ring * kDim;
const int part = FOLD ? kSub : b_hi - b_lo + 1;
const int k_lo = max(b_lo, tile_lo), k_hi = min(b_hi, tile_hi);
Expand Down
8 changes: 5 additions & 3 deletions zig/src/core/contract/hip_records_inkling2.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@ fn launch(comptime symbol: []const u8, threads: u32) limits.HipLaunch {
return .{ .name = "inkling2 " ++ symbol, .symbol = symbol, .image = "inkling2", .threads = threads };
}

pub const records = [6]Record{
pub const records = [8]Record{
.{ .launch = launch("tf_ink2_prep", 32), .usage = .{ .{ .vgprs = 20, .sgprs = 46, .lds = 0, .private = 0, .bound = 32, .wave = 32 }, .{ .vgprs = 20, .sgprs = 46, .lds = 0, .private = 0, .bound = 32, .wave = 32 }, .{ .vgprs = 20, .sgprs = 46, .lds = 0, .private = 0, .bound = 32, .wave = 32 }, .{ .vgprs = 19, .sgprs = 46, .lds = 0, .private = 0, .bound = 32, .wave = 32 } } },
.{ .launch = launch("tf_ink2_rel_table", 256), .usage = .{ .{ .vgprs = 49, .sgprs = 14, .lds = 0, .private = 0, .bound = 256, .wave = 32 }, .{ .vgprs = 54, .sgprs = 18, .lds = 0, .private = 0, .bound = 256, .wave = 32 }, .{ .vgprs = 54, .sgprs = 14, .lds = 0, .private = 0, .bound = 256, .wave = 32 }, .{ .vgprs = 54, .sgprs = 14, .lds = 0, .private = 0, .bound = 256, .wave = 32 } } },
.{ .launch = launch("tf_ink2_rel_table_e256", 256), .usage = .{ .{ .vgprs = 53, .sgprs = 44, .lds = 0, .private = 0, .bound = 256, .wave = 32 }, .{ .vgprs = 56, .sgprs = 37, .lds = 0, .private = 0, .bound = 256, .wave = 32 }, .{ .vgprs = 50, .sgprs = 36, .lds = 0, .private = 0, .bound = 256, .wave = 32 }, .{ .vgprs = 50, .sgprs = 19, .lds = 0, .private = 0, .bound = 256, .wave = 32 } } },
.{ .launch = launch("tf_ink2_rel_table_e128", 256), .usage = .{ .{ .vgprs = 50, .sgprs = 36, .lds = 0, .private = 0, .bound = 256, .wave = 32 }, .{ .vgprs = 56, .sgprs = 20, .lds = 0, .private = 0, .bound = 256, .wave = 32 }, .{ .vgprs = 50, .sgprs = 19, .lds = 0, .private = 0, .bound = 256, .wave = 32 }, .{ .vgprs = 50, .sgprs = 19, .lds = 0, .private = 0, .bound = 256, .wave = 32 } } },
.{ .launch = launch("tf_ink2_attn", 128), .usage = .{ .{ .vgprs = 101, .sgprs = 50, .lds = 35072, .private = 0, .bound = 128, .wave = 32 }, .{ .vgprs = 101, .sgprs = 50, .lds = 35072, .private = 0, .bound = 128, .wave = 32 }, .{ .vgprs = 101, .sgprs = 50, .lds = 35072, .private = 0, .bound = 128, .wave = 32 }, .{ .vgprs = 101, .sgprs = 52, .lds = 35072, .private = 0, .bound = 128, .wave = 32 } } },
.{ .launch = launch("tf_ink2_attn_fold", 128), .usage = .{ .{ .vgprs = 109, .sgprs = 46, .lds = 35072, .private = 0, .bound = 128, .wave = 32 }, .{ .vgprs = 109, .sgprs = 43, .lds = 35072, .private = 0, .bound = 128, .wave = 32 }, .{ .vgprs = 109, .sgprs = 43, .lds = 35072, .private = 0, .bound = 128, .wave = 32 }, .{ .vgprs = 108, .sgprs = 43, .lds = 35072, .private = 0, .bound = 128, .wave = 32 } } },
.{ .launch = launch("tf_ink2_attn_prompt", 256), .usage = .{ .{ .vgprs = 88, .sgprs = 43, .lds = 37120, .private = 0, .bound = 256, .wave = 32 }, .{ .vgprs = 88, .sgprs = 43, .lds = 37120, .private = 0, .bound = 256, .wave = 32 }, .{ .vgprs = 88, .sgprs = 43, .lds = 37120, .private = 0, .bound = 256, .wave = 32 }, .{ .vgprs = 88, .sgprs = 43, .lds = 37120, .private = 0, .bound = 256, .wave = 32 } } },
.{ .launch = launch("tf_ink2_merge", 128), .usage = .{ .{ .vgprs = 18, .sgprs = 32, .lds = 0, .private = 0, .bound = 128, .wave = 32 }, .{ .vgprs = 18, .sgprs = 32, .lds = 0, .private = 0, .bound = 128, .wave = 32 }, .{ .vgprs = 18, .sgprs = 32, .lds = 0, .private = 0, .bound = 128, .wave = 32 }, .{ .vgprs = 15, .sgprs = 38, .lds = 0, .private = 0, .bound = 128, .wave = 32 } } },
};

test "Inkling 2 HIP launch records cover prep, the table, each attention path and the merge on all served chips" {
try std.testing.expectEqual(@as(usize, 6), records.len);
test "Inkling 2 HIP launch records cover prep, each extent's table, each attention path and the merge on all served chips" {
try std.testing.expectEqual(@as(usize, 8), records.len);
try std.testing.expectEqual(@as(usize, 0), try hip_records.check(std.testing.allocator, &records, false));
}
Loading
Loading