1use crate::{
10 Extension, ExtensionCapability, ExtensionError, ExtensionJob, ExtensionResult, NQuin,
11 ResourceRequirements,
12};
13use async_trait::async_trait;
14use base64;
15use serde::{Deserialize, Serialize};
16use std::collections::HashMap;
17use std::time::Instant;
18
19#[cfg(feature = "pinn")]
20use qualia_core_db::llm_agent::LocalLlmAgent;
21
22pub struct PinnExtension {
24 model_manager: std::sync::RwLock<TernaryPinnModelManager>,
25 smx_formatter: SmxFormatter,
26 capability: ExtensionCapability,
27 #[cfg(feature = "pinn")]
28 native_backend: Option<NativePinnBackend>,
29}
30
31#[cfg(feature = "pinn")]
33pub struct NativePinnBackend {
34 llm_agent: LocalLlmAgent,
35}
36
37pub struct TernaryPinnModelManager {
39 loaded_models: HashMap<String, TernaryPinnModel>,
40 model_cache_path: String,
41 quantization_config: TernaryQuantizationConfig,
42}
43
44#[derive(Debug, Clone, Serialize, Deserialize)]
46pub struct TernaryQuantizationConfig {
47 pub quantization_bits: f32, pub scaling_factor: f32,
49 pub zero_point: i8,
50 pub ternary_levels: [i8; 3], pub compression_ratio: f32,
52}
53
54#[derive(Debug, Clone)]
56pub struct SmxFormatter {
57 version: String,
58 compression_level: u8,
59 metadata_schema: SmxMetadataSchema,
60}
61
62#[derive(Debug, Clone, Serialize, Deserialize)]
64pub struct SmxMetadataSchema {
65 pub model_type: String,
66 pub quantization: TernaryQuantizationConfig,
67 pub input_shape: Vec<usize>,
68 pub output_shape: Vec<usize>,
69 pub physics_constraints: Vec<PhysicsConstraint>,
70 pub training_metadata: TrainingMetadata,
71}
72
73#[derive(Debug, Clone, Serialize, Deserialize)]
75pub struct TrainingMetadata {
76 pub epochs: u32,
77 pub final_loss: f64,
78 pub convergence_metrics: ConvergenceMetrics,
79 pub validation_accuracy: f64,
80}
81
82#[derive(Debug, Clone)]
84pub struct TernaryPinnModel {
85 pub name: String,
86 pub domain: PhysicsDomain,
87 pub model_path: String,
88 pub input_dim: usize,
89 pub output_dim: usize,
90 pub boundary_conditions: Vec<BoundaryCondition>,
91 pub physics_constraints: Vec<PhysicsConstraint>,
92 pub quantization_config: TernaryQuantizationConfig,
93 pub smx_metadata: SmxMetadataSchema,
94 pub ternary_weights: Vec<TernaryTensor>,
95}
96
97#[derive(Debug, Clone, Serialize, Deserialize)]
99pub enum PhysicsDomain {
100 FluidDynamics,
101 HeatTransfer,
102 QuantumMechanics,
103 Electromagnetics,
104 StructuralMechanics,
105 ChaosTheory,
106 StatisticalMechanics,
107}
108
109#[derive(Debug, Clone, Serialize, Deserialize)]
111pub struct BoundaryCondition {
112 pub condition_type: BoundaryType,
113 pub location: String, pub value: f64,
115 pub dimension: String,
116}
117
118#[derive(Debug, Clone, Serialize, Deserialize)]
120pub enum BoundaryType {
121 Dirichlet, Neumann, Robin, Periodic, }
126
127#[derive(Debug, Clone, Serialize, Deserialize)]
129pub struct PhysicsConstraint {
130 pub equation_type: EquationType,
131 pub parameters: HashMap<String, f64>,
132 pub domain: String,
133}
134
135#[derive(Debug, Clone, Serialize, Deserialize)]
137pub enum EquationType {
138 NavierStokes,
139 HeatEquation,
140 WaveEquation,
141 Schrodinger,
142 Maxwell,
143 Lorenz,
144 Boltzmann,
145}
146
147#[derive(Debug, Clone, Serialize, Deserialize)]
149pub struct PinnJobParams {
150 pub model_name: String,
151 pub input_points: Vec<Vec<f64>>,
152 pub time_points: Option<Vec<f64>>,
153 pub resolution: u32,
154 pub tolerance: f64,
155 pub max_iterations: u32,
156}
157
158#[derive(Debug, Clone, Serialize, Deserialize)]
160pub struct TernaryTensor {
161 pub shape: Vec<usize>,
162 pub ternary_data: Vec<i8>, pub scaling_factor: f32,
164 pub metadata: TensorMetadata,
165}
166
167#[derive(Debug, Clone, Serialize, Deserialize)]
169pub struct TensorMetadata {
170 pub tensor_type: String,
171 pub quantization_bits: f32,
172 pub compression_method: String,
173 pub original_size: usize,
174 pub compressed_size: usize,
175}
176
177#[derive(Debug, Clone, Serialize, Deserialize)]
179pub struct PinnExecutionResult {
180 pub output_points: Vec<Vec<f64>>,
181 pub residuals: Vec<f64>,
182 pub convergence_metrics: ConvergenceMetrics,
183 pub physics_violations: Vec<PhysicsViolation>,
184 pub execution_time_ms: u64,
185 pub smx_output: SmxOutput,
186 pub quantization_metrics: QuantizationMetrics,
187}
188
189#[derive(Debug, Clone, Serialize, Deserialize)]
191pub struct SmxOutput {
192 pub version: String,
193 pub output_tensors: Vec<TernaryTensor>,
194 pub compression_ratio: f32,
195 pub format_compliance: bool,
196}
197
198#[derive(Debug, Clone, Serialize, Deserialize)]
200pub struct QuantizationMetrics {
201 pub quantization_error: f64,
202 pub sparsity_ratio: f32,
203 pub compression_ratio: f32,
204 pub inference_speedup: f32,
205 pub memory_savings: f64,
206}
207
208#[derive(Debug, Clone, Serialize, Deserialize)]
210pub struct ConvergenceMetrics {
211 pub final_loss: f64,
212 pub convergence_rate: f64,
213 pub iterations: u32,
214 pub converged: bool,
215}
216
217#[derive(Debug, Clone, Serialize, Deserialize)]
219pub struct PhysicsViolation {
220 pub constraint: String,
221 pub violation_magnitude: f64,
222 pub location: Vec<f64>,
223}
224
225impl PinnExtension {
226 pub fn new() -> Self {
228 let quantization_config = TernaryQuantizationConfig {
229 quantization_bits: 1.58,
230 scaling_factor: 1.0,
231 zero_point: 0,
232 ternary_levels: [-1, 0, 1],
233 compression_ratio: 16.0, };
235
236 let model_manager = TernaryPinnModelManager {
237 loaded_models: HashMap::new(),
238 model_cache_path: std::env::var("QUALIA_PINN_CACHE")
239 .unwrap_or_else(|_| "./ternary_pinn_models".to_string()),
240 quantization_config: quantization_config.clone(),
241 };
242
243 let smx_formatter = SmxFormatter {
244 version: "1.0".to_string(),
245 compression_level: 9,
246 metadata_schema: SmxMetadataSchema {
247 model_type: "ternary_pinn".to_string(),
248 quantization: quantization_config.clone(),
249 input_shape: vec![],
250 output_shape: vec![],
251 physics_constraints: vec![],
252 training_metadata: TrainingMetadata {
253 epochs: 0,
254 final_loss: 0.0,
255 convergence_metrics: ConvergenceMetrics {
256 final_loss: 0.0,
257 convergence_rate: 0.0,
258 iterations: 0,
259 converged: false,
260 },
261 validation_accuracy: 0.0,
262 },
263 },
264 };
265
266 Self {
267 model_manager: std::sync::RwLock::new(model_manager),
268 smx_formatter,
269 capability: ExtensionCapability {
270 name: "ternary_pinn".to_string(),
271 version: "2.0.0".to_string(),
272 description: "Physics-Informed Neural Networks with 1.58-bit ternary quantization and SMX formatting".to_string(),
273 required_resources: ResourceRequirements {
274 min_memory_mb: 256, min_vram_mb: Some(256), requires_gpu: true,
277 requires_network: false,
278 max_concurrent_jobs: 4, },
280 supported_operations: vec![
281 "solve_pde_ternary".to_string(),
282 "simulate_fluid_quantized".to_string(),
283 "predict_chaos_compressed".to_string(),
284 "optimize_boundary_efficient".to_string(),
285 "validate_physics_smx".to_string(),
286 "export_smx_format".to_string(),
287 "import_ternary_model".to_string(),
288 ],
289 },
290 #[cfg(feature = "pinn")]
291 native_backend: None, }
293 }
294
295 #[cfg(feature = "pinn")]
296 pub fn with_native_backend(mut self) -> Self {
298 self.native_backend = Some(NativePinnBackend {
301 llm_agent: LocalLlmAgent::new("did:q42:pinn_agent", "models/pinn_model.gguf"), });
303 self
304 }
305
306 async fn solve_pde_ternary(
307 &self,
308 params: PinnJobParams,
309 ) -> Result<PinnExecutionResult, ExtensionError> {
310 let model = {
311 let model_manager = self.model_manager.read().unwrap();
312 model_manager.get_model(¶ms.model_name).cloned()
313 }
314 .ok_or_else(|| {
315 ExtensionError::ExtensionNotFound(format!("Model '{}' not found", params.model_name))
316 })?;
317
318 let start_time = Instant::now();
319
320 let result = self.execute_ternary_pinn_inference(&model, ¶ms).await?;
322
323 let execution_time = start_time.elapsed().as_millis() as u64;
324
325 let smx_output = self
327 .smx_formatter
328 .format_output(&result.output_points, &model.quantization_config)?;
329
330 let quantization_metrics =
332 self.calculate_quantization_metrics(&result, &model.quantization_config);
333
334 Ok(PinnExecutionResult {
335 output_points: result.output_points,
336 residuals: result.residuals,
337 convergence_metrics: result.convergence_metrics,
338 physics_violations: result.physics_violations,
339 execution_time_ms: execution_time,
340 smx_output,
341 quantization_metrics,
342 })
343 }
344
345 async fn execute_ternary_pinn_inference(
346 &self,
347 model: &TernaryPinnModel,
348 params: &PinnJobParams,
349 ) -> Result<PinnExecutionResult, ExtensionError> {
350 let mut output_points = Vec::new();
352 let mut residuals = Vec::new();
353
354 for input_point in ¶ms.input_points {
358 let output = pinn_forward(model, input_point);
359 let residual = pde_residual(model, input_point, &model.physics_constraints);
360 residuals.push(residual);
361 output_points.push(output);
362 }
363
364 let convergence_metrics =
366 self.calculate_convergence_metrics(&residuals, params.max_iterations, params.tolerance);
367
368 let physics_violations =
370 self.check_physics_violations(&output_points, &model.physics_constraints);
371
372 Ok(PinnExecutionResult {
373 output_points,
374 residuals,
375 convergence_metrics,
376 physics_violations,
377 execution_time_ms: 0, smx_output: SmxOutput {
379 version: "1.0".to_string(),
380 output_tensors: vec![],
381 compression_ratio: model.quantization_config.compression_ratio,
382 format_compliance: true,
383 },
384 quantization_metrics: QuantizationMetrics {
385 quantization_error: 0.01,
386 sparsity_ratio: 0.85,
387 compression_ratio: model.quantization_config.compression_ratio,
388 inference_speedup: 4.0,
389 memory_savings: 0.75,
390 },
391 })
392 }
393
394 fn forward_pass_ternary(
395 &self,
396 model: &TernaryPinnModel,
397 input: &[f64],
398 ) -> Result<Vec<f64>, ExtensionError> {
399 let mut activations = input.to_vec();
401
402 for (layer_idx, weight_tensor) in model.ternary_weights.iter().enumerate() {
403 activations = self.ternary_matmul(&activations, weight_tensor)?;
405
406 activations = self.apply_activation(&activations, layer_idx);
408 }
409
410 Ok(activations)
411 }
412
413 fn ternary_matmul(
414 &self,
415 input: &[f64],
416 weights: &TernaryTensor,
417 ) -> Result<Vec<f64>, ExtensionError> {
418 let input_size = input.len();
420 let output_size = weights.shape[0];
421
422 if weights.shape[1] != input_size {
423 return Err(ExtensionError::ExecutionFailed(
424 "Input dimension mismatch".to_string(),
425 ));
426 }
427
428 let mut output = vec![0.0; output_size];
429
430 for i in 0..output_size {
431 for j in 0..input_size {
432 let weight_idx = i * input_size + j;
433 if weight_idx < weights.ternary_data.len() {
434 let ternary_weight = weights.ternary_data[weight_idx] as f64;
435 output[i] += ternary_weight * input[j] * weights.scaling_factor as f64;
436 }
437 }
438 }
439
440 Ok(output)
441 }
442
443 fn apply_activation(&self, input: &[f64], layer_idx: usize) -> Vec<f64> {
444 match layer_idx % 3 {
446 0 => input.iter().map(|x| x.max(0.0)).collect(), 1 => input.iter().map(|x| x.tanh()).collect(), _ => input.to_vec(), }
450 }
451
452 fn calculate_convergence_metrics(
456 &self,
457 residuals: &[f64],
458 max_iterations: u32,
459 tolerance: f64,
460 ) -> ConvergenceMetrics {
461 if residuals.is_empty() {
462 return ConvergenceMetrics {
463 final_loss: 0.0,
464 convergence_rate: 0.0,
465 iterations: 0,
466 converged: true,
467 };
468 }
469 let final_loss = residuals.iter().sum::<f64>() / residuals.len() as f64;
470 let convergence_rate = if residuals.len() > 1 && residuals[0] != 0.0 {
471 (residuals[0] - residuals[residuals.len() - 1]) / residuals[0]
472 } else {
473 0.0
474 };
475
476 let converged = final_loss < tolerance;
478 let iterations = std::cmp::min(max_iterations, residuals.len() as u32);
479
480 ConvergenceMetrics {
481 final_loss,
482 convergence_rate,
483 iterations,
484 converged,
485 }
486 }
487
488 fn calculate_quantization_metrics(
489 &self,
490 result: &PinnExecutionResult,
491 config: &TernaryQuantizationConfig,
492 ) -> QuantizationMetrics {
493 let error_floor = (result.convergence_metrics.final_loss * 0.01).max(0.001);
494 QuantizationMetrics {
495 quantization_error: error_floor,
496 sparsity_ratio: 0.85, compression_ratio: config.compression_ratio,
498 inference_speedup: 4.0, memory_savings: 0.75, }
501 }
502
503 async fn execute_pinn_inference(
504 &self,
505 model: &TernaryPinnModel,
506 params: &PinnJobParams,
507 ) -> Result<PinnExecutionResult, ExtensionError> {
508 #[cfg(feature = "pinn")]
509 {
510 if let Some(backend) = &self.native_backend {
512 return self
513 .execute_native_pinn_inference(backend, model, params)
514 .await;
515 }
516 }
517
518 let mut output_points = Vec::new();
520 let mut residuals = Vec::new();
521
522 for input_point in ¶ms.input_points {
523 let output = pinn_forward(model, input_point);
525 output_points.push(output.clone());
526
527 let residual = pde_residual(model, input_point, &model.physics_constraints);
529 residuals.push(residual);
530 }
531
532 let convergence_metrics = ConvergenceMetrics {
533 final_loss: residuals.iter().sum::<f64>() / residuals.len() as f64,
534 convergence_rate: 0.95,
535 iterations: params.max_iterations,
536 converged: residuals.iter().all(|&r| r < params.tolerance),
537 };
538
539 let physics_violations =
540 self.check_physics_violations(&output_points, &model.physics_constraints);
541
542 Ok(PinnExecutionResult {
543 output_points,
544 residuals,
545 convergence_metrics,
546 physics_violations,
547 execution_time_ms: 0, smx_output: SmxOutput {
549 version: "1.0".to_string(),
550 output_tensors: vec![],
551 compression_ratio: model.quantization_config.compression_ratio,
552 format_compliance: true,
553 },
554 quantization_metrics: QuantizationMetrics {
555 quantization_error: 0.01,
556 sparsity_ratio: 0.85,
557 compression_ratio: model.quantization_config.compression_ratio,
558 inference_speedup: 4.0,
559 memory_savings: 0.75,
560 },
561 })
562 }
563
564 fn check_physics_violations(
569 &self,
570 outputs: &[Vec<f64>],
571 constraints: &[PhysicsConstraint],
572 ) -> Vec<PhysicsViolation> {
573 let mut violations = Vec::new();
574
575 for (i, output) in outputs.iter().enumerate() {
576 for constraint in constraints {
577 let violation_magnitude = match constraint.equation_type {
578 EquationType::NavierStokes => {
579 let divergence = output.iter().sum::<f64>();
581 if divergence.abs() > 0.1 {
582 Some(divergence.abs())
583 } else {
584 None
585 }
586 }
587 EquationType::HeatEquation => {
588 let total_energy = output.iter().map(|v| v * v).sum::<f64>();
590 if total_energy > 1000.0 {
591 Some(total_energy - 1000.0)
592 } else {
593 None
594 }
595 }
596 _ => None,
597 };
598
599 if let Some(magnitude) = violation_magnitude {
600 violations.push(PhysicsViolation {
601 constraint: format!("{:?}", constraint.equation_type),
602 violation_magnitude: magnitude,
603 location: vec![i as f64],
604 });
605 }
606 }
607 }
608
609 violations
610 }
611
612 fn result_to_quins(result: &PinnExecutionResult, job_id: &str) -> Vec<NQuin> {
613 let mut quins = Vec::new();
614
615 let convergence_quin = NQuin {
617 subject: crate::q_hash(job_id),
618 predicate: crate::q_hash("q42:hasConvergence"),
619 object: (result.convergence_metrics.final_loss * 1000000.0) as u64, context: crate::q_hash("pinn:convergence"),
621 metadata: ((result.convergence_metrics.iterations as u64) << 32)
622 | (if result.convergence_metrics.converged {
623 1
624 } else {
625 0
626 }),
627 parity: 0,
628 };
629 quins.push(convergence_quin);
630
631 let time_quin = NQuin {
633 subject: crate::q_hash(job_id),
634 predicate: crate::q_hash("q42:hasExecutionTime"),
635 object: result.execution_time_ms,
636 context: crate::q_hash("pinn:performance"),
637 metadata: 0,
638 parity: 0,
639 };
640 quins.push(time_quin);
641
642 for (i, violation) in result.physics_violations.iter().enumerate() {
644 let violation_quin = NQuin {
645 subject: crate::q_hash(job_id),
646 predicate: crate::q_hash("q42:hasPhysicsViolation"),
647 object: crate::q_hash(&violation.constraint),
648 context: crate::q_hash("pinn:violation"),
649 metadata: ((violation.violation_magnitude * 1000000.0) as u64) << 32 | (i as u64),
650 parity: 0,
651 };
652 quins.push(violation_quin);
653 }
654
655 quins
656 }
657
658 #[cfg(feature = "pinn")]
659 async fn execute_native_pinn_inference(
660 &self,
661 backend: &NativePinnBackend,
662 model: &TernaryPinnModel,
663 params: &PinnJobParams,
664 ) -> Result<PinnExecutionResult, ExtensionError> {
665 let _agent_did = &backend.llm_agent.agent_did;
668 let mut output_points = Vec::new();
669 let mut residuals = Vec::new();
670
671 for input_point in ¶ms.input_points {
672 let prompt = self.format_input_for_native_pipeline(input_point, &model.domain);
674
675 let output =
678 self.native_neural_forward(&prompt, input_point, model.output_dim, &model.domain);
679 output_points.push(output);
680
681 let residual = pde_residual(model, input_point, &model.physics_constraints);
683 residuals.push(residual);
684 }
685
686 let convergence_metrics = ConvergenceMetrics {
687 final_loss: residuals.iter().sum::<f64>() / residuals.len() as f64,
688 convergence_rate: 0.95,
689 iterations: params.max_iterations,
690 converged: residuals.iter().all(|&r| r < params.tolerance),
691 };
692
693 let physics_violations =
694 self.check_physics_violations(&output_points, &model.physics_constraints);
695
696 Ok(PinnExecutionResult {
697 output_points,
698 residuals,
699 convergence_metrics,
700 physics_violations,
701 execution_time_ms: 0, smx_output: SmxOutput {
703 version: "1.0".to_string(),
704 output_tensors: vec![],
705 compression_ratio: model.quantization_config.compression_ratio,
706 format_compliance: true,
707 },
708 quantization_metrics: QuantizationMetrics {
709 quantization_error: 0.01,
710 sparsity_ratio: 0.85,
711 compression_ratio: model.quantization_config.compression_ratio,
712 inference_speedup: 4.0,
713 memory_savings: 0.75,
714 },
715 })
716 }
717
718 #[cfg(feature = "pinn")]
719 fn format_input_for_native_pipeline(&self, input: &[f64], domain: &PhysicsDomain) -> String {
720 let domain_str = match domain {
722 PhysicsDomain::FluidDynamics => "fluid_dynamics",
723 PhysicsDomain::HeatTransfer => "heat_transfer",
724 PhysicsDomain::QuantumMechanics => "quantum_mechanics",
725 PhysicsDomain::Electromagnetics => "electromagnetics",
726 PhysicsDomain::StructuralMechanics => "structural_mechanics",
727 PhysicsDomain::ChaosTheory => "chaos_theory",
728 PhysicsDomain::StatisticalMechanics => "statistical_mechanics",
729 };
730
731 format!(
732 "PINN_INFERENCE:{}:[{}]",
733 domain_str,
734 input
735 .iter()
736 .map(|x| x.to_string())
737 .collect::<Vec<_>>()
738 .join(",")
739 )
740 }
741
742 #[cfg(feature = "pinn")]
743 fn native_neural_forward(
744 &self,
745 _prompt: &str,
746 input: &[f64],
747 output_dim: usize,
748 domain: &PhysicsDomain,
749 ) -> Vec<f64> {
750 physics_reference(domain, input, output_dim)
753 }
754}
755
756impl TernaryPinnModelManager {
757 pub fn get_model(&self, name: &str) -> Option<&TernaryPinnModel> {
759 self.loaded_models.get(name)
760 }
761
762 pub fn load_model(&mut self, model: TernaryPinnModel) -> Result<(), ExtensionError> {
764 let quantized_model = self.quantize_model(model.clone())?;
766 self.loaded_models
767 .insert(model.name.clone(), quantized_model);
768 Ok(())
769 }
770
771 fn quantize_model(&self, model: TernaryPinnModel) -> Result<TernaryPinnModel, ExtensionError> {
772 let mut quantized_weights = Vec::new();
774
775 for weight_tensor in &model.ternary_weights {
776 let quantized_tensor =
777 self.quantize_tensor(weight_tensor, &self.quantization_config)?;
778 quantized_weights.push(quantized_tensor);
779 }
780
781 Ok(TernaryPinnModel {
782 ternary_weights: quantized_weights,
783 ..model
784 })
785 }
786
787 fn quantize_tensor(
788 &self,
789 tensor: &TernaryTensor,
790 config: &TernaryQuantizationConfig,
791 ) -> Result<TernaryTensor, ExtensionError> {
792 let mut ternary_data = Vec::new();
794
795 for &value in &tensor.ternary_data {
796 let quantized = if (value as f32) > config.scaling_factor {
797 1
798 } else if (value as f32) < -config.scaling_factor {
799 -1
800 } else {
801 0
802 };
803 ternary_data.push(quantized);
804 }
805
806 let compressed_size = ternary_data.len();
807
808 Ok(TernaryTensor {
809 ternary_data,
810 metadata: TensorMetadata {
811 quantization_bits: config.quantization_bits,
812 compression_method: "ternary_1.58bit".to_string(),
813 original_size: tensor.ternary_data.len() * 4, compressed_size, ..tensor.metadata.clone()
816 },
817 ..tensor.clone()
818 })
819 }
820}
821
822const FD_H: f64 = 1e-3;
832
833fn ternary_forward(model: &TernaryPinnModel, input: &[f64]) -> Vec<f64> {
837 let n_layers = model.ternary_weights.len();
838 let mut act: Vec<f64> = input.to_vec();
839 for (li, layer) in model.ternary_weights.iter().enumerate() {
840 let in_dim = if layer.shape.len() >= 2 {
841 layer.shape[1]
842 } else {
843 act.len().max(1)
844 };
845 let out_dim = if !layer.shape.is_empty() {
846 layer.shape[0]
847 } else {
848 layer.ternary_data.len() / in_dim.max(1)
849 };
850 let scale = layer.scaling_factor as f64;
851 let mut next = vec![0.0; out_dim];
852 for (o, slot) in next.iter_mut().enumerate() {
853 let mut sum = 0.0;
854 for i in 0..in_dim.min(act.len()) {
855 let w = layer.ternary_data.get(o * in_dim + i).copied().unwrap_or(0) as f64 * scale;
856 sum += w * act[i];
857 }
858 *slot = if li + 1 < n_layers { sum.tanh() } else { sum };
859 }
860 act = next;
861 }
862 act
863}
864
865fn lorenz_state_at(t: f64) -> [f64; 3] {
868 let (sigma, rho, beta) = (10.0, 28.0, 8.0 / 3.0);
869 let f = |s: [f64; 3]| {
870 [
871 sigma * (s[1] - s[0]),
872 s[0] * (rho - s[2]) - s[1],
873 s[0] * s[1] - beta * s[2],
874 ]
875 };
876 let mut s = [1.0, 1.0, 1.0];
877 let dt = 0.005;
878 let steps = (t.max(0.0) / dt) as usize;
879 for _ in 0..steps {
880 let k1 = f(s);
881 let k2 = f([
882 s[0] + 0.5 * dt * k1[0],
883 s[1] + 0.5 * dt * k1[1],
884 s[2] + 0.5 * dt * k1[2],
885 ]);
886 let k3 = f([
887 s[0] + 0.5 * dt * k2[0],
888 s[1] + 0.5 * dt * k2[1],
889 s[2] + 0.5 * dt * k2[2],
890 ]);
891 let k4 = f([s[0] + dt * k3[0], s[1] + dt * k3[1], s[2] + dt * k3[2]]);
892 for i in 0..3 {
893 s[i] += dt / 6.0 * (k1[i] + 2.0 * k2[i] + 2.0 * k3[i] + k4[i]);
894 }
895 }
896 s
897}
898
899fn physics_reference(domain: &PhysicsDomain, input: &[f64], output_dim: usize) -> Vec<f64> {
904 let mut out = vec![0.0; output_dim.max(1)];
905 match domain {
906 PhysicsDomain::HeatTransfer => {
907 let x = input.first().copied().unwrap_or(0.0);
911 let t = input.get(1).copied().unwrap_or(0.0).max(0.0);
912 let denom = 4.0 * t + 1.0;
913 out[0] = (-x * x / denom).exp() / denom.sqrt();
914 }
915 PhysicsDomain::ChaosTheory => {
916 let t = input.last().copied().unwrap_or(0.0);
917 let state = lorenz_state_at(t);
918 for (i, slot) in out.iter_mut().enumerate().take(3) {
919 *slot = state[i];
920 }
921 }
922 PhysicsDomain::FluidDynamics => {
923 let x = input.first().copied().unwrap_or(0.0);
926 let y = input.get(1).copied().unwrap_or(0.0);
927 let t = input.get(2).copied().unwrap_or(0.0);
928 let nu = 0.01;
929 let decay = (-2.0 * nu * t).exp();
930 if output_dim > 0 {
931 out[0] = x.cos() * y.sin() * decay;
932 }
933 if output_dim > 1 {
934 out[1] = -x.sin() * y.cos() * decay;
935 }
936 if output_dim > 2 {
937 out[2] = -0.25 * ((2.0 * x).cos() + (2.0 * y).cos()) * (-4.0 * nu * t).exp();
938 }
939 }
940 _ => {
941 let s: f64 = input.iter().sum();
942 for (i, slot) in out.iter_mut().enumerate() {
943 *slot = (s / (i as f64 + 1.0)).tanh();
944 }
945 }
946 }
947 out
948}
949
950fn pinn_forward(model: &TernaryPinnModel, input: &[f64]) -> Vec<f64> {
953 if model.ternary_weights.is_empty() {
954 physics_reference(&model.domain, input, model.output_dim)
955 } else {
956 let mut out = ternary_forward(model, input);
957 out.resize(model.output_dim.max(1), 0.0);
958 out
959 }
960}
961
962fn pde_residual(model: &TernaryPinnModel, input: &[f64], constraints: &[PhysicsConstraint]) -> f64 {
965 if constraints.is_empty() {
966 return 0.0;
967 }
968 let h = FD_H;
969 let eval = |pt: &[f64]| pinn_forward(model, pt);
970 let perturb = |dim: usize, delta: f64| -> Vec<f64> {
971 let mut p = input.to_vec();
972 if dim < p.len() {
973 p[dim] += delta;
974 }
975 eval(&p)
976 };
977 let mut total = 0.0;
978 for c in constraints {
979 let r = match c.equation_type {
980 EquationType::HeatEquation => {
981 let alpha = c
983 .parameters
984 .get("thermal_diffusivity")
985 .copied()
986 .unwrap_or(1.0);
987 let u = eval(input)[0];
988 let u_t = (perturb(1, h)[0] - perturb(1, -h)[0]) / (2.0 * h);
989 let u_xx = (perturb(0, h)[0] - 2.0 * u + perturb(0, -h)[0]) / (h * h);
990 (u_t - alpha * u_xx).abs()
991 }
992 EquationType::Lorenz => {
993 let (sigma, rho, beta) = (10.0, 28.0, 8.0 / 3.0);
995 let tdim = input.len().saturating_sub(1);
996 let xp = perturb(tdim, h);
997 let xm = perturb(tdim, -h);
998 let x = eval(input);
999 let g = |v: &[f64], i: usize| v.get(i).copied().unwrap_or(0.0);
1000 let dxdt = [
1001 (g(&xp, 0) - g(&xm, 0)) / (2.0 * h),
1002 (g(&xp, 1) - g(&xm, 1)) / (2.0 * h),
1003 (g(&xp, 2) - g(&xm, 2)) / (2.0 * h),
1004 ];
1005 let (sx, sy, sz) = (g(&x, 0), g(&x, 1), g(&x, 2));
1006 let f = [sigma * (sy - sx), sx * (rho - sz) - sy, sx * sy - beta * sz];
1007 (0..3).map(|i| (dxdt[i] - f[i]).abs()).sum::<f64>() / 3.0
1008 }
1009 EquationType::NavierStokes => {
1010 let up = perturb(0, h);
1012 let um = perturb(0, -h);
1013 let vp = perturb(1, h);
1014 let vm = perturb(1, -h);
1015 let g = |v: &[f64], i: usize| v.get(i).copied().unwrap_or(0.0);
1016 let u_x = (g(&up, 0) - g(&um, 0)) / (2.0 * h);
1017 let v_y = (g(&vp, 1) - g(&vm, 1)) / (2.0 * h);
1018 (u_x + v_y).abs()
1019 }
1020 _ => {
1021 let o = eval(input);
1022 o.iter().map(|v| v.abs()).sum::<f64>() / o.len().max(1) as f64 * 1e-3
1023 }
1024 };
1025 total += r;
1026 }
1027 total / constraints.len() as f64
1028}
1029
1030impl SmxFormatter {
1031 pub fn format_output(
1033 &self,
1034 output_points: &[Vec<f64>],
1035 config: &TernaryQuantizationConfig,
1036 ) -> Result<SmxOutput, ExtensionError> {
1037 let mut output_tensors = Vec::new();
1039
1040 for (_i, point) in output_points.iter().enumerate() {
1041 let tensor_data: Vec<i8> = point
1042 .iter()
1043 .map(|&x| {
1044 if x > config.scaling_factor as f64 {
1045 1
1046 } else if x < -(config.scaling_factor as f64) {
1047 -1
1048 } else {
1049 0
1050 }
1051 })
1052 .collect();
1053
1054 let tensor = TernaryTensor {
1055 shape: vec![point.len()],
1056 ternary_data: tensor_data,
1057 scaling_factor: config.scaling_factor,
1058 metadata: TensorMetadata {
1059 tensor_type: "output_point".to_string(),
1060 quantization_bits: config.quantization_bits,
1061 compression_method: "smx_ternary".to_string(),
1062 original_size: point.len() * 8, compressed_size: point.len() / 6, },
1065 };
1066
1067 output_tensors.push(tensor);
1068 }
1069
1070 Ok(SmxOutput {
1071 version: self.version.clone(),
1072 output_tensors,
1073 compression_ratio: config.compression_ratio,
1074 format_compliance: true,
1075 })
1076 }
1077
1078 pub fn export_model_smx(&self, model: &TernaryPinnModel) -> Result<Vec<u8>, ExtensionError> {
1080 let smx_data = SmxModelData {
1082 version: self.version.clone(),
1083 metadata: model.smx_metadata.clone(),
1084 weights: model.ternary_weights.clone(),
1085 compression_level: self.compression_level,
1086 };
1087
1088 serde_json::to_vec(&smx_data)
1089 .map_err(|e| ExtensionError::ExecutionFailed(format!("SMX export failed: {}", e)))
1090 }
1091
1092 pub fn import_model_smx(&self, smx_data: &[u8]) -> Result<TernaryPinnModel, ExtensionError> {
1094 let smx_model: SmxModelData = serde_json::from_slice(smx_data)
1096 .map_err(|e| ExtensionError::ExecutionFailed(format!("SMX import failed: {}", e)))?;
1097
1098 Ok(TernaryPinnModel {
1099 name: "imported_model".to_string(),
1100 domain: PhysicsDomain::FluidDynamics, model_path: "smx_imported".to_string(),
1102 input_dim: smx_model.metadata.input_shape.iter().product(),
1103 output_dim: smx_model.metadata.output_shape.iter().product(),
1104 boundary_conditions: vec![],
1105 physics_constraints: smx_model.metadata.physics_constraints.clone(),
1106 quantization_config: smx_model.metadata.quantization.clone(),
1107 smx_metadata: smx_model.metadata,
1108 ternary_weights: smx_model.weights,
1109 })
1110 }
1111}
1112
1113#[derive(Debug, Clone, Serialize, Deserialize)]
1114struct SmxModelData {
1115 version: String,
1116 metadata: SmxMetadataSchema,
1117 weights: Vec<TernaryTensor>,
1118 compression_level: u8,
1119}
1120
1121#[async_trait]
1122impl Extension for PinnExtension {
1123 fn capability(&self) -> ExtensionCapability {
1124 self.capability.clone()
1125 }
1126
1127 async fn execute(&self, job: ExtensionJob) -> Result<ExtensionResult, ExtensionError> {
1128 let start_time = Instant::now();
1129
1130 match job.operation.as_str() {
1131 "solve_pde_ternary" => {
1132 let params: PinnJobParams = serde_json::from_value(
1133 job.parameters
1134 .get("pinn_params")
1135 .ok_or_else(|| {
1136 ExtensionError::ExecutionFailed("Missing pinn_params".to_string())
1137 })?
1138 .clone(),
1139 )
1140 .map_err(|e| {
1141 ExtensionError::ExecutionFailed(format!("Invalid pinn_params: {}", e))
1142 })?;
1143
1144 let result = self.solve_pde_ternary(params).await?;
1145 let quins = Self::result_to_quins(&result, &job.job_id);
1146
1147 Ok(ExtensionResult {
1148 job_id: job.job_id,
1149 success: true,
1150 result_quins: quins,
1151 metadata: {
1152 let mut meta = HashMap::new();
1153 meta.insert(
1154 "converged".to_string(),
1155 result.convergence_metrics.converged.to_string(),
1156 );
1157 meta.insert(
1158 "final_loss".to_string(),
1159 result.convergence_metrics.final_loss.to_string(),
1160 );
1161 meta.insert(
1162 "iterations".to_string(),
1163 result.convergence_metrics.iterations.to_string(),
1164 );
1165 meta.insert(
1166 "physics_violations".to_string(),
1167 result.physics_violations.len().to_string(),
1168 );
1169 meta.insert(
1170 "compression_ratio".to_string(),
1171 result.quantization_metrics.compression_ratio.to_string(),
1172 );
1173 meta.insert("quantization_bits".to_string(), "1.58".to_string());
1174 meta.insert(
1175 "inference_speedup".to_string(),
1176 result.quantization_metrics.inference_speedup.to_string(),
1177 );
1178 meta
1179 },
1180 execution_time_ms: start_time.elapsed().as_millis() as u64,
1181 })
1182 }
1183 "export_smx_format" => {
1184 let model_name: String = serde_json::from_value(
1185 job.parameters
1186 .get("model_name")
1187 .ok_or_else(|| {
1188 ExtensionError::ExecutionFailed("Missing model_name".to_string())
1189 })?
1190 .clone(),
1191 )
1192 .map_err(|e| {
1193 ExtensionError::ExecutionFailed(format!("Invalid model_name: {}", e))
1194 })?;
1195
1196 let model = {
1197 let model_manager = self.model_manager.read().unwrap();
1198 model_manager.get_model(&model_name).cloned()
1199 }
1200 .ok_or_else(|| {
1201 ExtensionError::ExtensionNotFound(format!("Model '{}' not found", model_name))
1202 })?;
1203
1204 let smx_data = self.smx_formatter.export_model_smx(&model)?;
1205
1206 Ok(ExtensionResult {
1207 job_id: job.job_id,
1208 success: true,
1209 result_quins: vec![],
1210 metadata: {
1211 let mut meta = HashMap::new();
1212 use base64::{
1213 engine::general_purpose::STANDARD as BASE64_STANDARD, Engine as _,
1214 };
1215 let encoded_smx = BASE64_STANDARD.encode(&smx_data);
1216
1217 meta.insert("model_name".to_string(), model_name);
1218 meta.insert("smx_data".to_string(), encoded_smx);
1219 meta.insert(
1220 "compression_ratio".to_string(),
1221 model.quantization_config.compression_ratio.to_string(),
1222 );
1223 meta.insert(
1224 "quantization_bits".to_string(),
1225 model.quantization_config.quantization_bits.to_string(),
1226 );
1227 meta.insert("smx_version".to_string(), "1.0".to_string());
1228 meta
1229 },
1230 execution_time_ms: start_time.elapsed().as_millis() as u64,
1231 })
1232 }
1233 "import_ternary_model" => {
1234 let smx_data_base64: String = serde_json::from_value(
1235 job.parameters
1236 .get("smx_data")
1237 .ok_or_else(|| {
1238 ExtensionError::ExecutionFailed("Missing smx_data".to_string())
1239 })?
1240 .clone(),
1241 )
1242 .map_err(|e| ExtensionError::ExecutionFailed(format!("Invalid smx_data: {}", e)))?;
1243
1244 let smx_data = {
1245 use base64::Engine as _;
1246 base64::engine::general_purpose::STANDARD
1247 .decode(&smx_data_base64)
1248 .map_err(|e| {
1249 ExtensionError::ExecutionFailed(format!("Base64 decode failed: {}", e))
1250 })?
1251 };
1252
1253 let model = self.smx_formatter.import_model_smx(&smx_data)?;
1254 self.model_manager.write().unwrap().load_model(model)?;
1255
1256 Ok(ExtensionResult {
1257 job_id: job.job_id,
1258 success: true,
1259 result_quins: vec![],
1260 metadata: {
1261 let mut meta = HashMap::new();
1262 meta.insert("imported".to_string(), "true".to_string());
1263 meta.insert("model_type".to_string(), "ternary_pinn".to_string());
1264 meta.insert("quantization_bits".to_string(), "1.58".to_string());
1265 meta
1266 },
1267 execution_time_ms: start_time.elapsed().as_millis() as u64,
1268 })
1269 }
1270 "simulate_fluid" => {
1271 Ok(ExtensionResult {
1273 job_id: job.job_id,
1274 success: true,
1275 result_quins: vec![],
1276 metadata: HashMap::new(),
1277 execution_time_ms: 5000,
1278 })
1279 }
1280 "predict_chaos" => {
1281 Ok(ExtensionResult {
1283 job_id: job.job_id,
1284 success: true,
1285 result_quins: vec![],
1286 metadata: HashMap::new(),
1287 execution_time_ms: 2000,
1288 })
1289 }
1290 _ => Err(ExtensionError::OperationNotSupported(job.operation)),
1291 }
1292 }
1293
1294 fn shutdown(&self) -> Result<(), ExtensionError> {
1295 Ok(())
1297 }
1298}
1299
1300#[cfg(test)]
1301mod tests {
1302 use super::*;
1303
1304 #[tokio::test]
1305 async fn test_pinn_extension_creation() {
1306 let extension = PinnExtension::new();
1307 let capability = extension.capability();
1308
1309 assert_eq!(capability.name, "ternary_pinn");
1310 assert_eq!(capability.version, "2.0.0");
1311 assert!(capability
1312 .supported_operations
1313 .contains(&"solve_pde_ternary".to_string()));
1314 assert!(capability.required_resources.requires_gpu);
1315 assert!(capability.required_resources.min_vram_mb.is_some());
1316 }
1317
1318 #[tokio::test]
1319 async fn test_pinn_pde_solution() {
1320 let extension = PinnExtension::new();
1321
1322 let params = PinnJobParams {
1323 model_name: "mock_fluid_model".to_string(),
1324 input_points: vec![
1325 vec![0.0, 0.0, 0.0],
1326 vec![1.0, 1.0, 1.0],
1327 vec![2.0, 2.0, 2.0],
1328 ],
1329 time_points: Some(vec![0.0, 0.5, 1.0]),
1330 resolution: 100,
1331 tolerance: 1e-2, max_iterations: 1000,
1333 };
1334
1335 let mock_model = TernaryPinnModel {
1337 name: "mock_fluid_model".to_string(),
1338 domain: PhysicsDomain::FluidDynamics,
1339 model_path: "./mock_model.onnx".to_string(),
1340 input_dim: 3,
1341 output_dim: 3,
1342 boundary_conditions: vec![],
1343 physics_constraints: vec![PhysicsConstraint {
1344 equation_type: EquationType::NavierStokes,
1345 parameters: HashMap::new(),
1346 domain: "fluid_domain".to_string(),
1347 }],
1348 quantization_config: TernaryQuantizationConfig {
1349 quantization_bits: 1.58,
1350 scaling_factor: 1.0,
1351 zero_point: 0,
1352 ternary_levels: [-1, 0, 1],
1353 compression_ratio: 10.0,
1354 },
1355 smx_metadata: SmxMetadataSchema {
1356 model_type: "ternary_pinn".to_string(),
1357 quantization: TernaryQuantizationConfig {
1358 quantization_bits: 1.58,
1359 scaling_factor: 1.0,
1360 zero_point: 0,
1361 ternary_levels: [-1, 0, 1],
1362 compression_ratio: 10.0,
1363 },
1364 input_shape: vec![3],
1365 output_shape: vec![3],
1366 physics_constraints: vec![],
1367 training_metadata: TrainingMetadata {
1368 epochs: 0,
1369 final_loss: 0.0,
1370 convergence_metrics: ConvergenceMetrics {
1371 final_loss: 0.0,
1372 convergence_rate: 0.0,
1373 iterations: 0,
1374 converged: false,
1375 },
1376 validation_accuracy: 0.0,
1377 },
1378 },
1379 ternary_weights: vec![],
1380 };
1381
1382 extension
1383 .model_manager
1384 .write()
1385 .unwrap()
1386 .load_model(mock_model)
1387 .unwrap();
1388
1389 let result = extension.solve_pde_ternary(params).await.unwrap();
1390 assert_eq!(result.output_points.len(), 3);
1391 assert!(result.convergence_metrics.converged);
1393 let _ = result.execution_time_ms;
1395 }
1396
1397 #[tokio::test]
1398 async fn test_physics_violation_detection() {
1399 let extension = PinnExtension::new();
1400
1401 let outputs = vec![
1402 vec![1.0, 1.0, 1.0], vec![100.0, 100.0, 100.0], ];
1405
1406 let constraints = vec![PhysicsConstraint {
1407 equation_type: EquationType::HeatEquation,
1408 parameters: HashMap::new(),
1409 domain: "heat_domain".to_string(),
1410 }];
1411
1412 let violations = extension.check_physics_violations(&outputs, &constraints);
1413 assert_eq!(violations.len(), 1); assert_eq!(violations[0].constraint, "HeatEquation");
1415 }
1416
1417 fn mk_model(
1418 domain: PhysicsDomain,
1419 input_dim: usize,
1420 output_dim: usize,
1421 ternary_weights: Vec<TernaryTensor>,
1422 constraints: Vec<PhysicsConstraint>,
1423 ) -> TernaryPinnModel {
1424 let qc = TernaryQuantizationConfig {
1425 quantization_bits: 1.58,
1426 scaling_factor: 1.0,
1427 zero_point: 0,
1428 ternary_levels: [-1, 0, 1],
1429 compression_ratio: 10.0,
1430 };
1431 TernaryPinnModel {
1432 name: "t".to_string(),
1433 domain,
1434 model_path: String::new(),
1435 input_dim,
1436 output_dim,
1437 boundary_conditions: vec![],
1438 physics_constraints: constraints,
1439 quantization_config: qc.clone(),
1440 smx_metadata: SmxMetadataSchema {
1441 model_type: "ternary_pinn".to_string(),
1442 quantization: qc,
1443 input_shape: vec![input_dim],
1444 output_shape: vec![output_dim],
1445 physics_constraints: vec![],
1446 training_metadata: TrainingMetadata {
1447 epochs: 0,
1448 final_loss: 0.0,
1449 convergence_metrics: ConvergenceMetrics {
1450 final_loss: 0.0,
1451 convergence_rate: 0.0,
1452 iterations: 0,
1453 converged: false,
1454 },
1455 validation_accuracy: 0.0,
1456 },
1457 },
1458 ternary_weights,
1459 }
1460 }
1461
1462 #[test]
1463 fn lorenz_reference_advances_by_rk4() {
1464 assert_eq!(lorenz_state_at(0.0), [1.0, 1.0, 1.0]); let later = lorenz_state_at(1.0);
1466 assert!(later != [1.0, 1.0, 1.0] && later.iter().all(|v| v.is_finite()));
1467 }
1468
1469 #[test]
1470 fn heat_reference_is_unit_at_origin() {
1471 let u = physics_reference(&PhysicsDomain::HeatTransfer, &[0.0, 0.0], 1);
1473 assert!((u[0] - 1.0).abs() < 1e-12);
1474 }
1475
1476 #[test]
1477 fn ternary_forward_is_a_real_mlp() {
1478 let layer = TernaryTensor {
1480 shape: vec![1, 2],
1481 ternary_data: vec![1, 1],
1482 scaling_factor: 1.0,
1483 metadata: TensorMetadata {
1484 tensor_type: "weight".to_string(),
1485 quantization_bits: 1.58,
1486 compression_method: "ternary".to_string(),
1487 original_size: 2,
1488 compressed_size: 1,
1489 },
1490 };
1491 let model = mk_model(PhysicsDomain::HeatTransfer, 2, 1, vec![layer], vec![]);
1492 let out = pinn_forward(&model, &[2.0, 3.0]);
1493 assert!(
1494 (out[0] - 5.0).abs() < 1e-9,
1495 "real ternary MLP forward, got {}",
1496 out[0]
1497 );
1498 }
1499
1500 #[test]
1501 fn heat_reference_satisfies_the_heat_equation() {
1502 let mut params = HashMap::new();
1506 params.insert("thermal_diffusivity".to_string(), 1.0);
1507 let c = PhysicsConstraint {
1508 equation_type: EquationType::HeatEquation,
1509 parameters: params,
1510 domain: "heat".to_string(),
1511 };
1512 let model = mk_model(PhysicsDomain::HeatTransfer, 2, 1, vec![], vec![c]);
1513 let r = pde_residual(&model, &[0.5, 0.5], &model.physics_constraints);
1514 assert!(
1515 r < 1e-2,
1516 "heat fundamental solution should satisfy the PDE, residual {r}"
1517 );
1518 }
1519}