This repository has been archived on 2026-11-24. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
radiance-sycl/gemma4k/g4k.h
T
AshlynandClaude Opus 5.5 833173cc04 gemma4k: Gemma 4's kernels beyond libr4d -- paged attention, pair_argmax, the sandwich joins
A kernel library beside libr4d, gfx12 only, and AHEAD of it for Gemma 4:
`--kernels gemma4k,libr4d,...`. Its own library rather than rows in libr4d: a smaller change,
its own gfx12 gating, and no churn in libr4d's row table; folding it in can follow.

- attn_paged at head_dim 256 and 512, gqa 1/2/4/8, bf16 or fp8 cache, any window, causal or not.
  One scalar kernel serves decode, verify and prefill, with a split-KV path whose merge is one
  wave a row (live pieces found by ballot, four at a time; a piece that saw no key writes only
  its max and sum); a WMMA prefill kernel takes a query of at least 64 rows and 32 a sequence on
  average -- Q and K bf16, P and V f16 (bf16 P measured 0.25% of the V range in error), the
  denominator summed from the rounded P. RADIANCE_G4K_WMMA=0 keeps every launch on the scalar
  kernel.
- pair_argmax, device and host, described as libref describes it so rad-kbench checks both.
- norm_add_norm and join_add_scale, ops of its own (docs/OPS.md, a new subsection): each one
  kernel for a post-norm / residual add / pre-norm chain, rounding where the chain rounds. libref
  has no row for them on purpose -- a reference row would resolve them where gemma4k is not
  loaded and the architecture would never fall back to the chain -- and no row declares
  `replaces`, since rad-kbench assembles a replaced chain from the fusing library's own rows.

WHY FIRST. libr4d on this tree also has rows at h256/gqa2 and h512/gqa8, Gemma 4's own
geometries, written for the dense 31B (which has no drafter) and not measured on the 26B-A4B or
under a DFlash verify; the hierarchy is absolute, so the first library takes them. These rows are
the ones every Gemma 4 number below was measured with. No other row overlaps libr4d's (the h256
gqa4/gqa8 rows take a window only), so putting this library first takes nothing else from it. The
softcap is libr4d's and libavx's `logit_softcap`.

Measured on 4x R9700, gemma-4-26B-A4B-it + z-lab DFlash, TP4: decode attention 3.234 -> 0.104 ms
a step on the full layers and 1.906 -> 0.722 on the sliding ones (the split-KV merge); a
20,489-token prefill 4.01 -> 2.83 s (the WMMA kernel); rad-kbench against libref 176/176. Not yet
run on the cards: the joins.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-12 14:40:04 -05:00

39 lines
1.6 KiB
C

/* g4k.h -- what gemma4k's row table (g4k_rows.cpp, host C++) needs from its device units
* (g4k_attn.hip, g4k_pick.hip, g4k_norm.hip). Plain C declarations: the row table compiles
* without HIP. */
#ifndef G4K_H
#define G4K_H
#include "rad_abi.h"
#ifdef __cplusplus
extern "C" {
#endif
/* attn_paged at (head_dim, gqa, kv dtype). One entry point per instantiation, named for its
* geometry, refusing anything else at launch. */
#define G4K_ATTN_DECL(HD, G) \
int g4k_attn_h##HD##_gqa##G##_bf16kv(const RadArgs* a, RadStream s); \
int g4k_attn_h##HD##_gqa##G##_fp8kv(const RadArgs* a, RadStream s);
G4K_ATTN_DECL(256, 1) G4K_ATTN_DECL(256, 2) G4K_ATTN_DECL(256, 4) G4K_ATTN_DECL(256, 8)
G4K_ATTN_DECL(512, 1) G4K_ATTN_DECL(512, 2) G4K_ATTN_DECL(512, 4) G4K_ATTN_DECL(512, 8)
#undef G4K_ATTN_DECL
/* The split-KV partials one launch can need, at declare (rad_abi.h RadScratchFn). */
int64_t g4k_attn_h256_scratch(const RadArgs* a);
int64_t g4k_attn_h512_scratch(const RadArgs* a);
/* pair_argmax on the device: the top-1 of a row of gathered (id, value) pairs, as one i32 a row. */
int g4k_pair_argmax_f32(const RadArgs* a, RadStream s);
/* Gemma 4's sandwich-norm joins, one kernel each (g4k_norm.hip):
* norm_add_norm operands a, w_a, x (inout), w_y, y
* join_add_scale operands d, w_d, r, w_r, w_j, x (inout), s */
int g4k_norm_add_norm_bf16(const RadArgs* a, RadStream s);
int g4k_join_add_scale_bf16(const RadArgs* a, RadStream s);
#ifdef __cplusplus
}
#endif
#endif /* G4K_H */