Archived
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>
35 lines
1.7 KiB
CMake
35 lines
1.7 KiB
CMake
# gemma4k -- Gemma 4's kernels beyond libr4d: paged attention at head_dim 256/512 (gqa 1/2/4/8, any
|
|
# window), pair_argmax (the sharded draft head's cross-rank top-1), and the two sandwich-norm
|
|
# joins, which are ops of its own (docs/OPS.md). Ahead of libr4d for Gemma 4 (g4k_rows.cpp).
|
|
#
|
|
# Built inside radiance's tree (add_subdirectory(gemma4k) beside libr4d) or on its own against an
|
|
# installed radiance -- the same rad_add_plugin either way:
|
|
# cmake -S gemma4k -B build-g4k -DCMAKE_PREFIX_PATH=/opt/radiance && cmake --build build-g4k
|
|
if(NOT COMMAND rad_add_plugin)
|
|
cmake_minimum_required(VERSION 3.21)
|
|
project(gemma4k LANGUAGES CXX HIP)
|
|
find_package(radiance REQUIRED)
|
|
rad_plugin_defaults()
|
|
endif()
|
|
|
|
if(NOT RAD_HIP_FOUND)
|
|
message(STATUS "gemma4k: this radiance has no device build (RAD_HIP_FOUND is OFF) -- skipped")
|
|
return()
|
|
endif()
|
|
# gfx12 only, as libr4d: the kernels are wave32 and read an fp8 cache with RDNA4's hardware
|
|
# conversion; the software fallback compiles elsewhere but spills.
|
|
rad_plugin_gpu_targets(_g4k_targets "^gfx12")
|
|
if(NOT _g4k_targets)
|
|
message(STATUS "gemma4k: no gfx12 target in RAD_GPU_TARGETS (${RAD_GPU_TARGETS}) -- skipped")
|
|
return()
|
|
endif()
|
|
|
|
rad_add_plugin(gemma4k kernels
|
|
SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/g4k_rows.cpp
|
|
HIP_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/g4k_attn.hip ${CMAKE_CURRENT_SOURCE_DIR}/g4k_pick.hip
|
|
${CMAKE_CURRENT_SOURCE_DIR}/g4k_norm.hip ${CMAKE_CURRENT_SOURCE_DIR}/g4k_escale.hip
|
|
GPU_TARGETS ${_g4k_targets})
|
|
string(REPLACE ";" " " _g4k_desc "${_g4k_targets}")
|
|
target_compile_definitions(gemma4k PRIVATE "G4K_BUILD_TARGET=\"${_g4k_desc}\"")
|
|
set_target_properties(gemma4k PROPERTIES HIP_STANDARD 17 HIP_STANDARD_REQUIRED ON)
|