ovfetch 0.2.0

Resolve, download, and hash-verify the OpenVINO-enabled ONNX Runtime an Intel NPU needs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
//! Turning "the right OpenVINO for this machine" into one concrete, hash-checked download.
//!
//! Only prebuilt ONNX Runtime + OpenVINO builds are ever installed. When none
//! fits the device, resolution fails and says why; ovfetch never compiles.

use crate::consensus::{self, Trust};
use crate::data::{Data, Platform};
use crate::detect::Machine;
use crate::sources::{self, PypiFile};
use crate::version::Ver;
use anyhow::{Result, bail};
use serde::Serialize;

#[derive(Debug, Serialize)]
pub struct Floor {
    /// `None` when nothing constrains it (no NPU).
    pub openvino: Option<String>,
    pub reason: String,
}

/// Intel's onnxruntime-openvino wheel: ONNX Runtime and the OpenVINO EP,
/// built together against the OpenVINO it bundles.
#[derive(Debug, Clone, Serialize)]
pub struct Artifact {
    pub id: String,
    pub onnxruntime: String,
    pub url: String,
    pub sha256: String,
    pub trust: Trust,
}

#[derive(Debug, Serialize)]
pub struct Npu {
    pub driver_installed: Option<String>,
    pub driver_required: Option<String>,
    pub compiler_present: bool,
}

#[derive(Debug, Serialize)]
pub struct Plan {
    pub floor: Floor,
    /// Newest OpenVINO the installed NPU driver pairs with, when there is an NPU.
    pub driver_ceiling: Option<String>,
    pub openvino: String,
    pub artifact: Artifact,
    pub npu: Npu,
    pub warnings: Vec<String>,
}

pub fn floor(machine: &Machine, data: &Data, override_min: Option<&str>) -> Floor {
    if let Some(v) = override_min {
        return Floor {
            openvino: Some(v.to_owned()),
            reason: "--min-openvino".into(),
        };
    }
    let mut best: Option<(Ver, String)> = None;
    for npu in machine.devices.iter().filter(|d| d.kind == "npu") {
        let platform = data.platforms.list.iter().find(|p| p.pci_id == npu.pci_id);
        match platform.and_then(|p| Some((p, Ver::parse(p.min_openvino.as_deref()?)?))) {
            Some((p, v)) if best.as_ref().is_none_or(|(b, _)| v > *b) => {
                best = Some((v, format!("{} NPU ({})", p.codename, p.pci_id)));
            }
            Some(_) => {}
            None => {
                return Floor {
                    openvino: None,
                    reason: format!("NPU {} has no known floor yet", npu.pci_id),
                };
            }
        }
    }
    match best {
        Some((v, reason)) => Floor {
            openvino: Some(v.to_string()),
            reason,
        },
        None => Floor {
            openvino: None,
            reason: "no Intel NPU; any current OpenVINO runs the CPU and GPU".into(),
        },
    }
}

/// Newest OpenVINO known to work on the installed NPU driver: the newest
/// recorded release at or below it, at Intel's pairing or a newer measured
/// version. Deliberately pessimistic: beyond it is untested, not known to fail.
pub fn driver_ceiling(machine: &Machine, data: &Data) -> Option<Ver> {
    if !machine.devices.iter().any(|d| d.kind == "npu") {
        return None;
    }
    let installed = Ver::parse(machine.npu_driver.as_deref()?)?;
    data.npu_drivers
        .list
        .iter()
        .filter(|d| Ver::parse(&d.version).is_some_and(|v| v <= installed))
        .max_by_key(|d| Ver::parse(&d.version))
        .and_then(|d| d.max_openvino())
        .map(|v| v.minor())
}

fn artifact(file: &PypiFile, data: &Data) -> Result<Artifact> {
    let claims = sources::pypi_claims(file, &sources::mirror_pages(&file.project));
    let (sha256, trust) = consensus::agree(&file.ledger_id(), &claims, &data.ledger)?;
    Ok(Artifact {
        id: file.ledger_id(),
        onnxruntime: file.version.clone(),
        url: file.url.clone(),
        sha256,
        trust,
    })
}

pub fn resolve(
    machine: &Machine,
    data: &Data,
    override_min: Option<&str>,
    ignore_driver: bool,
) -> Result<Plan> {
    let floor = floor(machine, data, override_min);
    let mut warnings = Vec::new();
    let ceiling = if ignore_driver {
        None
    } else {
        driver_ceiling(machine, data)
    };
    let Some(need) = floor
        .openvino
        .as_deref()
        .map(Ver::parse)
        .unwrap_or(Some(Ver(vec![0])))
    else {
        bail!("bad floor {:?}", floor.openvino);
    };
    if floor.reason.contains("no known floor") {
        bail!(
            "{}. This platform is newer than ovfetch's data, so it cannot pick a safe OpenVINO. \
             Pass --min-openvino, or wait for `ci discover` to record it.",
            floor.reason
        );
    }
    if let Some(c) = &ceiling
        && *c < need
    {
        bail!(
            "this device needs OpenVINO {need}, but nothing newer than {c} is known to work with NPU driver {}. \
             Upgrade the driver (github.com/intel/linux-npu-driver/releases), or pass --ignore-driver",
            machine.npu_driver.as_deref().unwrap_or("?")
        );
    }

    // Wheels bundle ever-newer OpenVINO, so walk newest first and stop once
    // one falls below the floor.
    let wheels = sources::pypi_wheels("onnxruntime-openvino")?;
    let mut versions: Vec<&str> = wheels.iter().map(|w| w.version.as_str()).collect();
    versions.dedup();
    let mut newest_bundled = None;
    let mut chosen = None;
    for v in versions {
        let Some(w) = sources::pick_wheel(&wheels, v) else {
            continue;
        };
        let Some(ov) = sources::bundled_openvino(&w)? else {
            continue;
        };
        newest_bundled.get_or_insert(ov.clone());
        if ov.minor() < need {
            break;
        }
        if ceiling.as_ref().is_none_or(|c| ov.minor() <= *c) {
            chosen = Some((w, ov));
            break;
        }
    }
    let Some((wheel, openvino)) = chosen else {
        let newest = newest_bundled
            .map(|v| v.to_string())
            .unwrap_or_else(|| "none".into());
        bail!(
            "no prebuilt ONNX Runtime + OpenVINO fits this device: it needs OpenVINO >= {need}{}, \
             and the newest prebuilt build bundles {newest}. ovfetch does not compile ONNX Runtime; \
             this resolves once Intel publishes a newer onnxruntime-openvino wheel.",
            ceiling
                .as_ref()
                .map(|c| format!(" and <= {c} (NPU driver)"))
                .unwrap_or_default()
        );
    };

    if let Some(c) = &ceiling
        && newest_bundled.as_ref().is_some_and(|n| n.minor() > *c)
        && let Some(d) = data
            .npu_drivers
            .list
            .iter()
            .filter(|d| d.max_openvino().is_some_and(|o| o.minor() > *c))
            .max_by_key(|d| Ver::parse(&d.version))
    {
        warnings.push(format!(
            "held at OpenVINO {c}, the newest known to work with NPU driver {}; driver {} is known to work with OpenVINO {}",
            machine.npu_driver.as_deref().unwrap_or("?"),
            d.version,
            d.max_openvino().map(|v| v.to_string()).unwrap_or_default()
        ));
    }

    let artifact = artifact(&wheel, data)?;
    let npu = npu_requirements(machine, data, &openvino, &mut warnings);
    Ok(Plan {
        floor,
        driver_ceiling: ceiling.map(|c| c.to_string()),
        openvino: openvino.to_string(),
        artifact,
        npu,
        warnings,
    })
}

/// What `detect` adds to the machine: the platform's data and what it implies,
/// with no network access.
#[derive(Debug, Serialize)]
pub struct Status<'a> {
    #[serde(flatten)]
    pub machine: &'a Machine,
    pub platforms: Vec<&'a Platform>,
    /// Newest OpenVINO known to work on the installed NPU driver.
    pub openvino_ceiling: Option<String>,
    /// Intel's first driver release verified on the platform.
    pub npu_driver_required: Option<String>,
    pub warnings: Vec<String>,
}

pub fn status<'a>(machine: &'a Machine, data: &'a Data) -> Status<'a> {
    let npus: Vec<_> = machine.devices.iter().filter(|d| d.kind == "npu").collect();
    let platforms: Vec<&Platform> = data
        .platforms
        .list
        .iter()
        .filter(|p| npus.iter().any(|n| n.pci_id == p.pci_id))
        .collect();
    let mut warnings = Vec::new();
    for n in &npus {
        if !platforms
            .iter()
            .any(|p| p.pci_id == n.pci_id && p.min_npu_driver.is_some())
        {
            warnings.push(format!(
                "NPU {} is not in ovfetch's data yet, so nothing is known to work on it",
                n.pci_id
            ));
        }
    }
    let required = platforms
        .iter()
        .filter_map(|p| p.min_npu_driver.as_deref().and_then(Ver::parse))
        .max();
    if !npus.is_empty() {
        let installed = machine.npu_driver.as_deref().and_then(Ver::parse);
        match (&installed, &required) {
            (None, _) => warnings.push(
                "no NPU user-mode driver (libze_intel_npu.so.1) is on the linker path (github.com/intel/linux-npu-driver/releases)".into(),
            ),
            (Some(i), Some(r)) if i < r => warnings.push(format!(
                "NPU driver {i} is older than {r}, Intel's first release verified on this platform (github.com/intel/linux-npu-driver/releases)"
            )),
            _ => {}
        }
        if installed.is_some()
            && let Some(w) = compiler_warning(machine)
        {
            warnings.push(w);
        }
    }
    Status {
        machine,
        platforms,
        openvino_ceiling: driver_ceiling(machine, data).map(|v| v.to_string()),
        npu_driver_required: required.map(|v| v.to_string()),
        warnings,
    }
}

fn compiler_warning(machine: &Machine) -> Option<String> {
    if machine.npu_compiler {
        return None;
    }
    let needs: Vec<String> = machine
        .npu_compiler_needs
        .iter()
        .map(|set| set.join(" + "))
        .collect();
    Some(if needs.is_empty() {
        "the NPU driver names no compiler library ovfetch recognises; assuming it cannot compile models".into()
    } else {
        format!(
            "the NPU driver cannot compile models: it loads {}, and that is not on the linker path",
            needs.join(", or ")
        )
    })
}

/// The oldest driver known to work with `ov`, raised to the platform's
/// first verified driver.
fn npu_requirements(machine: &Machine, data: &Data, ov: &Ver, warnings: &mut Vec<String>) -> Npu {
    let npus: Vec<_> = machine.devices.iter().filter(|d| d.kind == "npu").collect();
    let mut required: Option<Ver> = data
        .npu_drivers
        .list
        .iter()
        .filter(|d| d.max_openvino().is_some_and(|o| o.minor() >= ov.minor()))
        .filter_map(|d| Ver::parse(&d.version))
        .min();
    if required.is_none() && !npus.is_empty() {
        required = data
            .npu_drivers
            .list
            .iter()
            .filter_map(|d| Ver::parse(&d.version))
            .max();
        warnings.push(format!(
            "no NPU driver release is known to work with OpenVINO {ov} yet; the NPU may fail to compile models"
        ));
    }
    for p in data
        .platforms
        .list
        .iter()
        .filter(|p| npus.iter().any(|n| n.pci_id == p.pci_id))
    {
        if let Some(min) = p.min_npu_driver.as_deref().and_then(Ver::parse) {
            required = required.max(Some(min));
        }
    }
    if npus.is_empty() {
        required = None;
    }
    let installed = machine.npu_driver.clone();
    if let Some(req) = &required {
        if installed
            .as_deref()
            .and_then(Ver::parse)
            .is_none_or(|i| i < *req)
        {
            warnings.push(format!(
                "NPU driver {} is older than {req}, the first known to work with OpenVINO {ov} on this platform (github.com/intel/linux-npu-driver/releases)",
                installed.as_deref().unwrap_or("(none)")
            ));
        }
        if let Some(w) = compiler_warning(machine) {
            warnings.push(w);
        }
    }
    Npu {
        driver_installed: installed,
        driver_required: required.map(|v| v.to_string()),
        compiler_present: machine.npu_compiler,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::data::{Ledger, NpuDriver, NpuDrivers, Platforms};
    use crate::detect::Device;

    fn driver(version: &str, openvino: &str, measured: Option<&str>) -> NpuDriver {
        NpuDriver {
            version: version.into(),
            openvino: openvino.into(),
            platforms: vec![],
            measured_openvino: measured.map(Into::into),
            measured_note: None,
        }
    }

    fn data(drivers: Vec<NpuDriver>) -> Data {
        Data {
            dir: None,
            platforms: Platforms::default(),
            npu_drivers: NpuDrivers { list: drivers },
            ledger: Ledger::default(),
        }
    }

    fn npu_machine(driver: &str) -> Machine {
        Machine {
            devices: vec![Device {
                kind: "npu",
                pci_id: "0xfd3e".into(),
                slot: "0000:00:0b.0".into(),
            }],
            npu_driver: Some(driver.into()),
            npu_compiler: true,
            npu_compiler_needs: vec![],
        }
    }

    fn ceiling(driver: &str, d: &Data) -> Option<String> {
        driver_ceiling(&npu_machine(driver), d).map(|v| v.to_string())
    }

    #[test]
    fn ceiling_is_the_pairing_without_a_measurement() {
        let d = data(vec![driver("1.32.0", "2026.0", None)]);
        assert_eq!(ceiling("1.32.0", &d).as_deref(), Some("2026.0"));
    }

    #[test]
    fn a_measured_openvino_raises_the_ceiling() {
        let d = data(vec![driver("1.35.0", "2026.2", Some("2026.4"))]);
        assert_eq!(ceiling("1.35.0", &d).as_deref(), Some("2026.4"));
    }

    #[test]
    fn a_newer_driver_does_not_inherit_an_older_ones_measurement() {
        let d = data(vec![
            driver("1.35.0", "2026.2", Some("2026.4")),
            driver("1.38.0", "2026.3.1", None),
        ]);
        assert_eq!(ceiling("1.38.0", &d).as_deref(), Some("2026.3"));
    }

    #[test]
    fn an_unrecorded_driver_uses_the_newest_release_below_it() {
        let d = data(vec![driver("1.35.0", "2026.2", Some("2026.4"))]);
        assert_eq!(ceiling("1.36.0", &d).as_deref(), Some("2026.4"));
    }

    #[test]
    fn drivers_newer_than_the_installed_one_do_not_count() {
        let d = data(vec![
            driver("1.32.0", "2026.0", None),
            driver("1.35.0", "2026.2", Some("2026.4")),
        ]);
        assert_eq!(ceiling("1.33.0", &d).as_deref(), Some("2026.0"));
    }

    #[test]
    fn shipped_data_puts_driver_1_35_at_2026_4() {
        let d = Data::load(None).unwrap();
        assert_eq!(ceiling("1.35.0", &d).as_deref(), Some("2026.4"));
    }
}