MLOps Diagnostics & Observability
Relevant source files
The following files were used as context for generating this wiki page:The MLOps Diagnostics and Observability system in monGARS provides a suite of tools for monitoring, profiling, and analyzing the runtime behavior of the application, with a strong focus on machine learning components. It is designed to ensure stability, optimize performance, and provide crucial feedback for the project's self-improvement and reinforcement learning loops. The system includes a command-line interface for on-demand analysis, specialized collectors for hardware metrics (like CUDA), and an integrated observability manager for continuous monitoring within the core application.
This system is a key part of the project's commitment to operational excellence, supporting goals outlined in the project's Roadmap and Contribution Guidelines. It provides telemetry through OpenTelemetry and structured logs, which are essential for debugging and performance tuning in both development and production environments.
Sources: README.md:195-200, AGENTS.md:70-73
Architecture Overview
The diagnostics system is composed of a user-facing CLI and several backend modules that perform specific analyses. The ReinforcementObservabilityManager integrates these diagnostic capabilities directly into the core reinforcement learning cycle, creating a feedback loop for performance and behavior.
The following diagram illustrates the high-level architecture of the diagnostics and observability components.
Diagnostics CLI
A command-line interface, built with Typer, serves as the primary entry point for running manual diagnostic checks.
Sources: monGARS/mlops/diagnostics/cli.py
Commands
The CLI provides commands to invoke the various diagnostic tools.
| Command | Description | Target Module |
|---|---|---|
run-analysis | Profiles a specified Python function using cProfile to identify performance bottlenecks. | monGARS.mlops.diagnostics.analysis.PerformanceAnalyzer |
cuda-metrics | Collects and displays detailed information and real-time stats about available NVIDIA GPUs. | monGARS.mlops.diagnostics.cuda_metrics.CUDAMetricsCollector |
diagnose-unsloth | Runs a separate script to check the Unsloth library installation, compatibility, and performance. | scripts.diagnose_unsloth |
Sources: monGARS/mlops/diagnostics/cli.py:14-48
Command Flow
The diagram below shows how the CLI dispatches commands to the appropriate diagnostic modules.
Performance Analysis
The PerformanceAnalyzer class provides a way to profile any synchronous or asynchronous Python function to understand its performance characteristics. It uses the standard cProfile and pstats libraries.
Sources: monGARS/mlops/diagnostics/analysis.py
Key Components
PerformanceAnalyzer: The main class that orchestrates the profiling.run(): The entry point method that executes the function undercProfile._profile_function(): A wrapper that handles the actual execution of the target function._generate_report(): Formats the profiling statistics frompstatsinto a human-readable string, sorted by cumulative time.
The following sequence diagram shows the process of profiling a function.
CUDA Metrics
The CUDAMetricsCollector is responsible for gathering detailed information about NVIDIA GPUs and their CUDA environment. It relies on the pynvml library, a Python binding for the NVIDIA Management Library (NVML).
Sources: monGARS/mlops/diagnostics/cuda_metrics.py
Class Structure
The collector is designed to be self-contained and gracefully handle environments where CUDA or NVML are not available.
Collected Metrics
The collect_metrics method returns a list of CUDAMetrics objects, one for each detected GPU. The following metrics are collected:
| Metric | Description | Source |
|---|---|---|
gpu_name | The official name of the GPU model. | pynvml.nvmlDeviceGetName |
driver_version | The version of the installed NVIDIA driver. | pynvml.nvmlSystemGetDriverVersion |
cuda_version | The version of the CUDA toolkit reported by the driver. | pynvml.nvmlDeviceGetCudaComputeCapability |
memory_total_mb | Total installed GPU memory in megabytes. | nvmlDeviceGetMemoryInfo |
memory_free_mb | Unallocated GPU memory in megabytes. | nvmlDeviceGetMemoryInfo |
memory_used_mb | Allocated GPU memory in megabytes. | nvmlDeviceGetMemoryInfo |
temperature | Current GPU die temperature in Celsius. | nvmlDeviceGetTemperature |
power_draw | Current GPU power consumption in watts. | nvmlDeviceGetPowerUsage |
utilization_gpu | GPU utilization rate as a percentage. | nvmlDeviceGetUtilizationRates |
utilization_memory | Memory controller utilization rate as a percentage. | nvmlDeviceGetUtilizationRates |
pytorch_cuda_version | The CUDA version PyTorch was compiled with. | torch.version.cuda |
pytorch_cudnn_version | The cuDNN version PyTorch is using. | torch.backends.cudnn.version |
Sources: monGARS/mlops/diagnostics/cuda_metrics.py:14-41, 69-116
Unsloth Diagnostics
A dedicated script, diagnose_unsloth.py, is provided to validate and benchmark the environment for the Unsloth library, a tool for accelerating LLM inference.
Sources: scripts/diagnose_unsloth.py
Diagnostic Flow
The script performs a series of checks and, if successful, runs an inference benchmark to measure performance.
Sources: scripts/diagnose_unsloth.py:64-88
Key Functions
check_unsloth_installation(): Attempts to importunslothand checks the availability of thetritoncompiler, printing status messages for each.run_inference_benchmark():- Loads a pre-defined model (
unsloth/mistral-7b-v0.1-bnb-4bit) usingFastLanguageModel. - Runs a warm-up inference pass.
- Executes a timed inference loop to generate a set number of tokens.
- Calculates and returns the performance in tokens per second.
- Loads a pre-defined model (
main(): Orchestrates the diagnostic process, calling the check and benchmark functions and printing a final summary report.
Sources: scripts/diagnose_unsloth.py:18-62
Reinforcement Learning Observability
The ReinforcementObservabilityManager is a core component that integrates system and hardware diagnostics into the reinforcement learning (RL) feedback loop. It collects a snapshot of the system's state, which can be used to correlate performance with RL outcomes.
Sources: monGARS/core/reinforcement_observability.py
Architecture
The manager collects data from various sources, including psutil for system stats and the CUDAMetricsCollector for GPU stats. It then uses a configurable ObservabilityEmitter to dispatch the collected payload to a monitoring backend.
Data Collection and Emission Flow
The collect_observation method is the primary function for gathering a state snapshot. The collected data is then passed to the emitter.
Data Structures
The observability payload is composed of several structured dataclasses.
SystemStats
| Field | Type | Description |
|---|---|---|
cpu_percent | float | System-wide CPU utilization as a percentage. |
memory_percent | float | System-wide RAM utilization as a percentage. |
disk_percent | float | Disk usage of the root partition as a percentage. |
| Sources: monGARS/core/reinforcement_observability.py:16-21 |
EnvironmentInfo
| Field | Type | Description |
|---|---|---|
python_version | str | The version of the running Python interpreter. |
platform | str | The platform identifier (e.g., 'Linux-x86_64'). |
| Sources: monGARS/core/reinforcement_observability.py:24-28 |
ObservabilityPayload
| Field | Type | Description |
|---|---|---|
timestamp | datetime | UTC timestamp of when the observation was collected. |
system_stats | SystemStats | A snapshot of system resource usage. |
cuda_metrics | list[CUDAMetrics] | A list of metrics for each available GPU. |
environment_info | EnvironmentInfo | Information about the runtime environment. |
event_name | str | An optional name for the event being recorded. |
event_data | dict | Optional arbitrary data associated with the event. |
| Sources: monGARS/core/reinforcement_observability.py:31-39 |
Conclusion
The MLOps Diagnostics and Observability features in monGARS form a comprehensive toolkit for ensuring the health and performance of the system. Through a combination of a flexible CLI for manual checks and an integrated manager for automated data collection, developers and operators can gain deep insights into resource utilization, performance bottlenecks, and the behavior of the reinforcement learning system. This infrastructure is vital for the project's goals of stability, performance, and enabling advanced self-improvement capabilities.