Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2100,17 +2100,17 @@ 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)
endif()

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)
Expand All @@ -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})
Expand Down
6 changes: 3 additions & 3 deletions docs/Q8_KV.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)).
Expand Down Expand Up @@ -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.
Expand Down
19 changes: 19 additions & 0 deletions include/strata/poly/kernels.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
13 changes: 10 additions & 3 deletions src/poly/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -1613,9 +1616,10 @@ std::unique_ptr<Engine> 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))
Expand All @@ -1632,6 +1636,9 @@ std::unique_ptr<Engine> Engine::open(const Options& o) {
if (name == "lfm2moe") return std::make_unique<Lfm2Moe>();
if (name == "flint") return std::make_unique<Flint>();
if (name == "kolibri1") return std::make_unique<Kolibri1>();
#if !defined(STRATA_GPU_METAL)
if (name == "laguna") return std::make_unique<Laguna>();
#endif
throw std::runtime_error(o.model + " is a '" + name + "' model, which this engine does not run");
};
int cards = 1, devices = 1;
Expand Down
50 changes: 46 additions & 4 deletions src/poly/kernels.cu
Original file line number Diff line number Diff line change
Expand Up @@ -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 <bool YARN>
__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;
Expand All @@ -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);
}
}
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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<<<rows * heads, dim >= 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<false><<<rows * heads, dim >= 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<true><<<rows * heads, dim >= 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,
Expand Down Expand Up @@ -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<<<dim3(grid(n, 256), std::min(65535, rows - r)), 256, 0, cs(stream)>>>(
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;
Expand Down
Loading