mirror of
https://github.com/NicolasBohn/NexQuant.git
synced 2026-07-28 16:07:46 +00:00
6e19c9e632
* refine prompt * small update * fix a small bug * remove debug config after execution * fix: only remove <think> at start * feat: support creating dataset & multi-eval frame (#1302) * feat: add iterative evolve and evaluation support with partial chain stop * feat: add FTDataEvaluator and support multiple implement functions in finetune * feat: data implement for pre-proposal and proposal and add datasets (#1303) * feat:(1) support for multi layer dataset extraction (2) add category.json for dataset in datasets/ * fix: fix bug for generate category.json * feat: add get_dataset_folder_desc * init data proposal and merge qzli/ft * update data proposal prompts and add max_position_embeddings and resolve confilcts * remove sample counts in data proposal * turn data and train to unified hypo_gen * refine prompts * remove category.json and add it to dataset_info * fix jinja problem and proposal done * lint * add ai-generated description and raw readme into dataset_info.json * update prompt for description * add datasets * initial fix for proposal of data * final version for data proposal * lint * feat: add stats in dataset_info, and enable data coder (#1306) * refactor(dataset): add stats into dataset_info.json, and remove dataset from gitignore_folder * feat: enable data coder and run data process * feat: Merge data coder (#1307) * feat: implement finetune data coding, evaluation, and config improvements * fix: deepspeed config path * fix: dataset info columns --------- Co-authored-by: Young <afe.young@gmail.com> * replace str length with token_limit * add readme to dataset_info and remove useless blank lines in scenario description * feat: dataset prepare * fix: extract prams script name * feat: add loss&predictions samples to feedback * remove duplicate envs and and add llm_api_preferences and enhance reasoning token limits * feat: network for ft_env * fix: remove gpt-4o, which has low quota * feat: a simple ui * feat: merge data and train task type (#1309) * feat: filter redundant prams of lf * fix: ui bug caused by removing task_type * fix: force agent to use high concurrency, and remove redundant prompt * feat: extract info from llama factory log, and check data exists before download * fix: add compatibility rules * feat: llm evaluator for data coder * feat: openai package in ft docker, and refine prompt * feat: refine ft ui, add more info * feat: add raw logs * refine data coder prompt(for feedback debug) * feat: select dataset in scen init * fix: ui for docker log seperately * feat: sync log through blob * improve ui, and add llm feedback in Runner&Exp2FB (#1312) * fix: ui bug to visualize docker log, and lint * feat: unified docker log for ft env, and some refactor * fix bugs and improve ui * feat: save log of evaluator(single feedback) * feat: add evaluator, set cleanup docker log * feat: call llm in RunnerEvaluator and Feedback * fix: extract structured error message in RunnerEvaluator * feat: feedback improve, and fix some bugs * feat: feedback improve when runner fails * small update * feat(UI): add running info and benchmark metric in loop expander * feat(UI): add render markdown toggle * feat: refine prompts and add error type in exp2fb * feat: add filterd params reason, set default benchmark timeout to infinite, and refine train loss express * recover dataset deepscaler * feat: set timeout in .env * refactor: unifiied ft_env timeout * feat: debug mode for data coder * feat: deliver data_stats after generate debug_data * feat: use gpt-5.1 as judge model, set judge_retry, and refine debug mode prompt * refine prompt * refactor: llama factory manager logic, and refine data processing prompt * feat(DockerEnv): support GPU selection via CUDA_VISIBLE_DEVICES * feat: set api concurrency via .env * fix: ft env timeout bug * feat: enable CondaEnv run * fix: can't update bin path in first run, and path bug in lf manager * feat(ui): set log path through .env * refactor(ui): wrap_lines, remove css * feat(coder): retry when parse code-block fail * fix: refine single-fb in ui, and fix path bug(not allow proposal to decide path) * fix: opencompass CondaEnv torch compatible with vllm * fix: refine error text in coding * feat: deepspeed config for CondaEnv * feat: memory estimator * fix: deepspeed package for condaenv * fix: use `client.chat.completions.create()` only * feat: flash attention for condaenv * feat: strong and weak models interface * fix: condaenv package dependency * use multi round conversation in llm finetune proposal * refine prompt for data processing * enable evolving in data coder * maximize output token size * fix: refine ui * fix: optional packages for llama factory * fix: torch denpendency for b200 * fix: opencompass dependency * update cot prompts * skip the sub implement * skip conda preparation if env exists * update chemcot datasets * fix: unify docker to use litellm * update readme and instructions * fix: set CUDA_VISIBLE_DEVICES for CondaEnv * feat: add panorama dataset, refactor dataset interface * feat: calculate token using tiktoken, and ndarray bug * fix: download subtasks of chemcotdataset seperately * feat: customized prepare func for datasets * feat: update new benchmarks * add datasets package * docs: readme for llm finetune * feat: download raw data directly, with post-process function * feat: analyze raw dataset * suppress litellm debug info * feat(ui): summary page * feat: run multi-jobs * feat: improve ui * feat: add path and checkout options to LLM finetune loop entrypoint * feat: add FinanceIQ_ppl benchmark with auto-download and dataset desc rendering * refactor: remove unused imports and dead code, fix session folder logging * feat: enable tablebench and tableInstruct dataset * refine dataset readme, and coder prompt * refine proposal and coder prompt * fix: ui path (default log path) * feat: add automatic LoRA model merging for benchmarking with vLLM * refactor: reorganize finetune benchmark and merge modules under benchmark dir * refactor: modularize benchmark config and error extraction for finetune scenario * fix: update benchmark import paths and disable env cache for device info * refactor docke&conda env and fix import bugs * modify init python file * feat: add FinanceIQ dataset split utility and integrate with pipeline * feat: set weak and strong model by env, distribute workload across models * feat: sample dataset and rm params for tensorboard, wandb * update script to run jobs * refine proposal prompt, remove specific dataset name * fix(ui): auto switch log folder * fix: estimate the processed full data after sample * feat: filter raw data more aggressively, and lower data_eval standard * feat: sync workspace to blob * feat: rdkit for chemcotbench * update qwen2.5&llama3.1 context * fix: force failure on validation error and remove try/except in validator * feat: unified error sample extraction (with test scripts) * feat: set conda cache with .env * feat: skip data eval if data pass in last evo * fix: rm redundant param * fix ui bug * refactor: centralize assign_code_list_to_evo in MultiProcessEvolvingStrategy * feat: add test_params.yaml generation and workspace cleanup improvements for finetune * refactor: replace get_clear_ws_cmd with clear_workspace and update prompts for hard check criteria * add bioprobench dataset * fix: handle commas in training config extraction and refactor prompt includes * bioprobench description * add bioprobench readme * feat: merge lora adapter for blackwell gpu * feat: support for multi benchmarks in one job * change dfficult aware content for training * update difficulty-aware and logging principles * fix: resolve variable name conflict in FTRunnerEvaluator * set job id accuracy to minute * feat(ui): display one selected metric per benchmark * feat: store sota exp, and fix ws_ckp bug * fix: truncate data.json in feedback * fix: opencompass data for conda env * fix: save only the last model * feat: set log path and ws path * fix: set overwrite_cache to avoid lock contention(through injecting params) * feat: redirect stdout to file in localenv * add pickle cache to dataset desc * fix CI * fix: remove redundant wrapper * feat: set python_unbuffered * move redirect stdout to env run * fix a small bug * move model folder * feat(ui): display benchmark baseline * fix: enrich scenario and benchmark description * fix: rewrite runner eval to accept easier * feat: compare with baseline when no SOTA * update tablebench readme * fix: switch back to single benchmark (for baseline) * feat(ui): add ws path in ui * refactor: update SOTA tracking to use DAG traversal and parent selection * fix: prioritize local_selection in trace and refactor sibling retrieval logic * refactor: unify error handling in feedback generation and update workspace injection * feat: add skip_loop_error_stepname to control error skip step in LoopBase * fix: set local_selection to NEW_ROOT for experiments without parent * feat: set different ports for jobs * feat: set different ports for jobs * feat: add upper data size limit for LLM fine-tuning and update related prompts * fix: replace get_truncated_stdout() with stdout for consistent output handling * refactor: remove data.json from cache and workspace logic, focus on script-based reuse * fix: rm target_scenario * feat: add selective cache extraction and custom cache key for data processing * fix(ui): bug when displaying tablebench * fix: filter config in dataset_info.json * feat: add test set, set valid set * feat(ui): update test score, and set color for final decision * feat: add test score for baseline and update ui * fix: use [-100:] as test range * feat: update data_stats in runner * feat: wait for opencompass init when run multi jobs * fix: adjust test&valid split * feat: force to generate COT(with <think> token), and add answer format in scenarios.json * feat: improve ui * fix: unify benchmark volume mounts and set extra_volumes for conda env * fix(ui): number color * fix: update GPU memory handling to use total memory in GB and streamline code * fix: set use_cot_postprocessor * feat: add env_dict to config classes and merge env vars in Env run * fix: let coder obey proposal * fix(ui): direction bug and update chemcot core metirc * fix: set consistent benchmark mount points and env vars for docker and conda * fix: addintional target for LoRA * feat: workspace dir log for benchmark running * fix: tableInstruct path bug and update benchmark description * feat: timeout for whole job * fix: align FinanceIQ import to opencompass * feat: use llm_judge for FinanceIQ * feat: switch to turn on <think> or not * feat: using scripts to redirect stdout, and run in different windows * feat: sync litellm log * fix: gpu memory format * fix: escape special characters in benchmark desc * fix: set data processing timeout to 1h * feat: set valid_loss and save_best_model * fix: inject timeout and stage * fix: loss history extract logic * feat: inject output dir * feat: inject eval batch size * feat: inject save_total_limit * feat: update data prompt * fix: escape shell special characters * fix: tablebench visualization UI * fix: move implementation validation to coder, and ignore injected params * feat: README for FinanceIQ dataset * fix: bioprobench desc error * fix: remove task alignment when coder eval * fix: FinanceIQ now extracts last capital as answer * fix: stdout contains binary data * feat: recover estimate full output and set eval setting automatically * fix(ui): precision for summary table * fix(ui): import error * feat: try to use lora * fix(api): fix litellm bug for code block * fix: refine prompts to give agent more decision space * chore(ci): fix mypy typing issues * chore(ci): format code with black * chore(ci): fix ruff lint violations * chore(ci): sort imports with isort * chore(ci): format code with black * test: temporarily skip extract_parameters imports due to numpy pin * fix: compatibility issues for qlib scenarios on finetune branch * fix(fin_factor): skip to fb for coder error * fix(loop): default skip to feedback step on skip_loop_error When skip_loop_error exception happens and skip_loop_error_stepname is not explicitly set, default to jumping to 'feedback' step if it exists, otherwise fall back to the last step (record). This prevents KeyError when record step tries to access feedback data that doesn't exist because we skipped the feedback phase. Also removed redundant skip_loop_error_stepname from finetune loop since it's now the default behavior. * add 'skip to record' to DS scenario like other scenarios * fix 2 scenarios bug about rd_loop class * fix: lint(mypy, ruff, black) error * fix: mypy lint error * fix data science scenario bug --------- Co-authored-by: Xu Yang <peteryang@vip.qq.com> Co-authored-by: Qizheng Li <jenssenlee@163.com> Co-authored-by: you-n-g <you-n-g@users.noreply.github.com> Co-authored-by: amstrongzyf <201840057@smail.nju.edu.cn> Co-authored-by: Young <afe.young@gmail.com> Co-authored-by: amstrongzyf <amstrongzyf@126.com> Co-authored-by: chelsea97 <zhuowbrown@gmail.com> Co-authored-by: SunsetWolf <Lv.Linlang@hotmail.com>
500 lines
19 KiB
Python
500 lines
19 KiB
Python
from __future__ import annotations
|
|
|
|
import io
|
|
import os
|
|
import platform
|
|
import re
|
|
import shutil
|
|
import typing
|
|
import uuid
|
|
import zipfile
|
|
from abc import ABC, abstractmethod
|
|
from collections.abc import Sequence
|
|
from copy import deepcopy
|
|
from dataclasses import dataclass
|
|
from pathlib import Path
|
|
from typing import TYPE_CHECKING, Any, Generic, TypeVar
|
|
|
|
from rdagent.core.conf import RD_AGENT_SETTINGS
|
|
from rdagent.core.evaluation import Feedback
|
|
|
|
if TYPE_CHECKING:
|
|
from rdagent.utils.env import EnvResult
|
|
|
|
|
|
if typing.TYPE_CHECKING:
|
|
from rdagent.core.proposal import Hypothesis
|
|
from rdagent.utils.env import Env
|
|
|
|
"""
|
|
This file contains the all the class about organizing the task in RD-Agent.
|
|
"""
|
|
|
|
|
|
class AbsTask(ABC):
|
|
def __init__(self, name: str, version: int = 1) -> None:
|
|
"""
|
|
The version of the task, default is 1
|
|
Because qlib tasks execution and kaggle tasks execution are different, we need to distinguish them.
|
|
TODO: We may align them in the future.
|
|
"""
|
|
self.version = version
|
|
self.name = name
|
|
|
|
@abstractmethod
|
|
def get_task_information(self) -> str:
|
|
"""
|
|
Get the task information string to build the unique key
|
|
"""
|
|
|
|
|
|
class UserInstructions(list[str]):
|
|
def __str__(self) -> str:
|
|
if self:
|
|
return ("\nUser Instructions (Top priority!):\n" + "\n".join(f"- {ui}" for ui in self)) if self else ""
|
|
return ""
|
|
|
|
|
|
class Task(AbsTask):
|
|
def __init__(
|
|
self,
|
|
name: str,
|
|
version: int = 1,
|
|
description: str = "",
|
|
user_instructions: UserInstructions | None = None,
|
|
) -> None:
|
|
super().__init__(name, version)
|
|
self.description = description
|
|
self.user_instructions = user_instructions
|
|
|
|
def get_task_information(self) -> str:
|
|
return f"Task Name: {self.name}\nDescription: {self.description}{self.user_instructions!s}"
|
|
|
|
def __repr__(self) -> str:
|
|
return f"<{self.__class__.__name__} {self.name}>"
|
|
|
|
|
|
ASpecificTask = TypeVar("ASpecificTask", bound=Task)
|
|
ASpecificFeedback = TypeVar("ASpecificFeedback", bound=Feedback)
|
|
|
|
|
|
@dataclass
|
|
class RunningInfo:
|
|
result: object = None # The result of the experiment, can be different types in different scenarios.
|
|
running_time: float | None = None
|
|
|
|
|
|
class Workspace(ABC, Generic[ASpecificTask, ASpecificFeedback]):
|
|
"""
|
|
A workspace is a place to store the task implementation. It evolves as the developer implements the task.
|
|
To get a snapshot of the workspace, make sure call `copy` to get a copy of the workspace.
|
|
"""
|
|
|
|
def __init__(self, target_task: ASpecificTask | None = None) -> None:
|
|
self.target_task: ASpecificTask | None = target_task
|
|
self.feedback: ASpecificFeedback | None = None
|
|
self.running_info: RunningInfo = RunningInfo()
|
|
|
|
@abstractmethod
|
|
def execute(self, *args: Any, **kwargs: Any) -> object | None:
|
|
error_message = "execute method is not implemented."
|
|
raise NotImplementedError(error_message)
|
|
|
|
@abstractmethod
|
|
def copy(self) -> Workspace:
|
|
error_message = "copy method is not implemented."
|
|
raise NotImplementedError(error_message)
|
|
|
|
@property
|
|
@abstractmethod
|
|
def all_codes(self) -> str:
|
|
"""
|
|
Get all the code files in the workspace as a single string.
|
|
"""
|
|
|
|
# when the workspace is mutable inplace, provide support for creating checkpoints and recovering.
|
|
@abstractmethod
|
|
def create_ws_ckp(self) -> None:
|
|
"""
|
|
Create an in-memory checkpoint of the workspace so it can be restored later.
|
|
"""
|
|
|
|
@abstractmethod
|
|
def recover_ws_ckp(self) -> None:
|
|
"""
|
|
Restore the workspace from the checkpoint created by :py:meth:`create_ws_ckp`.
|
|
"""
|
|
|
|
|
|
ASpecificWS = TypeVar("ASpecificWS", bound=Workspace)
|
|
|
|
|
|
class WsLoader(ABC, Generic[ASpecificTask, ASpecificWS]):
|
|
@abstractmethod
|
|
def load(self, task: ASpecificTask) -> ASpecificWS:
|
|
error_message = "load method is not implemented."
|
|
raise NotImplementedError(error_message)
|
|
|
|
|
|
class FBWorkspace(Workspace):
|
|
"""
|
|
File-based task workspace
|
|
|
|
The implemented task will be a folder which contains related elements.
|
|
- Data
|
|
- Code Workspace
|
|
- Output
|
|
- After execution, it will generate the final output as file.
|
|
|
|
A typical way to run the pipeline of FBWorkspace will be:
|
|
(We didn't add it as a method due to that we may pass arguments into
|
|
`prepare` or `execute` based on our requirements.)
|
|
|
|
.. code-block:: python
|
|
|
|
def run_pipeline(self, **files: str):
|
|
self.prepare()
|
|
self.inject_files(**files)
|
|
self.execute()
|
|
|
|
"""
|
|
|
|
def __init__(self, *args: Any, **kwargs: Any) -> None:
|
|
super().__init__(*args, **kwargs)
|
|
self.file_dict: dict[str, Any] = (
|
|
{}
|
|
) # The code injected into the folder, store them in the variable to reproduce the former result
|
|
self.workspace_path: Path = RD_AGENT_SETTINGS.workspace_path / uuid.uuid4().hex
|
|
self.ws_ckp: bytes | None = None # In-memory checkpoint data created by ``create_ws_ckp``.
|
|
self.change_summary: str | None = None # The change from the previous version of workspace
|
|
|
|
@staticmethod
|
|
def _format_code_dict(code_dict: dict[str, str]) -> str:
|
|
"""
|
|
Helper function to format the code dictionary into a string.
|
|
"""
|
|
code_string = ""
|
|
for file_name in sorted(code_dict.keys()):
|
|
code_string += f"\nFile Path: {file_name}\n```\n{code_dict[file_name]}\n```"
|
|
return code_string
|
|
|
|
@property
|
|
def all_codes(self) -> str:
|
|
"""
|
|
Get all the code files in the workspace as a single string, excluding test files.
|
|
"""
|
|
filtered_dict = {k: v for k, v in self.file_dict.items() if k.endswith(".py") and "test" not in k}
|
|
return self._format_code_dict(filtered_dict)
|
|
|
|
def get_codes(self, pattern: str) -> str:
|
|
"""
|
|
Get code files matching a specific pattern as a single string, excluding test files.
|
|
"""
|
|
filtered_dict = {
|
|
k: v for k, v in self.file_dict.items() if re.search(pattern, k) and k.endswith(".py") and "test" not in k
|
|
}
|
|
return self._format_code_dict(filtered_dict)
|
|
|
|
def prepare(self) -> None:
|
|
"""
|
|
Prepare the workspace except the injected code
|
|
- Data
|
|
- Documentation
|
|
typical usage of `*args, **kwargs`:
|
|
Different methods shares the same data. The data are passed by the arguments.
|
|
"""
|
|
self.workspace_path.mkdir(parents=True, exist_ok=True)
|
|
|
|
@staticmethod
|
|
def link_all_files_in_folder_to_workspace(data_path: Path, workspace_path: Path) -> None:
|
|
data_path = Path(data_path).absolute() # in case of relative path that will be invalid when we change cwd.
|
|
workspace_path = Path(workspace_path)
|
|
for data_file_path in data_path.iterdir():
|
|
workspace_data_file_path = workspace_path / data_file_path.name
|
|
if workspace_data_file_path.exists():
|
|
workspace_data_file_path.unlink()
|
|
if platform.system() in ("Linux", "Darwin"):
|
|
workspace_data_file_path.symlink_to(data_file_path)
|
|
if platform.system() == "Windows":
|
|
os.link(data_file_path, workspace_data_file_path)
|
|
|
|
DEL_KEY = "__DEL__"
|
|
|
|
def inject_files(self, **files: str) -> None:
|
|
"""
|
|
Inject the code into the folder.
|
|
{
|
|
<file name1>: <code>, // indicate writing <code> into <file name>
|
|
(create new file or replace existing file)
|
|
<file name2>: "__DEL__" // indicate removing file name2. When we want to replace a file to a new one,
|
|
we usually use this
|
|
}
|
|
"""
|
|
self.prepare()
|
|
for k, v in files.items():
|
|
target_file_path = self.workspace_path / k # Define target_file_path before using it
|
|
if v == self.DEL_KEY: # Use self.DEL_KEY to access the class variable
|
|
if target_file_path.exists():
|
|
target_file_path.unlink() # Unlink the file if it exists
|
|
self.file_dict.pop(k, None) # Safely remove the key from file_dict
|
|
else:
|
|
self.file_dict[k] = v
|
|
target_file_path.parent.mkdir(parents=True, exist_ok=True)
|
|
target_file_path.write_text(v)
|
|
|
|
def remove_files(self, file_names: str | list[str]) -> None:
|
|
"""
|
|
Remove specified files from the workspace.
|
|
"""
|
|
if isinstance(file_names, str):
|
|
file_names = [file_names]
|
|
for file_name in file_names:
|
|
target_file_path = self.workspace_path / file_name
|
|
if target_file_path.exists():
|
|
target_file_path.unlink() # Unlink the file if it exists
|
|
self.file_dict.pop(file_name, None) # Safely remove the key from file_dict
|
|
|
|
def get_files(self) -> list[Path]:
|
|
"""
|
|
Get the environment description.
|
|
|
|
To be general, we only return a list of filenames.
|
|
How to summarize the environment is the responsibility of the Developer.
|
|
"""
|
|
return list(self.workspace_path.iterdir())
|
|
|
|
def inject_code_from_folder(self, folder_path: Path) -> None:
|
|
"""
|
|
Load the workspace from the folder
|
|
"""
|
|
for file_path in folder_path.rglob("*"):
|
|
if file_path.suffix in (".py", ".yaml", ".md"):
|
|
relative_path = file_path.relative_to(folder_path)
|
|
self.inject_files(**{str(relative_path): file_path.read_text()})
|
|
|
|
def inject_code_from_file_dict(self, workspace: FBWorkspace) -> None:
|
|
"""
|
|
Load the workspace from the file_dict
|
|
"""
|
|
# NOTE: this is a deprecated method, use inject_from_workspace instead
|
|
# TODO: remove this method; it is only for compatibility with old codes
|
|
self.inject_from_workspace(workspace)
|
|
|
|
def inject_from_workspace(self, workspace: FBWorkspace) -> None:
|
|
for name, code in workspace.file_dict.items():
|
|
self.inject_files(**{name: code})
|
|
|
|
def copy(self) -> FBWorkspace:
|
|
"""
|
|
copy the workspace from the original one
|
|
"""
|
|
return deepcopy(self)
|
|
|
|
def clear(self) -> None:
|
|
"""
|
|
Clear the workspace
|
|
"""
|
|
shutil.rmtree(self.workspace_path, ignore_errors=True)
|
|
self.file_dict = {}
|
|
|
|
def before_execute(self) -> None:
|
|
"""
|
|
Before executing the code, we need to prepare the workspace and inject code into the workspace.
|
|
"""
|
|
self.prepare()
|
|
self.inject_files(**self.file_dict)
|
|
|
|
def execute(self, env: Env, entry: str) -> str:
|
|
"""
|
|
Before each execution, make sure to prepare and inject code.
|
|
"""
|
|
result = self.run(env, entry)
|
|
return result.stdout # NOTE: truncating just for aligning with the old code.
|
|
|
|
def run(self, env: Env, entry: str) -> EnvResult:
|
|
"""
|
|
Execute the code in the environment and return an EnvResult object (stdout, exit_code, running_time).
|
|
|
|
Before each execution, make sure to prepare and inject code.
|
|
"""
|
|
self.prepare()
|
|
self.inject_files(**self.file_dict)
|
|
return env.run(entry, str(self.workspace_path), env={"PYTHONPATH": "./"})
|
|
|
|
def create_ws_ckp(self) -> None:
|
|
"""
|
|
Zip the contents of ``workspace_path`` and persist the archive on
|
|
``self.ws_ckp`` for later restoration via :py:meth:`recover_ws_ckp`.
|
|
"""
|
|
buf = io.BytesIO()
|
|
with zipfile.ZipFile(buf, "w", zipfile.ZIP_DEFLATED) as zf:
|
|
for file_path in self.workspace_path.rglob("*"):
|
|
# Only include regular files up to 100 KB so that the checkpoint
|
|
# remains lightweight. Larger files (for example, datasets) are
|
|
# expected to be recreated or mounted separately.
|
|
if file_path.is_symlink():
|
|
# Preserve symbolic links within the archive
|
|
zi = zipfile.ZipInfo(str(file_path.relative_to(self.workspace_path)))
|
|
zi.create_system = 3 # indicates Unix
|
|
zi.external_attr = 0o120777 << 16 # symlink file type + 0777 perms
|
|
zf.writestr(zi, str(file_path.readlink()))
|
|
elif file_path.is_file():
|
|
size_limit = RD_AGENT_SETTINGS.workspace_ckp_size_limit
|
|
if (
|
|
RD_AGENT_SETTINGS.workspace_ckp_white_list_names is not None
|
|
and file_path.name in RD_AGENT_SETTINGS.workspace_ckp_white_list_names
|
|
) or (size_limit <= 0 or file_path.stat().st_size <= size_limit):
|
|
zf.write(file_path, file_path.relative_to(self.workspace_path))
|
|
self.ws_ckp = buf.getvalue()
|
|
|
|
def recover_ws_ckp(self) -> None:
|
|
"""
|
|
Restore the workspace directory from the in-memory checkpoint created by
|
|
:py:meth:`create_ws_ckp`.
|
|
"""
|
|
if self.ws_ckp is None:
|
|
msg = "Workspace checkpoint doesn't exist. Call `create_ws_ckp` first."
|
|
raise RuntimeError(msg)
|
|
shutil.rmtree(self.workspace_path, ignore_errors=True)
|
|
self.workspace_path.mkdir(parents=True, exist_ok=True)
|
|
buf = io.BytesIO(self.ws_ckp)
|
|
with zipfile.ZipFile(buf, "r") as zf:
|
|
for info in zf.infolist():
|
|
dest_path = self.workspace_path / info.filename
|
|
# File type bits (upper 4) are in high 16 bits of external_attr
|
|
mode = (info.external_attr >> 16) & 0o170000
|
|
symlink_mode = 0o120000 # Constant for symlink file type in Unix
|
|
if mode == symlink_mode: # Symlink
|
|
dest_path.parent.mkdir(parents=True, exist_ok=True)
|
|
link_target = zf.read(info).decode()
|
|
dest_path.symlink_to(link_target)
|
|
elif info.is_dir():
|
|
dest_path.mkdir(parents=True, exist_ok=True)
|
|
else:
|
|
dest_path.parent.mkdir(parents=True, exist_ok=True)
|
|
with dest_path.open("wb") as f:
|
|
f.write(zf.read(info))
|
|
# NOTE: very important to reduce the size of the object
|
|
self.ws_ckp = None
|
|
|
|
def __str__(self) -> str:
|
|
return f"Workspace[{self.workspace_path=}" + (
|
|
"]" if self.target_task is None else f",{self.target_task.name=}]"
|
|
)
|
|
|
|
|
|
ASpecificWSForExperiment = TypeVar("ASpecificWSForExperiment", bound=Workspace)
|
|
ASpecificWSForSubTasks = TypeVar("ASpecificWSForSubTasks", bound=Workspace)
|
|
|
|
|
|
class ExperimentPlan(dict[str, Any]):
|
|
"""
|
|
A plan for the experiment, which is a dictionary that contains the plan to each stage.
|
|
"""
|
|
|
|
|
|
class Experiment(
|
|
ABC,
|
|
Generic[ASpecificTask, ASpecificWSForExperiment, ASpecificWSForSubTasks],
|
|
):
|
|
"""
|
|
The experiment is a sequence of tasks and the implementations of the tasks after generated by the Developer.
|
|
"""
|
|
|
|
def __init__(
|
|
self,
|
|
sub_tasks: Sequence[ASpecificTask],
|
|
based_experiments: Sequence[ASpecificWSForExperiment] = [],
|
|
hypothesis: Hypothesis | None = None,
|
|
) -> None:
|
|
self.hypothesis: Hypothesis | None = hypothesis # Experiment is optionally generated by hypothesis
|
|
self.sub_tasks: Sequence[ASpecificTask] = sub_tasks
|
|
# None means
|
|
# - initialization placeholder before implementation
|
|
# - the developer actively skip the task;
|
|
self.sub_workspace_list: list[ASpecificWSForSubTasks | None] = [None] * len(self.sub_tasks)
|
|
# TODO:
|
|
# It will be used in runner in history
|
|
# If we implement the whole workflow, we don't have to use it, then we remove it.
|
|
self.based_experiments: Sequence[ASpecificWSForExperiment] = based_experiments
|
|
|
|
self.experiment_workspace: ASpecificWSForExperiment | None = None
|
|
|
|
# The experiment may be developed by different developers.
|
|
# Last feedback is used to propagate info to the next developer.
|
|
# Life cycle:
|
|
# - Developer assigns feedback for next component;
|
|
# - Workflow control clears feedback.
|
|
self.prop_dev_feedback: Feedback | None = None
|
|
|
|
# TODO: (xiao) I think this is too concrete; we should move it into
|
|
# NOTE: Assumption
|
|
# - only runner will assign this variable
|
|
# - We will always create a new Experiment without copying previous results when we goto the next new loop.
|
|
self.running_info = RunningInfo()
|
|
self.sub_results: dict[str, float] = (
|
|
{}
|
|
) # TODO: in Kaggle, now sub results are all saved in self.result, remove this in the future.
|
|
|
|
# For parallel multi-trace support
|
|
self.local_selection: tuple[int, ...] | None = None
|
|
self.plan: ExperimentPlan | None = (
|
|
None # To store the planning information for this experiment, should be generated inside exp_gen.gen
|
|
)
|
|
self.user_instructions: UserInstructions | None = None # To store the user instructions for this experiment
|
|
|
|
def set_user_instructions(self, user_instructions: UserInstructions | None) -> None:
|
|
if user_instructions is None:
|
|
return
|
|
if not isinstance(user_instructions, UserInstructions) and isinstance(user_instructions, list):
|
|
user_instructions = UserInstructions(user_instructions)
|
|
self.user_instructions = user_instructions
|
|
for ws in self.sub_workspace_list:
|
|
if ws is not None:
|
|
ws.target_task.user_instructions = user_instructions # type: ignore[union-attr]
|
|
for task in self.sub_tasks:
|
|
task.user_instructions = user_instructions
|
|
if self.experiment_workspace is not None and self.experiment_workspace.target_task is not None:
|
|
self.experiment_workspace.target_task.user_instructions = user_instructions
|
|
|
|
@property
|
|
def result(self) -> object:
|
|
return self.running_info.result
|
|
|
|
@result.setter
|
|
def result(self, value: object) -> None:
|
|
self.running_info.result = value
|
|
|
|
# when the workspace is mutable inplace, provide support for creating checkpoints and recovering.
|
|
def create_ws_ckp(self) -> None:
|
|
if self.experiment_workspace is not None:
|
|
self.experiment_workspace.create_ws_ckp()
|
|
for ws in self.sub_workspace_list:
|
|
if ws is not None:
|
|
ws.create_ws_ckp()
|
|
|
|
def recover_ws_ckp(self) -> None:
|
|
if self.experiment_workspace is not None:
|
|
self.experiment_workspace.recover_ws_ckp()
|
|
for ws in self.sub_workspace_list:
|
|
if ws is not None:
|
|
try:
|
|
ws.recover_ws_ckp()
|
|
except RuntimeError:
|
|
# the FBWorkspace is shared between experiment_workspace and sub_workspace_list,
|
|
# so recover_ws_ckp will raise RuntimeError if a workspace is recovered twice.
|
|
print("recover_ws_ckp failed due to one workspace is recovered twice.")
|
|
|
|
|
|
ASpecificExp = TypeVar("ASpecificExp", bound=Experiment)
|
|
ASpecificPlan = TypeVar("ASpecificPlan", bound=ExperimentPlan)
|
|
|
|
TaskOrExperiment = TypeVar("TaskOrExperiment", Task, Experiment)
|
|
|
|
|
|
class Loader(ABC, Generic[TaskOrExperiment]):
|
|
@abstractmethod
|
|
def load(self, *args: Any, **kwargs: Any) -> TaskOrExperiment:
|
|
err_msg = "load method is not implemented."
|
|
raise NotImplementedError(err_msg)
|