File size: 11,823 Bytes
1a4aa87 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 | """
Resource Allocator for Adaptive GPU Allocation
Handles intelligent resource allocation:
- GPU allocation decisions
- Node pool selection
- Spot vs on-demand selection
- Batch size optimization
- Inference mode selection
Supports adaptive resource allocation based on cluster load.
"""
import uuid
from dataclasses import dataclass
from datetime import datetime
from enum import Enum
from typing import Any, Dict, List, Optional
from pydantic import BaseModel
class InferenceMode(str, Enum):
"""Inference modes with different resource requirements."""
LIGHTWEIGHT = "lightweight" # Fast, less accurate
STANDARD = "standard" # Balanced
FULL = "full" # Complete, more accurate
class NodePoolType(str, Enum):
"""Node pool types for different workloads."""
CPU = "cpu"
GPU_STANDARD = "gpu_standard"
GPU_HIGH_MEMORY = "gpu_high_memory"
GPU_AMPERE = "gpu_ampere"
GPU_HOPPER = "gpu_hopper"
class AllocationDecision(BaseModel):
"""Resource allocation decision for a job."""
job_id: uuid.UUID
# Allocation details
gpu_count: int = 1
gpu_type: str = "v100"
node_pool: str = "gpu_standard"
inference_mode: str = "standard"
batch_size: int = 4
# Spot vs on-demand
use_spot: bool = True
# Optimization hints
enable_batching: bool = False
reduce_mutation_depth: bool = False
use_lightweight_hallucination: bool = False
# Metadata
allocated_at: datetime = None
def __init__(self, **data):
if "allocated_at" not in data or data["allocated_at"] is None:
data["allocated_at"] = datetime.utcnow()
super().__init__(**data)
class ResourceAllocator:
"""
Intelligent resource allocator for evaluation jobs.
Makes allocation decisions based on:
- Job requirements (GPU count, memory)
- Cluster state (available resources)
- Cost optimization (spot vs on-demand)
- Priority (high priority = better resources)
"""
# Default configurations
DEFAULT_BATCH_SIZE = 4
DEFAULT_GPU_COUNT = 1
# Node pool specifications
NODE_POOL_SPECS = {
NodePoolType.CPU: {
"gpu_count": 0,
"memory_gb": 32,
"cost_per_hour": 0.50,
},
NodePoolType.GPU_STANDARD: {
"gpu_count": 1,
"gpu_type": "v100",
"memory_gb": 60,
"cost_per_hour": 2.48,
},
NodePoolType.GPU_HIGH_MEMORY: {
"gpu_count": 1,
"gpu_type": "v100",
"memory_gb": 120,
"cost_per_hour": 3.50,
},
NodePoolType.GPU_AMPERE: {
"gpu_count": 1,
"gpu_type": "a100",
"memory_gb": 80,
"cost_per_hour": 3.67,
},
NodePoolType.GPU_HOPPER: {
"gpu_count": 1,
"gpu_type": "h100",
"memory_gb": 160,
"cost_per_hour": 6.50,
},
}
def __init__(
self,
default_node_pool: NodePoolType = NodePoolType.GPU_STANDARD,
enable_spot_by_default: bool = True,
):
"""
Initialize resource allocator.
Args:
default_node_pool: Default node pool type
enable_spot_by_default: Use spot instances by default
"""
self.default_node_pool = default_node_pool
self.enable_spot_by_default = enable_spot_by_default
# Cluster state (would be updated from monitoring)
self._cluster_load: float = 0.0
self._available_gpu_count: int = 0
def allocate_resources(
self,
job_id: uuid.UUID,
total_samples: int,
priority_score: float = 0.5,
required_gpu_memory_mb: int = 0,
model_size: str = "7b",
cluster_load: Optional[float] = None,
available_gpus: Optional[int] = None,
) -> AllocationDecision:
"""
Determine resource allocation for a job.
Args:
job_id: Unique job identifier
total_samples: Number of samples to process
priority_score: Job priority score (0-1)
required_gpu_memory_mb: Required GPU memory in MB
model_size: Model size (7b, 13b, 30b, 70b)
cluster_load: Current cluster load (0-1)
available_gpus: Number of available GPUs
Returns:
AllocationDecision with resource allocation details
"""
# Use provided cluster state or defaults
load = cluster_load if cluster_load is not None else self._cluster_load
gpus = available_gpus if available_gpus is not None else self._available_gpu_count
# Determine inference mode based on priority and load
inference_mode = self._determine_inference_mode(priority_score, load)
# Determine batch size
batch_size = self._determine_batch_size(
total_samples, load, inference_mode
)
# Determine GPU type and node pool
gpu_type, node_pool = self._determine_gpu_and_pool(
model_size, required_gpu_memory_mb
)
# Determine spot vs on-demand
use_spot = self._determine_use_spot(priority_score, load)
# Determine optimization flags
enable_batching = self._should_enable_batching(load, total_samples)
reduce_mutation_depth = self._should_reduce_mutation_depth(
priority_score, load
)
use_lightweight_hallucination = self._should_use_lightweight_hallucination(
load
)
return AllocationDecision(
job_id=job_id,
gpu_count=self.DEFAULT_GPU_COUNT,
gpu_type=gpu_type,
node_pool=node_pool.value,
inference_mode=inference_mode.value,
batch_size=batch_size,
use_spot=use_spot,
enable_batching=enable_batching,
reduce_mutation_depth=reduce_mutation_depth,
use_lightweight_hallucination=use_lightweight_hallucination,
)
def _determine_inference_mode(
self,
priority_score: float,
cluster_load: float,
) -> InferenceMode:
"""Determine inference mode based on priority and load."""
# High priority jobs get full mode
if priority_score >= 0.7:
return InferenceMode.FULL
# Low load allows full mode
if cluster_load < 0.5:
return InferenceMode.FULL
# Medium load - use standard
if cluster_load < 0.8:
return InferenceMode.STANDARD
# High load - use lightweight
return InferenceMode.LIGHTWEIGHT
def _determine_batch_size(
self,
total_samples: int,
cluster_load: float,
inference_mode: InferenceMode,
) -> int:
"""Determine optimal batch size."""
if inference_mode == InferenceMode.LIGHTWEIGHT:
# Lightweight mode allows larger batches
if cluster_load < 0.5:
return min(16, max(4, total_samples // 10))
return min(8, max(2, total_samples // 20))
if inference_mode == InferenceMode.FULL:
# Full mode requires smaller batches
return min(4, max(1, total_samples // 50))
# Standard mode
return min(8, max(2, total_samples // 25))
def _determine_gpu_and_pool(
self,
model_size: str,
required_memory_mb: int,
) -> tuple[str, NodePoolType]:
"""Determine GPU type and node pool."""
# Map model size to requirements
model_requirements = {
"7b": {"gpu_type": "v100", "memory_gb": 16},
"13b": {"gpu_type": "v100", "memory_gb": 30},
"30b": {"gpu_type": "a100", "memory_gb": 60},
"70b": {"gpu_type": "a100", "memory_gb": 120},
}
req = model_requirements.get(model_size, model_requirements["7b"])
# Check if more memory is required
if required_memory_mb > req["memory_gb"] * 1024:
return (req["gpu_type"], NodePoolType.GPU_HIGH_MEMORY)
# Select node pool based on GPU type
if req["gpu_type"] == "h100":
return (req["gpu_type"], NodePoolType.GPU_HOPPER)
elif req["gpu_type"] == "a100":
return (req["gpu_type"], NodePoolType.GPU_AMPERE)
else:
return (req["gpu_type"], NodePoolType.GPU_STANDARD)
def _determine_use_spot(
self,
priority_score: float,
cluster_load: float,
) -> bool:
"""Determine whether to use spot instances."""
# Don't use spot for critical jobs
if priority_score >= 0.9:
return False
# Use spot by default if enabled
if self.enable_spot_by_default:
# But avoid spot during high load (may get preempted)
if cluster_load > 0.9:
return False
return True
return False
def _should_enable_batching(
self,
cluster_load: float,
total_samples: int,
) -> bool:
"""Determine if batching should be enabled."""
# Enable for large jobs when load is moderate
return cluster_load < 0.7 and total_samples > 50
def _should_reduce_mutation_depth(
self,
priority_score: float,
cluster_load: float,
) -> bool:
"""Determine if mutation depth should be reduced."""
# Reduce for low priority jobs or high load
return priority_score < 0.4 or cluster_load > 0.8
def _should_use_lightweight_hallucination(
self,
cluster_load: float,
) -> bool:
"""Determine if lightweight hallucination detection should be used."""
return cluster_load > 0.85
def update_cluster_state(
self,
cluster_load: float,
available_gpu_count: int,
):
"""
Update cluster state for allocation decisions.
Args:
cluster_load: Current cluster load (0-1)
available_gpu_count: Number of available GPUs
"""
self._cluster_load = cluster_load
self._available_gpu_count = available_gpu_count
def get_allocation_cost_per_hour(
self,
allocation: AllocationDecision,
) -> float:
"""Calculate hourly cost for an allocation decision."""
node_spec = self.NODE_POOL_SPECS.get(
NodePoolType(allocation.node_pool),
self.NODE_POOL_SPECS[NodePoolType.GPU_STANDARD]
)
base_cost = node_spec.get("cost_per_hour", 2.48)
# Apply spot discount if using spot
if allocation.use_spot:
base_cost *= 0.35 # ~65% discount
return base_cost
# Global instance
_resource_allocator: Optional[ResourceAllocator] = None
def get_resource_allocator() -> ResourceAllocator:
"""Get or create the global ResourceAllocator instance."""
global _resource_allocator
if _resource_allocator is None:
_resource_allocator = ResourceAllocator()
return _resource_allocator
__all__ = [
"ResourceAllocator",
"AllocationDecision",
"InferenceMode",
"NodePoolType",
"get_resource_allocator",
]
|