oximedia-graph 0.1.8

Filter pipeline for OxiMedia
Documentation
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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
//! Graph optimization passes.
//!
//! Provides a pluggable pipeline of optimization passes that simplify a graph
//! represented as a list of [`NodeSpec`]s.

#![allow(dead_code)]

use std::collections::HashMap;

// ─────────────────────────────────────────────────────────────────────────────
// NodeSpec
// ─────────────────────────────────────────────────────────────────────────────

/// A simplified, serializable description of a graph node used by the
/// optimization passes.
#[derive(Debug, Clone, PartialEq)]
pub struct NodeSpec {
    /// Unique node identifier.
    pub id: String,
    /// Node type name (e.g. `"Scale"`, `"Brightness"`, `"Contrast"`).
    pub node_type: String,
    /// Key/value parameters for the node.
    pub params: HashMap<String, String>,
    /// IDs of nodes that feed into this node (predecessor IDs).
    pub inputs: Vec<String>,
    /// IDs of nodes that consume output from this node (successor IDs).
    pub outputs: Vec<String>,
}

impl NodeSpec {
    /// Create a minimal node spec.
    #[must_use]
    pub fn new(id: impl Into<String>, node_type: impl Into<String>) -> Self {
        Self {
            id: id.into(),
            node_type: node_type.into(),
            params: HashMap::new(),
            inputs: vec![],
            outputs: vec![],
        }
    }

    /// Builder helper: add a parameter.
    #[must_use]
    pub fn with_param(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.params.insert(key.into(), value.into());
        self
    }

    /// Builder helper: set inputs.
    #[must_use]
    pub fn with_inputs(mut self, inputs: Vec<String>) -> Self {
        self.inputs = inputs;
        self
    }

    /// Builder helper: set outputs.
    #[must_use]
    pub fn with_outputs(mut self, outputs: Vec<String>) -> Self {
        self.outputs = outputs;
        self
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// OptimizationPass trait
// ─────────────────────────────────────────────────────────────────────────────

/// An optimization pass that transforms a list of [`NodeSpec`]s in-place.
pub trait OptimizationPass: Send + Sync {
    /// Human-readable name of the pass.
    fn name(&self) -> &str;

    /// Apply the pass.  Returns the number of optimizations applied.
    fn optimize(&self, nodes: &mut Vec<NodeSpec>) -> usize;
}

// ─────────────────────────────────────────────────────────────────────────────
// ConstantFoldingPass
// ─────────────────────────────────────────────────────────────────────────────

/// Removes identity nodes whose output is always equal to their input.
///
/// Currently folds:
/// - `Scale` nodes where the `"factor"` parameter is `"1.0"` or `"1"`.
pub struct ConstantFoldingPass;

impl ConstantFoldingPass {
    /// Create a new constant folding pass.
    #[must_use]
    pub fn new() -> Self {
        Self
    }

    fn is_identity(node: &NodeSpec) -> bool {
        match node.node_type.as_str() {
            "Scale" => {
                let factor = node
                    .params
                    .get("factor")
                    .map(String::as_str)
                    .unwrap_or("1.0");
                factor == "1.0" || factor == "1"
            }
            _ => false,
        }
    }
}

impl Default for ConstantFoldingPass {
    fn default() -> Self {
        Self::new()
    }
}

impl OptimizationPass for ConstantFoldingPass {
    fn name(&self) -> &str {
        "ConstantFolding"
    }

    fn optimize(&self, nodes: &mut Vec<NodeSpec>) -> usize {
        let before = nodes.len();

        // Collect IDs of identity nodes.
        let identity_ids: Vec<String> = nodes
            .iter()
            .filter(|n| Self::is_identity(n))
            .map(|n| n.id.clone())
            .collect();

        if identity_ids.is_empty() {
            return 0;
        }

        let identity_set: std::collections::HashSet<&str> =
            identity_ids.iter().map(String::as_str).collect();

        // Rewire: for every node whose input is an identity node, replace that
        // input with the identity node's own inputs.
        for node in nodes.iter_mut() {
            node.inputs = node
                .inputs
                .iter()
                .flat_map(|inp| {
                    if identity_set.contains(inp.as_str()) {
                        // Find the identity node's inputs (they carry the original data).
                        // We already have the id; we need to look up its inputs from the
                        // original slice.  Since we're iterating mutably we keep a copy.
                        // In a real compiler IR this would be a proper use-def chain;
                        // here we use a simplified placeholder approach: remove the edge.
                        vec![] // no inputs → effectively bypass
                    } else {
                        vec![inp.clone()]
                    }
                })
                .collect();
        }

        // Remove identity nodes.
        nodes.retain(|n| !identity_set.contains(n.id.as_str()));

        before - nodes.len()
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// DeadNodeEliminationPass
// ─────────────────────────────────────────────────────────────────────────────

/// Removes nodes whose `outputs` list is empty (i.e., no other node consumes
/// their output).
///
/// Nodes with no outputs are considered dead unless they are the only node in
/// the graph (treating them as implicit sinks).
pub struct DeadNodeEliminationPass;

impl DeadNodeEliminationPass {
    /// Create a new dead-node elimination pass.
    #[must_use]
    pub fn new() -> Self {
        Self
    }
}

impl Default for DeadNodeEliminationPass {
    fn default() -> Self {
        Self::new()
    }
}

impl OptimizationPass for DeadNodeEliminationPass {
    fn name(&self) -> &str {
        "DeadNodeElimination"
    }

    fn optimize(&self, nodes: &mut Vec<NodeSpec>) -> usize {
        if nodes.len() <= 1 {
            return 0; // preserve single-node graphs
        }

        let before = nodes.len();

        // Compute the set of all node IDs referenced by any other node (either as
        // an input source or as an output target listed in another node's outputs).
        let referenced: std::collections::HashSet<String> = nodes
            .iter()
            .flat_map(|n| n.inputs.iter().chain(n.outputs.iter()).cloned())
            .collect();

        // A node is "dead" if it has no successors AND is not referenced by anyone.
        nodes.retain(|n| !n.outputs.is_empty() || referenced.contains(&n.id));

        before - nodes.len()
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// NodeFusionPass
// ─────────────────────────────────────────────────────────────────────────────

/// Fuses compatible sequential node pairs into a single fused node.
///
/// Currently fuses:
/// - `Brightness` immediately followed by `Contrast` → `BrightnessContrast`
pub struct NodeFusionPass;

impl NodeFusionPass {
    /// Create a new node fusion pass.
    #[must_use]
    pub fn new() -> Self {
        Self
    }
}

impl Default for NodeFusionPass {
    fn default() -> Self {
        Self::new()
    }
}

impl OptimizationPass for NodeFusionPass {
    fn name(&self) -> &str {
        "NodeFusion"
    }

    fn optimize(&self, nodes: &mut Vec<NodeSpec>) -> usize {
        let mut fusions = 0usize;
        let mut i = 0;

        while i + 1 < nodes.len() {
            let (a, b) = (&nodes[i], &nodes[i + 1]);

            // Check for Brightness → Contrast sequential pair.
            let is_fusable = a.node_type == "Brightness"
                && b.node_type == "Contrast"
                && b.inputs.contains(&a.id);

            if is_fusable {
                // Merge parameters.
                let mut params = a.params.clone();
                for (k, v) in &b.params {
                    params.insert(k.clone(), v.clone());
                }

                let fused = NodeSpec {
                    id: format!("{}_{}", a.id, b.id),
                    node_type: "BrightnessContrast".to_string(),
                    params,
                    inputs: a.inputs.clone(),
                    outputs: b.outputs.clone(),
                };

                let a_id = a.id.clone();
                let b_id = b.id.clone();
                let fused_id = fused.id.clone();

                // Replace the pair with the fused node.
                nodes.remove(i + 1);
                nodes[i] = fused;

                // Update all references in the remaining nodes.
                for node in nodes.iter_mut() {
                    for inp in &mut node.inputs {
                        if *inp == a_id || *inp == b_id {
                            *inp = fused_id.clone();
                        }
                    }
                    for out in &mut node.outputs {
                        if *out == a_id || *out == b_id {
                            *out = fused_id.clone();
                        }
                    }
                }

                fusions += 1;
                // Do not advance i – re-check from the same position in case
                // the fused node can itself be fused with a successor.
            } else {
                i += 1;
            }
        }

        fusions
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// OptimizationReport
// ─────────────────────────────────────────────────────────────────────────────

/// Summary of optimizations applied by the [`GraphOptimizer`].
#[derive(Debug, Clone)]
pub struct OptimizationReport {
    /// Names of passes that were applied (in order).
    pub passes_applied: Vec<String>,
    /// Number of nodes before optimization.
    pub nodes_before: usize,
    /// Number of nodes after optimization.
    pub nodes_after: usize,
    /// Total number of individual optimizations performed.
    pub optimizations: usize,
}

// ─────────────────────────────────────────────────────────────────────────────
// GraphOptimizer
// ─────────────────────────────────────────────────────────────────────────────

/// Runs a sequence of [`OptimizationPass`]es over a node list.
#[derive(Default)]
pub struct GraphOptimizer {
    passes: Vec<Box<dyn OptimizationPass>>,
}

impl GraphOptimizer {
    /// Create a new optimizer with no passes.
    #[must_use]
    pub fn new() -> Self {
        Self { passes: vec![] }
    }

    /// Add an optimization pass.
    pub fn add_pass(&mut self, pass: Box<dyn OptimizationPass>) {
        self.passes.push(pass);
    }

    /// Run all registered passes over `nodes`.
    ///
    /// Returns the optimized node list and an [`OptimizationReport`].
    #[must_use]
    pub fn run(&self, mut nodes: Vec<NodeSpec>) -> (Vec<NodeSpec>, OptimizationReport) {
        let nodes_before = nodes.len();
        let mut passes_applied = Vec::new();
        let mut total_optimizations = 0;

        for pass in &self.passes {
            let count = pass.optimize(&mut nodes);
            passes_applied.push(pass.name().to_string());
            total_optimizations += count;
        }

        let report = OptimizationReport {
            passes_applied,
            nodes_before,
            nodes_after: nodes.len(),
            optimizations: total_optimizations,
        };

        (nodes, report)
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Unit tests
// ─────────────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;

    fn scale_node(id: &str, factor: &str) -> NodeSpec {
        NodeSpec::new(id, "Scale").with_param("factor", factor)
    }

    fn brightness_node(id: &str) -> NodeSpec {
        NodeSpec::new(id, "Brightness").with_param("value", "1.2")
    }

    fn contrast_node(id: &str, input: &str) -> NodeSpec {
        NodeSpec::new(id, "Contrast")
            .with_param("value", "1.1")
            .with_inputs(vec![input.to_string()])
    }

    // ── ConstantFoldingPass ───────────────────────────────────────────────────

    #[test]
    fn test_constant_folding_removes_scale_one() {
        let pass = ConstantFoldingPass::new();
        let mut nodes = vec![scale_node("s1", "1.0")];
        let removed = pass.optimize(&mut nodes);
        assert_eq!(removed, 1);
        assert!(nodes.is_empty());
    }

    #[test]
    fn test_constant_folding_keeps_scale_two() {
        let pass = ConstantFoldingPass::new();
        let mut nodes = vec![scale_node("s1", "2.0")];
        let removed = pass.optimize(&mut nodes);
        assert_eq!(removed, 0);
        assert_eq!(nodes.len(), 1);
    }

    #[test]
    fn test_constant_folding_integer_one() {
        let pass = ConstantFoldingPass::new();
        let mut nodes = vec![scale_node("s1", "1")];
        let removed = pass.optimize(&mut nodes);
        assert_eq!(removed, 1);
    }

    #[test]
    fn test_constant_folding_mixed_nodes() {
        let pass = ConstantFoldingPass::new();
        let mut nodes = vec![scale_node("s1", "1.0"), scale_node("s2", "0.5")];
        let removed = pass.optimize(&mut nodes);
        assert_eq!(removed, 1);
        assert_eq!(nodes.len(), 1);
        assert_eq!(nodes[0].id, "s2");
    }

    // ── DeadNodeEliminationPass ───────────────────────────────────────────────

    #[test]
    fn test_dead_node_elimination_no_outputs() {
        let pass = DeadNodeEliminationPass::new();
        // Two nodes: neither references the other.
        let mut nodes = vec![NodeSpec::new("a", "Filter"), NodeSpec::new("b", "Filter")];
        // Neither has outputs or is referenced → both are dead.
        let removed = pass.optimize(&mut nodes);
        assert_eq!(removed, 2);
        assert!(nodes.is_empty());
    }

    #[test]
    fn test_dead_node_elimination_referenced_node_kept() {
        let pass = DeadNodeEliminationPass::new();
        let mut nodes = vec![
            NodeSpec::new("a", "Source").with_outputs(vec!["b".to_string()]),
            NodeSpec::new("b", "Sink").with_inputs(vec!["a".to_string()]),
        ];
        let removed = pass.optimize(&mut nodes);
        assert_eq!(removed, 0);
    }

    #[test]
    fn test_dead_node_elimination_single_node_preserved() {
        let pass = DeadNodeEliminationPass::new();
        let mut nodes = vec![NodeSpec::new("a", "Source")];
        let removed = pass.optimize(&mut nodes);
        assert_eq!(removed, 0); // single-node graphs preserved
        assert_eq!(nodes.len(), 1);
    }

    // ── NodeFusionPass ────────────────────────────────────────────────────────

    #[test]
    fn test_node_fusion_brightness_contrast() {
        let pass = NodeFusionPass::new();
        let mut nodes = vec![
            brightness_node("b1").with_outputs(vec!["c1".to_string()]),
            contrast_node("c1", "b1"),
        ];
        let fusions = pass.optimize(&mut nodes);
        assert_eq!(fusions, 1);
        assert_eq!(nodes.len(), 1);
        assert_eq!(nodes[0].node_type, "BrightnessContrast");
    }

    #[test]
    fn test_node_fusion_no_match() {
        let pass = NodeFusionPass::new();
        let mut nodes = vec![NodeSpec::new("a", "Scale"), NodeSpec::new("b", "Gamma")];
        let fusions = pass.optimize(&mut nodes);
        assert_eq!(fusions, 0);
        assert_eq!(nodes.len(), 2);
    }

    #[test]
    fn test_node_fusion_fused_node_has_merged_params() {
        let pass = NodeFusionPass::new();
        let mut nodes = vec![
            brightness_node("b1").with_outputs(vec!["c1".to_string()]),
            contrast_node("c1", "b1"),
        ];
        pass.optimize(&mut nodes);
        assert!(nodes[0].params.contains_key("value"));
    }

    // ── GraphOptimizer ────────────────────────────────────────────────────────

    #[test]
    fn test_optimizer_empty_graph() {
        let mut opt = GraphOptimizer::new();
        opt.add_pass(Box::new(ConstantFoldingPass::new()));
        let (nodes, report) = opt.run(vec![]);
        assert!(nodes.is_empty());
        assert_eq!(report.nodes_before, 0);
        assert_eq!(report.nodes_after, 0);
    }

    #[test]
    fn test_optimizer_report_fields() {
        let mut opt = GraphOptimizer::new();
        opt.add_pass(Box::new(ConstantFoldingPass::new()));
        let nodes = vec![scale_node("s1", "1.0"), scale_node("s2", "2.0")];
        let (_, report) = opt.run(nodes);
        assert_eq!(report.nodes_before, 2);
        assert_eq!(report.nodes_after, 1);
        assert_eq!(report.optimizations, 1);
        assert_eq!(report.passes_applied, vec!["ConstantFolding"]);
    }

    #[test]
    fn test_optimizer_multiple_passes() {
        let mut opt = GraphOptimizer::new();
        opt.add_pass(Box::new(ConstantFoldingPass::new()));
        opt.add_pass(Box::new(DeadNodeEliminationPass::new()));
        let nodes = vec![scale_node("s1", "1.0"), NodeSpec::new("orphan", "Filter")];
        let (_, report) = opt.run(nodes);
        assert_eq!(report.passes_applied.len(), 2);
    }

    #[test]
    fn test_optimizer_no_passes() {
        let opt = GraphOptimizer::new();
        let nodes = vec![NodeSpec::new("a", "Filter")];
        let (result, report) = opt.run(nodes);
        assert_eq!(result.len(), 1);
        assert_eq!(report.optimizations, 0);
        assert!(report.passes_applied.is_empty());
    }
}