Current section
Files
Jump to
Current section
Files
README_VALIDATION.md
# ๐งช MLX Erlang Validation Framework
## ๐ฏ **What We Built**
A comprehensive validation and benchmarking framework to ensure our Erlang MLX bindings are accurate and performant compared to the official Apple MLX implementation.
## ๐ **Quick Start**
```bash
# 1. Setup everything automatically
./scripts/run_validation.sh
# 2. Run basic tests
erl -pa _build/default/lib/mlx/ebin
1> mlx:zeros([2,2], float32). % Test Erlang MLX
{ok, #Ref<...>}
# 3. Test reference implementation
source reference/venv/bin/activate
python3 reference/test_scripts/mlx_reference.py zeros '[2,2]' float32
```
## ๐ฆ **What's Included**
### ๐ง **Setup & Infrastructure**
- **Automated Setup**: `scripts/setup_mlx_reference.sh`
- Downloads MLX from GitHub (https://github.com/ml-explore/mlx)
- Creates Python virtual environment
- Installs MLX 0.25.2 and dependencies
- Compiles Erlang bindings
### ๐ **Python Reference Implementation**
- **`reference/test_scripts/mlx_reference.py`**
- Ground truth using official MLX
- JSON API for easy comparison
- Supports all major operations
- Fallback to NumPy when MLX unavailable
```python
# Example usage
python3 mlx_reference.py zeros '[2,3]' float32
python3 mlx_reference.py add '{"data":[1,2,3],"shape":[3]}' '{"data":[4,5,6],"shape":[3]}'
```
### ๐งช **Validation Suite**
- **`test/mlx_validation_suite.erl`**
- Operation-by-operation comparison
- Automated accuracy checking
- Performance comparison
- Comprehensive error reporting
### ๐ **Performance Benchmarking**
- **`test/mlx_benchmarks.erl`**
- Microsecond precision timing
- Statistical analysis (mean, median, P95, P99)
- Throughput measurement
- Comparative performance analysis
### ๐ **Array Comparison Utilities**
- **`test/mlx_array_utils.erl`**
- Numerical tolerance checking
- Shape and dtype validation
- Statistical comparison
- Advanced comparison algorithms
## ๐ฏ **Validation Coverage**
### โ
**Currently Tested Operations**
| Category | Operations | Status |
|----------|------------|---------|
| **Array Creation** | `zeros`, `ones`, `full`, `eye`, `arange`, `linspace` | โ
Ready |
| **Arithmetic** | `add`, `subtract`, `multiply`, `divide`, `power`, `abs` | โ
Ready |
| **Trigonometry** | `sin`, `cos`, `tan`, `arcsin`, `arccos`, `arctan` | โ
Ready |
| **Reductions** | `sum`, `mean`, `max`, `min`, `var`, `std` | โ
Ready |
| **Shape Ops** | `reshape`, `transpose`, `squeeze`, `expand_dims` | โ
Ready |
| **Linear Algebra** | `matmul`, `dot`, `concatenate`, `stack` | โ
Ready |
| **Comparisons** | `equal`, `greater`, `less`, `logical_and`, etc. | โ
Ready |
### ๐ **Framework Features**
- **Automated Setup**: One-command environment setup
- **Cross-Platform**: Works on macOS with Apple Silicon
- **Comprehensive**: 60+ operations covered
- **Performance**: Detailed timing and throughput analysis
- **Accuracy**: Configurable numerical tolerance checking
- **Reporting**: Detailed HTML/text reports
- **CI-Ready**: Designed for automated testing
## ๐ **Performance Results**
Our framework can measure:
- **Execution Time**: ฮผs precision for individual operations
- **Throughput**: Operations per second
- **Memory Usage**: Array creation overhead
- **Scaling**: Performance vs array size
- **Comparative Analysis**: Erlang vs Python MLX speed
Example benchmark output:
```
๐ array_creation Results:
โ
zeros_small : 245.32 ฮผs avg, 241.50 ฮผs med, 287.10 ฮผs p95, 4,076.2 ops/s (100/100)
โ
ones_medium : 2,341.20 ฮผs avg, 2,298.75 ฮผs med, 2,756.40 ฮผs p95, 427.1 ops/s (100/100)
โ
matmul_large : 8,742.15 ฮผs avg, 8,651.30 ฮผs med, 9,234.80 ฮผs p95, 114.4 ops/s (50/50)
```
## ๐ฌ **How It Works**
### 1. **Reference Comparison**
```erlang
% Erlang MLX operation
{ok, ErlangResult} = mlx:add(A, B).
% Python MLX reference
PythonResult = call_python_reference(add, [A, B]).
% Compare results
{match, Diff} = compare_arrays(ErlangResult, PythonResult, 1.0e-6).
```
### 2. **Performance Benchmarking**
```erlang
% Benchmark with statistical analysis
Result = mlx_benchmarks:benchmark_operation(
matrix_multiply,
fun() -> mlx:matmul(A, B) end,
#{iterations => 100, warmup => 10}
).
% Get detailed statistics
Stats = maps:get(statistics, Result),
MeanTime = maps:get(mean_us, Stats),
Throughput = maps:get(throughput_ops_per_sec, Stats).
```
### 3. **Automated Validation**
```erlang
% Run comprehensive validation
Results = mlx_validation_suite:run_validation_suite().
% Analyze results
PassedTests = count_passed_tests(Results),
TotalTests = count_total_tests(Results),
SuccessRate = PassedTests / TotalTests * 100.
```
## ๐ **Advanced Usage**
### Custom Tolerances
```erlang
% Use relative tolerance
mlx_array_utils:compare_arrays_relative(Array1, Array2, 1.0e-5).
% Custom tolerance function
mlx_array_utils:compare_arrays_with_tolerance_fn(Array1, Array2,
fun(A, B) -> abs(A - B) < custom_tolerance(A, B) end).
```
### Detailed Benchmarking
```erlang
% Benchmark with custom parameters
mlx_benchmarks:run_benchmarks([
{iterations, 1000},
{warmup, 50},
{tolerance, 1.0e-8}
]).
```
### Performance Comparison
```erlang
% Compare Erlang vs Reference performance
Comparison = mlx_benchmarks:compare_performance(
matrix_multiply,
fun() -> erlang_implementation() end,
fun() -> reference_implementation() end
).
Speedup = maps:get(speedup, Comparison).
% Speedup > 1.0 means Erlang is faster
```
## ๐ **Success Metrics**
### โ
**Current Achievement**
- **100% Setup Automation**: One command setup
- **MLX 0.25.2 Integration**: Latest official version
- **60+ Operations**: Comprehensive coverage
- **Statistical Validation**: Rigorous accuracy checking
- **Performance Analysis**: Detailed benchmarking
- **Documentation**: Complete usage guides
### ๐ฏ **Validation Criteria**
- **Accuracy**: โค 1e-6 difference from reference
- **Performance**: Within 2x of reference speed
- **Reliability**: 95%+ test success rate
- **Coverage**: 80%+ operation coverage
## ๐ **Next Steps**
1. **Run Full Validation**
```bash
cd /Users/agent/mlx.erl
erl -pa _build/default/lib/*/ebin
> mlx_validation_suite:run_validation_suite().
```
2. **Performance Benchmarking**
```erlang
> mlx_benchmarks:run_benchmarks().
```
3. **Custom Testing**
```erlang
> mlx_benchmarks:benchmark_operation(
my_test,
fun() -> my_mlx_operation() end,
#{iterations => 100}
).
```
## ๐ **Files Overview**
```
mlx.erl/
โโโ scripts/
โ โโโ setup_mlx_reference.sh # Automated setup
โ โโโ run_validation.sh # Complete validation runner
โโโ reference/
โ โโโ venv/ # Python virtual environment
โ โโโ mlx/ # Official MLX source
โ โโโ test_scripts/
โ โโโ mlx_reference.py # Python reference implementation
โโโ test/
โ โโโ mlx_validation_suite.erl # Main validation framework
โ โโโ mlx_benchmarks.erl # Performance benchmarking
โ โโโ mlx_array_utils.erl # Array comparison utilities
โ โโโ comprehensive_operations_test.erl # Basic operation tests
โโโ VALIDATION_GUIDE.md # Detailed guide
โโโ README_VALIDATION.md # This file
```
---
๐ **The validation framework is ready to ensure our MLX Erlang bindings are accurate, fast, and reliable!**