[Core] M0 foundation: workspace, Lean 4 SlabRing/PrefixState contracts, checked safetensors/cache IO, serve proto, telemetry, and HIP ABI

This commit is contained in:
adm committed 2026-11-29 21:32:44 +01:00
commit 63b1668a92
25 files changed
+2991

No files matched your search

+105
View File
@@ -0,0 +1,105 @@
//! `vetta-hip`: thin HIP microkernel boundary & fence-guarded zero-copy UMA ring buffers.
//!
//! All unsafe GPU/DMA lifetime invariants are mediated by `vetta_core::slab::SlabRing`
//! (formally verified in `proofs/slab_ring_dma.lean`).
use vetta_core::slab::{SlabError, SlabRing, SlotState};
#[derive(Debug, Clone)]
pub struct DeviceInfo {
pub name: String,
pub gcn_arch: String,
pub cu_count: u32,
pub wave_size: u32,
pub total_vram_bytes: u64,
pub is_uma: bool,
}
/// Page-aligned host/UMA ring backing `SlabRing` transitions.
/// In Milestone 2 this wraps `mmap` + `hipHostRegisterMapped` + `io_uring` fixed buffers;
/// in Milestone 0/tests it runs directly on CPU aligned buffers with the exact same state machine.
pub struct UmaSlabPool {
ring: SlabRing,
buffers: Vec<Vec<u8>>,
next_req_id: u64,
next_fence_id: u64,
}
impl UmaSlabPool {
pub fn new(num_slots: usize, slot_bytes: usize) -> Self {
let mut buffers = Vec::with_capacity(num_slots);
for _ in 0..num_slots {
buffers.push(vec![0u8; slot_bytes]);
}
Self {
ring: SlabRing::new(num_slots, slot_bytes),
buffers,
next_req_id: 1,
next_fence_id: 1,
}
}
pub fn slot_state(&self, idx: usize) -> Result<SlotState, SlabError> {
self.ring.state(idx)
}
/// Acquires a free slot and enters `IoInFlight`, returning `(slot_idx, req_id, &mut [u8])`.
/// No GPU kernel can read this slot until `complete_io` transitions it to `GpuReady`.
pub fn begin_io(&mut self) -> Result<(usize, u64, &mut [u8]), SlabError> {
let idx = self.ring.find_free().ok_or(SlabError::NoFreeSlot)?;
let req_id = self.next_req_id;
self.next_req_id += 1;
self.ring.submit_io(idx, req_id)?;
Ok((idx, req_id, self.buffers[idx].as_mut_slice()))
}
pub fn complete_io(&mut self, idx: usize, req_id: u64, tag: u64) -> Result<(), SlabError> {
self.ring.complete_io(idx, req_id, tag)
}
pub fn abort_io(&mut self, idx: usize, req_id: u64) -> Result<(), SlabError> {
self.ring.abort_io(idx, req_id)
}
/// Transitions a `GpuReady` slot to `GpuInUse` and returns `(fence_id, &[u8])`.
/// The slot cannot be reused for DMA until `complete_gpu(idx, fence_id)` is called.
pub fn launch_gpu(&mut self, idx: usize, expected_tag: u64) -> Result<(u64, &[u8]), SlabError> {
let fence = self.next_fence_id;
self.next_fence_id += 1;
self.ring.launch_gpu(idx, expected_tag, fence)?;
Ok((fence, self.buffers[idx].as_slice()))
}
pub fn complete_gpu(&mut self, idx: usize, fence: u64) -> Result<(), SlabError> {
self.ring.complete_gpu(idx, fence)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn uma_slab_pool_enforces_dma_gpu_exclusion() {
let mut pool = UmaSlabPool::new(2, 4096);
let (s0, req0, buf0) = pool.begin_io().unwrap();
buf0[0] = 0xAB;
// Cannot launch GPU before IO completes
assert!(pool.launch_gpu(s0, 77).is_err());
pool.complete_io(s0, req0, 77).unwrap();
let (fence, ro) = pool.launch_gpu(s0, 77).unwrap();
assert_eq!(ro[0], 0xAB);
// Slot 0 is GpuInUse so begin_io must hand out Slot 1
let (s1, req1, _) = pool.begin_io().unwrap();
assert_eq!(s1, 1);
// Pool is now full (s0=GpuInUse, s1=IoInFlight)
assert_eq!(pool.begin_io().unwrap_err(), SlabError::NoFreeSlot);
pool.complete_gpu(s0, fence).unwrap();
assert_eq!(pool.slot_state(s0).unwrap(), SlotState::Free);
pool.abort_io(s1, req1).unwrap();
assert_eq!(pool.slot_state(s1).unwrap(), SlotState::Free);
}
}