Implement an asynchronous successive halving (ASHA) rung coordinator for hyperparameter tuning
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: hard
Interview Round: Other
Implement an `ASHAScheduler` class and a `run_asha` function for asynchronous successive halving. Rung `k` uses resource `min_resource * eta**k`, for every such value at or below `max_resource` (stored in the attribute `rung_resources`). Scores are maximized, and promotion never waits for pending jobs.
The interface is `ASHAScheduler(min_resource, max_resource, eta=3)` and `run_asha(objective, min_resource, max_resource, eta, n_jobs)`.
Requirements:
- `next_job()` scans rungs from the second-highest down to 0. In rung `k` it promotes, to rung `k + 1`, the first not-yet-promoted trial among the top `len(results) // eta`, ranked by score descending, then `trial_id` ascending. Otherwise it issues a new trial (ids 0, 1, 2, ...) at rung 0. It returns `(trial_id, rung, resource)`.
- `report(trial_id, rung, score)` records a result and raises `ValueError` for a job that was never issued or was already reported.
- `rung_results(rung)` returns a `{trial_id: score}` copy. `best_trial()` returns the top-ranked `(trial_id, rung, score)` at the highest non-empty rung, or `None`.
- The constructor raises `ValueError` if `eta < 2`, `min_resource <= 0` or `max_resource < min_resource`.
- `run_asha` runs `n_jobs` iterations: call `next_job`, then immediately report `objective(trial_id, resource)`. It returns a list of `(trial_id, rung, resource, score)`.
Example:
```python
s = ASHAScheduler(1, 4, 2) # rung_resources == [1, 2, 4]
s.next_job() # (0, 0, 1)
s.next_job() # (1, 0, 1)
s.report(1, 0, 0.8)
s.next_job() # (2, 0, 1) rung 0 has 1 result, 1 // 2 == 0
s.report(0, 0, 0.3)
s.next_job() # (1, 1, 2) trial 2 still pending
s.report(2, 0, 0.9)
s.next_job() # (2, 1, 2) 3 // 2 == 1 -> top is trial 2
```
```hint State to keep
Per rung you need the reported scores and the trials already promoted out of it. Across rungs you need the jobs that were issued but not yet reported, keyed so that `report` can tell a valid call from a bad one.
```
```hint The top set moves
The top `len(results) // eta` of a rung changes after every report, and a trial that has dropped out of it may already have been promoted. Make sure no trial can leave the same rung twice.
```
### Clarifying Questions to Ask
- Can `min_resource`, `max_resource` and `eta` be floats, and must the rung resources match the formula exactly?
- How should a `NaN` score rank, or should `report` reject it?
- Will `next_job` and `report` be called from several threads at once?
### What a Strong Answer Covers
- Building the rung resources from the formula and validating the constructor arguments.
- The state for per-rung results, promoted trials and outstanding jobs, and how `report` uses it to reject bad calls.
- The promotion rule: scan order, the `len(results) // eta` cutoff, the tie-break, and why pending jobs never block it.
- `best_trial`, the copy returned by `rung_results`, and the history built by `run_asha`.
- The cost of a `next_job` call as rungs grow, and how to reduce it.
### Follow-up Questions
- How would you make the scheduler safe with many concurrent workers?
- Early rungs promote noisy, under-sampled configs. How does that affect ASHA compared with synchronous successive halving?
- How would you cut the cost of `next_job` when each rung holds thousands of results?
Overview: Build an asynchronous successive halving scheduler that hands out new trials and promotes top configurations between rungs without waiting for pending jobs. It tests state tracking, deterministic ranking with tie-breaks, input validation and reasoning about hyperparameter search with parallel workers.