Coverage Report

Created: 2026-12-05 06:27

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
super-voice-app-core/src/rules/memory.rs
Line
Count
Source
1
// SPDX-License-Identifier: GPL-3.0-only
2
//! Whether a model looks too big for the GPU it is staged on.
3
4
use super_voice_shared::models::protocol::GpuInfo;
5
6
use crate::daemon::backends::BackendInfo;
7
use crate::state::Stage;
8
use crate::state::model_operations::ModelOperationState;
9
use crate::state::staged_picks::StagedPicks;
10
11
/// Pure VRAM-fit check for a staged load: given the staged `device`, the
12
/// model's conservative `estimated_vram_bytes`, and the primary GPU's
13
/// available bytes, returns `(needed, available)` when a **GPU** load looks
14
/// too big to fit. `None` when the framework is not a GPU one, the model
15
/// declares no estimate (online / unknown), no GPU memory is known, or it
16
/// should fit.
17
#[must_use]
18
16
pub fn vram_shortfall(
19
16
    device: Option<&str>,
20
16
    estimated_vram_bytes: u64,
21
16
    gpu_available_bytes: Option<u64>,
22
16
) -> Option<(u64, u64)> {
23
16
    if matches!(device, None | Some("cpu" | "none")) || 
estimated_vram_bytes == 09
{
24
10
        return None;
25
6
    }
26
6
    let available = gpu_available_bytes
?1
;
27
5
    (estimated_vram_bytes > available).then_some((estimated_vram_bytes, available))
28
16
}
29
30
/// [`vram_shortfall`] for transcription's staged model: its VRAM estimate
31
/// against the primary GPU's free memory, or its total when the daemon did
32
/// not report free.
33
///
34
/// Silent while the stage is loading (`operation`). The backend puts the
35
/// weights on the GPU before it builds its kernels, so the free memory polled
36
/// then already leaves out what this load has taken, and a model that fits
37
/// reads as one that doesn't.
38
#[must_use]
39
0
pub fn staged_vram_shortfall(
40
0
    operation: &ModelOperationState,
41
0
    picks: &StagedPicks,
42
0
    backend: &BackendInfo,
43
0
    gpus: &[GpuInfo],
44
0
) -> Option<(u64, u64)> {
45
0
    staged_vram_shortfall_in(Stage::Transcription, operation, picks, backend, gpus)
46
0
}
47
48
/// [`staged_vram_shortfall`] for any stage's staged model.
49
#[must_use]
50
10
pub fn staged_vram_shortfall_in(
51
10
    stage: Stage,
52
10
    operation: &ModelOperationState,
53
10
    picks: &StagedPicks,
54
10
    backend: &BackendInfo,
55
10
    gpus: &[GpuInfo],
56
10
) -> Option<(u64, u64)> {
57
10
    if matches!(operation, ModelOperationState::Loading { .. }) {
58
1
        return None;
59
9
    }
60
9
    let model_name = picks.get(stage).map(|p| p.model.as_str())
?0
;
61
10
    let model = 
backend.models.iter()9
.find(|m| m.name == model_name)
?0
;
62
9
    let gpu_available = gpus.first().map(|g| 
g.free_bytes4
.unwrap_or(
g.total_bytes4
));
63
9
    vram_shortfall(
64
9
        picks.device(stage),
65
9
        model.estimated_vram_bytes,
66
9
        gpu_available,
67
9
    )
68
10
}
69
70
#[cfg(test)]
71
mod vram_shortfall_tests {
72
    //! Pin the staged-load VRAM warning: it fires only for a GPU framework
73
    //! whose conservative estimate exceeds the GPU's available memory.
74
    //! Anything else — CPU, a fitting model, an online/unknown estimate of
75
    //! `0`, or no GPU info — stays silent.
76
    use super::*;
77
78
    const GIB: u64 = 1024 * 1024 * 1024;
79
80
    /// A GPU load bigger than available memory warns, echoing both amounts.
81
    #[test]
82
1
    fn gpu_over_budget_warns_with_amounts() {
83
1
        assert_eq!(
84
1
            vram_shortfall(Some("cuda"), 48 * GIB, Some(24 * GIB)),
85
1
            Some((48 * GIB, 24 * GIB)),
86
        );
87
1
    }
88
89
    /// A model that fits — including exactly filling memory — is silent; the
90
    /// check is strictly-greater.
91
    #[test]
92
1
    fn gpu_within_budget_is_silent() {
93
1
        assert_eq!(vram_shortfall(Some("cuda"), 8 * GIB, Some(24 * GIB)), None);
94
1
        assert_eq!(vram_shortfall(Some("cuda"), 24 * GIB, Some(24 * GIB)), None);
95
1
    }
96
97
    /// Online / unknown models declare a `0` estimate and never warn.
98
    #[test]
99
1
    fn zero_estimate_is_silent() {
100
1
        assert_eq!(vram_shortfall(Some("cuda"), 0, Some(GIB)), None);
101
1
    }
102
103
    /// The warning is GPU-specific — CPU and an unset device stay silent
104
    /// even when the estimate exceeds memory.
105
    #[test]
106
1
    fn non_gpu_devices_are_silent() {
107
1
        assert_eq!(vram_shortfall(Some("cpu"), 48 * GIB, Some(24 * GIB)), None);
108
1
        assert_eq!(vram_shortfall(None, 48 * GIB, Some(24 * GIB)), None);
109
1
    }
110
111
    /// Without known GPU memory there's nothing to judge fit against.
112
    #[test]
113
1
    fn no_gpu_info_is_silent() {
114
1
        assert_eq!(vram_shortfall(Some("cuda"), 48 * GIB, None), None);
115
1
    }
116
}