Run experiment with the code in the folder Args: folder_name: The folder containing the code run_num: The run number timeout: Maximum execution time in seconds gpu_ids: GPU IDs to use (string like "0,1" or None for CPU) log_file: Optional file object
(folder_name, run_num, timeout=None, gpu_ids=None, log_file=None, task_type='auto')
| 145 | return result.stdout |
| 146 | |
| 147 | def run_experiment(folder_name, run_num, timeout=None, gpu_ids=None, log_file=None, task_type='auto'): |
| 148 | """ |
| 149 | Run experiment with the code in the folder |
| 150 | |
| 151 | Args: |
| 152 | folder_name: The folder containing the code |
| 153 | run_num: The run number |
| 154 | timeout: Maximum execution time in seconds |
| 155 | gpu_ids: GPU IDs to use (string like "0,1" or None for CPU) |
| 156 | log_file: Optional file object to write logs to |
| 157 | |
| 158 | Returns: |
| 159 | Tuple of (return_code, next_prompt, traceback, message) |
| 160 | """ |
| 161 | def log_message(msg): |
| 162 | """Write message to both stdout and log file""" |
| 163 | print(msg) |
| 164 | sys.stdout.flush() |
| 165 | if log_file: |
| 166 | try: |
| 167 | log_file.write(msg + "\n") |
| 168 | log_file.flush() |
| 169 | except (ValueError, OSError): |
| 170 | pass |
| 171 | |
| 172 | timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") |
| 173 | logger.info(f"[{timestamp}] Starting experiment run {run_num} in {folder_name}") |
| 174 | |
| 175 | cwd = osp.abspath(folder_name) |
| 176 | # Create run directory |
| 177 | run_dir = osp.join(cwd, f"run_{run_num}") |
| 178 | if not osp.exists(run_dir): |
| 179 | os.makedirs(run_dir, exist_ok=True) |
| 180 | |
| 181 | # Copy all files from the main folder to the run directory |
| 182 | logger.info(f"Copying files to run directory: {run_dir}") |
| 183 | for item in os.listdir(cwd): |
| 184 | if item.startswith("run_") or item == ".git": |
| 185 | continue |
| 186 | src = osp.join(cwd, item) |
| 187 | dst = osp.join(run_dir, item) |
| 188 | if osp.isdir(src): |
| 189 | if task_type == 'sci' and item in SCI_SYMLINK_DIRS: |
| 190 | if osp.exists(dst) or osp.islink(dst): |
| 191 | os.remove(dst) if osp.islink(dst) else shutil.rmtree(dst) |
| 192 | os.symlink(osp.abspath(src), dst) |
| 193 | else: |
| 194 | if osp.exists(dst): |
| 195 | shutil.rmtree(dst) |
| 196 | shutil.copytree(src, dst) |
| 197 | else: |
| 198 | shutil.copy2(src, dst) |
| 199 | |
| 200 | # LAUNCH COMMAND |
| 201 | command = ["bash", f"launcher.sh"] |
| 202 | logger.info(f"Running command: {command} in {run_dir}") |
| 203 | |
| 204 | # Prepare environment variables (thread-safe copy) |
no test coverage detected