Coverage for mlos_core/mlos_core/spaces/adapters/__init__.py: 100%

21 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-04-01 00:52 +0000

1# 

2# Copyright (c) Microsoft Corporation. 

3# Licensed under the MIT License. 

4# 

5""" 

6Basic initializer module for the mlos_core space adapters. 

7 

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. 

11 

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. 

16 

17See the :py:mod:`mlos_bench.optimizers.mlos_core_optimizer` module for more 

18information on how to do this with :py:mod:`mlos_bench`. 

19 

20This module provides a simple :py:class:`.SpaceAdapterFactory` class to 

21:py:meth:`~.SpaceAdapterFactory.create` space adapters. 

22 

23Generally speaking, the :py:mod:`mlos_core` package is intended to be used in 

24conjunction with the :py:mod:`mlos_bench` package, which provides a higher-level 

25interface for running benchmarks and autotuning experiments. 

26 

27See Also 

28-------- 

29mlos_bench.optimizers.mlos_core_optimizer : 

30 The mlos_bench Optimizer class that uses the mlos_core Optimizers and 

31 SpaceAdapters to run autotuning experiments. 

32 

33Notes 

34----- 

35See `mlos_core/spaces/adapters/README.md 

36<https://github.com/microsoft/MLOS/tree/main/mlos_core/mlos_core/spaces/adapters>`_ 

37for additional documentation and examples in the source tree. 

38""" 

39 

40from enum import Enum 

41 

42import ConfigSpace 

43 

44from mlos_core.spaces.adapters.identity_adapter import IdentityAdapter 

45from mlos_core.spaces.adapters.llamatune import LlamaTuneAdapter 

46 

47__all__ = [ 

48 "ConcreteSpaceAdapter", 

49 "IdentityAdapter", 

50 "LlamaTuneAdapter", 

51 "SpaceAdapterFactory", 

52 "SpaceAdapterType", 

53] 

54 

55 

56class SpaceAdapterType(Enum): 

57 """Enumerate supported mlos_core space adapters.""" 

58 

59 IDENTITY = IdentityAdapter 

60 """A no-op adapter (:class:`.IdentityAdapter`) will be used.""" 

61 

62 LLAMATUNE = LlamaTuneAdapter 

63 """An instance of :class:`.LlamaTuneAdapter` class will be used.""" 

64 

65 

66ConcreteSpaceAdapter = IdentityAdapter | LlamaTuneAdapter 

67"""Type alias for concrete SpaceAdapter classes (e.g., 

68:class:`~mlos_core.spaces.adapters.identity_adapter.IdentityAdapter`, etc.) 

69""" 

70 

71 

72class SpaceAdapterFactory: 

73 """Simple factory class for creating 

74 :class:`~mlos_core.spaces.adapters.adapter.BaseSpaceAdapter`-derived objects. 

75 """ 

76 

77 # pylint: disable=too-few-public-methods 

78 

79 @staticmethod 

80 def create( 

81 *, 

82 parameter_space: ConfigSpace.ConfigurationSpace, 

83 space_adapter_type: SpaceAdapterType | None = SpaceAdapterType.IDENTITY, 

84 space_adapter_kwargs: dict | None = None, 

85 ) -> ConcreteSpaceAdapter: 

86 """ 

87 Create a new space adapter instance, given the parameter space and potential 

88 space adapter options. 

89 

90 Parameters 

91 ---------- 

92 parameter_space : ConfigSpace.ConfigurationSpace 

93 Input configuration space. 

94 space_adapter_type : SpaceAdapterType | None 

95 Space adapter class to be used alongside the optimizer. 

96 space_adapter_kwargs : dict | None 

97 Optional arguments passed in SpaceAdapter class constructor. 

98 

99 Returns 

100 ------- 

101 space_adapter : ConcreteSpaceAdapter 

102 Instance of concrete space adapter (e.g., None, LlamaTuneAdapter, etc.) 

103 """ 

104 if space_adapter_type is None: 

105 space_adapter_type = SpaceAdapterType.IDENTITY 

106 if space_adapter_kwargs is None: 

107 space_adapter_kwargs = {} 

108 

109 space_adapter: ConcreteSpaceAdapter = space_adapter_type.value( 

110 orig_parameter_space=parameter_space, 

111 **space_adapter_kwargs, 

112 ) 

113 

114 return space_adapter