Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure One Shot Optimizer always returns the same suggestion #759

Merged
merged 20 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
405b1b8
enforce OneShot optimizer to always return the same initial config
motus Jun 26, 2024
c1906e5
minor fix in the docstring
motus Jun 26, 2024
c8f67be
better naming for mock env parameters + fixes to unit tests
motus Jun 27, 2024
3e62896
use noisy mock env for optimziations
motus Jun 27, 2024
5f0f86d
Merge branch 'main' into sergiym/opt/one_shot
motus Jun 28, 2024
58aa2b1
better values for mock env seed
motus Jun 28, 2024
f45f96a
Merge branch 'main' of https://github.com/microsoft/MLOS into sergiym…
motus Jun 28, 2024
4863b65
Merge branch 'sergiym/opt/one_shot' of https://github.com/motus/MLOS …
motus Jun 28, 2024
46ee026
better unit tests for tunables intialization
motus Jun 28, 2024
32f7998
Merge branch 'main' into sergiym/opt/one_shot
motus Jun 28, 2024
f5ab2b0
roll back all changes not related to OneShotOptimizer
motus Jun 28, 2024
ab0e0e9
Merge branch 'main' of https://github.com/microsoft/MLOS into sergiym…
motus Jun 28, 2024
6c60566
Merge branch 'sergiym/opt/one_shot' of https://github.com/motus/MLOS …
motus Jun 28, 2024
4055971
Merge branch 'main' into sergiym/opt/one_shot
motus Jun 28, 2024
53a3165
Merge branch 'main' into sergiym/opt/one_shot
motus Jul 2, 2024
5f4d2b4
Merge branch 'main' of https://github.com/microsoft/MLOS into sergiym…
motus Jul 2, 2024
daee8c3
Merge branch 'sergiym/opt/one_shot' of https://github.com/motus/MLOS …
motus Jul 2, 2024
830aa46
Merge branch 'main' into sergiym/opt/one_shot
bpkroth Jul 2, 2024
6fcfe33
Merge branch 'main' into sergiym/opt/one_shot
bpkroth Jul 2, 2024
ee9eca5
Merge branch 'main' into sergiym/opt/one_shot
bpkroth Jul 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mlos_bench/mlos_bench/DEVNOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Each `Environment` config is a JSON5 file with the following structure:
}
// Environment constructor parameters
// (specific to the Environment class being instantiated):
"seed": 42,
"mock_env_seed": 42,
// ...
}
}
Expand Down
6 changes: 3 additions & 3 deletions mlos_bench/mlos_bench/config/environments/mock/mock_env.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
"linux-hugepages-2048kB",
"redis"
],
"seed": 42, // Seed for the random noise generator. Omit to produce noise-free data.
"range": [60, 120], // Range of the generated output values of the benchmark.
"metrics": ["score"] // Names of fake benchmark metrics to generate. (Default is one metric, "score").
"mock_env_seed": 42, // Seed for the random noise generator. Omit or set to 0 to produce noise-free data.
"mock_env_range": [60, 120], // Range of the generated output values of the benchmark.
"mock_env_metrics": ["score"] // Names of fake benchmark metrics to generate. (Default is one metric, "score").
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
},
{
"properties": {
"seed": {
"mock_env_seed": {
motus marked this conversation as resolved.
Show resolved Hide resolved
"type": "integer",
"description": "Seed for the random number generator",
"default": 0
},
"range": {
"mock_env_range": {
"type": "array",
"description": "Range of the random number generator",
"items": {
Expand All @@ -33,7 +33,7 @@
"minItems": 2,
"maxItems": 2
},
"metrics": {
"mock_env_metrics": {
"type": "array",
"description": "Names of fake benchmark metrics to be generate",
"items": {
Expand Down
11 changes: 5 additions & 6 deletions mlos_bench/mlos_bench/environments/mock_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
from mlos_bench.environments.status import Status
from mlos_bench.environments.base_environment import Environment
from mlos_bench.tunables import Tunable, TunableGroups, TunableValue
from mlos_bench.util import nullable

_LOG = logging.getLogger(__name__)

Expand Down Expand Up @@ -49,18 +48,18 @@ def __init__(self,
global_config : dict
Free-format dictionary of global parameters (e.g., security credentials)
to be mixed in into the "const_args" section of the local config.
Optional arguments are `seed`, `range`, and `metrics`.
Optional arguments are `mock_env_seed`, `mock_env_range`, and `mock_env_metrics`.
tunables : TunableGroups
A collection of tunable parameters for *all* environments.
service: Service
An optional service object. Not used by this class.
"""
super().__init__(name=name, config=config, global_config=global_config,
tunables=tunables, service=service)
seed = self.config.get("seed")
self._random = nullable(random.Random, seed)
self._range = self.config.get("range")
self._metrics = self.config.get("metrics", ["score"])
seed = int(self.config.get("mock_env_seed", 0))
self._random = random.Random(seed) if seed > 0 else None
self._range = self.config.get("mock_env_range")
self._metrics = self.config.get("mock_env_metrics", ["score"])
self._is_ready = True

def run(self) -> Tuple[Status, datetime, Optional[Dict[str, TunableValue]]]:
Expand Down
2 changes: 1 addition & 1 deletion mlos_bench/mlos_bench/optimizers/mock_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def suggest(self) -> TunableGroups:
"""
tunables = super().suggest()
if self._start_with_defaults:
_LOG.info("Use default values for the first trial")
_LOG.info("Use default tunable values")
self._start_with_defaults = False
else:
for (tunable, _group) in tunables:
Expand Down
10 changes: 9 additions & 1 deletion mlos_bench/mlos_bench/optimizers/one_shot_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

class OneShotOptimizer(MockOptimizer):
"""
Mock optimizer that proposes a single configuration and returns.
No-op optimizer that proposes a single configuration and returns.
Explicit configs (partial or full) are possible using configuration files.
"""

Expand All @@ -33,6 +33,14 @@ def __init__(self,
_LOG.info("Run a single iteration for: %s", self._tunables)
self._max_iter = 1 # Always run for just one iteration.

def suggest(self) -> TunableGroups:
"""
Always produce the same (initial) suggestion.
"""
tunables = super().suggest()
self._start_with_defaults = True
bpkroth marked this conversation as resolved.
Show resolved Hide resolved
return tunables

@property
def supports_preload(self) -> bool:
return False
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "mock_env-full",
"class": "mlos_bench.environments.mock_env.MockEnv",
"config": {
"metrics": [
"mock_env_metrics": [
{"bad": "metric type"}
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "partial mock env",
"class": "mlos_bench.environments.MockEnv",
"config": {
"seed": 42,
"mock_env_seed": 42,
"const_args": {
"foo": "bar",
"int": 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "partial mock env",
"class": "mlos_bench.environments.MockEnv",
"config": {
"seed": 42,
"mock_env_seed": 42,
"const_args": {
"foo": "bar",
"int": 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "mock_env-full",
"class": "mlos_bench.environments.mock_env.MockEnv",
"config": {
"metrics": [
"mock_env_metrics": [
// needs at least one element
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
// "name": "missing name is invalid",
"class": "mlos_bench.environments.MockEnv",
"config": {
"seed": 42
"mock_env_seed": 42
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
"required_args": [
"foo"
],
"range": [0, 1],
"seed": 42,
"metrics": [
"mock_env_range": [0, 1],
"mock_env_seed": 42,
"mock_env_metrics": [
"latency",
"cost"
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "partial mock env",
"class": "mlos_bench.environments.MockEnv",
"config": {
"seed": 42,
"mock_env_seed": 42,
"const_args": {
"foo": "bar",
"int": 1,
Expand Down
11 changes: 6 additions & 5 deletions mlos_bench/mlos_bench/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ def mock_env(tunable_groups: TunableGroups) -> MockEnv:
name="Test Env",
config={
"tunable_params": ["provision", "boot", "kernel"],
"seed": SEED,
"range": [60, 120],
"metrics": ["score"],
"mock_env_seed": SEED,
"mock_env_range": [60, 120],
"mock_env_metrics": ["score"],
},
tunables=tunable_groups
)
Expand All @@ -57,8 +57,9 @@ def mock_env_no_noise(tunable_groups: TunableGroups) -> MockEnv:
name="Test Env No Noise",
config={
"tunable_params": ["provision", "boot", "kernel"],
"range": [60, 120],
"metrics": ["score", "other_score"],
"mock_env_seed": 0,
"mock_env_range": [60, 120],
"mock_env_metrics": ["score", "other_score"],
},
tunables=tunable_groups
)
Expand Down
20 changes: 10 additions & 10 deletions mlos_bench/mlos_bench/tests/environments/composite_env_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ def composite_env(tunable_groups: TunableGroups) -> CompositeEnv:
"EnvId": 1,
},
"required_args": ["vmName", "someConst", "global_param"],
"range": [60, 120],
"metrics": ["score"],
"mock_env_range": [60, 120],
"mock_env_metrics": ["score"],
}
},
{
Expand All @@ -56,8 +56,8 @@ def composite_env(tunable_groups: TunableGroups) -> CompositeEnv:
"global_param": "local"
},
"required_args": ["vmName"],
"range": [60, 120],
"metrics": ["score"],
"mock_env_range": [60, 120],
"mock_env_metrics": ["score"],
}
},
{
Expand All @@ -70,8 +70,8 @@ def composite_env(tunable_groups: TunableGroups) -> CompositeEnv:
"EnvId": 3,
},
"required_args": ["vmName", "vm_server_name", "vm_client_name"],
"range": [60, 120],
"metrics": ["score"],
"mock_env_range": [60, 120],
"mock_env_metrics": ["score"],
}
}
]
Expand Down Expand Up @@ -193,8 +193,8 @@ def nested_composite_env(tunable_groups: TunableGroups) -> CompositeEnv:
"vm_server_name",
"global_param"
],
"range": [60, 120],
"metrics": ["score"],
"mock_env_range": [60, 120],
"mock_env_metrics": ["score"],
}
},
# ...
Expand All @@ -218,8 +218,8 @@ def nested_composite_env(tunable_groups: TunableGroups) -> CompositeEnv:
"config": {
"tunable_params": ["boot"],
"required_args": ["vmName", "EnvId", "vm_client_name"],
"range": [60, 120],
"metrics": ["score"],
"mock_env_range": [60, 120],
"mock_env_metrics": ["score"],
}
},
# ...
Expand Down
5 changes: 4 additions & 1 deletion mlos_bench/mlos_bench/tests/launcher_in_process_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@
("argv", "expected_score"), [
([
"--config", "mlos_bench/mlos_bench/tests/config/cli/mock-bench.jsonc",
], 65.6742),
"--trial_config_repeat_count", "5",
"--mock_env_seed", "0", # Deterministic Mock Environment.
motus marked this conversation as resolved.
Show resolved Hide resolved
], 67.40329),
([
"--config", "mlos_bench/mlos_bench/tests/config/cli/mock-opt.jsonc",
"--trial_config_repeat_count", "3",
"--max_suggestions", "3",
"--mock_env_seed", "42", # Noisy Mock Environment.
], 64.53897),
]
)
Expand Down
11 changes: 8 additions & 3 deletions mlos_bench/mlos_bench/tests/launcher_run_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,12 @@ def test_launch_main_app_bench(root_path: str, local_exec_service: LocalExecServ
"""
_launch_main_app(
root_path, local_exec_service,
"--config mlos_bench/mlos_bench/tests/config/cli/mock-bench.jsonc",
"--config mlos_bench/mlos_bench/tests/config/cli/mock-bench.jsonc" +
" --trial_config_repeat_count 5" +
" --mock_env_seed 0", # Deterministic Mock Environment.
[
f"^{_RE_DATE} run\\.py:\\d+ " +
r"_main INFO Final score: \{'score': 65\.67\d+\}\s*$",
r"_main INFO Final score: \{'score': 67\.40\d+\}\s*$",
]
)

Expand All @@ -93,7 +95,10 @@ def test_launch_main_app_opt(root_path: str, local_exec_service: LocalExecServic
"""
_launch_main_app(
root_path, local_exec_service,
"--config mlos_bench/mlos_bench/tests/config/cli/mock-opt.jsonc --trial_config_repeat_count 3 --max_suggestions 3",
"--config mlos_bench/mlos_bench/tests/config/cli/mock-opt.jsonc" +
" --trial_config_repeat_count 3" +
" --max_suggestions 3" +
" --mock_env_seed 42", # Noisy Mock Environment.
[
# Iteration 1: Expect first value to be the baseline
f"^{_RE_DATE} mlos_core_optimizer\\.py:\\d+ " +
Expand Down
Loading