Skip to content

Schedule-driven attention: parallel heads and copy-free K/V (SKaiNET SKEEP-005) - #419

Merged
michalharakal merged 4 commits into
developfrom
feature/attention-schedule
Sep 7, 2026
Merged

michalharakal merged 4 commits into
developfrom
feature/attention-schedule

Conversation

@michalharakal

Copy link
Copy Markdown
Contributor

Summary

MultiHeadAttention becomes the first transformer-level consumer of the engine's new Schedule (SKaiNET SKEEP-005): attention heads (or GQA groups) run as parallel tasks under ctx.schedule, bit-identical to the sequential path, and the positional KV caches hand the kernel copy-free views instead of copying the whole prefix per layer per token. The DSL is unchanged.

Design record: docs/specs/attention-schedule.md.

What changed

  • AttentionSchedulePolicy (Sequential / PerHead / PerKVGroup / Auto, default Auto) + HeadPlan; per-layer overrides mha.schedule / mha.schedulePolicy, tree helper Module.configureAttention(...), ATTENTION.schedulePolicy { } DSL clause.
  • ScalarHeadAttentionKernel — decode keeps the exact fused rounding order the golden gates were validated on; prefill uses the engine SDPA order, so the fused path now also covers batched prefill and sliding-window layers (repeatKVHeads / permute / reshape leave the hot path). Masking by loop bounds; an empty sliding-window band reproduces the engine's uniform softmax.
  • KVBufferView + KVCache.updateInPlace for PositionalKVCache, SharedPositionalKVCache, PaddedSharedPositionalKVCache, OwnerReadOnlyKVCache, SharedKVCache (best-effort for AppendKVCache); null while recording, so tracing/compile stay on the tensor-op path.
  • DecoderKVCacheKind (APPEND default, POSITIONAL), decoderTransformerNetwork(kvCacheKind = …), ATTENTION.positionalKvCache(...), LlamaNetworkLoader / QwenNetworkLoader.withKVCacheKind(...) and fromWeights(weights, kvCacheKind = …).
  • Worker rules (no ctx/ops/allocation/PhaseProfile inside the region) documented in the spec and the KDoc.
  • Also on the branch: refreshed apertus API dump (records the binary break from c08d05d, apiCheck was red on develop) and a tutorial xref fix.

Docs

  • Explanation Parallel Attention Heads via Schedules, tutorial Parallel Attention — Getting Started, CHANGELOG, README line.

Numbers (AttentionScheduleSpeedProfile, Llama-3.2-1B Q8_0, 512-token prefill + 32 decode, i7-9750H)

Schedule / KV cache attn.fused_compute attn.kvcache decode tok/s
Sequential / append (0.53.0) 8,991 ms 447 ms 7.7
hardware / append 2,773 ms 369 ms 8.7
hardware / positional 2,590 ms 10 ms 9.7

Daily-StandAPP acceptance (3B Q4_K_M, 697-token prompt): decode 1.9 → 3.0 tok/s, prefill 243 → 211 s, decode slab overflow 128 MB → 0, greedy summaries byte-identical.

Verification

  • MultiHeadAttentionScheduleParityTest (shuffled-pool schedule == sequential bit-for-bit; fused prefill == general SDPA bit-for-bit; in-place == copied views; sliding-window == general path), KVCacheInPlaceViewTest; all transformer-core / llm-core suites green.
  • Golden gates under SKAINET_ATTN_SCHEDULE=sequential|parallel × SKAINET_KV_CACHE=append|positional: Llama-3.2-1B, Qwen2.5-0.5B, Qwen3-1.7B (Q8_0) match mainline llama.cpp.
  • apiCheck green; dumps additive for transformer-core, llm-core, llama, qwen.
  • Antora site builds without errors.

Dependencies

Requires the engine branch SKaiNET-developers/SKaiNET feature/skeep-005-schedules (build with -PuseLocalSkainet=true until engine 0.54.0 is published; lock-step release).

Closes #412, closes #413.

🤖 Generated with Claude Code

michalharakal and others added 4 commits September 4, 2026 00:08
…SKEEP-005)

MultiHeadAttention becomes the first consumer of the engine's Schedule
(ExecutionContext.schedule, SKEEP-005): the fused attention kernel runs one
task per head or GQA group under ctx.schedule, bit-identical to the
sequential path, and now also covers batched prefill and sliding-window
layers. PositionalKVCache and its wrappers hand the kernel a copy-free
KVBufferView; Llama/Qwen opt in with withKVCacheKind(POSITIONAL).

- transformer-core: AttentionSchedulePolicy (Sequential/PerHead/PerKVGroup/
  Auto) + HeadPlan; ScalarHeadAttentionKernel (decode keeps the legacy
  fused rounding order, prefill the engine SDPA order; empty sliding-window
  bands reproduce the engine's uniform softmax); KVBufferView and
  KVCache.updateInPlace overrides (null while recording);
  ATTENTION.positionalKvCache / schedulePolicy DSL clauses;
  Module.configureAttention.
- llm-core: DecoderKVCacheKind, decoderTransformerNetwork(kvCacheKind).
- llama/qwen: withKVCacheKind, fromWeights(kvCacheKind); golden gates honour
  SKAINET_ATTN_SCHEDULE / SKAINET_KV_CACHE; AttentionScheduleSpeedProfile
  (opt-in) measures all four combinations and asserts identical greedy
  tokens.
- Tests: MultiHeadAttentionScheduleParityTest, KVCacheInPlaceViewTest.
  Llama-3.2-1B golden gate green under sequential/append and
  parallel/positional. Profile at 512 ctx: attn.fused_compute 8.99 s ->
  2.59 s, attn.kvcache 447 -> 10 ms, decode 7.7 -> 9.7 tok/s.
- Docs: docs/specs/attention-schedule.md, explanation + tutorial pages,
  CHANGELOG, README. API dumps refreshed (additive only).

Requires SKaiNET feature/skeep-005-schedules (-PuseLocalSkainet=true) until
engine 0.54.0. Closes #412, closes #413.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018sqoaGs5M7C5uVw6cpzAnH
- llm-inference/apertus: the dump predates c08d05d, which replaced the
  1-arg ApertusSafeTensorsLoader constructor by (String, DTypePolicy)
  with a default — apiCheck failed on develop. NOTE: the dump diff removes
  the old 1-arg <init>; that binary break happened in c08d05d, this commit
  only records it.
- qwen-tool-calling tutorial: `tool-calling.adoc` lives under tutorials/,
  Antora resolved the bare name against the module root.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018sqoaGs5M7C5uVw6cpzAnH
@michalharakal
michalharakal merged commit 0305c62 into develop Sep 7, 2026
4 checks passed
@michalharakal
michalharakal deleted the feature/attention-schedule branch September 7, 2026 20:52
@michalharakal michalharakal mentioned this pull request Sep 7, 2026
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant