1use crate::{
8 Extension, ExtensionCapability, ExtensionError, ExtensionJob, ExtensionResult, NQuin,
9 ResourceRequirements,
10};
11use async_trait::async_trait;
12use serde::{Deserialize, Serialize};
13use std::collections::HashMap;
14use std::time::{Duration, Instant};
15use uuid::Uuid;
16
17pub struct SnnExtension {
19 network_manager: SnnNetworkManager,
20 crdt_synchronizer: tokio::sync::Mutex<NoisyGradientCrdt>,
21 capability: ExtensionCapability,
22}
23
24pub struct SnnNetworkManager {
26 loaded_networks: HashMap<String, SpikingNetwork>,
27 network_cache_path: String,
28 temporal_processor: TemporalProcessor,
29}
30
31pub struct NoisyGradientCrdt {
33 node_id: Uuid,
34 gradient_state: GradientCrdtState,
35 noise_generator: NoiseGenerator,
36 sync_config: CrdtSyncConfig,
37}
38
39#[derive(Debug, Clone)]
41pub struct SpikingNetwork {
42 pub name: String,
43 pub network_type: NetworkType,
44 pub neurons: Vec<SpikingNeuron>,
45 pub synapses: Vec<Synapse>,
46 pub temporal_config: TemporalConfig,
47 pub crdt_config: CrdtConfig,
48}
49
50#[derive(Debug, Clone, Serialize, Deserialize)]
52pub enum NetworkType {
53 LIF, Izhikevich, HodgkinHuxley, SRM, AdEx, }
59
60#[derive(Debug, Clone)]
62pub struct SpikingNeuron {
63 pub id: u32,
64 pub neuron_type: NeuronType,
65 pub membrane_potential: f64,
66 pub threshold: f64,
67 pub refractory_period: Duration,
68 pub last_spike_time: Option<Instant>,
69 pub temporal_state: TemporalState,
70}
71
72#[derive(Debug, Clone, Serialize, Deserialize)]
74pub enum NeuronType {
75 Excitatory,
76 Inhibitory,
77 Modulatory,
78}
79
80#[derive(Debug, Clone)]
82pub struct TemporalState {
83 pub adaptation_current: f64,
84 pub recovery_variable: f64,
85 pub synaptic_current: f64,
86 pub noise_amplitude: f64,
87}
88
89#[derive(Debug, Clone)]
91pub struct Synapse {
92 pub pre_neuron_id: u32,
93 pub post_neuron_id: u32,
94 pub weight: f64,
95 pub delay: Duration,
96 pub plasticity_type: PlasticityType,
97 pub crdt_weight: CrdtWeight,
98}
99
100#[derive(Debug, Clone, Serialize, Deserialize)]
102pub enum PlasticityType {
103 Static,
104 STDP, RSTDP, Homeostatic, CRDT, }
109
110#[derive(Debug, Clone)]
112pub struct CrdtWeight {
113 pub value: f64,
114 pub version_vector: HashMap<Uuid, u64>,
115 pub last_update: Instant,
116 pub conflict_resolution: ConflictResolution,
117}
118
119#[derive(Debug, Clone, Serialize, Deserialize)]
121pub enum ConflictResolution {
122 LastWriterWins,
123 NoisyGradient, TemporalPriority, Consensus, }
127
128#[derive(Debug, Clone)]
130pub struct TemporalConfig {
131 pub time_step: Duration,
132 pub simulation_window: Duration,
133 pub spike_encoding: SpikeEncoding,
134 pub temporal_resolution: u32,
135}
136
137#[derive(Debug, Clone, Serialize, Deserialize)]
139pub enum SpikeEncoding {
140 Rate,
141 Temporal,
142 Phase,
143 RankOrder,
144}
145
146#[derive(Debug, Clone)]
148pub struct CrdtConfig {
149 pub sync_interval: Duration,
150 pub noise_amplitude: f64,
151 pub gradient_clipping: f64,
152 pub consensus_threshold: f64,
153 pub network_topology: NetworkTopology,
154}
155
156#[derive(Debug, Clone, Serialize, Deserialize)]
158pub enum NetworkTopology {
159 FullyConnected,
160 Ring,
161 Mesh,
162 Tree,
163 Random,
164}
165
166#[derive(Debug, Clone)]
168pub struct GradientCrdtState {
169 pub gradients: HashMap<String, CrdtGradient>,
170 pub version_vector: HashMap<Uuid, u64>,
171 pub pending_updates: Vec<PendingUpdate>,
172 pub conflict_buffer: ConflictBuffer,
173}
174
175#[derive(Debug, Clone)]
177pub struct CrdtGradient {
178 pub gradient_value: f64,
179 pub noisy_value: f64,
180 pub timestamp: Instant,
181 pub source_node: Uuid,
182 pub confidence: f64,
183}
184
185#[derive(Debug, Clone)]
187pub struct PendingUpdate {
188 pub update_id: Uuid,
189 pub gradient: CrdtGradient,
190 pub dependencies: Vec<Uuid>,
191 pub created_at: Instant,
192}
193
194#[derive(Debug, Clone)]
196pub struct ConflictBuffer {
197 pub conflicts: Vec<GradientConflict>,
198 pub resolution_strategy: ConflictResolution,
199 pub max_buffer_size: usize,
200}
201
202#[derive(Debug, Clone)]
204pub struct GradientConflict {
205 pub conflicting_gradients: Vec<CrdtGradient>,
206 pub conflict_type: ConflictType,
207 pub resolution_time: Instant,
208}
209
210#[derive(Debug, Clone, Serialize, Deserialize)]
212pub enum ConflictType {
213 VersionConflict,
214 ValueConflict,
215 TimingConflict,
216 TopologyConflict,
217}
218
219#[derive(Debug, Clone)]
221pub struct NoiseGenerator {
222 pub noise_type: NoiseType,
223 pub amplitude: f64,
224 pub correlation_time: Duration,
225 pub seed: u64,
226}
227
228#[derive(Debug, Clone, Serialize, Deserialize)]
230pub enum NoiseType {
231 Gaussian,
232 Uniform,
233 OrnsteinUhlenbeck,
234 Pink,
235 Brownian,
236}
237
238#[derive(Debug, Clone)]
240pub struct CrdtSyncConfig {
241 pub sync_protocol: SyncProtocol,
242 pub compression_enabled: bool,
243 pub encryption_enabled: bool,
244 pub batch_size: usize,
245 pub timeout: Duration,
246}
247
248#[derive(Debug, Clone, Serialize, Deserialize)]
250pub enum SyncProtocol {
251 Gossip,
252 PushPull,
253 TreeBased,
254 Consensus,
255}
256
257#[derive(Debug, Clone)]
259pub struct TemporalProcessor {
260 pub spike_queue: VecDeque<SpikeEvent>,
261 pub current_time: Instant,
262 pub time_step: Duration,
263 pub event_handlers: Vec<SpikeEventHandler>,
264}
265
266#[derive(Debug, Clone)]
268pub struct SpikeEvent {
269 pub neuron_id: u32,
270 pub spike_time: Instant,
271 pub spike_type: SpikeType,
272 pub propagation_delay: Duration,
273}
274
275#[derive(Debug, Clone, Serialize, Deserialize)]
277pub enum SpikeType {
278 Excitatory,
279 Inhibitory,
280 Modulatory,
281}
282
283#[derive(Debug, Clone)]
285pub struct SpikeEventHandler {
286 pub handler_type: HandlerType,
287 pub priority: u8,
288 pub enabled: bool,
289}
290
291#[derive(Debug, Clone, Serialize, Deserialize)]
293pub enum HandlerType {
294 Plasticity,
295 Synchronization,
296 Logging,
297 Monitoring,
298}
299
300#[derive(Debug, Clone, Serialize, Deserialize)]
302pub struct SnnJobParams {
303 pub network_name: String,
304 pub input_spikes: Vec<SpikeTrain>,
305 pub simulation_time: Duration,
306 pub learning_enabled: bool,
307 pub crdt_sync_enabled: bool,
308 pub noise_level: f64,
309}
310
311#[derive(Debug, Clone, Serialize, Deserialize)]
313pub struct SpikeTrain {
314 pub neuron_id: u32,
315 pub spike_times: Vec<Duration>,
316 pub spike_amplitudes: Vec<f64>,
317}
318
319#[derive(Debug, Clone, Serialize, Deserialize)]
321pub struct SnnExecutionResult {
322 pub output_spikes: Vec<SpikeTrain>,
323 pub membrane_potentials: Vec<Vec<f64>>,
324 pub synaptic_weights: Vec<Vec<f64>>,
325 pub learning_metrics: LearningMetrics,
326 pub crdt_sync_metrics: CrdtSyncMetrics,
327 pub execution_time_ms: u64,
328}
329
330#[derive(Debug, Clone, Serialize, Deserialize)]
332pub struct LearningMetrics {
333 pub final_loss: f64,
334 pub convergence_rate: f64,
335 pub spike_rate: f64,
336 pub synaptic_change: f64,
337 pub adaptation_level: f64,
338}
339
340#[derive(Debug, Clone, Serialize, Deserialize)]
342pub struct CrdtSyncMetrics {
343 pub sync_rounds: u32,
344 pub conflicts_resolved: u32,
345 pub convergence_achieved: bool,
346 pub noise_effectiveness: f64,
347 pub network_utilization: f64,
348}
349
350const STDP_A_PLUS: f64 = 0.01;
354const STDP_A_MINUS: f64 = 0.005;
356const STDP_W_MAX: f64 = 1.0;
358
359fn leak_factor(dt: Duration) -> f64 {
362 const TAU_M_S: f64 = 0.020;
363 (-dt.as_secs_f64() / TAU_M_S).exp()
364}
365
366fn group_into_spike_trains(events: &[(u32, Duration)]) -> Vec<SpikeTrain> {
368 let mut by_neuron: HashMap<u32, Vec<Duration>> = HashMap::new();
369 for &(nid, t) in events {
370 by_neuron.entry(nid).or_default().push(t);
371 }
372 by_neuron
373 .into_iter()
374 .map(|(neuron_id, mut spike_times)| {
375 spike_times.sort();
376 SpikeTrain {
377 neuron_id,
378 spike_amplitudes: vec![1.0; spike_times.len()],
379 spike_times,
380 }
381 })
382 .collect()
383}
384
385impl SnnExtension {
386 pub fn new() -> Self {
388 let node_id = Uuid::new_v4();
389 let crdt_synchronizer = NoisyGradientCrdt::new(node_id);
390
391 let network_manager = SnnNetworkManager {
392 loaded_networks: HashMap::new(),
393 network_cache_path: std::env::var("QUALIA_SNN_CACHE")
394 .unwrap_or_else(|_| "./snn_networks".to_string()),
395 temporal_processor: TemporalProcessor::new(),
396 };
397
398 Self {
399 network_manager,
400 crdt_synchronizer: tokio::sync::Mutex::new(crdt_synchronizer),
401 capability: ExtensionCapability {
402 name: "snn".to_string(),
403 version: "1.0.0".to_string(),
404 description: "Spiking Neural Networks with noisy gradient CRDT synchronization"
405 .to_string(),
406 required_resources: ResourceRequirements {
407 min_memory_mb: 512,
408 min_vram_mb: Some(256),
409 requires_gpu: true,
410 requires_network: true, max_concurrent_jobs: 2,
412 },
413 supported_operations: vec![
414 "simulate_snn".to_string(),
415 "train_distributed".to_string(),
416 "sync_gradients".to_string(),
417 "resolve_conflicts".to_string(),
418 "export_network".to_string(),
419 "import_network".to_string(),
420 ],
421 },
422 }
423 }
424
425 async fn simulate_snn(
426 &self,
427 params: SnnJobParams,
428 ) -> Result<SnnExecutionResult, ExtensionError> {
429 let network = self
430 .network_manager
431 .get_network(¶ms.network_name)
432 .ok_or_else(|| {
433 ExtensionError::ExtensionNotFound(format!(
434 "Network '{}' not found",
435 params.network_name
436 ))
437 })?;
438
439 let start_time = Instant::now();
440
441 let result = self.execute_snn_simulation(network, ¶ms).await?;
443
444 let sync_result = if params.crdt_sync_enabled {
445 let mut sync = self.crdt_synchronizer.lock().await;
446 sync.synchronize_gradients(&result).await?
447 } else {
448 CrdtSyncMetrics::default()
449 };
450
451 let execution_time = start_time.elapsed().as_millis() as u64;
452
453 Ok(SnnExecutionResult {
454 output_spikes: result.output_spikes,
455 membrane_potentials: result.membrane_potentials,
456 synaptic_weights: result.synaptic_weights,
457 learning_metrics: result.learning_metrics,
458 crdt_sync_metrics: sync_result,
459 execution_time_ms: execution_time,
460 })
461 }
462
463 async fn execute_snn_simulation(
464 &self,
465 network: &SpikingNetwork,
466 params: &SnnJobParams,
467 ) -> Result<SnnExecutionResult, ExtensionError> {
468 let mut network_sim = network.clone();
469 let dt = network_sim.temporal_config.time_step;
470 let mut current_time = Duration::ZERO;
471 let mut membrane_potentials = Vec::new();
472 let mut synaptic_weights = Vec::new();
473
474 let mut prev_fired: std::collections::HashSet<u32> = std::collections::HashSet::new();
476 let mut last_spike_sim: HashMap<u32, Duration> = HashMap::new();
477 let mut output_events: Vec<(u32, Duration)> = Vec::new();
478
479 let neuron_sign: HashMap<u32, f64> = network_sim
481 .neurons
482 .iter()
483 .map(|n| {
484 let s = match n.neuron_type {
485 NeuronType::Excitatory => 1.0,
486 NeuronType::Inhibitory => -1.0,
487 NeuronType::Modulatory => 0.5,
488 };
489 (n.id, s)
490 })
491 .collect();
492
493 while current_time < params.simulation_time {
494 let mut external_fired: std::collections::HashSet<u32> =
496 std::collections::HashSet::new();
497 for spike_train in ¶ms.input_spikes {
498 if spike_train
499 .spike_times
500 .iter()
501 .any(|&t| t <= current_time && current_time.saturating_sub(t) < dt)
502 {
503 external_fired.insert(spike_train.neuron_id);
504 }
505 }
506
507 let fired = self.step_neurons(
508 &mut network_sim,
509 current_time,
510 dt,
511 &prev_fired,
512 &external_fired,
513 &neuron_sign,
514 &mut last_spike_sim,
515 );
516 for &nid in &fired {
517 output_events.push((nid, current_time));
518 }
519
520 membrane_potentials.push(
521 network_sim
522 .neurons
523 .iter()
524 .map(|n| n.membrane_potential)
525 .collect(),
526 );
527 synaptic_weights.push(network_sim.synapses.iter().map(|s| s.weight).collect());
528
529 prev_fired = fired.into_iter().chain(external_fired).collect();
531 current_time += dt;
532 }
533
534 let output_spikes = group_into_spike_trains(&output_events);
535 let learning_metrics =
536 self.calculate_learning_metrics(&membrane_potentials, &synaptic_weights);
537
538 Ok(SnnExecutionResult {
539 output_spikes,
540 membrane_potentials,
541 synaptic_weights,
542 learning_metrics,
543 crdt_sync_metrics: CrdtSyncMetrics::default(),
544 execution_time_ms: 0, })
546 }
547
548 fn step_neurons(
553 &self,
554 network: &mut SpikingNetwork,
555 current_time: Duration,
556 dt: Duration,
557 prev_fired: &std::collections::HashSet<u32>,
558 external_fired: &std::collections::HashSet<u32>,
559 neuron_sign: &HashMap<u32, f64>,
560 last_spike_sim: &mut HashMap<u32, Duration>,
561 ) -> Vec<u32> {
562 let mut input: HashMap<u32, f64> = HashMap::new();
565 for syn in &network.synapses {
566 let pre_fired = prev_fired.contains(&syn.pre_neuron_id)
567 || external_fired.contains(&syn.pre_neuron_id);
568 if pre_fired {
569 let sign = neuron_sign.get(&syn.pre_neuron_id).copied().unwrap_or(1.0);
570 *input.entry(syn.post_neuron_id).or_insert(0.0) += sign * syn.weight;
571 }
572 }
573
574 let mut fired = Vec::new();
576 for neuron in &mut network.neurons {
577 if let Some(&last) = last_spike_sim.get(&neuron.id) {
578 if current_time.saturating_sub(last) < neuron.refractory_period {
579 neuron.membrane_potential = 0.0;
580 continue;
581 }
582 }
583 let syn_in = input.get(&neuron.id).copied().unwrap_or(0.0);
584 let noise = self.generate_noise(neuron.temporal_state.noise_amplitude);
585 neuron.membrane_potential =
586 neuron.membrane_potential * leak_factor(dt) + syn_in + noise;
587 if neuron.membrane_potential >= neuron.threshold {
588 fired.push(neuron.id);
589 neuron.membrane_potential = 0.0;
590 neuron.last_spike_time = Some(Instant::now());
591 last_spike_sim.insert(neuron.id, current_time);
592 }
593 }
594
595 let fired_set: std::collections::HashSet<u32> = fired.iter().copied().collect();
597 for syn in &mut network.synapses {
598 if !matches!(
599 syn.plasticity_type,
600 PlasticityType::STDP | PlasticityType::RSTDP | PlasticityType::CRDT
601 ) {
602 continue;
603 }
604 let pre_fired = prev_fired.contains(&syn.pre_neuron_id)
605 || external_fired.contains(&syn.pre_neuron_id);
606 let post_fired = fired_set.contains(&syn.post_neuron_id);
607 if pre_fired && post_fired {
608 syn.weight = (syn.weight + STDP_A_PLUS).clamp(0.0, STDP_W_MAX);
609 } else if post_fired && !pre_fired {
610 syn.weight = (syn.weight - STDP_A_MINUS).clamp(0.0, STDP_W_MAX);
611 }
612 }
613
614 fired
615 }
616
617 fn generate_noise(&self, amplitude: f64) -> f64 {
618 if amplitude <= 0.0 {
621 return 0.0;
622 }
623 use rand::RngExt;
624 let mut rng = rand::rng();
625 rng.random_range(-amplitude..amplitude)
626 }
627
628 fn calculate_learning_metrics(
629 &self,
630 potentials: &[Vec<f64>],
631 weights: &[Vec<f64>],
632 ) -> LearningMetrics {
633 let final_loss = if potentials.len() > 1 {
634 (potentials[potentials.len() - 1][0] - potentials[potentials.len() - 2][0]).abs()
635 } else {
636 0.0
637 };
638
639 let convergence_rate = if potentials.len() > 10 {
640 let recent_changes: Vec<f64> = potentials
641 .iter()
642 .skip(potentials.len() - 10)
643 .zip(potentials.iter().skip(potentials.len() - 9))
644 .map(|(curr, prev)| (curr[0] - prev[0]).abs())
645 .collect();
646 recent_changes.iter().sum::<f64>() / recent_changes.len() as f64
647 } else {
648 0.0
649 };
650
651 let spike_rate = potentials
652 .iter()
653 .map(|p| p.iter().filter(|&v| *v > 0.0).count() as f64 / p.len() as f64)
654 .sum::<f64>()
655 / potentials.len() as f64;
656
657 let synaptic_change = if weights.len() > 1 {
658 let initial_sum = weights[0].iter().sum::<f64>();
659 let final_sum = weights[weights.len() - 1].iter().sum::<f64>();
660 (final_sum - initial_sum).abs()
661 } else {
662 0.0
663 };
664
665 LearningMetrics {
666 final_loss,
667 convergence_rate,
668 spike_rate,
669 synaptic_change,
670 adaptation_level: 0.5, }
672 }
673
674 fn result_to_quins(result: &SnnExecutionResult, job_id: &str) -> Vec<NQuin> {
675 let mut quins = Vec::new();
676
677 let learning_quin = NQuin {
679 subject: crate::q_hash(job_id),
680 predicate: crate::q_hash("q42:hasLearningMetrics"),
681 object: (result.learning_metrics.final_loss * 1000000.0) as u64,
682 context: crate::q_hash("snn:learning"),
683 metadata: ((result.learning_metrics.convergence_rate * 1000000.0) as u64) << 32
684 | (if result.learning_metrics.convergence_rate < 0.001 {
685 1
686 } else {
687 0
688 }),
689 parity: 0,
690 };
691 quins.push(learning_quin);
692
693 let sync_quin = NQuin {
695 subject: crate::q_hash(job_id),
696 predicate: crate::q_hash("q42:hasCrdtSync"),
697 object: result.crdt_sync_metrics.sync_rounds as u64,
698 context: crate::q_hash("snn:crdt"),
699 metadata: ((result.crdt_sync_metrics.conflicts_resolved as u64) << 32)
700 | (if result.crdt_sync_metrics.convergence_achieved {
701 1
702 } else {
703 0
704 }),
705 parity: 0,
706 };
707 quins.push(sync_quin);
708
709 let time_quin = NQuin {
711 subject: crate::q_hash(job_id),
712 predicate: crate::q_hash("q42:hasExecutionTime"),
713 object: result.execution_time_ms,
714 context: crate::q_hash("snn:performance"),
715 metadata: 0,
716 parity: 0,
717 };
718 quins.push(time_quin);
719
720 quins
721 }
722}
723
724impl SnnNetworkManager {
725 pub fn get_network(&self, name: &str) -> Option<&SpikingNetwork> {
727 self.loaded_networks.get(name)
728 }
729
730 pub fn load_network(&mut self, network: SpikingNetwork) -> Result<(), ExtensionError> {
732 self.loaded_networks.insert(network.name.clone(), network);
733 Ok(())
734 }
735}
736
737impl NoisyGradientCrdt {
738 pub fn new(node_id: Uuid) -> Self {
740 Self {
741 node_id,
742 gradient_state: GradientCrdtState::new(),
743 noise_generator: NoiseGenerator::new(),
744 sync_config: CrdtSyncConfig::default(),
745 }
746 }
747
748 pub async fn synchronize_gradients(
750 &mut self,
751 result: &SnnExecutionResult,
752 ) -> Result<CrdtSyncMetrics, ExtensionError> {
753 let gradients = self.extract_gradients(&result.synaptic_weights)?;
755
756 let noisy_gradients = self.add_noise_to_gradients(&gradients)?;
758
759 let sync_result = self.perform_sync(&noisy_gradients).await?;
761
762 let conflicts_resolved = self.resolve_conflicts(&sync_result.conflicts)?;
764
765 Ok(CrdtSyncMetrics {
766 sync_rounds: sync_result.rounds,
767 conflicts_resolved,
768 convergence_achieved: sync_result.converged,
769 noise_effectiveness: sync_result.noise_effectiveness,
770 network_utilization: sync_result.utilization,
771 })
772 }
773
774 fn extract_gradients(
775 &self,
776 weights: &[Vec<f64>],
777 ) -> Result<HashMap<String, CrdtGradient>, ExtensionError> {
778 let mut gradients = HashMap::new();
779
780 for (i, weight_vector) in weights.iter().enumerate() {
781 for (j, &weight) in weight_vector.iter().enumerate() {
782 let gradient_id = format!("weight_{}_{}", i, j);
783 let gradient = CrdtGradient {
784 gradient_value: weight,
785 noisy_value: weight,
786 timestamp: Instant::now(),
787 source_node: self.node_id,
788 confidence: 1.0,
789 };
790 gradients.insert(gradient_id, gradient);
791 }
792 }
793
794 Ok(gradients)
795 }
796
797 fn add_noise_to_gradients(
798 &mut self,
799 gradients: &HashMap<String, CrdtGradient>,
800 ) -> Result<HashMap<String, CrdtGradient>, ExtensionError> {
801 let mut noisy_gradients = HashMap::new();
802
803 for (id, gradient) in gradients {
804 let noise = self.noise_generator.generate_noise();
805 let noisy_gradient = CrdtGradient {
806 gradient_value: gradient.gradient_value,
807 noisy_value: gradient.gradient_value + noise,
808 timestamp: Instant::now(),
809 source_node: self.node_id,
810 confidence: gradient.confidence * 0.9, };
812 noisy_gradients.insert(id.clone(), noisy_gradient);
813 }
814
815 Ok(noisy_gradients)
816 }
817
818 async fn perform_sync(
819 &mut self,
820 gradients: &HashMap<String, CrdtGradient>,
821 ) -> Result<SyncResult, ExtensionError> {
822 const VALUE_CONFLICT_EPS: f64 = 1e-6;
829 *self
830 .gradient_state
831 .version_vector
832 .entry(self.node_id)
833 .or_insert(0) += 1;
834
835 let mut conflicts = Vec::new();
836 let mut confidence_sum = 0.0;
837 for (id, incoming) in gradients {
838 confidence_sum += incoming.confidence;
839 match self.gradient_state.gradients.get(id).cloned() {
840 Some(existing)
841 if existing.source_node != incoming.source_node
842 && (existing.gradient_value - incoming.gradient_value).abs()
843 > VALUE_CONFLICT_EPS =>
844 {
845 conflicts.push(GradientConflict {
846 conflicting_gradients: vec![existing.clone(), incoming.clone()],
847 conflict_type: ConflictType::ValueConflict,
848 resolution_time: Instant::now(),
849 });
850 if incoming.confidence >= existing.confidence {
852 self.gradient_state
853 .gradients
854 .insert(id.clone(), incoming.clone());
855 }
856 }
857 _ => {
858 self.gradient_state
859 .gradients
860 .insert(id.clone(), incoming.clone());
861 }
862 }
863 }
864
865 let n = gradients.len().max(1) as f64;
866 Ok(SyncResult {
867 rounds: 1,
868 converged: conflicts.is_empty(),
869 conflicts,
870 noise_effectiveness: confidence_sum / n,
871 utilization: (self.gradient_state.gradients.len() as f64 / n).min(1.0),
872 })
873 }
874
875 fn resolve_conflicts(&mut self, conflicts: &[GradientConflict]) -> Result<u32, ExtensionError> {
876 let mut resolved = 0;
877
878 for conflict in conflicts {
879 let resolution = self.resolve_conflict_with_noise(conflict)?;
881 if resolution {
882 resolved += 1;
883 }
884 }
885
886 Ok(resolved)
887 }
888
889 fn resolve_conflict_with_noise(
890 &mut self,
891 conflict: &GradientConflict,
892 ) -> Result<bool, ExtensionError> {
893 let tie_break = self.noise_generator.generate_noise();
898 let winner = conflict.conflicting_gradients.iter().max_by(|a, b| {
899 a.confidence
900 .partial_cmp(&b.confidence)
901 .unwrap_or(std::cmp::Ordering::Equal)
902 .then(
903 (a.noisy_value + tie_break)
904 .partial_cmp(&b.noisy_value)
905 .unwrap_or(std::cmp::Ordering::Equal),
906 )
907 });
908 Ok(winner.is_some())
909 }
910}
911
912impl NoiseGenerator {
913 pub fn new() -> Self {
915 Self {
916 noise_type: NoiseType::Gaussian,
917 amplitude: 0.1,
918 correlation_time: Duration::from_millis(100),
919 seed: 42,
920 }
921 }
922
923 pub fn generate_noise(&mut self) -> f64 {
925 use rand::RngExt;
926 let mut rng = rand::rng();
927
928 match self.noise_type {
929 NoiseType::Gaussian => {
930 let u1: f64 = rng.random();
932 let u2: f64 = rng.random();
933 let noise = (-2.0 * u1.ln()).sqrt() * (2.0 * std::f64::consts::PI * u2).cos();
934 noise * self.amplitude
935 }
936 NoiseType::Uniform => rng.random_range(-self.amplitude..self.amplitude),
937 _ => 0.0, }
939 }
940}
941
942impl TemporalConfig {
943 pub fn create_processor(&self) -> TemporalProcessor {
945 TemporalProcessor {
946 spike_queue: VecDeque::new(),
947 current_time: Instant::now(),
948 time_step: self.time_step,
949 event_handlers: vec![SpikeEventHandler {
950 handler_type: HandlerType::Plasticity,
951 priority: 1,
952 enabled: true,
953 }],
954 }
955 }
956}
957
958impl TemporalProcessor {
959 pub fn new() -> Self {
961 Self {
962 spike_queue: VecDeque::new(),
963 current_time: Instant::now(),
964 time_step: Duration::from_millis(1),
965 event_handlers: vec![],
966 }
967 }
968
969 pub fn process_input_spike(&mut self, neuron_id: u32, spike_time: Duration) {
971 let spike_event = SpikeEvent {
972 neuron_id,
973 spike_time: self.current_time + spike_time,
974 spike_type: SpikeType::Excitatory,
975 propagation_delay: Duration::from_millis(1),
976 };
977 self.spike_queue.push_back(spike_event);
978 }
979
980 pub fn emit_spike(&mut self, neuron_id: u32, current_time: Duration) {
982 let spike_event = SpikeEvent {
983 neuron_id,
984 spike_time: self.current_time + current_time,
985 spike_type: SpikeType::Excitatory,
986 propagation_delay: Duration::from_millis(1),
987 };
988 self.spike_queue.push_back(spike_event);
989 }
990
991 }
995
996impl Default for CrdtSyncMetrics {
998 fn default() -> Self {
999 Self {
1000 sync_rounds: 0,
1001 conflicts_resolved: 0,
1002 convergence_achieved: false,
1003 noise_effectiveness: 0.0,
1004 network_utilization: 0.0,
1005 }
1006 }
1007}
1008
1009impl Default for GradientCrdtState {
1010 fn default() -> Self {
1011 Self {
1012 gradients: HashMap::new(),
1013 version_vector: HashMap::new(),
1014 pending_updates: vec![],
1015 conflict_buffer: ConflictBuffer::default(),
1016 }
1017 }
1018}
1019
1020impl GradientCrdtState {
1021 pub fn new() -> Self {
1023 Self::default()
1024 }
1025}
1026
1027impl Default for ConflictBuffer {
1028 fn default() -> Self {
1029 Self {
1030 conflicts: vec![],
1031 resolution_strategy: ConflictResolution::NoisyGradient,
1032 max_buffer_size: 1000,
1033 }
1034 }
1035}
1036
1037impl Default for CrdtSyncConfig {
1038 fn default() -> Self {
1039 Self {
1040 sync_protocol: SyncProtocol::Gossip,
1041 compression_enabled: true,
1042 encryption_enabled: false,
1043 batch_size: 100,
1044 timeout: Duration::from_secs(30),
1045 }
1046 }
1047}
1048
1049#[derive(Debug, Clone)]
1051struct SyncResult {
1052 rounds: u32,
1053 converged: bool,
1054 conflicts: Vec<GradientConflict>,
1055 noise_effectiveness: f64,
1056 utilization: f64,
1057}
1058
1059#[async_trait]
1060impl Extension for SnnExtension {
1061 fn capability(&self) -> ExtensionCapability {
1062 self.capability.clone()
1063 }
1064
1065 fn shutdown(&self) -> Result<(), ExtensionError> {
1066 Ok(())
1067 }
1068
1069 async fn execute(&self, job: ExtensionJob) -> Result<ExtensionResult, ExtensionError> {
1070 let start_time = Instant::now();
1071
1072 match job.operation.as_str() {
1073 "simulate_snn" => {
1074 let params: SnnJobParams = serde_json::from_value(
1075 job.parameters
1076 .get("snn_params")
1077 .ok_or_else(|| {
1078 ExtensionError::ExecutionFailed("Missing snn_params".to_string())
1079 })?
1080 .clone(),
1081 )
1082 .map_err(|e| {
1083 ExtensionError::ExecutionFailed(format!("Invalid snn_params: {}", e))
1084 })?;
1085
1086 let result = self.simulate_snn(params).await?;
1087 let quins = Self::result_to_quins(&result, &job.job_id);
1088
1089 Ok(ExtensionResult {
1090 job_id: job.job_id,
1091 success: true,
1092 result_quins: quins,
1093 metadata: {
1094 let mut meta = HashMap::new();
1095 meta.insert(
1096 "converged".to_string(),
1097 result.learning_metrics.convergence_rate.to_string(),
1098 );
1099 meta.insert(
1100 "final_loss".to_string(),
1101 result.learning_metrics.final_loss.to_string(),
1102 );
1103 meta.insert(
1104 "spike_rate".to_string(),
1105 result.learning_metrics.spike_rate.to_string(),
1106 );
1107 meta.insert(
1108 "sync_rounds".to_string(),
1109 result.crdt_sync_metrics.sync_rounds.to_string(),
1110 );
1111 meta.insert(
1112 "conflicts_resolved".to_string(),
1113 result.crdt_sync_metrics.conflicts_resolved.to_string(),
1114 );
1115 meta
1116 },
1117 execution_time_ms: start_time.elapsed().as_millis() as u64,
1118 })
1119 }
1120 _ => Err(ExtensionError::OperationNotSupported(job.operation)),
1121 }
1122 }
1123}
1124
1125use std::collections::VecDeque;
1127
1128#[cfg(test)]
1129mod tests {
1130 use super::*;
1131 use crate::Extension;
1132
1133 #[tokio::test]
1134 async fn test_snn_extension_creation() {
1135 let extension = SnnExtension::new();
1136 let capability = extension.capability();
1137
1138 assert_eq!(capability.name, "snn");
1139 assert_eq!(capability.version, "1.0.0");
1140 assert!(capability
1141 .supported_operations
1142 .contains(&"simulate_snn".to_string()));
1143 assert!(capability.required_resources.requires_network); }
1145
1146 #[tokio::test]
1147 async fn test_snn_simulation() {
1148 let extension = SnnExtension::new();
1149
1150 let params = SnnJobParams {
1151 network_name: "test_network".to_string(),
1152 input_spikes: vec![],
1153 simulation_time: Duration::from_millis(100),
1154 learning_enabled: true,
1155 crdt_sync_enabled: true,
1156 noise_level: 0.1,
1157 };
1158
1159 let result = extension.simulate_snn(params).await;
1161 assert!(result.is_err());
1162 }
1163
1164 #[test]
1165 fn test_noise_generator() {
1166 let mut noise_gen = NoiseGenerator::new();
1167 let noise1 = noise_gen.generate_noise();
1168 let noise2 = noise_gen.generate_noise();
1169
1170 assert_ne!(noise1, noise2);
1172
1173 assert!(noise1.is_finite() && noise2.is_finite());
1177 assert!(noise1.abs() < 5.0 && noise2.abs() < 5.0);
1178 }
1179
1180 #[test]
1181 fn test_crdt_gradient() {
1182 let node_id = Uuid::new_v4();
1183 let gradient = CrdtGradient {
1184 gradient_value: 0.5,
1185 noisy_value: 0.52,
1186 timestamp: Instant::now(),
1187 source_node: node_id,
1188 confidence: 0.9,
1189 };
1190
1191 assert_eq!(gradient.source_node, node_id);
1192 assert!(gradient.confidence < 1.0);
1193 assert!(gradient.noisy_value != gradient.gradient_value);
1194 }
1195
1196 #[test]
1197 fn leak_and_grouping_helpers() {
1198 let lf = leak_factor(Duration::from_millis(1));
1200 assert!((lf - (-0.05f64).exp()).abs() < 1e-9);
1201 let trains = group_into_spike_trains(&[
1203 (1, Duration::from_millis(2)),
1204 (1, Duration::from_millis(5)),
1205 (2, Duration::from_millis(3)),
1206 ]);
1207 assert_eq!(trains.len(), 2);
1208 let n1 = trains.iter().find(|t| t.neuron_id == 1).unwrap();
1209 assert_eq!(n1.spike_times.len(), 2);
1210 assert_eq!(n1.spike_amplitudes.len(), 2);
1211 }
1212
1213 #[test]
1214 fn real_lif_fires_on_synaptic_drive_and_stdp_potentiates() {
1215 use std::collections::HashSet;
1216 let mk_neuron = |id: u32, threshold: f64| SpikingNeuron {
1217 id,
1218 neuron_type: NeuronType::Excitatory,
1219 membrane_potential: 0.0,
1220 threshold,
1221 refractory_period: Duration::from_millis(2),
1222 last_spike_time: None,
1223 temporal_state: TemporalState {
1224 adaptation_current: 0.0,
1225 recovery_variable: 0.0,
1226 synaptic_current: 0.0,
1227 noise_amplitude: 0.0, },
1229 };
1230 let synapse = Synapse {
1233 pre_neuron_id: 1,
1234 post_neuron_id: 2,
1235 weight: 0.5,
1236 delay: Duration::from_millis(1),
1237 plasticity_type: PlasticityType::STDP,
1238 crdt_weight: CrdtWeight {
1239 value: 0.5,
1240 version_vector: HashMap::new(),
1241 last_update: Instant::now(),
1242 conflict_resolution: ConflictResolution::LastWriterWins,
1243 },
1244 };
1245 let mut net = SpikingNetwork {
1246 name: "t".to_string(),
1247 network_type: NetworkType::LIF,
1248 neurons: vec![mk_neuron(1, 10.0), mk_neuron(2, 0.4)],
1249 synapses: vec![synapse],
1250 temporal_config: TemporalConfig {
1251 time_step: Duration::from_millis(1),
1252 simulation_window: Duration::from_millis(10),
1253 spike_encoding: SpikeEncoding::Temporal,
1254 temporal_resolution: 1000,
1255 },
1256 crdt_config: CrdtConfig {
1257 sync_interval: Duration::from_millis(100),
1258 noise_amplitude: 0.0,
1259 gradient_clipping: 1.0,
1260 consensus_threshold: 0.5,
1261 network_topology: NetworkTopology::FullyConnected,
1262 },
1263 };
1264 let ext = SnnExtension::new();
1265 let sign: HashMap<u32, f64> = [(1u32, 1.0), (2u32, 1.0)].into_iter().collect();
1266 let mut last: HashMap<u32, Duration> = HashMap::new();
1267 let w0 = net.synapses[0].weight;
1268 let prev: HashSet<u32> = HashSet::new();
1269 let external: HashSet<u32> = [1u32].into_iter().collect();
1270
1271 let fired = ext.step_neurons(
1274 &mut net,
1275 Duration::from_millis(1),
1276 Duration::from_millis(1),
1277 &prev,
1278 &external,
1279 &sign,
1280 &mut last,
1281 );
1282 assert!(
1283 fired.contains(&2),
1284 "neuron 2 should fire from the real synaptic drive"
1285 );
1286 assert!(
1288 net.synapses[0].weight > w0,
1289 "STDP should potentiate the 1→2 synapse"
1290 );
1291 }
1292}