|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import asyncio |
| 4 | +from datetime import timedelta |
| 5 | +from typing import TYPE_CHECKING |
| 6 | + |
| 7 | +from rdagent.app.data_science.conf import DS_RD_SETTING |
| 8 | +from rdagent.core.conf import RD_AGENT_SETTINGS |
| 9 | +from rdagent.core.proposal import ExpGen |
| 10 | +from rdagent.log import rdagent_logger as logger |
| 11 | +from rdagent.log.timer import RD_Agent_TIMER_wrapper, RDAgentTimer |
| 12 | +from rdagent.scenarios.data_science.loop import DataScienceRDLoop |
| 13 | +from rdagent.scenarios.data_science.proposal.exp_gen.merge import ExpGen2Hypothesis |
| 14 | +from rdagent.scenarios.data_science.proposal.exp_gen.trace_scheduler import ( |
| 15 | + RoundRobinScheduler, |
| 16 | + TraceScheduler, |
| 17 | +) |
| 18 | + |
| 19 | +if TYPE_CHECKING: |
| 20 | + from rdagent.scenarios.data_science.experiment.experiment import DSExperiment |
| 21 | + from rdagent.scenarios.data_science.proposal.exp_gen.base import DSTrace, Experiment |
| 22 | + from rdagent.utils.workflow.loop import LoopBase |
| 23 | + |
| 24 | + |
| 25 | +class ParallelMultiTraceExpGen(ExpGen): |
| 26 | + """ |
| 27 | + An experiment generation strategy that enables parallel multi-trace exploration. |
| 28 | +
|
| 29 | + This generator is designed to work with the "Attribute Injection" model. |
| 30 | + It uses a TraceScheduler to determine which parent node to expand, and |
| 31 | + injects this parent context into the experiment object itself. |
| 32 | + """ |
| 33 | + |
| 34 | + def __init__(self, *args, **kwargs): |
| 35 | + super().__init__(*args, **kwargs) |
| 36 | + # The underlying generator for creating a single experiment |
| 37 | + self.exp_gen = DataScienceRDLoop._get_exp_gen( |
| 38 | + "rdagent.scenarios.data_science.proposal.exp_gen.DSExpGen", self.scen |
| 39 | + ) |
| 40 | + self.merge_exp_gen = ExpGen2Hypothesis(self.scen) |
| 41 | + self.trace_scheduler: TraceScheduler = RoundRobinScheduler() |
| 42 | + self.max_trace_num = DS_RD_SETTING.max_trace_num |
| 43 | + |
| 44 | + def gen(self, trace: "DSTrace") -> "Experiment": |
| 45 | + raise NotImplementedError( |
| 46 | + "ParallelMultiTraceExpGen is designed for async usage, please call async_gen instead." |
| 47 | + ) |
| 48 | + |
| 49 | + async def async_gen(self, trace: DSTrace, loop: LoopBase) -> DSExperiment: |
| 50 | + """ |
| 51 | + Waits for a free execution slot, selects a parent trace using the |
| 52 | + scheduler, generates a new experiment, and injects the parent context |
| 53 | + into it before returning. |
| 54 | + """ |
| 55 | + timer: RDAgentTimer = RD_Agent_TIMER_wrapper.timer |
| 56 | + logger.info(f"Remain time: {timer.remain_time_duration}") |
| 57 | + local_selection: tuple[int, ...] = None |
| 58 | + |
| 59 | + while True: |
| 60 | + |
| 61 | + if timer.remain_time_duration >= timedelta(hours=DS_RD_SETTING.merge_hours): |
| 62 | + |
| 63 | + if DS_RD_SETTING.enable_inject_knowledge_at_root: |
| 64 | + |
| 65 | + if len(trace.hist) == 0: |
| 66 | + # set the knowledge base option to True for the first trace |
| 67 | + DS_RD_SETTING.enable_knowledge_base = True |
| 68 | + |
| 69 | + else: |
| 70 | + # set the knowledge base option back to False for the other traces |
| 71 | + DS_RD_SETTING.enable_knowledge_base = False |
| 72 | + # step 1: select the parant trace to expand |
| 73 | + # Policy: if we have fewer traces than our target, start a new one. |
| 74 | + if trace.sub_trace_count < self.max_trace_num: |
| 75 | + local_selection = trace.NEW_ROOT |
| 76 | + else: |
| 77 | + # Otherwise, use the scheduler to pick an existing trace to expand. |
| 78 | + local_selection = await self.trace_scheduler.select_trace(trace) |
| 79 | + |
| 80 | + if loop.get_unfinished_loop_cnt(loop.loop_idx) < RD_AGENT_SETTINGS.get_max_parallel(): |
| 81 | + |
| 82 | + # set the local selection as the global current selection for the trace |
| 83 | + trace.set_current_selection(local_selection) |
| 84 | + # step 2: generate the experiment with the local selection |
| 85 | + exp = self.exp_gen.gen(trace) |
| 86 | + |
| 87 | + # Inject the local selection to the experiment object |
| 88 | + exp.set_local_selection(local_selection) |
| 89 | + |
| 90 | + return exp |
| 91 | + |
| 92 | + else: |
| 93 | + # enter the merging stage |
| 94 | + # make sure the all loops are finished |
| 95 | + if loop.get_unfinished_loop_cnt(loop.loop_idx) < 1: |
| 96 | + # disable reset in merging stage |
| 97 | + DS_RD_SETTING.coding_fail_reanalyze_threshold = 100000 |
| 98 | + DS_RD_SETTING.consecutive_errors = 100000 |
| 99 | + |
| 100 | + leaves: list[int] = trace.get_leaves() |
| 101 | + if len(leaves) < 2: |
| 102 | + trace.set_current_selection(selection=(-1,)) |
| 103 | + return self.exp_gen.gen(trace) |
| 104 | + else: |
| 105 | + selection = (leaves[0],) |
| 106 | + if trace.sota_exp_to_submit is not None: |
| 107 | + if trace.is_parent(trace.exp2idx(trace.sota_exp_to_submit), leaves[1]): |
| 108 | + selection = (leaves[1],) |
| 109 | + trace.set_current_selection(selection) |
| 110 | + return self.merge_exp_gen.gen(trace) |
| 111 | + |
| 112 | + await asyncio.sleep(1) |
0 commit comments