Archived
llama.cpp keeps a Gated DeltaNet's ssm_conv1d unquantised, in f32, and a container converted from a GGUF holds it so. The three kernels that read the convolution weight, gdn_conv_prep, gdn_conv_update and the convolution gdn_conv_recurrent_update folds in, read bf16, so their shims refuse an f32 weight. The weight's element type is now a template parameter of all three. r4d_gdn_conv_tap.h, which already holds the arithmetic the fold shares with the standalone path, gives the vector a channel's taps are loaded as (ushort4 or float4) and the widening of one tap, exact for either type. The shims take a bf16 or an f32 weight and pick the instance by its dtype; any other dtype is still refused. The raw entry points keep their signatures. r4d.h gets an f32 twin of each standalone one, r4d_gdn_conv_prep_w4_h128_bf16_f32wgt and r4d_gdn_conv_update_w4_h128_bf16_f32wgt, rather than a flag, so no caller of the bf16 ones changes. The fold has no raw entry point; its CV parameter now also says the weight's type (1 bf16, 2 f32), which leaves every existing instance's template arguments and name as they were. That is twelve new conv instances and sixteen new fold ones. The bf16 instances compile to the same code. Built before and after with the build's flags, all 116 kernels of the two units have the same instructions, resource counts and metadata; only the conv kernels' names change with the added template argument. The bf16 reads keep their shape: widening gdn_conv_update's ushort4 to a float4 before use reorders its loads. The f32 conv instances use no scratch, conv_prep 56 to 57 VGPRs (53 at bf16) and conv_update 108 (96 at bf16, 12 waves per SIMD instead of 16); each f32 fold instance has exactly its bf16 twin's counts. r4d_selftest gets three cases for the card. gdn_conv_prep and gdn_conv_update over an f32 weight holding bf16's values must write the bytes they write over the bf16 weight: q, k and v, g and beta at prefill, and the conv state. The sequences are uneven; at prefill two are seeded from their cache, one of them over two chunks, and a one-token one is not. Outputs are poisoned, the cache must move and an f16 weight must be refused. The fold over the f32 weight must match the pair over the bf16 one, byte for byte as the fold already does at bf16. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
30 lines
1.7 KiB
C++
30 lines
1.7 KiB
C++
// r4d_gdn_conv_tap.h -- the arithmetic of one gated-delta-net convolution output, shared by the
|
|
// standalone conv kernels (r4d_gdn_conv_w4_h128_bf16.hip) and the recurrent update that folds the
|
|
// decode convolution into its prologue (r4d_gdn_recurrent_update_k128_v128_bf16_fp32state.hip).
|
|
// A single definition keeps the fold byte-identical to the standalone path: the reciprocal form
|
|
// below changes the last bit of some bf16 outputs.
|
|
#pragma once
|
|
#include "r4d_gdn_wmma.h"
|
|
|
|
#define R4D_GDN_CONV_W 4 // conv width; the state holds R4D_GDN_CONV_W - 1 taps of history
|
|
|
|
// The weight is bf16 from a checkpoint and f32 from a GGUF, which keeps ssm_conv1d unquantised.
|
|
// cp_taps is a channel's taps as one vector load; cp_tap widens one tap to f32, exactly for
|
|
// either type.
|
|
template <typename WT> struct cp_vec4;
|
|
template <> struct cp_vec4<unsigned short> { using T = ushort4; };
|
|
template <> struct cp_vec4<float> { using T = float4; };
|
|
template <typename WT> using cp_taps = typename cp_vec4<WT>::T;
|
|
static_assert(R4D_GDN_CONV_W == 4, "cp_taps holds a channel's taps in one four-wide vector");
|
|
|
|
__device__ __forceinline__ float cp_tap(unsigned short w) { return bf2f(w); }
|
|
__device__ __forceinline__ float cp_tap(float w) { return w; }
|
|
|
|
// The result is rounded to bf16 immediately, so an IEEE-exact divide buys nothing: v_rcp_f32 is
|
|
// 1 ULP (~1e-7 relative) against bf16's 2^-8 quantum. The exact form is ~10 dependent instructions
|
|
// (v_div_scale, v_rcp, Newton, v_div_fmas, v_div_fixup); this is two. The conv update runs four
|
|
// waves on five workgroups, so its time is its instruction count.
|
|
__device__ __forceinline__ float cp_silu(float z) {
|
|
return z * __builtin_amdgcn_rcpf(1.0f + __expf(-z));
|
|
}
|