Fast GP model evaluation¶
For visualization in tools like Badger, same model is frequently reused many times. It is possible to use advanced PyTorch functionality to speed up the evaluation of the model and acquisition function. This comes with many caveats that limit the flexibility and dynamic/conditional features. As a general rule, if any non-tensor model parameter is changed, objects must be recompiled.
InĀ [1]:
Copied!
from copy import deepcopy
from xopt import Xopt, Evaluator
from xopt.generators.bayesian import UpperConfidenceBoundGenerator
from xopt.resources.test_functions.tnk import evaluate_TNK, tnk_vocs
# Ignore all warnings
import warnings
warnings.filterwarnings("ignore")
vocs = deepcopy(tnk_vocs)
vocs.objectives = {"y2": "MINIMIZE"}
generator = UpperConfidenceBoundGenerator(vocs=vocs)
generator.use_cuda = True
evaluator = Evaluator(function=evaluate_TNK)
X = Xopt(generator=generator, evaluator=evaluator)
generator = X.generator
from copy import deepcopy
from xopt import Xopt, Evaluator
from xopt.generators.bayesian import UpperConfidenceBoundGenerator
from xopt.resources.test_functions.tnk import evaluate_TNK, tnk_vocs
# Ignore all warnings
import warnings
warnings.filterwarnings("ignore")
vocs = deepcopy(tnk_vocs)
vocs.objectives = {"y2": "MINIMIZE"}
generator = UpperConfidenceBoundGenerator(vocs=vocs)
generator.use_cuda = True
evaluator = Evaluator(function=evaluate_TNK)
X = Xopt(generator=generator, evaluator=evaluator)
generator = X.generator
/home/runner/work/Xopt/Xopt/.venv/lib/python3.12/site-packages/pyro/ops/stats.py:527: SyntaxWarning: invalid escape sequence '\g'
we have :math:`ES^{*}(P,Q) \ge ES^{*}(Q,Q)` with equality holding if and only if :math:`P=Q`, i.e.
InĀ [2]:
Copied!
# add a lot of points, slowing down model
X.random_evaluate(1000);
# add a lot of points, slowing down model
X.random_evaluate(1000);
InĀ [3]:
Copied!
X.generator.train_model();
X.generator.train_model();
InĀ [4]:
Copied!
m = deepcopy(X.generator.model)
m = deepcopy(X.generator.model)
InĀ [5]:
Copied!
fig, ax = X.generator.visualize_model(
n_grid=30, output_names=["y2"], show_acquisition=False, show_samples=False
)
fig, ax = X.generator.visualize_model(
n_grid=30, output_names=["y2"], show_acquisition=False, show_samples=False
)
InĀ [6]:
Copied!
X.generator.model = m
X.generator.model = m
InĀ [7]:
Copied!
# first call does tracing
fig, ax = X.generator.visualize_model(
n_grid=30,
model_compile_mode="trace",
output_names=["y2"],
show_acquisition=False,
show_samples=False,
)
# first call does tracing
fig, ax = X.generator.visualize_model(
n_grid=30,
model_compile_mode="trace",
output_names=["y2"],
show_acquisition=False,
show_samples=False,
)
InĀ [8]:
Copied!
# second invocation uses a pre-traced model and is a tiny bit faster (most of walltime is used by plotting)
fig, ax = X.generator.visualize_model(
n_grid=30,
model_compile_mode="trace",
output_names=["y2"],
show_acquisition=False,
show_samples=False,
)
# second invocation uses a pre-traced model and is a tiny bit faster (most of walltime is used by plotting)
fig, ax = X.generator.visualize_model(
n_grid=30,
model_compile_mode="trace",
output_names=["y2"],
show_acquisition=False,
show_samples=False,
)