Skip to content

Commit 233a1b0

Browse files
committed
Merge PR 166: run Laguna S 2.1 on strata-poly
ErikOHiggins's laguna engine (laguna.cpp/.hpp, after flint.cpp: query heads per layer, YaRN on the full layers, a softplus output gate, no sinks), head_norm_rope_yarn() and RopeYarn for the YaRN turn as ggml's rope_yarn computes it, and mul_softplus(). The tokenizer and chat side is PR 167. Conflicts: - engine/include/strata/poly/kernels.hpp: this tree added AttnSeqs and attend_seqs() for batched sequences, the PR RopeYarn and head_norm_rope_yarn(). Both kept, this tree's first. - engine/src/poly/kernels.cu: the PR made head_norm_rope_kernel a template on YARN; this tree gave it at_stride for head_norm_rope_rows(). The kernel takes both (template<bool YARN>, int at_stride, RopeYarn yarn), and its <false> body is this tree's. head_norm_rope() and head_norm_rope_rows() pass <false> and a default RopeYarn, head_norm_rope_yarn() <true> and at_stride 0. - CMakeLists.txt: gone here (the poly targets are in engine/cmake/strata_poly.cmake). The PR's three target changes (laguna.cpp in strata-poly, poly_draft_race_test and poly_gdn_mma_state_test) went there by hand. - src/poly/laguna.{cpp,hpp}: placed at engine/src/poly/. laguna.cpp is a copy of flint.cpp from before 465649c, whose save_layer() change it lacks; a commit after the PR 180 merge brings it over. Checked with the rest of this branch merged: strata-poly builds for CUDA sm_89, sm_120 and sm_70 and for HIP gfx1201 and gfx1012. No Laguna file is on the lab machines, so the model was not run here.
2 parents e37e2d4 + 9788f7e commit 233a1b0

7 files changed

Lines changed: 801 additions & 15 deletions

File tree

‎docs/Q8_KV.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ opt-in: `--kv q8_0`. The default, `--kv fp16`, is unchanged and byte-identical t
55
option existed. The format halves the KV cache's VRAM. On the `qwen35moe` models the answers do not move; on
66
Flint and LFM2.5 they move at about 2-3% of the steps (below).
77

8-
In this version it is offered for `qwen35moe` (Tiel, Ornith, Occamy), `kolibri1`, `flint` and `lfm2moe`
9-
(LFM2.5-24B-A2B) files; another architecture is rejected at load.
8+
In this version it is offered for `qwen35moe` (Tiel, Ornith, Occamy), `kolibri1`, `flint`, `lfm2moe`
9+
(LFM2.5-24B-A2B) and `laguna` (Laguna S 2.1) files; another architecture is rejected at load.
1010

1111
Setup takes it too: `setup --family flint --kv q8_0` (`setup.sh` / `setup.bat`) writes `--kv q8_0` into the model's
1212
run config, and setup run again for that model keeps it; `--kv fp16` takes it out. For a model whose architecture
@@ -264,7 +264,7 @@ The three run configs (`strata-tiel.json`, `strata-ornith.json` and `strata-occa
264264
- `glm5next` / GLM 5.3 Flash (`dsa_attend`): its attention is not the one this cache quantizes, so neither
265265
`--kv q8_0` nor `--kv int8` is offered for it.
266266
- `--kv q8_0` and a `gemma4` file: still rejected cleanly at load
267-
(`--kv q8_0 is supported for qwen35moe, kolibri1, flint and lfm2moe models in v1; this is a 'gemma4' model`),
267+
(`--kv q8_0 is supported for qwen35moe, kolibri1, flint, lfm2moe and laguna models in v1; this is a 'gemma4' model`),
268268
not run.
269269
- ROCm/HIP: the same kernels, measured below on an AMD card (it was rejected on HIP until that was done).
270270
- The default path stays byte-identical to the last release.

‎engine/cmake/strata_poly.cmake‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ if(STRATA_BUILD_POLY AND STRATA_NATIVE_EXPERTS AND (STRATA_ENABLE_CUDA OR STRATA
176176
# Optional model/state check; run by hand with an MTP GGUF and frozen prompt IDs.
177177
add_executable(poly_gdn_mma_state_test src/poly/gdn_mma_state_test.cpp src/poly/engine.cpp src/poly/glm5next.cpp
178178
src/poly/qwen35.cpp src/poly/gemma4.cpp src/poly/deepseek4.cpp src/poly/lfm2moe.cpp
179-
src/poly/flint.cpp src/poly/kolibri1.cpp src/poly/cpu_experts.cpp src/poly/kernels.cu
179+
src/poly/flint.cpp src/poly/kolibri1.cpp src/poly/laguna.cpp src/poly/cpu_experts.cpp src/poly/kernels.cu
180180
src/poly/kernels_dsv4.cu src/core/conversation_memory.cpp)
181181
target_include_directories(poly_gdn_mma_state_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include ${_strata_gpu_include_directories})
182182
target_link_libraries(poly_gdn_mma_state_test PRIVATE strata_poly_mmq strata_kernels strata_spec strata_quant
@@ -185,7 +185,7 @@ if(STRATA_BUILD_POLY AND STRATA_NATIVE_EXPERTS AND (STRATA_ENABLE_CUDA OR STRATA
185185

186186
add_executable(strata-poly src/poly/main.cpp src/poly/engine.cpp src/poly/glm5next.cpp src/poly/qwen35.cpp
187187
src/poly/gemma4.cpp src/poly/deepseek4.cpp src/poly/lfm2moe.cpp src/poly/flint.cpp
188-
src/poly/kolibri1.cpp src/poly/cpu_experts.cpp src/poly/kernels.cu src/poly/kernels_dsv4.cu
188+
src/poly/kolibri1.cpp src/poly/laguna.cpp src/poly/cpu_experts.cpp src/poly/kernels.cu src/poly/kernels_dsv4.cu
189189
src/poly/spill.cpp src/core/conversation_memory.cpp src/core/conversation_file.cpp)
190190
target_include_directories(strata-poly PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include ${_strata_gpu_include_directories})
191191
# the driver's virtual memory calls: the attention caches grow with the conversation; on HIP they are HIP's
@@ -210,7 +210,7 @@ if(STRATA_BUILD_POLY AND STRATA_NATIVE_EXPERTS AND (STRATA_ENABLE_CUDA OR STRATA
210210
# poly_draft_race_test MODEL.gguf [RUNS] [LAYER_SPLIT]
211211
add_executable(poly_draft_race_test src/poly/draft_race_test.cpp src/poly/engine.cpp src/poly/glm5next.cpp
212212
src/poly/qwen35.cpp src/poly/gemma4.cpp src/poly/deepseek4.cpp
213-
src/poly/lfm2moe.cpp src/poly/flint.cpp src/poly/kolibri1.cpp
213+
src/poly/lfm2moe.cpp src/poly/flint.cpp src/poly/kolibri1.cpp src/poly/laguna.cpp
214214
src/poly/cpu_experts.cpp src/poly/kernels.cu src/poly/kernels_dsv4.cu
215215
src/core/conversation_memory.cpp)
216216
target_include_directories(poly_draft_race_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include

‎engine/include/strata/poly/kernels.hpp‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,22 @@ void attend_seqs(const AttnSeqs& seqs, int dim, int n_head, int kv_heads, float
302302
/// turn, from the turn of the first), the bits a call of its own gives the row. CUDA only.
303303
void head_norm_rope_rows(const float* x, int x_stride, const float* w, float* y, int dim, int heads, int n_rot,
304304
float theta_scale, int rows, float eps, void* stream, int sections, const long long* turns);
305+
/// YaRN as ggml's rope_yarn computes it, for head_norm_rope_yarn() (laguna's full layers). Pair i's angle is the
306+
/// interpolated one, freq_scale times the angle head_norm_rope() gives it, mixed with that angle itself by the
307+
/// ramp ext_factor * (1 - clamp((i - corr_low) / max(0.001, corr_high - corr_low), 0, 1)). mscale multiplies the
308+
/// rotated values' cos and sin: the final magnitude, the attention factor llama.cpp passes times the
309+
/// 1 + 0.1 * ln(1 / freq_scale) ggml adds when ext_factor is not 0. The values past n_rot are only normed.
310+
struct RopeYarn {
311+
float freq_scale = 1.0f;
312+
float ext_factor = 0.0f;
313+
float corr_low = 0.0f, corr_high = 0.0f; ///< ggml_rope_yarn_corr_dims()
314+
float mscale = 1.0f;
315+
};
316+
/// head_norm_rope() with YaRN: the same norm, NeoX pairing, positions and `at`, with no frequency factors or
317+
/// axes. CUDA and HIP only.
318+
void head_norm_rope_yarn(const float* x, int x_stride, const float* w, float* y, int dim, int heads, int n_rot,
319+
float theta_scale, int64_t pos, int rows, float eps, void* stream, const RopeYarn& yarn,
320+
const long long* at = nullptr);
305321
/// An attention cache stored as ggml Q8_0: a row is `n` int8 codes and `n / 32` fp16 scales with
306322
/// `n = kv_heads * dim`, row-major (`codes[row * n + blk * 32 + j]`, `scales[row * (n / 32) + blk]`).
307323
/// A q8 read is active wherever `codes` is not null. The value a block holds is
@@ -394,6 +410,9 @@ void likeliest(const float* logits, int n, Likeliest* out, void* stream);
394410
/// acc[r][i] *= sigmoid(g[r][i]) for `rows` groups of n values; group r's gates start r * g_stride floats into g.
395411
/// With one_gate a group has one gate for all its values.
396412
void mul_sigmoid(float* acc, const float* g, int n, int rows, int g_stride, bool one_gate, void* stream);
413+
/// mul_sigmoid() with ggml's softplus in place of the sigmoid: g above 20 as it is, else log1p(exp(g)) (laguna's
414+
/// attention output gate). CUDA and HIP only.
415+
void mul_softplus(float* acc, const float* g, int n, int rows, int g_stride, bool one_gate, void* stream);
397416
/// A causal convolution of four taps over each of `channels` channels, and its SiLU. x and out: `channels` values
398417
/// a token. state: 3 past inputs a channel, oldest first, advanced here by `rows` tokens. w: (4, channels).
399418
/// With snap, the state as it is after some of the call's tokens is also written there, for a window of guessed

‎engine/src/poly/engine.cpp‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
#include "kolibri1.hpp"
1010
#include "lfm2moe.hpp"
1111
#include "flint.hpp"
12+
#if !defined(STRATA_GPU_METAL)
13+
#include "laguna.hpp" // CUDA and HIP: its YaRN turn and softplus gate have no Metal kernels yet
14+
#endif
1215
#include "mmq.hpp"
1316
#include "qwen35.hpp"
1417
#include "runtime.hpp"
@@ -1766,9 +1769,10 @@ std::unique_ptr<Engine> Engine::open(const Options& o) {
17661769
const std::string name = arch ? arch->s : std::string();
17671770
// Clover is a qwen35moe model trained to also guess a block of tokens at once (Qwen35)
17681771
const bool qwen35 = name == "qwen35moe" || name == "clover_moe";
1769-
if (o.kv_format == KvFmt::kQ8_0 && !qwen35 && name != "kolibri1" && name != "flint" && name != "lfm2moe")
1770-
throw std::runtime_error("--kv q8_0 is supported for qwen35moe, kolibri1, flint and lfm2moe models in v1; "
1771-
"this is a '" + name + "' model");
1772+
if (o.kv_format == KvFmt::kQ8_0 && !qwen35 && name != "kolibri1" && name != "flint" && name != "lfm2moe" &&
1773+
name != "laguna")
1774+
throw std::runtime_error("--kv q8_0 is supported for qwen35moe, kolibri1, flint, lfm2moe and laguna models in "
1775+
"v1; this is a '" + name + "' model");
17721776
// int8 and int8k are Gemma 4's: their readers are not the arithmetic q8_0's are, so a model takes one
17731777
// family or the other rather than a choice between them.
17741778
if (name != "gemma4" && (o.kv_format == KvFmt::kInt8 || o.kv_format == KvFmt::kInt8K))
@@ -1785,6 +1789,9 @@ std::unique_ptr<Engine> Engine::open(const Options& o) {
17851789
if (name == "lfm2moe") return std::make_unique<Lfm2Moe>();
17861790
if (name == "flint") return std::make_unique<Flint>();
17871791
if (name == "kolibri1") return std::make_unique<Kolibri1>();
1792+
#if !defined(STRATA_GPU_METAL)
1793+
if (name == "laguna") return std::make_unique<Laguna>();
1794+
#endif
17881795
throw std::runtime_error(o.model + " is a '" + name + "' model, which this engine does not run");
17891796
};
17901797
int cards = 1, devices = 1;

‎engine/src/poly/kernels.cu‎

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,11 +1191,14 @@ __global__ void add_bias_kernel(const float* __restrict__ b, float* __restrict__
11911191
// 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
11921192
// axes, a token has a position on each of three axes, and pair j turns by the one on axis (j % sections) % 3.
11931193
// With `at`, the first token's position is what the card holds there when the launch runs.
1194+
// YARN (head_norm_rope_yarn, laguna's full layers): the angle and the magnitude as ggml's rope_yarn makes them; the
1195+
// other instantiation is the kernel as it was, which every other caller runs.
1196+
template <bool YARN>
11941197
__global__ void head_norm_rope_kernel(const float* __restrict__ x, int x_stride, const float* __restrict__ w,
11951198
float* __restrict__ y, int dim, int heads, int n_rot, float theta_scale,
11961199
const float* __restrict__ freq, long long pos, float eps,
11971200
const int* __restrict__ axes, int sections,
1198-
const long long* __restrict__ at, int at_stride = 0) {
1201+
const long long* __restrict__ at, int at_stride, RopeYarn yarn) {
11991202
__shared__ float part[8];
12001203
const float* xr = x + size_t(blockIdx.x) * x_stride;
12011204
float* yr = y + size_t(blockIdx.x) * dim;
@@ -1218,7 +1221,22 @@ __global__ void head_norm_rope_kernel(const float* __restrict__ x, int x_stride,
12181221
const float theta = freq ? turn / freq[j] : turn;
12191222
const float x0 = w ? xr[j] * scale * w[j] : xr[j] * scale;
12201223
const float x1 = w ? xr[j + half] * scale * w[j + half] : xr[j + half] * scale;
1221-
yr[i] = i < half ? x0 * cosf(theta) - x1 * sinf(theta) : x0 * sinf(theta) + x1 * cosf(theta);
1224+
if constexpr (YARN) {
1225+
// ggml's rope_yarn: the interpolated angle, mixed with the extrapolated one by the ramp over the pairs
1226+
// (rope_yarn_ramp: pair j's place between corr_low and corr_high), and the magnitude on cos and sin
1227+
const float interp = yarn.freq_scale * theta;
1228+
float angle = interp;
1229+
if (yarn.ext_factor != 0.0f) {
1230+
const float ramp_y = (float(j) - yarn.corr_low) / fmaxf(0.001f, yarn.corr_high - yarn.corr_low);
1231+
const float ramp = (1.0f - fminf(1.0f, fmaxf(0.0f, ramp_y))) * yarn.ext_factor;
1232+
angle = interp * (1.0f - ramp) + theta * ramp;
1233+
}
1234+
const float c = cosf(angle) * yarn.mscale, s = sinf(angle) * yarn.mscale;
1235+
yr[i] = i < half ? x0 * c - x1 * s : x0 * s + x1 * c;
1236+
} else {
1237+
(void) yarn;
1238+
yr[i] = i < half ? x0 * cosf(theta) - x1 * sinf(theta) : x0 * sinf(theta) + x1 * cosf(theta);
1239+
}
12221240
}
12231241
}
12241242

@@ -2369,6 +2387,17 @@ __global__ void mul_sigmoid_kernel(float* __restrict__ acc, const float* __restr
23692387
acc[size_t(blockIdx.y) * n + i] *= sigmoid(g[size_t(blockIdx.y) * g_stride + (one_gate ? 0 : i)]);
23702388
}
23712389

2390+
// ggml's softplus (op_softplus in ggml-cuda/unary.cu): x above 20 as it is, else log1p(exp(x)).
2391+
__device__ __forceinline__ float softplus_ggml(float x) { return x > 20.0f ? x : log1pf(expf(x)); }
2392+
2393+
// mul_sigmoid_kernel with softplus: laguna's attention output gate.
2394+
__global__ void mul_softplus_kernel(float* __restrict__ acc, const float* __restrict__ g, int n, int g_stride,
2395+
bool one_gate) {
2396+
const int i = blockIdx.x * blockDim.x + threadIdx.x;
2397+
if (i >= n) return;
2398+
acc[size_t(blockIdx.y) * n + i] *= softplus_ggml(g[size_t(blockIdx.y) * g_stride + (one_gate ? 0 : i)]);
2399+
}
2400+
23722401
// Where a token leaves the recurrent state as it is after it: `snaps` has a bit for each of a call's tokens that
23732402
// does, and their states lie in the tokens' order, `stride` floats apart. Null for a token that leaves none.
23742403
__device__ __forceinline__ float* snap_of(float* snap, size_t stride, unsigned snaps, int row) {
@@ -3378,14 +3407,21 @@ void add_bias(const float* b, float* acc, int n, int rows, void* stream) {
33783407
void head_norm_rope(const float* x, int x_stride, const float* w, float* y, int dim, int heads, int n_rot,
33793408
float theta_scale, int64_t pos, int rows, float eps, void* stream, const float* freq,
33803409
const int32_t* axes, int sections, const long long* at) {
3381-
head_norm_rope_kernel<<<rows * heads, dim >= 256 ? 256 : 128, 0, cs(stream)>>>(
3382-
x, x_stride, w, y, dim, heads, n_rot, theta_scale, freq, (long long) pos, eps, axes, sections, at);
3410+
head_norm_rope_kernel<false><<<rows * heads, dim >= 256 ? 256 : 128, 0, cs(stream)>>>(
3411+
x, x_stride, w, y, dim, heads, n_rot, theta_scale, freq, (long long) pos, eps, axes, sections, at, 0, RopeYarn{});
3412+
}
3413+
3414+
void head_norm_rope_yarn(const float* x, int x_stride, const float* w, float* y, int dim, int heads, int n_rot,
3415+
float theta_scale, int64_t pos, int rows, float eps, void* stream, const RopeYarn& yarn,
3416+
const long long* at) {
3417+
head_norm_rope_kernel<true><<<rows * heads, dim >= 256 ? 256 : 128, 0, cs(stream)>>>(
3418+
x, x_stride, w, y, dim, heads, n_rot, theta_scale, nullptr, (long long) pos, eps, nullptr, 1, at, 0, yarn);
33833419
}
33843420
33853421
void head_norm_rope_rows(const float* x, int x_stride, const float* w, float* y, int dim, int heads, int n_rot,
33863422
float theta_scale, int rows, float eps, void* stream, int sections, const long long* turns) {
3387-
head_norm_rope_kernel<<<rows * heads, dim >= 256 ? 256 : 128, 0, cs(stream)>>>(
3388-
x, x_stride, w, y, dim, heads, n_rot, theta_scale, nullptr, 0LL, eps, nullptr, sections, turns, 2);
3423+
head_norm_rope_kernel<false><<<rows * heads, dim >= 256 ? 256 : 128, 0, cs(stream)>>>(
3424+
x, x_stride, w, y, dim, heads, n_rot, theta_scale, nullptr, 0LL, eps, nullptr, sections, turns, 2, RopeYarn{});
33893425
}
33903426
33913427
void attend(const float* q, const uint16_t* kcache, const uint16_t* vcache, int64_t pos, int rows, float* score,
@@ -4451,6 +4487,12 @@ void mul_sigmoid(float* acc, const float* g, int n, int rows, int g_stride, bool
44514487
acc + size_t(r) * n, g + size_t(r) * g_stride, n, g_stride, one_gate);
44524488
}
44534489
4490+
void mul_softplus(float* acc, const float* g, int n, int rows, int g_stride, bool one_gate, void* stream) {
4491+
for (int r = 0; r < rows; r += 65535) // a grid has at most 65535 rows: 1,024 tokens of 72 heads are 73,728
4492+
mul_softplus_kernel<<<dim3(grid(n, 256), std::min(65535, rows - r)), 256, 0, cs(stream)>>>(
4493+
acc + size_t(r) * n, g + size_t(r) * g_stride, n, g_stride, one_gate);
4494+
}
4495+
44544496
void conv4_silu(const float* x, float* state, const float* w, float* out, int channels, int rows, void* stream,
44554497
float* snap, size_t stride, unsigned snaps) {
44564498
if (rows <= 0) return;

0 commit comments

Comments
 (0)