Repository navigation
Expand file tree
/
Copy pathlayer_split_kvflash.h
More file actions
106 lines (97 loc) · 3.24 KB
/
Copy pathlayer_split_kvflash.h
File metadata and controls
106 lines (97 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Shared KVFlash helpers for target layer-split adapters.
//
// Model adapters still own model-specific KV tensor discovery, scoring, and
// remote-shard synchronization. These helpers keep the common pager/history
// contracts identical across Qwen, Gemma, and Laguna layer-split backends.
#pragma once
#include "kvflash_pager.h"
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <vector>
namespace luce::common {
inline bool layer_split_kvflash_sync_identity(
KvFlashPager & pager,
int committed,
int pool_tokens,
const char * log_prefix) {
const char * prefix = log_prefix ? log_prefix : "target-split";
if (committed < 0) {
std::fprintf(stderr,
"[%s][kvflash] invalid committed position %d\n",
prefix, committed);
return false;
}
if (pager.row_base() != 0) { // identity sync writes rows from 0: a slot slice is not that
std::fprintf(stderr,
"[%s][kvflash] identity sync refused: pager is a slot slice (row_base %d)\n",
prefix, pager.row_base());
return false;
}
if (committed > pool_tokens) {
std::fprintf(stderr,
"[%s][kvflash] prefix (%d) exceeds resident pool %d\n",
prefix, committed, pool_tokens);
return false;
}
pager.reset();
for (int p = 0; p < committed; ++p) {
const int slot = pager.slot_for(p);
if (slot != p) {
std::fprintf(stderr,
"[%s][kvflash] identity slot mismatch %d != %d\n",
prefix, slot, p);
return false;
}
}
pager.zero_free_blocks();
return pager.is_identity();
}
inline void layer_split_kvflash_sync_history(
std::vector<int32_t> & history,
const std::vector<int32_t> & tokens,
int base_pos) {
if (base_pos <= 0) {
history.assign(tokens.begin(), tokens.end());
return;
}
if ((int)history.size() > base_pos) {
history.resize((size_t)base_pos);
} else if ((int)history.size() < base_pos) {
history.resize((size_t)base_pos, 0);
}
history.insert(history.end(), tokens.begin(), tokens.end());
}
inline void layer_split_kvflash_restore_history(
std::vector<int32_t> & history,
const std::vector<std::vector<int32_t>> & snapshots,
int slot,
int cur_pos) {
if (slot >= 0 && slot < (int)snapshots.size() &&
!snapshots[(size_t)slot].empty()) {
history = snapshots[(size_t)slot];
} else {
history.clear();
}
if (cur_pos <= 0) {
history.clear();
} else if ((int)history.size() > cur_pos) {
history.resize((size_t)cur_pos);
} else if ((int)history.size() < cur_pos) {
history.resize((size_t)cur_pos, 0);
}
}
inline void layer_split_kvflash_save_history_snapshot(
const std::vector<int32_t> & history,
int cur_pos,
std::vector<int32_t> & snapshot) {
snapshot.clear();
if (cur_pos <= 0) return;
const size_t keep = (size_t)std::min(cur_pos, (int)history.size());
snapshot.assign(history.begin(), history.begin() + keep);
if ((int)snapshot.size() < cur_pos) {
snapshot.resize((size_t)cur_pos, 0);
}
}
} // namespace luce::common