Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ax sweeper generation strategy feature #2978

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion plugins/hydra_ax_sweeper/hydra_plugins/hydra_ax_sweeper/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from ax.core import types as ax_types # type: ignore
from ax.exceptions.core import SearchSpaceExhausted # type: ignore
from ax.service.ax_client import AxClient # type: ignore
from ax.modelbridge.generation_strategy import GenerationStrategy, GenerationStep # type: ignore
from ax.modelbridge.registry import Models # type: ignore
from hydra.core.override_parser.overrides_parser import OverridesParser
from hydra.core.override_parser.types import IntervalSweep, Override, Transformer
from hydra.core.plugins import Plugins
Expand All @@ -15,7 +17,7 @@
from omegaconf import DictConfig, OmegaConf

from ._earlystopper import EarlyStopper
from .config import AxConfig, ClientConfig, ExperimentConfig
from .config import AxConfig, ClientConfig, ExperimentConfig, GenerationStrategyConfig

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -117,6 +119,7 @@ def __init__(self, ax_config: AxConfig, max_batch_size: Optional[int]):
epsilon=ax_config.early_stop.epsilon,
minimize=ax_config.early_stop.minimize,
)
self.generation_strategy: GenerationStrategyConfig = ax_config.generation_strategy
self.ax_client_config: ClientConfig = ax_config.client
self.max_trials = ax_config.max_trials
self.ax_params: DictConfig = OmegaConf.create({})
Expand Down Expand Up @@ -238,6 +241,30 @@ def sweep_over_batches(
trial_index=batch[idx].trial_index, raw_data=val
)

def _create_generation_strategy(self) -> GenerationStrategy:
"""Create an Ax GenerationStrategy from configuration."""
steps = []
for step_config in self.generation_strategy.steps:
# Convert string model name to Models enum if necessary
model = step_config.model
if isinstance(model, str):
model = getattr(Models, model.upper())

step = GenerationStep(
model=model,
num_trials=step_config.num_trials,
max_parallelism=step_config.max_parallelism,
model_kwargs=step_config.model_kwargs,
model_gen_kwargs=step_config.model_gen_kwargs,
)
steps.append(step)

return GenerationStrategy(
steps=steps,
name=self.generation_strategy.name,
)


def setup_ax_client(self, arguments: List[str]) -> AxClient:
"""Method to setup the Ax Client"""
parameters: List[Dict[Any, Any]] = []
Expand Down Expand Up @@ -265,9 +292,14 @@ def setup_ax_client(self, arguments: List[str]) -> AxClient:
log.info(
f"AxSweeper is optimizing the following parameters: {encoder_parameters_into_string(parameters)}"
)

generation_strategy = self._create_generation_strategy()
log.info(f"AxSweeper is optimizing the following generation strategy: {generation_strategy}")

ax_client = AxClient(
verbose_logging=self.ax_client_config.verbose_logging,
random_seed=self.ax_client_config.random_seed,
generation_strategy=generation_strategy,
)
ax_client.create_experiment(parameters=parameters, **self.experiment)

Expand Down
20 changes: 20 additions & 0 deletions plugins/hydra_ax_sweeper/hydra_plugins/hydra_ax_sweeper/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,24 @@ class ClientConfig:
random_seed: Optional[int] = None


@dataclass
class GenerationStepConfig:
"""Configuration for a single step in a generation strategy."""
model: str = "SOBOL" # e.g., "SOBOL", "GPEI", "MOO"
num_trials: int = 5 # -1 means unlimited trials
max_parallelism: int = 1
model_kwargs: Dict[str, Any] = field(default_factory=dict)
model_gen_kwargs: Dict[str, Any] = field(default_factory=dict)


@dataclass
class GenerationStrategyConfig:
"""Configuration for the generation strategy"""
steps: List[GenerationStepConfig] = field(default_factory=lambda: [GenerationStepConfig()])
name: Optional[str] = "default_strategy"



@dataclass
class AxConfig:
# max_trials is application-specific. Tune it for your use case
Expand All @@ -51,6 +69,8 @@ class AxConfig:
# is_noisy = True indicates measurements have unknown uncertainty
# is_noisy = False indicates measurements have an uncertainty of zero
is_noisy: bool = True
generation_strategy: GenerationStrategyConfig = field(default_factory=GenerationStrategyConfig)



@dataclass
Expand Down