ipfrs_tensorlogic/lib.rs
1//! IPFRS TensorLogic - Integration with TensorLogic IR
2//!
3//! This crate provides comprehensive integration between IPFRS and TensorLogic including:
4//!
5//! ## Core Features
6//!
7//! - **IR (Intermediate Representation)**: Serialization and storage of logic terms
8//! - **Term and Predicate Storage**: Content-addressed storage for logical statements
9//! - **Distributed Reasoning**: Query caching, goal decomposition, and proof assembly
10//! - **Zero-Copy Tensor Transport**: Apache Arrow integration for efficient data sharing
11//! - **Safetensors Support**: Read/write Safetensors format for ML models
12//! - **PyTorch Checkpoint Support**: Load/save PyTorch .pt/.pth model checkpoints
13//! - **Shared Memory**: Cross-process memory mapping for large tensors
14//! - **Gradient Management**: Compression, aggregation, and differential privacy
15//! - **Model Version Control**: Git-like versioning for ML models
16//! - **Provenance Tracking**: Complete lineage tracking for datasets and models
17//! - **Computation Graphs**: IPLD-based graph storage with optimization
18//! - **Device Management**: Heterogeneous device support with adaptive batch sizing
19//! - **FFI Profiling**: Overhead measurement and bottleneck identification
20//! - **Allocation Optimization**: Buffer pooling and zero-copy conversions
21//! - **GPU Support**: Stub implementation for future CUDA/OpenCL/Vulkan integration
22//!
23//! ## Performance Targets
24//!
25//! - FFI call overhead: < 1μs
26//! - Zero-copy tensor access: < 100ns
27//! - Query cache lookup: < 1μs
28//! - Term serialization: < 10μs for small terms
29//!
30//! # Examples
31//!
32//! ## Basic Term and Predicate Creation
33//!
34//! ```
35//! use ipfrs_tensorlogic::{Term, Predicate, Constant};
36//!
37//! // Create terms
38//! let alice = Term::Const(Constant::String("Alice".to_string()));
39//! let bob = Term::Const(Constant::String("Bob".to_string()));
40//! let x = Term::Var("X".to_string());
41//!
42//! // Create a predicate: parent(Alice, Bob)
43//! let pred = Predicate::new("parent".to_string(), vec![alice, bob]);
44//! assert!(pred.is_ground());
45//! ```
46//!
47//! ## Zero-Copy Tensor Operations
48//!
49//! ```
50//! use ipfrs_tensorlogic::{ArrowTensor, ArrowTensorStore};
51//!
52//! // Create a tensor from f32 data
53//! let data: Vec<f32> = vec![1.0, 2.0, 3.0, 4.0];
54//! let tensor = ArrowTensor::from_slice_f32("my_tensor", vec![4], &data);
55//!
56//! // Zero-copy access to the data
57//! let slice = tensor.as_slice_f32().unwrap();
58//! assert_eq!(slice[0], 1.0);
59//!
60//! // Create a tensor store
61//! let mut store = ArrowTensorStore::new();
62//! store.insert(tensor);
63//! assert_eq!(store.len(), 1);
64//! ```
65//!
66//! ## Query Caching
67//!
68//! ```
69//! use ipfrs_tensorlogic::{QueryCache, QueryKey};
70//!
71//! // Create a cache with capacity 100
72//! let cache = QueryCache::new(100);
73//!
74//! // Insert a query result
75//! let key = QueryKey {
76//! predicate_name: "parent".to_string(),
77//! ground_args: vec![],
78//! };
79//! cache.insert(key.clone(), vec![]);
80//!
81//! // Retrieve from cache
82//! let result = cache.get(&key);
83//! assert!(result.is_some());
84//! ```
85//!
86//! ## Gradient Compression
87//!
88//! ```
89//! use ipfrs_tensorlogic::GradientCompressor;
90//!
91//! let gradient = vec![0.1, 0.5, 0.01, 0.8, 0.02];
92//!
93//! // Top-k compression (keep largest 2 values)
94//! let sparse = GradientCompressor::top_k(&gradient, vec![5], 2).unwrap();
95//! assert_eq!(sparse.nnz(), 2); // Only 2 non-zero elements
96//!
97//! // Quantization to int8
98//! let quantized = GradientCompressor::quantize(&gradient, vec![5]);
99//! assert!(quantized.compression_ratio() > 1.0);
100//! ```
101//!
102//! ## Device-Aware Batch Sizing
103//!
104//! ```
105//! use ipfrs_tensorlogic::{DeviceCapabilities, AdaptiveBatchSizer};
106//! use std::sync::Arc;
107//!
108//! // Detect device capabilities
109//! let caps = DeviceCapabilities::detect().unwrap();
110//! println!("Device: {:?}, Memory: {} GB",
111//! caps.device_type,
112//! caps.memory.total_bytes / 1024 / 1024 / 1024);
113//!
114//! // Create adaptive batch sizer
115//! let sizer = AdaptiveBatchSizer::new(Arc::new(caps))
116//! .with_min_batch_size(1)
117//! .with_max_batch_size(256);
118//!
119//! // Calculate optimal batch size
120//! let model_size = 500 * 1024 * 1024; // 500MB model
121//! let item_size = 256 * 1024; // 256KB per item
122//! let batch_size = sizer.calculate(item_size, model_size);
123//! println!("Optimal batch size: {}", batch_size);
124//! ```
125//!
126//! ## FFI Profiling
127//!
128//! ```
129//! use ipfrs_tensorlogic::{FfiProfiler, global_profiler};
130//!
131//! let profiler = FfiProfiler::new();
132//!
133//! // Profile a function call
134//! {
135//! let _guard = profiler.start("my_ffi_function");
136//! // Your FFI code here
137//! }
138//!
139//! // Get statistics
140//! let stats = profiler.get_stats("my_ffi_function").unwrap();
141//! println!("Calls: {}, Avg: {:?}", stats.call_count, stats.avg_duration);
142//!
143//! // Use global profiler
144//! let global = global_profiler();
145//! let _guard = global.start("global_operation");
146//! ```
147//!
148//! ## Buffer Pooling
149//!
150//! ```
151//! use ipfrs_tensorlogic::BufferPool;
152//!
153//! let pool = BufferPool::new(4096, 10); // 4KB buffers, max 10 pooled
154//!
155//! // Acquire buffer from pool
156//! let mut buffer = pool.acquire();
157//! buffer.as_mut().extend_from_slice(&[1, 2, 3, 4]);
158//!
159//! // Buffer automatically returned to pool when dropped
160//! drop(buffer);
161//! assert!(pool.size() > 0); // Buffer available for reuse
162//! ```
163//!
164//! ## Zero-Copy Conversions
165//!
166//! ```
167//! use ipfrs_tensorlogic::ZeroCopyConverter;
168//!
169//! let floats: Vec<f32> = vec![1.0, 2.0, 3.0, 4.0];
170//!
171//! // Zero-copy conversion to bytes
172//! let bytes = ZeroCopyConverter::slice_to_bytes(&floats);
173//! assert_eq!(bytes.len(), 16); // 4 floats * 4 bytes
174//!
175//! // Zero-copy conversion back
176//! let floats_back: &[f32] = ZeroCopyConverter::bytes_to_slice(bytes);
177//! assert_eq!(floats, floats_back);
178//! ```
179//!
180//! ## Safetensors Multi-Dtype Support
181//!
182//! ```
183//! use ipfrs_tensorlogic::{SafetensorsWriter, SafetensorsReader, ArrowTensor};
184//! use bytes::Bytes;
185//!
186//! // Create a model with multiple data types
187//! let mut writer = SafetensorsWriter::new();
188//!
189//! // Add float32 weights
190//! writer.add_f32("layer1.weights", vec![128, 64], &vec![0.1; 8192]);
191//!
192//! // Add float64 biases for high precision
193//! writer.add_f64("layer1.bias", vec![64], &vec![0.01; 64]);
194//!
195//! // Add int32 indices
196//! writer.add_i32("vocab_indices", vec![1000], &vec![42; 1000]);
197//!
198//! // Add int64 large IDs
199//! writer.add_i64("entity_ids", vec![100], &vec![1000000; 100]);
200//!
201//! // Serialize and read back
202//! let bytes = writer.serialize().unwrap();
203//! let reader = SafetensorsReader::from_bytes(Bytes::from(bytes)).unwrap();
204//!
205//! // Load as Arrow tensors for zero-copy access
206//! let weights = reader.load_as_arrow("layer1.weights").unwrap();
207//! let bias = reader.load_as_arrow("layer1.bias").unwrap();
208//! let indices = reader.load_as_arrow("vocab_indices").unwrap();
209//! let ids = reader.load_as_arrow("entity_ids").unwrap();
210//!
211//! assert!(weights.as_slice_f32().is_some());
212//! assert!(bias.as_slice_f64().is_some());
213//! assert!(indices.as_slice_i32().is_some());
214//! assert!(ids.as_slice_i64().is_some());
215//! ```
216//!
217//! ## Memory Profiling
218//!
219//! ```
220//! use ipfrs_tensorlogic::MemoryProfiler;
221//! use std::time::Duration;
222//!
223//! let profiler = MemoryProfiler::new();
224//!
225//! {
226//! let _guard = profiler.start_tracking("tensor_allocation");
227//! let data: Vec<f32> = vec![1.0; 1000000]; // ~4 MB
228//! std::thread::sleep(Duration::from_millis(10));
229//! drop(data);
230//! }
231//!
232//! let stats = profiler.get_stats("tensor_allocation").unwrap();
233//! assert_eq!(stats.track_count, 1);
234//! assert!(stats.total_duration >= Duration::from_millis(10));
235//!
236//! // Generate and print a report
237//! let report = profiler.generate_report();
238//! println!("Total operations tracked: {}", report.total_operations);
239//! ```
240//!
241//! ## Model Quantization
242//!
243//! ```
244//! use ipfrs_tensorlogic::{QuantizedTensor, QuantizationConfig};
245//!
246//! // Per-tensor INT8 symmetric quantization
247//! let weights = vec![0.5, -0.3, 0.8, -0.1];
248//! let config = QuantizationConfig::int8_symmetric();
249//! let quantized = QuantizedTensor::quantize_per_tensor(&weights, vec![4], config).unwrap();
250//!
251//! // Dequantize back to f32
252//! let dequantized = quantized.dequantize();
253//! assert_eq!(dequantized.len(), 4);
254//!
255//! // Check compression ratio
256//! println!("Compression: {:.2}x", quantized.compression_ratio());
257//! ```
258//!
259//! ## More Examples
260//!
261//! For complete examples, see the `examples/` directory:
262//! - `basic_reasoning.rs` - TensorLogic inference and backward chaining
263//! - `query_optimization.rs` - Materialized views and query caching
264//! - `proof_storage.rs` - Proof fragment management and compression
265//! - `proof_explanation_demo.rs` - Automatic proof explanation in natural language (multiple styles)
266//! - `model_versioning.rs` - Git-like version control for ML models
267//! - `model_quantization.rs` - Model quantization for edge deployment (INT4/INT8, per-channel, dynamic)
268//! - `tensor_storage.rs` - Safetensors and Arrow integration
269//! - `device_aware_training.rs` - Device detection and adaptive batching
270//! - `federated_learning.rs` - Gradient compression and differential privacy
271//! - `allocation_optimization.rs` - Buffer pooling and zero-copy techniques
272//! - `ffi_profiling.rs` - FFI overhead measurement
273//! - `distributed_graph_execution.rs` - Graph partitioning across multiple workers
274//! - `memory_profiling.rs` - Memory usage tracking and profiling
275//! - `visualization_demo.rs` - Graph and proof visualization with DOT format
276
277use ipfrs_core::Cid;
278use serde::{Deserialize, Deserializer, Serializer};
279
280pub mod allocation_optimizer;
281pub mod arrow;
282pub mod cache;
283pub mod computation_graph;
284pub mod datalog;
285pub mod device;
286pub mod ffi_profiler;
287pub mod gpu;
288pub mod gradient;
289pub mod ir;
290pub mod memory_profiler;
291pub mod optimizer;
292pub mod proof_explanation;
293pub mod proof_storage;
294pub mod provenance;
295pub mod pytorch_checkpoint;
296pub mod quantization;
297pub mod reasoning;
298pub mod recursive_reasoning;
299pub mod remote_reasoning;
300pub mod safetensors_support;
301pub mod shared_memory;
302pub mod storage;
303pub mod utils;
304pub mod version_control;
305pub mod visualization;
306
307// Allocation optimization
308pub use allocation_optimizer::{
309 AdaptiveBuffer, AllocationError, BufferPool, PooledBuffer, StackBuffer, TypedBufferPool,
310 TypedPooledBuffer, ZeroCopyConverter,
311};
312
313// Arrow integration
314pub use arrow::{ArrowTensor, ArrowTensorStore, TensorDtype, TensorMetadata, ZeroCopyAccessor};
315
316// Caching
317pub use cache::{
318 CacheManager, CacheStats, CacheStatsSnapshot, CombinedCacheStats, QueryCache, QueryKey,
319 RemoteFactCache,
320};
321
322// Computation graphs
323pub use computation_graph::{
324 BatchScheduler, ComputationGraph, DistributedExecutor, ExecutionBatch, GraphError, GraphNode,
325 GraphOptimizer, GraphPartition, LazyCache, NodeAssignment, ParallelExecutor, StreamChunk,
326 StreamingExecutor, TensorOp,
327};
328
329// Datalog parsing
330pub use datalog::{parse_fact, parse_query, parse_rule, DatalogParser, ParseError, Statement};
331
332// Device capabilities
333pub use device::{
334 AdaptiveBatchSizer, CpuInfo, DeviceArch, DeviceCapabilities, DeviceError,
335 DevicePerformanceTier, DeviceProfiler, DeviceType, MemoryInfo,
336};
337
338// FFI profiling
339pub use ffi_profiler::{
340 global_profiler, FfiCallGuard, FfiCallStats, FfiProfiler, OverheadSummary, ProfilingReport,
341};
342
343// GPU execution (stub for future integration)
344pub use gpu::{
345 GpuBackend, GpuBuffer, GpuDevice, GpuError, GpuExecutor, GpuKernel, GpuMemoryManager,
346};
347
348// Gradient storage
349pub use gradient::{
350 ClientInfo, ClientState, ConvergenceDetector, DPMechanism, DifferentialPrivacy, FederatedRound,
351 GradientAggregator, GradientCompressor, GradientDelta, GradientError, GradientVerifier,
352 LayerGradient, ModelSyncProtocol, PrivacyBudget, QuantizedGradient, SecureAggregation,
353 SparseGradient,
354};
355
356// IR types
357pub use ir::{Constant, KnowledgeBase, KnowledgeBaseStats, Predicate, Rule, Term, TermRef};
358
359// Memory profiling
360pub use memory_profiler::{
361 MemoryProfiler, MemoryProfilingReport, MemoryStats, MemoryTrackingGuard,
362};
363
364// Query optimization
365pub use optimizer::{
366 OptimizationRecommendation, PlanNode, PredicateStats, QueryOptimizer, QueryPlan,
367};
368
369// Reasoning
370pub use reasoning::{
371 CycleDetector, DistributedReasoner, GoalDecomposition, InferenceEngine,
372 MemoizedInferenceEngine, Proof, ProofRule, Substitution,
373};
374
375// Recursive reasoning
376pub use recursive_reasoning::{
377 FixpointEngine, StratificationAnalyzer, StratificationResult, TableStats, TabledInferenceEngine,
378};
379
380// Remote reasoning
381pub use remote_reasoning::{
382 DistributedGoalResolver, DistributedProofAssembler, FactDiscoveryRequest,
383 FactDiscoveryResponse, GoalResolutionRequest, GoalResolutionResponse, IncrementalLoadRequest,
384 IncrementalLoadResponse, MockRemoteKnowledgeProvider, QueryRequest, QueryResponse,
385 RemoteKnowledgeProvider, RemoteReasoningError,
386};
387
388// Proof storage
389pub use proof_storage::{
390 ProofAssembler, ProofFragment, ProofFragmentRef, ProofFragmentStore, ProofMetadata, RuleRef,
391};
392
393// Proof explanation
394pub use proof_explanation::{
395 ExplanationConfig, ExplanationStyle, FragmentProofExplainer, ProofExplainer,
396 ProofExplanationBuilder,
397};
398
399// Provenance tracking
400pub use provenance::{
401 Attribution, DatasetProvenance, Hyperparameters, License, LineageTrace, ProvenanceError,
402 ProvenanceGraph, TrainingProvenance,
403};
404
405// PyTorch checkpoint support
406pub use pytorch_checkpoint::{
407 CheckpointMetadata, OptimizerState, ParamState, PyTorchCheckpoint, StateDict, TensorData,
408};
409
410// Quantization support
411pub use quantization::{
412 CalibrationMethod, DynamicQuantizer, QuantizationConfig, QuantizationError,
413 QuantizationGranularity, QuantizationParams, QuantizationScheme, QuantizedTensor,
414};
415
416// Safetensors support
417pub use safetensors_support::{
418 ChunkedModelStorage, ModelSummary, SafetensorError, SafetensorsReader, SafetensorsWriter,
419 TensorInfo,
420};
421
422// Shared memory
423pub use shared_memory::{
424 SharedMemoryError, SharedMemoryPool, SharedTensorBuffer, SharedTensorBufferReadOnly,
425 SharedTensorInfo,
426};
427
428// Storage
429pub use storage::TensorLogicStore;
430
431// Utilities
432pub use utils::{KnowledgeBaseUtils, PredicateBuilder, QueryUtils, RuleBuilder, TermUtils};
433
434// Version control
435pub use version_control::{
436 Branch, LayerDiff, ModelCommit, ModelDiff, ModelDiffer, ModelRepository, VersionControlError,
437};
438
439// Visualization
440pub use visualization::{GraphVisualizer, ProofVisualizer};
441
442/// Serialize CID as string
443pub(crate) fn serialize_cid<S>(cid: &Cid, serializer: S) -> Result<S::Ok, S::Error>
444where
445 S: Serializer,
446{
447 serializer.serialize_str(&cid.to_string())
448}
449
450/// Deserialize CID from string
451pub(crate) fn deserialize_cid<'de, D>(deserializer: D) -> Result<Cid, D::Error>
452where
453 D: Deserializer<'de>,
454{
455 let s = String::deserialize(deserializer)?;
456 s.parse().map_err(serde::de::Error::custom)
457}