Download cropping_analysis/launch_parallel_slice_drop.py from chn123/nlst_sybil: direct link, hf CLI and curl.
- Browser
- Download file 4.2 kB
-
https://huggingface.co/datasets/chn123/nlst_sybil/resolve/main/cropping_analysis/launch_parallel_slice_drop.py
- Command line
-
hf download hf://datasets/chn123/nlst_sybil/cropping_analysis/launch_parallel_slice_drop.py
-
curl -L -o launch_parallel_slice_drop.py https://huggingface.co/datasets/chn123/nlst_sybil/resolve/main/cropping_analysis/launch_parallel_slice_drop.py
4.2 kB
| #!/usr/bin/env python | |
| import os | |
| import sys | |
| import time | |
| import json | |
| import subprocess | |
| from pathlib import Path | |
| PYTHON_EXEC = "./anaconda3/envs/sybil_python311_env/bin/python" | |
| WORKER_SCRIPT = "./examples/slice_drop_worker.py" | |
| CHUNK_DIR = "./examples/slice_drop_chunks" | |
| FINAL_JSON = "./examples/slice_drop_all_2965_results.json" | |
| NUM_WORKERS = 3 | |
| GPU_MAP = ["cuda:0", "cuda:1", "cuda:2"] # Mapped via CUDA_VISIBLE_DEVICES=0,1,3 | |
| def start_workers(): | |
| os.makedirs(CHUNK_DIR, exist_ok=True) | |
| processes = [] | |
| env = os.environ.copy() | |
| env["CUDA_VISIBLE_DEVICES"] = "0,1,3" | |
| for wid in range(NUM_WORKERS): | |
| log_path = os.path.join(CHUNK_DIR, f"worker_{wid}.log") | |
| log_file = open(log_path, "a") | |
| cmd = [ | |
| PYTHON_EXEC, WORKER_SCRIPT, | |
| "--worker_id", str(wid), | |
| "--num_workers", str(NUM_WORKERS), | |
| "--device", GPU_MAP[wid], | |
| "--chunk_dir", CHUNK_DIR | |
| ] | |
| print(f"Launching Worker {wid} on {GPU_MAP[wid]} (Logs: {log_path})...") | |
| p = subprocess.Popen(cmd, env=env, stdout=log_file, stderr=subprocess.STDOUT) | |
| processes.append((wid, p, log_file)) | |
| return processes | |
| def get_progress(): | |
| counts = [] | |
| for wid in range(NUM_WORKERS): | |
| out_file = os.path.join(CHUNK_DIR, f"worker_{wid}_results.json") | |
| if os.path.exists(out_file): | |
| try: | |
| with open(out_file, "r") as f: | |
| data = json.load(f) | |
| counts.append(len(data)) | |
| except Exception: | |
| counts.append(0) | |
| else: | |
| counts.append(0) | |
| return counts | |
| def merge_results(): | |
| all_results = {} | |
| for wid in range(NUM_WORKERS): | |
| out_file = os.path.join(CHUNK_DIR, f"worker_{wid}_results.json") | |
| if os.path.exists(out_file): | |
| try: | |
| with open(out_file, "r") as f: | |
| data = json.load(f) | |
| for item in data: | |
| all_results[item["series_id"]] = item | |
| except Exception as e: | |
| print(f"Merge error on worker {wid}: {e}") | |
| merged_list = list(all_results.values()) | |
| with open(FINAL_JSON, "w") as f: | |
| json.dump(merged_list, f, indent=2) | |
| return len(merged_list) | |
| def monitor_workers(processes, total_target=2965): | |
| start_time = time.time() | |
| last_print = 0 | |
| print("\nAll workers started! Monitoring execution...") | |
| try: | |
| while True: | |
| time.sleep(15) | |
| counts = get_progress() | |
| total_done = sum(counts) | |
| # Check if any process died unexpectedly | |
| all_alive = True | |
| for wid, p, _ in processes: | |
| poll = p.poll() | |
| if poll is not None and poll != 0: | |
| print(f"WARNING: Worker {wid} exited with code {poll}!") | |
| all_alive = False | |
| now = time.time() | |
| if now - last_print >= 60 or total_done >= total_target or not all_alive: | |
| last_print = now | |
| elapsed = now - start_time | |
| speed = total_done / elapsed if elapsed > 0 else 0 | |
| eta_sec = (total_target - total_done) / speed if speed > 0 else 0 | |
| eta_hours = eta_sec / 3600 | |
| pct = (total_done / total_target) * 100 | |
| print(f"[{time.strftime('%X')}] Progress: {total_done}/{total_target} ({pct:.1f}%) | " | |
| f"Workers: {counts} | Speed: {speed*60:.1f} series/min | ETA: {eta_hours:.1f}h") | |
| sys.stdout.flush() | |
| # Update merged json periodically | |
| merged_count = merge_results() | |
| # If all processes finished | |
| if all(p.poll() is not None for _, p, _ in processes): | |
| print("\nAll worker processes completed.") | |
| break | |
| finally: | |
| merge_results() | |
| for _, _, log_file in processes: | |
| log_file.close() | |
| if __name__ == "__main__": | |
| procs = start_workers() | |
| monitor_workers(procs) | |