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 0d95046f6f gemma4k, arch/gemma4: expert_scale -- per_expert_scale on the routing weights in one launch
Gemma 4 multiplies each renormalised routing weight by per_expert_scale of the expert it chose.
In the conventional vocabulary that is an `embed_lookup` of the [n_expert, 1] table by the ids
and a `mul` into the weights -- two launches a MoE layer, a few dozen elements each at decode
(--profile-ops: ~0.26 and ~0.22 ms a step across the 30 layers). gemma4k's `expert_scale` is the
pair as one kernel, ew = bf16(ew * w_e[id]), with an id outside the table reading 0 as
embed_lookup's missed row does: one product rounded once, so the pair's value bit for bit.

Like the joins it has no libref row on purpose, so arch/gemma4 falls back to the pair wherever
gemma4k is not loaded, and RADIANCE_GEMMA4_FUSE=0 turns it off with them. 30 launches a step the
fewer.

g4k_emu: device and host rows against the pair over 32 shapes -- top-k 1 and 8, pitched rows,
pads and one-past-the-end ids, rows past M untouched -- every element equal. arch_test: a decode
step scales in one launch, and in two with it refused. docs/OPS.md: the row and its paragraph;
GUIDE: what the switch now covers.

Not yet run on the cards.

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

43 lines
1.8 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);
/* expert_scale: each routing weight times its expert's scale, in place (g4k_escale.hip):
* operands ids, w_e, ew (inout) */
int g4k_expert_scale_bf16(const RadArgs* a, RadStream s);
int g4k_join_add_scale_bf16(const RadArgs* a, RadStream s);
#ifdef __cplusplus
}
#endif
#endif /* G4K_H */