From ac79f4dc8a071fcc8943733d2f16faadc760ae84 Mon Sep 17 00:00:00 2001 From: Deadcode Date: Wed, 7 Oct 2026 12:37:53 +0200 Subject: [PATCH] radiance 1.2.2 - Static YaRN for the Qwen rotary paths (#37): a container whose config names YaRN gets it in attention, the QSA index keys and a DFlash2 drafter; one reader for every rotary type, and a LLaMA container whose type is not served (llama3, longrope, dynamic) is refused by name. - --rope-scaling yarn:FACTOR[:ORIG] (#38) writes YaRN over a container's own rotary keys at load, no reconvert; the persisted prefix cache's fingerprint carries the resolved rotary. - An uneven world's cheaper rank (#36) pages the common token count and gives the rest of its VRAM to its experts; a plan and pool that disagree refuse to start. - MoE prefill (#35): wide gather, parallel scan and run-map prefix, 256-column down tile, early weight loads -- by rule, bit-identical. - MoE decode (#34): the sorted operand's own down kernel, its column tiles by routed rows. - --draft-window fixed|adaptive (#24): a per-sequence draft window priced by measured step time, one head depth per step over the batch; fixed stays the default. New metric radiance:draft_position_accepted_total and Grafana panels. - Out-of-tree plugin builds compile to the tree's device code from any build directory. --- CMakeLists.txt | 2 +- NOTICE | 10 +- abi/rad_builder.h | 4 + arch/common/rad_arch.h | 81 +- arch/common/rad_block_attn_fp8.h | 2 +- arch/common/rad_block_dspark.h | 6 +- arch/llama_fp8/llama_fp8.cpp | 3 +- arch/qwen35_fp8/qwen35_fp8.cpp | 1 - cmake/RadAddPlugin.cmake | 16 + core/config.cpp | 19 + core/engine.cpp | 2 - core/engine.h | 12 +- core/engine_bringup.cpp | 197 +- core/format/checkpoint.cpp | 48 +- core/format/rope_override.h | 34 +- core/place/planner.cpp | 9 + core/place/planner.h | 7 + core/rad_core.h | 33 +- core/sched/batch.h | 4 + core/sched/draft.cpp | 386 ++- core/sched/draft.h | 223 +- core/sched/metrics.h | 2 +- core/sched/scheduler.cpp | 117 +- core/sched/scheduler.h | 46 +- core/server/iface.h | 2 +- core/server/metrics.cpp | 2 +- core/server/metrics.h | 2 +- deploy/grafana/README.md | 2 +- deploy/grafana/radiance.json | 4 +- docs/GUIDE.md | 59 +- docs/KERNELS.md | 2 +- docs/OPS.md | 10 +- docs/TP3.md | 30 +- docs/YARN-VALIDATION.md | 248 -- docs/YARN.md | 90 +- libavx/avx_attn.cpp | 102 + libavx/avx_cases.cpp | 11 + libavx/avx_isa.h | 2 +- libavx/avx_registry.cpp | 19 + libr4d/r4d.h | 29 +- libr4d/r4d_model_bf16.hip | 42 +- libr4d/r4d_moe.hip | 577 ++-- libr4d/r4d_selftest.cpp | 574 ++-- libref/ref_attn.cpp | 87 + libref/ref_ops.h | 2 + libref/ref_registry.cpp | 21 + libref/ref_shapes.cpp | 17 + tests/arch_test.cpp | 54 + tests/config_test.cpp | 29 +- tests/data/generate_yarn_reference.py | 27 - tests/data/yarn_reference.json | 3497 ------------------------- tests/data/yarn_reference_rocm.json | 3497 ------------------------- tests/format_test.cpp | 57 + tests/mem_test.cpp | 95 + tests/oot_plugins.sh | 8 +- tests/place_test.cpp | 42 + tests/ref_test.cpp | 161 +- tests/sched_test.cpp | 707 +++-- tests/server_test.cpp | 6 +- tests/yarn_test.cpp | 120 + tools/rad_convert.cpp | 1 - tools/rad_kbench.cpp | 2 +- tools/rad_tune.cpp | 2 +- 63 files changed, 2744 insertions(+), 8759 deletions(-) delete mode 100644 docs/YARN-VALIDATION.md delete mode 100644 tests/data/generate_yarn_reference.py delete mode 100644 tests/data/yarn_reference.json delete mode 100644 tests/data/yarn_reference_rocm.json diff --git a/CMakeLists.txt b/CMakeLists.txt index 35a4a04c..6abff545 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.21) # VERSION is what /version reports and what a client checks compatibility against. It is NOT the # ABI version -- that lives in abi/rad_abi.h and is read out of the header below, because # a plugin contract and a server release do not move together. -project(radiance VERSION 1.2.1 LANGUAGES C CXX) +project(radiance VERSION 1.2.2 LANGUAGES C CXX) set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) diff --git a/NOTICE b/NOTICE index ffc8c3b4..e9e7e043 100644 --- a/NOTICE +++ b/NOTICE @@ -7,10 +7,12 @@ Written by Deadcode, with contributions from tcclaviger (the N-rank all-reduce k and the draft refill on a prefix hit), goodman-b (four-rank tensor parallelism for Qwen3.8-Flash-Next), keptn42 (the four-rank all-reduce served in place, the fused four-rank writes, the n-gram table in RAM, the four-class expert split, the Grafana dashboard, the routed -GEMM's decode loads and run search, the gated residual's prefill read form and row groups, and -the fused router launch), mechkbfan (the token embedding in the pinned host pool), Code000 (the -n-gram gather on the card and the MTP head's history-pass skip) and baKon (three-rank tensor -parallelism, serving any world size from the shipped container, and the request log). +GEMM's decode loads and run search, the gated residual's prefill read form and row groups, the +fused router launch, the adaptive draft window, the MoE prefill forms and the sorted operand's +decode kernel), mechkbfan (the token embedding in the pinned host pool), Code000 (the n-gram +gather on the card and the MTP head's history-pass skip), baKon (three-rank tensor parallelism, +serving any world size from the shipped container, the request log, an uneven world's per-rank +KV and weights split, and --rope-scaling) and hazyumps (static YaRN for the Qwen rotary paths). This product includes software from the following projects, each under the MIT License. The license text is in the file named beside each. diff --git a/abi/rad_builder.h b/abi/rad_builder.h index e1c92a0e..13606344 100644 --- a/abi/rad_builder.h +++ b/abi/rad_builder.h @@ -39,6 +39,10 @@ typedef struct RadModelMeta { float rms_eps; float rope_theta; + /* Always 1. A rotary's scaling is a TYPE with parameters -- linear, YaRN, llama3, longrope -- + * and it is carried in the free-form keys below (rope_parameters.* / rope_scaling.*, with + * text_config.* and draft.* forms), which arch/common/rad_arch.h's yarn_from reads and refuses + * by name. A bare factor here would read every scheme as a linear position divide. */ float rope_scale; /* Free-form key/value from the container header, for anything the struct above does not diff --git a/arch/common/rad_arch.h b/arch/common/rad_arch.h index 6f834849..6e7c6c9a 100644 --- a/arch/common/rad_arch.h +++ b/arch/common/rad_arch.h @@ -791,7 +791,7 @@ inline int geom_from(Geom& g, const RadModelMeta* m, const RadBuildCtx* c, g.wire_min_bytes = c->tp_wire_min_bytes; g.eps = m->rms_eps; g.theta = m->rope_theta; - g.rope_scale= m->rope_scale ? m->rope_scale : 1.0f; + g.rope_scale= 1.0f; /* yarn_from reads the model's rotary scaling */ g.dtype = dtype; g.act_dtype = act_dtype; { @@ -1025,9 +1025,8 @@ inline RadOperand rope_posmc(const Geom& g, const RadBatch* b, int64_t T) { * cannot rotate. */ inline bool rope_mixed(const RadBatch* b) { return b->rope_pos && b->rope_mixed; } -/* `rope` declared for the geometry's rotary: M-RoPE's mode and sections when the model has them, - * `mode` otherwise. */ -/* Append only for explicit YaRN, preserving stock op identities and tune keys. */ +/* A rotary op declared with the geometry's YaRN coefficients appended. A model without YaRN declares + * exactly the parameters it was given, so its op identities and tune keys are unchanged. */ inline rad_op decl_rotary(RadBuilder* b, const Geom& g, const char* name, const RadParam* params, int n, const rad_weight* weights, int nw) { if (g.yarn.factor == 0.0f) return rad_decl_op(b, name, params, n, weights, nw); @@ -1039,23 +1038,45 @@ inline rad_op decl_rotary(RadBuilder* b, const Geom& g, const char* name, return rad_decl_op(b, name, p.data(), (int)p.size(), weights, nw); } -/* Both modern HF rope_parameters and legacy rope_scaling. Nested text_config is - * authoritative for a multimodal target; the draft has its own independent config. */ +/* THE ONE READER OF A MODEL'S ROTARY SCALING. Every architecture calls it after geom_from, for the + * target ("") and for a drafter with a config of its own ("draft."), and it sets `g.rope_scale` and + * `g.yarn` from the free-form keys and from nothing else. RadModelMeta::rope_scale is not read: a + * factor without its type would be a linear position divide for a scheme that is not one. + * + * The object read is the first of `text_config.rope_parameters.`, `rope_parameters.`, + * `text_config.rope_scaling.`, `rope_scaling.` (a drafter: `draft.rope_parameters.`, + * `draft.rope_scaling.`) that names a type in `rope_type` or `type`; a multimodal target's + * text_config is authoritative. core/format/rope_override.h writes --rope-scaling into the same + * object, found in the same order. + * + * default, mrope no scaling (`mrope` is the legacy Qwen2-VL spelling of default + sections) + * linear positions divided by `factor` + * yarn static YaRN: frequencies blended over the correction range that `factor`, + * `original_max_position_embeddings`, `beta_fast`/`beta_slow` and `truncate` + * give, cos/sin scaled by `attention_factor` (default 1 + 0.1 ln factor). The + * factor is the config's, read as vLLM and transformers read it. + * + * Anything else -- llama3, longrope, dynamic -- is refused by name, as is YaRN's `mscale` form. A + * refusal leaves the geometry as it was. */ inline int yarn_from(Geom& g, const RadModelMeta* meta, const char* prefix, int64_t rot) { - g.yarn = {}; const std::string root(prefix); + const char* who = root.empty() ? "target" : "draft"; const std::vector homes = root.empty() ? std::vector{"text_config.rope_parameters.", "rope_parameters.", "text_config.rope_scaling.", "rope_scaling."} : std::vector{root + "rope_parameters.", root + "rope_scaling."}; - std::string home; + std::string home, type_key; const char* type = nullptr; for (const auto& h : homes) { - type = rad_meta_gets(meta, (h + "rope_type").c_str(), - rad_meta_gets(meta, (h + "type").c_str(), nullptr)); + for (const char* t : {"rope_type", "type"}) + if ((type = rad_meta_gets(meta, (h + t).c_str(), nullptr))) { type_key = h + t; break; } if (type) { home = h; break; } } - if (!type || !std::strcmp(type, "default")) return RAD_OK; + if (!type || !std::strcmp(type, "default") || !std::strcmp(type, "mrope")) { + g.yarn = {}; + g.rope_scale = 1.0f; + return RAD_OK; + } auto get = [&](const char* key, double fallback) { const char* text = rad_meta_gets(meta, (home + key).c_str(), nullptr); if (!text) return fallback; @@ -1067,33 +1088,44 @@ inline int yarn_from(Geom& g, const RadModelMeta* meta, const char* prefix, int6 }; if (!std::strcmp(type, "linear")) { const double factor = get("factor", 0.0); - if (!std::isfinite(factor) || factor <= 0.0) return RAD_E_INVAL; const float scale = (float)factor; - if (!std::isfinite(scale) || scale <= 0.0f) return RAD_E_INVAL; + if (!std::isfinite(factor) || factor <= 0.0 || !std::isfinite(scale) || scale <= 0.0f) { + fprintf(stderr, "radiance: %s linear rotary scaling (%s) needs a finite factor > 0\n", + who, home.c_str()); + return RAD_E_INVAL; + } + g.yarn = {}; g.rope_scale = scale; return RAD_OK; } if (std::strcmp(type, "yarn")) { - fprintf(stderr, "radiance: %s rotary type '%s' is unsupported\n", prefix, type); + fprintf(stderr, "radiance: %s rotary type '%s' (%s) is not implemented; default, linear " + "and yarn are\n", who, type, type_key.c_str()); return RAD_E_UNSUPPORTED; } + /* `truncate` is a JSON boolean, which the converter writes as 1 or 0; true and false spelled + * out are the same answer. */ + double truncate = 1.0; + if (const char* t = rad_meta_gets(meta, (home + "truncate").c_str(), nullptr)) + truncate = !std::strcmp(t, "true") ? 1.0 : !std::strcmp(t, "false") ? 0.0 : get("truncate", 1.0); const double factor = get("factor", 0.0), original = get("original_max_position_embeddings", 0.0); const double fast = get("beta_fast", 32.0), slow = get("beta_slow", 1.0); const double magnitude = get("attention_factor", factor > 1.0 ? 1.0 + 0.1 * std::log(factor) : 1.0); - const double truncate = get("truncate", 1.0); if (!std::isfinite(factor) || factor < 1.0 || !std::isfinite(original) || original < 1.0 || original != std::floor(original) || (truncate != 0.0 && truncate != 1.0) || !std::isfinite(fast) || !std::isfinite(slow) || slow <= 0.0 || fast < slow || !std::isfinite(magnitude) || magnitude <= 0.0 || !std::isfinite(g.theta) || g.theta <= 1.0f || rot < 2 || (rot & 1)) { - fprintf(stderr, "radiance: %s YaRN needs finite factor >= 1, original context > 0, " - "beta_fast >= beta_slow > 0 and attention_factor > 0\n", prefix); + fprintf(stderr, "radiance: %s YaRN (%s) needs a finite factor >= 1, an integral " + "original_max_position_embeddings > 0, beta_fast >= beta_slow > 0, " + "attention_factor > 0 and truncate true or false\n", who, home.c_str()); return RAD_E_INVAL; } /* mscale variants are a different contract; never silently ignore them. */ for (const char* k : {"mscale", "mscale_all_dim"}) if (rad_meta_gets(meta, (home + k).c_str(), nullptr)) { - fprintf(stderr, "radiance: %s YaRN %s is unsupported; use attention_factor\n", prefix, k); + fprintf(stderr, "radiance: %s YaRN %s%s is not implemented; attention_factor is\n", + who, home.c_str(), k); return RAD_E_UNSUPPORTED; } auto correction = [&](double beta) { @@ -1102,13 +1134,20 @@ inline int yarn_from(Geom& g, const RadModelMeta* meta, const char* prefix, int6 }; double low = correction(fast), high = correction(slow); if (truncate != 0.0) { low = std::floor(low); high = std::ceil(high); } - g.yarn = {(float)factor, (float)std::max(0.0, low), - (float)std::min((double)rot - 1, high), (float)magnitude}; - if (!rad_yarn_valid(g.yarn)) return RAD_E_INVAL; + const RadYarn y = {(float)factor, (float)std::max(0.0, low), + (float)std::min((double)rot - 1, high), (float)magnitude}; + if (!rad_yarn_valid(y)) { + fprintf(stderr, "radiance: %s YaRN (%s) resolves to an empty correction range\n", who, + home.c_str()); + return RAD_E_INVAL; + } + g.yarn = y; g.rope_scale = 1.0f; /* YaRN blends frequencies; it does not divide positions. */ return RAD_OK; } +/* `rope` declared for the geometry's rotary: M-RoPE's mode and sections when the model has them, + * `mode` otherwise. */ inline rad_op decl_rope(RadBuilder* b, const Geom& g, int64_t rows, int64_t head_dim, int64_t n_head, int64_t n_head_kv, int64_t rot, const char* mode) { if (g.rope_mc) diff --git a/arch/common/rad_block_attn_fp8.h b/arch/common/rad_block_attn_fp8.h index 121d0364..4735b664 100644 --- a/arch/common/rad_block_attn_fp8.h +++ b/arch/common/rad_block_attn_fp8.h @@ -121,7 +121,7 @@ inline int AttnFP8::declare(RadBuilder* b, Names& nm, const Geom& geom, int l, r RAD_SHARD_ROW, grp_layer(l), { src.q, src.k, src.v }, w.h, w.qkv, 0, { q_dim, kv_dim, kv_dim })); - op_rope = rw(b, RAD_OP(b, "rope", + op_rope = rw(b, decl_rotary(b, g, "rope", RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("head_dim", g.head_dim), RAD_INT("n_head", g.n_head), RAD_INT("n_head_kv", g.n_head_kv), RAD_F64("theta", g.theta), RAD_F64("scale", g.rope_scale), diff --git a/arch/common/rad_block_dspark.h b/arch/common/rad_block_dspark.h index a913b002..4528da76 100644 --- a/arch/common/rad_block_dspark.h +++ b/arch/common/rad_block_dspark.h @@ -508,7 +508,7 @@ inline int Dspark::declare(RadBuilder* b, Names& nm, const Geom& geom, const Con * THE POSITION IS THE COMMITTED TOKEN'S OWN, which is what makes the split passes equal the * reference's one concatenated rope: it hands `k` the full position range and `q` only the * last `q_len` of it, so a context row at absolute position p is rotated by p either way. */ - op_crope = rw(b, RAD_OP(b, "rope", + op_crope = rw(b, decl_rotary(b, g, "rope", RAD_PARAMS(RAD_RANGE("M", 1, crows), RAD_INT("head_dim", g.head_dim), RAD_INT("n_head", g.n_head_kv), RAD_INT("n_head_kv", 0), RAD_F64("theta", g.theta), RAD_F64("scale", g.rope_scale), @@ -667,14 +667,14 @@ inline int Dspark::declare(RadBuilder* b, Names& nm, const Geom& geom, const Con {w.ffn.q, w.ffn.s}, {w.h.x}); } - op_qrope = rw(b, RAD_OP(b, "rope", + op_qrope = rw(b, decl_rotary(b, g, "rope", RAD_PARAMS(RAD_RANGE("M", 1, qrows), RAD_INT("head_dim", g.head_dim), RAD_INT("n_head", g.n_head), RAD_INT("n_head_kv", 0), RAD_F64("theta", g.theta), RAD_F64("scale", g.rope_scale), RAD_STR("mode", "neox")), RAD_NOWEIGHTS), {w.q}, {w.q}); - op_krope = rw(b, RAD_OP(b, "rope", + op_krope = rw(b, decl_rotary(b, g, "rope", RAD_PARAMS(RAD_RANGE("M", 1, qrows), RAD_INT("head_dim", g.head_dim), RAD_INT("n_head", g.n_head_kv), RAD_INT("n_head_kv", 0), RAD_F64("theta", g.theta), RAD_F64("scale", g.rope_scale), diff --git a/arch/llama_fp8/llama_fp8.cpp b/arch/llama_fp8/llama_fp8.cpp index 342cdefe..00bc5b27 100644 --- a/arch/llama_fp8/llama_fp8.cpp +++ b/arch/llama_fp8/llama_fp8.cpp @@ -185,7 +185,6 @@ static int declare_dspark(RadBuilder* b, const RadModelMeta* meta, const RadBuil dg.eps = (float)rad_meta_getf(meta, "draft.rms_norm_eps", 0.0); dg.theta = (float)rad_meta_getf(meta, "draft.rope_parameters.rope_theta", rad_meta_getf(meta, "draft.rope_theta", 0.0)); - dg.rope_scale = 1.0f; /* PLAIN RMS NORM: the reference norms with Qwen3RMSNorm and the config says `model_type: * qwen3`. It happens to agree with this MiniCPM5 target, and it is asked of the DRAFTER's * config anyway -- two conventions in one process is exactly why this is a second Geom. */ @@ -201,6 +200,7 @@ static int declare_dspark(RadBuilder* b, const RadModelMeta* meta, const RadBuil (long long)dg.head_dim, (long long)dg.n_ff_all, (double)dg.eps, (double)dg.theta); return RAD_E_INVAL; } + RAD_ARCH_TRY(yarn_from(dg, meta, "draft.", dg.head_dim)); int s; if ((s = divide_or_fail(dg.n_head_all, dg.world, "draft num_attention_heads", &dg.n_head)) < 0) return s; if ((s = divide_or_fail(dg.n_head_kv_all, dg.world, "draft num_key_value_heads", &dg.n_head_kv)) < 0) return s; @@ -431,6 +431,7 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c "%lld\n", (long long)m.n_rot, (long long)g.head_dim); return RAD_E_INVAL; } + RAD_ARCH_TRY(yarn_from(g, meta, "", m.n_rot)); /* ---- the KV cache. One group: every layer is full attention. */ RadKVGroupDecl fd{}; diff --git a/arch/qwen35_fp8/qwen35_fp8.cpp b/arch/qwen35_fp8/qwen35_fp8.cpp index fec43f62..f90c7439 100644 --- a/arch/qwen35_fp8/qwen35_fp8.cpp +++ b/arch/qwen35_fp8/qwen35_fp8.cpp @@ -339,7 +339,6 @@ static int declare_dflash2(RadBuilder* b, const RadModelMeta* meta, const RadBui dg.eps = (float)rad_meta_getf(meta, "draft.rms_norm_eps", 0.0); dg.theta = (float)rad_meta_getf(meta, "draft.rope_parameters.rope_theta", rad_meta_getf(meta, "draft.rope_theta", 0.0)); - dg.rope_scale = 1.0f; RAD_ARCH_TRY(yarn_from(dg, meta, "draft.", dg.head_dim)); /* PLAIN RMS NORM. The drafter uses the ordinary norm where the trunk uses GemmaRMSNorm, and * the checkpoint agrees rather than only vLLM -- rad_block_dflash2.h has the numbers. Two diff --git a/cmake/RadAddPlugin.cmake b/cmake/RadAddPlugin.cmake index 9c540986..31d2b745 100644 --- a/cmake/RadAddPlugin.cmake +++ b/cmake/RadAddPlugin.cmake @@ -97,6 +97,22 @@ function(rad_add_plugin name kind) "because the language a project compiles in is the project's own decision.") endif() set_source_files_properties(${P_HIP_SOURCES} PROPERTIES LANGUAGE HIP) + # EVERY DEVICE SOURCE NAMES ITS OWN COMPILATION UNIT ID, from the plugin's name and the + # source's path inside the plugin's directory. Left to clang, the ID is a hash of the absolute + # input path, the output path and the command line, so one source built in two directories -- + # the tree and an out-of-tree build of the same plugin -- gets two IDs. The ID becomes a symbol + # (__hip_cuid_) whose name is not of fixed length, and a longer one shifts the code + # object's read-only data and with it every PC-relative address .text forms into it: the same + # instructions, different bytes. Named from what identifies the unit and nothing about where it + # was built, the ID is the same wherever the plugin is built and still differs between any two + # sources, which is the property clang needs of it. + foreach(src ${P_HIP_SOURCES}) + get_filename_component(_rad_abs "${src}" ABSOLUTE) + file(RELATIVE_PATH _rad_rel "${CMAKE_CURRENT_SOURCE_DIR}" "${_rad_abs}") + string(SHA256 _rad_cuid "${name}/${_rad_rel}") + string(SUBSTRING "${_rad_cuid}" 0 16 _rad_cuid) + set_property(SOURCE "${src}" APPEND PROPERTY COMPILE_OPTIONS "-cuid=${_rad_cuid}") + endforeach() list(APPEND srcs ${P_HIP_SOURCES}) endif() add_library(${name} MODULE ${srcs}) diff --git a/core/config.cpp b/core/config.cpp index 61f36205..66b369b7 100644 --- a/core/config.cpp +++ b/core/config.cpp @@ -29,6 +29,15 @@ struct Flag { const char* help; }; +/* Built from kDraftWindowDefault, so the default is stated in one place. Defined before the table + * that points at it: the two are initialised in this order. */ +static const std::string g_draft_window_help = + std::string("fixed | adaptive: how many drafts each sequence carries. fixed drafts\n" + " --num-speculative-tokens every step; adaptive makes that\n" + " the ceiling and follows each sequence's acceptance and the\n" + " measured step cost (a serial draft head only). Default ") + + draft_window_name(kDraftWindowDefault); + static const Flag g_flags[] = { { "--model", "PATH", "the .rad container, or a checkpoint served as it stands" }, { "--radiance-home", "DIRS", "where kernels/, architectures/ and quantizers/ live; several\n" @@ -101,6 +110,7 @@ static const Flag g_flags[] = { { "--num-speculative-tokens", "N", "speculative window; 0 disables. Default `auto`: the drafter's\n" " own operating point, which the plugin states. A container\n" " with no draft head is unaffected -- auto resolves to 0." }, + { "--draft-window", "MODE", g_draft_window_help.c_str() }, { "--draft-model", "PATH", "NOT IMPLEMENTED -- merge the drafter at convert time" }, { "--host", "ADDR", "listen address" }, @@ -458,6 +468,15 @@ int config_parse(int argc, char** argv, Config* c) { return RAD_E_INVAL; } } + else if (S("--draft-window")) { + if (!(v = need(i))) return RAD_E_INVAL; + if (!std::strcmp(v, "fixed")) c->draft_window = DraftWindow::Fixed; + else if (!std::strcmp(v, "adaptive")) c->draft_window = DraftWindow::Adaptive; + else { + RAD_ERR("--draft-window takes fixed or adaptive, not '%s'", v); + return RAD_E_INVAL; + } + } else if (S("--draft-model")) { /* REFUSED, NOT QUIETLY IGNORED. Accepting this flag and then serving the container's * own drafter -- or none -- would hand the caller a server that is not the one they diff --git a/core/engine.cpp b/core/engine.cpp index 832fbc75..10ffd81e 100644 --- a/core/engine.cpp +++ b/core/engine.cpp @@ -410,8 +410,6 @@ int Engine::step_loop() { * collected, and a pass issued ahead would have overwritten them by then. */ pipelining_ = !cfg_.kld() && drafter_.kind != RAD_DRAFT_BLOCK && (serial_drafting() || cfg_.n_spec <= 0); - /* The adaptive draft window prices serial rounds; a block drafter keeps its depth (scheduler.h). */ - if (drafter_.kind == RAD_DRAFT_BLOCK) sched_.draft_adapt_off("a block drafter"); const bool t_on = (int)log_level() >= (int)Log::Debug; /* Read once. See the block in the idle branch below for what it is for. */ /* "kv" | "arena" | "all" | "kv:," -- the last narrows the KV scrub to named groups, diff --git a/core/engine.h b/core/engine.h index 37e107c8..6366698d 100644 --- a/core/engine.h +++ b/core/engine.h @@ -93,11 +93,13 @@ private: std::vector rank_threads_; Config cfg_; - /* THE SPLIT EACH RANK ACTUALLY TAKES. Equal to cfg_ on every rank of an even world; under an - * uneven one (an attention-zero rank) a rank whose cache costs less per token gets a KV pool - * sized for the common token count and the difference in its weights pool. Engine::plan fills - * it, and every reader of the resolved pool sizes -- Pools::configure and the planner -- takes - * its rank's entry, never cfg_: a grown pool the planner never reads is an empty pool. */ + /* THE SPLIT EACH RANK ACTUALLY TAKES. Equal to cfg_ on every rank whose cache costs as much per + * token as the dearest rank's -- every rank of a world whose ranks all cost the same; a rank + * whose cache costs less (an attention-zero rank) gets a KV pool sized for the common token + * count and the difference in its weights pool. Engine::plan fills it, and every reader of the + * resolved pool sizes -- Pools::configure and the planner -- takes its rank's entry, never + * cfg_: a grown pool the planner never reads is an empty pool, and plan() refuses a rank whose + * plan and weights pool disagree. */ std::vector rank_cfg_; int64_t kv_planned_tokens_ = 0; /* the common token count plan() sized the re-split pools for */ diff --git a/core/engine_bringup.cpp b/core/engine_bringup.cpp index d9b02ba5..24b47c89 100644 --- a/core/engine_bringup.cpp +++ b/core/engine_bringup.cpp @@ -684,7 +684,9 @@ int Engine::read_metadata() { meta_.n_expert_shared = geti("n_expert_shared", 0); meta_.rms_eps = getf("rms_eps", 1e-6); meta_.rope_theta = getf("rope_theta", 10000.0); - meta_.rope_scale = getf("rope_scale", 1.0); + /* 1 (abi/rad_builder.h): the rotary's scaling is read with its type from the free-form keys. + * An older container's `rope_scale` key holds the factor alone and is not read. */ + meta_.rope_scale = 1.0f; /* A container that says nothing about its own shape is refused HERE, by name, rather than at * declare as an "invalid argument" from a plugin that was handed zeros. */ @@ -770,7 +772,6 @@ int Engine::apply_rope_scaling() { return RAD_E_INVAL; } if (spec.kind != RopeSpec::Keep) { - const RopeResolved before = rope_resolve(meta_kv_k_, meta_kv_v_, ""); std::string why; if (!rope_override_group(meta_kv_k_, meta_kv_v_, spec, "", meta_.n_ctx_train, &why)) { RAD_ERR("--rope-scaling %s: %s", cfg_.rope_scaling.c_str(), why.c_str()); @@ -786,9 +787,6 @@ int Engine::apply_rope_scaling() { return RAD_E_INVAL; } } - /* A converter that read a YaRN factor as the linear rope_scale left it there; with YaRN - * taken off it would stay behind as a position divisor. */ - if (spec.kind == RopeSpec::None && before.type == "yarn") meta_.rope_scale = 1.0f; } const RopeResolved r = rope_resolve(meta_kv_k_, meta_kv_v_, ""); @@ -801,7 +799,8 @@ int Engine::apply_rope_scaling() { if (want > reach) RAD_WARN("--max-model-len %lld is past the %lld positions YaRN factor %g covers; " "positions beyond them are extrapolated", want, reach, r.factor); - } else if (meta_.n_ctx_train > 0 && want > meta_.n_ctx_train && !(meta_.rope_scale > 1.0f)) { + } else if (meta_.n_ctx_train > 0 && want > meta_.n_ctx_train && + !(r.type == "linear" && r.factor > 1.0)) { RAD_WARN("--max-model-len %lld is past the %lld positions the model was trained on and " "nothing scales its rotary, so positions beyond them are extrapolated; a model " "card's YaRN extension is --rope-scaling yarn:FACTOR (docs/YARN.md)", want, @@ -1304,59 +1303,96 @@ int Engine::plan() { * Only a DERIVED split under expert_tiered is re-split. An operator who stated either pool * has decided what every card gets. And under `auto` a rank whose grown pool covers all of * its weights would choose all_vram while its peers spill -- one strategy per world is the - * contract that keeps their movers alike, so `auto` keeps the common split. - * Where every rank costs the same -- every world the heads divide -- no rank qualifies and - * rank_cfg_ is cfg_ on every rank, field for field. */ + * contract that keeps their movers alike, so `auto` keeps the common split. A cache that + * does not lend (--deterministic, or a device without virtual memory management) is + * re-split all the same: the bytes it would never page are bytes its experts can hold, and + * without virtual memory management they are allocated whole. + * Where every rank's cache costs the same per token no rank qualifies and rank_cfg_ is cfg_ + * on every rank, field for field. A rank that qualifies and is not re-split is named with + * the reason, once. */ rank_cfg_.assign(ranks_.size(), cfg_); kv_planned_tokens_ = 0; - if (vb.derived_kv && vb.derived_weights && vb.lends && cfg_.placement == "expert_tiered" && - facts.size() == ranks_.size() && ranks_.size() > 1) { - constexpr int64_t kMiB = (int64_t)1 << 20; - int64_t kv_max = 0; - for (const VramFacts& f : facts) kv_max = std::max(kv_max, f.kv_ceiling); + constexpr int64_t kMiB = (int64_t)1 << 20; + int64_t kv_max = 0; + for (const VramFacts& f : facts) kv_max = std::max(kv_max, f.kv_ceiling); + std::vector cheaper; + for (size_t i = 0; i < facts.size() && i < ranks_.size(); ++i) + if (facts[i].kv_ceiling < kv_max) cheaper.push_back(i); + /* Why no cheaper rank is re-split, where the reason holds for the whole world. */ + std::string kept; + std::vector> probes(ranks_.size()); + int64_t T = -1; + if (cheaper.empty()) { + /* Every rank's cache costs the same per token: there is nothing to re-split. */ + } else if (!vb.derived_kv || !vb.derived_weights) { + kept = "a stated --vram-kv-mib or --vram-weights-mib is applied to every card"; + } else if (cfg_.placement != "expert_tiered") { + kept = fmt("--placement %s keeps one split for every card; only expert_tiered re-splits", + cfg_.placement.c_str()); + } else { /* THE TOKEN COUNT configure_kv WILL SETTLE ON: every rank's paged tokens in the common - * pool, the least of them. Asked of the same plan configure_kv makes. */ - std::vector> probes(ranks_.size()); - int64_t T = -1; + * pool, the least of them. Asked of the same plan configure_kv makes, against the same + * bytes: nothing reserves from a KV pool before its carve. */ for (size_t i = 0; i < ranks_.size(); ++i) { probes[i] = std::make_unique(); - if (probes[i]->plan_groups(ranks_[i]->program.kv_groups, cfg_) != RAD_OK) { T = -1; break; } + if (probes[i]->plan_groups(ranks_[i]->program.kv_groups, cfg_) != RAD_OK) { + kept = fmt("rank %d's KV groups did not plan", ranks_[i]->index); + break; + } const int64_t t = probes[i]->paged_tokens(cfg_.vram_kv_mib * kMiB); if (t == KVManager::kUnboundedTokens) continue; if (T < 0 || t < T) T = t; } - for (size_t i = 0; T > 0 && i < ranks_.size(); ++i) { - if (facts[i].kv_ceiling >= kv_max) continue; - const int64_t need = probes[i]->pool_mib_for_tokens(T, cfg_.vram_kv_mib); - if (need < 0) continue; - /* A MARGIN, so that whatever the pool's own alignment costs never makes the re-split - * rank the least: configure_kv names it if it ever does. */ - const int64_t need_m = std::min(need + 64, cfg_.vram_kv_mib); - const int64_t cap = facts[i].statics + expert_vram_floor(wfs[i], 0); - const RankSplit rs = - vram_budget_rank_split(cfg_.vram_weights_mib, cfg_.vram_kv_mib, need_m, cap); - if (!rs.resplit) continue; - rank_cfg_[i].vram_kv_mib = rs.kv_mib; - rank_cfg_[i].vram_weights_mib = rs.weights_mib; - kv_planned_tokens_ = T; - RAD_INFO("kv: rank %d's cache costs %s at the worst case where its dearest peer's costs " - "%s, so its KV pool pages the common %lld tokens in %lld MiB instead of %lld; " - "its weights pool grows %lld -> %lld MiB (every weight resident wants %s)%s", - ranks_[i]->index, humanb(facts[i].kv_ceiling).c_str(), humanb(kv_max).c_str(), - (long long)T, (long long)rs.kv_mib, (long long)cfg_.vram_kv_mib, - (long long)cfg_.vram_weights_mib, (long long)rs.weights_mib, - humanb(cap).c_str(), - rs.unusable_mib > 0 - ? fmt(", and %lld MiB stay unclaimed", (long long)rs.unusable_mib).c_str() - : ""); + if (kept.empty() && T <= 0) + kept = "no rank's cache pages a bounded token count in the common pool"; + } + for (size_t i : cheaper) { + const std::string costs = + fmt("kv: rank %d's cache costs %s at the worst case where its dearest peer's costs " + "%s", ranks_[i]->index, humanb(facts[i].kv_ceiling).c_str(), + humanb(kv_max).c_str()); + if (!kept.empty()) { + RAD_INFO("%s; its pools stay the common split: %s", costs.c_str(), kept.c_str()); + continue; } + /* EXACT, NO MARGIN: paged_split is monotone in the budget, so the least whole MiB that + * pages T here pages at least T in configure_kv -- the same plan, the same arithmetic, + * a pool whose budget is exactly this many MiB. The pool's own alignment is already + * among the plan's fixed costs (a RAD_ALIGN_UNIT a reservation). */ + const int64_t need = probes[i]->pool_mib_for_tokens(T, cfg_.vram_kv_mib); + if (need < 0) { + RAD_INFO("%s; its pools stay the common split: it does not page the common %lld " + "tokens in the common %lld MiB", costs.c_str(), (long long)T, + (long long)cfg_.vram_kv_mib); + continue; + } + const int64_t cap = facts[i].statics + expert_vram_floor(wfs[i], 0); + const RankSplit rs = + vram_budget_rank_split(cfg_.vram_weights_mib, cfg_.vram_kv_mib, need, cap); + if (!rs.resplit) { + RAD_INFO("%s; its pools stay the common split: it needs all %lld MiB of the common " + "pool to page the common %lld tokens", costs.c_str(), + (long long)cfg_.vram_kv_mib, (long long)T); + continue; + } + rank_cfg_[i].vram_kv_mib = rs.kv_mib; + rank_cfg_[i].vram_weights_mib = rs.weights_mib; + kv_planned_tokens_ = T; + RAD_INFO("%s, so its KV pool pages the common %lld tokens in %lld MiB instead of %lld; " + "its weights pool grows %lld -> %lld MiB (every weight resident wants %s)%s", + costs.c_str(), (long long)T, (long long)rs.kv_mib, (long long)cfg_.vram_kv_mib, + (long long)cfg_.vram_weights_mib, (long long)rs.weights_mib, + humanb(cap).c_str(), + rs.unusable_mib > 0 + ? fmt(", and %lld MiB stay unclaimed", (long long)rs.unusable_mib).c_str() + : ""); } } for (size_t ri = 0; ri < ranks_.size(); ++ri) { auto& r = ranks_[ri]; - /* This rank's own split: cfg_ itself on an even world, its re-split copy otherwise. */ - const Config& rc = rank_cfg_.size() == ranks_.size() ? rank_cfg_[ri] : cfg_; + /* This rank's own split: cfg_ field for field unless the block above re-split it. */ + const Config& rc = rank_cfg_[ri]; LogRank lr(r->index); /* THE MAIN THREAD RUNS EVERY RANK'S STARTUP, one rank after another, and every allocator * reachable from here takes the CURRENT device -- rad_dev_alloc has no device argument. @@ -1394,6 +1430,16 @@ int Engine::plan() { if (cfg_.debug_placement || s < 0) fputs(r->plan.report(r->program).c_str(), stderr); if (s < 0) return s; + /* The slab the planner filled is the pool the mover will fill: one split, read twice. */ + RAD_TRY(plan_check_weights_pool(r->plan, r->pools.weights().bytes())); + /* --host-pool-mib IS ONE NUMBER FOR EVERY RANK, allocated whole by Pools::configure, so a + * re-split rank whose grown weights pool took every weight still holds it. Said, because + * the `kv: rank N ...` line above reads as if that rank's memory were all accounted for. */ + if (rc.vram_kv_mib < cfg_.vram_kv_mib && cfg_.host_pool_mib > 0 && + r->plan.totals.bytes[(int)Tier::HostPinned] == 0) + RAD_INFO("kv: rank %d's weights all fit its grown weights pool, so its pinned host pool " + "holds no weights; its %lld MiB (--host-pool-mib) stay allocated, as on every " + "rank", r->index, (long long)cfg_.host_pool_mib); } return RAD_OK; } @@ -2680,23 +2726,25 @@ int Engine::configure_kv() { * every rank is capped at the least before any carves. At an even split the cap is what * every rank would have carved anyway. */ int64_t least = -1; - size_t least_pos = 0; for (size_t i = 0; i < ranks_.size(); ++i) { auto& r = ranks_[i]; LogRank lr(r->index); RAD_TRY(r->kv.plan_groups(r->program.kv_groups, cfg_)); const int64_t t = r->kv.paged_tokens(r->pools.kv().avail()); + /* A RANK plan() RE-SIZED FOR THE COMMON TOKEN COUNT PAGES AT LEAST THAT COUNT, by + * construction: its pool is the least whole MiB whose paged split reaches it, asked of the + * same plan against the same bytes. Short of it, this rank would set every rank's count + * under what its dearest peer pages -- a broken construction, refused by name. */ + if (rank_cfg_[i].vram_kv_mib < cfg_.vram_kv_mib && t < kv_planned_tokens_) { + RAD_ERR("kv: rank %d's KV pool was sized at %lld MiB to page the common %lld tokens " + "and pages %lld: the pool and the plan that sized it disagree", r->index, + (long long)rank_cfg_[i].vram_kv_mib, (long long)kv_planned_tokens_, + (long long)t); + return RAD_E_STATE; + } if (t == KVManager::kUnboundedTokens) continue; /* holds no cache; limits nobody */ - if (least < 0 || t < least) { least = t; least_pos = i; } + if (least < 0 || t < least) least = t; } - /* A RANK plan() RE-SIZED FOR THE COMMON TOKEN COUNT MUST NEVER BE THE ONE THAT SETS IT: its pool - * was sized, with a margin, to page what its dearest peer pages. If it does bind, every rank - * carves fewer tokens than its dearest peer could -- named, because nothing else would show it. */ - if (least >= 0 && least_pos < rank_cfg_.size() && - rank_cfg_[least_pos].vram_kv_mib < cfg_.vram_kv_mib) - RAD_WARN("kv: rank %d's re-sized pool pages %lld tokens and binds every rank (planned for " - "%lld); its margin was short", ranks_[least_pos]->index, (long long)least, - (long long)kv_planned_tokens_); for (auto& r : ranks_) { LogRank lr(r->index); RAD_TRY(rad_dev_set(r->device)); /* allocates on the CURRENT device; see plan() */ @@ -2869,16 +2917,20 @@ int Engine::configure_kv() { sc.n_ckpt_slots = ck_disk_slots; sc.n_block_slots = ((cfg_.prefix_cache_disk_mib << 20) - ck_disk_slots * ck_slot_bytes) / slot_bytes; - /* THE FINGERPRINT IS WHAT MAKES A PERSISTED STORE SAFE. A cached block is only - * meaningful under the container and layout that produced it; read back under - * another, it is a lossless function of somebody else's weights. */ /* THE FINGERPRINT IS WHAT MAKES A PERSISTED STORE SAFE, and it is built from * everything a cached block's bytes actually depend on. Read back under a - * different container, layout or KV width, a block is a lossless function of - * somebody else's weights -- fluent, confident and wrong. There is no cheaper - * check than refusing the whole store, because a block carries no evidence of - * what produced it beyond the tokens it was keyed by. */ + * different container, layout, KV width or rotary, a block is a lossless + * function of somebody else's weights -- fluent, confident and wrong. There is + * no cheaper check than refusing the whole store, because a block carries no + * evidence of what produced it beyond the tokens it was keyed by. + * + * The rotary is the RESOLVED one, target and drafter, after --rope-scaling + * (rope_identity): a YaRN container converted from the same checkpoint has the + * same name and shapes as the stock one, and every cached key and QSA block key + * is rotated differently under it. */ { + char theta[32]; + std::snprintf(theta, sizeof theta, "%.9g", (double)meta_.rope_theta); const std::string id = std::string(meta_.name ? meta_.name : "") + "|" + std::string(meta_.arch_id ? meta_.arch_id : "") + "|" + @@ -2887,10 +2939,7 @@ int Engine::configure_kv() { std::to_string(slot_bytes) + "|" + std::to_string(ck_slot_bytes) + "|" + cfg_.kv_cache_dtype + "|" + std::to_string((long long)ranks_.size()) + "|" + RAD_VERSION + - /* --rope-scaling rotates every cached key differently. Appended only - * when given, so a store written without it keeps its fingerprint. */ - (cfg_.rope_scaling.empty() ? std::string() - : "|rope=" + cfg_.rope_scaling); + "|theta=" + theta + "|" + rope_identity(meta_kv_k_, meta_kv_v_); uint64_t h = 1469598103934665603ull; for (char c : id) { h ^= (unsigned char)c; h *= 1099511628211ull; } sc.fingerprint = h; @@ -3491,7 +3540,8 @@ int Engine::start() { const int64_t MiB = 1024 * 1024; const int64_t headroom = std::max(cfg_.gpu_headroom_mib, 0) * MiB; const int64_t allowance = Config{}.gpu_headroom_mib * MiB; - for (auto& r : ranks_) { + for (size_t ri = 0; ri < ranks_.size(); ++ri) { + auto& r = ranks_[ri]; LogRank lr(r->index); RadDeviceProps props{}; if (rad_dev_props(r->device, &props) < 0 || props.vram_bytes <= 0) continue; @@ -3534,8 +3584,19 @@ int Engine::start() { * byte -- a small dense model already at the largest context its cache can hold * leaves most of the card -- and vram_budget.cpp printed it on the `unusable` line. * Warning here would contradict that receipt and send the reader looking for a flag - * to set. */ - if (cfg_.vram_budget_derived) + * to set. A rank plan() re-split for the common token count also leaves what its + * weights could not use, and the `kv: rank N ...` line said how much. */ + const int64_t unclaimed_mib = + (cfg_.vram_weights_mib + cfg_.vram_kv_mib) - + (rank_cfg_[ri].vram_weights_mib + rank_cfg_[ri].vram_kv_mib); + if (cfg_.vram_budget_derived && unclaimed_mib > 0) + RAD_INFO("card %d settled at %s free against %s. The split accounted for it: %lld " + "MiB are what rank %d's weights could not use of what its KV pool did not " + "need, on the `kv: rank %d ...` line above, and the rest is the `unusable` " + "line of the VRAM budget.", + r->device, humanb(got).c_str(), asked.c_str(), (long long)unclaimed_mib, + r->index, r->index); + else if (cfg_.vram_budget_derived) RAD_INFO("card %d settled at %s free against %s. The split accounted for it: " "neither pool could address another byte, which is the `unusable` line of " "the VRAM budget above.", diff --git a/core/format/checkpoint.cpp b/core/format/checkpoint.cpp index dab2b5ed..cf83b9a4 100644 --- a/core/format/checkpoint.cpp +++ b/core/format/checkpoint.cpp @@ -253,7 +253,10 @@ int meta_from_config_json(const std::string& dir, MetaOwn* out) { m.n_expert_shared = gi("shared_expert_intermediate_size", 0) ? 1 : 0; m.rms_eps = (float)gf("rms_norm_eps", 1e-6); m.rope_theta = (float)gf("rope_parameters.rope_theta", gf("rope_theta", 10000.0)); - m.rope_scale = (float)gf("rope_scaling.factor", 1.0); + /* The rotary's scaling stays in the free-form keys with its type beside it; the architecture + * reads both (arch/common/rad_arch.h, yarn_from). A factor copied here without its type would + * be a linear position divide for llama3, longrope or YaRN. */ + m.rope_scale = 1.0f; /* The quantisation descriptor, which with the architecture selects the plugin (spec §2.4). * "fp8" plus its format, so qwen35_fp8 and qwen35_bf16 are told apart by the checkpoint @@ -300,7 +303,48 @@ int meta_from_gguf(GgufFile& g, MetaOwn* out) { m.n_expert_shared= gi("expert_shared_count", 0); m.rms_eps = (float)gf("attention.layer_norm_rms_epsilon", 1e-6); m.rope_theta = (float)gf("rope.freq_base", 10000.0); - m.rope_scale = (float)gf("rope.scaling.factor", 1.0); + m.rope_scale = 1.0f; + + /* THE ROTARY'S SCALING, written as the config.json keys a safetensors release carries, because + * those are what the architecture reads (arch/common/rad_arch.h, yarn_from) -- one reader, so + * a type is refused or honoured by name whichever file it came from. llama.cpp's YaRN + * attn_factor, ext_factor and log multiplier modify the magnitude and ramp in ways config.json + * has no spelling for, so a file that sets one to anything but its neutral value is refused + * here rather than served as plain YaRN. */ + { + /* llama.cpp's reading: the type defaults to linear, the factor is rope.scaling.factor or + * the older rope.scale_linear, and 0 or 1 is no scaling. */ + std::string type = "linear"; + g.get_str(a + ".rope.scaling.type", &type); + const double factor = gf("rope.scaling.factor", gf("rope.scale_linear", 0.0)); + const bool none = type == "none" || (type == "linear" && (factor == 0.0 || factor == 1.0)); + if (!none) { + const struct { const char* key; double neutral; } kOff[] = { + { "rope.scaling.attn_factor", 1.0 }, { "rope.scaling.yarn_attn_factor", 1.0 }, + { "rope.scaling.yarn_log_multiplier", 0.0 } }; + for (const auto& k : kOff) + if (gf(k.key, k.neutral) != k.neutral) { + RAD_ERR("the GGUF sets %s.%s = %g; that modifier of the rotary's scaling is " + "not implemented", a.c_str(), k.key, gf(k.key, k.neutral)); + return RAD_E_UNSUPPORTED; + } + const double ext = gf("rope.scaling.yarn_ext_factor", -1.0); + if (ext >= 0.0 && ext != 1.0) { + RAD_ERR("the GGUF sets %s.rope.scaling.yarn_ext_factor = %g; YaRN is implemented " + "at extrapolation factor 1", a.c_str(), ext); + return RAD_E_UNSUPPORTED; + } + out->kv("rope_scaling.type", type); + if (factor != 0.0) out->kv("rope_scaling.factor", fmt("%.17g", factor)); + const int64_t orig = gi("rope.scaling.original_context_length", 0); + if (orig > 0) out->kv("rope_scaling.original_max_position_embeddings", + fmt("%lld", (long long)orig)); + for (const char* beta : { "beta_fast", "beta_slow" }) { + const double v = gf((std::string("rope.scaling.yarn_") + beta).c_str(), 0.0); + if (v != 0.0) out->kv(std::string("rope_scaling.") + beta, fmt("%.17g", v)); + } + } + } std::vector toks; if (g.get_strs("tokenizer.ggml.tokens", &toks)) m.n_vocab = (int64_t)toks.size(); diff --git a/core/format/rope_override.h b/core/format/rope_override.h index 7f3fa34a..3151b5bf 100644 --- a/core/format/rope_override.h +++ b/core/format/rope_override.h @@ -16,6 +16,7 @@ * (tests/yarn_test.cpp holds them to yarn_from). */ #pragma once +#include #include #include #include @@ -141,7 +142,8 @@ inline bool rope_override_group(std::vector& k, std::vector& k, std::vector.rope.* -- as key=value, sorted, after + * --rope-scaling has been applied. A persisted prefix store is fingerprinted with it: a container + * converted from the same checkpoint with YaRN has the same name and the same shapes, and its + * stock-rotated keys (and QSA block keys) read back under YaRN would be attended to at the wrong + * angles. Key order and unrelated keys do not change it. */ +inline std::string rope_identity(const std::vector& k, + const std::vector& v) { + auto names_rotary = [](const std::string& key) { + size_t s = 0; + while (s <= key.size()) { + size_t e = key.find('.', s); + if (e == std::string::npos) e = key.size(); + const std::string part = key.substr(s, e - s); + if (part.find("rope") != std::string::npos || part.find("rotary") != std::string::npos) + return true; + s = e + 1; + } + return false; + }; + std::vector rows; + for (size_t i = 0; i < k.size() && i < v.size(); ++i) + if (names_rotary(k[i])) rows.push_back(k[i] + "=" + v[i]); + std::sort(rows.begin(), rows.end()); + std::string out; + for (const auto& r : rows) { out += r; out += ';'; } + return out; +} + } /* namespace rad */ diff --git a/core/place/planner.cpp b/core/place/planner.cpp index 15845239..ded8ae94 100644 --- a/core/place/planner.cpp +++ b/core/place/planner.cpp @@ -113,6 +113,15 @@ int64_t expert_vram_floor(const WeightFootprint& wf, int64_t host_room) { return std::min(all, std::max(all + 2 * wf.expert_unit_max - host_room, 0)); } +int plan_check_weights_pool(const Plan& p, int64_t pool_bytes) { + if (p.vram_budget == pool_bytes) return RAD_OK; + RAD_ERR("placement: rank %d planned against a %s weights budget (%lld bytes) and its weights " + "pool holds %s (%lld bytes): the planner and the pool were given different splits", + p.rank, humanb(p.vram_budget).c_str(), (long long)p.vram_budget, + humanb(pool_bytes).c_str(), (long long)pool_bytes); + return RAD_E_STATE; +} + void weight_footprint(const Program& prog, WeightFootprint* out, TablePlacement embedding, TablePlacement ngram) { if (!out) return; diff --git a/core/place/planner.h b/core/place/planner.h index d7043215..ade2fe35 100644 --- a/core/place/planner.h +++ b/core/place/planner.h @@ -416,6 +416,13 @@ int64_t file_stage_reserve(int64_t widest, bool routed); * A weight budget of the statics and this much places every expert in VRAM or the host pool. */ int64_t expert_vram_floor(const WeightFootprint& wf, int64_t host_room); +/* THE PLAN AND ITS RANK'S WEIGHTS POOL READ ONE SPLIT. The planner places against + * Plan::vram_budget and the mover fills the pool Pools::configure allocated, each from the Config + * it was handed; a rank given two different Configs plans a slab the pool does not have, or leaves + * part of the pool unplanned and empty, and nothing downstream would say so. RAD_E_STATE, naming + * the rank and both sizes, when they differ. */ +int plan_check_weights_pool(const Plan& p, int64_t pool_bytes); + /* THE TABLES THE PLACEMENT FLAGS MOVE, by what reads them and not by name: a model-level weight * that at least one op reads and that only `embed_lookup` reads is a token embedding table, and * one only `embed_lookup_q` reads is an n-gram table. A lookup reads M rows a step, which is what diff --git a/core/rad_core.h b/core/rad_core.h index a6fc5f89..72a3d660 100644 --- a/core/rad_core.h +++ b/core/rad_core.h @@ -129,6 +129,27 @@ enum class Tier { VRAM = 0, HostPinned = 1, HostPageable = 2, SSD = 3, Mapped = * decides it and writes the answer into Config, so every rank plans from one choice. */ enum class TablePlacement { Auto = 0, Vram = 1, Ram = 2, Disk = 3 }; const char* table_placement_name(TablePlacement p); +/* --draft-window: how many drafts a step verifies for each sequence (core/sched/draft.h). + * Fixed every sequence drafts --num-speculative-tokens. + * Adaptive that is the ceiling; each step's head depth and each sequence's stop follow the + * measured acceptance and the measured step cost. A serial drafter's policy only. + * The default is kDraftWindowDefault, and the help text and the startup line read it from there. + * + * FIXED IS THE DEFAULT BECAUSE ADAPTIVE LOSES WHERE A SERVER IS BUSY. Qwen3.8-Flash-Next at TP2 on + * two R9700, distinct prompts a sequence, tokens a second over a steady window against fixed depth 3: + * + * concurrent sequences 1 2 4 8 + * adaptive, ceiling 3 +0.4% -0.1% +1.7% -0.3% + * adaptive, ceiling 5 +7.1% -2.3% +7.4% -11.9% + * + * At eight the head runs five rounds for little more text: 43.0 against 36.3 ms a step (1.18x) for + * 23.3 against 22.3 tokens. The cost model's prior puts depth 5 over depth 3 at 1.10x, and a depth + * not yet timed at a load is priced on the prior. */ +enum class DraftWindow { Fixed = 0, Adaptive = 1 }; +constexpr DraftWindow kDraftWindowDefault = DraftWindow::Fixed; +inline const char* draft_window_name(DraftWindow w) { + return w == DraftWindow::Adaptive ? "adaptive" : "fixed"; +} enum class Site { Device = 0, DeviceStaged = 1, DeviceZeroCopy = 2, Host = 3 }; /* Tier-indexed arrays are sized by this rather than by a literal: the report, the live view and @@ -638,9 +659,10 @@ struct Config { * what the driver already holds is measured rather than assumed. core/mem/vram_budget.cpp does * that sum and writes the answer back into the two fields below, so every consumer downstream * -- the planner, the KV manager, the pool report -- reads one resolved number and there is no - * second place for them to disagree. Under an uneven world Engine::plan derives each rank's own - * copy from it (rank_cfg_, see docs/TP3.md §1), and the rank's pools and planner both read that - * copy -- still one number per card, and the same number on every card of an even world. + * second place for them to disagree. Engine::plan derives each rank's own copy from it + * (rank_cfg_, see docs/TP3.md §1), and the rank's pools and planner both read that copy -- + * still one number per card, and the same number on every card whose cache costs as much per + * token as the dearest rank's. * * Zero means "derive"; a stated --vram-weights-mib / --vram-kv-mib still wins outright. */ int64_t vram_weights_mib = 0; @@ -727,8 +749,8 @@ struct Config { * code object, which HIP loads on the kernel's FIRST LAUNCH and not at startup, and which * faults inside the HIP runtime rather than returning an error when there is no room. */ - /* The speculative window, and the whole of the draft policy: the depth is constant and this - * is it. See core/sched/draft.h. */ + /* The speculative window: the depth, or under --draft-window adaptive its ceiling. See + * core/sched/draft.h. */ /* -1 is `auto`: engine_bringup asks rad_arch_probe for the depth THIS container's drafter was * trained at, and a container with no draft head resolves to 0 with a line saying so. * @@ -738,6 +760,7 @@ struct Config { * paying for it again in VRAM, and then running at one token a step -- a small fraction of * the throughput the same container gives at its declared depth. */ int n_spec = -1; /* --num-speculative-tokens; -1 = auto, 0 = off */ + DraftWindow draft_window = kDraftWindowDefault; /* --draft-window */ /* ---------------- KL DIVERGENCE AGAINST A REFERENCE (core/kld.h) ---------------- * diff --git a/core/sched/batch.h b/core/sched/batch.h index bfacbfcb..d238e71a 100644 --- a/core/sched/batch.h +++ b/core/sched/batch.h @@ -73,6 +73,10 @@ struct StepEntry { bool draft_out = false; bool is_prefill = false; bool produces_token = false; /* this entry's last token is the sequence's last */ + /* ITS DRAFTS CAME FROM PROMPT LOOKUP, not from the declared drafter. Their outcome is counted + * in the reported acceptance like any other, and kept out of the adaptive window's estimate, + * which describes the draft head (DraftController, draft.h). */ + bool ngram = false; /* A DEVICE ROW: the card fills its tokens, positions, rollback count and slots from the slot's * StepSlot before the pass runs (advance.h), and what the host staged for them is replaced. * `ctx_len` is then an upper bound, which is what the host-side ceilings read. The other three diff --git a/core/sched/draft.cpp b/core/sched/draft.cpp index 34ac12f2..9b8a146f 100644 --- a/core/sched/draft.cpp +++ b/core/sched/draft.cpp @@ -1,23 +1,35 @@ -/* draft.cpp -- the static draft-depth policy, and the adaptive one RADIANCE_DRAFT_ADAPT turns on. - * See draft.h for why the first is the default and what the second changes. +/* draft.cpp -- the static draft-depth policy, and the adaptive one `--draft-window adaptive` turns + * on. See draft.h for why the first is the default and what the second changes. */ #include "draft.h" #include #include #include -#include -#include namespace rad { namespace { -/* THE COST MODEL WHEN NO TABLE IS GIVEN: every draft adds six percent to a step -- one round of - * the head (about 0.4 ms profiled on QFN at tp4) and one more verify row, against a step of about - * 12 ms. A guess at a shape, not a measurement of any deployment: one that has measured its own - * steps by depth gives them as RADIANCE_DRAFT_COST. */ +/* THE STEP-COST PRIOR, a shape and not a measurement: every draft adds six percent to a step -- + * one round of a serial head and one more verify row against a weight-bound step. It prices a + * depth nothing has measured yet, and only its RATIOS are read once anything has been measured: + * an unmeasured depth is the nearest measured one scaled by prior(d) / prior(d'). */ constexpr double kCostSlope = 0.06; +double cost_prior(int d) { return 1.0 + kCostSlope * (double)d; } + +/* A STEP TIME'S MEAN IS A RUNNING MEAN FOR ITS FIRST STEPS AND THEN A MOVING AVERAGE WITH A + * HALF-LIFE OF 32 STEPS. Long enough that the scatter of one step against the next -- a tier + * copy, a recording, a prefix-cache trim -- averages out; short enough to follow a context that + * grows by a few thousand tokens over a reply. */ +constexpr double kCostHalfLife = 32.0; +/* AND ONE STEP COUNTS FOR AT MOST FOUR TIMES THE KEY'S MEAN, once the mean is over four steps. A + * step that waited on something other than its own work -- a first recording, an allocation, a + * host stall -- says nothing about what its depth costs, and one such step would otherwise move a + * short mean by more than any depth. A mean over fewer steps is not yet a reference to clip + * against: its first step may itself be the odd one. */ +constexpr double kCostOutlier = 4.0; +constexpr int64_t kCostClipAfter = 4; /* THE PRIOR WEIGHS AS MUCH AS THIS MANY VERIFIES. The (n+1)-th real one moves an estimate by * 1 / (n + k + 1) -- a running mean over the prior and the evidence -- until that step falls below @@ -26,169 +38,37 @@ constexpr double kCostSlope = 0.06; * a short answer. */ constexpr double kPriorWeight = 4.0; -/* A POSITION THE DEPLOYMENT HAS EVALUATED FEWER TIMES THAN THIS starts a new sequence from the - * fixed prior rather than from the global count: a rate over a handful of verifies is noise, and a - * position nobody has drafted to is no rate at all. */ -constexpr int64_t kGlobalMinReach = 64; - -/* RADIANCE_DRAFT_COST="1:10.6,2:11.3,3:12.0": depth:cost pairs, comma separated. Returns how many - * it read, or -1 when the string is not that. */ -int parse_cost(const char* s, float* cost) { - int n = 0; - const char* p = s; - for (;;) { - while (*p == ' ') ++p; - if (!*p) return n; - char* end = nullptr; - const long d = std::strtol(p, &end, 10); - if (end == p || *end != ':') return -1; - p = end + 1; - const double v = std::strtod(p, &end); - if (end == p) return -1; - if (d < 1 || d > RAD_SCHED_MAX_SPEC || !std::isfinite(v) || !(v > 0.0)) return -1; - cost[d] = (float)v; - ++n; - p = end; - while (*p == ' ') ++p; - if (*p == ',') ++p; - else if (*p) return -1; - } -} - -/* One knob, or the default with a warning when it does not parse. A knob that is silently not - * read is the one that gets reported as "made no difference". */ -void env_real(const char* name, double lo, double hi, float* out) { - const char* v = std::getenv(name); - if (!v || !*v) return; - char* end = nullptr; - const double x = std::strtod(v, &end); - if (end == v || *end || !(x >= lo && x <= hi)) { - RAD_WARN("%s=%s is not a number in [%g, %g]; keeping %g", name, v, lo, hi, (double)*out); - return; - } - *out = (float)x; -} - -void env_int(const char* name, long lo, long hi, int* out) { - const char* v = std::getenv(name); - if (!v || !*v) return; - char* end = nullptr; - const long x = std::strtol(v, &end, 10); - if (end == v || *end || x < lo || x > hi) { - RAD_WARN("%s=%s is not an integer in [%ld, %ld]; keeping %d", name, v, lo, hi, *out); - return; - } - *out = (int)x; -} } /* namespace */ -void DraftController::from_env(Params* p) { - static const Params env = [] { - Params e; - const char* on = std::getenv("RADIANCE_DRAFT_ADAPT"); - e.adapt = on && *on && *on != '0'; - if (const char* c = std::getenv("RADIANCE_DRAFT_COST"); c && *c) { - const int n = parse_cost(c, e.cost); - if (n < 2) { - RAD_WARN("RADIANCE_DRAFT_COST=\"%s\" %s; the adaptive draft depth prices a step at " - "1 + %.2f per draft instead", c, - n < 0 ? "is not a list of depth:cost pairs" - : "has fewer than two depth:cost pairs", kCostSlope); - std::memset(e.cost, 0, sizeof e.cost); - } - } - env_real("RADIANCE_DRAFT_ADAPT_HYST", 0.0, 10.0, &e.hyst); - env_int ("RADIANCE_DRAFT_ADAPT_HOLD", 0, 1000000, &e.hold); - env_real("RADIANCE_DRAFT_ADAPT_HALFLIFE", 0.5, 1e6, &e.half_life); - env_real("RADIANCE_DRAFT_ADAPT_PRIOR", 0.0, 1.0, &e.prior); - env_int ("RADIANCE_DRAFT_ADAPT_MIN", 1, RAD_SCHED_MAX_SPEC, &e.min_depth); - env_int ("RADIANCE_DRAFT_ADAPT_BUSY", 0, 1000000, &e.busy); - env_int ("RADIANCE_DRAFT_ADAPT_BUSY_DEPTH", 1, RAD_SCHED_MAX_SPEC, &e.busy_depth); - return e; - }(); - p->adapt = env.adapt; - std::memcpy(p->cost, env.cost, sizeof p->cost); - p->hyst = env.hyst; - p->hold = env.hold; - p->half_life = env.half_life; - p->prior = env.prior; - p->min_depth = env.min_depth; - p->busy = env.busy; - p->busy_depth = env.busy_depth; -} - int DraftController::configure(const Params& p) { if (p.max_depth < 0 || p.max_depth > RAD_SCHED_MAX_SPEC) { RAD_ERR("draft depth %d is outside 0..%d -- the verify kernel band is %d query rows", p.max_depth, RAD_SCHED_MAX_SPEC, RAD_SCHED_MAX_SPEC); return RAD_E_INVAL; } - if (p.adapt && (!(p.hyst >= 0.0f) || p.hold < 0 || !(p.half_life > 0.0f) || - !(p.prior >= 0.0f && p.prior <= 1.0f) || p.min_depth < 1 || p.busy < 0 || - p.busy_depth < 1)) { - RAD_ERR("adaptive draft depth: hysteresis %g, hold %d, half-life %g, prior %g, minimum %d, " - "busy %d, busy depth %d -- they want >= 0, >= 0, > 0, 0..1, >= 1, >= 0 and >= 1", - (double)p.hyst, p.hold, (double)p.half_life, (double)p.prior, p.min_depth, p.busy, - p.busy_depth); - return RAD_E_INVAL; - } p_ = p; tot_drafted_ = tot_accepted_ = 0; for (int j = 0; j < RAD_SCHED_MAX_SPEC; ++j) { reach_[j] = 0; hit_[j] = 0; } seen_ = 0; adapt_ = p.adapt && p.max_depth > 0; - alpha_ = 0.0; - for (int d = 0; d <= RAD_SCHED_MAX_SPEC; ++d) { cost_[d] = 0.0; by_depth_[d] = 0; } - /* SAID, because a switch that does nothing reads exactly like a switch that made no - * difference. */ - if (p.adapt && !adapt_) - RAD_WARN("RADIANCE_DRAFT_ADAPT is set and this deployment does not speculate " - "(--num-speculative-tokens 0): there is no window to adapt"); - if (!adapt_) return RAD_OK; - if (p_.min_depth > p_.max_depth) p_.min_depth = p_.max_depth; - alpha_ = 1.0 - std::exp2(-1.0 / (double)p_.half_life); - - /* THE COST TABLE, resolved for every window the policy may choose. A point that was measured - * is taken as it is; between two points the line between them; past the ends the line through - * the nearest two -- a table for depths 1..6 still prices depth 8 the way its own last two - * steps say. A table that extrapolates to a cost at or below zero is a table with the wrong - * sign in it, and the default is the better guess. */ - int pd[RAD_SCHED_MAX_SPEC]; - double pv[RAD_SCHED_MAX_SPEC]; - int np = 0; - for (int d = 1; d <= RAD_SCHED_MAX_SPEC; ++d) - if (std::isfinite(p.cost[d]) && p.cost[d] > 0.0f) { - pd[np] = d; - pv[np] = (double)p.cost[d]; - ++np; - } - bool table = np >= 2; - for (int d = 1; table && d <= p_.max_depth; ++d) { - int i = 0; - while (i + 2 < np && pd[i + 1] < d) ++i; - const double t = (double)(d - pd[i]) / (double)(pd[i + 1] - pd[i]); - cost_[d] = pv[i] + t * (pv[i + 1] - pv[i]); - if (!(cost_[d] > 0.0)) { - RAD_WARN("adaptive draft depth: the cost table extrapolates to %g at depth %d; pricing " - "a step at 1 + %.2f per draft instead", cost_[d], d, kCostSlope); - table = false; - } + alpha_ = 1.0 - std::exp2(-1.0 / kHalfLife); + step_d_ = 0; + step_w_ = 0; + step_held_ = 0; + prior_n_ = 0; + for (int j = 0; j < RAD_SCHED_MAX_SPEC; ++j) prior_[j] = 0.0f; + for (int d = 0; d <= RAD_SCHED_MAX_SPEC; ++d) { + by_depth_[d] = 0; + for (int c = 0; c < kCostClasses; ++c) { cost_ms_[d][c] = 0.0; cost_n_[d][c] = 0; } } - if (!table) - for (int d = 1; d <= p_.max_depth; ++d) cost_[d] = 1.0 + kCostSlope * (double)d; - - std::string t; - for (int d = 1; d <= p_.max_depth; ++d) t += fmt("%s%d:%.3g", d > 1 ? " " : "", d, cost_[d]); - RAD_INFO("draft depth: ADAPTIVE per sequence (RADIANCE_DRAFT_ADAPT), %d..%d -- the window " - "maximising E(d)/T(d) for T = [%s]%s; moves at +%.1f%% and at most every %d verifies; " - "acceptance half-life %.1f verifies, prior %.2f", - p_.min_depth, p_.max_depth, t.c_str(), table ? "" : " (the default model)", - 100.0 * (double)p_.hyst, p_.hold, (double)p_.half_life, (double)p_.prior); - if (p_.busy > 0) - RAD_INFO("draft depth: above %d running sequences every window is capped at %d " - "(RADIANCE_DRAFT_ADAPT_BUSY)", p_.busy, p_.busy_depth); + if (adapt_) + RAD_INFO("draft depth: ADAPTIVE (--draft-window adaptive), %d..%d -- the step's head depth " + "maximises the tokens its sequences are expected to emit over the step's measured " + "cost, and a sequence stops at the first draft emitted less than %.0f%% of the " + "time; moves at +%.0f%% and at most every %d steps", + kMinDepth, p_.max_depth, 100.0 * kDraftMinChance, 100.0 * kHyst, kHold); return RAD_OK; } @@ -212,49 +92,115 @@ void DraftController::observe(int drafted, int accepted) { if (adapt_) ++by_depth_[drafted]; } -/* ------------------------------------------------------------------ the adaptive policy */ -int DraftController::best_depth(const float* a, int at, double* best, double* at_gain) const { - double e = 1.0, run = 1.0, bg = -1.0, ag = 0.0; - int arg = p_.min_depth; - for (int d = 1; d <= p_.max_depth; ++d) { - run *= (double)a[d - 1]; - e += run; - const double g = e / cost_[d]; - if (d == at) ag = g; - if (d >= p_.min_depth && g > bg) { bg = g; arg = d; } +/* ------------------------------------------------------------------ the step's cost */ +int DraftController::cost_class(int n_rows) { + int c = 0; + while (c + 1 < kCostClasses && n_rows >= (2 << c)) ++c; + return c; +} + +void DraftController::step_time(int depth, int n_rows, double ms) { + if (!adapt_ || depth < 1 || depth > p_.max_depth || n_rows < 1 || !(ms > 0.0) || + !std::isfinite(ms)) + return; + const int c = cost_class(n_rows); + double& m = cost_ms_[depth][c]; + int64_t& n = cost_n_[depth][c]; + if (n >= kCostClipAfter && ms > kCostOutlier * m) ms = kCostOutlier * m; + const double w = std::fmax(1.0 - std::exp2(-1.0 / kCostHalfLife), 1.0 / (double)(n + 1)); + m = n ? m + w * (ms - m) : ms; + ++n; +} + +int64_t DraftController::cost_samples(int depth, int n_rows) const { + if (depth < 1 || depth > RAD_SCHED_MAX_SPEC || n_rows < 1) return 0; + return cost_n_[depth][cost_class(n_rows)]; +} + +double DraftController::step_cost(int depth, int n_rows) const { + if (depth < 1 || depth > RAD_SCHED_MAX_SPEC) return 0.0; + const int c = n_rows >= 1 ? cost_class(n_rows) : 0; + if (cost_n_[depth][c] > 0) return cost_ms_[depth][c]; + /* THE NEAREST MEASURED KEY: in this class first, the nearest depth and the shallower on a + * tie; then the nearest class. Only its ratio to the prior is borrowed, so a class whose steps + * are twice as dear prices every depth twice as dear, and the comparison D makes -- depths at + * ONE class -- reads the prior's shape until its own depths are measured. */ + for (int dc = 0; dc < kCostClasses; ++dc) { + for (int sgn = 0; sgn < 2; ++sgn) { + const int k = sgn ? c + dc : c - dc; + if (k < 0 || k >= kCostClasses || (dc == 0 && sgn)) continue; + int best = -1; + for (int d = 1; d <= p_.max_depth; ++d) { + if (cost_n_[d][k] <= 0) continue; + if (best < 0 || std::abs(d - depth) < std::abs(best - depth)) best = d; + } + if (best > 0) return cost_ms_[best][k] * cost_prior(depth) / cost_prior(best); + } } - if (best) *best = bg; - if (at_gain) *at_gain = ag; - return arg; + return cost_prior(depth); } -double DraftController::gain(const DraftAdapt& s, int d) const { - if (!adapt_ || d < 1 || d > p_.max_depth) return 0.0; - double e = 1.0, run = 1.0; - for (int k = 1; k <= d; ++k) { run *= (double)s.a[k - 1]; e += run; } - return e / cost_[d]; +/* ------------------------------------------------------------------ one sequence */ +int DraftController::stop_of(const float* a) const { + double p = 1.0; + int d = 0; + while (d < p_.max_depth) { + p *= (double)a[d]; + if (p < kDraftMinChance) break; + ++d; + } + return d < kMinDepth ? kMinDepth : d; } -int DraftController::depth_for(DraftAdapt& s) const { - if (!adapt_) return p_.max_depth; - if (s.depth > 0) return s.depth; - /* NOT STARTED. Each position from what the deployment has measured there, where it has - * measured enough; otherwise from the fixed prior, which is optimistic on purpose: a window - * that starts deep evaluates every position and learns all of them, and one that starts - * shallow learns nothing about the positions it never drafts to. */ +double DraftController::prior_at(int j) const { + if (j < 0 || j >= RAD_SCHED_MAX_SPEC) return 0.0; + return prior_n_ >= kPriorMinSeqs ? (double)prior_[j] : kPrior; +} + +void DraftController::start(DraftAdapt& s) const { + /* Each position from the sequences that finished before it once there are enough of them + * (adapt_retire); otherwise from the fixed prior, which is optimistic on purpose (kPrior). */ for (int j = 0; j < p_.max_depth; ++j) { - s.a[j] = reach_[j] >= kGlobalMinReach ? (float)((double)hit_[j] / (double)reach_[j]) - : p_.prior; + s.a[j] = (float)prior_at(j); s.n[j] = 0; } - s.depth = best_depth(s.a, 0, nullptr, nullptr); + s.depth = stop_of(s.a); s.held = 0; - return s.depth; +} + +void DraftController::adapt_retire(const DraftAdapt& s) { + if (!adapt_ || s.depth <= 0 || (double)s.n[0] < kHalfLife) return; + const double w = std::fmax(1.0 - std::exp2(-1.0 / kPriorHalfLifeSeqs), + 1.0 / (double)(prior_n_ + 1)); + for (int j = 0; j < p_.max_depth; ++j) + prior_[j] = prior_n_ ? (float)((double)prior_[j] + w * ((double)s.a[j] - (double)prior_[j])) + : s.a[j]; + ++prior_n_; +} + +double DraftController::yield(const DraftAdapt& s, int d) const { + if (d > p_.max_depth) d = p_.max_depth; + double e = 1.0, run = 1.0; + for (int k = 1; k <= d; ++k) { run *= (double)s.a[k - 1]; e += run; } + return e; +} + +int DraftController::window_of(const DraftAdapt& s) const { + if (!adapt_) return p_.max_depth; + if (s.depth <= 0) return 0; + const int D = step_d_ > 0 ? step_d_ : p_.max_depth; + return s.depth < D ? s.depth : D; +} + +int DraftController::window_for(DraftAdapt& s) const { + if (!adapt_) return p_.max_depth; + if (s.depth <= 0) start(s); + return window_of(s); } int DraftController::adapt_observe(DraftAdapt& s, int drafted, int accepted) const { if (!adapt_) return p_.max_depth; - if (s.depth <= 0) (void)depth_for(s); + if (s.depth <= 0) start(s); if (drafted <= 0) return s.depth; const int D = p_.max_depth; if (drafted > D) drafted = D; @@ -275,27 +221,73 @@ int DraftController::adapt_observe(DraftAdapt& s, int drafted, int accepted) con * learned about them while the window is shorter -- and a sequence held at three by prose that * turns into code would otherwise keep pricing positions four and up at whatever they read * before, and never draft deep enough to find out. The guess is that acceptance decays with - * position rather than falling off a cliff, which is what QFN's head measured, so the window's - * edge stands in for what lies past it. Where that is wrong it costs a move out, the verifies - * that refute it, and a move back. */ + * position rather than falling off a cliff, so the window's edge stands in for what lies past + * it. Where that is wrong it costs a move out, the verifies that refute it, and a move back. */ const double edge = (double)s.a[drafted - 1]; for (int j = drafted; j < D; ++j) s.a[j] = (float)((double)s.a[j] + alpha_ * (edge - (double)s.a[j])); - /* THE MOVE, rate-limited twice. A window changes the shape of the verify pass and of the - * head's history pass, and a shape is a new recording before it plays (runtime/ctx.h); a - * window that follows every flicker of the estimate pays that for nothing. So it moves only - * when the window it would take is worth `hyst` more than the one it has, and not again within - * `hold` verifies of the last move. */ + /* THE STOP MOVES ONLY PAST THE HOLD: it is this sequence's share of the step's shape. */ if (s.held < INT32_MAX) ++s.held; - if (s.held < p_.hold) return s.depth; - double best = 0.0, cur = 0.0; - const int d = best_depth(s.a, s.depth, &best, &cur); - if (d != s.depth && best > cur * (1.0 + (double)p_.hyst)) { + if (s.held < kHold) return s.depth; + const int d = stop_of(s.a); + if (d != s.depth) { s.depth = d; s.held = 0; } return s.depth; } +/* ------------------------------------------------------------------ the step */ +int DraftController::plan_step(DraftAdapt* const* seq, int n_seq, int n_rows) { + if (!adapt_ || n_seq <= 0) return step_d_; + if (n_rows < n_seq) n_rows = n_seq; + const int M = p_.max_depth; + + /* THE STEP'S EXPECTED TOKENS AT EVERY HEAD DEPTH: sum_i E_i(min(D, stop_i)), plus one for + * every row that does not draft. A depth past every stop is the deepest stop's step -- no + * sequence drafts further and the head runs no further -- so the candidates end there. */ + double tok[RAD_SCHED_MAX_SPEC + 1] = {}; + int top = kMinDepth; + for (int i = 0; i < n_seq; ++i) { + DraftAdapt& s = *seq[i]; + if (s.depth <= 0) start(s); + if (s.depth > top) top = s.depth; + double e = 1.0, run = 1.0; + for (int d = 1; d <= M; ++d) { + if (d <= s.depth) { run *= (double)s.a[d - 1]; e += run; } + tok[d] += e; + } + } + const double plain = (double)(n_rows - n_seq); + if (top > M) top = M; + + int best = kMinDepth; + double best_g = -1.0; + for (int d = kMinDepth; d <= top; ++d) { + const double g = (tok[d] + plain) / step_cost(d, n_rows); + if (g > best_g) { best_g = g; best = d; } + } + + /* THE MOVE, rate-limited twice: not within kHold steps of the last one, and only for a depth + * worth kHyst more than the one in force. The first step that drafts takes the best outright. */ + if (step_d_ <= 0) { + step_d_ = best; + step_held_ = 0; + } else { + if (step_held_ < INT32_MAX) ++step_held_; + const int cur = step_d_ < top ? step_d_ : top; + const double cur_g = (tok[cur] + plain) / step_cost(cur, n_rows); + if (best != cur && step_held_ >= kHold && best_g > cur_g * (1.0 + kHyst)) { + RAD_DEBUG("draft window: head depth %d -> %d over %d drafting of %d rows (%.3f -> " + "%.3f tokens per unit of step cost)", step_d_, best, n_seq, n_rows, cur_g, + best_g); + step_d_ = best; + step_held_ = 0; + } + } + step_w_ = step_d_ < top ? step_d_ : top; + return step_d_; +} + } /* namespace rad */ diff --git a/core/sched/draft.h b/core/sched/draft.h index c2e87776..182a789e 100644 --- a/core/sched/draft.h +++ b/core/sched/draft.h @@ -1,8 +1,8 @@ /* draft.h -- the speculative draft-depth controller. * - * IT IS STATIC. The depth is `--num-speculative-tokens` and nothing moves it. + * BY DEFAULT IT IS STATIC. The depth is `--num-speculative-tokens` and nothing moves it. * - * No dynamic policy sits here, and the reason is structural rather than a tuning failure. The + * No dynamic policy sits there, and the reason is structural rather than a tuning failure. The * dynamic policy this engine is otherwise modelled on -- a confidence-product gate against a * decayed per-position acceptance estimate, plus a batch-size ceiling schedule -- gates on the * DRAFTER's own top-1 confidences, per slot, on device, after the drafter has run. The scheduler @@ -17,21 +17,34 @@ * What survives is the accounting, because the acceptance rate is a reported metric (spec §14) * and because a verify that accepts nothing has to be visible. * - * UNLESS RADIANCE_DRAFT_ADAPT=1, and then the premise above is what does not hold. On a model - * whose weights stream -- a MoE that reads experts per verify row, a head that runs one pass a + * UNDER `--draft-window adaptive` THE PREMISE ABOVE IS WHAT DOES NOT HOLD. On a model whose + * weights stream -- a MoE that reads experts per verify row, a serial head that runs one pass a * round -- a step is NOT flat in depth: each draft is one more round of the head and one more - * verify row, and on QFN at tp4 a step measured 12.0 ms at depth 3 and 15.0 ms at depth 5. And - * the best depth is not a property of the model pair but of the TEXT: per-position acceptance - * holds at ~0.9 to position five on code and JSON, sits near 0.6 on prose and falls to 0.2-0.4 - * past the third position on a summary. No constant is right for all of them, and the one that is - * right for one costs the others either rounds nobody accepts or tokens nobody drafted. + * verify row. And the best depth is not a property of the model pair but of the TEXT: + * per-position acceptance stays high deep into the window on code and JSON and falls off after a + * position or two on prose and summaries. No constant is right for all of them, and the one that + * is right for one costs the others either rounds nobody accepts or tokens nobody drafted. * - * So the switch adds a policy, per sequence and on the host: a smoothed estimate of each - * position's conditional acceptance, fed by the same verify outcomes observe() counts (DraftAdapt), - * and before every draft the window d that maximises E(d) / T(d) -- the tokens a step of that - * window is expected to emit, 1 + sum_k prod_{j<=k} a_j, over what such a step costs. It moves - * only when the gain is worth a re-recorded pass (Params::hyst, Params::hold). `max_depth` stays - * the ceiling, and everything sized for it stays sized for it. + * So the adaptive window is a policy on the host, in two halves: + * + * PER SEQUENCE (DraftAdapt): a smoothed estimate of each draft position's conditional + * acceptance, fed by the same verify outcomes observe() counts, and the sequence's STOP -- the + * deepest draft whose chance of being emitted still pays for the verify row it adds + * (kDraftMinChance). + * + * PER STEP (plan_step): the HEAD DEPTH D. A serial head runs as many rounds as the deepest + * window in the step, so depth is a property of the step and not of one sequence: D maximises + * the tokens the whole step is expected to emit, sum_i E_i(min(D, stop_i)), over what a step + * at that depth and that many sequences costs, T(D, n) -- with E(d) = 1 + sum_k prod_{j<=k} a_j + * and T measured online (step_time). Every sequence then drafts min(D, stop_i). + * + * Both move only past a hold, and D also past a hysteresis, because a window is a pass shape and + * a new shape is a new recording before it plays (runtime/ctx.h). `max_depth` stays the ceiling, + * and everything sized for it stays sized for it. + * + * IT IS THE SERIAL DRAFTER'S POLICY. A block drafter proposes its whole block in one pass, and + * prompt lookup runs no pass at all, so neither pays the rounds the policy prices; the scheduler + * turns it on only for a serial drafter (Scheduler::init) and says so when it does not. * * IT CHANGES HOW MANY DRAFTS A STEP VERIFIES AND NOTHING ELSE. Acceptance is the rule in * core/sample/accept.h, so what a step emits is the target's own token at every position whatever @@ -50,14 +63,14 @@ namespace rad { enum { RAD_SCHED_MAX_SPEC = 64 }; /* ONE SEQUENCE'S HALF OF THE ADAPTIVE POLICY: what the drafter has done on this sequence's own - * text, and the window that buys. It lives in the scheduler's per-request state and the controller - * does the arithmetic, so neither has to look the other up. + * text, and how deep that makes it worth drafting. It lives in the scheduler's per-request state + * and the controller does the arithmetic, so neither has to look the other up. * - * `depth` 0 is "not started": the first ask fills the estimate from the prior and picks a window. + * `depth` 0 is "not started": the first ask fills the estimate from the prior and sets the stop. * It belongs to the REQUEST, not to the cache it holds -- a preemption keeps it, because the text * it describes is still the text the request is writing -- and a recycled slot starts it over. */ struct DraftAdapt { - int32_t depth = 0; /* the window the sequence gets now; 0 = not started */ + int32_t depth = 0; /* the sequence's stop; 0 = not started */ int32_t held = 0; /* verifies since `depth` last moved */ float a[RAD_SCHED_MAX_SPEC] = {}; /* conditional acceptance at each draft position */ uint16_t n[RAD_SCHED_MAX_SPEC] = {}; /* verifies that evaluated each one, saturating */ @@ -66,63 +79,125 @@ struct DraftAdapt { class DraftController { public: struct Params { - int max_depth = 0; /* the deployment's window; 0 disables speculation entirely */ - - /* THE ADAPTIVE POLICY (see the top of this file), off unless RADIANCE_DRAFT_ADAPT=1 -- - * from_env() is where the switch and the knobs below are read, once per process. With it - * off every field below is ignored and the depth is max_depth, as it always was. */ - bool adapt = false; - /* T(d), what a step verifying d drafts costs, by d; any unit, only ratios are read. An - * entry above zero is a measured point, the rest are interpolated between the points and - * extrapolated past them along the nearest two. Fewer than two points is no table, and - * then T(d) = 1 + 0.06 d. Index 0 is unused. */ - float cost[RAD_SCHED_MAX_SPEC + 1] = {}; - float hyst = 0.02f; /* a move must buy this much more E/T than the window it leaves */ - int hold = 8; /* and come at least this many verifies after the last move */ - float half_life = 16.0f; /* of each position's estimate, in verifies that evaluate it */ - float prior = 0.8f; /* for a position too few verifies have evaluated to say */ - int min_depth = 1; /* the policy never drafts fewer */ - /* THE LOAD CAP. The cost table is a step's cost at ONE sequence, and a deeper window costs - * more under load than it says -- every row a step adds is expert bandwidth all its - * neighbours wait on. So with more than `busy` sequences running, every window is capped - * at `busy_depth`; the estimates go on learning underneath and the windows come back when - * the load does. 0 is no cap. */ - int busy = 0; - int busy_depth = 3; + int max_depth = 0; /* the deployment's window; 0 disables speculation entirely */ + /* THE ADAPTIVE POLICY (see the top of this file): `--draft-window adaptive` with a serial + * drafter, which Scheduler::init decides. Off, the depth is max_depth, as it always was. */ + bool adapt = false; }; + /* ---------------------------------------------------------- the policy's constants + * Named rather than configurable: each is a property of how the policy works, not of a + * deployment, and the one property that IS a deployment's -- what a step costs -- is + * measured (step_time). */ + + /* A WINDOW MOVES ONLY FOR TWO PERCENT MORE YIELD. One draft more or less moves E/T by several + * percent on any drafter worth speculating with, while a smoothed estimate wobbles by about + * a percent verify to verify; below two percent a move would flip D between two near-equal + * neighbours and pay a recording each time. */ + static constexpr double kHyst = 0.02; + /* AND NOT WITHIN EIGHT VERIFIES (steps, for D) OF THE LAST MOVE. A pass shape is recorded the + * second time its key is seen and played from the third (runtime/ctx.h), so a shape held for + * eight steps plays from the tape for most of them; and eight verifies at the half-life below + * move an estimate by about a third of the way to a new rate, which is a trend rather than + * one rejection. */ + static constexpr int kHold = 8; + /* EACH POSITION'S ESTIMATE HAS A HALF-LIFE OF SIXTEEN VERIFIES THAT EVALUATE IT. At a few + * tokens a verify that is some fifty tokens of text: short enough to follow a reply from prose + * into a code block and back, long enough that a single rejection moves an estimate by four + * percent rather than resetting it. */ + static constexpr double kHalfLife = 16.0; + /* A POSITION NOTHING HAS MEASURED STARTS AT 0.8, optimistic on purpose: a window that starts + * deep evaluates every position and learns all of them, one that starts shallow learns nothing + * about the positions it never drafts to. 0.8 sits between what draft heads measure on prose + * and on code at the first positions, so a wrong start is corrected within a hold. */ + static constexpr double kPrior = 0.8; + /* ONCE FOUR SEQUENCES HAVE FINISHED, A NEW ONE STARTS FROM WHAT THEY ENDED ON (adapt_retire): + * the mean of their estimates, a running mean and then a moving average with a half-life of 32 + * sequences, so a deployment that moves from one kind of traffic to another follows it within + * a few dozen requests. Four, because a mean over fewer is one text's. A sequence counts only + * once it has had a half-life of verifies: before that its estimate is mostly the prior it + * started from, and folding it back would feed the prior to itself. */ + static constexpr int kPriorMinSeqs = 4; + static constexpr double kPriorHalfLifeSeqs = 32.0; + /* NEVER FEWER THAN ONE DRAFT. A sequence at zero would evaluate no position, so nothing could + * ever move its estimate off zero again. */ + static constexpr int kMinDepth = 1; + /* A SEQUENCE STOPS AT THE FIRST DRAFT WHOSE CHANCE OF BEING EMITTED -- every draft before it + * and itself accepted, prod_{j<=d} a_j -- IS UNDER ONE IN TEN. The draft adds a verify row to + * the step whatever D is, and that row is the only cost a sequence adds once the head runs D + * rounds for its deepest neighbour. At one sequence the step-cost prior (1 + 0.06 d) already + * breaks even near that chance -- one more draft is worth it while p_d / E exceeds the 5-6 % + * it adds to the step, with E around two -- and a verify row alone costs less than a round + * plus a row, so the threshold is never stricter than D's own trade. A constant rather than a + * measurement: a row's share of the step is not separable from the head's rounds in step + * times keyed by depth, and the measured T(D, n) already prices the rounds. */ + static constexpr double kDraftMinChance = 0.1; + /* STEP TIMES ARE KEPT IN THESE RUNNING-SEQUENCE CLASSES: 1, 2-3, 4-7, 8-15, 16 and up. A + * step's cost moves with how many rows share its weight reads, and a class a power of two + * wide keeps every key fed often enough to mean something. */ + static constexpr int kCostClasses = 5; + int configure(const Params& p); const Params& params() const { return p_; } - /* RADIANCE_DRAFT_ADAPT and its companions (RADIANCE_DRAFT_COST, _HYST, _HOLD, _HALFLIFE, - * _PRIOR, _MIN, _BUSY, _BUSY_DEPTH), parsed ONCE per process and copied into the adaptive fields of `p`; - * max_depth is left alone. Once, so that every step of a run is decided by the same rule -- - * a pass recorded under one setting and played under another is a pass the audit fails. */ - static void from_env(Params* p); - /* The draft depth for the next step. 0 means "do not speculate". Constant by construction: - * under the adaptive policy it is the CEILING, and depth_for() is the window. */ + * under the adaptive policy it is the CEILING, and window_for() is the window. */ int next_depth() const { return p_.max_depth; } /* ---------------------------------------------------------- the adaptive policy */ bool adaptive() const { return adapt_; } - /* The window this sequence may draft into: its policy depth, starting the estimate on the - * first ask; max_depth when the policy is off. */ - int depth_for(DraftAdapt& s) const; + /* The window this sequence may draft into: min(D, its stop), starting its estimate on the + * first ask; max_depth when the policy is off. Before any step has chosen D, D is the + * ceiling. */ + int window_for(DraftAdapt& s) const; + /* The same for a sequence whose estimate has started, without starting one: 0 when it has + * not. For reporting. */ + int window_of(const DraftAdapt& s) const; /* One verify of this sequence: `drafted` proposed, a prefix of `accepted` taken. Moves its - * estimate, and its depth when that clears the hysteresis. Returns the depth after. */ + * estimate, and its stop past the hold. Returns the stop after. */ int adapt_observe(DraftAdapt& s, int drafted, int accepted) const; - /* The load cap for a cycle with `n_running` sequences running: busy_depth past the threshold, - * 0 for none. The scheduler asks once a cycle and holds the answer (Scheduler::draft_cycle). */ - int busy_cap(int n_running) const { - return (adapt_ && p_.busy > 0 && n_running > p_.busy) ? p_.busy_depth : 0; - } - /* What the policy maximises, E(d) / T(d), for `s`'s estimate as it stands. For tests and logs. */ - double gain(const DraftAdapt& s, int d) const; - /* The cost table as configured, T(1..max_depth). */ - double cost(int d) const { - return (d >= 1 && d <= RAD_SCHED_MAX_SPEC) ? cost_[d] : 0.0; - } + /* A SEQUENCE THAT HAS FINISHED: its estimate joins the prior the next sequences start from. + * + * PER SEQUENCE, NOT PER VERIFY. The deployment's own counts -- accepted(j) / reached(j) over + * every verify -- are what a fixed window would want, and under this policy they are biased: + * only a sequence whose drafts keep being accepted is given a window that reaches the deep + * positions, so those positions are measured almost only on the texts that predict well, and + * a new sequence would start as if every text did. Each finished sequence counts once here, at + * every position, and at a position it never reached its estimate is the extrapolation from + * its own window's edge (adapt_observe) -- a shallow text pulls the deep positions down as + * much as a deep one holds them up. */ + void adapt_retire(const DraftAdapt& s); + /* The prior a sequence starting now is given at position `j`. For tests and logs. */ + double prior_at(int j) const; + + /* D FOR THE STEP THAT IS OPENING. `seq` are the estimates of the `n_seq` sequences that will + * draft in it, `n_rows` the decode rows it carries in all (the rest emit one token each and + * draft nothing). Started here if they have not been. Moves D past the hold and the + * hysteresis; with no sequence to draft, D stays. Returns D. */ + int plan_step(DraftAdapt* const* seq, int n_seq, int n_rows); + /* The step depth in force: 0 before the first step chose one. */ + int step_depth() const { return step_d_; } + /* The deepest window a step at that depth carries: D, but no deeper than the deepest stop it + * was chosen over, since no head-drafting sequence drafts past its stop; the ceiling before + * any step chose one, and with the policy off. What a proposal from outside the head (prompt + * lookup) is held to, so it adds no deeper pass shape than the head's own. */ + int step_window() const { return adapt_ && step_w_ > 0 ? step_w_ : p_.max_depth; } + + /* T(depth, n): a decode step whose deepest window was `depth`, with `n_rows` decode rows, + * took `ms` from the end of the step before it. Fed by Scheduler::step_finished; only the + * adaptive policy reads it. */ + void step_time(int depth, int n_rows, double ms); + /* What a step at `depth` with `n_rows` rows is expected to cost, in ms when anything has + * been measured: the key's own mean when it has samples; the nearest measured key scaled by + * the prior's ratio (1 + 0.06 d) when it does not; the prior itself when nothing has been + * measured. */ + double step_cost(int depth, int n_rows) const; + /* Samples behind a key: for tests and logs. */ + int64_t cost_samples(int depth, int n_rows) const; + + /* E(d) for `s`'s estimate as it stands: the tokens a verify of d drafts is expected to emit, + * the verified token included. For tests and logs. */ + double yield(const DraftAdapt& s, int d) const; /* Verifies by the window they carried, counted only under the policy: the histogram of the * depths it actually chose, which the mean gauge on its own cannot show. */ int64_t verified_at(int d) const { @@ -170,13 +245,23 @@ private: /* The adaptive policy as configure() resolved it. */ bool adapt_ = false; - double cost_[RAD_SCHED_MAX_SPEC + 1] = {}; /* T(d), resolved for 1..max_depth */ - double alpha_ = 0.0; /* the smoothing step from half_life */ + double alpha_ = 0.0; /* the smoothing step from kHalfLife */ int64_t by_depth_[RAD_SCHED_MAX_SPEC + 1] = {}; + int32_t step_d_ = 0; /* D; 0 until a step chose one */ + int32_t step_w_ = 0; /* min(D, the deepest stop it saw) */ + int32_t step_held_ = 0; /* steps since D last moved */ + /* The prior from finished sequences (adapt_retire), and how many are behind it. */ + float prior_[RAD_SCHED_MAX_SPEC] = {}; + int64_t prior_n_ = 0; + /* T(d, class): the mean step time of each key, and the steps behind it. */ + double cost_ms_[RAD_SCHED_MAX_SPEC + 1][kCostClasses] = {}; + int64_t cost_n_ [RAD_SCHED_MAX_SPEC + 1][kCostClasses] = {}; - /* The window in [min_depth, max_depth] that maximises E/T for estimate `a`, the smaller on a - * tie; its E/T in *best, and the E/T of window `at` in *at_gain. */ - int best_depth(const float* a, int at, double* best, double* at_gain) const; + /* Fill a sequence's estimate from the prior and set its first stop. */ + void start(DraftAdapt& s) const; + /* The deepest draft whose chance of being emitted is at least kDraftMinChance. */ + int stop_of(const float* a) const; + static int cost_class(int n_rows); }; } /* namespace rad */ diff --git a/core/sched/metrics.h b/core/sched/metrics.h index 0b25eca2..6c4a838b 100644 --- a/core/sched/metrics.h +++ b/core/sched/metrics.h @@ -137,7 +137,7 @@ struct SchedMetrics { int draft_depth_seen = 0; int64_t draft_pos_reached [RAD_METRICS_MAX_SPEC] = {}; int64_t draft_pos_accepted[RAD_METRICS_MAX_SPEC] = {}; - /* THE ADAPTIVE WINDOW (RADIANCE_DRAFT_ADAPT, draft.h), all zero while it is off. The mean + /* THE ADAPTIVE WINDOW (--draft-window adaptive, draft.h), all zero while it is off. The mean * window it gives the sequences running now -- at one sequence, that sequence's window -- and * the verifies by the window they carried, index = drafts verified, cumulative: the gauge says * where the policy stands, the histogram where it has been. */ diff --git a/core/sched/scheduler.cpp b/core/sched/scheduler.cpp index 7f83ef3f..9903d8f0 100644 --- a/core/sched/scheduler.cpp +++ b/core/sched/scheduler.cpp @@ -97,10 +97,29 @@ int Scheduler::init(Program& prog, const Config& cfg, IKVManager* kv, IPrefixCac DraftController::Params dp; dp.max_depth = cfg.n_spec; - /* The adaptive window's switch and knobs, read once per process (draft.h). Off unless set. */ - DraftController::from_env(&dp); + /* THE ADAPTIVE WINDOW IS THE SERIAL DRAFTER'S POLICY (draft.h): it prices a draft as one more + * round of the head. A block drafter proposes its block in one pass and prompt lookup runs no + * pass at all, so for either the window stays fixed -- decided here, once, before any step + * has counted an acceptance, and said, because a flag that changes nothing reads exactly + * like a flag that made no difference. */ + if (cfg.draft_window == DraftWindow::Adaptive) { + const RadDrafterDecl& dd = prog.drafter; + if (cfg.n_spec <= 0) + RAD_WARN("--draft-window adaptive: this deployment does not speculate " + "(--num-speculative-tokens %d), so there is no window to adapt", cfg.n_spec); + else if (dd.kind != RAD_DRAFT_SERIAL || dd.depth <= 0) + RAD_WARN("--draft-window adaptive: the window is fixed at %d with %s -- the adaptive " + "window prices the rounds of a serial draft head, which this deployment does " + "not run", cfg.n_spec, + dd.kind == RAD_DRAFT_BLOCK ? "a block drafter, which proposes its block in one " + "pass" + : "prompt lookup only"); + else + dp.adapt = true; + } RAD_TRY(draft_.configure(dp)); - draft_busy_cap_ = 0; + draft_est_.clear(); + draft_est_.reserve((size_t)cfg.max_seqs); RAD_TRY(builder_.init(prog, cfg, geo_, kvgeom_, ranks)); /* The builder's bound and not a second derivation of it: it is what the block tables were @@ -366,6 +385,12 @@ void Scheduler::close_request(SchedReq& s, ReqState st, const char* reason, bool } if (cfg_.prefix_cache && pc_) pc_->release(*s.req); + /* What the draft head did on this text is now part of what the next text starts from -- + * once: the estimate is marked unstarted, so a second close of the same request adds nothing. */ + if (draft_.adaptive()) { + draft_.adapt_retire(s.adapt); + s.adapt.depth = 0; + } if (defer_free) { if (!s.pending_free) ++n_deferred_; @@ -937,7 +962,7 @@ void Scheduler::undo_hit(SchedReq& s) { /* ------------------------------------------------------------------ the step */ const RadBatch* Scheduler::step() { std::lock_guard lk(mu_); - /* A cycle opens here, so its load cap is counted here (scheduler.h, draft_busy_cap_). */ + /* A cycle opens here, so its head depth is chosen here (scheduler.h, draft_cycle). */ draft_cycle(); /* The drafter's snapshots describe the step before this one and nothing reads them from here @@ -997,12 +1022,19 @@ const RadBatch* Scheduler::step() { * none. Request::may_speculate says why a request can be ineligible. * * AND THE CAP IS THE SEQUENCE'S OWN WINDOW, which is `depth` unless the adaptive - * policy is on -- then it is what this sequence's acceptance pays for, and the n-gram - * fallback is held to it like any other proposal. */ + * policy is on -- then it is what this sequence's acceptance of the HEAD's drafts pays + * for. The n-gram fallback is not the head and that estimate says nothing about it, so + * it is held to the step's window instead: as deep as the head drafts this step and no + * deeper, which adds no pass shape the head's own windows do not. */ const int cap = draft_.adaptive() ? draft_cap(s) : depth; + bool ngram = false; if (!r.may_speculate) s.n_draft = 0; - else if (s.n_draft == 0) s.n_draft = (int32_t)draft_ngram(s, cap); - if (s.n_draft > cap) s.n_draft = cap; + else if (s.n_draft == 0) { + s.n_draft = (int32_t)draft_ngram(s, draft_.step_window()); + ngram = s.n_draft > 0; + } + if (!ngram && s.n_draft > cap) s.n_draft = cap; + s.draft_ngram = ngram; /* NO VERIFIED POSITION AT OR PAST THE CONTEXT. The step computes positions n_computed * .. n_computed + n_draft, and nothing past ctx_limit_ - 1 has a rotary row or a * block-table column. commit() finishes a sequence before its length reaches the @@ -1089,6 +1121,7 @@ const RadBatch* Scheduler::step() { e.draft = s.draft; e.n_draft = s.n_draft; e.is_prefill = prefill; + e.ngram = !prefill && s.draft_ngram && s.n_draft > 0; e.produces_token = (r.n_computed + want >= target); /* The snapshot a hit resumes from, consumed once: the state is in the slot for the whole * sequence after this, and restoring it again on a later chunk would rewind it. */ @@ -1355,24 +1388,23 @@ void Scheduler::take_encodes(std::vector>* out) { out->swap(enc_want_); } -/* ------------------------------------------------------------------ the load cap */ -void Scheduler::draft_adapt_off(const char* why) { - std::lock_guard lk(mu_); - if (!draft_.adaptive()) return; - DraftController::Params p = draft_.params(); - p.adapt = false; - if (draft_.configure(p) == RAD_OK) - RAD_WARN("RADIANCE_DRAFT_ADAPT is set and the drafter is %s: the adaptive window is the serial " - "drafter's policy, so this run keeps the configured depth %d", why, p.max_depth); - draft_busy_cap_ = 0; -} - +/* ------------------------------------------------------------------ the step's head depth */ void Scheduler::draft_cycle() { - const int32_t cap = (int32_t)draft_.busy_cap((int)running_.size()); - if (cap != draft_busy_cap_) - RAD_DEBUG("draft window: %zu running, %s", running_.size(), - cap > 0 ? "every window capped at the busy depth" : "the cap is lifted"); - draft_busy_cap_ = cap; + if (!draft_.adaptive()) return; + /* THE ROWS THE OPENING STEP WILL DECODE: every running sequence past its prompt. Those that + * may speculate draft, and their estimates choose D; the rest emit one token each, which is + * part of what the step yields and of what it costs. A sequence still prefilling is neither. */ + draft_est_.clear(); + int n_rows = 0; + for (int32_t slot : running_) { + SchedReq& s = *reqs_[(size_t)slot]; + if (!s.req || !s.running) continue; + const Request& r = *s.req; + if (r.n_computed < (int64_t)r.prompt.size() || r.n_computed + 1 < s.seq_len()) continue; + ++n_rows; + if (r.may_speculate) draft_est_.push_back(&s.adapt); + } + (void)draft_.plan_step(draft_est_.data(), (int)draft_est_.size(), n_rows); } /* ------------------------------------------------------------------ the next step */ @@ -1383,7 +1415,7 @@ void Scheduler::expect_draft(uint64_t id, int n) { SchedReq& s = *reqs_[(size_t)it->second]; /* UNDER THE ADAPTIVE WINDOW THE ROUNDS RAN FOR THE DEEPEST SEQUENCE, and this one verifies the * first of them up to its own window -- the cap serial_begin sized the rounds from, unchanged - * since: nothing commits between the two, and the load cap is the cycle's (draft_busy_cap_). */ + * since: nothing commits between the two, and D is the cycle's (draft_cycle). */ if (draft_.adaptive() && n > 0) { const int cap = draft_cap(s); if (n > cap) n = cap; @@ -1473,7 +1505,7 @@ void Scheduler::promote_next() { std::lock_guard lk(mu_); if (!next_open_) return; /* The other place a cycle opens: the step issued ahead becomes this one, and step() is not - * called. Its proposals were counted under the last cycle's cap; the next ones are drafted + * called. Its proposals were counted under the last cycle's D; the next ones are drafted * under this one. */ draft_cycle(); std::swap(plan_, plan_next_); @@ -1634,9 +1666,12 @@ int Scheduler::commit(const StepResult& res) { if (e.n_spec > 0) { draft_.observe(e.n_spec, a); /* AND THIS SEQUENCE'S OWN ESTIMATE, from the same outcome, under the adaptive - * window. A move takes effect at the next draft: this step's drafter has already - * run, sized from the estimate as it stood before this commit. */ - if (draft_.adaptive()) { + * window -- when the head drafted it. A prompt-lookup proposal is accepted on + * different text by a different rule, and folding it in would price the head's + * next window on what the lookup found. A move takes effect at the next draft: + * this step's drafter has already run, sized from the estimate as it stood before + * this commit. */ + if (draft_.adaptive() && !e.ngram) { const int was = s.adapt.depth; const int now = draft_.adapt_observe(s.adapt, e.n_spec, a); if (now != was && was > 0 && (int)log_level() >= (int)Log::Debug) { @@ -1646,9 +1681,10 @@ int Scheduler::commit(const StepResult& res) { o += std::snprintf(est + o, sizeof est - (size_t)o, "%s%.2f", j ? " " : "", (double)s.adapt.a[j]); est[o] = 0; - RAD_DEBUG("draft window: request %llu %d -> %d (E/T %.4f -> %.4f; " - "acceptance by position %s)", (unsigned long long)r.id, was, - now, draft_.gain(s.adapt, was), draft_.gain(s.adapt, now), est); + RAD_DEBUG("draft window: request %llu stops at %d, was %d (expected " + "tokens a verify %.3f -> %.3f; acceptance by position %s)", + (unsigned long long)r.id, now, was, draft_.yield(s.adapt, was), + draft_.yield(s.adapt, now), est); } } m_.draft_tokens += e.n_spec; @@ -1777,6 +1813,16 @@ void Scheduler::step_finished() { if (m_.prefill_tps < 0.05f) m_.prefill_tps = 0.0f; if (m_.decode_tps < 0.05f) m_.decode_tps = 0.0f; } + /* AND WHAT THE STEP COST AT ITS DEPTH, for the adaptive window (draft.h): the same wall time, + * which is the step's share of the loop -- the draft rounds that followed it included, and a + * step issued ahead counted from the end of the one before rather than twice. A decode step + * only: a prefill chunk's time is the chunk's, not the window's. Keyed by the deepest window + * the step verified, which is the head depth it ran at whenever D held from the step before. */ + if (draft_.adaptive() && plan_.phase == RAD_PHASE_DECODE && plan_.n_spec > 0 && wall > 0) { + int rows = 0; + for (const StepEntry& e : plan_.e) rows += (e.produces_token && !e.is_prefill) ? 1 : 0; + draft_.step_time((int)plan_.n_spec, rows, wall * 1e3); + } last_step_at_ = step_end; /* So a second call, or one on a loop iteration that ran no step, charges nothing. */ step_start_ = {}; @@ -2392,9 +2438,8 @@ SchedMetrics Scheduler::metrics_locked() const { for (int32_t slot : running_) { const SchedReq& s = *reqs_[(size_t)slot]; if (s.adapt.depth <= 0) continue; - /* The window it drafts into, which under load is the cap and not its estimate. */ - sum += (draft_busy_cap_ > 0 && s.adapt.depth > draft_busy_cap_) ? draft_busy_cap_ - : s.adapt.depth; + /* The window it drafts into: its stop under the step's head depth. */ + sum += draft_.window_of(s.adapt); ++n; } m.draft_depth_adaptive = n ? (float)((double)sum / (double)n) : 0.0f; diff --git a/core/sched/scheduler.h b/core/sched/scheduler.h index 8fa1330b..3842a1d8 100644 --- a/core/sched/scheduler.h +++ b/core/sched/scheduler.h @@ -109,11 +109,13 @@ struct SchedReq { draft_hi = 0; } - /* THE ADAPTIVE DRAFT WINDOW'S ESTIMATE FOR THIS REQUEST (RADIANCE_DRAFT_ADAPT, draft.h), and + /* THE ADAPTIVE DRAFT WINDOW'S ESTIMATE FOR THIS REQUEST (--draft-window adaptive, draft.h), and * deliberately NOT in drafter_reset(): it describes the text the request is writing, which a * preemption does not change, rather than any cache the request holds. A recycled slot starts * it over in add_locked. Untouched while the policy is off. */ DraftAdapt adapt; + /* The proposal the step being planned carries came from prompt lookup (StepEntry::ngram). */ + bool draft_ngram = false; int32_t sched_tokens = 0; /* tokens given to this request in the current step */ int32_t entry = -1; /* its index in the current StepPlan, or -1 */ @@ -290,13 +292,6 @@ public: * (draft_cap): the controller's depth, or what the adaptive policy gives the sequence. */ int set_draft(uint64_t id, const int32_t* tokens, int n); - /* RADIANCE_DRAFT_ADAPT is the SERIAL drafter's policy: every draft there is one more round of - * the head, which is what its cost table prices. A block drafter proposes its whole block in - * one pass, so a shorter window only saves verify rows -- a different trade with a policy of its - * own. The engine calls this once, before the first step, when the drafter is a block drafter; - * the switch then changes nothing about that run. Says so when it was on. */ - void draft_adapt_off(const char* why); - /* ---------------------------------------------------------- media the encoder owes * A REQUEST CARRYING MEDIA IS ADMITTED ONLY ONCE EVERY ITEM IS ENCODED. Until then it keeps * its place in the queue, the requests behind it are admitted around it, and its items wait @@ -506,28 +501,27 @@ private: return hit_holes_draft_ ? ctx_len : 0; } - /* THE WINDOW A SEQUENCE MAY DRAFT INTO: the configured depth, or under RADIANCE_DRAFT_ADAPT - * the one its own acceptance pays for (DraftController::depth_for, which starts the estimate - * on the first ask), under this cycle's load cap. It is the rule every site that caps a - * proposal applies -- step(), set_draft(), expect_draft() and the rounds in serial_begin() -- - * and with the policy off it is next_depth(), exactly as before. */ + /* THE WINDOW A SEQUENCE MAY DRAFT INTO: the configured depth, or under the adaptive window + * min(D, its stop) (DraftController::window_for, which starts the estimate on the first ask). + * It is the rule every site that caps a proposal applies -- step(), set_draft(), + * expect_draft() and the rounds in serial_begin() -- and with the policy off it is + * next_depth(), exactly as before. */ int draft_cap(SchedReq& s) const { - if (!draft_.adaptive()) return draft_.next_depth(); - const int d = draft_.depth_for(s.adapt); - return (draft_busy_cap_ > 0 && d > draft_busy_cap_) ? draft_busy_cap_ : d; + return draft_.adaptive() ? draft_.window_for(s.adapt) : draft_.next_depth(); } - /* THE LOAD CAP OF THE CYCLE IN FLIGHT (RADIANCE_DRAFT_ADAPT_BUSY, draft.h), 0 for none. + /* THE STEP'S HEAD DEPTH D IS CHOSEN ONCE A CYCLE AND HELD, by draft_cycle(), which is the + * first thing step() and promote_next() do -- every cycle opens with exactly one of the two. + * A cycle's proposal passes through four sites -- the plan that verifies it, the rounds that + * draft the next one, expect_draft() and set_draft() -- and the step issued ahead holds them + * to agree: it was built with the count expect_draft() gave it, and set_draft() refuses a + * different one. A D read live could move between them, because a commit or a client hanging + * up changes the running set mid-cycle; held, it moves only where a cycle starts, and a + * proposal drafted under one D and planned under the next is only ever cut shorter. * - * COUNTED ONCE A CYCLE AND HELD, by draft_cycle(), which is the first thing step() and - * promote_next() do -- every cycle opens with exactly one of the two. A cycle's proposal - * passes through four sites -- the plan that verifies it, the rounds that draft the next one, - * expect_draft() and set_draft() -- and the step issued ahead holds them to agree: it was - * built with the count expect_draft() gave it, and set_draft() refuses a different one. A - * cap read live could move between them, because a commit or a client hanging up takes a - * sequence out of running_ mid-cycle; held, it moves only where a cycle starts, and a - * proposal drafted under one cap and planned under the next is only ever cut shorter. */ - int32_t draft_busy_cap_ = 0; + * `draft_est_` is the list plan_step reads, reserved at init so the step path does not + * allocate. */ + std::vector draft_est_; void draft_cycle(); /* The built-in prompt-lookup drafter (scheduler.cpp). Fills s.draft and returns how many diff --git a/core/server/iface.h b/core/server/iface.h index 32d29cd5..5245334b 100644 --- a/core/server/iface.h +++ b/core/server/iface.h @@ -114,7 +114,7 @@ struct SchedMetrics { int draft_depth_seen = 0; int64_t draft_pos_reached [MAX_DRAFT_POS] = {}; int64_t draft_pos_accepted[MAX_DRAFT_POS] = {}; - /* THE ADAPTIVE DRAFT WINDOW (RADIANCE_DRAFT_ADAPT), all zero while it is off: the mean window + /* THE ADAPTIVE DRAFT WINDOW (--draft-window adaptive), all zero while it is off: the mean window * of the running sequences, and the verifies by the window they carried (index = drafts). */ bool draft_adaptive = false; double draft_depth_adaptive = 0.0; diff --git a/core/server/metrics.cpp b/core/server/metrics.cpp index 04f45b38..becd40ef 100644 --- a/core/server/metrics.cpp +++ b/core/server/metrics.cpp @@ -417,7 +417,7 @@ std::string Metrics::render() const { w.out += "radiance:draft_position_accepted_total" + base + ",position=\"" + std::to_string(j) + "\"} " + num(g_draft_pos_accepted_[j].get()) + '\n'; } - /* THE ADAPTIVE DRAFT WINDOW, only while RADIANCE_DRAFT_ADAPT is on: where the policy stands -- + /* THE ADAPTIVE DRAFT WINDOW, only while --draft-window adaptive is in force: where the policy stands -- * at one sequence, that sequence's window -- and where it has been, as verifies by the * number of drafts they carried. */ if (draft_adapt_on_.load(std::memory_order_acquire)) { diff --git a/core/server/metrics.h b/core/server/metrics.h index c81b3d72..27396f32 100644 --- a/core/server/metrics.h +++ b/core/server/metrics.h @@ -123,7 +123,7 @@ private: Gauge g_draft_pos_reached_[MAX_DRAFT_POS]; Gauge g_draft_pos_accepted_[MAX_DRAFT_POS]; std::atomic draft_pos_seen_{0}; - /* THE ADAPTIVE DRAFT WINDOW (RADIANCE_DRAFT_ADAPT): the mean window of the running sequences, + /* THE ADAPTIVE DRAFT WINDOW (--draft-window adaptive): the mean window of the running sequences, * and the verifies by the window they carried, index = drafts. Rendered only while the policy * is on, so a deployment without it exposes exactly what it always did. */ Gauge g_draft_depth_adapt_; diff --git a/deploy/grafana/README.md b/deploy/grafana/README.md index 49f0d1dc..f402dda2 100644 --- a/deploy/grafana/README.md +++ b/deploy/grafana/README.md @@ -31,7 +31,7 @@ Window values are counter ratios with the definitions of the since-start gauges: | conditional acceptance at j | `draft_position_accepted`(j) / `draft_position_reached`(j) | `radiance:draft_position_acceptance_ratio` | Draft blocks per decode step is the reach of position 0 over `engine_steps_decode_total` (GUIDE §6.3). -The adaptive draft window and verifies by draft depth have data only while `RADIANCE_DRAFT_ADAPT` is on. +The adaptive draft window and verifies by draft depth have data only under `--draft-window adaptive`. Prometheus extrapolates `rate()` inside the window and VictoriaMetrics does not, so short windows at the edge of a burst differ between the two. The overview's ratios are taken over the dashboard's time diff --git a/deploy/grafana/radiance.json b/deploy/grafana/radiance.json index e041ef35..2b723299 100644 --- a/deploy/grafana/radiance.json +++ b/deploy/grafana/radiance.json @@ -1751,7 +1751,7 @@ "type": "prometheus", "uid": "${datasource}" }, - "description": "Mean draft window the adaptive policy gives the running sequences; a gap while none runs. Only while RADIANCE_DRAFT_ADAPT is on; no data otherwise.", + "description": "Mean draft window the adaptive policy gives the running sequences; a gap while none runs. Only under --draft-window adaptive; no data otherwise.", "gridPos": { "h": 8, "w": 12, @@ -1806,7 +1806,7 @@ "type": "prometheus", "uid": "${datasource}" }, - "description": "Share of verifies by the number of drafts they carried, under the adaptive window. Only while RADIANCE_DRAFT_ADAPT is on; no data otherwise.", + "description": "Share of verifies by the number of drafts they carried, under the adaptive window. Only under --draft-window adaptive; no data otherwise.", "gridPos": { "h": 8, "w": 12, diff --git a/docs/GUIDE.md b/docs/GUIDE.md index 6270254d..feec5c6e 100644 --- a/docs/GUIDE.md +++ b/docs/GUIDE.md @@ -459,9 +459,14 @@ whose cache costs most, and one block table serves every rank. Where a rank's ca token -- an attention-zero rank at `--tp 3` keeps no attention cache -- it gets, under `expert_tiered` with both pools derived, a KV pool that pages exactly the common token count and the difference in its weights pool, up to every one of its weights resident; the startup log names the -rank and both sizes (`kv: rank N's cache costs ...`). A stated `--vram-kv-mib` or -`--vram-weights-mib` is applied to every card unchanged, and an even world is never re-split -([`TP3.md`](TP3.md) §1). +rank and both sizes (`kv: rank N's cache costs ...`). It holds with or without a lending cache: +under `--deterministic`, or on a device without virtual memory management, the bytes that rank +would never page go to its experts all the same. A stated `--vram-kv-mib` or `--vram-weights-mib` +is applied to every card unchanged, and a rank whose cache costs as much per token as the dearest +rank's is never re-split -- which at `--tp 2` and `--tp 4` is every rank. A rank that costs less and +keeps the common split says why on the same `kv: rank N's cache costs ...` line. Every rank still +allocates its whole `--host-pool-mib`, a re-split rank whose weights all fit on the card included, +and that rank's log says its pinned host pool holds no weights ([`TP3.md`](TP3.md) §1). **The cache's size decides whether multi-turn works.** The KV side is not only concurrency: prefix caching holds a finished sequence's blocks so the next turn can reuse them. When the pool cannot @@ -744,27 +749,32 @@ from the first token. The reply is the same either way. A hit whose nearest chec point would give back more than two windows is kept whole. `radiance:draft_refills_total` counts the hits ended early and `radiance:draft_refill_tokens_total` the tokens they recomputed. -**One depth for every request is a compromise; `RADIANCE_DRAFT_ADAPT=1` gives each sequence its -own.** Acceptance depends on the text: code and JSON keep accepting deep into the window, prose and -summaries fall off after a position or two. With the switch set, `--num-speculative-tokens` is the -ceiling, each sequence keeps a smoothed acceptance per draft position, and it drafts into the window -that maximises the tokens a step is expected to emit over what that step costs. The costs are -`RADIANCE_DRAFT_COST`, the step time at each fixed depth on the same setup, in any unit: +**One depth for every request is a compromise; `--draft-window adaptive` lets the text decide.** +Acceptance depends on the text: code and JSON keep accepting deep into the window, prose and +summaries fall off after a position or two. With the adaptive window, `--num-speculative-tokens` is +the ceiling and the engine keeps a smoothed acceptance per draft position for every sequence: ```sh -RADIANCE_DRAFT_ADAPT=1 RADIANCE_DRAFT_COST=1:9.9,2:11.0,3:12.06,4:13.32,5:14.37,6:15.77 \ -RADIANCE_DRAFT_ADAPT_BUSY=3 radiance --model m.rad --num-speculative-tokens 6 +radiance --model m.rad --num-speculative-tokens 6 --draft-window adaptive ``` -The table belongs to the hardware; without one a step is priced at 1 + 0.06·d. A window moves only -past a hysteresis and a hold, because every new window is a new pass shape and a new recording. The -table prices a step at one sequence, and with more running a deeper window also costs every -neighbour, so `RADIANCE_DRAFT_ADAPT_BUSY=N` caps every window at `RADIANCE_DRAFT_ADAPT_BUSY_DEPTH` -(default 3) while more than N sequences run. The window is the serial drafter's policy: with a block -drafter the switch is turned off at load, with a warning. At temperature 0 the text is that of a -fixed depth wherever fixed depths agree with each other; above 0 it is distribution-exact. -`radiance:draft_depth_adaptive` and `radiance:draft_verify_depth_total` (§6.3) show where the -windows stand. +A serial draft head runs as many rounds as the deepest window in the step, so the depth is chosen +for the step, not for one sequence: each step's head depth is the one that maximises the tokens all +its sequences are expected to emit over what such a step costs. The cost is measured as the server +runs — the time of every decode step, by its depth and by how many sequences it carried — so there +is nothing to enter; a depth not yet measured is priced from the nearest one that was. Under that +depth each sequence stops at the first draft that would be emitted — it and every draft before it +accepted — less than one time in ten, so a prose reply beside a code reply does not pay verify rows +for drafts nobody takes. The depth moves only after eight steps and for a two-percent gain, because +every new window is a new pass shape and a new recording. A new sequence starts from what the +sequences before it finished on, each counted once. + +The adaptive window is the serial draft head's policy. With a block drafter, with prompt lookup +alone, or with speculation off the window stays fixed, and the server says so at startup. Prompt +lookup proposals made when the head declines are counted in the acceptance metrics but do not move +the head's window. At temperature 0 the text is that of a fixed depth wherever fixed depths agree +with each other; above 0 it is distribution-exact. `radiance:draft_depth_adaptive` and +`radiance:draft_verify_depth_total` (§6.3) show where the windows stand. The default is `fixed`. ### 4.7 Two cards @@ -1031,7 +1041,8 @@ one line naming each one the command line moved. | flag | default | | |---|---|---| -| `--num-speculative-tokens N` | auto | speculative window; 0 disables | +| `--num-speculative-tokens N` | auto | speculative window; 0 disables. The ceiling under `--draft-window adaptive` | +| `--draft-window MODE` | fixed | `fixed` drafts N every step; `adaptive` follows each sequence's acceptance and the measured step cost, serial draft heads only (§4.6) | | `--draft-model PATH` | — | **not implemented** — merge at convert time | **Server** @@ -1504,7 +1515,7 @@ lists the efforts the chat template takes, or is `null` when it takes any. `serv | `draft_position_reached_total`, `draft_position_accepted_total`, `draft_position_acceptance_ratio` | acceptance by position: the two counters over any window, the ratio since start | | `draft_refills_total`, `draft_refill_tokens_total` | prefix hits ended a drafter window early, and the tokens recomputed (§4.6) | | `decode_rows_unspeculated_total` | rows that never drafted at all | -| `draft_depth_adaptive`, `draft_verify_depth_total{depth}` | the adaptive window's mean over the running sequences, and verifies by the drafts they carried; only with `RADIANCE_DRAFT_ADAPT` (§4.6) | +| `draft_depth_adaptive`, `draft_verify_depth_total{depth}` | the adaptive window's mean over the running sequences, and verifies by the drafts they carried; only with `--draft-window adaptive` (§4.6) | | `attn_cached_tokens_total`, `linear_cached_tokens_total` | prefix reuse, both halves | | `linear_checkpoints_written_total`, `linear_prefix_cache_hit_rate` | | | `prefix_cache_evictions_total`, `prefix_cache_ckpt_evictions_total` | | @@ -1618,10 +1629,6 @@ rad-kbench --home $RADIANCE_HOME --max-cases 1 # are the kernels correct at al | `RADIANCE_HOME` | where `kernels/`, `architectures/` and `quantizers/` live; a `:`-separated search path, the first home winning. Same as `--radiance-home` | | `RADIANCE_KERNELS` | default kernel hierarchy, comma-separated. Same as `--kernels`, read by the engine *and* every tool, so a plugin validated with `rad-kbench` can be served without editing launchers | | `RADIANCE_LOG` | `error` \| `warn` \| `info` \| `debug` \| `trace` | -| `RADIANCE_DRAFT_ADAPT` | `1`: a draft window per sequence, `--num-speculative-tokens` the ceiling (§4.6) | -| `RADIANCE_DRAFT_COST` | with it, the step cost by depth, `d:cost,…` | -| `RADIANCE_DRAFT_ADAPT_BUSY`, `_BUSY_DEPTH` | above N running sequences, every window capped at `_BUSY_DEPTH` (3) | -| `RADIANCE_DRAFT_ADAPT_HYST`, `_HOLD`, `_HALFLIFE`, `_PRIOR`, `_MIN` | the policy's knobs: 0.02, 8 verifies, 16 verifies, 0.8, 1 | | `ROCM_PATH` | where ROCm lives, if not `/opt/rocm` | | `HF_TOKEN` | for `scripts/hfget.sh` on a gated repo | diff --git a/docs/KERNELS.md b/docs/KERNELS.md index db0642d3..72990a80 100644 --- a/docs/KERNELS.md +++ b/docs/KERNELS.md @@ -232,7 +232,7 @@ not. | `ple_gate` | | device | host | | `qk_norm_rope` | | - | host | | `qk_norm_rope_gate` | | device | - | -| `qsa_block_key` | | device | - | +| `qsa_block_key` | | device | host | | `qsa_score` | | device | - | | `qsa_select` | | device | - | | `qsa_tail_store` | | device | - | diff --git a/docs/OPS.md b/docs/OPS.md index d8285f15..fcdc9969 100644 --- a/docs/OPS.md +++ b/docs/OPS.md @@ -1161,7 +1161,7 @@ stages of its sorting network when the runs are a power of two long and fill the over two runs of 1024 where a full sort is 66 -- and the whole network otherwise. A producer that hands it unsorted runs gets a wrong order, not a slow one. -## Sparse attention (QSA) — device-only +## Sparse attention (QSA) | op | params | operands | |---|---|---| @@ -1173,7 +1173,8 @@ hands it unsorted runs gets a wrong order, not a slow one. Five ops decompose one thing: an indexer that scores blocks of key history against the query and hands `attn_paged` a shorter block table. `docs/QSA.md` is that decomposition; these are the -schemas, and `arch/common/rad_qsa.h` issues all five. +schemas, and `arch/common/rad_qsa.h` issues all five. **`qsa_block_key` has a host kernel in libref +and libavx** -- the oracle for libr4d's, YaRN included -- and the other four are **device-only**. `qsa_block_key` builds the **compressed key**: one vector per `ratio` tokens of key history, which is what the indexer's block scores are taken against. @@ -1182,6 +1183,11 @@ is what the indexer's block scores are taken against. out[b] = rope( rmsnorm( mean over j 21692 MiB (every weight resident wants 21.18 GiB), and 7061 MiB stay unclaimed -I[2] mover: 25142 slab slots (64 shadow), 0 pinned pool slots (was 22479 slots, 2663 pinned) +kv: rank 2's cache costs 2.27 GiB at the worst case where its dearest peer's costs 15.16 GiB, so its KV pool pages the common 1446302 tokens in 1811 MiB instead of 10881; its weights pool grows 19747 -> 21692 MiB (every weight resident wants 21.18 GiB), and 7125 MiB stay unclaimed +I[2] mover: 25142 slab slots (64 shadow), 0 pinned pool slots ``` +The settle check's INFO line on that card names the unclaimed MiB and points at this line rather +than at the budget's `unusable` line alone. + Under a smaller attention world the plain even shard over `world` is the wrong cut for every attention projection, so the attention ranks declare **spans** (`rad_weight_shard_span`, the same call the KV replication uses for k/v): q|gate rows and output columns `[attn_rank * n_head, diff --git a/docs/YARN-VALIDATION.md b/docs/YARN-VALIDATION.md deleted file mode 100644 index af1050f4..00000000 --- a/docs/YARN-VALIDATION.md +++ /dev/null @@ -1,248 +0,0 @@ -# YaRN qualification and final-diff audit - -## Decision - -The six published Qwen containers passed the recorded functional checks. The host -regression suite passed 41/41 tests. Architecture routing is covered by source review, -declaration tests and model runs as distinguished below. - -**Both compatibility findings are fixed and their focused regressions pass.** -The original findings and remedies are in section 5. The model runs predate these -fixes and do not exercise those edge cases. -Concurrency claims are limited to overlapping server request lifetimes; continuous -dual-context GPU residency has not been established by these logs. - -Audit date: 2026-10-06. Reviewed implementation: `1d42304`, against released upstream -`e95d397` (Radiance 1.2.0), including `3d7f066` and merge `10721e6`. -No production code was changed during the initial audit. The subsequent compatibility -fixes change argument normalization and linear-factor validation as detailed below. -At the initial audit, all 24 changed non-YARN-doc files -matched the remote source/fixtures by SHA-256; the remote `docs/YARN.md` was older. -`git diff --check upstream/main...HEAD` passed. This is a direct source review, -not an independent second-reviewer sign-off. - -## 1. Architecture audit - -| Architecture / component | Route traced | Evidence and boundary | -| --- | --- | --- | -| `qwen35_fp8` | `yarn_from` resolves target coefficients; `decl_rope` handles Q/K; `decl_rotary` handles shared `rope_table`; fused prologues consume that table | Three 27B encodings passed actual model checks; direct/table and fused/unfused numerical checks | -| DFlash2 | Own `Geom`, theta and `draft.*` metadata; context K, draft Q and draft K all use `decl_rotary` | Independent target/draft parser checks; 128-wide numerical checks; model logs record DFlash2 passes | -| `qwen35_bf16` | Target metadata resolved after partial rotary width; gated attention uses `decl_rope` | Dedicated declaration test and shared kernels; no BF16-weight end-to-end model run | -| `qwen35moe_fp8` | Includes the Qwen35 FP8 implementation with routed-FFN macros | 35B-A3B model passed at TP2; TP4 stock fails unsupported BF16 attention geometry | -| Qwen35 MTP | `MtpBlockFP8::declare` copies target `Geom` and passes it to attention | Source trace plus actual 35B MTP startup and recorded draft passes; no isolated YaRN/MTP declaration regression | -| `qwen4exp_fp8` | Target direct/table; QSA query indexer and compressed keys receive target coefficients; indexer rotary width equals target `m.n_rot` | Dedicated declaration test, compressed-key GPU oracle and both Flash model runs | -| Qwen4 MTP | `MtpHcBlock` copies target `Geom`; attention and QSA reuse it | Declaration fixture requests speculation; actual Flash logs record MTP passes | -| Vision tower | Existing axial rotations do not receive target YaRN parameters | Source trace and red-image smoke checks; not a broad multimodal-quality evaluation | -| Llama plugins | Do not call `yarn_from`; shared rotary ops retain absent-parameter defaults | Outside architecture-level YaRN support; no new Llama support claimed | - -Source anchors: `arch/qwen35_fp8/qwen35_fp8.cpp:343,639,957`, -`arch/qwen35moe_fp8/qwen35moe_fp8.cpp:20-24`, -`arch/qwen35_bf16/qwen35_bf16.cpp:240`, -`arch/qwen4exp_fp8/qwen4exp_fp8.cpp:367,700,1004,1397`, -`arch/common/rad_block_dflash2.h:541,715,723`, -`arch/common/rad_block_mtp_fp8.h:219,290`, -`arch/common/rad_block_mtp_hc.h:203-212,291-294`, -`arch/common/rad_qsa.h:162,230,272-281`. - -### Compatibility contract - -- Absent/default rotary metadata leaves the existing positional scale and adds no - YaRN op parameters. Default op identities therefore retain their parameter lists. -- Static YaRN uses the explicit original training length, not runtime/table length. - Target and DFlash2 configuration are independent. MTP shares target geometry. -- Both modern `rope_parameters` and legacy `rope_scaling` are read. Precedence is - nested modern, top-level modern, nested legacy, top-level legacy; the first type - found wins. Conflicting layouts are not merged. This precedence is documented, - but lacks a dedicated conflicting-layout regression. -- Unknown explicit rotary types now fail instead of being silently ignored. - `mscale` / `mscale_all_dim` are intentionally unsupported. This is an intentional - compatibility change for previously ignored metadata, not universal stock parity. -- YaRN plus axial mode or positional scale other than one is rejected by rotary - kernels. Model-level YaRN requires a positive even rotary width and finite inputs. -- Checkpoint JSON booleans are flattened to `1`/`0` - (`core/format/checkpoint.cpp:64`), matching `truncate`'s parser. -- Optional op-schema parameters are consistent across reference, AVX and HIP - rotary implementations. Compressed-key support is in the existing HIP-only op. - Rebuild plugins together; no mixed-old/new-plugin compatibility is claimed. -- Existing stock tests and stock checkpoints passed. No bitwise comparison of - complete upstream-versus-branch stock-model logits or throughput was performed. - -## 2. Model evidence - -All extended probes used static factor 2, original context 262144, BF16 KV and -prefix caching disabled. DFlash2 copies received their own factor/original keys. -Every single probe used **499997 prompt tokens**; each paired probe used -**255981 prompt tokens**, with three planted values at roughly 5%, 50%, 95% depth. -All returned exactly the requested values and `finish_reason=stop`. - -| Model (all under `StillDeadcode`) | TP | Single seconds | Pair A / B seconds | Run evidence | -| --- | ---: | ---: | ---: | --- | -| `qwen3.8-27b-fp8` | 4 | 309.4 | 283.6 / 283.6 | `v12-media-qualification.json`, `v12-media-engine-audit.log` | -| `qwen3.8-27b-uncensored-fp8` | 4 | 309.8 | 283.0 / 282.9 | `matrix-20261006-181056` | -| `qwen3.8-27b-mxfp4` | 4 | 301.9 | 274.4 / 274.4 | `matrix-20261006-181056` | -| `qwen3.6-35b-a3b-fp8` | 2 | 197.3 | 121.5 / 121.5 | `matrix-20261006-185632` | -| `qwen3.8-next-flash-fp8-iq4r-moe` | 4 | 174.5 | 80.9 / 163.3 | `matrix-20261006-183853` | -| `qwen3.8-next-flash-uncensored-fp8-iq4r-moe` | 4 | 141.2 | 133.5 / 67.2 | `matrix-20261006-183853` | - -Matrix runs include stock and YaRN arithmetic, tool-envelope, red-image and -approximately 20K/180K retrieval checks. The original 27B baseline was collected -separately (`ember-baseline*`, `baseline-retrieval.json`), not in this matrix; -the release-1.2 media-enabled YaRN run has its own smoke results. -These are synthetic functional checks, not a broad accuracy, perplexity or speed -comparison. Timings are single observations, not comparative benchmark medians. - -Hardware: four Radeon AI PRO R9700, gfx1201, approximately 31.86 GiB each; -121 GiB host RAM; Linux `7.2.3-070203-generic`. The 35B run uses TP2. -HIP/media build uses `g++-14`, `RAD_WITH_FFMPEG=ON`, AQL device backend. -Recorded serving executable SHA-256: -`0c99111b9514e5f30d17b98ce590a588a8cc857373cd7b61d170d03adf16e911`. - -Matrix launch options (exact per-model argv lives in each `command.json`): - -```sh -radiance --model MODEL.rad --host 0.0.0.0 --port 8000 \ - --served-model-name Ember --tp 4 --max-num-seqs 8 \ - --max-model-len 524288 --kv-cache-dtype bf16 \ - --gpu-headroom-mib 256 --no-prefix-cache --reasoning-effort medium -``` - -Stock uses `--max-model-len 262144`. 35B uses `--tp 2`. Both Flash models add -`--placement expert_tiered --host-pool-mib 24576 --max-num-batched-tokens 2048`. -The original 27B media trial used `--max-num-seqs 32`. Do not assume identical -capacity across those configurations. The retained container argv includes device -mapping, memlock, source/model mounts and plugin home. - -The five new YaRN artifacts were copied to separate files, then metadata appended; -verification checked all 21855 weights for 35B, 1101 for each new 27B, and 51489 -for each Flash variant. Tokenizer, template and expert profile were identical. -This tested metadata-copy workflow is distinct from the documented checkpoint -converter command; the full published-model matrix did not rerun that converter. - -### Stock artifact SHA-256 - -| Model suffix | SHA-256 | -| --- | --- | -| 27B FP8 | `e8f662be2be37312fb07b82d1aa6c58c6507856425ce0f28028bdd9440400737` | -| 27B uncensored FP8 | `fdb034bd0639d8fcc1c6d97d3b23939286ac7a7d114d1d53501653dd802fffe2` | -| 27B MXFP4 | `44aec7fb78d9515907c6ae46e10e506a8578155013af227247f22f3c63c997b2` | -| 35B-A3B FP8 | `deb3f9844cc5419ca8f546497b09fa7380f933f13e8244aa2af505bdfe593f36` | -| Flash FP8 IQ4R | `0af5e96244e80c21ac8edca1719dae49b8a201b94a2f3994c3e24026ceaa4d20` | -| Flash uncensored FP8 IQ4R | `19276213c4905100555508cf9d9383ae393424985f06c99b15935eaea5471f60` | - -Download revisions and sizes are retained in `models/qwen-download-manifest.json`. - -## 3. Concurrency verification - -`audit-evidence.py` matched each response ID to its server request-start and -completion lines, rechecked the exact answers, and asserted that both starts -preceded either completion. All six pairs passed. Both requests report cache=0. -The resulting `concurrency-audit.json` records request IDs, line numbers, exact log -excerpts and SHA-256 hashes of the input logs/results. - -| Model | Pair start lines | Pair completion lines | Engine log | -| --- | --- | --- | --- | -| 27B uncensored | 137, 138 | 148, 149 | corresponding `181056/*-yarn/engine.log` | -| 27B MXFP4 | 127, 128 | 138, 139 | corresponding `181056/*-yarn/engine.log` | -| 35B | 107, 108 | 109, 110 | corresponding `185632/*-yarn/engine.log` | -| Flash | 161, 162 | 163, 164 | corresponding `183853/*-yarn/engine.log` | -| Flash uncensored | 161, 162 | 163, 164 | corresponding `183853/*-yarn/engine.log` | - -Original 27B IDs and line numbers are also in the machine-readable audit; its log -contains several later server restarts, so it is retained separately. - -This proves overlapping HTTP/server request lifetimes and successful paired -completion. It does **not** identify scheduler admission, per-step sequence IDs, -preemption or simultaneous KV occupancy. The Flash completion timing is compatible -with substantial serialization. Dense 27B and 35B timings suggest overlapping -progress, but timing alone is not a GPU-residency measurement. A stronger claim -requires scheduler/batch traces or timestamped active-sequence/KV telemetry. - -## 4. Regression and numerical evidence - -- `final-host.log`: 41/41 CTest tests passed, 1633.32 seconds total, including - `yarn_test`, `arch_test`, `ref_test`, `avx_check`, `ops_doc`, out-of-tree plugins, - `kbench` and `kbench_moe`. Host suite success is not a full GPU-suite run. -- `v12-gpu.log`: explicit GPU YaRN table comparisons at rotary widths 64/128, - fused/unfused Q/K/gate byte equality, and compressed-key reference checks passed. -- CPU fixture cases cover widths 64/128/256 and positions through 524287. - GPU numerical oracle coverage is 64/128; presence of a 256-wide fixture is not - evidence of a 256-wide GPU run. Cos/sin tolerance is 0.004; compressed BF16 keys - use 0.008 absolute error. -- AVX checks compare against libref; they are not a separate direct-vLLM oracle. -- Independent fixtures use vLLM frequency computation and backend-specific torch - FP32 trigonometry. Exact generating vLLM/torch revisions are not embedded in the - fixture; pin/record them before calling fixture regeneration fully reproducible. - -Observed run limitations are preserved: initial 35B TP4 stock failure; obsolete -Flash launch flag on the first attempt; Flash per-card headroom warnings of roughly -157–165 MiB below its stated target; five-second device synchronization diagnostics -during some successful long probes. No claim of warning-free production qualification -is made. Stock success uses the feature branch, not a clean upstream binary. - -## 5. Final-diff findings - -### Resolved P2 — disabled coefficients diverged across reference and optimized backends - -`abi/rad_yarn.h:40-42` accepts factor zero without validating/normalizing magnitude. -`libref/ref_attn.cpp:500,597-598` applies `yarn.magnitude` unconditionally, while AVX -and HIP apply it only when factor is nonzero. With `{factor=0, magnitude=2}`, libref -scales the rotation by two and optimized backends preserve the stock rotation. -NaN magnitude can similarly contaminate only reference output. - -Architecture-generated stock ops omit all four fields, so the six model runs do -not hit this. It remains a public optional-op-parameter contract inconsistency. -The fix normalizes disabled coefficients to stock defaults in `rad_yarn_args`, shared -by all three backends. Reference direct/table tests cover magnitude 2 and NaN with -disabled factor and NaN bounds; AVX-versus-reference cases cover magnitude 2 and NaN. - -### Resolved P2 — linear factor validation happened before narrowing to float - -`arch/common/rad_arch.h:1068-1072` accepts positive finite doubles then casts to -`float` without checking representability. A compiled probe against the real parser -returned `RAD_OK` for `1e300` with scale infinity, and for `1e-300` with scale zero. -The former collapses finite positions toward zero; the latter takes the kernel's -zero-scale fallback to unscaled positions. Both silently change the intended model. -The fix checks the narrowed value is finite and positive before assigning it. -The parser regression rejects overflow, underflow and invalid values, checks that -rejection preserves the previous scale, and accepts representable fractional scaling. - -Reproducer: local `audit-edge-probe.cpp` includes the real architecture header and -supplies only its metadata lookup dependency. Its disabled-magnitude demonstration -evaluates the reviewed formulas; it is not a full kernel launch test. - -### P3 — reproducibility and coverage hardening - -- Pin vLLM/torch versions and record backend/version metadata in the fixture generator. -- Add explicit declaration assertions separating target and DFlash2 coefficients, - plus MoE/MTP and conflicting-metadata precedence cases. Current evidence combines - parser tests, shared source paths and model runs rather than isolated assertions. -- The rest of the traced YaRN propagation showed no missing rotary path. - -### Fix verification - -`compat-fixes-host.log` records a successful host rebuild and 5/5 focused CTest -targets: `yarn_test`, `ref_test`, `arch_test`, `avx_check` and `ops_doc`. -This includes the new parser and disabled-coefficient regressions. -The previous 41/41 suite and GPU/model results remain historical pre-fix evidence; -the GPU serving build was not rebuilt or restarted for this host-side verification. - -## Evidence location and reproduction - -Raw evidence is retained locally at -`~/repo/homelab/.local/experiments/radiance-20261006/evidence/`. -Trial tools are in its parent: `validate-qwen-matrix.py`, `smoke.py`, `retrieval.py`, -`qualify-yarn.py`, `relabel-rad.cpp`, `verify-rad-vocab.cpp`, `audit-evidence.py` and -`audit-edge-probe.cpp`. Raw logs are not bundled in this source change. - -```sh -ctest --test-dir build-host --output-on-failure -python3 audit-evidence.py > concurrency-audit.json -python3 smoke.py http://127.0.0.1:8080 smoke.json -python3 retrieval.py http://127.0.0.1:8080 retrieval.json -python3 qualify-yarn.py http://127.0.0.1:8080 qualify-yarn.json -``` - -The last three commands require the intended model already serving and run real -inference. The matrix runner stops/starts trial containers; it is not a generic -upstream CI tool. Package the portable probes and sanitized result excerpts with -the PR if reviewers need to reproduce the model qualification outside this lab. diff --git a/docs/YARN.md b/docs/YARN.md index a9101fc1..1b541cdb 100644 --- a/docs/YARN.md +++ b/docs/YARN.md @@ -1,48 +1,43 @@ -# Static YaRN for Qwen architectures +# Static YaRN Increasing `--max-model-len` reserves a larger serving window; it does not change -the model's rotary embeddings. A Qwen checkpoint trained at 262144 positions can +the model's rotary embeddings. A checkpoint trained at 262144 positions can explicitly request static YaRN for a 524288-token deployment. Whether that extension preserves useful accuracy must be measured on the model and workload. -The Qwen architectures read the target's `text_config.rope_parameters`, -then `rope_parameters`, then their legacy `rope_scaling` spellings, in that order. -The first object with `rope_type` (or legacy `type`) is authoritative. The DFlash2 -drafter reads its own `draft.rope_parameters` / `draft.rope_scaling`; it does not -inherit the target's scaling. The vision tower keeps its original axial rotations. +One reader takes every architecture's rotary scaling (`yarn_from`, +`arch/common/rad_arch.h`). For the target it reads `text_config.rope_parameters`, +then `rope_parameters`, then the legacy `text_config.rope_scaling` and `rope_scaling`, +in that order; the first object with `rope_type` (or legacy `type`) is authoritative. +A drafter (DFlash2, DSpark) reads its own `draft.rope_parameters` / +`draft.rope_scaling`; it does not inherit the target's scaling. The vision tower keeps +its original axial rotations. A GGUF's `rope.scaling.*` keys are carried in as the +same `rope_scaling` keys when it is read. -## Coverage +**The factor convention is the config's**, as vLLM and transformers read it: +`factor` 2 over `original_max_position_embeddings` 262144 reaches 524288 positions. -| Architecture | Rotary paths | Validation so far | -| --- | --- | --- | -| `qwen35_fp8` | Target direct/table; independent DFlash2 | 27B FP8, uncensored FP8 and MXFP4: text/tool/image checks, 499997-token retrieval and two overlapping 255981-token requests, TP4/BF16 KV | -| `qwen35_bf16` | Target direct rotary | Architecture declaration and shared numerical tests; no end-to-end BF16 model run yet | -| `qwen35moe_fp8` | Shared Qwen35 target and MTP geometry | 35B-A3B stock/YaRN smoke and retrieval, including 499997 tokens and paired 255981-token requests; TP2/BF16 KV | -| `qwen4exp_fp8` | Attention, indexer, compressed block keys and shared MTP geometry | Both Flash variants: stock/YaRN smoke and retrieval, including 499997 tokens and paired 255981-token requests; TP4, tiered experts, BF16 KV | - -Radiance 1.2.0 also serves the published Qwen3.8-27B MXFP4 container through the -Qwen35 FP8 architecture's encoding dispatch. It passed the model checks above. -Request logs prove overlapping request lifetimes, not continuous simultaneous GPU -residency or a common decode batch. See [qualification and audit](YARN-VALIDATION.md) -for exact settings, timings, evidence boundaries and outstanding review findings. -Synthetic retrieval is evidence for those requests, not a general accuracy benchmark -or a capacity guarantee for other model sizes. Original-context quality should be -checked alongside extended-context quality when deploying static YaRN. +Validated so far: the Qwen3.8 27B and 35B-A3B containers and both Flash-Next +variants at factor 2, with retrieval at ~500K tokens, and TP3 perplexity with +`--rope-scaling` within roughly ±1% of stock at factor 2. The LLaMA plugins read the +same keys and declare the same ops; YaRN there has had no model run. ## Metadata | Key within the rotary object | Meaning | | --- | --- | -| `rope_type` | `yarn`; default/absent preserves stock behavior; `linear` divides positions uniformly | +| `rope_type` | `yarn`; `default`, absent or legacy `mrope` is no scaling; `linear` divides positions by `factor` | | `factor` | finite number >= 1, required for YaRN | | `original_max_position_embeddings` | positive integer, required; the **training** length, not the enlarged deployment/table length | | `beta_fast`, `beta_slow` | defaults 32 and 1; finite, fast >= slow > 0 | | `attention_factor` | positive finite cos/sin multiplier; default 1 + 0.1 ln(factor) for factor > 1, otherwise 1 | -| `truncate` | 1 (default): floor/ceil correction bounds; 0: retain fractional bounds | +| `truncate` | true/1 (default): floor/ceil correction bounds; false/0: retain fractional bounds | -`mscale` and `mscale_all_dim` variants are refused; specify `attention_factor` -explicitly. Other explicit rotary types are refused in this architecture. Rotary -width (including partial rotary), theta and M-RoPE section selection are unchanged. +Every other type -- `llama3`, `longrope`, `dynamic` -- is refused by name at load, +as are the `mscale` and `mscale_all_dim` variants (specify `attention_factor`). A +GGUF that sets llama.cpp's YaRN `attn_factor`, `ext_factor` or log multiplier to +anything but its neutral value is refused when read. Rotary width (including +partial rotary), theta and M-RoPE section selection are unchanged. ## Convert a separate container @@ -95,7 +90,7 @@ radiance --model /models/qwen3.8-next-flash-fp8.rad \ (`ORIG`, default the container's own trained length) in the rotary object the reader above takes first -- for a Qwen3.8 container that is `text_config.rope_parameters`, which already says `default`; YaRN written under plain `rope_parameters` would sit behind it and never be read. A -DFlash2 drafter gets the same factor over its own trained length. Over a checkpoint's own YaRN +drafter gets the same factor over its own trained length. Over a checkpoint's own YaRN the factor is replaced, its trained length kept unless `ORIG` is given, and an explicit `attention_factor` (or `mscale` variant) dropped so the magnitude is derived for the new factor. `none` takes a checkpoint's YaRN off. A linear or other explicit rotary type is refused rather @@ -104,28 +99,29 @@ than stacked over. The result is resolved and checked exactly as a converted con Startup says what the rotary is (`rope: YaRN factor 2 over 262144 trained positions -- the rotary reaches 524288`) and warns when `--max-model-len` is past what it covers -- also with no scaling -at all, past the trained length. A persisted prefix cache (`--prefix-cache-dir`) written under a -different `--rope-scaling` is not reused: every cached key was rotated differently. +at all, past the trained length. A persisted prefix cache (`--prefix-cache-dir`) is fingerprinted +with the resolved rotary of the target and the drafter, so a store written under a different +one -- another `--rope-scaling`, or a container converted with YaRN -- is not reused: every cached +key was rotated differently. The flag is for trying a factor, or serving a container whose source checkpoints are not at hand; a deployment that settles on one can convert it in as above and drop the flag. ## Kernel and test contract -`rope` and `rope_table` take optional resolved coefficients `yarn_factor`, -`yarn_low`, `yarn_high`, `yarn_magnitude`. Absent factor is the original path. -With YaRN, `scale` must be 1: inverse frequencies are blended rather than all -positions being divided. Axial rotary with YaRN is refused. Rebuild the bundled -plugins together because their shared op schemas must match. +`rope`, `rope_table` and `qsa_block_key` take optional resolved coefficients +`yarn_factor`, `yarn_low`, `yarn_high`, `yarn_magnitude` (docs/OPS.md). Absent factor +is the original path, with the original op identities. With YaRN, `scale` must be 1: +inverse frequencies are blended rather than all positions being divided. Axial +rotary with YaRN is refused. Rebuild the bundled plugins together because their +shared op schemas must match. -The math follows the static YaRN formulation (Peng et al.) and is checked against -independent vLLM `YaRNScalingRotaryEmbedding` FP32 vectors for each backend. The fixture generator -is `tests/data/generate_yarn_reference.py`; it is not a runtime dependency. -Vectors cover target rotary width 64, draft width 128, an additional width 256, -and positions through 524287. -The CPU fixture uses torch CPU; the HIP fixture uses torch ROCm on gfx1201 -(`generate_yarn_reference.py cuda`). CPU and ROCm `pow` differ by a few ULPs; -at 524K positions their cos/sin values can differ by 0.033 even within torch. -Compare each implementation against its backend's independent vectors. -The numerical check allows 0.004 absolute cos/sin error; fused versus unfused HIP rotation is checked -byte-for-byte separately. Existing stock rotary tests remain active. +The math follows the static YaRN formulation (Peng et al.) as vLLM's +`YaRNScalingRotaryEmbedding` implements it. `tests/yarn_test.cpp` pins the +correction bounds to vLLM's integers (14/22, 28/43, 56/85 for rotary widths 64, 128, +256 at factor 2, theta 1e7, 262144 trained positions); `tests/ref_test.cpp` and +`r4d_selftest` compare libref and libr4d against a double-precision evaluation of the +same formula at positions through 524287, within 8 ulp of each angle -- tight where +the ramp sits, loose only in the fastest pairs at long positions, which no f32 rotary +places better than its angle's own ulp. libavx is held to libref by `avx_check`, and +fused versus unfused HIP rotation is checked byte-for-byte separately. diff --git a/libavx/avx_attn.cpp b/libavx/avx_attn.cpp index a63d8981..2a2702b0 100644 --- a/libavx/avx_attn.cpp +++ b/libavx/avx_attn.cpp @@ -987,3 +987,105 @@ AVX_KERNEL(rope_table) { } return RAD_OK; } + +/* ------------------------------------------------------------------ qsa_block_key */ +/* QSA's compressed key (docs/OPS.md): a block's `ratio` rows of k averaged in f32 and rounded to + * k's dtype, RMS-normed with gain (wadd + w), then NeoX-rotated over `rotary_dim` at the block's + * position, YaRN included. libref's arithmetic in libref's order; what differs is that the angles + * of a block come from one `angles` table, and the inverse frequencies are built once a launch. */ +AVX_KERNEL(qsa_block_key) { + const RadTensor* k = rad_arg_in(a, 0); + const RadTensor* w = rad_arg_in(a, 1); + const RadTensor* page = rad_arg_in(a, 2); + const RadTensor* pos = rad_arg_in(a, 3); + const RadTensor* out = rad_arg_in(a, 4); + if (!k || !w || !out) return RAD_E_INVAL; + const int64_t n = p_int(a, "n", 0), ratio = p_int(a, "ratio", 0); + const int64_t rot = p_int(a, "rotary_dim", 0), pos0 = p_int(a, "pos0", -1); + if (n <= 0 || ratio <= 0 || (pos0 < 0 && !pos)) return RAD_E_INVAL; + if (rot < 0 || rot > n || (rot & 1) || rot / 2 > 512) return RAD_E_SHAPE; + if (p_int(a, "block_size", ratio) != ratio) return RAD_E_SHAPE; + if (k->rank != 2 || out->rank < 2 || out->shape[0] < 1) return RAD_E_SHAPE; + const int64_t M = page ? numel(page) : out->shape[0]; + if (M < 1 || numel(out) / out->shape[0] != n) return RAD_E_SHAPE; + if (k->shape[0] != M * ratio || k->shape[1] != n || numel(w) < n) return RAD_E_SHAPE; + const bool pos2 = pos && pos->rank == 2; + if (pos && (pos2 ? pos->shape[0] < M : numel(pos) < M)) return RAD_E_SHAPE; + + const float eps = p_f32(a, "eps", 1e-6f), wadd = p_f32(a, "wadd", 1.0f); + const float theta = p_f32(a, "theta", 10000.0f), rscale = p_f32(a, "scale", 1.0f); + static const char* kModes[] = { "neox", "mrope", "imrope" }; + const int mi = p_enum(a, "mode", kModes, 3, 0); + if (mi < 0) return RAD_E_UNSUPPORTED; + int64_t sec[3] = {}; + int n_sec = 0; + for (const char* v = p_str(a, "sections", nullptr); v && *v;) { + char* e = nullptr; + const long long x = std::strtoll(v, &e, 10); + if (e == v) break; + if (x <= 0 || n_sec >= 3) return RAD_E_SHAPE; + sec[n_sec++] = x; + v = e; + } + /* Several components only with a mode, its sections and three columns of `pos`. */ + const bool mc = mi != 0 && n_sec > 0 && pos2 && pos->shape[1] >= 3; + const RadYarn yarn = rad_yarn_args(a); + if (!rad_yarn_valid(yarn) || (yarn.factor != 0.0f && (rot <= 0 || rscale != 1.0f))) + return RAD_E_INVAL; + const int64_t half = rot / 2; + float* invf = inv_freqs(half, rot, theta); + int comp[512] = {}; + if (mc) { + int64_t total = 0; + for (int c = 0; c < n_sec; ++c) total += sec[c]; + if (mi == 1 && total != half) return RAD_E_SHAPE; + for (int64_t i = 0; i < half; ++i) { + int c = 0; + if (mi == 1) { + int64_t start = 0; + while (c + 1 < n_sec && i >= start + sec[c]) { start += sec[c]; ++c; } + } else if (n_sec >= 3) { + if (i % 3 == 1 && i < 3 * sec[1]) c = 1; + else if (i % 3 == 2 && i < 3 * sec[2]) c = 2; + } + comp[i] = c; + } + } + for (int64_t i = 0; i < half; ++i) invf[i] = yarn.frequency(invf[i], (int)i); + float* gain = (float*)scratch_f32_c((size_t)n); + for (int64_t t = 0; t < n; ++t) gain[t] = wadd + ldt(w, offlin(w, t)); + + AVX_PARALLEL_FOR_IF(M * ratio * n >= AVX_PAR_MIN) + for (int64_t b = 0; b < M; ++b) { + const int64_t orow = page ? ldi(page, offlin(page, b)) : b; + if (orow < 0) continue; + float* y = scratch_f32((size_t)n); + float* cs = scratch_f32_b((size_t)(rot > 0 ? rot : 1)); + double ss = 0.0; + for (int64_t t = 0; t < n; ++t) { + float acc = 0.0f; + for (int64_t j = 0; j < ratio; ++j) acc += ldt(k, offlin(k, (b * ratio + j) * n + t)); + uint64_t tmp = 0; + st_elem(&tmp, k->dtype, 0, acc / (float)ratio); + y[t] = ld_elem(&tmp, k->dtype, 0); + ss += (double)y[t] * y[t]; + } + const float s = 1.0f / std::sqrt((float)(ss / (double)n) + eps); + for (int64_t t = 0; t < n; ++t) y[t] = y[t] * s * gain[t]; + const float div = rscale != 0.0f ? rscale : 1.0f; + float pc[3] = {}; + for (int c = 0; c < (mc ? 3 : 1); ++c) + pc[c] = (float)(!pos ? pos0 + b * ratio + : ldi(pos, offlin(pos, pos2 ? b * pos->shape[1] + c : b))) / div; + if (mc) angles_multi(cs, invf, comp, half, pc); + else angles(cs, invf, half, pc[0]); + for (int64_t i = 0; i < half; ++i) { + const float c = cs[i] * yarn.magnitude, sn = cs[half + i] * yarn.magnitude; + const float x0 = y[i], x1 = y[half + i]; + y[i] = x0 * c - x1 * sn; + y[half + i] = x0 * sn + x1 * c; + } + for (int64_t t = 0; t < n; ++t) stt(out, offlin(out, orow * n + t), y[t]); + } + return RAD_OK; +} diff --git a/libavx/avx_cases.cpp b/libavx/avx_cases.cpp index 396b1547..8a9df914 100644 --- a/libavx/avx_cases.cpp +++ b/libavx/avx_cases.cpp @@ -225,6 +225,17 @@ std::vector build_cases() { add("rope_table", { HI("M", 12), HI("rot", 64), HF("theta", 1e7), HF("scale", 1), HS("dtype", "f32"), HF("yarn_factor", 0), HF("yarn_magnitude", magnitude) }); } + /* QSA's compressed key at Flash-Next's geometry (n 128, ratio 4, a 64-wide rotary), stock and + * with YaRN, positions drawn up to the block count times the ratio. */ + for (const char* dt : { "f32", "bf16" }) { + add("qsa_block_key", { HI("M", 12), HI("n", 128), HI("ratio", 4), HI("rotary_dim", 64), + HF("eps", 1e-6), HF("theta", 1e7), HF("scale", 1), HF("wadd", 1), + HS("dtype", dt) }); + add("qsa_block_key", { HI("M", 12), HI("n", 128), HI("ratio", 4), HI("rotary_dim", 64), + HF("eps", 1e-6), HF("theta", 1e7), HF("scale", 1), HF("wadd", 0), + HS("dtype", dt), HF("yarn_factor", 2), HF("yarn_low", 14), + HF("yarn_high", 22), HF("yarn_magnitude", 1.069314718) }); + } /* The multi-component forms: Qwen3-VL's interleaved M-RoPE over a partial rotary, and a vision * tower's 2-D axial rotary over a whole 72-wide head. */ for (const char* dt : { "f32", "bf16" }) { diff --git a/libavx/avx_isa.h b/libavx/avx_isa.h index f0f65ee4..b6be024d 100644 --- a/libavx/avx_isa.h +++ b/libavx/avx_isa.h @@ -100,7 +100,7 @@ int avx_isa_force(int level); X(gemm_nt_q_gated) X(embed_lookup) X(embed_lookup_q) X(logits_gemm) \ /* ---- attention */ \ X(attn_paged) X(attn_dense) X(kv_store) X(rope) \ - X(rope_table) X(qk_norm_rope) \ + X(rope_table) X(qk_norm_rope) X(qsa_block_key) \ /* ---- mixture of experts */ \ X(router_topk) X(moe_scatter) X(moe_gemm) X(moe_gemm_q) \ X(moe_gather) X(row_topk) X(row_topk_merge) X(logit_rerank) \ diff --git a/libavx/avx_registry.cpp b/libavx/avx_registry.cpp index 99d761c6..0b2c5f85 100644 --- a/libavx/avx_registry.cpp +++ b/libavx/avx_registry.cpp @@ -229,6 +229,18 @@ static const RadParamSpec pRopeTable[] = { P_INT("M"), P_INT("rot"), P_F64("th P_F64O("yarn_high"), P_F64O("yarn_magnitude") }; static const RadOperandSpec oRopeTable[] = { OPD("positions"), OUT("cos_sin") }; +/* QSA's compressed key, libr4d's schema exactly (libr4d/r4d_rows.cpp): `block_size` is `ratio` + * spelled for the KV group the block-key store is, `page` and `pos` are optional. */ +static const RadParamSpec pQsaBlockKey[] = { P_INT("M"), P_INT("n"), P_INT("ratio"), + P_INT("rotary_dim"), P_INTO("pos0"), + P_INTO("block_size"), P_F64("eps"), P_F64("theta"), + P_F64O("scale"), P_F64O("wadd"), P_STR("dtype"), + P_STRO("mode"), P_STRO("sections"), + P_F64O("yarn_factor"), P_F64O("yarn_low"), + P_F64O("yarn_high"), P_F64O("yarn_magnitude") }; +static const RadOperandSpec oQsaBlockKey[] = { OPD("k"), WGT("w"), OPD_O("page"), OPD_O("pos"), + OUT("out") }; + static const RadParamSpec pQkNormRope[] = { P_INT("M"), P_INT("head_dim"), P_INT("n_head"), P_INT("n_head_kv"), P_F64("theta"), P_F64("eps"), P_F64O("scale"), P_STRO("mode"), @@ -510,6 +522,10 @@ static const RadOpSchema kSchemas[] = { { "attn_dense", ARR(pAttnDense), ARR(oAttnDense), "dense varlen attention over cu_seqlens." }, { "kv_store", ARR(pKvStore), ARR(oKvStore), "scatter k and v into the paged cache." }, { "rope_table", ARR(pRopeTable), ARR(oRopeTable), "cos/sin at each position's own row." }, +{ "qsa_block_key", ARR(pQsaBlockKey), ARR(oQsaBlockKey), + "QSA's compressed key: the mean of a block's `ratio` rows rounded to k's dtype, RMS-normed with " + "gain (wadd + w), rotated at the block's position with `rope`'s ladder and YaRN; libref's " + "arithmetic with the angles from one table a block." }, { "rope", ARR(pRope), ARR(oRope), "rotary embedding in place." }, { "qk_norm_rope", ARR(pQkNormRope), ARR(oQkNormRope), "per-head QK norm then rotary embedding." }, { "scale_rows", ARR(pScaleRows), ARR(oScaleRows), @@ -697,6 +713,9 @@ ROW("avx_kv_store", "kv_store", "attn", "scatter k and v into the paged cache", SIMPLE("avx_rope", "rope", "attn", "rotary embedding in place", DT_FLOAT, L(rope)), SIMPLE("avx_rope_table", "rope_table", "attn", "cos/sin at each position's own row", DT_FLOAT, L(rope_table)), +SIMPLE("avx_qsa_block_key", "qsa_block_key", "attn", + "QSA compressed key: pooled, normed and rotated at the block's position", DT_FLOAT, + L(qsa_block_key)), SIMPLE("avx_qk_norm_rope", "qk_norm_rope", "attn", "per-head QK norm then rotary embedding", DT_FLOAT, L(qk_norm_rope)), diff --git a/libr4d/r4d.h b/libr4d/r4d.h index 1e73f840..148fd8a0 100644 --- a/libr4d/r4d.h +++ b/libr4d/r4d.h @@ -652,19 +652,22 @@ void r4d_moe_gemm_pin_prefill(int form); // every class's K a multiple of 256, a format that folds scale groups). The two are bit-identical, // and r4d_selftest compares their bytes. void r4d_moe_gemm_pin_fk(int pin); -// THE PREFILL MoE FORMS (R4D_MOE_PF2), pinned for a test: below zero the switch's rule, otherwise -// the value the switch would hold -- 0 none, 1 every part, else a mask of the parts (r4d_moe.hip -// lists them). Every part is bit-identical to the form it replaces, and r4d_selftest compares their -// bytes: moe_gather and moe_scatter at a prefill chunk, and the GEMM's run-aligned form. -void r4d_moe_pf2_pin(int pin); -// The parts the launches since the last call took, under a pin, as R4D_MOE_PF2's mask; cleared by -// the call. A test reads it to know a part's kernel ran rather than the default's. -uint32_t r4d_moe_pf2_used(void); -// THE SORTED OPERAND'S DECODE KERNEL, pinned for a test: 0 is R4D_MOE_DOWN2's rule (and -// R4D_MOE_DOWN2_G's), -1 the (tile, run) form, n > 0 moe_down_dec_kernel at n column tiles a block. -// Inside the decode form only, which r4d_moe_gemm_pin_prefill(2) pins. Bit-identical, and -// r4d_selftest compares their bytes. -void r4d_moe_gemm_pin_down2(int pin); +// THE RUN-ALIGNED FORM'S COLUMN TILE, pinned for a test: 0 is the launcher's shape rule (the down +// projection's 256-column tile wherever the sorted operand, one part, a w4 weight and every class's +// width allow it), 1 never that tile, 2 that tile -- refused where the shape cannot take it or the +// launch is not the aligned form. Bit-identical, and r4d_selftest compares their bytes. +void r4d_moe_gemm_pin_down_tile(int pin); +// THE GATHER'S FORM, pinned for a test: 0 is the launcher's rule, 1 a column a thread +// (gather_kernel), 2 a wave a token and sixteen bytes a lane (gather_wide_kernel), refused on a +// shape it cannot take. Bit-identical, and r4d_selftest compares their bytes. +void r4d_moe_gather_pin_form(int form); +// THE SORTED OPERAND'S DECODE KERNEL, pinned for a test: 0 is the launcher's rule +// (moe_down_dec_kernel on the sorted operand, one part, at most 512 experts, at a column-tile count +// set by the routed rows -- kMoeDownDecG2Rows), -1 the (tile, run) form, n > 0 moe_down_dec_kernel +// at n column tiles a block, capped at 16 and refused on a shape the kernel does not serve. Inside +// the decode form only, which r4d_moe_gemm_pin_prefill(2) pins. Bit-identical, and r4d_selftest +// compares their bytes. +void r4d_moe_gemm_pin_down_dec(int pin); // THE GATED RESIDUAL READ'S PREFILL TILE, pinned for a test: 0 is the launcher's rule, 1 the decode // tiles' kernels at 128 rows (hc_down_tiled, then hc_up_tiled into `ubuf` and hc_mix), 2 the wide // kernels (which then refuse a shape they are not built for), 3 the PF2 kernels the rule takes from diff --git a/libr4d/r4d_model_bf16.hip b/libr4d/r4d_model_bf16.hip index 3e361f2e..61f0b17e 100644 --- a/libr4d/r4d_model_bf16.hip +++ b/libr4d/r4d_model_bf16.hip @@ -774,11 +774,12 @@ extern "C" int r4d_rope_bf16(long qkv, long pos, int M, int head_dim, int n_rope row_pitch, 0, nullptr, 0, 0, stream); } -// The multi-component form: `mode` 2 mrope, 3 imrope, 4 axial, `sec[0..n_sec)` the sections and -// `plane` the component pitch of `pos`; plane 0 is the one-component rotary above. -extern "C" int r4d_rope_mc_bf16(long qkv, long pos, int M, int head_dim, int n_rope_heads, - int rot, int gptj, float theta, float rscale, int row_pitch, - int mode, const int* sec, int n_sec, int plane, long stream) { +// THE ONE LAUNCH OF rope_kernel. `yarn` with factor 0 is the plain rotary: the kernel's YaRN +// blend returns the frequency it was given and the magnitude is not applied, so the C entry points +// below and the op without YaRN run the same arithmetic with the same arguments. +static int rope_launch(long qkv, long pos, int M, int head_dim, int n_rope_heads, int rot, + int gptj, float theta, float rscale, int row_pitch, int mode, + const int* sec, int n_sec, int plane, RadYarn yarn, long stream) { if (!qkv || !pos) return -1; if (M <= 0 || head_dim <= 0 || n_rope_heads <= 0) return -2; if (rot <= 0 || rot > head_dim || (rot & 1)) return -3; @@ -790,10 +791,19 @@ extern "C" int r4d_rope_mc_bf16(long qkv, long pos, int M, int head_dim, int n_r hipLaunchKernelGGL(rope_kernel, dim3((uint32_t)M, gy), dim3(kThreads), 0, (hipStream_t)stream, (uint16_t*)qkv, (const int32_t*)pos, (uint32_t)head_dim, (uint32_t)n_rope_heads, (uint32_t)rot, gptj, theta, rscale, - (uint32_t)row_pitch, mode, s0, s1, s2, n_sec, (uint32_t)plane, RadYarn{}); + (uint32_t)row_pitch, mode, s0, s1, s2, n_sec, (uint32_t)plane, yarn); return hipGetLastError() == hipSuccess ? 0 : -4; } +// The multi-component form: `mode` 2 mrope, 3 imrope, 4 axial, `sec[0..n_sec)` the sections and +// `plane` the component pitch of `pos`; plane 0 is the one-component rotary above. +extern "C" int r4d_rope_mc_bf16(long qkv, long pos, int M, int head_dim, int n_rope_heads, + int rot, int gptj, float theta, float rscale, int row_pitch, + int mode, const int* sec, int n_sec, int plane, long stream) { + return rope_launch(qkv, pos, M, head_dim, n_rope_heads, rot, gptj, theta, rscale, row_pitch, + mode, sec, n_sec, plane, RadYarn{}, stream); +} + extern "C" int r4d_kv_store_bf16(long k, long v, long slot, long kv, int M, int head_dim, int kv_heads, int block_size, int n_blocks, int k_ld, int v_ld, long stream) { @@ -1389,22 +1399,14 @@ extern "C" int r4d_rad_rope(const RadArgs* a, RadStream s) { const float rscale = (float)r4d_getf(a, "scale", 1.0); const RadYarn yarn = rad_yarn_args(a); if (!rad_yarn_valid(yarn)) return R4D_NO(RAD_E_INVAL); - if (yarn.factor != 0.0f) { - if (mc == 4 || rscale != 1.0f) return R4D_NO(RAD_E_UNSUPPORTED); - const uint32_t heads = (uint32_t)(n_head + n_head_kv); - const uint32_t gy = (heads * (uint32_t)(rot / 2) + kThreads - 1) / kThreads; - hipLaunchKernelGGL(rope_kernel, dim3((uint32_t)Mp, gy), dim3(kThreads), 0, (hipStream_t)s, - (uint16_t*)r4d_addr(qkv), (const int32_t*)r4d_addr(pos), - (uint32_t)head_dim, heads, (uint32_t)rot, gptj, theta, rscale, - (uint32_t)pitch, mc, sec[0], sec[1], sec[2], n_sec, (uint32_t)plane, yarn); - return hipGetLastError() == hipSuccess ? RAD_OK : RAD_E_DEVICE; - } + /* YaRN over the axial vision rotary, or over a linear position divide, is no model's. */ + if (yarn.factor != 0.0f && (mc == 4 || rscale != 1.0f)) return R4D_NO(RAD_E_UNSUPPORTED); /* The Q heads and the K heads rotate; the V heads that follow them do not, which is the whole * reason this op takes the fused projection rather than two tensors. */ - return r4d_rope_mc_bf16(r4d_addr(qkv), r4d_addr(pos), (int)Mp, (int)head_dim, - (int)(n_head + n_head_kv), (int)rot, gptj, theta, rscale, (int)pitch, - mc, sec, n_sec, plane, (long)(uintptr_t)s) == 0 ? RAD_OK - : RAD_E_DEVICE; + return rope_launch(r4d_addr(qkv), r4d_addr(pos), (int)Mp, (int)head_dim, + (int)(n_head + n_head_kv), (int)rot, gptj, theta, rscale, (int)pitch, + mc, sec, n_sec, plane, yarn, (long)(uintptr_t)s) == 0 ? RAD_OK + : RAD_E_DEVICE; } /* ------------------------------------------------------------------ kv_store */ diff --git a/libr4d/r4d_moe.hip b/libr4d/r4d_moe.hip index 8c7f931a..6a55388f 100644 --- a/libr4d/r4d_moe.hip +++ b/libr4d/r4d_moe.hip @@ -561,79 +561,19 @@ __global__ __launch_bounds__(kScatterTile) void scatter_hist_kernel( bcnt[(size_t)b * n_expert + e] = s_h[e]; } -/* One workgroup: totals per expert, the exclusive scan across experts, and the exclusive scan - * across tiles within each expert -- the last one in place, so `bcnt[b][e]` becomes "how many of - * expert e are in tiles before b". One thread an expert, which at n_expert 256 is one wave-group - * and no cross-thread communication at all. */ -__global__ __launch_bounds__(256) void scatter_scan_kernel( - int32_t* __restrict__ bcnt, int32_t* __restrict__ off, int32_t* __restrict__ cnt, - uint32_t n_tile, uint32_t n_expert) { - __shared__ int32_t s_cnt[kMaxExpert + 1]; - const uint32_t tid = threadIdx.x; - for (uint32_t e = tid; e < n_expert; e += blockDim.x) { - int32_t run = 0; - for (uint32_t b = 0; b < n_tile; ++b) { - const int32_t v = bcnt[(size_t)b * n_expert + e]; - bcnt[(size_t)b * n_expert + e] = run; - run += v; - } - s_cnt[e] = run; - cnt[e] = run; - } - __syncthreads(); - if (tid == 0) { - int32_t run = 0; - for (uint32_t e = 0; e < n_expert; ++e) { off[e] = run; run += s_cnt[e]; } - off[n_expert] = run; - } -} - -__global__ __launch_bounds__(kScatterTile) void scatter_place_kernel( - const int32_t* __restrict__ ids, const int32_t* __restrict__ bcnt, - const int32_t* __restrict__ off, int32_t* __restrict__ sorted, - uint32_t T, uint32_t n_expert, int32_t base, Protect prot) { - __shared__ int32_t s_id[kScatterTile]; - const uint32_t tid = threadIdx.x, b = blockIdx.x; - const uint32_t i = b * kScatterTile + tid; - /* THE PLACE IN THE SORT throughout -- the local expert index, with any protected experts moved - * last: s_id, the range test and the rank scan all work in the indices the offsets and the - * weight tables are built from. */ - const int32_t l = (i < T) ? ids[i] - base : -1; - const int32_t e = (l >= 0 && l < (int32_t)n_expert) ? sort_place(l, n_expert, prot) : -1; - s_id[tid] = e; - __syncthreads(); - if (i >= T || e < 0) return; - /* Rank within the tile: how many earlier slots of this tile chose the same expert. A serial - * scan of at most 256 LDS words a thread, which is the price of stability. */ - int32_t rank = 0; - for (uint32_t j = 0; j < tid; ++j) rank += (s_id[j] == e); - sorted[off[e] + bcnt[(size_t)b * n_expert + e] + rank] = (int32_t)i; -} - -__global__ void scatter_tail_kernel(const int32_t* __restrict__ off, int32_t* __restrict__ sorted, - uint32_t T, uint32_t n_expert) { - const uint32_t i = blockIdx.x * blockDim.x + threadIdx.x; - const uint32_t live = (uint32_t)off[n_expert]; - if (i >= live && i < T) sorted[i] = -1; -} - -/* ---- PF2: THE GENERAL SCATTER'S SCAN AND TAIL, AT A PREFILL CHUNK ---------------------------------- +/* ONE WORKGROUP, ONE THREAD AN EXPERT: the exclusive scan across tiles within each expert, in + * place -- so `bcnt[b][e]` becomes "how many of expert e are in tiles before b" -- each expert's + * total, and the exclusive scan of the totals across experts. * - * scatter_scan_kernel gives each thread an expert -- two at 512 -- and walks that expert's column of - * `bcnt` one tile at a time: load, add, store, and the next load only after. At a 2048 chunk and top - * 10 that is 80 tiles, two experts a thread, 160 dependent round trips on one workgroup, and then - * one thread prefixes the 512 totals alone. Here a thread holds one expert, its column comes in - * kScanBatch loads at a time, all issued before any is used, and the prefix across experts is the - * small sort's: a DPP scan in each wave and one exchange of the wave totals. Integers, and every sum - * has one answer whatever order it is formed in: the same bcnt, offsets and counts. - * - * THE TAIL RIDES IN THE PLACEMENT. scatter_tail_kernel is a launch of its own that writes -1 to the - * sorted rows past the live count; those rows are never a placement target (every placed slot lands - * below `off[n_expert]`), so the placement's own threads can write them, each its own index, with no - * thread able to race another for a row. One dependent launch a layer fewer; the same bytes. */ + * A thread's column of `bcnt` comes in kScanBatch loads at a time, all issued before any is used: + * at a 2048-token chunk and top 10 that is 80 tiles, and walked a load at a time -- load, add, + * store, then the next load -- it is 80 dependent round trips on the one workgroup a layer's sort + * waits for. The prefix across experts is the small sort's: a DPP scan in each wave and one + * exchange of the wave totals. Integers, and every sum has one answer whatever order it is formed + * in. */ constexpr uint32_t kScanBatch = 16; -__global__ __launch_bounds__(kMaxExpert) void scatter_scan_par_kernel( +__global__ __launch_bounds__(kMaxExpert) void scatter_scan_kernel( int32_t* __restrict__ bcnt, int32_t* __restrict__ off, int32_t* __restrict__ cnt, uint32_t n_tile, uint32_t n_expert) { constexpr uint32_t NW = kMaxExpert / kWave; @@ -673,15 +613,21 @@ __global__ __launch_bounds__(kMaxExpert) void scatter_scan_par_kernel( if (e == 0) off[n_expert] = tot; } -/* scatter_place_kernel, and the tail scatter_tail_kernel writes. The placement is that kernel's, - * line for line. */ -__global__ __launch_bounds__(kScatterTile) void scatter_place_tail_kernel( +/* THE PLACEMENT, AND THE TAIL. Every routed slot of the tile goes to its expert's offset, plus the + * expert's count in the tiles before this one, plus its rank in this tile. The sorted rows past the + * live count are -1; they are never a placement target (every placed slot lands below + * `off[n_expert]`), so the placement's own threads write them, each its own index, and no thread + * can race another for a row. */ +__global__ __launch_bounds__(kScatterTile) void scatter_place_kernel( const int32_t* __restrict__ ids, const int32_t* __restrict__ bcnt, const int32_t* __restrict__ off, int32_t* __restrict__ sorted, uint32_t T, uint32_t n_expert, int32_t base, Protect prot) { __shared__ int32_t s_id[kScatterTile]; const uint32_t tid = threadIdx.x, b = blockIdx.x; const uint32_t i = b * kScatterTile + tid; + /* THE PLACE IN THE SORT throughout -- the local expert index, with any protected experts moved + * last: s_id, the range test and the rank scan all work in the indices the offsets and the + * weight tables are built from. */ const int32_t l = (i < T) ? ids[i] - base : -1; const int32_t e = (l >= 0 && l < (int32_t)n_expert) ? sort_place(l, n_expert, prot) : -1; s_id[tid] = e; @@ -689,12 +635,13 @@ __global__ __launch_bounds__(kScatterTile) void scatter_place_tail_kernel( if (i >= live && i < T) sorted[i] = -1; __syncthreads(); if (i >= T || e < 0) return; + /* Rank within the tile: how many earlier slots of this tile chose the same expert. A serial + * scan of at most 256 LDS words a thread, which is the price of stability. */ int32_t rank = 0; for (uint32_t j = 0; j < tid; ++j) rank += (s_id[j] == e); sorted[off[e] + bcnt[(size_t)b * n_expert + e] + rank] = (int32_t)i; } - /* ================================================================== the 4-bit expert weight * * SIXTEEN PACKED BYTES TO THE THIRTY-TWO E4M3 BYTES OF A Stage8, and nothing downstream of it @@ -1007,20 +954,17 @@ static_assert(sizeof(MoeTab) == 16, "the code table is four words"); * its own activation scale. Same products, same sums, same order: bit-identical to FK 128. * * UW reads the weight and its scales from a uniform global base -- see the UW note at w4_load. - * The decode form always takes it. The prefill forms keep the plain loads: the waits UW removes - * were measured on the decode grid, and the prefill tiles' timing under UW is unmeasured, so - * their instructions stay as they were -- except under EW (R4D_MOE_PF2 64), below, which needs - * the uniform base for its early loads and was measured with it. + * The decode form and the run-aligned prefill form always take it; the aligned form needs it for + * the first pass of weight it issues at the map read (see THE ALIGNED FORM'S FIRST PASS, below + * the map read), and was measured with it. The uniform prefill grid keeps the plain loads: its + * timing under UW was never measured, so its instructions stay as they were. * * RS finds an ES block's run in one round trip -- see the RS note at the run search. It covers * up to kMaxExpert experts; a wider expert count takes the chunked search, which numbers the runs - * the same way. - * - * EW issues an AL block's first pass of weight as soon as its map entry has named the expert -- see - * the EW note below the map read. */ + * the same way. */ template + bool RS = false> __global__ __launch_bounds__(WGM * WGN * kWave) void moe_gemm_q_kernel( const uint8_t* __restrict__ Aq, const float* __restrict__ As, const MoeCls c_even, const MoeCls c_odd, const MoeCls c_2, const MoeCls c_3, @@ -1036,7 +980,7 @@ __global__ __launch_bounds__(WGM * WGN * kWave) void moe_gemm_q_kernel( constexpr uint32_t WM = MT / WGM, WN = NTL / WGN; static_assert(MT % WGM == 0 && NTL % WGN == 0, "the wave grid must divide the tile"); /* The block-fp8 weight has one scale a 128x128 tile; a w4 weight one a ROW, so its block may - * span two of the fp8 grid's scale rows (the 256-column down tile, R4D_MOE_PF2). */ + * span two of the fp8 grid's scale rows (the aligned form's 256-column down tile). */ static_assert(W4 != 0 || kBlock % FN == 0, "a block's N rows must sit in one 128-row weight scale row"); /* Scale groups a 128-wide block: one, or at W4 2, 3 and 4 the 64- or 32-column ones. */ @@ -1067,31 +1011,44 @@ __global__ __launch_bounds__(WGM * WGN * kWave) void moe_gemm_q_kernel( /* UNDER AL THE BLOCK IS NAMED BY THE MAP, not by arithmetic on blockIdx: entry i is * (expert, first row) and the block serves that run and no other. The grid is the upper bound * `n_expert + T/FM`, so the blocks past the real count exit here. */ + const uint32_t n0 = blockIdx.y * FN; uint32_t r0, rN, e_al = 0; - [[maybe_unused]] uint32_t al_end = 0; /* EW: off[e_al + 1], the run's end */ - bool tail = false; + [[maybe_unused]] uint32_t al_end = 0; /* AL: the run's end, off[e_al + 1] */ if constexpr (AL) { if (blockIdx.x >= map[0]) return; const uint32_t* ent = map + 1 + 2 * blockIdx.x; e_al = ent[0]; r0 = ent[1]; - tail = (e_al == kMapTail); - const uint32_t lim = tail ? T : (uint32_t)off[e_al + 1]; - al_end = lim; - rN = (r0 + FM < lim) ? (r0 + FM) : lim; + /* A TAIL BLOCK OWNS NO RUN: it exists to zero the rows past the live count, which no + * expert owns, and it does that here and leaves -- before the first pass's weight below is + * issued for an expert it does not have. Block-uniform (the map entry is the block's), so + * the early loads below are on one path with no join behind them. */ + if (e_al == kMapTail) { + const uint32_t live = (uint32_t)off[n_expert]; + const uint32_t rT = (r0 + FM < T) ? (r0 + FM) : T; + for (uint32_t r = (r0 > live ? r0 : live); r < rT; ++r) + for (uint32_t c = tid; c < FN && n0 + c < N; c += NTHR) + Y[(size_t)r * y_ld + n0 + c] = 0; + return; + } + /* The run's end, clamped to the output's rows: an operand may name fewer rows than its + * buffer, never more. */ + const uint32_t oe = (uint32_t)off[e_al + 1]; + al_end = oe < T ? oe : T; + rN = (r0 + FM < al_end) ? (r0 + FM) : al_end; } else { r0 = (ES ? (blockIdx.x / FM) : blockIdx.x) * FM; if (r0 >= T) return; rN = (r0 + FM < T) ? (r0 + FM) : T; } - const uint32_t n0 = blockIdx.y * FN; - /* EW: THE FIRST PASS'S WEIGHT, IN FLIGHT AS SOON AS THE MAP HAS NAMED THE EXPERT. Under AL the - * map entry names the expert, and nothing the weight's address needs comes later -- not the - * tokens, not the run's rows, not the live count. In the run loop, the weight's load waits - * behind the token loads, the barrier that publishes them, the live count, the run's end read - * again and the table entry: four round trips in front of the one that carries the bytes, on a - * block whose whole K is one or two passes at the down projection. + /* THE ALIGNED FORM'S FIRST PASS OF WEIGHT, IN FLIGHT AS SOON AS THE MAP HAS NAMED THE EXPERT + * (the `ew_` registers: the early weight). Under AL the map entry names the expert, and + * nothing the weight's address needs comes later -- not the tokens, not the run's rows, not + * the live count. Loaded in the run loop, the weight would wait behind the token loads, the + * barrier that publishes them, the live count, the run's end read again and the table entry: + * four round trips in front of the one that carries the bytes, on a block whose whole K is one + * or two passes at the down projection. * * So the block's loads go out in the order their waits want them. First everything the token * barrier needs -- the block's sorted rows and the live count -- then the table entries @@ -1105,23 +1062,23 @@ __global__ __launch_bounds__(WGM * WGN * kWave) void moe_gemm_q_kernel( * * EVERY EARLY LOAD IS UNCONDITIONAL. A load under an exec mask -- `if (live) load` -- may not be * issued at all, and the waitcnt pass, not knowing how many went out, then waits for every one - * of them where it needs the first. So a dead row (past the class's width, or the whole of a - * tail block) reads the expert's row 0 instead, a dead token slot reads a real one, and the - * zero is selected where the value is used -- the first issue() for the weight, the token's - * store for the slot. */ + * of them where it needs the first. So a dead row (past the class's width) reads the expert's + * row 0 instead, a dead token slot reads a real one, and the zero is selected where the value + * is used -- the first issue() for the weight, the token's store for the slot. A tail block + * has already left: it has no expert to load for. */ constexpr uint32_t kEwTPR = FK / 32, kEwRPP = NTHR / kEwTPR; constexpr uint32_t kEwBP = (FN + kEwRPP - 1) / kEwRPP; - [[maybe_unused]] uint4 ew_b4[EW && W4 ? kEwBP : 1]; - [[maybe_unused]] uint32_t ew_sp[EW && W4 == 4 ? kEwBP : 1]; - [[maybe_unused]] Stage8 ew_b8[EW && !W4 ? kEwBP : 1]; - [[maybe_unused]] bool ew_ok[EW ? kEwBP : 1]; + [[maybe_unused]] uint4 ew_b4[AL && W4 ? kEwBP : 1]; + [[maybe_unused]] uint32_t ew_sp[AL && W4 == 4 ? kEwBP : 1]; + [[maybe_unused]] Stage8 ew_b8[AL && !W4 ? kEwBP : 1]; + [[maybe_unused]] bool ew_ok[AL ? kEwBP : 1]; [[maybe_unused]] const uint8_t* ew_wp = nullptr; [[maybe_unused]] const uint16_t* ew_wsp = nullptr; [[maybe_unused]] int32_t ew_f = -1; [[maybe_unused]] uint32_t ew_live = 0; - if constexpr (EW) { - static_assert(AL && !ES && !RS && UW && FM <= NTHR, - "EW is the run-aligned form's, on UW's uniform weight base"); + if constexpr (AL) { + static_assert(!ES && !RS && UW && FM <= NTHR, + "the aligned form issues its first pass early, on UW's uniform weight base"); { const uint32_t i = r0 + tid % FM; /* a real row whatever the thread */ ew_f = sorted[i < T ? i : T - 1u]; @@ -1129,13 +1086,12 @@ __global__ __launch_bounds__(WGM * WGN * kWave) void moe_gemm_q_kernel( ew_live = (uint32_t)off[n_expert]; /* Nothing below is scheduled in front of these two: the counter retires in order. */ __builtin_amdgcn_sched_barrier(0); - const uint32_t ee = tail ? 0u : e_al; /* a tail block reads a real entry, unused */ - const uint32_t cls = ee & ((1u << cshift) - 1u); + const uint32_t cls = e_al & ((1u << cshift) - 1u); const MoeCls& C = cls == 0u ? c_even : cls == 1u ? c_odd : cls == 2u ? c_2 : c_3; const uint32_t fy = N / parts, part = n0 / fy, j0 = n0 - part * fy; const uint32_t fc = C.n / parts, wn0 = part * fc + j0; - ew_wp = uw_ptr(wtab_at(C.w, C.w_s0, ee >> cshift)); - ew_wsp = uw_ptr((const uint16_t*)wtab_at(C.s, C.s_s0, ee >> cshift)); + ew_wp = uw_ptr(wtab_at(C.w, C.w_s0, e_al >> cshift)); + ew_wsp = uw_ptr((const uint16_t*)wtab_at(C.s, C.s_s0, e_al >> cshift)); const uint32_t sn = tid / kEwTPR, sk = (tid % kEwTPR) * 32u; /* Where a staging pass covers the tile's rows exactly, every thread's every row exists: * no guard, so no exec mask around a load (a thread's `sn` is below kEwRPP). */ @@ -1143,7 +1099,7 @@ __global__ __launch_bounds__(WGM * WGN * kWave) void moe_gemm_q_kernel( for (uint32_t p = 0; p < kEwBP; ++p) { const uint32_t r = sn + p * kEwRPP; if constexpr (FN % kEwRPP != 0) { if (r >= FN) continue; } - ew_ok[p] = !tail && (j0 + r) < fc; + ew_ok[p] = (j0 + r) < fc; const uint8_t* wr = ew_wp + (size_t)(ew_ok[p] ? wn0 + r : 0u) * C.w_ld; if constexpr (W4) { ew_b4[p] = w4_load_w(wr + (sk >> 1), true); @@ -1191,9 +1147,10 @@ __global__ __launch_bounds__(WGM * WGN * kWave) void moe_gemm_q_kernel( if (lane == kWave - 1) s_rc[wave] = rs_inc; /* Both of RS's barriers order LDS alone -- the tokens, the counts, the selection. */ lds_barrier(); - } else if constexpr (EW) { - /* The tokens and the live count were loaded with the weight, ahead of it; see EW. A row - * past the block's is a dropped slot, as the loop of the plain form writes it. */ + } else if constexpr (AL) { + /* The tokens and the live count were loaded with the weight, ahead of it; see THE ALIGNED + * FORM'S FIRST PASS. A row past the block's is a dropped slot, as the loop of the plain + * form writes it. */ if (tid < FM) { const int32_t f = r0 + tid < rN ? ew_f : -1; s_tok[tid] = f < 0 ? -1 : (a_sorted ? (int32_t)(r0 + tid) : f / (int32_t)top_k); @@ -1218,15 +1175,14 @@ __global__ __launch_bounds__(WGM * WGN * kWave) void moe_gemm_q_kernel( } /* Rows past the live count are dropped slots: zeroed here, and then the run loop below has * nothing left to do for them. No run owns them, so under ES exactly one of the tile's blocks - * must -- every block writing the same zeros would be a benign race and is still a race. */ - if (!ES || jrun == 0) - for (uint32_t r = (r0 > live ? r0 : live); r < rN; ++r) - for (uint32_t c = tid; c < FN && n0 + c < N; c += NTHR) - Y[(size_t)r * y_ld + n0 + c] = 0; - - /* A TAIL BLOCK HAS NOW DONE ALL ITS WORK: it exists to zero rows no expert owns and holds no - * run for the loop below. Block-uniform, so no barrier is skipped by some threads only. */ - if constexpr (AL) { if (tail) return; } + * must -- every block writing the same zeros would be a benign race and is still a race. Under + * AL the tail blocks zeroed them at the map read, and every other block's rows are its run's. */ + if constexpr (!AL) { + if (!ES || jrun == 0) + for (uint32_t r = (r0 > live ? r0 : live); r < rN; ++r) + for (uint32_t c = tid; c < FN && n0 + c < N; c += NTHR) + Y[(size_t)r * y_ld + n0 + c] = 0; + } /* THE EXPERT OF THE BLOCK'S FIRST RUN. The row-tile grid finds the expert of its first row by * binary search over the offsets and walks the runs from there, which the loop below does. @@ -1247,7 +1203,7 @@ __global__ __launch_bounds__(WGM * WGN * kWave) void moe_gemm_q_kernel( uint32_t e = 0; [[maybe_unused]] uint32_t rs_first = 0, rs_end = 0; if constexpr (AL) { - e = tail ? 0u : e_al; /* the map already named it; a tail block owns no expert */ + e = e_al; /* the map already named it */ } else if constexpr (RS) { /* RS: THE SAME SEARCH, IN ONE ROUND TRIP AND TWO BARRIERS. The pass above walks the * experts a chunk of NTHR at a time, each chunk a broadcast load to skip it or two @@ -1358,13 +1314,13 @@ __global__ __launch_bounds__(WGM * WGN * kWave) void moe_gemm_q_kernel( * never be walked past the last expert -- `off[e + 1]` is read only for e < n_expert, and * off[n_expert] is the live row count, which is the bound the loop already respects. * Under RS the search handed over the run's end, and its start meets it: nothing to walk. */ - if constexpr (!RS && !EW) { + if constexpr (!RS && !AL) { while (e < n_expert && (uint32_t)off[e + 1] <= rs) ++e; if (e >= n_expert) break; } - /* EW: the map's run is e_al's from r0 on, so there is nothing to walk, and its end is the + /* AL: the map's run is e_al's from r0 on, so there is nothing to walk, and its end is the * one the map read loaded. */ - const uint32_t ehi = RS ? rs_end : EW ? al_end : (uint32_t)off[e + 1]; + const uint32_t ehi = RS ? rs_end : AL ? al_end : (uint32_t)off[e + 1]; const uint32_t re = rN < live ? (rN < ehi ? rN : ehi) : (live < ehi ? live : ehi); if (re <= rs) break; /* Not this block's run: walk past it. Block-uniform, so no barrier is skipped by some @@ -1397,8 +1353,8 @@ __global__ __launch_bounds__(WGM * WGN * kWave) void moe_gemm_q_kernel( * readfirstlanes wait on one round trip and not on two. */ const uint8_t* Wp0; const uint16_t* Wsp0; - if constexpr (EW) { - Wp0 = ew_wp; /* EW: loaded at the map read; AL's run is e_al's */ + if constexpr (AL) { + Wp0 = ew_wp; /* loaded at the map read; AL's run is e_al's */ Wsp0 = ew_wsp; } else { Wp0 = wtab_at(C.w, C.w_s0, e >> cshift); @@ -1536,8 +1492,9 @@ __global__ __launch_bounds__(WGM * WGN * kWave) void moe_gemm_q_kernel( const bool ok = tok >= 0 && (r0 + r) >= rs && (r0 + r) < re; apre[p] = stage8_load(Aq + (size_t)(ok ? tok : 0) * a_ld + cc + sk, ok); } - if constexpr (EW) { - static_assert(kEwBP == kBP && kEwRPP == kRPP, "EW stages the pass issue() would"); + if constexpr (AL) { + static_assert(kEwBP == kBP && kEwRPP == kRPP, + "the early weight stages the pass issue() would"); if (cc == 0) { /* the first pass's weight is already in flight */ #pragma unroll for (uint32_t p = 0; p < kBP; ++p) { @@ -1830,10 +1787,11 @@ static_assert(MoeQArgs::count <= RAD_MAX_KPARAMS, * flight while this one's last block is computed. The chain the (tile, run) form pays once a * block -- search, table, first load -- is paid once a G tiles here. * - * G is the launcher's: one at a step of a few sequences, where every run is needed to fill the part, - * and up to four at sixteen, where there are runs enough. The loads are UW's throughout -- uniform - * global bases and LDS-only barriers -- because a barrier that waits for the prefetch would make the - * pipeline a chain again. + * G is the launcher's (kMoeDownDecG2Rows, kMoeDownDecG4Rows): one at a step of a few sequences, + * where every run is needed to fill the part, and up to four at sixteen, where there are runs + * enough. At most kMoeDownDecMaxG, so the block's column tiles are bits of one word. The loads are + * UW's throughout -- uniform global bases and LDS-only barriers -- because a barrier that waits for + * the prefetch would make the pipeline a chain again. * * BIT-IDENTICAL to the (tile, run) form: an output element is one wave's WMMA chain over the same K * in the same order, folded by the same expression under the same scales; only which block, and @@ -1886,12 +1844,18 @@ __global__ __launch_bounds__(4 * kWave) void moe_down_dec_kernel( /* THE TILE: the j-th 16-row tile of a run, in expert order and then row order. Every thread * holds NPT consecutive experts' offsets and the next one, from one wave of loads; the tiles * are numbered a thread's own in order, then over the wave (DPP) and the waves before it. - * Both barriers order LDS. */ + * Both barriers order LDS. + * + * EVERY OFFSET IS CLAMPED TO T, so every run ends at the output's last row at the latest, as + * the (tile, run) form's runs do: an operand may name fewer rows than its buffer, never more. + * A run that would start past T is then empty and has no tile, so the tile count also stays + * inside the grid's bound. */ uint32_t ov[NPT + 1]; #pragma unroll for (uint32_t k = 0; k <= NPT; ++k) { const uint32_t x = tid * NPT + k; - ov[k] = x <= n_expert ? (uint32_t)off[x] : 0u; + const uint32_t o = x <= n_expert ? (uint32_t)off[x] : 0u; + ov[k] = o < T ? o : T; } uint32_t nt[NPT], cnt = 0; #pragma unroll @@ -2529,7 +2493,7 @@ __global__ __launch_bounds__(NTHR) void gather_kernel( } } -/* ---- PF2: THE PREFILL GATHER, A WAVE A TOKEN AND EIGHT COLUMNS A LANE ---------------------------- +/* ---- THE PREFILL GATHER, A WAVE A TOKEN AND EIGHT COLUMNS A LANE --------------------------------- * * At a prefill chunk this op is a read of `ye` -- one row a routed slot, 105 MB a layer at a 2048 * chunk of Qwen3.8-Flash-Next on a rank of four -- and gather_kernel moves it TWO BYTES A LANE: a @@ -2541,12 +2505,7 @@ __global__ __launch_bounds__(NTHR) void gather_kernel( * (uniform: scalar loads), then every slot's sixteen bytes are issued before any is used, then * summed. THE ARITHMETIC IS gather_kernel's: per column, ascending slot order, a dropped slot skipped, * `acc += w * x` in f32, and the shared arm's two narrows exactly as there -- the same bits. - * - * REV takes the column groups last first. The down projection writes `ye` one 128-column tile at a - * time, every routed row of a tile before the next (the tile is its grid's slow axis), so when it - * ends, the tiles it wrote last are the ones still in the infinity cache. Taken last first, this - * kernel's first column groups are read from there rather than from DRAM. Which block reads which - * group moves; no sum does. */ + */ constexpr uint32_t kGatherWideSlots = 16; /* top_k at most this: the slots live in registers */ constexpr uint32_t kGatherWideCols = 8; /* columns a lane, one sixteen-byte load */ static_assert(kGatherWideSlots <= kWave, "slot j is read off lane j"); @@ -2557,12 +2516,11 @@ __global__ __launch_bounds__(NW * kWave) void gather_wide_kernel( const int32_t* __restrict__ inv, uint16_t* __restrict__ y, uint32_t M, uint32_t n, uint32_t top_k, uint32_t ye_ld, uint32_t ew_ld, uint32_t y_ld, const uint16_t* __restrict__ sh, const uint16_t* __restrict__ sg, - uint32_t sh_ld, uint32_t sg_ld, int sig, int rev) { + uint32_t sh_ld, uint32_t sg_ld, int sig) { const uint32_t lane = threadIdx.x % kWave; const uint32_t wave = (uint32_t)__builtin_amdgcn_readfirstlane((int)(threadIdx.x / kWave)); const uint32_t t = blockIdx.x * NW + wave; - const uint32_t g = rev ? gridDim.y - 1u - blockIdx.y : blockIdx.y; - const uint32_t c = (g * kWave + lane) * kGatherWideCols; + const uint32_t c = (blockIdx.y * kWave + lane) * kGatherWideCols; /* No barrier anywhere below, so a wave past the last token simply leaves. */ if (t >= M) return; @@ -2664,48 +2622,12 @@ __global__ __launch_bounds__(NTHR) void scale_rows_kernel( } /* namespace */ -/* R4D_MOE_PF2: the prefill MoE forms -- the shapes a chunk of thousands of routed rows a layer - * takes and a decode step never reaches. Unset or 0: as upstream. 1: every part. Any other value is - * a mask of the parts, each bit-identical to the form it replaces: - * - * 2 moe_gather at a prefill chunk: a wave a token and eight columns a lane (gather_wide_kernel) - * 4 ...its column groups last first, so the down projection's last tiles come from the cache - * 8 moe_scatter's general form: its scan in parallel and its tail in the placement - * 16 the run map's prefix in parallel (moe_blockmap_par_kernel) - * 32 the down projection's prefill tile 256 columns wide over sixteen waves - * 64 the run-aligned form's first pass of weight issued at the map read (EW, both projections) - * - * Read once, so a recorded pass and a live one take the same kernels. A test pin over it: below - * zero the switch, anything else the value the switch would hold. */ -static constexpr uint32_t kPf2Gather = 2, kPf2GatherRev = 4, kPf2Scan = 8, kPf2Map = 16, - kPf2DnWide = 32, kPf2Ew = 64; -static constexpr uint32_t kPf2All = kPf2Gather | kPf2GatherRev | kPf2Scan | kPf2Map | kPf2DnWide | - kPf2Ew; -static uint32_t moe_pf2_mask(long long x) { - if (x <= 0) return 0u; - return x == 1 ? kPf2All : (uint32_t)x & kPf2All; -} -static int g_moe_pf2_pin = -1; -static uint32_t moe_pf2() { - static const uint32_t v = [] { - const char* e = std::getenv("R4D_MOE_PF2"); - return moe_pf2_mask(e ? std::atoll(e) : 0); - }(); - return g_moe_pf2_pin >= 0 ? moe_pf2_mask(g_moe_pf2_pin) : v; -} -/* WHICH PARTS A PINNED LAUNCH TOOK. A part whose kernel did not run -- a shape it refuses, a - * condition that never held -- would match the default trivially, so the test asks. Recorded only - * under a pin: the pin is a test's, and a serving process never writes either. */ -static uint32_t g_moe_pf2_used = 0; -static inline void moe_pf2_note(uint32_t part) { - if (g_moe_pf2_pin >= 0) g_moe_pf2_used |= part; -} -extern "C" void r4d_moe_pf2_pin(int pin) { g_moe_pf2_pin = pin; } -extern "C" uint32_t r4d_moe_pf2_used(void) { - const uint32_t u = g_moe_pf2_used; - g_moe_pf2_used = 0; - return u; -} +/* THE GATHER'S FORM, pinned for a test: 0 is the launcher's rule, 1 gather_kernel (a column a + * thread), 2 gather_wide_kernel (a wave a token). The rule takes 2 past the self-scan bound wherever + * the shape allows it; see r4d_rad_moe_gather. A pinned form the shape cannot take is refused. Both + * are bit-identical, and r4d_selftest compares their bytes. */ +static int g_moe_gather_pin = 0; +extern "C" void r4d_moe_gather_pin_form(int form) { g_moe_gather_pin = form; } /* ============================================================== the plugin ABI entry points */ @@ -2924,28 +2846,13 @@ extern "C" int r4d_rad_moe_scatter(const RadArgs* a, RadStream s) { (unsigned)(n_expert * sizeof(int32_t)), (hipStream_t)s, idp, bcnt, (uint32_t)T, (uint32_t)n_expert, (int32_t)base, prot); R4D_MOE_CHK(); - /* R4D_MOE_PF2 (8): the scan in parallel, the tail in the placement -- the same integers. */ - if (moe_pf2() & kPf2Scan) { - moe_pf2_note(kPf2Scan); - hipLaunchKernelGGL(scatter_scan_par_kernel, dim3(1), dim3(kMaxExpert), 0, (hipStream_t)s, - bcnt, op, cp, (uint32_t)n_tile, (uint32_t)n_expert); - R4D_MOE_CHK(); - hipLaunchKernelGGL(scatter_place_tail_kernel, dim3((unsigned)n_tile), dim3(kScatterTile), 0, - (hipStream_t)s, idp, bcnt, op, sp, (uint32_t)T, (uint32_t)n_expert, - (int32_t)base, prot); - R4D_MOE_CHK(); - return RAD_OK; - } - hipLaunchKernelGGL(scatter_scan_kernel, dim3(1), dim3(256), 0, (hipStream_t)s, + hipLaunchKernelGGL(scatter_scan_kernel, dim3(1), dim3(kMaxExpert), 0, (hipStream_t)s, bcnt, op, cp, (uint32_t)n_tile, (uint32_t)n_expert); R4D_MOE_CHK(); hipLaunchKernelGGL(scatter_place_kernel, dim3((unsigned)n_tile), dim3(kScatterTile), 0, (hipStream_t)s, idp, bcnt, op, sp, (uint32_t)T, (uint32_t)n_expert, (int32_t)base, prot); R4D_MOE_CHK(); - hipLaunchKernelGGL(scatter_tail_kernel, dim3((unsigned)((T + 255) / 256)), dim3(256), 0, - (hipStream_t)s, op, sp, (uint32_t)T, (uint32_t)n_expert); - R4D_MOE_CHK(); return RAD_OK; } @@ -3082,25 +2989,44 @@ static inline bool r4d_moe_gu_fk256(long long T, bool a_sorted) { if (g_moe_fk_pin == 2) return true; return T <= kMoeGuFk256Rows; } -/* R4D_MOE_DOWN2=1: the decode form of the SORTED-operand projection (down) is moe_down_dec_kernel - * -- a block a 16-row tile of a run, G 64-column tiles a block, see its note. Unset or 0: the - * (tile, run) form, as upstream. R4D_MOE_DOWN2_G=n fixes G (1 to 16); unset, G follows the - * routed-row count: 1 below 200 rows, 2 below 480, 4 from there, which keeps a few thousand working - * blocks from one sequence to sixteen. Bit-identical; read once, so a recorded pass and a live - * one take the same kernel. A test pin over both: 0 the switches, -1 off, n > 0 on at G = n. */ -static int g_moe_dn2_pin = 0; -static long long r4d_moe_down2_g(long long T) { - if (g_moe_dn2_pin) return g_moe_dn2_pin > 0 ? g_moe_dn2_pin : 0; - auto env = [](const char* name) { - const char* e = std::getenv(name); - const long long x = e ? std::atoll(e) : 0; - return x > 0 ? x : 0LL; - }; - static const long long on = env("R4D_MOE_DOWN2"); - static const long long gf = env("R4D_MOE_DOWN2_G"); - if (on <= 0) return 0; - if (gf > 0) return gf < 16 ? gf : 16; - return T >= 480 ? 4 : T >= 200 ? 2 : 1; +/* THE SORTED-OPERAND PROJECTION'S DECODE KERNEL, and its column tiles a block. The decode form of + * down -- the sorted operand, one part, at most kMaxExpert experts -- is moe_down_dec_kernel: a + * block a 16-row tile of a run and G 64-column tiles a block, see its note. Any other shape takes + * the (tile, run) form. G follows the routed-row count: one below kMoeDownDecG2Rows, two below + * kMoeDownDecG4Rows, four from there, which keeps a few thousand working blocks from one sequence + * to sixteen. Bit-identical to the (tile, run) form at every G. Constants, so a recorded pass and + * a live one take the same kernel. + * + * MEASURED at TP4 on four R9700 (gfx1201), MTP depth 3, end to end: ms a decode step with this + * rule against the (tile, run) form at the same concurrency. + * + * concurrent sequences 4 8 16 + * ms a step -0.9% -3.1% -7.8% + * + * At TP2 on two R9700, Qwen3.8-Flash-Next, MTP depth 3, distinct prompts a sequence, the same + * measure (one window each; boot to boot the same arm moves about 0.5%): + * + * concurrent sequences 1 4 6 8 + * (tile, run) form 14.78 25.64 - 36.15 ms a step + * this rule 14.64 25.53 - 36.26 + * G = 1 always - - 31.58 36.37 + * G = 2 always - 25.59 31.17 36.15 + * G = 4 always - - 31.66 36.48 + * + * Nothing at TP2 moves by more than that noise, so the TP4 thresholds stand: a rank holding twice + * each expert's width already keeps its blocks busy at the (tile, run) form's grid. */ +constexpr long long kMoeDownDecG2Rows = 200; +constexpr long long kMoeDownDecG4Rows = 480; +/* G's bound: the kernel keeps its column tiles in one 32-bit mask. */ +constexpr long long kMoeDownDecMaxG = 16; +/* THE SORTED OPERAND'S DECODE KERNEL, pinned for a test: 0 is the rule above, -1 the (tile, run) + * form, n > 0 moe_down_dec_kernel at n column tiles a block (capped at kMoeDownDecMaxG). A pin to + * the kernel on a shape it does not serve is refused. */ +static int g_moe_down_dec_pin = 0; +static inline long long r4d_moe_down_dec_g(long long T) { + if (g_moe_down_dec_pin < 0) return 0; + if (g_moe_down_dec_pin > 0) return g_moe_down_dec_pin; + return T >= kMoeDownDecG4Rows ? 4 : T >= kMoeDownDecG2Rows ? 2 : 1; } /* THE MAP'S UPPER BOUND, stated once because the scratch hook and the launcher must agree about * it exactly: a hook that answers for a different shape than the launch uses is how a kernel ends @@ -3117,13 +3043,21 @@ static inline long long r4d_blockmap_bytes(long long cap) { /* ONE WORKGROUP, because the whole of it is a scan over at most kMaxExpert counters and a second * launch to split that across the device would cost more than the scan does. The offsets are read - * into LDS once so the scan is not 512 dependent global loads. */ + * into LDS once so the scan is not 512 dependent global loads. + * + * THE PREFIX IS PARALLEL: a DPP scan in each wave and one exchange of the wave totals, 256 counts + * a tile -- three tiles at 512 experts, whose n_expert + 1 counts end one past the second. On one + * thread it is 513 dependent LDS round trips in front of a GEMM that waits for the map, and the map + * is built twice a routed layer. Integers: the offsets have one answer whatever order the sums are + * formed in. */ __global__ __launch_bounds__(256) void moe_blockmap_kernel( const int32_t* __restrict__ off, uint32_t n_expert, uint32_t T, uint32_t FM, uint32_t cap, uint32_t* __restrict__ map) { + constexpr uint32_t NT = 256, NW = NT / kWave, kTiles = (kMaxExpert + NT) / NT; __shared__ uint32_t s_cnt[kMaxExpert + 1]; __shared__ int32_t s_off[kMaxExpert + 1]; - const uint32_t tid = threadIdx.x; + __shared__ int32_t s_wt[kTiles][NW]; + const uint32_t tid = threadIdx.x, lane = tid % kWave, wave = tid / kWave; for (uint32_t e = tid; e <= n_expert; e += blockDim.x) s_off[e] = off[e]; __syncthreads(); @@ -3138,59 +3072,8 @@ __global__ __launch_bounds__(256) void moe_blockmap_kernel( if (tid == 0) s_cnt[n_expert] = T > live ? (T - live + FM - 1) / FM : 0u; __syncthreads(); - /* Exclusive scan, one thread. kMaxExpert + 1 adds is nothing beside the launch, and a parallel - * scan here would be three barriers to save a microsecond on a kernel that runs once a layer. */ - if (tid == 0) { - uint32_t acc = 0; - for (uint32_t e = 0; e <= n_expert; ++e) { const uint32_t c = s_cnt[e]; s_cnt[e] = acc; acc += c; } - map[0] = acc < cap ? acc : cap; /* the count, clamped to what the grid can serve */ - } - __syncthreads(); - - for (uint32_t e = tid; e < n_expert; e += blockDim.x) { - const uint32_t run = (uint32_t)(s_off[e + 1] - s_off[e]); - uint32_t o = s_cnt[e]; - for (uint32_t r = 0; r < run; r += FM, ++o) { - if (o >= cap) break; - map[1 + 2 * o + 0] = e; - map[1 + 2 * o + 1] = (uint32_t)s_off[e] + r; - } - } - if (tid == 0) { - uint32_t o = s_cnt[n_expert]; - for (uint32_t r = live; r < T; r += FM, ++o) { - if (o >= cap) break; - map[1 + 2 * o + 0] = kMapTail; - map[1 + 2 * o + 1] = r; - } - } -} - -/* PF2: THE SAME MAP WITH ITS PREFIX IN PARALLEL. Above, one thread prefixes the n_expert + 1 block - * counts alone -- 513 LDS round trips at 512 experts, in front of a GEMM that waits for the map, - * and the map is built twice a routed layer. Here the prefix is the small sort's: a DPP scan in each - * wave and one exchange of the wave totals, 256 counts a tile. Integers: the same offsets, so the - * same map entry for entry, and everything else is the kernel above line for line. */ -__global__ __launch_bounds__(256) void moe_blockmap_par_kernel( - const int32_t* __restrict__ off, uint32_t n_expert, uint32_t T, uint32_t FM, uint32_t cap, - uint32_t* __restrict__ map) { - constexpr uint32_t NT = 256, NW = NT / kWave, kTiles = (kMaxExpert + NT) / NT; - __shared__ uint32_t s_cnt[kMaxExpert + 1]; - __shared__ int32_t s_off[kMaxExpert + 1]; - __shared__ int32_t s_wt[kTiles][NW]; - const uint32_t tid = threadIdx.x, lane = tid % kWave, wave = tid / kWave; - - for (uint32_t e = tid; e <= n_expert; e += blockDim.x) s_off[e] = off[e]; - __syncthreads(); - for (uint32_t e = tid; e < n_expert; e += blockDim.x) { - const uint32_t run = (uint32_t)(s_off[e + 1] - s_off[e]); - s_cnt[e] = (run + FM - 1) / FM; - } - const uint32_t live = (uint32_t)s_off[n_expert]; - if (tid == 0) s_cnt[n_expert] = T > live ? (T - live + FM - 1) / FM : 0u; - __syncthreads(); - - /* Exclusive scan over the n_expert + 1 counts, a tile of NT at a time. */ + /* Exclusive scan over the n_expert + 1 counts, a tile of NT at a time. Each tile has its own + * exchange slots, so no tile's writes meet the reads of the one before it. */ uint32_t carry = 0; for (uint32_t b = 0, tl = 0; b <= n_expert; b += NT, ++tl) { const uint32_t e = b + tid; @@ -3208,7 +3091,7 @@ __global__ __launch_bounds__(256) void moe_blockmap_par_kernel( if (e <= n_expert) s_cnt[e] = carry + (uint32_t)(below + x - v); carry += (uint32_t)tot; } - if (tid == 0) map[0] = carry < cap ? carry : cap; + if (tid == 0) map[0] = carry < cap ? carry : cap; /* the count, clamped to the grid */ __syncthreads(); for (uint32_t e = tid; e < n_expert; e += blockDim.x) { @@ -3234,6 +3117,11 @@ __global__ __launch_bounds__(256) void moe_blockmap_par_kernel( * (tile, run) decode form and 3 the uniform prefill grid, whatever the band. r4d_selftest --perfmoe * times each on one input and compares it with form 1: all three must match byte for byte. */ static int g_moe_prefill_form = 0; +/* THE ALIGNED FORM'S COLUMN TILE, pinned for a test: 0 is the launcher's shape rule, 1 never the + * down projection's 256-column tile (the 128- or 64-column one), 2 the 256-column tile, refused + * where the shape cannot take it or the launch is not the aligned form. r4d_selftest compares the + * two byte for byte. */ +static int g_moe_down_tile_pin = 0; /* Both instantiations, from one body. The only things that differ are the weight table's dtype and * its two pitches, so the fp8 launcher and the w4 one are the same forty lines with three values @@ -3394,12 +3282,14 @@ static int moe_gemm_launch(const RadArgs* a, const R4dSink& sk, int ncls = 2) { /* THE DECODE GRID IS (tile, run) WIDE -- see the kernel's ES note. One tile of rows is all * a decode step has, so without this the whole layer runs on N/64 workgroups. */ const unsigned tiles = (unsigned)((T + 15) / 16); - /* R4D_MOE_DOWN2: the sorted operand's own decode kernel, a block a run tile and G column - * tiles -- see moe_down_dec_kernel. Its grid is the run tiles' upper bound, a tile for - * every run and one more for every 16 rows, and one block for the dropped tail, by the - * column groups. */ - const long long g2 = a_sorted ? r4d_moe_down2_g(T) : 0; - if (g2 > 0 && parts == 1 && n_expert <= (long long)kMaxExpert) { + /* THE SORTED OPERAND'S OWN DECODE KERNEL, a block a run tile and G column tiles -- see + * moe_down_dec_kernel and kMoeDownDecG2Rows. Its grid is the run tiles' upper bound, a + * tile for every run and one more for every 16 rows, and one block for the dropped tail, + * by the column groups. */ + const bool dn_dec = a_sorted && parts == 1 && n_expert <= (long long)kMaxExpert; + const long long g2 = r4d_moe_down_dec_g(T); + if (!dn_dec && g_moe_down_dec_pin > 0) return R4D_NO(RAD_E_UNSUPPORTED); + if (dn_dec && g2 > 0) { const unsigned slots = (unsigned)(std::min(T, n_expert) + (T + 15) / 16); const unsigned groups = (unsigned)((N + 64 * g2 - 1) / (64 * g2)); emit_rc = R4D_EMIT(sk, (moe_down_dec_kernel), @@ -3453,62 +3343,54 @@ static int moe_gemm_launch(const RadArgs* a, const R4dSink& sk, int ncls = 2) { /* The 128-column tile where no column tile straddles two parts of the weight. */ const bool wide_n = (N / parts) % kPrefillCols == 0; const unsigned nyw = (unsigned)(N / kPrefillCols); - /* R4D_MOE_PF2: the run map's prefix in parallel (16), and the down projection -- the - * sorted operand, one part, a w4 weight whose scale is a row's -- at 256 columns over - * sixteen waves where every class's width is whole tiles of it (32). Each wave's share is - * the 128-column tile's: one 16-row M tile by two N tiles, the same K passes and the same - * fold, so every element is the same sum in the same order. What the wider block buys is - * its rows' activation and its run's prologue once for twice the columns. */ - const uint32_t pf = moe_pf2(); - bool dn_wide = W4 != 0 && (pf & kPf2DnWide) && a_sorted && parts == 1; + /* THE DOWN PROJECTION'S 256-COLUMN TILE, a rule of shape: the sorted operand, one part, a + * w4 weight whose scale is a row's, and every class's width whole tiles of it take 256 + * columns over sixteen waves instead of 128 over eight. Each wave's share is the + * 128-column tile's: one 16-row M tile by two N tiles, the same K passes and the same fold, + * so every element is the same sum in the same order. What the wider block buys is its + * rows' activation and its run's prologue once for twice the columns. Any other shape keeps + * the 128- or 64-column tile. */ + bool dn_wide = W4 != 0 && a_sorted && parts == 1 && N % (2 * kPrefillCols) == 0; for (int q = 0; q < ncls; ++q) dn_wide = dn_wide && Nc[q] % (2 * kPrefillCols) == 0; + const int tpin = g_moe_down_tile_pin; + if (tpin < 0 || tpin > 2) return R4D_NO(RAD_E_INVAL); + if (tpin == 2 && (!map || !dn_wide)) return R4D_NO(RAD_E_UNSUPPORTED); + if (tpin == 1) dn_wide = false; if (map) { - auto bmap = (pf & kPf2Map) ? &moe_blockmap_par_kernel : &moe_blockmap_kernel; - moe_pf2_note(pf & kPf2Map); - emit_rc = R4D_EMIT(sk, bmap, dim3(1), dim3(256), 0, + emit_rc = R4D_EMIT(sk, moe_blockmap_kernel, dim3(1), dim3(256), 0, (const int32_t*)off->data, (uint32_t)n_expert, (uint32_t)T, (uint32_t)kPrefillRows, (uint32_t)cap, map); if (emit_rc != RAD_OK) return emit_rc; - /* R4D_MOE_PF2 (64): the first pass's weight at the map read -- see EW at the kernel. - * The same kernels otherwise; a function pointer picks the instantiation. */ - const bool ew = (pf & kPf2Ew) != 0; - moe_pf2_note(pf & kPf2Ew); + /* Every aligned instantiation reads the weight from a uniform base (UW) and issues its + * first pass at the map read -- see THE ALIGNED FORM'S FIRST PASS at the kernel. */ if (dn_wide) { /* Only a w4 weight takes it (dn_wide is false otherwise), and the block-fp8 * instantiation would not compile: its scale is a 128x128 tile's. */ - moe_pf2_note(kPf2DnWide); - if constexpr (W4 != 0) { - auto kd = ew ? &moe_gemm_q_kernel - : &moe_gemm_q_kernel; - emit_rc = R4D_EMIT(sk, kd, - dim3((unsigned)cap, (unsigned)(N / (2 * kPrefillCols))), - dim3(16 * kWave), 0, - (const uint8_t*)Aq->data, (const float*)As->data, ce, co, c2, c3, - (const int32_t*)sorted->data, (const int32_t*)off->data, - (uint16_t*)Y->data, (const uint32_t*)map, - (uint32_t)T, (uint32_t)N, (uint32_t)n_expert, (uint32_t)top_k, - (uint32_t)a_ld, (uint32_t)as_ld, (uint32_t)y_ld, mode, tabv); - } + if constexpr (W4 != 0) + emit_rc = R4D_EMIT(sk, (moe_gemm_q_kernel), + dim3((unsigned)cap, (unsigned)(N / (2 * kPrefillCols))), + dim3(16 * kWave), 0, + (const uint8_t*)Aq->data, (const float*)As->data, ce, co, c2, c3, + (const int32_t*)sorted->data, (const int32_t*)off->data, + (uint16_t*)Y->data, (const uint32_t*)map, + (uint32_t)T, (uint32_t)N, (uint32_t)n_expert, (uint32_t)top_k, + (uint32_t)a_ld, (uint32_t)as_ld, (uint32_t)y_ld, mode, tabv); } else if (wide_n) { - auto kw = ew ? &moe_gemm_q_kernel - : &moe_gemm_q_kernel; - emit_rc = R4D_EMIT(sk, kw, - dim3((unsigned)cap, nyw), dim3(8 * kWave), 0, - (const uint8_t*)Aq->data, (const float*)As->data, ce, co, c2, c3, - (const int32_t*)sorted->data, (const int32_t*)off->data, - (uint16_t*)Y->data, (const uint32_t*)map, - (uint32_t)T, (uint32_t)N, (uint32_t)n_expert, (uint32_t)top_k, - (uint32_t)a_ld, (uint32_t)as_ld, (uint32_t)y_ld, mode, tabv); + emit_rc = R4D_EMIT(sk, (moe_gemm_q_kernel), + dim3((unsigned)cap, nyw), dim3(8 * kWave), 0, + (const uint8_t*)Aq->data, (const float*)As->data, ce, co, c2, c3, + (const int32_t*)sorted->data, (const int32_t*)off->data, + (uint16_t*)Y->data, (const uint32_t*)map, + (uint32_t)T, (uint32_t)N, (uint32_t)n_expert, (uint32_t)top_k, + (uint32_t)a_ld, (uint32_t)as_ld, (uint32_t)y_ld, mode, tabv); } else { - auto kn = ew ? &moe_gemm_q_kernel - : &moe_gemm_q_kernel; - emit_rc = R4D_EMIT(sk, kn, - dim3((unsigned)cap, ny), dim3(4 * kWave), 0, - (const uint8_t*)Aq->data, (const float*)As->data, ce, co, c2, c3, - (const int32_t*)sorted->data, (const int32_t*)off->data, - (uint16_t*)Y->data, (const uint32_t*)map, - (uint32_t)T, (uint32_t)N, (uint32_t)n_expert, (uint32_t)top_k, - (uint32_t)a_ld, (uint32_t)as_ld, (uint32_t)y_ld, mode, tabv); + emit_rc = R4D_EMIT(sk, (moe_gemm_q_kernel), + dim3((unsigned)cap, ny), dim3(4 * kWave), 0, + (const uint8_t*)Aq->data, (const float*)As->data, ce, co, c2, c3, + (const int32_t*)sorted->data, (const int32_t*)off->data, + (uint16_t*)Y->data, (const uint32_t*)map, + (uint32_t)T, (uint32_t)N, (uint32_t)n_expert, (uint32_t)top_k, + (uint32_t)a_ld, (uint32_t)as_ld, (uint32_t)y_ld, mode, tabv); } } else if (wide_n) { emit_rc = R4D_EMIT(sk, (moe_gemm_q_kernel), @@ -3562,7 +3444,10 @@ extern "C" int64_t r4d_rad_moe_gemm_w4a8_scratch(const RadArgs* a) { return moe_ extern "C" void r4d_moe_gemm_pin_prefill(int form) { g_moe_prefill_form = form; } extern "C" void r4d_moe_gemm_pin_fk(int pin) { g_moe_fk_pin = pin; } -extern "C" void r4d_moe_gemm_pin_down2(int pin) { g_moe_dn2_pin = pin; } +extern "C" void r4d_moe_gemm_pin_down_tile(int pin) { g_moe_down_tile_pin = pin; } +extern "C" void r4d_moe_gemm_pin_down_dec(int pin) { + g_moe_down_dec_pin = pin < (int)kMoeDownDecMaxG ? pin : (int)kMoeDownDecMaxG; +} extern "C" int r4d_rad_moe_gemm_q(const RadArgs* a, RadStream s) { return moe_gemm_launch<0>(a, r4d_stream_sink(s)); @@ -3768,18 +3653,22 @@ extern "C" int r4d_rad_moe_gather(const RadArgs* a, RadStream s) { else if (act && std::strcmp(act, "none")) return R4D_NO(RAD_E_UNSUPPORTED); } - /* R4D_MOE_PF2 (2, and 4 for the column groups last first): the prefill form a wave a token - * and sixteen bytes a lane, where the slots fit its registers and every row it reads or writes - * starts on sixteen bytes. The same sums; see gather_wide_kernel. */ - const uint32_t pf = moe_pf2(); + /* THE PREFILL FORM, a wave a token and sixteen bytes a lane (gather_wide_kernel), past the + * self-scan bound wherever the slots fit its registers and every row it reads or writes starts + * on sixteen bytes. The same sums as gather_kernel's; see the kernel. Anything else -- a decode + * step, a top_k past kGatherWideSlots, a width or a pitch sixteen-byte pieces do not tile -- + * takes gather_kernel. */ auto al16 = [](const void* p, long long ld) { return ((uintptr_t)p % 16) == 0 && ld % (long long)kGatherWideCols == 0; }; - if (!self && (pf & kPf2Gather) && top_k <= (long long)kGatherWideSlots && - n % (long long)kGatherWideCols == 0 && al16(ye->data, ye_ld) && al16(Y->data, y_ld) && - (!sh || al16(sh->data, sh_ld))) { + const bool wide_ok = !self && top_k <= (long long)kGatherWideSlots && + n % (long long)kGatherWideCols == 0 && al16(ye->data, ye_ld) && + al16(Y->data, y_ld) && (!sh || al16(sh->data, sh_ld)); + const int gpin = g_moe_gather_pin; + if (gpin < 0 || gpin > 2) return R4D_NO(RAD_E_INVAL); + if (gpin >= 2 && !wide_ok) return R4D_NO(RAD_E_UNSUPPORTED); + if ((gpin == 0 && wide_ok) || gpin >= 2) { constexpr uint32_t NW = 8; - moe_pf2_note(kPf2Gather | (pf & kPf2GatherRev)); const unsigned gw = (unsigned)((n + kWave * kGatherWideCols - 1) / (kWave * kGatherWideCols)); hipLaunchKernelGGL((gather_wide_kernel), dim3((unsigned)((M + NW - 1) / NW), gw), dim3(NW * kWave), 0, (hipStream_t)s, @@ -3788,7 +3677,7 @@ extern "C" int r4d_rad_moe_gather(const RadArgs* a, RadStream s) { (uint32_t)ye_ld, (uint32_t)ew_ld, (uint32_t)y_ld, sh ? (const uint16_t*)sh->data : nullptr, sh ? (const uint16_t*)sg->data : nullptr, - (uint32_t)sh_ld, (uint32_t)sg_ld, sig, (pf & kPf2GatherRev) ? 1 : 0); + (uint32_t)sh_ld, (uint32_t)sg_ld, sig); R4D_MOE_CHK(); return RAD_OK; } diff --git a/libr4d/r4d_selftest.cpp b/libr4d/r4d_selftest.cpp index 27953d00..a80b67e7 100644 --- a/libr4d/r4d_selftest.cpp +++ b/libr4d/r4d_selftest.cpp @@ -45,8 +45,6 @@ #include "rad_abi.h" #include "rad_plugin.h" -#include "../vendor/nlohmann/json.hpp" -#include #include "r4d_args.h" #include "r4d_hc_fp8.h" #include "r4d_fp8_frag.h" @@ -165,9 +163,9 @@ struct Plugin { void (*pin_qsa)(int) = nullptr; void (*pin_moe)(int) = nullptr; void (*pin_fk)(int) = nullptr; - void (*pin_pf2)(int) = nullptr; - uint32_t (*pf2_used)(void) = nullptr; - void (*pin_dn2)(int) = nullptr; + void (*pin_dtile)(int) = nullptr; + void (*pin_gather)(int) = nullptr; + void (*pin_ddec)(int) = nullptr; void (*pin_hc)(int) = nullptr; void (*pin_qsel)(int) = nullptr; }; @@ -199,9 +197,9 @@ static bool load(Plugin* pl, const char* path) { okall &= sym(pl->h, "r4d_qsa_score_pin_form", &pl->pin_qsa); okall &= sym(pl->h, "r4d_moe_gemm_pin_prefill", &pl->pin_moe); okall &= sym(pl->h, "r4d_moe_gemm_pin_fk", &pl->pin_fk); - okall &= sym(pl->h, "r4d_moe_pf2_pin", &pl->pin_pf2); - okall &= sym(pl->h, "r4d_moe_pf2_used", &pl->pf2_used); - okall &= sym(pl->h, "r4d_moe_gemm_pin_down2", &pl->pin_dn2); + okall &= sym(pl->h, "r4d_moe_gemm_pin_down_tile", &pl->pin_dtile); + okall &= sym(pl->h, "r4d_moe_gather_pin_form", &pl->pin_gather); + okall &= sym(pl->h, "r4d_moe_gemm_pin_down_dec", &pl->pin_ddec); okall &= sym(pl->h, "r4d_hc_read_pin_prefill", &pl->pin_hc); okall &= sym(pl->h, "r4d_qsa_select_pin_form", &pl->pin_qsel); return okall; @@ -1186,6 +1184,36 @@ static bool restores(const RadKernelInfo* r, const std::vector& lp, in * r's R pairs at [r*R, (r+1)*R) of each row. If that interleave is ever spelled differently in * the graph, this case is what says so. */ +/* ------------------------------------------------- static YaRN, in double + * + * cos and sin of one rotary pair at one position: the inverse frequency theta^(-2i/rot), blended + * toward inv/factor over the linear ramp [low, high] -- vLLM's YaRNScalingRotaryEmbedding, whose + * correction bounds tests/yarn_test.cpp holds yarn_from to -- and both scaled by the magnitude. + * Computed here, so the YaRN cases need nothing beside this binary. + * + * `tol` IS WHAT AN f32 EVALUATION OF THE SAME FORMULA MAY DIFFER BY. powf, the reciprocal, the + * blend and the product with the position each cost an ulp or so of the ANGLE, so the bound is 8 + * ulp of the angle plus the rounding of the result. Where the ramp sits it is a few 1e-5 even at + * position 524287, so a wrong factor, bound or blend order is far outside it; it is loose only in + * the fastest pairs at long positions, which YaRN leaves unscaled and which no f32 rotary places + * better than its own angle's ulp. A double reference cannot reproduce the device's powf rounding + * there, and a tolerance that pretended to would be a table of this card's answers. */ +struct YarnRef { double c, s, tol; }; +static YarnRef yarn_ref(double pos, int i, int rot, double theta, double factor, double low, + double high, double magnitude) { + const double inv = std::pow(theta, -2.0 * i / rot); + const double ramp = std::min(1.0, std::max(0.0, (i - low) / (high == low ? 0.001 : high - low))); + const double a = pos * (inv / factor * ramp + inv * (1.0 - ramp)); + return { std::cos(a) * magnitude, std::sin(a) * magnitude, + magnitude * (std::fabs(a) * 0x1p-20 + 0x1p-21) }; +} +/* The YaRN cases' parameters: factor 2 over 262144 trained positions at theta 1e7, and the + * positions a 512K context reaches -- the ends, the trained length on both sides, and past it. */ +static const double kYarnMag = 1.069314718055994531; /* 1 + 0.1 ln 2 */ +static const int32_t kYarnPos[] = { 0, 1, 4096, 262143, 262144, 500000, 524287 }; +static double yarn_low(int64_t rot) { return rot == 64 ? 14 : rot == 128 ? 28 : 56; } +static double yarn_high(int64_t rot) { return rot == 64 ? 22 : rot == 128 ? 43 : 85; } + /* ------------------------------------------------- the fused attention prologue, against its pair * * THE QUESTION IS BIT EQUALITY AND NOTHING ELSE. `qk_norm_rope_gate` replaces rmsnorm(q) + @@ -1224,8 +1252,8 @@ static void qk_prologue_case(const Plugin& pl, uint32_t gain_dt, bool yarn = fal const int64_t M = 512, nq = 12, nkv = 2, hd = 256, ctx = yarn ? 524288 : 4096; auto add_yarn = [&](std::vector& ps) { if (!yarn) return; - ps.push_back(PF("yarn_factor", 2)); ps.push_back(PF("yarn_low", rot == 64 ? 14 : 28)); - ps.push_back(PF("yarn_high", rot == 64 ? 22 : 43)); ps.push_back(PF("yarn_magnitude", 1.069314718)); + ps.push_back(PF("yarn_factor", 2)); ps.push_back(PF("yarn_low", yarn_low(rot))); + ps.push_back(PF("yarn_high", yarn_high(rot))); ps.push_back(PF("yarn_magnitude", kYarnMag)); }; const double theta = 1e7, eps = 1e-6, wadd = 1.0; /* MANY ROWS, because the difference this case is hunting is one bf16 in tens of thousands: two @@ -1235,10 +1263,7 @@ static void qk_prologue_case(const Plugin& pl, uint32_t gain_dt, bool yarn = fal * near the top. */ std::vector pos((size_t)M); for (int64_t i = 0; i < M; ++i) pos[(size_t)i] = (int32_t)((i * 7 + i * i) % ctx); - if (yarn) { - const int32_t edges[] = {0, 1, 4096, 262143, 262144, 500000, 524287}; - std::copy(std::begin(edges), std::end(edges), pos.begin()); - } + if (yarn) std::copy(std::begin(kYarnPos), std::end(kYarnPos), pos.begin()); auto rnd = [](int64_t i) { /* deterministic, and spans the exponent */ const uint32_t x = (uint32_t)(i * 2654435761u + 12345u); @@ -1340,22 +1365,25 @@ static void qk_prologue_case(const Plugin& pl, uint32_t gain_dt, bool yarn = fal add_yarn(ps); run_case(rt, ts, ps); if (yarn) { - const std::string here = __FILE__; - std::ifstream input(here.substr(0, here.find_last_of('/')) + "/../tests/data/yarn_reference_rocm.json"); - if (!input) { ok("YaRN independent vectors", false, "fixture missing"); return; } - const auto fixture = nlohmann::json::parse(input); - const auto& c = fixture["cases"][rot == 64 ? 0 : 1]; - std::vector row(rot); + /* The table's rows at the YaRN positions against the double-precision formula, as a + * fraction of what f32 may differ by (yarn_ref): at or under 1 passes. */ + std::vector row((size_t)rot); double worst = 0.0; - for (size_t m = 0; m < fixture["positions"].size(); ++m) { - st_memcpy(row.data(), (float*)d_cs + (size_t)pos[m] * rot, rot * sizeof(float), hipMemcpyDeviceToHost); + for (int32_t p : kYarnPos) { + st_memcpy(row.data(), (float*)d_cs + (size_t)p * rot, (size_t)rot * sizeof(float), + hipMemcpyDeviceToHost); for (int i = 0; i < rot / 2; ++i) { - worst = std::max(worst, std::abs((double)row[i] - c["cos"][m][i].get())); - worst = std::max(worst, std::abs((double)row[rot/2+i] - c["sin"][m][i].get())); + const YarnRef y = yarn_ref(p, i, (int)rot, theta, 2.0, yarn_low(rot), + yarn_high(rot), kYarnMag); + worst = std::max(worst, std::fabs(row[(size_t)i] - y.c) / y.tol); + worst = std::max(worst, std::fabs(row[(size_t)(rot / 2 + i)] - y.s) / y.tol); } } - ok("YaRN GPU table vs independent vLLM through 524287", worst <= 0.004, - ("worst absolute error " + std::to_string(worst)).c_str()); + char what[96], got[96]; + std::snprintf(what, sizeof what, "YaRN table, rot %lld, vs a double YaRN through 524287", + (long long)rot); + std::snprintf(got, sizeof got, "worst %.3g of the f32 bound", worst); + ok(what, worst <= 1.0, got); } } { @@ -2067,30 +2095,23 @@ static void router_gemm_fused_case(const Plugin& pl) { * with the rest closed up -- and keep every run stable. Both forms of the standalone sort run: 450 * slots is the one-workgroup form, 3000 the three-pass one, and the first, a middle and the last * expert of a rank's slice are protected so the closing-up is tested at both ends. The fused - * decode kernel is held to the standalone pair in topk_scatter_case. */ -/* `pf2` >= 0 pins R4D_MOE_PF2 for the launch (0 off, 1 every part), and the case is then also run - * at a prefill chunk's sizes: the general form's scan and tail are what that switch replaces, and - * both forms are held to the same host sort. -1 leaves the switch alone. */ -static void scatter_protect_at(const Plugin& pl, const RadKernelInfo* rs, int64_t M, int64_t nl, - int64_t base, int pf2); + * decode kernel is held to the standalone pair in topk_scatter_case. + * + * The general form also runs at a prefill chunk's sizes: 79 and 80 tiles of 256 slots, so a + * scan thread's column of tile counts takes several batches of loads with a short last one, on a + * rank's slice and with every one of 512 experts local (the mod4 split's case), where the scan's + * workgroup is every thread an expert. */ +static void scatter_protect_at(const RadKernelInfo* rs, int64_t M, int64_t nl, int64_t base); static void scatter_protect_case(const Plugin& pl) { if (!sel("moe_scatter_i32")) return; const RadKernelInfo* rs = find(pl, "moe_scatter_i32"); if (!rs) { ok("moe_scatter protect", false, "row absent"); return; } - for (const int64_t M : { 45, 300 }) scatter_protect_at(pl, rs, M, 254, 3, -1); - /* R4D_MOE_PF2, off and on, at a prefill chunk: 79 and 80 tiles of the general form, a rank's - * slice and every one of 512 experts local (the mod4 split's case). */ - if (pl.pin_pf2) - for (const int pf : { 0, 1 }) { - scatter_protect_at(pl, rs, 300, 254, 3, pf); - scatter_protect_at(pl, rs, 2000, 254, 3, pf); - scatter_protect_at(pl, rs, 2048, 512, 0, pf); - } + for (const int64_t M : { 45, 300, 2000 }) scatter_protect_at(rs, M, 254, 3); + scatter_protect_at(rs, 2048, 512, 0); } -static void scatter_protect_at(const Plugin& pl, const RadKernelInfo* rs, int64_t M, int64_t nl, - int64_t base, int pf2) { +static void scatter_protect_at(const RadKernelInfo* rs, int64_t M, int64_t nl, int64_t base) { const int64_t ne = 512, k = 10; const std::vector prot = { 0, 17, 101, 253 }; std::vector place((size_t)nl); @@ -2103,7 +2124,6 @@ static void scatter_protect_at(const Plugin& pl, const RadKernelInfo* rs, int64_ { const int64_t NS = M * k; const std::string at = " (" + std::to_string(NS) + " slots, slice " + std::to_string(nl) + - (pf2 < 0 ? "" : pf2 ? ", R4D_MOE_PF2 on" : ", R4D_MOE_PF2 off") + ")"; /* Each row's k distinct experts from the whole router, a quarter of them in this slice and * the protected ones over-represented so their runs are long. */ @@ -2150,17 +2170,7 @@ static void scatter_protect_at(const Plugin& pl, const RadKernelInfo* rs, int64_ T(d_of, RAD_I32, { nl + 1 }), T(d_cn, RAD_I32, { nl }) }; std::vector ps = { PI("M", M), PI("n_expert", nl), PI("top_k", k), PI("expert_base", base), PS("protect", "0,17,101,253") }; - if (pf2 >= 0) { pl.pin_pf2(pf2); pl.pf2_used(); } run_case(rs, ts, ps); - if (pf2 >= 0) { - /* Pinned on, the general form must have taken the parallel scan -- else the bytes - * below would be the default's against itself. The one-workgroup form takes nothing. */ - const uint32_t u = pl.pf2_used(); - pl.pin_pf2(-1); - const bool want = pf2 != 0 && NS > 1024; - ok(("moe_scatter took " + std::string(want ? "the R4D_MOE_PF2 scan" : "the default scan") + - at).c_str(), ((u & 8u) != 0) == want, "parts used " + std::to_string(u)); - } std::vector got_s((size_t)NS), got_o((size_t)(nl + 1)), got_c((size_t)nl); st_memcpy(got_s.data(), d_so, (size_t)NS * 4, hipMemcpyDeviceToHost); st_memcpy(got_o.data(), d_of, (size_t)(nl + 1) * 4, hipMemcpyDeviceToHost); @@ -2181,16 +2191,18 @@ static void scatter_protect_at(const Plugin& pl, const RadKernelInfo* rs, int64_ } } -/* moe_gather AT A PREFILL CHUNK: R4D_MOE_PF2 against the form it replaces, byte for byte. +/* moe_gather'S FORMS, against each other byte for byte. * - * Past the self-scan cap the op builds the slot inverse in scratch and sums each token's slots a - * column a thread. R4D_MOE_PF2 replaces that sum with a wave a token and sixteen bytes a lane (part - * 2) and takes the column groups last first (part 4). Each must write the default's bytes: with and - * without the shared arm folded in, under both gates, with dropped slots, at a width that ends - * inside a column group, and at Qwen3.8-Flash-Next's 2048-token chunk on a rank of four -- where the - * case also prints what each form costs. A decode step (under the cap), a top_k past the wide - * form's registers and a width it cannot take in sixteen-byte pieces keep the default form, and the - * case checks that they did. `wide` is whether the shape takes it. */ + * Past the self-scan cap the op builds the slot inverse in scratch and sums each token's slots. + * Its rule takes gather_wide_kernel there -- a wave a token and sixteen bytes a lane -- wherever + * the slots fit its registers and every row starts on sixteen bytes, and gather_kernel -- a column + * a thread -- everywhere else. r4d_moe_gather_pin_form pins each (1 the column a thread, 2 the wave + * a token), and every form must write the column-a-thread form's bytes: with and without the shared arm folded + * in, under both gates, with dropped slots, at a width that ends inside a column group, and at + * Qwen3.8-Flash-Next's 2048-token chunk on a rank of four -- where the case also prints what each + * form costs. A decode step (under the cap), a top_k past the wide form's registers and a width it + * cannot take in sixteen-byte pieces must refuse the wide pins, and the rule then writes the same + * bytes as the column a thread. `wide` is whether the shape takes the wide form. */ struct MoeGatherShape { int64_t M, n, k; const char* act; bool wide; }; static void moe_gather_at(const Plugin& pl, const RadKernelInfo* r, const MoeGatherShape& sh) { @@ -2231,7 +2243,7 @@ static void moe_gather_at(const Plugin& pl, const RadKernelInfo* r, const MoeGat void* d_sg = shared ? dalloc(sgv.size() * 2) : nullptr; void* d_y = dalloc((size_t)(M * n * 2)); if (!d_ye || !d_ew || !d_so || !d_y || (shared && (!d_sh || !d_sg))) { - ok(("moe_gather R4D_MOE_PF2" + at).c_str(), false, "hipMalloc failed"); + ok(("moe_gather forms" + at).c_str(), false, "hipMalloc failed"); return; } st_memcpy(d_ye, ye.data(), ye.size() * 2, hipMemcpyHostToDevice); @@ -2256,38 +2268,38 @@ static void moe_gather_at(const Plugin& pl, const RadKernelInfo* r, const MoeGat const int64_t need = r->scratch ? r->scratch(&a) : 0; if (need > 0) { a.scratch = dalloc((size_t)need); a.scratch_bytes = need; } - /* One launch at a pin, over stale bytes, and the parts it took. */ - auto run = [&](int pin, std::vector& out, uint32_t* used) -> bool { + /* One launch at a pinned form, over stale bytes; its return code. */ + auto run = [&](int form, std::vector& out) -> int { std::vector junk((size_t)(M * n), 0x7f7fu); st_memcpy(d_y, junk.data(), junk.size() * 2, hipMemcpyHostToDevice); - pl.pin_pf2(pin); - pl.pf2_used(); + pl.pin_gather(form); const int rc = r->launch(&a, nullptr); const hipError_t he = (hipError_t)st_stream_sync(nullptr); - *used = pl.pf2_used(); - pl.pin_pf2(-1); - if (rc != RAD_OK || he != hipSuccess) { - ok(("moe_gather R4D_MOE_PF2 pin " + std::to_string(pin) + at).c_str(), false, - "rc=" + std::to_string(rc) + " hip=" + hipGetErrorString(he)); - return false; - } + pl.pin_gather(0); + if (rc == RAD_OK && he != hipSuccess) return RAD_E_DEVICE; out.resize((size_t)(M * n)); st_memcpy(out.data(), d_y, out.size() * 2, hipMemcpyDeviceToHost); - return true; + return rc; }; std::vector base, got; - uint32_t used = 0; - if (!run(0, base, &used)) return; - ok(("moe_gather, R4D_MOE_PF2 off, takes no part of it" + at).c_str(), used == 0, - "parts used " + std::to_string(used)); - struct Var { int pin; const char* name; uint32_t parts; }; + if (const int rc = run(1, base); rc != RAD_OK) { + ok(("moe_gather, a column a thread" + at).c_str(), false, "rc=" + std::to_string(rc)); + return; + } + struct Var { int form; const char* name; bool wide; }; static const Var kVar[] = { - { 2, "a wave a token", 2u }, - { 6, "a wave a token, column groups last first", 6u }, - { 1, "every part", 6u }, + { 0, "the rule", false }, + { 2, "a wave a token", true }, }; for (const Var& v : kVar) { - if (!run(v.pin, got, &used)) return; + const std::string label = "moe_gather, " + std::string(v.name) + at; + const int rc = run(v.form, got); + if (v.wide && !sh.wide) { + /* A shape the wide form cannot take refuses the pin rather than run another form. */ + ok((label + " is refused").c_str(), rc == RAD_E_UNSUPPORTED, "rc=" + std::to_string(rc)); + continue; + } + if (rc != RAD_OK) { ok(label.c_str(), false, "rc=" + std::to_string(rc)); continue; } int64_t diff = 0; std::string first; for (size_t i = 0; i < got.size(); ++i) @@ -2297,21 +2309,18 @@ static void moe_gather_at(const Plugin& pl, const RadKernelInfo* r, const MoeGat std::to_string((int64_t)i % n); ++diff; } - const uint32_t want = sh.wide ? v.parts : 0u; - ok(("moe_gather R4D_MOE_PF2, " + std::string(v.name) + - (sh.wide ? "" : " (the default form, which this shape keeps)") + at).c_str(), - diff == 0 && (used & 6u) == want, - std::to_string(diff) + " of " + std::to_string(M * n) + " differ" + first + - "; parts used " + std::to_string(used) + ", expected " + std::to_string(want)); + ok(label.c_str(), diff == 0, + std::to_string(diff) + " of " + std::to_string(M * n) + + " differ from a column a thread" + first); } - if (M >= 2048) { + if (M >= 2048 && sh.wide) { hipEvent_t e0, e1; st_ev_create(&e0); st_ev_create(&e1); - auto time = [&](int pin) -> float { + auto time = [&](int form) -> float { const int it = 20; - pl.pin_pf2(pin); + pl.pin_gather(form); for (int w = 0; w < 3; ++w) r->launch(&a, nullptr); st_ev_record(e0, nullptr); for (int i = 0; i < it; ++i) r->launch(&a, nullptr); @@ -2319,15 +2328,13 @@ static void moe_gather_at(const Plugin& pl, const RadKernelInfo* r, const MoeGat st_ev_sync(e1); float ms = 0.0f; st_ev_elapsed(&ms, e0, e1); - pl.pin_pf2(-1); - pl.pf2_used(); + pl.pin_gather(0); return ms * 1000.0f / (float)it; }; - const float t0 = time(0), t2 = time(2), t6 = time(6); - std::printf(" moe_gather at %lld routed rows x %lld: default %.1f us, a wave a token " - "%.1f us, last first %.1f us a call, prepass included (the cache that last " - "first is for needs the down projection in front, which this does not run)\n", - (long long)NS, (long long)n, t0, t2, t6); + const float t1 = time(1), t2 = time(2); + std::printf(" moe_gather at %lld routed rows x %lld: a column a thread %.1f us, a wave " + "a token %.1f us a call, prepass included\n", + (long long)NS, (long long)n, t1, t2); st_ev_destroy(e0); st_ev_destroy(e1); } @@ -2337,7 +2344,7 @@ static void moe_gather_case(const Plugin& pl) { if (!sel("moe_gather_bf16")) return; const RadKernelInfo* r = find(pl, "moe_gather_bf16"); if (!r) { ok("moe_gather_bf16", false, "row absent"); return; } - if (!pl.pin_pf2 || !pl.pf2_used) { ok("moe_gather_bf16 R4D_MOE_PF2", false, "pin hooks absent"); return; } + if (!pl.pin_gather) { ok("moe_gather_bf16 forms", false, "pin hook absent"); return; } static const MoeGatherShape kSh[] = { { 600, 2560, 10, "sigmoid", true }, { 600, 2560, 10, nullptr, true }, @@ -6767,97 +6774,101 @@ static void moe_dec_variants(const Plugin& pl, const RadKernelInfo* row, RadArgs } } -/* THE RUN-ALIGNED PREFILL FORM UNDER R4D_MOE_PF2, against the same form without it, byte for byte: - * the run map's prefix in parallel (16) on every shape, the first pass's weight issued at the map - * read (64) on every shape, and on the sorted operand -- one part, every class's width whole - * 256-column tiles -- the down projection's 256-column tile (32); each alone and all together. - * Described first where the row can be: the map's kernel must differ from the default's exactly - * when 16 is on, and the GEMM's exactly when 64 is or the wide tile applies, so the bytes below are - * never one kernel against itself; and the launch must report the parts it took. `base` is the - * form's own bytes, already held to the host decode by the form loop. +/* THE RUN-ALIGNED FORM'S TWO COLUMN TILES ON THE DOWN PROJECTION, byte for byte. Where the + * sorted operand, one part and every class's width in whole 256-column tiles let the rule take the + * 256-column tile, the 128-column tile (r4d_moe_gemm_pin_down_tile 1) must write its bytes. Every + * launch is DESCRIBED first where the row can be: the rule and the 256 pin must name one GEMM and + * the 128 pin another, so the bytes below are never one kernel against itself. Where the shape + * cannot take the 256-column tile, the pin to it is refused. `base` is the aligned form's own bytes + * under the rule, already held to the host decode by the form loop. * * PAST 512 EXPERTS THERE IS NO ALIGNED LAUNCH: the map's builder holds kMaxExpert + 1 offsets, so - * the launcher hands a wider expert count the uniform grid whatever scratch it is given, and none - * of these parts has a launch to take. Such a shape is skipped, not failed. */ -static void moe_pf2_aligned(const Plugin& pl, const RadKernelInfo* row, RadArgs& a, void* d_y, - void* d_map, int64_t map_bytes, size_t elems, - const std::vector& base, bool dn_wide, int64_t ne, - const std::string& what) { - if (!pl.pin_pf2 || !pl.pf2_used || !pl.pin_moe || base.empty()) return; + * the launcher hands a wider expert count the uniform grid whatever scratch it is given. Such a + * shape is skipped, not failed. */ +static void moe_aligned_down_tile(const Plugin& pl, const RadKernelInfo* row, RadArgs& a, + void* d_y, void* d_map, int64_t map_bytes, size_t elems, + const std::vector& base, bool dn_wide, int64_t ne, + const std::string& what) { + if (!pl.pin_dtile || !pl.pin_moe || base.empty()) return; + const std::string head = what + " aligned prefill form"; if (ne > 512) { - skip((what + " aligned prefill form, R4D_MOE_PF2").c_str(), - "no run map past 512 experts, so no aligned launch"); + skip((head + ", column tiles").c_str(), "no run map past 512 experts, so no aligned launch"); return; } void* const sc0 = a.scratch; const int64_t sb0 = a.scratch_bytes; - struct Var { int pin; uint32_t mask; const char* name; }; - static const Var kVar[] = { - { 16, 16u, "map prefix in parallel" }, - { 48, 48u, "map prefix in parallel, 256-column down tile" }, - { 64, 64u, "first pass's weight at the map read" }, - { 1, 112u, "every part" }, + a.scratch = d_map; + a.scratch_bytes = map_bytes; + pl.pin_moe(1); + auto restore = [&] { + pl.pin_dtile(0); + pl.pin_moe(0); + a.scratch = sc0; + a.scratch_bytes = sb0; }; - for (const Var& v : kVar) { - if ((v.mask & 32u) && v.mask != 112u && !dn_wide) continue; /* the same as 16 here */ - const bool map_new = (v.mask & 16u) != 0; - const bool gemm_new = (v.mask & 64u) || (dn_wide && (v.mask & 32u)); - const uint32_t want = (v.mask & 16u) | (v.mask & 64u) | (dn_wide ? (v.mask & 32u) : 0u); - const std::string label = what + " aligned prefill form, R4D_MOE_PF2 " + - std::to_string(v.pin) + " (" + v.name + ")"; - a.scratch = d_map; - a.scratch_bytes = map_bytes; - auto restore = [&] { - pl.pin_pf2(-1); - pl.pf2_used(); - pl.pin_moe(0); - a.scratch = sc0; - a.scratch_bytes = sb0; - }; - pl.pin_moe(1); - if (row->describe) { - static RadLaunchDesc d0[4], d1[4]; - int n0 = 0, n1 = 0; - pl.pin_pf2(0); - const int c0 = row->describe(&a, nullptr, d0, 4, &n0); - pl.pin_pf2(v.pin); - const int c1 = row->describe(&a, nullptr, d1, 4, &n1); - if (c0 != RAD_OK || c1 != RAD_OK || n0 != 2 || n1 != 2) { - restore(); - ok(label.c_str(), false, "describe rc " + std::to_string(c0) + "/" + - std::to_string(c1) + ", launches " + - std::to_string(n0) + "/" + std::to_string(n1)); - continue; - } - if ((d0[0].func != d1[0].func) != map_new || (d0[1].func != d1[1].func) != gemm_new) { - restore(); - ok(label.c_str(), false, std::string("described kernels: the map's ") + - (d0[0].func == d1[0].func ? "is the default's" : "differs") + - ", the GEMM's " + - (d0[1].func == d1[1].func ? "is the default's" : "differs")); - continue; - } - } + auto launch = [&](int pin, std::vector* got) { { std::vector junk(elems, 0x7f7fu); st_memcpy(d_y, junk.data(), junk.size() * 2, hipMemcpyHostToDevice); } - pl.pin_pf2(v.pin); - pl.pf2_used(); + pl.pin_dtile(pin); const int rc = row->launch(&a, nullptr); st_stream_sync(nullptr); - const uint32_t used = pl.pf2_used(); + pl.pin_dtile(0); + if (got) { + got->assign(elems, 0); + st_memcpy(got->data(), d_y, elems * 2, hipMemcpyDeviceToHost); + } + return rc; + }; + if (!dn_wide) { + const int rc = launch(2, nullptr); restore(); + ok((head + ", the 256-column down tile is refused on this shape").c_str(), + rc == RAD_E_UNSUPPORTED, "rc=" + std::to_string(rc)); + return; + } + if (row->describe) { + const void* fn[3] = {}; + for (int pin = 0; pin < 3; ++pin) { + static RadLaunchDesc d[4]; + int n = 0; + pl.pin_dtile(pin); + const int drc = row->describe(&a, nullptr, d, 4, &n); + pl.pin_dtile(0); + if (drc != RAD_OK || n != 2) { + restore(); + ok((head + ", column tiles described").c_str(), false, + "pin " + std::to_string(pin) + ": describe rc " + std::to_string(drc) + + ", launches " + std::to_string(n)); + return; + } + fn[pin] = d[1].func; + } + if (fn[0] != fn[2] || fn[1] == fn[2]) { + restore(); + ok((head + ", column tiles described").c_str(), false, + std::string("the rule ") + (fn[0] == fn[2] ? "names" : "does not name") + + " the 256-column GEMM; the 128 pin " + + (fn[1] == fn[2] ? "names it too" : "names another")); + return; + } + } + struct Var { int pin; const char* name; }; + static const Var kVar[] = { { 1, "the 128-column down tile" }, + { 2, "the 256-column down tile, pinned" } }; + for (const Var& v : kVar) { + const std::string label = head + ", " + v.name + + (row->describe ? "" : " (launch not described)"); + std::vector got; + const int rc = launch(v.pin, &got); if (rc != RAD_OK) { ok(label.c_str(), false, "launch rc=" + std::to_string(rc)); continue; } - std::vector got(elems); - st_memcpy(got.data(), d_y, got.size() * 2, hipMemcpyDeviceToHost); int64_t diff = 0; for (size_t i = 0; i < elems; ++i) diff += got[i] != base[i]; - ok(label.c_str(), diff == 0 && (used & 112u) == want, - std::to_string(diff) + " of " + std::to_string(elems) + - " elements differ from the form without it; parts used " + std::to_string(used) + - ", expected " + std::to_string(want)); + ok(label.c_str(), diff == 0, std::to_string(diff) + " of " + std::to_string(elems) + + " elements differ from the rule's 256-column tile"); } + restore(); } /* EVERY FORM THE LAUNCHER HAS, each against the host decode: the run-aligned prefill form (a run @@ -6939,11 +6950,14 @@ static void moe_w4_gemm(const Plugin& pl, int64_t Tr, const MoeW4Shape& sh, int } /* The activation is written as E4M3 BYTES and a per-(row, group) f32 scale, so the host and - * the device read the same numbers and the only difference left is summation order. */ + * the device read the same numbers and the only difference left is summation order. The scale + * varies along the rows as well as the groups: one the same for every row would let a kernel + * that reads another row's scale pass. */ std::vector aq((size_t)(M * K)); std::vector as((size_t)(M * kb)); for (int64_t m = 0; m < M; ++m) { - for (int64_t g = 0; g < kb; ++g) as[(size_t)(m * kb + g)] = 0.011f + 0.003f * (float)g; + for (int64_t g = 0; g < kb; ++g) + as[(size_t)(m * kb + g)] = 0.011f + 0.003f * (float)g + 0.0007f * (float)(m % 11); for (int64_t k = 0; k < K; ++k) { /* 8..126: no subnormal, and no 0x7F -- which is E4M3's NaN, and which a comparison * that lets NaN through never noticed. */ @@ -7113,11 +7127,11 @@ static void moe_w4_gemm(const Plugin& pl, int64_t Tr, const MoeW4Shape& sh, int diff == 0, std::to_string(diff) + " of " + std::to_string(Tr * N) + " elements differ"); } - /* The aligned form under R4D_MOE_PF2: the parallel map prefix always, the 256-column down - * tile where the sorted operand's every class is whole tiles of it. */ + /* The aligned form's two column tiles on the down projection, where the sorted operand's every + * class is whole 256-column tiles; elsewhere the 256-column pin is refused. */ const bool dn_wide = sh.sorted_a && sh.parts == 1 && sh.N0 % 256 == 0 && sh.N1 % 256 == 0; - moe_pf2_aligned(pl, r, a, d_y, d_map, map_bytes, (size_t)(Tr * N), two_cls[1], dn_wide, ne, - what); + moe_aligned_down_tile(pl, r, a, d_y, d_map, map_bytes, (size_t)(Tr * N), two_cls[1], dn_wide, + ne, what); /* The decode form's staged K both ways, where this shape can take two blocks a pass -- * never on the sorted operand, which the rule keeps at one. */ const bool fk2 = !sh.sorted_a && g64 && sh.K0 % 256 == 0 && sh.K1 % 256 == 0; @@ -7185,8 +7199,8 @@ static void moe_w4_gemm(const Plugin& pl, int64_t Tr, const MoeW4Shape& sh, int ok(label.c_str(), diff == 0, std::to_string(diff) + " of " + std::to_string(Tr * N) + " elements differ from the two-class op's"); } - moe_pf2_aligned(pl, r4, a4, d_y, d_map, map_bytes, (size_t)(Tr * N), two_cls[1], dn_wide, ne, - what4); + moe_aligned_down_tile(pl, r4, a4, d_y, d_map, map_bytes, (size_t)(Tr * N), two_cls[1], + dn_wide, ne, what4); moe_dec_variants(pl, r4, a4, d_y, (size_t)(Tr * N), fk2, what4); } @@ -7296,11 +7310,12 @@ static void moe_q4_gemm(const Plugin& pl, int64_t Tr, const MoeQ4Shape& sh, cons } /* moe_w4_gemm's activation and routing: E4M3 bytes with no subnormal and no NaN, an f32 scale - * a (row, group), an uneven run on each expert and a dropped tail. */ + * a (row, group) that varies along both, an uneven run on each expert and a dropped tail. */ std::vector aq((size_t)(M * K)); std::vector as((size_t)(M * kb)); for (int64_t m = 0; m < M; ++m) { - for (int64_t g = 0; g < kb; ++g) as[(size_t)(m * kb + g)] = 0.011f + 0.003f * (float)g; + for (int64_t g = 0; g < kb; ++g) + as[(size_t)(m * kb + g)] = 0.011f + 0.003f * (float)g + 0.0007f * (float)(m % 11); for (int64_t k = 0; k < K; ++k) { const uint32_t c = (uint32_t)((m * 13 + k * 5) % 119) + 8; aq[(size_t)(m * K + k)] = (uint8_t)(((m + k) & 1) ? (c | 0x80u) : c); @@ -7412,7 +7427,7 @@ static void moe_q4_gemm(const Plugin& pl, int64_t Tr, const MoeQ4Shape& sh, cons } const int f_lo = pl.pin_moe ? 1 : 0, f_hi = pl.pin_moe ? 3 : 0; - std::vector aligned; /* the aligned form's bytes, for the R4D_MOE_PF2 pass */ + std::vector aligned; /* the aligned form's bytes, for the column-tile pass */ for (int form = f_lo; form <= f_hi; ++form) { const std::string label = what + " " + kForm[form]; if (pl.pin_moe) pl.pin_moe(form); @@ -7451,9 +7466,9 @@ static void moe_q4_gemm(const Plugin& pl, int64_t Tr, const MoeQ4Shape& sh, cons " elements off, worst " + std::to_string(worst) + first); if (form == 1) aligned = got; } - /* The aligned form under R4D_MOE_PF2, each class at its own geometry; fp8a8 runs W4 0 here, - * the token operand, so no 256-column down tile. */ - moe_pf2_aligned(pl, r, a, d_y, d_map, map_bytes, (size_t)(Tr * N), aligned, false, ne, what); + /* The token operand takes no 256-column down tile, and the pin to it is refused. */ + moe_aligned_down_tile(pl, r, a, d_y, d_map, map_bytes, (size_t)(Tr * N), aligned, false, ne, + what); /* The decode form's variants, each class at its own geometry; fp8a8 runs W4 0 here. */ bool fk2 = g64; for (int q = 0; q < 4; ++q) fk2 = fk2 && sh.K[q] % 256 == 0; @@ -7501,7 +7516,8 @@ static void moe_route(int64_t M, int64_t top_k, int64_t ne, uint64_t seed, bool } /* THE SORTED OPERAND -- the down projection -- AT DECODE, every form against a host decode, and - * moe_down_dec_kernel (R4D_MOE_DOWN2) against the (tile, run) form BYTE FOR BYTE. + * moe_down_dec_kernel, which the rule gives the decode form here, against the (tile, run) form + * BYTE FOR BYTE. * * moe_q4_gemm reads the activation in token order; down reads it in sorted order, one row a routed * slot, so that is the operand here, with a routing shaped like a decode step's (moe_route): at @@ -7511,11 +7527,15 @@ static void moe_route(int64_t M, int64_t top_k, int64_t ne, uint64_t seed, bool * several column groups, three of them uneven over the output's 64-column tiles; and the (tile, * run) form itself against the uniform prefill grid, byte for byte, on the sorted-operand path the * token-order cases never take. Every pinned variant is DESCRIBED first where the row can be: one - * that named the reference's kernel would match it trivially. */ + * that named the reference's kernel would match it trivially. + * + * AND AN OUTPUT THAT NAMES FEWER ROWS THAN ITS BUFFER, while the offsets still name the whole + * routing: both decode forms must write those rows as the full launch did and never a row past + * them. */ struct MoeDnShape { const char* name; int64_t N[4], K[4], ne, top_k; }; -static void moe_down2_case(const Plugin& pl, const MoeDnShape& sh, const char* dt, - std::initializer_list Ts) { +static void moe_down_dec_case(const Plugin& pl, const MoeDnShape& sh, const char* dt, + std::initializer_list Ts) { const bool fp8 = std::strcmp(dt, "fp8a8") == 0, w5 = std::strcmp(dt, "w5nl64a8h") == 0; const int64_t sub = std::strcmp(dt, "w4nl32a8h") == 0 ? 4 : (w5 || std::strcmp(dt, "w4nl64a8h") == 0) ? 2 : 0; @@ -7530,7 +7550,7 @@ static void moe_down2_case(const Plugin& pl, const MoeDnShape& sh, const char* d ok(head.c_str(), false, "row or hooks absent"); return; } - if (!pl.pin_moe || !pl.pin_dn2) { + if (!pl.pin_moe || !pl.pin_ddec) { skip(head.c_str(), "the plugin exports no form pins"); return; } @@ -7622,11 +7642,12 @@ static void moe_down2_case(const Plugin& pl, const MoeDnShape& sh, const char* d } /* One activation row a routed slot: E4M3 bytes with no subnormal and no NaN, an f32 scale - * a (row, group). */ + * a (row, group) that varies along both, so a row read under another's scale shows. */ std::vector aq((size_t)(Tr * K)); std::vector as((size_t)(Tr * kb)); for (int64_t m = 0; m < Tr; ++m) { - for (int64_t g = 0; g < kb; ++g) as[(size_t)(m * kb + g)] = 0.011f + 0.003f * (float)g; + for (int64_t g = 0; g < kb; ++g) + as[(size_t)(m * kb + g)] = 0.011f + 0.003f * (float)g + 0.0007f * (float)(m % 11); for (int64_t k = 0; k < K; ++k) { const uint32_t c = (uint32_t)((m * 13 + k * 5) % 119) + 8; aq[(size_t)(m * K + k)] = (uint8_t)(((m + k) & 1) ? (c | 0x80u) : c); @@ -7702,7 +7723,8 @@ static void moe_down2_case(const Plugin& pl, const MoeDnShape& sh, const char* d } } - /* One pinned launch into stale bytes, its output back. `pins` is (form, down2). */ + /* One pinned launch into stale bytes, its output back. `pins` is (form, the sorted + * operand's decode kernel: -1 the (tile, run) form, 0 the rule, n its column tiles). */ auto run = [&](const int (&pins)[2], std::vector* got) { { std::vector junk((size_t)(Tr * N), 0x7f7fu); @@ -7711,11 +7733,11 @@ static void moe_down2_case(const Plugin& pl, const MoeDnShape& sh, const char* d a.scratch = pins[0] == 3 ? nullptr : d_map; a.scratch_bytes = pins[0] == 3 ? 0 : map_bytes; pl.pin_moe(pins[0]); - pl.pin_dn2(pins[1]); + pl.pin_ddec(pins[1]); const int rc = r->launch(&a, nullptr); st_stream_sync(nullptr); pl.pin_moe(0); - pl.pin_dn2(0); + pl.pin_ddec(0); got->assign((size_t)(Tr * N), 0); st_memcpy(got->data(), d_y, got->size() * 2, hipMemcpyDeviceToHost); return rc; @@ -7727,10 +7749,10 @@ static void moe_down2_case(const Plugin& pl, const MoeDnShape& sh, const char* d a.scratch = pins[0] == 3 ? nullptr : d_map; a.scratch_bytes = pins[0] == 3 ? 0 : map_bytes; pl.pin_moe(pins[0]); - pl.pin_dn2(pins[1]); + pl.pin_ddec(pins[1]); const int drc = r->describe(&a, nullptr, d, 4, &n); pl.pin_moe(0); - pl.pin_dn2(0); + pl.pin_ddec(0); return drc == RAD_OK && n >= 1 ? d[n - 1].func : nullptr; }; @@ -7776,15 +7798,18 @@ static void moe_down2_case(const Plugin& pl, const MoeDnShape& sh, const char* d " elements differ"); } - /* The sorted operand's own decode kernel at several column groups, each against the - * (tile, run) form's bytes. */ - struct Var { int dn2; const char* name; }; + /* The sorted operand's own decode kernel under the rule and at several column groups -- + * sixteen is the cap, past the output's five tiles here -- each against the (tile, run) + * form's bytes. */ + struct Var { int dn; const char* name; }; static const Var kVar[] = { + { 0, "down kernel, the rule's column tiles" }, { 1, "down kernel, one column tile a block" }, { 2, "down kernel, two column tiles a block" }, { 3, "down kernel, three column tiles a block" }, { 4, "down kernel, four column tiles a block" }, { 8, "down kernel, eight column tiles a block" }, + { 16, "down kernel, sixteen column tiles a block" }, }; const int ref_pins[2] = { 2, -1 }; const void* fn0 = described(ref_pins); @@ -7793,7 +7818,7 @@ static void moe_down2_case(const Plugin& pl, const MoeDnShape& sh, const char* d std::to_string(nrun) + " runs, longest " + std::to_string(longest) + ")" + (r->describe ? "" : " (launch not described)"); - const int pins[2] = { 2, v.dn2 }; + const int pins[2] = { 2, v.dn }; if (r->describe) { const void* fn = described(pins); if (!fn || fn == fn0) { @@ -7810,6 +7835,33 @@ static void moe_down2_case(const Plugin& pl, const MoeDnShape& sh, const char* d ok(label.c_str(), diff == 0, std::to_string(diff) + " of " + std::to_string(Tr * N) + " elements differ from the (tile, run) form"); } + + /* THE OUTPUT SEVEN ROWS SHORT: Y declared at Tr - 7 rows over the same buffer, the + * offsets unchanged, so the routing names rows past the output's last. Each decode form + * must write the rows it has exactly as the full launch did and leave the rest stale. */ + { + const int64_t Tv = Tr - 7; + ts[12] = T(d_y, RAD_BF16, { Tv, N }); + for (const int dn : { -1, 0 }) { + const std::string label = what + " decode form, " + + (dn < 0 ? "(tile, run)" : "down kernel") + + ", an output " + std::to_string(Tv) + " rows long, " + + std::to_string(off[(size_t)ne]) + " routed"; + const int pins[2] = { 2, dn }; + std::vector got; + const int rc = run(pins, &got); + if (rc != RAD_OK) { ok(label.c_str(), false, "launch rc=" + std::to_string(rc)); continue; } + int64_t diff = 0, past = 0; + for (int64_t i = 0; i < Tr * N; ++i) { + if (i < Tv * N) diff += got[(size_t)i] != base[(size_t)i]; + else past += got[(size_t)i] != 0x7f7fu; + } + ok(label.c_str(), diff == 0 && past == 0, + std::to_string(diff) + " of the output's elements differ from the full launch, " + + std::to_string(past) + " written past its last row"); + } + ts[12] = T(d_y, RAD_BF16, { Tr, N }); + } } } @@ -9336,51 +9388,6 @@ static void qsa_score_rows_case(const Plugin& pl) { * block's worth of angle, which is the mistake a reader makes when they take the position of the * block's LAST token instead of its first, and it is invisible in magnitude: the vectors stay the * same length and point somewhere else. rel_l2 is what sees it. */ -static void qsa_block_key_yarn_case(const Plugin& pl) { - const char* what = "qsa_block_key YaRN vs independent ROCm vectors through 524287"; - const RadKernelInfo* r = find(pl, "qsa_block_key_bf16"); - if (!r) { ok(what, false, "row absent"); return; } - const std::string here = __FILE__; - std::ifstream input(here.substr(0, here.find_last_of('/')) + "/../tests/data/yarn_reference_rocm.json"); - if (!input) { ok(what, false, "fixture absent"); return; } - const auto fixture = nlohmann::json::parse(input); - const auto positions = fixture["positions"].get>(); - const auto& c = fixture["cases"][0]; - const int64_t M = positions.size(), n = 128, ratio = 4; - /* Identical unit keys make the block mean exactly one. Unit gain and eps=0 - * make the norm exactly one too, isolating the compressed-key rotation. */ - std::vector keys(M * ratio * n, h_to_bf16(1.0f)); - std::vector gains(n, 1.0f); - void* dk = dalloc(keys.size()*2), *dw = dalloc(gains.size()*4); - void* dp = dalloc(M*4), *dout = dalloc(M*n*2); - st_memcpy(dk, keys.data(), keys.size()*2, hipMemcpyHostToDevice); - st_memcpy(dw, gains.data(), gains.size()*4, hipMemcpyHostToDevice); - st_memcpy(dp, positions.data(), M*4, hipMemcpyHostToDevice); - std::vector ts = {T(dk, RAD_BF16, {M*ratio,n}), T(dw, RAD_F32, {n}), - T(nullptr, RAD_I32, {M}), T(dp, RAD_I32, {M}), T(dout, RAD_BF16, {M,n})}; - std::vector ps = {PI("M",M), PI("n",n), PI("ratio",ratio), PI("rotary_dim",64), - PF("eps",0), PF("theta",1e7), PF("scale",1), PF("wadd",0), PS("dtype","bf16"), - PF("yarn_factor",2), PF("yarn_low",14), PF("yarn_high",22), PF("yarn_magnitude",1.069314718)}; - RadArgs a{}; a.t=ts.data(); a.n_t=ts.size(); a.p=ps.data(); a.n_p=ps.size(); - bool passed = r->launch(&a,nullptr) == RAD_OK; - st_stream_sync(nullptr); - std::vector got(M*n); - st_memcpy(got.data(),dout,got.size()*2,hipMemcpyDeviceToHost); - double worst=0; - for (int64_t m=0;m keys((size_t)(M * ratio * n), h_to_bf16(1.0f)); + std::vector gains((size_t)n, 1.0f); + void* dk = dalloc(keys.size() * 2), *dw = dalloc(gains.size() * 4); + void* dp = dalloc((size_t)M * 4), *dout = dalloc((size_t)(M * n * 2)); + st_memcpy(dk, keys.data(), keys.size() * 2, hipMemcpyHostToDevice); + st_memcpy(dw, gains.data(), gains.size() * 4, hipMemcpyHostToDevice); + st_memcpy(dp, kYarnPos, (size_t)M * 4, hipMemcpyHostToDevice); + std::vector ts = { T(dk, RAD_BF16, { M * ratio, n }), T(dw, RAD_F32, { n }), + T(nullptr, RAD_I32, { M }), T(dp, RAD_I32, { M }), + T(dout, RAD_BF16, { M, n }) }; + std::vector ps = { PI("M", M), PI("n", n), PI("ratio", ratio), PI("rotary_dim", rot), + PF("eps", 0), PF("theta", 1e7), PF("scale", 1), PF("wadd", 0), + PS("dtype", "bf16"), PF("yarn_factor", 2), + PF("yarn_low", yarn_low(rot)), PF("yarn_high", yarn_high(rot)), + PF("yarn_magnitude", kYarnMag) }; + RadArgs a{}; a.t = ts.data(); a.n_t = ts.size(); a.p = ps.data(); a.n_p = ps.size(); + bool passed = r->launch(&a, nullptr) == RAD_OK; + st_stream_sync(nullptr); + std::vector got((size_t)(M * n)); + st_memcpy(got.data(), dout, got.size() * 2, hipMemcpyDeviceToHost); + double worst = 0.0; + for (int64_t m = 0; m < M; ++m) + for (int64_t i = 0; i < n; ++i) { + double want = 1.0, tol = 0.0; + if (i < rot) { + const YarnRef y = yarn_ref(kYarnPos[m], (int)(i % (rot / 2)), (int)rot, 1e7, 2.0, + yarn_low(rot), yarn_high(rot), kYarnMag); + want = i < rot / 2 ? y.c - y.s : y.c + y.s; + tol = 2.0 * y.tol; + } + tol += std::fabs(want) * 0x1p-8 + 1e-6; + const double have = h_bf16(got[(size_t)(m * n + i)]); + passed = passed && std::isfinite(have); + worst = std::max(worst, std::fabs(have - want) / tol); + } + char note[64]; + std::snprintf(note, sizeof note, "worst %.3g of the bf16 + f32 bound", worst); + ok(what, passed && worst <= 1.0, note); +} + /* gram_accum, against an f64 host Gram of the same dequantised bytes. * * THE THING THIS IS ACTUALLY FOR IS THE ACCUMULATE. A kernel that stored instead of adding would @@ -11804,8 +11861,12 @@ static void device_phase(const Plugin& pl) { { "640 experts, gate/up 2:1, K 256", 256, 256, 128, 256, 2, 640 }, /* The down projection as a rank of four runs it: the sorted operand, one part, the * classes at 256 and 128 of K -- and a width of whole 256-column tiles, which is what - * R4D_MOE_PF2's wide down tile takes. */ + * the aligned form's 256-column down tile takes. Then with the odd class half as wide, + * so that tile is padding for every odd expert's run; and at 512 experts, whose + * n_expert + 1 block counts the run map scans in three 256-count tiles. */ { "down, sorted rows, 512 x K 256:128", 512, 256, 512, 128, 1, 10, true }, + { "down, sorted rows, 512:256 x K 256:128", 512, 256, 256, 128, 1, 10, true }, + { "512 experts, down, sorted rows, 256 x K 128", 256, 128, 256, 128, 1, 512, true }, }; for (const MoeW4Shape& sh : kShapes) { moe_w4_gemm(pl, 24, sh); @@ -11833,6 +11894,8 @@ static void device_phase(const Plugin& pl) { { "640 experts, gate/up 2:1, K 256", 256, 256, 128, 256, 2, 640 }, { "down, sorted rows, 512 x K 256:128", 512, 256, 512, 128, 1, 10, true }, { "40 experts, down, sorted rows, 256 x K 128", 256, 128, 256, 128, 1, 40, true }, + { "down, sorted rows, 512:256 x K 256:128", 512, 256, 256, 128, 1, 10, true }, + { "512 experts, down, sorted rows, 256 x K 128", 256, 128, 256, 128, 1, 512, true }, }; for (const MoeW4Shape& sh : kShapes4) for (const int64_t t : { (int64_t)24, (int64_t)200, (int64_t)600 }) { @@ -11846,11 +11909,12 @@ static void device_phase(const Plugin& pl) { for (const int64_t t : { (int64_t)24, (int64_t)200, (int64_t)600 }) for (const char* dt : { "w4a8", "w4nl64a8h", "w4nl32a8h", "w5nl64a8h" }) moe_q4_gemm(pl, t, sh, dt); - /* The sorted operand at decode, and its own kernel (R4D_MOE_DOWN2) against the (tile, run) - * form: one sequence at depth 3 (T 40), five, sixteen and nineteen. */ - moe_down2_case(pl, kMoeDnShapes[0], "w4nl64a8h", { 40, 200, 640, 760 }); + /* The sorted operand at decode, and its own kernel against the (tile, run) form: one + * sequence at depth 3 (T 40), five, sixteen and nineteen -- each side of both of the + * rule's column-tile thresholds. */ + moe_down_dec_case(pl, kMoeDnShapes[0], "w4nl64a8h", { 40, 200, 640, 760 }); for (const char* dt : { "w4a8", "w4nl64a8h", "w4nl32a8h", "w5nl64a8h" }) - moe_down2_case(pl, kMoeDnShapes[1], dt, { 40, 96 }); + moe_down_dec_case(pl, kMoeDnShapes[1], dt, { 40, 96 }); } if (sel("moe_gemm_fp8a8_mod4")) { @@ -11859,7 +11923,7 @@ static void device_phase(const Plugin& pl) { for (const MoeQ4Shape& sh : kMoeQ4Shapes) for (const int64_t t : { (int64_t)24, (int64_t)200, (int64_t)600 }) moe_q4_gemm(pl, t, sh, "fp8a8"); - moe_down2_case(pl, kMoeDnShapes[1], "fp8a8", { 40, 96 }); + moe_down_dec_case(pl, kMoeDnShapes[1], "fp8a8", { 40, 96 }); } if (sel("hc_read_e4m3")) { diff --git a/libref/ref_attn.cpp b/libref/ref_attn.cpp index f0eab16b..6e90a119 100644 --- a/libref/ref_attn.cpp +++ b/libref/ref_attn.cpp @@ -600,3 +600,90 @@ int ref_rope_table(const RadArgs* a, RadStream) { } return RAD_OK; } + +/* ------------------------------------------------------------------ qsa_block_key */ +/* The QSA indexer's compressed key (docs/OPS.md, docs/QSA.md): for each of M blocks, + * + * pooled = mean of the block's `ratio` rows of k, accumulated in f32 and ROUNDED TO k's dtype + * normed = pooled * rsqrt(mean(pooled^2) + eps) * (wadd + w) + * out = NeoX rotary of normed's first `rotary_dim` lanes at the block's position, the rest + * passed through, rounded once at the store + * + * The rounding after the mean is the reference model's (`.float().mean(dim=1).to(dtype)`) and is + * part of the answer. The block's position is `pos[b]` (a [>= M, 1 or 3] list carries a block's + * components side by side, read under `mode` mrope/imrope and `sections`), or `pos0 + b*ratio` + * without one. `page[b]`, when given, is the output row, and a negative entry writes nothing. The + * rotary is `rope`'s ladder with YaRN's blend and magnitude when the coefficients are given. */ +int ref_qsa_block_key(const RadArgs* a, RadStream) { + const RadTensor* k = t_in(a, 0); + const RadTensor* w = t_in(a, 1); + const RadTensor* page = t_in(a, 2); + const RadTensor* pos = t_in(a, 3); + const RadTensor* out = t_in(a, 4); + if (!k || !w || !out) return RAD_E_INVAL; + const int64_t n = p_int(a, "n", 0), ratio = p_int(a, "ratio", 0); + const int64_t rot = p_int(a, "rotary_dim", 0), pos0 = p_int(a, "pos0", -1); + if (n <= 0 || n > REF_HD_MAX || ratio <= 0 || (pos0 < 0 && !pos)) return RAD_E_INVAL; + if (rot < 0 || rot > n || (rot & 1)) return RAD_E_SHAPE; + if (p_int(a, "block_size", ratio) != ratio) return RAD_E_SHAPE; + if (k->rank != 2 || out->rank < 2 || out->shape[0] < 1) return RAD_E_SHAPE; + const int64_t M = page ? numel(page) : out->shape[0]; + if (M < 1 || numel(out) / out->shape[0] != n) return RAD_E_SHAPE; + if (k->shape[0] != M * ratio || k->shape[1] != n || numel(w) < n) return RAD_E_SHAPE; + const bool pos2 = pos && pos->rank == 2; + if (pos && (pos2 ? pos->shape[0] < M : numel(pos) < M)) return RAD_E_SHAPE; + + const float eps = p_f32(a, "eps", 1e-6f), wadd = p_f32(a, "wadd", 1.0f); + const float theta = p_f32(a, "theta", 10000.0f), rscale = p_f32(a, "scale", 1.0f); + static const char* kModes[] = { "neox", "mrope", "imrope" }; + const int mi = p_enum(a, "mode", kModes, 3, 0); + if (mi < 0) return RAD_E_UNSUPPORTED; + const int mode = mi == 1 ? ROPE_MROPE : mi == 2 ? ROPE_IMROPE : ROPE_NEOX; + int64_t sec[8] = {}; + const int n_sec = rope_sections(a, sec); + if (n_sec < 0 || n_sec > 3) return RAD_E_SHAPE; + /* Several components only with a mode, its sections and three columns of `pos`; otherwise + * every frequency reads the block's one position, which every mode reads as NeoX. */ + const bool mc = mode != ROPE_NEOX && n_sec > 0 && pos2 && pos->shape[1] >= 3; + const RadYarn yarn = rad_yarn_args(a); + if (!rad_yarn_valid(yarn) || (yarn.factor != 0.0f && (rot <= 0 || rscale != 1.0f))) + return RAD_E_INVAL; + RopeFreq f; + RAD_REF_TRY(rope_freqs(mc ? mode : ROPE_NEOX, rot, theta, sec, mc ? n_sec : 0, &f)); + for (int64_t i = 0; i < rot / 2; ++i) f.inv[i] = yarn.frequency(f.inv[i], (int)i); + + #pragma omp parallel for schedule(static) + for (int64_t b = 0; b < M; ++b) { + const int64_t orow = page ? ld_int(page->dtype, page->data, offlin(page, b)) : b; + if (orow < 0) continue; + float x[REF_HD_MAX], y[REF_HD_MAX]; + double ss = 0.0; + for (int64_t t = 0; t < n; ++t) { + float acc = 0.0f; + for (int64_t j = 0; j < ratio; ++j) acc += ldt(k, offlin(k, (b * ratio + j) * n + t)); + uint64_t tmp = 0; + st_dt(k->dtype, &tmp, 0, acc / (float)ratio); + x[t] = ld_dt(k->dtype, &tmp, 0); + ss += (double)x[t] * x[t]; + } + const float inv = 1.0f / std::sqrt((float)(ss / (double)n) + eps); + for (int64_t t = 0; t < n; ++t) y[t] = x[t] * inv * (wadd + ldt(w, offlin(w, t))); + float p[3] = {}; + for (int c = 0; c < (mc ? 3 : 1); ++c) { + const int64_t bp = !pos ? pos0 + b * ratio + : ld_int(pos->dtype, pos->data, + offlin(pos, pos2 ? b * pos->shape[1] + c : b)); + p[c] = (float)bp / (rscale != 0.0f ? rscale : 1.0f); + } + const int64_t half = rot / 2; + for (int64_t i = 0; i < half; ++i) { + const float ang = p[f.comp[i]] * f.inv[i]; + const float c = std::cos(ang) * yarn.magnitude, s = std::sin(ang) * yarn.magnitude; + const float x0 = y[i], x1 = y[half + i]; + y[i] = x0 * c - x1 * s; + y[half + i] = x0 * s + x1 * c; + } + for (int64_t t = 0; t < n; ++t) stt(out, offlin(out, orow * n + t), y[t]); + } + return RAD_OK; +} diff --git a/libref/ref_ops.h b/libref/ref_ops.h index 527382d8..b076923c 100644 --- a/libref/ref_ops.h +++ b/libref/ref_ops.h @@ -115,6 +115,7 @@ int ref_kv_store (const RadArgs*, RadStream); int ref_rope (const RadArgs*, RadStream); int ref_rope_table (const RadArgs*, RadStream); int ref_qk_norm_rope (const RadArgs*, RadStream); +int ref_qsa_block_key (const RadArgs*, RadStream); /* ---- mixture of experts */ int ref_router_topk (const RadArgs*, RadStream); @@ -229,6 +230,7 @@ REF_SHAPE_FN(ref_kv_store); REF_SHAPE_FN(ref_rope); REF_SHAPE_FN(ref_rope_table); REF_SHAPE_FN(ref_qk_norm_rope); +REF_SHAPE_FN(ref_qsa_block_key); REF_SHAPE_FN(ref_router_topk); REF_SHAPE_FN(ref_moe_scatter); diff --git a/libref/ref_registry.cpp b/libref/ref_registry.cpp index f159fc34..116c3cd3 100644 --- a/libref/ref_registry.cpp +++ b/libref/ref_registry.cpp @@ -292,6 +292,18 @@ static const RadParamSpec pRopeTable[] = { P_INT("M"), P_INT("rot"), P_F64("thet P_F64O("yarn_high"), P_F64O("yarn_magnitude") }; static const RadOperandSpec oRopeTable[] = { OPD("positions"), OUT("cos_sin") }; +/* QSA's compressed key, libr4d's schema exactly (libr4d/r4d_rows.cpp): `block_size` is `ratio` + * spelled for the KV group the block-key store is, `page` and `pos` are optional. */ +static const RadParamSpec pQsaBlockKey[] = { P_INT("M"), P_INT("n"), P_INT("ratio"), + P_INT("rotary_dim"), P_INTO("pos0"), + P_INTO("block_size"), P_F64("eps"), P_F64("theta"), + P_F64O("scale"), P_F64O("wadd"), P_STR("dtype"), + P_STRO("mode"), P_STRO("sections"), + P_F64O("yarn_factor"), P_F64O("yarn_low"), + P_F64O("yarn_high"), P_F64O("yarn_magnitude") }; +static const RadOperandSpec oQsaBlockKey[] = { OPD("k"), WGT("w"), OPD_O("page"), OPD_O("pos"), + OUT("out") }; + static const RadParamSpec pQkNormRope[] = { P_INT("M"), P_INT("head_dim"), P_INT("n_head"), P_INT("n_head_kv"), P_F64("theta"), P_F64("eps"), P_F64O("scale"), P_STRO("mode"), P_INTO("rotary_dim"), @@ -751,6 +763,12 @@ static const RadOpSchema kSchemas[] = { "cos/sin for the positions a step names, at each position's own row of a [max_ctx, rot] plane: " "rot values a row, cos for the first half and sin for the second. The angles are `rope`'s " "exactly. A row no position names is not written; a negative position writes nothing." }, +{ "qsa_block_key", ARR(pQsaBlockKey), ARR(oQsaBlockKey), + "QSA's compressed key, one vector per `ratio` rows of k: the block's mean accumulated in f32 and " + "rounded to k's dtype, RMS-normed with gain (wadd + w), then NeoX-rotated over `rotary_dim` at " + "the block's position -- `pos[b]` (with `mode` mrope/imrope and `sections`, a [>= M, 3] list's " + "three components) or `pos0 + b*ratio` -- with YaRN's blend and magnitude when given. `page[b]`, " + "when given, is the output row; a negative entry writes nothing." }, { "rope", ARR(pRope), ARR(oRope), "Rotary embedding in place over the q and k heads of a fused [q|k|v] row; the v heads are left " "alone. `mode` is \"neox\" or \"gptj\", or a multi-component form: \"mrope\" (contiguous " @@ -1084,6 +1102,9 @@ SIMPLE("ref_moe_gemm_q_mod4", "moe_gemm_q_mod4", "moe", SIMPLE("ref_moe_gather", "moe_gather", "moe", "weighted scatter back to token order", DT_FLOAT, ref_moe_gather), SIMPLE("ref_rope_table", "rope_table", "attn", "cos/sin at each position's own row", DT_FLOAT, ref_rope_table), +SIMPLE("ref_qsa_block_key", "qsa_block_key", "attn", + "QSA compressed key: pooled, normed and rotated at the block's position", DT_FLOAT, + ref_qsa_block_key), SIMPLE("ref_row_topk", "row_topk", "moe", "exact per-row top-R", DT_FLOAT, ref_row_topk), SIMPLE("ref_row_topk_merge", "row_topk_merge", "moe", "the global top-R of the gathered per-rank candidate sets", DT_FLOAT, ref_row_topk_merge), SIMPLE("ref_logit_rerank", "logit_rerank", "moe", "R candidates a row rescored exactly against the full-precision head", DT_FLOAT, ref_logit_rerank), diff --git a/libref/ref_shapes.cpp b/libref/ref_shapes.cpp index 5885ed99..9ae0a7e2 100644 --- a/libref/ref_shapes.cpp +++ b/libref/ref_shapes.cpp @@ -1040,6 +1040,23 @@ int ref_rope_table_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* ou return o.finish("rope_table", operand, out); } +int ref_qsa_block_key_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) { + const int64_t M = gi(p, n_p, "M", 0); + const int64_t n = gi(p, n_p, "n", 0); + const int64_t r = gi(p, n_p, "ratio", 0); + if (M <= 0 || n <= 0 || r <= 0) return RAD_E_UNSUPPORTED; + const uint32_t D = act_dt(p, n_p); + Opd o; + o.t(D, { M * r, n }); /* k -- `ratio` rows a block */ + o.t((uint32_t)RAD_F32, { n }); /* w -- the norm gain */ + /* page: absent, so the write is contiguous. A drawn page list may repeat an entry, and two + * blocks onto one row is a race whose winner the comparison would then measure. */ + o.skip(); + o.t_idx({ M }, M * r, -1, 0); /* pos -- each block's first-token position */ + o.t(D, { M, n }); /* out */ + return o.finish("qsa_block_key", operand, out); +} + /* ================================================================== mixture of experts */ int ref_scale_rows_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) { diff --git a/tests/arch_test.cpp b/tests/arch_test.cpp index de28e3fb..1540f514 100644 --- a/tests/arch_test.cpp +++ b/tests/arch_test.cpp @@ -306,6 +306,8 @@ extern "C" void* rad_buf_ptr (RadCtx* c, rad_buf b) { (void)c; return (void #include "../arch/qwen35_bf16/qwen35_bf16.cpp" /* And the production architecture, for its sizing declare (RadBuildCtx::shape_probe). */ #include "../arch/qwen4exp_fp8/qwen4exp_fp8.cpp" +/* And the LLaMA family, for how it reads its rotary scaling. */ +#include "../arch/llama_fp8/llama_fp8.cpp" /* The PLE block on its own: no architecture in this file declares it, and what its step() hands * the two windowed ops is a contract of its own (rad_block_ple.h). */ #include @@ -578,6 +580,58 @@ TEST(qwen35_bf16_yarn_reaches_every_text_rotary_op) { CHECK_EQ(qwen35_bf16::declare(&invalid, &meta, &ctx), RAD_E_INVAL); } +/* THE LLAMA FAMILY READS ITS ROTARY SCALING THROUGH THE SAME READER. A Llama 3.1 checkpoint says + * `rope_type: llama3` with a factor of 8; read as a bare factor that is a linear position divide + * by 8 and fluent nonsense, so it is refused by name at declare. Linear stays linear and YaRN + * reaches the rope op. */ +TEST(llama_reads_its_rotary_scaling_by_type) { + (void)&llama_fp8::step; /* included for its declare; nothing here runs a step */ + (void)&llama_fp8::probe; + auto declare = [](const char* type, const char* factor, RadBuilder* b) { + const char* keys[] = {"rope_scaling.rope_type", "rope_scaling.factor", + "rope_scaling.original_max_position_embeddings"}; + const char* vals[] = {type, factor, "8192"}; + RadModelMeta m{}; + m.arch_id = "llama"; m.name = "test-llama"; m.quant = "fp8_e4m3"; + m.n_layers = 2; m.n_embd = 1024; m.n_head = 8; m.n_head_kv = 2; m.head_dim = 128; + m.n_ff = 2048; m.n_vocab = 32000; m.n_ctx_train = 8192; + m.rms_eps = 1e-5f; m.rope_theta = 5e5f; m.rope_scale = 1.0f; + m.n_kv = type ? 3 : 0; m.kv_key = keys; m.kv_val = vals; + RadBuildCtx c = build_ctx(); + return llama_fp8::declare(b, &m, &c); + }; + for (const char* refused : {"llama3", "longrope", "dynamic"}) { + RadBuilder b; + CHECK_EQ(declare(refused, "8", &b), RAD_E_UNSUPPORTED); + } + { + RadBuilder b; + CHECK_OK(declare("linear", "2", &b)); + CHECK(count_op(b, "rope") > 0); + for (const auto& op : b.ops) if (op.op == "rope") { + CHECK_EQ(pflt(&op, "scale"), 2.0); + CHECK(!param(&op, "yarn_factor")); + } + } + { + RadBuilder b; + CHECK_OK(declare("yarn", "4", &b)); + CHECK(count_op(b, "rope") > 0); + for (const auto& op : b.ops) if (op.op == "rope") { + CHECK_EQ(pflt(&op, "scale"), 1.0); + CHECK_EQ(pflt(&op, "yarn_factor"), 4.0); + } + } + { + RadBuilder b; + CHECK_OK(declare(nullptr, "", &b)); + for (const auto& op : b.ops) if (op.op == "rope") { + CHECK_EQ(pflt(&op, "scale"), 1.0); + CHECK(!param(&op, "yarn_factor")); + } + } +} + TEST(the_alternation_puts_full_attention_at_3_and_7) { RadBuilder b; RadModelMeta meta = qwen35_meta(); diff --git a/tests/config_test.cpp b/tests/config_test.cpp index f14a3941..50f81d3b 100644 --- a/tests/config_test.cpp +++ b/tests/config_test.cpp @@ -362,6 +362,28 @@ TEST(the_table_placements_are_auto_vram_ram_or_disk) { CHECK_EQ(parse({ "--ngram-placement" }, &c), RAD_E_INVAL); } +/* --draft-window takes its two names and nothing else; the default is kDraftWindowDefault, and the + * help text states it from there. Whether the window CAN adapt -- a serial drafter, a depth above + * zero -- is the scheduler's to say with the program in hand, not the parser's. */ +TEST(the_draft_window_is_fixed_or_adaptive) { + Config d; + CHECK_EQ(parse({}, &d), RAD_OK); + CHECK(d.draft_window == kDraftWindowDefault); + Config c; + CHECK_EQ(parse({ "--draft-window", "adaptive" }, &c), RAD_OK); + CHECK(c.draft_window == DraftWindow::Adaptive); + CHECK_EQ(parse({ "--draft-window", "fixed" }, &c), RAD_OK); + CHECK(c.draft_window == DraftWindow::Fixed); + CHECK_EQ(parse({ "--draft-window", "adaptive", "--num-speculative-tokens", "6" }, &c), RAD_OK); + CHECK(c.draft_window == DraftWindow::Adaptive); + CHECK_EQ(c.n_spec, 6); + for (const char* bad : { "Adaptive", "auto", "dynamic", "1", "" }) + CHECK_EQ(parse({ "--draft-window", bad }, &c), RAD_E_INVAL); + CHECK_EQ(parse({ "--draft-window" }, &c), RAD_E_INVAL); + CHECK_EQ(std::string(draft_window_name(DraftWindow::Adaptive)), std::string("adaptive")); + CHECK_EQ(std::string(draft_window_name(DraftWindow::Fixed)), std::string("fixed")); +} + /* A POLICY NAME THIS BUILD DOES NOT KNOW WOULD FALL THROUGH TO THE DEFAULT, and a measurement * comparing the default against itself is the failure that costs the most to notice. */ TEST(the_checkpoint_policy_is_one_of_the_three_that_exist) { @@ -808,6 +830,9 @@ TEST(usage_names_every_flag_it_documents) { CHECK(text.find("--host-pool-mib") != std::string::npos); CHECK(text.find("--kv-cache-dtype") != std::string::npos); CHECK(text.find("--num-speculative-tokens") != std::string::npos); + CHECK(text.find("--draft-window") != std::string::npos); + CHECK(text.find(std::string("Default ") + draft_window_name(kDraftWindowDefault)) != + std::string::npos); CHECK(text.find("--checkpoint-policy") != std::string::npos); CHECK(text.find("--prefix-cache-host-mib") != std::string::npos); CHECK(text.find("--tp-wire") != std::string::npos); @@ -824,8 +849,6 @@ TEST(usage_names_every_flag_it_documents) { CHECK(text.size() > 1000); } -RAD_TEST_MAIN() - /* --rope-scaling is checked at the command line: a typo is a refusal, not a model served with its * trained rotary at a length it cannot reach. */ TEST(rope_scaling_takes_yarn_with_a_factor_above_one_or_none) { @@ -843,3 +866,5 @@ TEST(rope_scaling_takes_yarn_with_a_factor_above_one_or_none) { CHECK_EQ(parse({}, &d), RAD_OK); CHECK(d.rope_scaling.empty()); } + +RAD_TEST_MAIN() diff --git a/tests/data/generate_yarn_reference.py b/tests/data/generate_yarn_reference.py deleted file mode 100644 index 6cc9c3a3..00000000 --- a/tests/data/generate_yarn_reference.py +++ /dev/null @@ -1,27 +0,0 @@ -"""Generate independent YaRN vectors using vLLM (Apache-2.0). - -Run in the reference environment, redirect stdout to yarn_reference.json (CPU) -or pass cuda and redirect to yarn_reference_rocm.json (ROCm/gfx1201). -This script is not a runtime dependency and does not load model weights. -""" -import json -import sys -from types import SimpleNamespace -import torch -from vllm.model_executor.layers.rotary_embedding.yarn_scaling_rope import YaRNScalingRotaryEmbedding - -positions = [0, 1, 4096, 262143, 262144, 500000, 524287] -cases = [] -device = sys.argv[1] if len(sys.argv) > 1 else 'cpu' -with torch.device(device): - for rot in (64, 128, 256): - cfg = SimpleNamespace(base=10000000.0, rotary_dim=rot, max_position_embeddings=262144, - beta_fast=32, beta_slow=1, truncate=True) - inv = YaRNScalingRotaryEmbedding._compute_inv_freq(cfg, 2.0) - phase = torch.tensor(positions, dtype=torch.float32)[:, None] * inv[None, :] - magnitude = 1.0 + 0.1 * __import__('math').log(2.0) - cases.append(dict(rot=rot, theta=cfg.base, factor=2.0, original=262144, - magnitude=magnitude, inv_freq=inv.tolist(), - cos=(phase.cos()*magnitude).tolist(), sin=(phase.sin()*magnitude).tolist())) -print(json.dumps({'reference': f'vLLM YaRNScalingRotaryEmbedding; torch {device} FP32; static factor=2', - 'positions': positions, 'cases': cases}, indent=2)) diff --git a/tests/data/yarn_reference.json b/tests/data/yarn_reference.json deleted file mode 100644 index 74637dd1..00000000 --- a/tests/data/yarn_reference.json +++ /dev/null @@ -1,3497 +0,0 @@ -{ - "reference": "vLLM YaRNScalingRotaryEmbedding; torch CPU FP32; static factor=2", - "positions": [ - 0, - 1, - 4096, - 262143, - 262144, - 500000, - 524287 - ], - "cases": [ - { - "rot": 64, - "theta": 10000000.0, - "factor": 2.0, - "original": 262144, - "magnitude": 1.0693147180559945, - "inv_freq": [ - 1.0, - 0.6042963862419128, - 0.36517414450645447, - 0.22067339718341827, - 0.1333521455526352, - 0.08058421313762665, - 0.04869675263762474, - 0.02942727319896221, - 0.017782794311642647, - 0.010746078565716743, - 0.006493816617876291, - 0.003924189601093531, - 0.0023713738191872835, - 0.0014330126577988267, - 0.0008659643353894353, - 0.0004905929090455174, - 0.00027669931296259165, - 0.0001552649337099865, - 8.660864841658622e-05, - 4.797585279447958e-05, - 2.6356034140917473e-05, - 1.4334168554341886e-05, - 7.699632078583818e-06, - 4.6528602979378775e-06, - 2.81170673588349e-06, - 1.6991041320579825e-06, - 1.0267624475090997e-06, - 6.204688816069392e-07, - 3.7494712046282075e-07, - 2.2657918918866926e-07, - 1.3692098832507327e-07, - 8.274085416815069e-08 - ], - "cos": [ - [ - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646 - ], - [ - 0.5777531862258911, - 0.8799412846565247, - 0.9988058805465698, - 1.0433841943740845, - 1.0598211288452148, - 1.0658446550369263, - 1.068047046661377, - 1.0688518285751343, - 1.069145679473877, - 1.0692529678344727, - 1.0692921876907349, - 1.069306492805481, - 1.0693117380142212, - 1.069313645362854, - 1.0693143606185913, - 1.0693145990371704, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.0693145990371704, - 1.06931471824646, - 1.06931471824646 - ], - [ - 0.8597189784049988, - 0.9942150712013245, - 1.0025675296783447, - 0.6643202304840088, - 0.9734213352203369, - -1.0467698574066162, - -0.03125664219260216, - 0.4332946836948395, - -0.8934475779533386, - 1.0687097311019897, - 0.11192795634269714, - -0.9986746907234192, - -1.0251615047454834, - 0.9791648387908936, - -0.9826422333717346, - -0.4541783928871155, - 0.45298120379447937, - 0.860262393951416, - 1.002732515335083, - 1.0487347841262817, - 1.0630897283554077, - 1.067472219467163, - 1.0687830448150635, - 1.0691205263137817, - 1.0692437887191772, - 1.0692888498306274, - 1.069305181503296, - 1.0693113803863525, - 1.0693135261535645, - 1.0693142414093018, - 1.0693145990371704, - 1.0693145990371704 - ], - [ - -0.6513853669166565, - 0.9871648550033569, - -0.9953957200050354, - 0.2872334420681, - -0.7212009429931641, - 0.9285621643066406, - -0.36571380496025085, - -0.02881699800491333, - 0.9443736672401428, - -0.5783533453941345, - 0.9698985815048218, - -0.18379777669906616, - 0.986435055732727, - 0.2478526532649994, - 0.7358847260475159, - -1.0480421781539917, - -1.0282130241394043, - -1.0589879751205444, - -0.8090404272079468, - 1.069259524345398, - 0.8666334748268127, - -0.8727647662162781, - -0.4628109335899353, - 0.3677518665790558, - 0.791765034198761, - 0.9649869203567505, - 1.0308140516281128, - 1.0552012920379639, - 1.0641536712646484, - 1.0674290657043457, - 1.068626046180725, - 1.0690631866455078 - ], - [ - -1.0655258893966675, - 0.5742302536964417, - -0.7887967228889465, - 0.5039116740226746, - -0.6103026866912842, - 0.8831673860549927, - -0.316233366727829, - 0.0025070905685424805, - 0.9530443549156189, - -0.5879814028739929, - 0.972791314125061, - -0.17968153953552246, - 0.9874148368835449, - 0.2493443638086319, - 0.7352097034454346, - -1.0481456518173218, - -1.0281323194503784, - -1.0590109825134277, - -0.8089804649353027, - 1.0692589282989502, - 0.8666170239448547, - -0.8727558851242065, - -0.46281829476356506, - 0.3677472174167633, - 0.7917630076408386, - 0.9649860858917236, - 1.0308138132095337, - 1.0552011728286743, - 1.0641536712646484, - 1.0674290657043457, - 1.0686259269714355, - 1.0690631866455078 - ], - [ - -1.0522708892822266, - -0.7683091163635254, - -0.7019533514976501, - -0.7297235727310181, - 0.49248647689819336, - -0.4076384902000427, - 0.5469351410865784, - -0.013338646851480007, - 0.8245336413383484, - 0.6515282988548279, - 0.07713282108306885, - -0.1811583787202835, - -0.279379278421402, - 1.0427840948104858, - 0.9073381423950195, - 1.0354822874069214, - 1.0616787672042847, - -0.6585246324539185, - 0.8328317999839783, - 0.44185671210289, - 0.8754521608352661, - 0.6780965924263, - -0.8121644854545593, - -0.7332826256752014, - 0.17557725310325623, - 0.706089437007904, - 0.93146812915802, - 1.0182678699493408, - 1.0505784749984741, - 1.062459945678711, - 1.0668097734451294, - 1.0683997869491577 - ], - [ - 0.7204014658927917, - 0.16690370440483093, - 0.46298733353614807, - -0.7731164693832397, - -0.23665851354599, - 0.4698231816291809, - -0.8517317771911621, - -1.06899094581604, - 0.6142278909683228, - -0.4332176446914673, - 0.6953068971633911, - -1.0075379610061646, - 0.7525005340576172, - -0.9537095427513123, - -0.05741226673126221, - 0.9852739572525024, - 0.9079124927520752, - 1.0282539129257202, - 0.15483181178569794, - 1.0690927505493164, - 0.33539730310440063, - 0.35535576939582825, - -0.6686892509460449, - -0.8163681030273438, - 0.10319384932518005, - 0.6723594665527344, - 0.9180838465690613, - 1.0132331848144531, - 1.0487200021743774, - 1.0617786645889282, - 1.0665606260299683, - 1.0683088302612305 - ] - ], - "sin": [ - [ - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0 - ], - [ - 0.899797260761261, - 0.6075667142868042, - 0.38186508417129517, - 0.23405882716178894, - 0.1421731561422348, - 0.08607665449380875, - 0.05205157399177551, - 0.031462475657463074, - 0.019014401361346245, - 0.011490718461573124, - 0.006943884771317244, - 0.0041961828246712685, - 0.002535742474719882, - 0.0015323410043492913, - 0.0009259883081540465, - 0.0005245982320047915, - 0.0002958786499220878, - 0.00016602707910351455, - 9.26119028008543e-05, - 5.130128556629643e-05, - 2.8182896130601875e-05, - 1.5327737855841406e-05, - 8.233329936047085e-06, - 4.975372121407418e-06, - 3.0065993996686302e-06, - 1.8168771021009888e-06, - 1.097932226912235e-06, - 6.634764986301889e-07, - 4.0093647157846135e-07, - 2.422844715965766e-07, - 1.4641162238149263e-07, - 8.847601407069305e-08 - ], - [ - -0.635859489440918, - -0.3936627209186554, - 0.3718767762184143, - -0.8379216194152832, - -0.44258883595466614, - -0.2184189260005951, - -1.0688577890396118, - 0.9775938391685486, - -0.5875248312950134, - 0.03596566617488861, - 1.0634406805038452, - -0.38220784068107605, - -0.30410149693489075, - -0.42973271012306213, - -0.4217204451560974, - 0.9680681824684143, - 0.9686289429664612, - 0.6351240277290344, - 0.3714318573474884, - 0.20878027379512787, - 0.1152130514383316, - 0.06274635344743729, - 0.033718131482601166, - 0.02037789113819599, - 0.012314759194850922, - 0.0074418680742383, - 0.004497116897255182, - 0.002717596711590886, - 0.0016422352055087686, - 0.000992397079244256, - 0.0005997019470669329, - 0.00036239775363355875 - ], - [ - 0.8480159640312195, - 0.41102248430252075, - -0.3906678557395935, - -1.030014991760254, - -0.7894954681396484, - 0.5302888751029968, - -1.0048320293426514, - -1.0689263343811035, - -0.5015896558761597, - 0.8994117379188538, - -0.4502561688423157, - -1.0534002780914307, - -0.4127710163593292, - -1.040193796157837, - 0.7758270502090454, - 0.21222993731498718, - -0.29361870884895325, - 0.14825202524662018, - -0.6992049217224121, - 0.010867959819734097, - 0.6264026761054993, - -0.6178314685821533, - 0.963970959186554, - 1.0040879249572754, - 0.7187086343765259, - 0.4606889486312866, - 0.2843526303768158, - 0.17315985262393951, - 0.10493354499340057, - 0.0634758397936821, - 0.03837253898382187, - 0.023191550746560097 - ], - [ - -0.08993688225746155, - 0.902049720287323, - -0.7219651341438293, - -0.9431368112564087, - -0.878045916557312, - 0.602867603302002, - -1.0214844942092896, - -1.0693117380142212, - -0.4849127233028412, - 0.8931471705436707, - -0.44397181272506714, - -1.0541102886199951, - -0.410421758890152, - -1.039837121963501, - 0.7764667868614197, - 0.21171817183494568, - -0.29390108585357666, - 0.14808638393878937, - -0.6992743611335754, - 0.010918946005403996, - 0.6264254450798035, - -0.6178439855575562, - 0.9639674425125122, - 1.004089593887329, - 0.7187108397483826, - 0.46069058775901794, - 0.28435367345809937, - 0.17316049337387085, - 0.10493393987417221, - 0.0634760782122612, - 0.03837268799543381, - 0.02319163829088211 - ], - [ - 0.19015751779079437, - 0.7437304854393005, - -0.8066569566726685, - -0.7816249132156372, - -0.9491527676582336, - -0.988567054271698, - 0.9188557863235474, - -1.0692315101623535, - 0.6808658242225647, - 0.8479061126708984, - -1.0665291547775269, - 1.053857445716858, - -1.0321731567382812, - 0.23671789467334747, - -0.5658369064331055, - 0.2668529450893402, - 0.12756243348121643, - 0.8424839973449707, - -0.6706900596618652, - -0.9737538695335388, - 0.6140174865722656, - 0.8268125057220459, - -0.6955736875534058, - 0.7782869338989258, - 1.0548015832901, - 0.803039014339447, - 0.5251676440238953, - 0.3264423906803131, - 0.1992960274219513, - 0.1208832636475563, - 0.07314864546060562, - 0.044225383549928665 - ], - [ - -0.7902251482009888, - 1.0562087297439575, - 0.9638862609863281, - -0.7387320399284363, - 1.042797565460205, - 0.9605727791786194, - 0.646519124507904, - 0.026310738176107407, - -0.8753045797348022, - -0.9776279926300049, - -0.8123929500579834, - 0.35819175839424133, - -0.759721577167511, - -0.4836031496524811, - 1.0677722692489624, - -0.4155347943305969, - 0.5649150013923645, - -0.29347580671310425, - 1.0580458641052246, - 0.02178577519953251, - 1.0153534412384033, - 1.0085417032241821, - -0.8344391584396362, - 0.6906352639198303, - 1.0643237829208374, - 0.8314846754074097, - 0.548229992389679, - 0.3417493999004364, - 0.208854541182518, - 0.12672804296016693, - 0.07669580727815628, - 0.04637227579951286 - ] - ] - }, - { - "rot": 128, - "theta": 10000000.0, - "factor": 2.0, - "original": 262144, - "magnitude": 1.0693147180559945, - "inv_freq": [ - 1.0, - 0.7773650288581848, - 0.6042963862419128, - 0.46975886821746826, - 0.36517414450645447, - 0.283873587846756, - 0.22067339718341827, - 0.17154379189014435, - 0.1333521455526352, - 0.10366329550743103, - 0.08058421313762665, - 0.06264335662126541, - 0.04869675263762474, - 0.037855152040719986, - 0.02942727319896221, - 0.02287573181092739, - 0.017782794311642647, - 0.01382372248917818, - 0.010746078565716743, - 0.008353625424206257, - 0.006493816617876291, - 0.005048065911978483, - 0.003924189601093531, - 0.0030505280010402203, - 0.0023713738191872835, - 0.0018434228841215372, - 0.0014330126577988267, - 0.0011139739071950316, - 0.0008659643353894353, - 0.0006507313228212297, - 0.0004884125082753599, - 0.00036611498217098415, - 0.00027406407753005624, - 0.00020485366985667497, - 0.0001528762368252501, - 0.00011388894927222282, - 8.468401210848242e-05, - 6.283809489104897e-05, - 4.6522036427631974e-05, - 3.4356373362243176e-05, - 2.530179153836798e-05, - 1.857602001109626e-05, - 1.3590914932137821e-05, - 9.904783837555442e-06, - 7.699632078583818e-06, - 5.98542510488187e-06, - 4.6528602979378775e-06, - 3.616970843722811e-06, - 2.81170673588349e-06, - 2.1857224510313245e-06, - 1.6991041320579825e-06, - 1.3208241398388054e-06, - 1.0267624475090997e-06, - 7.981692533576279e-07, - 6.204688816069392e-07, - 4.823307904189278e-07, - 3.7494712046282075e-07, - 2.9147076929803006e-07, - 2.2657918918866926e-07, - 1.761347334650054e-07, - 1.3692098832507327e-07, - 1.0643758230344247e-07, - 8.274085416815069e-08, - 6.43198490024588e-08 - ], - "cos": [ - [ - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646 - ], - [ - 0.5777531862258911, - 0.7621692419052124, - 0.8799412846565247, - 0.9534838795661926, - 0.9988058805465698, - 1.0265183448791504, - 1.0433841943740845, - 1.0536197423934937, - 1.0598211288452148, - 1.0635744333267212, - 1.0658446550369263, - 1.067217230796814, - 1.068047046661377, - 1.068548560142517, - 1.0688518285751343, - 1.0690349340438843, - 1.069145679473877, - 1.069212555885315, - 1.0692529678344727, - 1.0692774057388306, - 1.0692921876907349, - 1.0693011283874512, - 1.069306492805481, - 1.0693097114562988, - 1.0693117380142212, - 1.0693129301071167, - 1.069313645362854, - 1.0693141222000122, - 1.0693143606185913, - 1.0693144798278809, - 1.0693145990371704, - 1.0693145990371704, - 1.0693145990371704, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.0693145990371704, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.0693145990371704, - 1.0693145990371704, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646 - ], - [ - 0.8597189784049988, - 0.08865529298782349, - 0.9942150712013245, - 0.09949047118425369, - 1.0025675296783447, - 1.0019184350967407, - 0.6643202304840088, - 0.5102278590202332, - 0.9734213352203369, - -0.9435669183731079, - -1.0467698574066162, - 0.5565336346626282, - -0.03125664219260216, - -0.46909114718437195, - 0.4332946836948395, - 0.9122970700263977, - -0.8934475779533386, - 1.0664433240890503, - 1.0687097311019897, - -1.0077195167541504, - 0.11192795634269714, - -0.27130818367004395, - -0.9986746907234192, - 1.066589593887329, - -1.0251615047454834, - 0.31939542293548584, - 0.9791648387908936, - -0.15932254493236542, - -0.9826422333717346, - -0.9503476619720459, - -0.44551464915275574, - 0.07605954259634018, - 0.4634099006652832, - 0.7144595980644226, - 0.8664352297782898, - 0.9550614953041077, - 1.0056294202804565, - 1.0340903997421265, - 1.0499595403671265, - 1.0587443113327026, - 1.06357741355896, - 1.066220998764038, - 1.0676583051681519, - 1.0684348344802856, - 1.0687830448150635, - 1.068993330001831, - 1.0691205263137817, - 1.069197416305542, - 1.0692437887191772, - 1.0692718029022217, - 1.0692888498306274, - 1.0692991018295288, - 1.069305181503296, - 1.0693089962005615, - 1.0693113803863525, - 1.0693126916885376, - 1.0693135261535645, - 1.0693140029907227, - 1.0693142414093018, - 1.0693143606185913, - 1.0693145990371704, - 1.0693145990371704, - 1.0693145990371704, - 1.06931471824646 - ], - [ - -0.6513853669166565, - -0.19290786981582642, - 0.9871648550033569, - 1.0574928522109985, - -0.9953957200050354, - -0.8994616866111755, - 0.2872334420681, - 1.0369493961334229, - -0.7212009429931641, - 1.054075002670288, - 0.9285621643066406, - -0.9795029759407043, - -0.36571380496025085, - -0.7229933738708496, - -0.02881699800491333, - -0.8900305032730103, - 0.9443736672401428, - -0.037561286240816116, - -0.5783533453941345, - -1.056593656539917, - 0.9698985815048218, - -0.8146253228187561, - -0.18379777669906616, - -0.14840036630630493, - 0.986435055732727, - 0.9032163023948669, - 0.2478526532649994, - -1.057676911354065, - 0.7358847260475159, - 0.6318320631980896, - -0.7666455507278442, - -0.16603565216064453, - -0.9795569181442261, - -1.023470401763916, - -0.7711856961250305, - 0.010757197625935078, - -1.046228289604187, - -0.7716785073280334, - 0.9965856671333313, - -0.9770345091819763, - 1.0046674013137817, - 0.16738855838775635, - -0.9758684039115906, - -0.9143319725990295, - -0.4628109335899353, - 0.0018809328321367502, - 0.3677518665790558, - 0.623598575592041, - 0.791765034198761, - 0.8985382914543152, - 0.9649869203567505, - 1.005854845046997, - 1.0308140516281128, - 1.0459932088851929, - 1.0552012920379639, - 1.0607784986495972, - 1.0641536712646484, - 1.0661948919296265, - 1.0674290657043457, - 1.0681750774383545, - 1.068626046180725, - 1.0688984394073486, - 1.0690631866455078, - 1.0691627264022827 - ], - [ - -1.0655258893966675, - 0.6036512851715088, - 0.5742302536964417, - 1.0150601863861084, - -0.7887967228889465, - -0.7036169171333313, - 0.5039116740226746, - 0.9770159721374512, - -0.6103026866912842, - 1.0670206546783447, - 0.8831673860549927, - -0.9507979154586792, - -0.316233366727829, - -0.7524679899215698, - 0.0025070905685424805, - -0.9033966660499573, - 0.9530443549156189, - -0.022686710581183434, - -0.5879814028739929, - -1.055192232131958, - 0.972791314125061, - -0.8111482262611389, - -0.17968153953552246, - -0.15163138508796692, - 0.9874148368835449, - 0.9042628407478333, - 0.2493443638086319, - -1.0578539371490479, - 0.7352097034454346, - 0.6312658786773682, - -0.767009437084198, - -0.16642247140407562, - -0.9796746969223022, - -1.0234065055847168, - -0.7712987065315247, - 0.01087956503033638, - -1.046209692955017, - -0.7716318368911743, - 0.996603786945343, - -0.9770494103431702, - 1.0046582221984863, - 0.16740818321704865, - -0.975862443447113, - -0.9143375158309937, - -0.46281829476356506, - 0.0018745592096820474, - 0.3677472174167633, - 0.6235953569412231, - 0.7917630076408386, - 0.8985369801521301, - 0.9649860858917236, - 1.0058544874191284, - 1.0308138132095337, - 1.0459930896759033, - 1.0552011728286743, - 1.0607783794403076, - 1.0641536712646484, - 1.0661948919296265, - 1.0674290657043457, - 1.0681750774383545, - 1.0686259269714355, - 1.0688985586166382, - 1.0690631866455078, - 1.0691627264022827 - ], - [ - -1.0522708892822266, - -0.05930701270699501, - -0.7683091163635254, - 0.17716597020626068, - -0.7019533514976501, - 1.0010639429092407, - -0.7297235727310181, - 1.0594686269760132, - 0.49248647689819336, - -0.08763059973716736, - -0.4076384902000427, - 1.0693141222000122, - 0.5469351410865784, - -0.9282023310661316, - -0.013338646851480007, - -0.8363955616950989, - 0.8245336413383484, - 1.0017105340957642, - 0.6515282988548279, - 0.06952807307243347, - 0.07713282108306885, - -0.250789612531662, - -0.1811583787202835, - 0.022245237603783607, - -0.279379278421402, - -0.3626629710197449, - 1.0427840948104858, - -0.6435174345970154, - 0.9073381423950195, - 0.22376880049705505, - 0.7153396606445312, - 0.7096406817436218, - 0.3894338309764862, - -0.3415154218673706, - 0.5414062738418579, - 0.986642062664032, - -0.07425130158662796, - 1.0693094730377197, - -0.31695008277893066, - -0.10736316442489624, - 1.0654970407485962, - -1.0593292713165283, - 0.9320500493049622, - 0.2541821599006653, - -0.8121644854545593, - -1.0574856996536255, - -0.7332826256752014, - -0.2517779469490051, - 0.17557725310325623, - 0.49182772636413574, - 0.706089437007904, - 0.844480037689209, - 0.93146812915802, - 0.9852848649024963, - 1.0182678699493408, - 1.038369059562683, - 1.0505784749984741, - 1.0579793453216553, - 1.062459945678711, - 1.0651706457138062, - 1.0668097734451294, - 1.067800760269165, - 1.0683997869491577, - 1.0687618255615234 - ], - [ - 0.7204014658927917, - -0.9770510196685791, - 0.16690370440483093, - 1.0537045001983643, - 0.46298733353614807, - 0.1564042568206787, - -0.7731164693832397, - 0.8413293361663818, - -0.23665851354599, - 1.0395491123199463, - 0.4698231816291809, - 0.6746538877487183, - -0.8517317771911621, - -0.049967240542173386, - -1.06899094581604, - 0.4353052079677582, - 0.6142278909683228, - -1.0676320791244507, - -0.4332176446914673, - 1.016000509262085, - 0.6953068971633911, - 0.16672539710998535, - -1.0075379610061646, - -1.027222752571106, - 0.7525005340576172, - 0.45829126238822937, - -0.9537095427513123, - 1.0233770608901978, - -0.05741226673126221, - -0.32333067059516907, - 0.030500834807753563, - -1.0176328420639038, - 0.7255676984786987, - 0.8897460699081421, - 0.043200526386499405, - -1.0690957307815552, - 0.9779296517372131, - 0.044390320777893066, - 0.7883260846138, - 0.716148316860199, - 0.8185253143310547, - -1.0169029235839844, - 0.7118510603904724, - 0.4943186342716217, - -0.6686892509460449, - -1.0693081617355347, - -0.8163681030273438, - -0.3419831693172455, - 0.10319384932518005, - 0.4407549202442169, - 0.6723594665527344, - 0.8230066299438477, - 0.9180838465690613, - 0.9770455360412598, - 1.0132331848144531, - 1.0353062152862549, - 1.0487200021743774, - 1.0568535327911377, - 1.0617786645889282, - 1.0647586584091187, - 1.0665606260299683, - 1.0676501989364624, - 1.0683088302612305, - 1.068706750869751 - ] - ], - "sin": [ - [ - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0 - ], - [ - 0.899797260761261, - 0.7500213384628296, - 0.6075667142868042, - 0.484048068523407, - 0.38186508417129517, - 0.29948970675468445, - 0.23405882716178894, - 0.18253596127033234, - 0.1421731561422348, - 0.11065026372671127, - 0.08607665449380875, - 0.06694166362285614, - 0.05205157399177551, - 0.04046940430998802, - 0.031462475657463074, - 0.024459224194288254, - 0.019014401361346245, - 0.014781438745558262, - 0.011490718461573124, - 0.008932551369071007, - 0.006943884771317244, - 0.005397948436439037, - 0.0041961828246712685, - 0.0032619694247841835, - 0.002535742474719882, - 0.0019711980130523443, - 0.0015323410043492913, - 0.001191188464872539, - 0.0009259883081540465, - 0.0006958364974707365, - 0.0005222666659392416, - 0.000391492125345394, - 0.0002930607588496059, - 0.0002190530503867194, - 0.00016347281052730978, - 0.00012178313045296818, - 9.055386180989444e-05, - 6.719370139762759e-05, - 4.9746697186492383e-05, - 3.6737776099471375e-05, - 2.705557744775433e-05, - 1.9863611669279635e-05, - 1.4532964996760711e-05, - 1.0591331374598667e-05, - 8.233329936047085e-06, - 6.400302936526714e-06, - 4.975372121407418e-06, - 3.867680334224133e-06, - 3.0065993996686302e-06, - 2.337225168957957e-06, - 1.8168771021009888e-06, - 1.4123767186902114e-06, - 1.097932226912235e-06, - 8.534941571269883e-07, - 6.634764986301889e-07, - 5.157634177521686e-07, - 4.0093647157846135e-07, - 3.1167397196441016e-07, - 2.422844715965766e-07, - 1.8834346349194675e-07, - 1.4641162238149263e-07, - 1.1381527542653203e-07, - 8.847601407069305e-08, - 6.877816360884026e-08 - ], - [ - -0.635859489440918, - -1.0656331777572632, - -0.3936627209186554, - 1.064676284790039, - 0.3718767762184143, - 0.3736218810081482, - -0.8379216194152832, - -0.9397348165512085, - -0.44258883595466614, - -0.5031058192253113, - -0.2184189260005951, - -0.9130740761756897, - -1.0688577890396118, - -0.9609305262565613, - 0.9775938391685486, - -0.5578064918518066, - -0.5875248312950134, - 0.07831012457609177, - 0.03596566617488861, - 0.3576805591583252, - 1.0634406805038452, - 1.0343239307403564, - -0.38220784068107605, - -0.07629266381263733, - -0.30410149693489075, - 1.0205000638961792, - -0.42973271012306213, - -1.0573790073394775, - -0.4217204451560974, - 0.490176796913147, - 0.97208571434021, - 1.0666062831878662, - 0.9636831879615784, - 0.7956013083457947, - 0.6266769170761108, - 0.48092788457870483, - 0.36351555585861206, - 0.2721966505050659, - 0.2025315761566162, - 0.14998175203800201, - 0.110621377825737, - 0.08128286898136139, - 0.05949628725647926, - 0.04337019473314285, - 0.033718131482601166, - 0.026213016360998154, - 0.02037789113819599, - 0.015841437503695488, - 0.012314759194850922, - 0.00957314670085907, - 0.0074418680742383, - 0.00578506663441658, - 0.004497116897255182, - 0.0034959057811647654, - 0.002717596711590886, - 0.0021125655621290207, - 0.0016422352055087686, - 0.0012766163563355803, - 0.000992397079244256, - 0.000771454768255353, - 0.0005997019470669329, - 0.0004661873681470752, - 0.00036239775363355875, - 0.0002817153581418097 - ], - [ - 0.8480159640312195, - -1.0517702102661133, - 0.41102248430252075, - -0.158564954996109, - -0.3906678557395935, - -0.5782755613327026, - -1.030014991760254, - 0.2610936164855957, - -0.7894954681396484, - -0.17988857626914978, - 0.5302888751029968, - -0.4289613664150238, - -1.0048320293426514, - 0.7878543734550476, - -1.0689263343811035, - 0.5926885008811951, - -0.5015896558761597, - -1.0686548948287964, - 0.8994117379188538, - -0.1644500195980072, - -0.4502561688423157, - -0.6926901340484619, - -1.0534002780914307, - 1.058967113494873, - -0.4127710163593292, - -0.5723935961723328, - -1.040193796157837, - 0.15733245015144348, - 0.7758270502090454, - 0.862683117389679, - 0.745445191860199, - 1.0563455820083618, - 0.4288382828235626, - -0.3097456097602844, - 0.740747332572937, - -1.069260597229004, - -0.22099833190441132, - -0.7402340173721313, - -0.38762226700782776, - 0.43455445766448975, - 0.3661656677722931, - -1.0561320781707764, - -0.43716686964035034, - 0.55446457862854, - 0.963970959186554, - 1.0693130493164062, - 1.0040879249572754, - 0.8686534762382507, - 0.7187086343765259, - 0.5797093510627747, - 0.4606889486312866, - 0.3628910481929779, - 0.2843526303768158, - 0.22210857272148132, - 0.17315985262393951, - 0.13484381139278412, - 0.10493354499340057, - 0.08162368088960648, - 0.0634758397936821, - 0.04935538396239281, - 0.03837253898382187, - 0.029832005500793457, - 0.023191550746560097, - 0.018028859049081802 - ], - [ - -0.08993688225746155, - -0.8826319575309753, - 0.902049720287323, - 0.33628395199775696, - -0.7219651341438293, - -0.805206298828125, - -0.9431368112564087, - 0.43459609150886536, - -0.878045916557312, - -0.07000716775655746, - 0.602867603302002, - -0.4893029034137726, - -1.0214844942092896, - 0.7597538232803345, - -1.0693117380142212, - 0.5721087455749512, - -0.4849127233028412, - -1.069074034690857, - 0.8931471705436707, - -0.17321480810642242, - -0.44397181272506714, - -0.696758508682251, - -1.0541102886199951, - 1.0585092306137085, - -0.410421758890152, - -0.5707387328147888, - -1.039837121963501, - 0.156138077378273, - 0.7764667868614197, - 0.8630974888801575, - 0.74507075548172, - 1.056284785270691, - 0.4285692274570465, - -0.30995646119117737, - 0.7406296133995056, - -1.0692594051361084, - -0.22108612954616547, - -0.7402825951576233, - -0.3875757157802582, - 0.4345209002494812, - 0.3661910593509674, - -1.056128978729248, - -0.43718013167381287, - 0.5544553995132446, - 0.9639674425125122, - 1.0693130493164062, - 1.004089593887329, - 0.8686556816101074, - 0.7187108397483826, - 0.5797113180160522, - 0.46069058775901794, - 0.36289238929748535, - 0.28435367345809937, - 0.22210940718650818, - 0.17316049337387085, - 0.1348443180322647, - 0.10493393987417221, - 0.08162398636341095, - 0.0634760782122612, - 0.04935557022690773, - 0.03837268799543381, - 0.02983211912214756, - 0.02319163829088211, - 0.018028927966952324 - ], - [ - 0.19015751779079437, - -1.0676687955856323, - 0.7437304854393005, - 1.054535984992981, - -0.8066569566726685, - -0.37590548396110535, - -0.7816249132156372, - 0.14477676153182983, - -0.9491527676582336, - 1.0657179355621338, - -0.988567054271698, - -0.0010927487164735794, - 0.9188557863235474, - 0.5309184789657593, - -1.0692315101623535, - 0.6662405133247375, - 0.6808658242225647, - 0.3741791248321533, - 0.8479061126708984, - -1.067051887512207, - -1.0665291547775269, - -1.039489507675171, - 1.053857445716858, - -1.069083333015442, - -1.0321731567382812, - -1.0059372186660767, - 0.23671789467334747, - -0.8540019392967224, - -0.5658369064331055, - -1.0456392765045166, - -0.7948101162910461, - 0.7999024987220764, - -0.9958791732788086, - 1.0133119821548462, - 0.9221243858337402, - 0.41227594017982483, - -1.066733717918396, - 0.003338152775540948, - -1.021262288093567, - -1.0639111995697021, - 0.09027673304080963, - 0.14579284191131592, - 0.5241341590881348, - -1.0386651754379272, - -0.6955737471580505, - 0.15861226618289948, - 0.7782869338989258, - 1.0392506122589111, - 1.0548015832901, - 0.9494943022727966, - 0.803039014339447, - 0.6559629440307617, - 0.5251676440238953, - 0.41550904512405396, - 0.3264423906803131, - 0.25538918375968933, - 0.1992960274219513, - 0.15528593957424164, - 0.1208832636475563, - 0.09405004978179932, - 0.07314864546060562, - 0.05688077583909035, - 0.044225383549928665, - 0.034383151680231094 - ], - [ - -0.7902251482009888, - -0.4345173239707947, - 1.0562087297439575, - 0.18204641342163086, - 0.9638862609863281, - 1.057814598083496, - -0.7387320399284363, - 0.6599991917610168, - 1.042797565460205, - -0.25054293870925903, - 0.9605727791786194, - 0.8296240568161011, - 0.646519124507904, - -1.0681465864181519, - 0.026310738176107407, - -0.9767002463340759, - -0.8753045797348022, - 0.05996488779783249, - -0.9776279926300049, - 0.33343228697776794, - -0.8123929500579834, - 1.056236982345581, - 0.35819175839424133, - -0.2970648407936096, - -0.759721577167511, - -0.966127872467041, - -0.4836031496524811, - -0.3100537061691284, - 1.0677722692489624, - 1.0192601680755615, - -1.0688796043395996, - -0.32841649651527405, - -0.785484254360199, - 0.5931153297424316, - -1.06844162940979, - -0.02163565531373024, - 0.4325360655784607, - 1.0683929920196533, - -0.722478985786438, - -0.7940815687179565, - 0.6880772709846497, - -0.3306693434715271, - 0.7979361414909363, - -0.9481998682022095, - -0.8344391584396362, - 0.003755486337468028, - 0.6906352639198303, - 1.013154149055481, - 1.0643237829208374, - 0.9742530584335327, - 0.8314846754074097, - 0.6827107667922974, - 0.548229992389679, - 0.4345296621322632, - 0.3417493999004364, - 0.2675352394580841, - 0.208854541182518, - 0.16277137398719788, - 0.12672804296016693, - 0.09860574454069138, - 0.07669580727815628, - 0.059640903025865555, - 0.04637227579951286, - 0.03605266287922859 - ] - ] - }, - { - "rot": 256, - "theta": 10000000.0, - "factor": 2.0, - "original": 262144, - "magnitude": 1.0693147180559945, - "inv_freq": [ - 1.0, - 0.8816831111907959, - 0.7773650288581848, - 0.6853895783424377, - 0.6042963862419128, - 0.5327978730201721, - 0.46975886821746826, - 0.4141784608364105, - 0.36517414450645447, - 0.32196784019470215, - 0.283873587846756, - 0.2502865493297577, - 0.22067339718341827, - 0.19456401467323303, - 0.17154379189014435, - 0.15124726295471191, - 0.1333521455526352, - 0.11757431924343109, - 0.10366329550743103, - 0.09139817208051682, - 0.08058421313762665, - 0.07104974240064621, - 0.06264335662126541, - 0.05523158237338066, - 0.04869675263762474, - 0.04293510317802429, - 0.037855152040719986, - 0.03337624669075012, - 0.02942727319896221, - 0.025945525616407394, - 0.02287573181092739, - 0.020169146358966827, - 0.017782794311642647, - 0.01567878946661949, - 0.01382372248917818, - 0.012188142165541649, - 0.010746078565716743, - 0.009474635124206543, - 0.008353625424206257, - 0.007365249563008547, - 0.006493816617876291, - 0.0057254876010119915, - 0.005048065911978483, - 0.004450793843716383, - 0.003924189601093531, - 0.003459891537204385, - 0.0030505280010402203, - 0.0026895988266915083, - 0.0023713738191872835, - 0.0020908000878989697, - 0.0018434228841215372, - 0.0016253149369731545, - 0.0014330126577988267, - 0.0012634629383683205, - 0.0011139739071950316, - 0.000982171855866909, - 0.0008659643353894353, - 0.0007503421511501074, - 0.0006499576265923679, - 0.0005628234939649701, - 0.0004872095014434308, - 0.00042160949669778347, - 0.00036471226485446095, - 0.0003153767320327461, - 0.00027261016657575965, - 0.00023554862127639353, - 0.00020344088261481375, - 0.00017563349683769047, - 0.00015155834262259305, - 0.000130721484310925, - 0.00011269371316302568, - 9.710194717627019e-05, - 8.362214430235326e-05, - 7.197279774118215e-05, - 6.190945714479312e-05, - 5.3219908295432106e-05, - 4.571993486024439e-05, - 3.924968768842518e-05, - 3.3670494303805754e-05, - 2.8862075851066038e-05, - 2.472014239174314e-05, - 2.1154290152480826e-05, - 1.80861861736048e-05, - 1.5447960322489962e-05, - 1.318084559898125e-05, - 1.123395031754626e-05, - 9.904783837555442e-06, - 8.732879905437585e-06, - 7.699632078583818e-06, - 6.788635801058263e-06, - 5.98542510488187e-06, - 5.277247964841081e-06, - 4.6528602979378775e-06, - 4.102348157175584e-06, - 3.616970843722811e-06, - 3.189021754224086e-06, - 2.81170673588349e-06, - 2.479034037605743e-06, - 2.1857224510313245e-06, - 1.9271144537924556e-06, - 1.6991041320579825e-06, - 1.4980713558543357e-06, - 1.3208241398388054e-06, - 1.1645482800304308e-06, - 1.0267624475090997e-06, - 9.052791938302107e-07, - 7.981692533576279e-07, - 7.037323257463868e-07, - 6.204688816069392e-07, - 5.470569135468395e-07, - 4.823307904189278e-07, - 4.252629253187479e-07, - 3.7494712046282075e-07, - 3.3058452686418605e-07, - 2.9147076929803006e-07, - 2.569848618350079e-07, - 2.2657918918866926e-07, - 1.9977102283519343e-07, - 1.761347334650054e-07, - 1.552950124050767e-07, - 1.3692098832507327e-07, - 1.20720912377692e-07, - 1.0643758230344247e-07, - 9.384421417735211e-08, - 8.274085416815069e-08, - 7.295120951766876e-08, - 6.43198490024588e-08, - 5.6709719586933716e-08 - ], - "cos": [ - [ - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646 - ], - [ - 0.5777531862258911, - 0.6799269914627075, - 0.7621692419052124, - 0.8278340101242065, - 0.8799412846565247, - 0.9210963249206543, - 0.9534838795661926, - 0.978901207447052, - 0.9988058805465698, - 1.0143675804138184, - 1.0265183448791504, - 1.035996437072754, - 1.0433841943740845, - 1.0491390228271484, - 1.0536197423934937, - 1.0571073293685913, - 1.0598211288452148, - 1.0619323253631592, - 1.0635744333267212, - 1.0648515224456787, - 1.0658446550369263, - 1.0666167736053467, - 1.067217230796814, - 1.0676840543746948, - 1.068047046661377, - 1.0683292150497437, - 1.068548560142517, - 1.0687191486358643, - 1.0688518285751343, - 1.0689548254013062, - 1.0690349340438843, - 1.0690972805023193, - 1.069145679473877, - 1.069183349609375, - 1.069212555885315, - 1.0692353248596191, - 1.0692529678344727, - 1.069266676902771, - 1.0692774057388306, - 1.0692857503890991, - 1.0692921876907349, - 1.069297194480896, - 1.0693011283874512, - 1.06930410861969, - 1.069306492805481, - 1.0693084001541138, - 1.0693097114562988, - 1.0693109035491943, - 1.0693117380142212, - 1.0693124532699585, - 1.0693129301071167, - 1.0693132877349854, - 1.069313645362854, - 1.069313883781433, - 1.0693141222000122, - 1.0693142414093018, - 1.0693143606185913, - 1.0693143606185913, - 1.0693144798278809, - 1.0693145990371704, - 1.0693145990371704, - 1.0693145990371704, - 1.06931471824646, - 1.0693145990371704, - 1.0693145990371704, - 1.06931471824646, - 1.0693145990371704, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.0693145990371704, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.0693145990371704, - 1.06931471824646, - 1.0693145990371704, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.0693145990371704, - 1.0693145990371704, - 1.0693145990371704, - 1.0693145990371704, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646 - ], - [ - 0.8597189784049988, - 0.12086046487092972, - 0.08865529298782349, - 0.3592999577522278, - 0.9942150712013245, - -0.5163969397544861, - 0.09949047118425369, - 1.0691953897476196, - 1.0025675296783447, - 0.8256288766860962, - 1.0019184350967407, - 0.5646449327468872, - 0.6643202304840088, - 0.550200879573822, - 0.5102278590202332, - -0.8733654618263245, - 0.9734213352203369, - -0.6471863985061646, - -0.9435669183731079, - -0.9293569326400757, - -1.0467698574066162, - -0.4383901357650757, - 0.5565336346626282, - 1.0687007904052734, - -0.03125664219260216, - 1.0669151544570923, - -0.46909114718437195, - 0.05325764790177345, - 0.4332946836948395, - 0.9164575934410095, - 0.9122970700263977, - 0.6380595564842224, - -0.8934475779533386, - 0.19388730823993683, - 1.0664433240890503, - 1.007080316543579, - 1.0687097311019897, - 0.4764467477798462, - -1.0077195167541504, - 0.339345246553421, - 0.11192795634269714, - -0.11775708943605423, - -0.27130818367004395, - 0.8708483576774597, - -0.9986746907234192, - -0.036936186254024506, - 1.066589593887329, - 0.022478019818663597, - -1.0251615047454834, - -0.6969631910324097, - 0.31939542293548584, - 0.9953558444976807, - 0.9791648387908936, - 0.4773583710193634, - -0.15932254493236542, - -0.6801744699478149, - -0.9826422333717346, - -1.0668294429779053, - -0.9487895369529724, - -0.7166948318481445, - -0.4407193064689636, - -0.16626004874706268, - 0.08218647539615631, - 0.29449692368507385, - 0.4691406190395355, - 0.6090553402900696, - 0.7190515995025635, - 0.8043435215950012, - 0.8698054552078247, - 0.9196605682373047, - 0.9574045538902283, - 0.985846996307373, - 1.0072009563446045, - 1.0231846570968628, - 1.0351183414459229, - 1.044008731842041, - 1.0506192445755005, - 1.055525779724121, - 1.0591614246368408, - 1.0618512630462646, - 1.0638378858566284, - 1.065303087234497, - 1.066381812095642, - 1.0671747922897339, - 1.0677566528320312, - 1.0681829452514648, - 1.0684348344802856, - 1.0686306953430176, - 1.0687830448150635, - 1.0689013004302979, - 1.068993330001831, - 1.069064974784851, - 1.0691205263137817, - 1.0691637992858887, - 1.069197416305542, - 1.0692235231399536, - 1.0692437887191772, - 1.0692596435546875, - 1.0692718029022217, - 1.0692813396453857, - 1.0692888498306274, - 1.0692945718765259, - 1.0692991018295288, - 1.0693025588989258, - 1.069305181503296, - 1.0693074464797974, - 1.0693089962005615, - 1.0693103075027466, - 1.0693113803863525, - 1.0693119764328003, - 1.0693126916885376, - 1.0693131685256958, - 1.0693135261535645, - 1.069313645362854, - 1.0693140029907227, - 1.0693141222000122, - 1.0693142414093018, - 1.0693143606185913, - 1.0693143606185913, - 1.0693144798278809, - 1.0693145990371704, - 1.0693145990371704, - 1.0693145990371704, - 1.0693145990371704, - 1.0693145990371704, - 1.0693145990371704, - 1.06931471824646, - 1.0693145990371704 - ], - [ - -0.6513853669166565, - 1.0648926496505737, - -0.19290786981582642, - -0.7843515872955322, - 0.9871648550033569, - 1.0189357995986938, - 1.0574928522109985, - 0.915911853313446, - -0.9953957200050354, - 0.9802453517913818, - -0.8994616866111755, - -0.2907947599887848, - 0.2872334420681, - -1.055144190788269, - 1.0369493961334229, - 0.06404394656419754, - -0.7212009429931641, - -0.6810116171836853, - 1.054075002670288, - -0.036619432270526886, - 0.9285621643066406, - -0.27625617384910583, - -0.9795029759407043, - -0.5520841479301453, - -0.36571380496025085, - -0.3976605534553528, - -0.7229933738708496, - -1.0692088603973389, - -0.02881699800491333, - -1.062839388847351, - -0.8900305032730103, - -1.063999056816101, - 0.9443736672401428, - 0.6798552870750427, - -0.037561286240816116, - -1.0686062574386597, - -0.5783533453941345, - -0.295753538608551, - -1.056593656539917, - -0.2541494071483612, - 0.9698985815048218, - 0.7565687894821167, - -0.8146253228187561, - -0.37385234236717224, - -0.18379777669906616, - -0.6358828544616699, - -0.14840036630630493, - 0.24174010753631592, - 0.986435055732727, - 0.12726156413555145, - 0.9032163023948669, - 0.39577341079711914, - 0.2478526532649994, - -0.24382933974266052, - -1.057676911354065, - 1.058685541152954, - 0.7358847260475159, - -0.36405718326568604, - 0.7926532030105591, - -1.0622985363006592, - -0.4976309835910797, - -0.9024218916893005, - 0.22480200231075287, - 0.584578275680542, - -0.7497508525848389, - 0.49981385469436646, - -1.0661896467208862, - -0.5013577342033386, - -0.47476762533187866, - -1.0247266292572021, - -0.3193347752094269, - 1.0144059658050537, - -1.0666817426681519, - 1.0691490173339844, - -0.9273503422737122, - 0.1976853907108307, - 0.8937321901321411, - -0.6942081451416016, - -0.8835727572441101, - 0.30371344089508057, - 1.0486266613006592, - 0.7912856340408325, - 0.030768586322665215, - -0.657989501953125, - -1.0171390771865845, - -1.0486968755722046, - -0.9143319725990295, - -0.7038564085960388, - -0.4628109335899353, - -0.22165097296237946, - 0.0018809328321367502, - 0.19922155141830444, - 0.3677518665790558, - 0.508329451084137, - 0.623598575592041, - 0.7169232368469238, - 0.791765034198761, - 0.8513538241386414, - 0.8985382914543152, - 0.9357439875602722, - 0.9649869203567505, - 0.9879140257835388, - 1.005854845046997, - 1.0198732614517212, - 1.0308140516281128, - 1.039345383644104, - 1.0459932088851929, - 1.0511707067489624, - 1.0552012920379639, - 1.058337926864624, - 1.0607784986495972, - 1.062677025794983, - 1.0641536712646484, - 1.0653018951416016, - 1.0661948919296265, - 1.0668891668319702, - 1.0674290657043457, - 1.0678486824035645, - 1.0681750774383545, - 1.0684287548065186, - 1.068626046180725, - 1.0687793493270874, - 1.0688984394073486, - 1.0689911842346191, - 1.0690631866455078, - 1.0691192150115967, - 1.0691627264022827, - 1.0691965818405151 - ], - [ - -1.0655258893966675, - 0.6080281734466553, - 0.6036512851715088, - -1.067401647567749, - 0.5742302536964417, - 0.7141802310943604, - 1.0150601863861084, - 0.616484522819519, - -0.7887967228889465, - 1.0649163722991943, - -0.7036169171333313, - -0.53633713722229, - 0.5039116740226746, - -1.068755865097046, - 0.9770159721374512, - -0.0986805111169815, - -0.6103026866912842, - -0.7727307677268982, - 1.0670206546783447, - -0.1344297081232071, - 0.8831673860549927, - -0.3481474816799164, - -0.9507979154586792, - -0.6021782755851746, - -0.316233366727829, - -0.4399321377277374, - -0.7524679899215698, - -1.0681201219558716, - 0.0025070905685424805, - -1.0655239820480347, - -0.9033966660499573, - -1.0659174919128418, - 0.9530443549156189, - 0.6668764352798462, - -0.022686710581183434, - -1.0680515766143799, - -0.5879814028739929, - -0.3055242598056793, - -1.055192232131958, - -0.2617500126361847, - 0.972791314125061, - 0.7608917951583862, - -0.8111482262611389, - -0.36944615840911865, - -0.17968153953552246, - -0.6388699412345886, - -0.15163138508796692, - 0.23894187808036804, - 0.9874148368835449, - 0.12505802512168884, - 0.9042628407478333, - 0.39737963676452637, - 0.2493443638086319, - -0.24252645671367645, - -1.0578539371490479, - 1.058831810951233, - 0.7352097034454346, - -0.3648087978363037, - 0.7921820878982544, - -1.0623674392700195, - -0.4980930685997009, - -0.902181088924408, - 0.22441914677619934, - 0.5842981934547424, - -0.7499602437019348, - 0.5000373721122742, - -1.0662062168121338, - -0.501523494720459, - -0.4749138057231903, - -1.024766206741333, - -0.3192199170589447, - 1.0143730640411377, - -1.066688060760498, - 1.0691475868225098, - -0.9273179173469543, - 0.19762927293777466, - 0.8937591314315796, - -0.6941763162612915, - -0.8835928440093994, - 0.3036836087703705, - 1.048621416091919, - 0.7913007140159607, - 0.03078795224428177, - -0.6579766869544983, - -1.017134666442871, - -1.048699140548706, - -0.9143375158309937, - -0.7038635611534119, - -0.46281829476356506, - -0.22165808081626892, - 0.0018745592096820474, - 0.19921603798866272, - 0.3677472174167633, - 0.5083256363868713, - 0.6235953569412231, - 0.7169206738471985, - 0.7917630076408386, - 0.8513522148132324, - 0.8985369801521301, - 0.9357430338859558, - 0.9649860858917236, - 0.9879133701324463, - 1.0058544874191284, - 1.019872784614563, - 1.0308138132095337, - 1.039345145225525, - 1.0459930896759033, - 1.0511704683303833, - 1.0552011728286743, - 1.058337926864624, - 1.0607783794403076, - 1.062677025794983, - 1.0641536712646484, - 1.0653018951416016, - 1.0661948919296265, - 1.0668891668319702, - 1.0674290657043457, - 1.067848801612854, - 1.0681750774383545, - 1.0684287548065186, - 1.0686259269714355, - 1.0687793493270874, - 1.0688985586166382, - 1.0689911842346191, - 1.0690631866455078, - 1.0691192150115967, - 1.0691627264022827, - 1.0691965818405151 - ], - [ - -1.0522708892822266, - 0.8074479699134827, - -0.05930701270699501, - -0.9720513820648193, - -0.7683091163635254, - -0.28062352538108826, - 0.17716597020626068, - -0.1693471074104309, - -0.7019533514976501, - -0.810597836971283, - 1.0010639429092407, - 0.511832594871521, - -0.7297235727310181, - 0.9114500880241394, - 1.0594686269760132, - 0.7560088634490967, - 0.49248647689819336, - -0.11486396938562393, - -0.08763059973716736, - 0.09781012684106827, - -0.4076384902000427, - 1.0337498188018799, - 1.0693141222000122, - 0.39584189653396606, - 0.5469351410865784, - -0.5337479710578918, - -0.9282023310661316, - 1.0691578388214111, - -0.013338646851480007, - -0.45949146151542664, - -0.8363955616950989, - 1.0673373937606812, - 0.8245336413383484, - -0.4650532007217407, - 1.0017105340957642, - 0.871248722076416, - 0.6515282988548279, - 1.0470679998397827, - 0.06952807307243347, - 0.8327017426490784, - 0.07713282108306885, - -0.7802234888076782, - -0.250789612531662, - 0.43741264939308167, - -0.1811583787202835, - -0.5117603540420532, - 0.022245237603783607, - 1.048467993736267, - -0.279379278421402, - -0.7821656465530396, - -0.3626629710197449, - -0.5641676187515259, - 1.0427840948104858, - -1.0301964282989502, - -0.6435174345970154, - 0.5800156593322754, - 0.9073381423950195, - -0.26376578211784363, - -0.18724913895130157, - 0.25334858894348145, - 0.14002013206481934, - -1.0156885385513306, - 1.0582853555679321, - 0.8772346377372742, - -0.3708774447441101, - -0.03786427900195122, - 0.39793872833251953, - 1.0576493740081787, - 0.9926570653915405, - -0.8748014569282532, - 1.0476127862930298, - -0.15314719080924988, - -0.6041578054428101, - -0.15124283730983734, - 0.9575930237770081, - 0.09992338716983795, - -0.6904867887496948, - 0.7637256383895874, - -0.45885878801345825, - -0.30973705649375916, - 1.0466406345367432, - -0.43449050188064575, - -0.9923649430274963, - 0.1386214792728424, - 1.0192415714263916, - 0.8406626582145691, - 0.2541821599006653, - -0.3625936806201935, - -0.8121644854545593, - -1.0353474617004395, - -1.0574856996536255, - -0.9368858933448792, - -0.7332826256752014, - -0.49414563179016113, - -0.2517779469490051, - -0.025355977937579155, - 0.17557725310325623, - 0.3477979004383087, - 0.49182772636413574, - 0.6101534366607666, - 0.706089437007904, - 0.7831081748008728, - 0.844480037689209, - 0.8931068181991577, - 0.93146812915802, - 0.9616302847862244, - 0.9852848649024963, - 1.0037990808486938, - 1.0182678699493408, - 1.0295616388320923, - 1.038369059562683, - 1.0452326536178589, - 1.0505784749984741, - 1.054740309715271, - 1.0579793453216553, - 1.0604995489120483, - 1.062459945678711, - 1.0639848709106445, - 1.0651706457138062, - 1.066092848777771, - 1.0668097734451294, - 1.0673673152923584, - 1.067800760269165, - 1.068137764930725, - 1.0683997869491577, - 1.068603515625, - 1.0687618255615234, - 1.0688848495483398 - ], - [ - 0.7204014658927917, - 0.525600016117096, - -0.9770510196685791, - 0.7394928932189941, - 0.16690370440483093, - 0.4391297399997711, - 1.0537045001983643, - 0.08546723425388336, - 0.46298733353614807, - 0.9334536194801331, - 0.1564042568206787, - -0.7443639039993286, - -0.7731164693832397, - 1.0602015256881714, - 0.8413293361663818, - -1.0686066150665283, - -0.23665851354599, - -0.07772661000490189, - 1.0395491123199463, - -1.0552698373794556, - 0.4698231816291809, - -0.8867855668067932, - 0.6746538877487183, - -0.4449033737182617, - -0.8517317771911621, - -0.7411206960678101, - -0.049967240542173386, - 1.0673035383224487, - -1.06899094581604, - 1.0490853786468506, - 0.4353052079677582, - 1.0520436763763428, - 0.6142278909683228, - -0.22120295464992523, - -1.0676320791244507, - 1.0654528141021729, - -0.4332176446914673, - -0.9001201391220093, - 1.016000509262085, - -0.9448631405830383, - 0.6953068971633911, - 0.0075360615737736225, - 0.16672539710998535, - -0.8109739422798157, - -1.0075379610061646, - -0.30941998958587646, - -1.027222752571106, - -0.9612756371498108, - 0.7525005340576172, - -1.0395455360412598, - 0.45829126238822937, - -0.775180459022522, - -0.9537095427513123, - -0.9586957693099976, - 1.0233770608901978, - 1.027299404144287, - -0.05741226673126221, - -0.820920467376709, - 0.10511433333158493, - 1.0414828062057495, - -0.6057157516479492, - 0.4534398913383484, - -0.9749553799629211, - -0.4304531514644623, - -0.017644371837377548, - -0.6018646955490112, - 1.0568652153015137, - -0.5990272164344788, - -0.6475985646247864, - 0.8947566151618958, - -0.8786530494689941, - 0.8552548885345459, - 1.0588085651397705, - 1.0686489343643188, - 0.5390958786010742, - -0.9962427616119385, - 0.4246912896633148, - -0.16798348724842072, - 0.3909064531326294, - -0.8968067765235901, - 0.9873525500297546, - 0.1017996296286583, - -1.0675429105758667, - -0.2595592439174652, - 0.8656955361366272, - 0.9876425862312317, - 0.4943186342716217, - -0.14270439743995667, - -0.6686892509460449, - -0.9774226546287537, - -1.0693081617355347, - -0.995083749294281, - -0.8163681030273438, - -0.5860204100608826, - -0.3419831693172455, - -0.10799416899681091, - 0.10319384932518005, - 0.2863234579563141, - 0.4407549202442169, - 0.5683994293212891, - 0.6723594665527344, - 0.7561036944389343, - 0.8230066299438477, - 0.8761200904846191, - 0.9180838465690613, - 0.9511165618896484, - 0.9770455360412598, - 0.9973538517951965, - 1.0132331848144531, - 1.0256330966949463, - 1.0353062152862549, - 1.0428462028503418, - 1.0487200021743774, - 1.0532935857772827, - 1.0568535327911377, - 1.0596235990524292, - 1.0617786645889282, - 1.0634549856185913, - 1.0647586584091187, - 1.0657724142074585, - 1.0665606260299683, - 1.0671736001968384, - 1.0676501989364624, - 1.0680205821990967, - 1.0683088302612305, - 1.0685327053070068, - 1.068706750869751, - 1.0688421726226807 - ] - ], - "sin": [ - [ - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0 - ], - [ - 0.899797260761261, - 0.8253079652786255, - 0.7500213384628296, - 0.6768491268157959, - 0.6075667142868042, - 0.5431534647941589, - 0.484048068523407, - 0.43033286929130554, - 0.38186508417129517, - 0.33836743235588074, - 0.29948970675468445, - 0.26484957337379456, - 0.23405882716178894, - 0.20674002170562744, - 0.18253596127033234, - 0.16111500561237335, - 0.1421731561422348, - 0.1254344880580902, - 0.11065026372671127, - 0.0975973978638649, - 0.08607665449380875, - 0.07591063529253006, - 0.06694166362285614, - 0.059029921889305115, - 0.05205157399177551, - 0.04589703306555748, - 0.04046940430998802, - 0.03568308800458908, - 0.031462475657463074, - 0.02774081937968731, - 0.024459224194288254, - 0.02156570367515087, - 0.019014401361346245, - 0.016764873638749123, - 0.014781438745558262, - 0.013032637536525726, - 0.011490718461573124, - 0.010131215676665306, - 0.008932551369071007, - 0.007875698618590832, - 0.006943884771317244, - 0.006122314836829901, - 0.005397948436439037, - 0.004759283270686865, - 0.0041961828246712685, - 0.003699705470353365, - 0.0032619694247841835, - 0.002876024227589369, - 0.002535742474719882, - 0.0022357215639203787, - 0.0019711980130523443, - 0.0017379724886268377, - 0.0015323410043492913, - 0.001351039158180356, - 0.001191188464872539, - 0.0010502507211640477, - 0.0009259883081540465, - 0.0008023518603295088, - 0.0006950091919861734, - 0.0006018353742547333, - 0.000520980276633054, - 0.0004508332349359989, - 0.00038999220123514533, - 0.00033723696833476424, - 0.00029150606133043766, - 0.00025187560822814703, - 0.00021754232875537127, - 0.0001878074835985899, - 0.00016206355940084904, - 0.00013978240895085037, - 0.00012050504301441833, - 0.00010383254266344011, - 8.94183904165402e-05, - 7.696157263126224e-05, - 6.620069325435907e-05, - 5.6908829719759524e-05, - 4.888900002697483e-05, - 4.1970270103774965e-05, - 3.600435593398288e-05, - 3.0862644052831456e-05, - 2.64336122199893e-05, - 2.2620593881583773e-05, - 1.9339824575581588e-05, - 1.6518732081749476e-05, - 1.4094472135184333e-05, - 1.201262875838438e-05, - 1.0591331374598667e-05, - 9.338196832686663e-06, - 8.233329936047085e-06, - 7.259188350872137e-06, - 6.400302936526714e-06, - 5.6430390031891875e-06, - 4.975372121407418e-06, - 4.386701220937539e-06, - 3.867680334224133e-06, - 3.4100678476534085e-06, - 3.0065993996686302e-06, - 2.6508676000958076e-06, - 2.337225168957957e-06, - 2.0606919406418456e-06, - 1.8168771021009888e-06, - 1.6019097301978036e-06, - 1.4123767186902114e-06, - 1.2452686632968835e-06, - 1.097932226912235e-06, - 9.680284165369812e-07, - 8.534941571269883e-07, - 7.525113119299931e-07, - 6.634764986301889e-07, - 5.849760214005073e-07, - 5.157634177521686e-07, - 4.5473990439859335e-07, - 4.0093647157846135e-07, - 3.534989048148418e-07, - 3.1167397196441016e-07, - 2.747976850514533e-07, - 2.422844715965766e-07, - 2.1361809388054098e-07, - 1.8834346349194675e-07, - 1.660592374719272e-07, - 1.4641162238149263e-07, - 1.2908864732708025e-07, - 1.1381527542653203e-07, - 1.0034899844413303e-07, - 8.847601407069305e-08, - 7.800780110756023e-08, - 6.877816360884026e-08, - 6.064053792442792e-08 - ], - [ - -0.635859489440918, - -1.062462568283081, - -1.0656331777572632, - -1.0071431398391724, - -0.3936627209186554, - 0.9363590478897095, - 1.064676284790039, - 0.015977798029780388, - 0.3718767762184143, - -0.6795372366905212, - 0.3736218810081482, - 0.9080803990364075, - -0.8379216194152832, - -0.9169039130210876, - -0.9397348165512085, - -0.6169819235801697, - -0.44258883595466614, - -0.8512248992919922, - -0.5031058192253113, - -0.5288947224617004, - -0.2184189260005951, - 0.975319504737854, - -0.9130740761756897, - 0.03623250871896744, - -1.0688577890396118, - -0.07159688323736191, - -0.9609305262565613, - -1.0679876804351807, - 0.9775938391685486, - -0.5509442090988159, - -0.5578064918518066, - 0.8580873608589172, - -0.5875248312950134, - 1.051590085029602, - 0.07831012457609177, - -0.3594764471054077, - 0.03596566617488861, - 0.957304835319519, - 0.3576805591583252, - -1.0140408277511597, - 1.0634406805038452, - -1.0628108978271484, - 1.0343239307403564, - -0.6205296516418457, - -0.38220784068107605, - 1.0686765909194946, - -0.07629266381263733, - -1.0690784454345703, - -0.30410149693489075, - 0.8109724521636963, - 1.0205000638961792, - 0.3907696306705475, - -0.42973271012306213, - -0.9568504691123962, - -1.0573790073394775, - -0.8251040577888489, - -0.4217204451560974, - 0.07286135107278824, - 0.4931860566139221, - 0.7935883402824402, - 0.9742691516876221, - 1.0563104152679443, - 1.0661516189575195, - 1.0279618501663208, - 0.9609063863754272, - 0.8789116144180298, - 0.7914536595344543, - 0.7046030759811401, - 0.6219906806945801, - 0.5455808639526367, - 0.4762464165687561, - 0.41417354345321655, - 0.35913822054862976, - 0.3106883764266968, - 0.26826128363609314, - 0.2312568575143814, - 0.1990809440612793, - 0.17117063701152802, - 0.1470067799091339, - 0.12611913681030273, - 0.10808715969324112, - 0.09253805875778198, - 0.07914348691701889, - 0.0676155835390091, - 0.057702917605638504, - 0.04918636754155159, - 0.04337019473314285, - 0.03824109956622124, - 0.033718131482601166, - 0.029729802161455154, - 0.026213016360998154, - 0.023112086579203606, - 0.02037789113819599, - 0.01796708256006241, - 0.015841437503695488, - 0.013967241160571575, - 0.012314759194850922, - 0.010857767425477505, - 0.00957314670085907, - 0.008440505713224411, - 0.0074418680742383, - 0.006561380811035633, - 0.00578506663441658, - 0.005100600887089968, - 0.004497116897255182, - 0.003965035080909729, - 0.0034959057811647654, - 0.003082282142713666, - 0.002717596711590886, - 0.002396059688180685, - 0.0021125655621290207, - 0.001862613600678742, - 0.0016422352055087686, - 0.0014479310484603047, - 0.0012766163563355803, - 0.001125571085140109, - 0.000992397079244256, - 0.000874979596119374, - 0.000771454768255353, - 0.0006801785784773529, - 0.0005997019470669329, - 0.0005287470994517207, - 0.0004661873681470752, - 0.0004110294976271689, - 0.00036239775363355875, - 0.0003195199533365667, - 0.0002817153581418097, - 0.00024838364333845675 - ], - [ - 0.8480159640312195, - 0.09714719653129578, - -1.0517702102661133, - 0.7267919182777405, - 0.41102248430252075, - 0.3243515193462372, - -0.158564954996109, - 0.5518509149551392, - -0.3906678557395935, - -0.4272622764110565, - -0.5782755613327026, - 1.029015302658081, - -1.030014991760254, - 0.17350749671459198, - 0.2610936164855957, - 1.0673950910568237, - -0.7894954681396484, - 0.8244131803512573, - -0.17988857626914978, - 1.0686874389648438, - 0.5302888751029968, - 1.0330133438110352, - -0.4289613664150238, - 0.9157713651657104, - -1.0048320293426514, - 0.9926228523254395, - 0.7878543734550476, - -0.015043829567730427, - -1.0689263343811035, - 0.11750099062919617, - 0.5926885008811951, - 0.10648989677429199, - -0.5015896558761597, - 0.8253670334815979, - -1.0686548948287964, - -0.038918860256671906, - 0.8994117379188538, - 1.0276010036468506, - -0.1644500195980072, - 1.0386731624603271, - -0.4502561688423157, - -0.7556703090667725, - -0.6926901340484619, - -1.0018326044082642, - -1.0534002780914307, - 0.8597016334533691, - 1.058967113494873, - 1.0416312217712402, - -0.4127710163593292, - 1.0617148876190186, - -0.5723935961723328, - -0.9933766722679138, - -1.040193796157837, - -1.0411441326141357, - 0.15733245015144348, - -0.1503962129354477, - 0.7758270502090454, - 1.005433440208435, - 0.7177289128303528, - 0.12229378521442413, - 0.946465790271759, - -0.5736451148986816, - 1.0454176664352417, - 0.8953782916069031, - 0.7624354362487793, - -0.9453148245811462, - 0.0816914290189743, - 0.9444968700408936, - 0.958138644695282, - 0.3055640161037445, - -1.0205191373825073, - 0.3382522761821747, - 0.07499263435602188, - 0.01882825791835785, - -0.5324051976203918, - 1.0508826971054077, - -0.5870917439460754, - -0.8133320212364197, - 0.6022734045982361, - 1.0252766609191895, - 0.20932331681251526, - -0.719236433506012, - -1.0688719749450684, - -0.8429020047187805, - -0.3299427032470703, - 0.20897124707698822, - 0.55446457862854, - 0.804996907711029, - 0.963970959186554, - 1.0460902452468872, - 1.0693130493164062, - 1.0505926609039307, - 1.0040879249572754, - 0.9407630562782288, - 0.8686534762382507, - 0.7933820486068726, - 0.7187086343765259, - 0.6470167636871338, - 0.5797093510627747, - 0.5175104737281799, - 0.4606889486312866, - 0.4092187285423279, - 0.3628910481929779, - 0.32139164209365845, - 0.2843526303768158, - 0.25138670206069946, - 0.22210857272148132, - 0.1961485892534256, - 0.17315985262393951, - 0.15282228589057922, - 0.13484381139278412, - 0.11896012723445892, - 0.10493354499340057, - 0.0925513207912445, - 0.08162368088960648, - 0.07198182493448257, - 0.0634758397936821, - 0.05597289279103279, - 0.04935538396239281, - 0.043519243597984314, - 0.03837253898382187, - 0.03383404016494751, - 0.029832005500793457, - 0.026303134858608246, - 0.023191550746560097, - 0.020447952672839165, - 0.018028859049081802, - 0.01589590683579445 - ], - [ - -0.08993688225746155, - 0.8796225190162659, - -0.8826319575309753, - 0.06393515318632126, - 0.902049720287323, - 0.7958520650863647, - 0.33628395199775696, - 0.8737166523933411, - -0.7219651341438293, - -0.09688730537891388, - -0.805206298828125, - 0.9250819087028503, - -0.9431368112564087, - -0.034566495567560196, - 0.43459609150886536, - 1.0647516250610352, - -0.878045916557312, - 0.7391353249549866, - -0.07000716775655746, - 1.060831069946289, - 0.602867603302002, - 1.0110526084899902, - -0.4893029034137726, - 0.883637547492981, - -1.0214844942092896, - 0.9746248722076416, - 0.7597538232803345, - -0.0505300909280777, - -1.0693117380142212, - 0.08995960652828217, - 0.5721087455749512, - 0.085169218480587, - -0.4849127233028412, - 0.8358886241912842, - -1.069074034690857, - -0.051960140466690063, - 0.8931471705436707, - 1.0247384309768677, - -0.17321480810642242, - 1.0367839336395264, - -0.44397181272506714, - -0.7513172030448914, - -0.696758508682251, - -1.0034657716751099, - -1.0541102886199951, - 0.8574842810630798, - 1.0585092306137085, - 1.0422767400741577, - -0.410421758890152, - 1.061976671218872, - -0.5707387328147888, - -0.9927352666854858, - -1.039837121963501, - -1.0414484739303589, - 0.156138077378273, - -0.14936228096485138, - 0.7764667868614197, - 1.0051610469818115, - 0.7182489037513733, - 0.12169402092695236, - 0.9462226629257202, - -0.5740237236022949, - 1.0454998016357422, - 0.8955610394477844, - 0.7622294425964355, - -0.9451965093612671, - 0.08147586137056351, - 0.9444089531898499, - 0.9580661654472351, - 0.3054310977458954, - -1.0205551385879517, - 0.33835095167160034, - 0.07490310817956924, - 0.018905749544501305, - -0.5324617624282837, - 1.0508933067321777, - -0.5870507955551147, - -0.8133592009544373, - 0.6022439002990723, - 1.0252854824066162, - 0.20934931933879852, - -0.7192198038101196, - -1.0688713788986206, - -0.8429120182991028, - -0.3299560546875, - 0.20895949006080627, - 0.5544553995132446, - 0.8049907088279724, - 0.9639674425125122, - 1.0460888147354126, - 1.0693130493164062, - 1.050593614578247, - 1.004089593887329, - 0.9407651424407959, - 0.8686556816101074, - 0.793384313583374, - 0.7187108397483826, - 0.6470188498497009, - 0.5797113180160522, - 0.5175122618675232, - 0.46069058775901794, - 0.40922024846076965, - 0.36289238929748535, - 0.32139280438423157, - 0.28435367345809937, - 0.2513876259326935, - 0.22210940718650818, - 0.1961493194103241, - 0.17316049337387085, - 0.15282288193702698, - 0.1348443180322647, - 0.11896057426929474, - 0.10493393987417221, - 0.09255167096853256, - 0.08162398636341095, - 0.07198208570480347, - 0.0634760782122612, - 0.0559731088578701, - 0.04935557022690773, - 0.043519411236047745, - 0.03837268799543381, - 0.03383416682481766, - 0.02983211912214756, - 0.026303235441446304, - 0.02319163829088211, - 0.020448030903935432, - 0.018028927966952324, - 0.015895968303084373 - ], - [ - 0.19015751779079437, - 0.7010433077812195, - -1.0676687955856323, - -0.4455896317958832, - 0.7437304854393005, - -1.0318354368209839, - 1.054535984992981, - 1.0558197498321533, - -0.8066569566726685, - 0.697398841381073, - -0.37590548396110535, - 0.9388617277145386, - -0.7816249132156372, - -0.5591893196105957, - 0.14477676153182983, - -0.7562305331230164, - -0.9491527676582336, - 1.0631276369094849, - 1.0657179355621338, - 1.0648319721221924, - -0.988567054271698, - -0.2734871804714203, - -0.0010927487164735794, - 0.9933494329452515, - 0.9188557267189026, - -0.926578164100647, - 0.5309184789657593, - -0.018315389752388, - -1.0692315101623535, - -0.9655576348304749, - 0.6662404537200928, - 0.06500007212162018, - 0.6808658242225647, - -0.9628912210464478, - 0.3741791248321533, - -0.619967520236969, - 0.8479061126708984, - -0.2169850766658783, - -1.067051887512207, - 0.670851469039917, - -1.0665291547775269, - -0.7312217354774475, - -1.039489507675171, - 0.9757582545280457, - 1.053857445716858, - 0.9389011263847351, - -1.069083333015442, - 0.21011583507061005, - -1.0321731567382812, - 0.7291439175605774, - -1.0059372186660767, - 0.9083770513534546, - 0.23671789467334747, - -0.28658193349838257, - -0.8540019392967224, - 0.8983405232429504, - -0.5658369064331055, - -1.0362728834152222, - -1.0527923107147217, - -1.0388689041137695, - -1.060107707977295, - -0.3343813121318817, - 0.15318618714809418, - 0.6114681959152222, - -1.0029376745224, - -1.0686441659927368, - 0.9925113916397095, - -0.15751725435256958, - 0.3975750207901001, - 0.6149442195892334, - -0.21433930099010468, - -1.0582910776138306, - -0.8822853565216064, - -1.0585649013519287, - -0.47586727142333984, - 1.0646357536315918, - -0.8164936304092407, - 0.748436450958252, - -0.965858519077301, - 1.0234729051589966, - -0.21903759241104126, - -0.9770629405975342, - 0.3983036279678345, - 1.0602915287017822, - 0.32338908314704895, - -0.6608481407165527, - -1.0386651754379272, - -1.0059621334075928, - -0.6955737471580505, - -0.26737523078918457, - 0.15861226618289948, - 0.5154403448104858, - 0.7782869338989258, - 0.9482901096343994, - 1.0392506122589111, - 1.069014072418213, - 1.0548015832901, - 1.011172890663147, - 0.9494943022727966, - 0.8781496286392212, - 0.803039014339447, - 0.7281314730644226, - 0.6559629440307617, - 0.5880426168441772, - 0.5251676440238953, - 0.4676550030708313, - 0.41550904512405396, - 0.36853957176208496, - 0.3264423906803131, - 0.2888544201850891, - 0.25538918375968933, - 0.225660502910614, - 0.1992960274219513, - 0.1759456992149353, - 0.15528593957424164, - 0.13702107965946198, - 0.1208832636475563, - 0.10663152486085892, - 0.09405004978179932, - 0.08294621855020523, - 0.07314864546060562, - 0.06450513750314713, - 0.05688077583909035, - 0.050156090408563614, - 0.044225383549928665, - 0.038995254784822464, - 0.034383151680231094, - 0.030316205695271492 - ], - [ - -0.7902251482009888, - 0.9312242269515991, - -0.4345173239707947, - -0.7723886370658875, - 1.0562087297439575, - 0.9749866724014282, - 0.18204641342163086, - 1.0658936500549316, - 0.9638862609863281, - -0.5216302871704102, - 1.057814598083496, - -0.767695426940918, - -0.7387320399284363, - -0.1393083930015564, - 0.6599991917610168, - -0.038907237350940704, - 1.042797565460205, - -1.0664860010147095, - -0.25054293870925903, - -0.17274115979671478, - 0.9605727791786194, - -0.5975326895713806, - 0.8296240568161011, - -0.9723656177520752, - 0.646519124507904, - -0.7708269357681274, - -1.0681465864181519, - 0.06555211544036865, - 0.026310738176107407, - -0.20701156556606293, - -0.9767002463340759, - -0.1914111226797104, - -0.8753045797348022, - 1.04618501663208, - 0.05996488779783249, - 0.09079860150814056, - -0.9776279926300049, - -0.5772501826286316, - 0.33343228697776794, - -0.500667154788971, - -0.8123929500579834, - -1.0692881345748901, - 1.056236982345581, - 0.6969614624977112, - 0.35819175839424133, - -1.023568868637085, - -0.2970648407936096, - 0.4683835506439209, - -0.759721577167511, - 0.25055745244026184, - -0.966127872467041, - -0.7365657687187195, - -0.4836031496524811, - 0.47364166378974915, - -0.3100537061691284, - -0.2967993915081024, - 1.0677722692489624, - -0.6852179169654846, - 1.0641357898712158, - -0.24237889051437378, - -0.8812164068222046, - 0.968414306640625, - 0.4391992390155792, - 0.9788482785224915, - -1.0691691637039185, - -0.8838511109352112, - -0.1626957207918167, - -0.8857767581939697, - -0.8509113788604736, - -0.5855292677879333, - 0.6094281077384949, - 0.6418512463569641, - -0.14952710270881653, - 0.03772813826799393, - 0.9234769344329834, - 0.38850247859954834, - -0.9813619256019592, - 1.0560375452041626, - -0.9953020215034485, - 0.5823843479156494, - 0.41057151556015015, - -1.0644580125808716, - -0.061531033366918564, - 1.0373345613479614, - 0.6276984214782715, - -0.4098731279373169, - -0.9481998682022095, - -1.059749722480774, - -0.8344391584396362, - -0.43368062376976013, - 0.003755486337468028, - 0.39146173000335693, - 0.6906352639198303, - 0.8944350481033325, - 1.013154149055481, - 1.063847303390503, - 1.0643237829208374, - 1.0302683115005493, - 0.9742530584335327, - 0.9057350754737854, - 0.8314846754074097, - 0.7561357021331787, - 0.6827107667922974, - 0.6130641102790833, - 0.548229992389679, - 0.48868316411972046, - 0.4345296621322632, - 0.3856413662433624, - 0.3417493999004364, - 0.3025076389312744, - 0.2675352394580841, - 0.23644381761550903, - 0.208854541182518, - 0.18440836668014526, - 0.16277137398719788, - 0.14363735914230347, - 0.12672804296016693, - 0.11179253458976746, - 0.09860574454069138, - 0.08696653693914413, - 0.07669580727815628, - 0.06763432174921036, - 0.059640903025865555, - 0.05259045213460922, - 0.04637227579951286, - 0.040888503193855286, - 0.03605266287922859, - 0.031788360327482224 - ] - ] - } - ] -} diff --git a/tests/data/yarn_reference_rocm.json b/tests/data/yarn_reference_rocm.json deleted file mode 100644 index 2d824eb2..00000000 --- a/tests/data/yarn_reference_rocm.json +++ /dev/null @@ -1,3497 +0,0 @@ -{ - "reference": "vLLM YaRNScalingRotaryEmbedding; torch cuda FP32; static factor=2", - "positions": [ - 0, - 1, - 4096, - 262143, - 262144, - 500000, - 524287 - ], - "cases": [ - { - "rot": 64, - "theta": 10000000.0, - "factor": 2.0, - "original": 262144, - "magnitude": 1.0693147180559945, - "inv_freq": [ - 1.0, - 0.6042963862419128, - 0.36517414450645447, - 0.22067342698574066, - 0.1333521455526352, - 0.08058421313762665, - 0.04869674891233444, - 0.02942727319896221, - 0.017782794311642647, - 0.010746078565716743, - 0.006493816152215004, - 0.003924190066754818, - 0.0023713738191872835, - 0.0014330126577988267, - 0.0008659643353894353, - 0.0004905929090455174, - 0.0002766992838587612, - 0.0001552649337099865, - 8.660864841658622e-05, - 4.797585279447958e-05, - 2.635603232192807e-05, - 1.4334171282825992e-05, - 7.699632078583818e-06, - 4.6528602979378775e-06, - 2.81170673588349e-06, - 1.6991041320579825e-06, - 1.0267624475090997e-06, - 6.204688816069392e-07, - 3.7494712046282075e-07, - 2.2657918918866926e-07, - 1.3692098832507327e-07, - 8.274085416815069e-08 - ], - "cos": [ - [ - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646 - ], - [ - 0.5777531862258911, - 0.8799412846565247, - 0.998805820941925, - 1.043384075164795, - 1.0598211288452148, - 1.0658446550369263, - 1.068047046661377, - 1.0688517093658447, - 1.069145679473877, - 1.0692529678344727, - 1.0692921876907349, - 1.069306492805481, - 1.0693117380142212, - 1.069313645362854, - 1.0693143606185913, - 1.0693145990371704, - 1.0693145990371704, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646 - ], - [ - 0.8597189784049988, - 0.9942150712013245, - 1.0025675296783447, - 0.6644225120544434, - 0.9734212756156921, - -1.0467698574066162, - -0.031272951513528824, - 0.4332946836948395, - -0.8934475779533386, - 1.0687097311019897, - 0.11192997545003891, - -0.9986739158630371, - -1.025161623954773, - 0.9791648387908936, - -0.9826422333717346, - -0.4541783630847931, - 0.4529813230037689, - 0.860262393951416, - 1.002732515335083, - 1.0487347841262817, - 1.0630898475646973, - 1.067472219467163, - 1.068782925605774, - 1.0691205263137817, - 1.0692437887191772, - 1.0692888498306274, - 1.0693053007125854, - 1.069311261177063, - 1.069313406944275, - 1.0693142414093018, - 1.0693144798278809, - 1.0693145990371704 - ], - [ - -0.6513853669166565, - 0.9871648550033569, - -0.9953956604003906, - 0.2952715754508972, - -0.7212009429931641, - 0.9285621643066406, - -0.36669492721557617, - -0.02881699800491333, - 0.9443737268447876, - -0.5783533453941345, - 0.9698436856269836, - -0.18366917967796326, - 0.9864349961280823, - 0.2478526532649994, - 0.7358847260475159, - -1.0480421781539917, - -1.0282152891159058, - -1.0589878559112549, - -0.8090404868125916, - 1.0692594051361084, - 0.8666337728500366, - -0.8727642893791199, - -0.4628109037876129, - 0.3677518963813782, - 0.791765034198761, - 0.9649868607521057, - 1.0308140516281128, - 1.0552012920379639, - 1.0641536712646484, - 1.0674290657043457, - 1.0686259269714355, - 1.0690631866455078 - ], - [ - -1.0655258893966675, - 0.5742302536964417, - -0.7887967228889465, - 0.511264443397522, - -0.6103026866912842, - 0.8831673860549927, - -0.317230761051178, - 0.0025070905685424805, - 0.9530444145202637, - -0.5879814028739929, - 0.9727371335029602, - -0.1795528680086136, - 0.9874147772789001, - 0.2493443638086319, - 0.7352097034454346, - -1.0481456518173218, - -1.0281345844268799, - -1.0590109825134277, - -0.8089804649353027, - 1.0692589282989502, - 0.8666173219680786, - -0.8727554678916931, - -0.4628182649612427, - 0.3677472472190857, - 0.7917630076408386, - 0.9649860858917236, - 1.0308136940002441, - 1.0552011728286743, - 1.0641535520553589, - 1.0674290657043457, - 1.0686259269714355, - 1.0690631866455078 - ], - [ - -1.0522708892822266, - -0.7683091163635254, - -0.7019533514976501, - -0.7174220681190491, - 0.49248647689819336, - -0.4076384902000427, - 0.5487287640571594, - -0.013338646851480007, - 0.8245336413383484, - 0.6515282988548279, - 0.07687243074178696, - -0.18141566216945648, - -0.279379278421402, - 1.0427840948104858, - 0.9073381423950195, - 1.0354822874069214, - 1.0616806745529175, - -0.6585246324539185, - 0.832831859588623, - 0.44185671210289, - 0.8754527568817139, - 0.6780954003334045, - -0.8121644854545593, - -0.7332826256752014, - 0.17557725310325623, - 0.706089437007904, - 0.93146812915802, - 1.0182678699493408, - 1.0505784749984741, - 1.062459945678711, - 1.0668097734451294, - 1.0683997869491577 - ], - [ - 0.7204014658927917, - 0.16690370440483093, - 0.46298733353614807, - -0.7614797949790955, - -0.23665851354599, - 0.4698231816291809, - -0.8504674434661865, - -1.06899094581604, - 0.614227831363678, - -0.4332176446914673, - 0.6951085329055786, - -1.0076253414154053, - 0.7525005340576172, - -0.9537095427513123, - -0.05741226673126221, - 0.9852739572525024, - 0.9079210758209229, - 1.0282537937164307, - 0.15483182668685913, - 1.0690927505493164, - 0.33539828658103943, - 0.35535430908203125, - -0.6686892509460449, - -0.8163681030273438, - 0.10319384932518005, - 0.6723594665527344, - 0.9180837869644165, - 1.0132330656051636, - 1.0487200021743774, - 1.0617786645889282, - 1.0665606260299683, - 1.0683088302612305 - ] - ], - "sin": [ - [ - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0 - ], - [ - 0.899797260761261, - 0.6075666546821594, - 0.38186508417129517, - 0.23405885696411133, - 0.1421731561422348, - 0.08607665449380875, - 0.052051570266485214, - 0.031462475657463074, - 0.019014401361346245, - 0.011490718461573124, - 0.006943884305655956, - 0.004196183290332556, - 0.002535742474719882, - 0.0015323410043492913, - 0.0009259883081540465, - 0.0005245982320047915, - 0.00029587862081825733, - 0.00016602707910351455, - 9.26119028008543e-05, - 5.130128556629643e-05, - 2.818289249262307e-05, - 1.532773967483081e-05, - 8.233329936047085e-06, - 4.975372121407418e-06, - 3.0065993996686302e-06, - 1.8168771021009888e-06, - 1.097932226912235e-06, - 6.634764986301889e-07, - 4.0093647157846135e-07, - 2.422844715965766e-07, - 1.4641162238149263e-07, - 8.847601407069305e-08 - ], - [ - -0.6358594298362732, - -0.3936627209186554, - 0.3718767762184143, - -0.8378404974937439, - -0.44258883595466614, - -0.2184189260005951, - -1.0688573122024536, - 0.9775937795639038, - -0.5875248312950134, - 0.03596566617488861, - 1.0634404420852661, - -0.38220974802970886, - -0.30410149693489075, - -0.42973271012306213, - -0.4217204451560974, - 0.9680681824684143, - 0.9686288237571716, - 0.6351240277290344, - 0.3714318573474884, - 0.20878027379512787, - 0.11521304398775101, - 0.06274636089801788, - 0.033718131482601166, - 0.02037789113819599, - 0.012314759194850922, - 0.0074418680742383, - 0.004497116897255182, - 0.002717596711590886, - 0.0016422352055087686, - 0.000992397079244256, - 0.0005997019470669329, - 0.00036239775363355875 - ], - [ - 0.8480159640312195, - 0.41102248430252075, - -0.3906678557395935, - -1.0277395248413086, - -0.7894955277442932, - 0.5302888751029968, - -1.004474401473999, - -1.0689263343811035, - -0.5015896558761597, - 0.899411678314209, - -0.4503745436668396, - -1.0534228086471558, - -0.4127710163593292, - -1.040193796157837, - 0.7758269906044006, - 0.21222993731498718, - -0.2936108708381653, - 0.14825202524662018, - -0.6992049217224121, - 0.010867959819734097, - 0.6264023184776306, - -0.6178320646286011, - 0.963970959186554, - 1.0040879249572754, - 0.7187086343765259, - 0.4606889486312866, - 0.2843526303768158, - 0.17315985262393951, - 0.10493354499340057, - 0.0634758397936821, - 0.03837253898382187, - 0.023191550746560097 - ], - [ - -0.08993688225746155, - 0.9020496606826782, - -0.7219651341438293, - -0.9391712546348572, - -0.8780458569526672, - 0.602867603302002, - -1.0211750268936157, - -1.0693117380142212, - -0.4849127233028412, - 0.8931471109390259, - -0.4440905451774597, - -1.0541322231292725, - -0.410421758890152, - -1.039837121963501, - 0.7764667868614197, - 0.21171817183494568, - -0.2938932478427887, - 0.14808638393878937, - -0.6992743611335754, - 0.010918946005403996, - 0.62642502784729, - -0.6178445219993591, - 0.9639674425125122, - 1.004089593887329, - 0.7187108397483826, - 0.46069058775901794, - 0.28435367345809937, - 0.17316049337387085, - 0.10493393987417221, - 0.0634760782122612, - 0.03837268799543381, - 0.02319163829088211 - ], - [ - 0.19015751779079437, - 0.7437305450439453, - -0.8066569566726685, - -0.7929309606552124, - -0.9491528272628784, - -0.988567054271698, - 0.9177857637405396, - -1.0692315101623535, - 0.6808658242225647, - 0.8479061126708984, - -1.0665479898452759, - 1.0538132190704346, - -1.0321730375289917, - 0.23671789467334747, - -0.5658369660377502, - 0.2668529450893402, - 0.12754622101783752, - 0.8424839973449707, - -0.6706900596618652, - -0.9737538695335388, - 0.6140166521072388, - 0.8268135786056519, - -0.6955736875534058, - 0.7782869338989258, - 1.0548017024993896, - 0.803039014339447, - 0.5251676440238953, - 0.3264423906803131, - 0.1992960274219513, - 0.1208832636475563, - 0.07314864546060562, - 0.044225383549928665 - ], - [ - -0.7902251482009888, - 1.056208848953247, - 0.9638862609863281, - -0.750721275806427, - 1.042797565460205, - 0.9605727791786194, - 0.6481813788414001, - 0.026310738176107407, - -0.8753045797348022, - -0.9776279330253601, - -0.8125626444816589, - 0.3579457700252533, - -0.759721577167511, - -0.4836031496524811, - 1.067772388458252, - -0.4155347943305969, - 0.5649010539054871, - -0.29347580671310425, - 1.0580458641052246, - 0.02178577519953251, - 1.0153530836105347, - 1.0085421800613403, - -0.8344391584396362, - 0.6906352639198303, - 1.0643237829208374, - 0.8314846754074097, - 0.548229992389679, - 0.3417493999004364, - 0.208854541182518, - 0.12672804296016693, - 0.07669580727815628, - 0.04637227579951286 - ] - ] - }, - { - "rot": 128, - "theta": 10000000.0, - "factor": 2.0, - "original": 262144, - "magnitude": 1.0693147180559945, - "inv_freq": [ - 1.0, - 0.7773650288581848, - 0.6042963862419128, - 0.46975892782211304, - 0.36517414450645447, - 0.283873587846756, - 0.22067342698574066, - 0.17154379189014435, - 0.1333521455526352, - 0.10366329550743103, - 0.08058421313762665, - 0.06264335662126541, - 0.04869674891233444, - 0.037855152040719986, - 0.02942727319896221, - 0.02287573181092739, - 0.017782794311642647, - 0.01382372248917818, - 0.010746078565716743, - 0.008353625424206257, - 0.006493816152215004, - 0.005048065446317196, - 0.003924190066754818, - 0.0030505280010402203, - 0.0023713738191872835, - 0.0018434228841215372, - 0.0014330126577988267, - 0.0011139739071950316, - 0.0008659643353894353, - 0.0006507313228212297, - 0.0004884125082753599, - 0.0003661149530671537, - 0.0002740640484262258, - 0.00020485366985667497, - 0.0001528762368252501, - 0.00011388894927222282, - 8.468401210848242e-05, - 6.283809489104897e-05, - 4.6522036427631974e-05, - 3.4356373362243176e-05, - 2.5301789719378576e-05, - 1.8576018192106858e-05, - 1.3590916751127224e-05, - 9.904783837555442e-06, - 7.699632078583818e-06, - 5.98542510488187e-06, - 4.6528602979378775e-06, - 3.6169706163491355e-06, - 2.81170673588349e-06, - 2.1857224510313245e-06, - 1.6991041320579825e-06, - 1.3208241398388054e-06, - 1.0267624475090997e-06, - 7.981693102010468e-07, - 6.204688816069392e-07, - 4.823307904189278e-07, - 3.7494712046282075e-07, - 2.9147076929803006e-07, - 2.2657918918866926e-07, - 1.761347334650054e-07, - 1.3692098832507327e-07, - 1.0643758230344247e-07, - 8.274085416815069e-08, - 6.43198490024588e-08 - ], - "cos": [ - [ - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646 - ], - [ - 0.5777531862258911, - 0.7621692419052124, - 0.8799412846565247, - 0.9534838199615479, - 0.998805820941925, - 1.0265183448791504, - 1.043384075164795, - 1.0536197423934937, - 1.0598211288452148, - 1.0635744333267212, - 1.0658446550369263, - 1.067217230796814, - 1.068047046661377, - 1.068548560142517, - 1.0688517093658447, - 1.0690349340438843, - 1.069145679473877, - 1.069212555885315, - 1.0692529678344727, - 1.0692774057388306, - 1.0692921876907349, - 1.0693011283874512, - 1.069306492805481, - 1.0693097114562988, - 1.0693117380142212, - 1.0693128108978271, - 1.069313645362854, - 1.0693141222000122, - 1.0693143606185913, - 1.0693144798278809, - 1.0693145990371704, - 1.0693145990371704, - 1.0693145990371704, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646 - ], - [ - 0.8597189784049988, - 0.08865529298782349, - 0.9942150712013245, - 0.09923054277896881, - 1.0025675296783447, - 1.0019184350967407, - 0.6644225120544434, - 0.5102278590202332, - 0.9734212756156921, - -0.9435669183731079, - -1.0467698574066162, - 0.5565336346626282, - -0.031272951513528824, - -0.46909114718437195, - 0.4332946836948395, - 0.9122970104217529, - -0.8934475779533386, - 1.0664434432983398, - 1.0687097311019897, - -1.0077195167541504, - 0.11192997545003891, - -0.2713061571121216, - -0.9986739158630371, - 1.066589593887329, - -1.025161623954773, - 0.3193954527378082, - 0.9791648387908936, - -0.15932254493236542, - -0.9826422333717346, - -0.9503476619720459, - -0.44551461935043335, - 0.07605966925621033, - 0.46341004967689514, - 0.7144595980644226, - 0.8664352297782898, - 0.9550614356994629, - 1.005629301071167, - 1.0340903997421265, - 1.049959421157837, - 1.0587443113327026, - 1.06357741355896, - 1.0662208795547485, - 1.0676583051681519, - 1.0684348344802856, - 1.068782925605774, - 1.068993330001831, - 1.0691205263137817, - 1.069197416305542, - 1.0692437887191772, - 1.0692719221115112, - 1.0692888498306274, - 1.0692989826202393, - 1.0693053007125854, - 1.0693089962005615, - 1.069311261177063, - 1.069312572479248, - 1.069313406944275, - 1.0693140029907227, - 1.0693142414093018, - 1.0693144798278809, - 1.0693144798278809, - 1.0693145990371704, - 1.0693145990371704, - 1.0693145990371704 - ], - [ - -0.6513853669166565, - -0.19290786981582642, - 0.9871648550033569, - 1.059841275215149, - -0.9953956604003906, - -0.8994616866111755, - 0.2952715754508972, - 1.0369493961334229, - -0.7212009429931641, - 1.054075002670288, - 0.9285621643066406, - -0.9795029759407043, - -0.36669492721557617, - -0.7229933142662048, - -0.02881699800491333, - -0.8900305032730103, - 0.9443737268447876, - -0.03756128251552582, - -0.5783533453941345, - -1.056593656539917, - 0.9698436856269836, - -0.8147097826004028, - -0.18366917967796326, - -0.14840036630630493, - 0.9864349961280823, - 0.9032161831855774, - 0.2478526532649994, - -1.057676911354065, - 0.7358847260475159, - 0.6318320631980896, - -0.7666455507278442, - -0.16602759063243866, - -0.9795535802841187, - -1.023470401763916, - -0.7711856365203857, - 0.010757197625935078, - -1.046228289604187, - -0.7716785073280334, - 0.9965856075286865, - -0.9770345091819763, - 1.0046676397323608, - 0.16738805174827576, - -0.9758681058883667, - -0.9143319725990295, - -0.4628109037876129, - 0.0018809328321367502, - 0.3677518963813782, - 0.623598575592041, - 0.791765034198761, - 0.8985382914543152, - 0.9649868607521057, - 1.005854845046997, - 1.0308140516281128, - 1.0459932088851929, - 1.0552012920379639, - 1.0607784986495972, - 1.0641536712646484, - 1.0661948919296265, - 1.0674290657043457, - 1.0681750774383545, - 1.0686259269714355, - 1.0688985586166382, - 1.0690631866455078, - 1.0691627264022827 - ], - [ - -1.0655258893966675, - 0.6036512851715088, - 0.5742302536964417, - 1.0096819400787354, - -0.7887967228889465, - -0.7036169171333313, - 0.511264443397522, - 0.9770159721374512, - -0.6103026866912842, - 1.0670206546783447, - 0.8831673860549927, - -0.9507979154586792, - -0.317230761051178, - -0.7524679899215698, - 0.0025070905685424805, - -0.9033966660499573, - 0.9530444145202637, - -0.022686710581183434, - -0.5879814028739929, - -1.055192232131958, - 0.9727371335029602, - -0.8112333416938782, - -0.1795528680086136, - -0.15163138508796692, - 0.9874147772789001, - 0.9042627811431885, - 0.2493443638086319, - -1.0578539371490479, - 0.7352097034454346, - 0.6312658786773682, - -0.767009437084198, - -0.16641440987586975, - -0.97967129945755, - -1.0234065055847168, - -0.7712987065315247, - 0.01087956503033638, - -1.046209692955017, - -0.7716318964958191, - 0.9966037273406982, - -0.9770494103431702, - 1.0046583414077759, - 0.16740769147872925, - -0.9758622050285339, - -0.9143375158309937, - -0.4628182649612427, - 0.0018745592096820474, - 0.3677472472190857, - 0.6235954761505127, - 0.7917630076408386, - 0.8985370397567749, - 0.9649860858917236, - 1.0058543682098389, - 1.0308136940002441, - 1.0459929704666138, - 1.0552011728286743, - 1.0607784986495972, - 1.0641535520553589, - 1.0661948919296265, - 1.0674290657043457, - 1.0681750774383545, - 1.0686259269714355, - 1.0688985586166382, - 1.0690631866455078, - 1.0691627264022827 - ], - [ - -1.0522708892822266, - -0.05930701270699501, - -0.7683091163635254, - 0.14413057267665863, - -0.7019533514976501, - 1.0010639429092407, - -0.7174220681190491, - 1.0594686269760132, - 0.49248647689819336, - -0.08763059973716736, - -0.4076384902000427, - 1.0693141222000122, - 0.5487287640571594, - -0.9282023310661316, - -0.013338646851480007, - -0.8363955616950989, - 0.8245336413383484, - 1.0017105340957642, - 0.6515282988548279, - 0.06952807307243347, - 0.07687243074178696, - -0.2510434091091156, - -0.18141566216945648, - 0.022245239466428757, - -0.279379278421402, - -0.3626629710197449, - 1.0427840948104858, - -0.6435174345970154, - 0.9073381423950195, - 0.22376880049705505, - 0.7153396606445312, - 0.709652841091156, - 0.3894186317920685, - -0.3415154218673706, - 0.5414062738418579, - 0.9866420030593872, - -0.07425130903720856, - 1.0693094730377197, - -0.31695008277893066, - -0.10736316442489624, - 1.0654971599578857, - -1.0593290328979492, - 0.9320496320724487, - 0.2541821599006653, - -0.8121644854545593, - -1.0574856996536255, - -0.7332826256752014, - -0.2517778277397156, - 0.17557725310325623, - 0.49182775616645813, - 0.706089437007904, - 0.844480037689209, - 0.93146812915802, - 0.9852848052978516, - 1.0182678699493408, - 1.038369059562683, - 1.0505784749984741, - 1.0579793453216553, - 1.062459945678711, - 1.0651706457138062, - 1.0668097734451294, - 1.067800760269165, - 1.0683997869491577, - 1.0687618255615234 - ], - [ - 0.7204014658927917, - -0.9770510196685791, - 0.16690370440483093, - 1.047502040863037, - 0.46298733353614807, - 0.1564042568206787, - -0.7614797949790955, - 0.8413293957710266, - -0.23665851354599, - 1.0395491123199463, - 0.4698231816291809, - 0.6746538877487183, - -0.8504674434661865, - -0.049967240542173386, - -1.06899094581604, - 0.4353052079677582, - 0.614227831363678, - -1.0676320791244507, - -0.4332176446914673, - 1.0160003900527954, - 0.6951085329055786, - 0.16698326170444489, - -1.0076253414154053, - -1.0272226333618164, - 0.7525005340576172, - 0.45829126238822937, - -0.9537095427513123, - 1.0233770608901978, - -0.05741226673126221, - -0.3233306407928467, - 0.030500834807753563, - -1.017637848854065, - 0.7255557179450989, - 0.8897461295127869, - 0.043200526386499405, - -1.0690957307815552, - 0.9779296517372131, - 0.044390324503183365, - 0.7883260846138, - 0.716148316860199, - 0.8185259699821472, - -1.016903281211853, - 0.7118502855300903, - 0.4943186342716217, - -0.6686892509460449, - -1.0693081617355347, - -0.8163681030273438, - -0.34198302030563354, - 0.10319384932518005, - 0.4407549500465393, - 0.6723594665527344, - 0.8230066299438477, - 0.9180837869644165, - 0.9770455360412598, - 1.0132330656051636, - 1.0353060960769653, - 1.0487200021743774, - 1.0568535327911377, - 1.0617786645889282, - 1.0647586584091187, - 1.0665606260299683, - 1.0676501989364624, - 1.0683088302612305, - 1.068706750869751 - ] - ], - "sin": [ - [ - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0 - ], - [ - 0.899797260761261, - 0.7500213384628296, - 0.6075666546821594, - 0.48404812812805176, - 0.38186508417129517, - 0.29948970675468445, - 0.23405885696411133, - 0.18253596127033234, - 0.1421731561422348, - 0.11065026372671127, - 0.08607665449380875, - 0.06694166362285614, - 0.052051570266485214, - 0.04046940430998802, - 0.031462475657463074, - 0.024459224194288254, - 0.019014401361346245, - 0.014781438745558262, - 0.011490718461573124, - 0.008932551369071007, - 0.006943884305655956, - 0.00539794797077775, - 0.004196183290332556, - 0.0032619694247841835, - 0.002535742474719882, - 0.0019711980130523443, - 0.0015323410043492913, - 0.001191188464872539, - 0.0009259883081540465, - 0.0006958364974707365, - 0.0005222666659392416, - 0.00039149209624156356, - 0.00029306072974577546, - 0.0002190530503867194, - 0.00016347281052730978, - 0.00012178313045296818, - 9.055386180989444e-05, - 6.719370139762759e-05, - 4.9746697186492383e-05, - 3.6737776099471375e-05, - 2.7055575628764927e-05, - 1.986360985029023e-05, - 1.4532967725244816e-05, - 1.0591331374598667e-05, - 8.233329936047085e-06, - 6.400302936526714e-06, - 4.975372121407418e-06, - 3.867679879476782e-06, - 3.0065993996686302e-06, - 2.337225168957957e-06, - 1.8168771021009888e-06, - 1.4123767186902114e-06, - 1.097932226912235e-06, - 8.534942139704071e-07, - 6.634764986301889e-07, - 5.157634177521686e-07, - 4.0093647157846135e-07, - 3.1167397196441016e-07, - 2.422844715965766e-07, - 1.8834346349194675e-07, - 1.4641162238149263e-07, - 1.1381527542653203e-07, - 8.847601407069305e-08, - 6.877816360884026e-08 - ], - [ - -0.6358594298362732, - -1.0656332969665527, - -0.3936627209186554, - 1.0647004842758179, - 0.3718767762184143, - 0.3736218810081482, - -0.8378404974937439, - -0.9397347569465637, - -0.44258883595466614, - -0.5031058192253113, - -0.2184189260005951, - -0.9130740761756897, - -1.0688573122024536, - -0.9609304666519165, - 0.9775937795639038, - -0.5578064918518066, - -0.5875248312950134, - 0.07831012457609177, - 0.03596566617488861, - 0.3576805591583252, - 1.0634404420852661, - 1.0343244075775146, - -0.38220974802970886, - -0.07629266381263733, - -0.30410149693489075, - 1.0205000638961792, - -0.42973271012306213, - -1.0573790073394775, - -0.4217204451560974, - 0.490176796913147, - 0.97208571434021, - 1.0666062831878662, - 0.9636830687522888, - 0.7956013083457947, - 0.6266769170761108, - 0.48092782497406006, - 0.36351555585861206, - 0.2721966505050659, - 0.2025315761566162, - 0.14998175203800201, - 0.1106213703751564, - 0.08128286153078079, - 0.05949629470705986, - 0.04337019473314285, - 0.033718131482601166, - 0.026213016360998154, - 0.02037789113819599, - 0.015841437503695488, - 0.012314759194850922, - 0.00957314670085907, - 0.0074418680742383, - 0.00578506663441658, - 0.004497116897255182, - 0.003495906013995409, - 0.002717596711590886, - 0.0021125655621290207, - 0.0016422352055087686, - 0.0012766163563355803, - 0.000992397079244256, - 0.000771454768255353, - 0.0005997019470669329, - 0.0004661873681470752, - 0.00036239775363355875, - 0.0002817153581418097 - ], - [ - 0.8480159640312195, - -1.0517702102661133, - 0.41102248430252075, - -0.14202295243740082, - -0.3906678557395935, - -0.5782755017280579, - -1.0277395248413086, - 0.2610936164855957, - -0.7894955277442932, - -0.17988857626914978, - 0.5302888751029968, - -0.4289613664150238, - -1.004474401473999, - 0.7878543734550476, - -1.0689263343811035, - 0.5926885008811951, - -0.5015896558761597, - -1.0686547756195068, - 0.899411678314209, - -0.1644500195980072, - -0.4503745436668396, - -0.6925907135009766, - -1.0534228086471558, - 1.058967113494873, - -0.4127710163593292, - -0.5723935961723328, - -1.040193796157837, - 0.1573324352502823, - 0.7758269906044006, - 0.8626831769943237, - 0.745445191860199, - 1.0563468933105469, - 0.42884573340415955, - -0.3097456097602844, - 0.740747332572937, - -1.069260597229004, - -0.22099833190441132, - -0.7402339577674866, - -0.38762226700782776, - 0.43455445766448975, - 0.3661651909351349, - -1.056132197380066, - -0.43716734647750854, - 0.55446457862854, - 0.963970959186554, - 1.0693130493164062, - 1.0040879249572754, - 0.868653416633606, - 0.7187086343765259, - 0.5797093510627747, - 0.4606889486312866, - 0.3628910481929779, - 0.2843526303768158, - 0.22210858762264252, - 0.17315985262393951, - 0.13484381139278412, - 0.10493354499340057, - 0.08162368088960648, - 0.0634758397936821, - 0.04935538396239281, - 0.03837253898382187, - 0.029832005500793457, - 0.023191550746560097, - 0.018028859049081802 - ], - [ - -0.08993688225746155, - -0.8826319575309753, - 0.9020496606826782, - 0.3521025776863098, - -0.7219651341438293, - -0.805206298828125, - -0.9391712546348572, - 0.43459609150886536, - -0.8780458569526672, - -0.07000717520713806, - 0.602867603302002, - -0.4893029034137726, - -1.0211750268936157, - 0.7597538232803345, - -1.0693117380142212, - 0.5721087455749512, - -0.4849127233028412, - -1.069074034690857, - 0.8931471109390259, - -0.17321480810642242, - -0.4440905451774597, - -0.6966595649719238, - -1.0541322231292725, - 1.0585092306137085, - -0.410421758890152, - -0.5707387328147888, - -1.039837121963501, - 0.15613806247711182, - 0.7764667868614197, - 0.8630974888801575, - 0.74507075548172, - 1.0562859773635864, - 0.42857667803764343, - -0.30995646119117737, - 0.7406296730041504, - -1.0692594051361084, - -0.22108612954616547, - -0.7402825355529785, - -0.3875757157802582, - 0.4345209002494812, - 0.3661905825138092, - -1.0561290979385376, - -0.43718060851097107, - 0.5544554591178894, - 0.9639674425125122, - 1.0693130493164062, - 1.004089593887329, - 0.8686556816101074, - 0.7187108397483826, - 0.5797113180160522, - 0.46069058775901794, - 0.36289238929748535, - 0.28435367345809937, - 0.22210942208766937, - 0.17316049337387085, - 0.1348443180322647, - 0.10493393987417221, - 0.08162398636341095, - 0.0634760782122612, - 0.04935557022690773, - 0.03837268799543381, - 0.02983211912214756, - 0.02319163829088211, - 0.018028927966952324 - ], - [ - 0.19015751779079437, - -1.0676687955856323, - 0.7437305450439453, - 1.0595566034317017, - -0.8066569566726685, - -0.37590548396110535, - -0.7929309606552124, - 0.14477676153182983, - -0.9491528272628784, - 1.0657180547714233, - -0.988567054271698, - -0.0010927487164735794, - 0.9177857637405396, - 0.5309184193611145, - -1.0692315101623535, - 0.6662405133247375, - 0.6808658242225647, - 0.3741791546344757, - 0.8479061126708984, - -1.067051887512207, - -1.0665479898452759, - -1.0394282341003418, - 1.0538132190704346, - -1.069083333015442, - -1.0321730375289917, - -1.005937099456787, - 0.23671789467334747, - -0.8540018796920776, - -0.5658369660377502, - -1.0456392765045166, - -0.7948101162910461, - 0.7998917102813721, - -0.9958850741386414, - 1.0133119821548462, - 0.9221243262290955, - 0.41227594017982483, - -1.066733717918396, - 0.003338152775540948, - -1.021262288093567, - -1.0639111995697021, - 0.09027571231126785, - 0.1457938700914383, - 0.5241350531578064, - -1.0386651754379272, - -0.6955736875534058, - 0.15861226618289948, - 0.7782869338989258, - 1.0392506122589111, - 1.0548017024993896, - 0.9494943022727966, - 0.803039014339447, - 0.6559628844261169, - 0.5251676440238953, - 0.41550907492637634, - 0.3264423906803131, - 0.2553892135620117, - 0.1992960274219513, - 0.15528593957424164, - 0.1208832636475563, - 0.09405004978179932, - 0.07314864546060562, - 0.05688077583909035, - 0.044225383549928665, - 0.034383151680231094 - ], - [ - -0.7902251482009888, - -0.4345173239707947, - 1.056208848953247, - 0.21488043665885925, - 0.9638862609863281, - 1.057814598083496, - -0.750721275806427, - 0.6599991917610168, - 1.042797565460205, - -0.25054293870925903, - 0.9605727791786194, - 0.8296240568161011, - 0.6481813788414001, - -1.0681465864181519, - 0.026310738176107407, - -0.9767001867294312, - -0.8753045797348022, - 0.05996488779783249, - -0.9776279330253601, - 0.33343228697776794, - -0.8125626444816589, - 1.0561962127685547, - 0.3579457700252533, - -0.2970648407936096, - -0.759721577167511, - -0.966127872467041, - -0.4836031496524811, - -0.31005367636680603, - 1.067772388458252, - 1.0192601680755615, - -1.0688796043395996, - -0.32840093970298767, - -0.7854953408241272, - 0.5931153297424316, - -1.0684417486190796, - -0.02163565531373024, - 0.4325360655784607, - 1.0683929920196533, - -0.722478985786438, - -0.7940815687179565, - 0.6880764365196228, - -0.3306683599948883, - 0.797936737537384, - -0.9481998682022095, - -0.8344391584396362, - 0.003755486337468028, - 0.6906352639198303, - 1.0131542682647705, - 1.0643237829208374, - 0.9742530584335327, - 0.8314846754074097, - 0.6827107667922974, - 0.548229992389679, - 0.43452969193458557, - 0.3417493999004364, - 0.2675352394580841, - 0.208854541182518, - 0.16277137398719788, - 0.12672804296016693, - 0.09860574454069138, - 0.07669580727815628, - 0.059640903025865555, - 0.04637227579951286, - 0.03605266287922859 - ] - ] - }, - { - "rot": 256, - "theta": 10000000.0, - "factor": 2.0, - "original": 262144, - "magnitude": 1.0693147180559945, - "inv_freq": [ - 1.0, - 0.8816831111907959, - 0.7773650288581848, - 0.6853895783424377, - 0.6042963862419128, - 0.5327979326248169, - 0.46975892782211304, - 0.4141784608364105, - 0.36517414450645447, - 0.32196786999702454, - 0.283873587846756, - 0.2502865493297577, - 0.22067342698574066, - 0.19456399977207184, - 0.17154379189014435, - 0.15124724805355072, - 0.1333521455526352, - 0.11757431924343109, - 0.10366329550743103, - 0.09139817208051682, - 0.08058421313762665, - 0.07104974240064621, - 0.06264335662126541, - 0.05523158237338066, - 0.04869674891233444, - 0.04293510317802429, - 0.037855152040719986, - 0.03337624669075012, - 0.02942727319896221, - 0.025945525616407394, - 0.02287573181092739, - 0.020169146358966827, - 0.017782794311642647, - 0.01567878946661949, - 0.01382372248917818, - 0.012188142165541649, - 0.010746078565716743, - 0.009474635124206543, - 0.008353625424206257, - 0.0073652504943311214, - 0.006493816152215004, - 0.0057254876010119915, - 0.005048065446317196, - 0.004450793843716383, - 0.003924190066754818, - 0.003459891537204385, - 0.0030505280010402203, - 0.0026895988266915083, - 0.0023713738191872835, - 0.0020908000878989697, - 0.0018434228841215372, - 0.0016253149369731545, - 0.0014330126577988267, - 0.0012634629383683205, - 0.0011139739071950316, - 0.000982171855866909, - 0.0008659643353894353, - 0.0007503421511501074, - 0.0006499576265923679, - 0.0005628234939649701, - 0.0004872095014434308, - 0.00042160949669778347, - 0.00036471226485446095, - 0.0003153767320327461, - 0.0002726101374719292, - 0.00023554862127639353, - 0.00020344088261481375, - 0.00017563349683769047, - 0.00015155834262259305, - 0.000130721484310925, - 0.00011269371316302568, - 9.710196172818542e-05, - 8.362214430235326e-05, - 7.197279774118215e-05, - 6.190945714479312e-05, - 5.32199046574533e-05, - 4.571993486024439e-05, - 3.924968768842518e-05, - 3.3670494303805754e-05, - 2.8862075851066038e-05, - 2.472013875376433e-05, - 2.1154290152480826e-05, - 1.80861861736048e-05, - 1.5447960322489962e-05, - 1.3180847417970654e-05, - 1.123395031754626e-05, - 9.904783837555442e-06, - 8.732880814932287e-06, - 7.699632078583818e-06, - 6.788635801058263e-06, - 5.98542510488187e-06, - 5.277247964841081e-06, - 4.6528602979378775e-06, - 4.102348157175584e-06, - 3.6169706163491355e-06, - 3.189021754224086e-06, - 2.81170673588349e-06, - 2.479034037605743e-06, - 2.1857224510313245e-06, - 1.9271144537924556e-06, - 1.6991041320579825e-06, - 1.4980713558543357e-06, - 1.3208241398388054e-06, - 1.1645482800304308e-06, - 1.0267624475090997e-06, - 9.052791938302107e-07, - 7.981693102010468e-07, - 7.037323825898056e-07, - 6.204688816069392e-07, - 5.470569135468395e-07, - 4.823307904189278e-07, - 4.252629253187479e-07, - 3.7494712046282075e-07, - 3.3058452686418605e-07, - 2.9147076929803006e-07, - 2.569848618350079e-07, - 2.2657918918866926e-07, - 1.9977102283519343e-07, - 1.761347334650054e-07, - 1.552950124050767e-07, - 1.3692098832507327e-07, - 1.20720912377692e-07, - 1.0643758230344247e-07, - 9.384421417735211e-08, - 8.274085416815069e-08, - 7.295121662309612e-08, - 6.43198490024588e-08, - 5.6709719586933716e-08 - ], - "cos": [ - [ - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646 - ], - [ - 0.5777531862258911, - 0.6799269914627075, - 0.7621692419052124, - 0.8278340101242065, - 0.8799412846565247, - 0.92109614610672, - 0.9534838199615479, - 0.978901207447052, - 0.998805820941925, - 1.0143674612045288, - 1.0265183448791504, - 1.035996437072754, - 1.043384075164795, - 1.0491389036178589, - 1.0536197423934937, - 1.0571073293685913, - 1.0598211288452148, - 1.0619323253631592, - 1.0635744333267212, - 1.0648515224456787, - 1.0658446550369263, - 1.0666168928146362, - 1.067217230796814, - 1.0676841735839844, - 1.068047046661377, - 1.0683293342590332, - 1.068548560142517, - 1.0687191486358643, - 1.0688517093658447, - 1.0689548254013062, - 1.0690349340438843, - 1.0690972805023193, - 1.069145679473877, - 1.069183349609375, - 1.069212555885315, - 1.0692353248596191, - 1.0692529678344727, - 1.069266676902771, - 1.0692774057388306, - 1.0692857503890991, - 1.0692921876907349, - 1.069297194480896, - 1.0693011283874512, - 1.06930410861969, - 1.069306492805481, - 1.0693084001541138, - 1.0693097114562988, - 1.0693107843399048, - 1.0693117380142212, - 1.069312334060669, - 1.0693128108978271, - 1.0693132877349854, - 1.069313645362854, - 1.069313883781433, - 1.0693141222000122, - 1.0693142414093018, - 1.0693143606185913, - 1.0693143606185913, - 1.0693144798278809, - 1.0693144798278809, - 1.0693145990371704, - 1.0693145990371704, - 1.0693145990371704, - 1.0693145990371704, - 1.0693145990371704, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646, - 1.06931471824646 - ], - [ - 0.8597189784049988, - 0.12086047232151031, - 0.08865529298782349, - 0.3592999577522278, - 0.9942150712013245, - -0.5166255235671997, - 0.09923054277896881, - 1.0691953897476196, - 1.0025675296783447, - 0.8257118463516235, - 1.0019184350967407, - 0.5646449327468872, - 0.6644225120544434, - 0.5501449108123779, - 0.5102278590202332, - -0.8734031319618225, - 0.9734212756156921, - -0.6471863985061646, - -0.9435669183731079, - -0.9293569326400757, - -1.0467698574066162, - -0.4383901357650757, - 0.5565336346626282, - 1.0687006711959839, - -0.031272951513528824, - 1.0669151544570923, - -0.46909114718437195, - 0.05325764790177345, - 0.4332946836948395, - 0.9164575338363647, - 0.9122970104217529, - 0.6380595564842224, - -0.8934475779533386, - 0.19388730823993683, - 1.0664434432983398, - 1.0070801973342896, - 1.0687097311019897, - 0.4764467477798462, - -1.0077195167541504, - 0.3393491506576538, - 0.11192997545003891, - -0.11775708943605423, - -0.2713061571121216, - 0.8708483576774597, - -0.9986739158630371, - -0.036936186254024506, - 1.066589593887329, - 0.022478019818663597, - -1.025161623954773, - -0.6969631910324097, - 0.3193954527378082, - 0.9953557848930359, - 0.9791648387908936, - 0.4773583710193634, - -0.15932254493236542, - -0.6801744103431702, - -0.9826422333717346, - -1.0668295621871948, - -0.9487894773483276, - -0.7166948318481445, - -0.44071927666664124, - -0.16626004874706268, - 0.08218647539615631, - 0.29449695348739624, - 0.4691407382488251, - 0.6090553402900696, - 0.7190515995025635, - 0.8043435215950012, - 0.8698054552078247, - 0.9196605086326599, - 0.9574044942855835, - 0.9858469367027283, - 1.0072009563446045, - 1.0231845378875732, - 1.0351182222366333, - 1.044008731842041, - 1.0506192445755005, - 1.055525779724121, - 1.0591614246368408, - 1.061851143836975, - 1.0638378858566284, - 1.065303087234497, - 1.066381812095642, - 1.0671747922897339, - 1.0677566528320312, - 1.0681829452514648, - 1.0684348344802856, - 1.0686306953430176, - 1.068782925605774, - 1.0689013004302979, - 1.068993330001831, - 1.069064974784851, - 1.0691205263137817, - 1.0691637992858887, - 1.069197416305542, - 1.0692235231399536, - 1.0692437887191772, - 1.0692596435546875, - 1.0692719221115112, - 1.0692813396453857, - 1.0692888498306274, - 1.0692945718765259, - 1.0692989826202393, - 1.0693025588989258, - 1.0693053007125854, - 1.0693074464797974, - 1.0693089962005615, - 1.0693103075027466, - 1.069311261177063, - 1.0693120956420898, - 1.069312572479248, - 1.0693131685256958, - 1.069313406944275, - 1.0693137645721436, - 1.0693140029907227, - 1.0693141222000122, - 1.0693142414093018, - 1.0693143606185913, - 1.0693144798278809, - 1.0693144798278809, - 1.0693144798278809, - 1.0693145990371704, - 1.0693145990371704, - 1.0693145990371704, - 1.0693145990371704, - 1.0693145990371704, - 1.0693145990371704, - 1.06931471824646 - ], - [ - -0.6513853669166565, - 1.0648926496505737, - -0.19290786981582642, - -0.7843515872955322, - 0.9871648550033569, - 1.01374351978302, - 1.059841275215149, - 0.9159117937088013, - -0.9953956604003906, - 0.9835533499717712, - -0.8994616866111755, - -0.2907947599887848, - 0.2952715754508972, - -1.0544582605361938, - 1.0369493961334229, - 0.06821295619010925, - -0.7212009429931641, - -0.6810116171836853, - 1.054075002670288, - -0.036619432270526886, - 0.9285621643066406, - -0.27625617384910583, - -0.9795029759407043, - -0.5520840883255005, - -0.36669492721557617, - -0.3976605534553528, - -0.7229933142662048, - -1.0692088603973389, - -0.02881699800491333, - -1.0628392696380615, - -0.8900305032730103, - -1.063999056816101, - 0.9443737268447876, - 0.6798552870750427, - -0.03756128251552582, - -1.0686062574386597, - -0.5783533453941345, - -0.295753538608551, - -1.056593656539917, - -0.2544029951095581, - 0.9698436856269836, - 0.7565687894821167, - -0.8147097826004028, - -0.37385228276252747, - -0.18366917967796326, - -0.6358828544616699, - -0.14840036630630493, - 0.24174010753631592, - 0.9864349961280823, - 0.12726154923439026, - 0.9032161831855774, - 0.39577341079711914, - 0.2478526532649994, - -0.24382933974266052, - -1.057676911354065, - 1.0586854219436646, - 0.7358847260475159, - -0.36405715346336365, - 0.7926532030105591, - -1.0622985363006592, - -0.4976309835910797, - -0.9024218916893005, - 0.22480200231075287, - 0.584578275680542, - -0.7497449517250061, - 0.4998137950897217, - -1.0661896467208862, - -0.5013577342033386, - -0.4747675955295563, - -1.0247266292572021, - -0.3193347454071045, - 1.014404535293579, - -1.0666817426681519, - 1.0691488981246948, - -0.9273504018783569, - 0.19768640398979187, - 0.8937321901321411, - -0.6942081451416016, - -0.8835726976394653, - 0.30371347069740295, - 1.0486267805099487, - 0.7912856340408325, - 0.030768586322665215, - -0.657989501953125, - -1.0171388387680054, - -1.048696756362915, - -0.9143319725990295, - -0.7038566470146179, - -0.4628109037876129, - -0.22165097296237946, - 0.0018809328321367502, - 0.19922155141830444, - 0.3677518963813782, - 0.508329451084137, - 0.623598575592041, - 0.7169232368469238, - 0.791765034198761, - 0.8513538241386414, - 0.8985382914543152, - 0.9357439875602722, - 0.9649868607521057, - 0.9879139065742493, - 1.005854845046997, - 1.0198732614517212, - 1.0308140516281128, - 1.0393452644348145, - 1.0459932088851929, - 1.0511705875396729, - 1.0552012920379639, - 1.0583380460739136, - 1.0607784986495972, - 1.062677025794983, - 1.0641536712646484, - 1.0653020143508911, - 1.0661948919296265, - 1.0668891668319702, - 1.0674290657043457, - 1.067848801612854, - 1.0681750774383545, - 1.0684287548065186, - 1.0686259269714355, - 1.0687793493270874, - 1.0688985586166382, - 1.0689911842346191, - 1.0690631866455078, - 1.0691192150115967, - 1.0691627264022827, - 1.0691965818405151 - ], - [ - -1.0655258893966675, - 0.6080281734466553, - 0.6036512851715088, - -1.067401647567749, - 0.5742302536964417, - 0.7016583681106567, - 1.0096819400787354, - 0.616484522819519, - -0.7887967228889465, - 1.0656408071517944, - -0.7036169171333313, - -0.53633713722229, - 0.511264443397522, - -1.0688828229904175, - 0.9770159721374512, - -0.09452058374881744, - -0.6103026866912842, - -0.772730827331543, - 1.0670206546783447, - -0.1344296932220459, - 0.8831673860549927, - -0.3481474816799164, - -0.9507979154586792, - -0.6021782159805298, - -0.317230761051178, - -0.4399321377277374, - -0.7524679899215698, - -1.0681201219558716, - 0.0025070905685424805, - -1.0655239820480347, - -0.9033966660499573, - -1.0659174919128418, - 0.9530444145202637, - 0.6668764352798462, - -0.022686710581183434, - -1.0680515766143799, - -0.5879814028739929, - -0.3055242598056793, - -1.055192232131958, - -0.2620031237602234, - 0.9727371335029602, - 0.7608917951583862, - -0.8112333416938782, - -0.36944612860679626, - -0.1795528680086136, - -0.6388699412345886, - -0.15163138508796692, - 0.23894187808036804, - 0.9874147772789001, - 0.12505801022052765, - 0.9042627811431885, - 0.39737963676452637, - 0.2493443638086319, - -0.24252645671367645, - -1.0578539371490479, - 1.058831810951233, - 0.7352097034454346, - -0.3648087680339813, - 0.7921821475028992, - -1.0623674392700195, - -0.4980930685997009, - -0.902181088924408, - 0.22441914677619934, - 0.5842981934547424, - -0.749954342842102, - 0.5000373721122742, - -1.0662062168121338, - -0.501523494720459, - -0.4749138057231903, - -1.024766206741333, - -0.3192198872566223, - 1.0143717527389526, - -1.066688060760498, - 1.0691475868225098, - -0.9273179173469543, - 0.19763028621673584, - 0.8937591314315796, - -0.6941763162612915, - -0.8835928440093994, - 0.30368363857269287, - 1.0486215353012085, - 0.7913007140159607, - 0.03078795224428177, - -0.6579766273498535, - -1.0171345472335815, - -1.048699140548706, - -0.9143375158309937, - -0.7038637399673462, - -0.4628182649612427, - -0.22165808081626892, - 0.0018745592096820474, - 0.19921603798866272, - 0.3677472472190857, - 0.5083256363868713, - 0.6235954761505127, - 0.7169206738471985, - 0.7917630076408386, - 0.8513522148132324, - 0.8985370397567749, - 0.9357430338859558, - 0.9649860858917236, - 0.9879133105278015, - 1.0058543682098389, - 1.019872784614563, - 1.0308136940002441, - 1.039345145225525, - 1.0459929704666138, - 1.0511704683303833, - 1.0552011728286743, - 1.058337926864624, - 1.0607784986495972, - 1.062677025794983, - 1.0641535520553589, - 1.0653018951416016, - 1.0661948919296265, - 1.0668891668319702, - 1.0674290657043457, - 1.067848801612854, - 1.0681750774383545, - 1.0684287548065186, - 1.0686259269714355, - 1.0687793493270874, - 1.0688985586166382, - 1.0689911842346191, - 1.0690631866455078, - 1.0691192150115967, - 1.0691627264022827, - 1.0691965818405151 - ], - [ - -1.0522708892822266, - 0.8074479699134827, - -0.05930701270699501, - -0.9720513820648193, - -0.7683091163635254, - -0.2482469081878662, - 0.14413057267665863, - -0.1693471074104309, - -0.7019533514976501, - -0.8213952779769897, - 1.0010639429092407, - 0.511832594871521, - -0.7174220681190491, - 0.9070536494255066, - 1.0594686269760132, - 0.7500777840614319, - 0.49248647689819336, - -0.11486396938562393, - -0.08763059973716736, - 0.09781012684106827, - -0.4076384902000427, - 1.0337498188018799, - 1.0693141222000122, - 0.39584189653396606, - 0.5487287640571594, - -0.5337479710578918, - -0.9282023310661316, - 1.0691578388214111, - -0.013338646851480007, - -0.45949143171310425, - -0.8363955616950989, - 1.0673372745513916, - 0.8245336413383484, - -0.4650532305240631, - 1.0017105340957642, - 0.8712486624717712, - 0.6515282988548279, - 1.0470679998397827, - 0.06952807307243347, - 0.8323741555213928, - 0.07687243074178696, - -0.780223548412323, - -0.2510434091091156, - 0.43741264939308167, - -0.18141566216945648, - -0.5117603540420532, - 0.022245239466428757, - 1.0484681129455566, - -0.279379278421402, - -0.7821656465530396, - -0.3626629710197449, - -0.5641676187515259, - 1.0427840948104858, - -1.0301964282989502, - -0.6435174345970154, - 0.5800156593322754, - 0.9073381423950195, - -0.26376578211784363, - -0.18724913895130157, - 0.25334858894348145, - 0.14002014696598053, - -1.0156885385513306, - 1.0582853555679321, - 0.8772346377372742, - -0.370892733335495, - -0.03786427900195122, - 0.39793872833251953, - 1.0576493740081787, - 0.9926570653915405, - -0.8748014569282532, - 1.0476127862930298, - -0.1531391143798828, - -0.6041576862335205, - -0.15124283730983734, - 0.9575929641723633, - 0.09992540627717972, - -0.6904867887496948, - 0.7637256383895874, - -0.45885878801345825, - -0.30973702669143677, - 1.046640157699585, - -0.43449050188064575, - -0.9923649430274963, - 0.1386214792728424, - 1.019241213798523, - 0.8406627178192139, - 0.2541821599006653, - -0.3625932037830353, - -0.8121644854545593, - -1.0353474617004395, - -1.0574856996536255, - -0.9368858337402344, - -0.7332826256752014, - -0.49414560198783875, - -0.2517778277397156, - -0.025355977937579155, - 0.17557725310325623, - 0.3477979302406311, - 0.49182775616645813, - 0.6101534366607666, - 0.706089437007904, - 0.7831082344055176, - 0.844480037689209, - 0.8931068181991577, - 0.93146812915802, - 0.9616302251815796, - 0.9852848052978516, - 1.0037989616394043, - 1.0182678699493408, - 1.0295616388320923, - 1.038369059562683, - 1.0452326536178589, - 1.0505784749984741, - 1.054740309715271, - 1.0579793453216553, - 1.0604995489120483, - 1.062459945678711, - 1.063984751701355, - 1.0651706457138062, - 1.066092848777771, - 1.0668097734451294, - 1.0673673152923584, - 1.067800760269165, - 1.068137764930725, - 1.0683997869491577, - 1.0686033964157104, - 1.0687618255615234, - 1.0688848495483398 - ], - [ - 0.7204014658927917, - 0.525600016117096, - -0.9770510196685791, - 0.7394928932189941, - 0.16690370440483093, - 0.40845200419425964, - 1.047502040863037, - 0.08546723425388336, - 0.46298733353614807, - 0.9414898753166199, - 0.1564042568206787, - -0.7443639039993286, - -0.7614797949790955, - 1.059080719947815, - 0.8413293957710266, - -1.0688780546188354, - -0.23665851354599, - -0.07772661000490189, - 1.0395491123199463, - -1.0552698373794556, - 0.4698231816291809, - -0.886785626411438, - 0.6746538877487183, - -0.44490334391593933, - -0.8504674434661865, - -0.7411206364631653, - -0.049967240542173386, - 1.0673035383224487, - -1.06899094581604, - 1.0490853786468506, - 0.4353052079677582, - 1.0520436763763428, - 0.614227831363678, - -0.22120295464992523, - -1.0676320791244507, - 1.0654528141021729, - -0.4332176446914673, - -0.9001200795173645, - 1.0160003900527954, - -0.9446185827255249, - 0.6951085329055786, - 0.0075360615737736225, - 0.16698326170444489, - -0.8109739422798157, - -1.0076253414154053, - -0.30941998958587646, - -1.0272226333618164, - -0.9612756371498108, - 0.7525005340576172, - -1.0395455360412598, - 0.45829126238822937, - -0.7751805186271667, - -0.9537095427513123, - -0.9586957693099976, - 1.0233770608901978, - 1.027299404144287, - -0.05741226673126221, - -0.8209205269813538, - 0.10511433333158493, - 1.0414828062057495, - -0.6057157516479492, - 0.4534398913383484, - -0.9749553799629211, - -0.4304531514644623, - -0.017660684883594513, - -0.6018646359443665, - 1.0568652153015137, - -0.5990272164344788, - -0.6475985646247864, - 0.8947566151618958, - -0.8786531090736389, - 0.8552500605583191, - 1.0588085651397705, - 1.0686489343643188, - 0.539095938205719, - -0.996242105960846, - 0.4246912896633148, - -0.16798348724842072, - 0.3909064531326294, - -0.8968067169189453, - 0.9873533248901367, - 0.1017996296286583, - -1.0675429105758667, - -0.2595592439174652, - 0.8656948804855347, - 0.9876425862312317, - 0.4943186342716217, - -0.14270390570163727, - -0.6686892509460449, - -0.9774226546287537, - -1.0693081617355347, - -0.995083749294281, - -0.8163681030273438, - -0.5860204100608826, - -0.34198302030563354, - -0.10799416899681091, - 0.10319384932518005, - 0.2863234877586365, - 0.4407549500465393, - 0.5683994293212891, - 0.6723594665527344, - 0.7561036944389343, - 0.8230066299438477, - 0.8761200904846191, - 0.9180837869644165, - 0.9511165618896484, - 0.9770455360412598, - 0.9973538517951965, - 1.0132330656051636, - 1.0256329774856567, - 1.0353060960769653, - 1.0428462028503418, - 1.0487200021743774, - 1.0532935857772827, - 1.0568535327911377, - 1.0596235990524292, - 1.0617786645889282, - 1.0634549856185913, - 1.0647586584091187, - 1.0657724142074585, - 1.0665606260299683, - 1.0671736001968384, - 1.0676501989364624, - 1.0680207014083862, - 1.0683088302612305, - 1.0685327053070068, - 1.068706750869751, - 1.0688421726226807 - ] - ], - "sin": [ - [ - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0, - 0.0 - ], - [ - 0.899797260761261, - 0.8253079652786255, - 0.7500213384628296, - 0.6768491268157959, - 0.6075666546821594, - 0.5431535243988037, - 0.48404812812805176, - 0.43033286929130554, - 0.38186508417129517, - 0.3383674621582031, - 0.29948970675468445, - 0.26484957337379456, - 0.23405885696411133, - 0.20674000680446625, - 0.18253596127033234, - 0.16111499071121216, - 0.1421731561422348, - 0.1254344880580902, - 0.11065026372671127, - 0.0975973978638649, - 0.08607665449380875, - 0.07591063529253006, - 0.06694166362285614, - 0.059029921889305115, - 0.052051570266485214, - 0.04589703306555748, - 0.04046940430998802, - 0.03568308800458908, - 0.031462475657463074, - 0.02774081937968731, - 0.024459224194288254, - 0.02156570367515087, - 0.019014401361346245, - 0.016764873638749123, - 0.014781438745558262, - 0.013032637536525726, - 0.011490718461573124, - 0.010131215676665306, - 0.008932551369071007, - 0.007875699549913406, - 0.006943884305655956, - 0.006122314836829901, - 0.00539794797077775, - 0.004759283270686865, - 0.004196183290332556, - 0.003699705470353365, - 0.0032619694247841835, - 0.002876024227589369, - 0.002535742474719882, - 0.0022357215639203787, - 0.0019711980130523443, - 0.0017379724886268377, - 0.0015323410043492913, - 0.001351039158180356, - 0.001191188464872539, - 0.0010502507211640477, - 0.0009259883081540465, - 0.0008023518603295088, - 0.0006950091919861734, - 0.0006018353742547333, - 0.000520980276633054, - 0.0004508332349359989, - 0.00038999220123514533, - 0.00033723696833476424, - 0.0002915060322266072, - 0.00025187560822814703, - 0.00021754232875537127, - 0.0001878074835985899, - 0.00016206355940084904, - 0.00013978240895085037, - 0.00012050504301441833, - 0.00010383255721535534, - 8.94183904165402e-05, - 7.696157263126224e-05, - 6.620069325435907e-05, - 5.690882608178072e-05, - 4.888900002697483e-05, - 4.1970270103774965e-05, - 3.600435593398288e-05, - 3.0862644052831456e-05, - 2.6433608582010493e-05, - 2.2620593881583773e-05, - 1.9339824575581588e-05, - 1.6518732081749476e-05, - 1.4094473954173736e-05, - 1.201262875838438e-05, - 1.0591331374598667e-05, - 9.338197742181364e-06, - 8.233329936047085e-06, - 7.259188350872137e-06, - 6.400302936526714e-06, - 5.6430390031891875e-06, - 4.975372121407418e-06, - 4.386701220937539e-06, - 3.867679879476782e-06, - 3.4100678476534085e-06, - 3.0065993996686302e-06, - 2.6508676000958076e-06, - 2.337225168957957e-06, - 2.0606919406418456e-06, - 1.8168771021009888e-06, - 1.6019097301978036e-06, - 1.4123767186902114e-06, - 1.2452686632968835e-06, - 1.097932226912235e-06, - 9.680284165369812e-07, - 8.534942139704071e-07, - 7.52511368773412e-07, - 6.634764986301889e-07, - 5.849760214005073e-07, - 5.157634177521686e-07, - 4.5473990439859335e-07, - 4.0093647157846135e-07, - 3.534989048148418e-07, - 3.1167397196441016e-07, - 2.747976850514533e-07, - 2.422844715965766e-07, - 2.1361809388054098e-07, - 1.8834346349194675e-07, - 1.660592374719272e-07, - 1.4641162238149263e-07, - 1.2908864732708025e-07, - 1.1381527542653203e-07, - 1.0034899844413303e-07, - 8.847601407069305e-08, - 7.800780821298758e-08, - 6.877816360884026e-08, - 6.064053792442792e-08 - ], - [ - -0.6358594298362732, - -1.062462568283081, - -1.0656332969665527, - -1.007143259048462, - -0.3936627209186554, - 0.93623286485672, - 1.0647004842758179, - 0.015977798029780388, - 0.3718767762184143, - -0.679436445236206, - 0.3736218810081482, - 0.9080803990364075, - -0.8378404974937439, - -0.9169375896453857, - -0.9397347569465637, - -0.6169286370277405, - -0.44258883595466614, - -0.8512247800827026, - -0.5031058192253113, - -0.5288947224617004, - -0.2184189260005951, - 0.975319504737854, - -0.9130740761756897, - 0.03623250871896744, - -1.0688573122024536, - -0.07159688323736191, - -0.9609304666519165, - -1.0679875612258911, - 0.9775937795639038, - -0.5509442090988159, - -0.5578064918518066, - 0.858087420463562, - -0.5875248312950134, - 1.051590085029602, - 0.07831012457609177, - -0.3594764471054077, - 0.03596566617488861, - 0.9573047757148743, - 0.3576805591583252, - -1.0140395164489746, - 1.0634404420852661, - -1.062811017036438, - 1.0343244075775146, - -0.6205296516418457, - -0.38220974802970886, - 1.0686765909194946, - -0.07629266381263733, - -1.0690784454345703, - -0.30410149693489075, - 0.8109724521636963, - 1.0205000638961792, - 0.3907695710659027, - -0.42973271012306213, - -0.956850528717041, - -1.0573790073394775, - -0.8251040577888489, - -0.4217204451560974, - 0.07286135107278824, - 0.4931860566139221, - 0.793588399887085, - 0.9742691516876221, - 1.0563102960586548, - 1.0661516189575195, - 1.0279618501663208, - 0.9609062671661377, - 0.8789116144180298, - 0.7914536595344543, - 0.7046030759811401, - 0.6219906806945801, - 0.5455808639526367, - 0.4762464165687561, - 0.41417360305786133, - 0.35913822054862976, - 0.3106883764266968, - 0.26826128363609314, - 0.23125684261322021, - 0.1990809440612793, - 0.17117063701152802, - 0.1470067799091339, - 0.12611913681030273, - 0.10808714479207993, - 0.09253805875778198, - 0.07914348691701889, - 0.0676155835390091, - 0.0577029250562191, - 0.04918636754155159, - 0.04337019473314285, - 0.038241103291511536, - 0.033718131482601166, - 0.029729802161455154, - 0.026213016360998154, - 0.023112086579203606, - 0.02037789113819599, - 0.01796708256006241, - 0.015841437503695488, - 0.013967241160571575, - 0.012314759194850922, - 0.010857767425477505, - 0.00957314670085907, - 0.008440505713224411, - 0.0074418680742383, - 0.006561380811035633, - 0.00578506663441658, - 0.005100600887089968, - 0.004497116897255182, - 0.003965035080909729, - 0.003495906013995409, - 0.0030822823755443096, - 0.002717596711590886, - 0.002396059688180685, - 0.0021125655621290207, - 0.001862613600678742, - 0.0016422352055087686, - 0.0014479310484603047, - 0.0012766163563355803, - 0.001125571085140109, - 0.000992397079244256, - 0.000874979596119374, - 0.000771454768255353, - 0.0006801785784773529, - 0.0005997019470669329, - 0.0005287470994517207, - 0.0004661873681470752, - 0.0004110294976271689, - 0.00036239775363355875, - 0.00031951998244039714, - 0.0002817153581418097, - 0.00024838364333845675 - ], - [ - 0.8480159640312195, - 0.09714719653129578, - -1.0517702102661133, - 0.7267919182777405, - 0.41102248430252075, - 0.34023213386535645, - -0.14202295243740082, - 0.5518509149551392, - -0.3906678557395935, - -0.41959115862846375, - -0.5782755017280579, - 1.029015302658081, - -1.0277395248413086, - 0.1776278167963028, - 0.2610936164855957, - 1.0671367645263672, - -0.7894955277442932, - 0.8244131803512573, - -0.17988857626914978, - 1.0686874389648438, - 0.5302888751029968, - 1.0330133438110352, - -0.4289613664150238, - 0.9157713651657104, - -1.004474401473999, - 0.9926228523254395, - 0.7878543734550476, - -0.015043829567730427, - -1.0689263343811035, - 0.11750100553035736, - 0.5926885008811951, - 0.10648989677429199, - -0.5015896558761597, - 0.8253670334815979, - -1.0686547756195068, - -0.03891885653138161, - 0.899411678314209, - 1.0276010036468506, - -0.1644500195980072, - 1.0386111736297607, - -0.4503745436668396, - -0.7556703090667725, - -0.6925907135009766, - -1.0018324851989746, - -1.0534228086471558, - 0.8597016334533691, - 1.058967113494873, - 1.0416312217712402, - -0.4127710163593292, - 1.0617148876190186, - -0.5723935961723328, - -0.9933767318725586, - -1.040193796157837, - -1.0411441326141357, - 0.1573324352502823, - -0.1503962129354477, - 0.7758269906044006, - 1.005433440208435, - 0.7177289128303528, - 0.12229378521442413, - 0.9464656710624695, - -0.5736451148986816, - 1.0454176664352417, - 0.8953782916069031, - 0.762441098690033, - -0.9453148245811462, - 0.0816914290189743, - 0.9444968700408936, - 0.958138644695282, - 0.3055640161037445, - -1.0205191373825073, - 0.3382561504840851, - 0.07499263435602188, - 0.01882825791835785, - -0.5324051380157471, - 1.0508824586868286, - -0.5870917439460754, - -0.8133320808410645, - 0.6022734045982361, - 1.0252765417099, - 0.20932230353355408, - -0.719236433506012, - -1.0688719749450684, - -0.8429019451141357, - -0.3299431800842285, - 0.20897124707698822, - 0.55446457862854, - 0.8049967885017395, - 0.963970959186554, - 1.0460902452468872, - 1.0693130493164062, - 1.0505925416946411, - 1.0040879249572754, - 0.9407630562782288, - 0.868653416633606, - 0.7933820486068726, - 0.7187086343765259, - 0.6470167636871338, - 0.5797093510627747, - 0.5175104737281799, - 0.4606889486312866, - 0.40921875834465027, - 0.3628910481929779, - 0.32139164209365845, - 0.2843526303768158, - 0.25138670206069946, - 0.22210858762264252, - 0.1961486041545868, - 0.17315985262393951, - 0.15282228589057922, - 0.13484381139278412, - 0.11896012723445892, - 0.10493354499340057, - 0.0925513207912445, - 0.08162368088960648, - 0.07198182493448257, - 0.0634758397936821, - 0.05597289279103279, - 0.04935538396239281, - 0.043519243597984314, - 0.03837253898382187, - 0.03383404016494751, - 0.029832005500793457, - 0.026303134858608246, - 0.023191550746560097, - 0.020447954535484314, - 0.018028859049081802, - 0.01589590683579445 - ], - [ - -0.08993688225746155, - 0.8796225190162659, - -0.8826319575309753, - 0.06393515318632126, - 0.9020496606826782, - 0.8069136142730713, - 0.3521025776863098, - 0.8737166523933411, - -0.7219651341438293, - -0.08856476843357086, - -0.805206298828125, - 0.9250818490982056, - -0.9391712546348572, - -0.03039141371846199, - 0.43459609150886536, - 1.065129041671753, - -0.8780458569526672, - 0.7391353249549866, - -0.07000717520713806, - 1.060831069946289, - 0.602867603302002, - 1.0110526084899902, - -0.4893029034137726, - 0.883637547492981, - -1.0211750268936157, - 0.9746248722076416, - 0.7597538232803345, - -0.0505300909280777, - -1.0693117380142212, - 0.08995961397886276, - 0.5721087455749512, - 0.085169218480587, - -0.4849127233028412, - 0.8358886241912842, - -1.069074034690857, - -0.051960136741399765, - 0.8931471109390259, - 1.0247384309768677, - -0.17321480810642242, - 1.0367200374603271, - -0.4440905451774597, - -0.7513172626495361, - -0.6966595649719238, - -1.0034657716751099, - -1.0541322231292725, - 0.8574842214584351, - 1.0585092306137085, - 1.0422767400741577, - -0.410421758890152, - 1.061976671218872, - -0.5707387328147888, - -0.9927353262901306, - -1.039837121963501, - -1.0414484739303589, - 0.15613806247711182, - -0.14936228096485138, - 0.7764667868614197, - 1.005160927772522, - 0.7182489037513733, - 0.12169402092695236, - 0.9462226033210754, - -0.5740237236022949, - 1.0454999208450317, - 0.8955610394477844, - 0.762235164642334, - -0.9451965689659119, - 0.08147586137056351, - 0.9444088935852051, - 0.9580661654472351, - 0.3054310977458954, - -1.020555019378662, - 0.33835482597351074, - 0.07490310817956924, - 0.018905749544501305, - -0.5324617624282837, - 1.0508930683135986, - -0.5870507955551147, - -0.8133592009544373, - 0.6022439002990723, - 1.0252853631973267, - 0.20934830605983734, - -0.7192198038101196, - -1.0688713788986206, - -0.8429120182991028, - -0.3299565315246582, - 0.20895950496196747, - 0.5544554591178894, - 0.8049905300140381, - 0.9639674425125122, - 1.0460888147354126, - 1.0693130493164062, - 1.050593614578247, - 1.004089593887329, - 0.9407651424407959, - 0.8686556816101074, - 0.793384313583374, - 0.7187108397483826, - 0.6470188498497009, - 0.5797113180160522, - 0.5175122618675232, - 0.46069058775901794, - 0.40922024846076965, - 0.36289238929748535, - 0.32139280438423157, - 0.28435367345809937, - 0.2513876259326935, - 0.22210942208766937, - 0.1961493343114853, - 0.17316049337387085, - 0.15282288193702698, - 0.1348443180322647, - 0.11896057426929474, - 0.10493393987417221, - 0.09255167096853256, - 0.08162398636341095, - 0.07198208570480347, - 0.0634760782122612, - 0.0559731088578701, - 0.04935557022690773, - 0.043519411236047745, - 0.03837268799543381, - 0.03383416682481766, - 0.02983211912214756, - 0.026303235441446304, - 0.02319163829088211, - 0.02044803276658058, - 0.018028927966952324, - 0.015895968303084373 - ], - [ - 0.19015751779079437, - 0.7010433077812195, - -1.0676687955856323, - -0.4455896317958832, - 0.7437305450439453, - -1.0400997400283813, - 1.0595566034317017, - 1.0558197498321533, - -0.8066569566726685, - 0.6846486926078796, - -0.37590548396110535, - 0.9388617277145386, - -0.7929309606552124, - -0.5662928223609924, - 0.14477676153182983, - -0.7621136903762817, - -0.9491528272628784, - 1.0631276369094849, - 1.0657180547714233, - 1.0648319721221924, - -0.988567054271698, - -0.2734871804714203, - -0.0010927487164735794, - 0.9933494925498962, - 0.9177857637405396, - -0.926578164100647, - 0.5309184193611145, - -0.018315389752388, - -1.0692315101623535, - -0.9655576348304749, - 0.6662405133247375, - 0.06500007212162018, - 0.6808658242225647, - -0.9628912210464478, - 0.3741791546344757, - -0.619967520236969, - 0.8479061126708984, - -0.2169850915670395, - -1.067051887512207, - 0.6712579727172852, - -1.0665479898452759, - -0.7312217354774475, - -1.0394282341003418, - 0.9757582545280457, - 1.0538132190704346, - 0.9389011263847351, - -1.069083333015442, - 0.21011583507061005, - -1.0321730375289917, - 0.7291439175605774, - -1.005937099456787, - 0.9083770513534546, - 0.23671789467334747, - -0.28658196330070496, - -0.8540018796920776, - 0.8983405828475952, - -0.5658369660377502, - -1.0362728834152222, - -1.0527923107147217, - -1.03886878490448, - -1.060107707977295, - -0.3343813121318817, - 0.15318617224693298, - 0.6114681959152222, - -1.0029319524765015, - -1.0686441659927368, - 0.9925112724304199, - -0.15751725435256958, - 0.3975750207901001, - 0.6149442195892334, - -0.21433930099010468, - -1.0582921504974365, - -0.8822853565216064, - -1.0585649013519287, - -0.47586727142333984, - 1.0646355152130127, - -0.8164936900138855, - 0.7484363913536072, - -0.9658584594726562, - 1.0234729051589966, - -0.21903958916664124, - -0.9770629405975342, - 0.3983036279678345, - 1.0602914094924927, - 0.32339009642601013, - -0.660848081111908, - -1.0386651754379272, - -1.0059622526168823, - -0.6955736875534058, - -0.26737523078918457, - 0.15861226618289948, - 0.5154404044151306, - 0.7782869338989258, - 0.9482901096343994, - 1.0392506122589111, - 1.069014072418213, - 1.0548017024993896, - 1.011172890663147, - 0.9494943022727966, - 0.8781496286392212, - 0.803039014339447, - 0.7281314730644226, - 0.6559628844261169, - 0.5880426168441772, - 0.5251676440238953, - 0.4676550030708313, - 0.41550907492637634, - 0.36853960156440735, - 0.3264423906803131, - 0.2888544201850891, - 0.2553892135620117, - 0.225660502910614, - 0.1992960274219513, - 0.1759456992149353, - 0.15528593957424164, - 0.13702107965946198, - 0.1208832636475563, - 0.10663152486085892, - 0.09405004978179932, - 0.08294621855020523, - 0.07314864546060562, - 0.06450513750314713, - 0.05688077583909035, - 0.050156090408563614, - 0.044225383549928665, - 0.03899525851011276, - 0.034383151680231094, - 0.030316205695271492 - ], - [ - -0.7902251482009888, - 0.9312242269515991, - -0.4345173239707947, - -0.7723886370658875, - 1.056208848953247, - 0.9882312417030334, - 0.21488043665885925, - 1.0658936500549316, - 0.9638862609863281, - -0.5069819688796997, - 1.057814598083496, - -0.767695426940918, - -0.750721275806427, - -0.1475868970155716, - 0.6599991917610168, - -0.030557645484805107, - 1.042797565460205, - -1.0664860010147095, - -0.25054293870925903, - -0.17274115979671478, - 0.9605727791786194, - -0.5975326895713806, - 0.8296240568161011, - -0.9723656177520752, - 0.6481813788414001, - -0.7708269357681274, - -1.0681465864181519, - 0.06555211544036865, - 0.026310738176107407, - -0.20701158046722412, - -0.9767001867294312, - -0.19141113758087158, - -0.8753045797348022, - 1.04618501663208, - 0.05996488779783249, - 0.09079859405755997, - -0.9776279330253601, - -0.5772501230239868, - 0.33343228697776794, - -0.5011284351348877, - -0.8125626444816589, - -1.0692881345748901, - 1.0561962127685547, - 0.6969614624977112, - 0.3579457700252533, - -1.023568868637085, - -0.2970648407936096, - 0.4683835506439209, - -0.759721577167511, - 0.25055742263793945, - -0.966127872467041, - -0.7365657687187195, - -0.4836031496524811, - 0.47364166378974915, - -0.31005367636680603, - -0.2967993915081024, - 1.067772388458252, - -0.6852179169654846, - 1.0641357898712158, - -0.24237890541553497, - -0.8812164068222046, - 0.9684142470359802, - 0.4391992390155792, - 0.9788482785224915, - -1.0691689252853394, - -0.8838511109352112, - -0.1626957356929779, - -0.885776698589325, - -0.8509113788604736, - -0.5855292677879333, - 0.6094281077384949, - 0.6418576836585999, - -0.14952710270881653, - 0.03772813826799393, - 0.9234768152236938, - 0.38850441575050354, - -0.9813619256019592, - 1.0560376644134521, - -0.9953019618988037, - 0.5823844075202942, - 0.4105696380138397, - -1.0644580125808716, - -0.061531033366918564, - 1.0373345613479614, - 0.6276991367340088, - -0.4098731577396393, - -0.9481998682022095, - -1.059749722480774, - -0.8344391584396362, - -0.43368062376976013, - 0.003755486337468028, - 0.39146173000335693, - 0.6906352639198303, - 0.8944350481033325, - 1.0131542682647705, - 1.0638474225997925, - 1.0643237829208374, - 1.0302683115005493, - 0.9742530584335327, - 0.9057350754737854, - 0.8314846754074097, - 0.7561357617378235, - 0.6827107667922974, - 0.6130641102790833, - 0.548229992389679, - 0.48868316411972046, - 0.43452969193458557, - 0.3856413960456848, - 0.3417493999004364, - 0.3025076687335968, - 0.2675352394580841, - 0.23644381761550903, - 0.208854541182518, - 0.18440836668014526, - 0.16277137398719788, - 0.14363735914230347, - 0.12672804296016693, - 0.11179253458976746, - 0.09860574454069138, - 0.08696653693914413, - 0.07669580727815628, - 0.06763432174921036, - 0.059640903025865555, - 0.05259045213460922, - 0.04637227579951286, - 0.040888506919145584, - 0.03605266287922859, - 0.031788360327482224 - ] - ] - } - ] -} diff --git a/tests/format_test.cpp b/tests/format_test.cpp index d0c37fa2..189c0b48 100644 --- a/tests/format_test.cpp +++ b/tests/format_test.cpp @@ -12,6 +12,7 @@ #include "format/tunecache.h" #include "format/imatrix.h" #include "format/safetensors.h" +#include "format/checkpoint.h" #include #include @@ -1901,6 +1902,62 @@ TEST(a_legacy_entry_count_that_is_not_one_is_refused) { std::remove(path.c_str()); } +/* A GGUF's rotary scaling arrives as the config.json keys the architecture reads, type and all, + * and never as a bare factor in RadModelMeta::rope_scale. A YaRN modifier config.json has no + * spelling for is refused. */ +TEST(a_gguf_rotary_scaling_becomes_the_config_keys_with_its_type) { + auto meta = [](const std::vector& extra, const char* leaf, MetaOwn* out) -> int { + std::vector kvs = { { "general.architecture", 8, 0, "llama" }, + { "llama.block_count", 4, 2, "" } }; + kvs.insert(kvs.end(), extra.begin(), extra.end()); + GgufFile g; + if (g.open(write_bin(leaf, gguf_bytes({}, kvs)).c_str()) != RAD_OK) return RAD_E_IO; + return meta_from_gguf(g, out); + }; + { + MetaOwn m; + CHECK_OK(meta({ { "llama.rope.scaling.type", 8, 0, "yarn" }, + { "llama.rope.scaling.factor", 12, 4, "" }, + { "llama.rope.scaling.original_context_length", 4, 32768, "" } }, + "yarn.gguf", &m)); + CHECK_EQ(m.m.rope_scale, 1.0f); + REQUIRE(m.get("rope_scaling.type")); + CHECK_EQ(std::string(m.get("rope_scaling.type")), std::string("yarn")); + CHECK_EQ(std::string(m.get("rope_scaling.factor")), std::string("4")); + CHECK_EQ(std::string(m.get("rope_scaling.original_max_position_embeddings")), + std::string("32768")); + } + { /* llama.cpp's default type is linear, and the older key names its factor. */ + MetaOwn m; + CHECK_OK(meta({ { "llama.rope.scale_linear", 12, 2, "" } }, "linear.gguf", &m)); + CHECK_EQ(std::string(m.get("rope_scaling.type")), std::string("linear")); + CHECK_EQ(std::string(m.get("rope_scaling.factor")), std::string("2")); + CHECK_EQ(m.m.rope_scale, 1.0f); + } + { /* A type the architecture does not implement is carried by name for it to refuse. */ + MetaOwn m; + CHECK_OK(meta({ { "llama.rope.scaling.type", 8, 0, "longrope" }, + { "llama.rope.scaling.factor", 12, 4, "" } }, "longrope.gguf", &m)); + CHECK_EQ(std::string(m.get("rope_scaling.type")), std::string("longrope")); + } + { + MetaOwn m; + CHECK_OK(meta({ { "llama.rope.scaling.type", 8, 0, "none" }, + { "llama.rope.scaling.factor", 12, 4, "" } }, "none.gguf", &m)); + CHECK(!m.get("rope_scaling.type")); + MetaOwn n; + CHECK_OK(meta({}, "plain.gguf", &n)); + CHECK(!n.get("rope_scaling.type")); + } + { + MetaOwn m; + CHECK_EQ(meta({ { "llama.rope.scaling.type", 8, 0, "yarn" }, + { "llama.rope.scaling.factor", 12, 4, "" }, + { "llama.rope.scaling.attn_factor", 12, 2, "" } }, "attn.gguf", &m), + RAD_E_UNSUPPORTED); + } +} + TEST(a_gguf_version_this_reader_does_not_know_is_refused) { const std::string path = write_bin("v2.imatrix", gguf_bytes( { { "t.in_sum2", { 2 }, { 1.0f, 2.0f } } }, {}, /*version=*/2)); diff --git a/tests/mem_test.cpp b/tests/mem_test.cpp index 4f61551c..d90df530 100644 --- a/tests/mem_test.cpp +++ b/tests/mem_test.cpp @@ -621,6 +621,101 @@ TEST(kv_pool_mib_for_tokens_is_the_inverse_of_paged_tokens) { CHECK(zn == 0 || (zn - 1) * MiB < zero.kv.fixed_bytes()); } +/* THE INVERSE ON A SHAPE, AND THE CARVE IT PROMISES. The dear rank's pool of `common_mib` sets the + * token count; the cheap rank's inverse names the least whole MiB that pages it (one MiB less does + * not, or does not hold the fixed costs); and a pool of exactly that size, capped at the count the + * way configure_kv caps every rank, carves every paged group the dear rank's block count -- the + * one block table. Returns the MiB the cheap rank needed. */ +static int64_t check_inverse_and_carve(const std::vector& dear_g, + const std::vector& cheap_g, int64_t common_mib) { + constexpr int64_t MiB = 1ll << 20; + Fixture dear, cheap; + dear.cfg.checkpoint_slots = cheap.cfg.checkpoint_slots = 0; + dear.cfg.vram_kv_mib = common_mib; + CHECK_OK(dear.kv.plan_groups(dear_g, dear.cfg)); + CHECK_OK(cheap.kv.plan_groups(cheap_g, cheap.cfg)); + const int64_t T = dear.kv.paged_tokens(common_mib * MiB); + CHECK(T > 0 && T != KVManager::kUnboundedTokens); + const int64_t need = cheap.kv.pool_mib_for_tokens(T, common_mib); + CHECK(need > 0 && need < common_mib); + if (need <= 0) return need; + CHECK(cheap.kv.paged_tokens(need * MiB) >= T); + CHECK((need - 1) * MiB < cheap.kv.fixed_bytes() || cheap.kv.paged_tokens((need - 1) * MiB) < T); + + cheap.cfg.vram_kv_mib = need; + CHECK_OK(dear.pools.configure(dear.cfg, 0, &rad_malloc_allocator(), &rad_malloc_allocator())); + CHECK_OK(cheap.pools.configure(cheap.cfg, 0, &rad_malloc_allocator(), &rad_malloc_allocator())); + CHECK_EQ(cheap.pools.kv().avail(), need * MiB); + CHECK_EQ(dear.kv.paged_tokens(dear.pools.kv().avail()), T); + dear.kv.cap_paged_tokens(T); + cheap.kv.cap_paged_tokens(T); + CHECK_OK(dear.kv.carve(dear.pools.kv())); + CHECK_OK(cheap.kv.carve(cheap.pools.kv())); + for (int32_t g = 1; g < (int32_t)dear_g.size(); ++g) { + if (!dear.kv.plan(g)->paged()) continue; + CHECK_EQ(cheap.kv.plan(g)->n_blocks, dear.kv.plan(g)->n_blocks); + CHECK(dear.kv.plan(g)->n_blocks > 0); + } + return need; +} + +static KVGroupInfo paged_group(const char* name, int kind, int64_t heads, int64_t block, + std::vector layers, int64_t window = 0) { + KVGroupInfo k; + k.name = name; + k.decl.kind = kind; + k.decl.dtype = RAD_BF16; + k.decl.n_head_kv = heads; + k.decl.head_dim = 8; + k.decl.window = window; + k.block_size = block; + k.layers = std::move(layers); + return k; +} + +/* A WINDOWED GROUP is capped at its window on both ranks, and the cap moves the bytes it frees to + * the other groups -- a kink in paged_split the inverse has to cross exactly. Here the dear rank's + * count is past the window, so its window group holds max_seqs x window_blocks on both ranks. */ +TEST(kv_pool_mib_for_tokens_crosses_a_windowed_groups_cap) { + std::vector dear = hybrid_groups(); + dear.push_back(paged_group("swa", RAD_KV_WINDOW, 4, 16, { 6, 7, 8, 9 }, 2048)); + std::vector cheap = dear; + cheap[G_FULL].decl.n_head_kv = 1; + check_inverse_and_carve(dear, cheap, 32); + /* And with the cheap rank's window group the cheaper one: what it saves is all in the window. */ + cheap = dear; + cheap[3].decl.n_head_kv = 1; + check_inverse_and_carve(dear, cheap, 32); +} + +/* TWO PAGED GROUPS AT DIFFERENT BLOCK SIZES: one token count, two block counts, each the dear + * rank's. */ +TEST(kv_pool_mib_for_tokens_holds_across_block_sizes) { + std::vector dear = hybrid_groups(); + dear.push_back(paged_group("full32", RAD_KV_FULL, 2, 32, { 6, 7, 8 })); + std::vector cheap = dear; + cheap[G_FULL].decl.n_head_kv = 1; + check_inverse_and_carve(dear, cheap, 8); + cheap = dear; + cheap[3].decl.n_head_kv = 1; + check_inverse_and_carve(dear, cheap, 8); +} + +/* AN ATTENTION-ZERO GROUP BESIDE ANOTHER PAGED GROUP: the zero group costs this rank nothing and + * takes no part in the split, so the other group alone sets what its pool pages -- and the zero + * group still holds the shared table's block count after the carve. */ +TEST(kv_pool_mib_for_tokens_with_a_zero_group_beside_a_paged_one) { + std::vector dear = hybrid_groups(); + dear.push_back(paged_group("full32", RAD_KV_FULL, 2, 32, { 6, 7, 8 })); + std::vector cheap = dear; + cheap[G_FULL].zero = true; + const int64_t need = check_inverse_and_carve(dear, cheap, 8); + /* Cheaper than a rank that keeps the group at half its heads. */ + std::vector kept = dear; + kept[G_FULL].decl.n_head_kv = 1; + CHECK(need < check_inverse_and_carve(dear, kept, 8)); +} + /* The re-split moves what the cheaper rank's KV pool saves into its weights pool, never past every * weight resident and never below the common pool; what the cap leaves is reported, not allocated. * A need that is not below the common pool changes nothing -- the even world's case. */ diff --git a/tests/oot_plugins.sh b/tests/oot_plugins.sh index 519bcdf6..ae197be1 100644 --- a/tests/oot_plugins.sh +++ b/tests/oot_plugins.sh @@ -117,9 +117,11 @@ grep -E "passed|failed" "$W/logs/kbench.log" | tail -3 say "4. the installed rad-kbench replays the fixture from the out-of-tree home: $(ls "$HH/kernels" | tr '\n' ' ')" # 5. The device library's machine code, against the tree's, object by object. A code object also -# carries its compilation unit's ID, which clang hashes from the command line and the output path, -# so two builds of one source never agree byte for byte; their instructions (.text) and kernel -# descriptors do, unless the flags that reach the device compiler differ. +# carries its compilation unit's ID, which rad_add_plugin names from the plugin and the source's +# path inside it rather than leaving clang to hash the build directory into it +# (cmake/RadAddPlugin.cmake) -- so the instructions (.text), the kernel descriptors and the rest of +# the read-only data agree between the two builds wherever each was made, unless the flags that +# reach the device compiler differ. # # THE DESCRIPTORS ARE COMPARED BY NAME, NOT AS .rodata's BYTES. The compiler lays them out in an # order that changes from one run of the same command to the next -- ROCm 7's clang does it to the diff --git a/tests/place_test.cpp b/tests/place_test.cpp index dced52ca..81b97753 100644 --- a/tests/place_test.cpp +++ b/tests/place_test.cpp @@ -25,6 +25,8 @@ #include "rad_test.h" #include "device/directio.h" +#include "mem/pools.h" +#include "mem/vram_budget.h" #include "place/heat.h" #include "place/mover.h" #include "place/stager.h" @@ -410,6 +412,46 @@ TEST(planner_spills_experts_before_dense_weights) { CHECK_EQ(zero_copy, 6); } +/* A RE-SPLIT RANK'S CONFIG REACHES BOTH READERS OF THE POOL SIZES. Engine::plan hands a rank whose + * cache costs less per token its own Config (vram_budget_rank_split): Pools::configure allocates + * the weights pool from it and the planner budgets the slab from it (Budgets::from), and the two + * have to read the same copy -- a grown pool the planner never reads is an empty pool, and a plan + * against a pool the rank does not have overruns it. plan_check_weights_pool is the startup check + * that they did. */ +TEST(a_resplit_ranks_config_reaches_the_pools_and_the_planner) { + Fixture f; + build_moe(f, 8, 1 * MiB, nullptr); + + Config common = base_config(); + common.placement = "expert_tiered"; + common.vram_weights_mib = 5; /* 1 MiB embedding and room for three experts and a shadow */ + common.vram_kv_mib = 8; + common.host_pool_mib = 64; + const RankSplit rs = vram_budget_rank_split(common.vram_weights_mib, common.vram_kv_mib, 2, + 64 * MiB); + CHECK(rs.resplit); + Config rc = common; + rc.vram_weights_mib = rs.weights_mib; + rc.vram_kv_mib = rs.kv_mib; + + Pools pools; + CHECK_OK(pools.configure(rc, 0, &rad_malloc_allocator(), &rad_malloc_allocator())); + CHECK_EQ(pools.weights().bytes(), rs.weights_mib * MiB); + CHECK_EQ(pools.kv().bytes(), rs.kv_mib * MiB); + CHECK_EQ(Budgets::from(rc).vram_weights, pools.weights().bytes()); + + Plan grown, flat; + { PlannerInput in{&f.prog, &rc, 0, 1, nullptr, 0}; CHECK_OK(Planner(in).plan(&grown)); } + { PlannerInput in{&f.prog, &common, 0, 1, nullptr, 0}; CHECK_OK(Planner(in).plan(&flat)); } + CHECK_EQ(grown.vram_budget, pools.weights().bytes()); + CHECK_EQ(plan_check_weights_pool(grown, pools.weights().bytes()), RAD_OK); + /* The grown pool is planned into: more of the experts are on the card. */ + CHECK(grown.totals.bytes[(int)Tier::VRAM] > flat.totals.bytes[(int)Tier::VRAM]); + CHECK(grown.totals.bytes[(int)Tier::VRAM] + grown.slab_overhead <= pools.weights().bytes()); + /* A planner handed the common split beside the rank's grown pool is refused by name. */ + CHECK_EQ(plan_check_weights_pool(flat, pools.weights().bytes()), RAD_E_STATE); +} + /* THE BUDGET'S EXPERT FLOOR PLACES EVERY EXPERT. A lending cache leaves the expert slab exactly * expert_vram_floor, so a floor short of what the planner spends -- the residents, the slab's * spare slots beside them, and the slack each first-fit pass can strand -- is experts with no diff --git a/tests/ref_test.cpp b/tests/ref_test.cpp index facf9cd7..8c254434 100644 --- a/tests/ref_test.cpp +++ b/tests/ref_test.cpp @@ -22,8 +22,6 @@ #include "rad_abi.h" #include "../libref/ref_common.h" #include "rad_sample.h" -#include "../vendor/nlohmann/json.hpp" -#include #include #include @@ -270,7 +268,7 @@ TEST(op_vocabulary) { "gated_had_quant_i8", "dequant", "gemm_nt", "gemm_nt_bias", "gemm_nt_q", "gemm_nt_q_bias", "grid_embed", - "attn_paged", "attn_dense", "kv_store", "rope", "rope_table", "qk_norm_rope", + "attn_paged", "attn_dense", "kv_store", "rope", "rope_table", "qk_norm_rope", "qsa_block_key", "router_topk", "moe_scatter", "moe_gemm", "moe_gemm_q", "moe_gemm_q_mod4", "moe_gather", "row_topk", "row_topk_merge", "logit_rerank", @@ -1781,16 +1779,33 @@ TEST(op_rope) { CHECK_EQ(qkv.at(5), 9.0f); } -TEST(yarn_rotary_matches_independent_vllm_vectors) { - const std::string here = __FILE__; - std::ifstream input(here.substr(0, here.find_last_of('/')) + "/data/yarn_reference.json"); - REQUIRE(input.good()); - const auto fixture = nlohmann::json::parse(input); - const auto positions = fixture["positions"].get>(); - for (const auto& c : fixture["cases"]) { - const int rot = c["rot"], half = rot / 2, hd = rot + 16; - const double low = rot == 64 ? 14 : (rot == 128 ? 28 : 56); - const double high = rot == 64 ? 22 : (rot == 128 ? 43 : 85); +/* STATIC YaRN, IN DOUBLE: cos and sin of rotary pair i at one position -- the inverse frequency + * theta^(-2i/rot), blended toward inv/factor over the linear ramp [low, high] as vLLM's + * YaRNScalingRotaryEmbedding blends it, times the magnitude. The bounds are vLLM's correction range + * at factor 2 over 262144 trained positions, theta 1e7 (tests/yarn_test.cpp holds yarn_from to the + * same integers), so these and the formula are the whole of the vLLM agreement. + * + * `tol` is what an f32 evaluation may differ by: 8 ulp of the angle (powf, the reciprocal, the blend + * and the product each cost about one) plus the rounding of the result. It is a few 1e-5 where the + * ramp sits at position 524287 and loose only in the fastest pairs there, which YaRN leaves + * unscaled and no f32 rotary places better than its angle's own ulp. */ +struct YarnRef { double c, s, tol; }; +YarnRef yarn_ref(double pos, int i, int rot, double low, double high) { + const double theta = 1e7, factor = 2.0, magnitude = 1.0 + 0.1 * std::log(2.0); + const double inv = std::pow(theta, -2.0 * i / rot); + const double ramp = std::min(1.0, std::max(0.0, (i - low) / (high == low ? 0.001 : high - low))); + const double a = pos * (inv / factor * ramp + inv * (1.0 - ramp)); + return { std::cos(a) * magnitude, std::sin(a) * magnitude, + magnitude * (std::fabs(a) * 0x1p-20 + 0x1p-21) }; +} + +TEST(yarn_rotary_matches_a_double_yarn_through_524287) { + const std::vector positions = { 0, 1, 4096, 262143, 262144, 500000, 524287 }; + const double magnitude = 1.0 + 0.1 * std::log(2.0); + const struct { int rot; double low, high; } cases[] = { { 64, 14, 22 }, { 128, 28, 43 }, + { 256, 56, 85 } }; + for (const auto& c : cases) { + const int rot = c.rot, half = rot / 2, hd = rot + 16; T pos(RAD_I32, {(int64_t)positions.size()}); pos.n(positions); T qkv(RAD_F32, {(int64_t)positions.size(), hd * 3}); std::vector values(positions.size() * hd * 3, 9.0f); @@ -1800,20 +1815,19 @@ TEST(yarn_rotary_matches_independent_vllm_vectors) { qkv.f(values); A direct; direct.t(qkv).t(pos).i("M", positions.size()).i("head_dim", hd).i("n_head", 1).i("n_head_kv", 1) .i("rotary_dim", rot).d("theta", 1e7).d("scale", 1).s("mode", "neox") - .d("yarn_factor", 2).d("yarn_low", low).d("yarn_high", high).d("yarn_magnitude", c["magnitude"]); + .d("yarn_factor", 2).d("yarn_low", c.low).d("yarn_high", c.high).d("yarn_magnitude", magnitude); CHECK_OK(run("rope", direct)); /* The table is position-addressed. Allocate its real extent, not a compact stand-in. */ T table(RAD_F32, {524288, rot}); A ta; ta.t(pos).t(table).i("M", positions.size()).i("rot", rot).d("theta", 1e7).d("scale", 1).s("dtype", "f32") - .d("yarn_factor", 2).d("yarn_low", low).d("yarn_high", high).d("yarn_magnitude", c["magnitude"]); + .d("yarn_factor", 2).d("yarn_low", c.low).d("yarn_high", c.high).d("yarn_magnitude", magnitude); CHECK_OK(run("rope_table", ta)); for (size_t m = 0; m < positions.size(); ++m) { for (int i = 0; i < half; ++i) { - const float cosine = c["cos"][m][i], sine = c["sin"][m][i]; - /* FP32 libm/pow versus torch; at long positions an inverse-frequency ULP - * is amplified into the phase. Bound the actual absolute cosine error. */ - CHECK_NEAR(qkv.at(m * hd * 3 + i), cosine, 0.004); - CHECK_NEAR(qkv.at(m * hd * 3 + half + i), sine, 0.004); + const YarnRef y = yarn_ref((double)positions[m], i, rot, c.low, c.high); + /* A unit pair (1, 0) rotated is (cos, sin). */ + CHECK_NEAR(qkv.at(m * hd * 3 + i), y.c, y.tol); + CHECK_NEAR(qkv.at(m * hd * 3 + half + i), y.s, y.tol); CHECK_EQ(table.at(positions[m] * rot + i), qkv.at(m * hd * 3 + i)); CHECK_EQ(table.at(positions[m] * rot + half + i), qkv.at(m * hd * 3 + half + i)); CHECK_EQ(qkv.at(m * hd * 3 + hd + i), qkv.at(m * hd * 3 + i)); @@ -1824,6 +1838,113 @@ TEST(yarn_rotary_matches_independent_vllm_vectors) { } } +/* THE REFERENCE IS NOT VACUOUS: the same op with the bounds one pair off, or without the + * magnitude, lands outside yarn_ref's bound somewhere in the ramp. */ +TEST(yarn_reference_bound_sees_a_shifted_ramp_and_a_missing_magnitude) { + const int rot = 64, hd = 64; + const double magnitude = 1.0 + 0.1 * std::log(2.0); + for (int variant = 0; variant < 2; ++variant) { + T pos(RAD_I32, {1}); pos.n({500000}); + T qkv(RAD_F32, {1, hd}); + std::vector values((size_t)hd, 0.0f); + for (int i = 0; i < rot / 2; ++i) values[(size_t)i] = 1.0f; + qkv.f(values); + A a; a.t(qkv).t(pos).i("M", 1).i("head_dim", hd).i("n_head", 1).i("n_head_kv", 0) + .i("rotary_dim", rot).d("theta", 1e7).d("scale", 1).s("mode", "neox") + .d("yarn_factor", 2).d("yarn_low", variant == 0 ? 15 : 14) + .d("yarn_high", variant == 0 ? 23 : 22).d("yarn_magnitude", variant == 0 ? magnitude : 1.0); + CHECK_OK(run("rope", a)); + int outside = 0; + for (int i = 0; i < rot / 2; ++i) { + const YarnRef y = yarn_ref(500000.0, i, rot, 14, 22); + outside += std::fabs(qkv.at(i) - y.c) > y.tol || std::fabs(qkv.at(rot / 2 + i) - y.s) > y.tol; + } + CHECK(outside > 0); + } +} + +/* QSA's compressed key. Identical unit keys pool to one and a unit gain with eps 0 norms to one, + * so what is left is the rotation at the block's position: c - s, then s + c, then the unrotated + * tail -- held to yarn_ref with YaRN on. Then the three things the op does besides rotate: the + * mean ROUNDED to k's dtype before the norm, `page` choosing the output row (and -1 none), and a + * [M, 3] position list read per frequency under imrope. */ +TEST(op_qsa_block_key) { + const int64_t n = 128, ratio = 4, rot = 64; + const double magnitude = 1.0 + 0.1 * std::log(2.0); + { + const std::vector blocks = { 0, 262144, 524287 }; + const int64_t M = (int64_t)blocks.size(); + T k(RAD_F32, {M * ratio, n}); k.f(std::vector((size_t)(M * ratio * n), 1.0f)); + T w(RAD_F32, {n}); w.f(std::vector((size_t)n, 1.0f)); + T pos(RAD_I32, {M}); pos.n(blocks); + T out(RAD_F32, {M, n}); + A a; a.t(k).t(w).tnull().t(pos).t(out).i("M", M).i("n", n).i("ratio", ratio) + .i("rotary_dim", rot).d("eps", 0).d("theta", 1e7).d("scale", 1).d("wadd", 0) + .s("dtype", "f32").d("yarn_factor", 2).d("yarn_low", 14).d("yarn_high", 22) + .d("yarn_magnitude", magnitude); + CHECK_OK(run("qsa_block_key", a)); + for (int64_t b = 0; b < M; ++b) + for (int64_t i = 0; i < n; ++i) { + if (i >= rot) { CHECK_EQ(out.at(b * n + i), 1.0f); continue; } + const YarnRef y = yarn_ref((double)blocks[(size_t)b], (int)(i % (rot / 2)), (int)rot, + 14, 22); + CHECK_NEAR(out.at(b * n + i), i < rot / 2 ? y.c - y.s : y.c + y.s, 2 * y.tol + 1e-6); + } + } + { /* bf16 keys whose lane-0 mean, 1 + 2^-9, rounds to 1 before the norm reads it. */ + T k(RAD_BF16, {ratio, 2}); + k.f({ 1.0f, 2.0f, 1.0f, 2.0f, 1.0f, 2.0f, 1.0f + 0x1p-7f, 2.0f }); + T w(RAD_F32, {2}); w.f({ 1.0f, 1.0f }); + T out(RAD_F32, {1, 2}); + A a; a.t(k).t(w).tnull().tnull().t(out).i("M", 1).i("n", 2).i("ratio", ratio) + .i("rotary_dim", 0).i("pos0", 0).d("eps", 0).d("theta", 1e7).d("wadd", 0) + .s("dtype", "bf16"); + CHECK_OK(run("qsa_block_key", a)); + CHECK_NEAR(out.at(0), 1.0 / std::sqrt(2.5), 1e-6); + CHECK_NEAR(out.at(1), 2.0 / std::sqrt(2.5), 1e-6); + } + { /* page: block 0 to row 1, block 1 nowhere, block 2 to row 0. Block b's lanes are + * (1, b + 1), so each normed row says which block it came from. */ + T k(RAD_F32, {3 * ratio, 2}); + std::vector kv; + for (int b = 0; b < 3; ++b) + for (int r = 0; r < ratio; ++r) { kv.push_back(1.0f); kv.push_back((float)(b + 1)); } + k.f(kv); + T w(RAD_F32, {2}); w.f({ 1.0f, 1.0f }); + T page(RAD_I32, {3}); page.n({ 1, -1, 0 }); + T out(RAD_F32, {2, 2}); out.f({ 7, 7, 7, 7 }); + A a; a.t(k).t(w).t(page).tnull().t(out).i("M", 3).i("n", 2).i("ratio", ratio) + .i("rotary_dim", 0).i("pos0", 0).d("eps", 0).d("theta", 1e7).d("wadd", 0) + .s("dtype", "f32"); + CHECK_OK(run("qsa_block_key", a)); + CHECK_NEAR(out.at(0), 1.0 / std::sqrt(5.0), 1e-6); + CHECK_NEAR(out.at(1), 3.0 / std::sqrt(5.0), 1e-6); + CHECK_NEAR(out.at(2), 1.0, 1e-6); + CHECK_NEAR(out.at(3), 1.0, 1e-6); + } + { /* imrope with a [M, 3] list: equal components are the one-position rotation, and moving + * component 1 moves frequency 1 (i % 3 == 1) and leaves frequency 0 alone. */ + T k(RAD_F32, {ratio, n}); k.f(std::vector((size_t)(ratio * n), 1.0f)); + T w(RAD_F32, {n}); w.f(std::vector((size_t)n, 1.0f)); + auto key = [&](std::vector p, bool multi) { + T pos(RAD_I32, multi ? std::vector{1, 3} : std::vector{1}); pos.n(p); + T out(RAD_F32, {1, n}); + A a; a.t(k).t(w).tnull().t(pos).t(out).i("M", 1).i("n", n).i("ratio", ratio) + .i("rotary_dim", rot).d("eps", 0).d("theta", 1e7).d("wadd", 0).s("dtype", "f32") + .s("mode", "imrope").s("sections", "11 11 10"); + CHECK_OK(run("qsa_block_key", a)); + std::vector v((size_t)n); + for (int64_t i = 0; i < n; ++i) v[(size_t)i] = out.at(i); + return v; + }; + const std::vector one = key({ 5000 }, false), same = key({ 5000, 5000, 5000 }, true); + const std::vector moved = key({ 5000, 9000, 5000 }, true); + for (int64_t i = 0; i < n; ++i) CHECK_EQ(same[(size_t)i], one[(size_t)i]); + CHECK_EQ(moved[0], one[0]); + CHECK(std::fabs(moved[1] - one[1]) > 1e-3); + } +} + TEST(disabled_yarn_ignores_auxiliary_coefficients) { for (double magnitude : {2.0, (double)NAN}) { T pos(RAD_I32, {1}); pos.n({0}); diff --git a/tests/sched_test.cpp b/tests/sched_test.cpp index 6fb07045..b1c9b83d 100644 --- a/tests/sched_test.cpp +++ b/tests/sched_test.cpp @@ -16,6 +16,9 @@ #include #include #include +#include + +#include using namespace rad; @@ -1605,12 +1608,12 @@ TEST(the_draft_depth_is_the_configured_one_whatever_the_acceptance_trace) { } /* ------------------------------------------------------------------ the adaptive window - * RADIANCE_DRAFT_ADAPT (draft.h). The switch is read from the environment once per process, so - * these cases turn the policy on through Params instead -- the controller cannot tell the two - * apart, and the tests do not depend on what the suite was started with. */ + * --draft-window adaptive (draft.h). The controller cases turn the policy on through Params, which + * is what Scheduler::init does with a serial drafter; the scheduler cases start a Fixture whose + * program declares one and whose Config asks for the window. */ -/* A small deterministic coin, the one the static case above uses. */ namespace { +/* A small deterministic coin, the one the static case above uses. */ struct Coin { uint64_t s = 12345; bool operator()(double pr) { @@ -1618,96 +1621,131 @@ struct Coin { return ((double)((s >> 33) & 0xFFFFFF) / (double)0x1000000) < pr; } }; + +DraftController adaptive_controller(int max_depth) { + DraftController c; + DraftController::Params p; + p.max_depth = max_depth; + p.adapt = true; + CHECK_OK(c.configure(p)); + return c; +} + +/* An estimate as a test states it: acceptance `a0` at the first position and `rest` after it, + * started, with its stop where the controller puts it. */ +DraftAdapt estimate(const DraftController& c, double a0, double rest) { + DraftAdapt s; + s.a[0] = (float)a0; + for (int j = 1; j < RAD_SCHED_MAX_SPEC; ++j) s.a[j] = (float)rest; + /* The stop draft.h states: the deepest draft emitted at least kDraftMinChance of the time. */ + double p = 1.0; + int d = 0; + while (d < c.next_depth()) { + p *= (double)s.a[d]; + if (p < DraftController::kDraftMinChance) break; + ++d; + } + s.depth = d < DraftController::kMinDepth ? DraftController::kMinDepth : d; + return s; +} + +/* What a scheduler fixture with the adaptive window looks like: a serial head of `depth` + * declared by the program, --num-speculative-tokens `depth`, --draft-window adaptive. */ +void adaptive_fixture(Fixture& f, int depth) { + f.build(/*blocks=*/4096, /*states=*/64); + f.cfg.n_spec = depth; + f.cfg.draft_window = DraftWindow::Adaptive; + f.prog.drafter.kind = RAD_DRAFT_SERIAL; + f.prog.drafter.depth = depth; +} + +/* Everything written to stderr while `fn` runs: the log lines a case checks for. */ +template std::string stderr_of(F fn) { + std::string text; + FILE* tmp = std::tmpfile(); + if (!tmp) return text; + const Log level = log_level(); + log_set_level(Log::Info); + std::fflush(stderr); + const int saved = dup(2); + dup2(fileno(tmp), 2); + fn(); + std::fflush(stderr); + dup2(saved, 2); + log_set_level(level); + close(saved); + std::rewind(tmp); + char buf[4096]; + size_t n; + while ((n = std::fread(buf, 1, sizeof buf, tmp)) > 0) text.append(buf, n); + std::fclose(tmp); + return text; +} } /* namespace */ TEST(the_adaptive_window_follows_each_sequences_own_acceptance) { - DraftController c; - DraftController::Params p; - p.max_depth = 6; - p.adapt = true; - CHECK_OK(c.configure(p)); + DraftController c = adaptive_controller(6); CHECK(c.adaptive()); CHECK_EQ(c.next_depth(), 6); /* the ceiling is still the flag */ + CHECK_EQ(c.step_depth(), 0); /* no step has chosen D: the ceiling stands */ - /* Nothing measured yet: both start from the prior, which on the default cost wants it all. */ + /* Nothing measured yet: both start from the prior, which drafts the whole window. */ DraftAdapt code, summary; - CHECK_EQ(c.depth_for(code), 6); - CHECK_EQ(c.depth_for(summary), 6); + CHECK_EQ(c.window_for(code), 6); + CHECK_EQ(c.window_for(summary), 6); /* Two texts through one controller. The drafter almost always agrees on one; on the other it - * agrees at the first position and rarely after it. Each window follows its own text. */ + * agrees at the first position and rarely after it. Each stop follows its own text. */ Coin coin; for (int t = 0; t < 600; ++t) { - int d = c.depth_for(code), a = 0; + int d = c.window_for(code), a = 0; while (a < d && coin(0.95)) ++a; c.observe(d, a); c.adapt_observe(code, d, a); - d = c.depth_for(summary); + d = c.window_for(summary); a = 0; while (a < d && coin(a == 0 ? 0.6 : 0.2)) ++a; c.observe(d, a); c.adapt_observe(summary, d, a); } - CHECK_EQ(c.depth_for(code), 6); - CHECK(c.depth_for(summary) >= 1); - CHECK(c.depth_for(summary) <= 2); + CHECK_EQ(c.window_for(code), 6); + CHECK(c.window_for(summary) >= 1); + CHECK(c.window_for(summary) <= 2); CHECK(code.a[0] > 0.85f); CHECK_NEAR(summary.a[0], 0.6, 0.2); - /* The estimate is the conditional rate, and the global hazard is still counted. */ + CHECK(c.yield(code, 6) > c.yield(summary, 6)); + /* The global hazard is still counted, and the windows the verifies carried. */ CHECK(c.reached(0) == 1200); - /* The windows the verifies carried are counted while the policy is on: 6 throughout for one, - * and 6 until the other moved off it. */ CHECK(c.verified_at(6) >= 600); CHECK(c.verified_at(1) + c.verified_at(2) > 0); CHECK_EQ(c.verified_at(1) + c.verified_at(2) + c.verified_at(3) + c.verified_at(4) + c.verified_at(5) + c.verified_at(6), (int64_t)1200); - /* And a window that went shallow comes back when the text changes: the positions it stopped + /* And a stop that went shallow comes back when the text changes: the positions it stopped * drafting drift toward the deepest it still drafts, so a text the drafter predicts again is * found without anyone drafting deep first. */ for (int t = 0; t < 600; ++t) { - const int d = c.depth_for(summary); + const int d = c.window_for(summary); int a = 0; while (a < d && coin(0.95)) ++a; c.adapt_observe(summary, d, a); } - CHECK_EQ(c.depth_for(summary), 6); + CHECK_EQ(c.window_for(summary), 6); } -/* A MOVE IS RATE-LIMITED: not within `hold` verifies of the last one, and only for a window worth - * `hyst` more than the one it leaves. A window that followed every flicker of its estimate would - * re-record its passes for nothing (runtime/ctx.h). */ -TEST(the_adaptive_window_moves_only_past_its_hold_and_its_hysteresis) { - DraftController c; - DraftController::Params p; - p.max_depth = 6; - p.adapt = true; - p.hold = 50; - CHECK_OK(c.configure(p)); +/* A STOP MOVES ONLY PAST THE HOLD: it is the sequence's share of the step's pass shape, and a shape + * that followed every flicker of its estimate would re-record its passes for nothing + * (runtime/ctx.h). And it never goes below one draft, or the estimate could never recover. */ +TEST(the_adaptive_window_moves_only_past_its_hold_and_never_below_one_draft) { + DraftController c = adaptive_controller(6); DraftAdapt s; - CHECK_EQ(c.depth_for(s), 6); - for (int t = 0; t < 49; ++t) CHECK_EQ(c.adapt_observe(s, 6, 0), 6); /* held */ - CHECK(c.adapt_observe(s, 6, 0) < 6); /* the 50th moves */ + CHECK_EQ(c.window_for(s), 6); + for (int t = 0; t < DraftController::kHold - 1; ++t) CHECK_EQ(c.adapt_observe(s, 6, 0), 6); + CHECK(c.adapt_observe(s, 6, 0) < 6); /* the kHold-th moves */ - /* The same trace with a hysteresis no estimate can clear never moves at all. */ - DraftController h; - p.hold = 0; - p.hyst = 1000.0f; - CHECK_OK(h.configure(p)); - DraftAdapt u; - CHECK_EQ(h.depth_for(u), 6); - for (int t = 0; t < 200; ++t) h.adapt_observe(u, 6, 0); - CHECK_EQ(u.depth, 6); - - /* The floor holds whatever the estimate says. */ - DraftController m; - p.hyst = 0.0f; - p.min_depth = 2; - CHECK_OK(m.configure(p)); - DraftAdapt v; - for (int t = 0; t < 200; ++t) m.adapt_observe(v, m.depth_for(v), 0); - CHECK_EQ(m.depth_for(v), 2); + for (int t = 0; t < 400; ++t) c.adapt_observe(s, c.window_for(s), 0); + CHECK_EQ(c.window_for(s), DraftController::kMinDepth); /* Off, the window is the flag and an observation is not taken. */ DraftController off; @@ -1716,151 +1754,199 @@ TEST(the_adaptive_window_moves_only_past_its_hold_and_its_hysteresis) { CHECK_OK(off.configure(q)); CHECK(!off.adaptive()); DraftAdapt w; - CHECK_EQ(off.depth_for(w), 5); + CHECK_EQ(off.window_for(w), 5); CHECK_EQ(off.adapt_observe(w, 5, 0), 5); CHECK_EQ(w.depth, 0); off.observe(5, 2); CHECK_EQ(off.verified_at(5), (int64_t)0); + off.step_time(3, 1, 12.0); /* and no step is timed */ + CHECK_EQ(off.cost_samples(3, 1), (int64_t)0); - /* Nonsense knobs are refused rather than run. */ - DraftController bad; - DraftController::Params r; - r.max_depth = 4; - r.adapt = true; - r.half_life = 0.0f; - CHECK(bad.configure(r) < 0); + /* Adaptive with no speculation is no policy at all. */ + DraftController zero = adaptive_controller(0); + CHECK(!zero.adaptive()); } -/* THE COST TABLE: measured points as they are, lines between them, the nearest two past the ends; - * and with fewer than two points, the default 1 + 0.06 d. A dear depth is a shallower window at - * the same estimate. */ -TEST(the_adaptive_window_prices_a_step_from_the_cost_table) { - DraftController c; - DraftController::Params p; - p.max_depth = 6; - p.adapt = true; - p.cost[2] = 11.3f; - p.cost[3] = 12.0f; - CHECK_OK(c.configure(p)); - CHECK_NEAR(c.cost(2), 11.3, 1e-4); - CHECK_NEAR(c.cost(3), 12.0, 1e-4); - CHECK_NEAR(c.cost(1), 10.6, 1e-4); /* extrapolated below */ - CHECK_NEAR(c.cost(6), 14.1, 1e-4); /* and above */ +/* THE STEP COST IS MEASURED. A key -- the step's deepest window and its class of running + * sequences -- with samples is its own mean; one without borrows the nearest measured key's, + * scaled by the prior's shape; nothing measured is the prior. The same times give the same + * choice, which is what makes the policy a function of what it was shown. */ +TEST(the_adaptive_window_prices_a_step_from_measured_step_times) { + DraftController c = adaptive_controller(6); + CHECK_NEAR(c.step_cost(1, 1), 1.06, 1e-12); /* the prior */ + CHECK_NEAR(c.step_cost(6, 1), 1.36, 1e-12); - DraftController d; - DraftController::Params q; - q.max_depth = 6; - q.adapt = true; - q.cost[1] = 1.0f; - q.cost[2] = 10.0f; /* a second draft costs nine times the first */ - CHECK_OK(d.configure(q)); - DraftAdapt s; - CHECK_EQ(d.depth_for(s), 1); + for (int i = 0; i < 10; ++i) c.step_time(3, 1, 12.0); + CHECK_EQ(c.cost_samples(3, 1), (int64_t)10); + CHECK_NEAR(c.step_cost(3, 1), 12.0, 1e-9); + CHECK_NEAR(c.step_cost(5, 1), 12.0 * 1.30 / 1.18, 1e-9); /* borrowed, the prior's shape */ + CHECK_NEAR(c.step_cost(3, 9), 12.0, 1e-9); /* the nearest class */ + CHECK_EQ(c.cost_samples(3, 9), (int64_t)0); + /* The classes: 1, 2-3, 4-7, 8-15, 16 and up. */ + c.step_time(3, 2, 20.0); + CHECK_NEAR(c.step_cost(3, 3), 20.0, 1e-9); + CHECK_NEAR(c.step_cost(3, 4), 20.0, 1e-9); /* 4-7 borrows from 2-3 */ + c.step_time(3, 16, 40.0); + CHECK_NEAR(c.step_cost(3, 1000), 40.0, 1e-9); + CHECK_NEAR(c.step_cost(3, 9), 40.0, 1e-9); /* 8-15: the nearest, 16 and up */ - DraftController e; - DraftController::Params r; - r.max_depth = 4; - r.adapt = true; - r.cost[3] = 5.0f; /* one point is no table */ - CHECK_OK(e.configure(r)); - CHECK_NEAR(e.cost(1), 1.06, 1e-9); - CHECK_NEAR(e.cost(4), 1.24, 1e-9); + /* A step that waited on something else counts for at most four times the mean. */ + const double before = c.step_cost(3, 1); + c.step_time(3, 1, 1.0e6); + CHECK(c.step_cost(3, 1) <= before * 4.0); + CHECK(c.step_cost(3, 1) > before); - /* A table whose line runs below zero is the wrong sign, and the default is used. */ - DraftController g; - DraftController::Params t; - t.max_depth = 6; - t.adapt = true; - t.cost[1] = 10.0f; - t.cost[2] = 4.0f; - CHECK_OK(g.configure(t)); - CHECK_NEAR(g.cost(6), 1.36, 1e-9); + /* Nonsense is not a sample. */ + c.step_time(0, 1, 5.0); + c.step_time(7, 1, 5.0); + c.step_time(2, 1, -1.0); + c.step_time(2, 0, 5.0); + CHECK_EQ(c.cost_samples(2, 1), (int64_t)0); + + /* One sequence that accepts nearly everything: on the prior the whole window pays... */ + DraftController p = adaptive_controller(6); + DraftAdapt s = estimate(p, 0.9, 0.9); + DraftAdapt* one[1] = { &s }; + CHECK_EQ(p.plan_step(one, 1, 1), 6); + /* ...and where the steps past three were measured dear, three is what pays. */ + DraftController m = adaptive_controller(6); + const double t[7] = { 0, 10.0, 11.0, 12.0, 30.0, 30.0, 30.0 }; + for (int d = 1; d <= 6; ++d) m.step_time(d, 1, t[d]); + DraftAdapt u = estimate(m, 0.9, 0.9); + DraftAdapt* mu[1] = { &u }; + CHECK_EQ(m.plan_step(mu, 1, 1), 3); + CHECK_EQ(m.window_for(u), 3); + /* Deterministic: the same times and the same estimate give the same depth. */ + DraftController m2 = adaptive_controller(6); + for (int d = 1; d <= 6; ++d) m2.step_time(d, 1, t[d]); + DraftAdapt u2 = estimate(m2, 0.9, 0.9); + DraftAdapt* mu2[1] = { &u2 }; + CHECK_EQ(m2.plan_step(mu2, 1, 1), 3); } -/* THE SCHEDULER'S HALF: the cap on a proposal, and the rounds the head runs. One sequence whose - * prior pays for two drafts of a ceiling of four verifies two, and the head runs two rounds. */ -TEST(the_adaptive_window_caps_the_proposal_and_the_rounds) { - Fixture f; - f.build(/*blocks=*/4096, /*states=*/64); - f.cfg.n_spec = 4; - CHECK_OK(f.start()); - DraftController::Params p; - p.max_depth = 4; - p.adapt = true; - p.prior = 0.3f; /* on the default cost: two */ - CHECK_OK(f.sch.draft().configure(p)); +/* THE HEAD DEPTH IS THE STEP'S. A serial head runs as many rounds as the deepest window in the + * step, so a sequence that accepts deep is worth deep rounds only while what its neighbours lose + * to them -- every round is a pass over every drafting row -- does not outweigh it. */ +TEST(the_adaptive_window_chooses_the_head_depth_over_the_whole_batch) { + DraftController c = adaptive_controller(6); + DraftAdapt deep = estimate(c, 0.95, 0.95); + DraftAdapt shallow = estimate(c, 0.5, 0.15); + CHECK_EQ(deep.depth, 6); + CHECK_EQ(shallow.depth, 1); /* its second draft is emitted 7.5 % of the time */ - const uint64_t a = f.submit(160, 64); - CHECK(f.sch.step() != nullptr); - f.commit_plain(11); + /* Side by side on the prior: deep rounds pay for the deep sequence, and the shallow one stops + * at its own first draft under them. */ + DraftAdapt* two[2] = { &deep, &shallow }; + CHECK_EQ(c.plan_step(two, 2, 2), 6); + CHECK_EQ(c.window_for(deep), 6); + CHECK_EQ(c.window_for(shallow), 1); - int32_t draft[4] = { 21, 22, 23, 24 }; - CHECK_OK(f.sch.set_draft(a, draft, 4)); - CHECK_EQ(f.sch.find(a)->n_draft, 2); - const RadBatch* b = f.sch.step(); - REQUIRE(b != nullptr); - CHECK_EQ(b->n_spec, 2); - CHECK_EQ(b->n_tok, 3); - CHECK_EQ(f.sch.plan().e[0].n_spec, 2); + /* The same deep sequence among seven shallow ones, at a class whose steps were measured to + * grow by three units a round: what the rounds cost is paid by eight rows, and the one + * that gains from them does not repay it. Every window comes down to one. */ + DraftController b = adaptive_controller(6); + for (int d = 1; d <= 6; ++d) b.step_time(d, 8, 10.0 + 3.0 * d); + DraftAdapt bd = estimate(b, 0.95, 0.95); + DraftAdapt bs[7]; + DraftAdapt* eight[8] = { &bd }; + for (int i = 0; i < 7; ++i) { bs[i] = estimate(b, 0.5, 0.15); eight[i + 1] = &bs[i]; } + CHECK_EQ(b.plan_step(eight, 8, 8), 1); + CHECK_EQ(b.window_for(bd), 1); + /* Alone at its own class, unmeasured, the same sequence takes the whole window. */ + DraftController a = adaptive_controller(6); + for (int d = 1; d <= 6; ++d) a.step_time(d, 8, 10.0 + 3.0 * d); + DraftAdapt ad = estimate(a, 0.95, 0.95); + DraftAdapt* lone[1] = { &ad }; + CHECK(a.plan_step(lone, 1, 1) > 1); - int rounds = 4; - Scheduler::SerialRow rows[4]; - CHECK_EQ(f.sch.serial_begin(&rounds, rows, 4), 1); - CHECK_EQ(rounds, 2); - const RadBatch* hb = f.sch.serial_batch(0, 1); - REQUIRE(hb != nullptr); - CHECK_EQ(hb->n_tok, 3); /* every verified position, as ever */ - CHECK(f.sch.serial_batch(2, 1) != nullptr); - f.sch.serial_end(2); + /* Rows that do not draft are part of the step: they emit a token whatever D is and pay for + * every round. Here three plain rows beside one drafting row at the same measured class. */ + DraftController r = adaptive_controller(6); + for (int d = 1; d <= 6; ++d) r.step_time(d, 4, 10.0 + 3.0 * d); + DraftAdapt rd = estimate(r, 0.95, 0.95); + DraftAdapt* rone[1] = { &rd }; + const int with_plain = r.plan_step(rone, 1, 4); + DraftController r2 = adaptive_controller(6); + for (int d = 1; d <= 6; ++d) r2.step_time(d, 4, 10.0 + 3.0 * d); + DraftAdapt rd2 = estimate(r2, 0.95, 0.95); + DraftAdapt* rone2[1] = { &rd2 }; + const int alone = r2.plan_step(rone2, 1, 1); /* class 1 unmeasured: borrows class 4-7 */ + CHECK(with_plain <= alone); - const SchedMetrics m = f.sch.metrics(); - CHECK(m.draft_adaptive); - CHECK_NEAR(m.draft_depth_adaptive, 2.0, 1e-6); - CHECK_EQ(m.draft_depth, 4); /* the ceiling, as before */ + /* D MOVES PAST A HOLD AND A HYSTERESIS. The first step that drafts takes the best outright; + * after that a better depth waits kHold steps. */ + DraftController h = adaptive_controller(6); + DraftAdapt hs = estimate(h, 0.95, 0.95); + DraftAdapt* hone[1] = { &hs }; + CHECK_EQ(h.plan_step(hone, 1, 1), 6); + for (int d = 1; d <= 6; ++d) h.step_time(d, 1, d <= 2 ? 10.0 : 40.0); + for (int t = 0; t < DraftController::kHold - 1; ++t) CHECK_EQ(h.plan_step(hone, 1, 1), 6); + CHECK_EQ(h.plan_step(hone, 1, 1), 2); + /* No drafting sequence: D stays where it is. */ + CHECK_EQ(h.plan_step(hone, 0, 3), 2); } -/* A BLOCK DRAFTER KEEPS ITS DEPTH. The engine turns the policy off before the first step when the - * drafter proposes its block in one pass; the proposal then reaches the step uncut, as without the - * switch. */ -TEST(the_adaptive_window_turns_off_for_a_block_drafter) { - Fixture f; - f.build(/*blocks=*/4096, /*states=*/64); - f.cfg.n_spec = 4; - CHECK_OK(f.start()); - DraftController::Params p; - p.max_depth = 4; - p.adapt = true; - p.prior = 0.3f; /* on the default cost: two */ - CHECK_OK(f.sch.draft().configure(p)); - f.sch.draft_adapt_off("a block drafter"); - CHECK(!f.sch.draft().adaptive()); +/* THE FLAG, AND ONLY FOR A SERIAL HEAD. --draft-window adaptive with a block drafter, with prompt + * lookup alone, or with no speculation keeps the configured depth and says so at startup; the + * proposal then reaches the step uncut. The default is the fixed window. */ +TEST(the_adaptive_window_is_a_flag_for_a_serial_drafter_and_said_when_it_is_not) { + { + Fixture f; + adaptive_fixture(f, 4); + int st = RAD_E_STATE; + const std::string log = stderr_of([&] { st = f.start(); }); + CHECK_OK(st); + CHECK(f.sch.draft().adaptive()); + CHECK(log.find("ADAPTIVE") != std::string::npos); + } + { + Fixture f; + adaptive_fixture(f, 4); + f.cfg.draft_window = DraftWindow::Fixed; + CHECK_OK(f.start()); + CHECK(!f.sch.draft().adaptive()); + Config d; + CHECK(d.draft_window == kDraftWindowDefault); /* the default */ + } + for (int kind : { (int)RAD_DRAFT_BLOCK, (int)RAD_DRAFT_NONE }) { + Fixture f; + adaptive_fixture(f, 4); + f.prog.drafter.kind = (decltype(f.prog.drafter.kind))kind; + if (kind == RAD_DRAFT_NONE) f.prog.drafter.depth = 0; + int st = RAD_E_STATE; + const std::string log = stderr_of([&] { st = f.start(); }); + CHECK_OK(st); + CHECK(!f.sch.draft().adaptive()); + CHECK(log.find("--draft-window adaptive: the window is fixed at 4") != std::string::npos); + CHECK(log.find(kind == RAD_DRAFT_BLOCK ? "a block drafter" : "prompt lookup only") != + std::string::npos); - const uint64_t a = f.submit(160, 64); - CHECK(f.sch.step() != nullptr); - f.commit_plain(11); - int32_t draft[4] = { 21, 22, 23, 24 }; - CHECK_OK(f.sch.set_draft(a, draft, 4)); - CHECK_EQ(f.sch.find(a)->n_draft, 4); - const SchedMetrics m = f.sch.metrics(); - CHECK(!m.draft_adaptive); - f.sch.draft_adapt_off("again"); /* a second call is a no-op */ - CHECK(!f.sch.draft().adaptive()); + const uint64_t a = f.submit(160, 64); + CHECK(f.sch.step() != nullptr); + f.commit_plain(11); + int32_t draft[4] = { 21, 22, 23, 24 }; + CHECK_OK(f.sch.set_draft(a, draft, 4)); + CHECK_EQ(f.sch.find(a)->n_draft, 4); + CHECK(!f.sch.metrics().draft_adaptive); + } + { + Fixture f; + adaptive_fixture(f, 4); + f.cfg.n_spec = 0; + const std::string log = stderr_of([&] { CHECK_OK(f.start()); }); + CHECK(!f.sch.draft().adaptive()); + CHECK(log.find("does not speculate") != std::string::npos); + } } -/* EACH SEQUENCE ITS OWN WINDOW, AND THE ROUNDS FOLLOW THE DEEPEST. The step issued ahead of a - * commit verifies, for each sequence, the first of those rounds up to its own window -- and the - * drafter's longer proposal is taken cut to that count rather than refused as a mismatch. */ +/* EACH SEQUENCE ITS OWN STOP, AND THE ROUNDS FOLLOW THE DEEPEST. The step issued ahead of a commit + * verifies, for each sequence, the first of those rounds up to its own window -- and the drafter's + * longer proposal is taken cut to that count rather than refused as a mismatch. */ TEST(the_adaptive_window_is_per_sequence_and_the_rounds_follow_the_deepest) { Fixture f; - f.build(/*blocks=*/4096, /*states=*/64); - f.cfg.n_spec = 4; + adaptive_fixture(f, 4); CHECK_OK(f.start()); - DraftController::Params p; - p.max_depth = 4; - p.adapt = true; - p.hold = 0; - p.hyst = 0.0f; - CHECK_OK(f.sch.draft().configure(p)); + REQUIRE(f.sch.draft().adaptive()); const uint64_t a = f.submit(160, 2000); const uint64_t b = f.submit(160, 2000); @@ -1892,6 +1978,7 @@ TEST(the_adaptive_window_is_per_sequence_and_the_rounds_follow_the_deepest) { CHECK_OK(f.sch.set_draft(b, draft, 4)); const RadBatch* s = f.sch.step(); REQUIRE(s != nullptr); + CHECK_EQ(f.sch.draft().step_depth(), 4); CHECK_EQ(s->n_spec, 4); CHECK_EQ(s->n_tok, 2 + 5); for (const StepEntry& e : f.sch.plan().e) CHECK_EQ(e.n_spec, e.seq == a ? 1 : 4); @@ -1929,27 +2016,141 @@ TEST(the_adaptive_window_is_per_sequence_and_the_rounds_follow_the_deepest) { CHECK(f.sch.metrics().draft_verify_depth[4] > 0); } -/* THE LOAD CAP (RADIANCE_DRAFT_ADAPT_BUSY) IS COUNTED ONCE A CYCLE AND HELD FOR IT. The load moves - * between cycles -- a sequence admitted, a client hanging up -- and the cap follows it from the - * next cycle on; within one, the plan, the rounds, the step issued ahead and the proposal - * published after the commit all see the same cap, so a count that changes mid-cycle is never a - * mismatch. The estimates learn on underneath: the windows come back with the load. */ -TEST(the_busy_cap_is_counted_once_a_cycle_and_follows_the_load_between_cycles) { - Fixture f; - f.build(/*blocks=*/4096, /*states=*/64); - f.cfg.n_spec = 4; - CHECK_OK(f.start()); - DraftController::Params p; - p.max_depth = 4; - p.adapt = true; - p.busy = 2; /* more than two running is busy */ - p.busy_depth = 2; - CHECK_OK(f.sch.draft().configure(p)); - CHECK_EQ(f.sch.draft().busy_cap(2), 0); - CHECK_EQ(f.sch.draft().busy_cap(3), 2); +/* A NEW SEQUENCE STARTS FROM THE SEQUENCES THAT FINISHED, ONE VOTE EACH. Pooled over verifies, a + * deep position is measured almost only on the texts good enough to be given a window that + * reaches it; here two texts the head predicts and two it does not, and the deep positions start + * a new sequence from all four. Until four have finished, the fixed prior. */ +TEST(the_adaptive_window_starts_a_sequence_from_finished_sequences_not_pooled_verifies) { + DraftController c = adaptive_controller(6); + /* The pooled counts a deep-drafting good text leaves: position 5 accepted 95 % of the times + * it was reached. They do not set the prior. */ + for (int t = 0; t < 1000; ++t) c.observe(6, 6); + CHECK_NEAR(c.prior_at(5), DraftController::kPrior, 1e-9); + DraftAdapt fresh; + (void)c.window_for(fresh); + CHECK_NEAR(fresh.a[5], DraftController::kPrior, 1e-6); + auto finished = [&](double a0, double rest) { + DraftAdapt s = estimate(c, a0, rest); + s.n[0] = 100; + return s; + }; + /* Too few verifies to be more than its own prior: not counted. */ + DraftAdapt young = finished(0.1, 0.1); + young.n[0] = 3; + c.adapt_retire(young); + c.adapt_retire(finished(0.95, 0.95)); + c.adapt_retire(finished(0.95, 0.95)); + c.adapt_retire(finished(0.3, 0.2)); + CHECK_NEAR(c.prior_at(5), DraftController::kPrior, 1e-9); /* three: not yet */ + c.adapt_retire(finished(0.3, 0.2)); + CHECK_NEAR(c.prior_at(0), (0.95 + 0.95 + 0.3 + 0.3) / 4.0, 1e-6); + CHECK_NEAR(c.prior_at(5), (0.95 + 0.95 + 0.2 + 0.2) / 4.0, 1e-6); + DraftAdapt next; + (void)c.window_for(next); + CHECK_NEAR(next.a[5], (0.95 + 0.95 + 0.2 + 0.2) / 4.0, 1e-6); + /* An unstarted estimate is not a sequence to count. */ + c.adapt_retire(DraftAdapt{}); + CHECK_NEAR(c.prior_at(5), (0.95 + 0.95 + 0.2 + 0.2) / 4.0, 1e-6); + + /* And through the scheduler: four requests whose head drafts are all refused finish, and the + * fifth starts at one draft where the first four started at the whole window. */ + Fixture f; + adaptive_fixture(f, 4); + CHECK_OK(f.start()); + std::vector ids; + for (int i = 0; i < 4; ++i) ids.push_back(f.submit(160, 48)); + REQUIRE(f.sch.step() != nullptr); + f.commit_plain(11); + CHECK_NEAR(f.sch.metrics().draft_depth_adaptive, 0.0, 1e-6); /* none drafted yet */ int32_t draft[4] = { 21, 22, 23, 24 }; - /* Every draft taken, so every estimate stays at the whole window. */ + for (uint64_t id : ids) CHECK_OK(f.sch.set_draft(id, draft, 4)); + CHECK_NEAR(f.sch.metrics().draft_depth_adaptive, 4.0, 1e-6); + for (int t = 0; t < 200 && f.sch.metrics().finished < 4; ++t) { + for (uint64_t id : ids) + if (f.sch.find(id)) (void)f.sch.set_draft(id, draft, 4); + REQUIRE(f.sch.step() != nullptr); + f.commit_plain(11); + } + CHECK_EQ(f.sch.metrics().finished, (int64_t)4); + CHECK(f.sch.draft().prior_at(0) < 0.2); + /* The step depth came down with them; a fresh sequence that started at the fixed prior would + * take it back to four at its first cycle (the hold has long passed), and this one does not. */ + CHECK_EQ(f.sch.draft().step_depth(), 1); + const uint64_t e = f.submit(160, 48); + REQUIRE(f.sch.step() != nullptr); + f.commit_plain(11); + CHECK_OK(f.sch.set_draft(e, draft, 4)); + REQUIRE(f.sch.step() != nullptr); /* its first cycle chooses D */ + f.commit_plain(11); + CHECK_EQ(f.sch.draft().step_depth(), 1); + CHECK_OK(f.sch.set_draft(e, draft, 4)); + CHECK_EQ(f.sch.find(e)->n_draft, 1); +} + +/* PROMPT LOOKUP IS NOT THE HEAD. When the head declines and the n-gram fallback proposes, the + * outcome is counted in the reported acceptance like any other draft and kept out of the + * sequence's estimate, which prices the head's next window. A run of lookups that are all refused + * leaves the head's window where it was; the same run of refusals from the head brings it down. */ +TEST(the_adaptive_window_is_not_fed_by_prompt_lookup) { + Fixture f; + adaptive_fixture(f, 4); + CHECK_OK(f.start()); + REQUIRE(f.sch.draft().adaptive()); + + /* A prompt that repeats, so the lookup always has a continuation to propose. */ + auto r = std::make_unique(); + r->id = f.next_id++; + r->max_tokens = 2000; + r->prompt.resize(160); + for (int i = 0; i < 160; ++i) r->prompt[(size_t)i] = 100 + i % 8; + const uint64_t a = r->id; + CHECK_OK(f.sch.add(std::move(r))); + REQUIRE(f.sch.step() != nullptr); + f.commit_plain(100); /* the pattern goes on */ + + int lookups = 0; + for (int t = 0; t < 4 * DraftController::kHold; ++t) { + REQUIRE(f.sch.step() != nullptr); /* nothing published: the lookup fills */ + const StepEntry& e = f.sch.plan().e[0]; + if (e.n_spec > 0) { + CHECK(e.ngram); + CHECK(e.n_spec <= f.sch.draft().step_window()); + ++lookups; + } + f.commit_plain(100 + (t + 1) % 8); /* every lookup refused */ + } + CHECK(lookups >= 2 * DraftController::kHold); + const SchedMetrics m = f.sch.metrics(); + CHECK(m.draft_tokens > 0); /* reported, as ever */ + CHECK(m.draft_pos_reached[0] >= lookups); + CHECK_EQ(m.draft_accepted, (int64_t)0); + CHECK_NEAR(m.draft_depth_adaptive, 4.0, 1e-6); /* the head's window did not move */ + + /* The head's own refusals are what move it. */ + int32_t draft[4] = { 21, 22, 23, 24 }; + for (int t = 0; t < 4 * DraftController::kHold; ++t) { + CHECK_OK(f.sch.set_draft(a, draft, 4)); + REQUIRE(f.sch.step() != nullptr); + CHECK(!f.sch.plan().e[0].ngram); + f.commit_plain(100 + (t + 1) % 8); + } + CHECK_NEAR(f.sch.metrics().draft_depth_adaptive, 1.0, 1e-6); +} + +/* THE SCHEDULER'S HALF OF THE COST: a decode step that verified drafts is timed at its deepest + * window and its row count when it finishes, and the head depth the next cycle chooses reads it. + * Measured dear past one round at two rows, both sequences' windows come down to one, and the + * rounds with them. */ +TEST(the_adaptive_window_times_its_steps_and_the_next_cycle_reads_them) { + Fixture f; + adaptive_fixture(f, 4); + CHECK_OK(f.start()); + REQUIRE(f.sch.draft().adaptive()); + + const uint64_t a = f.submit(160, 2000); + const uint64_t b = f.submit(160, 2000); + /* Every draft taken, so both stops stay at the whole window and only the cost moves D. */ auto commit_all = [&]() { const StepPlan& pl = f.sch.plan(); std::vector acc(pl.e.size(), 0), tok(pl.e.size(), 9); @@ -1962,116 +2163,40 @@ TEST(the_busy_cap_is_counted_once_a_cycle_and_follows_the_load_between_cycles) { r.eos = eos.data(); CHECK_OK(f.sch.commit(r)); }; - auto spec_of = [&](uint64_t id) { - for (const StepEntry& e : f.sch.plan().e) if (e.seq == id) return (int)e.n_spec; - return -1; - }; - - const uint64_t a = f.submit(160, 2000); - const uint64_t b = f.submit(160, 2000); - REQUIRE(f.sch.step() != nullptr); /* both prompts */ + REQUIRE(f.sch.step() != nullptr); /* both prompts */ commit_all(); + f.sch.step_finished(); /* a prefill step: not a sample */ + for (int d = 1; d <= 4; ++d) CHECK_EQ(f.sch.draft().cost_samples(d, 2), (int64_t)0); - /* Two running is not busy: each takes its own window. */ + int32_t draft[4] = { 21, 22, 23, 24 }; CHECK_OK(f.sch.set_draft(a, draft, 4)); CHECK_OK(f.sch.set_draft(b, draft, 4)); REQUIRE(f.sch.step() != nullptr); - CHECK_EQ(spec_of(a), 4); - CHECK_EQ(spec_of(b), 4); + CHECK_EQ(f.sch.plan().n_spec, 4); commit_all(); + f.sch.step_finished(); + CHECK_EQ(f.sch.draft().cost_samples(4, 2), (int64_t)1); - /* A third arrives. The plan that admits it counted two when its cycle opened, so that cycle - * is not busy yet... */ - const uint64_t c = f.submit(160, 2000); + /* The hold, then a cycle that reads steps past one round as four times dearer. */ + for (int d = 1; d <= 4; ++d) + for (int i = 0; i < 64; ++i) f.sch.draft().step_time(d, 2, d == 1 ? 10.0 : 40.0); + for (int t = 0; t < DraftController::kHold + 1; ++t) { + CHECK_OK(f.sch.set_draft(a, draft, 4)); + CHECK_OK(f.sch.set_draft(b, draft, 4)); + REQUIRE(f.sch.step() != nullptr); + commit_all(); + } + CHECK_EQ(f.sch.draft().step_depth(), 1); CHECK_OK(f.sch.set_draft(a, draft, 4)); CHECK_OK(f.sch.set_draft(b, draft, 4)); REQUIRE(f.sch.step() != nullptr); - CHECK_EQ(spec_of(a), 4); - CHECK_EQ(spec_of(c), 0); /* its prompt */ - commit_all(); - - /* ...and the next one is: three running, every window capped, no estimate moved. The - * proposals were published under the last cycle, four long; this plan cuts them. */ - CHECK_OK(f.sch.set_draft(a, draft, 4)); - CHECK_OK(f.sch.set_draft(b, draft, 4)); - REQUIRE(f.sch.step() != nullptr); - CHECK_EQ(spec_of(a), 2); - CHECK_EQ(spec_of(b), 2); - CHECK(spec_of(c) <= 2); - CHECK_NEAR(f.sch.metrics().draft_depth_adaptive, 2.0, 1e-6); + for (const StepEntry& e : f.sch.plan().e) CHECK_EQ(e.n_spec, 1); int rounds = 4; Scheduler::SerialRow rows[4]; - CHECK_EQ(f.sch.serial_begin(&rounds, rows, 4), 3); - CHECK_EQ(rounds, 2); - REQUIRE(f.sch.serial_batch(0, 1) != nullptr); - REQUIRE(f.sch.serial_batch(2, 1) != nullptr); - - /* The step issued ahead verifies the cycle's count... */ - for (uint64_t id : { a, b, c }) f.sch.find(id)->speculation_fixed = true; - for (uint64_t id : { a, b, c }) f.sch.expect_draft(id, rounds); - const RadBatch* nb = f.sch.step_next(); - REQUIRE(nb != nullptr); - CHECK_EQ(nb->n_spec, 2); - - /* ...and a client hangs up before the commit. Two running would not be busy, and the cycle - * keeps its cap anyway: the proposal published after the commit matches the step that is - * already on the card, so set_draft has nothing to refuse. */ - CHECK_OK(f.sch.cancel(c)); - CHECK_NEAR(f.sch.metrics().draft_depth_adaptive, 2.0, 1e-6); - commit_all(); - f.sch.serial_end(rounds); - CHECK_OK(f.sch.set_draft(a, draft, rounds)); - CHECK_OK(f.sch.set_draft(b, draft, rounds)); - CHECK_EQ(f.sch.find(a)->n_draft, 2); - - /* The next cycle opens on the load as it now is: two running, the cap lifted, and the - * windows are the whole window again at once -- the estimates never stopped learning. */ - f.sch.promote_next(); - CHECK_EQ(spec_of(a), 2); /* planned last cycle */ - rounds = 4; CHECK_EQ(f.sch.serial_begin(&rounds, rows, 4), 2); - CHECK_EQ(rounds, 4); - REQUIRE(f.sch.serial_batch(0, 1) != nullptr); - for (int rd = 2; rd <= rounds; ++rd) REQUIRE(f.sch.serial_batch(rd, 1) != nullptr); - commit_all(); - f.sch.serial_end(rounds); - CHECK_OK(f.sch.set_draft(a, draft, rounds)); - CHECK_OK(f.sch.set_draft(b, draft, rounds)); - REQUIRE(f.sch.step() != nullptr); - CHECK_EQ(spec_of(a), 4); - CHECK_EQ(spec_of(b), 4); - CHECK_NEAR(f.sch.metrics().draft_depth_adaptive, 4.0, 1e-6); - commit_all(); - - /* THE SAME HANG-UP WITH NO STEP ISSUED AHEAD, where the held cap is what the proposal gets: a - * busy cycle stays busy to its end, and the next plan is the first to see the load fall. */ - const uint64_t e = f.submit(160, 2000); - CHECK_OK(f.sch.set_draft(a, draft, 4)); - CHECK_OK(f.sch.set_draft(b, draft, 4)); - REQUIRE(f.sch.step() != nullptr); /* admits it; counted two */ - commit_all(); - CHECK_OK(f.sch.set_draft(a, draft, 4)); - CHECK_OK(f.sch.set_draft(b, draft, 4)); - REQUIRE(f.sch.step() != nullptr); /* counted three: busy */ - CHECK_EQ(spec_of(a), 2); - CHECK_OK(f.sch.cancel(e)); - commit_all(); - CHECK_OK(f.sch.set_draft(a, draft, 4)); - CHECK_EQ(f.sch.find(a)->n_draft, 2); /* the cycle's cap, not the live count's */ - REQUIRE(f.sch.step() != nullptr); /* counted two: lifted */ - CHECK_EQ(spec_of(a), 2); /* drafted under the busy cycle */ - rounds = 4; - CHECK_EQ(f.sch.serial_begin(&rounds, rows, 4), 2); - CHECK_EQ(rounds, 4); + CHECK_EQ(rounds, 1); f.sch.serial_end(0); - - /* No threshold set: no cap, whatever the load. */ - DraftController::Params q; - q.max_depth = 4; - q.adapt = true; - DraftController d; - CHECK_OK(d.configure(q)); - CHECK_EQ(d.busy_cap(1000), 0); + CHECK_NEAR(f.sch.metrics().draft_depth_adaptive, 1.0, 1e-6); } /* ================================================================== the step path */ diff --git a/tests/server_test.cpp b/tests/server_test.cpp index 9983be70..6e003849 100644 --- a/tests/server_test.cpp +++ b/tests/server_test.cpp @@ -2473,9 +2473,9 @@ TEST(each_draft_position_counts_its_acceptances) { 5.0 / 7.0, 1e-9); } -/* THE ADAPTIVE DRAFT WINDOW (RADIANCE_DRAFT_ADAPT) IS EXPOSED ONLY WHILE IT IS ON, so a deployment - * without it scrapes exactly what it always did -- and when it is on, the gauge and the histogram - * are well-formed families with the labels every other sample carries. */ +/* THE ADAPTIVE DRAFT WINDOW (--draft-window adaptive) IS EXPOSED ONLY WHILE IT IS ON, so a + * deployment without it scrapes exactly what it always did -- and when it is on, the gauge and the + * histogram are well-formed families with the labels every other sample carries. */ TEST(the_adaptive_draft_window_is_exposed_only_while_it_is_on) { Metrics m("m", 1024); server::SchedMetrics s; diff --git a/tests/yarn_test.cpp b/tests/yarn_test.cpp index 0bea0a69..ea58e913 100644 --- a/tests/yarn_test.cpp +++ b/tests/yarn_test.cpp @@ -2,6 +2,7 @@ #include "../arch/common/rad_arch.h" #include "format/rope_override.h" +#include #include #include #include @@ -30,6 +31,72 @@ TEST(yarn_metadata_is_independent_of_deployment_context) { CHECK_EQ(draft.yarn.factor, 4.0f); CHECK_EQ(target.yarn.factor, 2.0f); } +/* THE CORRECTION BOUNDS ARE vLLM's: _yarn_find_correction_range at beta 32/1 over 262144 trained + * positions and theta 1e7, floored and ceiled, for the three rotary widths the Qwen family uses. + * These integers and the blend formula in abi/rad_yarn.h are the whole of the vLLM agreement; the + * frequency and phase references in tests/ref_test.cpp are pinned to them. */ +TEST(yarn_correction_bounds_match_vllm_at_every_rotary_width) { + const char* keys[] = {"rope_parameters.rope_type", "rope_parameters.factor", + "rope_parameters.original_max_position_embeddings"}; + const char* vals[] = {"yarn", "2", "262144"}; + RadModelMeta m{}; m.n_kv = 3; m.kv_key = keys; m.kv_val = vals; + const struct { int64_t rot; float low, high; } want[] = { + {64, 14.0f, 22.0f}, {128, 28.0f, 43.0f}, {256, 56.0f, 85.0f}}; + for (const auto& w : want) { + Geom g{}; g.theta = 1e7f; + CHECK_OK(yarn_from(g, &m, "", w.rot)); + CHECK_EQ(g.yarn.low, w.low); CHECK_EQ(g.yarn.high, w.high); + CHECK_NEAR(g.yarn.magnitude, 1.0 + 0.1 * std::log(2.0), 1e-7); + } +} + +/* ONE READER, AND IT ANSWERS FOR EVERY TYPE: default and the legacy `mrope` spelling are no + * scaling, linear divides positions, and a scheme it does not implement is refused by name with + * the geometry left as it was -- never read as a linear divide by its factor. */ +TEST(the_rotary_reader_names_every_type_it_does_not_implement) { + const char* keys[] = {"rope_scaling.type", "rope_scaling.factor", + "rope_scaling.original_max_position_embeddings"}; + const char* vals[] = {"mrope", "8", "8192"}; + RadModelMeta m{}; m.n_kv = 3; m.kv_key = keys; m.kv_val = vals; + Geom g{}; g.theta = 5e5f; g.rope_scale = 7.0f; + CHECK_OK(yarn_from(g, &m, "", 128)); + CHECK_EQ(g.yarn.factor, 0.0f); CHECK_EQ(g.rope_scale, 1.0f); + for (const char* t : {"llama3", "longrope", "dynamic", "su", "bogus"}) { + vals[0] = t; + g.rope_scale = 7.0f; + CHECK_EQ(yarn_from(g, &m, "", 128), RAD_E_UNSUPPORTED); + CHECK_EQ(g.rope_scale, 7.0f); CHECK_EQ(g.yarn.factor, 0.0f); + } + vals[0] = "linear"; + CHECK_OK(yarn_from(g, &m, "", 128)); + CHECK_EQ(g.rope_scale, 8.0f); CHECK_EQ(g.yarn.factor, 0.0f); + /* Nothing named at all is the default, whatever a previous read left. */ + m.n_kv = 0; + CHECK_OK(yarn_from(g, &m, "", 128)); + CHECK_EQ(g.rope_scale, 1.0f); +} + +/* `truncate` is a JSON boolean: the converter writes 1/0, and true/false spelled out is the same. */ +TEST(yarn_truncate_takes_true_and_false_as_well_as_one_and_zero) { + const char* keys[] = {"rope_parameters.rope_type", "rope_parameters.factor", + "rope_parameters.original_max_position_embeddings", + "rope_parameters.truncate"}; + const char* vals[] = {"yarn", "2", "262144", "true"}; + RadModelMeta m{}; m.n_kv = 4; m.kv_key = keys; m.kv_val = vals; + Geom a{}, b{}; a.theta = b.theta = 1e7f; + CHECK_OK(yarn_from(a, &m, "", 64)); + CHECK_EQ(a.yarn.low, 14.0f); CHECK_EQ(a.yarn.high, 22.0f); + vals[3] = "1"; CHECK_OK(yarn_from(b, &m, "", 64)); CHECK_EQ(b.yarn.low, a.yarn.low); + vals[3] = "false"; + CHECK_OK(yarn_from(a, &m, "", 64)); + CHECK(a.yarn.low > 14.0f && a.yarn.low < 15.0f); + vals[3] = "0"; CHECK_OK(yarn_from(b, &m, "", 64)); CHECK_EQ(b.yarn.low, a.yarn.low); + for (const char* bad : {"yes", "2", "True"}) { + vals[3] = bad; + CHECK_EQ(yarn_from(a, &m, "", 64), RAD_E_INVAL); + } +} + TEST(yarn_metadata_rejects_bad_inputs_and_preserves_default) { const char* keys[] = {"rope_parameters.rope_type", "rope_parameters.factor", "rope_parameters.original_max_position_embeddings"}; @@ -217,6 +284,12 @@ TEST(rope_override_refuses_linear_and_scales_a_drafter_on_its_own) { CHECK(!rad::rope_override_group(bare.k, bare.v, spec("yarn:2"), "", 0, &why)); CHECK(rad::rope_override_group(bare.k, bare.v, spec("yarn:2:65536"), "", 0, &why)); CHECK_EQ(*bare.get("rope_parameters.rope_type"), std::string("yarn")); + /* The legacy `mrope` type is default with sections, so YaRN goes over it. */ + Meta vl{ { "rope_scaling.type", "mrope" }, { "rope_scaling.mrope_section", "16 24 24" } }; + CHECK(rad::rope_override_group(vl.k, vl.v, spec("yarn:2"), "", 32768, &why)); + Geom gv{}; gv.theta = 1e6f; + CHECK_OK(yarn_from(gv, vl.meta(), "", 128)); + CHECK_EQ(gv.yarn.factor, 2.0f); Meta d = qfn_container(); d.k.push_back("draft.rope_parameters.rope_type"); d.v.push_back("default"); @@ -230,4 +303,51 @@ TEST(rope_override_refuses_linear_and_scales_a_drafter_on_its_own) { CHECK_EQ(dr.yarn.low, 28.0f); CHECK_EQ(dr.yarn.high, 43.0f); } +/* A persisted prefix store is fingerprinted with rope_identity (Engine::configure_kv). The stock + * container, the same container under --rope-scaling, one converted with YaRN and the same model + * whose drafter alone is scaled must all differ; key order and unrelated keys must not matter. */ +TEST(the_rotary_identity_tells_every_resolved_rotary_apart) { + std::string why; + Meta stock = qfn_container(); + const std::string base = rad::rope_identity(stock.k, stock.v); + CHECK(!base.empty()); + CHECK(base.find("text_config.rope_parameters.rope_type=default") != std::string::npos); + + Meta flag = qfn_container(); + CHECK(rad::rope_override_group(flag.k, flag.v, spec("yarn:2"), "", 262144, &why)); + const std::string scaled = rad::rope_identity(flag.k, flag.v); + CHECK(scaled != base); + Meta flag4 = qfn_container(); + CHECK(rad::rope_override_group(flag4.k, flag4.v, spec("yarn:4"), "", 262144, &why)); + CHECK(rad::rope_identity(flag4.k, flag4.v) != scaled); + + /* Converted with YaRN: the same keys written by the converter, not by the flag. */ + Meta conv = qfn_container(); + conv.v[9] = "yarn"; + conv.k.push_back("text_config.rope_parameters.factor"); conv.v.push_back("2"); + conv.k.push_back("text_config.rope_parameters.original_max_position_embeddings"); + conv.v.push_back("262144"); + CHECK(rad::rope_identity(conv.k, conv.v) != base); + conv.k.push_back("text_config.rope_parameters.beta_fast"); conv.v.push_back("16"); + CHECK(rad::rope_identity(conv.k, conv.v) != rad::rope_identity(flag.k, flag.v)); + + Meta draft = qfn_container(); + draft.k.push_back("draft.rope_theta"); draft.v.push_back("10000000"); + const std::string d0 = rad::rope_identity(draft.k, draft.v); + draft.k.push_back("draft.rope_parameters.rope_type"); draft.v.push_back("yarn"); + CHECK(rad::rope_identity(draft.k, draft.v) != d0); + + Meta shuffled = qfn_container(); + std::reverse(shuffled.k.begin(), shuffled.k.end()); + std::reverse(shuffled.v.begin(), shuffled.v.end()); + shuffled.k.push_back("eos_token_id"); shuffled.v.push_back("248044"); + shuffled.k.push_back("max_position_embeddings"); shuffled.v.push_back("1"); + CHECK_EQ(rad::rope_identity(shuffled.k, shuffled.v), base); + Meta gguf{ { "qwen35.rope.dimension_count", "64" }, { "qwen35.rope.freq_base", "1e7" }, + { "partial_rotary_factor", "0.25" }, { "qwen35.block_count", "64" } }; + CHECK_EQ(rad::rope_identity(gguf.k, gguf.v), + std::string("partial_rotary_factor=0.25;qwen35.rope.dimension_count=64;" + "qwen35.rope.freq_base=1e7;")); +} + RAD_TEST_MAIN() diff --git a/tools/rad_convert.cpp b/tools/rad_convert.cpp index ba5acfff..ae9324ef 100644 --- a/tools/rad_convert.cpp +++ b/tools/rad_convert.cpp @@ -747,7 +747,6 @@ int main(int argc, char** argv) { w.meta_i("n_expert_shared",meta.m.n_expert_shared); w.meta_f("rms_eps", meta.m.rms_eps); w.meta_f("rope_theta", meta.m.rope_theta); - w.meta_f("rope_scale", meta.m.rope_scale); for (int i = 0; i < meta.m.n_kv; ++i) w.meta_s(meta.m.kv_key[i], meta.m.kv_val[i]); /* THE CONTAINER CARRIES AN ENCODER'S WEIGHTS: what `--mm-max-patches auto` keys on. */ if (prog.encoder.modalities != 0) w.meta_s("radiance.encoder", prog.encoder_name.c_str()); diff --git a/tools/rad_kbench.cpp b/tools/rad_kbench.cpp index d8ac627d..784eee63 100644 --- a/tools/rad_kbench.cpp +++ b/tools/rad_kbench.cpp @@ -1707,7 +1707,7 @@ int main(int argc, char** argv) { meta.n_expert = mi("n_expert", 0); meta.n_expert_used = mi("n_expert_used", 0); meta.n_expert_shared = mi("n_expert_shared", 0); meta.rms_eps = mf("rms_eps", 1e-6); meta.rope_theta = mf("rope_theta", 10000.f); - meta.rope_scale = mf("rope_scale", 1.f); + meta.rope_scale = 1.f; /* the rotary's scaling is the free-form rope keys (rad_builder.h) */ /* The gated-delta-net head counts. No op names them -- the kernels read them off the operands * -- so a recipe sizing a GDN case would otherwise have to invent them, and a GDN check at an diff --git a/tools/rad_tune.cpp b/tools/rad_tune.cpp index a80b2eb9..f8933eca 100644 --- a/tools/rad_tune.cpp +++ b/tools/rad_tune.cpp @@ -333,7 +333,7 @@ int main(int argc, char** argv) { meta.n_expert = mi("n_expert", 0); meta.n_expert_used = mi("n_expert_used", 0); meta.n_expert_shared = mi("n_expert_shared", 0); meta.rms_eps = mf("rms_eps", 1e-6); meta.rope_theta = mf("rope_theta", 10000.f); - meta.rope_scale = mf("rope_scale", 1.f); + meta.rope_scale = 1.f; /* the rotary's scaling is the free-form rope keys (rad_builder.h) */ std::vector kk, kv; std::vector kp, vp;