|
| 1 | +# ML-CPP: Elastic Machine Learning Core |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +The ML-CPP repository contains the C++ core implementation of Elastic's Machine Learning capabilities, providing high-performance analytics for anomaly detection, data frame analytics, and PyTorch model inference within the Elastic Stack. |
| 6 | + |
| 7 | +## Purpose and Scope |
| 8 | + |
| 9 | +This codebase implements the computational engine for: |
| 10 | + |
| 11 | +- **Time Series Anomaly Detection**: Real-time detection of anomalies in time series data using statistical models |
| 12 | +- **Data Frame Analytics**: Supervised learning (classification/regression) and unsupervised learning (outlier detection) on structured data |
| 13 | +- **PyTorch Model Inference**: High-performance inference for custom PyTorch models |
| 14 | +- **Data Categorization**: Automatic categorization of log messages and text data |
| 15 | + |
| 16 | +## High-Level Architecture |
| 17 | + |
| 18 | +The system follows a layered architecture with clear separation of concerns: |
| 19 | + |
| 20 | +```mermaid |
| 21 | +graph TB |
| 22 | + subgraph "Executables (bin/)" |
| 23 | + A[autodetect] --> B[controller] |
| 24 | + C[data_frame_analyzer] --> B |
| 25 | + D[pytorch_inference] --> B |
| 26 | + E[categorize] --> B |
| 27 | + F[normalize] --> B |
| 28 | + end |
| 29 | + |
| 30 | + subgraph "API Layer (lib/api/)" |
| 31 | + G[CAnomalyJob] --> H[CDataFrameAnalyzer] |
| 32 | + I[CIoManager] --> J[CPersistenceManager] |
| 33 | + end |
| 34 | + |
| 35 | + subgraph "Model Layer (lib/model/)" |
| 36 | + K[CAnomalyDetector] --> L[CDataGatherer] |
| 37 | + M[CModelFactory] --> N[CResourceMonitor] |
| 38 | + end |
| 39 | + |
| 40 | + subgraph "Mathematics (lib/maths/)" |
| 41 | + O[Time Series] --> P[Analytics] |
| 42 | + Q[Common] --> R[Linear Algebra] |
| 43 | + end |
| 44 | + |
| 45 | + subgraph "Core (lib/core/)" |
| 46 | + S[CLogger] --> T[CDataFrame] |
| 47 | + U[CMemoryUsage] --> V[CStatePersistInserter] |
| 48 | + end |
| 49 | + |
| 50 | + A --> G |
| 51 | + C --> H |
| 52 | + G --> K |
| 53 | + H --> M |
| 54 | + K --> O |
| 55 | + M --> Q |
| 56 | + O --> S |
| 57 | + Q --> S |
| 58 | +``` |
| 59 | + |
| 60 | +## Key Design Principles |
| 61 | + |
| 62 | +### 1. Memory-Conscious Design |
| 63 | +- **Resource Monitoring**: Continuous tracking of memory usage with configurable limits |
| 64 | +- **Memory Circuit Breakers**: Automatic process termination when memory limits are exceeded |
| 65 | +- **Efficient Data Structures**: Specialized containers for time series and sparse data |
| 66 | + |
| 67 | +### 2. State Management |
| 68 | +- **Persistence**: Complete model state can be saved and restored |
| 69 | +- **Incremental Updates**: Models update incrementally as new data arrives |
| 70 | +- **Fault Tolerance**: Robust handling of state corruption and version mismatches |
| 71 | + |
| 72 | +### 3. Performance Optimization |
| 73 | +- **Parallel Processing**: Multi-threaded execution where beneficial |
| 74 | +- **SIMD Operations**: Vectorized mathematical operations |
| 75 | +- **Memory Pooling**: Efficient memory allocation patterns |
| 76 | +- **Caching**: Strategic caching of expensive computations |
| 77 | + |
| 78 | +### 4. Extensibility |
| 79 | +- **Plugin Architecture**: Modular design for different model types |
| 80 | +- **Factory Pattern**: Dynamic model creation based on configuration |
| 81 | +- **Interface-Based Design**: Clear abstractions for different components |
| 82 | + |
| 83 | +## Core Components |
| 84 | + |
| 85 | +### Executables (`bin/`) |
| 86 | + |
| 87 | +| Executable | Purpose | Key Features | |
| 88 | +|------------|---------|--------------| |
| 89 | +| `autodetect` | Time series anomaly detection | Real-time processing, multiple detector types | |
| 90 | +| `controller` | Process management | Spawns and manages other ML processes | |
| 91 | +| `data_frame_analyzer` | Supervised/unsupervised learning | Boosted trees, outlier detection | |
| 92 | +| `pytorch_inference` | PyTorch model inference | Custom model support, batch processing | |
| 93 | +| `categorize` | Text categorization | Tokenization, pattern matching | |
| 94 | +| `normalize` | Data normalization | Feature scaling, outlier handling | |
| 95 | + |
| 96 | +### Core Libraries |
| 97 | + |
| 98 | +#### `lib/core/` - Fundamental Utilities |
| 99 | +- **Logging**: Multi-level logging with named pipe support |
| 100 | +- **I/O Management**: Efficient data streaming and parsing |
| 101 | +- **Memory Management**: Usage tracking and circuit breakers |
| 102 | +- **State Persistence**: Serialization and restoration |
| 103 | +- **Concurrency**: Thread-safe operations and synchronization |
| 104 | + |
| 105 | +#### `lib/maths/` - Mathematical Foundation |
| 106 | +- **Common**: Statistical functions, linear algebra, probability distributions |
| 107 | +- **Time Series**: Seasonal decomposition, trend analysis, forecasting |
| 108 | +- **Analytics**: Boosted trees, clustering, feature importance |
| 109 | + |
| 110 | +#### `lib/model/` - Anomaly Detection Models |
| 111 | +- **Detectors**: Individual and population-based anomaly detection |
| 112 | +- **Data Gatherers**: Time series data collection and bucketing |
| 113 | +- **Model Factory**: Dynamic model creation and management |
| 114 | +- **Resource Monitoring**: Memory and CPU usage tracking |
| 115 | + |
| 116 | +#### `lib/api/` - High-Level API |
| 117 | +- **Job Management**: Configuration and lifecycle management |
| 118 | +- **Data Processing**: Input parsing and output formatting |
| 119 | +- **Persistence**: State management and restoration |
| 120 | +- **I/O Coordination**: Stream management and error handling |
| 121 | + |
| 122 | +## Data Flow Overview |
| 123 | + |
| 124 | +```mermaid |
| 125 | +sequenceDiagram |
| 126 | + participant ES as Elasticsearch |
| 127 | + participant C as Controller |
| 128 | + participant A as Autodetect |
| 129 | + participant M as Model |
| 130 | + participant O as Output |
| 131 | + |
| 132 | + ES->>C: Start job |
| 133 | + C->>A: Spawn process |
| 134 | + A->>M: Initialize model |
| 135 | + ES->>A: Stream data |
| 136 | + A->>M: Process records |
| 137 | + M->>M: Update model |
| 138 | + M->>A: Generate results |
| 139 | + A->>O: Write output |
| 140 | + O->>ES: Return results |
| 141 | + A->>A: Persist state |
| 142 | +``` |
| 143 | + |
| 144 | +## Key Algorithms |
| 145 | + |
| 146 | +### Time Series Anomaly Detection |
| 147 | +- **Statistical Models**: Normal, Poisson, and Gamma distributions |
| 148 | +- **Seasonal Decomposition**: Automatic detection of seasonal patterns |
| 149 | +- **Change Point Detection**: Identification of regime changes |
| 150 | +- **Population Analysis**: Multi-dimensional anomaly detection |
| 151 | + |
| 152 | +### Data Frame Analytics |
| 153 | +- **Boosted Trees**: Gradient boosting for classification and regression |
| 154 | +- **Outlier Detection**: Distance-based and density-based methods |
| 155 | +- **Feature Engineering**: Automatic feature selection and encoding |
| 156 | +- **Cross-Validation**: Model validation and hyperparameter tuning |
| 157 | + |
| 158 | +### PyTorch Integration |
| 159 | +- **Model Loading**: TorchScript model deserialization |
| 160 | +- **Inference Pipeline**: Batch processing and result formatting |
| 161 | +- **Memory Management**: Efficient tensor operations |
| 162 | +- **Security**: Sandboxed execution environment |
| 163 | + |
| 164 | +## Performance Characteristics |
| 165 | + |
| 166 | +- **Memory Efficiency**: Sub-linear memory growth with data size |
| 167 | +- **CPU Optimization**: SIMD operations and parallel processing |
| 168 | +- **I/O Efficiency**: Streaming data processing with minimal buffering |
| 169 | +- **Scalability**: Horizontal scaling through process spawning |
| 170 | + |
| 171 | +## Development Philosophy |
| 172 | + |
| 173 | +The codebase emphasizes: |
| 174 | + |
| 175 | +1. **Correctness**: Extensive testing and validation |
| 176 | +2. **Performance**: Optimized for production workloads |
| 177 | +3. **Maintainability**: Clear interfaces and documentation |
| 178 | +4. **Reliability**: Robust error handling and recovery |
| 179 | +5. **Security**: Sandboxed execution and input validation |
| 180 | + |
| 181 | +## Getting Started |
| 182 | + |
| 183 | +For developers new to the codebase: |
| 184 | + |
| 185 | +1. **Start with Core**: Understand `lib/core/` utilities and abstractions |
| 186 | +2. **Explore Models**: Study `lib/model/` for anomaly detection concepts |
| 187 | +3. **Examine APIs**: Review `lib/api/` for high-level interfaces |
| 188 | +4. **Run Examples**: Use the executables with sample data |
| 189 | +5. **Read Tests**: Unit tests provide excellent usage examples |
| 190 | + |
| 191 | +## Next Steps |
| 192 | + |
| 193 | +- [Architecture Details](02-architecture.md) - Deep dive into system design |
| 194 | +- [Core Libraries](03-core-libraries.md) - Fundamental utilities and abstractions |
| 195 | +- [Mathematical Foundation](04-mathematics.md) - Algorithms and statistical methods |
| 196 | +- [Model Layer](05-model-layer.md) - Anomaly detection implementation |
| 197 | + |
| 198 | + |
0 commit comments