Skip to content

Support Multi-Objective Optimization GPSampler - #6069

Merged
nabenabe0928 merged 20 commits into
optuna:masterfrom
nabenabe0928:feat/support-ehvi-in-gp-sampler
May 16, 2025
Merged

nabenabe0928 merged 20 commits into
optuna:masterfrom
nabenabe0928:feat/support-ehvi-in-gp-sampler

Conversation

@nabenabe0928

Copy link
Copy Markdown
Contributor

Motivation

This PR adds the multi-objective GPSampler as a followup of these PRs:

Description of the changes

  • Modify GPSampler so that the multi-objective optimization is supported

@nabenabe0928 nabenabe0928 changed the title Feat/support ehvi in gp sampler Support Multi-Objective Optimization GPSampler May 2, 2025
@nabenabe0928
nabenabe0928 marked this pull request as ready for review May 2, 2025 05:51
@c-bata

c-bata commented May 12, 2025

Copy link
Copy Markdown
Member

@kAIto47802 @HideakiImamura Could you review this PR?

@HideakiImamura HideakiImamura left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the PR. I have several comments. Could you take a look?

Comment thread optuna/samplers/_gp/sampler.py Resolved
Comment thread optuna/samplers/_gp/sampler.py Resolved
Comment thread optuna/samplers/_gp/sampler.py Outdated
Comment on lines +290 to +293
size = min(self._n_local_search // 2, n_pareto_sols)
chosen_indices = self._rng.rng.choice(
np.arange(n_pareto_sols), size=size, replace=False
)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the solution on the Pareto-front is large enough, you randomly select n_local_search // 2 solutions from it as best_params. Is there any reference for this selection?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this is a heuristic inspired from the single-objective optimization by GPSampler.
We can also follow the BoTorch convention if necessary.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. This raises a performance concern. In multi-objective optimization, unlike single-objective optimization, there are multiple Pareto-optimal solutions to reach (and in many cases, there are infinitely many). Therefore, we should ideally start from multiple points for each of these infinitely many Pareto solutions. Of course, that is not feasible, but at the very least, shouldn’t we be optimizing the acquisition function toward Pareto solutions using a larger number of initial points compared to single-objective optimization? I suspect that this might be the reason for the performance difference compared to BoTorch. How does BoTorch handle this?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you check this comment?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although BoTorch itself does not have a fixed parameter for the number of local searches, BoTorchSampler does have it.

I would like you to refer to this comment for more details:

Comment thread optuna/samplers/_gp/sampler.py Resolved
Comment thread optuna/samplers/_gp/sampler.py Resolved
Comment thread optuna/samplers/_gp/sampler.py Resolved
Comment thread optuna/samplers/_gp/sampler.py Resolved
Comment thread tests/samplers_tests/test_samplers.py Resolved
@nabenabe0928

nabenabe0928 commented May 13, 2025 •

Copy link
Copy Markdown
Contributor Author

I documented the differences between BoTorchSampler and GPSampler at the time of this comment:

  • Number of local search: 20 (BoTorchSampler) vs 10 (GPSampler)
  • Number of warm start sampling (QMC): 1024 (BoTorchSampler) vs 2048 (GPSampler)
  • Warm start sampling selection: Roulette selection with standardization (BoTorchSampler) vs 5 best parameters from the past and roulette selection by the ratios with the maximum acquisition function value (GPSampler)
  • Reference point: minimum minus small value (BoTorchSampler) vs Same as TPESampler (GPSampler)
  • EHVI calculation: Monte-Carlo expectation of HVI values based on log_fatplus (BoTorchSampler) vs Monte-Carlo expectation of the exact HVI values (GPSampler)
  • Gaussian process modeling: Joint modeling of all the objectives (BoTorchSampler) vs separated modeling of each objective (GPSampler)
  • Prior of kernel hyperparameters

Note

The default configuration of the logEHVI in BoTorch is available here.

I personally believe that the performance degradation may happen due to the difference in the kernel hyperparameter prior.

Note that the fatplus things are discussed in this paper.
But as long as I locally observed, these operations are not the cause of the degradation.

More specifically, this requires some changes here:

diff = torch.maximum(
_EPS,
torch.minimum(Y_post[..., torch.newaxis, :], non_dominated_box_upper_bounds)
- non_dominated_box_lower_bounds,
)
# NOTE(nabenabe): logsumexp with dim=-1 is for the HVI calculation and that with dim=-2 is for
# expectation of the HVIs over the fixed_samples.
return torch.special.logsumexp(diff.log().sum(dim=-1), dim=(-2, -1)) - log_n_qmc_samples

# log_smooth_relu and smooth_minimum must be implemented.
# cf. https://github.com/pytorch/botorch/blob/v0.13.0/botorch/utils/safe_math.py
# cf. https://github.com/pytorch/botorch/blob/v0.13.0/botorch/acquisition/multi_objective/logei.py
# smooth_minimum could be torch.minimum as well.
log_diff = smooth_minimum(
    log_smooth_relu(Y_post[..., torch.newaxis, :] - non_dominated_box_lower_bounds),
    (non_dominated_box_upper_bounds - non_dominated_box_lower_bounds).log()
)
return torch.special.logsumexp(log_diff.sum(dim=-1), dim=(-2, -1)) - log_n_qmc_samples
log_smooth_relu
from torch.nn.functional import softplus


def log_smooth_relu(x: torch.Tensor) -> torch.Tensor:
    # tau = 1e-6 is the default value in BoTorch.
    # https://github.com/pytorch/botorch/blob/v0.13.0/botorch/acquisition/multi_objective/logei.py#L301
    tau = 1e-6
    # alpha = 1e-1 is the default value in BoTorch.
    # https://github.com/pytorch/botorch/blob/v0.14.0/botorch/utils/safe_math.py#L297
    alpha = 1e-1
    z = x / tau
    return tau * (alpha / (1.0 + z**2) + softplus(z))
fat minimum/maximum
import torch


def fatmax(x: torch.Tensor, dim: int, tau: float) -> torch.Tensor:
    max_val = x.amax(dim=dim, keepdim=True)
    diff = x - max_val
    diff = diff.masked_fill(diff.isnan(), 0.0)
    z = -diff / tau
    return max_val.squeeze(dim) + tau * (1.0 / (1.0 + z + 0.5 * z**2)).sum(dim=dim).log()


def fatmin(x: torch.Tensor, dim: int, tau: float) -> torch.Tensor:
    min_val = x.amin(dim=dim, keepdim=True)
    diff = min_val - x
    diff = diff.masked_fill(diff.isnan(), 0.0)
    z = -diff / tau
    return min_val.squeeze(dim) - tau * (1.0 / (1.0 + z + 0.5 * z**2)).sum(dim=dim).log()


def fatmaximum(a: torch.Tensor, b: torch.Tensor, tau: float) -> torch.Tensor:
    return fatmax(torch.stack(torch.broadcast_tensors(a, b), dim=-1), dim=-1, tau=tau)


def fatminimum(a: torch.Tensor, b: torch.Tensor, tau: float) -> torch.Tensor:
    return fatmin(torch.stack(torch.broadcast_tensors(a, b), dim=-1), dim=-1, tau=tau)

@kAIto47802 kAIto47802 left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've just started my review, but for now, I'll leave some comments on what I've noticed so far. PTAL :octocat:

Comment thread optuna/samplers/_gp/sampler.py Resolved
Comment thread optuna/samplers/_gp/sampler.py Resolved
Comment thread optuna/samplers/_gp/sampler.py Resolved

@HideakiImamura HideakiImamura left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update. Basically, LGTM. I have some additional comments. PTAL.

Comment thread optuna/samplers/_gp/sampler.py Resolved
Comment thread optuna/samplers/_gp/sampler.py Outdated
Comment on lines +290 to +293
size = min(self._n_local_search // 2, n_pareto_sols)
chosen_indices = self._rng.rng.choice(
np.arange(n_pareto_sols), size=size, replace=False
)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you check this comment?

@nabenabe0928

Copy link
Copy Markdown
Contributor Author

Comment thread optuna/samplers/_gp/sampler.py Resolved
Comment thread optuna/samplers/_gp/sampler.py Resolved
Comment thread optuna/samplers/_gp/sampler.py Resolved
Comment thread optuna/samplers/_gp/sampler.py Resolved

@HideakiImamura HideakiImamura left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! LGTM!

@HideakiImamura HideakiImamura removed their assignment May 16, 2025
@HideakiImamura HideakiImamura added the feature Change that does not break compatibility, but affects the public interfaces. label May 16, 2025
@HideakiImamura HideakiImamura added this to the v4.4.0 milestone May 16, 2025
qmc_seed=self._rng.rng.randint(1 << 30),
)
pareto_params = normalized_params[
_is_pareto_front(-standardized_score_vals, assume_unique_lexsorted=False)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the sign has changed in 81e7e8e and the choice has changed from the worst one to the best one, the benchmarking results might become different.

@nabenabe0928 nabenabe0928 May 16, 2025 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeap, you are right. I will work on it at another time!

@kAIto47802 kAIto47802 left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your update. LGTM!

Comment thread optuna/samplers/_gp/sampler.py Resolved
@nabenabe0928
nabenabe0928 enabled auto-merge May 16, 2025 17:09
@nabenabe0928
nabenabe0928 merged commit 7236ea3 into optuna:master May 16, 2025
@nabenabe0928

Copy link
Copy Markdown
Contributor Author

Other than these,

The prior for the kernel hyperparameters is different from BoTorch, whose prior follows this paper.

@optuna optuna deleted a comment from HideakiImamura May 26, 2025
@nabenabe0928

Copy link
Copy Markdown
Contributor Author

Note

Memo for future development.

Talking about this concern, we can discuss it by checking how much acquisition function value we get at each Pareto solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature Change that does not break compatibility, but affects the public interfaces.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants