Coverage for mlos_core/mlos_core/spaces/adapters/__init__.py: 100%
22 statements
« prev ^ index » next coverage.py v7.6.9, created at 2024-12-20 00:44 +0000
« prev ^ index » next coverage.py v7.6.9, created at 2024-12-20 00:44 +0000
1#
2# Copyright (c) Microsoft Corporation.
3# Licensed under the MIT License.
4#
5"""
6Basic initializer module for the mlos_core space adapters.
8Space adapters provide a mechanism for automatic transformation of the original
9:py:class:`ConfigSpace.ConfigurationSpace` provided to the optimizer into a new
10space that is more suitable for the optimizer.
12By default the :py:class:`.IdentityAdapter` is used, which does not perform any
13transformation.
14But, for instance, the :py:class:`.LlamaTuneAdapter` can be used to automatically
15transform the space to a lower dimensional one.
17See the :py:mod:`mlos_bench.optimizers.mlos_core_optimizer` module for more
18information on how to do this with :py:mod:`mlos_bench`.
20This module provides a simple :py:class:`.SpaceAdapterFactory` class to
21:py:meth:`~.SpaceAdapterFactory.create` space adapters.
23Examples
24--------
25TODO: Add example usage here.
27Notes
28-----
29See `mlos_core/spaces/adapters/README.md
30<https://github.com/microsoft/MLOS/tree/main/mlos_core/mlos_core/spaces/adapters>`_
31for additional documentation and examples in the source tree.
32"""
34from enum import Enum
35from typing import Optional, TypeVar
37import ConfigSpace
39from mlos_core.spaces.adapters.identity_adapter import IdentityAdapter
40from mlos_core.spaces.adapters.llamatune import LlamaTuneAdapter
42__all__ = [
43 "ConcreteSpaceAdapter",
44 "IdentityAdapter",
45 "LlamaTuneAdapter",
46 "SpaceAdapterFactory",
47 "SpaceAdapterType",
48]
51class SpaceAdapterType(Enum):
52 """Enumerate supported mlos_core space adapters."""
54 IDENTITY = IdentityAdapter
55 """A no-op adapter (:class:`.IdentityAdapter`) will be used."""
57 LLAMATUNE = LlamaTuneAdapter
58 """An instance of :class:`.LlamaTuneAdapter` class will be used."""
61# To make mypy happy, we need to define a type variable for each optimizer type.
62# https://github.com/python/mypy/issues/12952
63# ConcreteSpaceAdapter = TypeVar(
64# "ConcreteSpaceAdapter",
65# *[member.value for member in SpaceAdapterType],
66# )
67# To address this, we add a test for complete coverage of the enum.
68ConcreteSpaceAdapter = TypeVar(
69 "ConcreteSpaceAdapter",
70 IdentityAdapter,
71 LlamaTuneAdapter,
72)
73"""Type variable for concrete SpaceAdapter classes (e.g.,
74:class:`~mlos_core.spaces.adapters.identity_adapter.IdentityAdapter`, etc.)
75"""
78class SpaceAdapterFactory:
79 """Simple factory class for creating
80 :class:`~mlos_core.spaces.adapters.adapter.BaseSpaceAdapter`-derived objects.
81 """
83 # pylint: disable=too-few-public-methods
85 @staticmethod
86 def create(
87 *,
88 parameter_space: ConfigSpace.ConfigurationSpace,
89 space_adapter_type: SpaceAdapterType = SpaceAdapterType.IDENTITY,
90 space_adapter_kwargs: Optional[dict] = None,
91 ) -> ConcreteSpaceAdapter: # type: ignore[type-var]
92 """
93 Create a new space adapter instance, given the parameter space and potential
94 space adapter options.
96 Parameters
97 ----------
98 parameter_space : ConfigSpace.ConfigurationSpace
99 Input configuration space.
100 space_adapter_type : Optional[SpaceAdapterType]
101 Space adapter class to be used alongside the optimizer.
102 space_adapter_kwargs : Optional[dict]
103 Optional arguments passed in SpaceAdapter class constructor.
105 Returns
106 -------
107 space_adapter : ConcreteSpaceAdapter
108 Instance of concrete space adapter (e.g., None, LlamaTuneAdapter, etc.)
109 """
110 if space_adapter_type is None:
111 space_adapter_type = SpaceAdapterType.IDENTITY
112 if space_adapter_kwargs is None:
113 space_adapter_kwargs = {}
115 space_adapter: ConcreteSpaceAdapter = space_adapter_type.value(
116 orig_parameter_space=parameter_space,
117 **space_adapter_kwargs,
118 )
120 return space_adapter