| 880 | } |
| 881 | |
| 882 | void PleTable::gather(const uint32_t* rows16, float* out2560) const { |
| 883 | std::lock_guard<std::recursive_mutex> lk(impl_->mu); // issue + collect as one |
| 884 | if (impl_->mode == PleIo::Direct) { |
| 885 | // `gather` stays const for its existing callers; the reader's state is the table's I/O state. |
| 886 | PleTable* self = const_cast<PleTable*>(this); |
| 887 | std::string err; |
| 888 | if (!self->issue(rows16) || !self->collect(out2560, err)) { |
| 889 | std::fprintf(stderr, "PleTable::gather: %s\n", err.empty() ? "a token is already in flight" : err.c_str()); |
| 890 | std::memset(out2560, 0, (size_t) NG_N_EMBD * sizeof(float)); |
| 891 | } |
| 892 | return; |
| 893 | } |
| 894 | // ================================ SIXTEEN SERIAL PAGE FAULTS, MEASURED ================================ |
| 895 | // |
| 896 | // **THIS COST 2.10-2.61 ms PER TOKEN AND HAD NEVER BEEN IN THE PLAN'S BUDGET AT ALL.** The round-309 |
| 897 | // `token host phases` line put it second behind the layer loop among avoidable terms, and the arithmetic |
| 898 | // says why: the table is 320,001,536 rows of `PLE_ROW_BYTES` = 90 B in a 26.8 GB mapping, so the sixteen |
| 899 | // rows a token needs are 1,440 B - **0.5 MB/s**. That is not bandwidth, it is latency: sixteen reads into |
| 900 | // sixteen different 4 KB pages scattered across 26.8 GB, taken ONE AT A TIME, and on this machine the PLE |
| 901 | // shard is 26.8 GB against 63 GB of RAM that the 31.6 GB expert arena is also competing for, so they are |
| 902 | // not in the OS cache. Sixteen serial NVMe reads at ~150 us is 2.4 ms, which is the measurement. |
| 903 | // |
| 904 | // `PrefetchVirtualMemory` issues all sixteen in ONE call and lets them complete in parallel. It is a hint |
| 905 | // and cannot change the answer - a range it does not fetch is simply faulted in by the read that follows - |
| 906 | // so the only risk is that it does nothing. |
| 907 | #if defined(_WIN32) |
| 908 | if (impl_->data != nullptr && g_ple_prefetch) { |
| 909 | WIN32_MEMORY_RANGE_ENTRY ranges[PLE_N_HEADS]; |
| 910 | ULONG_PTR n = 0; |
| 911 | for (int h = 0; h < PLE_N_HEADS; ++h) { |
| 912 | // Out-of-range rows are handled by `read_row` as zeros and have no address to prefetch. |
| 913 | if (rows16[h] >= impl_->n_rows) continue; |
| 914 | ranges[n].VirtualAddress = (PVOID) (impl_->data + (size_t) rows16[h] * impl_->rb); |
| 915 | ranges[n].NumberOfBytes = impl_->rb; |
| 916 | ++n; |
| 917 | } |
| 918 | if (n > 0) (void) PrefetchVirtualMemory(GetCurrentProcess(), n, ranges, 0); |
| 919 | } |
| 920 | #endif |
| 921 | // HEAD-SLOWEST, which is what `ggml_get_rows` does and what the source's own comment says: head h's 160 |
| 922 | // values occupy [h*160, (h+1)*160). A head-fastest layout would put element (d, h) at d*16 + h and needs |
| 923 | // a real transpose - a reshape of the same flat buffer compares equal and would make the check vacuous. |
| 924 | for (int h = 0; h < PLE_N_HEADS; ++h) read_row(rows16[h], out2560 + (size_t) h * PLE_HEAD_DIM); |
| 925 | } |
| 926 | |
| 927 | } // namespace strata::kernels |