vyre-runtime 0.6.1

Persistent megakernel + io_uring zero-copy streaming runtime for vyre - GPU as VIR0 bytecode interpreter
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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
use super::*;
use crate::megakernel::protocol::{
    slot, ARG0_WORD, ARGS_PER_SLOT, OPCODE_WORD, PRIORITY_WORD, SLOT_WORDS, STATUS_WORD,
    TENANT_WORD,
};
use crate::megakernel::scheduler;
use crate::megakernel::MegakernelWorkItem;

#[test]
fn encode_control_produces_aligned_buffer() {
    let buf = Megakernel::encode_control(false, 1, 4).unwrap();
    assert!(
        buf.len() % 4 == 0,
        "control buffer must be u32-word aligned"
    );
    assert!(
        !buf.is_empty(),
        "control buffer must have at least the fixed header"
    );
}

#[test]
fn encode_control_with_shutdown_sets_flag() {
    let buf = Megakernel::encode_control(true, 1, 0).unwrap();
    // The shutdown word should be non-zero.
    let shutdown_word = u32::from_le_bytes([
        buf[protocol::control::SHUTDOWN as usize * 4],
        buf[protocol::control::SHUTDOWN as usize * 4 + 1],
        buf[protocol::control::SHUTDOWN as usize * 4 + 2],
        buf[protocol::control::SHUTDOWN as usize * 4 + 3],
    ]);
    assert_ne!(shutdown_word, 0, "shutdown flag must be set");
}

#[test]
fn try_encode_control_delegates_to_encode_control() {
    let a = Megakernel::encode_control(false, 2, 8).unwrap();
    let b = Megakernel::try_encode_control(false, 2, 8).unwrap();
    assert_eq!(a, b, "try_encode_control must produce identical output");
}

#[test]
fn encode_into_reuses_and_zeroes_protocol_buffers() {
    let mut control = vec![0xAA; 4096];
    let control_capacity = control.capacity();
    Megakernel::try_encode_control_into(false, 2, 8, &mut control).unwrap();
    assert_eq!(control.capacity(), control_capacity);
    assert_eq!(
        control,
        Megakernel::try_encode_control(false, 2, 8).unwrap()
    );

    let mut ring = vec![0xAA; 4096];
    let ring_capacity = ring.capacity();
    Megakernel::try_encode_empty_ring_into(4, &mut ring).unwrap();
    assert_eq!(ring.capacity(), ring_capacity);
    assert_eq!(ring, Megakernel::try_encode_empty_ring(4).unwrap());

    let mut debug_log = vec![0xAA; 4096];
    let debug_capacity = debug_log.capacity();
    Megakernel::try_encode_empty_debug_log_into(4, &mut debug_log).unwrap();
    assert_eq!(debug_log.capacity(), debug_capacity);
    assert_eq!(
        debug_log,
        Megakernel::try_encode_empty_debug_log(4).unwrap()
    );
}

#[test]
fn encode_empty_ring_respects_slot_count() {
    let buf = Megakernel::encode_empty_ring(16).unwrap();
    let expected_bytes = 16 * SLOT_WORDS as usize * 4;
    assert_eq!(
        buf.len(),
        expected_bytes,
        "ring must be slot_count * SLOT_WORDS * 4 bytes"
    );
}

#[test]
fn encode_empty_ring_zero_slots() {
    let buf = Megakernel::encode_empty_ring(0).unwrap();
    assert!(buf.is_empty(), "0 slots must produce empty buffer");
}

#[test]
fn publish_slot_writes_and_reads_back() {
    let mut ring = Megakernel::encode_empty_ring(4).unwrap();
    Megakernel::publish_slot(&mut ring, 0, 42, protocol::opcode::STORE_U32, &[100, 200]).unwrap();

    // Verify status is PUBLISHED.
    let status = read_word(&ring, 0, STATUS_WORD as usize);
    assert_eq!(status, slot::PUBLISHED);

    // Verify opcode.
    let op = read_word(&ring, 0, OPCODE_WORD as usize);
    assert_eq!(op, protocol::opcode::STORE_U32);

    // Verify tenant.
    let tenant = read_word(&ring, 0, TENANT_WORD as usize);
    assert_eq!(tenant, 42);

    let priority = read_word(&ring, 0, PRIORITY_WORD as usize);
    assert_eq!(priority, scheduler::priority::NORMAL);

    // Verify args.
    let a0 = read_word(&ring, 0, ARG0_WORD as usize);
    let a1 = read_word(&ring, 0, ARG0_WORD as usize + 1);
    assert_eq!(a0, 100);
    assert_eq!(a1, 200);
}

#[test]
fn publish_slot_rejects_inflight_slot() {
    let mut ring = Megakernel::encode_empty_ring(4).unwrap();
    // Publish once (now status = PUBLISHED).
    Megakernel::publish_slot(&mut ring, 0, 1, protocol::opcode::STORE_U32, &[1]).unwrap();
    // Try to publish again  -  slot is PUBLISHED (not EMPTY/DONE).
    let err = Megakernel::publish_slot(&mut ring, 0, 1, protocol::opcode::STORE_U32, &[2])
        .expect_err("must reject publishing to an in-flight slot");
    assert!(
        err.to_string().contains("not publishable"),
        "unexpected error: {err}"
    );
}

#[test]
fn publish_slot_rejects_out_of_bounds() {
    let mut ring = Megakernel::encode_empty_ring(2).unwrap();
    let err = Megakernel::publish_slot(&mut ring, 99, 1, protocol::opcode::STORE_U32, &[1])
        .expect_err("must reject slot_idx beyond ring capacity");
    assert!(
        err.to_string().contains("slot_idx exceeds ring slot count"),
        "unexpected error: {err}"
    );
}

#[test]
fn publish_slot_rejects_too_many_args() {
    let mut ring = Megakernel::encode_empty_ring(2).unwrap();
    let too_many = vec![0u32; ARGS_PER_SLOT as usize + 1];
    let err = Megakernel::publish_slot(&mut ring, 0, 1, protocol::opcode::STORE_U32, &too_many)
        .expect_err("must reject args exceeding ARGS_PER_SLOT");
    assert!(
        err.to_string().contains("too many args for one slot"),
        "unexpected error: {err}"
    );
}

#[test]
fn publish_slot_allows_republish_after_done() {
    let mut ring = Megakernel::encode_empty_ring(4).unwrap();
    // Publish, then manually mark as DONE.
    Megakernel::publish_slot(&mut ring, 0, 1, protocol::opcode::STORE_U32, &[1]).unwrap();
    write_word(&mut ring, 0, STATUS_WORD as usize, slot::DONE);
    // Should succeed  -  DONE slots are recyclable.
    Megakernel::publish_slot(&mut ring, 0, 1, protocol::opcode::ATOMIC_ADD, &[2]).unwrap();
    let op = read_word(&ring, 0, OPCODE_WORD as usize);
    assert_eq!(op, protocol::opcode::ATOMIC_ADD);
}

#[test]
fn encode_work_items_ring_into_publishes_contiguous_slots() {
    let items = [
        MegakernelWorkItem {
            op_handle: protocol::opcode::STORE_U32,
            input_handle: 10,
            output_handle: 20,
            param: 30,
        },
        MegakernelWorkItem {
            op_handle: protocol::opcode::ATOMIC_ADD,
            input_handle: 40,
            output_handle: 50,
            param: 60,
        },
    ];
    let mut ring = vec![0xAA; 4096];

    Megakernel::encode_work_items_ring_into(4, 7, &items, &mut ring).unwrap();

    assert_eq!(read_word(&ring, 0, STATUS_WORD as usize), slot::PUBLISHED);
    assert_eq!(
        read_word(&ring, 0, OPCODE_WORD as usize),
        protocol::opcode::STORE_U32
    );
    assert_eq!(read_word(&ring, 0, TENANT_WORD as usize), 7);
    assert_eq!(
        read_word(&ring, 0, PRIORITY_WORD as usize),
        scheduler::priority::NORMAL
    );
    assert_eq!(read_word(&ring, 0, ARG0_WORD as usize), 10);
    assert_eq!(read_word(&ring, 0, ARG0_WORD as usize + 1), 20);
    assert_eq!(read_word(&ring, 0, ARG0_WORD as usize + 2), 30);
    assert_eq!(read_word(&ring, 1, STATUS_WORD as usize), slot::PUBLISHED);
    assert_eq!(
        read_word(&ring, 1, OPCODE_WORD as usize),
        protocol::opcode::ATOMIC_ADD
    );
    assert_eq!(read_word(&ring, 1, ARG0_WORD as usize), 40);
    assert_eq!(read_word(&ring, 1, ARG0_WORD as usize + 1), 50);
    assert_eq!(read_word(&ring, 1, ARG0_WORD as usize + 2), 60);
    assert_eq!(read_word(&ring, 2, STATUS_WORD as usize), slot::EMPTY);
}

#[test]
fn encode_work_items_ring_words_into_matches_byte_encoder() {
    let items = [
        MegakernelWorkItem {
            op_handle: protocol::opcode::STORE_U32,
            input_handle: 10,
            output_handle: 20,
            param: 30,
        },
        MegakernelWorkItem {
            op_handle: protocol::opcode::ATOMIC_ADD,
            input_handle: 40,
            output_handle: 50,
            param: 60,
        },
    ];
    let mut bytes = Vec::new();
    let mut words = Vec::new();

    Megakernel::encode_work_items_ring_into(4, 7, &items, &mut bytes).unwrap();
    Megakernel::encode_work_items_ring_words_into(4, 7, &items, &mut words).unwrap();

    assert_eq!(bytemuck::cast_slice::<u32, u8>(&words), bytes.as_slice());
}

#[test]
fn encode_work_items_ring_words_into_reuses_buffer_by_clearing_status_words() {
    let first = [
        MegakernelWorkItem {
            op_handle: protocol::opcode::STORE_U32,
            input_handle: 10,
            output_handle: 20,
            param: 30,
        },
        MegakernelWorkItem {
            op_handle: protocol::opcode::ATOMIC_ADD,
            input_handle: 40,
            output_handle: 50,
            param: 60,
        },
    ];
    let second = [MegakernelWorkItem {
        op_handle: protocol::opcode::STORE_U32,
        input_handle: 70,
        output_handle: 80,
        param: 90,
    }];
    let mut words = Vec::new();

    Megakernel::encode_work_items_ring_words_into(4, 7, &first, &mut words).unwrap();
    Megakernel::encode_work_items_ring_words_into(4, 7, &second, &mut words).unwrap();

    assert_eq!(
        read_word_words(&words, 0, STATUS_WORD as usize),
        slot::PUBLISHED
    );
    assert_eq!(read_word_words(&words, 0, ARG0_WORD as usize), 70);
    assert_eq!(read_word_words(&words, 0, ARG0_WORD as usize + 1), 80);
    assert_eq!(read_word_words(&words, 0, ARG0_WORD as usize + 2), 90);
    assert_eq!(
        read_word_words(&words, 1, STATUS_WORD as usize),
        slot::EMPTY
    );
    assert_eq!(
        read_word_words(&words, 2, STATUS_WORD as usize),
        slot::EMPTY
    );
    assert_eq!(
        read_word_words(&words, 3, STATUS_WORD as usize),
        slot::EMPTY
    );
}

#[test]
fn publish_work_items_updates_window_without_resetting_unrelated_slots() {
    let mut ring = Megakernel::encode_empty_ring(4).unwrap();
    write_word(&mut ring, 0, ARG0_WORD as usize, 0xDEAD_BEEF);
    write_word(&mut ring, 3, ARG0_WORD as usize, 0xABCD_EF01);
    let items = [
        MegakernelWorkItem {
            op_handle: protocol::opcode::STORE_U32,
            input_handle: 10,
            output_handle: 20,
            param: 30,
        },
        MegakernelWorkItem {
            op_handle: protocol::opcode::ATOMIC_ADD,
            input_handle: 40,
            output_handle: 50,
            param: 60,
        },
    ];

    let published = Megakernel::publish_work_items(&mut ring, 1, 7, &items).unwrap();

    assert_eq!(published, 2);
    assert_eq!(read_word(&ring, 0, ARG0_WORD as usize), 0xDEAD_BEEF);
    assert_eq!(read_word(&ring, 3, ARG0_WORD as usize), 0xABCD_EF01);
    assert_eq!(read_word(&ring, 1, STATUS_WORD as usize), slot::PUBLISHED);
    assert_eq!(
        read_word(&ring, 1, OPCODE_WORD as usize),
        protocol::opcode::STORE_U32
    );
    assert_eq!(read_word(&ring, 1, TENANT_WORD as usize), 7);
    assert_eq!(read_word(&ring, 1, ARG0_WORD as usize), 10);
    assert_eq!(read_word(&ring, 1, ARG0_WORD as usize + 1), 20);
    assert_eq!(read_word(&ring, 1, ARG0_WORD as usize + 2), 30);
    assert_eq!(read_word(&ring, 2, STATUS_WORD as usize), slot::PUBLISHED);
    assert_eq!(
        read_word(&ring, 2, OPCODE_WORD as usize),
        protocol::opcode::ATOMIC_ADD
    );
    assert_eq!(read_word(&ring, 2, ARG0_WORD as usize), 40);
    assert_eq!(read_word(&ring, 2, ARG0_WORD as usize + 1), 50);
    assert_eq!(read_word(&ring, 2, ARG0_WORD as usize + 2), 60);
}

#[test]
fn publish_work_items_rejects_inflight_window_without_mutating() {
    let mut ring = Megakernel::encode_empty_ring(4).unwrap();
    write_word(&mut ring, 1, STATUS_WORD as usize, slot::CLAIMED);
    let before = ring.clone();
    let items = [MegakernelWorkItem {
        op_handle: protocol::opcode::STORE_U32,
        input_handle: 10,
        output_handle: 20,
        param: 30,
    }];

    let error = Megakernel::publish_work_items(&mut ring, 1, 7, &items)
        .expect_err("in-flight target slots must be rejected before mutation");

    assert!(error.to_string().contains("not publishable"));
    assert_eq!(ring, before);
}

#[test]
fn encode_work_items_ring_into_rejects_oversized_queue_without_mutating() {
    let items = [
        MegakernelWorkItem {
            op_handle: protocol::opcode::STORE_U32,
            input_handle: 1,
            output_handle: 2,
            param: 3,
        },
        MegakernelWorkItem {
            op_handle: protocol::opcode::STORE_U32,
            input_handle: 4,
            output_handle: 5,
            param: 6,
        },
    ];
    let mut ring = vec![0xAA; 8];

    let result = Megakernel::encode_work_items_ring_into(1, 0, &items, &mut ring);

    assert!(result.is_err(), "oversized queue must be rejected");
    assert_eq!(ring, vec![0xAA; 8], "rejection must not mutate ring");
}

#[test]
fn encode_work_items_ring_into_rejects_bad_opcode_without_mutating() {
    let items = [MegakernelWorkItem {
        op_handle: protocol::opcode::RESERVED_MAX_RANGE_MIN,
        input_handle: 1,
        output_handle: 2,
        param: 3,
    }];
    let mut ring = vec![0xAA; 8];

    let result = Megakernel::encode_work_items_ring_into(1, 0, &items, &mut ring);

    assert!(result.is_err(), "invalid opcode must be rejected");
    assert_eq!(ring, vec![0xAA; 8], "rejection must not mutate ring");
}

#[test]
fn batch_publish_writes_items_plus_fence() {
    let mut ring = Megakernel::encode_empty_ring(8).unwrap();
    let items: Vec<(u32, Vec<u32>)> = vec![
        (protocol::opcode::STORE_U32, vec![10, 20]),
        (protocol::opcode::ATOMIC_ADD, vec![30, 40]),
    ];
    let slots_used = Megakernel::batch_publish(&mut ring, 0, 1, &items, 99).unwrap();
    // 2 items + 1 fence = 3 slots consumed.
    assert_eq!(slots_used, 3);

    // Last slot should be BATCH_FENCE.
    let fence_op = read_word(&ring, 2, OPCODE_WORD as usize);
    assert_eq!(fence_op, protocol::opcode::BATCH_FENCE);
}

#[test]
fn batch_publish_rejects_fence_collision_without_partial_publish() {
    let mut ring = Megakernel::encode_empty_ring(4).unwrap();
    write_word(&mut ring, 1, STATUS_WORD as usize, slot::PUBLISHED);
    let before = ring.clone();
    let items: Vec<(u32, Vec<u32>)> = vec![(protocol::opcode::STORE_U32, vec![10, 20])];

    let result = Megakernel::batch_publish(&mut ring, 0, 1, &items, 99);

    assert!(result.is_err(), "fence collision must reject the batch");
    assert_eq!(ring, before, "rejection must not publish earlier slots");
}

#[test]
fn read_done_count_starts_at_zero() {
    let control = Megakernel::encode_control(false, 1, 0).unwrap();
    assert_eq!(Megakernel::read_done_count(&control), 0);
}

#[test]
fn read_epoch_starts_at_zero() {
    let control = Megakernel::encode_control(false, 1, 0).unwrap();
    assert_eq!(Megakernel::read_epoch(&control), 0);
}

#[test]
fn encode_empty_debug_log_round_trips() {
    let log = Megakernel::encode_empty_debug_log(32).unwrap();
    let records = Megakernel::read_debug_log(&log);
    assert!(
        records.is_empty(),
        "fresh debug log must contain zero records"
    );
}

#[test]
fn read_metrics_on_fresh_control_returns_empty() {
    let control = Megakernel::encode_control(false, 1, 4).unwrap();
    let metrics = Megakernel::read_metrics(&control);
    assert!(
        metrics.is_empty(),
        "fresh control buffer must have no non-zero metric counters"
    );
}

#[test]
fn validate_control_bytes_rejects_too_short() {
    let err = validate_control_bytes(&[0u8; 4])
        .expect_err("must reject undersized control buffer");
    assert!(
        err.to_string().contains("expected at least"),
        "unexpected error: {err}"
    );
}

#[test]
fn validate_control_bytes_rejects_misaligned() {
    let err = validate_control_bytes(&[0u8; 101])
        .expect_err("must reject non-4-byte-aligned control buffer");
    assert!(
        err.to_string().contains("4-byte alignment"),
        "unexpected error: {err}"
    );
}

#[test]

fn validate_control_bytes_accepts_valid() {
    let control = Megakernel::encode_control(false, 1, 0).unwrap();
    validate_control_bytes(&control).expect("Fix: valid control buffer must pass validation");
}

#[test]
fn validate_debug_log_bytes_rejects_wrong_size() {
    let err = validate_debug_log_bytes(&[0u8; 4]).expect_err("must reject undersized debug log");
    assert!(
        err.to_string().contains("expected exactly"),
        "unexpected error: {err}"
    );
}

#[test]
fn validate_debug_log_bytes_accepts_valid() {
    let log = Megakernel::encode_empty_debug_log(protocol::debug::RECORD_CAPACITY).unwrap();
    validate_debug_log_bytes(&log).expect("Fix: valid debug log must pass validation");
}

#[test]
fn packed_slot_publish_roundtrips() {
    let mut ring = Megakernel::encode_empty_ring(4).unwrap();
    let ops: Vec<(u8, Vec<u32>)> = vec![
        (protocol::opcode::STORE_U32 as u8, vec![10, 20]),
        (protocol::opcode::ATOMIC_ADD as u8, vec![30]),
    ];
    Megakernel::publish_packed_slot(&mut ring, 0, 1, &ops).unwrap();

    let status = read_word(&ring, 0, STATUS_WORD as usize);
    assert_eq!(status, slot::PUBLISHED);

    let op = read_word(&ring, 0, OPCODE_WORD as usize);
    assert_eq!(op, protocol::opcode::PACKED_SLOT);
}

#[test]
fn packed_slot_rejects_overflow() {
    let mut ring = Megakernel::encode_empty_ring(4).unwrap();
    // Each op gets 3 arg words, so 5 ops × 3 args = 15 words > 12 budget.
    let ops: Vec<(u8, Vec<u32>)> = (0..5).map(|i| (i as u8, vec![1, 2, 3])).collect();
    let err = Megakernel::publish_packed_slot(&mut ring, 0, 1, &ops)
        .expect_err("must reject packed slot exceeding arg budget");
    assert!(
        err.to_string().contains("exceeds the 12-word slot argument budget"),
        "unexpected error: {err}"
    );
}

// Helper: read a u32 word from a ring buffer at (slot_idx, word_idx).
fn read_word(ring: &[u8], slot_idx: usize, word_idx: usize) -> u32 {
    let base = slot_idx * SLOT_WORDS as usize * 4;
    let off = base + word_idx * 4;
    u32::from_le_bytes([ring[off], ring[off + 1], ring[off + 2], ring[off + 3]])
}

// Helper: read a native u32 word from a ring-word buffer at (slot_idx, word_idx).
fn read_word_words(ring: &[u32], slot_idx: usize, word_idx: usize) -> u32 {
    ring[slot_idx * SLOT_WORDS as usize + word_idx]
}

// Helper: write a u32 word into a ring buffer at (slot_idx, word_idx).
fn write_word(ring: &mut [u8], slot_idx: usize, word_idx: usize, value: u32) {
    let base = slot_idx * SLOT_WORDS as usize * 4;
    let off = base + word_idx * 4;
    ring[off..off + 4].copy_from_slice(&value.to_le_bytes());
}