metrale_circuit/pipeline/
query.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2
3//! 2026-10-02: The "what we need" view (`met circuit precision`): the required pipeline of every
4//! planned node whose id matches a glob, with the rule and kernels that run it on the device.
5//!
6//! Owner: metrale-circuit (pipeline).
7//! Invariants:
8//! - Pure: the model and its plan arrive as values; the text is a function of them.
9//! - Every line is a node of the primary plan, in execution order; a node no kernel of the
10//!   class covers still has its requirement, marked as a gap.
11
12use std::fmt::Write as _;
13
14use crate::hardware::plan::NOVEL_EMITTER;
15use crate::hardware::{ModelUnderPlan, OnePlan};
16
17/// 2026-10-02: The required pipelines of the nodes of `one` (a plan of `model`) whose ids match
18/// `pattern` (`*` matches any run, dots included; `None` matches every node). An error when none
19/// matches.
20pub fn precision_text(
21    model: &ModelUnderPlan,
22    one: &OnePlan,
23    pattern: Option<&str>,
24) -> Result<String, String> {
25    let c = &model.circuit;
26    let plan = &one.planned.plan;
27    let mut s = String::new();
28    let _ = writeln!(
29        s,
30        "# required node pipelines: {} on {} ({}), {} n={}",
31        model.label,
32        one.resolved.device.id,
33        one.resolved.device.class,
34        plan.mode.name(),
35        plan.rows
36    );
37    let _ = writeln!(s, "# precision: {}", model.precision);
38    for (k, v) in &one.header {
39        if k == "settings" || k == "checkpoint" {
40            let _ = writeln!(s, "# {k}: {v}");
41        }
42    }
43    let _ = writeln!(
44        s,
45        "# node op: in -> [steps] -> out; (rule) marks what the routing states over the reference"
46    );
47    let mut matched = 0usize;
48    for g in &plan.groups {
49        let mut shown = false;
50        for &n in &g.nodes {
51            let node = &c.nodes[n];
52            if pattern.is_some_and(|p| !crate::precision::glob(p, &node.id)) {
53                continue;
54            }
55            let Some(line) = one.planned.pipelines.line(n) else {
56                continue;
57            };
58            matched += 1;
59            // 2026-10-02: A group's kernels once, at its first listed node; distinct, in launch
60            // order.
61            let ran = if g.emitter == NOVEL_EMITTER {
62                format!("gap: no kernel of {} covers it", one.resolved.device.class)
63            } else if shown {
64                format!("{}: the launch group above", g.rule)
65            } else if g.kernels.is_empty() {
66                format!("{}: host ({})", g.rule, g.emitter)
67            } else {
68                let mut k: Vec<String> = Vec::new();
69                for id in &g.kernels {
70                    let id = id.to_string();
71                    if !k.contains(&id) {
72                        k.push(id);
73                    }
74                }
75                format!("{}: {}", g.rule, k.join(" + "))
76            };
77            shown = true;
78            let _ = writeln!(s, "{} {}: {line}\n    <- {ran}", node.id, node.op.name());
79        }
80    }
81    if matched == 0 {
82        return Err(format!(
83            "no node of the {} n={} plan matches `{}`",
84            plan.mode.name(),
85            plan.rows,
86            pattern.unwrap_or("*")
87        ));
88    }
89    Ok(s)
90}