Skip to content

Commit

Permalink
Remove redundant logging from Eagle designer for expediting benchmark…
Browse files Browse the repository at this point in the history
…ing jobs / convergence tests.

PiperOrigin-RevId: 573903127
  • Loading branch information
belenkil authored and copybara-github committed Oct 16, 2023
1 parent 116788b commit b24e46c
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 56 deletions.
51 changes: 5 additions & 46 deletions vizier/_src/algorithms/designers/eagle_strategy/eagle_strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@
"""

import json
import logging
import time
from typing import Optional, Sequence

from absl import logging
import attr
import numpy as np
from vizier import algorithms as vza
Expand Down Expand Up @@ -177,7 +177,6 @@ def dump(self) -> vz.Metadata:
metadata.ns('eagle').ns('random_designer').attach(
self._initial_designer.dump()
)
logging.info('Dump metadata:\n%s', metadata.ns('eagle'))
return metadata

def load(self, metadata: vz.Metadata) -> None:
Expand All @@ -199,7 +198,6 @@ def load(self, metadata: vz.Metadata) -> None:
logging.info('Eagle designer was called for the first time. No state was'
' recovered.')
else:
logging.info('Load metadata:\n%s', metadata.ns('eagle'))
try:
self._rng = serialization.restore_rng(metadata.ns('eagle')['rng'])
except Exception as e:
Expand All @@ -211,7 +209,6 @@ def load(self, metadata: vz.Metadata) -> None:
firefly_pool = metadata.ns('eagle')['firefly_pool']
self._firefly_pool = serialization.restore_firefly_pool(
self._utils, firefly_pool)
logging.info('Restored firefly pool:\n%s', self._firefly_pool)
except Exception as e:
raise serializable.HarmlessDecodeError(
"Couldn't load firefly pool from metadata.") from e
Expand Down Expand Up @@ -256,21 +253,15 @@ def _suggest_one(self) -> vz.TrialSuggestion:
# Create a new parent fly id and assign it to the trial, this will be
# used during Update to match the trial to its parent fly in the pool.
parent_fly_id = self._firefly_pool.generate_new_fly_id()
logging.info('Pool is underpopulated. Generated random trial parameters.')
else:
moving_fly = self._firefly_pool.get_next_moving_fly_copy()
self._mutate_fly(moving_fly)
self._perturb_fly(moving_fly)
suggested_parameters = moving_fly.trial.parameters
parent_fly_id = moving_fly.id_
logging.info('Created a trial from parent fly ID: %s.', parent_fly_id)

suggested_trial.parameters = suggested_parameters
suggested_trial.metadata.ns('eagle')['parent_fly_id'] = str(parent_fly_id)
logging.info(
'Suggested trial: %s',
self._utils.display_trial(suggested_trial.to_trial(-1)),
)
return suggested_trial

def _mutate_fly(self, moving_fly: Firefly) -> None:
Expand Down Expand Up @@ -360,25 +351,20 @@ def update(
if not trial.metadata.ns('eagle').get('parent_fly_id'):
# Trial was not generated from Eagle Strategy. Set a new parent fly id.
trial.metadata.ns('eagle')['parent_fly_id'] = str(
self._firefly_pool.generate_new_fly_id())
logging.info('Received a trial without parent_fly_id.')
self._firefly_pool.generate_new_fly_id()
)
self._update_one(trial)

def _update_one(self, trial: vz.Trial) -> None:
"""Update the pool using a single trial."""
parent_fly_id = int(trial.metadata.ns('eagle').get('parent_fly_id'))
parent_fly = self._firefly_pool.find_parent_fly(parent_fly_id)
if parent_fly is None:
logging.info('Parent fly ID: %s is not in pool.', parent_fly_id)
if trial.infeasible:
# Ignore infeasible trials without parent fly.
logging.info('Got infeasible trial without a parent fly in the pool.')
pass
elif self._firefly_pool.size < self._firefly_pool.capacity:
# Pool is below capacity. Create a new firefly or update existing one.
logging.info(
'Pool is below capacity (size=%s). Invoke create or update pool.',
self._firefly_pool.size,
)
self._firefly_pool.create_or_update_fly(trial, parent_fly_id)
return
else:
Expand All @@ -387,31 +373,16 @@ def _update_one(self, trial: vz.Trial) -> None:

if parent_fly is None:
# Parent fly wasn't established. No need to continue.
logging.info('Parent fly was not established.')
return

elif not trial.infeasible and self._utils.is_better_than(
trial, parent_fly.trial):
# There's improvement. Update the parent with the new trial.
logging.info(
'Good step:\nParent trial (val=%s): %s\nChild trial (val=%s): %s\n',
self._utils.get_metric(parent_fly.trial),
self._utils.display_trial(parent_fly.trial),
self._utils.get_metric(trial),
self._utils.display_trial(trial),
)
parent_fly.trial = trial
parent_fly.generation += 1
else:
# There's no improvement. Penalize the parent by decreasing its
# exploration capability and potenitally remove it from the pool.
logging.info(
'Bad step:\nParent trial (val=%s): %s\nChild trial (val=%s): %s\n',
self._utils.get_metric(parent_fly.trial),
self._utils.display_trial(parent_fly.trial),
self._utils.get_metric(trial),
self._utils.display_trial(trial),
)
self._penalize_parent_fly(parent_fly, trial)

def _assign_closest_parent(self, trial: vz.Trial) -> Optional[Firefly]:
Expand Down Expand Up @@ -458,21 +429,10 @@ def _penalize_parent_fly(self, parent_fly: Firefly, trial: vz.Trial) -> None:
parent_fly.perturbation = min(
parent_fly.perturbation * 10, self._config.max_perturbation
)
logging.info(
('Penalize Parent Id: %s. Parameters are stuck. '
'New perturbation factor: %s'),
parent_fly.id_,
parent_fly.perturbation,
)
else:
# Otherwise, penalize the parent by decreasing its perturbation factor.
parent_fly.perturbation *= 0.9
logging.info(
('Penalize Parent Id: %s. Decrease perturbation factor. '
'New perturbation factor: %s'),
parent_fly.id_,
parent_fly.perturbation,
)

if parent_fly.perturbation < self._config.perturbation_lower_bound:
# If the perturbation factor is too low we attempt to eliminate the
# unsuccessful parent fly from the pool.
Expand All @@ -482,4 +442,3 @@ def _penalize_parent_fly(self, parent_fly: Firefly, trial: vz.Trial) -> None:
if not self._firefly_pool.is_best_fly(parent_fly):
# Check that the fly is not the best one we have thus far.
self._firefly_pool.remove_fly(parent_fly)
logging.info('Removed fly ID: %s from pool.', parent_fly.id_)
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

import collections
import copy
import logging
import math
from typing import Optional, Dict, DefaultDict
from typing import DefaultDict, Dict, Optional
from absl import logging
import attr
import numpy as np
from vizier import pyvizier as vz
Expand Down Expand Up @@ -104,7 +104,7 @@ def __attrs_post_init__(self):
self.problem_statement.single_objective_metric_name
)
self._goal = self.problem_statement.metric_information.item().goal
logging.info('EagleStrategyUtils was created.\n%s', str(self))
logging.info('EagleStrategyUtils instance was created.\n%s', str(self))

def compute_pull_weight_by_type(
self,
Expand Down Expand Up @@ -438,7 +438,6 @@ def get_shuffled_flies(self, rng: np.random.Generator) -> list[Firefly]:
def generate_new_fly_id(self) -> int:
"""Generates a unique fly id (starts from 0) to identify a fly in the pool."""
self._max_fly_id += 1
logging.info('New fly id generated (%s).', self._max_fly_id - 1)
return self._max_fly_id - 1

def get_next_moving_fly_copy(self) -> Firefly:
Expand Down Expand Up @@ -466,7 +465,6 @@ def get_next_moving_fly_copy(self) -> Firefly:
return copy.deepcopy(self._pool[current_fly_id])
current_fly_id += 1

logging.info("Couldn't find another fly in the pool to move.")
return copy.deepcopy(self._pool[self._last_id])

def is_best_fly(self, fly: Firefly) -> bool:
Expand Down Expand Up @@ -530,7 +528,6 @@ def create_or_update_fly(self, trial: vz.Trial, parent_fly_id: int) -> None:
"""
if parent_fly_id not in self._pool:
# Create a new Firefly in pool.
logging.info('Create a fly in pool. Parent fly ID: %s.', parent_fly_id)
new_fly = Firefly(
id_=parent_fly_id,
perturbation=self.utils.config.perturbation,
Expand All @@ -540,6 +537,5 @@ def create_or_update_fly(self, trial: vz.Trial, parent_fly_id: int) -> None:
self._pool[parent_fly_id] = new_fly
else:
# Parent fly id already in pool. Update trial if there was improvement.
logging.info('Parent fly ID (%s) is already in the pool.', parent_fly_id)
if self.utils.is_better_than(trial, self._pool[parent_fly_id].trial):
self._pool[parent_fly_id].trial = trial
3 changes: 1 addition & 2 deletions vizier/_src/algorithms/testing/comparator_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@
use of the class.
"""

import logging

from absl import logging
import attr
from jax import random
import numpy as np
Expand Down
2 changes: 1 addition & 1 deletion vizier/_src/algorithms/testing/simple4d_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

"""Simple4D convergence tests."""

import logging
from absl import logging
import attrs
from vizier import algorithms as vza
from vizier._src.benchmarks.experimenters.synthetic import simple4d
Expand Down

0 comments on commit b24e46c

Please sign in to comment.