Source: https://github.com/Niko1221/Strata, commit 6f32ec070f23ced9f50e704d854d775da52591ab (engine 0.1.39, MIT). Every file is as upstream published it; later commits show exactly what this fork changes. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
97 lines
7.0 KiB
C++
97 lines
7.0 KiB
C++
#pragma once
|
|
|
|
#include "strata/core/conversation_cache.hpp"
|
|
#include "strata/core/layer.hpp"
|
|
#include "strata/core/session.hpp"
|
|
|
|
#include <string>
|
|
|
|
namespace strata::core {
|
|
|
|
// Caller synchronizes the device before saving, and after restoring all layers.
|
|
// include_index is false for the draft layer (its attention has no indexer).
|
|
size_t conversation_kv_bytes(const QsaState& state, const ModelGeometry& g, int64_t upto, bool include_index);
|
|
// A nonzero unchanged_tokens is valid only for storage retained from an image
|
|
// actually restored into this session, bounded by every subsequent rewrite.
|
|
// Equal token IDs alone do not establish that its K/V bytes are unchanged.
|
|
bool conversation_kv_save(ConversationKv& image, const QsaState& state, const ModelGeometry& g,
|
|
int64_t upto, bool include_index, std::string& error,
|
|
int64_t unchanged_tokens = 0, size_t* reused_bytes = nullptr);
|
|
bool conversation_kv_capture_bytes(const ConversationKv& image, const QsaState& state, const ModelGeometry& g,
|
|
int64_t upto, bool include_index, size_t& bytes, std::string& error);
|
|
// No CUDA calls or destination writes. Used for whole-session prevalidation.
|
|
bool conversation_kv_validate(const ConversationKv& image, const QsaState& state, const ModelGeometry& g,
|
|
int64_t upto, bool include_index, std::string& error);
|
|
bool conversation_kv_restore(const ConversationKv& image, const QsaState& state, const ModelGeometry& g,
|
|
int64_t upto, bool include_index, std::string& error);
|
|
// Diagnostic read-back after a synchronized restore. Uses 64 KiB of stack
|
|
// workspace, compares authoritative bytes and resident draft-ring pages, and
|
|
// fingerprints the authoritative payload only. Never changes model state.
|
|
bool conversation_kv_verify(const ConversationKv& image, const QsaState& state, const ModelGeometry& g,
|
|
int64_t upto, bool include_index, uint64_t& fingerprint, std::string& error);
|
|
|
|
struct ConversationStateSizes {
|
|
size_t gdn = 0, ple = 0, tail = 0, dead = 0, block_pos = 0;
|
|
};
|
|
/// Whole-model sizes: `gdn` covers every GDN layer, the indexer sizes are per QSA layer.
|
|
bool conversation_state_sizes(const ModelGeometry& g, ConversationStateSizes& sizes, std::string& error);
|
|
/// The same for one session's layer carve (#216): `gdn` covers its `gdn_alloc` rows; the per-QSA-layer sizes
|
|
/// apply to each owned state [qsa_ord0, qsa_ord0 + qsa_alloc). Rejects an inconsistent carve. Checkpoints,
|
|
/// snapshots and their validation all use this: a session saves and restores only the state it owns.
|
|
bool conversation_session_sizes(const ModelGeometry& g, const SessionState& session, ConversationStateSizes& sizes,
|
|
std::string& error);
|
|
bool conversation_checkpoint_validate(const ConversationCheckpoint& checkpoint, const SessionState& session,
|
|
const ModelGeometry& g, std::string& error);
|
|
bool conversation_checkpoint_save(ConversationCheckpoint& checkpoint, const SessionState& session,
|
|
const ModelGeometry& g, std::string& error);
|
|
bool conversation_checkpoint_restore(const ConversationCheckpoint& checkpoint, SessionState& session,
|
|
const ModelGeometry& g, std::string& error);
|
|
|
|
struct ConversationView {
|
|
const std::vector<int32_t>& ids;
|
|
const std::vector<ConversationImageKey>& images;
|
|
const std::vector<ConversationCheckpoint>& checkpoints;
|
|
bool cvec;
|
|
};
|
|
bool conversation_snapshot_bytes(const ConversationView& view, const SessionState& session,
|
|
const ModelGeometry& g, const QsaState& draft, size_t& bytes, std::string& error);
|
|
bool conversation_snapshot_capture_bytes(const ConversationKvReuse& reuse, const ConversationView& view,
|
|
const SessionState& session, const ModelGeometry& g,
|
|
const QsaState& draft, size_t& bytes, std::string& error);
|
|
// The capture estimate includes retained capacity and transient segment directories;
|
|
// only estimate - reuse.bytes() requires additional physical RAM. Capture consumes
|
|
// the uniquely owned reusable buffers, including on failure.
|
|
// Caller admits the estimate before invoking capture. Allocation failures propagate
|
|
// to the RAM policy; the active session is never modified by capture.
|
|
bool conversation_snapshot_save(SavedConversation& image, const ConversationView& view,
|
|
const SessionState& session, const ModelGeometry& g,
|
|
const QsaState& draft, std::string& error,
|
|
ConversationKvReuse reuse = {}, size_t* reused_bytes = nullptr);
|
|
bool conversation_snapshot_validate(const SavedConversation& image, const SessionState& session,
|
|
const ModelGeometry& g, const QsaState& draft, std::string& error);
|
|
enum class ConversationRestore { restored, invalid, transfer_failed };
|
|
// Invalid images are rejected before any CUDA call/write. Transfer failure may
|
|
// leave partial state: caller MUST NOT continue inference from that session.
|
|
ConversationRestore conversation_snapshot_restore(const SavedConversation& image, SessionState& session,
|
|
const ModelGeometry& g, const QsaState& draft,
|
|
std::string& error);
|
|
|
|
// The same with `draft == nullptr`: an image WITHOUT the draft layer's K/V (kv holds the session's own QSA layers
|
|
// only). A layer split's later stages park this way; the draft ring is saved once, with the first stage's image.
|
|
// An image is validated and restored with the same kind of call it was saved with (a K/V layer count mismatch is
|
|
// rejected as invalid).
|
|
bool conversation_snapshot_bytes(const ConversationView& view, const SessionState& session, const ModelGeometry& g,
|
|
const QsaState* draft, size_t& bytes, std::string& error);
|
|
bool conversation_snapshot_capture_bytes(const ConversationKvReuse& reuse, const ConversationView& view,
|
|
const SessionState& session, const ModelGeometry& g, const QsaState* draft,
|
|
size_t& bytes, std::string& error);
|
|
bool conversation_snapshot_save(SavedConversation& image, const ConversationView& view, const SessionState& session,
|
|
const ModelGeometry& g, const QsaState* draft, std::string& error,
|
|
ConversationKvReuse reuse = {}, size_t* reused_bytes = nullptr);
|
|
bool conversation_snapshot_validate(const SavedConversation& image, const SessionState& session,
|
|
const ModelGeometry& g, const QsaState* draft, std::string& error);
|
|
ConversationRestore conversation_snapshot_restore(const SavedConversation& image, SessionState& session,
|
|
const ModelGeometry& g, const QsaState* draft, std::string& error);
|
|
|
|
} // namespace strata::core
|