Conversation snapshots and checkpoints follow the session carve (#216): a session saves and restores only the layers it owns
conversation_session_sizes() gives one session's running-state sizes: the GDN bytes of its gdn_alloc rows, the indexer tail/dead/block_pos per owned QSA state; an inconsistent carve is rejected. Checkpoint save/validate/restore, the snapshot size estimate, capture, validation and restore iterate the owned QSA ordinals [qsa_ord0, qsa_ord0 + qsa_alloc) instead of every model layer (the spare pooled row is written back per owned layer only), and a parked snapshot records the carve it was taken from (layer_lo, layer_hi): a restore into another range is refused before any write. One GPU (the default full range: gdn_alloc = n_gdn_layers, qsa_ord0 = 0, qsa_alloc = n_qsa_layers) saves and restores exactly the bytes it did before, in the same order. generate.cpp's conv_state_sizes uses the same function. The tests' hand-built sessions carry the full-range carve, and the host-only validation test checks the carved sizes and the rejections.
This commit is contained in:
1 parent
abbf12caf5
commit
d263ec58f9
6 files changed
+86
-34
No files matched your search
@@ -63,6 +63,8 @@ struct ConversationKvReuse {
|
||||
struct SavedConversation {
|
||||
// Runtime compatibility only; NOT a model/weights identity or disk schema.
|
||||
std::array<int64_t, 18> geometry{};
|
||||
// The session's layer carve the image was captured from ([0, n_layers) on one GPU); restore requires the same.
|
||||
int64_t layer_lo = 0, layer_hi = 0;
|
||||
ConversationCheckpoint live;
|
||||
std::vector<ConversationCheckpoint> checkpoints;
|
||||
std::vector<ConversationKv> kv; // main layers followed by the draft layer
|
||||
|
||||
@@ -33,7 +33,13 @@ bool conversation_kv_verify(const ConversationKv& image, const QsaState& state,
|
||||
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,
|
||||
|
||||
@@ -111,6 +111,7 @@ void full_session(int fmt, int mode, int experts) {
|
||||
g.ssm_state_size=2; g.ssm_v_heads=2; g.ssm_conv_channels=8;
|
||||
SessionState ss;
|
||||
ss.max_cells=96; ss.qsa_states=&main.state;
|
||||
ss.layer_hi=g.n_layers; ss.gdn_alloc=g.n_gdn_layers(); ss.qsa_alloc=g.n_qsa_layers(); // the whole-model carve
|
||||
std::string err;
|
||||
ConversationStateSizes sizes;
|
||||
check(conversation_state_sizes(g,sizes,err),"whole-session geometry sizes");
|
||||
|
||||
@@ -38,6 +38,11 @@ bool copy(void* dst, const void* src, size_t bytes, std::string& error) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// The session's carve (#216): `qsa_states` keeps global ordinals and holds the owned ones
|
||||
// [qsa_ord0, qsa_ord0 + qsa_alloc); `gdn_state` holds `gdn_alloc` rows. A whole-model session owns every layer.
|
||||
size_t owned_qsa(const SessionState& ss) { return (size_t) std::max<int64_t>(ss.qsa_alloc, 0); }
|
||||
const QsaState& owned(const SessionState& ss, size_t j) { return ss.qsa_states[(size_t) ss.qsa_ord0 + j]; }
|
||||
|
||||
bool image_keys(const std::vector<ConversationImageKey>& images, size_t tokens) {
|
||||
int64_t previous = -1;
|
||||
for (const auto& image : images) {
|
||||
@@ -49,11 +54,11 @@ bool image_keys(const std::vector<ConversationImageKey>& images, size_t tokens)
|
||||
|
||||
bool checkpoint_targets(const SessionState& ss, const ModelGeometry& g, size_t tokens,
|
||||
ConversationStateSizes& z, std::string& error) {
|
||||
if (!conversation_state_sizes(g, z, error)) return false;
|
||||
if (!conversation_session_sizes(g, ss, z, error)) return false;
|
||||
if (ss.max_cells < 0 || tokens > (uint64_t) ss.max_cells || (z.gdn && !ss.gdn_state) ||
|
||||
(g.n_qsa_layers() && !ss.qsa_states)) return fail(error, "invalid session running-state targets");
|
||||
for (int64_t i = 0; i < g.n_qsa_layers(); ++i) {
|
||||
const auto& st = ss.qsa_states[i];
|
||||
(owned_qsa(ss) && !ss.qsa_states)) return fail(error, "invalid session running-state targets");
|
||||
for (size_t j = 0; j < owned_qsa(ss); ++j) {
|
||||
const auto& st = owned(ss, j);
|
||||
const auto block = strata::kernels::qsa_real_shapes().idx_block;
|
||||
size_t pooled_bytes = 0;
|
||||
if (!st.idx_tail || !st.idx_dead || !st.idx_block_pos || !st.idx_pooled ||
|
||||
@@ -122,11 +127,23 @@ bool conversation_state_sizes(const ModelGeometry& g, ConversationStateSizes& z,
|
||||
return true;
|
||||
}
|
||||
|
||||
bool conversation_session_sizes(const ModelGeometry& g, const SessionState& ss, ConversationStateSizes& z,
|
||||
std::string& error) {
|
||||
if (!conversation_state_sizes(g, z, error)) return false;
|
||||
const int64_t n_gdn = g.n_gdn_layers(), n_qsa = g.n_qsa_layers();
|
||||
if (ss.gdn_alloc < 0 || ss.gdn_alloc > n_gdn || ss.qsa_ord0 < 0 || ss.qsa_alloc < 0 ||
|
||||
ss.qsa_ord0 > n_qsa || ss.qsa_alloc > n_qsa - ss.qsa_ord0)
|
||||
return fail(error, "invalid session layer carve");
|
||||
// z.gdn is n_gdn whole rows; the session holds gdn_alloc of them (all of them for the default full range)
|
||||
z.gdn = n_gdn > 0 ? z.gdn / (size_t) n_gdn * (size_t) ss.gdn_alloc : 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool conversation_checkpoint_validate(const ConversationCheckpoint& c, const SessionState& ss,
|
||||
const ModelGeometry& g, std::string& error) {
|
||||
ConversationStateSizes z;
|
||||
if (!checkpoint_targets(ss, g, c.ids.size(), z, error)) return false;
|
||||
const size_t layers = (size_t) g.n_qsa_layers();
|
||||
const size_t layers = owned_qsa(ss);
|
||||
if (c.gdn.size() != z.gdn || c.ple.size() != (ss.ple_hist ? z.ple : 0) ||
|
||||
c.tails.size() != layers * z.tail || c.dead.size() != layers * z.dead ||
|
||||
c.block_pos.size() != layers * z.block_pos || !image_keys(c.imgs, c.ids.size()))
|
||||
@@ -138,16 +155,16 @@ bool conversation_checkpoint_save(ConversationCheckpoint& c, const SessionState&
|
||||
const ModelGeometry& g, std::string& error) {
|
||||
ConversationStateSizes z;
|
||||
if (!checkpoint_targets(ss, g, c.ids.size(), z, error)) return false;
|
||||
const size_t layers = (size_t) g.n_qsa_layers();
|
||||
const size_t layers = owned_qsa(ss);
|
||||
c.gdn.resize(z.gdn); c.ple.resize(ss.ple_hist ? z.ple : 0);
|
||||
c.tails.resize(layers * z.tail); c.dead.resize(layers * z.dead); c.block_pos.resize(layers * z.block_pos);
|
||||
if (!copy(c.gdn.data(), ss.gdn_state, c.gdn.size(), error) ||
|
||||
!copy(c.ple.data(), ss.ple_hist, c.ple.size(), error)) return false;
|
||||
for (size_t i = 0; i < layers; ++i) {
|
||||
const auto& st = ss.qsa_states[i];
|
||||
if (!copy(c.tails.data() + i * z.tail, st.idx_tail, z.tail, error) ||
|
||||
!copy(c.dead.data() + i * z.dead, st.idx_dead, z.dead, error) ||
|
||||
!copy(c.block_pos.data() + i * z.block_pos, st.idx_block_pos, z.block_pos, error)) return false;
|
||||
for (size_t j = 0; j < layers; ++j) {
|
||||
const auto& st = owned(ss, j);
|
||||
if (!copy(c.tails.data() + j * z.tail, st.idx_tail, z.tail, error) ||
|
||||
!copy(c.dead.data() + j * z.dead, st.idx_dead, z.dead, error) ||
|
||||
!copy(c.block_pos.data() + j * z.block_pos, st.idx_block_pos, z.block_pos, error)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -156,17 +173,17 @@ bool conversation_checkpoint_restore(const ConversationCheckpoint& c, SessionSta
|
||||
const ModelGeometry& g, std::string& error) {
|
||||
if (!conversation_checkpoint_validate(c, ss, g, error)) return false;
|
||||
ConversationStateSizes z;
|
||||
if (!conversation_state_sizes(g, z, error)) return false;
|
||||
if (!conversation_session_sizes(g, ss, z, error)) return false;
|
||||
if (!copy(ss.gdn_state, c.gdn.data(), c.gdn.size(), error) ||
|
||||
!copy(ss.ple_hist, c.ple.data(), c.ple.size(), error)) return false;
|
||||
for (size_t i = 0; i < (size_t) g.n_qsa_layers(); ++i) {
|
||||
const auto& st = ss.qsa_states[i];
|
||||
if (!copy(st.idx_tail, c.tails.data() + i * z.tail, z.tail, error) ||
|
||||
!copy(st.idx_dead, c.dead.data() + i * z.dead, z.dead, error) ||
|
||||
!copy(st.idx_block_pos, c.block_pos.data() + i * z.block_pos, z.block_pos, error)) return false;
|
||||
for (size_t j = 0; j < owned_qsa(ss); ++j) {
|
||||
const auto& st = owned(ss, j);
|
||||
if (!copy(st.idx_tail, c.tails.data() + j * z.tail, z.tail, error) ||
|
||||
!copy(st.idx_dead, c.dead.data() + j * z.dead, z.dead, error) ||
|
||||
!copy(st.idx_block_pos, c.block_pos.data() + j * z.block_pos, z.block_pos, error)) return false;
|
||||
if (!c.ids.empty()) {
|
||||
const size_t row = c.ids.size() / strata::kernels::qsa_real_shapes().idx_block;
|
||||
if (!copy(st.idx_pooled + row * g.idx_key_dim, c.dead.data() + i * z.dead, z.dead, error)) return false;
|
||||
if (!copy(st.idx_pooled + row * g.idx_key_dim, c.dead.data() + j * z.dead, z.dead, error)) return false;
|
||||
}
|
||||
}
|
||||
const size_t tokens = c.ids.size();
|
||||
@@ -180,9 +197,9 @@ bool conversation_snapshot_bytes(const ConversationView& view, const SessionStat
|
||||
bytes = 0;
|
||||
if (!view_validate(view, ss, g, error)) return false;
|
||||
ConversationStateSizes z;
|
||||
if (!conversation_state_sizes(g, z, error)) return false;
|
||||
if (!conversation_session_sizes(g, ss, z, error)) return false;
|
||||
size_t ids = 0, images = 0, checkpoints = 0, layers = 0, tails = 0, dead = 0, positions = 0;
|
||||
const auto qsa = (uint64_t) g.n_qsa_layers();
|
||||
const auto qsa = (uint64_t) owned_qsa(ss);
|
||||
if (!product(ids, {view.ids.size(), sizeof(int32_t)}) ||
|
||||
!product(images, {view.images.size(), sizeof(ConversationImageKey)}) ||
|
||||
!product(checkpoints, {view.checkpoints.size(), sizeof(ConversationCheckpoint)}) ||
|
||||
@@ -195,7 +212,7 @@ bool conversation_snapshot_bytes(const ConversationView& view, const SessionStat
|
||||
if (!metadata_bytes(c, bytes)) return fail(error, "checkpoint byte count overflow");
|
||||
const int64_t upto = (int64_t) view.ids.size(); // view_validate bounds this by signed max_cells
|
||||
for (uint64_t i = 0; i <= qsa; ++i) {
|
||||
const auto& st = i == qsa ? draft : ss.qsa_states[i];
|
||||
const auto& st = i == qsa ? draft : owned(ss, (size_t) i);
|
||||
const size_t n = conversation_kv_bytes(st, g, upto, i != qsa);
|
||||
if (!n || !add(bytes, n)) return fail(error, "invalid or overflowing K/V byte estimate");
|
||||
}
|
||||
@@ -207,13 +224,13 @@ bool conversation_snapshot_capture_bytes(const ConversationKvReuse& reuse, const
|
||||
const QsaState& draft, size_t& bytes, std::string& error) {
|
||||
if (!conversation_snapshot_bytes(view, ss, g, draft, bytes, error)) return false;
|
||||
if (reuse.kv.empty()) return true;
|
||||
const size_t layers = size_t(g.n_qsa_layers()) + 1;
|
||||
const size_t layers = owned_qsa(ss) + 1;
|
||||
if (reuse.kv.size() != layers || reuse.unchanged_tokens < 0 ||
|
||||
reuse.unchanged_tokens > reuse.captured_tokens || reuse.unchanged_tokens > int64_t(view.ids.size()))
|
||||
return fail(error, "invalid retained K/V prefix");
|
||||
for (size_t i = 0; i < layers; ++i) {
|
||||
const bool index = i + 1 != layers;
|
||||
const auto& st = index ? ss.qsa_states[i] : draft;
|
||||
const auto& st = index ? owned(ss, i) : draft;
|
||||
if (!conversation_kv_validate(reuse.kv[i], st, g, reuse.captured_tokens, index, error)) return false;
|
||||
const size_t fresh = conversation_kv_bytes(st, g, int64_t(view.ids.size()), index);
|
||||
size_t retained = 0;
|
||||
@@ -236,15 +253,17 @@ bool conversation_snapshot_save(SavedConversation& image, const ConversationView
|
||||
// Build into a new object so a failure cannot publish a partial snapshot.
|
||||
SavedConversation captured;
|
||||
captured.geometry = geometry_key(g);
|
||||
captured.layer_lo = ss.layer_lo; captured.layer_hi = ss.layer_hi;
|
||||
captured.live.ids = view.ids; captured.live.imgs = view.images;
|
||||
captured.cvec = view.cvec; captured.checkpoints = view.checkpoints;
|
||||
const int64_t unchanged = reuse.kv.empty() ? 0 : reuse.unchanged_tokens;
|
||||
captured.kv = std::move(reuse.kv);
|
||||
captured.kv.resize((size_t) g.n_qsa_layers() + 1);
|
||||
const size_t layers = owned_qsa(ss);
|
||||
captured.kv.resize(layers + 1);
|
||||
if (!conversation_checkpoint_save(captured.live, ss, g, error)) return false;
|
||||
const int64_t upto = (int64_t) view.ids.size();
|
||||
for (int64_t i = 0; i < g.n_qsa_layers(); ++i)
|
||||
if (!conversation_kv_save(captured.kv[(size_t) i], ss.qsa_states[i], g, upto, true, error,
|
||||
for (size_t j = 0; j < layers; ++j)
|
||||
if (!conversation_kv_save(captured.kv[j], owned(ss, j), g, upto, true, error,
|
||||
unchanged, reused_bytes)) return false;
|
||||
// The draft's final cell may not have been computed when the output cap was
|
||||
// reached. Refresh that page even when the main prefix continued unchanged.
|
||||
@@ -258,12 +277,16 @@ bool conversation_snapshot_validate(const SavedConversation& image, const Sessio
|
||||
const ModelGeometry& g, const QsaState& draft, std::string& error) {
|
||||
if (!image.live.stage_parts.empty()) return fail(error, "layer-split parking is not supported");
|
||||
if (image.geometry != geometry_key(g)) return fail(error, "incompatible runtime geometry");
|
||||
// an image holds exactly one carve's running state and K/V: same layer range or nothing
|
||||
if (image.layer_lo != ss.layer_lo || image.layer_hi != ss.layer_hi)
|
||||
return fail(error, "snapshot from another session layer range");
|
||||
const ConversationView view{image.live.ids, image.live.imgs, image.checkpoints, image.cvec};
|
||||
if (!view_validate(view, ss, g, error) || !conversation_checkpoint_validate(image.live, ss, g, error)) return false;
|
||||
if (image.kv.size() != (size_t) g.n_qsa_layers() + 1) return fail(error, "invalid K/V layer count");
|
||||
const size_t layers = owned_qsa(ss);
|
||||
if (image.kv.size() != layers + 1) return fail(error, "invalid K/V layer count");
|
||||
const int64_t upto = (int64_t) image.live.ids.size();
|
||||
for (int64_t i = 0; i < g.n_qsa_layers(); ++i)
|
||||
if (!conversation_kv_validate(image.kv[(size_t) i], ss.qsa_states[i], g, upto, true, error)) return false;
|
||||
for (size_t j = 0; j < layers; ++j)
|
||||
if (!conversation_kv_validate(image.kv[j], owned(ss, j), g, upto, true, error)) return false;
|
||||
return conversation_kv_validate(image.kv.back(), draft, g, upto, false, error);
|
||||
}
|
||||
|
||||
@@ -272,8 +295,8 @@ ConversationRestore conversation_snapshot_restore(const SavedConversation& image
|
||||
if (!conversation_snapshot_validate(image, ss, g, draft, error)) return ConversationRestore::invalid;
|
||||
if (!sync(error)) return ConversationRestore::transfer_failed;
|
||||
const int64_t upto = (int64_t) image.live.ids.size();
|
||||
for (int64_t i = 0; i < g.n_qsa_layers(); ++i)
|
||||
if (!conversation_kv_restore(image.kv[(size_t) i], ss.qsa_states[i], g, upto, true, error))
|
||||
for (size_t j = 0; j < owned_qsa(ss); ++j)
|
||||
if (!conversation_kv_restore(image.kv[j], owned(ss, j), g, upto, true, error))
|
||||
return ConversationRestore::transfer_failed;
|
||||
if (!conversation_kv_restore(image.kv.back(), draft, g, upto, false, error) ||
|
||||
!conversation_checkpoint_restore(image.live, ss, g, error)) return ConversationRestore::transfer_failed;
|
||||
|
||||
@@ -82,12 +82,15 @@ void fixture(int format, int experts, bool zero_qsa, bool ple) {
|
||||
std::vector<uint8_t> gdn(z.gdn, 0xa5), history(ple ? z.ple : 0, 0xa5);
|
||||
SessionState ss;
|
||||
ss.max_cells = 96; ss.gdn_state = (float*) gdn.data();
|
||||
// the whole-model carve, as session_init leaves a one-GPU session
|
||||
ss.layer_hi = g.n_layers; ss.gdn_alloc = g.n_gdn_layers(); ss.qsa_alloc = g.n_qsa_layers();
|
||||
ss.ple_hist = ple ? (float*) history.data() : nullptr;
|
||||
ss.qsa_states = zero_qsa ? nullptr : layers.data();
|
||||
SavedConversation image;
|
||||
image.geometry = {g.n_embd,g.n_layers,g.qsa_interval,g.ssm_state_size,g.ssm_k_heads,g.ssm_v_heads,
|
||||
g.ssm_d_conv,g.ssm_conv_channels,g.ssm_value_dim,g.n_head,g.n_head_kv,g.head_dim,
|
||||
g.idx_q_heads,g.idx_key_dim,g.hc,g.hc_lr,g.n_expert,g.n_ff};
|
||||
image.layer_hi = g.n_layers;
|
||||
auto checkpoint = [&](size_t tokens) {
|
||||
ConversationCheckpoint c;
|
||||
c.ids.resize(tokens);
|
||||
@@ -138,6 +141,23 @@ void fixture(int format, int experts, bool zero_qsa, bool ple) {
|
||||
ss.qsa_states = nullptr;
|
||||
check(!conversation_snapshot_validate(image,ss,g,draft.st,error), "missing QSA targets rejected");
|
||||
ss.qsa_states = layers.data();
|
||||
reject([](auto& s){s.layer_lo = 4;}, "snapshot from another layer range rejected");
|
||||
// #216's carve: a split stage owning layers [4, 8) holds GDN rows 3..5 and QSA ordinal 1 only
|
||||
SessionState stage = ss;
|
||||
stage.layer_lo = 4; stage.gdn_alloc = 3; stage.qsa_ord0 = 1; stage.qsa_alloc = 1;
|
||||
ConversationStateSizes zs;
|
||||
check(conversation_session_sizes(g,stage,zs,error) && zs.gdn == z.gdn / 6 * 3 && zs.tail == z.tail,
|
||||
"carved session sizes cover the owned rows only");
|
||||
check(!conversation_checkpoint_validate(image.live,stage,g,error), "whole-model checkpoint rejected by a carve");
|
||||
ConversationCheckpoint part = image.live;
|
||||
part.gdn.resize(zs.gdn); part.tails.resize(zs.tail); part.dead.resize(zs.dead);
|
||||
part.block_pos.resize(zs.block_pos);
|
||||
check(conversation_checkpoint_validate(part,stage,g,error), "carve-sized checkpoint validates");
|
||||
check(!conversation_snapshot_validate(image,stage,g,draft.st,error), "whole-model snapshot rejected by a carve");
|
||||
stage.qsa_alloc = 2;
|
||||
check(!conversation_session_sizes(g,stage,zs,error), "carve past the last QSA ordinal rejected");
|
||||
stage.qsa_alloc = 1; stage.gdn_alloc = 7;
|
||||
check(!conversation_session_sizes(g,stage,zs,error), "carve past the GDN rows rejected");
|
||||
}
|
||||
auto bad_geometry = g; bad_geometry.ssm_state_size = std::numeric_limits<int64_t>::max();
|
||||
check(!conversation_state_sizes(bad_geometry,z,error), "running-state arithmetic overflow rejected");
|
||||
|
||||
@@ -809,9 +809,9 @@ using ConvStateSizes = strata::core::ConversationStateSizes;
|
||||
ConvStateSizes conv_state_sizes(const strata::core::ModelGeometry& g, const strata::core::SessionState& ss) {
|
||||
ConvStateSizes z;
|
||||
std::string error;
|
||||
strata::core::conversation_state_sizes(g, z, error); // geometry has already passed engine validation
|
||||
// a split stage's session owns only its layer range's state (see SessionState's carve note)
|
||||
if (g.n_gdn_layers() > 0) z.gdn = z.gdn / (size_t) g.n_gdn_layers() * (size_t) ss.gdn_alloc;
|
||||
// a split stage's session owns only its layer range's state (see SessionState's carve note); the geometry
|
||||
// and the carve have already passed engine validation
|
||||
strata::core::conversation_session_sizes(g, ss, z, error);
|
||||
return z;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user