bb-runtime 0.3.3

Sans-IO engine for the bytesandbrains framework — Node, Engine, Framework, Backends, roles, snapshot, runtime-side syscalls.
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
//! Shared default impls for the `Backend` Contract trait.
//!
//! The Contract surface is *thirty typed per-op methods*
//! ([`super::Backend::add`], [`super::Backend::matmul`], …) plus
//! [`super::Backend::execute`] (`&GraphProto, HashMap` →
//! `HashMap`). Each side has a default body that calls into the
//! other so backend authors override whichever side is natural:
//!
//! - **Override per-op methods** (e.g. `CpuBackend` over ndarray):
//!   each Contract method is a direct kernel call. The default
//!   `execute` walks the graph node-by-node and dispatches through
//!   the overridden per-op methods.
//!
//! - **Override `execute`** (e.g. a graph-compiling backend like
//!   Burn): the whole `GraphProto` body is handed to the native
//!   execution engine. The per-op defaults wrap a single-node
//!   `GraphProto` and call back into `execute`.
//!
//! Pathological case: a backend that overrides *neither* side
//! stack-overflows — every `add` call walks into a single-node
//! graph that calls `add` again. Document loudly on the trait.
//!
//! This module also encodes the attribute conventions every
//! `BackendSubgraph_*` carrier uses for primitive ops with
//! attributes (`ReduceSum.axes`, `Reshape.shape`, `Cast.to`, …).
//! Per-op defaults call [`ints_attr`] / [`int_attr`] / [`tensor_attr`]
//! to encode; the walker calls `attr_ints` / `attr_int` /
//! `attr_tensor` to decode. ONNX-style names are preserved
//! (`axes`, `keepdims`, `shape`, `perm`, `axis`, `to`, `value`).

use std::collections::HashMap;

use bb_ir::proto::onnx::{AttributeProto, GraphProto, NodeProto, TensorProto, ValueInfoProto};

use super::backend::Backend;

const SINGLE_OP_OUTPUT_NAME: &str = "__bb_default_walk_output";

/// Failures the default walker surfaces when handed a malformed
/// `GraphProto` body or when a `Backend::execute` impl violates
/// its output-name contract. Required `From` bound on
/// [`Backend::Error`] makes the walker fail with a typed error
/// instead of `panic!`-ing on peer-supplied or buggy input.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BackendWalkError {
    /// A `NodeProto` references an input value not in the running
    /// environment. The graph either uses a value the caller didn't
    /// bind OR an upstream node failed to populate one of its
    /// declared outputs.
    MissingInput {
        /// `op_type` of the consuming node.
        op_type: String,
        /// Name of the missing input value.
        input_name: String,
    },
    /// A per-op method produced a different number of outputs than
    /// the consuming `NodeProto` declares. Indicates a compiler
    /// or graph-builder bug.
    OutputArityMismatch {
        /// `op_type` of the node.
        op_type: String,
        /// Number of outputs the per-op method produced.
        produced: usize,
        /// Number of outputs the graph declares.
        declared: usize,
    },
    /// `op_type` is not one of [`bb_ir::tensor_primitives::TENSOR_PRIMITIVES_OPS`].
    /// Backends that use the default per-op walker must keep graphs
    /// to primitives only. The wire path can hit this when an
    /// adversarial peer ships a `BackendSubgraph_*` carrier whose
    /// body references an extension op.
    UnknownOpType(String),
    /// A `Backend::execute` impl returned successfully but did not
    /// populate the declared output `output_name` in the result
    /// map. Indicates a Backend impl bug — `execute` MUST honor
    /// the graph's output names.
    MissingExecuteOutput {
        /// `op_type` of the single-node graph that was executed.
        op_type: String,
        /// Output name the graph declared but `execute` omitted.
        output_name: String,
    },
    /// Default [`crate::contracts::Backend::materialize_from_wire`]
    /// failed before reaching the backend: no registered decoder,
    /// the decoder rejected the bytes, or the decoded carrier was
    /// not assignable to `Self::Tensor`. Backends overriding
    /// `materialize_from_wire` never surface this — they emit their
    /// own typed error.
    WireMaterializeFailed {
        /// `type_hash` the inbound `SlotFill` advertised.
        type_hash: u64,
        /// Why the default path could not produce a `Self::Tensor`.
        reason: String,
    },
}

impl std::fmt::Display for BackendWalkError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::MissingInput { op_type, input_name } => write!(
                f,
                "Backend default walker: `{op_type}` references input `{input_name}` not in the value env",
            ),
            Self::OutputArityMismatch { op_type, produced, declared } => write!(
                f,
                "Backend default walker: per-op `{op_type}` produced {produced} outputs but graph declares {declared}",
            ),
            Self::UnknownOpType(op_type) => write!(
                f,
                "Backend default walker: op_type `{op_type}` is not in TENSOR_PRIMITIVES_OPS",
            ),
            Self::MissingExecuteOutput { op_type, output_name } => write!(
                f,
                "Backend::execute (op_type `{op_type}`) did not produce its declared output `{output_name}`",
            ),
            Self::WireMaterializeFailed { type_hash, reason } => write!(
                f,
                "Backend default materialize_from_wire (type_hash {type_hash:#018x}): {reason}",
            ),
        }
    }
}

impl std::error::Error for BackendWalkError {}

/// Default body for every per-op method on [`Backend`] — wraps the
/// op in a one-node `GraphProto` and routes through
/// [`Backend::execute`]. Backends that override `execute` natively
/// (graph-compiling backends) get the per-op surface free; backends
/// that override the per-op methods directly bypass this helper
/// entirely.
///
/// `attributes` carries the ONNX-style per-op attributes the typed
/// per-op signatures encode (`ReduceSum`'s `axes` + `keepdims`,
/// `Reshape`'s `shape`, etc.). The walker decodes them back via
/// `attr_ints` / `attr_int` / `attr_tensor` before calling
/// the typed per-op method on the backend.
pub fn execute_single<B: Backend + ?Sized>(
    backend: &B,
    op_type: &str,
    inputs: &[&B::Tensor],
    attributes: Vec<AttributeProto>,
) -> Result<B::Tensor, B::Error> {
    let input_names: Vec<String> = (0..inputs.len())
        .map(|i| format!("__bb_default_walk_in_{i}"))
        .collect();

    let node = NodeProto {
        op_type: op_type.to_string(),
        input: input_names.clone(),
        output: vec![SINGLE_OP_OUTPUT_NAME.to_string()],
        attribute: attributes,
        ..Default::default()
    };
    let graph = GraphProto {
        node: vec![node],
        output: vec![ValueInfoProto {
            name: SINGLE_OP_OUTPUT_NAME.to_string(),
            ..Default::default()
        }],
        ..Default::default()
    };

    let input_map: HashMap<String, B::Tensor> = input_names
        .into_iter()
        .zip(inputs.iter().map(|t| (*t).clone()))
        .collect();

    let mut output_map = backend.execute(
        &graph,
        input_map,
        super::backend::BackendAttrs {
            current_node_attributes: &[],
            current_node_metadata: &[],
        },
    )?;
    let result = output_map.remove(SINGLE_OP_OUTPUT_NAME).ok_or_else(|| {
        BackendWalkError::MissingExecuteOutput {
            op_type: op_type.to_string(),
            output_name: SINGLE_OP_OUTPUT_NAME.to_string(),
        }
    })?;
    Ok(result)
}

/// Default body for multi-output per-op methods ([`Backend::split`]
/// today). Builds a one-node `GraphProto` with `output_count`
/// positionally-named outputs, calls [`Backend::execute`], and
/// extracts the outputs in declared order.
///
/// If `output_count == 0`, returns an empty `Vec` without invoking
/// `execute` — multi-output ops with zero outputs are degenerate
/// and the engine never produces such carriers.
pub fn execute_multi<B: Backend + ?Sized>(
    backend: &B,
    op_type: &str,
    inputs: &[&B::Tensor],
    attributes: Vec<AttributeProto>,
    output_count: usize,
) -> Result<Vec<B::Tensor>, B::Error> {
    if output_count == 0 {
        return Ok(Vec::new());
    }

    let input_names: Vec<String> = (0..inputs.len())
        .map(|i| format!("__bb_default_walk_in_{i}"))
        .collect();
    let output_names: Vec<String> = (0..output_count)
        .map(|i| format!("__bb_default_walk_out_{i}"))
        .collect();

    let node = NodeProto {
        op_type: op_type.to_string(),
        input: input_names.clone(),
        output: output_names.clone(),
        attribute: attributes,
        ..Default::default()
    };
    let graph = GraphProto {
        node: vec![node],
        output: output_names
            .iter()
            .map(|n| ValueInfoProto {
                name: n.clone(),
                ..Default::default()
            })
            .collect(),
        ..Default::default()
    };

    let input_map: HashMap<String, B::Tensor> = input_names
        .into_iter()
        .zip(inputs.iter().map(|t| (*t).clone()))
        .collect();

    let mut output_map = backend.execute(
        &graph,
        input_map,
        super::backend::BackendAttrs {
            current_node_attributes: &[],
            current_node_metadata: &[],
        },
    )?;
    output_names
        .into_iter()
        .map(|n| {
            output_map.remove(&n).ok_or_else(|| {
                BackendWalkError::MissingExecuteOutput {
                    op_type: op_type.to_string(),
                    output_name: n,
                }
                .into()
            })
        })
        .collect()
}

/// Default body for [`Backend::execute`] — walks `graph.node` in
/// topological order, dispatching each through the typed per-op
/// methods on `backend`. The implementation is a tight linear scan:
/// ONNX guarantees `graph.node` is topologically ordered, so no
/// petgraph / explicit ordering is needed.
///
/// Op-types must be in [`bb_ir::tensor_primitives::TENSOR_PRIMITIVES_OPS`].
/// A graph containing extension ops (Relu, Conv, …) needs either a
/// backend that overrides `execute` natively OR a lowering pass
/// (future work) decomposing the extensions into primitives.
pub fn execute_graph_via_per_op<B: Backend + ?Sized>(
    backend: &B,
    graph: &GraphProto,
    inputs: HashMap<String, B::Tensor>,
) -> Result<HashMap<String, B::Tensor>, B::Error> {
    let mut env: HashMap<String, B::Tensor> = inputs;

    for node in &graph.node {
        let input_tensors: Vec<&B::Tensor> = node
            .input
            .iter()
            .filter(|n| !n.is_empty())
            .map(|n| {
                env.get(n).ok_or_else(|| BackendWalkError::MissingInput {
                    op_type: node.op_type.clone(),
                    input_name: n.clone(),
                })
            })
            .collect::<Result<Vec<&B::Tensor>, BackendWalkError>>()
            .map_err(B::Error::from)?;

        let outputs = dispatch_per_op(backend, &node.op_type, &input_tensors, &node.attribute)?;

        for (i, name) in node.output.iter().enumerate() {
            if name.is_empty() {
                continue;
            }
            let Some(tensor) = outputs.get(i) else {
                return Err(BackendWalkError::OutputArityMismatch {
                    op_type: node.op_type.clone(),
                    produced: outputs.len(),
                    declared: node.output.len(),
                }
                .into());
            };
            env.insert(name.clone(), tensor.clone());
        }
    }

    let mut result: HashMap<String, B::Tensor> = HashMap::new();
    for vi in &graph.output {
        if let Some(t) = env.remove(&vi.name) {
            result.insert(vi.name.clone(), t);
        }
    }
    Ok(result)
}

/// Dispatch a single `NodeProto` (whose `op_type` MUST be one of
/// the 30 `TENSOR_PRIMITIVES_OPS`) through the appropriate typed
/// per-op method on `backend`. Returns a `Vec` to handle multi-
/// output primitives (`Split`).
fn dispatch_per_op<B: Backend + ?Sized>(
    backend: &B,
    op_type: &str,
    inputs: &[&B::Tensor],
    attrs: &[AttributeProto],
) -> Result<Vec<B::Tensor>, B::Error> {
    let single = |t: B::Tensor| Ok(vec![t]);
    match op_type {
        // Arithmetic
        "Add" => single(backend.add(inputs[0], inputs[1])?),
        "Sub" => single(backend.sub(inputs[0], inputs[1])?),
        "Mul" => single(backend.mul(inputs[0], inputs[1])?),
        "Div" => single(backend.div(inputs[0], inputs[1])?),
        "Neg" => single(backend.neg(inputs[0])?),
        "Abs" => single(backend.abs(inputs[0])?),
        // Math
        "Sqrt" => single(backend.sqrt(inputs[0])?),
        "Pow" => single(backend.pow(inputs[0], inputs[1])?),
        "Exp" => single(backend.exp(inputs[0])?),
        "Log" => single(backend.log(inputs[0])?),
        // Linear algebra
        "MatMul" => single(backend.matmul(inputs[0], inputs[1])?),
        // Reductions
        "ReduceSum" => single(backend.reduce_sum(
            inputs[0],
            &attr_ints(attrs, "axes"),
            attr_int(attrs, "keepdims", 1) != 0,
        )?),
        "ReduceMean" => single(backend.reduce_mean(
            inputs[0],
            &attr_ints(attrs, "axes"),
            attr_int(attrs, "keepdims", 1) != 0,
        )?),
        "ReduceMax" => single(backend.reduce_max(
            inputs[0],
            &attr_ints(attrs, "axes"),
            attr_int(attrs, "keepdims", 1) != 0,
        )?),
        "ReduceMin" => single(backend.reduce_min(
            inputs[0],
            &attr_ints(attrs, "axes"),
            attr_int(attrs, "keepdims", 1) != 0,
        )?),
        // Shape
        "Reshape" => single(backend.reshape(inputs[0], &attr_ints(attrs, "shape"))?),
        "Transpose" => single(backend.transpose(inputs[0], &attr_ints(attrs, "perm"))?),
        "Concat" => single(backend.concat(inputs, attr_int(attrs, "axis", 0))?),
        "Slice" => single(backend.slice(
            inputs[0],
            &attr_ints(attrs, "starts"),
            &attr_ints(attrs, "ends"),
            &attr_ints(attrs, "axes"),
            &attr_ints(attrs, "steps"),
        )?),
        "Split" => Ok(backend.split(
            inputs[0],
            attr_int(attrs, "axis", 0),
            &attr_ints(attrs, "split"),
        )?),
        "Squeeze" => single(backend.squeeze(inputs[0], &attr_ints(attrs, "axes"))?),
        "Unsqueeze" => single(backend.unsqueeze(inputs[0], &attr_ints(attrs, "axes"))?),
        "Identity" => single(backend.identity(inputs[0])?),
        "Cast" => single(backend.cast(inputs[0], attr_int(attrs, "to", 1) as i32)?),
        // Comparison
        "Equal" => single(backend.equal(inputs[0], inputs[1])?),
        "Greater" => single(backend.greater(inputs[0], inputs[1])?),
        "Less" => single(backend.less(inputs[0], inputs[1])?),
        // Conditional
        "Where" => single(backend.r#where(inputs[0], inputs[1], inputs[2])?),
        // Creation
        "Constant" => single(backend.constant(attr_tensor(attrs, "value").unwrap_or_default())?),
        // Indexing
        "Gather" => single(backend.gather(inputs[0], inputs[1], attr_int(attrs, "axis", 0))?),
        other => Err(BackendWalkError::UnknownOpType(other.to_string()).into()),
    }
}

// ──────────────────────────────────────────────────────────────────
// Attribute encoders — called from the Contract per-op defaults.
// ──────────────────────────────────────────────────────────────────

/// Build an `AttributeProto` of type `INT` (per ONNX
/// [`AttributeType::Int`]). Used by the Contract per-op defaults
/// for scalar `i64` attributes (`axis`, `to`, `keepdims`).
pub fn int_attr(name: &str, value: i64) -> AttributeProto {
    AttributeProto {
        name: name.to_string(),
        r#type: bb_ir::proto::onnx::attribute_proto::AttributeType::Int as i32,
        i: value,
        ..Default::default()
    }
}

/// Build an `AttributeProto` of type `INTS`. Used for vector
/// attributes (`axes`, `shape`, `perm`, `starts`, `ends`, `steps`,
/// `split`).
pub fn ints_attr(name: &str, values: &[i64]) -> AttributeProto {
    AttributeProto {
        name: name.to_string(),
        r#type: bb_ir::proto::onnx::attribute_proto::AttributeType::Ints as i32,
        ints: values.to_vec(),
        ..Default::default()
    }
}

/// Build an `AttributeProto` of type `TENSOR`. Used for
/// `Constant`'s `value` attribute.
pub fn tensor_attr(name: &str, tensor: TensorProto) -> AttributeProto {
    AttributeProto {
        name: name.to_string(),
        r#type: bb_ir::proto::onnx::attribute_proto::AttributeType::Tensor as i32,
        t: Some(tensor),
        ..Default::default()
    }
}

// ──────────────────────────────────────────────────────────────────
// Attribute decoders — called from the walker.
// ──────────────────────────────────────────────────────────────────

fn attr_int(attrs: &[AttributeProto], name: &str, default: i64) -> i64 {
    attrs
        .iter()
        .find(|a| a.name == name)
        .map(|a| a.i)
        .unwrap_or(default)
}

fn attr_ints(attrs: &[AttributeProto], name: &str) -> Vec<i64> {
    attrs
        .iter()
        .find(|a| a.name == name)
        .map(|a| a.ints.clone())
        .unwrap_or_default()
}

fn attr_tensor(attrs: &[AttributeProto], name: &str) -> Option<TensorProto> {
    attrs
        .iter()
        .find(|a| a.name == name)
        .and_then(|a| a.t.clone())
}