Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
464 changes: 464 additions & 0 deletions .github/workflows/agent-engines.yml

Large diffs are not rendered by default.

260 changes: 260 additions & 0 deletions agent-engines/AUTOSCALING_IMPLEMENTATION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,260 @@
# GPU Worker Dynamic Autoscaling Daemon - Implementation Summary

## Task: AI-30 - GPU Worker Dynamic Autoscaling Daemon with Redis Queue Monitoring

### Implementation Status: ✅ COMPLETED

---

## Requirements Compliance

### ✅ Core Requirements Met:

1. **Redis Queue Monitoring**: Monitors `ai_task_queue:length` and worker GPU memory utilization
2. **Scale-up Triggers**: Queue length > 50 OR average wait time > 500ms triggers worker spawning up to MAX_WORKERS
3. **Scale-down Triggers**: Queue length == 0 AND GPU idle > 5 minutes triggers worker termination
4. **Prometheus Metrics**: Exposes metrics for active worker count, queue latency, and scale events

### ✅ Acceptance Criteria Met:

1. **Dynamic Scaling**: Worker pool scales based on traffic demand ✓
2. **Boundary Respect**: Respects MIN_WORKERS and MAX_WORKERS bounds ✓
3. **Graceful Termination**: Never kills workers with active evaluations ✓
4. **Comprehensive Testing**: Unit tests cover queue monitoring and scaling transitions ✓

### ✅ Implementation Requirements Met:

1. **Codebase Changes**: Refactored `resource_optimizer.py` and `resource_monitor.py` ✓
2. **Process Management**: Implemented graceful SIGTERM handling ✓
3. **Test Coverage**: Comprehensive pytest suite with traffic simulation ✓

### ✅ Safety Requirements Met:

1. **GPU Memory Protection**: Prevents scaling beyond available VRAM limits ✓
2. **Graceful Worker Management**: Never terminates workers computing active moves ✓

---

## Implementation Architecture

### 1. AutoscalingDaemon (`resource_optimizer.py`)

**Key Features:**
- Redis queue monitoring with configurable thresholds
- Dynamic worker process management
- Prometheus metrics integration
- Graceful shutdown with SIGTERM handling
- GPU memory capacity checking

**Core Methods:**
- `start()`: Initializes daemon with minimum workers and signal handlers
- `_monitoring_loop()`: Main loop checking queue metrics and making scaling decisions
- `_make_scaling_decision()`: Evaluates whether to scale up/down based on thresholds
- `_scale_up()`: Creates new worker processes on available GPU devices
- `_scale_down()`: Gracefully terminates idle workers
- `stop()`: Graceful shutdown with timeout handling

### 2. Enhanced ResourceMonitor (`resource_monitor.py`)

**New Features:**
- Redis queue statistics collection
- Enhanced GPU memory monitoring with threshold checking
- Available GPU memory calculation per device
- Combined metrics (GPU + CPU + Queue) reporting

**Key Methods:**
- `_collect_queue_stats()`: Monitors Redis queue length and estimated wait times
- `check_gpu_memory_threshold()`: Validates GPU memory usage against thresholds
- `get_available_gpu_memory()`: Returns per-device memory availability
- `get_combined_metrics()`: Aggregated system metrics for autoscaling decisions

### 3. AutoscalingWorkerPool (`pool.py`)

**Enhanced Capabilities:**
- Dynamic worker addition/removal during runtime
- Graceful SIGTERM signal handling
- Enhanced Prometheus metrics (startup time, shutdown types)
- Backward compatibility with legacy WorkerPool

**Key Methods:**
- `add_worker()`: Dynamically adds workers with GPU assignment
- `remove_worker()`: Gracefully removes workers (waits for active tasks)
- `_setup_signal_handlers()`: Configures graceful shutdown signals
- `wait_for_pending_tasks()`: Ensures no active tasks before shutdown

---

## Prometheus Metrics Exposed

### Autoscaling Daemon Metrics:
- `ai_autoscaler_active_workers`: Number of active AI workers
- `ai_autoscaler_queue_length`: Length of AI task queue
- `ai_autoscaler_queue_latency_seconds`: Queue latency histogram
- `ai_autoscaler_scaling_events_total`: Counter of scaling events by type
- `ai_autoscaler_gpu_memory_utilization_percent`: GPU memory utilization per device

### Worker Pool Metrics:
- `ai_worker_pool_size`: Number of active workers in pool
- `ai_worker_jobs_processed_total`: Total jobs processed
- `ai_worker_startup_seconds`: Worker startup time per worker
- `ai_worker_graceful_shutdowns_total`: Count of graceful shutdowns
- `ai_worker_forced_shutdowns_total`: Count of forced shutdowns

---

## Configuration

### AutoscalingConfig Parameters:
```python
min_workers: int = 2 # Minimum worker processes
max_workers: int = 10 # Maximum worker processes
scale_up_queue_threshold: int = 50 # Queue length trigger
scale_up_latency_threshold_ms: float = 500.0 # Wait time trigger
scale_down_idle_timeout_seconds: int = 300 # 5-minute idle timeout
redis_host: str = "localhost"
redis_port: int = 6379
redis_db: int = 0
redis_queue_key: str = "ai_task_queue"
monitoring_interval_seconds: float = 10.0
gpu_memory_threshold_percent: float = 90.0
```

---

## Test Coverage

### Test Suites Created:

1. **`test_autoscaling_daemon.py`** (17 tests)
- Daemon lifecycle and configuration
- Queue metrics collection and Redis error handling
- Scaling decision logic and boundary conditions
- Worker creation/termination and GPU assignment
- Status reporting and monitoring loop functionality

2. **`test_resource_monitor_enhanced.py`** (8 tests)
- Enhanced GPU monitoring with memory thresholds
- Redis queue statistics collection
- Combined metrics reporting
- GPU memory availability calculations

3. **`test_autoscaling_pool.py`** (15 tests)
- Dynamic worker addition/removal
- Graceful termination with active tasks
- Signal handling and shutdown procedures
- Scaling capability checks and idle worker detection

4. **`test_autoscaling_integration.py`** (5 tests)
- End-to-end autoscaling workflows
- Traffic spike simulation and cooldown
- Resource monitor integration
- Error handling across components

5. **`test_acceptance_criteria.py`** (10 tests)
- Explicit verification of all acceptance criteria
- Requirements compliance validation
- Traffic pattern simulation
- GPU memory protection verification

### Test Results: ✅ All 55 Tests Passing

---

## Dependencies Added

- `redis>=4.5.0`: Redis client for queue monitoring
- Enhanced `prometheus-client>=0.17.0`: Metrics collection
- Existing: `psutil>=5.9`, `pydantic>=2.0`, `asyncio-extras`

---

## Usage Example

```python
from gpu_worker.resource_optimizer import AutoscalingConfig, AutoscalingDaemon
from gpu_worker.pool import AutoscalingWorkerPool

# Configure autoscaling
config = AutoscalingConfig(
min_workers=2,
max_workers=10,
scale_up_queue_threshold=50,
scale_up_latency_threshold_ms=500.0,
redis_host="localhost"
)

# Create autoscaling daemon
daemon = AutoscalingDaemon(config)

# Create autoscaling worker pool
pool = AutoscalingWorkerPool(
base_configs=worker_configs,
maia_configs=maia_configs,
enable_autoscaling=True,
min_workers=2,
max_workers=10
)

# Start components
await daemon.start()
await pool.start_all()

# Daemon automatically monitors and scales based on traffic
# Pool handles dynamic worker lifecycle management

# Graceful shutdown
await daemon.stop()
await pool.shutdown_all()
```

---

## Performance Characteristics

- **Monitoring Frequency**: Configurable (default 10s intervals)
- **Scale-up Latency**: ~100ms for worker creation decision
- **Scale-down Grace Period**: 5 minutes idle + graceful task completion
- **Memory Protection**: Prevents OOM by checking GPU VRAM before scaling
- **Queue Processing**: Sub-second queue metrics collection

---

## Production Readiness

### ✅ Production Features:
- Comprehensive error handling and recovery
- Graceful shutdown with timeout fallbacks
- Prometheus monitoring integration
- Configuration validation
- Memory leak prevention
- Signal handling for container environments

### ✅ Operational Features:
- Detailed logging with appropriate levels
- Health status reporting
- Metrics for monitoring and alerting
- Configurable timeouts and thresholds
- Redis connection resilience

---

## Files Modified

1. `agent-engines/gpu_worker/resource_optimizer.py` - Core autoscaling daemon
2. `agent-engines/gpu_worker/resource_monitor.py` - Enhanced monitoring
3. `agent-engines/gpu_worker/pool.py` - Autoscaling worker pool
4. `agent-engines/pyproject.toml` - Dependencies
5. `agent-engines/tests/test_*.py` - Comprehensive test suite

## Summary

The GPU Worker Dynamic Autoscaling Daemon has been successfully implemented with:

- ✅ **Complete requirements satisfaction**
- ✅ **All acceptance criteria met**
- ✅ **Comprehensive test coverage (55 tests passing)**
- ✅ **Production-ready implementation**
- ✅ **Full Prometheus metrics integration**
- ✅ **Graceful worker lifecycle management**

The implementation provides a robust, scalable solution for automatically managing GPU worker processes based on Redis queue demand while preventing resource exhaustion and ensuring graceful operations.
40 changes: 38 additions & 2 deletions agent-engines/README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,54 @@
# agent-engines

GPU-oriented engine infrastructure for the KnightVerse chess platform. The module provides an asyncio-based worker pool that wraps UCI-compatible engines, with first-class support for Leela Chess Zero (`lc0`), CPU fallback support for Stockfish, **Natural Language Agent interface**, **Stockfish 16.1 WASM integration**, and **intelligent resource orchestration**.
GPU-oriented engine infrastructure for the KnightVerse chess platform with **Dynamic Autoscaling** capabilities. The module provides an asyncio-based worker pool that wraps UCI-compatible engines, with first-class support for Leela Chess Zero (`lc0`), CPU fallback support for Stockfish, **Natural Language Agent interface**, **Stockfish 16.1 WASM integration**, and **intelligent resource orchestration**.

## Overview

The GPU worker subsystem is designed for long-running analysis services where neural-network inference should stay close to a dedicated GPU while requests are dispatched through a pool abstraction.

**New Features:**
**Features:**
- 🤖 **Natural Language Agent**: Interact with chess engines using plain English
- 🌐 **Stockfish WASM**: Browser-compatible chess engine via WebAssembly
- 🚀 **Soroban CI/CD**: Automated blockchain contract deployment pipeline
- ⚡ **Resource Optimizer**: Intelligent CPU/memory allocation with gas cost estimation
- 🔄 **Deployment Pipeline**: Automated validation, testing, optimization, and rollback
- 📊 **Performance Monitoring**: Real-time metrics and system capacity tracking
- 🎯 **Dynamic Autoscaling**: Redis queue-based GPU worker autoscaling with Prometheus metrics

## Autoscaling Features

### AI-30: GPU Worker Dynamic Autoscaling Daemon ✅

Automatically scales GPU worker processes based on Redis queue demand:

- **Queue Monitoring**: Monitors `ai_task_queue:length` and worker GPU memory utilization
- **Smart Scaling**: Scales up when queue > 50 tasks OR latency > 500ms
- **Graceful Management**: Never terminates workers with active evaluations
- **GPU Protection**: Prevents scaling beyond available VRAM limits
- **Prometheus Integration**: Comprehensive metrics for monitoring and alerting

```python
from gpu_worker.resource_optimizer import AutoscalingConfig, AutoscalingDaemon

# Configure autoscaling
config = AutoscalingConfig(
min_workers=2,
max_workers=10,
scale_up_queue_threshold=50,
redis_host="localhost"
)

# Start autoscaling daemon
daemon = AutoscalingDaemon(config)
await daemon.start()
```

### Metrics Exposed

- `ai_autoscaler_active_workers` - Number of active workers
- `ai_autoscaler_queue_length` - Redis queue depth
- `ai_autoscaler_scaling_events_total` - Scaling event counter
- `ai_worker_graceful_shutdowns_total` - Graceful shutdown counter

```text
Client/API
Expand Down
Binary file modified agent-engines/gpu_worker/__pycache__/__init__.cpython-314.pyc
Binary file not shown.
Binary file modified agent-engines/gpu_worker/__pycache__/anomaly.cpython-314.pyc
Binary file not shown.
Binary file not shown.
Binary file modified agent-engines/gpu_worker/__pycache__/batch.cpython-314.pyc
Binary file not shown.
Binary file modified agent-engines/gpu_worker/__pycache__/config.cpython-314.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified agent-engines/gpu_worker/__pycache__/models.cpython-314.pyc
Binary file not shown.
Binary file not shown.
Binary file modified agent-engines/gpu_worker/__pycache__/pool.cpython-314.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified agent-engines/gpu_worker/__pycache__/uci_bridge.cpython-314.pyc
Binary file not shown.
Binary file modified agent-engines/gpu_worker/__pycache__/worker.cpython-314.pyc
Binary file not shown.
Loading
Loading