okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-q36-fit: strata-q36 context fit check counts the staging pair in a build without MMQ727890fde

main
6.9 KiB100 linesraw
1// strata-q36's one-session context check (GitHub #5715, include/strata/qwen35/context_fit.hpp): the reporter's RTX 5070 12 GB starts from its log,
2// a card where every expert fits, and a card too small for any sentence.  CPU only.
3#include "strata/qwen35/context_fit.hpp"
4
5#include <cstdio>
6#include <string>
7
8namespace {
9int fails = 0;
10void check(bool ok, const char* what) {
11    if (!ok) {
12        std::printf("FAIL: %s\n", what);
13        ++fails;
14    }
15}
16constexpr size_t MiB = (size_t) 1 << 20;
17constexpr double GiB = (double) (1ull << 30);
18constexpr size_t KV = 11 * 2 * 256 * 2 * 4, SHARED = 2 * 256 * 2 * 2;
19constexpr size_t STATE = 30 * 32 * 128 * 128 * 4;     // the GDN layers' state (~0.06 GiB)
20
21// UD-Q4_K_XL on the reporter's RTX 5070: at 64K the log says 0.45 GiB free after the session and 1256 MiB needed by the hybrid buffers.
22// fr0 (before the session, after the fp16 copy at this context) = 0.45 GiB + the session; the fp16 copy grows with the context.
23strata::qwen35::OneSessionInput rtx5070(size_t ctx) {
24    const size_t fr0_64k = (size_t) (0.45 * GiB) + KV * 65536 + STATE;
25    const size_t fr0 = fr0_64k + SHARED * 65536 - SHARED * ctx;
26    const size_t outputs = 2048 * 8 * 2048 * 4 + 2048 * 8 * 4;
27    return {ctx, KV, SHARED, STATE, fr0, (size_t) (19.0 * GiB), 0, (size_t) (48 << 20) * 6, 1256 * MiB - outputs, outputs};
28}
29
30// UD-Q4_K_XL on an RTX 3060 Ti 8 GB, prompt chunk 512: the build with MMQ said 1.50 GiB needed, 0.41 GiB free, 7168 tokens fit at 32K; the build
31// without MMQ went on and ended in place_experts with 1159 MiB needed (the staging pair, ~1127 MiB, + the CPU part's outputs) and 114 MiB free.
32// staging_counted: whether the engine passes the staging pair (one_session_staging_counted), as the engine does at q36_engine.cu's fit check.
33strata::qwen35::OneSessionInput rtx3060ti(size_t ctx, bool staging_counted) {
34    const size_t outputs = 512 * 8 * 2048 * 4 + 512 * 8 * 4;
35    const size_t staging = 1127 * MiB;
36    const size_t fr0_32k = (size_t) (0.41 * GiB) + staging + outputs - SHARED * 32768;
37    const size_t fr0 = fr0_32k + SHARED * 32768 - SHARED * ctx;
38    return {ctx, KV, SHARED, STATE, fr0, (size_t) (19.0 * GiB), 0, (size_t) (48 << 20) * 6, staging_counted ? staging : 0, outputs};
39}
40}  // namespace
41
42int main() {
43    using strata::qwen35::one_session_fit;
44    using strata::qwen35::one_session_fit_line;
45    // 32K ran on that card: no refusal
46    check(!one_session_fit(rtx5070(32768)).refuse, "32K on the 12 GB card is not refused");
47    // 64K ended with the hybrid buffers' text, 128K with the bare out-of-memory line: now the owner's sentence, with the size that fits
48    for (size_t ctx : {(size_t) 65536, (size_t) 131072}) {
49        const auto f = one_session_fit(rtx5070(ctx));
50        check(f.refuse, "64K / 128K on the 12 GB card are refused in words");
51        check(f.fit >= 32768 && f.fit < 49152 && f.fit % 1024 == 0, "the size said to fit is between 32K and 48K, in steps of 1024");
52        check(f.x_gib > f.y_gib, "the memory it needs is more than the memory said to be free");
53        std::printf("%s\n", one_session_fit_line((long long) ctx, f).c_str());
54    }
55    const auto f64 = one_session_fit(rtx5070(65536));
56    check(one_session_fit_line(65536, f64) ==
57              "a context of 65536 tokens does not fit on this graphics card: its conversation memory needs 2.93 GiB and only 2.16 GiB is free "
58              "after the model. Start it again with a shorter context (47104 tokens fit here).",
59          "the 64K line, word for word");
60    check(!one_session_fit(rtx5070((size_t) f64.fit)).refuse, "the size it names is not refused");
61    // a card where every main layer's experts still fit after the session: no hybrid minimum asked, only the session itself
62    {
63        strata::qwen35::OneSessionInput in{131072, KV, SHARED, STATE, (size_t) (24.0 * GiB), (size_t) (18.0 * GiB), 0, 0, 1100 * MiB, 128 * MiB};
64        check(!one_session_fit(in).refuse, "a 32 GB card at 128K with every expert fitting is not refused");
65        in.fr0 = (size_t) (5.0 * GiB);   // the session alone (5.4 GiB) does not fit
66        check(one_session_fit(in).refuse, "a session larger than the free VRAM is refused");
67    }
68    // a card where not even 1024 tokens fit: nothing said here, the engine goes on as before
69    check(!one_session_fit(strata::qwen35::OneSessionInput{32768, KV, SHARED, STATE, 1100 * MiB, (size_t) (19.0 * GiB), 0, 0, 1000 * MiB, 128 * MiB}).refuse,
70          "no sentence when nothing fits");
71    // the staging pair in the floor: the build with MMQ keeps its rule (MMQ or our int8 kernels on a layer), the build without MMQ counts it on the
72    // streamed prompt path too (the fused kernels take the layers there: use_mmq false, n_i8 0), as place_experts allocates it
73    {
74        using strata::qwen35::one_session_staging_counted;
75        check(one_session_staging_counted(true, false, true, true), "MMQ build, a layer on MMQ: counted (as before)");
76        check(one_session_staging_counted(false, true, true, true), "MMQ build, a layer on our int8 kernels: counted (as before)");
77        check(!one_session_staging_counted(false, false, true, true), "MMQ build, every layer fused: not counted (as before)");
78        check(!one_session_staging_counted(false, false, false, true), "MMQ build, no streamed prompt path: not counted (as before)");
79        check(one_session_staging_counted(false, false, true, false), "no-MMQ build, every layer fused: counted (new)");
80        check(one_session_staging_counted(false, true, true, false), "no-MMQ build, a layer on our int8 kernels: counted (as before)");
81        check(!one_session_staging_counted(false, false, false, false), "no-MMQ build, no streamed prompt path: not counted (place_experts has no staging)");
82    }
83    // the RTX 3060 Ti at 32K: without the staging pair (the no-MMQ build before) no sentence; with it the MMQ build's line, word for word
84    {
85        using strata::qwen35::one_session_staging_counted;
86        check(!one_session_fit(rtx3060ti(32768, false)).refuse, "3060 Ti 32K, staging not counted: not refused (the old no-MMQ gap)");
87        const auto f = one_session_fit(rtx3060ti(32768, one_session_staging_counted(false, false, true, false)));
88        check(f.refuse, "3060 Ti 32K, no-MMQ build, fused layers: refused in words");
89        check(one_session_fit_line(32768, f) ==
90                  "a context of 32768 tokens does not fit on this graphics card: its conversation memory needs 1.50 GiB and only 0.41 GiB is free "
91                  "after the model. Start it again with a shorter context (7168 tokens fit here).",
92              "the 3060 Ti 32K line, word for word");
93        std::printf("%s\n", one_session_fit_line(32768, f).c_str());
94        check(!one_session_fit(rtx3060ti((size_t) f.fit, true)).refuse, "3060 Ti: the size it names is not refused");
95        check(!one_session_fit(rtx3060ti(7168, true)).refuse, "3060 Ti 7168: not refused (starts as before)");
96    }
97    if (fails) return 1;
98    std::printf("q36_context_fit_test: ok\n");
99    return 0;
100}