okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
6e1085c1d
main
1diff --git a/src/prefill/prefill.cpp b/src/prefill/prefill.cpp 2index23eaf1d ..9491683 100644 3--- a/src/prefill/prefill.cpp 4+++ b/src/prefill/prefill.cpp 5@@ -99,0 +100,3 @@ constexpr int STAGE = 8; // host->device expert staging ring (chunks b 6+// The staged walk's ring when its MMQ groups take the staged experts too: two groups, so a slot is refilled only after 7+// the group that read it was gathered (the walk stages at most STAGE_GRP - MMQ_GROUP - 1 entries ahead) 8+constexpr int STAGE_GRP = 32; 9@@ -253,0 +257,3 @@ inline int ring_slots(size_t T) { 10+// The staging buffers a layout holds: the ring, and at least STAGE_GRP for the staged walk's grouped gather. Counted 11+// and taken through this one place, so the bytes the prompt path borrows cover every buffer it writes. 12+inline int stage_slots(size_t T) { return std::max(ring_slots(T), STAGE_GRP); } 13@@ -1089,0 +1096 @@ bool Prefill::carve(size_t T, void* alloc) { 14+ const int nstage = stage_slots(T); // the staged walk may use STAGE_GRP of them 15@@ -1094,2 +1101,2 @@ bool Prefill::carve(size_t T, void* alloc) { 16- uint8_t* ring_base = o.take<uint8_t>((size_t) m.ring * (size_t) MAXBLOB(), ok); 17- for (int i = 0; ok && i < m.ring; ++i) { 18+ uint8_t* ring_base = o.take<uint8_t>((size_t) nstage * (size_t) MAXBLOB(), ok); 19+ for (int i = 0; ok && i < nstage; ++i) { 20@@ -1101 +1108 @@ bool Prefill::carve(size_t T, void* alloc) { 21- for (int i = 0; i < m.ring; ++i) { 22+ for (int i = 0; i < nstage; ++i) { 23@@ -1607 +1614 @@ uint64_t Prefill::bytes_needed_impl(const core::ModelGeometry& g, const core::Se 24- if (ring_slots(T) > 0) o.take<uint8_t>((size_t) ring_slots(T) * (size_t) MAXBLOB(), ok); // one allocation 25+ if (stage_slots(T) > 0) o.take<uint8_t>((size_t) stage_slots(T) * (size_t) MAXBLOB(), ok); // one allocation 26@@ -1609 +1616 @@ uint64_t Prefill::bytes_needed_impl(const core::ModelGeometry& g, const core::Se 27- for (int i = 0; i < ring_slots(T); ++i) o.take<uint8_t>((size_t) MAXBLOB(), ok); 28+ for (int i = 0; i < stage_slots(T); ++i) o.take<uint8_t>((size_t) MAXBLOB(), ok); 29@@ -1619 +1626 @@ uint64_t Prefill::bytes_needed_no_ring(const core::ModelGeometry& g, const core: 30- return bytes_needed(g, ss, chunk) - (uint64_t) ring_slots((size_t) chunk) * (uint64_t) MAXBLOB(); 31+ return bytes_needed(g, ss, chunk) - (uint64_t) stage_slots((size_t) chunk) * (uint64_t) MAXBLOB(); 32@@ -3127,0 +3135,15 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri 33+ // STRATA_PREFILL_GROUP_GATHER=0: one gather, one wait and one record per expert. 34+ static const bool group_env = [] { 35+ const char* v = std::getenv("STRATA_PREFILL_GROUP_GATHER"); 36+ return v == nullptr || std::atoi(v) != 0; 37+ }(); 38+ const bool group_gather = group_env && stream_all && use_mmq && lay.native && 39+ MMQ_GROUP <= mmq::kGatherGroupMax; 40+ // The staged walk (a short prompt) groups too, resident and staged experts alike: its ring is 41+ // then STAGE_GRP slots, two groups, and it stages no further ahead than the open group allows, 42+ // so a group's staged slots stay unread-over until its flush (one wait on the last copy, one 43+ // event releasing them all, as in the streamed walk). 44+ const bool group_resident = group_env && !stream_all && use_mmq && lay.native && 45+ MMQ_GROUP <= mmq::kGatherGroupMax; 46+ const bool gg_now = group_gather || group_resident; 47+ const int stage_n = group_resident ? STAGE_GRP : STAGE; 48@@ -3156 +3178 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri 49- stage_next = (stage_next + 1) % STAGE; 50+ stage_next = (stage_next + 1) % stage_n; 51@@ -3187,6 +3208,0 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri 52- static const bool group_env = [] { 53- const char* v = std::getenv("STRATA_PREFILL_GROUP_GATHER"); 54- return v == nullptr || std::atoi(v) != 0; 55- }(); 56- const bool group_gather = group_env && stream_all && use_mmq && lay.native && 57- MMQ_GROUP <= mmq::kGatherGroupMax; 58@@ -3228 +3244 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri 59- if (group_gather) { 60+ if (gg_now) { 61@@ -3242 +3258 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri 62- if (slot >= 0 && !group_gather) { cudaEventRecord(m.used[slot], m.cs); m.used_of[slot] = slot; } 63+ if (slot >= 0 && !gg_now) { cudaEventRecord(m.used[slot], m.cs); m.used_of[slot] = slot; } 64@@ -3293 +3309 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri 65- const size_t lookahead = STAGE - 1; 66+ const size_t lookahead = group_resident ? (size_t) (STAGE_GRP - MMQ_GROUP - 1) : STAGE - 1; 67@@ -3307,2 +3323,4 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri 68- pt.mark(kPfWaitCopy, cs); 69- cudaStreamWaitEvent(m.cs, m.copied[stage_of[j]], 0); 70+ if (!group_resident) { // grouped: the flush waits once, on the group's last copy 71+ pt.mark(kPfWaitCopy, cs); 72+ cudaStreamWaitEvent(m.cs, m.copied[stage_of[j]], 0); 73+ }