fix(app): the game placement reads the card again and stage retries start over with each new wish
- the game stage reads the card again: other programs as at the last normal placement made with no game running, and a reserve of at least what grew since (the game and programs opened after that reading); with a game already running at the start (game detection now looks once before the LLM starts) it counts what the card holds now and leaves only the room for other programs, so it no longer pauses for counting the game twice - the retry count starts over whenever another stage is wanted, also when the game ends with the normal stage already running, so the next game tries again - the wait before the next try counts from when the failed switch and its way back are over - the worker client keeps, sends and readies a connection under one lock (llm, then state), so an LLM state set while a worker connects is not lost - a game watch dropped by a start that fails part way stops its thread Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -252,9 +252,12 @@ pub struct Llm {
|
||||
stop_timeout: Duration,
|
||||
/// The graphics card's memory (none: Strata places the model itself).
|
||||
gpu: Option<GpuReader>,
|
||||
/// What other programs held at the latest normal placement (no game then): the game stage
|
||||
/// counts other programs from it, as the card then also holds the game, which its reserve is for.
|
||||
/// What other programs held at the latest normal placement made with no game running: the
|
||||
/// game stage counts other programs from it (the card then also holds the game, which its
|
||||
/// reserve is for). None when the services started with a game running.
|
||||
baseline: Mutex<Option<i64>>,
|
||||
/// A game ran when the services started (set before `start`).
|
||||
game_at_start: AtomicBool,
|
||||
/// The STT worker's model is on the card (true at switches while the worker runs), so it is not
|
||||
/// taken for another program's memory.
|
||||
stt_on_card: AtomicBool,
|
||||
@@ -283,10 +286,16 @@ impl Llm {
|
||||
pub fn with_exe(layout: Layout, exe: std::path::PathBuf) -> Self {
|
||||
Self { layout, exe, status: Mutex::new(LlmStatus { state: LlmState::Off, managed: false, percent: None, note: None }),
|
||||
active: Mutex::new(None), cancel: Arc::new(AtomicBool::new(false)), port: Mutex::new(0),
|
||||
stop_timeout: Duration::from_secs(120), gpu: None, baseline: Mutex::new(None), stt_on_card: AtomicBool::new(false), room: Mutex::new(None), threads: Mutex::new(None),
|
||||
stop_timeout: Duration::from_secs(120), gpu: None, baseline: Mutex::new(None), game_at_start: AtomicBool::new(false), stt_on_card: AtomicBool::new(false), room: Mutex::new(None), threads: Mutex::new(None),
|
||||
stage: Mutex::new((Stage::Normal, None)) }
|
||||
}
|
||||
|
||||
/// Whether a game runs as the services start: the start's reading of the card then includes it
|
||||
/// and is not kept as other programs' memory.
|
||||
pub fn game_at_start(&self, on: bool) {
|
||||
self.game_at_start.store(on, Ordering::SeqCst);
|
||||
}
|
||||
|
||||
/// `{stage, gameRoomMib}` for the dashboard.
|
||||
pub fn stage_view(&self) -> Value {
|
||||
let (stage, room) = *self.stage.lock().unwrap();
|
||||
@@ -342,10 +351,12 @@ impl Llm {
|
||||
// from the latest normal placement, as the card then also holds the game.
|
||||
let stt_model = if self.stt_on_card.load(Ordering::SeqCst) { crate::placement::STT_MODEL_MIB } else { 0 };
|
||||
let now_others = (card.used_mib - stt_model).max(0);
|
||||
let others = match stage {
|
||||
Stage::Gaming => *self.baseline.lock().unwrap().get_or_insert(now_others),
|
||||
_ => *self.baseline.lock().unwrap().insert(now_others),
|
||||
};
|
||||
// A normal placement keeps its reading, unless a game ran at the start (its memory would be
|
||||
// taken for other programs'; a later normal placement comes only after the game ended).
|
||||
if stage != Stage::Gaming && !self.game_at_start.swap(false, Ordering::SeqCst) {
|
||||
*self.baseline.lock().unwrap() = Some(now_others);
|
||||
}
|
||||
let baseline = *self.baseline.lock().unwrap();
|
||||
let key = format!("{}/{}", c.context, c.parallel);
|
||||
let file = self.probe_file();
|
||||
let mut all: serde_json::Map<String, Value> = std::fs::read_to_string(&file).ok()
|
||||
@@ -374,9 +385,14 @@ impl Llm {
|
||||
p
|
||||
}
|
||||
};
|
||||
let expert = match stage {
|
||||
Stage::Gaming => {
|
||||
let g = crate::placement::game(card.total_mib, others, &probe, settings.game.reserve_mib());
|
||||
let mut others = now_others;
|
||||
let expert = match (stage, baseline) {
|
||||
// With a reading from before the game: other programs as then, and the reserve at least
|
||||
// what has grown since (the game, and any program opened after that reading).
|
||||
(Stage::Gaming, Some(base)) => {
|
||||
others = base;
|
||||
let reserve = settings.game.reserve_mib().max(now_others - base);
|
||||
let g = crate::placement::game(card.total_mib, base, &probe, reserve);
|
||||
if g.paused {
|
||||
crate::info_log!("llm", "placement: no room for a game beside the model; paused while the game runs");
|
||||
return Ok(None);
|
||||
@@ -384,7 +400,9 @@ impl Llm {
|
||||
*self.room.lock().unwrap() = Some(g.game_room_mib);
|
||||
g.expert_gib
|
||||
}
|
||||
_ => crate::placement::normal(card.total_mib, others, &probe, settings.game.other_room_mib()),
|
||||
// A game that already ran at the start is in what the card holds now: no reserve on
|
||||
// top, only the room for other programs.
|
||||
_ => crate::placement::normal(card.total_mib, now_others, &probe, settings.game.other_room_mib()),
|
||||
};
|
||||
crate::info_log!("llm", "placement ({stage:?}): experts {expert} GiB (card {} MiB, held by others {others} MiB, model without experts {} MiB)",
|
||||
card.total_mib, probe.model_mib);
|
||||
@@ -1029,6 +1047,53 @@ pub(crate) mod tests {
|
||||
assert!(launches(&l).last().unwrap().ends_with(&format!("--set expert-vram={expected} --set prefill=2048")), "{:?}", launches(&l));
|
||||
}
|
||||
|
||||
/// `live_card` on a card of `total` MiB.
|
||||
#[cfg(unix)]
|
||||
fn live_card_of(l: &Layout, others: Arc<std::sync::atomic::AtomicI64>, total: i64) -> GpuReader {
|
||||
let calls = l.data.join("calls");
|
||||
Arc::new(move || {
|
||||
let c = std::fs::read_to_string(&calls).unwrap_or_default();
|
||||
let running = c.lines().last().is_some_and(|x| x.starts_with("launch --family"));
|
||||
Some(Gpu { id: "GPU-test".into(), used_mib: others.load(Ordering::SeqCst) + if running { 9874 } else { 0 }, total_mib: total })
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[tokio::test]
|
||||
async fn a_game_running_when_the_services_start_is_not_counted_twice() {
|
||||
// A game holds 2500 MiB already: it is in what the card holds, so no reserve on top.
|
||||
let l = layout("llm-stage-game-at-start");
|
||||
let port = server_after_launch(&l, "list").await;
|
||||
let others = Arc::new(std::sync::atomic::AtomicI64::new(1460 + 2500));
|
||||
let llm = Llm::with_exe(l.clone(), fake_placed(&l)).with_gpu(live_card_of(&l, others.clone(), 16303));
|
||||
llm.game_at_start(true);
|
||||
let s = parse_settings(&json!({"llm": {"port": port}})).unwrap();
|
||||
llm.start(&s).await.unwrap();
|
||||
assert_eq!(llm.switch(&s, Stage::Gaming, false).await.unwrap(), Stage::Gaming, "not paused");
|
||||
let probe16 = crate::placement::Probe { context: 16384, parallel: 1, gpu: "GPU-test".into(), model_mib: 9874, staging_gib: 1.1 };
|
||||
let expected = crate::placement::normal(16303, 1460 + 2500, &probe16, 1024);
|
||||
assert!(launches(&l).last().unwrap().contains(&format!("--set expert-vram={expected} ")), "{:?}", launches(&l));
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[tokio::test]
|
||||
async fn the_game_stage_reads_the_card_again_and_counts_what_grew_since_the_normal_reading() {
|
||||
// Normal reading: 1460 MiB. Then a program takes 4000 MiB and the game 4500 MiB: together
|
||||
// more than the 8 GB reserve, so the placement leaves that much, not just 8 GB.
|
||||
let l = layout("llm-stage-grew");
|
||||
let port = server_after_launch(&l, "list").await;
|
||||
let others = Arc::new(std::sync::atomic::AtomicI64::new(1460));
|
||||
let llm = Llm::with_exe(l.clone(), fake_placed(&l)).with_gpu(live_card_of(&l, others.clone(), 24576));
|
||||
let s = parse_settings(&json!({"llm": {"port": port}})).unwrap();
|
||||
llm.start(&s).await.unwrap();
|
||||
others.store(1460 + 4000 + 4500, Ordering::SeqCst);
|
||||
llm.switch(&s, Stage::Gaming, false).await.unwrap();
|
||||
let probe16 = crate::placement::Probe { context: 16384, parallel: 1, gpu: "GPU-test".into(), model_mib: 9874, staging_gib: 1.1 };
|
||||
let expected = crate::placement::game(24576, 1460, &probe16, 8500).expert_gib;
|
||||
assert_ne!(expected, crate::placement::game(24576, 1460, &probe16, 8192).expert_gib, "the test tells them apart");
|
||||
assert!(launches(&l).last().unwrap().contains(&format!("--set expert-vram={expected} ")), "{:?}", launches(&l));
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[tokio::test]
|
||||
async fn a_game_stage_on_a_fallback_placement_still_shows_as_the_game_stage() {
|
||||
|
||||
Reference in New Issue
Block a user