Files
leleka/crates/native-cpu-expert/src/rust_general_pool.rs
T

125 lines
3.3 KiB
Rust

//! Original seven-mode ExpertPool with lifetime-bounded Rust job adapters.
//! Each raw destination write is one disjoint claimed row; no whole mutable
//! output slices are shared. Batch operands remain borrowed until done AND park.
use super::rust_ggml::NativeFormat;
use super::rust_legacy::{PlanarActivation, SingleScratch};
use super::rust_native_general::NativeActivation;
use super::rust_topology::{CpuTopology, PoolAffinity};
use super::{Error, FF, H, Result};
use std::{
cell::UnsafeCell,
sync::{
Arc, Condvar, Mutex, OnceLock,
atomic::{AtomicBool, AtomicI32, AtomicI64, AtomicU32, AtomicU64, Ordering},
},
thread,
time::{Duration, Instant},
};
pub struct PlanarJob<'a> {
pub blob: &'a [u8],
pub activation: &'a PlanarActivation,
pub output: &'a mut [f32],
}
pub struct PlanarJobMulti<'a> {
pub blob: &'a [u8],
pub activation: &'a [&'a PlanarActivation],
pub output: &'a mut [&'a mut [f32]],
}
pub struct NativeJobMulti<'a> {
pub blob: &'a [u8],
pub activation: &'a [&'a NativeActivation],
pub planar: &'a [&'a PlanarActivation],
pub output: &'a mut [&'a mut [f32]],
}
#[derive(Clone, Copy)]
struct Job {
blob: *const u8,
len: usize,
nt: usize,
a: [*const PlanarActivation; 8],
na: [*const NativeActivation; 8],
out: [*mut f32; 8],
}
#[repr(align(64))]
struct Line<T>(T);
struct Split {
ff: [[f32; FF]; 8],
a2: [PlanarActivation; 8],
hq: [[u8; 1024]; 8],
}
// `hq` rows are reinterpreted as Q8Zero/Q8One (align 2) by rust_ggml::{quantize_raw,dot_raw}; pin the layout.
const _: () = assert!(std::mem::offset_of!(Split, hq) % 4 == 0);
#[repr(C, align(64))]
struct SingleSplit {
ff: [f32; FF],
a2: PlanarActivation,
}
struct Batch {
jobs: Vec<Job>,
format: Option<NativeFormat>,
mode: u32,
rows: usize,
tasks: usize,
parts: usize,
}
struct Shared {
head: Line<AtomicU64>,
done: Line<AtomicU32>,
parked: Line<AtomicU32>,
epoch: Line<AtomicU32>,
stop: Line<AtomicBool>,
sleepers: Line<AtomicU32>,
sleep_mu: Mutex<()>,
sleep_cv: Condvar,
spin: Duration,
n: usize,
batch: UnsafeCell<Batch>,
split: Vec<UnsafeCell<Split>>,
scratch: Vec<UnsafeCell<SingleScratch>>,
single: Vec<UnsafeCell<SingleSplit>>,
states: Vec<AtomicI32>,
host: AtomicI32,
host_ms: AtomicI64,
}
static RELEASE_GPU_WAITS: OnceLock<fn()> = OnceLock::new();
#[derive(Clone, Debug)]
pub struct Diagnostic {
pub epoch: u32,
pub batch_epoch: u32,
pub claimed: u16,
pub jobs: u16,
pub done: u32,
pub parked: u32,
pub sleeping: u32,
pub mode: u32,
pub workers: Vec<i32>,
pub host: i32,
pub host_for_ms: i64,
}
pub struct ExpertPool {
shared: Arc<Shared>,
threads: Vec<thread::JoinHandle<()>>,
pub topology: CpuTopology,
pub affinity: PoolAffinity,
pub host_works: bool,
pub ms_multi_gu: f64,
pub ms_multi_q: f64,
pub ms_multi_down: f64,
pub multi_bytes: u64,
phase: [f64; 3],
}
mod impl_default_for_job;
#[allow(unused_imports)]
use impl_default_for_job::alloc_with;
#[allow(unused_imports)]
use impl_default_for_job::cells;
#[allow(unused_imports)]
pub use impl_default_for_job::install_release_gpu_waits;
#[allow(unused_imports)]
use impl_default_for_job::now_ms;
mod impl_expertpool;
mod impl_expertpool_2;
mod impl_shared;
mod impl_shared_2;