diff --git a/docs/SETTINGS.md b/docs/SETTINGS.md index 2fe687581e..edda968eb9 100644 --- a/docs/SETTINGS.md +++ b/docs/SETTINGS.md @@ -470,7 +470,7 @@ Off unless you set them. The last column says where each one is described. | `STRATA_WATCHDOG_S` | text | NVIDIA and AMD, Intel Arc | [DETAILS.md](DETAILS.md) | | `STRATA_WDDM_BUDGET` | number | AMD, Intel Arc | [AMD_HIP.md](AMD_HIP.md) | -## Switches that go back to earlier behaviour (142) +## Switches that go back to earlier behaviour (143) These behaviours are on by default. Setting the switch returns the earlier behaviour; the notes in the register say which version made it the default. @@ -522,6 +522,7 @@ version made it the default. | `STRATA_INK_IQ_TC_MASK` | number | NVIDIA and AMD | v0.2.23 | - | | `STRATA_INK_KEEP_MAPPED_PAGES` | on / off | NVIDIA and AMD | v0.2.21 | - | | `STRATA_INK_KV_VMM` | on / off | NVIDIA and AMD | - | [INKLING_KV.md](INKLING_KV.md) | +| `STRATA_INK_LANE_PROBE_OLD` | on / off | NVIDIA and AMD | - | [INKLING_KV.md](INKLING_KV.md) | | `STRATA_INK_MOE_V1` | on / off | NVIDIA and AMD | v0.2.21 | - | | `STRATA_INK_NO_CAND` | on / off | NVIDIA and AMD | v0.2.21 | - | | `STRATA_INK_OOM_RETRY` | number | NVIDIA and AMD | v0.2.21 | - | diff --git a/engine/CMakeLists.txt b/engine/CMakeLists.txt index f04e63fa63..00dba50af6 100644 --- a/engine/CMakeLists.txt +++ b/engine/CMakeLists.txt @@ -324,6 +324,9 @@ if(STRATA_BUILD_TESTS) target_include_directories(ink_batch_budget_test PRIVATE src/inkling2) add_test(NAME ink_batch_budget_test COMMAND ink_batch_budget_test) # #6770 / #6755: when strata-inkling's CUDA global K/V may grow in VRAM (net growth, not the whole new K/V; no GPU) + add_executable(ink_cold_lane_test tests/core/ink_cold_lane_test.cpp) + target_include_directories(ink_cold_lane_test PRIVATE include) + add_test(NAME ink_cold_lane_test COMMAND ink_cold_lane_test) add_executable(ink_kv_grow_test tests/core/ink_kv_grow_test.cpp) target_include_directories(ink_kv_grow_test PRIVATE include) add_test(NAME ink_kv_grow_test COMMAND ink_kv_grow_test) diff --git a/engine/include/strata/core/ink_cold_lane.hpp b/engine/include/strata/core/ink_cold_lane.hpp new file mode 100644 index 0000000000..fabbd8a025 --- /dev/null +++ b/engine/include/strata/core/ink_cold_lane.hpp @@ -0,0 +1,33 @@ +// strata-inkling CUDA, hybrid placement: which lane computes the cold experts (the ones that are not on the card), decided by a short probe at the start. +// +// STRATA_INK_EXACT unset: the same few decode steps are timed with the CPU lane and with the card's own copy-in lane (the "exact" lane: the same bits as an all-resident run), once +// per lane, and the faster one is kept. The probe is noisy (the PC's own load moves the CPU lane's time by 10 to 30 %) and the old rule kept the CPU lane unless the copy-in lane was +// clearly (10 %) faster: on an RTX 5070 12 GB (Windows, SQ-10, drafter off, the published 0.2.24 packages) the probe measured the CPU lane at 9.0 to 9.5 ms and the copy-in lane at +// 8.6 ms (ratio 0.95), the old rule kept the CPU lane, and every start decoded 65-68 tok/s and 34.5 on a 4K document where the copy-in lane gives 73 and 56.4 (+11 to +64 %); with a +// busier PC the same card measured 12 against 8.6 and chose the copy-in lane. The rule below runs the probe three times, alternating the lanes (the median of each lane), and keeps the +// exact lane unless the CPU lane is clearly (10 %) faster; a tie goes to the exact lane. +// +// STRATA_INK_LANE_PROBE_OLD=1 brings the old rule back (one probe per lane, the CPU lane unless the copy-in lane is 10 % faster). +#pragma once + +#include +#include + +namespace strata::core { + +/// The median of the timed probes of one lane (the middle one of an odd count, the mean of the two middle ones of an even count); 0 for no probe. +inline double ink_probe_median(std::vector v) { + if (v.empty()) return 0.0; + std::sort(v.begin(), v.end()); + const size_t n = v.size(); + return (n % 2) ? v[n / 2] : 0.5 * (v[n / 2 - 1] + v[n / 2]); +} + +/// true: the cold experts are copied into the card and computed by its kernels (the exact lane); false: computed on the CPU where they are. +/// `t_cpu`, `t_card`: the probe's time per token with each lane. `old_rule`: STRATA_INK_LANE_PROBE_OLD=1. +inline bool ink_cold_lane_exact(double t_cpu, double t_card, bool old_rule) { + if (old_rule) return t_card < 0.9 * t_cpu; + return !(t_cpu < 0.9 * t_card); +} + +} // namespace strata::core diff --git a/engine/src/inkling2/cuda/ink_runner.cuh b/engine/src/inkling2/cuda/ink_runner.cuh index fc4f3622af..b4a2a11068 100644 --- a/engine/src/inkling2/cuda/ink_runner.cuh +++ b/engine/src/inkling2/cuda/ink_runner.cuh @@ -24,6 +24,7 @@ #include "ink_head_screen.cuh" // PERF-STEP: INT8-screened exact top-128 of the head (STRATA_INK_HEAD_SCREEN) #include "strata/artifact/strata_reader.hpp" +#include "strata/core/ink_cold_lane.hpp" #include "strata/core/ink_kv_grow.hpp" #include "strata/core/ink_kv_reserve.hpp" #include "strata/core/vmm.hpp" @@ -6786,9 +6787,10 @@ struct InkCudaRunner { } // Launch probe for the cold-expert lane (STRATA_INK_EXACT unset): the same short run of decode steps is timed with the CPU lane and with - // the card's own copy-in lane (exact), on the placement the loaders made (the tier is paused and learns nothing), and the faster one is kept: - // the CPU lane unless the card's copy is clearly (10%) faster, because a CPU lane is the default of this engine and the probe is noisy. - // Runs once after the runner exists, before any request; a slot is reset when a request takes it, so the probe leaves no state behind. + // the card's own copy-in lane (exact), on the placement the loaders made (the tier is paused and learns nothing). The probe is run three times, alternating the lanes, and the median of + // each lane is compared (strata/core/ink_cold_lane.hpp): the exact (copy-in) lane unless the CPU lane is clearly (10%) faster, a tie going to the exact lane. The old rule (one probe per + // lane, the CPU lane unless the copy-in lane was 10% faster) kept the CPU lane on a quiet RTX 5070 at a ratio of 0.95 and decoded 11 to 64% slower than the copy-in lane; + // STRATA_INK_LANE_PROBE_OLD=1 brings it back. Runs once after the runner exists, before any request; a slot is reset when a request takes it, so the probe leaves no state behind. void pick_cold_lane() { auto& h = m.hybrid; if (h.lane_pref != 0 || !h.active || m.n_host_experts == 0) return; // forced lane, or nothing is cold @@ -6816,10 +6818,17 @@ struct InkCudaRunner { return ms[ms.size() / 2]; }; h.probing = true; - const double t_cpu = run_lane(false); - const double t_card = run_lane(true); + const char* old_env = std::getenv("STRATA_INK_LANE_PROBE_OLD"); + const bool old_rule = old_env && old_env[0] == '1'; + std::vector v_cpu, v_card; + for (int rep = 0; rep < (old_rule ? 1 : 3); ++rep) { + v_cpu.push_back(run_lane(false)); + v_card.push_back(run_lane(true)); + } h.probing = false; - const bool exact = t_card < 0.9 * t_cpu; + const double t_cpu = strata::core::ink_probe_median(v_cpu); + const double t_card = strata::core::ink_probe_median(v_card); + const bool exact = strata::core::ink_cold_lane_exact(t_cpu, t_card, old_rule); h.set_lane(exact); if (ink_rtx5060ti_cfg(m.primary_gpu).compact_hybrid && !exact && t_cpu > 0.0 && t_card > 0.0 && h.dec_share_user < 0.0) { // COMPACT_HYBRID row: the split of the batched cold experts comes from this box's own two lane times (see auto_pcie_share) @@ -6836,8 +6845,8 @@ struct InkCudaRunner { std::fill(h.h_use_batch.begin(), h.h_use_batch.end(), 0.0f); // the eager copy lane counted its routing here if (h.d_trace_n) { INK_CK(cudaMemsetAsync(h.d_trace_n, 0, 4, st)); h.trace_flushed = 0; } INK_CK(cudaStreamSynchronize(st)); - std::fprintf(stderr, "[InkHybrid] cold experts: launch probe (%d decode steps each): CPU lane %.1f ms/token, copy into the card %.1f ms/token -> %s%s\n", - kTimed, t_cpu, t_card, exact ? "copied into the card and computed by its kernels (identical answers wherever an expert lives)" + std::fprintf(stderr, "[InkHybrid] cold experts: launch probe (%d decode steps each, median of %d): CPU lane %.1f ms/token, copy into the card %.1f ms/token -> %s%s\n", + kTimed, (int)v_cpu.size(), t_cpu, t_card, exact ? "copied into the card and computed by its kernels (identical answers wherever an expert lives)" : "computed on the CPU where they are (the answer can differ from an all-resident run in rounding)", " (STRATA_INK_EXACT=1 or 0 forces a lane)"); } diff --git a/engine/tests/core/ink_cold_lane_test.cpp b/engine/tests/core/ink_cold_lane_test.cpp new file mode 100644 index 0000000000..0d679c7728 --- /dev/null +++ b/engine/tests/core/ink_cold_lane_test.cpp @@ -0,0 +1,40 @@ +// ink_cold_lane.hpp: the probe's median and the lane rule (no GPU). +#include "strata/core/ink_cold_lane.hpp" + +#include + +using namespace strata::core; + +static int failed = 0; +#define CHECK(c) do { if (!(c)) { std::fprintf(stderr, "ink_cold_lane_test: FAILED line %d: %s\n", __LINE__, #c); ++failed; } } while (0) + +int main() { + // the median + CHECK(ink_probe_median({}) == 0.0); + CHECK(ink_probe_median({8.6}) == 8.6); + CHECK(ink_probe_median({9.5, 9.0, 12.4}) == 9.5); // an outlier does not move it + CHECK(ink_probe_median({3.0, 1.0}) == 2.0); + CHECK(ink_probe_median({5.0, 1.0, 3.0, 2.0}) == 2.5); + + // the quiet RTX 5070 of the published packages: CPU lane 9.1 ms, copy-in lane 8.6 ms (ratio 0.945): the exact lane now (the old rule kept the CPU lane and lost 11 to 64 %) + CHECK(ink_cold_lane_exact(9.1, 8.6, false)); + CHECK(!ink_cold_lane_exact(9.1, 8.6, true)); + // a busy PC: CPU lane 12.1 ms, copy-in 8.6: the exact lane under both rules + CHECK(ink_cold_lane_exact(12.1, 8.6, false)); + CHECK(ink_cold_lane_exact(12.1, 8.6, true)); + // the CPU lane clearly faster (more than 10 %): the CPU lane under both rules + CHECK(!ink_cold_lane_exact(7.0, 8.6, false)); + CHECK(!ink_cold_lane_exact(7.0, 8.6, true)); + // a tie goes to the exact lane (the old rule kept the CPU lane) + CHECK(ink_cold_lane_exact(8.6, 8.6, false)); + CHECK(!ink_cold_lane_exact(8.6, 8.6, true)); + // the CPU lane 5 % faster: not clearly, so the exact lane (the new rule's hysteresis is on the CPU side) + CHECK(ink_cold_lane_exact(8.17, 8.6, false)); + // exactly 10 % faster is not "clearly": the boundary stays with the exact lane + CHECK(ink_cold_lane_exact(0.9 * 8.6, 8.6, false)); + CHECK(!ink_cold_lane_exact(0.89 * 8.6, 8.6, false)); + + if (failed) { std::fprintf(stderr, "ink_cold_lane_test: %d failure(s)\n", failed); return 1; } + std::printf("ink_cold_lane_test: ok\n"); + return 0; +} diff --git a/terminal/data/settings-register.json b/terminal/data/settings-register.json index 5f51e993cd..40308978ad 100644 --- a/terminal/data/settings-register.json +++ b/terminal/data/settings-register.json @@ -23524,6 +23524,41 @@ "retire_after": "", "retired_in": "" }, + { + "name": "STRATA_INK_LANE_PROBE_OLD", + "backends": [ + "cuda-hip" + ], + "reads": [ + { + "file": "engine/src/inkling2/cuda/ink_runner.cuh", + "line": 6821, + "backend": "cuda-hip", + "fn": "getenv" + } + ], + "parse": "flag", + "default_state": "off", + "code_default": null, + "engine_sets": [], + "engine_defaults": [], + "launch_files": [], + "launch_sets": [], + "launch_reads": [], + "doc_mentions": [ + "docs/INKLING_KV.md:73" + ], + "first_commit": "", + "first_date": "2026-12-02", + "since": null, + "last_commit": "", + "last_date": "2026-12-02", + "status": "rollback", + "owner": "inkling2/cuda", + "note": "Rollback (0.2.25): =1 brings back the old cold-expert lane rule of the launch probe (one probe per lane, the CPU lane unless the copy-in lane is 10% faster). The default now runs the probe three times, alternating the lanes, and keeps the median of each: the exact (copy-in) lane unless the CPU lane is clearly (10%) faster, a tie going to the exact lane. A quiet RTX 5070 12 GB measured CPU 9.0-9.2 ms against copy-in 8.7 and the old rule kept the slower CPU lane (decode 65-68 tok/s and 34 on a 4K document against 73 and 56 with the copy-in lane). STRATA_INK_EXACT=1 or 0 still forces a lane.", + "retire_after": "", + "retired_in": "" + }, { "name": "STRATA_INK_M1_DNPF", "backends": [