|
| 1 | +#! /usr/bin/env python3 |
| 2 | + |
| 3 | +# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved |
| 4 | + |
| 5 | +import unittest |
| 6 | + |
| 7 | +import torch |
| 8 | +from botorch.exceptions import UnsupportedError |
| 9 | +from botorch.models import ( |
| 10 | + FixedNoiseGP, |
| 11 | + HeteroskedasticSingleTaskGP, |
| 12 | + ModelListGP, |
| 13 | + SingleTaskGP, |
| 14 | +) |
| 15 | +from botorch.models.converter import batched_to_model_list, model_list_to_batched |
| 16 | + |
| 17 | +from .test_gpytorch import SimpleGPyTorchModel |
| 18 | + |
| 19 | + |
| 20 | +class TestConverters(unittest.TestCase): |
| 21 | + def test_batched_to_model_list(self, cuda=False): |
| 22 | + device = torch.device("cuda") if cuda else torch.device("cpu") |
| 23 | + for dtype in (torch.float, torch.double): |
| 24 | + # test SingleTaskGP |
| 25 | + train_X = torch.rand(10, 2, device=device, dtype=dtype) |
| 26 | + train_Y1 = train_X.sum(dim=-1) |
| 27 | + train_Y2 = train_X[:, 0] - train_X[:, 1] |
| 28 | + train_Y = torch.stack([train_Y1, train_Y2], dim=-1) |
| 29 | + batch_gp = SingleTaskGP(train_X, train_Y) |
| 30 | + list_gp = batched_to_model_list(batch_gp) |
| 31 | + self.assertIsInstance(list_gp, ModelListGP) |
| 32 | + # test FixedNoiseGP |
| 33 | + batch_gp = FixedNoiseGP(train_X, train_Y, torch.rand_like(train_Y)) |
| 34 | + list_gp = batched_to_model_list(batch_gp) |
| 35 | + self.assertIsInstance(list_gp, ModelListGP) |
| 36 | + # test HeteroskedasticSingleTaskGP |
| 37 | + batch_gp = HeteroskedasticSingleTaskGP( |
| 38 | + train_X, train_Y, torch.rand_like(train_Y) |
| 39 | + ) |
| 40 | + with self.assertRaises(NotImplementedError): |
| 41 | + batched_to_model_list(batch_gp) |
| 42 | + |
| 43 | + def test_batched_to_model_list_cuda(self): |
| 44 | + if torch.cuda.is_available(): |
| 45 | + self.test_batched_to_model_list(cuda=True) |
| 46 | + |
| 47 | + def test_model_list_to_batched(self, cuda=False): |
| 48 | + device = torch.device("cuda") if cuda else torch.device("cpu") |
| 49 | + for dtype in (torch.float, torch.double): |
| 50 | + # basic test |
| 51 | + train_X = torch.rand(10, 2, device=device, dtype=dtype) |
| 52 | + train_Y1 = train_X.sum(dim=-1) |
| 53 | + train_Y2 = train_X[:, 0] - train_X[:, 1] |
| 54 | + gp1 = SingleTaskGP(train_X, train_Y1) |
| 55 | + gp2 = SingleTaskGP(train_X, train_Y2) |
| 56 | + list_gp = ModelListGP(gp1, gp2) |
| 57 | + batch_gp = model_list_to_batched(list_gp) |
| 58 | + self.assertIsInstance(batch_gp, SingleTaskGP) |
| 59 | + # test degenerate (single model) |
| 60 | + batch_gp = model_list_to_batched(ModelListGP(gp1)) |
| 61 | + self.assertEqual(batch_gp._num_outputs, 1) |
| 62 | + # test different model classes |
| 63 | + gp2 = FixedNoiseGP(train_X, train_Y1, torch.ones_like(train_Y1)) |
| 64 | + with self.assertRaises(UnsupportedError): |
| 65 | + model_list_to_batched(ModelListGP(gp1, gp2)) |
| 66 | + # test non-batched models |
| 67 | + gp1_ = SimpleGPyTorchModel(train_X, train_Y1) |
| 68 | + gp2_ = SimpleGPyTorchModel(train_X, train_Y2) |
| 69 | + with self.assertRaises(UnsupportedError): |
| 70 | + model_list_to_batched(ModelListGP(gp1_, gp2_)) |
| 71 | + # test list of multi-output models |
| 72 | + train_Y = torch.stack([train_Y1, train_Y2], dim=-1) |
| 73 | + gp2 = SingleTaskGP(train_X, train_Y) |
| 74 | + with self.assertRaises(UnsupportedError): |
| 75 | + model_list_to_batched(ModelListGP(gp1, gp2)) |
| 76 | + # test different training inputs |
| 77 | + gp2 = SingleTaskGP(2 * train_X, train_Y2) |
| 78 | + with self.assertRaises(UnsupportedError): |
| 79 | + model_list_to_batched(ModelListGP(gp1, gp2)) |
| 80 | + # check scalar agreement |
| 81 | + gp2 = SingleTaskGP(train_X, train_Y2) |
| 82 | + gp2.likelihood.noise_covar.noise_prior.rate.fill_(1.0) |
| 83 | + with self.assertRaises(UnsupportedError): |
| 84 | + model_list_to_batched(ModelListGP(gp1, gp2)) |
| 85 | + # check tensor shape agreement |
| 86 | + gp2 = SingleTaskGP(train_X, train_Y2) |
| 87 | + gp2.covar_module.raw_outputscale = torch.nn.Parameter( |
| 88 | + torch.tensor([0.0], device=device, dtype=dtype) |
| 89 | + ) |
| 90 | + with self.assertRaises(UnsupportedError): |
| 91 | + model_list_to_batched(ModelListGP(gp1, gp2)) |
| 92 | + # test HeteroskedasticSingleTaskGP |
| 93 | + gp2 = HeteroskedasticSingleTaskGP( |
| 94 | + train_X, train_Y1, torch.ones_like(train_Y1) |
| 95 | + ) |
| 96 | + with self.assertRaises(NotImplementedError): |
| 97 | + model_list_to_batched(ModelListGP(gp2)) |
| 98 | + # test FixedNoiseGP |
| 99 | + train_X = torch.rand(10, 2, device=device, dtype=dtype) |
| 100 | + train_Y1 = train_X.sum(dim=-1) |
| 101 | + train_Y2 = train_X[:, 0] - train_X[:, 1] |
| 102 | + gp1_ = FixedNoiseGP(train_X, train_Y1, torch.rand_like(train_Y1)) |
| 103 | + gp2_ = FixedNoiseGP(train_X, train_Y2, torch.rand_like(train_Y2)) |
| 104 | + list_gp = ModelListGP(gp1_, gp2_) |
| 105 | + batch_gp = model_list_to_batched(list_gp) |
| 106 | + |
| 107 | + def test_model_list_to_batched_cuda(self): |
| 108 | + if torch.cuda.is_available(): |
| 109 | + self.test_model_list_to_batched(cuda=True) |
| 110 | + |
| 111 | + def test_roundtrip(self, cuda=False): |
| 112 | + device = torch.device("cuda") if cuda else torch.device("cpu") |
| 113 | + for dtype in (torch.float, torch.double): |
| 114 | + train_X = torch.rand(10, 2, device=device, dtype=dtype) |
| 115 | + train_Y1 = train_X.sum(dim=-1) |
| 116 | + train_Y2 = train_X[:, 0] - train_X[:, 1] |
| 117 | + train_Y = torch.stack([train_Y1, train_Y2], dim=-1) |
| 118 | + # SingleTaskGP |
| 119 | + batch_gp = SingleTaskGP(train_X, train_Y) |
| 120 | + list_gp = batched_to_model_list(batch_gp) |
| 121 | + batch_gp_recov = model_list_to_batched(list_gp) |
| 122 | + sd_orig = batch_gp.state_dict() |
| 123 | + sd_recov = batch_gp_recov.state_dict() |
| 124 | + self.assertTrue(set(sd_orig) == set(sd_recov)) |
| 125 | + self.assertTrue(all(torch.equal(sd_orig[k], sd_recov[k]) for k in sd_orig)) |
| 126 | + # FixedNoiseGP |
| 127 | + batch_gp = FixedNoiseGP(train_X, train_Y, torch.rand_like(train_Y)) |
| 128 | + list_gp = batched_to_model_list(batch_gp) |
| 129 | + batch_gp_recov = model_list_to_batched(list_gp) |
| 130 | + sd_orig = batch_gp.state_dict() |
| 131 | + sd_recov = batch_gp_recov.state_dict() |
| 132 | + self.assertTrue(set(sd_orig) == set(sd_recov)) |
| 133 | + self.assertTrue(all(torch.equal(sd_orig[k], sd_recov[k]) for k in sd_orig)) |
| 134 | + |
| 135 | + def test_roundtrip_cuda(self): |
| 136 | + if torch.cuda.is_available(): |
| 137 | + self.test_roundtrip(cuda=True) |
0 commit comments