MCPcopy Create free account
hub / github.com/Niko1221/Strata / gdn_layer

Function gdn_layer

engine/src/core/layer.cpp:252–384  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

250static void st_begin(int64_t layer, int slot, void* stream);
251static void st_end(int64_t layer, int slot, void* stream);
252bool gdn_layer(const WeightTable& tables, const ModelGeometry& g, int64_t layer, const GdnBuffers& b, const float* mixed, float* out, void* stream, std::string& err) { const LayerView v(tables, layer); const int64_t C = g.ssm_conv_channels;
253// ---- resolve. Every one of these was shape-checked by `check_layer` at load time, so a miss here is a
254// NAME problem and not a geometry one, and the message says which name.
255const WeightRef* w_qkv = v.get("attn_qkv.weight"); const WeightRef* w_gate = v.get("attn_gate.weight"); const WeightRef* w_out = v.get("ssm_out.weight"); const WeightRef* w_alpha = v.get("ssm_alpha.weight"); const WeightRef* w_beta = v.get("ssm_beta.weight"); const WeightRef* w_conv = v.get("ssm_conv1d.weight"); const WeightRef* w_norm = v.get("ssm_norm.weight"); const WeightRef* w_dt = v.get("ssm_dt.bias"); const WeightRef* w_a = v.get("ssm_a"); const char* missing = !w_qkv ? "attn_qkv.weight" : !w_gate ? "attn_gate.weight" : !w_out ? "ssm_out.weight" : !w_alpha ? "ssm_alpha.weight" : !w_beta ? "ssm_beta.weight" : !w_conv ? "ssm_conv1d.weight" : !w_norm ? "ssm_norm.weight" : !w_dt ? "ssm_dt.bias" : !w_a ? "ssm_a" : nullptr; if (missing) { err = v.name(missing) + " is missing"; return false; }
256// The conv kernel and the two norms are F32 source types, and the loader copied them verbatim - so their
257// device bytes ARE f32 and can be handed straight to the kernels that want `const float*`.
258const float* conv_kernel = (const float*) w_conv->data; const float* ssm_norm = (const float*) w_norm->data; const float* dt = (const float*) w_dt->data; const float* ssm_a = (const float*) w_a->data; strata::kernels::SForm f_qkv, f_gate, f_out; if (!sform_of(*w_qkv, f_qkv, v.name("attn_qkv.weight"), err)) return false; if (!sform_of(*w_gate, f_gate, v.name("attn_gate.weight"), err)) return false; if (!sform_of(*w_out, f_out, v.name("ssm_out.weight"), err)) return false; Planes p_qkv, p_gate, p_out; if (!plane_ptrs(*w_qkv, v.name("attn_qkv.weight"), p_qkv, err)) return false; if (!plane_ptrs(*w_gate, v.name("attn_gate.weight"), p_gate, err)) return false; if (!plane_ptrs(*w_out, v.name("ssm_out.weight"), p_out, err)) return false; using namespace strata::kernels; cudaStream_t st = (cudaStream_t) stream;
259// ---- 1. the activation, in EVERY format a weight on this layer might ask for. BOTH quantized images are
260// produced, because which one is wanted is a property of the TENSOR and the pack mixes them by
261// layer - producing only the "right" one is how the per-role assumption gets baked back in.
262st_begin(layer, 8, stream);
263 // Plan v0.3 P3: the canonical activation images only when a canonical projection reads them.
264 if (!w_qkv->native_data || !w_gate->native_data) {
265#if defined(STRATA_EXPERIMENTAL_SM60)
266 if (strata::kernels::fuse_decode()) {
267 quantize_act_images(mixed, g.n_embd, b.x_q8_0, b.x_q8k, nullptr, stream); // ONE pass, was two kernels
268 } else
269#endif
270 {
271 quantize_q8_K(mixed, b.x_q8k, g.n_embd, stream);
272 quantize_q8_0(mixed, b.x_q8_0, g.n_embd, stream);
273 }
274 }
275 st_end(layer, 8, stream);
276// ---- 2. qkv = wqkv @ x, with the activation THIS layer's `attn_qkv` asks for
277st_begin(layer, 9, stream); if (!gemv_quantized(*w_qkv, p_qkv, f_qkv, b.x_q8_0, b.x_q8k, b.qkv, g.n_embd, C, v.name("attn_qkv.weight"), stream, err, mixed)) return false; st_end(layer, 9, stream);
278// ---- 3. conv, then SiLU on the WHOLE conv output - before the q|k|v split, which is what
279// `ref/gdn.py` L262 says and what makes the split a reshape rather than a copy.
280st_begin(layer, 10, stream);
281// Plan v0.3 P3: under the native contract, conv + SiLU + both L2 norms are one kernel, and alpha/beta with their
282// epilogues another (`fused_gdn.hpp`).
283const bool fused_pre = g_fused_gdn && native_gdn_enabled() && native_bf16_projections && g.ssm_d_conv == 4 &&
284 g.ssm_state_size == 128;
285if (fused_pre) {
286 fused_gdn_conv_l2(b.conv_state, b.qkv, conv_kernel, b.h, (int) C, (int) (2 * g.ssm_k_heads), RMS_EPS, stream);
287} else
288try {
289 if (native_gdn_enabled()) native_gdn_conv_silu(b.conv_state, b.qkv, conv_kernel, b.conv_out, b.h, C, g.ssm_d_conv, stream);
290 else {
291 gdn_conv_step(b.conv_state, b.qkv, conv_kernel, b.conv_out, C, g.ssm_d_conv, stream);
292 cudaMemcpyAsync(b.h, b.conv_out, (size_t) C * 4, cudaMemcpyDeviceToDevice, st);
293 silu_inplace(b.h, C, stream);
294 }
295} catch (const std::exception& error) { err = v.name("gdn_conv_silu") + ": " + error.what(); return false; }
296st_end(layer, 10, stream);
297// ---- 4. q,k = l2_norm; v is NOT normalised. The three views are contiguous runs of the same buffer:
298// q at [0, 2048), k at [2048, 4096), v at [4096, 10240), each 128-wide heads. const
299st_begin(layer, 11, stream); int64_t qk = g.ssm_state_size * g.ssm_k_heads;
300if (!fused_pre) try {
301 if (native_gdn_enabled()) {
302 native_gdn_l2_norm(b.h, g.ssm_k_heads, g.ssm_state_size, RMS_EPS, stream);
303 native_gdn_l2_norm(b.h + qk, g.ssm_k_heads, g.ssm_state_size, RMS_EPS, stream);
304 } else {
305 gdn_l2_norm(b.h, g.ssm_k_heads, g.ssm_state_size, RMS_EPS, stream);
306 gdn_l2_norm(b.h + qk, g.ssm_k_heads, g.ssm_state_size, RMS_EPS, stream);
307 }
308} catch (const std::exception& error) { err = v.name("gdn_l2_norm") + ": " + error.what(); return false; }
309// The recurrence needs another 1/sqrt(S) beyond Q/K normalization. The legacy

Callers 1

block_layer_preFunction · 0.70

Calls 15

fuse_decodeFunction · 0.85
project_bf16_pairFunction · 0.85
sform_ofFunction · 0.70
plane_ptrsFunction · 0.70
st_beginFunction · 0.70
st_endFunction · 0.70
gemv_quantizedFunction · 0.70
project_bf16Function · 0.70
getMethod · 0.65
quantize_q8_KFunction · 0.50
quantize_q8_0Function · 0.50
native_gdn_enabledFunction · 0.50

Tested by

no test coverage detected