in src/python/tensorflow_cloud/tuner/vizier_client.py [0:0]
def should_trial_stop(self, trial_id: Text) -> bool:
"""Returns whether trial should stop early.
Args:
trial_id: trial_id.
Returns:
Whether it is recommended to stop the trial early.
"""
trial_name = self._make_trial_name(trial_id)
try:
resp = (
self.service_client.projects()
.locations()
.studies()
.trials()
.checkEarlyStoppingState(name=trial_name)
.execute()
)
except errors.HttpError as e:
tf.get_logger().info("CheckEarlyStoppingState failed.")
raise e
# Polls the stop decision of long-running operations.
operation = self._obtain_long_running_operation(resp)
tf.get_logger().info("CheckEarlyStoppingStateResponse")
if operation["response"].get("shouldStop"):
# Stops a trial.
try:
tf.get_logger().info("Stop the Trial.")
self.service_client.projects().locations().studies().trials(
).stop(name=trial_name).execute()
except errors.HttpError as e:
tf.get_logger().info("StopTrial failed.")
raise e
return True
return False