Files
Strata/engine/include/strata/core/geometry.hpp
T
Niko1221 4058aa782b Layout move for 0.2.16: the top level becomes app/, engine/, terminal/, docs/, tools/ and .github/
Made by tools/layout/move_0216.py (the commit before this one) on db8e0be9c, the final code head of 0.2.16:
- 75 git mv (renames, so `git log --follow` and blame keep working; packaging/acceptance_matrix.md shows as delete + add because its links
  changed too) and 3 removals: SETUP.bat, UPDATE.bat and update.sh (setup.py --update pulls the code now);
- 1,425 path edits in 307 files: root-relative paths in CI, Docker, .gitignore and the release scripts; Markdown links; Python and
  Node joins from a root that moved with its folder; explicit edits for code that computes a root or names a moved path; tables of
  repository paths inside tests.

engine/ holds what CMake builds (src, include, cmake, metal, sycl, spark, third_party, the C++ tests and benches). app/ is the desktop
app and its host (was apps/). terminal/ is the Terminal in the layout of the installed one (strata.py, setup.py, strata_cli,
strata_control, serve, bin, data, ref, crates, the Python tools and tests). docs/ has the documentation, docs/lang/ the translated
READMEs, docs/history and docs/bench-results. tools/ has the repository's own checks, packaging, docker, research and nix.
tests/core/test_tqp_ref.py is a pytest file and stays with the Python tests (terminal/tests/core/).
AGENTS.md, README.md, LICENSE, setup.sh and START-HERE.bat stay at the root.

No behaviour change: the same command lines, the same sources. For this head a replay of the script gave the same numbers and the path
scan (tools/layout/checks/path_scan.py) reports no hit; the suites and the builds run on this commit next.
2026-11-17 13:19:21 +01:00

93 lines
5.4 KiB
C++

// include/strata/core/geometry.hpp - the model's geometry: one struct for the shape a model file declares and
// the shape the kernels are compiled for.
//
// A model file's geometry is read from its own metadata (`strata::read_geometry`, gguf_reader.hpp), and the
// engine passes that struct everywhere it needs a dimension. The CUDA/HIP kernels still fix most of these
// numbers at compile time (`constexpr int N = 2560` and the like); `kCompiledGeometry` is that shape, written
// once, and each kernel that pins a number `static_assert`s it against this struct. So a kernel and this file
// cannot drift apart without the build failing, and a model file of another shape is refused at load with the
// metadata key that differs (`strata::geometry_unsupported`) instead of mis-running.
//
// Dependency-free on purpose: the kernels include it.
#pragma once
#include "strata/artifact/model_family.hpp"
#include <cstdint>
namespace strata::core {
/// Every field is a number a kernel depends on, so a change to the compiled values is a change to a kernel
/// contract and not a tuning knob. The defaults are Qwen3.8-Flash-Next's (qwen4exp), the shape the kernels
/// are compiled for; the GGUF key each field is read from is named beside it.
struct ModelGeometry {
int64_t n_embd = 2560; ///< embedding_length
int64_t n_layers = 48; ///< block_count (the trunk; an in-file MTP block is counted in n_mtp_layers)
int64_t qsa_interval = 4; ///< full_attention_interval: every 4th layer is full attention, 3, 7, ... 47
int64_t n_vocab = 248320; ///< the length of tokenizer.ggml.tokens
double rms_eps = 1e-6; ///< attention.layer_norm_rms_epsilon
// GDN (36 layers)
int64_t ssm_state_size = 128; ///< ssm.state_size: the key head width, and the value head width
int64_t ssm_k_heads = 16; ///< ssm.group_count
int64_t ssm_v_heads = 48; ///< ssm.time_step_rank
int64_t ssm_d_conv = 4; ///< ssm.conv_kernel
int64_t ssm_conv_channels = 10240; ///< 2*128*16 + 128*48, derived
int64_t ssm_value_dim = 6144; ///< ssm.inner_size, 128 * 48
// QSA (12 layers)
int64_t n_head = 24; ///< attention.head_count
int64_t n_head_kv = 2; ///< attention.head_count_kv
int64_t head_dim = 256; ///< attention.key_length (= attention.value_length)
int64_t n_rot = 64; ///< rope.dimension_count: the rotated slice of head_dim and of idx_key_dim
int64_t idx_q_heads = 4; ///< attention.indexer.head_count
int64_t idx_key_dim = 128; ///< attention.indexer.key_length
int64_t idx_block = 4; ///< the non-zero attention.compress_ratios entry: one pooled key per 4 cells
int64_t idx_top_k = 2048; ///< attention.indexer.top_k: cells of selection budget
// gated residual, on every layer
int64_t hc = 4; ///< hyper_connection.count
int64_t hc_lr = 320; ///< hyper_connection.low_rank
// MoE, on every layer
int64_t n_expert = 512; ///< expert_count; pruned variants (GSQ-RCO Coder) ship fewer
int64_t n_expert_used = 10; ///< expert_used_count
int64_t n_ff = 640; ///< expert_feed_forward_length (= expert_shared_feed_forward_length)
// appended for the model families (Qwen3.6 = qwen35moe); the defaults keep Flash-Next's values
int64_t n_ff_shexp = 640; ///< the shared expert's FFN width (qwen4exp: equal to n_ff, read_geometry checks)
ModelFamily family = ModelFamily::Qwen4Exp;
int64_t n_mtp_layers = 0; ///< MTP blocks inside the model file, after the trunk (qwen35moe: blk.40)
constexpr bool has_indexer() const { return idx_q_heads > 0; } ///< false: dense attention only
constexpr bool has_hyper_connections() const { return hc > 0; } ///< false: plain pre-norm residual
constexpr int64_t hc_dim() const { return hc * n_embd; }
/// `layer % qsa_interval == qsa_interval - 1` is full attention. Derived, not a second list.
constexpr int64_t n_qsa_layers() const { return n_layers / qsa_interval; }
constexpr int64_t n_gdn_layers() const { return n_layers - n_qsa_layers(); }
};
} // namespace strata::core
// The shape the kernels are compiled for: `kCompiledGeometry`, defined by the geometry header chosen at configure
// time (`-DSTRATA_GEOMETRY=<name>` selects include/strata/core/geometries/<name>.hpp, `-DSTRATA_GEOMETRY_FILE=<path>`
// takes any header; cmake/strata_geometry.cmake writes geometry_selected.hpp into the build tree for either). With
// neither option the build uses Flash-Next's header and adds no compile flag. A kernel that fixes a dimension at
// compile time asserts it against `kCompiledGeometry`; geometry_checks.hpp holds the constraints every shape must meet.
#if __has_include("strata/core/geometry_selected.hpp")
#include "strata/core/geometry_selected.hpp"
#else
#include "strata/core/geometries/flash_next.hpp"
#endif
#include "strata/core/geometry_checks.hpp"
namespace strata::core {
/// True for the full-attention layers. `docs/semantics.md` gives this twice over - `full_attention_interval
/// = 4` and an explicit `attention.compress_ratios` array - and `read_geometry` checks that the two agree.
inline bool is_qsa_layer(const ModelGeometry& g, int64_t layer) {
return layer % g.qsa_interval == g.qsa_interval - 1;
}
} // namespace strata::core