STRA-338 params h02: file expert source arena fill, ranking and exchange copy take named structs

This commit is contained in:
Pavlo Ratushnyi
2026-11-08 01:20:43 +01:00
parent 68aabec645
commit c9d42e565b
5 changed files with 34 additions and 22 deletions
@@ -180,7 +180,7 @@ impl OriginalFileExpertSource<'_> {
let copies = stream.with_enqueue(|h| {
for (x, b, capacity) in candidates {
let b = b.checked_subspan(*capacity, 0, x.bytes)?;
api.exchange_copy(arena, x.q * self.maximum, b, x.bytes, h)?;
api.exchange_copy(arena, abi::ArenaSpan { offset: x.q * self.maximum, bytes: x.bytes }, b, h)?;
}
Ok(())
});
@@ -17,6 +17,19 @@ pub(super) struct Layout {
pub(super) physical: usize,
}
/// The ranked experts placed so far: each one's arena offset and the byte end of the last placement.
pub(super) struct RankedPlacement {
offsets: Vec<usize>,
end: usize,
}
/// What `fill_arena` copies into the arena: the layout, the residency options and the stack size of the fill workers.
pub(super) struct ArenaFillPlan<'a> {
pub(super) layout: &'a Layout,
pub(super) options: OriginalResidentOptions,
pub(super) stack: usize,
}
/// Marks the source failed and poisons the context (disjoint fields, so a live `api` borrow stays valid).
pub(super) fn poison_parts(failed: &mut bool, context: &Context<'_>) {
*failed = true;
@@ -160,19 +173,18 @@ impl<'ctx> OriginalFileExpertSource<'ctx> {
if layout.budget > (room as u64) {
layout.budget = room.saturating_sub(256 << 20) as u64;
}
let mut ranked = vec_filled(N, NONE)?;
let mut at = 0usize;
let mut placement = RankedPlacement { offsets: vec_filled(N, NONE)?, end: 0 };
let mut held = 0;
for &entry in rank.unwrap_or_default() {
if self.rank_one(entry, &layout.offsets, &mut ranked, &mut at, layout.budget)? {
if self.rank_one(entry, &layout.offsets, &mut placement, layout.budget)? {
held += 1;
}
}
if options.budget_bytes == u64::MAX && held == 0 {
return Err(Error::BudgetExceeded);
}
layout.offsets = ranked;
layout.bytes = at;
layout.offsets = placement.offsets;
layout.bytes = placement.end;
layout.lend = None;
Ok(())
}
@@ -181,18 +193,18 @@ impl<'ctx> OriginalFileExpertSource<'ctx> {
fn rank_one(
&self,
(l, e): (i32, i32),
offsets: &[usize],
ranked: &mut [usize],
at: &mut usize,
planned_offsets: &[usize],
placement: &mut RankedPlacement,
budget: u64,
) -> Result<bool> {
let RankedPlacement { offsets: ranked, end: at } = placement;
if l < 0 || e < 0 {
return Ok(false);
}
let Ok(i) = key(l as usize, e as usize) else {
return Ok(false);
};
if offsets[i] == NONE || ranked[i] != NONE {
if planned_offsets[i] == NONE || ranked[i] != NONE {
return Ok(false);
}
let b = self.layer_bytes[l as usize];
@@ -280,14 +292,8 @@ impl<'ctx> OriginalFileExpertSource<'ctx> {
}
/// Pins the arena as asked, then copies every planned expert from the mapped source with worker threads.
pub(super) fn fill_arena(
&self,
api: &abi::Api,
arena: &mut abi::Arena,
layout: &Layout,
options: OriginalResidentOptions,
stack: usize,
) -> Result<()> {
pub(super) fn fill_arena(&self, api: &abi::Api, arena: &mut abi::Arena, plan: ArenaFillPlan<'_>) -> Result<()> {
let ArenaFillPlan { layout, options, stack } = plan;
if options.pin && arena.pin_limit() == 0 {
if layout.budget > 0 && options.partial_pin {
self.pin_partial(api, arena, layout, options)?;
@@ -1,7 +1,7 @@
//! Moved out of the parent module by STRA-338: impl OriginalFileExpertSource (2).
#[allow(unused_imports)]
use super::build_phases::{alloc_failed, discard_arena, leak_arena, poison_parts};
use super::build_phases::{ArenaFillPlan, alloc_failed, discard_arena, leak_arena, poison_parts};
use super::*;
impl<'ctx> OriginalFileExpertSource<'ctx> {
@@ -24,7 +24,7 @@ impl<'ctx> OriginalFileExpertSource<'ctx> {
Ok(a) => a,
Err(primary) => return alloc_failed(&mut self.failed, self.context, guard, primary),
};
let prepare = self.fill_arena(api, &mut arena, &layout, options, stack);
let prepare = self.fill_arena(api, &mut arena, ArenaFillPlan { layout: &layout, options, stack });
let pop = guard.finish();
if pop.is_err() {
return leak_arena(&mut self.failed, self.context, arena, combine(prepare, pop));
@@ -20,6 +20,12 @@ pub(crate) struct Api {
alias: Alias,
dtoh: DtoH,
}
/// A byte range inside an `Arena`: where a copy lands and how long it is.
#[derive(Clone, Copy)]
pub(crate) struct ArenaSpan {
pub(crate) offset: usize,
pub(crate) bytes: usize,
}
/// Raw allocation is not readable until every retained fill job has joined.
/// No Drop CUDA call without a genuine entered Context; actor closes explicitly.
pub(crate) struct Arena {
@@ -126,11 +126,11 @@ impl Api {
pub(crate) fn exchange_copy(
&self,
arena: &Arena,
offset: usize,
span: ArenaSpan,
source: super::super::BufferHandle,
bytes: usize,
stream: usize,
) -> Result<()> {
let ArenaSpan { offset, bytes } = span;
source.validate_extent(bytes)?;
if bytes == 0 || offset.checked_add(bytes).is_none_or(|n| n > arena.bytes) || arena.address == 0 {
return Err(Error::InvalidSize);