Skip to content

Commit 72e872a

Browse files
esantorellafacebook-github-bot
authored andcommitted
Set seed in Turbo tutorial + tiny improvements (meta-pytorch#1643)
Summary: ## Motivation The graphs in meta-pytorch#1591 show that tutorials tend to vary quite a bit from run to run in terms of both runtime and memory usage, especially Turbo. This will make it hard for us to spot improvements or degradations in performance. I also checked the information_theoretic_acquisition_functions, constraint_active_search, and composite_bo_with_hogp tutorials to see if setting the seed would help since those seemed to be a bit variable too, but they seem to be (numerically) deterministic. I also attempted to take a look at the thompson_sampling tutorial, but it's just been making my laptop warm and loud. ### Have you read the [Contributing Guidelines on pull requests](https://github.com/pytorch/botorch/blob/main/CONTRIBUTING.md#pull-requests)? [x] Set seed in Turbo tutorial [x] Added command-line argument for running one tutorial by name [x]: `composite_bo_with_hogp` tutorial: Remove 'maxiter' and 'disp' arguments from `fit_gpytorch_torch` , since they were being ignored anyway (Adam doesn't accept them). Print instead of logging. [ ] Run Turbo without smoke test in order to make sure the inputs match the outputs again Pull Request resolved: meta-pytorch#1643 Test Plan: - Repeated Actions runs confirm that Turbo is much more consistent in runtime and memory before Reviewed By: Balandat Differential Revision: D42723251 Pulled By: esantorella fbshipit-source-id: b4209845f5e695066153bcceee75a80462854863
1 parent 2a4baa1 commit 72e872a

File tree

4 files changed

+29
-17
lines changed

4 files changed

+29
-17
lines changed

botorch/optim/optimize.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
"sample_around_best",
4646
"sample_around_best_sigma",
4747
"sample_around_best_prob_perturb",
48-
"sample_around_best_prob_perturb",
4948
"seed",
5049
"thinning",
5150
}

scripts/run_tutorials.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,17 +103,24 @@ def run_tutorials(
103103
repo_dir: str,
104104
include_ignored: bool = False,
105105
smoke_test: bool = False,
106+
name: Optional[str] = None,
106107
) -> None:
107-
print(f"Running tutorials in {'smoke test' if smoke_test else 'standard'} mode.")
108+
print(f"Running tutorial(s) in {'smoke test' if smoke_test else 'standard'} mode.")
108109
if not smoke_test:
109110
print("This may take a long time...")
110111
tutorial_dir = Path(repo_dir).joinpath("tutorials")
111112
num_runs = 0
112113
num_errors = 0
113114
ignored_tutorials = IGNORE if smoke_test else IGNORE | IGNORE_SMOKE_TEST_ONLY
114-
for tutorial in tutorial_dir.iterdir():
115-
if not tutorial.is_file or tutorial.suffix != ".ipynb":
116-
continue
115+
116+
tutorials = sorted(
117+
t for t in tutorial_dir.iterdir() if t.is_file and t.suffix == ".ipynb"
118+
)
119+
if name is not None:
120+
tutorials = [t for t in tutorials if t.name == name]
121+
if len(tutorials) == 0:
122+
raise RuntimeError(f"Specified tutorial {name} not found in directory.")
123+
for tutorial in tutorials:
117124
if not include_ignored and tutorial.name in ignored_tutorials:
118125
print(f"Ignoring tutorial {tutorial.name}.")
119126
continue
@@ -141,9 +148,17 @@ def run_tutorials(
141148
action="store_true",
142149
help="Run all tutorials (incl. ignored).",
143150
)
151+
parser.add_argument(
152+
"-n",
153+
"--name",
154+
help="Run a specific tutorial by name. The name should include the "
155+
".ipynb extension. If the tutorial is on the ignore list, you still need "
156+
"to specify --include-ignored.",
157+
)
144158
args = parser.parse_args()
145159
run_tutorials(
146160
repo_dir=args.path,
147161
include_ignored=args.include_ignored,
148162
smoke_test=args.smoke,
163+
name=args.name,
149164
)

tutorials/composite_bo_with_hogp.ipynb

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@
6363
"source": [
6464
"import torch\n",
6565
"import os\n",
66-
"import logging\n",
6766
"import math\n",
6867
"import matplotlib.pyplot as plt\n",
6968
"import time\n",
@@ -389,11 +388,9 @@
389388
"if SMOKE_TEST:\n",
390389
" n_batches = 1\n",
391390
" batch_size = 2\n",
392-
" n_trials = 1\n",
393391
"else:\n",
394392
" n_batches = 15\n",
395-
" batch_size = 4\n",
396-
" n_trials = 3"
393+
" batch_size = 4"
397394
],
398395
"execution_count": 8,
399396
"outputs": []
@@ -421,8 +418,6 @@
421418
"customOutput": null
422419
},
423420
"source": [
424-
"all_objective_vals = []\n",
425-
"\n",
426421
"with gpt_settings.cholesky_jitter(1e-4):\n",
427422
" c_batched, objective, bounds, num_samples = prepare_data(device=device, dtype=dtype)\n",
428423
"\n",
@@ -440,7 +435,7 @@
440435
" # get best observations, log status\n",
441436
" best_f = {k: objective(v).max().detach() for k, v in train_Y.items()}\n",
442437
"\n",
443-
" logging.info(\n",
438+
" print(\n",
444439
" f\"It {i+1:>2}/{n_batches}, best obs.: \"\n",
445440
" \", \".join([f\"{k}: {v:.3f}\" for k, v in best_f.items()])\n",
446441
" )\n",
@@ -465,7 +460,7 @@
465460
" )\n",
466461
"\n",
467462
" mll = ExactMarginalLogLikelihood(model_ei.likelihood, model_ei)\n",
468-
" fit_gpytorch_torch(mll, options={\"lr\": 0.01, \"maxiter\": 3000, \"disp\": False})\n",
463+
" fit_gpytorch_torch(mll, options={\"lr\": 0.01})\n",
469464
"\n",
470465
" # generate qEI candidate (single output modeling)\n",
471466
" qEI = qExpectedImprovement(model_ei, best_f=best_f[\"ei\"], sampler=sampler)\n",
@@ -480,7 +475,7 @@
480475
" )\n",
481476
"\n",
482477
" mll = ExactMarginalLogLikelihood(model_ei_hogp_cf.likelihood, model_ei_hogp_cf)\n",
483-
" fit_gpytorch_torch(mll, options={\"lr\": 0.01, \"maxiter\": 3000, \"disp\": False})\n",
478+
" fit_gpytorch_torch(mll, options={\"lr\": 0.01})\n",
484479
"\n",
485480
" # generate qEI candidate (multi-output modeling)\n",
486481
" qEI_hogp_cf = qExpectedImprovement(\n",
@@ -498,7 +493,7 @@
498493
" train_X[k] = torch.cat([Xold, Xnew])\n",
499494
" train_Y[k] = torch.cat([train_Y[k], c_batched(Xnew)])\n",
500495
"\n",
501-
" logging.info(f\"Wall time: {time.monotonic() - tic:1f}\")\n",
496+
" print(f\"Wall time: {time.monotonic() - tic:1f}\")\n",
502497
"\n",
503498
" objective_dict = {k: objective(train_Y[k]) for k in train_Y}"
504499
],

tutorials/turbo_1.ipynb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,7 @@
401401
"RAW_SAMPLES = 512 if not SMOKE_TEST else 4\n",
402402
"N_CANDIDATES = min(5000, max(2000, 200 * dim)) if not SMOKE_TEST else 4\n",
403403
"\n",
404+
"torch.manual_seed(0)\n",
404405
"\n",
405406
"while not state.restart_triggered: # Run until TuRBO converges\n",
406407
" # Fit a GP model\n",
@@ -565,6 +566,8 @@
565566
}
566567
],
567568
"source": [
569+
"torch.manual_seed(0)\n",
570+
"\n",
568571
"X_ei = get_initial_points(dim, n_init)\n",
569572
"Y_ei = torch.tensor(\n",
570573
" [eval_objective(x) for x in X_ei], dtype=dtype, device=device\n",
@@ -700,7 +703,7 @@
700703
"bento/extensions/theme/main.css": true
701704
},
702705
"kernelspec": {
703-
"display_name": "Python 3",
706+
"display_name": "Python 3 (ipykernel)",
704707
"language": "python",
705708
"name": "python3"
706709
},
@@ -714,7 +717,7 @@
714717
"name": "python",
715718
"nbconvert_exporter": "python",
716719
"pygments_lexer": "ipython3",
717-
"version": "3.8.3"
720+
"version": "3.10.8"
718721
}
719722
},
720723
"nbformat": 4,

0 commit comments

Comments
 (0)