HFPretrainedMolecularGenerator¶
NovoMolGen, MolGen, Molexar, or SAFE-GPT checkpoints; constraints depend on the model family.
from torch_molecule import HFPretrainedMolecularGenerator
Constructor¶
- class HFPretrainedMolecularGenerator(repo_id, max_length=128, revision=None, trust_remote_code=False, tokenizer_repo_id=None, generate_max_length=64, batch_size=8, epochs=1, learning_rate=5e-05, weight_decay=0.01, grad_norm_clip=1.0, *, device=None, model_name='HFPretrainedMolecularGenerator', verbose='none')[source]¶
Hugging Face pretrained models as molecular generators.
This class loads pretrained generative models from Hugging Face and exposes a sklearn-style
fit/generateinterface consistent with other generators in torch-molecule.Supported generation modes depend on the model family:
NovoMolGen: de novo SMILES generation from BOS.
MolGen-large / MolGen-large-opt: SELFIES seq2seq generation via
prefix_selfies=orscaffold=(SMILES converted internally).Molexar: Fragment-SELFIES de novo and fragment-constrained generation via
start_smiles/start_string/conditions(omni).SAFE-GPT: GPT-2 causal LM on SAFE strings; de novo and
scaffold=prefix.
Supported model repositories include:
NovoMolGen: Causal LM pretrained on ZINC-22 for de novo SMILES generation.
repo_id:
"chandar-lab/NovoMolGen_32M_SMILES_BPE"(https://huggingface.co/chandar-lab/NovoMolGen_32M_SMILES_BPE)MolGen-large: Seq2Seq SELFIES generator with high chemical validity.
repo_id:
"zjunlp/MolGen-large"(https://huggingface.co/zjunlp/MolGen-large)MolGen-large-opt: MolGen-large fine-tuned for QED / p-logP optimization.
repo_id:
"zjunlp/MolGen-large-opt"(https://huggingface.co/zjunlp/MolGen-large-opt)Molexar-10M-base: Fragment-SELFIES de novo and fragment-constrained generation.
repo_id:
"fairydance/molexar-10m-base"(https://huggingface.co/fairydance/molexar-10m-base)Molexar-10M-omni: Multi-condition Molexar model for property-guided generation.
repo_id:
"fairydance/molexar-10m-omni"(https://huggingface.co/fairydance/molexar-10m-omni)SAFE-GPT: GPT-2 causal LM pretrained on 1.1B SAFE strings for de novo generation and scaffold-prefix completion.
repo_id:
"datamol-io/safe-gpt"(https://huggingface.co/datamol-io/safe-gpt)
- Parameters:
repo_id (str) – Hugging Face repository id of the pretrained generator.
max_length (int, default=128) – Maximum sequence length used when loading the tokenizer.
revision (Optional[str], default=None) – Model revision on the Hugging Face Hub. NovoMolGen defaults to
"hf-checkpoint"so standardmodel.generateworks out of the box.trust_remote_code (bool, default=False) – Whether to trust remote code when loading from Hugging Face. Automatically enabled for Molexar.
tokenizer_repo_id (Optional[str], default=None) – Optional Hugging Face repo for the tokenizer.
generate_max_length (int, default=64) – Default generation length. For causal LMs (NovoMolGen, SAFE-GPT) this is
max_new_tokensso ascaffold=prefix does not consume the budget. For MolGen this is still decodermax_length.batch_size (int, default=8) – Batch size used when fine-tuning on SMILES data.
epochs (int, default=1) – Number of fine-tuning epochs when
fit(X)is called.learning_rate (float, default=5e-5) – Learning rate for fine-tuning.
weight_decay (float, default=0.01) – Weight decay for fine-tuning.
grad_norm_clip (Optional[float], default=1.0) – Maximum gradient norm during fine-tuning. Set to
Noneto disable clipping.device (Optional[Union[torch.device, str]], default=None) – Device to run the model on.
model_name (str, default="HFPretrainedMolecularGenerator") – Name identifier for the model instance.
verbose (str, default="none") – Progress display mode:
"none","progress_bar", or"print_statement".
Example¶
import tempfile
import shutil
from torch_molecule import HFPretrainedMolecularGenerator
REPO_ID = "datamol-io/safe-gpt"
N_SAMPLES = 5
SHORT_SCAFFOLD = "c1ccccc1"
LONG_SCAFFOLD = "CC1=CC=C(C=C1)C2=CC(=NN2C3=CC=C(C=C3)S(=O)(=O)N)C(F)(F)F"
def test_safe_gpt_generator():
print("\n=== Testing SAFE-GPT initialization ===")
model = HFPretrainedMolecularGenerator(
repo_id=REPO_ID,
verbose="progress_bar",
)
print("SAFE-GPT initialized successfully")
print("\n=== Testing SAFE-GPT loading from Hugging Face ===")
model.fit()
print("SAFE-GPT loaded successfully")
print("\n=== Testing SAFE-GPT de novo generation ===")
generated_smiles = model.generate(n_samples=N_SAMPLES)
print(f"Generated {len(generated_smiles)} molecules")
print("Example generated SMILES:", generated_smiles[:2])
print("\n=== Testing SAFE-GPT short scaffold generation ===")
generated_smiles = model.generate(n_samples=N_SAMPLES, scaffold=SHORT_SCAFFOLD)
print(f"Generated {len(generated_smiles)} molecules from scaffold {SHORT_SCAFFOLD}")
print("Example generated SMILES:", generated_smiles[:2])
print("\n=== Testing SAFE-GPT long scaffold generation ===")
generated_smiles = model.generate(n_samples=2, scaffold=LONG_SCAFFOLD)
print(f"Generated {len(generated_smiles)} molecules from long scaffold")
print("Example generated SMILES:", generated_smiles[:2])
print("\n=== Testing SAFE-GPT saving and loading ===")
save_path = tempfile.mkdtemp(prefix="torchmol-safe-gpt-")
model.save_to_local(save_path)
print(f"SAFE-GPT saved to {save_path}")
loaded_model = HFPretrainedMolecularGenerator(repo_id=REPO_ID)
loaded_model.load_from_local(save_path)
print("SAFE-GPT loaded from local directory")
generated_smiles = loaded_model.generate(n_samples=2, scaffold=SHORT_SCAFFOLD)
print(f"Generated {len(generated_smiles)} molecules with loaded model")
print("Example generated SMILES:", generated_smiles[:2])
shutil.rmtree(save_path)
print(f"Cleaned up {save_path}")
if __name__ == "__main__":
test_safe_gpt_generator()
Methods¶
Load the pretrained model from Hugging Face. |
|
Generate molecules as SMILES strings. |
|
Save the model and tokenizer to a local directory. |
|
Load a model and tokenizer saved by |
For shared checkpoint and parameter operations, see Base Modules.
fit¶
- HFPretrainedMolecularGenerator.fit(X=None, y=None)[source]¶
Load the pretrained model from Hugging Face.
- Parameters:
X (Optional[List[str]], default=None) – Optional SMILES strings for fine-tuning. When provided, the pretrained weights are adapted on the encoded family-specific representation.
y (Optional[np.ndarray], default=None) – Reserved for future conditional fine-tuning. Currently ignored with a warning.
- Returns:
The fitted generator instance.
- Return type:
generate¶
- HFPretrainedMolecularGenerator.generate(n_samples=10, **kwargs)[source]¶
Generate molecules as SMILES strings.
- Parameters:
n_samples (int, default=10) – Number of molecules to generate.
**kwargs – Additional arguments forwarded to the family-specific generator. For causal LMs, common options include
max_new_tokens,temperature,do_sample, andscaffold.max_lengthis still accepted as a Hugging Face total-length override. For MolGen, useprefix_selfiesorscaffoldplus optionalnum_beams,min_length, andmax_length. For Molexar, usestart_smiles,start_string,generation_task, orconditionsfor omni models. For SAFE-GPT, usescaffold=with a SMILES prefix (converted to SAFE internally). A prefix does not guarantee that the final molecule retains the supplied substructure.
- Returns:
Generated SMILES strings. For MolGen, Molexar, and SAFE-GPT, invalid decodes are dropped, so the list may be shorter than
n_samples.- Return type:
List[str]
save_to_local¶
load_from_local¶
- HFPretrainedMolecularGenerator.load_from_local(path)[source]¶
Load a model and tokenizer saved by
save_to_local().- Parameters:
path (str)
- Return type:
None