-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunner
More file actions
executable file
·647 lines (538 loc) · 25 KB
/
runner
File metadata and controls
executable file
·647 lines (538 loc) · 25 KB
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
#!/usr/bin/env python3
"""
AFSysBench Runner - Python integration for AlphaFold benchmarking
Updated to work with the new modular architecture
"""
import argparse
import subprocess
import json
import os
import sys
import time
import csv
import shutil
import logging
from datetime import datetime
from pathlib import Path
from typing import Dict, List, Optional, Tuple, Any
from dataclasses import dataclass, field
from concurrent.futures import ThreadPoolExecutor, as_completed
@dataclass
class BenchmarkConfig:
"""Configuration for AlphaFold benchmarking"""
# System information
system_name: str = "my_system"
system_type: str = "workstation"
cpu_architecture: str = "amd"
# Paths
db_dir: str = ""
model_dir: str = ""
docker_image: str = "alphafold3"
# Input/Output directories
msa_input_dir: str = "input_msa"
inference_input_dir: str = "input_inference"
msa_output_base: str = "output_msa"
inference_output_base: str = "output_inference"
# Benchmark settings
thread_counts: List[int] = field(default_factory=lambda: [4, 8, 16])
profiling_mode: bool = False
profiling_tool: str = ""
run_purpose: str = "performance"
# Docker resources
docker_memory: str = "32g"
docker_shm_size: str = "8g"
docker_cpus: Optional[int] = None
# Performance settings
system_monitor: bool = True
gpu_monitor: bool = True
memory_monitor: bool = True
# Model generation
num_models: int = 5
@classmethod
def from_file(cls, config_path: str) -> 'BenchmarkConfig':
"""Load configuration from shell config file"""
config = cls()
if not os.path.exists(config_path):
raise FileNotFoundError(f"Config file not found: {config_path}")
# Parse shell-style config file
with open(config_path, 'r') as f:
for line in f:
line = line.strip()
if line and not line.startswith('#') and '=' in line:
key, value = line.split('=', 1)
key = key.strip()
# Remove comments first
if '#' in value:
value = value.split('#')[0]
value = value.strip().strip('"').strip("'")
# Map config keys to dataclass attributes
if key == 'SYSTEM_NAME':
config.system_name = value
elif key == 'SYSTEM_TYPE':
config.system_type = value
elif key == 'CPU_ARCHITECTURE':
config.cpu_architecture = value
elif key == 'DB_DIR':
config.db_dir = value
elif key == 'MODEL_DIR':
config.model_dir = value
elif key == 'DOCKER_IMAGE':
config.docker_image = value
elif key == 'MSA_INPUT_DIR':
config.msa_input_dir = value
elif key == 'INFERENCE_INPUT_DIR':
config.inference_input_dir = value
elif key == 'MSA_OUTPUT_BASE':
config.msa_output_base = value
elif key == 'INFERENCE_OUTPUT_BASE':
config.inference_output_base = value
elif key == 'THREAD_COUNTS':
# Value should already be cleaned by the main parser
config.thread_counts = [int(x.strip()) for x in value.split()]
elif key == 'PERF_STAT':
if value.lower() == 'true':
config.profiling_tool = "perf_stat"
config.profiling_mode = True
config.run_purpose = "profiling"
print(f"[CONFIG] Converting PERF_STAT=true to PROFILING_TOOL=perf_stat")
elif key == 'PERF_RECORD':
if value.lower() == 'true':
config.profiling_tool = "perf_record"
config.profiling_mode = True
config.run_purpose = "profiling"
print(f"[CONFIG] Converting PERF_RECORD=true to PROFILING_TOOL=perf_record")
elif key == 'SYSTEM_MONITOR':
config.system_monitor = value.lower() == 'true'
elif key == 'GPU_MONITOR':
config.gpu_monitor = value.lower() == 'true'
elif key == 'MEMORY_MONITOR':
config.memory_monitor = value.lower() == 'true'
elif key == 'DOCKER_MEMORY':
config.docker_memory = value
elif key == 'DOCKER_SHM_SIZE':
config.docker_shm_size = value
elif key == 'DOCKER_CPUS':
config.docker_cpus = int(value) if value else None
elif key == 'NUM_MODELS':
config.num_models = int(value)
return config
class AFBenchRunner:
"""Main benchmark runner class that uses the new modular scripts"""
def __init__(self, config: BenchmarkConfig):
self.config = config
self.logger = self._setup_logging()
self.base_dir = os.path.dirname(os.path.abspath(__file__))
self.scripts_dir = os.path.join(self.base_dir, 'scripts')
# Check if scripts directory exists
if not os.path.exists(self.scripts_dir):
self.logger.warning(f"Scripts directory not found at {self.scripts_dir}, using base directory")
self.scripts_dir = self.base_dir
def _setup_logging(self) -> logging.Logger:
"""Setup logging configuration"""
logger = logging.getLogger('AFBenchRunner')
logger.setLevel(logging.INFO)
# Console handler
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
logger.addHandler(ch)
return logger
def _get_script_path(self, script_name: str) -> str:
"""Get the full path to a script"""
# Map to modular script names
script_mapping = {
'benchmark_msa.sh': 'benchmark_msa_modular.sh',
'benchmark_inference.sh': 'benchmark_inference_modular.sh'
}
# Use modular version if available
if script_name in script_mapping:
modular_name = script_mapping[script_name]
script_path = os.path.join(self.scripts_dir, modular_name)
if os.path.exists(script_path):
return script_path
# First try scripts directory
script_path = os.path.join(self.scripts_dir, script_name)
if os.path.exists(script_path):
return script_path
# Fall back to base directory
script_path = os.path.join(self.base_dir, script_name)
if os.path.exists(script_path):
return script_path
raise FileNotFoundError(f"Script not found: {script_name}")
def _run_command(self, cmd: List[str], cwd: Optional[str] = None,
capture_output: bool = True, env: Optional[Dict] = None) -> subprocess.CompletedProcess:
"""Run a shell command and return the result"""
self.logger.debug(f"Running command: {' '.join(cmd)}")
# Merge environment variables
cmd_env = os.environ.copy()
if env:
cmd_env.update(env)
# Add profiling configuration to environment
if self.config.profiling_mode:
cmd_env['PROFILING_ENABLED'] = 'true'
cmd_env['PROFILING_TOOL'] = self.config.profiling_tool
# Set timeout based on the command type - longer for inference
timeout_seconds = 10000 # Extended timeout (2h 47m)
if any("inference" in str(arg) for arg in cmd):
timeout_seconds = 10000 # Same extended timeout for inference
result = subprocess.run(
cmd,
cwd=cwd or self.base_dir,
capture_output=capture_output,
text=True,
shell=False,
env=cmd_env,
timeout=timeout_seconds
)
if result.returncode != 0:
self.logger.error(f"Command failed: {' '.join(cmd)}")
if capture_output:
self.logger.error(f"STDOUT: {result.stdout}")
self.logger.error(f"STDERR: {result.stderr}")
return result
def run_msa_benchmark(self, input_file: str, thread_counts: Optional[List[int]] = None,
output_dir: Optional[str] = None) -> Dict[str, Any]:
"""Run MSA benchmark using the new modular script"""
self.logger.info(f"Running MSA benchmark for {input_file}")
# Auto-determine input path if not already prefixed
if not input_file.startswith('input_msa/') and not os.path.isabs(input_file):
full_input_path = f"input_msa/{input_file}"
else:
full_input_path = input_file
# Build command
script_path = self._get_script_path('benchmark_msa.sh')
cmd = [script_path, "-c", self.config.__dict__.get('config_file', 'myenv.config')]
# Add thread counts if specified
if thread_counts:
cmd.extend(["-t", " ".join(map(str, thread_counts))])
# Add output directory if specified
if output_dir:
cmd.extend(["-o", output_dir])
# Add input file
cmd.append(full_input_path)
# Run the benchmark
start_time = time.time()
result = self._run_command(cmd)
duration = time.time() - start_time
return {
'input_file': input_file,
'command': ' '.join(cmd),
'duration': duration,
'success': result.returncode == 0,
'stdout': result.stdout if result.returncode == 0 else None,
'stderr': result.stderr if result.returncode != 0 else None,
'run_purpose': self.config.run_purpose
}
def run_inference_benchmark(self, input_file: str, thread_counts: Optional[List[int]] = None,
enable_nsys: bool = False, num_models: Optional[int] = None,
output_dir: Optional[str] = None) -> Dict[str, Any]:
"""Run inference benchmark using the new modular script"""
self.logger.info(f"Running inference benchmark for {input_file}")
# Auto-determine input path if not already prefixed
if not input_file.startswith('input_inference/') and not os.path.isabs(input_file):
full_input_path = f"input_inference/{input_file}"
else:
full_input_path = input_file
# Build command
script_path = self._get_script_path('benchmark_inference.sh')
cmd = [script_path, "-c", self.config.__dict__.get('config_file', 'myenv.config')]
# Add thread counts if specified
if thread_counts:
cmd.extend(["-t", " ".join(map(str, thread_counts))])
# Add NSYS profiling
if enable_nsys:
cmd.append("-n")
# Add model count
if num_models:
cmd.extend(["-m", str(num_models)])
# Add output directory if specified
if output_dir:
cmd.extend(["-o", output_dir])
# Add input file
cmd.append(full_input_path)
# Run the benchmark
start_time = time.time()
result = self._run_command(cmd)
duration = time.time() - start_time
return {
'input_file': input_file,
'command': ' '.join(cmd),
'duration': duration,
'success': result.returncode == 0,
'stdout': result.stdout if result.returncode == 0 else None,
'stderr': result.stderr if result.returncode != 0 else None,
'run_purpose': self.config.run_purpose
}
def run_batch(self, input_dir: str, stage: str = 'msa',
thread_counts: Optional[List[int]] = None,
batch_name: Optional[str] = None) -> Dict[str, Any]:
"""Run batch benchmarks using the new batch_runner.sh script"""
self.logger.info(f"Running batch {stage} benchmarks from {input_dir}")
# Build command
script_path = self._get_script_path('batch_runner.sh')
cmd = [script_path, "-c", self.config.__dict__.get('config_file', 'myenv.config')]
# Add input directory and stage
cmd.extend(["-i", input_dir, "-s", stage])
# Add thread counts if specified
if thread_counts:
cmd.extend(["-t", " ".join(map(str, thread_counts))])
# Add batch name if specified
if batch_name:
cmd.extend(["-n", batch_name])
# Run the batch
start_time = time.time()
result = self._run_command(cmd)
duration = time.time() - start_time
return {
'input_dir': input_dir,
'stage': stage,
'command': ' '.join(cmd),
'duration': duration,
'success': result.returncode == 0,
'stdout': result.stdout if result.returncode == 0 else None,
'stderr': result.stderr if result.returncode != 0 else None
}
def collect_results(self, input_dir: str, result_type: str = 'all',
output_format: str = 'csv') -> Dict[str, Any]:
"""Collect and aggregate results using result_collector.sh"""
self.logger.info(f"Collecting {result_type} results from {input_dir}")
# Build command
script_path = self._get_script_path('result_collector.sh')
cmd = [script_path, "-i", input_dir]
# Add result type and format
cmd.extend(["-t", result_type, "-f", output_format])
# Run the collection
start_time = time.time()
result = self._run_command(cmd)
duration = time.time() - start_time
return {
'input_dir': input_dir,
'result_type': result_type,
'format': output_format,
'command': ' '.join(cmd),
'duration': duration,
'success': result.returncode == 0,
'stdout': result.stdout if result.returncode == 0 else None,
'stderr': result.stderr if result.returncode != 0 else None
}
def get_master_results(self) -> List[Dict[str, Any]]:
"""Read results from the master CSV file"""
master_csv = os.path.join(self.base_dir, 'results', 'master_results.csv')
if not os.path.exists(master_csv):
return []
results = []
with open(master_csv, 'r') as f:
# Skip comment lines
lines = [line for line in f if not line.startswith('#')]
if lines:
reader = csv.DictReader(lines)
for row in reader:
results.append(row)
return results
def get_profiling_metadata(self) -> List[Dict[str, Any]]:
"""Read profiling metadata from CSV"""
profiling_csv = os.path.join(self.base_dir, 'results', 'profiling_metadata.csv')
if not os.path.exists(profiling_csv):
return []
results = []
with open(profiling_csv, 'r') as f:
# Skip comment lines
lines = [line for line in f if not line.startswith('#')]
if lines:
reader = csv.DictReader(lines)
for row in reader:
results.append(row)
return results
def main():
"""Main entry point"""
parser = argparse.ArgumentParser(
description='AFSysBench Runner - Python integration for AlphaFold benchmarking (Updated for modular architecture)',
formatter_class=argparse.RawDescriptionHelpFormatter
)
# Global options
parser.add_argument('-c', '--config',
help='Configuration file path')
parser.add_argument('-v', '--verbose', action='store_true',
help='Enable verbose logging')
# Subcommands
subparsers = parser.add_subparsers(dest='command', help='Command to run')
# MSA command
msa_parser = subparsers.add_parser('msa', help='Run MSA benchmark')
msa_parser.add_argument('-i', '--input', required=True,
help='Input JSON/FASTA file')
msa_parser.add_argument('-t', '--threads', nargs='+', type=int,
help='Thread counts to test')
msa_parser.add_argument('-o', '--output', help='Output directory')
# Inference command
inf_parser = subparsers.add_parser('inference', help='Run inference benchmark')
inf_parser.add_argument('-i', '--input', required=True,
help='Input JSON file with MSA data')
inf_parser.add_argument('-t', '--threads', nargs='+', type=int,
help='Thread counts to test')
inf_parser.add_argument('--nsys', action='store_true',
help='Enable NSYS profiling')
inf_parser.add_argument('-m', '--models', type=int,
help='Number of models to generate')
inf_parser.add_argument('-o', '--output', help='Output directory')
# Batch command
batch_parser = subparsers.add_parser('batch', help='Run batch benchmarks')
batch_parser.add_argument('-i', '--input', required=True,
help='Input directory')
batch_parser.add_argument('-s', '--stage', choices=['msa', 'inference'],
default='msa', help='Stage to run')
batch_parser.add_argument('-t', '--threads', nargs='+', type=int,
help='Thread counts to test')
batch_parser.add_argument('-n', '--name', help='Batch name')
# Collect results command
collect_parser = subparsers.add_parser('collect', help='Collect and aggregate results')
collect_parser.add_argument('-i', '--input', required=True,
help='Input directory with results')
collect_parser.add_argument('-t', '--type',
choices=['all', 'performance', 'profiling'],
default='all', help='Result type to collect')
collect_parser.add_argument('-f', '--format',
choices=['csv', 'json', 'summary'],
default='csv', help='Output format')
# Show results command
show_parser = subparsers.add_parser('show', help='Show collected results')
show_parser.add_argument('-t', '--type',
choices=['master', 'profiling'],
default='master',
help='Type of results to show')
args = parser.parse_args()
if not args.command:
parser.print_help()
sys.exit(1)
# Some commands don't need configuration
if args.command == 'show':
# Create a minimal runner for show command
class ShowRunner:
def __init__(self):
self.base_dir = os.path.dirname(os.path.abspath(__file__))
def get_master_results(self):
master_csv = os.path.join(self.base_dir, 'results', 'master_results.csv')
if not os.path.exists(master_csv):
return []
results = []
with open(master_csv, 'r') as f:
lines = [line for line in f if not line.startswith('#')]
if lines:
reader = csv.DictReader(lines)
for row in reader:
results.append(row)
return results
def get_profiling_metadata(self):
profiling_csv = os.path.join(self.base_dir, 'results', 'profiling_metadata.csv')
if not os.path.exists(profiling_csv):
return []
results = []
with open(profiling_csv, 'r') as f:
lines = [line for line in f if not line.startswith('#')]
if lines:
reader = csv.DictReader(lines)
for row in reader:
results.append(row)
return results
runner = ShowRunner()
else:
# Load configuration for other commands
if not args.config:
print("Error: Configuration file (-c/--config) is required for this command")
sys.exit(1)
try:
config = BenchmarkConfig.from_file(args.config)
config.config_file = args.config # Store config file path
except Exception as e:
print(f"Error loading config: {e}")
sys.exit(1)
# Create runner
runner = AFBenchRunner(config)
# Set logging level
if args.verbose:
runner.logger.setLevel(logging.DEBUG)
# Execute command
try:
if args.command == 'msa':
result = runner.run_msa_benchmark(
args.input,
args.threads,
args.output
)
if result['success']:
print(f"✓ MSA benchmark completed in {result['duration']:.2f}s")
else:
print(f"✗ MSA benchmark failed")
if result['stderr']:
print(f"Error: {result['stderr']}")
elif args.command == 'inference':
result = runner.run_inference_benchmark(
args.input,
args.threads,
args.nsys,
args.models,
args.output
)
if result['success']:
print(f"✓ Inference benchmark completed in {result['duration']:.2f}s")
else:
print(f"✗ Inference benchmark failed")
if result['stderr']:
print(f"Error: {result['stderr']}")
elif args.command == 'batch':
result = runner.run_batch(
args.input,
args.stage,
args.threads,
args.name
)
if result['success']:
print(f"✓ Batch {args.stage} completed in {result['duration']:.2f}s")
else:
print(f"✗ Batch {args.stage} failed")
if result['stderr']:
print(f"Error: {result['stderr']}")
elif args.command == 'collect':
result = runner.collect_results(
args.input,
args.type,
args.format
)
if result['success']:
print(f"✓ Results collected in {result['duration']:.2f}s")
else:
print(f"✗ Result collection failed")
if result['stderr']:
print(f"Error: {result['stderr']}")
elif args.command == 'show':
if args.type == 'master':
results = runner.get_master_results()
print(f"Found {len(results)} entries in master results")
# Show recent results
if results:
recent = sorted(results, key=lambda x: x.get('timestamp', ''), reverse=True)[:10]
print("\nRecent results:")
for r in recent:
print(f" - {r.get('timestamp')}: {r.get('input_file')} "
f"({r.get('stage')}, {r.get('threads')} threads) - "
f"{r.get('status')} in {r.get('duration_sec')}s")
elif args.type == 'profiling':
results = runner.get_profiling_metadata()
print(f"Found {len(results)} profiling runs")
if results:
print("\nProfiling runs:")
for r in results:
print(f" - {r.get('timestamp')}: {r.get('input_file')} "
f"({r.get('profiling_tool')}, {r.get('threads')} threads) - "
f"{r.get('status')}")
except KeyboardInterrupt:
print("\nBenchmark interrupted by user")
sys.exit(1)
except Exception as e:
print(f"Error: {e}")
if args.verbose:
import traceback
traceback.print_exc()
sys.exit(1)
if __name__ == "__main__":
main()