xlog_neural/lib.rs
1//! Neural network integration for XLOG probabilistic logic programs.
2#![warn(missing_docs)]
3//!
4//! This crate provides the infrastructure for integrating PyTorch neural networks
5//! with XLOG's probabilistic inference engine, following the DeepProbLog paradigm.
6//!
7//! # Architecture
8//!
9//! The neural integration consists of:
10//!
11//! - **NetworkRegistry**: Central registry managing all neural networks
12//! - **NetworkHandle**: Holds PyTorch module, optimizer, and configuration
13//! - **NetworkConfig**: Configuration options for network behavior
14//!
15//! # Features
16//!
17//! - `python` - Enable Python interop via PyO3. Required for actual PyTorch integration.
18//!
19//! # Example
20//!
21//! ```
22//! use xlog_neural::{NetworkRegistry, NetworkConfig};
23//!
24//! let mut registry = NetworkRegistry::new();
25//! registry.register(NetworkConfig::default("mnist_net"));
26//!
27//! // Set all networks to training mode
28//! registry.set_train_mode(true);
29//! ```
30
31pub mod batch;
32pub mod bridge;
33pub mod handle;
34pub mod registry;
35pub mod tensor_source;
36
37pub use batch::{BatchCollector, BatchMapping, BatchResult, NeuralCall};
38pub use bridge::{ADProbability, CircuitLeaf, NeuralBridge, NeuralOutput};
39pub use handle::{EmbeddingHandle, NetworkHandle};
40pub use registry::{NetworkConfig, NetworkRegistry};
41pub use tensor_source::{TensorMetadata, TensorSourceError, TensorSourceRegistry};
42
43/// Re-export pyo3 when the python feature is enabled
44#[cfg(feature = "python")]
45pub use pyo3;
46
47/// Error types for neural network operations
48#[derive(Debug, thiserror::Error)]
49#[non_exhaustive]
50pub enum NeuralError {
51 /// Network not found in registry
52 #[error("Network not found: {0}")]
53 NetworkNotFound(String),
54
55 /// Network already registered
56 #[error("Network already registered: {0}")]
57 NetworkAlreadyExists(String),
58
59 /// PyTorch module error
60 #[error("PyTorch error: {0}")]
61 PyTorchError(String),
62
63 /// Invalid network configuration
64 #[error("Invalid configuration: {0}")]
65 InvalidConfig(String),
66
67 /// Batch processing error
68 #[error("Batch error: {0}")]
69 BatchError(String),
70
71 /// Cache error
72 #[error("Cache error: {0}")]
73 CacheError(String),
74}
75
76/// Result type for neural operations
77pub type NeuralResult<T> = std::result::Result<T, NeuralError>;
78
79// Error conversion seams — orphan rule: NeuralError is defined here.
80impl From<NeuralError> for xlog_core::XlogError {
81 fn from(e: NeuralError) -> Self {
82 xlog_core::XlogError::Execution(e.to_string())
83 }
84}
85
86impl From<tensor_source::TensorSourceError> for xlog_core::XlogError {
87 fn from(e: tensor_source::TensorSourceError) -> Self {
88 xlog_core::XlogError::Execution(e.to_string())
89 }
90}