Skip to main content

bb_ops/syscalls/structural/constant/
mod.rs

1//! `Constant` syscall component - emits a constant value at
2//! bootstrap. Reads the `value` AttributeProto's TensorProto at
3//! invoke time and emits the encoded `TensorProto` bytes as a
4//! `BytesValue`. Downstream tensor-shaped consumers decode via
5//! `Tensor::from_proto` using their declared scalar type.
6
7use prost::Message;
8
9use bb_ir::proto::onnx::NodeProto;
10use bb_runtime::atomic::DispatchResult;
11use bb_runtime::bus::OpError;
12use bb_runtime::registry::OpRegistration;
13use bb_runtime::runtime::RuntimeResourceRef;
14use bb_runtime::slot_value::SlotValue;
15use bb_runtime::syscall::values::BytesValue;
16
17pub use bb_ir::syscall_ids::OP_CONSTANT as OP_TYPE;
18pub use bb_ir::syscall_ids::SYSCALL_DOMAIN as DOMAIN;
19
20/// Engine dispatch-table marker.
21pub struct ConstantOp;
22
23/// Invoke fn - emits the `value` attribute's TensorProto bytes
24/// as a `BytesValue`.
25pub fn invoke(
26    node: &NodeProto,
27    _inputs: &[(&str, &dyn SlotValue)],
28    _ctx: &mut RuntimeResourceRef<'_>,
29) -> Result<DispatchResult, OpError> {
30    let value_attr = node
31        .attribute
32        .iter()
33        .find(|a| a.name == "value")
34        .ok_or_else(|| OpError {
35            detail: "Constant requires a `value` attribute".to_string(),
36            ..Default::default()
37        })?;
38    let Some(tensor) = &value_attr.t else {
39        return Err(OpError {
40            detail: "Constant `value` attribute missing TensorProto".to_string(),
41            ..Default::default()
42        });
43    };
44    Ok(DispatchResult::Immediate(vec![(
45        "value".to_string(),
46        Box::new(BytesValue(tensor.encode_to_vec())),
47    )]))
48}
49
50inventory::submit! {
51    OpRegistration {
52        domain: DOMAIN,
53        op_type: OP_TYPE,
54        invoke,
55        kind: bb_runtime::registry::RegistrationKind::Syscall,
56    }
57}
58