Skip to content

fix: validate hyperparameter tuning search spaces - #407

Merged
ivantha merged 1 commit into
c2siorg:mainfrom
ashishsoni-ai:fix/tuning-invalid-search-space-validation
Sep 9, 2026
Merged

ivantha merged 1 commit into
c2siorg:mainfrom
ashishsoni-ai:fix/tuning-invalid-search-space-validation

Conversation

@ashishsoni-ai

Copy link
Copy Markdown
Contributor

Summary

Fixes #406. Malformed or empty search_space payloads to POST /api/v1/model/tuning/{model_name} caused silent failures: sessions either got stuck in RUNNING forever (background thread crashed with no handler, no completion event) or completed a zero-trial session with no work done.

Changes

  • app/services/tuning_service.py

    • Added TuningService.validate_search_space(search_space, strategy) which raises AppException(400) for: empty search space, empty list values, malformed dict specs (missing/non-numeric min/max, min >= max, log_uniform with min <= 0), unsupported spec types, and grid search with no discrete (list) parameter.
    • Added _fail_tuning_session() helper that marks a session FAILED, sets completed_at, and emits a tuning_complete event.
    • run_tuning_loop now validates the search space defensively at startup and marks the session FAILED instead of crashing the background thread and leaving it stuck in RUNNING.
  • app/routers/tuning.py

    • start_tuning now calls validate_search_space before creating the session, returning a clear 400 for invalid payloads.
  • tests/test_tuning.py

    • Unit tests for all validation rules.
    • HTTP tests asserting 400 for empty search space, empty list value, malformed dict spec, and continuous-only grid.
    • Regression test that run_tuning_loop marks a malformed session FAILED (not stuck RUNNING).

Testing

  • uv run pytest tests/test_tuning.py — 39 passed
  • uv run pytest tests/test_tuning_integration.py — 3 passed
  • uv run ruff check . and uv run ruff format --check . clean

@ivantha ivantha added bug Something isn't working ready-for-maintainer Reviewed; looks mergeable, awaiting maintainer labels Sep 9, 2026
@ivantha

ivantha commented Sep 9, 2026

Copy link
Copy Markdown
Member

Automated review summary

Triage: bug | Recommendation: ready-for-maintainer
GSoC scope: no overlap with reserved GSoC scope

Findings

  • [minor/quality] tensormap-backend/app/routers/tuning.py:62 validate_search_space() is called after generate_grid_combinations() has already run for the GRID strategy (lines 53-56 compute combos/n_trials, validation is only added at line 62). So for an invalid grid search_space, the pre-existing MAX_GRID_COMBINATIONS cartesian-product check fires first and can return a generic/less-specific 400 (or just waste computation on already-invalid input) instead of the clearer, per-field error validate_search_space is meant to provide. This undercuts the PR's own stated goal of returning 'a clear 400 instead of silently failing' — validation should run before the grid-size computation, not after.

Conventions

  • PR title: follows Conventional Commits
  • Description: clear and complete
  • Commits (1): clean, conventional history

This PR adds a validate_search_space() check that rejects empty/malformed hyperparameter search spaces with a clear 400, plus a _fail_tuning_session() helper so a bad search space in the background tuning loop now marks the session FAILED instead of crashing the thread and leaving it stuck RUNNING forever — a real and well-tested fix for a genuine bug, with solid unit, HTTP, and regression test coverage. One ordering issue in the router: validate_search_space() is called after generate_grid_combinations() already ran for the GRID strategy, so the new clearer error can be preempted by the pre-existing grid-size check on invalid input — move the validation call before the n_trials/grid-combination computation. PR title, description, and single commit are all clean and Conventional-Commits compliant with no hygiene issues.

Generated by /review-prs. A maintainer will follow up.

Malformed or empty search spaces caused silent user-facing failures:

- Random search with a dict spec missing min/max, or with an empty list,
  raised KeyError/ValueError inside the background tuning thread. The
  exception propagated out of asyncio.to_thread with no handler, leaving
  the session stuck in RUNNING with no completion event.
- Grid search with an empty list value produced a zero-trial session that
  was immediately marked COMPLETED without doing any work.
- Grid search with only continuous (dict) params produced a single useless
  {} trial.

Fix:
- Add TuningService.validate_search_space() and call it in start_tuning so
  invalid search spaces return a clear 400 before a session is created.
- Validate defensively at the top of run_tuning_loop and mark the session
  FAILED + emit a tuning_complete event if an invalid row is reached, so a
  session can never be left stuck in RUNNING.

Add unit + HTTP regression tests covering validation and the FAILED path.
@ivantha
ivantha force-pushed the fix/tuning-invalid-search-space-validation branch from bd26f95 to de04db3 Compare September 9, 2026 03:36
@ivantha
ivantha merged commit cd9bb3c into c2siorg:main Sep 9, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working ready-for-maintainer Reviewed; looks mergeable, awaiting maintainer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: malformed or empty tuning search spaces cause silent failures (stuck RUNNING session or zero-trial session)

3 participants