|
3 | 3 | import os
|
4 | 4 | from copy import deepcopy
|
5 | 5 | from typing import List, Dict, Tuple, Optional, Union
|
| 6 | +from pyre_extensions import assert_is_instance |
6 | 7 |
|
7 | 8 | import numpy as np
|
8 | 9 | import torch
|
9 |
| -from packaging import version |
10 | 10 |
|
11 |
| -from ax.version import version as ax_version |
12 | 11 | from ax.core.arm import Arm
|
13 | 12 | from ax.core.batch_trial import BatchTrial
|
14 | 13 | from ax.core.multi_type_experiment import MultiTypeExperiment
|
|
22 | 21 | from ax.core.observation import ObservationFeatures
|
23 | 22 | from ax.core.generator_run import GeneratorRun
|
24 | 23 | from ax.storage.json_store.save import save_experiment
|
25 |
| -from ax.storage.metric_registry import register_metric |
26 |
| -from ax.modelbridge.factory import get_MTGP_LEGACY as get_MTGP |
| 24 | +from ax.storage.metric_registry import register_metrics |
| 25 | + |
| 26 | +from ax.modelbridge.registry import Models, ST_MTGP_trans |
| 27 | + |
| 28 | +try: |
| 29 | + # For Ax >= 0.5.0 |
| 30 | + from ax.modelbridge.transforms.derelativize import Derelativize |
| 31 | + from ax.modelbridge.transforms.convert_metric_names import ( |
| 32 | + ConvertMetricNames, |
| 33 | + ) |
| 34 | + from ax.modelbridge.transforms.trial_as_task import TrialAsTask |
| 35 | + from ax.modelbridge.transforms.stratified_standardize_y import ( |
| 36 | + StratifiedStandardizeY, |
| 37 | + ) |
| 38 | + from ax.modelbridge.transforms.task_encode import TaskChoiceToIntTaskChoice |
| 39 | + from ax.modelbridge.registry import MBM_X_trans |
| 40 | + |
| 41 | + MT_MTGP_trans = MBM_X_trans + [ |
| 42 | + Derelativize, |
| 43 | + ConvertMetricNames, |
| 44 | + TrialAsTask, |
| 45 | + StratifiedStandardizeY, |
| 46 | + TaskChoiceToIntTaskChoice, |
| 47 | + ] |
| 48 | + |
| 49 | +except ImportError: |
| 50 | + # For Ax < 0.5.0 |
| 51 | + from ax.modelbridge.registry import MT_MTGP_trans |
| 52 | + |
| 53 | +from ax.core.experiment import Experiment |
| 54 | +from ax.core.data import Data |
| 55 | +from ax.modelbridge.transforms.convert_metric_names import ( |
| 56 | + tconfig_from_mt_experiment, |
| 57 | +) |
27 | 58 |
|
28 | 59 | from optimas.generators.ax.base import AxGenerator
|
29 | 60 | from optimas.core import (
|
|
37 | 68 | )
|
38 | 69 | from .ax_metric import AxMetric
|
39 | 70 |
|
40 |
| - |
41 | 71 | # Define generator states.
|
42 | 72 | NOT_STARTED = "not_started"
|
43 | 73 | LOFI_RETURNED = "lofi_returned"
|
44 | 74 | HIFI_RETURNED = "hifi_returned"
|
45 | 75 |
|
46 | 76 |
|
| 77 | +# get_MTGP is not part of the Ax codebase, as of Ax 0.4.1, due to this PR: |
| 78 | +# https://github.com/facebook/Ax/pull/2508 |
| 79 | +# Here we use `get_MTGP` https://ax.dev/docs/tutorials/multi_task/ |
| 80 | +def get_MTGP( |
| 81 | + experiment: Experiment, |
| 82 | + data: Data, |
| 83 | + search_space: Optional[SearchSpace] = None, |
| 84 | + trial_index: Optional[int] = None, |
| 85 | + device: torch.device = torch.device("cpu"), |
| 86 | + dtype: torch.dtype = torch.double, |
| 87 | +) -> TorchModelBridge: |
| 88 | + """Instantiate a Multi-task Gaussian Process (MTGP) model. |
| 89 | +
|
| 90 | + Points are generated with EI (Expected Improvement). |
| 91 | + If the input experiment is a MultiTypeExperiment then a |
| 92 | + Multi-type Multi-task GP model will be instantiated. |
| 93 | + Otherwise, the model will be a Single-type Multi-task GP. |
| 94 | + """ |
| 95 | + if isinstance(experiment, MultiTypeExperiment): |
| 96 | + trial_index_to_type = { |
| 97 | + t.index: t.trial_type for t in experiment.trials.values() |
| 98 | + } |
| 99 | + transforms = MT_MTGP_trans |
| 100 | + transform_configs = { |
| 101 | + "TrialAsTask": { |
| 102 | + "trial_level_map": {"trial_type": trial_index_to_type} |
| 103 | + }, |
| 104 | + "ConvertMetricNames": tconfig_from_mt_experiment(experiment), |
| 105 | + } |
| 106 | + else: |
| 107 | + # Set transforms for a Single-type MTGP model. |
| 108 | + transforms = ST_MTGP_trans |
| 109 | + transform_configs = None |
| 110 | + |
| 111 | + # Choose the status quo features for the experiment from the selected |
| 112 | + # trial. If trial_index is None, we will look for a status quo from the |
| 113 | + # last experiment trial to use as a status quo for the experiment. |
| 114 | + if trial_index is None: |
| 115 | + trial_index = len(experiment.trials) - 1 |
| 116 | + elif trial_index >= len(experiment.trials): |
| 117 | + raise ValueError( |
| 118 | + "trial_index is bigger than the number of experiment trials" |
| 119 | + ) |
| 120 | + |
| 121 | + status_quo = experiment.trials[trial_index].status_quo |
| 122 | + if status_quo is None: |
| 123 | + status_quo_features = None |
| 124 | + else: |
| 125 | + status_quo_features = ObservationFeatures( |
| 126 | + parameters=status_quo.parameters, |
| 127 | + trial_index=trial_index, # pyre-ignore[6] |
| 128 | + ) |
| 129 | + |
| 130 | + return assert_is_instance( |
| 131 | + Models.ST_MTGP( |
| 132 | + experiment=experiment, |
| 133 | + search_space=search_space or experiment.search_space, |
| 134 | + data=data, |
| 135 | + transforms=transforms, |
| 136 | + transform_configs=transform_configs, |
| 137 | + torch_dtype=dtype, |
| 138 | + torch_device=device, |
| 139 | + status_quo_features=status_quo_features, |
| 140 | + ), |
| 141 | + TorchModelBridge, |
| 142 | + ) |
| 143 | + |
| 144 | + |
47 | 145 | class AxMultitaskGenerator(AxGenerator):
|
48 | 146 | """Multitask Bayesian optimization using the Ax developer API.
|
49 | 147 |
|
@@ -307,7 +405,9 @@ def _create_experiment(self) -> MultiTypeExperiment:
|
307 | 405 | )
|
308 | 406 |
|
309 | 407 | # Register metric in order to be able to save experiment to json file.
|
310 |
| - _, encoder_registry, decoder_registry = register_metric(AxMetric) |
| 408 | + _, encoder_registry, decoder_registry = register_metrics( |
| 409 | + {AxMetric: None} |
| 410 | + ) |
311 | 411 | self._encoder_registry = encoder_registry
|
312 | 412 | self._decoder_registry = decoder_registry
|
313 | 413 |
|
|
0 commit comments