1#![allow(clippy::len_zero)]
2#[macro_use]
3pub mod macros;
4
5pub mod fact;
6pub mod model;
7pub mod ops;
8
9pub mod internal {
10 pub use std::fmt;
11 pub use tract_nnef::internal::*;
12 pub use tract_pulse_opl::tract_nnef;
13
14 pub use downcast_rs::Downcast;
15
16 pub use crate::fact::PulsedFact;
17 pub use crate::model::{PulsedModel, PulsedModelExt};
18 pub use crate::ops::{OpPulsifier, PulsedOp};
19}
20
21use std::ops::ControlFlow;
22
23use internal::*;
24use tract_core::transform::ModelTransform;
25use tract_pulse_opl::tract_nnef::tract_core;
26
27pub use ops::PulsedOp;
28
29#[derive(Debug, Default, serde::Deserialize)]
30pub struct PulseConfig {
31 pub symbol: Option<String>,
32 pub pulse: String,
33}
34
35#[derive(Debug)]
36struct PulseTransform(PulseConfig);
37
38impl ModelTransform for PulseTransform {
39 fn name(&self) -> std::borrow::Cow<'static, str> {
40 "pulse".into()
41 }
42 fn transform(&self, model: &mut TypedModel) -> TractResult<()> {
43 let symbol = self.0.symbol.as_deref().unwrap_or("S");
44 let sym = model.symbols.sym(symbol);
45 let pulse_dim = parse_tdim(&model.symbols, &self.0.pulse)?;
46 let pulsed = model::PulsedModel::new(model, sym, &pulse_dim)?;
47 *model = pulsed.into_typed()?;
48 Ok(())
49 }
50}
51
52register_model_transform!("pulse", PulseConfig, |config| Ok(Box::new(PulseTransform(config))));
53
54pub trait WithPulse {
55 fn enable_pulse(&mut self);
56 fn with_pulse(self) -> Self;
57}
58
59impl WithPulse for tract_nnef::framework::Nnef {
60 fn enable_pulse(&mut self) {
61 self.enable_tract_core();
62 self.registries.push(tract_nnef_registry());
63 }
64 fn with_pulse(mut self) -> Self {
65 self.enable_pulse();
66 self
67 }
68}
69
70pub fn tract_nnef_registry() -> Registry {
71 let mut reg = tract_pulse_opl::tract_nnef_registry();
72 ops::delay::register(&mut reg);
73 reg.extensions.push(Box::new(decl_stream_symbol));
74 reg
75}
76
77fn decl_stream_symbol(
78 _proto_model: &mut ModelBuilder,
79 name: &Identifier,
80 _rest: &str,
81) -> TractResult<ControlFlow<(), ()>> {
82 if name.0 == "tract_pulse_streaming_symbol" {
83 Ok(ControlFlow::Break(()))
84 } else {
85 Ok(ControlFlow::Continue(()))
86 }
87}
88
89#[cfg(test)]
90mod tests {
91 use super::*;
92
93 #[test]
94 fn test_source_must_stream() {
95 let mut model = TypedModel::default();
96 let s = model.symbols.sym("S");
97 let _a = model.add_source("a", f32::fact([1, 2, 3])).unwrap();
98 model.auto_outputs().unwrap();
99 assert!(PulsedModel::new(&model, s.clone(), &4.to_dim()).is_err());
100
101 let mut model = TypedModel::default();
102 let _a = model.add_source("a", f32::fact(dims![1, s, 3].as_ref())).unwrap();
103 model.auto_outputs().unwrap();
104 let pulse = PulsedModel::new(&model, s, &4.to_dim()).unwrap();
105 assert_eq!(
106 *pulse.outlet_fact(OutletId::new(0, 0)).unwrap().to_typed_fact().unwrap(),
107 f32::fact([1usize, 4, 3])
108 );
109 }
110
111 #[test]
112 fn test_immediate() {
113 let mut model = TypedModel::default();
114 let s = model.symbols.sym("S");
115 let _a = model.add_source("a", f32::fact(dims![s, 2, 3].as_ref())).unwrap();
116 model.auto_outputs().unwrap();
117
118 let pulse = PulsedModel::new(&model, s, &4.to_dim()).unwrap();
119
120 assert_eq!(*pulse.input_fact(0).unwrap().to_typed_fact().unwrap(), f32::fact([4, 2, 3]));
121 assert_eq!(*pulse.output_fact(0).unwrap().to_typed_fact().unwrap(), f32::fact([4, 2, 3]));
122 }
123}