|
| 1 | +# ------------------------------------------------------------- |
| 2 | +# |
| 3 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 4 | +# or more contributor license agreements. See the NOTICE file |
| 5 | +# distributed with this work for additional information |
| 6 | +# regarding copyright ownership. The ASF licenses this file |
| 7 | +# to you under the Apache License, Version 2.0 (the |
| 8 | +# "License"); you may not use this file except in compliance |
| 9 | +# with the License. You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, |
| 14 | +# software distributed under the License is distributed on an |
| 15 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 | +# KIND, either express or implied. See the License for the |
| 17 | +# specific language governing permissions and limitations |
| 18 | +# under the License. |
| 19 | +# |
| 20 | +# ------------------------------------------------------------- |
| 21 | +from concurrent.futures import ProcessPoolExecutor, as_completed |
| 22 | +import multiprocessing as mp |
| 23 | +import os |
| 24 | +import pickle |
| 25 | + |
| 26 | +import time |
| 27 | +from typing import Any, Dict, List, Optional |
| 28 | +from systemds.scuro import Modality |
| 29 | +from systemds.scuro.drsearch.representation_dag import ( |
| 30 | + LRUCache, |
| 31 | + RepresentationDag, |
| 32 | + group_dags_by_dependencies, |
| 33 | +) |
| 34 | +from systemds.scuro.utils.checkpointing import CheckpointManager |
| 35 | +from systemds.scuro.drsearch.dag_group_scheduler import DAGGroupScheduler |
| 36 | + |
| 37 | + |
| 38 | +def _process_dag_group( |
| 39 | + dag_group_pickle: bytes, |
| 40 | + modality_pickle: bytes, |
| 41 | + tasks_pickle: bytes, |
| 42 | + modality_id: int, |
| 43 | + dag_group_idx: int, |
| 44 | +) -> Dict[str, Any]: |
| 45 | + checkpoint_manager = CheckpointManager( |
| 46 | + checkpoint_dir=os.getcwd(), |
| 47 | + prefix=f"unimodal_checkpoint_group_{modality_id}_{dag_group_idx}_", |
| 48 | + checkpoint_every=1, |
| 49 | + resume=False, |
| 50 | + ) |
| 51 | + results = [] |
| 52 | + |
| 53 | + dag_group = pickle.loads(dag_group_pickle) |
| 54 | + modality = pickle.loads(modality_pickle) |
| 55 | + tasks = pickle.loads(tasks_pickle) |
| 56 | + |
| 57 | + group_cache = LRUCache(max_size=6) |
| 58 | + |
| 59 | + for i, dag in enumerate(dag_group): |
| 60 | + representation = dag.execute([modality], external_cache=group_cache) |
| 61 | + |
| 62 | + for task in tasks: |
| 63 | + start = time.perf_counter() |
| 64 | + scores = task.run(representation.data) |
| 65 | + end = time.perf_counter() |
| 66 | + |
| 67 | + results.append( |
| 68 | + { |
| 69 | + "scores": scores, |
| 70 | + "transform_time": representation.transform_time, |
| 71 | + "task_name": task.model.name, |
| 72 | + "task_time": end - start, |
| 73 | + "dag": dag, |
| 74 | + "modality_id": modality_id, |
| 75 | + } |
| 76 | + ) |
| 77 | + |
| 78 | + checkpoint_manager.increment(modality_id, 1, dag_group_idx=dag_group_idx) |
| 79 | + checkpoint_manager.checkpoint_if_due(results) |
| 80 | + |
| 81 | + return {"results": results} |
| 82 | + |
| 83 | + |
| 84 | +class DAGGroupExecutor: |
| 85 | + def __init__( |
| 86 | + self, |
| 87 | + dags: List[RepresentationDag], |
| 88 | + modalities: List[Modality], |
| 89 | + tasks: List[Any], |
| 90 | + checkpoint_manager: Optional[CheckpointManager] = None, |
| 91 | + max_workers: Optional[int] = None, |
| 92 | + ): |
| 93 | + self.dags = dags |
| 94 | + self.dag_groups = group_dags_by_dependencies(dags) |
| 95 | + self.modalities = modalities |
| 96 | + self.tasks = tasks |
| 97 | + self.max_workers = max_workers or mp.cpu_count() |
| 98 | + self.checkpoint_manager = checkpoint_manager |
| 99 | + self.scheduler = DAGGroupScheduler( |
| 100 | + dag_groups=self.dag_groups, modality=modalities[0] |
| 101 | + ) |
| 102 | + |
| 103 | + def run(self): |
| 104 | + results = [] |
| 105 | + ctx = mp.get_context("spawn") |
| 106 | + max_workers = min(len(self.dag_groups), self.max_workers) |
| 107 | + |
| 108 | + modality_pickle = pickle.dumps( |
| 109 | + self.modalities[0] |
| 110 | + ) # TODO: handle multiple modalities |
| 111 | + tasks_pickle = pickle.dumps(self.tasks) |
| 112 | + |
| 113 | + pending_dag_groups = set(range(len(self.dag_groups))) |
| 114 | + running_dag_groups = {} |
| 115 | + all_groups_succeeded = True |
| 116 | + with ProcessPoolExecutor(max_workers=max_workers, mp_context=ctx) as executor: |
| 117 | + while pending_dag_groups or running_dag_groups: |
| 118 | + pending_resources = [ |
| 119 | + ( |
| 120 | + i, |
| 121 | + self.scheduler.group_resources[i][0], |
| 122 | + self.scheduler.group_resources[i][1], |
| 123 | + ) |
| 124 | + for i in pending_dag_groups |
| 125 | + ] |
| 126 | + ready_to_execute = self.scheduler.get_runnable( |
| 127 | + pending_resources, max_concurrent=max_workers |
| 128 | + ) |
| 129 | + for group_id, gpu_id in ready_to_execute: |
| 130 | + pending_dag_groups.remove(group_id) |
| 131 | + dag_group = self.dag_groups[group_id] |
| 132 | + cpu_mem, gpu_mem = self.scheduler.group_resources[group_id] |
| 133 | + |
| 134 | + future = executor.submit( |
| 135 | + _process_dag_group, |
| 136 | + pickle.dumps(dag_group), |
| 137 | + modality_pickle, |
| 138 | + tasks_pickle, |
| 139 | + self.modalities[0].modality_id, |
| 140 | + group_id, |
| 141 | + ) |
| 142 | + running_dag_groups[future] = (group_id, cpu_mem, gpu_mem, gpu_id) |
| 143 | + if not running_dag_groups: |
| 144 | + break |
| 145 | + done = next(as_completed(running_dag_groups), None) |
| 146 | + if done is None: |
| 147 | + break |
| 148 | + group_id, cpu_mem, gpu_mem, gpu_id = running_dag_groups.pop(done) |
| 149 | + self.scheduler.release(cpu_mem, gpu_mem, gpu_id) |
| 150 | + |
| 151 | + try: |
| 152 | + result_dict = future.result() |
| 153 | + |
| 154 | + for result_entry in result_dict["results"]: |
| 155 | + results.append( |
| 156 | + { |
| 157 | + "scores": result_entry["scores"], |
| 158 | + "transform_time": result_entry["transform_time"], |
| 159 | + "task_name": result_entry["task_name"], |
| 160 | + "task_time": result_entry["task_time"], |
| 161 | + "dag": result_entry["dag"], |
| 162 | + "modality_id": self.modalities[0].modality_id, |
| 163 | + } |
| 164 | + ) |
| 165 | + except Exception as e: |
| 166 | + all_groups_succeeded = False |
| 167 | + print( |
| 168 | + f"Error processing DAG group {group_id} for modality {self.modalities[0].modality_id}: {e}" |
| 169 | + ) |
| 170 | + return results, all_groups_succeeded |
0 commit comments