diff --git a/crates/experimental-ref/src/remote_experts.rs b/crates/experimental-ref/src/remote_experts.rs index 2455af3c..d0605e92 100644 --- a/crates/experimental-ref/src/remote_experts.rs +++ b/crates/experimental-ref/src/remote_experts.rs @@ -348,59 +348,9 @@ pub fn dispatch( Ok(d) } -/// The device image of a dispatch (`struct RemoteMeta`): group device pointers (`device_slot(slot)`, supplied -/// by the cache), starts, destinations, token indices and the group count. Unused tails are zero. -pub struct RemoteMeta; - -impl RemoteMeta { - pub fn to_bytes(d: &Dispatch, device_slot: &dyn Fn(i32) -> u64) -> [u8; META_BYTES] { - let mut b = [0u8; META_BYTES]; - for (g, grp) in d.groups.iter().enumerate() { - b[META_PTR_OFFSET + g * 8..][..8].copy_from_slice(&device_slot(grp.slot).to_le_bytes()); - } - for (i, s) in d.start.iter().enumerate() { - b[META_START_OFFSET + i * 4..][..4].copy_from_slice(&s.to_le_bytes()); - } - for (i, v) in d.dst.iter().enumerate() { - b[META_DST_OFFSET + i * 4..][..4].copy_from_slice(&v.to_le_bytes()); - } - for (i, v) in d.tok.iter().enumerate() { - b[META_TOK_OFFSET + i * 4..][..4].copy_from_slice(&v.to_le_bytes()); - } - b[META_COUNT_OFFSET..][..4].copy_from_slice(&(d.groups.len() as i32).to_le_bytes()); - b - } -} - -/// `RemoteExperts::finish`'s copy: compact row `i` of `h_out` returns to routed row `original_row[i]` of `out` -/// (`row_len` floats each; `H` in the engine). -pub fn scatter(out: &mut [f32], h_out: &[f32], original_row: &[i32], row_len: usize) { - for (i, &r) in original_row.iter().enumerate() { - out[r as usize * row_len..][..row_len].copy_from_slice(&h_out[i * row_len..][..row_len]); - } -} - -/// Counters the engine reports per request. -#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] -pub struct Totals { - pub computed: u64, - pub launched_layers: u64, - pub returned_bytes: u64, - pub full_row_bytes: u64, -} - -impl Totals { - /// After a `begin` that launched (`groups` non-empty). - pub fn record(&mut self, d: &Dispatch) { - self.computed += d.computed; - if !d.is_empty() { - self.launched_layers += 1; - self.returned_bytes += d.returned_bytes(); - self.full_row_bytes += d.full_row_bytes(); - } - } -} - +mod device_image; +#[allow(unused_imports)] +pub use device_image::*; mod dispatch_groups; use dispatch_groups::{assign_groups, pack_groups, pick_unclaimed}; diff --git a/crates/experimental-ref/src/remote_experts/device_image.rs b/crates/experimental-ref/src/remote_experts/device_image.rs new file mode 100644 index 00000000..c88599fa --- /dev/null +++ b/crates/experimental-ref/src/remote_experts/device_image.rs @@ -0,0 +1,57 @@ +//! The device image of a dispatch, the copy back of its compact rows and the per-request counters. + +#[allow(unused_imports)] +use super::*; + +/// The device image of a dispatch (`struct RemoteMeta`): group device pointers (`device_slot(slot)`, supplied +/// by the cache), starts, destinations, token indices and the group count. Unused tails are zero. +pub struct RemoteMeta; + +impl RemoteMeta { + pub fn to_bytes(d: &Dispatch, device_slot: &dyn Fn(i32) -> u64) -> [u8; META_BYTES] { + let mut b = [0u8; META_BYTES]; + for (g, grp) in d.groups.iter().enumerate() { + b[META_PTR_OFFSET + g * 8..][..8].copy_from_slice(&device_slot(grp.slot).to_le_bytes()); + } + for (i, s) in d.start.iter().enumerate() { + b[META_START_OFFSET + i * 4..][..4].copy_from_slice(&s.to_le_bytes()); + } + for (i, v) in d.dst.iter().enumerate() { + b[META_DST_OFFSET + i * 4..][..4].copy_from_slice(&v.to_le_bytes()); + } + for (i, v) in d.tok.iter().enumerate() { + b[META_TOK_OFFSET + i * 4..][..4].copy_from_slice(&v.to_le_bytes()); + } + b[META_COUNT_OFFSET..][..4].copy_from_slice(&(d.groups.len() as i32).to_le_bytes()); + b + } +} + +/// `RemoteExperts::finish`'s copy: compact row `i` of `h_out` returns to routed row `original_row[i]` of `out` +/// (`row_len` floats each; `H` in the engine). +pub fn scatter(out: &mut [f32], h_out: &[f32], original_row: &[i32], row_len: usize) { + for (i, &r) in original_row.iter().enumerate() { + out[r as usize * row_len..][..row_len].copy_from_slice(&h_out[i * row_len..][..row_len]); + } +} + +/// Counters the engine reports per request. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub struct Totals { + pub computed: u64, + pub launched_layers: u64, + pub returned_bytes: u64, + pub full_row_bytes: u64, +} + +impl Totals { + /// After a `begin` that launched (`groups` non-empty). + pub fn record(&mut self, d: &Dispatch) { + self.computed += d.computed; + if !d.is_empty() { + self.launched_layers += 1; + self.returned_bytes += d.returned_bytes(); + self.full_row_bytes += d.full_row_bytes(); + } + } +} diff --git a/crates/gptoss/src/prefill.rs b/crates/gptoss/src/prefill.rs index 51a45ce9..5f63840d 100644 --- a/crates/gptoss/src/prefill.rs +++ b/crates/gptoss/src/prefill.rs @@ -77,11 +77,11 @@ impl Model { /// returns the hidden state of the last token after storing every layer's keys and values; the output /// projection and the greedy choice run here ([`Model::next_from_hidden`]). `progress` receives the /// number of prompt tokens finished (reused ones included) and the total. - pub fn prefill_reuse_accelerated( + pub fn prefill_reuse_accelerated( &self, state: &mut State, prompt: &[u32], - accelerate: PrefillOffload, + accelerate: PrefillOffload, progress: &mut dyn FnMut(usize, usize) -> bool, ) -> Result, ModelError> { let PrefillOffload { accelerator, min_tokens } = accelerate; diff --git a/crates/gptoss/src/state_and_cache.rs b/crates/gptoss/src/state_and_cache.rs index ad4dd0dc..8b992e0c 100644 --- a/crates/gptoss/src/state_and_cache.rs +++ b/crates/gptoss/src/state_and_cache.rs @@ -124,8 +124,8 @@ impl LayerCache for State { /// An accelerator and the number of prompt tokens to compute from which it is used (see /// [`Model::prefill_reuse_accelerated`]). -pub struct PrefillOffload<'a> { - pub accelerator: &'a mut dyn PrefillAccelerator, +pub struct PrefillOffload<'a, A: PrefillAccelerator + ?Sized> { + pub accelerator: &'a mut A, pub min_tokens: usize, }