Skip to content
Merged
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
21 changes: 21 additions & 0 deletions .claude/board/entries/2026-11-10-int8-direct-packed-b.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# 2026-11-10 — INT8 direct arm on prepacked INT8 B

**Status:** MEASURED (lab). Needs AdaWorldAPI/ndarray#418.

**What.** `dr-isa-stream-lab`'s INT8 arm now reads A in place and takes B
from bands packed once (`PreparedI8B`, `PackedI8B`), through
`ndarray::simd::int8_gemm_packed_strided`. The prepared-operand reuse rule is
one `shape::ReuseKey` for BF16 and INT8.

**Result** (one AMX VM, 41 warm runs, three runs). At 256 fields the GEMM
goes from 78.3–78.8 µs to 45.5–48.1 µs, end to end from 88.9–92.2 µs to
47.5–57.9 µs; at 64 fields from 3.4–3.6 µs to 0.98–1.01 µs.

**Negative result.** One call per 16×16 tile (99–108 µs) was slower than
`gemm_i8` with its pack: per-call tile configuration and lost 2×2 blocking.
The pack only pays to hoist when the whole-matrix kernel takes it.

**Governing question** (`dr-dt64-locality-execution-contract.md`): the VNNI
pack of B was recomputed per operation; it is now done once per binding and
reused under `ReuseKey` (operand identity and revision, selection, frame,
width).
3 changes: 2 additions & 1 deletion .claude/board/entries/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ index row, (3) no duplicate entry id. Checks 1 and 2 are deliberately
opposite directions; the stranding this convention prevents shows up in
exactly one of them, never both.

592 entries, 2026-08-06 .. 2026-11-10.
593 entries, 2026-08-06 .. 2026-11-10.

| date | entry id | finding | file |
|---|---|---|---|
| 2026-11-10 | `lab-resolve-borrow-all` | | [2026-11-10-lab-resolve-borrow-all.md](2026-11-10-lab-resolve-borrow-all.md) |
| 2026-11-10 | `int8-direct-packed-b` | | [2026-11-10-int8-direct-packed-b.md](2026-11-10-int8-direct-packed-b.md) |
| 2026-11-10 | `D-FOUNDRY-PARITY-1` | | [2026-11-10-foundry-parity-1-lab.md](2026-11-10-foundry-parity-1-lab.md) |
| 2026-11-10 | `dt64-lane-borrow-all` | | [2026-11-10-dt64-lane-borrow-all.md](2026-11-10-dt64-lane-borrow-all.md) |
| 2026-11-10 | `dt64-int8-facet-stride` | | [2026-11-10-dt64-int8-facet-stride.md](2026-11-10-dt64-int8-facet-stride.md) |
Expand Down
31 changes: 31 additions & 0 deletions crates/dr-isa-stream-lab/BENCH-SHAPE.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,3 +313,34 @@ three are now fixed:

The FACET STRIDE guard (`facet-stride-guard.sh --diff`) reports 0 hits on
this change. ndarray is unchanged.

## INT8 direct arm on prepacked INT8 B (ndarray#418)

The OPTIMIZATION FOUND above, now built. `int8::direct` reads A in place at
the u8 lanes' pitch and takes B from `PreparedI8B` (one `PackedI8B` band per
16 output columns, packed once) through
`ndarray::simd::int8_gemm_packed_strided`. Reuse follows `ReuseKey`, the one
rule now shared with the BF16 `PreparedB`.

One AMX VM, median of 41 warm runs, three runs
(`results/shape-2026-11-10-int8-direct-run*.txt`):

| Row | 64 fields | 256 fields |
|---|---|---|
| `backend::gemm_i8` (packs B per call) | 3.4–3.6 µs | 78.3–78.8 µs |
| … of which band gather + VNNI pack | 1.9–2.1 µs | 28.6–29.4 µs |
| **direct GEMM (A in place, B prepared once)** | **0.98–1.01 µs** | **45.5–48.1 µs** (348–368 GMAC/s) |
| prepare B once | 6.4–6.7 µs | 105 µs |
| warm end to end, staged arm | 5.7–5.9 µs | 88.9–92.2 µs |
| **warm end to end, direct arm** | **2.9–3.0 µs** | **47.5–57.9 µs** |

The direct arm allocates nothing and copies no A. Preparing B costs about
three `gemm_i8` packs, so it pays once B is reused three times.

**A negative result kept on record.** The first version of this arm called
`int8_tile_gemm_16x16_packed_strided` once per 16×16 tile. It measured 99–108
µs at 256 fields, slower than `gemm_i8` *with* its per-call pack: 256 calls
each pay the tile configuration (~105 ns), and the per-tile calls lose the
2×2 register blocking. Hoisting the pack was not enough; the whole-matrix
kernel had to take the prepacked bands. That is why ndarray#418 adds
`int8_gemm_packed_strided`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
Host: Intel(R) Xeon(R) Processor @ 2.10GHz; 4 logical CPUs; cache (cpu0): L1 Data 48K, L1 Instruction 32K, L2 Unified 2048K, L3 Unified 266240K
Tile tier: AMX TDPBF16PS. Single-threaded. 41 warm runs per row (median).

Sparse demand (256 fields x 256 cols, 40 scattered identities): logical selection 40 · K padded 64 · lanes borrowed 256 · A bytes copied 20480 · dense dot-product steps 512 · staged AMX 382.6 µs

| Metric | 64×64 | 256×256 |
|---|---|---|
| Output tiles / K=32 dot-product steps | 16 / 32 | 256 / 2048 |
| DT64 bind (cold) | 3187 ns · 2 allocs | 8623 ns · 6 allocs |
| Resolve: one check + borrows (warm) | 1256 ns · 0 allocs · 0 B | 1983 ns · 0 allocs · 0 B |
| Resolve: `lane()` per field (warm) | 11.7 µs | 146.5 µs |
| B preparation (VNNI panels) | 6703 ns · 8192 B written · 8320 B alloc | 98.7 µs · 131072 B written · 131584 B alloc |
| Direct AMX (A in place) | 4306 ns · 60.9 GMAC/s · 0 allocs · 0 B tmp | 137.3 µs · 122.2 GMAC/s · 0 allocs · 0 B tmp |
| Direct AMX, pitch 65,568 (not 2^k) | 3759 ns · 69.7 GMAC/s | 120.7 µs · 139.0 GMAC/s |
| Staged AMX (dense A tiles) | 4140 ns · 1 allocs · 2048 B tmp | 127.7 µs · 1 allocs · 8192 B tmp |
| ndarray CBLAS BF16 | 36.4 µs · 23 allocs · 54272 B tmp | 1081.9 µs · 263 allocs · 803840 B tmp |
| Source bytes copied (direct / staged / CBLAS) | 0 / 8192 / 8192 | 0 / 131072 / 131072 |
| CBLAS B materialized | 8192 B | 131072 B |
| FP32 accumulator (C) | 16384 B | 262144 B |
| End-to-end warm (resolve + direct, B reused) | 6528 ns | 147.9 µs |
| End-to-end cold (bind + prepare + resolve + direct) | 23.0 µs | 303.5 µs |
| Warm repetitions (100, per run, one prepare) | 7019 ns | 154.4 µs |
| INT8 resolve (u8 lanes, one check) | 1213 ns | 1960 ns |
| INT8 stage A (region: one copy per lane) | 310 ns · 4096 B | 4700 ns · 65536 B |
| INT8 stage A, per-value walk (baseline) | 3312 ns | 30.8 µs |
| INT8 B for a region, in place from the table | 30 ns · 0 B written | 30 ns · 0 B written |
| INT8 materialize B (row-major i8) | 7564 ns · 4096 B | 124.1 µs · 65536 B |
| INT8 GEMM `backend::gemm_i8` (packs B per call) | 3552 ns · 73.8 GMAC/s · 3 allocs · 3072 B tmp | 78.3 µs · 214.3 GMAC/s · 3 allocs · 12288 B tmp |
| INT8 B band gather + VNNI pack (done inside each gemm_i8 call) | 2009 ns · 57% of the GEMM row | 29.4 µs · 38% of the GEMM row |
| INT8 end-to-end warm (resolve + stage + GEMM) | 5856 ns | 88.9 µs |
| INT8 prepare B once (PackedI8B bands, `ndarray::simd`) | 6637 ns · 4096 B | 105.3 µs · 65536 B |
| INT8 direct GEMM (A in place, prepared B) | 1011 ns · 259.3 GMAC/s · 0 allocs · 0 B of A copied | 48.1 µs · 348.5 GMAC/s · 0 allocs · 0 B of A copied |
| INT8 direct end-to-end warm (resolve + GEMM) | 3013 ns | 57.9 µs |
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
Host: Intel(R) Xeon(R) Processor @ 2.10GHz; 4 logical CPUs; cache (cpu0): L1 Data 48K, L1 Instruction 32K, L2 Unified 2048K, L3 Unified 266240K
Tile tier: AMX TDPBF16PS. Single-threaded. 41 warm runs per row (median).

Sparse demand (256 fields x 256 cols, 40 scattered identities): logical selection 40 · K padded 64 · lanes borrowed 256 · A bytes copied 20480 · dense dot-product steps 512 · staged AMX 385.0 µs

| Metric | 64×64 | 256×256 |
|---|---|---|
| Output tiles / K=32 dot-product steps | 16 / 32 | 256 / 2048 |
| DT64 bind (cold) | 3088 ns · 2 allocs | 8409 ns · 6 allocs |
| Resolve: one check + borrows (warm) | 1181 ns · 0 allocs · 0 B | 2488 ns · 0 allocs · 0 B |
| Resolve: `lane()` per field (warm) | 11.3 µs | 181.1 µs |
| B preparation (VNNI panels) | 6570 ns · 8192 B written · 8320 B alloc | 110.5 µs · 131072 B written · 131584 B alloc |
| Direct AMX (A in place) | 4157 ns · 63.1 GMAC/s · 0 allocs · 0 B tmp | 123.2 µs · 136.2 GMAC/s · 0 allocs · 0 B tmp |
| Direct AMX, pitch 65,568 (not 2^k) | 3701 ns · 70.8 GMAC/s | 119.5 µs · 140.4 GMAC/s |
| Staged AMX (dense A tiles) | 4007 ns · 1 allocs · 2048 B tmp | 113.1 µs · 1 allocs · 8192 B tmp |
| ndarray CBLAS BF16 | 35.7 µs · 23 allocs · 54272 B tmp | 1184.9 µs · 263 allocs · 803840 B tmp |
| Source bytes copied (direct / staged / CBLAS) | 0 / 8192 / 8192 | 0 / 131072 / 131072 |
| CBLAS B materialized | 8192 B | 131072 B |
| FP32 accumulator (C) | 16384 B | 262144 B |
| End-to-end warm (resolve + direct, B reused) | 6254 ns | 131.1 µs |
| End-to-end cold (bind + prepare + resolve + direct) | 21.9 µs | 274.3 µs |
| Warm repetitions (100, per run, one prepare) | 6777 ns | 145.2 µs |
| INT8 resolve (u8 lanes, one check) | 1151 ns | 1979 ns |
| INT8 stage A (region: one copy per lane) | 302 ns · 4096 B | 4704 ns · 65536 B |
| INT8 stage A, per-value walk (baseline) | 3369 ns | 30.4 µs |
| INT8 B for a region, in place from the table | 29 ns · 0 B written | 30 ns · 0 B written |
| INT8 materialize B (row-major i8) | 7343 ns · 4096 B | 125.3 µs · 65536 B |
| INT8 GEMM `backend::gemm_i8` (packs B per call) | 3432 ns · 76.4 GMAC/s · 3 allocs · 3072 B tmp | 78.8 µs · 212.8 GMAC/s · 3 allocs · 12288 B tmp |
| INT8 B band gather + VNNI pack (done inside each gemm_i8 call) | 2149 ns · 63% of the GEMM row | 29.4 µs · 37% of the GEMM row |
| INT8 end-to-end warm (resolve + stage + GEMM) | 5745 ns | 89.0 µs |
| INT8 prepare B once (PackedI8B bands, `ndarray::simd`) | 6444 ns · 4096 B | 105.8 µs · 65536 B |
| INT8 direct GEMM (A in place, prepared B) | 976 ns · 268.6 GMAC/s · 0 allocs · 0 B of A copied | 47.3 µs · 354.7 GMAC/s · 0 allocs · 0 B of A copied |
| INT8 direct end-to-end warm (resolve + GEMM) | 2924 ns | 52.7 µs |
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
Host: Intel(R) Xeon(R) Processor @ 2.10GHz; 4 logical CPUs; cache (cpu0): L1 Data 48K, L1 Instruction 32K, L2 Unified 2048K, L3 Unified 266240K
Tile tier: AMX TDPBF16PS. Single-threaded. 41 warm runs per row (median).

Sparse demand (256 fields x 256 cols, 40 scattered identities): logical selection 40 · K padded 64 · lanes borrowed 256 · A bytes copied 20480 · dense dot-product steps 512 · staged AMX 388.9 µs

| Metric | 64×64 | 256×256 |
|---|---|---|
| Output tiles / K=32 dot-product steps | 16 / 32 | 256 / 2048 |
| DT64 bind (cold) | 3179 ns · 2 allocs | 8882 ns · 6 allocs |
| Resolve: one check + borrows (warm) | 1210 ns · 0 allocs · 0 B | 1991 ns · 0 allocs · 0 B |
| Resolve: `lane()` per field (warm) | 11.7 µs | 151.3 µs |
| B preparation (VNNI panels) | 6703 ns · 8192 B written · 8320 B alloc | 109.0 µs · 131072 B written · 131584 B alloc |
| Direct AMX (A in place) | 4314 ns · 60.8 GMAC/s · 0 allocs · 0 B tmp | 170.5 µs · 98.4 GMAC/s · 0 allocs · 0 B tmp |
| Direct AMX, pitch 65,568 (not 2^k) | 3804 ns · 68.9 GMAC/s | 152.2 µs · 110.2 GMAC/s |
| Staged AMX (dense A tiles) | 4232 ns · 1 allocs · 2048 B tmp | 146.4 µs · 1 allocs · 8192 B tmp |
| ndarray CBLAS BF16 | 36.8 µs · 23 allocs · 54272 B tmp | 1527.3 µs · 263 allocs · 803840 B tmp |
| Source bytes copied (direct / staged / CBLAS) | 0 / 8192 / 8192 | 0 / 131072 / 131072 |
| CBLAS B materialized | 8192 B | 131072 B |
| FP32 accumulator (C) | 16384 B | 262144 B |
| End-to-end warm (resolve + direct, B reused) | 6474 ns | 145.1 µs |
| End-to-end cold (bind + prepare + resolve + direct) | 18.3 µs | 295.9 µs |
| Warm repetitions (100, per run, one prepare) | 6384 ns | 169.6 µs |
| INT8 resolve (u8 lanes, one check) | 1188 ns | 1941 ns |
| INT8 stage A (region: one copy per lane) | 309 ns · 4096 B | 4742 ns · 65536 B |
| INT8 stage A, per-value walk (baseline) | 3424 ns | 29.3 µs |
| INT8 B for a region, in place from the table | 30 ns · 0 B written | 30 ns · 0 B written |
| INT8 materialize B (row-major i8) | 7569 ns · 4096 B | 125.4 µs · 65536 B |
| INT8 GEMM `backend::gemm_i8` (packs B per call) | 3553 ns · 73.8 GMAC/s · 3 allocs · 3072 B tmp | 78.5 µs · 213.8 GMAC/s · 3 allocs · 12288 B tmp |
| INT8 B band gather + VNNI pack (done inside each gemm_i8 call) | 1909 ns · 54% of the GEMM row | 28.6 µs · 37% of the GEMM row |
| INT8 end-to-end warm (resolve + stage + GEMM) | 5878 ns | 92.2 µs |
| INT8 prepare B once (PackedI8B bands, `ndarray::simd`) | 6659 ns · 4096 B | 105.4 µs · 65536 B |
| INT8 direct GEMM (A in place, prepared B) | 1006 ns · 260.6 GMAC/s · 0 allocs · 0 B of A copied | 45.5 µs · 368.3 GMAC/s · 0 allocs · 0 B of A copied |
| INT8 direct end-to-end warm (resolve + GEMM) | 3035 ns | 47.5 µs |
46 changes: 42 additions & 4 deletions crates/dr-isa-stream-lab/src/bin/shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,18 +376,19 @@ fn main() {
assert_eq!(c8, want8);
// The B packing gemm_i8 does inside every call, timed on its own:
// per 16-column band, gather the band (K x 16) and VNNI-pack it with
// ndarray's own `vnni_pack_i8` (int8_gemm_amx_tiled_rb's steps, minus
// its three buffer allocations, which the GEMM row reports).
// `ndarray::simd::PackedI8B::pack_from` (the same layout and loop
// int8_gemm_amx_tiled_rb packs with, minus its three buffer
// allocations, which the GEMM row reports).
let k8 = r8.k_pad;
assert_eq!(b8.len(), k8 * dim, "B materialized row-major");
let (mut band, mut packed) = (vec![0i8; k8 * 16], vec![0i8; k8 * 16]);
let (mut band, mut packed) = (vec![0i8; k8 * 16], ndarray::simd::PackedI8B::zeroed(k8));
let t8_pack = median_ns(|| {
for j in (0..dim).step_by(16) {
for kk in 0..k8 {
band[kk * 16..kk * 16 + 16]
.copy_from_slice(&b8[kk * dim + j..kk * dim + j + 16]);
}
ndarray::hpc::amx_matmul::vnni_pack_i8(&band, &mut packed, k8, 16);
packed.pack_from(&band);
black_box(&packed);
}
});
Expand All @@ -397,6 +398,24 @@ fn main() {
});
assert_eq!(c8, want8);
let (a8_allocs, m8_gemm, _) = mem(|| int8::gemm(&o8, &a8, &b8, &mut c8));
// The direct arm: A read in place at the lane pitch, B prepared once
// into PackedI8B bands (ndarray's INT8 twins of #414).
let prep8 = int8::PreparedI8B::prepare(&o8, &bt).unwrap();
let mut d8 = vec![0i32; o8.c_len()];
int8::direct(&o8, &l8, &prep8, &bt, &mut d8).unwrap();
assert_eq!(d8, want8, "int8 direct {dim}");
let t8_prep = median_ns(|| {
black_box(int8::PreparedI8B::prepare(&o8, &bt).unwrap());
});
let t8_direct = median_ns(|| {
int8::direct(&o8, &l8, &prep8, &bt, &mut d8).unwrap();
});
let t8_direct_all = median_ns(|| {
let l = int8::resolve(&o8, &world, &u8s).unwrap();
int8::direct(&o8, &l, &prep8, &bt, &mut d8).unwrap();
});
assert_eq!(d8, want8);
let (d8_allocs, _, _) = mem(|| int8::direct(&o8, &l8, &prep8, &bt, &mut d8).unwrap());
push("INT8 resolve (u8 lanes, one check)", col, fmt(t8_resolve));
push(
"INT8 stage A (region: one copy per lane)",
Expand Down Expand Up @@ -441,6 +460,25 @@ fn main() {
col,
fmt(t8_all),
);
push(
"INT8 prepare B once (PackedI8B bands, `ndarray::simd`)",
col,
format!("{} · {} B", fmt(t8_prep), prep8.bytes()),
);
push(
"INT8 direct GEMM (A in place, prepared B)",
col,
format!(
"{} · {:.1} GMAC/s · {d8_allocs} allocs · 0 B of A copied",
fmt(t8_direct),
macs / t8_direct
),
);
push(
"INT8 direct end-to-end warm (resolve + GEMM)",
col,
fmt(t8_direct_all),
);
}

// Sparse demand: 40 scattered identities, 256 fields x 256 columns.
Expand Down
Loading
Loading