Skip to main content

lapeft-bayesopt: Discrete Bayesian Optimization with LLM + PEFT + Laplace Approximation

This is the accompanying library of the paper A Sober Look at LLMs for Material Discovery: Are They Actually Good for Bayesian Optimization Over Molecules?.

@inproceedings{kristiadi2024sober,
  title={A Sober Look at {LLMs} for Material Discovery: {A}re They Actually Good for {B}ayesian Optimization Over Molecules?},
  author={Kristiadi, Agustinus and Strieth-Kalthoff, Felix and Skreta, Marta and Poupart, Pascal and Aspuru-Guzik, Al\'{a}n and Pleiss, Geoff},
  booktitle={ICML},
  year={2024}
}

Table of Contents

  1. Setup
  2. Warmup: Using LLMs as fixed feature extractors
  3. Using finetuned LLMs as surrogates

Setup

You need Python 3.11 or newer. If you want a specific PyTorch build (e.g. for your CUDA version), install it first: https://pytorch.org/get-started/locally/. Then install the package from PyPI. This also installs laplace-torch, transformers, and peft:

pip install lapeft-bayesopt

The examples are not part of the package. To run them, clone this repo and install it with the examples dependency group, which adds BoTorch, matplotlib, and scikit-learn:

git clone https://github.com/wiseodd/lapeft-bayesopt.git
cd lapeft-bayesopt
pip install -e . --group examples  # needs pip 25.1 or newer; or: uv sync --group examples
cd examples

Warmup: Using LLMs as fixed feature extractors

Full example: examples/run_fixed_features.py

The simplest way to incorporate LLMs into BO surrogates is by viewing them as fixed feature extractors. Given a data point $x \in \mathcal{D}_\mathrm{cand}$ from the pool of candidates $\mathcal{D}_\mathrm{cand} = \{x_1, \dots, x_n\}$ we want to find the best from, we wrap it in a textual prompt $c(x)$ and then do a forward pass over the LLM and take the last transformer embedding which has shape (seq_len, embd_dim). Then, we aggregate it by e.g. averaging over the sequence dimension to get a feature vector for $h(x)$ with shape (embd_dim,). Doing this for all $x$'s, we can then do the usual discrete BO loop with standard surrogate functions like GPs or Bayesian NNs over the candidates $\mathcal{D}_\mathrm{cand}$.

This package provides an easy way to do the transformation from $x$ to $h(x)$. Here are the steps:

  1. We assume that your dataset is a pandas dataframe. Inherit lapeft_bayesopt.problems.data_processor.DataProcessor. Example (from examples/data_processor.py):
from lapeft_bayesopt.problems.data_processor import DataProcessor


class RedoxDataProcessor(DataProcessor):
    """
    Pandas dataframe spec:
    ----------------------
    RangeIndex: 1407 entries, 0 to 1406
    Data columns (total 6 columns):
    #   Column                 Non-Null Count  Dtype
    --  ------                 --------------  -----
    0   Entry Number           1407 non-null   int64
    1   File Name              1407 non-null   object
    2   SMILES                 1407 non-null   object
    3   Ered                   1407 non-null   float64
    4   HOMO                   1407 non-null   float64
    5   Gsol                   1407 non-null   float64
    6   Absorption Wavelength  1407 non-null   float64
    dtypes: float64(4), int64(1), object(2)
    memory usage: 77.1+ KB

    Objective: Minimize Ered over a list of molecules in SMILES.
    """

    def __init__(self, prompt_builder, tokenizer):
        # num_outputs = 1 since this is a single-objective problem
        super().__init__(
            prompt_builder=prompt_builder, num_outputs=1, tokenizer=tokenizer
        )

        # We must specify the four properties below
        # -----------------------------------------
        # `x_col` is the column name of your x
        self.x_col = "SMILES"
        # `target_col` is the pandas column name of the property that we want to optimize
        self.target_col = "Ered"
        # `obj_str` is the textual description of that property (useful for prompting the LLM later)
        self.obj_str = "redox potential"
        # `maximization` is whether we want to maximize or minimize the property
        self.maximization = False

    def _get_columns_to_remove(self) -> list[str]:
        # List all columns of your dataset! We will remove all of them after we preprocess the dataset using Huggingface (we only need the resulting `input_ids` and `labels`)
        return [
            "Entry Number",
            "File Name",
            "SMILES",
            "HOMO",
            "Ered",
            "Gsol",
            "Absorption Wavelength",
        ]
  1. Next, we create prompting schemes $c(x)$. Here's an example (from examples/prompting.py):
from lapeft_bayesopt.problems.prompting import PromptBuilder


class MyPromptBuilder(PromptBuilder):
    def __init__(self, kind: str):
        self.kind = kind

    def get_prompt(self, smiles_str: str, obj_str: str) -> str:
        if self.kind == "completion":
            return f"The estimated {obj_str} of the molecule {smiles_str} is: "
        elif self.kind == "just-smiles":
            return smiles_str
        else:
            return NotImplementedError
  1. Then, we need the LLM feature extractor itself. This package has some ready-made ones, e.g., lapeft_bayesopt.foundation_models.t5.T5Regressor. Feel free to follow that example to create your own.

  2. Then, we can start extracting the LLM features from $\mathcal{D}_\mathrm{cand}$. See the load_features method in examples/run_fixed_features.py.

  3. Finally, we can do the discrete BO loop using those features (cache provided in examples/data/cache). See examples/run_fixed_features.py for a complete, self-contained example. Note that, at this point, we can use any BO algorithm and surrogate function. E.g. we can just use BoTorch for the BO loop.

Using finetuned LLMs as surrogates

Note: Please check the previous section first since we will reuse some objects here.

Full example: examples/run_finetuning.py

We can go one step further by making $h(x)$ trainable. This can be done by attaching a PEFT method (LoRA, PrefixTuning, Adapter, etc) to the frozen LLM. Then, we train and do a Laplace approximation on the PEFT's and regression head's weights.

To do this, we can use the surrogates provided in lapeft_bayesopt.surrogates. Currently LoRA is supported and it is very easy to support other PEFT methods, using Huggingface's peft library.

See examples/run_finetuning.py for an example. It's actually quite simple to use!

  1. First, define the base PEFT-infused LLM in a function so that it is freshly initialized at each call. (Useful since at each BO iteration, the surrogate model is retrained.)
def get_model():
    # Load a foundation model with a regression head attached
    model = T5Regressor(
        kind="GT4SD/multitask-text-and-chemistry-t5-base-augm", tokenizer=tokenizer
    )

    # Attach LoRA or any other PEFT on the foundation model
    target_modules = ["q", "v"]
    config = LoraConfig(
        r=4,
        lora_alpha=16,
        target_modules=target_modules,
        lora_dropout=0.1,
        bias="none",
        # This is necessary so that the regression head is also trained
        modules_to_save=["head"],
    )
    lora_model = get_peft_model(model, config)

    # For some reason HF's peft duplicates the head. So we need to "detach" the one that is unused
    for p in lora_model.base_model.head.original_module.parameters():
        p.requires_grad = False

    return lora_model
  1. Then, we can configure the training and the Laplace approximation of this PEFT surrogate. For full options, check out lapeft_bayesopt.utils.configs.LaplaceConfig
# Config for the Laplace approx over PEFT
config = LaplaceConfig(
    noise_var=0.001,
    hess_factorization="kron",
    subset_of_weights="all",
    marglik_mode="posthoc",
    prior_prec_structure="layerwise",
)
  1. We then initialize a training dataset, e.g. via random sampling from the candidate set (a pandas dataframe). The training dataset should be a list of pandas rows or dicts. Don't forget to remove that row from the original dataset $\mathcal{D}_\mathrm{cand}$. We can use lapeft_bayesopt.utils.helpers.pop_df() to do so.
dataset_train = []
while len(dataset_train) < n_init_data:
    idx = np.random.randint(len(pd_dataset))
    # Make sure that the optimum is not included
    if pd_dataset.loc[idx][OBJ_COL] >= ground_truth_max:
        continue
    dataset_train.append(helpers.pop_df(pd_dataset, idx))
  1. Next, create the surrogate.
# Create the surrogate model based on the LLM+PEFT regressor above
model = LAPEFTBayesOptLoRA(
    get_model, dataset_train, data_processor, laplace_config=config
)
  1. At each BO iteration, we preprocess the candidate $x$'s and infer the posterior mean and variance of the Laplace approximation over PEFT (LAPEFT!) to compute the acquisition function. Here we use an approximate Thompson sampling, but other acquisition function like EI can of course be used.
# Preprocess D_cand (`dataset`) so that we can make predictions over it
dataloader = data_processor.get_dataloader(pd_dataset, batch_size=16, shuffle=False)

# Make prediction over D_cand, get means and vars, compute the acqf
acq_vals = []
for data in dataloader:
    posterior = model.posterior(data)
    f_mean, f_var = posterior.mean, posterior.variance
    acq_vals.append(thompson_sampling(f_mean, f_var))
acq_vals = torch.cat(acq_vals, dim=0).cpu().squeeze()
  1. We pick the $x$ that maximizes the acquisition function and remove it from the candidate set. That $x$ is represented by a pandas row that we popped from the candidate set (a pandas dataframe).
# Pick an x (a row in the current pandas dataset) that maximizes the acquisition
idx_best = torch.argmax(acq_vals).item()
new_data = helpers.pop_df(pd_dataset, idx_best)

# Update the current best y
if new_data[OBJ_COL] > best_y:
    best_y = new_data[OBJ_COL]
  1. Finally, we just feed this new data point to the surrogate object. It will append it to the training set and retrain the surrogate for the next iteration.
# Update surrogate using the new data point
model = model.condition_on_observations(new_data)

Metadata

Release files for lapeft-bayesopt 0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for lapeft-bayesopt 0.2
File Size Uploaded Provenance Actions
lapeft_bayesopt-0.2.tar.gz 16.0 kB Without provenance Details for lapeft_bayesopt-0.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for lapeft-bayesopt 0.2
File Interpreter ABI Platform Actions
lapeft_bayesopt-0.2-py3-none-any.whl Python 3 none any Details for lapeft_bayesopt-0.2-py3-none-any.whl

Total release size: 36.5 kB

Release files / lapeft_bayesopt-0.2.tar.gz

Download URL lapeft_bayesopt-0.2.tar.gz
Size 16.0 kB
Tags Source
SHA-256 checksum
How to use checksums
2c46530e8d610cc6f025ba04ab8fe65ebfd3c18ca2416792057b5be7bae2f9c1
BLAKE2b-256 checksum
How to use checksums
06bd54bc183946652edb142587c219922be126ac94524b7dd8023c92950e7463
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.21 {"installer":{"name":"uv","version":"0.11.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"NixOS","version":"26.05","id":"yarara","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / lapeft_bayesopt-0.2-py3-none-any.whl

Download URL lapeft_bayesopt-0.2-py3-none-any.whl
Size 20.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1ec012b13cfa61678395237cc53e0c0ccdf712889e93c04d11d4eed2d6be6dd5
BLAKE2b-256 checksum
How to use checksums
8d6e793174d9d22d01999282fb65a29d28c944b68a0f2d5df301dd6a32fc214e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.21 {"installer":{"name":"uv","version":"0.11.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"NixOS","version":"26.05","id":"yarara","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for lapeft-bayesopt 0.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.2 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page