diff --git a/CMakeLists.txt b/CMakeLists.txt index 606e5a73c..dd031409f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2100,8 +2100,8 @@ if(STRATA_NATIVE_EXPERTS AND (STRATA_ENABLE_CUDA OR STRATA_ENABLE_HIP) AND NOT S # Optional model/state check; run by hand with an MTP GGUF and frozen prompt IDs. add_executable(poly_gdn_mma_state_test src/poly/gdn_mma_state_test.cpp src/poly/engine.cpp src/poly/glm5next.cpp src/poly/qwen35.cpp src/poly/gemma4.cpp src/poly/deepseek4.cpp src/poly/lfm2moe.cpp - src/poly/flint.cpp src/poly/kolibri1.cpp src/poly/cpu_experts.cpp src/poly/kernels.cu - src/poly/kernels_dsv4.cu src/core/conversation_memory.cpp) + src/poly/flint.cpp src/poly/kolibri1.cpp src/poly/laguna.cpp src/poly/cpu_experts.cpp + src/poly/kernels.cu src/poly/kernels_dsv4.cu src/core/conversation_memory.cpp) target_include_directories(poly_gdn_mma_state_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include ${_strata_gpu_include_directories}) target_link_libraries(poly_gdn_mma_state_test PRIVATE strata_poly_mmq strata_kernels strata_spec ggml-cpu ggml-base ${_strata_gpu_blas_target} ${_strata_gpu_runtime_target} CUDA::cuda_driver Threads::Threads) @@ -2109,8 +2109,8 @@ if(STRATA_NATIVE_EXPERTS AND (STRATA_ENABLE_CUDA OR STRATA_ENABLE_HIP) AND NOT S add_executable(strata-poly src/poly/main.cpp src/poly/engine.cpp src/poly/glm5next.cpp src/poly/qwen35.cpp src/poly/gemma4.cpp src/poly/deepseek4.cpp src/poly/lfm2moe.cpp src/poly/flint.cpp - src/poly/kolibri1.cpp src/poly/cpu_experts.cpp src/poly/kernels.cu src/poly/kernels_dsv4.cu - src/poly/spill.cpp src/core/conversation_memory.cpp) + src/poly/kolibri1.cpp src/poly/laguna.cpp src/poly/cpu_experts.cpp src/poly/kernels.cu + src/poly/kernels_dsv4.cu src/poly/spill.cpp src/core/conversation_memory.cpp) target_include_directories(strata-poly PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include ${_strata_gpu_include_directories}) # the driver's virtual memory calls: the attention caches grow with the conversation; on HIP they are HIP's # memory management calls, which the HIP runtime itself provides (hip_compat/cuda.h) @@ -2135,7 +2135,8 @@ if(STRATA_NATIVE_EXPERTS AND (STRATA_ENABLE_CUDA OR STRATA_ENABLE_HIP) AND NOT S add_executable(poly_draft_race_test src/poly/draft_race_test.cpp src/poly/engine.cpp src/poly/glm5next.cpp src/poly/qwen35.cpp src/poly/gemma4.cpp src/poly/deepseek4.cpp src/poly/lfm2moe.cpp src/poly/flint.cpp src/poly/kolibri1.cpp - src/poly/cpu_experts.cpp src/poly/kernels.cu src/poly/kernels_dsv4.cu + src/poly/laguna.cpp src/poly/cpu_experts.cpp src/poly/kernels.cu + src/poly/kernels_dsv4.cu src/core/conversation_memory.cpp) target_include_directories(poly_draft_race_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include ${_strata_gpu_include_directories}) diff --git a/docs/Q8_KV.md b/docs/Q8_KV.md index 519ecb66a..07ca6d82d 100644 --- a/docs/Q8_KV.md +++ b/docs/Q8_KV.md @@ -5,8 +5,8 @@ opt-in: `--kv q8_0`. The default, `--kv fp16`, is unchanged and byte-identical t option existed. The format halves the KV cache's VRAM. On the `qwen35moe` models the answers do not move; on Flint and LFM2.5 they move at about 2-3% of the steps (below). -In this version it is offered for `qwen35moe` (Tiel, Ornith, Occamy), `kolibri1`, `flint` and `lfm2moe` -(LFM2.5-24B-A2B) files; another architecture is rejected at load. +In this version it is offered for `qwen35moe` (Tiel, Ornith, Occamy), `kolibri1`, `flint`, `lfm2moe` +(LFM2.5-24B-A2B) and `laguna` (Laguna S 2.1) files; another architecture is rejected at load. Gemma 4 has its own two, in the same Q8_0 rule but its own layout: `--kv int8` and `--kv int8k` ([the int8 pair, below](#the-int8-pair-for-gemma-4-strata-poly---kv-int8-and---kv-int8k)). @@ -260,7 +260,7 @@ The three run configs (`strata-tiel.json`, `strata-ornith.json` and `strata-occa - `glm5next` / GLM 5.3 Flash (`dsa_attend`): its attention is not the one this cache quantizes, so neither `--kv q8_0` nor `--kv int8` is offered for it. - `--kv q8_0` and a `gemma4` file: still rejected cleanly at load - (`--kv q8_0 is supported for qwen35moe, kolibri1, flint and lfm2moe models in v1; this is a 'gemma4' model`), + (`--kv q8_0 is supported for qwen35moe, kolibri1, flint, lfm2moe and laguna models in v1; this is a 'gemma4' model`), not run. - ROCm/HIP: the same kernels, measured below on an AMD card (it was rejected on HIP until that was done). - The default path stays byte-identical to the last release. diff --git a/include/strata/poly/kernels.hpp b/include/strata/poly/kernels.hpp index 584b9de69..796ad7cf1 100644 --- a/include/strata/poly/kernels.hpp +++ b/include/strata/poly/kernels.hpp @@ -233,6 +233,22 @@ void add_bias(const float* b, float* acc, int n, int rows, void* stream); void head_norm_rope(const float* x, int x_stride, const float* w, float* y, int dim, int heads, int n_rot, float theta_scale, int64_t pos, int rows, float eps, void* stream, const float* freq = nullptr, const int32_t* axes = nullptr, int sections = 1, const long long* at = nullptr); +/// YaRN as ggml's rope_yarn computes it, for head_norm_rope_yarn() (laguna's full layers). Pair i's angle is the +/// interpolated one, freq_scale times the angle head_norm_rope() gives it, mixed with that angle itself by the +/// ramp ext_factor * (1 - clamp((i - corr_low) / max(0.001, corr_high - corr_low), 0, 1)). mscale multiplies the +/// rotated values' cos and sin: the final magnitude, the attention factor llama.cpp passes times the +/// 1 + 0.1 * ln(1 / freq_scale) ggml adds when ext_factor is not 0. The values past n_rot are only normed. +struct RopeYarn { + float freq_scale = 1.0f; + float ext_factor = 0.0f; + float corr_low = 0.0f, corr_high = 0.0f; ///< ggml_rope_yarn_corr_dims() + float mscale = 1.0f; +}; +/// head_norm_rope() with YaRN: the same norm, NeoX pairing, positions and `at`, with no frequency factors or +/// axes. CUDA and HIP only. +void head_norm_rope_yarn(const float* x, int x_stride, const float* w, float* y, int dim, int heads, int n_rot, + float theta_scale, int64_t pos, int rows, float eps, void* stream, const RopeYarn& yarn, + const long long* at = nullptr); /// An attention cache stored as ggml Q8_0: a row is `n` int8 codes and `n / 32` fp16 scales with /// `n = kv_heads * dim`, row-major (`codes[row * n + blk * 32 + j]`, `scales[row * (n / 32) + blk]`). /// A q8 read is active wherever `codes` is not null. The value a block holds is @@ -322,6 +338,9 @@ void likeliest(const float* logits, int n, Likeliest* out, void* stream); /// acc[r][i] *= sigmoid(g[r][i]) for `rows` groups of n values; group r's gates start r * g_stride floats into g. /// With one_gate a group has one gate for all its values. void mul_sigmoid(float* acc, const float* g, int n, int rows, int g_stride, bool one_gate, void* stream); +/// mul_sigmoid() with ggml's softplus in place of the sigmoid: g above 20 as it is, else log1p(exp(g)) (laguna's +/// attention output gate). CUDA and HIP only. +void mul_softplus(float* acc, const float* g, int n, int rows, int g_stride, bool one_gate, void* stream); /// A causal convolution of four taps over each of `channels` channels, and its SiLU. x and out: `channels` values /// a token. state: 3 past inputs a channel, oldest first, advanced here by `rows` tokens. w: (4, channels). /// With snap, the state as it is after some of the call's tokens is also written there, for a window of guessed diff --git a/src/poly/engine.cpp b/src/poly/engine.cpp index 2ac1c874a..d4e86ba79 100644 --- a/src/poly/engine.cpp +++ b/src/poly/engine.cpp @@ -9,6 +9,9 @@ #include "kolibri1.hpp" #include "lfm2moe.hpp" #include "flint.hpp" +#if !defined(STRATA_GPU_METAL) +#include "laguna.hpp" // CUDA and HIP: its YaRN turn and softplus gate have no Metal kernels yet +#endif #include "mmq.hpp" #include "qwen35.hpp" #include "runtime.hpp" @@ -1613,9 +1616,10 @@ std::unique_ptr Engine::open(const Options& o) { const std::string name = arch ? arch->s : std::string(); // Clover is a qwen35moe model trained to also guess a block of tokens at once (Qwen35) const bool qwen35 = name == "qwen35moe" || name == "clover_moe"; - if (o.kv_format == KvFmt::kQ8_0 && !qwen35 && name != "kolibri1" && name != "flint" && name != "lfm2moe") - throw std::runtime_error("--kv q8_0 is supported for qwen35moe, kolibri1, flint and lfm2moe models in v1; " - "this is a '" + name + "' model"); + if (o.kv_format == KvFmt::kQ8_0 && !qwen35 && name != "kolibri1" && name != "flint" && name != "lfm2moe" && + name != "laguna") + throw std::runtime_error("--kv q8_0 is supported for qwen35moe, kolibri1, flint, lfm2moe and laguna models in " + "v1; this is a '" + name + "' model"); // int8 and int8k are Gemma 4's: their readers are not the arithmetic q8_0's are, so a model takes one // family or the other rather than a choice between them. if (name != "gemma4" && (o.kv_format == KvFmt::kInt8 || o.kv_format == KvFmt::kInt8K)) @@ -1632,6 +1636,9 @@ std::unique_ptr Engine::open(const Options& o) { if (name == "lfm2moe") return std::make_unique(); if (name == "flint") return std::make_unique(); if (name == "kolibri1") return std::make_unique(); +#if !defined(STRATA_GPU_METAL) + if (name == "laguna") return std::make_unique(); +#endif throw std::runtime_error(o.model + " is a '" + name + "' model, which this engine does not run"); }; int cards = 1, devices = 1; diff --git a/src/poly/kernels.cu b/src/poly/kernels.cu index 4f312c61c..5ff1e075f 100644 --- a/src/poly/kernels.cu +++ b/src/poly/kernels.cu @@ -1113,11 +1113,14 @@ __global__ void add_bias_kernel(const float* __restrict__ b, float* __restrict__ // One block per head and token. The pairs of the rotation are i and i + n_rot / 2 for i below n_rot / 2. With // axes, a token has a position on each of three axes, and pair j turns by the one on axis (j % sections) % 3. // With `at`, the first token's position is what the card holds there when the launch runs. +// YARN (head_norm_rope_yarn, laguna's full layers): the angle and the magnitude as ggml's rope_yarn makes them; the +// other instantiation is the kernel as it was, which every other caller runs. +template __global__ void head_norm_rope_kernel(const float* __restrict__ x, int x_stride, const float* __restrict__ w, float* __restrict__ y, int dim, int heads, int n_rot, float theta_scale, const float* __restrict__ freq, long long pos, float eps, const int* __restrict__ axes, int sections, - const long long* __restrict__ at) { + const long long* __restrict__ at, RopeYarn yarn) { __shared__ float part[8]; const float* xr = x + size_t(blockIdx.x) * x_stride; float* yr = y + size_t(blockIdx.x) * dim; @@ -1139,7 +1142,22 @@ __global__ void head_norm_rope_kernel(const float* __restrict__ x, int x_stride, const float theta = freq ? turn / freq[j] : turn; const float x0 = w ? xr[j] * scale * w[j] : xr[j] * scale; const float x1 = w ? xr[j + half] * scale * w[j + half] : xr[j + half] * scale; - yr[i] = i < half ? x0 * cosf(theta) - x1 * sinf(theta) : x0 * sinf(theta) + x1 * cosf(theta); + if constexpr (YARN) { + // ggml's rope_yarn: the interpolated angle, mixed with the extrapolated one by the ramp over the pairs + // (rope_yarn_ramp: pair j's place between corr_low and corr_high), and the magnitude on cos and sin + const float interp = yarn.freq_scale * theta; + float angle = interp; + if (yarn.ext_factor != 0.0f) { + const float ramp_y = (float(j) - yarn.corr_low) / fmaxf(0.001f, yarn.corr_high - yarn.corr_low); + const float ramp = (1.0f - fminf(1.0f, fmaxf(0.0f, ramp_y))) * yarn.ext_factor; + angle = interp * (1.0f - ramp) + theta * ramp; + } + const float c = cosf(angle) * yarn.mscale, s = sinf(angle) * yarn.mscale; + yr[i] = i < half ? x0 * c - x1 * s : x0 * s + x1 * c; + } else { + (void) yarn; + yr[i] = i < half ? x0 * cosf(theta) - x1 * sinf(theta) : x0 * sinf(theta) + x1 * cosf(theta); + } } } @@ -2212,6 +2230,17 @@ __global__ void mul_sigmoid_kernel(float* __restrict__ acc, const float* __restr acc[size_t(blockIdx.y) * n + i] *= sigmoid(g[size_t(blockIdx.y) * g_stride + (one_gate ? 0 : i)]); } +// ggml's softplus (op_softplus in ggml-cuda/unary.cu): x above 20 as it is, else log1p(exp(x)). +__device__ __forceinline__ float softplus_ggml(float x) { return x > 20.0f ? x : log1pf(expf(x)); } + +// mul_sigmoid_kernel with softplus: laguna's attention output gate. +__global__ void mul_softplus_kernel(float* __restrict__ acc, const float* __restrict__ g, int n, int g_stride, + bool one_gate) { + const int i = blockIdx.x * blockDim.x + threadIdx.x; + if (i >= n) return; + acc[size_t(blockIdx.y) * n + i] *= softplus_ggml(g[size_t(blockIdx.y) * g_stride + (one_gate ? 0 : i)]); +} + // Where a token leaves the recurrent state as it is after it: `snaps` has a bit for each of a call's tokens that // does, and their states lie in the tokens' order, `stride` floats apart. Null for a token that leaves none. __device__ __forceinline__ float* snap_of(float* snap, size_t stride, unsigned snaps, int row) { @@ -3038,8 +3067,15 @@ void add_bias(const float* b, float* acc, int n, int rows, void* stream) { void head_norm_rope(const float* x, int x_stride, const float* w, float* y, int dim, int heads, int n_rot, float theta_scale, int64_t pos, int rows, float eps, void* stream, const float* freq, const int32_t* axes, int sections, const long long* at) { - head_norm_rope_kernel<<= 256 ? 256 : 128, 0, cs(stream)>>>( - x, x_stride, w, y, dim, heads, n_rot, theta_scale, freq, (long long) pos, eps, axes, sections, at); + head_norm_rope_kernel<<= 256 ? 256 : 128, 0, cs(stream)>>>( + x, x_stride, w, y, dim, heads, n_rot, theta_scale, freq, (long long) pos, eps, axes, sections, at, RopeYarn{}); +} + +void head_norm_rope_yarn(const float* x, int x_stride, const float* w, float* y, int dim, int heads, int n_rot, + float theta_scale, int64_t pos, int rows, float eps, void* stream, const RopeYarn& yarn, + const long long* at) { + head_norm_rope_kernel<<= 256 ? 256 : 128, 0, cs(stream)>>>( + x, x_stride, w, y, dim, heads, n_rot, theta_scale, nullptr, (long long) pos, eps, nullptr, 1, at, yarn); } void attend(const float* q, const uint16_t* kcache, const uint16_t* vcache, int64_t pos, int rows, float* score, @@ -4036,6 +4072,12 @@ void mul_sigmoid(float* acc, const float* g, int n, int rows, int g_stride, bool acc + size_t(r) * n, g + size_t(r) * g_stride, n, g_stride, one_gate); } +void mul_softplus(float* acc, const float* g, int n, int rows, int g_stride, bool one_gate, void* stream) { + for (int r = 0; r < rows; r += 65535) // a grid has at most 65535 rows: 1,024 tokens of 72 heads are 73,728 + mul_softplus_kernel<<>>( + acc + size_t(r) * n, g + size_t(r) * g_stride, n, g_stride, one_gate); +} + void conv4_silu(const float* x, float* state, const float* w, float* out, int channels, int rows, void* stream, float* snap, size_t stride, unsigned snaps) { if (rows <= 0) return; diff --git a/src/poly/laguna.cpp b/src/poly/laguna.cpp new file mode 100644 index 000000000..fd08ea72f --- /dev/null +++ b/src/poly/laguna.cpp @@ -0,0 +1,617 @@ +// src/poly/laguna.cpp - the layers of Laguna. The reference for the math is llama.cpp's laguna graph +// (src/models/laguna.cpp) and its rope: llama_model_laguna::get_rope_params(), llama-context.cpp's YaRN factor +// and ggml's rope_yarn(). The caches, the window rings and the MoE are Flint's (flint.cpp), which this +// architecture shares but for the sinks. +#include "laguna.hpp" + +#include "runtime.hpp" +#include "strata/poly/kernels.hpp" +#include "strata/kernels/native_mmvq.hpp" + +#include +#include +#include +#include +#include + +namespace strata::poly { +namespace { + +constexpr float kMinWeightSum = 6.103515625e-5f; // the floor ggml's MoE graph puts under the gate sum +constexpr size_t kScoreFloats = size_t(16) << 20; // the attention's scores of the tokens it takes at a time +constexpr size_t kQ8WorkRows = 2048; // the K or V blocks a q8 prompt batch dequantizes at a time +constexpr int64_t kGatingSigmoid = 2; // llama.cpp's LLAMA_EXPERT_GATING_FUNC_TYPE_SIGMOID +constexpr int kPeriod = 4; // without a pattern in the file: full at il % 4 == 0 +constexpr float kPi = 3.14159265358979323846f; // (float) M_PI, which MSVC does not define by default + +int64_t of_layer(const MetaValue& v, int il) { + if (v.type != MetaType::ARRAY) return (int64_t) v.u; + if ((size_t) il >= v.items.size()) throw std::runtime_error("a per-layer value of the GGUF ends before its layers"); + return (int64_t) v.items[(size_t) il].u; +} + +// ggml_rope_yarn_corr_dim(): the pair whose wavelength fits n_rot turns into the original context. +float yarn_corr_dim(int n_dims, float n_ctx_orig, float n_rot, float base) { + return (float) n_dims * std::log(n_ctx_orig / (n_rot * 2.0f * kPi)) / (2.0f * std::log(base)); +} + +} // namespace + +void Laguna::begin_pass() { blocks_.assign(layers_.size(), Block{}); } + +void Laguna::load_geometry() { + load_shape(); + auto find = [&](const char* key) { return meta_in(*gg_, key); }; + auto need = [](bool ok, const char* what) { + if (!ok) throw std::runtime_error(std::string("unsupported model: ") + what); + }; + auto must = [&](const char* key) -> const MetaValue& { + const MetaValue* v = find(key); + need(v != nullptr, key); + return *v; + }; + auto num = [&](const char* key, double otherwise) { + const MetaValue* v = find(key); + return v && v->is_num() ? v->num() : otherwise; + }; + + // the query heads differ by layer (48 full, 72 window in S 2.1); the key and value heads do not + const MetaValue& heads = must("attention.head_count"); + const MetaValue& kv = must("attention.head_count_kv"); + head_dim_ = shape_i("attention.key_length"); + need(shape_i("attention.value_length") == head_dim_, "the keys and values have different widths"); + need(head_dim_ % 32 == 0 && head_dim_ <= 256, "bad head size"); + heads_.assign((size_t) n_layer_, 0); + n_kv_ = (int) of_layer(kv, 0); + max_head_ = 0; + for (int il = 0; il < n_layer_; ++il) { + const int h = (int) of_layer(heads, il); + need((int) of_layer(kv, il) == n_kv_, "the key and value heads differ by layer"); + need(h > 0 && n_kv_ > 0 && h % n_kv_ == 0 && h <= 256, "bad attention.head_count(_kv)"); + heads_[(size_t) il] = h; + max_head_ = std::max(max_head_, h); + } + + // the window layers: from the file's pattern where it has one, else llama.cpp's load_swa_pattern(4, dense + // first), full at il % 4 == 0. No window in the file: every layer is full (Laguna M.1). + const MetaValue* window = find("attention.sliding_window"); + window_ = window ? (int) window->u : 0; + need(window_ >= 0, "bad attention.sliding_window"); + swa_.assign((size_t) n_layer_, 0); + if (window_ > 0) { + const MetaValue* pattern = find("attention.sliding_window_pattern"); + for (int il = 0; il < n_layer_; ++il) + swa_[(size_t) il] = pattern ? of_layer(*pattern, il) != 0 : il % kPeriod != 0; + } + + // the rotation: a full layer turns rope.dimension_count values with rope.freq_base and the file's scaling; a + // window layer its own count and base, plain (llama.cpp zeroes YaRN's terms for them) + rot_full_ = shape_i("rope.dimension_count"); + const MetaValue* rot_swa = find("rope.dimension_count_swa"); + rot_swa_ = rot_swa ? (int) rot_swa->u : rot_full_; + need(rot_full_ > 0 && rot_full_ % 2 == 0 && rot_full_ <= head_dim_, "bad rope.dimension_count"); + need(rot_swa_ > 0 && rot_swa_ % 2 == 0 && rot_swa_ <= head_dim_, "bad rope.dimension_count_swa"); + const float base_full = shape_f("rope.freq_base"); + const float base_swa = (float) num("rope.freq_base_swa", base_full); + theta_full_ = std::pow(base_full, -2.f / (float) rot_full_); + theta_swa_ = std::pow(base_swa, -2.f / (float) rot_swa_); + + const MetaValue* scaling = find("rope.scaling.type"); + const std::string kind = scaling && scaling->type == MetaType::STRING ? scaling->s : std::string("none"); + need(kind == "none" || kind == "yarn" || kind == "linear", "rope.scaling.type is not none, linear or yarn"); + const float factor = (float) num("rope.scaling.factor", 1.0); + need(kind == "none" || factor > 0.f, "bad rope.scaling.factor"); + yarn_ = RopeYarn{}; + yarn_on_ = false; + if (kind != "none" && factor != 1.f) { + yarn_on_ = true; + yarn_.freq_scale = 1.0f / factor; + // llama-context.cpp: the attention factor; for YaRN its own magnitude, cancelled there and put back by + // ggml's rope_yarn (1 + 0.1 * ln(1 / freq_scale)), so the rotated values come out scaled by about that + float attn = (float) num("rope.scaling.yarn_attn_factor", 1.0); + if (kind == "yarn") { + yarn_.ext_factor = 1.0f; + auto mscale = [](float scale, float m) { return scale <= 1.0f ? 1.0f : 0.1f * m * std::log(scale) + 1.0f; }; + const float log_mul = (float) num("rope.scaling.yarn_log_multiplier", 0.0); + attn = log_mul != 0.f ? mscale(factor, 1.0f) / mscale(factor, log_mul) : mscale(factor, 1.0f); + attn *= 1.0f / (1.0f + 0.1f * std::log(factor)); + const MetaValue* orig = find("rope.scaling.original_context_length"); + const float n_orig = (float) (orig ? orig->u : (uint64_t) shape_i("context_length")); + const float fast = (float) num("rope.scaling.yarn_beta_fast", 32.0); + const float slow = (float) num("rope.scaling.yarn_beta_slow", 1.0); + yarn_.corr_low = std::max(0.0f, std::floor(yarn_corr_dim(rot_full_, n_orig, fast, base_full))); + yarn_.corr_high = + std::min((float) (rot_full_ - 1), std::ceil(yarn_corr_dim(rot_full_, n_orig, slow, base_full))); + } + attn *= (float) num("rope.scaling.attn_factor", 1.0); + yarn_.mscale = yarn_.ext_factor != 0.f ? attn * (1.0f + 0.1f * std::log(1.0f / yarn_.freq_scale)) : attn; + std::fprintf(stderr, + "strata-poly: laguna: full layers turn %d of %d values by %s (factor %g, pairs %g to %g, " + "magnitude %.4f); window layers %d, plain\n", + rot_full_, head_dim_, kind.c_str(), (double) factor, (double) yarn_.corr_low, + (double) yarn_.corr_high, (double) yarn_.mscale, rot_swa_); + } + + // the gate's width a layer, for its scratch: one a head or one a value (llama.cpp reads it from the tensor) + max_gate_ = 0; + for (int il = 0; il < n_layer_; ++il) { + const TensorInfo* g = tensor(il, "attn_gate.weight"); + need(g != nullptr && g->shape.size() == 2, "a layer without attn_gate.weight"); + const int w = (int) g->shape[1], h = heads_[(size_t) il]; + need(w == h || w == h * head_dim_, "attn_gate.weight is neither a gate a head nor one a value"); + max_gate_ = std::max(max_gate_, w); + } + + const MetaValue* gating = find("expert_gating_func"); + need(!gating || (int64_t) gating->u == kGatingSigmoid, "the expert gate is not a sigmoid"); + const MetaValue* shared = find("expert_shared_count"); + need(!shared || shared->u == 1, "expert_shared_count is not 1"); + const MetaValue* dense = find("leading_dense_block_count"); + n_dense_ = dense ? (int) dense->u : 0; + need(n_dense_ >= 0 && n_dense_ < n_layer_, "bad leading_dense_block_count"); + n_ff_ = n_dense_ > 0 ? shape_i("feed_forward_length") : 0; + const MetaValue* norm = find("expert_weights_norm"); + w_norm_ = norm ? norm->u != 0 : false; // llama.cpp's default + w_scale_ = (float) num("expert_weights_scale", 0.0); + + // this architecture puts no limit on a SwiGLU's inputs + clamp_exp_.assign((size_t) n_layer_ + 1, INFINITY); + clamp_shexp_ = clamp_exp_; + // a prompt batch's products guard their half sums until a run shows Laguna's stay in range (Flint's did not) + half_sum_guard_ = true; +} + +void Laguna::load_layer(int il) { + Layer& L = layers_[(size_t) il]; + Block& B = blocks_[(size_t) il]; + auto f32 = [&](const char* n, int64_t a, int64_t b = 1) { return upload(tensor(il, n), blk(il, n), a, b, kF32); }; + auto mat = [&](const char* n, int64_t a, int64_t b) { return upload(tensor(il, n), blk(il, n), a, b, kAnyMat); }; + + B.swa = swa_[(size_t) il] != 0; + B.n_head = heads_[(size_t) il]; + B.n_gate = (int) tensor(il, "attn_gate.weight")->shape[1]; + B.n_rot = B.swa ? rot_swa_ : rot_full_; + B.theta = B.swa ? theta_swa_ : theta_full_; + B.yarn = !B.swa && yarn_on_; + L.attn_norm = f32("attn_norm.weight", n_embd_); + L.ffn_norm = f32("ffn_norm.weight", n_embd_); + L.fixed = true; // the attention's launches read the position on the card when they are recorded + B.wq = mat("attn_q.weight", n_embd_, (int64_t) B.n_head * head_dim_); + B.wk = mat("attn_k.weight", n_embd_, (int64_t) n_kv_ * head_dim_); + B.wv = mat("attn_v.weight", n_embd_, (int64_t) n_kv_ * head_dim_); + B.wg = mat("attn_gate.weight", n_embd_, B.n_gate); + B.q_norm = f32("attn_q_norm.weight", head_dim_); + B.k_norm = f32("attn_k_norm.weight", head_dim_); + B.wo = mat("attn_output.weight", (int64_t) B.n_head * head_dim_, n_embd_); + if (il < n_dense_) { + B.ffn_gate = mat("ffn_gate.weight", n_embd_, n_ff_); + B.ffn_up = mat("ffn_up.weight", n_embd_, n_ff_); + B.ffn_down = mat("ffn_down.weight", n_ff_, n_embd_); + return; + } + load_moe(il, true); + B.probs_b.assign((size_t) n_expert_, 0.f); + if (const TensorInfo* b = tensor(il, "exp_probs_b.bias")) { + if (b->type != kTypeF32 || (int64_t) b->elements() != n_expert_) + throw std::runtime_error(blk(il, "exp_probs_b.bias") + " is " + shape_of(*b) + ", expected F32 [" + + std::to_string(n_expert_) + "]"); + home(*b).payload(*b); + std::memcpy(B.probs_b.data(), home(*b).data(*b), (size_t) n_expert_ * sizeof(float)); + B.probs_b_dev = upload(b, blk(il, "exp_probs_b.bias"), n_expert_, 1, kF32); + } +} + +// The caches: a full layer keeps a row a position, a window layer a ring of the rows a call can read (Flint's). +void Laguna::alloc_caches() { + ring_ = window_ > 0 ? std::min(opt_.max_context, window_ + rows_) : 0; + keep_ = std::min(window_, ring_); + const bool q8 = opt_.kv_format == KvFmt::kQ8_0; + const size_t row = (size_t) n_kv_ * head_dim_; + const size_t scale_row = (size_t) n_kv_ * (head_dim_ / 32) * sizeof(uint16_t); // a Q8_0 block's scale + if (q8) std::fprintf(stderr, "strata-poly: q8_0 KV cache: %zu bytes a row\n", row + scale_row); + size_t rings = 0, codes = 0, scales = 0; + for (int il = 0; il < (int) blocks_.size(); ++il) { + if (!runs(il) || !blocks_[(size_t) il].swa) continue; + if (q8) { + codes += 2 * (size_t) ring_ * row; + scales += 2 * (size_t) ring_ * scale_row; + } else { + rings += 2 * (size_t) ring_ * row; + } + } + uint8_t* rc = codes ? (uint8_t*) seq_alloc(codes, "the attention cache") : nullptr; + uint8_t* rs = scales ? (uint8_t*) seq_alloc(scales, "the attention cache") : nullptr; + uint16_t* kv = rings ? (uint16_t*) seq_alloc(rings * sizeof(uint16_t), "the attention cache") : nullptr; + for (int il = 0; il < (int) blocks_.size(); ++il) { + Block& B = blocks_[(size_t) il]; + if (!runs(il)) continue; + if (B.swa) { + if (q8) { + B.k_codes = (int8_t*) rc; + rc += (size_t) ring_ * row; + B.k_scales = (uint16_t*) rs; + rs += (size_t) ring_ * scale_row; + B.v_codes = (int8_t*) rc; + rc += (size_t) ring_ * row; + B.v_scales = (uint16_t*) rs; + rs += (size_t) ring_ * scale_row; + } else { + B.keys = kv; + kv += ring_ * row; + B.vals = kv; + kv += ring_ * row; + } + } else if (q8) { + B.k_codes = (int8_t*) kv_alloc(row, 1); + B.k_scales = (uint16_t*) kv_alloc(scale_row, 1); + B.v_codes = (int8_t*) kv_alloc(row, 1); + B.v_scales = (uint16_t*) kv_alloc(scale_row, 1); + } else { + B.keys = (uint16_t*) kv_alloc(row * sizeof(uint16_t), 1); + B.vals = (uint16_t*) kv_alloc(row * sizeof(uint16_t), 1); + } + } + chunk_ = (int) std::clamp(kScoreFloats / ((size_t) max_head_ * opt_.max_context), 1, (size_t) rows_); +} + +// Per-token buffers hold rows_ tokens, row after row, sized for the layer with the most query heads. +void Laguna::assign_scratch() { + const size_t n = (size_t) rows_, heads = n * max_head_ * head_dim_, kv = n * n_kv_ * head_dim_; + qraw_ = fbuf(heads); + q_ = fbuf(heads); + kraw_ = fbuf(kv); + k_ = fbuf(kv); + v_ = fbuf(kv); + att_ = fbuf(heads); + gate_ = fbuf(n * max_gate_); + score_n_ = (size_t) chunk_ * max_head_ * opt_.max_context; + score_ = fbuf(score_n_); + if (rows_ >= kStageRows) awork_ = fbuf(heads + heads / 2 + 1 + 3 * n * max_head_); // a prompt batch's attention + if (rows_ >= kStageRows && opt_.kv_format == KvFmt::kQ8_0) { + work_halves_ = kQ8WorkRows * (size_t) n_kv_ * head_dim_; + q8w_ = fbuf((2 * work_halves_ + 1) / 2); + } +} + +// A sequence's rows are the full layers': a window layer's are part of its state (save_layer()). Under --kv q8_0 +// a row is its codes and, in a second array, a scale every 32 of them, saved one after the other. +size_t Laguna::card_row_bytes() const { + const bool q8 = opt_.kv_format == KvFmt::kQ8_0; + const size_t code_row = (size_t) n_kv_ * head_dim_ * (q8 ? 1 : sizeof(uint16_t)); + const size_t scale_row = q8 ? (size_t) n_kv_ * (head_dim_ / 32) * sizeof(uint16_t) : 0; + size_t layers = 0; + for (const Block& B : blocks_) + if ((q8 ? B.k_codes != nullptr : B.keys != nullptr) && !B.swa) ++layers; + return layers * 2 * (code_row + scale_row); +} + +void Laguna::save_card_rows(int64_t first, int64_t n, uint8_t* dst) { + if (first < 0 || n < 0 || first + n > opt_.max_context) + throw std::logic_error("save_rows: the rows are outside the context"); + ck(cudaStreamSynchronize(cs(st_)), "save rows"); + const bool q8 = opt_.kv_format == KvFmt::kQ8_0; + const size_t code_row = (size_t) n_kv_ * head_dim_ * (q8 ? 1 : sizeof(uint16_t)); + const size_t scale_row = q8 ? (size_t) n_kv_ * (head_dim_ / 32) * sizeof(uint16_t) : 0; + const size_t at_c = (size_t) first * code_row, at_s = (size_t) first * scale_row; + const size_t n_c = (size_t) n * code_row, n_s = (size_t) n * scale_row; + for (const Block& B : blocks_) { + if (B.swa || n == 0 || !(q8 ? B.k_codes != nullptr : B.keys != nullptr)) continue; + if (q8) { + ck(cudaMemcpy(dst, (const uint8_t*) B.k_codes + at_c, n_c, cudaMemcpyDeviceToHost), "save rows"); + dst += n_c; + ck(cudaMemcpy(dst, (const uint8_t*) B.k_scales + at_s, n_s, cudaMemcpyDeviceToHost), "save rows"); + dst += n_s; + ck(cudaMemcpy(dst, (const uint8_t*) B.v_codes + at_c, n_c, cudaMemcpyDeviceToHost), "save rows"); + dst += n_c; + ck(cudaMemcpy(dst, (const uint8_t*) B.v_scales + at_s, n_s, cudaMemcpyDeviceToHost), "save rows"); + dst += n_s; + continue; + } + for (const uint16_t* cache : {B.keys, B.vals}) { + ck(cudaMemcpy(dst, (const uint8_t*) cache + at_c, n_c, cudaMemcpyDeviceToHost), "save rows"); + dst += n_c; + } + } +} + +void Laguna::load_card_rows(const uint8_t* src, int64_t from, int64_t count, int64_t first, int64_t n) { + if (from < 0 || first < from || n < 0 || first + n > from + count || first + n > opt_.max_context) + throw std::logic_error("load_rows: the rows are outside what was saved"); + cover(first + n); + ck(cudaStreamSynchronize(cs(st_)), "load rows"); + const bool q8 = opt_.kv_format == KvFmt::kQ8_0; + const size_t code_row = (size_t) n_kv_ * head_dim_ * (q8 ? 1 : sizeof(uint16_t)); + const size_t scale_row = q8 ? (size_t) n_kv_ * (head_dim_ / 32) * sizeof(uint16_t) : 0; + const size_t at_c = (size_t) first * code_row, at_s = (size_t) first * scale_row; + const size_t n_c = (size_t) n * code_row, n_s = (size_t) n * scale_row; + const size_t sk_c = (size_t) (first - from) * code_row, sk_s = (size_t) (first - from) * scale_row; + const size_t all_c = (size_t) count * code_row, all_s = (size_t) count * scale_row; + for (const Block& B : blocks_) { + if (B.swa || n == 0 || !(q8 ? B.k_codes != nullptr : B.keys != nullptr)) continue; + if (q8) { + ck(cudaMemcpy((uint8_t*) B.k_codes + at_c, src + sk_c, n_c, cudaMemcpyHostToDevice), "load rows"); + src += all_c; + ck(cudaMemcpy((uint8_t*) B.k_scales + at_s, src + sk_s, n_s, cudaMemcpyHostToDevice), "load rows"); + src += all_s; + ck(cudaMemcpy((uint8_t*) B.v_codes + at_c, src + sk_c, n_c, cudaMemcpyHostToDevice), "load rows"); + src += all_c; + ck(cudaMemcpy((uint8_t*) B.v_scales + at_s, src + sk_s, n_s, cudaMemcpyHostToDevice), "load rows"); + src += all_s; + continue; + } + for (uint16_t* cache : {B.keys, B.vals}) { + ck(cudaMemcpy((uint8_t*) cache + at_c, src + sk_c, n_c, cudaMemcpyHostToDevice), "load rows"); + src += all_c; + } + } +} + +// The last keep_ positions before `pos` as runs of a ring's rows: f(the run's place among the keep_, its first +// ring row, its rows). A position before the sequence's start has no run. +template +void Laguna::window_runs(int64_t pos, F&& f) const { + for (int64_t p = std::max(0, pos - keep_); p < pos;) { + const int64_t at = p % ring_, m = std::min(pos - p, ring_ - at); + f((size_t) (p - (pos - keep_)), (size_t) at, (size_t) m); + p += m; + } +} + +// What a window layer has of the sequence: the rows the next token attends (Flint's). +size_t Laguna::extra_bytes() const { + const bool q8 = opt_.kv_format == KvFmt::kQ8_0; + const size_t code_row = (size_t) n_kv_ * head_dim_ * (q8 ? 1 : sizeof(uint16_t)); + const size_t scale_row = q8 ? (size_t) n_kv_ * (head_dim_ / 32) * sizeof(uint16_t) : 0; + size_t layers = 0; + for (const Block& B : blocks_) + if (B.swa) ++layers; + return layers * 2 * (size_t) keep_ * (code_row + scale_row); +} + +// A window layer's rows, behind the window layers before it (there is no recurrent state). +void Laguna::save_layer(int il, int64_t pos, uint8_t* dst) { + if (!blocks_[(size_t) il].swa) return; + const bool q8 = opt_.kv_format == KvFmt::kQ8_0; + const size_t code_row = (size_t) n_kv_ * head_dim_ * (q8 ? 1 : sizeof(uint16_t)); + const size_t scale_row = q8 ? (size_t) n_kv_ * (head_dim_ / 32) * sizeof(uint16_t) : 0; + const size_t whole = 2 * (size_t) keep_ * (code_row + scale_row); + dst += state_n_ * sizeof(float); + for (int j = 0; j < il; ++j) + if (blocks_[(size_t) j].swa) dst += whole; + const Block& B = blocks_[(size_t) il]; + auto save_ring = [&](const uint8_t* cache, size_t one_row) { + std::memset(dst, 0, (size_t) keep_ * one_row); // the places of positions before the sequence's start + window_runs(pos, [&](size_t slot, size_t at, size_t m) { + ck(cudaMemcpy(dst + slot * one_row, cache + at * one_row, m * one_row, cudaMemcpyDeviceToHost), + "save state"); + }); + dst += (size_t) keep_ * one_row; + }; + if (q8) { + save_ring((const uint8_t*) B.k_codes, code_row); + save_ring((const uint8_t*) B.k_scales, scale_row); + save_ring((const uint8_t*) B.v_codes, code_row); + save_ring((const uint8_t*) B.v_scales, scale_row); + return; + } + save_ring((const uint8_t*) B.keys, code_row); + save_ring((const uint8_t*) B.vals, code_row); +} + +void Laguna::load_extra(const uint8_t* src, int64_t pos) { + const bool q8 = opt_.kv_format == KvFmt::kQ8_0; + const size_t code_row = (size_t) n_kv_ * head_dim_ * (q8 ? 1 : sizeof(uint16_t)); + const size_t scale_row = q8 ? (size_t) n_kv_ * (head_dim_ / 32) * sizeof(uint16_t) : 0; + for (const Block& B : blocks_) { + if (!B.swa) continue; + auto load_ring = [&](uint8_t* cache, size_t one_row) { + window_runs(pos, [&](size_t slot, size_t at, size_t m) { + ck(cudaMemcpy(cache + at * one_row, src + slot * one_row, m * one_row, cudaMemcpyHostToDevice), + "load state"); + }); + src += (size_t) keep_ * one_row; + }; + if (q8) { + load_ring((uint8_t*) B.k_codes, code_row); + load_ring((uint8_t*) B.k_scales, scale_row); + load_ring((uint8_t*) B.v_codes, code_row); + load_ring((uint8_t*) B.v_scales, scale_row); + continue; + } + load_ring((uint8_t*) B.keys, code_row); + load_ring((uint8_t*) B.vals, code_row); + } +} + +// The recordings of a full layer's head are numbered by the rows they are sized for; a window layer's never +// reads more than its window. +int Laguna::head_key(int il, int n) const { return blocks_[(size_t) il].swa ? 0 : room_key(n); } + +// A side of a layer's cache: a full layer's at the positions, a window layer's at their places in the ring. +void Laguna::store(const float* x, const Block& B, int64_t pos, int n, bool value) { + const int width = n_kv_ * head_dim_; + if (opt_.kv_format == KvFmt::kQ8_0) { + int8_t* codes = value ? B.v_codes : B.k_codes; + uint16_t* scales = value ? B.v_scales : B.k_scales; + if (recording_) return store_q8_at(x, codes, scales, width, at_, n, B.swa ? ring_ : 0, st_); + if (!B.swa) return store_q8(x, codes, scales, width, pos, n, st_); + const int at = (int) (pos % ring_), head = std::min(n, ring_ - at); + store_q8(x, codes, scales, width, at, head, st_); + if (head < n) store_q8(x + (size_t) head * width, codes, scales, width, 0, n - head, st_); + return; + } + uint16_t* cache = value ? B.vals : B.keys; + if (recording_) return store_f16_at(x, cache, width, at_, n, B.swa ? ring_ : 0, st_); + if (!B.swa) return store_f16(x, cache, width, pos, n, st_); + const int at = (int) (pos % ring_), head = std::min(n, ring_ - at); + store_f16(x, cache, width, at, head, st_); + if (head < n) store_f16(x + (size_t) head * width, cache, width, 0, n - head, st_); +} + +// A head's norm and its turn, the layer's kind: a full layer of a scaled file by YaRN, else plain RoPE. +void Laguna::rope_heads(const Block& B, const float* x, const Dev& norm, float* y, int heads, int64_t pos, int rows, + const long long* at) { + if (B.yarn) + return head_norm_rope_yarn(x, head_dim_, fp(norm), y, head_dim_, heads, B.n_rot, B.theta, pos, rows, rms_eps_, + st_, yarn_, at); + head_norm_rope(x, head_dim_, fp(norm), y, head_dim_, heads, B.n_rot, B.theta, pos, rows, rms_eps_, st_, nullptr, + nullptr, 1, at); +} + +// The attention's output times the softplus of the gate (gate_, computed from the layer's normed input next to the +// queries): a gate a head broadcast over its values, or one a value. +void Laguna::gate(const Block& B, int n) { + if (B.n_gate == B.n_head) + mul_softplus(att_, gate_, head_dim_, n * B.n_head, 1, true, st_); + else + mul_softplus(att_, gate_, B.n_head * head_dim_, n, B.n_gate, false, st_); +} + +// The attention: queries and keys normed a head and turned by the position; the keys and values into the caches; +// each head's softmax over the rows (no sinks); the gate; the output matrix. +void Laguna::attn(Block& B, int64_t pos, int n) { + if (rows_of_) return attn_rows(B, n); + const long long* at = recording_ ? at_ : nullptr; // a recorded launch: the tokens from what at_ holds + const long long* rot = at ? at + 1 : nullptr; // and the turn each of them is given, read there too + const int heads = B.n_head; + lin(B.wq, xn_, qraw_, n, xq_); + rope_heads(B, qraw_, B.q_norm, q_, heads, pos, n, rot); + lin(B.wk, xn_, kraw_, n, xq_); + rope_heads(B, kraw_, B.k_norm, k_, n_kv_, pos, n, rot); + lin(B.wv, xn_, v_, n, xq_); + lin(B.wg, xn_, gate_, n, xq_); + store(k_, B, pos, n, false); + store(v_, B, pos, n, true); + const float scale = 1.f / std::sqrt((float) head_dim_); + const int window = B.swa ? window_ : 0, ring = B.swa ? ring_ : 0; + const bool q8 = opt_.kv_format == KvFmt::kQ8_0; + const Kvq8 kq = q8 ? Kvq8{B.k_codes, B.k_scales} : Kvq8{}; + const Kvq8 vq = q8 ? Kvq8{B.v_codes, B.v_scales} : Kvq8{}; + if (n >= kStageRows) + attend_batch(blas_, q_, q8 ? nullptr : B.keys, q8 ? nullptr : B.vals, pos, n, score_, score_n_, awork_, + att_, head_dim_, heads, n_kv_, scale, st_, window, ring, false, kq, vq, + q8 ? (uint16_t*) q8w_ : nullptr, q8 ? (uint16_t*) q8w_ + work_halves_ : nullptr, + q8 ? work_halves_ : 0); + else + attend(q_, q8 ? nullptr : B.keys, q8 ? nullptr : B.vals, pos, n, score_, chunk_, att_, head_dim_, heads, n_kv_, + scale, st_, window, ring, false, at, + !at ? 0 : B.swa ? std::min(window_ + n - 1, opt_.max_context) : room(n), score_n_, kq, vq); + gate(B, n); + lin(B.wo, att_, ao_, n); +} + +// step()'s rows, each of its own sequence: the products for all of them at once, and a row at a time the turn (it +// takes one position and counts on from it), the stored keys and values and the attention, in the row's sequence. +void Laguna::attn_rows(Block& B, int n) { + const int heads = B.n_head; + lin(B.wq, xn_, qraw_, n, xq_); + lin(B.wk, xn_, kraw_, n, xq_); + lin(B.wv, xn_, v_, n, xq_); + lin(B.wg, xn_, gate_, n, xq_); + const float scale = 1.f / std::sqrt((float) head_dim_); + const size_t kv = (size_t) n_kv_ * head_dim_, hq = (size_t) heads * head_dim_; + const int window = B.swa ? window_ : 0, ring = B.swa ? ring_ : 0; + const bool q8 = opt_.kv_format == KvFmt::kQ8_0; + for (int i = 0; i < n; ++i) { + const Row& r = rows_of_[i]; + const long long* ai = at_ + 2 * i; + rope_heads(B, qraw_ + i * hq, B.q_norm, q_ + i * hq, heads, 0, 1, ai + 1); + rope_heads(B, kraw_ + i * kv, B.k_norm, k_ + i * kv, n_kv_, 0, 1, ai + 1); + uint16_t* kch = q8 ? nullptr : (uint16_t*) seq_buf(r.seq, B.keys); + uint16_t* vch = q8 ? nullptr : (uint16_t*) seq_buf(r.seq, B.vals); + const Kvq8 kq = q8 ? Kvq8{(int8_t*) seq_buf(r.seq, B.k_codes), (uint16_t*) seq_buf(r.seq, B.k_scales)} + : Kvq8{}; + const Kvq8 vq = q8 ? Kvq8{(int8_t*) seq_buf(r.seq, B.v_codes), (uint16_t*) seq_buf(r.seq, B.v_scales)} + : Kvq8{}; + if (q8) { + store_q8_at(k_ + i * kv, (int8_t*) kq.codes, (uint16_t*) kq.scales, (int) kv, ai, 1, ring, st_); + store_q8_at(v_ + i * kv, (int8_t*) vq.codes, (uint16_t*) vq.scales, (int) kv, ai, 1, ring, st_); + } else { + store_f16_at(k_ + i * kv, kch, (int) kv, ai, 1, ring, st_); + store_f16_at(v_ + i * kv, vch, (int) kv, ai, 1, ring, st_); + } + const int64_t keep = pos_; + pos_ = r.pos; + const int sized = B.swa ? std::min(window_, opt_.max_context) : room(1); + pos_ = keep; + attend(q_ + i * hq, kch, vch, r.pos, 1, score_, chunk_, att_ + i * hq, head_dim_, heads, n_kv_, scale, st_, + window, ring, false, ai, sized, score_n_, kq, vq); + } + gate(B, n); + lin(B.wo, att_, ao_, n); +} + +void Laguna::dense_ffn(Block& B, int il, int n) { + lin(B.ffn_gate, xn_, g_, n, xq_); + lin(B.ffn_up, xn_, u_, n, xq_); + swiglu_clamp(g_, u_, h_, n * n_ff_, clamp_shexp_[(size_t) il], st_); + lin(B.ffn_down, h_, fo_, n); +} + +// A layer from its input to its router's copy (a dense layer: to its end). Pre-norms only, as llama.cpp's graph. +void Laguna::layer_head(Layer& L, int il, int n) { + Block& B = blocks_[(size_t) il]; + const int threads = sum_threads(n_embd_); + kernels::native_add_rms_norm_q8_1(r_, nullptr, fp(L.attn_norm), xn_, xq_, n_embd_, n, rms_eps_, threads, st_); + dbg("attn_in", il, xn_, n_embd_); + attn(B, pos_, n); + dbg("attn", il, ao_, n_embd_); + + kernels::native_add_rms_norm_q8_1(r_, ao_, fp(L.ffn_norm), xn_, xq_, n_embd_, n, rms_eps_, threads, st_); + dbg("ffn_in", il, xn_, n_embd_); + if (!L.moe) { + dense_ffn(B, il, n); + dbg("ffn", il, fo_, n_embd_); + axpy(fo_, 1.f, r_, n * n_embd_, true, st_); + } else if (!batched(L, n)) { + moe_router(L, n); + } +} + +void Laguna::layer_tail(Layer&, int, int n) { axpy(fo_, 1.f, r_, n * n_embd_, true, st_); } + +void Laguna::output_rows(int first, int rows, float* h) { + rms_norm(r_ + (size_t) first * n_embd_, fp(out_norm_), h, n_embd_, rows, rms_eps_, st_); +} + +// What ggml's MoE graph does for this model: sigmoid gates, the n_used largest of gate + bias, and weights from the +// bare gates, made to sum to one (expert_weights_norm) and scaled (expert_weights_scale). +void Laguna::route(int il, const float* logits, int32_t, int* ids, float* w, float* probs) const { + const std::vector& bias = blocks_[(size_t) il].probs_b; + float* pick = probs + n_expert_; + for (int e = 0; e < n_expert_; ++e) { + probs[e] = 1.f / (1.f + std::exp(-logits[e])); + pick[e] = probs[e] + bias[(size_t) e]; + } + float sum = 0.f; + for (int k = 0; k < n_used_; ++k) { + int best = 0; + for (int e = 1; e < n_expert_; ++e) + if (pick[e] > pick[best]) best = e; + pick[best] = -INFINITY; + ids[k] = best; + w[k] = probs[best]; + sum += w[k]; + } + const float denom = w_norm_ ? std::max(sum, kMinWeightSum) : 1.f; + for (int k = 0; k < n_used_; ++k) { + w[k] /= denom; + if (w_scale_ != 0.f) w[k] *= w_scale_; + } +} + +bool Laguna::route_card(int il, int n, int32_t* ids, float* w) { + const Block& B = blocks_[(size_t) il]; + route_rows(rl_, n_expert_, n_used_, n, 1, 1.f, fp(B.probs_b_dev), nullptr, w_norm_, w_scale_, ids, w, st_); + return true; +} + +// The shared expert, ungated: the routed experts' sum starts from its output. +void Laguna::moe_shared(const Layer& L, int il, int n) { + lin(L.sh_gate, xn_, g_, n, xq_); + lin(L.sh_up, xn_, u_, n, xq_); + swiglu_clamp(g_, u_, h_, n * n_ff_shexp_, clamp_shexp_[(size_t) il], st_); + lin(L.sh_down, h_, fo_, n); +} + +} // namespace strata::poly diff --git a/src/poly/laguna.hpp b/src/poly/laguna.hpp new file mode 100644 index 000000000..79b873219 --- /dev/null +++ b/src/poly/laguna.hpp @@ -0,0 +1,101 @@ +// src/poly/laguna.hpp - poolside's Laguna (llama.cpp architecture "laguna"): S 2.1 is 118B-A8B. +// +// One residual stream and attention in every layer, with normed queries and keys. Three layers in four attend +// only the last tokens (a sliding window, keys and values in a ring); the fourth, from the first layer on, attends +// every token so far. Every layer turns its queries and keys by the position, the two kinds of layer differently: +// a window layer turns the whole head with plain RoPE, a full layer half of it with YaRN. The window layers have +// more query heads than the full ones; every layer has the same key and value heads. The attention's output is +// gated by the softplus of a projection of the layer's input, one gate a head (or one a value), before its output +// matrix. The first layer's FFN is dense; the others' are routed experts with sigmoid gates, a selection bias and +// weights made to sum to one and then scaled, beside an ungated shared expert. The output matrix is a tensor of +// its own. +#pragma once + +#include "engine.hpp" +#include "strata/poly/kernels.hpp" + +namespace strata::poly { + +class Laguna final : public Engine { +public: + size_t card_row_bytes() const override; + void save_card_rows(int64_t first, int64_t n, uint8_t* dst) override; + void load_card_rows(const uint8_t* src, int64_t from, int64_t count, int64_t first, int64_t n) override; + +protected: + void load_geometry() override; + void load_layer(int il) override; + void begin_pass() override; + size_t recurrent_floats() const override { return 0; } + void alloc_caches() override; + void assign_scratch() override; + int widest() const override { return max_head_ * head_dim_; } + int streams() const override { return 1; } + void layer_head(Layer& L, int il, int n) override; + void layer_tail(Layer& L, int il, int n) override; + void output_rows(int first, int rows, float* h) override; + void route(int il, const float* logits, int32_t token, int* ids, float* w, float* tmp) const override; + bool route_card(int il, int n, int32_t* ids, float* w) override; + void moe_shared(const Layer& L, int il, int n) override; + size_t extra_bytes() const override; + void save_layer(int il, int64_t pos, uint8_t* dst) override; + void load_extra(const uint8_t* src, int64_t pos) override; + int head_key(int il, int n) const override; + bool batch_heads() const override { return true; } + +private: + /// A layer's weights beside the ones every architecture has (Layer), and its caches. + struct Block { + bool swa = false; ///< attends a sliding window; its caches are rings + int n_head = 0; ///< its query heads + int n_gate = 0; ///< its gate's outputs a token: n_head (a gate a head) or n_head * head_dim (a value) + int n_rot = 0; ///< the values of a head it turns + float theta = 0; ///< a rotary pair turns by position * theta^pair + bool yarn = false; ///< and by YaRN (yarn_), a full layer of a file that scales its positions + Dev wq, wk, wv, wo, wg, q_norm, k_norm; + uint16_t* keys = nullptr; ///< rows of n_kv * head_dim, fp16: max_context of them, or ring_ in a window layer + uint16_t* vals = nullptr; + int8_t* k_codes = nullptr; ///< --kv q8_0: a row is n_kv * head_dim codes and n_kv * head_dim / 32 scales + uint16_t* k_scales = nullptr; + int8_t* v_codes = nullptr; + uint16_t* v_scales = nullptr; + Dev ffn_gate, ffn_up, ffn_down; ///< a dense layer's FFN + std::vector probs_b; ///< the bias the routed experts' pick adds to the gates (zeros without one) + Dev probs_b_dev; + }; + + void attn(Block& B, int64_t pos, int n); + void attn_rows(Block& B, int n); + void rope_heads(const Block& B, const float* x, const Dev& norm, float* y, int heads, int64_t pos, int rows, + const long long* at); + void gate(const Block& B, int n); + void store(const float* x, const Block& B, int64_t pos, int n, bool value); + void dense_ffn(Block& B, int il, int n); + template + void window_runs(int64_t pos, F&& f) const; + + std::vector blocks_; ///< one a layer of layers_ + std::vector swa_; ///< per layer: a window layer + std::vector heads_; ///< per layer: its query heads + int max_head_ = 0, max_gate_ = 0, n_kv_ = 0, head_dim_ = 0, n_dense_ = 0; + int rot_full_ = 0, rot_swa_ = 0; ///< the values of a head each kind of layer turns + float theta_full_ = 0, theta_swa_ = 0; ///< and its rotary base, as theta_scale + bool yarn_on_ = false; ///< the full layers turn by YaRN + RopeYarn yarn_; + bool w_norm_ = false; ///< the picked experts' weights are made to sum to one + float w_scale_ = 0; ///< and then scaled by this, when it is not 0 + int window_ = 0; ///< the tokens a window layer attends, the token itself among them; 0: no window layers + int ring_ = 0; ///< rows of a window layer's caches: the window and a call's tokens + int keep_ = 0; ///< rows of a window layer a sequence's state keeps + int chunk_ = 1; ///< tokens the attention takes at a time + + // device scratch + float *qraw_ = nullptr, *q_ = nullptr, *kraw_ = nullptr, *k_ = nullptr, *v_ = nullptr, *att_ = nullptr, + *gate_ = nullptr, *score_ = nullptr; + size_t score_n_ = 0; ///< the floats of score_ + float* awork_ = nullptr; ///< a prompt batch's attention (attend_batch) + float* q8w_ = nullptr; ///< a --kv q8_0 prompt batch's K and V, dequantized to fp16 (kwork | vwork) + size_t work_halves_ = 0; ///< the halves of each of q8w_'s two halves +}; + +} // namespace strata::poly