Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 6 additions & 0 deletions docs/config/server.full.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ local_inference_mode:
enabled: false
probability: 0.2

# Smoothing factor for the EMA used to update per-layer device/edge inference
# times. Weight given to the newest measurement; (1 - ema_alpha) is the
# weight kept from history. Optional, defaults to 0.5.
offloading_algo:
ema_alpha: 0.5

delay_simulation:
computation:
enabled: false
Expand Down
24 changes: 24 additions & 0 deletions src/sciot/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,36 @@ def validate_server_config(config: Mapping[str, Any]) -> dict[str, Any]:
_optional_bool(normalized, "verbose", errors)
_optional_bool(normalized, "debug_cprofiler", errors)
_validate_evaluation_config(normalized.get("evaluation", {}), "evaluation", errors)
_validate_offloading_algo_config(
normalized.get("offloading_algo", {}), "offloading_algo", errors
)

if errors:
raise ConfigValidationError(errors)
return normalized


def _validate_offloading_algo_config(
value: Any, path: str, errors: list[str]
) -> None:
"""Validate the optional ``offloading_algo`` section of the server config."""
if not value:
return
if not isinstance(value, dict):
errors.append(f"{path}: must be a mapping")
return

unexpected = set(value) - {"ema_alpha"}
if unexpected:
errors.append(
f"{path}: unsupported keys {sorted(unexpected)}; expected 'ema_alpha'"
)

ema_alpha = _optional_number(value, "ema_alpha", errors, path=f"{path}.ema_alpha")
if ema_alpha is not None and not (0.0 < ema_alpha <= 1.0):
errors.append(f"{path}.ema_alpha: must be between 0.0 (exclusive) and 1.0")


def _validate_evaluation_config(
value: Any, path: str, errors: list[str]
) -> None:
Expand Down
7 changes: 6 additions & 1 deletion src/server/communication/request_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ def load_evaluation_firestore_config():
return _get_settings().get("evaluation", {}).get("firestore", {}) or {}


def load_offloading_ema_alpha_config() -> float:
"""Load the EMA smoothing factor for device/edge layer inference times."""
return float(_get_settings().get("offloading_algo", {}).get("ema_alpha", 0.5))


# ── Background I/O writer ───────────────────────────────────────────────────
# A single daemon thread drains a queue of callables, so that debug-JSON,
# simulation-CSV, and evaluation-CSV writes never block the inference path.
Expand Down Expand Up @@ -541,7 +546,7 @@ def handle_device_inference_result(self, body, received_timestamp):
device_inference_times = RequestHandler.device_profiles[device_id]["device_inference_times"]
edge_inference_times = RequestHandler.device_profiles[device_id]["edge_inference_times"]

alpha = 0.5
alpha = load_offloading_ema_alpha_config()
for l_id, inference_time in enumerate(message_data.device_layers_inference_time):
layer_key = f"layer_{l_id}"
if layer_key in device_inference_times:
Expand Down
6 changes: 6 additions & 0 deletions src/server/settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ communication:
ntp_server: 0.it.pool.ntp.org
port: 8080

offloading_algo:
# Smoothing factor for the EMA used to update per-layer device/edge inference
# times (RequestHandler.handle_device_inference_result). Weight given to the
# newest measurement; (1 - ema_alpha) is the weight kept from history.
ema_alpha: 0.5

delay_simulation:
computation:
enabled: false
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/test_config_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,24 @@ def test_documented_example_configs_are_valid(path, loader):
),
"evaluation.firestore: unsupported keys",
),
(
lambda cfg: cfg.__setitem__(
"offloading_algo", {"ema_alpha": 0}
),
r"offloading_algo.ema_alpha: must be between 0.0 \(exclusive\) and 1.0",
),
(
lambda cfg: cfg.__setitem__(
"offloading_algo", {"ema_alpha": 1.5}
),
r"offloading_algo.ema_alpha: must be between 0.0 \(exclusive\) and 1.0",
),
(
lambda cfg: cfg.__setitem__(
"offloading_algo", {"unknown": True}
),
"offloading_algo: unsupported keys",
),
],
)
def test_invalid_server_config_reports_field_errors(mutate, expected_error):
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/test_request_handler_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
_to_float_list,
load_evaluation_firestore_config,
load_evaluation_outputs_config,
load_offloading_ema_alpha_config,
)


Expand Down Expand Up @@ -87,6 +88,18 @@ def test_evaluation_firestore_config_is_loaded(monkeypatch):
}


def test_offloading_ema_alpha_defaults_to_half(monkeypatch):
monkeypatch.setattr(rh, "_get_settings", lambda: {})
assert load_offloading_ema_alpha_config() == 0.5


def test_offloading_ema_alpha_is_loaded(monkeypatch):
monkeypatch.setattr(
rh, "_get_settings", lambda: {"offloading_algo": {"ema_alpha": 0.2}}
)
assert load_offloading_ema_alpha_config() == 0.2


def test_append_and_publish_inference_cycle_keeps_local_write(monkeypatch):
calls = []

Expand Down
Loading