tokenspeed-wire 0.1.0

TokenSpeed engine IPC schema and msgpack codec
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
// 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.

//! Control operations (`engine/io_struct.py`): cache flush, pause, memory
//! release, weight updates, profiling and the like.
//!
//! A frontend sends one to every engine as a CONTROL message; each engine
//! answers with the matching reply on its output socket, beside the output
//! batches. Replies carry no request id: an engine answers each kind of
//! operation in the order it received them. Every struct is tagged and
//! starts with the two `BaseReq` fields.

use crate::{
    codec::{Dict, StrOrList, Wire},
    error::{Error, Result},
    msgpack::{Decoder, Encoder},
    value::Value,
};

/// Declares tagged control structs that start with the `BaseReq` fields.
macro_rules! control_struct {
    ($(
        $(#[$meta:meta])*
        $name:ident {
            $( $(#[$fmeta:meta])* $field:ident: $fty:ty $(= $fdefault:expr)? ),* $(,)?
        }
    )*) => {$(
        wire_struct! {
            $(#[$meta])*
            #[derive(Debug, Clone, PartialEq)]
            pub struct $name: array(stringify!($name)) {
                /// Request id; control operations rarely set it.
                pub rid: Option<StrOrList> = None,
                /// In-process HTTP worker address; unused across processes.
                pub http_worker_ipc: Option<String> = None,
                $( $(#[$fmeta])* pub $field: $fty $(= $fdefault)? ),*
            }
        }
    )*};
}

control_struct! {
    /// Drop the prefix caches.
    FlushCacheReqInput {}
    /// [`FlushCacheReqInput`]'s reply.
    FlushCacheReqOutput {
        /// Whether the caches were dropped.
        success: bool,
    }

    /// Stop scheduling: `abort` aborts what runs, `wait` lets it finish,
    /// `keep` freezes it in place.
    PauseSchedulerReqInput {
        /// `abort`, `wait` or `keep`.
        mode: String = "abort".to_owned(),
    }
    /// [`PauseSchedulerReqInput`]'s reply, once the pause took hold.
    PauseSchedulerReqOutput {
        /// Whether the engine paused.
        success: bool,
        /// Why not, if it did not.
        message: String = String::new(),
    }

    /// Resume scheduling after a pause.
    ResumeSchedulerReqInput {}
    /// [`ResumeSchedulerReqInput`]'s reply.
    ResumeSchedulerReqOutput {
        /// Whether the engine resumed.
        success: bool,
        /// Why not, if it did not.
        message: String = String::new(),
    }

    /// Whether scheduling is paused.
    IsSchedulerPausedReqInput {}
    /// [`IsSchedulerPausedReqInput`]'s reply.
    IsSchedulerPausedReqOutput {
        /// Paused.
        is_paused: bool,
    }

    /// Load new weights from a checkpoint on disk.
    UpdateWeightFromDiskReqInput {
        /// The checkpoint.
        model_path: String,
        /// Its format.
        load_format: Option<String> = None,
        /// The weight version after a successful load.
        weight_version: Option<String> = None,
        /// Drop the cached KV first: it was computed under the old weights.
        flush_cache: bool = true,
    }
    /// [`UpdateWeightFromDiskReqInput`]'s reply.
    UpdateWeightFromDiskReqOutput {
        /// Whether the weights were loaded.
        success: bool,
        /// Detail.
        message: String,
        /// Requests paused during the load.
        num_paused_requests: Option<i64> = Some(0),
    }

    /// Receive new weights from the trainer's NCCL group.
    UpdateWeightsFromDistributedReqInput {
        /// Parameter names, in broadcast order.
        names: Vec<String>,
        /// Their dtypes.
        dtype_names: Vec<String>,
        /// Their shapes.
        shapes: Vec<Vec<i64>>,
        /// The weight-update group.
        group_name: String = "weight_update_group".to_owned(),
        /// Drop the cached KV first.
        flush_cache: bool = true,
        /// The L3 namespace afterwards; `None` keeps the current one.
        weight_version: Option<String>,
    }
    /// [`UpdateWeightsFromDistributedReqInput`]'s reply.
    UpdateWeightsFromDistributedReqOutput {
        /// Whether the weights were received.
        success: bool,
        /// Detail.
        message: String,
    }

    /// Load weights from serialized tensors; the engines refuse it.
    UpdateWeightsFromTensorReqInput {
        /// One serialized tensor dict per world rank.
        serialized_named_tensors: Vec<Value>,
        /// Their format.
        load_format: Option<String>,
        /// Drop the cached KV first.
        flush_cache: bool,
        /// The weight version afterwards.
        weight_version: Option<String> = None,
    }
    /// [`UpdateWeightsFromTensorReqInput`]'s reply.
    UpdateWeightsFromTensorReqOutput {
        /// Whether the weights were loaded.
        success: bool,
        /// Detail.
        message: String,
    }

    /// Join the trainer's weight-update group.
    InitWeightsUpdateGroupReqInput {
        /// The group's rendezvous host.
        master_address: String,
        /// Its port.
        master_port: i64,
        /// This engine's first rank in the group.
        rank_offset: i64,
        /// The group's size.
        world_size: i64,
        /// The group's name.
        group_name: String = "weight_update_group".to_owned(),
        /// Its backend.
        backend: String = "nccl".to_owned(),
    }
    /// [`InitWeightsUpdateGroupReqInput`]'s reply.
    InitWeightsUpdateGroupReqOutput {
        /// Whether the engine joined.
        success: bool,
        /// Detail.
        message: String,
    }

    /// Leave a weight-update group.
    DestroyWeightsUpdateGroupReqInput {
        /// The group's name.
        group_name: String = "weight_update_group".to_owned(),
    }
    /// [`DestroyWeightsUpdateGroupReqInput`]'s reply.
    DestroyWeightsUpdateGroupReqOutput {
        /// Whether the engine left.
        success: bool,
        /// Detail.
        message: String,
    }

    /// Load one committed version from a Mooncake weight store.
    UpdateWeightsFromMooncakeReqInput {
        /// The version.
        version: i64,
        /// Drop the cached KV first.
        flush_cache: bool,
        /// The L3 namespace afterwards; `None` for the version's default.
        weight_version: Option<String>,
    }
    /// [`UpdateWeightsFromMooncakeReqInput`]'s reply.
    UpdateWeightsFromMooncakeReqOutput {
        /// Whether the weights were loaded.
        success: bool,
        /// Detail.
        message: String,
    }

    /// Start one online expert rebalance now.
    RebalanceExpertsReqInput {}
    /// [`RebalanceExpertsReqInput`]'s reply.
    RebalanceExpertsReqOutput {
        /// Whether the rebalance started.
        success: bool,
        /// Detail.
        message: String,
    }

    /// Release GPU memory (`weights`, `kv_cache`; all when `None`).
    ReleaseMemoryOccupationReqInput {
        /// The regions.
        tags: Option<Vec<String>> = None,
    }
    /// [`ReleaseMemoryOccupationReqInput`]'s reply.
    ReleaseMemoryOccupationReqOutput {
        /// Whether the memory was released.
        success: bool = true,
        /// Detail.
        message: String = String::new(),
    }

    /// Take released GPU memory back (all released regions when `None`).
    ResumeMemoryOccupationReqInput {
        /// The regions.
        tags: Option<Vec<String>> = None,
    }
    /// [`ResumeMemoryOccupationReqInput`]'s reply.
    ResumeMemoryOccupationReqOutput {
        /// Whether the memory was taken back.
        success: bool = true,
        /// Detail.
        message: String = String::new(),
    }

    /// Whether the engine released its memory.
    IsSleepingReqInput {}
    /// [`IsSleepingReqInput`]'s reply.
    IsSleepingReqOutput {
        /// Released.
        is_sleeping: bool,
    }

    /// The engine's internal state.
    GetInternalStateReq {}
    /// [`GetInternalStateReq`]'s reply.
    GetInternalStateReqOutput {
        /// The state.
        internal_state: Dict<Value, Value>,
    }

    /// Change server arguments at run time.
    SetInternalStateReq {
        /// The arguments to change.
        server_args: Dict<Value, Value>,
    }
    /// [`SetInternalStateReq`]'s reply.
    SetInternalStateReqOutput {
        /// Whether anything changed.
        updated: bool,
        /// The arguments now.
        server_args: Dict<Value, Value>,
    }

    /// Start (`kind` 1) or stop (`kind` 2) the profiler.
    ProfileReq {
        /// `ProfileReqType`: 1 starts, 2 stops.
        kind: i64,
        /// Where traces go.
        output_dir: Option<String> = None,
        /// The forward step to start at.
        start_step: Option<i64> = None,
        /// Steps to profile before stopping.
        num_steps: Option<i64> = None,
        /// What to record (`CPU`, `GPU`, `MEM`, ...).
        activities: Option<Vec<String>> = None,
        /// One trace per stage (prefill, decode).
        profile_by_stage: bool = false,
        /// Record Python stacks.
        with_stack: Option<bool> = None,
        /// Record tensor shapes.
        record_shapes: Option<bool> = None,
        /// Names the trace files.
        profile_id: Option<String> = None,
    }
    /// [`ProfileReq`]'s reply.
    ProfileReqOutput {
        /// Whether the profiler changed state.
        success: bool,
        /// Detail.
        message: String,
    }
}

/// `ProfileReq.kind` that starts the profiler.
pub const PROFILE_START: i64 = 1;
/// `ProfileReq.kind` that stops it.
pub const PROFILE_STOP: i64 = 2;

/// Declares an enum over tagged structs, decoded by their tag.
macro_rules! tagged_union {
    (
        $(#[$meta:meta])*
        $vis:vis enum $name:ident { $( $(#[$vmeta:meta])* $variant:ident($ty:ident) ),* $(,)? }
    ) => {
        $(#[$meta])*
        $vis enum $name {
            $( $(#[$vmeta])* $variant($ty) ),*
        }

        impl $name {
            /// The variant's wire tag (its Python class name).
            pub fn tag(&self) -> &'static str {
                match self {
                    $( Self::$variant(_) => stringify!($ty) ),*
                }
            }

            /// Whether `tag` names a variant.
            pub fn knows(tag: &str) -> bool {
                matches!(tag, $( stringify!($ty) )|*)
            }
        }

        impl Wire for $name {
            fn encode(&self, encoder: &mut Encoder) {
                match self {
                    $( Self::$variant(value) => value.encode(encoder) ),*
                }
            }

            fn decode(decoder: &mut Decoder<'_>) -> Result<Self> {
                match decoder.peek_tag()? {
                    $( stringify!($ty) => Ok(Self::$variant($ty::decode(decoder)?)), )*
                    other => Err(Error::Invalid(format!(
                        concat!("unknown ", stringify!($name), " {}"),
                        other
                    ))),
                }
            }
        }
    };
}

tagged_union! {
    /// A control operation, the payload of a CONTROL message.
    #[derive(Debug, Clone, PartialEq)]
    pub enum ControlRequest {
        /// See [`FlushCacheReqInput`].
        FlushCache(FlushCacheReqInput),
        /// See [`PauseSchedulerReqInput`].
        PauseScheduler(PauseSchedulerReqInput),
        /// See [`ResumeSchedulerReqInput`].
        ResumeScheduler(ResumeSchedulerReqInput),
        /// See [`IsSchedulerPausedReqInput`].
        IsSchedulerPaused(IsSchedulerPausedReqInput),
        /// See [`UpdateWeightFromDiskReqInput`].
        UpdateWeightFromDisk(UpdateWeightFromDiskReqInput),
        /// See [`UpdateWeightsFromDistributedReqInput`].
        UpdateWeightsFromDistributed(UpdateWeightsFromDistributedReqInput),
        /// See [`UpdateWeightsFromTensorReqInput`].
        UpdateWeightsFromTensor(UpdateWeightsFromTensorReqInput),
        /// See [`InitWeightsUpdateGroupReqInput`].
        InitWeightsUpdateGroup(InitWeightsUpdateGroupReqInput),
        /// See [`DestroyWeightsUpdateGroupReqInput`].
        DestroyWeightsUpdateGroup(DestroyWeightsUpdateGroupReqInput),
        /// See [`UpdateWeightsFromMooncakeReqInput`].
        UpdateWeightsFromMooncake(UpdateWeightsFromMooncakeReqInput),
        /// See [`RebalanceExpertsReqInput`].
        RebalanceExperts(RebalanceExpertsReqInput),
        /// See [`ReleaseMemoryOccupationReqInput`].
        ReleaseMemoryOccupation(ReleaseMemoryOccupationReqInput),
        /// See [`ResumeMemoryOccupationReqInput`].
        ResumeMemoryOccupation(ResumeMemoryOccupationReqInput),
        /// See [`IsSleepingReqInput`].
        IsSleeping(IsSleepingReqInput),
        /// See [`GetInternalStateReq`].
        GetInternalState(GetInternalStateReq),
        /// See [`SetInternalStateReq`].
        SetInternalState(SetInternalStateReq),
        /// See [`ProfileReq`].
        Profile(ProfileReq),
    }
}

tagged_union! {
    /// A control operation's reply.
    #[derive(Debug, Clone, PartialEq)]
    pub enum ControlOutput {
        /// See [`FlushCacheReqOutput`].
        FlushCache(FlushCacheReqOutput),
        /// See [`PauseSchedulerReqOutput`].
        PauseScheduler(PauseSchedulerReqOutput),
        /// See [`ResumeSchedulerReqOutput`].
        ResumeScheduler(ResumeSchedulerReqOutput),
        /// See [`IsSchedulerPausedReqOutput`].
        IsSchedulerPaused(IsSchedulerPausedReqOutput),
        /// See [`UpdateWeightFromDiskReqOutput`].
        UpdateWeightFromDisk(UpdateWeightFromDiskReqOutput),
        /// See [`UpdateWeightsFromDistributedReqOutput`].
        UpdateWeightsFromDistributed(UpdateWeightsFromDistributedReqOutput),
        /// See [`UpdateWeightsFromTensorReqOutput`].
        UpdateWeightsFromTensor(UpdateWeightsFromTensorReqOutput),
        /// See [`InitWeightsUpdateGroupReqOutput`].
        InitWeightsUpdateGroup(InitWeightsUpdateGroupReqOutput),
        /// See [`DestroyWeightsUpdateGroupReqOutput`].
        DestroyWeightsUpdateGroup(DestroyWeightsUpdateGroupReqOutput),
        /// See [`UpdateWeightsFromMooncakeReqOutput`].
        UpdateWeightsFromMooncake(UpdateWeightsFromMooncakeReqOutput),
        /// See [`RebalanceExpertsReqOutput`].
        RebalanceExperts(RebalanceExpertsReqOutput),
        /// See [`ReleaseMemoryOccupationReqOutput`].
        ReleaseMemoryOccupation(ReleaseMemoryOccupationReqOutput),
        /// See [`ResumeMemoryOccupationReqOutput`].
        ResumeMemoryOccupation(ResumeMemoryOccupationReqOutput),
        /// See [`IsSleepingReqOutput`].
        IsSleeping(IsSleepingReqOutput),
        /// See [`GetInternalStateReqOutput`].
        GetInternalState(GetInternalStateReqOutput),
        /// See [`SetInternalStateReqOutput`].
        SetInternalState(SetInternalStateReqOutput),
        /// See [`ProfileReqOutput`].
        Profile(ProfileReqOutput),
    }
}

impl ControlRequest {
    /// The tag of the reply this operation gets.
    pub fn reply_tag(&self) -> &'static str {
        match self {
            Self::FlushCache(_) => "FlushCacheReqOutput",
            Self::PauseScheduler(_) => "PauseSchedulerReqOutput",
            Self::ResumeScheduler(_) => "ResumeSchedulerReqOutput",
            Self::IsSchedulerPaused(_) => "IsSchedulerPausedReqOutput",
            Self::UpdateWeightFromDisk(_) => "UpdateWeightFromDiskReqOutput",
            Self::UpdateWeightsFromDistributed(_) => "UpdateWeightsFromDistributedReqOutput",
            Self::UpdateWeightsFromTensor(_) => "UpdateWeightsFromTensorReqOutput",
            Self::InitWeightsUpdateGroup(_) => "InitWeightsUpdateGroupReqOutput",
            Self::DestroyWeightsUpdateGroup(_) => "DestroyWeightsUpdateGroupReqOutput",
            Self::UpdateWeightsFromMooncake(_) => "UpdateWeightsFromMooncakeReqOutput",
            Self::RebalanceExperts(_) => "RebalanceExpertsReqOutput",
            Self::ReleaseMemoryOccupation(_) => "ReleaseMemoryOccupationReqOutput",
            Self::ResumeMemoryOccupation(_) => "ResumeMemoryOccupationReqOutput",
            Self::IsSleeping(_) => "IsSleepingReqOutput",
            Self::GetInternalState(_) => "GetInternalStateReqOutput",
            Self::SetInternalState(_) => "SetInternalStateReqOutput",
            Self::Profile(_) => "ProfileReqOutput",
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::codec::{decode_frames, encode_frames};

    #[test]
    fn a_reply_tag_names_a_reply() {
        let requests = [
            ControlRequest::FlushCache(FlushCacheReqInput {
                rid: None,
                http_worker_ipc: None,
            }),
            ControlRequest::PauseScheduler(PauseSchedulerReqInput {
                rid: None,
                http_worker_ipc: None,
                mode: "wait".to_owned(),
            }),
        ];
        for request in requests {
            assert!(ControlOutput::knows(request.reply_tag()));
            assert!(ControlRequest::knows(request.tag()));
            let decoded: ControlRequest = decode_frames(&encode_frames(&request)).unwrap();
            assert_eq!(decoded, request);
        }
        assert!(!ControlOutput::knows("BatchTokenIDOutSlim"));
    }
}