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

21 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2025-01-21 01:50 +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 

23Examples 

24-------- 

25TODO: Add example usage here. 

26 

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""" 

33 

34from enum import Enum 

35 

36import ConfigSpace 

37 

38from mlos_core.spaces.adapters.identity_adapter import IdentityAdapter 

39from mlos_core.spaces.adapters.llamatune import LlamaTuneAdapter 

40 

41__all__ = [ 

42 "ConcreteSpaceAdapter", 

43 "IdentityAdapter", 

44 "LlamaTuneAdapter", 

45 "SpaceAdapterFactory", 

46 "SpaceAdapterType", 

47] 

48 

49 

50class SpaceAdapterType(Enum): 

51 """Enumerate supported mlos_core space adapters.""" 

52 

53 IDENTITY = IdentityAdapter 

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

55 

56 LLAMATUNE = LlamaTuneAdapter 

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

58 

59 

60ConcreteSpaceAdapter = IdentityAdapter | LlamaTuneAdapter 

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

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

63""" 

64 

65 

66class SpaceAdapterFactory: 

67 """Simple factory class for creating 

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

69 """ 

70 

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

72 

73 @staticmethod 

74 def create( 

75 *, 

76 parameter_space: ConfigSpace.ConfigurationSpace, 

77 space_adapter_type: SpaceAdapterType | None = SpaceAdapterType.IDENTITY, 

78 space_adapter_kwargs: dict | None = None, 

79 ) -> ConcreteSpaceAdapter: 

80 """ 

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

82 space adapter options. 

83 

84 Parameters 

85 ---------- 

86 parameter_space : ConfigSpace.ConfigurationSpace 

87 Input configuration space. 

88 space_adapter_type : SpaceAdapterType | None 

89 Space adapter class to be used alongside the optimizer. 

90 space_adapter_kwargs : dict | None 

91 Optional arguments passed in SpaceAdapter class constructor. 

92 

93 Returns 

94 ------- 

95 space_adapter : ConcreteSpaceAdapter 

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

97 """ 

98 if space_adapter_type is None: 

99 space_adapter_type = SpaceAdapterType.IDENTITY 

100 if space_adapter_kwargs is None: 

101 space_adapter_kwargs = {} 

102 

103 space_adapter: ConcreteSpaceAdapter = space_adapter_type.value( 

104 orig_parameter_space=parameter_space, 

105 **space_adapter_kwargs, 

106 ) 

107 

108 return space_adapter