tokenspeed-pd 0.1.0

TokenSpeed's PD transfer peer: the P/D handshake, room states and KV transfers over the Mooncake transfer engine
Documentation
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
// Copyright (c) 2026 LightSeek Foundation
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

//! One request's transferable blocks (`CachePDBlockManifest`), read off the
//! scheduler's block tables by each group's transfer policy, and the blocks
//! one layerwise prefill chunk makes ready.

use std::collections::{BTreeMap, HashSet};

use serde_json::Value;

use super::{
    contract::{load_wire_json, Contract, GroupSpec},
    LayoutError,
};

/// The largest manifest the wire carries.
pub const MAX_MANIFEST_WIRE_BYTES: usize = 2 << 20;

/// A state group's live block-table slot (`STATE_LIVE_SLOT`): the exact
/// prompt-end state the request lands in.
pub const STATE_LIVE_SLOT: i64 = 1;

/// One group's blocks, in logical slot order.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GroupBlocks {
    /// The group.
    pub group_id: String,
    /// The publishing side's scheduler block ids (virtual ids for a
    /// KVP-sharded group).
    pub block_ids: Vec<i64>,
}

/// One request's transferable blocks per group.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Manifest {
    /// The groups, in the contract's group order.
    pub groups: Vec<GroupBlocks>,
    /// Prompt tokens the receiver already holds.
    pub prefix_len: i64,
    /// The prompt's length.
    pub prompt_len: i64,
    /// The receiver found every state group's prompt-end checkpoint in its
    /// own cache: no `latest_snapshot` group transfers.
    pub state_prefix_cached: bool,
}

/// Python's `bool(value)` of a JSON value.
fn truthy(value: &Value) -> bool {
    match value {
        Value::Null => false,
        Value::Bool(flag) => *flag,
        Value::Number(number) => number.as_f64().is_some_and(|n| n != 0.0),
        Value::String(text) => !text.is_empty(),
        Value::Array(items) => !items.is_empty(),
        Value::Object(map) => !map.is_empty(),
    }
}

impl Manifest {
    /// Parse a manifest off the wire (`CachePDBlockManifest.from_wire_bytes`).
    pub fn from_wire(raw: &[u8]) -> Result<Self, LayoutError> {
        let name = "Cache block manifest";
        let payload = load_wire_json(raw, name, MAX_MANIFEST_WIRE_BYTES)?;
        let field = |key: &str| {
            payload
                .get(key)
                .ok_or_else(|| LayoutError::new(format!("{name} misses {key:?}")))
        };
        let int = |value: &Value, what: &str| {
            value
                .as_i64()
                .ok_or_else(|| LayoutError::new(format!("{name} {what} must be an integer")))
        };
        let groups = field("groups")?
            .as_array()
            .ok_or_else(|| LayoutError::new(format!("{name} groups must be a list")))?
            .iter()
            .map(|group| {
                let group_id = group
                    .get("group_id")
                    .and_then(Value::as_str)
                    .ok_or_else(|| LayoutError::new(format!("{name} group misses its id")))?;
                let block_ids = group
                    .get("block_ids")
                    .and_then(Value::as_array)
                    .ok_or_else(|| LayoutError::new(format!("{name} group misses its blocks")))?
                    .iter()
                    .map(|block| int(block, "block id"))
                    .collect::<Result<_, _>>()?;
                Ok(GroupBlocks {
                    group_id: group_id.to_owned(),
                    block_ids,
                })
            })
            .collect::<Result<_, LayoutError>>()?;
        Ok(Self {
            groups,
            prefix_len: int(field("prefix_len")?, "prefix_len")?,
            prompt_len: int(field("prompt_len")?, "prompt_len")?,
            state_prefix_cached: truthy(field("state_prefix_cached")?),
        })
    }

    /// The manifest's wire form (`to_wire_bytes`): the Python sender's key
    /// order and compact separators.
    pub fn to_wire(&self) -> Result<Vec<u8>, LayoutError> {
        let mut out = String::from("{\"groups\":[");
        for (index, group) in self.groups.iter().enumerate() {
            if index > 0 {
                out.push(',');
            }
            out.push_str(&format!(
                "{{\"group_id\":{},\"block_ids\":{}}}",
                Value::from(group.group_id.as_str()),
                Value::from(group.block_ids.clone())
            ));
        }
        out.push_str(&format!(
            "],\"prefix_len\":{},\"prompt_len\":{},\"state_prefix_cached\":{}}}",
            self.prefix_len, self.prompt_len, self.state_prefix_cached
        ));
        if out.len() > MAX_MANIFEST_WIRE_BYTES {
            return Err(LayoutError::new(format!(
                "Cache block manifest exceeds {MAX_MANIFEST_WIRE_BYTES} wire bytes"
            )));
        }
        Ok(out.into_bytes())
    }
}

/// The block-table slots one group transfers for a prompt (`_logical_slots`):
/// a `full_suffix` group every retained block from the one holding
/// `prefix_len` through the final partial block, a snapshot group its live
/// slot unless the receiver holds the prompt-end checkpoint.
pub fn logical_slots(
    spec: &GroupSpec,
    prefix_len: i64,
    prompt_len: i64,
    state_prefix_cached: bool,
) -> Result<Vec<i64>, LayoutError> {
    if spec.full_suffix() {
        let granularity = spec.block_granularity()?;
        let mut begin = prefix_len.div_euclid(granularity);
        if spec.attn_kind == "sliding_window" {
            // The next decode token attends the preceding window - 1 tokens:
            // every block intersecting that retained tail goes.
            let window = spec.sliding_window_tokens.unwrap_or(0);
            let retained_begin = (prompt_len - window + 1).max(0);
            begin = begin.max(retained_begin.div_euclid(granularity));
        }
        let end = (prompt_len + granularity - 1).div_euclid(granularity);
        return Ok((begin..end).collect());
    }
    Ok(if state_prefix_cached {
        Vec::new()
    } else {
        vec![STATE_LIVE_SLOT]
    })
}

/// Check a manifest against the contract whose blocks it names
/// (`validate_cache_manifest`).
pub fn validate_manifest(
    manifest: &Manifest,
    layout: &Contract,
    peer: &str,
) -> Result<(), LayoutError> {
    if !(0 <= manifest.prefix_len && manifest.prefix_len < manifest.prompt_len) {
        return Err(LayoutError::new(format!(
            "{peer} manifest requires 0 <= prefix_len < prompt_len, got prefix_len={} \
             prompt_len={}",
            manifest.prefix_len, manifest.prompt_len
        )));
    }
    let expected: Vec<&str> = layout
        .group_specs
        .iter()
        .map(|s| s.group_id.as_str())
        .collect();
    let actual: Vec<&str> = manifest
        .groups
        .iter()
        .map(|g| g.group_id.as_str())
        .collect();
    if actual != expected {
        return Err(LayoutError::new(format!(
            "{peer} manifest group order disagrees with layout"
        )));
    }
    for (group, spec) in manifest.groups.iter().zip(&layout.group_specs) {
        let required = logical_slots(
            spec,
            manifest.prefix_len,
            manifest.prompt_len,
            manifest.state_prefix_cached,
        )?;
        if group.block_ids.len() != required.len() {
            return Err(LayoutError::new(format!(
                "{peer} manifest group {:?} block count disagrees with its transfer policy",
                group.group_id
            )));
        }
        let capacity = layout.virtual_block_count(&spec.group_id)?;
        if group
            .block_ids
            .iter()
            .any(|&block| block <= 0 || block >= capacity)
        {
            return Err(LayoutError::new(format!(
                "{peer} manifest group {:?} has an out-of-bounds block",
                group.group_id
            )));
        }
    }
    Ok(())
}

/// One group's block table: a row of scheduler block ids per request.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BlockTable {
    /// Block-table slots per row.
    pub columns: usize,
    /// The rows, row-major.
    pub blocks: Vec<i64>,
}

impl BlockTable {
    /// A table from its rows; every row has `columns` entries.
    pub fn from_rows(rows: &[Vec<i64>], columns: usize) -> Self {
        Self {
            columns,
            blocks: rows.iter().flatten().copied().collect(),
        }
    }

    fn rows(&self) -> usize {
        self.blocks.len().checked_div(self.columns).unwrap_or(0)
    }

    fn block(&self, row: usize, slot: i64) -> Result<i64, LayoutError> {
        let slot = usize::try_from(slot)
            .map_err(|_| LayoutError::new(format!("block-table slot {slot} is negative")))?;
        if row >= self.rows() || slot >= self.columns {
            return Err(LayoutError::new(format!(
                "block-table entry ({row}, {slot}) is outside the table"
            )));
        }
        Ok(self.blocks[row * self.columns + slot])
    }
}

/// A forward's block tables by group.
pub type BlockTables = BTreeMap<String, BlockTable>;

/// The tables name exactly the contract's groups.
fn check_tables(tables: &BlockTables, layout: &Contract) -> Result<(), LayoutError> {
    let expected: HashSet<&str> = layout
        .group_specs
        .iter()
        .map(|s| s.group_id.as_str())
        .collect();
    let actual: HashSet<&str> = tables.keys().map(String::as_str).collect();
    if expected != actual {
        return Err(LayoutError::new(
            "scheduler group IDs disagree with the cache-transfer layout",
        ));
    }
    Ok(())
}

/// Read the table entries at `slots`, each a valid block of the group.
fn blocks_at(
    table: &BlockTable,
    layout: &Contract,
    group_id: &str,
    request_row: usize,
    slots: &[i64],
) -> Result<Vec<i64>, LayoutError> {
    if let Some(&last) = slots.last() {
        if last >= table.columns as i64 {
            return Err(LayoutError::new(format!(
                "table {group_id:?} misses logical slot {last}"
            )));
        }
    }
    let capacity = layout.virtual_block_count(group_id)?;
    slots
        .iter()
        .map(|&slot| {
            let block = table.block(request_row, slot)?;
            if block <= 0 || block >= capacity {
                return Err(LayoutError::new(format!(
                    "table {group_id:?} logical slot {slot} has invalid block ID {block}"
                )));
            }
            Ok(block)
        })
        .collect()
}

/// One request's manifest off its block tables
/// (`build_cache_block_manifest`).
pub fn build_manifest(
    tables: &BlockTables,
    layout: &Contract,
    request_row: usize,
    prefix_len: i64,
    prompt_len: i64,
    state_prefix_cached: bool,
) -> Result<Manifest, LayoutError> {
    if prefix_len >= prompt_len {
        return Err(LayoutError::new(
            "Cache-transfer PD requires prefix_len < prompt_len",
        ));
    }
    check_tables(tables, layout)?;
    let groups = layout
        .group_specs
        .iter()
        .map(|spec| {
            let slots = logical_slots(spec, prefix_len, prompt_len, state_prefix_cached)?;
            Ok(GroupBlocks {
                group_id: spec.group_id.clone(),
                block_ids: blocks_at(
                    &tables[&spec.group_id],
                    layout,
                    &spec.group_id,
                    request_row,
                    &slots,
                )?,
            })
        })
        .collect::<Result<_, LayoutError>>()?;
    Ok(Manifest {
        groups,
        prefix_len,
        prompt_len,
        state_prefix_cached,
    })
}

/// One group's blocks a layerwise chunk made ready, and their positions in
/// the receiver's manifest.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GroupSelection {
    /// This rank's scheduler block ids.
    pub source_block_ids: Vec<i64>,
    /// Their positions in the receiver's group of its manifest.
    pub destination_positions: Vec<usize>,
}

/// The blocks one layerwise prefill chunk made ready, per group
/// (`CachePDLayerwiseBlockSelection`).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Selection {
    /// The groups, in the contract's order.
    pub groups: Vec<GroupSelection>,
}

/// The blocks that became transferable during one prefill chunk
/// (`build_cache_layerwise_block_selection`): a history group's newly
/// completed retained blocks (and the final partial one), a snapshot group's
/// prompt-end state once the final chunk wrote it.
#[allow(clippy::too_many_arguments)]
pub fn build_selection(
    tables: &BlockTables,
    layout: &Contract,
    request_row: usize,
    prefix_len: i64,
    prompt_len: i64,
    chunk_start: i64,
    chunk_end: i64,
    state_prefix_cached: bool,
) -> Result<Selection, LayoutError> {
    if prefix_len >= prompt_len {
        return Err(LayoutError::new(
            "layerwise selection requires prefix_len < prompt_len",
        ));
    }
    if chunk_start >= chunk_end {
        return Err(LayoutError::new(
            "layerwise selection requires chunk_start < chunk_end",
        ));
    }
    if chunk_end > prompt_len {
        return Err(LayoutError::new(
            "layerwise selection requires chunk_end <= prompt_len",
        ));
    }
    let is_final = chunk_end == prompt_len;
    check_tables(tables, layout)?;
    let mut groups = Vec::with_capacity(layout.group_specs.len());
    for spec in &layout.group_specs {
        let final_slots = logical_slots(spec, prefix_len, prompt_len, state_prefix_cached)?;
        let (positions, slots): (Vec<usize>, Vec<i64>) = match spec.transfer_policy.as_deref() {
            Some("full_suffix") => {
                let granularity = spec.block_granularity()?;
                let count = final_slots.len() as i64;
                let (start, ready) = match final_slots.first() {
                    Some(&first) => {
                        let ready_end = if is_final {
                            (chunk_end + granularity - 1).div_euclid(granularity)
                        } else {
                            chunk_end.div_euclid(granularity)
                        };
                        (
                            (chunk_start.div_euclid(granularity) - first).clamp(0, count),
                            (ready_end - first).clamp(0, count),
                        )
                    }
                    None => (0, 0),
                };
                (start..ready)
                    .map(|position| (position as usize, final_slots[position as usize]))
                    .unzip()
            }
            Some("latest_snapshot") => {
                let positions = if is_final && !final_slots.is_empty() {
                    vec![0]
                } else {
                    Vec::new()
                };
                let slots = if is_final { final_slots } else { Vec::new() };
                (positions, slots)
            }
            _ => {
                return Err(LayoutError::new(format!(
                    "group {:?} has no transfer policy",
                    spec.group_id
                )))
            }
        };
        groups.push(GroupSelection {
            source_block_ids: blocks_at(
                &tables[&spec.group_id],
                layout,
                &spec.group_id,
                request_row,
                &slots,
            )?,
            destination_positions: positions,
        });
    }
    Ok(Selection { groups })
}