Skip to main content

vyre_libs/
builder.rs

1//! Shared helpers used by the per-op Cat-A builders.
2//!
3//! Each op in `vyre-libs` ships a chainable builder that:
4//!
5//! 1. Accepts [`TensorRef`]s instead of bare `&str` buffer names, so
6//!    dtype + shape mismatches fail at `build()` time.
7//! 2. Checks every pair of buffer names is unique.
8//! 3. Verifies every [`TensorRef`]'s dtype against the op's expected dtype.
9//! 4. Verifies element-count overflow.
10//! 5. Allows chained overrides (workgroup size, region generator,
11//!    tenant id) without churning the function signature  -  extension
12//!    fields live inside a `#[non_exhaustive]` options struct so new
13//!    knobs never break existing call sites.
14//!
15//! `BuildOptions` is intentionally small at launch; fields are added
16//! rather than removed (the `#[non_exhaustive]` attribute enforces
17//! this). Every Cat-A op exposes its builder as `<Op>Builder::new(...)`
18//! and delegates defaults through `BuildOptions::default()`.
19
20use vyre::ir::{BufferDecl, DataType, Expr, Node, Program};
21use vyre_foundation::ir::model::expr::GeneratorRef;
22
23use crate::tensor_ref::{TensorRef, TensorRefError};
24
25/// Shared child region for one-output indexed maps.
26///
27/// This is the kernel skeleton behind embedding lookup, byte shuffles,
28/// quant pack/unpack, and similar data-layout transforms:
29/// `for i in 0..n { out[dst(i)] = value(i) }`.
30pub(crate) const INDEXED_MAP_OP_ID: &str = "vyre-libs::substrate::indexed_map";
31/// Shared child region for strided per-lane workgroup accumulators.
32pub(crate) const STRIDED_ACCUMULATE_OP_ID: &str = "vyre-libs::substrate::strided_accumulate";
33/// Shared child region for strided writeback after a tiled row reduction.
34pub(crate) const STRIDED_WRITEBACK_OP_ID: &str =
35    "anonymous::vyre-libs::substrate::strided_writeback";
36
37/// Shared options every Cat-A builder threads through. Lives here so
38/// every op agrees on the same surface.
39#[derive(Debug, Clone, Default)]
40#[non_exhaustive]
41pub struct BuildOptions {
42    /// Workgroup size override. `None` = op's canonical default.
43    pub workgroup_size: Option<[u32; 3]>,
44    /// Region generator override. `None` = op's canonical `"vyre-libs::…"`
45    /// identifier. Used when a downstream crate wraps a Cat-A op and
46    /// wants its own generator id in conformance certificates.
47    pub region_generator: Option<&'static str>,
48    /// Tenant id baked into the region metadata for multi-tenant
49    /// deployments. Routed through the megakernel's tenant-mask table
50    /// when the Program runs inside `vyre-runtime`.
51    pub tenant_id: Option<u32>,
52}
53
54impl BuildOptions {
55    /// Fluent constructor  -  start with defaults and chain overrides.
56    #[must_use]
57    pub fn new() -> Self {
58        Self::default()
59    }
60
61    /// Override the workgroup size.
62    #[must_use]
63    pub fn with_workgroup_size(mut self, size: [u32; 3]) -> Self {
64        self.workgroup_size = Some(size);
65        self
66    }
67
68    /// Override the region generator name (must be `&'static str`).
69    #[must_use]
70    pub fn with_region_generator(mut self, name: &'static str) -> Self {
71        self.region_generator = Some(name);
72        self
73    }
74
75    /// Stamp a tenant id into the Cat-A op's region metadata.
76    #[must_use]
77    pub fn with_tenant_id(mut self, tenant_id: u32) -> Self {
78        self.tenant_id = Some(tenant_id);
79        self
80    }
81}
82
83macro_rules! impl_cat_a_builder_options {
84    ($builder:ident) => {
85        impl $builder {
86            /// Override the generated Program workgroup size.
87            #[must_use]
88            pub fn with_workgroup_size(mut self, size: [u32; 3]) -> Self {
89                self.options = self.options.with_workgroup_size(size);
90                self
91            }
92
93            /// Override the Region generator id.
94            #[must_use]
95            pub fn with_region_generator(mut self, name: &'static str) -> Self {
96                self.options = self.options.with_region_generator(name);
97                self
98            }
99
100            /// Stamp the Region metadata with a tenant id.
101            #[must_use]
102            pub fn with_tenant_id(mut self, tenant_id: u32) -> Self {
103                self.options = self.options.with_tenant_id(tenant_id);
104                self
105            }
106        }
107    };
108}
109
110pub(crate) use impl_cat_a_builder_options;
111
112/// Validate a slice of `TensorRef`s against an expected `DataType`
113/// for each position, plus name-uniqueness across the whole slice.
114/// Used by every op's `build()` to consolidate the fanout of checks.
115pub fn check_tensors(
116    op: &'static str,
117    tensors: &[(&TensorRef, DataType)],
118) -> Result<(), TensorRefError> {
119    // Dtype check per tensor.
120    for (r, expected) in tensors {
121        crate::tensor_ref::check_dtype(r, expected.clone(), op)?;
122        if r.element_count().is_none() {
123            return Err(TensorRefError::ElementCountOverflow {
124                name: r.name.as_str().to_string(),
125                shape: r.shape.to_vec(),
126            });
127        }
128    }
129    for (idx, (left, _)) in tensors.iter().enumerate() {
130        for (right, _) in &tensors[idx + 1..] {
131            if left.name_str() == right.name_str() {
132                return Err(TensorRefError::NameCollision {
133                    name: left.name.as_str().to_string(),
134                    op,
135                });
136            }
137        }
138    }
139    Ok(())
140}
141
142#[cfg(test)]
143mod cat_a_builder_option_macro_tests {
144    #![allow(unreachable_pub)]
145
146    use super::BuildOptions;
147
148    #[derive(Debug, Clone)]
149    struct DemoBuilder {
150        options: BuildOptions,
151    }
152
153    impl DemoBuilder {
154        fn new() -> Self {
155            Self {
156                options: BuildOptions::default(),
157            }
158        }
159    }
160
161    super::impl_cat_a_builder_options!(DemoBuilder);
162
163    #[test]
164    fn generated_option_surface_threads_every_shared_knob() {
165        let builder = DemoBuilder::new()
166            .with_workgroup_size([8, 4, 2])
167            .with_region_generator("custom::generator")
168            .with_tenant_id(17);
169
170        assert_eq!(builder.options.workgroup_size, Some([8, 4, 2]));
171        assert_eq!(builder.options.region_generator, Some("custom::generator"));
172        assert_eq!(builder.options.tenant_id, Some(17));
173    }
174}
175
176/// Build the canonical one-output indexed-map skeleton.
177///
178/// Callers provide buffer declarations plus the semantic mapping from logical
179/// element `i` to `(dst_index, value)`. The loop, bounds guard, invocation id,
180/// workgroup default, and composition region stay centralized.
181pub(crate) fn build_indexed_map<F>(
182    op_id: &'static str,
183    buffers: Vec<BufferDecl>,
184    output: &str,
185    count: u32,
186    workgroup_size: [u32; 3],
187    f: F,
188) -> Program
189where
190    F: FnOnce(Expr) -> (Expr, Expr),
191{
192    let i = Expr::var("i");
193    let (dst_index, value) = f(i.clone());
194    let child_body = vec![
195        Node::let_bind("i", Expr::InvocationId { axis: 0 }),
196        Node::if_then(
197            Expr::lt(i, Expr::u32(count)),
198            vec![Node::store(output, dst_index, value)],
199        ),
200    ];
201    let parent = GeneratorRef {
202        name: op_id.to_string(),
203    };
204
205    Program::wrapped(
206        buffers,
207        workgroup_size,
208        vec![crate::region::wrap_anonymous(
209            op_id,
210            vec![crate::region::wrap_child(
211                INDEXED_MAP_OP_ID,
212                parent,
213                child_body,
214            )],
215        )],
216    )
217}
218
219/// Build a shared strided single-accumulator child region.
220///
221/// The parent must bind `local = LocalId(0)` before this child. The child
222/// accumulates `i = chunk * tile + local` for `chunk in 0..chunks`, guards
223/// `i < n`, and stores the lane-local accumulator into `scratch[local]`.
224pub(crate) fn strided_accumulate_child<F>(
225    parent_op_id: &'static str,
226    tile: u32,
227    chunks: u32,
228    n: u32,
229    acc_name: &'static str,
230    initial: Expr,
231    scratch: &'static str,
232    step: F,
233) -> Node
234where
235    F: Fn(Expr, Expr) -> Expr,
236{
237    let local = Expr::var("local");
238    let idx = Expr::var("idx");
239    let acc = Expr::var(acc_name);
240    let child_body = vec![Node::if_then(
241        Expr::is_first_workgroup(),
242        vec![
243            Node::let_bind(acc_name, initial),
244            strided_loop(
245                tile,
246                chunks,
247                n,
248                vec![Node::assign(acc_name, step(idx, acc))],
249            ),
250            Node::store(scratch, local, Expr::var(acc_name)),
251        ],
252    )];
253
254    child_region(parent_op_id, STRIDED_ACCUMULATE_OP_ID, child_body)
255}
256
257/// Build a shared strided dual-accumulator child region.
258///
259/// This keeps paired reductions such as `(sum, sum_sq)` in one memory pass
260/// instead of forcing two separate scans over the input.
261#[allow(dead_code)]
262pub(crate) fn strided_accumulate2_child<F1, F2>(
263    parent_op_id: &'static str,
264    tile: u32,
265    chunks: u32,
266    n: u32,
267    first: (&'static str, Expr, &'static str, F1),
268    second: (&'static str, Expr, &'static str, F2),
269) -> Node
270where
271    F1: Fn(Expr, Expr) -> Expr,
272    F2: Fn(Expr, Expr) -> Expr,
273{
274    let (first_name, first_initial, first_scratch, first_step) = first;
275    let (second_name, second_initial, second_scratch, second_step) = second;
276    let local = Expr::var("local");
277    let idx = Expr::var("idx");
278    let child_body = vec![Node::if_then(
279        Expr::is_first_workgroup(),
280        vec![
281            Node::let_bind(first_name, first_initial),
282            Node::let_bind(second_name, second_initial),
283            strided_loop(
284                tile,
285                chunks,
286                n,
287                vec![
288                    Node::assign(first_name, first_step(idx.clone(), Expr::var(first_name))),
289                    Node::assign(second_name, second_step(idx, Expr::var(second_name))),
290                ],
291            ),
292            Node::store(first_scratch, local.clone(), Expr::var(first_name)),
293            Node::store(second_scratch, local, Expr::var(second_name)),
294        ],
295    )];
296
297    child_region(parent_op_id, STRIDED_ACCUMULATE_OP_ID, child_body)
298}
299
300/// Build a shared strided writeback child region.
301///
302/// The parent must bind `local = LocalId(0)` before this child. Optional
303/// `prelude` nodes run once in workgroup zero before the strided write loop,
304/// which lets row reductions load reduced scalars exactly once per lane.
305pub(crate) fn strided_writeback_child<F>(
306    parent_op_id: &'static str,
307    tile: u32,
308    chunks: u32,
309    n: u32,
310    output: &str,
311    prelude: Vec<Node>,
312    value: F,
313) -> Node
314where
315    F: Fn(Expr) -> Expr,
316{
317    let idx = Expr::var("idx");
318    let mut guarded = prelude;
319    guarded.push(strided_loop(
320        tile,
321        chunks,
322        n,
323        vec![Node::store(output, idx.clone(), value(idx))],
324    ));
325    child_region(
326        parent_op_id,
327        STRIDED_WRITEBACK_OP_ID,
328        vec![Node::if_then(Expr::is_first_workgroup(), guarded)],
329    )
330}
331
332fn strided_loop(tile: u32, chunks: u32, n: u32, guarded_body: Vec<Node>) -> Node {
333    Node::loop_for(
334        "chunk",
335        Expr::u32(0),
336        Expr::u32(chunks),
337        vec![
338            Node::let_bind(
339                "idx",
340                Expr::add(
341                    Expr::mul(Expr::var("chunk"), Expr::u32(tile)),
342                    Expr::var("local"),
343                ),
344            ),
345            Node::if_then(Expr::lt(Expr::var("idx"), Expr::u32(n)), guarded_body),
346        ],
347    )
348}
349
350fn child_region(parent_op_id: &'static str, child_op_id: &'static str, body: Vec<Node>) -> Node {
351    crate::region::wrap_child(
352        child_op_id,
353        GeneratorRef {
354            name: parent_op_id.to_string(),
355        },
356        body,
357    )
358}
359
360/// Build a scalar-output trap program for invalid Cat-A builder inputs.
361///
362/// This keeps public compatibility wrappers infallible without panicking on
363/// user-controlled names or shapes. Typed builders should still return
364/// `Result`; this helper is for legacy `fn foo(...) -> Program` surfaces.
365#[allow(dead_code)]
366pub(crate) fn invalid_output_program(
367    op_id: &'static str,
368    output: &str,
369    data_type: DataType,
370    message: String,
371) -> Program {
372    Program::wrapped(
373        vec![BufferDecl::output(output, 0, data_type).with_count(1)],
374        [1, 1, 1],
375        vec![crate::region::wrap_anonymous(
376            op_id,
377            vec![Node::trap(Expr::u32(0), message)],
378        )],
379    )
380}
381
382/// Tensor-ref elementwise binary builder, used by `math::avg_floor`,
383/// `math::algebra`, and other binary-arithmetic primitives.
384#[allow(dead_code)]
385pub(crate) fn build_elementwise_binary<F>(
386    op_id: &'static str,
387    a: crate::tensor_ref::TensorRef,
388    b: crate::tensor_ref::TensorRef,
389    out: crate::tensor_ref::TensorRef,
390    options: BuildOptions,
391    f: F,
392) -> Result<vyre::ir::Program, crate::tensor_ref::TensorRefError>
393where
394    F: Fn(vyre::ir::Expr, vyre::ir::Expr) -> vyre::ir::Expr,
395{
396    check_tensors(
397        op_id,
398        &[
399            (&a, vyre::ir::DataType::U32),
400            (&b, vyre::ir::DataType::U32),
401            (&out, vyre::ir::DataType::U32),
402        ],
403    )?;
404
405    if a.shape != b.shape || a.shape != out.shape {
406        return Err(crate::tensor_ref::TensorRefError::ShapeMismatch {
407            name: "elementwise_binary".into(),
408            found: vec![],
409            expected: vec![],
410            op: op_id,
411        });
412    }
413
414    let a_count = a.element_count().ok_or_else(|| {
415        crate::tensor_ref::TensorRefError::ElementCountOverflow {
416            name: a.name_str().to_string(),
417            shape: a.shape.to_vec(),
418        }
419    })?;
420    let out_count = out.element_count().ok_or_else(|| {
421        crate::tensor_ref::TensorRefError::ElementCountOverflow {
422            name: out.name_str().to_string(),
423            shape: out.shape.to_vec(),
424        }
425    })?;
426    if out_count < a_count {
427        return Err(crate::tensor_ref::TensorRefError::ShapeMismatch {
428            name: out.name_str().to_string(),
429            found: out.shape.to_vec(),
430            expected: a.shape.to_vec(),
431            op: op_id,
432        });
433    }
434
435    let n = a_count;
436    let body = vec![
437        vyre::ir::Node::let_bind("idx", vyre::ir::Expr::InvocationId { axis: 0 }),
438        vyre::ir::Node::if_then(
439            vyre::ir::Expr::lt(vyre::ir::Expr::var("idx"), vyre::ir::Expr::u32(n)),
440            vec![vyre::ir::Node::store(
441                out.name_str(),
442                vyre::ir::Expr::var("idx"),
443                f(
444                    vyre::ir::Expr::load(a.name_str(), vyre::ir::Expr::var("idx")),
445                    vyre::ir::Expr::load(b.name_str(), vyre::ir::Expr::var("idx")),
446                ),
447            )],
448        ),
449    ];
450
451    let group = options.workgroup_size.unwrap_or([64, 1, 1]);
452
453    Ok(vyre::ir::Program::wrapped(
454        vec![
455            vyre::ir::BufferDecl::storage(
456                a.name_str(),
457                0,
458                vyre::ir::BufferAccess::ReadOnly,
459                vyre::ir::DataType::U32,
460            )
461            .with_count(n),
462            vyre::ir::BufferDecl::storage(
463                b.name_str(),
464                1,
465                vyre::ir::BufferAccess::ReadOnly,
466                vyre::ir::DataType::U32,
467            )
468            .with_count(n),
469            vyre::ir::BufferDecl::output(out.name_str(), 2, vyre::ir::DataType::U32).with_count(n),
470        ],
471        group,
472        vec![crate::region::wrap_anonymous(op_id, body)],
473    ))
474}
475
476#[allow(dead_code)]
477pub(crate) fn build_elementwise_unary<F>(
478    op_id: &'static str,
479    a: crate::tensor_ref::TensorRef,
480    out: crate::tensor_ref::TensorRef,
481    options: BuildOptions,
482    f: F,
483) -> Result<vyre::ir::Program, crate::tensor_ref::TensorRefError>
484where
485    F: Fn(vyre::ir::Expr) -> vyre::ir::Expr,
486{
487    check_tensors(
488        op_id,
489        &[
490            (&a, vyre::ir::DataType::U32),
491            (&out, vyre::ir::DataType::U32),
492        ],
493    )?;
494
495    if a.shape != out.shape {
496        return Err(crate::tensor_ref::TensorRefError::ShapeMismatch {
497            name: "elementwise_unary".into(),
498            found: vec![],
499            expected: vec![],
500            op: op_id,
501        });
502    }
503
504    let n = a.element_count().ok_or_else(|| {
505        crate::tensor_ref::TensorRefError::ElementCountOverflow {
506            name: a.name_str().to_string(),
507            shape: a.shape.to_vec(),
508        }
509    })?;
510    let body = vec![
511        vyre::ir::Node::let_bind("idx", vyre::ir::Expr::InvocationId { axis: 0 }),
512        vyre::ir::Node::if_then(
513            vyre::ir::Expr::lt(vyre::ir::Expr::var("idx"), vyre::ir::Expr::u32(n)),
514            vec![vyre::ir::Node::store(
515                out.name_str(),
516                vyre::ir::Expr::var("idx"),
517                f(vyre::ir::Expr::load(
518                    a.name_str(),
519                    vyre::ir::Expr::var("idx"),
520                )),
521            )],
522        ),
523    ];
524
525    let group = options.workgroup_size.unwrap_or([64, 1, 1]);
526
527    Ok(vyre::ir::Program::wrapped(
528        vec![
529            vyre::ir::BufferDecl::storage(
530                a.name_str(),
531                0,
532                vyre::ir::BufferAccess::ReadOnly,
533                vyre::ir::DataType::U32,
534            )
535            .with_count(n),
536            vyre::ir::BufferDecl::output(out.name_str(), 1, vyre::ir::DataType::U32).with_count(n),
537        ],
538        group,
539        vec![crate::region::wrap_anonymous(op_id, body)],
540    ))
541}
542
543#[cfg(test)]
544
545mod tests {
546    use super::*;
547
548    #[test]
549    fn build_options_defaults_are_all_none() {
550        let o = BuildOptions::default();
551        assert!(o.workgroup_size.is_none());
552        assert!(o.region_generator.is_none());
553        assert!(o.tenant_id.is_none());
554    }
555
556    #[test]
557    fn build_options_chain_preserves_earlier_setters() {
558        let o = BuildOptions::new()
559            .with_workgroup_size([128, 1, 1])
560            .with_region_generator("test::op")
561            .with_tenant_id(7);
562        assert_eq!(o.workgroup_size, Some([128, 1, 1]));
563        assert_eq!(o.region_generator, Some("test::op"));
564        assert_eq!(o.tenant_id, Some(7));
565    }
566
567    #[test]
568    fn check_tensors_passes_on_clean_inputs() {
569        let a = TensorRef::u32_1d("a", 4);
570        let b = TensorRef::u32_1d("b", 4);
571        assert!(matches!(
572            check_tensors("op", &[(&a, DataType::U32), (&b, DataType::U32)]),
573            Ok(())
574        ));
575    }
576
577    #[test]
578    fn check_tensors_catches_dtype_mismatch() {
579        let a = TensorRef::u32_1d("a", 4);
580        let err = check_tensors("op", &[(&a, DataType::F32)]).unwrap_err();
581        assert!(matches!(err, TensorRefError::DtypeMismatch { .. }));
582    }
583
584    #[test]
585    fn check_tensors_catches_overflow() {
586        let a = TensorRef::new("big", DataType::U32, vec![1u32 << 20, 1u32 << 20]);
587        let err = check_tensors("op", &[(&a, DataType::U32)]).unwrap_err();
588        assert!(matches!(err, TensorRefError::ElementCountOverflow { .. }));
589    }
590
591    #[test]
592    fn check_tensors_catches_name_collision() {
593        let a = TensorRef::u32_1d("x", 4);
594        let b = TensorRef::u32_1d("x", 4);
595        let err = check_tensors("op", &[(&a, DataType::U32), (&b, DataType::U32)]).unwrap_err();
596        assert!(matches!(err, TensorRefError::NameCollision { .. }));
597    }
598
599    #[test]
600    fn indexed_map_builder_emits_shared_child_region() {
601        let program = build_indexed_map(
602            "vyre-libs::test::indexed_map_user",
603            vec![
604                BufferDecl::storage("input", 0, vyre::ir::BufferAccess::ReadOnly, DataType::U32)
605                    .with_count(4),
606                BufferDecl::output("output", 1, DataType::U32).with_count(4),
607            ],
608            "output",
609            4,
610            [64, 1, 1],
611            |i| (i.clone(), Expr::load("input", i)),
612        );
613        let rendered = format!("{:?}", program.entry());
614        assert!(
615            rendered.contains(INDEXED_MAP_OP_ID),
616            "Fix: indexed-map users must share the same child region instead of copying loop skeletons: {rendered}"
617        );
618    }
619
620    #[test]
621    fn strided_writeback_builder_emits_shared_child_region() {
622        let program = Program::wrapped(
623            vec![BufferDecl::output("out", 0, DataType::F32).with_count(4)],
624            [4, 1, 1],
625            vec![crate::region::wrap_anonymous(
626                "vyre-libs::test::row_reduction_user",
627                vec![
628                    Node::let_bind("local", Expr::LocalId { axis: 0 }),
629                    strided_writeback_child(
630                        "vyre-libs::test::row_reduction_user",
631                        4,
632                        1,
633                        4,
634                        "out",
635                        vec![Node::let_bind("scale", Expr::f32(0.5))],
636                        |_idx| Expr::var("scale"),
637                    ),
638                ],
639            )],
640        );
641        let rendered = format!("{:?}", program.entry());
642        assert!(
643            rendered.contains(STRIDED_WRITEBACK_OP_ID),
644            "Fix: row-reduction writeback users must share the same child region instead of copying loop skeletons: {rendered}"
645        );
646    }
647}