okami.mom/strata

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

Niko1221Merge f-0216-core into f-0216-allbd0e878f4

main
3.6 KiB85 linesraw
1// STRATA_PREFILL_TIMING mark coalescing (PR #4006): repeated marks in the running phase add nothing to the totals.
2// No GPU: the timeline is simulated, one fake clock tick per launch.
3#include <cstdio>
4#include <vector>
5
6#include "strata/prefill/timer_marks.hpp"
7
8using strata::prefill::timer_mark_repeats;
9
10static int failed = 0;
11#define CHECK(x) do { if (!(x)) { std::fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #x); ++failed; } } while (0)
12
13enum { kIdle, kDequant, kGemm, kWaitCopy, kPhases };
14
15struct Mark { int phase; double t; };
16
17// PfTimer::fold(): the gap between mark i and mark i+1 is charged to the phase of mark i.
18static std::vector<double> fold_totals(const std::vector<Mark>& marks) {
19    std::vector<double> ms(kPhases, 0.0);
20    for (size_t i = 0; i + 1 < marks.size(); ++i) ms[marks[i].phase] += marks[i + 1].t - marks[i].t;
21    return ms;
22}
23
24// PfTimer::mark(): record unless the newest recorded mark is in the same phase.
25static std::vector<Mark> record(const std::vector<Mark>& calls, size_t* skipped) {
26    std::vector<Mark> out;
27    std::vector<int> ph;
28    *skipped = 0;
29    for (const Mark& m : calls) {
30        if (timer_mark_repeats(ph.data(), ph.size(), m.phase)) { ++*skipped; continue; }
31        out.push_back(m);
32        ph.push_back(m.phase);
33    }
34    return out;
35}
36
37int main() {
38    // the predicate
39    const int ph[] = {kGemm, kDequant};
40    CHECK(!timer_mark_repeats(ph, 0, kDequant));        // nothing recorded yet: always record
41    CHECK(timer_mark_repeats(ph, 2, kDequant));         // the newest mark is already dequant
42    CHECK(!timer_mark_repeats(ph, 2, kGemm));           // an older mark's phase does not count, only the newest
43    CHECK(!timer_mark_repeats(ph, 1, kDequant));
44    CHECK(timer_mark_repeats(ph, 1, kGemm));
45
46    // a compute() like timeline: gemm, then one dequant mark per expert (16 of them), then a copy wait, then dequant again
47    std::vector<Mark> calls;
48    double t = 0;
49    calls.push_back({kGemm, t});
50    t += 5;
51    for (int e = 0; e < 16; ++e) { calls.push_back({kDequant, t}); t += 0.5 + e * 0.01; }
52    calls.push_back({kWaitCopy, t});
53    t += 3;
54    for (int e = 0; e < 4; ++e) { calls.push_back({kDequant, t}); t += 1.0; }
55    calls.push_back({kIdle, t});                         // the run's closing mark
56
57    size_t skipped = 0;
58    const std::vector<Mark> kept = record(calls, &skipped);
59    const std::vector<double> full = fold_totals(calls), coal = fold_totals(kept);
60    CHECK(skipped == 15 + 3);                            // 18 records saved of 23
61    CHECK(kept.size() == 5);                             // gemm, dequant, wait copy, dequant, idle
62    for (int p = 0; p < kPhases; ++p) CHECK(full[p] - coal[p] < 1e-9 && coal[p] - full[p] < 1e-9);   // the totals are the same
63
64    // alternating phases: nothing is skipped
65    std::vector<Mark> alt;
66    for (int i = 0; i < 10; ++i) alt.push_back({i % 2 ? kGemm : kDequant, (double) i});
67    record(alt, &skipped);
68    CHECK(skipped == 0);
69
70    // the same phase for the whole run: one record plus the closing mark
71    std::vector<Mark> same;
72    for (int i = 0; i < 100; ++i) same.push_back({kDequant, (double) i});
73    same.push_back({kIdle, 100.0});
74    const std::vector<Mark> k2 = record(same, &skipped);
75    CHECK(skipped == 99 && k2.size() == 2);
76    CHECK(fold_totals(k2)[kDequant] == fold_totals(same)[kDequant]);
77
78    // after a fold the timer keeps its last mark (used == 1): a repeat of that phase is still skipped
79    const int kept_ph[] = {kWaitCopy};
80    CHECK(timer_mark_repeats(kept_ph, 1, kWaitCopy));
81
82    if (failed) { std::fprintf(stderr, "%d check(s) failed\n", failed); return 1; }
83    std::puts("timer_marks_test: ok");
84    return 0;
85}