Files
Strata/include/strata/core/conversation_buffer.hpp
T
linxuhao f73b3c86a4 Conversation cache: opt-in STRATA_PARK_ASYNC=1 parks the K/V in the background and moves the dropped checkpoints (#3779, #3712)
Problem: parking the outgoing conversation copies its K/V device to host and
deep-copies its checkpoints before the next prompt can start: 213 ms at a 3K
context, 334 ms at 13K on a PCIe Gen2 x4 card (the reporter's CMP 170HX).

Change, opt-in and CUDA only (off on HIP, gfx906 and Metal):
- STRATA_PARK_ASYNC=1: conversation_kv_save copies the device K/V device to
  device into a VRAM stage (only while 1.5 GB stay free beside it; otherwise the
  save copies as before), and a background thread moves the stage into the host
  buffers in 4 MiB pieces through two pinned buffers. conversation_park_join
  waits for it: every reader of a parked image's K/V, the cache before it frees an
  entry, and the image itself (SavedConversation::pending) before its buffers go.
  ConversationBuffer::resize_for_overwrite leaves the added bytes unfilled for
  that copy; the buffer's segments use an allocator that value-initializes only
  when a fill value is given, and resize(n, value) is unchanged for every other
  caller.
- With it, the checkpoints this request drops right after the park (all of them
  when another state comes in, else the ones off this prompt's path) move into
  the parked image instead of being copied, and a background allocation refills
  STRATA_CKPT_POOL for the next save. Only the main session's checkpoints move,
  not an idle batch slot's (the park_source of #2526 serves both).
- conversation_snapshot_save takes dropped checkpoints' storage (the existing
  STRATA_CKPT_POOL) for its copies; with the pool off, nothing is taken.
- Unset: park_drops is null, no storage is taken, the K/V copies are the
  synchronous ones; the added work is the K/V size sum for the stage request and
  a few join calls.

Differs from the PR as submitted: the PR's join in conversation_file.cpp
(put_payload) is not ported; the SAVE/RESTORE handler of generate.cpp joins the
park first (see the previous commit, which carries the PR's 33a8146a fix).
Hand-merged into the park_source / ParkSource refactor of #2526.

Not covered: if the background copy fails after resize_for_overwrite, the
parked image keeps unfilled bytes; the failure is logged. A restore of such an
image would read them. No test of the park path with a failing copy.

Tests: the PARK_ASYNC path has no unit test in this commit (it needs a card with 1.5 GB free for the
stage); the reporter's A/B/A conversation check covers it, see the checklist.

Reporter's numbers (linxuhao, CMP 170HX): park 213 -> 64-75 ms (3K), 334 -> 68 ms
(13K); greedy answers identical on their 11 prompts and the A/B/A conversation
check. Theirs, not measured here.

Original change by linxuhao (PR #3779, commit 3e0c141c).
2026-10-27 00:52:52 +01:00

179 lines
8.3 KiB
C++

#pragma once
#include "strata/core/progress.hpp"
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <initializer_list>
#include <memory>
#include <utility>
#include <vector>
namespace strata::core {
// Snapshot-owned host storage. Appending never relocates existing payloads;
// only the small segment directory can move. No sharing or copy-on-write is
// needed because a parked image and its retained active buffers have one owner.
class ConversationBuffer {
public:
static constexpr size_t segment_bytes = 16 * 1024 * 1024;
ConversationBuffer() = default;
ConversationBuffer(std::initializer_list<uint8_t> values) {
resize(values.size());
visit(0, size(), [&](uint8_t* p, size_t n, size_t at) {
std::memcpy(p, values.begin() + at, n);
return true;
});
}
ConversationBuffer(const ConversationBuffer&) = default;
ConversationBuffer& operator=(const ConversationBuffer&) = default;
ConversationBuffer(ConversationBuffer&& other) noexcept
: segments_(std::move(other.segments_)), size_(std::exchange(other.size_, 0)) {}
ConversationBuffer& operator=(ConversationBuffer&& other) noexcept {
if (this == &other) return *this;
segments_ = std::move(other.segments_);
size_ = std::exchange(other.size_, 0);
return *this;
}
size_t size() const { return size_; }
bool empty() const { return size_ == 0; }
size_t bytes() const {
size_t n = segments_.capacity() * sizeof(Segment);
for (const auto& segment : segments_) n += segment.capacity();
return n;
}
// Includes the old directory while reserve allocates its replacement.
// Saturation makes an overflowing estimate fail admission before resize.
size_t allocation_peak(size_t n) const {
size_t total = bytes();
if (n <= size_) return total;
const size_t spare = segments_.empty() ? 0 : segments_.back().capacity() - segments_.back().size();
const size_t extra = n - size_ - std::min(n - size_, spare);
const size_t full = extra / segment_bytes, tail = extra % segment_bytes;
if (!add(total, full * segment_bytes)) return SIZE_MAX;
if (tail && !add(total, segment_capacity(tail, full ? segment_bytes :
segments_.empty() ? 0 : segments_.back().capacity(), size_ != 0, size_))) return SIZE_MAX;
size_t count = segments_.size();
if (!add(count, full + (tail != 0))) return SIZE_MAX;
if (count > segments_.capacity() &&
(count > SIZE_MAX / sizeof(Segment) || !add(total, count * sizeof(Segment)))) return SIZE_MAX;
return total;
}
void resize(size_t n, uint8_t value = 0) { resize_impl(n, &value); }
void resize_impl(size_t n, const uint8_t* value) {
if (n > size_) {
const size_t held = size_;
const bool growing = held != 0;
const size_t spare = segments_.empty() ? 0 : segments_.back().capacity() - segments_.back().size();
const size_t extend = std::min(n - size_, spare);
const size_t extra = n - size_ - extend;
segments_.reserve(segments_.size() + extra / segment_bytes + (extra % segment_bytes != 0));
if (extend) {
if (value) segments_.back().resize(segments_.back().size() + extend, *value);
else segments_.back().resize(segments_.back().size() + extend);
size_ += extend;
}
while (size_ < n) {
const size_t count = std::min(segment_bytes, n - size_);
const size_t previous = segments_.empty() ? 0 : segments_.back().capacity();
Segment segment;
segment.reserve(segment_capacity(count, previous, growing, held));
if (value) segment.resize(count, *value);
else segment.resize(count);
segments_.push_back(std::move(segment));
size_ += count;
progress_beat(); // #2049: zero-filling 16 MiB of fresh pages is slow when the host is paging
}
} else {
while (!segments_.empty() && size_ - segments_.back().size() >= n) {
size_ -= segments_.back().size();
segments_.pop_back();
}
if (size_ > n) segments_.back().resize(segments_.back().size() - (size_ - n));
size_ = n;
}
}
void pop_back() { resize(size_ - 1); }
// resize() without the zero fill of what it adds (the fresh pages are not touched here): only for a caller that
// writes every added byte before anything reads it (STRATA_PARK_ASYNC's background copy)
void resize_for_overwrite(size_t n) { resize_impl(n, nullptr); }
// fn(pointer, count, absolute_offset); false aborts a transfer immediately.
template<class Fn> bool visit(size_t offset, size_t count, Fn fn) {
return visit_impl(*this, offset, count, fn);
}
template<class Fn> bool visit(size_t offset, size_t count, Fn fn) const {
return visit_impl(*this, offset, count, fn);
}
bool read(void* target, size_t offset, size_t count) const {
return visit(offset, count, [&](const uint8_t* p, size_t n, size_t at) {
std::memcpy(static_cast<uint8_t*>(target) + at - offset, p, n);
return true;
});
}
bool operator==(const ConversationBuffer& other) const {
if (size_ != other.size_) return false;
size_t a = 0, b = 0, x = 0, y = 0;
while (a < segments_.size() && b < other.segments_.size()) {
const auto& left = segments_[a];
const auto& right = other.segments_[b];
const size_t n = std::min(left.size() - x, right.size() - y);
if (std::memcmp(left.data() + x, right.data() + y, n)) return false;
x += n; y += n;
if (x == left.size()) { ++a; x = 0; }
if (y == right.size()) { ++b; y = 0; }
}
return true;
}
private:
// value-initializes only when asked (resize's explicit fill value): resize_for_overwrite leaves the bytes alone
template<class T> struct DefaultInit : std::allocator<T> {
template<class U> struct rebind { using other = DefaultInit<U>; };
DefaultInit() = default;
template<class U> DefaultInit(const DefaultInit<U>&) noexcept {}
template<class U> void construct(U* p) { ::new ((void*) p) U; }
template<class U, class... A> void construct(U* p, A&&... a) { ::new ((void*) p) U(std::forward<A>(a)...); }
};
using Segment = std::vector<uint8_t, DefaultInit<uint8_t>>;
// A fresh capture allocates exactly its payload. Later appends reserve
// geometrically growing segments, bounded by 16 MiB. Small chat turns then
// extend the final allocation instead of adding one transfer per turn.
// The reserve is also bounded by an eighth of what the buffer held before
// the append (at least 64 KiB): a parked conversation carries it in every
// K/V buffer, and at up to 16 MiB each it came to 0.4-0.5 GB of a ~1 GB
// conversation (Qwen3.8-Flash-Next), counted against the cache's budget.
static size_t segment_capacity(size_t count, size_t previous, bool growing, size_t held) {
if (!growing) return count;
const size_t geometric = std::min(segment_bytes, std::max<size_t>(65536, std::min(previous, segment_bytes/2)*2));
return std::max(count, std::min(geometric, std::max<size_t>(65536, held / 8)));
}
static bool add(size_t& n, size_t extra) {
if (extra > SIZE_MAX - n) return false;
n += extra;
return true;
}
template<class Self, class Fn> static bool visit_impl(Self& self, size_t offset, size_t count, Fn fn) {
if (offset > self.size_ || count > self.size_ - offset) return false;
size_t begin = 0;
for (auto& segment : self.segments_) {
if (!count) break;
const size_t end = begin + segment.size();
if (offset < end) {
const size_t n = std::min(count, end - offset);
if (!fn(segment.data() + offset - begin, n, offset)) return false;
offset += n; count -= n;
}
begin = end;
}
return count == 0;
}
std::vector<Segment> segments_;
size_t size_ = 0;
};
} // namespace strata::core