Skip to content
Draft
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
84 commits
Select commit Hold shift + click to select a range
cd8b88f
Start introducing MockScheduler for better Scheduler testing
bpkroth May 19, 2025
2c9c968
fixup scheduler schemas refactor
bpkroth May 19, 2025
2f4a82e
reorg optimizer fixtures for reuse
bpkroth May 19, 2025
24ccf5a
reorg files based storage fixture
bpkroth May 19, 2025
bdfd9b0
adding basic scheduler config example loader test
bpkroth May 19, 2025
b66e17a
Refactor Scheduler schema definitions to make it easier to add new ones.
bpkroth May 19, 2025
30191c0
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 19, 2025
bc8dc8f
Refactor some test fixtures for better reuse so we can test loading S…
bpkroth May 19, 2025
ea7b3ff
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 19, 2025
7563b8a
fixup
bpkroth May 19, 2025
b03e31c
lint
bpkroth May 19, 2025
8eb14c0
apply suggestions
bpkroth May 19, 2025
8ad4c3d
revert
bpkroth May 19, 2025
d4d5153
load test configs too
bpkroth May 19, 2025
1eb1acb
format
bpkroth May 19, 2025
947b759
Merge branch 'refactor/tests' into refactor/mock-scheduler-and-tests
bpkroth May 19, 2025
0c3c805
Merge branch 'refactor/scheduler-schemas' into refactor/mock-schedule…
bpkroth May 19, 2025
1112af4
list in __all__ so we load it as a part of tests schemas checking
bpkroth May 19, 2025
973ad2b
Merge branch 'main' into refactor/mock-scheduler-and-tests
bpkroth May 19, 2025
7a0d087
new mock scheduler schema and test configs
bpkroth May 19, 2025
dded243
refactor to split steps for easier hooking
bpkroth May 20, 2025
c305196
Merge remote-tracking branch 'upstream/main' into refactor/mock-sched…
bpkroth May 20, 2025
b16db23
Merge branch 'main' into refactor/mock-scheduler-and-tests
bpkroth May 20, 2025
f1fe022
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 20, 2025
83ff70f
Update mlos_bench/mlos_bench/tests/config/schedulers/test_load_schedu…
bpkroth May 20, 2025
71420e6
fixup
bpkroth May 20, 2025
70130ba
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 20, 2025
0a440a1
ignore the build tree in vscode
bpkroth May 20, 2025
efafc6f
adding more accessors
bpkroth May 22, 2025
842d393
wip: enable mock env to report arbitrary data
bpkroth May 22, 2025
19cec78
spelling
bpkroth May 22, 2025
175f315
refactor status parsing a little bit again
bpkroth May 22, 2025
7904716
extra test too
bpkroth May 22, 2025
54a96e8
more links
bpkroth May 22, 2025
2e1c4db
start adding a mock trial runner
bpkroth May 22, 2025
626554d
Revert "start adding a mock trial runner"
bpkroth May 22, 2025
b87c02a
schema work on mock trial data
bpkroth May 22, 2025
b4e5640
remove mock scheduler
bpkroth May 22, 2025
80cf2fa
allow mock trial data to be in the global config as well
bpkroth May 22, 2025
8426621
comments as prompts in preparation to run a trial
bpkroth May 22, 2025
cdc614f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 22, 2025
b3f49e9
fixups
bpkroth May 22, 2025
a41e85f
revert
bpkroth May 22, 2025
63e0f88
remove
bpkroth May 22, 2025
44bdde2
refactor status parsing a little bit again
bpkroth May 22, 2025
607fffd
extra test too
bpkroth May 22, 2025
aaf0842
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 22, 2025
8f472bb
revert
bpkroth May 22, 2025
8a4aac2
comments
bpkroth May 22, 2025
50ffde4
add accessor for the mock_trial_data property
bpkroth May 22, 2025
b1e2a52
make it a property
bpkroth May 22, 2025
f0f7c4c
add some basic fixtures to get started with
bpkroth May 22, 2025
c27b3c2
add a method for creating schedulers, intended to be used with pytest…
bpkroth May 22, 2025
9813268
stubbing out a very basic test to get started
bpkroth May 22, 2025
3b92017
wip: testing
bpkroth May 22, 2025
341564e
wip
bpkroth May 22, 2025
5c06814
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 22, 2025
836dcd9
comments
bpkroth May 22, 2025
0072141
fixups
bpkroth May 22, 2025
5400bd6
more checks
bpkroth May 22, 2025
e62a378
Add more checks
bpkroth May 22, 2025
206bb77
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 22, 2025
abe412b
linting
bpkroth May 22, 2025
f5cb468
doc tweaks
bpkroth May 22, 2025
c047bc2
Merge branch 'refactor/parse-status' into refactor/mock-scheduler-and…
bpkroth May 22, 2025
dc7a7b2
fixup
bpkroth May 22, 2025
76e94ed
fixup
bpkroth May 22, 2025
7f8522a
fixups
bpkroth May 22, 2025
9e03e63
remove old files
bpkroth May 22, 2025
f9e3339
add some common globals
bpkroth May 22, 2025
a05b5ae
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 22, 2025
94e31b5
simplify
bpkroth May 27, 2025
21e49db
rename
bpkroth May 27, 2025
46be004
fixup
bpkroth May 27, 2025
01f7a02
always try and convert metrics back to numerics if possible
bpkroth May 27, 2025
b035e57
wip: tell mysql to use a datetime that can store fractional seconds
bpkroth May 27, 2025
ca101ef
add more trial data for testing
bpkroth May 27, 2025
98ec1cc
:type fixups
bpkroth May 27, 2025
82f602c
check the telemetry
bpkroth May 27, 2025
176480f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 27, 2025
22f4e79
revert the bulk conversion one
bpkroth May 27, 2025
aa9f728
debugging
bpkroth May 27, 2025
6289216
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 27, 2025
5be025e
Merge branch 'main' into refactor/mock-scheduler-and-tests
bpkroth Jun 5, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"description": "The name of the scheduler class to use.",
"type": "string",
"$comment": "Exact matches are handled elsewhere.",
"pattern": "^mlos_bench[.]schedulers[.]"
"pattern": "^mlos_bench([.]tests)?[.]schedulers[.]"
},

"config": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://raw.githubusercontent.com/microsoft/MLOS/main/mlos_bench/mlos_bench/config/schemas/schedulers/mock-scheduler-subschema.json",
"title": "mlos_bench Mock Scheduler config",
"description": "config for an mlos_bench Mock Scheduler",
"type": "object",
"properties": {
"class": {
"enum": [
"mlos_bench.tests.schedulers.mock_scheduler.MockScheduler"
]
},
"config": {
"type": "object",
"allOf": [
{
"$ref": "base-scheduler-subschema.json#/$defs/base_scheduler_config"
},
{
"type": "object",
"properties": {
"mock_trial_data": {
"description": "A list of trial data to use for testing.",
"type": "array",
"items": {
"type": "object",
"properties": {
"comments": {
"type": "string",
"description": "Optional comments about the trial status being reported."
},
"trial_id": {
"type": "integer",
"description": "Unique identifier for the trial.",
"examples": [1, 2, 3],
"minimum": 1
},
"status": {
"enum": [
null,
"UNKNOWN",
"PENDING",
"READY",
"RUNNING",
"SUCCEEDED",
"CANCELED",
"FAILED",
"TIMED_OUT"
]
},
"metrics": {
"type": "object",
"description": "A dictionary of metrics for the trial.",
"additionalProperties": {
"type": ["number", "string", "boolean"],
"description": "The value of the metric."
},
"examples": [
{
"score": 0.95,
"color": "green"
},
{
"accuracy": 0.85,
"loss": 0.15
}
]
}
},
"required": ["trial_id", "status"],
"additionalProperties": false
}
}
},
"minProperties": 1
}
],
"minProperties": 1,
"unevaluatedProperties": false
}
},
"required": ["class"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
{
"$comment": "The set of known Scheduler subschemas. Add others as needed.",
"oneOf": [
{
"$ref": "./mock-scheduler-subschema.json"
},
{
"$ref": "./sync-scheduler-subschema.json"
}
Expand Down
42 changes: 30 additions & 12 deletions mlos_bench/mlos_bench/schedulers/base_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,8 @@ def __exit__(
self._in_context = False
return False # Do not suppress exceptions

def start(self) -> None:
"""Start the scheduling loop."""
def _prepare_start(self) -> bool:
"""Prepare the scheduler for starting."""
assert self.experiment is not None
_LOG.info(
"START: Experiment: %s Env: %s Optimizer: %s",
Expand All @@ -262,21 +262,39 @@ def start(self) -> None:
is_warm_up: bool = self.optimizer.supports_preload
if not is_warm_up:
_LOG.warning("Skip pending trials and warm-up: %s", self.optimizer)
return is_warm_up

def start(self) -> None:
"""Start the scheduling loop."""
assert self.experiment is not None
is_warm_up = self._prepare_start()
not_done: bool = True
while not_done:
_LOG.info("Optimization loop: Last trial ID: %d", self._last_trial_id)
self.run_schedule(is_warm_up)
not_done = self.add_new_optimizer_suggestions()
self.assign_trial_runners(
self.experiment.pending_trials(
datetime.now(UTC),
running=False,
trial_runner_assigned=False,
)
)
not_done = self._execute_scheduling_step(is_warm_up)
is_warm_up = False

def _execute_scheduling_step(self, is_warm_up: bool) -> bool:
"""
Perform a single scheduling step.

Notes
-----
This method is called by the :py:meth:`Scheduler.start` method.
It is split out mostly to allow for easier testing with MockSchedulers.
"""
assert self.experiment is not None
_LOG.info("Optimization loop: Last trial ID: %d", self._last_trial_id)
self.run_schedule(is_warm_up)
not_done = self.add_new_optimizer_suggestions()
self.assign_trial_runners(
self.experiment.pending_trials(
datetime.now(UTC),
running=False,
trial_runner_assigned=False,
)
)
return not_done

def teardown(self) -> None:
"""
Tear down the TrialRunners/Environment(s).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from mlos_bench.util import get_class_from_name

mock_opt = mlos_bench.tests.optimizers.fixtures.mock_opt

storage = mlos_bench.tests.storage.sql.fixtures.storage


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"class": "mlos_bench.tests.schedulers.mock_scheduler.MockScheduler",
"config": {
"mock_trial_data": [
{
// MISSING: "trial_id": 1,
"status": "SUCCEEDED"
}
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"class": "mlos_bench.tests.schedulers.mock_scheduler.MockScheduler",
"config": {
"mock_trial_data": [
{
"trial_id": 1,
"status": "INVALID"
},
{
"status": "SUCCEEDED"
}
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"class": "mlos_bench.tests.schedulers.mock_scheduler.MockScheduler",
"config": {
"extra": "unsupported"
},
"extra": "unsupported"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"$schema": "https://raw.githubusercontent.com/microsoft/MLOS/main/mlos_bench/mlos_bench/config/schemas/schedulers/scheduler-schema.json",
"class": "mlos_bench.tests.schedulers.mock_scheduler.MockScheduler",
"config": {
"trial_config_repeat_count": 3,
"teardown": false,
"experiment_id": "MyExperimentName",
"config_id": 1,
"trial_id": 1,
"max_trials": 2,

"mock_trial_data": [
{
"status": "SUCCEEDED",
"trial_id": 1,
"metrics": {
"score": 0.9,
"color": "green"
}
},
{
"status": "FAILED",
"trial_id": 2,
"metrics": {
"score": 0.1,
"color": "red"
}
}
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"class": "mlos_bench.tests.schedulers.mock_scheduler.MockScheduler",
"config": {
"mock_trial_data": [
{
"status": "SUCCEEDED",
"trial_id": 1,
"metrics": {
"score": 0.9,
"color": "green"
}
},
{
"status": "FAILED",
"trial_id": 2
// missing metrics - OK
}
]
}
}
10 changes: 10 additions & 0 deletions mlos_bench/mlos_bench/tests/schedulers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
#
"""mlos_bench.tests.schedulers."""
from mlos_bench.tests.schedulers.mock_scheduler import MockScheduler

__all__ = [
"MockScheduler",
]
5 changes: 5 additions & 0 deletions mlos_bench/mlos_bench/tests/schedulers/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
#
"""Pytest fixtures for mlos_bench.schedulers tests."""
65 changes: 65 additions & 0 deletions mlos_bench/mlos_bench/tests/schedulers/mock_scheduler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
#
"""A mock scheduler that returns predefined status and score for specific trial IDs."""

import logging
from collections.abc import Iterable
from datetime import datetime
from typing import Any

from pytz import UTC

from mlos_bench.schedulers.base_scheduler import Optimizer, Scheduler
from mlos_bench.schedulers.trial_runner import TrialRunner
from mlos_bench.storage.base_storage import Storage

_LOG = logging.getLogger(__name__)


class MockScheduler(Scheduler):
"""A mock scheduler that returns predefined status and score for specific trial
IDs.
"""

def __init__( # pylint: disable=too-many-arguments
self,
*,
config: dict[str, Any],
global_config: dict[str, Any],
trial_runners: Iterable[TrialRunner],
optimizer: Optimizer,
storage: Storage,
root_env_config: str,
) -> None:
super().__init__(
config=config,
global_config=global_config,
trial_runners=trial_runners,
optimizer=optimizer,
storage=storage,
root_env_config=root_env_config,
)
self._mock_trial_data = config.get("mock_trial_data", [])
self._mock_trial_data = {
trial_info["trial_id"]: trial_info for trial_info in self._mock_trial_data
}

def run_trial(self, trial: Storage.Trial) -> None:
"""
Mock the execution of a trial.

Parameters
----------
trial : Storage.Trial
The trial to be executed.
"""
trial_id = trial.trial_id
if trial_id not in self._mock_trial_data:
raise ValueError(f"Trial ID {trial_id} not found in mock trial data.")

trial_info = self._mock_trial_data[trial_id]
_LOG.info("Running trial %d: %s", trial_id, trial_info)
# Don't run it - just update the status and optionally score.
trial.update(trial_info["status"], datetime.now(UTC), trial_info.get("score"))
Loading