Skip to content

Add: A Hugging Face pretrained molecular generator+ train_test_split implementation - #22

Open
m21hm9 wants to merge 4 commits into
liugangcode:mainfrom
m21hm9:feat/generation-model
Open

m21hm9 wants to merge 4 commits into
liugangcode:mainfrom
m21hm9:feat/generation-model

Conversation

@m21hm9

@m21hm9 m21hm9 commented Sep 9, 2026

Copy link
Copy Markdown

This PR adds HFPretrainedMolecularGenerator: a sklearn-style fit / generate wrapper for Hugging Face pretrained molecular generators, in the same spirit as HFPretrainedMolecularEncoder. The existing generators (LSTM, MolGPT, DiGress, and the rest) are left as they are.

"API": We know some of these Hub models don’t actually speak SMILES internally, MolGen works in SELFIES, Molexar works in Fragment-SELFIES. So, we convert SMILES → SELFIES (or Fragment-SELFIES) on the way in, and convert back to SMILES on the way out.

"Supported models": NovoMolGen and GP-MoLFormer (causal LM / SMILES), MolGen-large and MolGen-large-opt (seq2seq SELFIES), Molexar-10M-base and Molexar-10M-omni (Fragment-SELFIES via the official engine). Extra deps stay in README Additional Packages, not in pyproject.toml. GP-MoLFormer needs transformers<=4.56.2; Molexar needs transformers>=5.8, in which they cannot share an environment.

"Handled": Invalid training SMILES raise with an index (no silent skip). Failed MolGen/Molexar decodes are dropped instead of padded with "", with a got k/n valid SMILES warning. MolGen fine-tuning is denoising, not identity copy. GP-MoLFormer generation is patched for transformers 4.56 cache issues, scaffold= must be an open prefix like "c1cccc", not a closed ring.

… SAFE-GPT due to the transformers incompatibility.
@m21hm9

m21hm9 commented Sep 11, 2026

Copy link
Copy Markdown
Author

We added more README / install details: Hub links for the supported generators, extra-package notes and importantly we also changed from GP-MoLFormer to SAFE-GPT.

Two problems exist in GP-MoLFormer:
-IBM’s remote code does not play well with current transformers 5.x. It needed transformers<=4.56.2, while Molexar needs transformers>=5.8, so they could not live in the same environment.
-Even after the cache workaround, sampling quality was not good enough (especially scaffold completion)

So we replaced that scaffold / de novo causal-LM slot with SAFE-GPT(https://huggingface.co/datamol-io/safe-gpt). It is a normal GPT-2 model, so it can use standard Hugging Face generate() on transformers 5.x and sit next to NovoMolGen / MolGen / Molexar. scaffold= can be a full SMILES like "c1ccccc1"; we convert it to SAFE internally. Extra dep is just pip install safe-mol.

@m21hm9 m21hm9 changed the title Add: A Hugging Face pretrained molecular generator with NovoMolGen, GP-MoLFormer, MolGen, and Molexar support Add: A Hugging Face pretrained molecular generator+ train_test_split implementation Sep 15, 2026
@m21hm9

m21hm9 commented Sep 18, 2026

Copy link
Copy Markdown
Author

We added train test split on the dataset. Total 4 methods:

-random: baseline. Use as a control; scores are often a bit high.
-scaffold: Bemis–Murcko groups. The same scaffold does not appear in both splits (unseen-framework generalization). Optional use_csk=True for generic cyclic skeletons.
-butina: Taylor–Butina clusters on Morgan fingerprints (radius 2, 2048 bits). Neighbors are pairs with Tanimoto similarity ≥ 0.65 (not DeepChem’s distance cutoff). We use a sparse neighbor graph + exclusion spheres so we do not build the full distance matrix. Whole clusters go to train or holdout. Invalid SMILES raise, same as scaffold.
-size: split by heavy-atom count (small → large by default). This is only the split, not SizeShiftReg coarsening / CMD. mode="sizeshiftreg" is the paper protocol (smallest 50% train, largest 10% holdout).

Thanks!

@m21hm9

m21hm9 commented Sep 18, 2026

Copy link
Copy Markdown
Author

We fixed causal LM scaffold generation and cleaned up the pretrained generator tests:

-max_new_tokens: NovoMolGen / SAFE-GPT used Hugging Face max_length as a total sequence cap (prefix + new tokens). A long SAFE/SMILES scaffold ate that budget (53-token prefix → only 11 new tokens). They now default to max_new_tokens=generate_max_length (64 new tokens, prefix does not count). max_length= is still an explicit total-length override. MolGen decoder max_length and Molexar max_new_tokens are unchanged.
-SAFE-GPT save/load: tokenizer.save_pretrained cannot serialize the custom pre-tokenizer. Local save writes tokenizer.json via SAFETokenizer.save_pretrained.
-tests: replaced pytest files with script-style pretrained_{model}.py (novomolgen, molgen, molexar, safe_gpt), same pattern as lstm.py. SAFE-GPT is tested with a short scaffold (c1ccccc1) and a long one under the default 64 new-token budget, not by raising the cap to 128.

Please let me know if there is any issues!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant