ray.tune.search.ConcurrencyLimiter#

class ray.tune.search.ConcurrencyLimiter(searcher: ray.tune.search.searcher.Searcher, max_concurrent: int, batch: bool = False)[source]#

Bases: ray.tune.search.searcher.Searcher

A wrapper algorithm for limiting the number of concurrent trials.

Certain Searchers have their own internal logic for limiting the number of concurrent trials. If such a Searcher is passed to a ConcurrencyLimiter, the max_concurrent of the ConcurrencyLimiter will override the max_concurrent value of the Searcher. The ConcurrencyLimiter will then let the Searcher’s internal logic take over.

Parameters
  • searcher – Searcher object that the ConcurrencyLimiter will manage.

  • max_concurrent – Maximum concurrent samples from the underlying searcher.

  • batch – Whether to wait for all concurrent samples to finish before updating the underlying searcher.

Example:

from ray.tune.search import ConcurrencyLimiter
search_alg = HyperOptSearch(metric="accuracy")
search_alg = ConcurrencyLimiter(search_alg, max_concurrent=2)
tuner = tune.Tuner(
    trainable,
    tune_config=tune.TuneConfig(
        search_alg=search_alg
    ),
)
tuner.fit()

PublicAPI: This API is stable across Ray releases.

set_max_concurrency(max_concurrent: int) bool[source]#

Set max concurrent trials this searcher can run.

This method will be called on the wrapped searcher by the ConcurrencyLimiter. It is intended to allow for searchers which have custom, internal logic handling max concurrent trials to inherit the value passed to ConcurrencyLimiter.

If this method returns False, it signifies that no special logic for handling this case is present in the searcher.

Parameters

max_concurrent – Number of maximum concurrent trials.

set_search_properties(metric: Optional[str], mode: Optional[str], config: Dict, **spec) bool[source]#

Pass search properties to searcher.

This method acts as an alternative to instantiating search algorithms with their own specific search spaces. Instead they can accept a Tune config through this method. A searcher should return True if setting the config was successful, or False if it was unsuccessful, e.g. when the search space has already been set.

Parameters
  • metric – Metric to optimize

  • mode – One of [“min”, “max”]. Direction to optimize.

  • config – Tune config dict.

  • **spec – Any kwargs for forward compatiblity. Info like Experiment.PUBLIC_KEYS is provided through here.

suggest(trial_id: str) Optional[Dict][source]#

Queries the algorithm to retrieve the next set of parameters.

Parameters

trial_id – Trial ID used for subsequent notifications.

Returns

Configuration for a trial, if possible.

If FINISHED is returned, Tune will be notified that no more suggestions/configurations will be provided. If None is returned, Tune will skip the querying of the searcher for this step.

Return type

dict | FINISHED | None

on_trial_complete(trial_id: str, result: Optional[Dict] = None, error: bool = False)[source]#

Notification for the completion of trial.

Typically, this method is used for notifying the underlying optimizer of the result.

Parameters
  • trial_id – A unique string ID for the trial.

  • result – Dictionary of metrics for current training progress. Note that the result dict may include NaNs or may not include the optimization metric. It is up to the subclass implementation to preprocess the result to avoid breaking the optimization process. Upon errors, this may also be None.

  • error – True if the training process raised an error.

on_trial_result(trial_id: str, result: Dict) None[source]#

Optional notification for result during training.

Note that by default, the result dict may include NaNs or may not include the optimization metric. It is up to the subclass implementation to preprocess the result to avoid breaking the optimization process.

Parameters
  • trial_id – A unique string ID for the trial.

  • result – Dictionary of metrics for current training progress. Note that the result dict may include NaNs or may not include the optimization metric. It is up to the subclass implementation to preprocess the result to avoid breaking the optimization process.

add_evaluated_point(parameters: Dict, value: float, error: bool = False, pruned: bool = False, intermediate_values: Optional[List[float]] = None)[source]#

Pass results from a point that has been evaluated separately.

This method allows for information from outside the suggest - on_trial_complete loop to be passed to the search algorithm. This functionality depends on the underlying search algorithm and may not be always available.

Parameters
  • parameters – Parameters used for the trial.

  • value – Metric value obtained in the trial.

  • error – True if the training process raised an error.

  • pruned – True if trial was pruned.

  • intermediate_values – List of metric values for intermediate iterations of the result. None if not applicable.

save(checkpoint_path: str)[source]#

Save state to path for this search algorithm.

Parameters

checkpoint_path – File where the search algorithm state is saved. This path should be used later when restoring from file.

Example:

search_alg = Searcher(...)

tuner = tune.Tuner(
    cost,
    tune_config=tune.TuneConfig(
        search_alg=search_alg,
        num_samples=5
    ),
    param_space=config
)
results = tuner.fit()

search_alg.save("./my_favorite_path.pkl")

Changed in version 0.8.7: Save is automatically called by Tuner().fit(). You can use Tuner().restore() to restore from an experiment directory such as /ray_results/trainable.

restore(checkpoint_path: str)[source]#

Restore state for this search algorithm

Parameters

checkpoint_path – File where the search algorithm state is saved. This path should be the same as the one provided to “save”.

Example:

search_alg.save("./my_favorite_path.pkl")

search_alg2 = Searcher(...)
search_alg2 = ConcurrencyLimiter(search_alg2, 1)
search_alg2.restore(checkpoint_path)
tuner = tune.Tuner(
    cost,
    tune_config=tune.TuneConfig(
        search_alg=search_alg2,
        num_samples=5
    ),
)
tuner.fit()