MCPcopy Create free account
hub / github.com/Niko1221/Strata / gather_batch

Method gather_batch

engine/src/kernels/ngram.cpp:684–841  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

682}
683
684bool PleTable::gather_batch(const uint32_t* rows, size_t n_tokens, float* out, std::string& err) {
685 std::lock_guard<std::recursive_mutex> lk(impl_->mu);
686 if (impl_->pending) { err = "PleTable::gather_batch while a token is in flight"; return false; }
687 const size_t n = n_tokens * (size_t) PLE_N_HEADS;
688 if (impl_->mode == PleIo::Direct) {
689 // Fast path: match each requested token to any in-flight/completed prefetch slot, then collect directly
690 // without issuing a second PleReader ticket or copying through RowCache.
691 if (n_tokens <= Impl::kMaxPrefetch) {
692 size_t matched[Impl::kMaxPrefetch];
693 bool exact_match = true;
694 for (size_t t = 0; t < n_tokens; ++t) {
695 size_t i = 0;
696 while (i < impl_->n_prefetch &&
697 std::memcmp(impl_->prefetch_keys[i], rows + t * PLE_N_HEADS,
698 sizeof(uint32_t) * PLE_N_HEADS) != 0) ++i;
699 if (i == impl_->n_prefetch) { exact_match = false; break; }
700 matched[t] = i; // duplicate requests may share one immutable prefetch slot
701 }
702 if (exact_match) {
703 bool collect_ok = true;
704 for (size_t i = 0; i < impl_->n_prefetch; ++i) {
705 std::string ignored;
706 if (!impl_->reader.collect(impl_->prefetch_tickets[i], collect_ok ? err : ignored))
707 collect_ok = false; // retain the first error, but drain every output buffer
708 }
709 impl_->n_prefetch = 0;
710 if (!collect_ok) return false;
711 for (size_t t = 0; t < n_tokens; ++t) {
712 for (int h = 0; h < PLE_N_HEADS; ++h) {
713 impl_->decode(impl_->prefetch_raw[matched[t]] + (size_t) h * impl_->rb,
714 out + (t * PLE_N_HEADS + (size_t) h) * PLE_HEAD_DIM);
715 if (Impl::check_on())
716 impl_->check_row("gather_batch (prefetched)", rows[t * PLE_N_HEADS + (size_t) h],
717 impl_->prefetch_raw[matched[t]] + (size_t) h * impl_->rb,
718 out + (t * PLE_N_HEADS + (size_t) h) * PLE_HEAD_DIM);
719 }
720 }
721 impl_->bytes_read += (uint64_t) n * impl_->rb;
722 return true;
723 }
724 }
725 wait_prefetches();
726 std::vector<uint8_t> raw(n * impl_->rb);
727 const auto ticket = impl_->reader.issue(rows, n, raw.data());
728 if (!impl_->reader.collect(ticket, err)) return false;
729 for (size_t i = 0; i < n; ++i) {
730 impl_->decode(raw.data() + i * impl_->rb, out + i * PLE_HEAD_DIM);
731 if (Impl::check_on()) impl_->check_row("gather_batch", rows[i], raw.data() + i * impl_->rb, out + i * PLE_HEAD_DIM);
732 }
733 impl_->bytes_read += (uint64_t) n * impl_->rb;
734 return true;
735 }
736 wait_prefetches();
737 // #1984 #1971: a mapped table that is not in the file cache served this loop as serial demand faults, one random
738 // 4 KiB page at a time (16 rows a token, 16 x the chunk's tokens for a prompt), and the first chunk of a prompt has
739 // only layer 0 to hide it behind: a cold 27 GB table at ~0.2-1 ms a fault is minutes with no progress mark, which
740 // is what the stall watchdog stops. Ask the OS for the next window's pages in one go (PrefetchVirtualMemory /
741 // MADV_WILLNEED, a hint that cannot change a value: an unfetched page is faulted by the read) while this window is read.

Callers 15

runMethod · 0.80
stage_batchMethod · 0.80
serviceMethod · 0.80
run_implMethod · 0.80
ple_direct_rows_per_sFunction · 0.80
runMethod · 0.80
stage_batchMethod · 0.80
pl_launchMethod · 0.80
serviceMethod · 0.80
run_ple_checkFunction · 0.80
mainFunction · 0.80
check_gguf_pleFunction · 0.80

Calls 12

progress_beatFunction · 0.85
check_rowMethod · 0.80
fetch_addMethod · 0.80
minFunction · 0.50
workFunction · 0.50
collectMethod · 0.45
decodeMethod · 0.45
issueMethod · 0.45
dataMethod · 0.45
reserveMethod · 0.45
joinMethod · 0.45
loadMethod · 0.45

Tested by 3

format_round_tripFunction · 0.64