Keras 3 API documentation / KerasTuner / Oracles / @synchronized decorator

@synchronized decorator


synchronized function

keras_tuner.synchronized(func, )

Decorator to synchronize the multi-threaded calls to Oracle functions.

In parallel tuning, there may be concurrent gRPC calls from multiple threads to the Oracle methods like create_trial(), update_trial(), and end_trial(). To avoid concurrent writing to the data, use @synchronized to ensure the calls are synchronized, which only allows one call to run at a time.

Concurrent calls to different Oracle objects would not block one another. Concurrent calls to the same or different functions of the same Oracle object would block one another.

You can decorate a subclass function, which overrides an already decorated function in the base class, without worrying about creating a deadlock. However, the decorator only support methods within classes, and cannot be applied to standalone functions.

You do not need to decorate Oracle.populate_space(), which is only called by Oracle.create_trial(), which is decorated.


class MyOracle(keras_tuner.Oracle):
    def create_trial(self, tuner_id):

    def update_trial(self, trial_id, metrics, step=0):
        super().update_trial(trial_id, metrics, step)

    def end_trial(self, trial):