okami.mom/strata

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

Niko1221Merge main 6c642df75 into f-own-int (round 16: network waits, robustness, Windows kill-on-close job start, GitHub and transfer fixes)cd9745050

main
5.6 KiB106 linesraw
1// #5722: a layer split prices each participant's prompt-path loan (Prefill::bytes_needed) on THAT participant's device.
2// bytes_needed answers for the current device (prefill.cpp fused_ring: the fused layout and its smaller MoE buffers are
3// decided per card), so a later stage priced while CUDA0 was current got CUDA0's figure: on an RTX 5080 (fused) + RTX
4// 4090 (not) split the 4090's loan was 2882 MiB for a chunk whose buffers there take 3014 MiB, and its prompt path fell
5// back to 1024-token chunks.  No GPU: the devices are faked (a "current device" and a price per device).  No real
6// mixed-card run backs this test.
7#include "strata/program/split_ring.hpp"
8
9#include <cstdint>
10#include <cstdio>
11#include <vector>
12
13using strata::program::split_lend_slots;
14using strata::program::split_part_figure;
15
16namespace {
17int failed = 0;
18#define CHECK(x) do { if (!(x)) { std::fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #x); ++failed; } } while (0)
19
20constexpr uint64_t MiB = 1ull << 20;
21int current = 0;                         // the faked current device
22
23struct FakeOnDevice {                    // strata::core::OnDevice's contract: -1 leaves the device alone
24    int previous = -1;
25    explicit FakeOnDevice(int dev) { if (dev >= 0 && dev != current) { previous = current; current = dev; } }
26    ~FakeOnDevice() { if (previous >= 0) current = previous; }
27    FakeOnDevice(const FakeOnDevice&) = delete;
28    FakeOnDevice& operator=(const FakeOnDevice&) = delete;
29};
30auto on_device = [](int dev) { return FakeOnDevice(dev); };
31
32// one card's buffers per chunk token, in bytes: a "fused" card needs fewer (its smaller MoE buffers)
33struct Card { const char* name; uint64_t per_token; int64_t slots; };
34
35// bytes_needed as the engine computes it: for the CURRENT device
36uint64_t price_now(const std::vector<Card>& cards, int64_t chunk) {
37    return (uint64_t) chunk * cards[(size_t) current].per_token;
38}
39
40struct Part { int dev; int64_t slots; };   // dev -1: CUDA0's own part
41
42// the lend of one participant: the slots of its cache that cover the price of `chunk` (uniform max_blob slots)
43int64_t lend(const std::vector<Card>& cards, const Part& p, int64_t chunk, uint64_t blob) {
44    return split_lend_slots(p.slots, p.slots * (int64_t) blob, nullptr, blob,
45                            split_part_figure(on_device, p.dev, [&] { return price_now(cards, chunk); }));
46}
47
48// what the participant's own init carves, on its own device
49uint64_t carved(const std::vector<Card>& cards, const Part& p, int64_t chunk) {
50    return (uint64_t) chunk * cards[(size_t) (p.dev < 0 ? 0 : p.dev)].per_token;
51}
52
53std::vector<Part> parts_of(const std::vector<Card>& cards) {
54    std::vector<Part> parts{{-1, cards[0].slots}};
55    for (size_t d = 1; d < cards.size(); ++d) parts.push_back({(int) d, cards[d].slots});
56    return parts;
57}
58}  // namespace
59
60int main() {
61    const uint64_t blob = 4 * MiB;
62    const int64_t chunks[] = {256, 512, 1024, 2048, 3072, 4096, 6144, 8192, 16384, 32768};
63
64    // 1. the reporter's pair: CUDA0 an RTX 5080 (fused: 2880 MiB at 8192), CUDA1 an RTX 4090 (3014 MiB at 8192)
65    {
66        const std::vector<Card> cards{{"RTX 5080", 2880 * MiB / 8192, 4000}, {"RTX 4090", 3014 * MiB / 8192, 4000}};
67        const auto parts = parts_of(cards);
68        current = 0;
69        const int64_t k1 = lend(cards, parts[1], 8192, blob);
70        const uint64_t lent = (uint64_t) k1 * blob;
71        std::printf("split_part_price_test: CUDA1 (%s) chunk 8192: lent %llu MiB, its buffers there %llu MiB\n",
72                    cards[1].name, (unsigned long long) (lent / MiB), (unsigned long long) (carved(cards, parts[1], 8192) / MiB));
73        // the stage's init then lays its buffers out in that loan, stepping its chunk down until they fit ("trying a
74        // 6144-token chunk ..." in the report)
75        int64_t reads = 0;
76        for (const int64_t c : {8192, 6144, 4096, 3072, 2048, 1024})
77            if (carved(cards, parts[1], c) <= lent) { reads = c; break; }
78        std::printf("split_part_price_test: CUDA1 reads its prompt in %lld-token chunks (planned 8192)\n", (long long) reads);
79        CHECK(reads == 8192);
80        CHECK(lent >= carved(cards, parts[1], 8192));                 // the loan covers what the stage's init carves
81        CHECK(current == 0);                                          // the device is put back
82        const int64_t k0 = lend(cards, parts[0], 8192, blob);
83        CHECK((uint64_t) k0 * blob >= carved(cards, parts[0], 8192)); // CUDA0's own part unchanged
84        CHECK((uint64_t) (k0 - 1) * blob < carved(cards, parts[0], 8192));
85    }
86
87    // 2. like cards: the device switch changes no number.  Two cards (z440's 2x A4000 class) and four cards (the 4x
88    //    R9700 class), every chunk of the scan's grid, priced with the switch against priced on the current device
89    //    alone (the old rule).  Equal per-device prices are what like cards give: fused_ring answers the same on each.
90    for (int n : {2, 4}) {
91        const std::vector<Card> cards((size_t) n, Card{"like card", 2950 * MiB / 8192, 3500});
92        const auto parts = parts_of(cards);
93        for (const int64_t c : chunks)
94            for (const Part& p : parts) {
95                current = 0;
96                const int64_t with_switch = lend(cards, p, c, blob);
97                const int64_t old_rule = split_lend_slots(p.slots, p.slots * (int64_t) blob, nullptr, blob, price_now(cards, c));
98                CHECK(with_switch == old_rule);
99                CHECK(current == 0);
100            }
101    }
102
103    if (failed) { std::fprintf(stderr, "split_part_price_test: %d failed\n", failed); return 1; }
104    std::printf("split_part_price_test: ok\n");
105    return 0;
106}