flodl 0.5.0

floDl — a flow-graph deep learning framework built on libtorch
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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
//! Graph tree: hierarchical subgraph composition with label-path addressing.
//!
//! When a labeled [`Graph`] is used inside a [`FlowBuilder`](super::FlowBuilder),
//! the parent registers it as a child subgraph. Dot-separated label paths
//! (`"encoder.scan.hidden"`) address subgraphs and tags across boundaries.
//!
//! All operations are build-time or explicit-query-time. The forward path is untouched.
//!
//! # Key methods on [`Graph`]
//!
//! - **Navigation**: [`tree_children()`](Graph::tree_children), [`child_graph()`](Graph::child_graph),
//!   [`subgraph()`](Graph::subgraph), [`is_composed()`](Graph::is_composed)
//! - **Parameters**: [`parameters_at()`](Graph::parameters_at), [`named_parameters_at()`](Graph::named_parameters_at)
//! - **Freeze/thaw**: [`freeze()`](Graph::freeze), [`thaw()`](Graph::thaw), [`is_frozen()`](Graph::is_frozen)
//! - **Checkpoints**: [`load_subgraph_checkpoint()`](Graph::load_subgraph_checkpoint)
//! - **Observation**: [`tagged_at()`](Graph::tagged_at), [`collect_at()`](Graph::collect_at),
//!   [`record_at()`](Graph::record_at), [`trend_at()`](Graph::trend_at)

use std::collections::HashMap;
use crate::autograd::Variable;
use crate::nn::{self, Buffer, Module, Parameter};
use crate::tensor::{Result, TensorError};
use super::Graph;
use super::trend::Trend;

/// What a label path resolves to.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PathKind {
    /// An entire child subgraph.
    Subgraph,
    /// A named tag within a graph.
    Tag,
}

/// Internal resolution result (borrowed references, no ownership).
#[allow(dead_code)]
pub(crate) enum ResolvedPath<'a> {
    /// Path resolves to an entire child subgraph.
    Subgraph(&'a Graph),
    /// Path resolves to a tag within a specific graph.
    Tag { graph: &'a Graph, tag: String },
}

impl Graph {
    // ── Path resolution ──────────────────────────────────────────────

    /// Resolve a dot-separated label path to a subgraph or tag.
    ///
    /// **Strict dot semantics:**
    /// - `"scan"` — local: check children first (Subgraph), then tags (Tag).
    /// - `"letter.scan"` — child `"letter"`, then `"scan"` within it.
    /// - `"letter.scan.location"` — child `"letter"`, child/tag `"scan"`, then `"location"`.
    ///
    /// Returns `Err` if any segment doesn't resolve.
    pub(crate) fn resolve(&self, path: &str) -> Result<ResolvedPath<'_>> {
        if path.is_empty() {
            return Err(TensorError::new("empty label path"));
        }
        let segments: Vec<&str> = path.split('.').collect();
        self.resolve_segments(&segments, path, false)
    }

    fn resolve_segments<'a>(
        &'a self,
        segments: &[&str],
        full_path: &str,
        cross_boundary: bool,
    ) -> Result<ResolvedPath<'a>> {
        debug_assert!(!segments.is_empty());
        let first = segments[0];

        if segments.len() == 1 {
            // Single segment: children take priority over tags
            if let Some(g) = self.child_graph(first) {
                return Ok(ResolvedPath::Subgraph(g));
            }
            if self.tag_names.contains_key(first) {
                // Block internal tags when accessed from outside
                if cross_boundary && self.internal_tags.contains(first) {
                    return Err(TensorError::new(&format!(
                        "tag {:?} is internal and cannot be accessed from a parent graph (path: {:?})",
                        first, full_path
                    )));
                }
                return Ok(ResolvedPath::Tag { graph: self, tag: first.to_string() });
            }
            return Err(TensorError::new(&format!(
                "{:?} is not a subgraph or tag of this graph (path: {:?})",
                first, full_path
            )));
        }

        // Multi-segment: first MUST be a child label
        let child = self.child_graph(first).ok_or_else(|| {
            TensorError::new(&format!(
                "{:?} is not a subgraph of this graph (path: {:?})",
                first, full_path
            ))
        })?;

        // Once we cross into a child, all subsequent resolution is cross-boundary
        child.resolve_segments(&segments[1..], full_path, true)
    }

    // ── Public navigation ────────────────────────────────────────────

    /// Direct children: label -> child graph.
    pub fn tree_children(&self) -> HashMap<&str, &Graph> {
        self.children.iter()
            .filter_map(|(label, &ni)| {
                self.nodes[ni].module.as_ref()
                    .and_then(|m| m.as_graph())
                    .map(|g| (label.as_str(), g))
            })
            .collect()
    }

    /// Get a direct child graph by label (one level only).
    pub fn child_graph(&self, label: &str) -> Option<&Graph> {
        self.children.get(label)
            .and_then(|&ni| self.nodes[ni].module.as_ref())
            .and_then(|m| m.as_graph())
    }

    /// Get a subgraph at any depth via dot-path.
    pub fn subgraph(&self, path: &str) -> Result<&Graph> {
        match self.resolve(path)? {
            ResolvedPath::Subgraph(g) => Ok(g),
            ResolvedPath::Tag { .. } => Err(TensorError::new(&format!(
                "path {:?} resolves to a tag, not a subgraph", path
            ))),
        }
    }

    /// Whether this graph has been composed into a parent graph.
    pub fn is_composed(&self) -> bool {
        self.composed.get()
    }

    /// Tags marked as internal (hidden from parent resolution).
    pub fn internal_tags(&self) -> &std::collections::HashSet<String> {
        &self.internal_tags
    }

    /// Validate that a path resolves, returning what it resolves to.
    pub fn validate_path(&self, path: &str) -> Result<PathKind> {
        match self.resolve(path)? {
            ResolvedPath::Subgraph(_) => Ok(PathKind::Subgraph),
            ResolvedPath::Tag { .. } => Ok(PathKind::Tag),
        }
    }

    // ── Parameter operations ─────────────────────────────────────────

    /// All parameters at a label path.
    pub fn parameters_at(&self, path: &str) -> Result<Vec<Parameter>> {
        match self.resolve(path)? {
            ResolvedPath::Subgraph(g) => Ok(g.parameters()),
            ResolvedPath::Tag { graph, ref tag } => {
                if let Some(&(ni, _)) = graph.tag_names.get(tag.as_str()) {
                    if let Some(ref module) = graph.nodes[ni].module {
                        Ok(module.parameters())
                    } else {
                        Ok(vec![])
                    }
                } else {
                    Ok(vec![])
                }
            }
        }
    }

    /// Named parameters at a label path, using the target's own namespace.
    /// For subgraphs: delegates to the child graph's `named_parameters()`.
    /// For tags: qualifies with the tag name as prefix.
    pub fn named_parameters_at(&self, path: &str) -> Result<Vec<(String, Parameter)>> {
        match self.resolve(path)? {
            ResolvedPath::Subgraph(g) => Ok(g.named_parameters()),
            ResolvedPath::Tag { graph, ref tag } => {
                if let Some(&(ni, _)) = graph.tag_names.get(tag.as_str()) {
                    if let Some(ref module) = graph.nodes[ni].module {
                        Ok(module.parameters().into_iter()
                            .map(|p| (format!("{}/{}", tag, p.name), p))
                            .collect())
                    } else {
                        Ok(vec![])
                    }
                } else {
                    Ok(vec![])
                }
            }
        }
    }

    /// Named buffers at a label path, using the target's own namespace.
    pub fn named_buffers_at(&self, path: &str) -> Result<Vec<(String, Buffer)>> {
        match self.resolve(path)? {
            ResolvedPath::Subgraph(g) => Ok(g.named_buffers()),
            ResolvedPath::Tag { graph, ref tag } => {
                if let Some(&(ni, _)) = graph.tag_names.get(tag.as_str()) {
                    if let Some(ref module) = graph.nodes[ni].module {
                        Ok(module.buffers().into_iter()
                            .map(|b| (format!("{}/{}", tag, b.name), b))
                            .collect())
                    } else {
                        Ok(vec![])
                    }
                } else {
                    Ok(vec![])
                }
            }
        }
    }

    // ── Freeze / thaw ────────────────────────────────────────────────

    /// Freeze all parameters at the given label path.
    pub fn freeze(&self, path: &str) -> Result<()> {
        for p in self.parameters_at(path)? {
            p.freeze()?;
        }
        Ok(())
    }

    /// Thaw (unfreeze) all parameters at the given label path.
    pub fn thaw(&self, path: &str) -> Result<()> {
        for p in self.parameters_at(path)? {
            p.unfreeze()?;
        }
        Ok(())
    }

    /// Check if all parameters at the path are frozen.
    /// Returns true only if there are parameters and ALL are frozen.
    pub fn is_frozen(&self, path: &str) -> Result<bool> {
        let params = self.parameters_at(path)?;
        if params.is_empty() {
            return Ok(false);
        }
        Ok(params.iter().all(|p| p.is_frozen()))
    }

    // ── Training mode ────────────────────────────────────────────────

    // ── Checkpoint composition ────────────────────────────────────────

    /// Load a checkpoint into a specific subgraph.
    ///
    /// The checkpoint's structural hash is validated against the target
    /// subgraph's hash. Named parameters/buffers are matched within the
    /// subgraph's own namespace.
    pub fn load_subgraph_checkpoint(&self, path: &str, file: &str) -> Result<nn::LoadReport> {
        let target = self.subgraph(path)?;
        let params = target.named_parameters();
        let buffers = target.named_buffers();
        let hash = target.structural_hash();
        nn::load_checkpoint_file(file, &params, &buffers, Some(hash))
    }

    // ── Training mode ────────────────────────────────────────────────

    /// Set training mode on a specific subgraph or tagged module.
    pub fn set_training_at(&self, path: &str, training: bool) -> Result<()> {
        match self.resolve(path)? {
            ResolvedPath::Subgraph(g) => {
                g.set_training(training);
            }
            ResolvedPath::Tag { graph, ref tag } => {
                if let Some(&(ni, _)) = graph.tag_names.get(tag.as_str()) {
                    if let Some(ref module) = graph.nodes[ni].module {
                        crate::nn::walk_modules(module.as_ref(), &mut |m| {
                            m.set_training(training);
                        });
                    }
                }
            }
        }
        Ok(())
    }

    // ── Cross-boundary observation ───────────────────────────────────

    /// Get a tagged output by label path.
    /// Returns `Err` if the path doesn't exist (null -- wiring bug).
    /// Returns `Ok(None)` if the path exists but hasn't been computed yet (nil).
    /// Returns `Ok(Some(v))` if the value is available.
    pub fn tagged_at(&self, path: &str) -> Result<Option<Variable>> {
        match self.resolve(path)? {
            ResolvedPath::Subgraph(_) => Err(TensorError::new(&format!(
                "path {:?} resolves to a subgraph, not a tag", path
            ))),
            ResolvedPath::Tag { graph, ref tag } => Ok(graph.tagged(tag)),
        }
    }

    /// Collect metrics from label paths into observation buffers.
    /// Each path must resolve to a tag (not a subgraph).
    /// Metrics are stored in the target graph's batch buffer.
    pub fn collect_at(&self, paths: &[&str]) -> Result<()> {
        for &path in paths {
            match self.resolve(path)? {
                ResolvedPath::Subgraph(_) => {
                    return Err(TensorError::new(&format!(
                        "collect_at: {:?} resolves to a subgraph, not a tag", path
                    )));
                }
                ResolvedPath::Tag { graph, ref tag } => {
                    graph.collect(&[tag.as_str()])?;
                }
            }
        }
        Ok(())
    }

    /// Record a scalar metric at a label path.
    /// For dotted paths, the metric is stored in the target graph's buffer
    /// under the final segment name.
    pub fn record_at(&self, path: &str, value: f64) -> Result<()> {
        let segments: Vec<&str> = path.split('.').collect();
        if segments.len() < 2 {
            // Single segment: record into self
            self.record_scalar(path, value);
            return Ok(());
        }
        // Multi-segment: resolve parent graph, record under last segment
        let parent_path = segments[..segments.len() - 1].join(".");
        let tag = segments[segments.len() - 1];
        let target = self.subgraph(&parent_path)?;
        target.record_scalar(tag, value);
        Ok(())
    }

    /// Get trend for a label-path metric.
    /// For dotted paths, reads from the target graph's epoch history.
    pub fn trend_at(&self, path: &str) -> Result<Trend> {
        let segments: Vec<&str> = path.split('.').collect();
        if segments.len() < 2 {
            return Ok(self.trend(path));
        }
        let parent_path = segments[..segments.len() - 1].join(".");
        let tag = segments[segments.len() - 1];
        let target = self.subgraph(&parent_path)?;
        Ok(target.trend(tag))
    }
}

#[cfg(test)]
mod tests {
    use crate::autograd::Variable;
    use crate::graph::FlowBuilder;
    use crate::nn::{Linear, Module};
    use crate::nn::ReLU;
    use crate::tensor::{test_device, test_opts, Tensor};
    use super::PathKind;

    #[test]
    fn test_unlabeled_graph_no_children() {
        let dev = test_device();

        let inner = FlowBuilder::from(Linear::on_device(3, 4, dev).unwrap())
            .through(ReLU::new())
            .build()
            .unwrap();

        let outer = FlowBuilder::from(inner)
            .through(Linear::on_device(4, 2, dev).unwrap())
            .build()
            .unwrap();

        // Unlabeled child is NOT registered
        assert!(outer.tree_children().is_empty());
        // But parameters are still collected (backward compat)
        assert_eq!(outer.parameters().len(), 4); // 2 from inner Linear + 2 from outer Linear
    }

    #[test]
    fn test_labeled_child_registered() {
        let dev = test_device();

        let inner = FlowBuilder::from(Linear::on_device(3, 4, dev).unwrap())
            .through(ReLU::new())
            .label("encoder")
            .build()
            .unwrap();

        let outer = FlowBuilder::from(inner)
            .through(Linear::on_device(4, 2, dev).unwrap())
            .build()
            .unwrap();

        assert_eq!(outer.tree_children().len(), 1);
        assert!(outer.tree_children().contains_key("encoder"));
        assert!(outer.child_graph("encoder").is_some());
        assert_eq!(outer.child_graph("encoder").unwrap().label(), Some("encoder"));
    }

    #[test]
    fn test_composed_flag() {
        let dev = test_device();

        let inner = FlowBuilder::from(Linear::on_device(3, 4, dev).unwrap())
            .label("child")
            .build()
            .unwrap();

        // Standalone: not composed
        assert!(!inner.is_composed());

        let outer = FlowBuilder::from(inner)
            .through(Linear::on_device(4, 2, dev).unwrap())
            .build()
            .unwrap();

        // After composition: child is composed
        let child = outer.child_graph("child").unwrap();
        assert!(child.is_composed());
        // Parent is not composed
        assert!(!outer.is_composed());
    }

    #[test]
    fn test_label_collision_error() {
        let dev = test_device();

        let a = FlowBuilder::from(Linear::on_device(3, 4, dev).unwrap())
            .label("dupe")
            .build()
            .unwrap();
        let b = FlowBuilder::from(Linear::on_device(4, 2, dev).unwrap())
            .label("dupe")
            .build()
            .unwrap();

        let result = FlowBuilder::from(a)
            .through(b)
            .build();

        let msg = result.err().expect("should be Err").to_string();
        assert!(msg.contains("duplicate child graph label"), "got: {}", msg);
    }

    #[test]
    fn test_dot_in_label_error() {
        let dev = test_device();

        let inner = FlowBuilder::from(Linear::on_device(3, 4, dev).unwrap())
            .label("a.b")
            .build()
            .unwrap();

        let result = FlowBuilder::from(inner)
            .through(Linear::on_device(4, 2, dev).unwrap())
            .build();

        let msg = result.err().expect("should be Err").to_string();
        assert!(msg.contains("contains a dot"), "got: {}", msg);
    }

    #[test]
    fn test_label_tag_same_node_ok() {
        let dev = test_device();

        let inner = FlowBuilder::from(Linear::on_device(3, 4, dev).unwrap())
            .label("encoder")
            .build()
            .unwrap();

        // Tag the same node as the child graph label
        let outer = FlowBuilder::from(inner)
            .tag("encoder")
            .through(Linear::on_device(4, 2, dev).unwrap())
            .build();

        assert!(outer.is_ok());
    }

    #[test]
    fn test_resolve_single_segment_child() {
        let dev = test_device();

        let inner = FlowBuilder::from(Linear::on_device(3, 4, dev).unwrap())
            .label("encoder")
            .build()
            .unwrap();

        let outer = FlowBuilder::from(inner)
            .through(Linear::on_device(4, 2, dev).unwrap())
            .build()
            .unwrap();

        assert_eq!(outer.validate_path("encoder").unwrap(), PathKind::Subgraph);
    }

    #[test]
    fn test_resolve_single_segment_tag() {
        let dev = test_device();

        let outer = FlowBuilder::from(Linear::on_device(3, 4, dev).unwrap())
            .tag("hidden")
            .through(Linear::on_device(4, 2, dev).unwrap())
            .build()
            .unwrap();

        assert_eq!(outer.validate_path("hidden").unwrap(), PathKind::Tag);
    }

    #[test]
    fn test_resolve_multi_segment() {
        let dev = test_device();

        let inner = FlowBuilder::from(Linear::on_device(3, 4, dev).unwrap())
            .tag("hidden")
            .through(Linear::on_device(4, 2, dev).unwrap())
            .label("encoder")
            .build()
            .unwrap();

        let outer = FlowBuilder::from(inner)
            .through(Linear::on_device(2, 1, dev).unwrap())
            .build()
            .unwrap();

        assert_eq!(outer.validate_path("encoder.hidden").unwrap(), PathKind::Tag);
    }

    #[test]
    fn test_resolve_multi_level() {
        let dev = test_device();

        let innermost = FlowBuilder::from(Linear::on_device(3, 4, dev).unwrap())
            .label("read")
            .build()
            .unwrap();
        let middle = FlowBuilder::from(innermost)
            .through(Linear::on_device(4, 2, dev).unwrap())
            .label("letter")
            .build()
            .unwrap();
        let outer = FlowBuilder::from(middle)
            .through(Linear::on_device(2, 1, dev).unwrap())
            .build()
            .unwrap();

        assert_eq!(outer.validate_path("letter").unwrap(), PathKind::Subgraph);
        assert_eq!(outer.validate_path("letter.read").unwrap(), PathKind::Subgraph);
    }

    #[test]
    fn test_resolve_invalid_path_error() {
        let dev = test_device();

        let inner = FlowBuilder::from(Linear::on_device(3, 4, dev).unwrap())
            .label("encoder")
            .build()
            .unwrap();

        let outer = FlowBuilder::from(inner)
            .through(Linear::on_device(4, 2, dev).unwrap())
            .build()
            .unwrap();

        // Non-existent single segment
        assert!(outer.validate_path("nonexistent").is_err());
        // Non-existent dotted path
        assert!(outer.validate_path("encoder.nonexistent").is_err());
        // Dotting into non-child first segment
        assert!(outer.validate_path("nonexistent.foo").is_err());
    }

    #[test]
    fn test_subgraph_returns_graph() {
        let dev = test_device();

        let inner = FlowBuilder::from(Linear::on_device(3, 4, dev).unwrap())
            .label("encoder")
            .build()
            .unwrap();

        let outer = FlowBuilder::from(inner)
            .through(Linear::on_device(4, 2, dev).unwrap())
            .build()
            .unwrap();

        let sub = outer.subgraph("encoder").unwrap();
        assert_eq!(sub.label(), Some("encoder"));
        assert_eq!(sub.parameters().len(), 2); // 1 Linear: weight + bias
    }

    #[test]
    fn test_forward_still_works_with_tree() {
        let dev = test_device();
        let opts = test_opts();

        let inner = FlowBuilder::from(Linear::on_device(3, 4, dev).unwrap())
            .through(ReLU::new())
            .label("encoder")
            .build()
            .unwrap();

        let outer = FlowBuilder::from(inner)
            .through(Linear::on_device(4, 2, dev).unwrap())
            .build()
            .unwrap();

        let x = Variable::new(
            Tensor::randn(&[1, 3], opts).unwrap(),
            false,
        );
        let y = outer.forward(&x).unwrap();
        assert_eq!(y.shape(), vec![1, 2]);
    }

    // ── Phase B: Training control ────────────────────────────────────

    #[test]
    fn test_parameters_at_subgraph() {
        let dev = test_device();
        let inner = FlowBuilder::from(Linear::on_device(3, 4, dev).unwrap())
            .through(Linear::on_device(4, 2, dev).unwrap())
            .label("encoder")
            .build()
            .unwrap();

        let outer = FlowBuilder::from(inner)
            .through(Linear::on_device(2, 1, dev).unwrap())
            .build()
            .unwrap();

        // Child has 2 Linear layers = 4 params (2 weight + 2 bias)
        let params = outer.parameters_at("encoder").unwrap();
        assert_eq!(params.len(), 4);
        // Outer total = 4 (child) + 2 (outer Linear) = 6
        assert_eq!(outer.parameters().len(), 6);
    }

    #[test]
    fn test_parameters_at_tag() {
        let dev = test_device();
        let g = FlowBuilder::from(Linear::on_device(3, 4, dev).unwrap())
            .tag("first")
            .through(Linear::on_device(4, 2, dev).unwrap())
            .build()
            .unwrap();

        let params = g.parameters_at("first").unwrap();
        assert_eq!(params.len(), 2); // 1 Linear: weight + bias
    }

    #[test]
    fn test_freeze_thaw_roundtrip() {
        let dev = test_device();
        let inner = FlowBuilder::from(Linear::on_device(3, 4, dev).unwrap())
            .label("encoder")
            .build()
            .unwrap();

        let outer = FlowBuilder::from(inner)
            .through(Linear::on_device(4, 2, dev).unwrap())
            .build()
            .unwrap();

        // Initially not frozen
        assert!(!outer.is_frozen("encoder").unwrap());

        // Freeze child
        outer.freeze("encoder").unwrap();
        assert!(outer.is_frozen("encoder").unwrap());
        // All child params should have requires_grad = false
        for p in outer.parameters_at("encoder").unwrap() {
            assert!(p.is_frozen());
        }
        // Outer params still trainable
        let outer_params = outer.parameters();
        let outer_only: Vec<_> = outer_params.iter()
            .filter(|p| !p.is_frozen())
            .collect();
        assert_eq!(outer_only.len(), 2); // outer Linear: weight + bias

        // Thaw child
        outer.thaw("encoder").unwrap();
        assert!(!outer.is_frozen("encoder").unwrap());
        for p in outer.parameters_at("encoder").unwrap() {
            assert!(!p.is_frozen());
        }
    }

    #[test]
    fn test_freeze_deep_path() {
        let dev = test_device();
        let innermost = FlowBuilder::from(Linear::on_device(3, 4, dev).unwrap())
            .label("read")
            .build()
            .unwrap();
        let middle = FlowBuilder::from(innermost)
            .through(Linear::on_device(4, 2, dev).unwrap())
            .label("letter")
            .build()
            .unwrap();
        let outer = FlowBuilder::from(middle)
            .through(Linear::on_device(2, 1, dev).unwrap())
            .build()
            .unwrap();

        // Freeze only the innermost
        outer.freeze("letter.read").unwrap();
        assert!(outer.is_frozen("letter.read").unwrap());
        // "letter" overall is NOT fully frozen (it has its own Linear too)
        assert!(!outer.is_frozen("letter").unwrap());
    }

    #[test]
    fn test_named_parameters_at_uses_target_namespace() {
        let dev = test_device();
        let inner = FlowBuilder::from(Linear::on_device(3, 4, dev).unwrap())
            .tag("hidden")
            .through(Linear::on_device(4, 2, dev).unwrap())
            .label("encoder")
            .build()
            .unwrap();

        let outer = FlowBuilder::from(inner)
            .through(Linear::on_device(2, 1, dev).unwrap())
            .build()
            .unwrap();

        // Subgraph: uses child's own namespace
        let named = outer.named_parameters_at("encoder").unwrap();
        assert_eq!(named.len(), 4);
        // Names should use child-local prefixes (tag "hidden" and node id)
        assert!(named.iter().any(|(n, _)| n.starts_with("hidden/")));
    }

    #[test]
    fn test_freeze_invalid_path_error() {
        let dev = test_device();
        let g = FlowBuilder::from(Linear::on_device(3, 4, dev).unwrap())
            .build()
            .unwrap();

        assert!(g.freeze("nonexistent").is_err());
        assert!(g.thaw("nonexistent").is_err());
        assert!(g.is_frozen("nonexistent").is_err());
        assert!(g.parameters_at("nonexistent").is_err());
    }

    #[test]
    fn test_set_training_at() {
        let dev = test_device();
        let inner = FlowBuilder::from(Linear::on_device(3, 4, dev).unwrap())
            .through(crate::nn::Dropout::new(0.5))
            .label("encoder")
            .build()
            .unwrap();

        let outer = FlowBuilder::from(inner)
            .through(Linear::on_device(4, 2, dev).unwrap())
            .build()
            .unwrap();

        // Set child to eval mode
        outer.set_training_at("encoder", false).unwrap();
        // Set child back to training mode
        outer.set_training_at("encoder", true).unwrap();
        // Invalid path errors
        assert!(outer.set_training_at("nonexistent", false).is_err());
    }

    // ── Phase C: Checkpoint composition ──────────────────────────────

    #[test]
    fn test_subgraph_checkpoint_roundtrip() {
        let dev = test_device();
        // Build and "train" a child graph standalone
        let child = FlowBuilder::from(Linear::on_device(3, 4, dev).unwrap())
            .through(ReLU::new())
            .through(Linear::on_device(4, 2, dev).unwrap())
            .label("encoder")
            .build()
            .unwrap();

        // Save child checkpoint
        let dir = std::env::temp_dir().join("flodl_test_subgraph_ckpt");
        std::fs::create_dir_all(&dir).unwrap();
        let ckpt_path = dir.join("encoder.fdl");
        child.save_checkpoint(ckpt_path.to_str().unwrap()).unwrap();

        // Build parent with a fresh (randomly initialized) child of same architecture
        let fresh_child = FlowBuilder::from(Linear::on_device(3, 4, dev).unwrap())
            .through(ReLU::new())
            .through(Linear::on_device(4, 2, dev).unwrap())
            .label("encoder")
            .build()
            .unwrap();

        let parent = FlowBuilder::from(fresh_child)
            .through(Linear::on_device(2, 1, dev).unwrap())
            .build()
            .unwrap();

        // Load child checkpoint into parent's subgraph
        let report = parent.load_subgraph_checkpoint("encoder", ckpt_path.to_str().unwrap()).unwrap();
        assert!(report.loaded.len() >= 4); // At least weight+bias from 2 Linears
        assert!(report.missing.is_empty());

        // Clean up
        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn test_subgraph_checkpoint_preserves_parent_params() {
        let dev = test_device();
        let child = FlowBuilder::from(Linear::on_device(3, 4, dev).unwrap())
            .label("encoder")
            .build()
            .unwrap();

        let dir = std::env::temp_dir().join("flodl_test_preserve_parent");
        std::fs::create_dir_all(&dir).unwrap();
        let ckpt_path = dir.join("encoder.fdl");
        child.save_checkpoint(ckpt_path.to_str().unwrap()).unwrap();

        // Build parent with fresh child
        let fresh_child = FlowBuilder::from(Linear::on_device(3, 4, dev).unwrap())
            .label("encoder")
            .build()
            .unwrap();
        let parent = FlowBuilder::from(fresh_child)
            .through(Linear::on_device(4, 2, dev).unwrap())
            .build()
            .unwrap();

        // Snapshot parent-level param data
        let parent_w = parent.parameters().last().unwrap().variable.data().clone();

        // Load child checkpoint
        parent.load_subgraph_checkpoint("encoder", ckpt_path.to_str().unwrap()).unwrap();

        // Parent param unchanged
        let parent_w_after = parent.parameters().last().unwrap().variable.data().clone();
        let diff = parent_w.sub(&parent_w_after).unwrap().abs().unwrap().sum().unwrap().item().unwrap();
        assert!(diff < 1e-10, "parent params should be unchanged, diff={}", diff);

        let _ = std::fs::remove_dir_all(&dir);
    }

    // ── Phase D: Cross-boundary observation ──────────────────────────

    #[test]
    fn test_tagged_at_returns_value_after_forward() {
        let dev = test_device();
        let opts = test_opts();
        let inner = FlowBuilder::from(Linear::on_device(3, 4, dev).unwrap())
            .tag("hidden")
            .through(Linear::on_device(4, 2, dev).unwrap())
            .label("encoder")
            .build()
            .unwrap();

        let outer = FlowBuilder::from(inner)
            .through(Linear::on_device(2, 1, dev).unwrap())
            .build()
            .unwrap();

        let x = Variable::new(Tensor::randn(&[1, 3], opts).unwrap(), false);
        outer.forward(&x).unwrap();

        let val = outer.tagged_at("encoder.hidden").unwrap();
        assert!(val.is_some());
        assert_eq!(val.unwrap().shape(), vec![1, 4]);
    }

    #[test]
    fn test_tagged_at_before_forward_returns_none() {
        let dev = test_device();
        let inner = FlowBuilder::from(Linear::on_device(3, 4, dev).unwrap())
            .tag("hidden")
            .through(Linear::on_device(4, 2, dev).unwrap())
            .label("encoder")
            .build()
            .unwrap();

        let outer = FlowBuilder::from(inner)
            .through(Linear::on_device(2, 1, dev).unwrap())
            .build()
            .unwrap();

        // Before forward: path exists but no value computed
        let val = outer.tagged_at("encoder.hidden").unwrap();
        assert!(val.is_none());
    }

    #[test]
    fn test_tagged_at_invalid_path_returns_err() {
        let dev = test_device();
        let g = FlowBuilder::from(Linear::on_device(3, 4, dev).unwrap())
            .build()
            .unwrap();

        assert!(g.tagged_at("nonexistent.tag").is_err());
    }

    #[test]
    fn test_record_at_and_trend_at() {
        let dev = test_device();
        let inner = FlowBuilder::from(Linear::on_device(3, 4, dev).unwrap())
            .label("encoder")
            .build()
            .unwrap();

        let outer = FlowBuilder::from(inner)
            .through(Linear::on_device(4, 2, dev).unwrap())
            .build()
            .unwrap();

        // Record into child's buffer
        outer.record_at("encoder.loss", 0.5).unwrap();
        outer.record_at("encoder.loss", 0.3).unwrap();

        // Flush child's buffers to see the trend
        let child = outer.child_graph("encoder").unwrap();
        child.flush(&[]);

        let trend = outer.trend_at("encoder.loss").unwrap();
        assert_eq!(trend.len(), 1); // one epoch flushed
    }

    // ── Phase E: Developer experience ────────────────────────────────

    #[test]
    fn test_internal_tag_hidden_from_parent() {
        let dev = test_device();
        let inner = FlowBuilder::from(Linear::on_device(3, 4, dev).unwrap())
            .tag("_plumbing")
            .through(Linear::on_device(4, 2, dev).unwrap())
            .tag("output")
            .label("encoder")
            .build()
            .unwrap();

        let outer = FlowBuilder::from(inner)
            .through(Linear::on_device(2, 1, dev).unwrap())
            .build()
            .unwrap();

        // Auto-internal: _plumbing starts with underscore
        assert!(outer.child_graph("encoder").unwrap().internal_tags().contains("_plumbing"));
        // Internal tag blocked from parent
        assert!(outer.tagged_at("encoder._plumbing").is_err());
        // Non-internal tag accessible
        assert_eq!(outer.validate_path("encoder.output").unwrap(), PathKind::Tag);
    }

    #[test]
    fn test_explicit_internal_tag() {
        let dev = test_device();
        let inner = FlowBuilder::from(Linear::on_device(3, 4, dev).unwrap())
            .tag("intermediate")
            .internal("intermediate")
            .through(Linear::on_device(4, 2, dev).unwrap())
            .label("encoder")
            .build()
            .unwrap();

        let outer = FlowBuilder::from(inner)
            .through(Linear::on_device(2, 1, dev).unwrap())
            .build()
            .unwrap();

        // Explicitly internal: blocked from parent
        assert!(outer.tagged_at("encoder.intermediate").is_err());
    }

    #[test]
    fn test_tree_summary_output() {
        let dev = test_device();
        let inner = FlowBuilder::from(Linear::on_device(3, 4, dev).unwrap())
            .tag("hidden")
            .through(Linear::on_device(4, 2, dev).unwrap())
            .label("encoder")
            .build()
            .unwrap();

        let outer = FlowBuilder::from(inner)
            .through(Linear::on_device(2, 1, dev).unwrap())
            .build()
            .unwrap();

        let summary = outer.tree_summary();
        assert!(summary.contains("Graph Tree"), "missing header:\n{}", summary);
        assert!(summary.contains("encoder"), "missing child label:\n{}", summary);
        assert!(summary.contains("Parameter Summary"), "missing param summary:\n{}", summary);
    }

    #[test]
    fn test_param_summary_output() {
        let dev = test_device();
        let inner = FlowBuilder::from(Linear::on_device(3, 4, dev).unwrap())
            .label("encoder")
            .build()
            .unwrap();

        let outer = FlowBuilder::from(inner)
            .through(Linear::on_device(4, 2, dev).unwrap())
            .build()
            .unwrap();

        let summary = outer.param_summary();
        assert!(summary.contains("encoder"), "missing child:\n{}", summary);
        assert!(summary.contains("(own)"), "missing own params:\n{}", summary);
        assert!(summary.contains("trainable"), "missing trainable:\n{}", summary);
    }

    // ── Phase F: Tree-aware observation ──────────────────────────────

    #[test]
    fn test_flush_recurses_into_children() {
        let dev = test_device();
        let inner = FlowBuilder::from(Linear::on_device(3, 4, dev).unwrap())
            .label("encoder")
            .build()
            .unwrap();

        let outer = FlowBuilder::from(inner)
            .through(Linear::on_device(4, 2, dev).unwrap())
            .build()
            .unwrap();

        // Record into child via tree path
        outer.record_at("encoder.loss", 0.5).unwrap();
        outer.record_at("encoder.loss", 0.3).unwrap();
        // Record into parent
        outer.record_scalar("parent_loss", 1.0);

        // Single flush on parent should flush both
        outer.flush(&[]);

        // Parent flushed
        assert_eq!(outer.flush_count(), 1);
        assert_eq!(outer.trend("parent_loss").len(), 1);

        // Child also flushed
        let child = outer.child_graph("encoder").unwrap();
        assert_eq!(child.flush_count(), 1);
        assert_eq!(child.trend("loss").len(), 1);
    }

    #[test]
    fn test_latest_metrics_includes_children() {
        let dev = test_device();
        let inner = FlowBuilder::from(Linear::on_device(3, 4, dev).unwrap())
            .label("encoder")
            .build()
            .unwrap();

        let outer = FlowBuilder::from(inner)
            .through(Linear::on_device(4, 2, dev).unwrap())
            .build()
            .unwrap();

        // Record and flush
        outer.record_at("encoder.ce", 0.5).unwrap();
        outer.record_scalar("total_loss", 1.0);
        outer.flush(&[]);

        let metrics = outer.latest_metrics();
        let names: Vec<&str> = metrics.iter().map(|(n, _)| n.as_str()).collect();

        // Parent metric present
        assert!(names.contains(&"total_loss"), "missing parent metric: {:?}", names);
        // Child metric present with dotted prefix
        assert!(names.contains(&"encoder.ce"), "missing child metric: {:?}", names);
    }

    #[test]
    fn test_latest_metrics_local_excludes_children() {
        let dev = test_device();
        let inner = FlowBuilder::from(Linear::on_device(3, 4, dev).unwrap())
            .label("encoder")
            .build()
            .unwrap();

        let outer = FlowBuilder::from(inner)
            .through(Linear::on_device(4, 2, dev).unwrap())
            .build()
            .unwrap();

        outer.record_at("encoder.ce", 0.5).unwrap();
        outer.record_scalar("total_loss", 1.0);
        outer.flush(&[]);

        let local = outer.latest_metrics_local();
        let names: Vec<&str> = local.iter().map(|(n, _)| n.as_str()).collect();

        assert!(names.contains(&"total_loss"));
        assert!(!names.contains(&"encoder.ce"), "local should not include children: {:?}", names);
    }

    #[test]
    fn test_double_flush_is_safe() {
        let dev = test_device();
        let inner = FlowBuilder::from(Linear::on_device(3, 4, dev).unwrap())
            .label("encoder")
            .build()
            .unwrap();

        let outer = FlowBuilder::from(inner)
            .through(Linear::on_device(4, 2, dev).unwrap())
            .build()
            .unwrap();

        outer.record_at("encoder.loss", 0.5).unwrap();

        // Flush child explicitly first
        let child = outer.child_graph("encoder").unwrap();
        child.flush(&[]);
        assert_eq!(child.flush_count(), 1);

        // Parent flush recurses — child buffer already empty, no double epoch
        outer.flush(&[]);
        assert_eq!(child.flush_count(), 1); // still 1, not 2
        assert_eq!(child.trend("loss").len(), 1); // one epoch, not two
    }

    #[test]
    fn test_flush_local_skips_children() {
        let dev = test_device();
        let inner = FlowBuilder::from(Linear::on_device(3, 4, dev).unwrap())
            .label("encoder")
            .build()
            .unwrap();

        let outer = FlowBuilder::from(inner)
            .through(Linear::on_device(4, 2, dev).unwrap())
            .build()
            .unwrap();

        outer.record_at("encoder.loss", 0.5).unwrap();
        outer.record_scalar("parent_loss", 1.0);

        // flush_local: only parent
        outer.flush_local(&[]);

        assert_eq!(outer.flush_count(), 1);
        assert_eq!(outer.trend("parent_loss").len(), 1);

        // Child NOT flushed — data still in batch buffer
        let child = outer.child_graph("encoder").unwrap();
        assert_eq!(child.flush_count(), 0);
        assert_eq!(child.collected("loss").len(), 1); // still in batch buffer
    }

    #[test]
    fn test_flush_recurses_multi_level() {
        let dev = test_device();
        let innermost = FlowBuilder::from(Linear::on_device(3, 4, dev).unwrap())
            .label("read")
            .build()
            .unwrap();
        let middle = FlowBuilder::from(innermost)
            .through(Linear::on_device(4, 2, dev).unwrap())
            .label("letter")
            .build()
            .unwrap();
        let outer = FlowBuilder::from(middle)
            .through(Linear::on_device(2, 1, dev).unwrap())
            .build()
            .unwrap();

        // Record into deepest child
        outer.record_at("letter.read.hidden_loss", 0.7).unwrap();
        // Record into middle child
        outer.record_at("letter.mid_loss", 0.4).unwrap();

        outer.flush(&[]);

        // All levels flushed
        let metrics = outer.latest_metrics();
        let names: Vec<&str> = metrics.iter().map(|(n, _)| n.as_str()).collect();
        assert!(names.contains(&"letter.mid_loss"), "missing middle: {:?}", names);
        assert!(names.contains(&"letter.read.hidden_loss"), "missing deep: {:?}", names);
    }

    #[test]
    fn test_metrics_no_children_unchanged() {
        // Verify single-graph behavior is identical (no regression)
        let dev = test_device();
        let g = FlowBuilder::from(Linear::on_device(3, 4, dev).unwrap())
            .build()
            .unwrap();

        g.record_scalar("loss", 0.5);
        g.record_scalar("loss", 0.3);
        g.flush(&[]);

        let metrics = g.latest_metrics();
        assert_eq!(metrics.len(), 1);
        assert_eq!(metrics[0].0, "loss");
        assert!((metrics[0].1 - 0.4).abs() < 1e-10); // mean of 0.5 and 0.3
    }
}