Cost-aware Finetuning for Retrosynthesis

Cutting both the data cost and the compute cost of predicting reactants from products

Final project for DS-GA-1011, Natural Language Processing with Representation Learning, at NYU, with Zhaodong Liu and Haoqi (Kevin) Zhang. The full report is Cost-aware Finetuning on Pretrained LM to Efficiently Perform Retrosynthesis of Chemical Reactions.

Retrosynthesis as a sequence-to-sequence problem

Retrosynthesis runs a reaction backwards: given a product molecule, predict the reactants that would produce it. What makes this an NLP problem is SMILES (Simplified Molecular Input Line Entry System), a notation that writes a molecule as one line of text, encoding atoms, bonds, branches and ring closures as characters. Under that representation retrosynthesis is string-to-string translation, and an encoder-decoder language model applies directly — the framing established by the Molecular Transformer (Schwaller et al., ACS Central Science, 2019). We built on Chemformer (Irwin et al., 2022), which supplies a BART-style model pretrained on SMILES reconstruction; that checkpoint is what we finetuned.

Two costs

Chemical reaction data is scarce because producing it means running experiments, which are slow and expensive. Nearly every public dataset is a variant of USPTO — reactions mined from US patents, 1976 to September 2016 (Lowe, 2017) — and USPTO-50K is a preprocessed 50,000-reaction subset of it. Separately, the large chemistry models that perform best on this task, such as LlaSMol (Yu et al., 2024), are expensive to train and to serve. We went after both costs: cheaper data, and cheaper finetuning.

Building USPTO-50K_γ

Instead of running more experiments, we generated labels. Starting from the real USPTO-50K data, we used LlaSMol to predict outputs for reactions that would be slow or costly to obtain in a lab, and blended the generated examples back in. We call the result USPTO-50K_γ, where

γ = (examples generated by the LLM) / (total examples)

so γ = 0 is untouched USPTO-50K and γ = 1 replaces every label with a model prediction. The dataset is published at scaliaven/uspto-50k.

What the γ sweep shows

We score models on two metrics. Token accuracy is the fraction of correct output tokens. Molecular accuracy is the fraction of predictions where the whole molecule is right, reported at top-1, top-3 and top-5. Molecular accuracy is the metric that matters: a prediction can get almost every token right and still describe a molecule that is chemically wrong.

γ Mol. top-1 Mol. top-3 Mol. top-5 Token acc.
1.0 0.7418 0.8234 0.8449 0.9755
0.8 0.6973 0.7922 0.8115 0.9740
0.5 0.6553 0.7512 0.7707 0.9768
0.2 0.5930 0.7057 0.7455 0.9773
0.0 0.5879 0.6928 0.7359 0.9788

Molecular accuracy climbs steadily with γ — top-1 goes from 0.5879 on the untouched data to 0.7418 when every label is generated — while token accuracy drifts slightly the other way. Our reading is that the LLM’s outputs, coming from a model trained on far more chemistry, are more self-consistent than raw patent labels and therefore easier for a small model to fit. The practical payoff is that this data costs compute hours instead of lab time.

Which parameters are worth updating

The model is a 6-layer Transformer encoder-decoder, embedding size 512, feedforward dimension 2048. Against a full-parameter finetuning baseline we tried freezing the encoder or the decoder, inserting MLPs between encoder and decoder (mid), after the decoder (end), or both (all), and LoRA on the output projection layers of the multi-head attention blocks. Every run used the original USPTO-50K, 100 epochs, batch size 128, and took roughly four hours on a single RTX 8000 on NYU HPC.

Treatment Token acc. Top-1 Top-3 Top-5
Full parameter (baseline) 0.9783 0.5634 0.6840 0.7220
Fix encoder 0.9571 0.2556 0.3731 0.4077
Fix decoder 0.9699 0.5113 0.6209 0.6875
Fix decoder + MLP end 0.9731 0.5355 0.6582 0.6852
Fixed encoder + LoRA decoder 0.9752 0.5534 0.6584 0.6937

Without LoRA, freezing the decoder (0.5113) is far better than freezing the encoder (0.2556), and adding MLPs after the decoder recovers most of the remaining gap (0.5355). Adding LoRA inverts that ordering: the best configuration we found is a frozen encoder with LoRA on the decoder, at 0.5534 top-1 and 0.9752 token accuracy — within a point of full finetuning while updating a small fraction of the weights. Once LoRA gives the decoder a cheap way to adapt, holding the encoder fixed becomes the better trade.

Caveats and future work

The γ sweep ran at batch size 64 rather than the 128 used everywhere else; we hit out-of-memory at the intended setting. Batch size matters enough here that the γ numbers are not directly comparable to the finetuning-treatment numbers — which is why γ = 0 (0.5879) reads higher than the full-parameter baseline (0.5634) on what is otherwise the same data.

Two directions we did not get to: distilling from LlaSMol into the BART model rather than only training on its sampled outputs, so the loss accounts for the larger model’s behaviour; and better decoding, since token accuracy sits near 0.97 across every configuration while molecular accuracy is much lower.

Artifacts

References

2024

  1. LlaSMol: Advancing Large Language Models for Chemistry with a Large-Scale, Comprehensive, High-Quality Instruction Tuning Dataset
    Botao Yu, Frazier N. Baker, Ziqi Chen, and 2 more authors
    2024

2022

  1. Chemformer: a pre-trained transformer for computational chemistry
    Ross Irwin, Spyridon Dimitriadis, Jiazhen He, and 1 more author
    Machine Learning: Science and Technology, Jan 2022

2017

  1. Chemical reactions from US patents (1976-Sep2016)
    Daniel Lowe
    Jan 2017
    URL https://doi.org/10.6084/m9.figshare.5104873.v1