diffai
AI/ML specialized diff tool for PyTorch, Safetensors, NumPy, and MATLAB files
A next-generation diff tool specialized for AI/ML and scientific computing workflows that understands model structures, tensor statistics, and numerical data - not just text changes. Native support for PyTorch, Safetensors, NumPy arrays, MATLAB files, and structured data.
# Traditional diff fails with binary model files
# diffai shows meaningful model changes
Key Features
- AI/ML Native: Direct support for PyTorch (.pt/.pth), Safetensors (.safetensors), NumPy (.npy/.npz), and MATLAB (.mat) files
- Tensor Analysis: Automatic calculation of tensor statistics (mean, std, min, max, shape, memory usage)
- ML Analysis Functions: Statistical analysis, quantization analysis, architecture comparison, and more
- Scientific Data Support: NumPy arrays and MATLAB matrices with complex number support
- Pure Rust Implementation: No system dependencies, works on Windows/Linux/macOS without additional installations
- Multiple Output Formats: Colored CLI, JSON for MLOps integration, YAML for human-readable reports
- Fast and Memory Efficient: Built in Rust for handling large model files efficiently
Why diffai?
Traditional diff tools are inadequate for AI/ML workflows:
Challenge | Traditional Tools | diffai |
---|---|---|
Binary model files | "Binary files differ" | Tensor-level analysis with statistics |
Large files (GB+) | Memory issues or failures | Efficient streaming and chunked processing |
Statistical changes | No semantic understanding | Mean/std/shape comparison with significance |
ML-specific formats | No support | Native PyTorch/Safetensors/NumPy/MATLAB |
Scientific workflows | Text-only comparison | Numerical array analysis and visualization |
diffai vs MLOps Tools
diffai complements existing MLOps tools by focusing on structural comparison rather than experiment management:
Aspect | diffai | MLflow / DVC / ModelDB |
---|---|---|
Focus | "Making incomparable things comparable" | Systematization, reproducibility, CI/CD integration |
Data Assumption | Unknown origin files / black-box generated artifacts | Well-documented and tracked data |
Operation | Structural and visual comparison optimization | Version control and experiment tracking specialization |
Scope | Visualization of "ambiguous structures" including JSON/YAML/model files | Experiment metadata, version management, reproducibility |
Installation
From crates.io (Recommended)
From Source
Quick Start
Basic Model Comparison
# Compare PyTorch models
# Compare Safetensors with statistical analysis
# Compare NumPy arrays
# Compare MATLAB files
Advanced ML Analysis
# Current available analysis
# Combined analysis with sorting
# JSON output for automation
# Detailed diagnostic information with verbose mode
# Future Phase 3 features (coming soon)
Supported File Formats
ML Model Formats
- Safetensors (.safetensors) - HuggingFace standard format
- PyTorch (.pt, .pth) - PyTorch model files with Candle integration
Scientific Data Formats
- NumPy (.npy, .npz) - NumPy arrays with full statistical analysis
- MATLAB (.mat) - MATLAB matrices with complex number support
Structured Data Formats
- JSON (.json) - JavaScript Object Notation
- YAML (.yaml, .yml) - YAML Ain't Markup Language
- TOML (.toml) - Tom's Obvious Minimal Language
- XML (.xml) - Extensible Markup Language
- INI (.ini) - Configuration files
- CSV (.csv) - Comma-separated values
ML Analysis Functions
Currently Available (v0.2.7)
--stats
- Detailed tensor statistics (mean, std, min, max, shape, memory)--quantization-analysis
- Analyze quantization effects and efficiency--sort-by-change-magnitude
- Sort differences by magnitude for prioritization--show-layer-impact
- Layer-by-layer impact analysis--architecture-comparison
- Compare model architectures and structural changes--memory-analysis
- Analyze memory usage and optimization opportunities--anomaly-detection
- Detect numerical anomalies in model parameters--change-summary
- Generate detailed change summaries--convergence-analysis
- Analyze convergence patterns in model parameters--gradient-analysis
- Analyze gradient information when available--similarity-matrix
- Generate similarity matrix for model comparison
Coming in Phase 4 (ML Framework Expansion)
- TensorFlow format support (.pb, .h5, SavedModel)
- ONNX format support
- Advanced visualization and charting features
Design Philosophy
diffai follows UNIX philosophy: simple, composable tools that do one thing well. Features are orthogonal and can be combined for powerful analysis workflows.
Debugging and Diagnostics
Verbose Mode (--verbose
/ -v
)
Get comprehensive diagnostic information for debugging and performance analysis:
# Basic verbose output
# Verbose with ML analysis features
Verbose output includes:
- Configuration diagnostics: Active ML features, format settings, filters
- File analysis: Paths, sizes, detected formats, processing context
- Performance metrics: Processing time, difference counts, optimization status
- Directory statistics: File counts, comparison summaries (with
--recursive
)
Example verbose output:
=== diffai verbose mode enabled ===
Configuration:
Input format: None
Output format: Cli
ML analysis features: statistics, architecture_comparison
Epsilon tolerance: 0.001
File analysis:
Input 1: model1.safetensors
Input 2: model2.safetensors
Detected format: Safetensors
File 1 size: 1048576 bytes
File 2 size: 1048576 bytes
Processing results:
Total processing time: 1.234ms
Differences found: 15
ML/Scientific data analysis completed
📚 See Verbose Output Guide for detailed usage
Output Formats
CLI Output (Default)
Colored, human-readable output with intuitive symbols:
~
Changed tensors/arrays with statistical comparison+
Added tensors/arrays with metadata-
Removed tensors/arrays with metadata
JSON Output
Structured output for MLOps integration and automation:
|
YAML Output
Human-readable structured output for documentation:
Real-World Use Cases
Research & Development
# Compare model before and after fine-tuning
# Analyze architectural changes during development
MLOps & CI/CD
# Automated model validation in CI/CD
# Performance impact assessment
Scientific Computing
# Compare NumPy experiment results
# Analyze MATLAB simulation data
# Compare compressed NumPy archives
Experiment Tracking
# Generate comprehensive reports
# A/B test analysis
Command-Line Options
Basic Options
-f, --format <FORMAT>
- Specify input file format-o, --output <OUTPUT>
- Choose output format (cli, json, yaml)-r, --recursive
- Compare directories recursively--stats
- Show detailed statistics for ML models
Advanced Options
--path <PATH>
- Filter differences by specific path--ignore-keys-regex <REGEX>
- Ignore keys matching regex pattern--epsilon <FLOAT>
- Set tolerance for float comparisons--array-id-key <KEY>
- Specify key for array element identification--sort-by-change-magnitude
- Sort by change magnitude
Examples
Basic Tensor Comparison
Advanced Analysis
Scientific Data Analysis
MATLAB File Comparison
Performance
diffai is optimized for large files and scientific workflows:
- Memory Efficient: Streaming processing for GB+ files
- Fast: Rust implementation with optimized tensor operations
- Scalable: Handles models with millions/billions of parameters
- Cross-Platform: Works on Windows, Linux, and macOS without dependencies
Contributing
We welcome contributions! Please see CONTRIBUTING for guidelines.
Development Setup
Running Tests
# Run all tests
# Run specific test categories
License
This project is licensed under the MIT License - see the LICENSE file for details.
Related Projects
- diffx - General-purpose structured data diff tool (diffai's sibling project)
- safetensors - Simple, safe way to store and distribute tensors
- PyTorch - Machine learning framework
- NumPy - Fundamental package for scientific computing with Python