mlos_bench.services.config_persistence

Helper functions to load, instantiate, and serialize Python objects that encapsulate benchmark environments, tunable parameters, and service functions.

Classes

ConfigPersistenceService

Collection of methods to deserialize the Environment, Service, and TunableGroups

Module Contents

class mlos_bench.services.config_persistence.ConfigPersistenceService(config: Dict[str, Any] | None = None, global_config: Dict[str, Any] | None = None, parent: mlos_bench.services.base_service.Service | None = None, methods: Dict[str, Callable] | List[Callable] | None = None)[source]

Bases: mlos_bench.services.base_service.Service, mlos_bench.services.types.config_loader_type.SupportsConfigLoading

Collection of methods to deserialize the Environment, Service, and TunableGroups objects.

Create a new instance of config persistence service.

Parameters:
  • config (dict) – Free-format dictionary that contains parameters for the service. (E.g., root path for config files, etc.)

  • global_config (dict) – Free-format dictionary of global parameters.

  • parent (Service) – An optional parent service that can provide mixin functions.

  • methods (Union[Dict[str, Callable], List[Callable], None]) – New methods to register with the service.

build_environment(config: Dict[str, Any], tunables: mlos_bench.tunables.tunable_groups.TunableGroups, global_config: Dict[str, Any] | None = None, parent_args: Dict[str, mlos_bench.tunables.tunable.TunableValue] | None = None, service: mlos_bench.services.base_service.Service | None = None) mlos_bench.environments.base_environment.Environment[source]

Factory method for a new environment with a given config.

Parameters:
  • config (dict) –

    A dictionary with three mandatory fields:

    ”name”: Human-readable string describing the environment; “class”: FQN of a Python class to instantiate; “config”: Free-format dictionary to pass to the constructor.

  • tunables (TunableGroups) – A (possibly empty) collection of groups of tunable parameters for all environments.

  • global_config (dict) – Global parameters to add to the environment config.

  • parent_args (Dict[str, TunableValue]) – An optional reference of the parent CompositeEnv’s const_args used to expand dynamic config parameters from.

  • service (Service) – An optional service object (e.g., providing methods to deploy or reboot a VM, etc.).

Returns:

env – An instance of the Environment class initialized with config.

Return type:

Environment

build_optimizer(*, tunables: mlos_bench.tunables.tunable_groups.TunableGroups, service: mlos_bench.services.base_service.Service, config: Dict[str, Any], global_config: Dict[str, Any] | None = None) mlos_bench.optimizers.base_optimizer.Optimizer[source]

Instantiation of mlos_bench Optimizer that depend on Service and TunableGroups.

A class MUST have a constructor that takes four named arguments: (tunables, config, global_config, service)

Parameters:
  • tunables (TunableGroups) – Tunable parameters of the environment. We need them to validate the configurations of merged-in experiments and restored/pending trials.

  • service (Service) – An optional service object (e.g., providing methods to load config files, etc.)

  • config (dict) – Configuration of the class to instantiate, as loaded from JSON.

  • global_config (dict) – Global configuration parameters (optional).

Returns:

inst – A new instance of the Optimizer class.

Return type:

Optimizer

build_scheduler(*, config: Dict[str, Any], global_config: Dict[str, Any], environment: mlos_bench.environments.base_environment.Environment, optimizer: mlos_bench.optimizers.base_optimizer.Optimizer, storage: mlos_bench.storage.base_storage.Storage, root_env_config: str) mlos_bench.schedulers.base_scheduler.Scheduler[source]

Instantiation of mlos_bench Scheduler.

Parameters:
  • config (dict) – Configuration of the class to instantiate, as loaded from JSON.

  • global_config (dict) – Global configuration parameters.

  • environment (Environment) – The environment to benchmark/optimize.

  • optimizer (Optimizer) – The optimizer to use.

  • storage (Storage) – The storage to use.

  • root_env_config (str) – Path to the root environment configuration.

Returns:

inst – A new instance of the Scheduler.

Return type:

Scheduler

build_service(config: Dict[str, Any], global_config: Dict[str, Any] | None = None, parent: mlos_bench.services.base_service.Service | None = None) mlos_bench.services.base_service.Service[source]

Factory method for a new service with a given config.

Parameters:
  • config (dict) –

    A dictionary with 2 mandatory fields:

    ”class”: FQN of a Python class to instantiate; “config”: Free-format dictionary to pass to the constructor.

  • global_config (dict) – Global parameters to add to the service config.

  • parent (Service) – An optional reference of the parent service to mix in.

Returns:

svc – An instance of the Service class that is a combination of all services from the list plus the parent mix-in.

Return type:

Service

build_storage(*, service: mlos_bench.services.base_service.Service, config: Dict[str, Any], global_config: Dict[str, Any] | None = None) mlos_bench.storage.base_storage.Storage[source]

Instantiation of mlos_bench Storage objects.

Parameters:
  • service (Service) – An optional service object (e.g., providing methods to load config files, etc.)

  • config (dict) – Configuration of the class to instantiate, as loaded from JSON.

  • global_config (dict) – Global configuration parameters (optional).

Returns:

inst – A new instance of the Storage class.

Return type:

Storage

load_config(json_file_name: str, schema_type: mlos_bench.config.schemas.config_schemas.ConfigSchema | None) Dict[str, Any][source]

Load JSON config file. Search for a file relative to _config_path if the input path is not absolute. This method is exported to be used as a service.

Parameters:
  • json_file_name (str) – Path to the input config file.

  • schema_type (Optional[ConfigSchema]) – The schema type to validate the config against.

Returns:

config – Free-format dictionary that contains the configuration.

Return type:

Union[dict, List[dict]]

load_environment(json_file_name: str, tunables: mlos_bench.tunables.tunable_groups.TunableGroups, global_config: Dict[str, Any] | None = None, parent_args: Dict[str, mlos_bench.tunables.tunable.TunableValue] | None = None, service: mlos_bench.services.base_service.Service | None = None) mlos_bench.environments.base_environment.Environment[source]

Load and build new environment from the config file.

Parameters:
  • json_file_name (str) – The environment JSON configuration file.

  • tunables (TunableGroups) – A (possibly empty) collection of tunables to add to the environment.

  • global_config (dict) – Global parameters to add to the environment config.

  • parent_args (Dict[str, TunableValue]) – An optional reference of the parent CompositeEnv’s const_args used to expand dynamic config parameters from.

  • service (Service) – An optional reference of the parent service to mix in.

Returns:

env – A new benchmarking environment.

Return type:

Environment

load_environment_list(json_file_name: str, tunables: mlos_bench.tunables.tunable_groups.TunableGroups, global_config: Dict[str, Any] | None = None, parent_args: Dict[str, mlos_bench.tunables.tunable.TunableValue] | None = None, service: mlos_bench.services.base_service.Service | None = None) List[mlos_bench.environments.base_environment.Environment][source]

Load and build a list of environments from the config file.

Parameters:
  • json_file_name (str) – The environment JSON configuration file. Can contain either one environment or a list of environments.

  • tunables (TunableGroups) – An (possibly empty) collection of tunables to add to the environment.

  • global_config (dict) – Global parameters to add to the environment config.

  • service (Service) – An optional reference of the parent service to mix in.

  • parent_args (Dict[str, TunableValue]) – An optional reference of the parent CompositeEnv’s const_args used to expand dynamic config parameters from.

Returns:

env – A list of new benchmarking environments.

Return type:

List[Environment]

load_services(json_file_names: Iterable[str], global_config: Dict[str, Any] | None = None, parent: mlos_bench.services.base_service.Service | None = None) mlos_bench.services.base_service.Service[source]

Read the configuration files and bundle all service methods from those configs into a single Service object.

Parameters:
  • json_file_names (list of str) – A list of service JSON configuration files.

  • global_config (dict) – Global parameters to add to the service config.

  • parent (Service) – An optional reference of the parent service to mix in.

Returns:

service – A collection of service methods.

Return type:

Service

prepare_class_load(config: Dict[str, Any], global_config: Dict[str, Any] | None = None, parent_args: Dict[str, mlos_bench.tunables.tunable.TunableValue] | None = None) Tuple[str, Dict[str, Any]][source]

Extract the class instantiation parameters from the configuration. Mix-in the global parameters and resolve the local file system paths, where it is required.

Parameters:
  • config (dict) – Configuration of the optimizer.

  • global_config (dict) – Global configuration parameters (optional).

  • parent_args (Dict[str, TunableValue]) – An optional reference of the parent CompositeEnv’s const_args used to expand dynamic config parameters from.

Returns:

(class_name, class_config) – Name of the class to instantiate and its configuration.

Return type:

(str, dict)

resolve_path(file_path: str, extra_paths: Iterable[str] | None = None) str[source]

Prepend the suitable _config_path to path if the latter is not absolute. If _config_path is None or path is absolute, return path as is.

Parameters:
  • file_path (str) – Path to the input config file.

  • extra_paths (Iterable[str]) – Additional directories to prepend to the list of search paths.

Returns:

path – An actual path to the config or script.

Return type:

str

BUILTIN_CONFIG_PATH[source]
property config_paths: List[str][source]

Gets the list of config paths this service will search for config files.

Return type:

List[str]