super-voice-app-core/src/pages/models.rs
Line | Count | Source |
1 | | // SPDX-License-Identifier: GPL-3.0-only |
2 | | //! The Models page's stage cards: which backend fills each stage, which of |
3 | | //! its models to run and on what, and loading, reloading or unloading it, |
4 | | //! with the download and the load shown as they go. |
5 | | //! |
6 | | //! A card stages a model and a device first, then Load commits both, as the |
7 | | //! app's cards do. What the card shows as running comes from the |
8 | | //! daemon's view of the stage, read again whenever the daemon says a stage |
9 | | //! changed: the daemon is the one that knows. |
10 | | //! |
11 | | //! A load can take minutes (a download first). The daemon reports how far it |
12 | | //! is on its `download_progress` events ([`Input::Progress`]), and the request |
13 | | //! that started it answers once it is done. One change at a time per stage: |
14 | | //! while one runs, the card waits. |
15 | | //! |
16 | | //! A framework picked whose build isn't installed asks first ([`Effect::OfferBuild`]), |
17 | | //! then installs the build through the Library's install, then moves the model |
18 | | //! onto it. A download or load the daemon stops reporting on fails after |
19 | | //! [`STALL_TIMEOUT`], on the app's [`Input::Tick`]. |
20 | | //! |
21 | | //! Both apps' cards run on this page. |
22 | | |
23 | | use std::collections::{HashMap, HashSet}; |
24 | | use std::time::Duration; |
25 | | |
26 | | use super_voice_shared::models::protocol::{DownloadProgress, GpuInfo}; |
27 | | |
28 | | use crate::daemon::backends::BackendInfo; |
29 | | use crate::daemon::client::{self, ModelState, StageState}; |
30 | | use crate::rules::load::{StagedLoad, staged_load_device}; |
31 | | use crate::state::Stage; |
32 | | use crate::state::device_offers::{DeviceOffers, build_installed, staged_device}; |
33 | | use crate::state::model_operations::{LoadCause, ModelOperationState, ModelOperations, Progressed}; |
34 | | use crate::state::stage_catalog::StageCatalog; |
35 | | use crate::state::staged_picks::StagedPicks; |
36 | | |
37 | | /// The stages a card is shown for. |
38 | | pub const STAGES: [Stage; 3] = [ |
39 | | Stage::Transcription, |
40 | | Stage::PostProcessing, |
41 | | Stage::Synthesis, |
42 | | ]; |
43 | | |
44 | | /// How long a download or load may go without a word from the daemon before |
45 | | /// its card says it stalled. Generous, so a load onto the device that reports |
46 | | /// nothing for a while, or a slow download, isn't cut off. |
47 | | pub const STALL_TIMEOUT: Duration = Duration::from_mins(5); |
48 | | |
49 | | mod heard; |
50 | | mod summary; |
51 | | pub use heard::heard; |
52 | | pub use summary::{Memory, memory, stage_status}; |
53 | | |
54 | | /// A framework picked for a stage's model whose backend doesn't have that |
55 | | /// build installed. |
56 | | /// |
57 | | /// Picking one asks first, every time, because it is a download. Once the |
58 | | /// user agrees the build is installed in place of the one there, keeping the |
59 | | /// downloaded models, and the model moves onto the framework when the install |
60 | | /// is done. |
61 | | #[derive(Debug, Clone, PartialEq, Eq)] |
62 | | pub struct PendingBuild { |
63 | | pub stage: Stage, |
64 | | /// The backend whose build this is. |
65 | | pub source: String, |
66 | | /// The framework picked, as the daemon names it (`cuda`). |
67 | | pub framework: String, |
68 | | /// The user agreed and the build is downloading. Until then the app asks. |
69 | | pub downloading: bool, |
70 | | } |
71 | | |
72 | | /// The Models page's cards. |
73 | | #[derive(Debug, Default)] |
74 | | pub struct ModelsState { |
75 | | /// Each stage as the daemon last reported it. |
76 | | pub views: HashMap<Stage, StageState>, |
77 | | /// The backends that can fill each stage, and their models. |
78 | | pub catalog: StageCatalog, |
79 | | /// The model and device each card has staged for Load. |
80 | | pub picks: StagedPicks, |
81 | | /// What each staged model can run on here. |
82 | | pub offers: DeviceOffers, |
83 | | /// Each stage's download or load, as far as it has come, or why it failed. |
84 | | pub operations: ModelOperations, |
85 | | /// Whether each backend's secrets are stored, by (source, name). |
86 | | pub secrets: HashMap<(String, String), bool>, |
87 | | /// The machine's GPUs, for whether a staged model looks too big. |
88 | | pub gpus: Vec<GpuInfo>, |
89 | | /// The stages with a request in flight that isn't a load. |
90 | | pub busy: HashSet<Stage>, |
91 | | /// A framework picked whose build isn't installed: being asked about, or |
92 | | /// downloading. |
93 | | pub pending_build: Option<PendingBuild>, |
94 | | } |
95 | | |
96 | | impl ModelsState { |
97 | | /// `stage` as the daemon last reported it; empty before it has. |
98 | | #[must_use] |
99 | 68 | pub fn view(&self, stage: Stage) -> &StageState { |
100 | | static EMPTY: std::sync::OnceLock<StageState> = std::sync::OnceLock::new(); |
101 | 68 | self.views |
102 | 68 | .get(&stage) |
103 | 41 | .unwrap_or_else(|| EMPTY.get_or_init(StageState::default)) |
104 | 68 | } |
105 | | |
106 | | /// The build downloading for `stage`, once the user agreed to it. |
107 | | #[must_use] |
108 | 22 | pub fn build_downloading(&self, stage: Stage) -> Option<&PendingBuild> { |
109 | 22 | self.pending_build |
110 | 22 | .as_ref() |
111 | 3 | .filter(|p| p.downloading && p.stage == stage) |
112 | 22 | } |
113 | | |
114 | | /// The build the app asks about, until the user answers. |
115 | | #[must_use] |
116 | 2 | pub fn build_question(&self) -> Option<&PendingBuild> { |
117 | 2 | self.pending_build.as_ref().filter(|p| !p.downloading) |
118 | 2 | } |
119 | | } |
120 | | |
121 | | /// What happened. |
122 | | #[derive(Debug, Clone)] |
123 | | pub enum Input { |
124 | | /// Read every stage: the page was opened, or the daemon's events are |
125 | | /// live again. |
126 | | Reload, |
127 | | /// The daemon is back after it was gone: what was under way went with the |
128 | | /// session it belonged to. Read every stage. |
129 | | Reconnected, |
130 | | /// Read which backends can fill each stage again: one was installed, |
131 | | /// updated or removed, or its settings changed. |
132 | | ReloadBackends, |
133 | | /// Read the machine's GPUs again, for their memory in use. |
134 | | RefreshGpus, |
135 | | /// Read `stage` again: the daemon said it changed. |
136 | | Changed(Stage), |
137 | | /// The daemon said `stage` settled: its model came up (`loaded`), or left |
138 | | /// memory. A load it reported is over; the stage is read again. |
139 | | Ready { |
140 | | stage: Stage, |
141 | | loaded: bool, |
142 | | }, |
143 | | /// The daemon said it started loading `model` in `stage`, whoever asked. |
144 | | LoadStarted { |
145 | | stage: Stage, |
146 | | model: String, |
147 | | }, |
148 | | /// The daemon couldn't move `stage`'s model to another device; it is back |
149 | | /// on the one it had. |
150 | | DeviceSwitchFailed(Stage, String), |
151 | | /// Time passed: a download or load the daemon stopped reporting on fails. |
152 | | Tick, |
153 | | ViewLoaded(Stage, Result<StageState, String>), |
154 | | BackendsLoaded(Stage, Result<Vec<BackendInfo>, String>), |
155 | | SecretsLoaded { |
156 | | source: String, |
157 | | items: Vec<(String, bool)>, |
158 | | }, |
159 | | GpusLoaded(Vec<GpuInfo>), |
160 | | /// What a stage's backend runs its models on, or why not. |
161 | | BackendDevicesLoaded { |
162 | | stage: Stage, |
163 | | source: String, |
164 | | result: Result<Vec<String>, String>, |
165 | | }, |
166 | | /// What the daemon offers a model on, and the device it has; or why not. |
167 | | DevicesLoaded { |
168 | | stage: Stage, |
169 | | source: String, |
170 | | model: String, |
171 | | result: Result<(Vec<String>, Option<String>), String>, |
172 | | }, |
173 | | /// How the stage's download or load is coming along. |
174 | | Progress(Stage, DownloadProgress), |
175 | | /// The daemon's answer to a poll: `None` when nothing is under way. |
176 | | Polled(Stage, Option<DownloadProgress>), |
177 | | /// A backend was picked; `None` leaves the stage empty. |
178 | | PickBackend(Stage, Option<String>), |
179 | | /// A model was picked: staged for Load. |
180 | | PickModel(Stage, String), |
181 | | /// A device was picked: staged, or the running model moved onto it. One |
182 | | /// whose build isn't installed asks first ([`Effect::OfferBuild`]). |
183 | | PickDevice(Stage, String), |
184 | | /// The user agreed to download the build asked about. |
185 | | ConfirmBuild, |
186 | | /// The user declined it. Nothing changes. |
187 | | CancelBuild, |
188 | | /// `source` finished installing: a build the user asked for moves the |
189 | | /// model onto its framework. |
190 | | BuildInstalled { |
191 | | source: String, |
192 | | }, |
193 | | /// `source` failed to install. |
194 | | BuildFailed { |
195 | | source: String, |
196 | | error: String, |
197 | | }, |
198 | | /// Load the staged model on the staged device. |
199 | | Load(Stage), |
200 | | /// Bring the running model back up on the device it has. |
201 | | ReloadModel(Stage), |
202 | | Unload(Stage), |
203 | | /// Stop the stage's download. |
204 | | CancelDownload(Stage), |
205 | | /// A request finished, or why not. |
206 | | Done(Stage, Change, Result<(), String>), |
207 | | } |
208 | | |
209 | | /// Which request an [`Input::Done`] answers. |
210 | | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
211 | | pub enum Change { |
212 | | Backend, |
213 | | /// A load, a reload, or a running model moved to another device: the |
214 | | /// stage's operation ends with it. |
215 | | Load, |
216 | | /// A device set for a model that isn't running. |
217 | | Device, |
218 | | Unload, |
219 | | Cancel, |
220 | | } |
221 | | |
222 | | /// What the app should do. |
223 | | #[derive(Debug, Clone, PartialEq, Eq)] |
224 | | pub enum Effect { |
225 | | /// Read the stage: [`run_view`]. |
226 | | ReadView(Stage), |
227 | | /// Read which backends can fill it: [`run_backends`]. |
228 | | ReadBackends(Stage), |
229 | | /// Read which of a backend's secrets are stored: [`run_secrets`]. |
230 | | ReadSecrets(String), |
231 | | /// Read the machine's GPUs: [`run_gpus`]. |
232 | | ReadGpus, |
233 | | /// Read what a stage's backend runs its models on: [`run_backend_devices`]. |
234 | | ReadBackendDevices { |
235 | | stage: Stage, |
236 | | source: String, |
237 | | }, |
238 | | /// Read what a model can run on: [`run_devices`]. |
239 | | ReadDevices { |
240 | | stage: Stage, |
241 | | source: String, |
242 | | model: String, |
243 | | }, |
244 | | /// Ask how the stage's download or load is coming along: [`run_poll`]. |
245 | | Poll(Stage), |
246 | | SetBackend(Stage, Option<String>), |
247 | | /// Set the model's device first when `device` is `Some`, then load it. |
248 | | Load { |
249 | | stage: Stage, |
250 | | model: String, |
251 | | source: String, |
252 | | device: Option<String>, |
253 | | }, |
254 | | /// Set a model's device. `running`: it is up, and moves onto it now. |
255 | | SetDevice { |
256 | | stage: Stage, |
257 | | model: String, |
258 | | device: String, |
259 | | running: bool, |
260 | | }, |
261 | | ReloadModel(Stage), |
262 | | Unload(Stage), |
263 | | CancelDownload(Stage), |
264 | | /// The card shows `model` now: read its language, and for synthesis its |
265 | | /// voice. |
266 | | Focus { |
267 | | stage: Stage, |
268 | | source: String, |
269 | | model: String, |
270 | | }, |
271 | | /// The synthesis model changed, or came up or went: what the Voices tab |
272 | | /// offers follows it. |
273 | | VoicesChanged, |
274 | | /// Ask whether to download `source`'s build for `framework` |
275 | | /// ([`ModelsState::build_question`]). An app that has the build on disk |
276 | | /// already may answer [`Input::ConfirmBuild`] at once: that isn't a |
277 | | /// download. |
278 | | OfferBuild { |
279 | | stage: Stage, |
280 | | source: String, |
281 | | framework: String, |
282 | | }, |
283 | | /// Install `source`'s build for `framework`, through the Library's |
284 | | /// install: [`run_install_build`]. Its end comes back as |
285 | | /// [`Input::BuildInstalled`] or [`Input::BuildFailed`]. |
286 | | InstallBuild { |
287 | | source: String, |
288 | | framework: String, |
289 | | }, |
290 | | } |
291 | | |
292 | | /// Something to tell the user. The app says it in its own words. |
293 | | #[derive(Debug, Clone, PartialEq, Eq)] |
294 | | pub enum Notice { |
295 | | CouldNotRead(Stage, String), |
296 | | CouldNotChange(Stage, String), |
297 | | /// The daemon couldn't move the stage's model to another device. |
298 | | DeviceSwitchFailed(Stage, String), |
299 | | /// The build the user asked for didn't install. |
300 | | BuildFailed { |
301 | | stage: Stage, |
302 | | framework: String, |
303 | | error: String, |
304 | | }, |
305 | | /// The stage's download or load stopped reporting; its card shows it |
306 | | /// failed. |
307 | | Stalled(Stage), |
308 | | } |
309 | | |
310 | | /// What an input came to. |
311 | | #[derive(Debug, Default)] |
312 | | pub struct Outcome { |
313 | | pub effects: Vec<Effect>, |
314 | | /// Take the notice down. |
315 | | pub clear_notice: bool, |
316 | | pub notice: Option<Notice>, |
317 | | } |
318 | | |
319 | | impl Outcome { |
320 | 55 | fn effects(effects: Vec<Effect>) -> Self { |
321 | 55 | Self { |
322 | 55 | effects, |
323 | 55 | ..Self::default() |
324 | 55 | } |
325 | 55 | } |
326 | | |
327 | 1 | fn notice(notice: Notice) -> Self { |
328 | 1 | Self { |
329 | 1 | notice: Some(notice), |
330 | 1 | ..Self::default() |
331 | 1 | } |
332 | 1 | } |
333 | | |
334 | | /// Start `effect`, with the notice down. |
335 | 16 | fn start(effect: Effect) -> Self { |
336 | 16 | Self { |
337 | 16 | effects: vec![effect], |
338 | 16 | clear_notice: true, |
339 | 16 | notice: None, |
340 | 16 | } |
341 | 16 | } |
342 | | } |
343 | | |
344 | | /// Take in `input`, and say what to do. |
345 | 144 | pub fn update(cards: &mut ModelsState, input: Input) -> Outcome { |
346 | 144 | match input { |
347 | 0 | Input::Reload => reload(), |
348 | | Input::Reconnected => { |
349 | 1 | cards.operations.reset(); |
350 | 1 | cards.busy.clear(); |
351 | 1 | reload() |
352 | | } |
353 | 0 | Input::ReloadBackends => Outcome::effects(STAGES.map(Effect::ReadBackends).to_vec()), |
354 | 0 | Input::RefreshGpus => Outcome::effects(vec![Effect::ReadGpus]), |
355 | 0 | Input::Changed(stage) => Outcome::effects(vec![Effect::ReadView(stage)]), |
356 | | input @ (Input::Ready { .. } |
357 | | | Input::LoadStarted { .. } |
358 | | | Input::DeviceSwitchFailed(..) |
359 | 7 | | Input::Tick) => daemon_said(cards, input), |
360 | | input @ (Input::BackendsLoaded(..) |
361 | | | Input::SecretsLoaded { .. } |
362 | | | Input::GpusLoaded(_) |
363 | | | Input::BackendDevicesLoaded { .. } |
364 | 51 | | Input::DevicesLoaded { .. }) => answered(cards, input), |
365 | | input @ (Input::ConfirmBuild |
366 | | | Input::CancelBuild |
367 | | | Input::BuildInstalled { .. } |
368 | 6 | | Input::BuildFailed { .. }) => build(cards, input), |
369 | 49 | Input::ViewLoaded(stage, Ok(view)) => Outcome::effects(view_loaded(cards, stage, view)), |
370 | 0 | Input::ViewLoaded(stage, Err(e)) => Outcome::notice(Notice::CouldNotRead(stage, e)), |
371 | | Input::Progress(stage, progress) | Input::Polled(stage, Some(progress)) => { |
372 | 3 | progressed(cards, stage, &progress); |
373 | 3 | Outcome::default() |
374 | | } |
375 | | Input::Polled(stage, None) => { |
376 | | // Nothing is downloading. A load reports its own end, so only a |
377 | | // download the card still shows is over. |
378 | 2 | if matches!( |
379 | 2 | cards.operations.get(stage), |
380 | | ModelOperationState::Provisioning { .. } |
381 | | ) { |
382 | 1 | cards.operations.set_ready(stage); |
383 | 1 | } |
384 | 2 | Outcome::default() |
385 | | } |
386 | 3 | Input::PickBackend(stage, source) => pick_backend(cards, stage, source), |
387 | 2 | Input::PickModel(stage, model) => pick_model(cards, stage, model), |
388 | 8 | Input::PickDevice(stage, device) => pick_framework(cards, stage, device), |
389 | 9 | Input::Load(stage) => load(cards, stage), |
390 | | Input::ReloadModel(stage) => { |
391 | 0 | let Some(model) = running_model(cards, stage).map(ToString::to_string) else { |
392 | 0 | return Outcome::default(); |
393 | | }; |
394 | 0 | if !ready(cards, stage) { |
395 | 0 | return Outcome::default(); |
396 | 0 | } |
397 | 0 | cards |
398 | 0 | .operations |
399 | 0 | .start(stage, loading(model, LoadCause::Reload)); |
400 | 0 | Outcome::start(Effect::ReloadModel(stage)) |
401 | | } |
402 | | Input::Unload(stage) => { |
403 | 1 | if !cards.busy.insert(stage) { |
404 | 0 | return Outcome::default(); |
405 | 1 | } |
406 | 1 | cards.operations.set_ready(stage); |
407 | 1 | Outcome::start(Effect::Unload(stage)) |
408 | | } |
409 | 0 | Input::CancelDownload(stage) => Outcome::start(Effect::CancelDownload(stage)), |
410 | 2 | Input::Done(stage, change, result) => done(cards, stage, change, result), |
411 | | } |
412 | 144 | } |
413 | | |
414 | | /// What the daemon said on its own: a load started or ended, a device move |
415 | | /// failed; or time passed. |
416 | 7 | fn daemon_said(cards: &mut ModelsState, input: Input) -> Outcome { |
417 | 7 | match input { |
418 | | Input::Ready { stage, loaded } => { |
419 | | // An unload says nothing of a load the user started meanwhile. |
420 | 2 | if loaded && cards.operations.get(stage).is_pending()1 { |
421 | 1 | cards.operations.set_ready(stage); |
422 | 1 | } |
423 | 2 | Outcome::effects(vec![Effect::ReadView(stage)]) |
424 | | } |
425 | | Input::LoadStarted { stage, model } => { |
426 | | // A load this app started already says so, maybe with more. |
427 | 2 | if !cards.operations.get(stage).is_pending() { |
428 | 1 | cards |
429 | 1 | .operations |
430 | 1 | .start(stage, loading(model, LoadCause::Reported)); |
431 | 1 | } |
432 | 2 | Outcome::default() |
433 | | } |
434 | | Input::DeviceSwitchFailed(stage, error) => { |
435 | 1 | show_failure(cards, stage, &error); |
436 | 1 | let mut effects = vec![Effect::ReadView(stage)]; |
437 | 1 | effects.extend(restage_device(cards, stage)); |
438 | 1 | Outcome { |
439 | 1 | effects, |
440 | 1 | clear_notice: false, |
441 | 1 | notice: Some(Notice::DeviceSwitchFailed(stage, error)), |
442 | 1 | } |
443 | | } |
444 | | Input::Tick => { |
445 | | // Worded by the app: the notice says which stage stalled. |
446 | 2 | let stalled = cards.operations.fail_stalled(STALL_TIMEOUT, ""); |
447 | 2 | for stage in &stalled { |
448 | 0 | log::warn!( |
449 | 0 | "The {stage} stage's download or load stalled: no word from the daemon for {}s", |
450 | 0 | STALL_TIMEOUT.as_secs() |
451 | | ); |
452 | | } |
453 | 2 | stalled.first().map_or_else(Outcome::default, |stage| {0 |
454 | 0 | Outcome::notice(Notice::Stalled(*stage)) |
455 | 0 | }) |
456 | | } |
457 | 0 | _ => Outcome::default(), |
458 | | } |
459 | 7 | } |
460 | | |
461 | | /// What the daemon answered to a read the page asked for. |
462 | 51 | fn answered(cards: &mut ModelsState, input: Input) -> Outcome { |
463 | 51 | match input { |
464 | | Input::BackendsLoaded(stage, Ok(backends)) => { |
465 | | // Each backend's secrets decide whether its card can load at all. |
466 | 0 | let effects = backends |
467 | 0 | .iter() |
468 | 0 | .filter(|b| !b.secrets.is_empty()) |
469 | 0 | .map(|b| Effect::ReadSecrets(b.source.clone())) |
470 | | .collect(); |
471 | 0 | cards.catalog.record(stage, backends); |
472 | 0 | Outcome::effects(effects) |
473 | | } |
474 | | Input::BackendsLoaded(stage, Err(e)) => { |
475 | 0 | log::warn!("Could not read the {stage} stage's backends: {e}"); |
476 | 0 | Outcome::default() |
477 | | } |
478 | | Input::SecretsLoaded { source, items } => { |
479 | 0 | for (name, stored) in items { |
480 | 0 | cards.secrets.insert((source.clone(), name), stored); |
481 | | } |
482 | 0 | Outcome::default() |
483 | | } |
484 | | Input::GpusLoaded(gpus) => { |
485 | 10 | cards.gpus = gpus; |
486 | 10 | Outcome::default() |
487 | | } |
488 | | Input::BackendDevicesLoaded { |
489 | | stage, |
490 | | source, |
491 | | result, |
492 | | } => { |
493 | 2 | match result { |
494 | | // An answer about a backend the stage no longer has would evict |
495 | | // the current one's. |
496 | 2 | Ok(devices) if cards.view(stage).source.as_deref() == Some(source.as_str()) => { |
497 | 1 | cards.offers.record(stage, source, None, devices); |
498 | | } |
499 | 1 | Ok(_) => {} |
500 | | // The chips fall back to the catalog's own reading. |
501 | 0 | Err(e) => log::warn!("Could not read the devices for {source}: {e}"), |
502 | | } |
503 | 2 | Outcome::default() |
504 | | } |
505 | | Input::DevicesLoaded { |
506 | | stage, |
507 | | source, |
508 | | model, |
509 | | result, |
510 | | } => { |
511 | 39 | devices_loaded(cards, stage, source, &model, result); |
512 | 39 | Outcome::default() |
513 | | } |
514 | 0 | _ => Outcome::default(), |
515 | | } |
516 | 51 | } |
517 | | |
518 | | /// A framework's build: the user's answer, and the install's end. |
519 | 6 | fn build(cards: &mut ModelsState, input: Input) -> Outcome { |
520 | 6 | match input { |
521 | 2 | Input::ConfirmBuild => confirm_build(cards), |
522 | | Input::CancelBuild => { |
523 | 1 | if cards.build_question().is_some() { |
524 | 1 | cards.pending_build = None; |
525 | 0 | } |
526 | 1 | Outcome::default() |
527 | | } |
528 | 2 | Input::BuildInstalled { source } => build_installed_for(cards, &source), |
529 | | Input::BuildFailed { source, error } => { |
530 | 1 | let Some(pending) = take_build(cards, &source) else { |
531 | 0 | return Outcome::default(); |
532 | | }; |
533 | | // The build that was there is untouched. |
534 | 1 | Outcome::notice(Notice::BuildFailed { |
535 | 1 | stage: pending.stage, |
536 | 1 | framework: pending.framework, |
537 | 1 | error, |
538 | 1 | }) |
539 | | } |
540 | 0 | _ => Outcome::default(), |
541 | | } |
542 | 6 | } |
543 | | |
544 | | /// Read every stage, what can fill it, and the GPUs. |
545 | 1 | fn reload() -> Outcome { |
546 | 1 | let mut effects: Vec<Effect> = STAGES |
547 | 1 | .iter() |
548 | 3 | .flat_map(|stage| [Effect::ReadView(*stage), Effect::ReadBackends(*stage)]) |
549 | | .collect(); |
550 | 1 | effects.push(Effect::ReadGpus); |
551 | 1 | Outcome::effects(effects) |
552 | 1 | } |
553 | | |
554 | | /// Show a failure on `stage`'s card, where the user is looking: the |
555 | | /// daemon's words, or the app's when it words them itself. The user's home |
556 | | /// directory is collapsed back to `$HOME`. |
557 | 2 | pub fn show_failure(cards: &mut ModelsState, stage: Stage, message: &str) { |
558 | 2 | let home = std::env::var("HOME").unwrap_or_default(); |
559 | 2 | cards.operations.set( |
560 | 2 | stage, |
561 | 2 | ModelOperationState::Error { |
562 | 2 | message: crate::rules::load::sanitize_home(message, &home), |
563 | 2 | }, |
564 | 2 | ); |
565 | 2 | } |
566 | | |
567 | | /// Whether `stage` is free to start a change. |
568 | 23 | fn ready(cards: &ModelsState, stage: Stage) -> bool { |
569 | 23 | let settled = cards.operations.is_ready(stage) |
570 | 3 | || matches!( |
571 | 3 | cards.operations.get(stage), |
572 | | ModelOperationState::Error { .. } |
573 | | ); |
574 | 23 | settled && !cards.busy.contains(&stage)21 |
575 | 23 | } |
576 | | |
577 | | /// A load of `model` just starting: the card says so until the daemon reports |
578 | | /// how far it is. |
579 | 11 | fn loading(model: String, cause: LoadCause) -> ModelOperationState { |
580 | 11 | ModelOperationState::Loading { |
581 | 11 | target_model: model, |
582 | 11 | cause, |
583 | 11 | load: None, |
584 | 11 | } |
585 | 11 | } |
586 | | |
587 | | /// The model `stage` has up, as the daemon reports it. |
588 | | #[must_use] |
589 | 27 | pub fn running_model(cards: &ModelsState, stage: Stage) -> Option<&str> { |
590 | 27 | let view = cards.views.get(&stage)?3 ; |
591 | 24 | if view.loaded { |
592 | 7 | view.model.as_deref() |
593 | | } else { |
594 | 17 | None |
595 | | } |
596 | 27 | } |
597 | | |
598 | | /// The model `stage` keeps out of memory until its next use, when nothing is |
599 | | /// under way on it. |
600 | | #[must_use] |
601 | 27 | pub fn idle_model(cards: &ModelsState, stage: Stage) -> Option<&str> { |
602 | 27 | let view = cards.views.get(&stage)?3 ; |
603 | 24 | if view.state == ModelState::Idle && cards.operations.is_ready(stage)2 { |
604 | 2 | view.model.as_deref() |
605 | | } else { |
606 | 22 | None |
607 | | } |
608 | 27 | } |
609 | | |
610 | | /// Take the daemon's view of `stage`. Its selection is staged like any pick, |
611 | | /// unless the user has one of their own on the same backend, so the card offers |
612 | | /// the model the daemon remembers and asks what it runs on. |
613 | 49 | fn view_loaded(cards: &mut ModelsState, stage: Stage, view: StageState) -> Vec<Effect> { |
614 | 49 | let mut effects = Vec::new(); |
615 | 49 | let previous = cards.view(stage); |
616 | 49 | let selection_changed = previous.selection() != view.selection(); |
617 | 49 | let source_changed = previous.source != view.source; |
618 | | // What the Voices tab offers follows the synthesis model. |
619 | | let voices_changed = |
620 | 49 | stage == Stage::Synthesis && (selection_changed4 || previous.loaded != view.loaded2 ); |
621 | 49 | let mut focus = selection_changed; |
622 | 49 | match view.selection() { |
623 | | Some((model, source)) => { |
624 | 49 | let own = cards |
625 | 49 | .picks |
626 | 49 | .get(stage) |
627 | 11 | .is_some_and(|pick| pick.source == source); |
628 | 49 | if !own { |
629 | 38 | cards |
630 | 38 | .picks |
631 | 38 | .stage_model(stage, source.clone(), model.clone()); |
632 | 38 | effects.push(Effect::ReadDevices { |
633 | 38 | stage, |
634 | 38 | source: source.clone(), |
635 | 38 | model: model.clone(), |
636 | 38 | }); |
637 | 38 | focus = true; |
638 | 11 | } |
639 | 49 | if focus { |
640 | 39 | effects.push(Effect::Focus { |
641 | 39 | stage, |
642 | 39 | source, |
643 | 39 | model, |
644 | 39 | }); |
645 | 10 | } |
646 | | } |
647 | | None => { |
648 | | // A backend picked and no model: a pick on another backend is |
649 | | // stale. |
650 | 0 | if cards.picks.get(stage).map(|pick| pick.source.as_str()) != view.source.as_deref() { |
651 | 0 | cards.picks.clear(stage); |
652 | 0 | } |
653 | | } |
654 | | } |
655 | | // The card's chips are the daemon's answer for the backend there now. |
656 | 49 | if let Some(source) = &view.source |
657 | 49 | && (source_changed || cards.offers11 .backend(stage, source).is_none()) |
658 | | { |
659 | 48 | effects.push(Effect::ReadBackendDevices { |
660 | 48 | stage, |
661 | 48 | source: source.clone(), |
662 | 48 | }); |
663 | 1 | } |
664 | 49 | if cards.operations.get(stage).is_pending() { |
665 | 0 | effects.push(Effect::Poll(stage)); |
666 | | } else { |
667 | 49 | adopt_running_device(cards, stage, &view); |
668 | | } |
669 | 49 | if voices_changed { |
670 | 3 | effects.push(Effect::VoicesChanged); |
671 | 46 | } |
672 | 49 | cards.views.insert(stage, view); |
673 | 49 | effects |
674 | 49 | } |
675 | | |
676 | | /// Show the device the running model is on as its pick, when another client |
677 | | /// moved it. Only for the model the card has picked, and only a device the |
678 | | /// picker offers. |
679 | 49 | fn adopt_running_device(cards: &mut ModelsState, stage: Stage, view: &StageState) { |
680 | | let (Some(model), Some(source), Some(device)) = ( |
681 | 49 | view.model.as_deref().filter(|_| view.loaded), |
682 | 49 | view.source.as_deref(), |
683 | 49 | view.device.as_ref().map(|d| d.preference2 .as_str()), |
684 | | ) else { |
685 | 47 | return; |
686 | | }; |
687 | 2 | if cards.picks.model(stage, source) != Some(model) { |
688 | 0 | return; |
689 | 2 | } |
690 | 2 | let offered = cards |
691 | 2 | .offers |
692 | 2 | .model(stage, source, model) |
693 | 4 | .is_some_and(|devices| devices.iter()2 .any(|d| d == device)); |
694 | 2 | if offered { |
695 | 1 | cards.picks.set_device(stage, Some(device.to_string())); |
696 | 1 | } |
697 | 49 | } |
698 | | |
699 | | /// Take what the daemon offers `model` on. An answer about a backend or a model |
700 | | /// the card no longer has staged is dropped: a pick made since wins. |
701 | 39 | fn devices_loaded( |
702 | 39 | cards: &mut ModelsState, |
703 | 39 | stage: Stage, |
704 | 39 | source: String, |
705 | 39 | model: &str, |
706 | 39 | result: Result<(Vec<String>, Option<String>), String>, |
707 | 39 | ) { |
708 | 39 | let (devices, current) = match result { |
709 | 39 | Ok(answer) => answer, |
710 | | Err(e) => { |
711 | | // Nothing is staged onto a device the daemon never offered, so a |
712 | | // failed read leaves the picker hidden and Load disabled. |
713 | 0 | log::warn!("Could not read the devices for {model}: {e}"); |
714 | 0 | return; |
715 | | } |
716 | | }; |
717 | 39 | if cards.picks.model(stage, &source) != Some(model) { |
718 | 1 | return; |
719 | 38 | } |
720 | 38 | cards |
721 | 38 | .offers |
722 | 38 | .record(stage, source, Some(model.to_string()), devices.clone()); |
723 | | // The model's own device when the picker can show it, otherwise the first |
724 | | // offered, and nothing when none is, which keeps Load disabled. |
725 | 38 | cards |
726 | 38 | .picks |
727 | 38 | .set_device(stage, staged_device(&devices, current)); |
728 | 39 | } |
729 | | |
730 | 3 | fn progressed(cards: &mut ModelsState, stage: Stage, progress: &DownloadProgress) { |
731 | 3 | if cards.operations.apply_progress(stage, progress) == Progressed::Finished |
732 | 0 | && progress.status == "cancelled" |
733 | | { |
734 | 0 | cards.operations.set_ready(stage); |
735 | 3 | } |
736 | 3 | } |
737 | | |
738 | | /// The installed backends a filled step can switch to: those that fit it, |
739 | | /// other than the one it has, in the catalog's order. Empty while its |
740 | | /// backends are unread, and for a step no backend fills (it offers Select a |
741 | | /// backend… instead). |
742 | | #[must_use] |
743 | 3 | pub fn switch_targets(cards: &ModelsState, stage: Stage) -> Vec<&BackendInfo> { |
744 | 3 | let Some(current) = cards.views.get(&stage).and_then(|v| v.source2 .as_deref()) else { |
745 | 1 | return Vec::new(); |
746 | | }; |
747 | 2 | cards |
748 | 2 | .catalog |
749 | 2 | .backends(stage) |
750 | 2 | .unwrap_or_default() |
751 | 2 | .iter() |
752 | 3 | .filter(|backend| backend.source != current) |
753 | | .collect() |
754 | 3 | } |
755 | | |
756 | 3 | fn pick_backend(cards: &mut ModelsState, stage: Stage, source: Option<String>) -> Outcome { |
757 | | // Picking what is already there is not a change: sending it would unload |
758 | | // the running model for nothing. |
759 | 3 | let current = cards.views.get(&stage).and_then(|v| v.source.clone()); |
760 | 3 | if current == source || !2 ready2 (cards, stage) || !cards.busy.insert(stage)2 { |
761 | 1 | return Outcome::default(); |
762 | 2 | } |
763 | 2 | cards.picks.clear(stage); |
764 | 2 | cards.operations.set_ready(stage); |
765 | 2 | Outcome::start(Effect::SetBackend(stage, source)) |
766 | 3 | } |
767 | | |
768 | 2 | fn pick_model(cards: &mut ModelsState, stage: Stage, model: String) -> Outcome { |
769 | 2 | let Some(source) = cards.views.get(&stage).and_then(|v| v.source.clone()) else { |
770 | 0 | return Outcome::default(); |
771 | | }; |
772 | 2 | cards |
773 | 2 | .picks |
774 | 2 | .stage_model(stage, source.clone(), model.clone()); |
775 | | // A new pick is a fresh start: the last one's failure no longer applies. |
776 | 2 | if matches!( |
777 | 2 | cards.operations.get(stage), |
778 | | ModelOperationState::Error { .. } |
779 | | ) { |
780 | 0 | cards.operations.set_ready(stage); |
781 | 2 | } |
782 | 2 | Outcome { |
783 | 2 | effects: vec![ |
784 | 2 | Effect::ReadDevices { |
785 | 2 | stage, |
786 | 2 | source: source.clone(), |
787 | 2 | model: model.clone(), |
788 | 2 | }, |
789 | 2 | // So the card shows the model's language and voice before Load. |
790 | 2 | Effect::Focus { |
791 | 2 | stage, |
792 | 2 | source, |
793 | 2 | model, |
794 | 2 | }, |
795 | 2 | ], |
796 | 2 | clear_notice: true, |
797 | 2 | notice: None, |
798 | 2 | } |
799 | 2 | } |
800 | | |
801 | | /// A framework picked in `stage`'s Runs on row. One whose build is installed |
802 | | /// is chosen at once; one whose build isn't asks first, since getting it |
803 | | /// means installing it over the one there. |
804 | 8 | fn pick_framework(cards: &mut ModelsState, stage: Stage, device: String) -> Outcome { |
805 | | // One build at a time: the pick waits for it. |
806 | 8 | if cards.build_downloading(stage).is_some() { |
807 | 1 | return Outcome::default(); |
808 | 7 | } |
809 | 7 | let source = cards.view(stage).source.clone(); |
810 | 7 | let installed = source |
811 | 7 | .as_deref() |
812 | 7 | .and_then(|source| cards.catalog.backend(stage, source)) |
813 | 7 | .is_none_or(|b| build_installed(&device, &b.installed_accel)); |
814 | 7 | match source { |
815 | 7 | Some(source) if !installed => { |
816 | 3 | cards.pending_build = Some(PendingBuild { |
817 | 3 | stage, |
818 | 3 | source: source.clone(), |
819 | 3 | framework: device.clone(), |
820 | 3 | downloading: false, |
821 | 3 | }); |
822 | 3 | Outcome::effects(vec![Effect::OfferBuild { |
823 | 3 | stage, |
824 | 3 | source, |
825 | 3 | framework: device, |
826 | 3 | }]) |
827 | | } |
828 | 4 | _ => pick_device(cards, stage, device), |
829 | | } |
830 | 8 | } |
831 | | |
832 | | /// The user agreed, or the build is on disk: install it. |
833 | 2 | fn confirm_build(cards: &mut ModelsState) -> Outcome { |
834 | 2 | let Some(pending) = cards.pending_build.as_mut().filter(|p| !p.downloading) else { |
835 | 0 | return Outcome::default(); |
836 | | }; |
837 | 2 | pending.downloading = true; |
838 | 2 | Outcome::start(Effect::InstallBuild { |
839 | 2 | source: pending.source.clone(), |
840 | 2 | framework: pending.framework.clone(), |
841 | 2 | }) |
842 | 2 | } |
843 | | |
844 | | /// The build the user agreed to for `source`, now its install has ended. |
845 | 3 | fn take_build(cards: &mut ModelsState, source: &str) -> Option<PendingBuild> { |
846 | 3 | cards |
847 | 3 | .pending_build |
848 | 3 | .take_if(|p| p.downloading && p.source == source) |
849 | 3 | } |
850 | | |
851 | | /// `source` installed. When that was a build the user asked for, move the |
852 | | /// model onto its framework: a running model reloads onto it, one not |
853 | | /// running has it staged for its Load. The catalog is read again, for the |
854 | | /// build it has now. |
855 | 2 | fn build_installed_for(cards: &mut ModelsState, source: &str) -> Outcome { |
856 | 2 | let Some(pending) = take_build(cards, source) else { |
857 | 1 | return Outcome::default(); |
858 | | }; |
859 | | // The stage moved on to another backend while the build downloaded. |
860 | 1 | if cards.view(pending.stage).source.as_deref() != Some(source) { |
861 | 0 | return Outcome::default(); |
862 | 1 | } |
863 | 1 | let mut outcome = pick_device(cards, pending.stage, pending.framework); |
864 | 1 | outcome.effects.push(Effect::ReadBackends(pending.stage)); |
865 | 1 | outcome |
866 | 2 | } |
867 | | |
868 | | /// Choose the device `stage`'s model runs on. Staged while nothing is up, |
869 | | /// for Load to commit. Once the picked model is running the choice goes to |
870 | | /// the daemon, which reloads it onto the new device in place; for a model |
871 | | /// that is idle, the daemon keeps it for the next load. |
872 | 5 | fn pick_device(cards: &mut ModelsState, stage: Stage, device: String) -> Outcome { |
873 | 5 | if cards.picks.device(stage) == Some(device.as_str()) { |
874 | 0 | return Outcome::default(); |
875 | 5 | } |
876 | 5 | cards.picks.set_device(stage, Some(device.clone())); |
877 | 5 | let Some(picked) = cards.picks.get(stage).map(|pick| pick.model.clone()) else { |
878 | 0 | return Outcome::default(); |
879 | | }; |
880 | 5 | let running = running_model(cards, stage) == Some(picked.as_str()); |
881 | 5 | let idle = idle_model(cards, stage) == Some(picked.as_str()); |
882 | 5 | if !(running || idle3 ) || !3 ready3 (cards, stage) { |
883 | 2 | return Outcome::default(); |
884 | 3 | } |
885 | 3 | if running { |
886 | 2 | cards.operations.start( |
887 | 2 | stage, |
888 | 2 | loading( |
889 | 2 | picked.clone(), |
890 | 2 | LoadCause::Move { |
891 | 2 | device: device.clone(), |
892 | 2 | }, |
893 | 2 | ), |
894 | 2 | ); |
895 | | } else { |
896 | 1 | cards.busy.insert(stage); |
897 | | } |
898 | 3 | Outcome::start(Effect::SetDevice { |
899 | 3 | stage, |
900 | 3 | model: picked, |
901 | 3 | device, |
902 | 3 | running, |
903 | 3 | }) |
904 | 5 | } |
905 | | |
906 | | /// Commit `stage`'s staged pick: set the model's device, then load it. |
907 | 9 | fn load(cards: &mut ModelsState, stage: Stage) -> Outcome { |
908 | 9 | let Some(pick) = cards.picks.get(stage).cloned() else { |
909 | 0 | return Outcome::default(); |
910 | | }; |
911 | 9 | if !ready(cards, stage) { |
912 | 1 | return Outcome::default(); |
913 | 8 | } |
914 | 8 | let backends = cards.catalog.backends(stage).unwrap_or_default(); |
915 | | // A backend can be uninstalled between staging and the click. |
916 | | let StagedLoad::Load { device_to_set } = |
917 | 8 | staged_load_device(backends, &pick.source, &pick.model, pick.device.as_deref()) |
918 | | else { |
919 | 0 | return Outcome::default(); |
920 | | }; |
921 | 8 | cards |
922 | 8 | .operations |
923 | 8 | .start(stage, loading(pick.model.clone(), LoadCause::Switch)); |
924 | 8 | Outcome::start(Effect::Load { |
925 | 8 | stage, |
926 | 8 | model: pick.model, |
927 | 8 | source: pick.source, |
928 | 8 | device: device_to_set, |
929 | 8 | }) |
930 | 9 | } |
931 | | |
932 | 2 | fn done( |
933 | 2 | cards: &mut ModelsState, |
934 | 2 | stage: Stage, |
935 | 2 | change: Change, |
936 | 2 | result: Result<(), String>, |
937 | 2 | ) -> Outcome { |
938 | 2 | cards.busy.remove(&stage); |
939 | 2 | let mut effects = vec![Effect::ReadView(stage)]; |
940 | 2 | let mut notice = None; |
941 | 2 | match (change, result) { |
942 | | (Change::Load | Change::Cancel | Change::Unload, Ok(())) => { |
943 | 0 | cards.operations.set_ready(stage); |
944 | | } |
945 | 1 | (Change::Backend | Change::Device, Ok(())) => {} |
946 | | // A failed load, move or unload shows on the card itself, where the |
947 | | // user is looking. |
948 | | (Change::Load | Change::Cancel | Change::Unload, Err(e)) => { |
949 | 1 | show_failure(cards, stage, &e); |
950 | | // The device the daemon kept is the one to show again. |
951 | 1 | effects.extend(restage_device(cards, stage)); |
952 | | } |
953 | | (Change::Backend | Change::Device, Err(e)) => { |
954 | 0 | effects.extend(restage_device(cards, stage)); |
955 | 0 | notice = Some(Notice::CouldNotChange(stage, e)); |
956 | | } |
957 | | } |
958 | 2 | Outcome { |
959 | 2 | effects, |
960 | 2 | clear_notice: false, |
961 | 2 | notice, |
962 | 2 | } |
963 | 2 | } |
964 | | |
965 | | /// Read the staged model's devices again, which stages the device the daemon |
966 | | /// has for it. |
967 | 2 | fn restage_device(cards: &ModelsState, stage: Stage) -> Option<Effect> { |
968 | 2 | let pick = cards.picks.get(stage)?0 ; |
969 | 2 | Some(Effect::ReadDevices { |
970 | 2 | stage, |
971 | 2 | source: pick.source.clone(), |
972 | 2 | model: pick.model.clone(), |
973 | 2 | }) |
974 | 2 | } |
975 | | |
976 | | /// What a stage's card shows. |
977 | | #[derive(Debug, Clone, PartialEq, Eq)] |
978 | | pub enum Shown { |
979 | | /// Nothing fills the stage. |
980 | | Empty, |
981 | | /// The backend's model is up, or out of memory until its next use |
982 | | /// (`idle`). |
983 | | Running { model: String, idle: bool }, |
984 | | /// The model staged for Load, if any. |
985 | | Staging { model: Option<String> }, |
986 | | } |
987 | | |
988 | | /// What a stage's card shows, worked out from the page's state. |
989 | | #[derive(Debug, Clone)] |
990 | | pub struct Card<'a> { |
991 | | pub stage: Stage, |
992 | | /// The backend filling the stage, when it is one of those that can. |
993 | | pub backend: Option<&'a BackendInfo>, |
994 | | /// The backend's source, even when it isn't in the catalog yet. |
995 | | pub source: Option<String>, |
996 | | /// The labels of required secrets and options not set yet. While any are, |
997 | | /// the card offers no model: Configure is the way forward. |
998 | | pub unmet: Vec<&'a str>, |
999 | | pub shown: Shown, |
1000 | | /// The models the backend serves in this stage. |
1001 | | pub models: Vec<String>, |
1002 | | /// What the shown model can run on here; `None` before the daemon says. |
1003 | | pub devices: Option<&'a [String]>, |
1004 | | /// The device picked for the shown model. |
1005 | | pub device: Option<&'a str>, |
1006 | | /// The framework a running model fell back to, when not the one picked. |
1007 | | pub fallback: Option<String>, |
1008 | | /// How much memory the staged model says it needs. |
1009 | | pub memory: Option<u64>, |
1010 | | /// The staged model and device are ready to load. |
1011 | | pub can_load: bool, |
1012 | | /// The staged model can't run on anything this install has. |
1013 | | pub no_device: bool, |
1014 | | /// The staged model looks too big for the GPU: (needed, available). |
1015 | | pub vram_shortfall: Option<(u64, u64)>, |
1016 | | /// The stage's download or load, or why it failed. |
1017 | | pub operation: &'a ModelOperationState, |
1018 | | /// A request is in flight; the card waits. |
1019 | | pub busy: bool, |
1020 | | /// The build the user agreed to for this stage, while it downloads. |
1021 | | pub build: Option<&'a PendingBuild>, |
1022 | | } |
1023 | | |
1024 | | /// `stage`'s card. |
1025 | | #[must_use] |
1026 | 13 | pub fn card(cards: &ModelsState, stage: Stage) -> Card<'_> { |
1027 | 13 | let view = cards.views.get(&stage); |
1028 | 13 | let source = view.and_then(|v| v.source.clone()); |
1029 | 13 | let backend = source |
1030 | 13 | .as_deref() |
1031 | 13 | .and_then(|source| cards.catalog.backend(stage, source)); |
1032 | 13 | let unmet = backend.map_or_else(Vec::new, |b| { |
1033 | 13 | crate::rules::status::unmet_requirements(&cards.secrets, b) |
1034 | 13 | }); |
1035 | 13 | let picked = source |
1036 | 13 | .as_deref() |
1037 | 13 | .and_then(|source| cards.picks.model(stage, source)); |
1038 | 13 | let shown = match ( |
1039 | 13 | &source, |
1040 | 13 | running_model(cards, stage), |
1041 | 13 | idle_model(cards, stage), |
1042 | 13 | ) { |
1043 | 0 | (None, ..) => Shown::Empty, |
1044 | 2 | (Some(_), Some(model), _) => Shown::Running { |
1045 | 2 | model: model.to_string(), |
1046 | 2 | idle: false, |
1047 | 2 | }, |
1048 | 1 | (Some(_), None, Some(model)) => Shown::Running { |
1049 | 1 | model: model.to_string(), |
1050 | 1 | idle: true, |
1051 | 1 | }, |
1052 | 10 | (Some(_), None, None) => Shown::Staging { |
1053 | 10 | model: picked.map(ToString::to_string), |
1054 | 10 | }, |
1055 | | }; |
1056 | 13 | let shown_model = match &shown { |
1057 | 3 | Shown::Running { model, .. } => Some(model.as_str()), |
1058 | 10 | Shown::Staging { model } => model.as_deref(), |
1059 | 0 | Shown::Empty => None, |
1060 | | }; |
1061 | 13 | let devices = match (source.as_deref(), shown_model) { |
1062 | 13 | (Some(source), Some(model)) => cards.offers.model(stage, source, model), |
1063 | 0 | _ => None, |
1064 | | }; |
1065 | | // The picked device belongs to the shown model only when it is the pick. |
1066 | 13 | let device = (shown_model.is_some() && shown_model == picked) |
1067 | 13 | .then(|| cards.picks.device(stage)) |
1068 | | .flatten(); |
1069 | 13 | let build = cards.build_downloading(stage); |
1070 | | // Not while a switch or a build is under way: the model is still on the |
1071 | | // old framework because it hasn't moved yet. |
1072 | 13 | let fallback = match &shown { |
1073 | | Shown::Running { idle: false, .. } |
1074 | 2 | if cards.operations.is_ready(stage) && build.is_none() => |
1075 | | { |
1076 | 1 | view.and_then(StageState::running_device) |
1077 | 0 | .and_then(|running| crate::rules::load::fallback(device, running)) |
1078 | 1 | .map(ToString::to_string) |
1079 | | } |
1080 | 12 | _ => None, |
1081 | | }; |
1082 | 13 | let staging = matches!(shown, Shown::Staging { .. }); |
1083 | 13 | let memory = backend |
1084 | 13 | .zip(shown_model.filter(|_| staging)) |
1085 | 11 | .and_then(|(b, model)| b.models.iter()10 .find(|m| m.name == model)) |
1086 | 10 | .map(|m| m.estimated_vram_bytes) |
1087 | 10 | .filter(|bytes| *bytes > 0); |
1088 | 13 | let online = backend |
1089 | 13 | .zip(shown_model) |
1090 | 13 | .is_some_and(|(b, model)| crate::rules::backends::model_is_online(b, model)); |
1091 | 13 | let no_device = staging && !online10 && devices.is_some_and(<[String]>::is_empty)9 ; |
1092 | 13 | let can_load = staging |
1093 | 10 | && unmet.is_empty() |
1094 | 9 | && shown_model.is_some() |
1095 | 9 | && (device.is_some() || online1 ) |
1096 | 9 | && ready(cards, stage); |
1097 | 13 | let vram_shortfall = backend.filter(|_| staging && !no_device10 ).and_then(|b| {10 |
1098 | 10 | crate::rules::memory::staged_vram_shortfall_in( |
1099 | 10 | stage, |
1100 | 10 | cards.operations.get(stage), |
1101 | 10 | &cards.picks, |
1102 | 10 | b, |
1103 | 10 | &cards.gpus, |
1104 | 10 | ) |
1105 | 10 | }); |
1106 | | Card { |
1107 | 13 | stage, |
1108 | 13 | backend, |
1109 | 13 | source, |
1110 | 13 | unmet, |
1111 | 13 | models: backend.map_or_else(Vec::new, |b| cards.catalog.models(stage, &b.source)), |
1112 | 13 | shown, |
1113 | 13 | devices, |
1114 | 13 | device, |
1115 | 13 | fallback, |
1116 | 13 | memory, |
1117 | 13 | can_load, |
1118 | 13 | no_device, |
1119 | 13 | vram_shortfall, |
1120 | 13 | operation: cards.operations.get(stage), |
1121 | 13 | busy: cards.busy.contains(&stage), |
1122 | 13 | build, |
1123 | | } |
1124 | 13 | } |
1125 | | |
1126 | 0 | pub async fn run_view(stage: Stage) -> Input { |
1127 | | Input::ViewLoaded( |
1128 | 0 | stage, |
1129 | 0 | client::get_stage_view(stage) |
1130 | 0 | .await |
1131 | 0 | .map_err(|e| e.to_string()), |
1132 | | ) |
1133 | 0 | } |
1134 | | |
1135 | 0 | pub async fn run_backends(stage: Stage) -> Input { |
1136 | | Input::BackendsLoaded( |
1137 | 0 | stage, |
1138 | 0 | client::list_stage_backends(stage) |
1139 | 0 | .await |
1140 | 0 | .map_err(|e| e.to_string()), |
1141 | | ) |
1142 | 0 | } |
1143 | | |
1144 | 0 | pub async fn run_secrets(source: String) -> Input { |
1145 | 0 | let items = client::list_backend_secrets(source.clone()) |
1146 | 0 | .await |
1147 | 0 | .unwrap_or_else(|e| { |
1148 | 0 | log::warn!("Could not read which of {source}'s secrets are stored: {e}"); |
1149 | 0 | Vec::new() |
1150 | 0 | }); |
1151 | 0 | Input::SecretsLoaded { source, items } |
1152 | 0 | } |
1153 | | |
1154 | | /// What `source`, `stage`'s backend, runs its models on: the union over the |
1155 | | /// models it serves there. |
1156 | 0 | pub async fn run_backend_devices(stage: Stage, source: String) -> Input { |
1157 | | Input::BackendDevicesLoaded { |
1158 | 0 | stage, |
1159 | 0 | result: client::list_stage_devices(stage) |
1160 | 0 | .await |
1161 | 0 | .map_err(|e| e.to_string()), |
1162 | 0 | source, |
1163 | | } |
1164 | 0 | } |
1165 | | |
1166 | | /// Install `source`'s build for `framework`, as the Library installs: its |
1167 | | /// progress and its end are the Library's to follow. |
1168 | 0 | pub async fn run_install_build(source: String, framework: String) -> crate::pages::library::Input { |
1169 | | use crate::pages::library::Input; |
1170 | 0 | match crate::daemon::registry::install_by_source(&source, Some(&framework)).await { |
1171 | 0 | Ok(accepted) => Input::InstallAccepted { |
1172 | 0 | source, |
1173 | 0 | install_id: accepted.install_id, |
1174 | 0 | }, |
1175 | 0 | Err(e) => Input::InstallFailedToStart { |
1176 | 0 | source, |
1177 | 0 | error: e.to_string(), |
1178 | 0 | }, |
1179 | | } |
1180 | 0 | } |
1181 | | |
1182 | 0 | pub async fn run_gpus() -> Input { |
1183 | 0 | Input::GpusLoaded(client::get_gpu_info().await.unwrap_or_default()) |
1184 | 0 | } |
1185 | | |
1186 | | /// What `model` can run on, and the device it has: one read for both, so the |
1187 | | /// picker and the device it starts on land together. |
1188 | 0 | pub async fn run_devices(stage: Stage, source: String, model: String) -> Input { |
1189 | 0 | let result = async { |
1190 | 0 | let devices = client::list_model_devices(stage, model.clone()).await?; |
1191 | | // The recorded device is the nicety: a model never loaded has none. |
1192 | 0 | let current = client::get_model_device(stage, model.clone()) |
1193 | 0 | .await |
1194 | | .ok() |
1195 | 0 | .map(|d| d.device); |
1196 | 0 | Ok::<_, super_voice_shared::daemon::http_client::HttpError>((devices, current)) |
1197 | 0 | } |
1198 | 0 | .await |
1199 | 0 | .map_err(|e| e.to_string()); |
1200 | 0 | Input::DevicesLoaded { |
1201 | 0 | stage, |
1202 | 0 | source, |
1203 | 0 | model, |
1204 | 0 | result, |
1205 | 0 | } |
1206 | 0 | } |
1207 | | |
1208 | 0 | pub async fn run_poll(stage: Stage) -> Input { |
1209 | | Input::Polled( |
1210 | 0 | stage, |
1211 | 0 | client::get_download_status(stage).await.ok().flatten(), |
1212 | | ) |
1213 | 0 | } |
1214 | | |
1215 | | /// Run `effect`, a change: the stage is read again when it is done. |
1216 | 0 | pub async fn run_change(effect: Effect) -> Option<Input> { |
1217 | 0 | let (stage, change, result) = match effect { |
1218 | | Effect::SetBackend(stage, Some(source)) => ( |
1219 | 0 | stage, |
1220 | 0 | Change::Backend, |
1221 | 0 | client::set_stage_backend(stage, source).await, |
1222 | | ), |
1223 | | Effect::SetBackend(stage, None) => ( |
1224 | 0 | stage, |
1225 | 0 | Change::Backend, |
1226 | 0 | client::clear_stage_backend(stage).await, |
1227 | | ), |
1228 | | Effect::Load { |
1229 | | stage, |
1230 | | model, |
1231 | | source, |
1232 | | device, |
1233 | | } => { |
1234 | 0 | let result = async { |
1235 | | // The device is the model's own, set before the load so the |
1236 | | // load picks it up. |
1237 | 0 | if let Some(device) = device { |
1238 | 0 | client::set_model_device(stage, model.clone(), device).await?; |
1239 | 0 | } |
1240 | 0 | client::set_stage_model(stage, model, Some(source)).await |
1241 | 0 | } |
1242 | 0 | .await; |
1243 | 0 | (stage, Change::Load, result) |
1244 | | } |
1245 | | Effect::SetDevice { |
1246 | | stage, |
1247 | | model, |
1248 | | device, |
1249 | | running, |
1250 | | } => ( |
1251 | 0 | stage, |
1252 | 0 | if running { |
1253 | 0 | Change::Load |
1254 | | } else { |
1255 | 0 | Change::Device |
1256 | | }, |
1257 | 0 | client::set_model_device(stage, model, device).await, |
1258 | | ), |
1259 | | Effect::ReloadModel(stage) => { |
1260 | 0 | (stage, Change::Load, client::reload_stage_model(stage).await) |
1261 | | } |
1262 | | Effect::Unload(stage) => ( |
1263 | 0 | stage, |
1264 | 0 | Change::Unload, |
1265 | 0 | client::unload_stage_model(stage).await, |
1266 | | ), |
1267 | | Effect::CancelDownload(stage) => { |
1268 | 0 | (stage, Change::Cancel, client::cancel_download(stage).await) |
1269 | | } |
1270 | | Effect::ReadView(_) |
1271 | | | Effect::ReadBackends(_) |
1272 | | | Effect::ReadSecrets(_) |
1273 | | | Effect::ReadGpus |
1274 | | | Effect::ReadBackendDevices { .. } |
1275 | | | Effect::ReadDevices { .. } |
1276 | | | Effect::Poll(_) |
1277 | | | Effect::Focus { .. } |
1278 | | | Effect::VoicesChanged |
1279 | | | Effect::OfferBuild { .. } |
1280 | 0 | | Effect::InstallBuild { .. } => return None, |
1281 | | }; |
1282 | | Some(Input::Done( |
1283 | 0 | stage, |
1284 | 0 | change, |
1285 | 0 | result.map_err(|e| e.to_string()), |
1286 | | )) |
1287 | 0 | } |
1288 | | |
1289 | | #[cfg(test)] |
1290 | | mod tests; |