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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
//! `bb::Backend` — Contract trait for tensor compute backends.
//!
//! The Contract has THREE surfaces, exposed side-by-side:
//!
//! 1. **One typed method per mandatory primitive op** (the 30
//!    entries in [`bb_ir::tensor_primitives::TENSOR_PRIMITIVES_OPS`]):
//!    `add`, `mul`, `matmul`, `reduce_sum`, `reshape`, …. Components
//!    reach these inline through `ctx.backends` for short-form
//!    tensor math — an Index's distance kernel calls
//!    `backend.matmul(&query, &vectors)?` instead of hand-rolling
//!    a loop.
//!
//! 2. **One method to execute a subgraph**:
//!    `execute(&GraphProto, HashMap<String, Tensor>, BackendAttrs) →
//!    HashMap<String, Tensor>`. Backends that prefer whole-graph
//!    dispatch override this entry point; the per-op defaults call
//!    through to it via a one-node `GraphProto`.
//!
//! 3. **One dispatch entry point for `BackendSubgraph` carriers**:
//!    `dispatch(&GraphProto, inputs, attrs, completion) →
//!    ContractResponse`. The engine calls this for every
//!    `BackendSubgraph` carrier op. The default falls through to
//!    `execute` synchronously. Backends with per-subgraph caching,
//!    JIT compilation, or async device execution override `dispatch`
//!    to return `ContractResponse::Later` while device work runs.
//!
//! ### How the two sides compose
//!
//! Default impls in [`crate::contracts::backend_default_walk`]
//! bridge the surfaces so a backend author overrides only the side
//! that's natural for their target:
//!
//! - **CpuBackend** overrides the 30 per-op methods directly
//!   (`add` runs ndarray's `Add` impl, `matmul` runs `dot`, …).
//!   It does NOT override `execute` — the default walker uses
//!   the overridden per-op methods.
//!
//! - **A Burn-style backend** overrides `execute` natively (Burn
//!   compiles the whole `GraphProto` to its own IR + runs once).
//!   It does NOT override per-op methods — they default-wrap a
//!   one-node `GraphProto` and call `execute`.
//!
//! Backends overriding *neither* side stack-overflow on the first
//! call: every per-op default wraps into `execute`, whose default
//! walks back to per-op, ad infinitum. Backends MUST override at
//! least one side.
//!
//! ### Extension ops
//!
//! Activation functions (Relu, Sigmoid, Softmax), pooling (MaxPool,
//! AveragePool), normalization (BatchNormalization, LayerNorm),
//! Conv, and so on are NOT on the Contract surface. They're
//! extensions — a backend MAY declare them via
//! [`crate::roles::BackendRuntime::extension_opsets`] and handle
//! them through its own `execute` override; OR a future lowering
//! pass decomposes them into primitives so the Contract surface
//! covers any graph.

use std::collections::HashMap;

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

use crate::completion::{CompletionHandle, ContractResponse};
use crate::contracts::backend_default_walk;

/// Per-call NodeProto context surfaced to `Backend::execute` so
/// kernels overriding the whole-graph path see the original call
/// site's attributes + metadata alongside the body. Per-op
/// methods (which receive their attributes positionally as typed
/// args) don't need this struct.
pub struct BackendAttrs<'a> {
    /// Attribute list from the call NodeProto's `attribute` field.
    pub current_node_attributes: &'a [AttributeProto],
    /// `metadata_props` from the call NodeProto.
    pub current_node_metadata: &'a [StringStringEntryProto],
}

/// User-facing Contract trait for a tensor compute backend.
///
/// The `Tensor` associated type lets backends dispatch over their
/// native storage (`Dense<f32>`, an `ndarray::ArrayD<f32>`, an
/// opaque GPU handle, …); the framework round-trips through the
/// producer/consumer `SlotValue` carriers via the derive bridge
/// in [`crate::roles::BackendRuntime`].
///
/// `Self::Tensor: Clone` is required because the per-op default
/// impls clone tensors into a temporary `HashMap<String, _>` to
/// feed [`Backend::execute`]. Backends overriding the per-op
/// methods directly never invoke this clone; backends overriding
/// `execute` natively pay one clone per per-op call. ndarray's
/// `ArrayD<f32>` clones the shape + bumps an internal refcount —
/// a few-hundred-nanosecond cost, not a memcpy.
pub trait Backend: Send + Sync {
    /// Library-maker-defined error type. The
    /// `From<BackendWalkError>` bound lets the default per-op /
    /// `execute_graph_via_per_op` walker surface graph-validation
    /// failures as typed errors instead of panicking on
    /// peer-supplied or malformed `GraphProto` bodies.
    type Error: std::error::Error
        + std::fmt::Display
        + Send
        + Sync
        + From<crate::contracts::backend_default_walk::BackendWalkError>
        + 'static;

    /// Native tensor representation.
    type Tensor: Clone + Send + Sync + 'static + bb_ir::types::Storage;

    // ──────────────────────────────────────────────────────────
    // Per-op surface — one method per primitive in
    // `TENSOR_PRIMITIVES_OPS`. Each default wraps a one-node
    // `GraphProto` and calls `execute`.
    // ──────────────────────────────────────────────────────────

    // ─── Arithmetic (6) ───────────────────────────────────────

    /// Element-wise `a + b` with NumPy broadcasting.
    fn add(&self, a: &Self::Tensor, b: &Self::Tensor) -> Result<Self::Tensor, Self::Error> {
        backend_default_walk::execute_single(self, "Add", &[a, b], Vec::new())
    }
    /// Element-wise `a - b` with NumPy broadcasting.
    fn sub(&self, a: &Self::Tensor, b: &Self::Tensor) -> Result<Self::Tensor, Self::Error> {
        backend_default_walk::execute_single(self, "Sub", &[a, b], Vec::new())
    }
    /// Element-wise `a * b` with NumPy broadcasting.
    fn mul(&self, a: &Self::Tensor, b: &Self::Tensor) -> Result<Self::Tensor, Self::Error> {
        backend_default_walk::execute_single(self, "Mul", &[a, b], Vec::new())
    }
    /// Element-wise `a / b` with NumPy broadcasting.
    fn div(&self, a: &Self::Tensor, b: &Self::Tensor) -> Result<Self::Tensor, Self::Error> {
        backend_default_walk::execute_single(self, "Div", &[a, b], Vec::new())
    }
    /// Element-wise unary negation.
    fn neg(&self, a: &Self::Tensor) -> Result<Self::Tensor, Self::Error> {
        backend_default_walk::execute_single(self, "Neg", &[a], Vec::new())
    }
    /// Element-wise absolute value.
    fn abs(&self, a: &Self::Tensor) -> Result<Self::Tensor, Self::Error> {
        backend_default_walk::execute_single(self, "Abs", &[a], Vec::new())
    }

    // ─── Math (4) ─────────────────────────────────────────────

    /// Element-wise square root.
    fn sqrt(&self, a: &Self::Tensor) -> Result<Self::Tensor, Self::Error> {
        backend_default_walk::execute_single(self, "Sqrt", &[a], Vec::new())
    }
    /// Element-wise `a ** b` with NumPy broadcasting.
    fn pow(&self, a: &Self::Tensor, b: &Self::Tensor) -> Result<Self::Tensor, Self::Error> {
        backend_default_walk::execute_single(self, "Pow", &[a, b], Vec::new())
    }
    /// Element-wise natural exponential.
    fn exp(&self, a: &Self::Tensor) -> Result<Self::Tensor, Self::Error> {
        backend_default_walk::execute_single(self, "Exp", &[a], Vec::new())
    }
    /// Element-wise natural logarithm.
    fn log(&self, a: &Self::Tensor) -> Result<Self::Tensor, Self::Error> {
        backend_default_walk::execute_single(self, "Log", &[a], Vec::new())
    }

    // ─── Linear algebra (1) ───────────────────────────────────

    /// Matrix multiplication (NumPy semantics: 2-D × 2-D + batched
    /// higher-rank broadcasting).
    fn matmul(&self, a: &Self::Tensor, b: &Self::Tensor) -> Result<Self::Tensor, Self::Error> {
        backend_default_walk::execute_single(self, "MatMul", &[a, b], Vec::new())
    }

    // ─── Reductions (4) ───────────────────────────────────────

    /// Sum-reduce `a` along `axes`. `keepdims = true` preserves
    /// the reduced dims as length-1.
    fn reduce_sum(
        &self,
        a: &Self::Tensor,
        axes: &[i64],
        keepdims: bool,
    ) -> Result<Self::Tensor, Self::Error> {
        backend_default_walk::execute_single(
            self,
            "ReduceSum",
            &[a],
            vec![
                backend_default_walk::ints_attr("axes", axes),
                backend_default_walk::int_attr("keepdims", keepdims as i64),
            ],
        )
    }
    /// Mean-reduce `a` along `axes`.
    fn reduce_mean(
        &self,
        a: &Self::Tensor,
        axes: &[i64],
        keepdims: bool,
    ) -> Result<Self::Tensor, Self::Error> {
        backend_default_walk::execute_single(
            self,
            "ReduceMean",
            &[a],
            vec![
                backend_default_walk::ints_attr("axes", axes),
                backend_default_walk::int_attr("keepdims", keepdims as i64),
            ],
        )
    }
    /// Max-reduce `a` along `axes`.
    fn reduce_max(
        &self,
        a: &Self::Tensor,
        axes: &[i64],
        keepdims: bool,
    ) -> Result<Self::Tensor, Self::Error> {
        backend_default_walk::execute_single(
            self,
            "ReduceMax",
            &[a],
            vec![
                backend_default_walk::ints_attr("axes", axes),
                backend_default_walk::int_attr("keepdims", keepdims as i64),
            ],
        )
    }
    /// Min-reduce `a` along `axes`.
    fn reduce_min(
        &self,
        a: &Self::Tensor,
        axes: &[i64],
        keepdims: bool,
    ) -> Result<Self::Tensor, Self::Error> {
        backend_default_walk::execute_single(
            self,
            "ReduceMin",
            &[a],
            vec![
                backend_default_walk::ints_attr("axes", axes),
                backend_default_walk::int_attr("keepdims", keepdims as i64),
            ],
        )
    }

    // ─── Shape (9) ────────────────────────────────────────────

    /// Reshape `a` to the given dims. Total element count must
    /// match.
    fn reshape(&self, a: &Self::Tensor, shape: &[i64]) -> Result<Self::Tensor, Self::Error> {
        backend_default_walk::execute_single(
            self,
            "Reshape",
            &[a],
            vec![backend_default_walk::ints_attr("shape", shape)],
        )
    }
    /// Transpose axes. Empty `perm` reverses all dims.
    fn transpose(&self, a: &Self::Tensor, perm: &[i64]) -> Result<Self::Tensor, Self::Error> {
        backend_default_walk::execute_single(
            self,
            "Transpose",
            &[a],
            vec![backend_default_walk::ints_attr("perm", perm)],
        )
    }
    /// Concatenate `inputs` along `axis`.
    fn concat(&self, inputs: &[&Self::Tensor], axis: i64) -> Result<Self::Tensor, Self::Error> {
        backend_default_walk::execute_single(
            self,
            "Concat",
            inputs,
            vec![backend_default_walk::int_attr("axis", axis)],
        )
    }
    /// NumPy-style slice. Empty `axes` defaults to all dims;
    /// empty `steps` defaults to 1 per axis.
    fn slice(
        &self,
        a: &Self::Tensor,
        starts: &[i64],
        ends: &[i64],
        axes: &[i64],
        steps: &[i64],
    ) -> Result<Self::Tensor, Self::Error> {
        backend_default_walk::execute_single(
            self,
            "Slice",
            &[a],
            vec![
                backend_default_walk::ints_attr("starts", starts),
                backend_default_walk::ints_attr("ends", ends),
                backend_default_walk::ints_attr("axes", axes),
                backend_default_walk::ints_attr("steps", steps),
            ],
        )
    }
    /// Split `a` along `axis` into parts of the given `sizes`.
    /// Empty `sizes` means equal-sized splits (count comes from
    /// the consumer side downstream).
    fn split(
        &self,
        a: &Self::Tensor,
        axis: i64,
        sizes: &[i64],
    ) -> Result<Vec<Self::Tensor>, Self::Error> {
        // `Split` is the only primitive returning multiple
        // tensors. We can't use `execute_single`'s single-output
        // path — instead we wrap into a graph that names each
        // output positionally and extract them.
        backend_default_walk::execute_multi(
            self,
            "Split",
            &[a],
            vec![
                backend_default_walk::int_attr("axis", axis),
                backend_default_walk::ints_attr("split", sizes),
            ],
            sizes.len(),
        )
    }
    /// Remove dimensions of size 1. Empty `axes` removes all
    /// size-1 dims.
    fn squeeze(&self, a: &Self::Tensor, axes: &[i64]) -> Result<Self::Tensor, Self::Error> {
        backend_default_walk::execute_single(
            self,
            "Squeeze",
            &[a],
            vec![backend_default_walk::ints_attr("axes", axes)],
        )
    }
    /// Insert dimensions of size 1 at the given axes.
    fn unsqueeze(&self, a: &Self::Tensor, axes: &[i64]) -> Result<Self::Tensor, Self::Error> {
        backend_default_walk::execute_single(
            self,
            "Unsqueeze",
            &[a],
            vec![backend_default_walk::ints_attr("axes", axes)],
        )
    }
    /// Identity / clone — pass-through useful for graph rewrites.
    fn identity(&self, a: &Self::Tensor) -> Result<Self::Tensor, Self::Error> {
        backend_default_walk::execute_single(self, "Identity", &[a], Vec::new())
    }
    /// Cast to the given ONNX `DataType` enum value (matches
    /// `bb_ir::proto::onnx::tensor_proto::DataType`).
    fn cast(&self, a: &Self::Tensor, dtype: i32) -> Result<Self::Tensor, Self::Error> {
        backend_default_walk::execute_single(
            self,
            "Cast",
            &[a],
            vec![backend_default_walk::int_attr("to", dtype as i64)],
        )
    }

    // ─── Comparison (3) ───────────────────────────────────────

    /// Element-wise `a == b`. Result is boolean-typed.
    fn equal(&self, a: &Self::Tensor, b: &Self::Tensor) -> Result<Self::Tensor, Self::Error> {
        backend_default_walk::execute_single(self, "Equal", &[a, b], Vec::new())
    }
    /// Element-wise `a > b`. Result is boolean-typed.
    fn greater(&self, a: &Self::Tensor, b: &Self::Tensor) -> Result<Self::Tensor, Self::Error> {
        backend_default_walk::execute_single(self, "Greater", &[a, b], Vec::new())
    }
    /// Element-wise `a < b`. Result is boolean-typed.
    fn less(&self, a: &Self::Tensor, b: &Self::Tensor) -> Result<Self::Tensor, Self::Error> {
        backend_default_walk::execute_single(self, "Less", &[a, b], Vec::new())
    }

    // ─── Conditional (1) ──────────────────────────────────────

    /// Element-wise ternary: `where cond { t } else { f }`.
    /// Named `r#where` to dodge the reserved Rust keyword.
    fn r#where(
        &self,
        cond: &Self::Tensor,
        t: &Self::Tensor,
        f: &Self::Tensor,
    ) -> Result<Self::Tensor, Self::Error> {
        backend_default_walk::execute_single(self, "Where", &[cond, t, f], Vec::new())
    }

    // ─── Creation (1) ─────────────────────────────────────────

    /// Materialize a constant from an ONNX `TensorProto`. The
    /// `value` attribute on the ONNX `Constant` op carries the
    /// data; rank, dtype, raw bytes all come from the proto.
    fn constant(&self, value: TensorProto) -> Result<Self::Tensor, Self::Error> {
        backend_default_walk::execute_single(
            self,
            "Constant",
            &[],
            vec![backend_default_walk::tensor_attr("value", value)],
        )
    }

    // ─── Indexing (1) ─────────────────────────────────────────

    /// Gather slices of `data` along `axis` indexed by `indices`.
    fn gather(
        &self,
        data: &Self::Tensor,
        indices: &Self::Tensor,
        axis: i64,
    ) -> Result<Self::Tensor, Self::Error> {
        backend_default_walk::execute_single(
            self,
            "Gather",
            &[data, indices],
            vec![backend_default_walk::int_attr("axis", axis)],
        )
    }

    // ──────────────────────────────────────────────────────────
    // Whole-graph surface — default walks `graph.node` and
    // dispatches each through the typed per-op methods above.
    // ──────────────────────────────────────────────────────────

    /// Execute every NodeProto in `graph.node` against the value
    /// env `inputs`. Returns the subset of values named in
    /// `graph.output`.
    ///
    /// `graph.node` is topologically ordered per the ONNX spec,
    /// so the default walker (a linear scan) suffices for any
    /// `GraphProto` whose ops are all in
    /// [`bb_ir::tensor_primitives::TENSOR_PRIMITIVES_OPS`]. A
    /// backend overriding this method may detect fused patterns,
    /// compile to GPU, or any other strategy.
    fn execute(
        &self,
        graph: &GraphProto,
        inputs: HashMap<String, Self::Tensor>,
        _attrs: BackendAttrs<'_>,
    ) -> Result<HashMap<String, Self::Tensor>, Self::Error> {
        backend_default_walk::execute_graph_via_per_op(self, graph, inputs)
    }

    /// Dispatch a `BackendSubgraph` carrier — the engine-facing entry
    /// point for whole-subgraph execution.
    ///
    /// The default falls through to [`Self::execute`] synchronously,
    /// keeping existing backends' behaviour identical. Backends with
    /// per-subgraph caching, JIT compilation, or async device execution
    /// override this to:
    ///
    /// - Cache the compiled subgraph by identity (e.g. graph name or
    ///   hash).
    /// - Return [`ContractResponse::Later`] and retain `completion`
    ///   while the device runs. The engine schedules other work;
    ///   the backend completes the handle from whatever runtime it
    ///   uses — `std::thread`, tokio task, custom event loop, single-
    ///   thread no-std loop.
    /// - Fall through to [`Self::execute`] on compile failure or
    ///   unsupported op.
    ///
    /// The `completion` parameter in the default impl is intentionally
    /// discarded (`let _ = completion`) because [`ContractResponse::Now`]
    /// does not retain the handle. This is correct — only overriders
    /// that return [`ContractResponse::Later`] must hold it.
    fn dispatch(
        &self,
        graph: &GraphProto,
        inputs: HashMap<String, Self::Tensor>,
        attrs: BackendAttrs<'_>,
        completion: CompletionHandle<HashMap<String, Self::Tensor>, Self::Error>,
    ) -> ContractResponse<HashMap<String, Self::Tensor>, Self::Error> {
        let _ = completion; // default doesn't retain it; signature stays for opt-in overriders
        ContractResponse::Now(self.execute(graph, inputs, attrs))
    }

    /// Materialise an inbound tensor `SlotFill` into this backend's
    /// native tensor representation.
    ///
    /// The framework has already (a) capped `bytes.len()` against the
    /// envelope's `EnvelopeCaps::max_per_fill_bytes`, (b) charged the
    /// length against `NodeConfig::ingress_byte_budget`, and (c) moved
    /// ownership of the wire bytes into this call. The backend may
    /// adopt the `Vec<u8>` directly (zero-copy via
    /// `ArrayD::from_shape_vec` when alignment permits), pull a buffer
    /// from a pool and copy in, or allocate fresh. The framework will
    /// not touch `bytes` after this call returns.
    ///
    /// The default delegates to the global wire-decoder registry: it
    /// looks up the decoder for `type_hash`, runs it on the bytes,
    /// then downcasts the resulting boxed `SlotValue` to `Self::Tensor`
    /// via the registry's `Box<dyn Any>` repackaging. Backends that
    /// have not implemented tensor pooling continue to work through
    /// this path; backends that override pay the registry hop only at
    /// override time.
    ///
    /// On `Err`, the engine drops the fill, releases the byte charge,
    /// and emits `WireReceiveError { kind: BackendMaterializeFailed }`.
    ///
    /// Ownership note: `bytes: Vec<u8>` by value (not `&[u8]` or
    /// `Cow`). This is the framework→backend handoff, NOT an external
    /// boundary — the backend lives inside the framework ecosystem
    /// and plays by the runtime contract. Principle 1a (ephemeral
    /// borrowed slices at external boundaries) does not apply here:
    /// the framework copied or owned the bytes already, and a backend
    /// that wants to adopt them (zero-copy) needs ownership.
    fn materialize_from_wire(
        &self,
        type_hash: u64,
        bytes: Vec<u8>,
    ) -> Result<Self::Tensor, Self::Error> {
        use crate::contracts::backend_default_walk::BackendWalkError;
        let decoder = bb_ir::slot_value::wire_decoder_registry()
            .get(&type_hash)
            .copied()
            .ok_or_else(|| BackendWalkError::WireMaterializeFailed {
                type_hash,
                reason: "no decoder registered for type_hash".into(),
            })?;
        let boxed = decoder(&bytes).map_err(|e| BackendWalkError::WireMaterializeFailed {
            type_hash,
            reason: e.to_string(),
        })?;
        let any = boxed.into_any_boxed();
        any.downcast::<Self::Tensor>().map(|b| *b).map_err(|_| {
            BackendWalkError::WireMaterializeFailed {
                type_hash,
                reason: "decoded carrier is not Self::Tensor".into(),
            }
            .into()
        })
    }
}