trustformers-core 0.2.1

Core traits and utilities for TrustformeRS
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
//! Runtime topology modification and multi-path execution.
//!
//! A [`DynamicArchitectureManager`] plans several execution paths for an input,
//! runs them through a caller-supplied [`PathExecutor`], and combines the
//! results by the configured voting mechanism. Without an executor it refuses
//! to execute rather than echoing the input back.

use super::{ComputationBudget, LayerMetrics, LayerSkipPattern, ResourceAllocation};
use crate::tensor::Tensor;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::{Arc, RwLock};
use std::time::{Duration, Instant};

/// Base id for layers inserted by a runtime branch, kept clear of the base
/// architecture's layer numbering.
const BRANCH_LAYER_ID_BASE: usize = 9000;

// ===== DYNAMIC ARCHITECTURE SUPPORT =====

/// Dynamic architecture configuration supporting runtime topology modification
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DynamicArchitectureConfig {
    pub enable_dynamic_topology: bool,
    pub max_concurrent_paths: usize,
    pub path_selection_strategy: PathSelectionStrategy,
    pub architecture_search_enabled: bool,
    pub runtime_modification_enabled: bool,
    pub voting_mechanism: VotingMechanism,
    pub layer_insertion_threshold: f32,
    pub layer_removal_threshold: f32,
    pub branching_confidence_threshold: f32,
}

impl Default for DynamicArchitectureConfig {
    fn default() -> Self {
        Self {
            enable_dynamic_topology: true,
            max_concurrent_paths: 4,
            path_selection_strategy: PathSelectionStrategy::ConfidenceBased,
            architecture_search_enabled: false,
            runtime_modification_enabled: true,
            voting_mechanism: VotingMechanism::WeightedAverage,
            layer_insertion_threshold: 0.3,
            layer_removal_threshold: 0.8,
            branching_confidence_threshold: 0.5,
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum PathSelectionStrategy {
    ConfidenceBased,
    UncertaintyBased,
    EnsembleVoting,
    AdaptiveRouting,
    CostEffectiveness,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum VotingMechanism {
    MajorityVote,
    WeightedAverage,
    ConfidenceWeighted,
    UncertaintyWeighted,
    ExpertMixing,
}

/// Runs an input through the layers of one [`ExecutionPath`].
///
/// The adaptive-computation machinery decides *which* path to take; it does not
/// own a layer stack, so the caller supplies the thing that can actually run
/// one. Without an executor, [`DynamicArchitectureManager::execute_multi_path`]
/// reports that it cannot execute anything rather than returning the input with
/// an invented confidence.
pub trait PathExecutor: Send + Sync {
    /// Execute `input` through `path`, returning the path's output.
    fn execute(
        &self,
        input: &Tensor,
        path: &ExecutionPath,
    ) -> Result<Tensor, Box<dyn std::error::Error>>;
}

/// Dynamic architecture manager supporting runtime topology changes
pub struct DynamicArchitectureManager {
    config: DynamicArchitectureConfig,
    #[allow(dead_code)]
    active_paths: Arc<RwLock<HashMap<String, ExecutionPath>>>,
    #[allow(dead_code)]
    architecture_cache: Arc<RwLock<HashMap<String, CachedArchitecture>>>,
    performance_history: Arc<RwLock<Vec<ArchitecturePerformance>>>,
    topology_modifier: TopologyModifier,
    path_router: PathRouter,
    /// The layer stack that actually runs a path, when one is wired up.
    path_executor: Option<Arc<dyn PathExecutor>>,
}

impl DynamicArchitectureManager {
    pub fn new(config: DynamicArchitectureConfig) -> Self {
        Self {
            config: config.clone(),
            active_paths: Arc::new(RwLock::new(HashMap::new())),
            architecture_cache: Arc::new(RwLock::new(HashMap::new())),
            performance_history: Arc::new(RwLock::new(Vec::new())),
            topology_modifier: TopologyModifier::new(config.clone()),
            path_router: PathRouter::new(config.clone()),
            path_executor: None,
        }
    }

    /// Wire up the layer stack that executes a path.
    pub fn with_path_executor(mut self, executor: Arc<dyn PathExecutor>) -> Self {
        self.path_executor = Some(executor);
        self
    }

    /// Wire up the layer stack that executes a path, in place.
    pub fn set_path_executor(&mut self, executor: Arc<dyn PathExecutor>) {
        self.path_executor = Some(executor);
    }

    /// Whether a path executor is available.
    pub fn has_path_executor(&self) -> bool {
        self.path_executor.is_some()
    }

    /// Create dynamic execution plan with multiple paths
    pub fn create_dynamic_execution_plan(
        &self,
        input: &Tensor,
        base_architecture: &ArchitectureBlueprint,
        constraints: &ComputationBudget,
    ) -> Result<DynamicExecutionPlan, Box<dyn std::error::Error>> {
        let input_complexity = self.analyze_input_complexity(input)?;

        // Generate multiple execution paths
        let execution_paths =
            self.generate_execution_paths(input_complexity, base_architecture, constraints)?;

        // Select optimal paths based on strategy
        let selected_paths = self.path_router.select_optimal_paths(
            &execution_paths,
            &self.config.path_selection_strategy,
            self.config.max_concurrent_paths,
        )?;

        Ok(DynamicExecutionPlan {
            paths: selected_paths,
            voting_mechanism: self.config.voting_mechanism.clone(),
            fallback_path: self.create_fallback_path(base_architecture)?,
            modification_points: self.identify_modification_points(base_architecture)?,
        })
    }

    /// Modify architecture topology at runtime
    pub fn modify_architecture_runtime(
        &self,
        current_state: &ExecutionState,
        performance_metrics: &LayerMetrics,
        architecture: &mut ArchitectureBlueprint,
    ) -> Result<Vec<TopologyModification>, Box<dyn std::error::Error>> {
        if !self.config.runtime_modification_enabled {
            return Ok(vec![]);
        }

        let mut modifications = Vec::new();

        // Dynamic layer insertion based on uncertainty
        if performance_metrics.uncertainty_score > self.config.layer_insertion_threshold {
            let insertion_point = self
                .topology_modifier
                .find_optimal_insertion_point(current_state, architecture)?;

            if let Some(point) = insertion_point {
                let new_layer =
                    self.topology_modifier.create_adaptive_layer(point, performance_metrics)?;

                modifications.push(TopologyModification::InsertLayer {
                    position: point,
                    layer_config: new_layer,
                });
            }
        }

        // Dynamic layer removal based on confidence
        if performance_metrics.confidence_score > self.config.layer_removal_threshold {
            let removal_candidates =
                self.topology_modifier.identify_redundant_layers(current_state, architecture)?;

            for candidate in removal_candidates {
                modifications.push(TopologyModification::RemoveLayer {
                    position: candidate,
                });
            }
        }

        // Dynamic branching based on confidence distribution
        if self.should_create_branch(performance_metrics) {
            let branch_config = self
                .topology_modifier
                .create_branch_configuration(current_state, performance_metrics)?;

            modifications.push(TopologyModification::CreateBranch {
                source_position: current_state.current_layer,
                branch_config,
            });
        }

        // Apply modifications to architecture
        for modification in &modifications {
            self.topology_modifier.apply_modification(architecture, modification)?;
        }

        Ok(modifications)
    }

    /// Execute multi-path computation with voting
    pub fn execute_multi_path(
        &self,
        input: &Tensor,
        execution_plan: &DynamicExecutionPlan,
    ) -> Result<MultiPathResult, Box<dyn std::error::Error>> {
        let start_time = Instant::now();
        let mut path_results = Vec::new();
        let mut path_metrics = Vec::new();

        // Execute all paths concurrently (simplified serial execution for now)
        for (path_id, path) in execution_plan.paths.iter().enumerate() {
            let path_start = Instant::now();
            let result = self.execute_single_path(input, path)?;
            let execution_time = path_start.elapsed();

            let metrics = PathExecutionMetrics {
                path_id,
                execution_time,
                confidence: result.confidence,
                accuracy_estimate: result.accuracy_estimate,
                resource_usage: result.resource_usage.clone(),
            };

            path_results.push(result);
            path_metrics.push(metrics);
        }

        // Combine results using voting mechanism
        let final_result = self.combine_path_results(
            &path_results,
            &path_metrics,
            &execution_plan.voting_mechanism,
        )?;

        // Record performance for future optimization
        self.record_multi_path_performance(&path_metrics, &final_result);

        Ok(MultiPathResult {
            result: final_result,
            path_metrics,
            total_execution_time: start_time.elapsed(),
            paths_executed: path_results.len(),
        })
    }

    fn analyze_input_complexity(&self, input: &Tensor) -> Result<f32, Box<dyn std::error::Error>> {
        // Compute input complexity using entropy, variance, and distribution analysis
        let entropy = self.compute_entropy(input)?;
        let variance = self.compute_variance(input)?;
        let sparsity = self.compute_sparsity(input)?;

        // Combine metrics into unified complexity score
        let complexity = (entropy * 0.4 + variance * 0.3 + (1.0 - sparsity) * 0.3).clamp(0.0, 1.0);
        Ok(complexity)
    }

    fn generate_execution_paths(
        &self,
        complexity: f32,
        architecture: &ArchitectureBlueprint,
        constraints: &ComputationBudget,
    ) -> Result<Vec<ExecutionPath>, Box<dyn std::error::Error>> {
        let mut paths = Vec::new();

        // Conservative path (minimal layers, high accuracy)
        let conservative_path = ExecutionPath {
            path_id: "conservative".to_string(),
            layers: self.select_essential_layers(architecture)?,
            skip_patterns: HashMap::new(),
            resource_allocation: self.allocate_resources_conservatively(constraints)?,
            expected_accuracy: 0.9,
            expected_latency: Duration::from_millis(50),
        };
        paths.push(conservative_path);

        // Aggressive path (maximum layers, highest accuracy)
        let aggressive_path = ExecutionPath {
            path_id: "aggressive".to_string(),
            layers: architecture.layers.clone(),
            skip_patterns: HashMap::new(),
            resource_allocation: self.allocate_resources_aggressively(constraints)?,
            expected_accuracy: 0.95,
            expected_latency: Duration::from_millis(200),
        };
        paths.push(aggressive_path);

        // Adaptive path (complexity-based selection)
        let adaptive_layers = self.select_adaptive_layers(architecture, complexity)?;
        let adaptive_path = ExecutionPath {
            path_id: "adaptive".to_string(),
            layers: adaptive_layers,
            skip_patterns: self.generate_skip_patterns(complexity)?,
            resource_allocation: self.allocate_resources_adaptively(constraints, complexity)?,
            expected_accuracy: 0.85 + complexity * 0.1,
            expected_latency: Duration::from_millis((100.0 + complexity * 100.0) as u64),
        };
        paths.push(adaptive_path);

        // Efficient path (optimized for speed)
        let efficient_path = ExecutionPath {
            path_id: "efficient".to_string(),
            layers: self.select_efficient_layers(architecture)?,
            skip_patterns: self.generate_efficiency_skip_patterns()?,
            resource_allocation: self.allocate_resources_efficiently(constraints)?,
            expected_accuracy: 0.8,
            expected_latency: Duration::from_millis(30),
        };
        paths.push(efficient_path);

        Ok(paths)
    }

    fn should_create_branch(&self, metrics: &LayerMetrics) -> bool {
        metrics.uncertainty_score > self.config.branching_confidence_threshold
            && metrics.confidence_score < 0.8 // High uncertainty, moderate confidence
    }

    /// Execute one path through the wired-up layer stack.
    ///
    /// `confidence` is measured from the path's own output distribution and the
    /// resource figures are measured from the real output tensor and the real
    /// elapsed time. Without a [`PathExecutor`] this errors instead of echoing
    /// the input back with a fixed confidence.
    fn execute_single_path(
        &self,
        input: &Tensor,
        path: &ExecutionPath,
    ) -> Result<PathResult, Box<dyn std::error::Error>> {
        let executor =
            self.path_executor.as_ref().ok_or_else(|| -> Box<dyn std::error::Error> {
                format!(
                    "cannot execute path '{}': no PathExecutor is wired up. Call \
                 DynamicArchitectureManager::with_path_executor with the layer stack that \
                 should run the path.",
                    path.path_id
                )
                .into()
            })?;

        let start = Instant::now();
        let output = executor.execute(input, path)?;
        let elapsed = start.elapsed();

        let confidence = Self::output_confidence(&output)?;
        let memory_mb = (output.size_bytes() / (1024 * 1024)).max(1) as u32;
        let flops = output.shape().iter().product::<usize>() as u64;

        Ok(PathResult {
            output,
            confidence,
            accuracy_estimate: path.expected_accuracy,
            resource_usage: ResourceUsage {
                memory_mb,
                flops,
                time_ms: elapsed.as_millis() as u32,
            },
        })
    }

    /// Confidence of an output distribution: `1 - H(softmax(x)) / ln(n)`.
    ///
    /// 1.0 for a one-hot output, 0.0 for a uniform one.
    fn output_confidence(output: &Tensor) -> Result<f32, Box<dyn std::error::Error>> {
        let values = output.data()?;
        if values.len() < 2 {
            return Ok(0.0);
        }

        let max_value = values.iter().copied().fold(f32::NEG_INFINITY, f32::max);
        let exponentials: Vec<f32> = values.iter().map(|value| (value - max_value).exp()).collect();
        let sum: f32 = exponentials.iter().sum();
        if sum <= 0.0 || !sum.is_finite() {
            return Ok(0.0);
        }

        let mut entropy = 0.0f32;
        for exponential in &exponentials {
            let probability = exponential / sum;
            if probability > 1e-12 {
                entropy -= probability * probability.ln();
            }
        }

        let max_entropy = (values.len() as f32).ln();
        Ok((1.0 - entropy / max_entropy).clamp(0.0, 1.0))
    }

    fn combine_path_results(
        &self,
        results: &[PathResult],
        metrics: &[PathExecutionMetrics],
        voting_mechanism: &VotingMechanism,
    ) -> Result<CombinedResult, Box<dyn std::error::Error>> {
        match voting_mechanism {
            VotingMechanism::WeightedAverage => {
                // Weight by confidence scores
                let total_confidence: f32 = metrics.iter().map(|m| m.confidence).sum();
                let mut weighted_output = Tensor::zeros_like(&results[0].output)?;

                for (result, metric) in results.iter().zip(metrics.iter()) {
                    let weight = metric.confidence / total_confidence;
                    let scaled_output = result.output.mul_scalar(weight)?;
                    weighted_output = weighted_output.add(&scaled_output)?;
                }

                Ok(CombinedResult {
                    output: weighted_output,
                    confidence: total_confidence / results.len() as f32,
                    consensus_score: self.calculate_consensus_score(metrics),
                })
            },
            VotingMechanism::MajorityVote => {
                // Find the most confident result
                let best_idx = metrics
                    .iter()
                    .enumerate()
                    .max_by(|(_, a), (_, b)| {
                        a.confidence
                            .partial_cmp(&b.confidence)
                            .unwrap_or(::std::cmp::Ordering::Equal)
                    })
                    .map(|(idx, _)| idx)
                    .unwrap_or(0);

                Ok(CombinedResult {
                    output: results[best_idx].output.clone(),
                    confidence: metrics[best_idx].confidence,
                    consensus_score: self.calculate_consensus_score(metrics),
                })
            },
            _ => {
                // Fallback to weighted average
                self.combine_path_results(results, metrics, &VotingMechanism::WeightedAverage)
            },
        }
    }

    fn calculate_consensus_score(&self, metrics: &[PathExecutionMetrics]) -> f32 {
        if metrics.len() < 2 {
            return 1.0;
        }

        let mean_confidence: f32 =
            metrics.iter().map(|m| m.confidence).sum::<f32>() / metrics.len() as f32;
        let variance =
            metrics.iter().map(|m| (m.confidence - mean_confidence).powi(2)).sum::<f32>()
                / metrics.len() as f32;

        // Higher consensus when variance is low
        (1.0 - variance.sqrt()).clamp(0.0, 1.0)
    }

    fn record_multi_path_performance(
        &self,
        path_metrics: &[PathExecutionMetrics],
        result: &CombinedResult,
    ) {
        let performance = ArchitecturePerformance {
            timestamp: Instant::now(),
            path_count: path_metrics.len(),
            average_confidence: path_metrics.iter().map(|m| m.confidence).sum::<f32>()
                / path_metrics.len() as f32,
            consensus_score: result.consensus_score,
            total_resource_usage: path_metrics.iter().map(|m| m.resource_usage.memory_mb).sum(),
        };

        if let Ok(mut history) = self.performance_history.write() {
            history.push(performance);
            // Keep only recent history (last 1000 entries)
            if history.len() > 1000 {
                let drain_count = history.len() - 1000;
                history.drain(0..drain_count);
            }
        }
    }

    // Helper methods for path generation
    fn select_essential_layers(
        &self,
        arch: &ArchitectureBlueprint,
    ) -> Result<Vec<LayerConfig>, Box<dyn std::error::Error>> {
        Ok(arch.layers.iter().take(arch.layers.len() / 2).cloned().collect())
    }

    fn select_adaptive_layers(
        &self,
        arch: &ArchitectureBlueprint,
        complexity: f32,
    ) -> Result<Vec<LayerConfig>, Box<dyn std::error::Error>> {
        let layer_count =
            ((arch.layers.len() as f32 * (0.5 + complexity * 0.5)) as usize).min(arch.layers.len());
        Ok(arch.layers.iter().take(layer_count).cloned().collect())
    }

    fn select_efficient_layers(
        &self,
        arch: &ArchitectureBlueprint,
    ) -> Result<Vec<LayerConfig>, Box<dyn std::error::Error>> {
        Ok(arch.layers.iter().step_by(2).cloned().collect())
    }

    fn generate_skip_patterns(
        &self,
        complexity: f32,
    ) -> Result<HashMap<usize, LayerSkipPattern>, Box<dyn std::error::Error>> {
        let mut patterns = HashMap::new();
        if complexity < 0.3 {
            // Skip every other layer for simple inputs
            for i in (1..10).step_by(2) {
                patterns.insert(i, LayerSkipPattern::Skip);
            }
        }
        Ok(patterns)
    }

    fn generate_efficiency_skip_patterns(
        &self,
    ) -> Result<HashMap<usize, LayerSkipPattern>, Box<dyn std::error::Error>> {
        let mut patterns = HashMap::new();
        // Aggressive skipping for efficiency
        for i in 2..10 {
            if i % 3 == 0 {
                patterns.insert(i, LayerSkipPattern::Approximate);
            }
        }
        Ok(patterns)
    }

    fn allocate_resources_conservatively(
        &self,
        budget: &ComputationBudget,
    ) -> Result<ResourceAllocation, Box<dyn std::error::Error>> {
        Ok(ResourceAllocation {
            memory_per_layer: (0..5).map(|i| (i, budget.max_memory_mb / 10)).collect(),
            compute_intensity: (0..5).map(|i| (i, 0.5)).collect(),
            parallelism_factor: (0..5).map(|i| (i, 1)).collect(),
        })
    }

    fn allocate_resources_aggressively(
        &self,
        budget: &ComputationBudget,
    ) -> Result<ResourceAllocation, Box<dyn std::error::Error>> {
        Ok(ResourceAllocation {
            memory_per_layer: (0..10).map(|i| (i, budget.max_memory_mb / 5)).collect(),
            compute_intensity: (0..10).map(|i| (i, 1.0)).collect(),
            parallelism_factor: (0..10).map(|i| (i, 2)).collect(),
        })
    }

    fn allocate_resources_adaptively(
        &self,
        budget: &ComputationBudget,
        complexity: f32,
    ) -> Result<ResourceAllocation, Box<dyn std::error::Error>> {
        let layer_count = (8.0 * (0.5 + complexity * 0.5)) as usize;
        Ok(ResourceAllocation {
            memory_per_layer: (0..layer_count)
                .map(|i| {
                    (
                        i,
                        (budget.max_memory_mb as f32 * (0.5 + complexity * 0.5)) as u32
                            / layer_count as u32,
                    )
                })
                .collect(),
            compute_intensity: (0..layer_count).map(|i| (i, 0.5 + complexity * 0.5)).collect(),
            parallelism_factor: (0..layer_count)
                .map(|i| (i, 1 + (complexity * 2.0) as u32))
                .collect(),
        })
    }

    fn allocate_resources_efficiently(
        &self,
        budget: &ComputationBudget,
    ) -> Result<ResourceAllocation, Box<dyn std::error::Error>> {
        Ok(ResourceAllocation {
            memory_per_layer: (0..3).map(|i| (i, budget.max_memory_mb / 20)).collect(),
            compute_intensity: (0..3).map(|i| (i, 0.3)).collect(),
            parallelism_factor: (0..3).map(|i| (i, 4)).collect(),
        })
    }

    fn create_fallback_path(
        &self,
        arch: &ArchitectureBlueprint,
    ) -> Result<ExecutionPath, Box<dyn std::error::Error>> {
        Ok(ExecutionPath {
            path_id: "fallback".to_string(),
            layers: vec![arch.layers[0].clone()], // Minimal single layer
            skip_patterns: HashMap::new(),
            resource_allocation: ResourceAllocation {
                memory_per_layer: HashMap::from([(0, 50)]),
                compute_intensity: HashMap::from([(0, 0.1)]),
                parallelism_factor: HashMap::from([(0, 1)]),
            },
            expected_accuracy: 0.6,
            expected_latency: Duration::from_millis(10),
        })
    }

    fn identify_modification_points(
        &self,
        arch: &ArchitectureBlueprint,
    ) -> Result<Vec<ModificationPoint>, Box<dyn std::error::Error>> {
        let mut points = Vec::new();

        // Add modification points between layers
        for i in 0..arch.layers.len() - 1 {
            points.push(ModificationPoint {
                position: i,
                modification_type: ModificationType::LayerInsertion,
                confidence_threshold: 0.3,
            });
        }

        // Add branching points at quarter, half, and three-quarter positions
        for &fraction in &[0.25, 0.5, 0.75] {
            let position = (arch.layers.len() as f32 * fraction) as usize;
            points.push(ModificationPoint {
                position,
                modification_type: ModificationType::BranchingPoint,
                confidence_threshold: 0.5,
            });
        }

        Ok(points)
    }

    // Tensor operation helpers
    fn compute_entropy(&self, tensor: &Tensor) -> Result<f32, Box<dyn std::error::Error>> {
        // Normalised softmax entropy in [0, 1] — high = uncertain input.
        Ok(tensor.softmax_entropy_normalized()?)
    }

    fn compute_variance(&self, tensor: &Tensor) -> Result<f32, Box<dyn std::error::Error>> {
        // Population variance reduced to a scalar via the core statistical op.
        let var = tensor.variance(None, false)?;
        Ok(var.to_vec_f32()?.first().copied().unwrap_or(0.0))
    }

    fn compute_sparsity(&self, tensor: &Tensor) -> Result<f32, Box<dyn std::error::Error>> {
        // Fraction of zero elements via the core sparsity op.
        Ok(tensor.sparsity()?)
    }
}

// Supporting data structures for dynamic architectures

#[derive(Debug, Clone)]
pub struct ArchitectureBlueprint {
    pub layers: Vec<LayerConfig>,
    pub connections: Vec<ConnectionConfig>,
    pub metadata: ArchitectureMetadata,
}

#[derive(Debug, Clone)]
pub struct LayerConfig {
    pub layer_id: usize,
    pub layer_type: LayerType,
    pub parameters: HashMap<String, f32>,
    pub optional: bool,
}

#[derive(Debug, Clone)]
pub enum LayerType {
    Attention,
    FeedForward,
    Normalization,
    Embedding,
    Output,
    Custom(String),
}

#[derive(Debug, Clone)]
pub struct ConnectionConfig {
    pub from_layer: usize,
    pub to_layer: usize,
    pub connection_type: ConnectionType,
}

#[derive(Debug, Clone)]
pub enum ConnectionType {
    Sequential,
    Residual,
    Attention,
    Custom(String),
}

#[derive(Debug, Clone)]
pub struct ArchitectureMetadata {
    pub name: String,
    pub version: String,
    pub parameter_count: u64,
    pub memory_footprint_mb: u32,
}

#[derive(Debug, Clone)]
pub struct ExecutionPath {
    pub path_id: String,
    pub layers: Vec<LayerConfig>,
    pub skip_patterns: HashMap<usize, LayerSkipPattern>,
    pub resource_allocation: ResourceAllocation,
    pub expected_accuracy: f32,
    pub expected_latency: Duration,
}

#[derive(Debug)]
pub struct DynamicExecutionPlan {
    pub paths: Vec<ExecutionPath>,
    pub voting_mechanism: VotingMechanism,
    pub fallback_path: ExecutionPath,
    pub modification_points: Vec<ModificationPoint>,
}

#[derive(Debug)]
pub struct ExecutionState {
    pub current_layer: usize,
    pub intermediate_results: HashMap<usize, Tensor>,
    pub execution_metrics: Vec<LayerMetrics>,
    pub resource_usage: ResourceUsage,
}

#[derive(Debug, Clone)]
pub struct ResourceUsage {
    pub memory_mb: u32,
    pub flops: u64,
    pub time_ms: u32,
}

#[derive(Debug)]
pub enum TopologyModification {
    InsertLayer {
        position: usize,
        layer_config: LayerConfig,
    },
    RemoveLayer {
        position: usize,
    },
    CreateBranch {
        source_position: usize,
        branch_config: BranchConfig,
    },
    ModifyConnection {
        connection: ConnectionConfig,
    },
}

#[derive(Debug, Clone)]
pub struct BranchConfig {
    pub branch_layers: Vec<LayerConfig>,
    pub merge_strategy: MergeStrategy,
    pub condition: BranchCondition,
}

#[derive(Debug, Clone)]
pub enum MergeStrategy {
    Concatenation,
    Average,
    WeightedSum,
    Attention,
}

#[derive(Debug, Clone)]
pub enum BranchCondition {
    Always,
    ConfidenceThreshold(f32),
    UncertaintyThreshold(f32),
    Custom(String),
}

#[derive(Debug)]
pub struct ModificationPoint {
    pub position: usize,
    pub modification_type: ModificationType,
    pub confidence_threshold: f32,
}

#[derive(Debug)]
pub enum ModificationType {
    LayerInsertion,
    LayerRemoval,
    BranchingPoint,
    ConnectionModification,
}

#[derive(Debug)]
pub struct TopologyModifier {
    #[allow(dead_code)]
    config: DynamicArchitectureConfig,
}

impl TopologyModifier {
    pub fn new(config: DynamicArchitectureConfig) -> Self {
        Self { config }
    }

    pub fn find_optimal_insertion_point(
        &self,
        state: &ExecutionState,
        architecture: &ArchitectureBlueprint,
    ) -> Result<Option<usize>, Box<dyn std::error::Error>> {
        // Find the best position to insert a new layer based on current execution state
        if state.current_layer > 0 && state.current_layer < architecture.layers.len() {
            Ok(Some(state.current_layer))
        } else {
            Ok(None)
        }
    }

    pub fn create_adaptive_layer(
        &self,
        position: usize,
        metrics: &LayerMetrics,
    ) -> Result<LayerConfig, Box<dyn std::error::Error>> {
        // Create a new layer configuration based on current metrics
        let layer_type = if metrics.uncertainty_score > 0.7 {
            LayerType::Attention // Add attention for high uncertainty
        } else {
            LayerType::FeedForward // Add feedforward for general improvement
        };

        Ok(LayerConfig {
            layer_id: position * 1000, // Unique ID for inserted layers
            layer_type,
            parameters: HashMap::from([
                ("hidden_size".to_string(), 512.0),
                ("dropout".to_string(), 0.1),
            ]),
            optional: true,
        })
    }

    pub fn identify_redundant_layers(
        &self,
        state: &ExecutionState,
        architecture: &ArchitectureBlueprint,
    ) -> Result<Vec<usize>, Box<dyn std::error::Error>> {
        let mut redundant = Vec::new();

        // Identify layers that can be safely removed
        for (i, layer) in architecture.layers.iter().enumerate() {
            if layer.optional
                && state.execution_metrics.get(i).is_some_and(|m| m.confidence_score > 0.9)
            {
                redundant.push(i);
            }
        }

        Ok(redundant)
    }

    /// Build the configuration for a branch taken at the current execution
    /// state.
    ///
    /// The branch layer id is derived from the state's current layer, so it
    /// never collides with the base architecture's numbering.
    pub fn create_branch_configuration(
        &self,
        state: &ExecutionState,
        metrics: &LayerMetrics,
    ) -> Result<BranchConfig, Box<dyn std::error::Error>> {
        let branch_layers = vec![LayerConfig {
            layer_id: BRANCH_LAYER_ID_BASE + state.current_layer,
            layer_type: LayerType::Attention,
            parameters: HashMap::from([("heads".to_string(), 8.0)]),
            optional: true,
        }];

        Ok(BranchConfig {
            branch_layers,
            merge_strategy: MergeStrategy::WeightedSum,
            condition: BranchCondition::UncertaintyThreshold(metrics.uncertainty_score),
        })
    }

    pub fn apply_modification(
        &self,
        architecture: &mut ArchitectureBlueprint,
        modification: &TopologyModification,
    ) -> Result<(), Box<dyn std::error::Error>> {
        match modification {
            TopologyModification::InsertLayer {
                position,
                layer_config,
            } => {
                if *position <= architecture.layers.len() {
                    architecture.layers.insert(*position, layer_config.clone());
                }
            },
            TopologyModification::RemoveLayer { position } => {
                if *position < architecture.layers.len() {
                    architecture.layers.remove(*position);
                }
            },
            TopologyModification::CreateBranch {
                source_position,
                branch_config,
            } => {
                // Add branch layers after source position
                for (i, layer) in branch_config.branch_layers.iter().enumerate() {
                    architecture.layers.insert(source_position + i + 1, layer.clone());
                }
            },
            TopologyModification::ModifyConnection { connection } => {
                // Add or modify connections
                architecture.connections.push(connection.clone());
            },
        }
        Ok(())
    }
}

#[derive(Debug)]
pub struct PathRouter {
    #[allow(dead_code)]
    config: DynamicArchitectureConfig,
}

impl PathRouter {
    pub fn new(config: DynamicArchitectureConfig) -> Self {
        Self { config }
    }

    pub fn select_optimal_paths(
        &self,
        paths: &[ExecutionPath],
        strategy: &PathSelectionStrategy,
        max_paths: usize,
    ) -> Result<Vec<ExecutionPath>, Box<dyn std::error::Error>> {
        let mut selected = match strategy {
            PathSelectionStrategy::ConfidenceBased => {
                let mut sorted_paths = paths.to_vec();
                sorted_paths.sort_by(|a, b| {
                    b.expected_accuracy
                        .partial_cmp(&a.expected_accuracy)
                        .unwrap_or(::std::cmp::Ordering::Equal)
                });
                sorted_paths
            },
            PathSelectionStrategy::CostEffectiveness => {
                let mut sorted_paths = paths.to_vec();
                sorted_paths.sort_by(|a, b| {
                    let cost_a = a.expected_latency.as_millis() as f32 / a.expected_accuracy;
                    let cost_b = b.expected_latency.as_millis() as f32 / b.expected_accuracy;
                    cost_a.partial_cmp(&cost_b).unwrap_or(::std::cmp::Ordering::Equal)
                });
                sorted_paths
            },
            _ => paths.to_vec(),
        };

        selected.truncate(max_paths);
        Ok(selected)
    }
}

#[derive(Debug)]
pub struct PathResult {
    pub output: Tensor,
    pub confidence: f32,
    pub accuracy_estimate: f32,
    pub resource_usage: ResourceUsage,
}

#[derive(Debug)]
pub struct PathExecutionMetrics {
    pub path_id: usize,
    pub execution_time: Duration,
    pub confidence: f32,
    pub accuracy_estimate: f32,
    pub resource_usage: ResourceUsage,
}

#[derive(Debug)]
pub struct CombinedResult {
    pub output: Tensor,
    pub confidence: f32,
    pub consensus_score: f32,
}

#[derive(Debug)]
pub struct MultiPathResult {
    pub result: CombinedResult,
    pub path_metrics: Vec<PathExecutionMetrics>,
    pub total_execution_time: Duration,
    pub paths_executed: usize,
}

#[derive(Debug)]
pub struct ArchitecturePerformance {
    pub timestamp: Instant,
    pub path_count: usize,
    pub average_confidence: f32,
    pub consensus_score: f32,
    pub total_resource_usage: u32,
}

#[derive(Debug)]
pub struct CachedArchitecture {
    pub blueprint: ArchitectureBlueprint,
    pub performance_history: Vec<ArchitecturePerformance>,
    pub last_used: Instant,
}