-
Notifications
You must be signed in to change notification settings - Fork 372
Expand file tree
/
Copy pathdecoder.py
More file actions
1520 lines (1395 loc) · 61.3 KB
/
decoder.py
File metadata and controls
1520 lines (1395 loc) · 61.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
# pyre-strict
import copy
import datetime
import json
from collections import OrderedDict
from collections.abc import Mapping
from dataclasses import dataclass
from enum import Enum
from functools import partial
from inspect import isclass
from io import StringIO
from logging import Logger
from typing import Any, TypeVar
import numpy as np
import pandas as pd
import torch
from ax.adapter.registry import GeneratorRegistryBase
from ax.core.arm import Arm
from ax.core.base_trial import BaseTrial
from ax.core.data import Data
from ax.core.experiment import Experiment
from ax.core.generator_run import GeneratorRun
from ax.core.multi_type_experiment import MultiTypeExperiment
from ax.core.objective import Objective
from ax.core.optimization_config import OptimizationConfig
from ax.core.parameter import Parameter
from ax.core.parameter_constraint import ParameterConstraint
from ax.core.search_space import SearchSpace
from ax.exceptions.storage import JSON_STORAGE_DOCS_SUFFIX, JSONDecodeError
from ax.generation_strategy.generation_node_input_constructors import (
InputConstructorPurpose,
)
from ax.generation_strategy.generation_strategy import (
GenerationNode,
GenerationStep,
GenerationStrategy,
)
from ax.generation_strategy.generator_spec import GeneratorSpec
from ax.generation_strategy.transition_criterion import (
MinTrials,
PausingCriterion,
TransitionCriterion,
)
from ax.generators.torch.botorch_modular.generator import BoTorchGenerator
from ax.generators.torch.botorch_modular.surrogate import Surrogate, SurrogateSpec
from ax.generators.torch.botorch_modular.utils import ModelConfig
from ax.storage.json_store.decoders import (
_cast_parameter_value,
batch_trial_from_json,
botorch_component_from_json,
tensor_from_json,
trial_from_json,
)
from ax.storage.json_store.registry import (
CORE_CLASS_DECODER_REGISTRY,
CORE_DECODER_REGISTRY,
)
from ax.storage.utils import data_by_trial_to_data
from ax.utils.common.logger import get_logger
from ax.utils.common.serialization import (
extract_init_args,
SerializationMixin,
TClassDecoderRegistry,
TDecoderRegistry,
)
from ax.utils.common.typeutils_torch import torch_type_from_str
from botorch.utils.types import DEFAULT
from pyre_extensions import assert_is_instance, none_throws
T = TypeVar("T")
logger: Logger = get_logger(__name__)
def _cast_arm_parameters(arm: Arm, search_space: SearchSpace) -> None:
"""Cast arm parameter values to the appropriate Python type.
This is necessary because JSON may deserialize values as different types
(e.g., ints as floats). This function modifies the arm in place.
Args:
arm: The arm whose parameter values should be cast.
search_space: The search space containing parameter type information.
"""
for param_name, param_value in arm._parameters.items():
if param_name in search_space.parameters:
parameter = search_space.parameters[param_name]
arm._parameters[param_name] = _cast_parameter_value(
param_value, parameter.parameter_type
)
def _raise_on_legacy_callable_refs(kwarg_dict: dict[str, Any]) -> dict[str, Any]:
"""Returns kwarg_dict unchanged if no legacy callable refs are present.
Raises:
JSONDecodeError: If any value is a legacy encoded callable reference.
"""
for k, v in kwarg_dict.items():
if isinstance(v, dict) and v.get("is_callable_as_path", False):
raise JSONDecodeError(
f"Legacy callable reference '{k}' cannot be decoded. "
"Callable serialization is not supported."
)
return kwarg_dict
# Deprecated generators registry entries and their replacements.
# Used below in `_update_deprecated_model_registry`.
_DEPRECATED_GENERATOR_TO_REPLACEMENT: dict[str, str] = {
"GPEI": "BOTORCH_MODULAR",
"MOO": "BOTORCH_MODULAR",
"FULLYBAYESIAN": "SAASBO",
"FULLYBAYESIANMOO": "SAASBO",
"FULLYBAYESIAN_MTGP": "SAAS_MTGP",
"FULLYBAYESIANMOO_MTGP": "SAAS_MTGP",
"ST_MTGP_LEGACY": "ST_MTGP",
"ST_MTGP_NEHVI": "ST_MTGP",
"CONTEXT_SACBO": "BOTORCH_MODULAR",
"LEGACY_BOTORCH": "BOTORCH_MODULAR",
}
# Deprecated generator kwargs, to be removed from GStep / GNodes.
_DEPRECATED_GENERATOR_KWARGS: tuple[str, ...] = (
"fit_on_update",
"fit_out_of_design",
"fit_abandoned",
"fit_only_completed_map_metrics",
"torch_dtype",
"status_quo_name",
"status_quo_features",
)
# Deprecated node input constructors, removed from GNodes.
# NOTE: These are the enum keys, which are typically upper-case.
_DEPRECATED_NODE_INPUT_CONSTRUCTORS: tuple[str, ...] = ("STATUS_QUO_FEATURES",)
@dataclass
class RegistryKwargs:
decoder_registry: TDecoderRegistry
class_decoder_registry: TClassDecoderRegistry
def object_from_json(
object_json: Any,
decoder_registry: TDecoderRegistry = CORE_DECODER_REGISTRY,
class_decoder_registry: TClassDecoderRegistry = CORE_CLASS_DECODER_REGISTRY,
) -> Any:
"""Recursively load objects from a JSON-serializable dictionary."""
registry_kwargs = RegistryKwargs(
decoder_registry=decoder_registry, class_decoder_registry=class_decoder_registry
)
_object_from_json = partial(object_from_json, **vars(registry_kwargs))
if type(object_json) in (str, int, float, bool, type(None)) or isinstance(
object_json, Enum
):
return object_json
elif isinstance(object_json, list):
return [_object_from_json(i) for i in object_json]
elif isinstance(object_json, tuple):
return tuple(_object_from_json(i) for i in object_json)
elif isinstance(object_json, dict):
if "__type" not in object_json:
# this is just a regular dictionary, e.g. the one in Parameter
# containing parameterizations
result = {}
for k, v in object_json.items():
# Convert "null" string back to None for dictionary keys
# This handles the case where _trial_type_to_runner has None keys
# that get serialized to "null" strings in JSON
key = None if k == "null" else k
result[key] = _object_from_json(v)
return result
_type = object_json.pop("__type")
if _type == "datetime":
return datetime.datetime.strptime(
object_json["value"], "%Y-%m-%d %H:%M:%S.%f"
)
elif _type == "OrderedDict":
return OrderedDict(
[(k, _object_from_json(v)) for k, v in object_json["value"]]
)
elif _type == "DataFrame":
# Need dtype=False, otherwise infers arm_names like "4_1"
# should be int 41
return pd.read_json(StringIO(object_json["value"]), dtype=False)
elif _type == "ndarray":
return np.array(object_json["value"])
elif _type == "Tensor":
return tensor_from_json(json=object_json)
elif _type.startswith("torch"):
# Torch types will be encoded as "torch_<type_name>", so we drop prefix
return torch_type_from_str(
identifier=object_json["value"], type_name=_type[6:]
)
elif _type == "ListSurrogate":
return surrogate_from_list_surrogate_json(
list_surrogate_json=object_json, **vars(registry_kwargs)
)
elif _type == "set":
return set(object_json["value"])
# Used for decoding classes (not objects).
elif _type in class_decoder_registry:
return class_decoder_registry[_type](object_json)
elif _type == "GeneratorRunStruct":
object_json.pop("weight", None) # Deprecated.
gr_json = object_json["generator_run"]
assert gr_json.pop("__type") == "GeneratorRun"
return generator_run_from_json(object_json=gr_json, **vars(registry_kwargs))
elif _type not in decoder_registry:
err = (
f"The JSON dictionary passed to `object_from_json` has a type "
f"{_type} that is not registered with a corresponding class in "
f"DECODER_REGISTRY. {JSON_STORAGE_DOCS_SUFFIX}"
)
raise JSONDecodeError(err)
# pyre-fixme[9, 24]: Generic type `type` expects 1 type parameter, use
# `typing.Type[<base type>]` to avoid runtime subscripting errors.
_class: type = decoder_registry[_type]
if isclass(_class) and issubclass(_class, Enum):
name = object_json["name"]
if issubclass(_class, GeneratorRegistryBase):
name = _update_deprecated_model_registry(name=name)
# to access enum members by name, use item access
return _class[name]
elif isclass(_class) and issubclass(_class, torch.nn.Module):
return botorch_component_from_json(botorch_class=_class, json=object_json)
elif _class == GeneratorRun:
return generator_run_from_json(
object_json=object_json, **vars(registry_kwargs)
)
# Backward compatibility. `GenerationStep`-s are now just encoded as
# `GenerationNode`-s, but we still need to support loading old GSteps.
elif _class == GenerationStep:
return generation_step_from_json(
generation_step_json=object_json, **vars(registry_kwargs)
)
elif _class == GenerationNode:
return generation_node_from_json(
generation_node_json=object_json, **vars(registry_kwargs)
)
elif _class == GeneratorSpec:
return generator_spec_from_json(
generator_spec_json=object_json, **vars(registry_kwargs)
)
elif _class == GenerationStrategy:
return generation_strategy_from_json(
generation_strategy_json=object_json, **vars(registry_kwargs)
)
elif _class == MultiTypeExperiment:
return multi_type_experiment_from_json(
object_json=object_json, **vars(registry_kwargs)
)
elif _class == Experiment:
return experiment_from_json(
object_json=object_json, **vars(registry_kwargs)
)
elif _class == SearchSpace:
return search_space_from_json(
search_space_json=object_json, **vars(registry_kwargs)
)
elif _class == Objective:
return objective_from_json(object_json=object_json, **vars(registry_kwargs))
elif _class in (SurrogateSpec, Surrogate, ModelConfig):
if "input_transform" in object_json:
(
input_transform_classes_json,
input_transform_options_json,
) = get_input_transform_json_components(
input_transforms_json=object_json.pop("input_transform"),
**vars(registry_kwargs),
)
object_json["input_transform_classes"] = input_transform_classes_json
object_json["input_transform_options"] = input_transform_options_json
if "outcome_transform" in object_json:
(
outcome_transform_classes_json,
outcome_transform_options_json,
) = get_outcome_transform_json_components(
outcome_transforms_json=object_json.pop("outcome_transform"),
**vars(registry_kwargs),
)
object_json["outcome_transform_classes"] = (
outcome_transform_classes_json
)
object_json["outcome_transform_options"] = (
outcome_transform_options_json
)
elif isclass(_class) and issubclass(_class, TransitionCriterion):
# TransitionCriterion may contain nested Ax objects (TrialStatus, etc.)
# that need recursive deserialization via object_from_json.
return transition_criterion_from_json(
transition_criterion_class=_class,
object_json=object_json,
**vars(registry_kwargs),
)
elif isclass(_class) and issubclass(_class, PausingCriterion):
return pausing_criterion_from_json(
pausing_criterion_class=_class,
object_json=object_json,
**vars(registry_kwargs),
)
elif isclass(_class) and issubclass(_class, SerializationMixin):
# Special handling for Data backward compatibility
if _class is Data:
data_json_str = object_json.get("df", {}).get("value", "")
data_json = json.loads(data_json_str)
if data_json and "metric_signature" not in data_json:
object_json["df"]["value"] = (
_update_data_json_with_metric_signature(
object_json["df"]["value"]
)
)
return _class(
# Note: we do not recursively call object_from_json here again as
# that would invalidate design principles behind deserialize_init_args.
# Any Ax class that needs serialization and who's init args include
# another Ax class that needs serialization should implement its own
# _to_json and _from_json methods and register them appropriately.
**_class.deserialize_init_args(
args=object_json, **vars(registry_kwargs)
)
)
if _class in (BoTorchGenerator, Surrogate):
# Updates deprecated surrogate spec related inputs.
object_json = _sanitize_surrogate_spec_input(object_json=object_json)
if _class is Surrogate:
object_json = _sanitize_legacy_surrogate_inputs(object_json=object_json)
if _class is SurrogateSpec:
object_json = _sanitize_inputs_to_surrogate_spec(object_json=object_json)
if isclass(_class) and issubclass(_class, OptimizationConfig):
object_json.pop("risk_measure", None) # Deprecated.
return ax_class_from_json_dict(
_class=_class, object_json=object_json, **vars(registry_kwargs)
)
else:
err = (
f"The object {object_json} passed to `object_from_json` has an "
f"unsupported type: {type(object_json)}."
)
raise JSONDecodeError(err)
def ax_class_from_json_dict(
# pyre-fixme[24]: Generic type `type` expects 1 type parameter, use
# `typing.Type` to avoid runtime subscripting errors.
_class: type,
object_json: dict[str, Any],
decoder_registry: TDecoderRegistry = CORE_DECODER_REGISTRY,
class_decoder_registry: TClassDecoderRegistry = CORE_CLASS_DECODER_REGISTRY,
) -> Any:
"""Reinstantiates an Ax class registered in `DECODER_REGISTRY` from a JSON
dict.
"""
return _class(
**{
k: object_from_json(
v,
decoder_registry=decoder_registry,
class_decoder_registry=class_decoder_registry,
)
for k, v in object_json.items()
}
)
def generator_run_from_json(
object_json: dict[str, Any],
decoder_registry: TDecoderRegistry = CORE_DECODER_REGISTRY,
class_decoder_registry: TClassDecoderRegistry = CORE_CLASS_DECODER_REGISTRY,
) -> GeneratorRun:
"""Load Ax GeneratorRun from JSON."""
time_created_json = object_json.pop("time_created")
type_json = object_json.pop("generator_run_type")
object_json.pop("index", None) # Deprecated.
object_json.pop("generation_step_index", None) # Deprecated.
# Remove `objective_thresholds` to avoid issues with registries, since
# `ObjectiveThreshold` depend on `Metric` objects.
object_json.pop("objective_thresholds", None)
# Backwards compatibility: handle old field names
if "model_key" in object_json:
object_json["generator_key"] = object_json.pop("model_key")
if "model_kwargs" in object_json:
object_json["generator_kwargs"] = object_json.pop("model_kwargs")
if "bridge_kwargs" in object_json:
object_json["adapter_kwargs"] = object_json.pop("bridge_kwargs")
if "model_state_after_gen" in object_json:
object_json["generator_state_after_gen"] = object_json.pop(
"model_state_after_gen"
)
generator_run = GeneratorRun(
**{
k: object_from_json(
v,
decoder_registry=decoder_registry,
class_decoder_registry=class_decoder_registry,
)
for k, v in object_json.items()
}
)
# NOTE: JSON converts all tuples to lists, and we need to convert them back. This is
# an ad hoc fix, though.
if isinstance(generator_run._best_arm_predictions, list):
arm, arm_prediction = generator_run._best_arm_predictions
if arm_prediction is not None and isinstance(arm_prediction, list):
arm_prediction = tuple(arm_prediction)
generator_run._best_arm_predictions = (arm, arm_prediction)
if isinstance(generator_run._model_predictions, list):
generator_run._model_predictions = tuple(generator_run._model_predictions)
# Remove deprecated kwargs from generator kwargs & adapter kwargs.
if generator_run._generator_kwargs is not None:
generator_run._generator_kwargs = {
k: v
for k, v in generator_run._generator_kwargs.items()
if k not in _DEPRECATED_GENERATOR_KWARGS
}
if generator_run._adapter_kwargs is not None:
generator_run._adapter_kwargs = {
k: v
for k, v in generator_run._adapter_kwargs.items()
if k not in _DEPRECATED_GENERATOR_KWARGS
}
generator_run._time_created = object_from_json(
time_created_json,
decoder_registry=decoder_registry,
class_decoder_registry=class_decoder_registry,
)
generator_run._generator_run_type = object_from_json(
type_json,
decoder_registry=decoder_registry,
class_decoder_registry=class_decoder_registry,
)
return generator_run
def _criterion_from_json(
criterion_class: type[T],
object_json: dict[str, Any],
decoder_registry: TDecoderRegistry = CORE_DECODER_REGISTRY,
class_decoder_registry: TClassDecoderRegistry = CORE_CLASS_DECODER_REGISTRY,
) -> T:
"""Generic helper to load criterion objects from JSON.
Handles recursive deserialization of nested Ax objects and filters
to valid constructor arguments for backwards compatibility.
"""
decoded = {
key: object_from_json(
object_json=value,
decoder_registry=decoder_registry,
class_decoder_registry=class_decoder_registry,
)
for key, value in object_json.items()
}
init_args = extract_init_args(args=decoded, class_=criterion_class)
return criterion_class(**init_args)
def transition_criterion_from_json(
transition_criterion_class: type[TransitionCriterion],
object_json: dict[str, Any],
decoder_registry: TDecoderRegistry = CORE_DECODER_REGISTRY,
class_decoder_registry: TClassDecoderRegistry = CORE_CLASS_DECODER_REGISTRY,
) -> TransitionCriterion:
"""Load TransitionCriterion from JSON."""
# Handle deprecated MinimumTrialsInStatus -> MinTrials conversion
if transition_criterion_class is MinTrials and "status" in object_json:
logger.warning(
"`MinimumTrialsInStatus` has been deprecated and removed. "
"Converting to `MinTrials` with equivalent functionality."
)
status = object_from_json(
object_json=object_json.get("status"),
decoder_registry=decoder_registry,
class_decoder_registry=class_decoder_registry,
)
return MinTrials(
threshold=object_json.get("threshold"),
only_in_statuses=[status],
transition_to=object_json.get("transition_to"),
use_all_trials_in_exp=True,
)
return _criterion_from_json(
criterion_class=transition_criterion_class,
object_json=object_json,
decoder_registry=decoder_registry,
class_decoder_registry=class_decoder_registry,
)
def pausing_criterion_from_json(
pausing_criterion_class: type[PausingCriterion],
object_json: dict[str, Any],
decoder_registry: TDecoderRegistry = CORE_DECODER_REGISTRY,
class_decoder_registry: TClassDecoderRegistry = CORE_CLASS_DECODER_REGISTRY,
) -> PausingCriterion:
"""Load PausingCriterion from JSON."""
return _criterion_from_json(
criterion_class=pausing_criterion_class,
object_json=object_json,
decoder_registry=decoder_registry,
class_decoder_registry=class_decoder_registry,
)
def search_space_from_json(
search_space_json: dict[str, Any],
decoder_registry: TDecoderRegistry = CORE_DECODER_REGISTRY,
class_decoder_registry: TClassDecoderRegistry = CORE_CLASS_DECODER_REGISTRY,
) -> SearchSpace:
"""Load a SearchSpace from JSON.
This function is necessary due to the coupled loading of SearchSpace
and parameter constraints.
"""
parameters = object_from_json(
search_space_json.pop("parameters"),
decoder_registry=decoder_registry,
class_decoder_registry=class_decoder_registry,
)
json_param_constraints = search_space_json.pop("parameter_constraints")
return SearchSpace(
parameters=parameters,
parameter_constraints=parameter_constraints_from_json(
parameter_constraint_json=json_param_constraints,
parameters=parameters,
decoder_registry=decoder_registry,
class_decoder_registry=class_decoder_registry,
),
)
def parameter_constraints_from_json(
parameter_constraint_json: list[dict[str, Any]],
parameters: list[Parameter],
decoder_registry: TDecoderRegistry = CORE_DECODER_REGISTRY,
class_decoder_registry: TClassDecoderRegistry = CORE_CLASS_DECODER_REGISTRY,
) -> list[ParameterConstraint]:
"""Load ParameterConstraints from JSON.
Order and SumConstraint are tied to a search space,
and require that SearchSpace's parameters to be passed in for decoding.
Args:
parameter_constraint_json: JSON representation of parameter constraints.
parameters: Parameter definitions for decoding via parameter names.
Returns:
parameter_constraints: Python classes for parameter constraints.
"""
parameter_constraints = []
for constraint in parameter_constraint_json:
# For backwards compatibility
if constraint["__type"] == "OrderConstraint":
parameter_constraints.append(
ParameterConstraint(
inequality=(
f"{constraint['lower_name']} <= {constraint['upper_name']}"
)
)
)
elif constraint["__type"] == "SumConstraint":
parameter_constraints.append(
ParameterConstraint(
inequality=" + ".join(constraint["parameter_names"])
+ ("<=" if constraint["is_upper_bound"] else ">=")
+ str(constraint["bound"])
)
)
else:
# Respect legacy json representation of parameter constraints
if "constraint_dict" in constraint and "bound" in constraint:
expr = " + ".join(
f"{coeff} * {param}"
for param, coeff in constraint["constraint_dict"].items()
)
parameter_constraints.append(
ParameterConstraint(
inequality=f"{expr} <= {constraint['bound']}",
)
)
else:
parameter_constraints.append(
object_from_json(
constraint,
decoder_registry=decoder_registry,
class_decoder_registry=class_decoder_registry,
)
)
return parameter_constraints
def trials_from_json(
experiment: Experiment,
trials_json: dict[str, Any],
decoder_registry: TDecoderRegistry = CORE_DECODER_REGISTRY,
class_decoder_registry: TClassDecoderRegistry = CORE_CLASS_DECODER_REGISTRY,
) -> dict[int, BaseTrial]:
"""Load Ax Trials from JSON."""
loaded_trials = {}
for index, trial_json in trials_json.items():
is_trial = trial_json["__type"] == "Trial"
trial_json = {
k: object_from_json(
v,
decoder_registry=decoder_registry,
class_decoder_registry=class_decoder_registry,
)
for k, v in trial_json.items()
if k != "__type"
}
if "generator_run_structs" in trial_json:
# `GeneratorRunStruct` (deprecated) will be decoded into a `GeneratorRun`,
# so all we have to do here is change the key it's stored under.
trial_json["generator_runs"] = trial_json.pop("generator_run_structs")
trial_json.pop("status_quo_weight_override", None) # Deprecated.
loaded_trials[int(index)] = (
trial_from_json(experiment=experiment, **trial_json)
if is_trial
else batch_trial_from_json(experiment=experiment, **trial_json)
)
return loaded_trials
def data_from_json(
data_by_trial_json: Mapping[str, Any] | Mapping[int, Any],
decoder_registry: TDecoderRegistry = CORE_DECODER_REGISTRY,
class_decoder_registry: TClassDecoderRegistry = CORE_CLASS_DECODER_REGISTRY,
) -> Data:
"""
Load Ax Data from JSON.
Experiments used to have `_data_by_trial` is in the format
`{trial_index: {timestamp: Data}}`; they now have a single `Data`. Data is
still serialized via the old format (we intend to overhaul storage shortly).
This function
- combines multiple Datas for the same trial index into one, if there are
multiple, keeping only the trial_index-arm_name-metric_name[-step]
observation if it appears with multiple timestamps.
- concatenates the data for each trial into one
Produce `None` if `_data_by_trial` is empty. We do this rather than creating
an empty data since we don't know the type of the data in that case.
"""
data_by_trial = object_from_json(
data_by_trial_json,
decoder_registry=decoder_registry,
class_decoder_registry=class_decoder_registry,
)
# hack necessary because Python's json module converts dictionary
# keys to strings: https://stackoverflow.com/q/1450957
deserialized = {
int(k): OrderedDict({int(k2): v2 for k2, v2 in v.items()})
for k, v in data_by_trial.items()
}
return data_by_trial_to_data(data_by_trial=deserialized)
def multi_type_experiment_from_json(
object_json: dict[str, Any],
decoder_registry: TDecoderRegistry = CORE_DECODER_REGISTRY,
class_decoder_registry: TClassDecoderRegistry = CORE_CLASS_DECODER_REGISTRY,
) -> MultiTypeExperiment:
"""Load AE MultiTypeExperiment from JSON."""
experiment_info = _get_experiment_info(object_json)
_metric_to_canonical_name = object_json.pop("_metric_to_canonical_name")
_metric_to_trial_type = object_json.pop("_metric_to_trial_type")
_trial_type_to_runner = object_from_json(
object_json.pop("_trial_type_to_runner"),
decoder_registry=decoder_registry,
class_decoder_registry=class_decoder_registry,
)
tracking_metrics = object_from_json(
object_json.pop("tracking_metrics"),
decoder_registry=decoder_registry,
class_decoder_registry=class_decoder_registry,
)
# not relevant to multi type experiment
del object_json["runner"]
kwargs = {
k: object_from_json(
v,
decoder_registry=decoder_registry,
class_decoder_registry=class_decoder_registry,
)
for k, v in object_json.items()
}
kwargs["default_runner"] = _trial_type_to_runner[object_json["default_trial_type"]]
experiment = MultiTypeExperiment(**kwargs)
for metric in tracking_metrics:
experiment._metrics[metric.name] = metric
experiment._metric_to_canonical_name = _metric_to_canonical_name
experiment._metric_to_trial_type = _metric_to_trial_type
experiment._trial_type_to_runner = _trial_type_to_runner
# Rebuild _trial_type_to_metric_names from _metric_to_trial_type
trial_type_to_metric_names: dict[str, set[str]] = {}
for metric_name, trial_type in _metric_to_trial_type.items():
trial_type_to_metric_names.setdefault(trial_type, set()).add(metric_name)
experiment._trial_type_to_metric_names = trial_type_to_metric_names
_load_experiment_info(
exp=experiment,
exp_info=experiment_info,
decoder_registry=decoder_registry,
class_decoder_registry=class_decoder_registry,
)
return experiment
def experiment_from_json(
object_json: dict[str, Any],
decoder_registry: TDecoderRegistry = CORE_DECODER_REGISTRY,
class_decoder_registry: TClassDecoderRegistry = CORE_CLASS_DECODER_REGISTRY,
) -> Experiment:
"""Load Ax Experiment from JSON."""
experiment_info = _get_experiment_info(object_json)
_trial_type_to_runner_json = object_json.pop("_trial_type_to_runner", None)
_trial_type_to_runner = (
object_from_json(
_trial_type_to_runner_json,
decoder_registry=decoder_registry,
class_decoder_registry=class_decoder_registry,
)
if _trial_type_to_runner_json is not None
else None
)
experiment = Experiment(
**{
k: object_from_json(
v,
decoder_registry=decoder_registry,
class_decoder_registry=class_decoder_registry,
)
for k, v in object_json.items()
}
)
experiment._arms_by_name = {}
if _trial_type_to_runner is not None:
experiment._trial_type_to_runner = _trial_type_to_runner
_load_experiment_info(
exp=experiment,
exp_info=experiment_info,
decoder_registry=decoder_registry,
class_decoder_registry=class_decoder_registry,
)
return experiment
def _get_experiment_info(object_json: dict[str, Any]) -> dict[str, Any]:
"""Returns basic information from `Experiment` object_json."""
return {
"time_created_json": object_json.pop("time_created"),
"trials_json": object_json.pop("trials"),
"experiment_type_json": object_json.pop("experiment_type"),
"data_by_trial_json": object_json.pop("data_by_trial"),
}
def _load_experiment_info(
exp: Experiment,
exp_info: dict[str, Any],
decoder_registry: TDecoderRegistry = CORE_DECODER_REGISTRY,
class_decoder_registry: TClassDecoderRegistry = CORE_CLASS_DECODER_REGISTRY,
) -> None:
"""Loads `Experiment` object with basic information."""
exp._time_created = object_from_json(
exp_info.get("time_created_json"),
decoder_registry=decoder_registry,
class_decoder_registry=class_decoder_registry,
)
exp._trials = trials_from_json(
exp,
exp_info.get("trials_json"),
decoder_registry=decoder_registry,
class_decoder_registry=class_decoder_registry,
)
exp._experiment_type = object_from_json(
exp_info.get("experiment_type_json"),
decoder_registry=decoder_registry,
class_decoder_registry=class_decoder_registry,
)
exp.data = data_from_json(
exp_info.get("data_by_trial_json", {}),
decoder_registry=decoder_registry,
class_decoder_registry=class_decoder_registry,
)
for trial in exp._trials.values():
for arm in trial.arms:
# Cast arm parameter values to the appropriate type based on the
# search space parameter types. This is necessary because JSON may
# deserialize values as different types (e.g., ints as floats).
_cast_arm_parameters(arm, exp.search_space)
exp._register_arm(arm)
if trial.ttl_seconds is not None:
exp._trials_have_ttl = True
if exp.status_quo is not None:
sq = none_throws(exp.status_quo)
# Cast status_quo arm parameter values as well.
_cast_arm_parameters(sq, exp.search_space)
exp._register_arm(sq)
def _convert_generation_step_keys_for_backwards_compatibility(
object_json: dict[str, Any],
) -> dict[str, Any]:
"""If necessary, converts keys in a JSON dict representing a `GenerationStep`
for backwards compatibility.
"""
# NOTE: this is a hack to make generation steps able to load after the
# renaming of generation step fields to be in terms of 'trials' rather than
# 'arms'.
keys = list(object_json.keys())
for k in keys:
if "arms" in k:
object_json[k.replace("arms", "trials")] = object_json.pop(k)
if k == "recommended_max_parallelism":
object_json["max_parallelism"] = object_json.pop(k)
return object_json
def generation_node_from_json(
generation_node_json: dict[str, Any],
decoder_registry: TDecoderRegistry = CORE_DECODER_REGISTRY,
class_decoder_registry: TClassDecoderRegistry = CORE_CLASS_DECODER_REGISTRY,
) -> GenerationNode:
"""Load GenerationNode object from JSON."""
# Due to input_constructors being a dictionary with both keys and values being of
# type enum, we must manually decode them here because object_from_json doesn't
# recursively decode dictionary key values.
decoded_input_constructors = None
if "input_constructors" in generation_node_json.keys():
decoded_input_constructors = {}
for key, value in generation_node_json.pop("input_constructors").items():
if key in _DEPRECATED_NODE_INPUT_CONSTRUCTORS:
# Skip deprecated input constructors.
continue
decoded_input_constructors[InputConstructorPurpose[key]] = object_from_json(
value,
decoder_registry=decoder_registry,
class_decoder_registry=class_decoder_registry,
)
if "model_specs" in generation_node_json:
# Check for all kwarg to support backwards compatibility.
generator_specs = generation_node_json.pop("model_specs")
else:
generator_specs = generation_node_json.pop("generator_specs")
if "node_name" in generation_node_json.keys():
name = generation_node_json.pop("node_name")
else:
name = generation_node_json.pop("name")
# Pop step_index if present (for backward compatibility), but don't use it.
# _step_index is non-persistent state that will be set by GenerationStrategy
# if needed during _validate_and_set_step_sequence.
generation_node_json.pop("step_index", None)
transition_criteria_json = generation_node_json.pop("transition_criteria", None)
generation_pausing_criteria_json = generation_node_json.pop(
"generation_pausing_criteria", None
)
# Backwards compatibility: For old experiments, TransitionCriterion with
# block_gen_if_met=True need to be migrated to PausingCriterion.
# Also, transition_to=None needs to be handled for transition criteria, these
# now point to self.
if transition_criteria_json is not None:
migrated_pausing_criteria = []
migrated_transition_criteria_json = []
for tc_json in transition_criteria_json:
tc_type = tc_json.get("__type", "")
if tc_json.get("block_gen_if_met") is True:
if tc_type == "MaxGenerationParallelism":
migrated_pausing_criteria.append(tc_json)
elif tc_type == "MinTrials":
# Copy and update type
pausing_json = tc_json.copy()
pausing_json["__type"] = "MaxTrialsAwaitingData"
migrated_pausing_criteria.append(pausing_json)
# Only keep in transition_criteria if block_transition_if_unmet=True
if tc_json.get("block_transition_if_unmet") is not True:
continue # Skip adding to transition_criteria
# handle transition_to=None
if tc_json.get("transition_to") is None:
tc_json["transition_to"] = name
migrated_transition_criteria_json.append(tc_json)
# Merge migrated criteria with any existing generation_pausing_criteria
if generation_pausing_criteria_json is None:
generation_pausing_criteria_json = migrated_pausing_criteria
else:
generation_pausing_criteria_json = (
migrated_pausing_criteria + generation_pausing_criteria_json
)
transition_criteria_json = migrated_transition_criteria_json
return GenerationNode(
name=name,
generator_specs=object_from_json(
object_json=generator_specs,
decoder_registry=decoder_registry,
class_decoder_registry=class_decoder_registry,
),
best_model_selector=object_from_json(
object_json=generation_node_json.pop("best_model_selector", None),
decoder_registry=decoder_registry,
class_decoder_registry=class_decoder_registry,
),
should_deduplicate=generation_node_json.pop("should_deduplicate", False),
# Deep-copy criteria JSON before decoding. object_from_json mutates
# its input dicts (pops "__type" keys). If the transport layer (e.g.
# FBLearner/msgpack) returns data with shared dict objects between
# transition_criteria and generation_pausing_criteria (e.g. shared
# TrialStatus dicts), decoding one would strip "__type" from the other,
# causing enums to be deserialized as plain dicts.
transition_criteria=object_from_json(
object_json=copy.deepcopy(transition_criteria_json),
decoder_registry=decoder_registry,
class_decoder_registry=class_decoder_registry,
),
pausing_criteria=object_from_json(
object_json=copy.deepcopy(generation_pausing_criteria_json),
decoder_registry=decoder_registry,
class_decoder_registry=class_decoder_registry,
),
input_constructors=decoded_input_constructors,
previous_node_name=(
generation_node_json.pop("previous_node_name")
if "previous_node_name" in generation_node_json.keys()
else None
),
trial_type=(
object_from_json(
object_json=generation_node_json.pop("trial_type"),
decoder_registry=decoder_registry,
class_decoder_registry=class_decoder_registry,
)
if "trial_type" in generation_node_json.keys()
else None
),
suggested_experiment_status=(
object_from_json(
object_json=generation_node_json.pop("suggested_experiment_status"),
decoder_registry=decoder_registry,
class_decoder_registry=class_decoder_registry,
)
if "suggested_experiment_status" in generation_node_json.keys()
else None # Default for old records without the field
),
)
def _sanitize_inputs_to_surrogate_spec(
object_json: dict[str, Any],
) -> dict[str, Any]:
"""This is a backwards compatibility helper for inputs to ``SurrogateSpec``.
It replaces the legacy inputs in the json with a ``ModelConfig`` and discards
the legacy inputs.
"""
new_json = object_json.copy()
# If no model configs are available, this spec was constructed using legacy inputs.
# We will replace it with a model config constructed from the legacy inputs.
# It is possible that both inputs are available, in which case we will discard the
# legacy inputs and only keep the existing model config.
model_configs = new_json.get("model_configs", [])
new_config = [
{