forked from marwinsteiner/hyperopter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parallel_optimizer.py
343 lines (285 loc) · 12.2 KB
/
parallel_optimizer.py
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
"""Parallel optimizer for distributed hyperparameter optimization."""
import multiprocessing as mp
from concurrent.futures import ProcessPoolExecutor, as_completed
from dataclasses import dataclass
from datetime import datetime
from pathlib import Path
from typing import Dict, Any, Optional, List, Tuple, Callable, Union
import os
import time
import numpy as np
from loguru import logger
from results_manager import ResultsManager
@dataclass
class WorkerConfig:
"""Configuration for a worker process."""
worker_id: int
batch_size: int
memory_limit: float
log_dir: Path
class ParallelError(Exception):
"""Base exception class for parallel optimization errors."""
pass
class WorkerInitError(ParallelError):
"""Raised when worker initialization fails."""
pass
class TaskDistributionError(ParallelError):
"""Raised when task distribution fails."""
pass
class ResultCollectionError(ParallelError):
"""Raised when result collection fails."""
pass
class ParallelOptimizer:
"""Manager for parallel hyperparameter optimization."""
def __init__(self,
n_workers: int = None,
batch_size: int = 10,
log_dir: Optional[Union[str, Path]] = None):
"""
Initialize the parallel optimizer.
Args:
n_workers: Number of worker processes (default: CPU count)
batch_size: Number of trials per batch
log_dir: Directory for worker logs
Raises:
ParallelError: If initialization fails
"""
# Validate worker count
if n_workers is not None and n_workers < 1:
raise ParallelError("Number of workers must be positive")
# Validate batch size
if batch_size < 1:
raise ParallelError("Batch size must be positive")
self.n_workers = n_workers or mp.cpu_count()
self.batch_size = batch_size
self.log_dir = Path(log_dir) if log_dir else Path.cwd() / "worker_logs"
# Create components
try:
self.results_manager = ResultsManager(
output_dir=self.log_dir
)
# Create log directory
self.log_dir.mkdir(parents=True, exist_ok=True)
# Initialize worker pool
self.worker_configs = [
WorkerConfig(
worker_id=i,
batch_size=self.batch_size,
memory_limit=0.0,
log_dir=self.log_dir
)
for i in range(self.n_workers)
]
logger.info(f"Initialized parallel optimizer with {self.n_workers} workers")
except Exception as e:
logger.error(f"Failed to initialize parallel optimizer: {str(e)}")
raise ParallelError(f"Initialization failed: {str(e)}")
def _worker_setup(self, config: WorkerConfig) -> None:
"""
Setup function for worker processes.
Args:
config: Worker configuration
Raises:
WorkerInitError: If worker setup fails
"""
try:
# Configure worker-specific logger
log_file = config.log_dir / f"worker_{config.worker_id}.log"
logger.add(log_file, format="{time} - {level} - Worker {extra[worker_id]} - {message}")
logger.configure(extra={"worker_id": config.worker_id})
# Set process name for monitoring
mp.current_process().name = f"worker_{config.worker_id}"
logger.info("Worker setup complete")
except Exception as e:
logger.error(f"Worker setup failed: {str(e)}")
raise WorkerInitError(f"Worker {config.worker_id} setup failed: {str(e)}")
def _worker_cleanup(self, config: WorkerConfig) -> None:
"""
Cleanup function for worker processes.
Args:
config: Worker configuration
"""
try:
logger.info("Worker cleanup started")
# Remove worker-specific handlers
logger.remove()
except Exception as e:
logger.error(f"Worker cleanup failed: {str(e)}")
def _execute_batch(self,
config: WorkerConfig,
objective_fn: Callable,
trials: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
"""
Execute a batch of trials on a worker.
Args:
config: Worker configuration
objective_fn: Objective function to optimize
trials: List of trial configurations
Returns:
List of trial results
Raises:
TaskDistributionError: If batch execution fails
"""
try:
self._worker_setup(config)
results = []
for trial in trials:
start_time = time.time()
try:
# Call the objective function with parameters
result = objective_fn(trial["parameters"])
# Record successful result
results.append({
"trial_id": trial["trial_id"],
"parameters": trial["parameters"],
"result": float(result), # Ensure result is numeric
"status": "completed",
"duration": time.time() - start_time,
"worker_id": config.worker_id,
"error": None
})
except Exception as e:
logger.error(f"Trial {trial['trial_id']} failed: {str(e)}")
results.append({
"trial_id": trial["trial_id"],
"parameters": trial["parameters"],
"result": None,
"status": "failed",
"duration": time.time() - start_time,
"worker_id": config.worker_id,
"error": str(e)
})
return results
except Exception as e:
logger.error(f"Batch execution failed: {str(e)}")
raise TaskDistributionError(f"Batch execution failed: {str(e)}")
finally:
self._worker_cleanup(config)
def optimize(self,
objective_fn: Callable,
parameter_space: Dict[str, Any],
n_trials: int,
timeout: Optional[float] = None) -> Dict[str, Any]:
"""
Run parallel optimization.
Args:
objective_fn: Function to optimize
parameter_space: Dictionary defining parameter search space
n_trials: Number of trials to run
timeout: Optional timeout in seconds
Returns:
Dictionary containing optimization results
Raises:
ParallelError: If optimization fails
"""
start_time = time.time()
active_workers = []
try:
# Generate trial configurations
trials = []
for i in range(n_trials):
params = {}
for k, v in parameter_space.items():
if isinstance(v, dict):
if v["type"] == "int":
start, end = v["range"]
step = v.get("step", 1)
value = np.random.randint(start, end + 1)
params[k] = value
else:
params[k] = v["range"][0] # Default to first value
else:
params[k] = v
trials.append({
"trial_id": i,
"parameters": params
})
# Split trials into batches
batches = [
trials[i:i + self.batch_size]
for i in range(0, len(trials), self.batch_size)
]
# Execute batches in parallel
with ProcessPoolExecutor(max_workers=self.n_workers) as executor:
future_to_batch = {
executor.submit(
self._execute_batch,
config,
objective_fn,
batch
): batch
for config, batch in zip(self.worker_configs, batches)
}
# Collect results
all_results = []
for future in as_completed(future_to_batch, timeout=timeout):
batch = future_to_batch[future]
try:
results = future.result(timeout=timeout)
all_results.extend(results)
# Update results manager
self.results_manager.add_batch_results([
{
"trial_id": r["trial_id"],
"parameters": r["parameters"],
"metrics": {"result": r["result"]} if r["status"] == "completed"
else {"error": r["error"]}
}
for r in results
])
except TimeoutError as e:
msg = "Trial execution timeout"
logger.error(msg)
raise ParallelError(msg) from e
except Exception as e:
logger.error(f"Batch processing failed: {str(e)}")
# Mark all trials in batch as failed
failed_results = [
{
"trial_id": t["trial_id"],
"parameters": t["parameters"],
"error": str(e),
"status": "failed",
"duration": time.time() - start_time
}
for t in batch
]
all_results.extend(failed_results)
# Generate optimization summary
completed_trials = [r for r in all_results if r["status"] == "completed"]
best_trial = max(completed_trials, key=lambda x: x["result"]) if completed_trials else None
summary = {
"status": "completed",
"total_trials": n_trials,
"completed_trials": len(completed_trials),
"failed_trials": len(all_results) - len(completed_trials),
"duration": time.time() - start_time,
"best_trial": best_trial,
"trials": all_results
}
logger.info(f"Optimization completed: {len(completed_trials)}/{n_trials} trials successful")
return summary
except Exception as e:
logger.error(f"Optimization failed: {str(e)}")
raise ParallelError(f"Optimization failed: {str(e)}")
def get_worker_status(self) -> Dict[str, Any]:
"""
Get status of worker processes.
Returns:
Dictionary containing worker status information
"""
try:
return {
"error": None,
"timestamp": datetime.now(),
"worker_metrics": {},
"total_workers": self.n_workers
}
except Exception as e:
logger.error(f"Failed to get worker status: {str(e)}")
return {
"error": str(e),
"timestamp": datetime.now(),
"worker_metrics": {},
"total_workers": self.n_workers
}