MolWeaver is a natural-language-conditioned molecular graph generation framework. It converts qualitative and quantitative text prompts into discrete 2D molecular graphs by combining a frozen Qwen3.5-0.8B language backbone, explicit molecular constraint parsing and compilation, and a scaffold-aware graph decoder. This repository contains the code for pretraining, downstream fine-tuning, molecular generation and evaluation. The corresponding paper is under review. Preprint version is available at ChemRxiv.
@article{xu_2026_molweaver,
author = {Li-Cheng Xu and Yu-Duo Qian and Fenglei Cao and Yuan Qi },
title = {Controllable molecular graph generation from natural-language chemical constraints},
journal = {ChemRxiv},
volume = {2026},
number = {0813},
pages = {},
year = {2026},
doi = {10.26434/chemrxiv.15007383/v1},
URL = {https://chemrxiv.org/doi/abs/10.26434/chemrxiv.15007383/v1},
eprint = {https://chemrxiv.org/doi/pdf/10.26434/chemrxiv.15007383/v1},
}
- Natural-language molecular design from quantitative constraints and qualitative chemical preferences.
- Explicit molecular constraint parsing and compilation into graph-control signals.
- Scaffold-aware discrete 2D graph decoding with chemistry-aware generation checks.
- Downstream transfer to ring-size control, Lipinski rule-of-five-guided generation and phosphine ligand generation.
- RDKit-based evaluation for validity, uniqueness, novelty, diversity and prompt adherence.
MolWeaver/
|-- dataset/
| |-- corpus/ # processed molecule-prompt corpora
| `-- data_source/ # ZINC22 download links and raw Kraken data
|-- molweaver/
| |-- configs/ # training and fine-tuning configs
| |-- scripts/ # data caching, training, inference and evaluation scripts
| `-- src/molweaver/ # model, chemistry, data and evaluation modules
|-- benchmark/ # released benchmark prediction and metric files
|-- assets/ # logo and Web UI screenshots
|-- DATA_PREPARATION_FROM_SOURCE.md
`-- README.md
Run all commands from the repository root.
git clone https://github.com/licheng-xu-echo/MolWeaver.git cd MolWeaver pip install .
Download the Qwen3.5-0.8B language backbone:
python molweaver/scripts/download_backbone.py \ --model-id Qwen/Qwen3.5-0.8B \ --local-dir molweaver/models/Qwen3.5-0.8B
The processed molecule-prompt corpora and model checkpoints are distributed through ModelScope at XuLiCheng2025/MolWeaver. Download them into the repository root so that the files are placed under dataset/corpus/ and molweaver/ckpts/.
pip install modelscope-hub ms-hub download XuLiCheng2025/MolWeaver \ --repo-type model \ --include 'dataset/corpus/**' 'molweaver/ckpts/**' \ --local-dir '.' \ --max-workers 8
The repository also keeps the raw data sources under dataset/data_source/, including ZINC22 curl manifests and the Kraken phosphine descriptor file. To regenerate the processed corpora from these sources, see DATA_PREPARATION_FROM_SOURCE.md.
After downloading dataset/corpus/ and molweaver/ckpts/ from ModelScope, you can start a lightweight local demo UI from the repository root:
python webui/server.py --host 127.0.0.1 --port 8000
Open http://127.0.0.1:8000 in your browser. The UI reads model presets, checkpoint paths and sample prompts from webui/models.json; edit that file if you want to add new model tags or change the default prompt examples.
MolWeaver training uses tensorized cache files generated from the processed JSONL corpora.
PRETRAIN_CONFIG="molweaver/configs/pretrain_qwen3p5_0p8b_4090_h04_h20_221m_bs168_validityguard_heterocycle_categorical_v2.yaml" RING_CONFIG="molweaver/configs/finetune_qwen3p5_0p8b_4090_ring_size_2k.yaml" LIPINSKI_CONFIG="molweaver/configs/finetune_qwen3p5_0p8b_4090_lipinski_rule_of_five_5k.yaml" PHOSPHINE_CONFIG="molweaver/configs/finetune_qwen3p5_0p8b_4090_kraken_expanded_phosphine_localbulk_size.yaml"
Cache the pretraining corpus:
python molweaver/scripts/cache_tensor_dataset.py \ --config "${PRETRAIN_CONFIG}" \ --input dataset/corpus/pretrain/train \ --output-dir dataset/cache/pretrain/train python molweaver/scripts/cache_tensor_dataset.py \ --config "${PRETRAIN_CONFIG}" \ --input dataset/corpus/pretrain/valid \ --output-dir dataset/cache/pretrain/valid
Cache the ring-size fine-tuning corpus:
python molweaver/scripts/cache_tensor_dataset.py \ --config "${RING_CONFIG}" \ --input dataset/corpus/finetune_ring_size/train \ --output-dir dataset/cache/finetune_ring_size/train python molweaver/scripts/cache_tensor_dataset.py \ --config "${RING_CONFIG}" \ --input dataset/corpus/finetune_ring_size/valid \ --output-dir dataset/cache/finetune_ring_size/valid
Cache the Lipinski rule-of-five fine-tuning corpus:
python molweaver/scripts/cache_tensor_dataset.py \ --config "${LIPINSKI_CONFIG}" \ --input dataset/corpus/finetune_lipinski/train \ --output-dir dataset/cache/finetune_lipinski/train python molweaver/scripts/cache_tensor_dataset.py \ --config "${LIPINSKI_CONFIG}" \ --input dataset/corpus/finetune_lipinski/valid \ --output-dir dataset/cache/finetune_lipinski/valid
Cache the phosphine ligand fine-tuning corpus:
python molweaver/scripts/cache_tensor_dataset.py \ --config "${PHOSPHINE_CONFIG}" \ --input dataset/corpus/finetune_phospine/train \ --output-dir dataset/cache/finetune_phospine/train \ --tokenize-batch-size 2048 python molweaver/scripts/cache_tensor_dataset.py \ --config "${PHOSPHINE_CONFIG}" \ --input dataset/corpus/finetune_phospine/valid \ --output-dir dataset/cache/finetune_phospine/valid \ --tokenize-batch-size 2048
The following commands train the full pretraining model, three downstream fine-tuning models and three pretraining ablation models. Fine-tuning starts from the released full pretraining checkpoint under molweaver/ckpts/pretrain_full/.
Set shared paths:
PRETRAIN_CONFIG="molweaver/configs/pretrain_qwen3p5_0p8b_4090_h04_h20_221m_bs168_validityguard_heterocycle_categorical_v2.yaml" RING_CONFIG="molweaver/configs/finetune_qwen3p5_0p8b_4090_ring_size_2k.yaml" LIPINSKI_CONFIG="molweaver/configs/finetune_qwen3p5_0p8b_4090_lipinski_rule_of_five_5k.yaml" PHOSPHINE_CONFIG="molweaver/configs/finetune_qwen3p5_0p8b_4090_kraken_expanded_phosphine_localbulk_size.yaml" PRETRAIN_CKPT="molweaver/ckpts/pretrain_full/best_generation.pt"
PRETRAIN_OUT="molweaver/outputs/pretrain_full" OMP_NUM_THREADS=1 NCCL_SHM_DISABLE=1 CUDA_VISIBLE_DEVICES=0,1,2,3 \ torchrun --nproc_per_node=4 molweaver/scripts/train_pretrain_ddp.py \ --config "${PRETRAIN_CONFIG}" \ --dataset dataset/cache/pretrain/train \ --valid-dataset dataset/cache/pretrain/valid \ --eval-dataset dataset/corpus/pretrain/valid \ --output-dir "${PRETRAIN_OUT}" \ --ddp-timeout-minutes 180 \ --eval-every-epochs 1 \ --eval-max-samples 1000 \ --eval-decode-num-samples 32 \ --eval-decode-temperature 0.9 \ --eval-decode-top-k 12 \ --eval-decode-node-thresholds 0.42 0.50 0.58 \ --eval-property-guidance-scales 0.8 1.6
Ring-size control:
RING_FINETUNE_OUT="molweaver/outputs/finetune_ring_size" CUDA_VISIBLE_DEVICES=0 python molweaver/scripts/train_finetune.py \ --config "${RING_CONFIG}" \ --dataset dataset/cache/finetune_ring_size/train \ --valid-dataset dataset/cache/finetune_ring_size/valid \ --checkpoint "${PRETRAIN_CKPT}" \ --output-dir "${RING_FINETUNE_OUT}" \ --eval-dataset dataset/corpus/finetune_ring_size/valid \ --eval-every-epochs 1 \ --eval-max-samples 1134 \ --eval-decode-num-samples 32 \ --eval-decode-top-k 12 \ --eval-decode-node-thresholds 0.42 0.50 0.58 \ --eval-property-guidance-scales 0.8 1.6
Lipinski rule-of-five generation:
LIPINSKI_FINETUNE_OUT="molweaver/outputs/finetune_lipinski_rule_of_five" CUDA_VISIBLE_DEVICES=0 python molweaver/scripts/train_finetune.py \ --config "${LIPINSKI_CONFIG}" \ --dataset dataset/cache/finetune_lipinski/train \ --valid-dataset dataset/cache/finetune_lipinski/valid \ --checkpoint "${PRETRAIN_CKPT}" \ --output-dir "${LIPINSKI_FINETUNE_OUT}" \ --eval-dataset dataset/corpus/finetune_lipinski/valid \ --eval-every-epochs 1 \ --eval-max-samples 816 \ --eval-decode-num-samples 8 \ --eval-decode-top-k 8 \ --eval-decode-node-thresholds 0.50 \ --eval-property-guidance-scales 0.8 1.6 \ --generation-score-prompt-weight 1.0 \ --generation-score-rdkit-weight 0.0
Phosphine ligand generation:
PHOSPHINE_FINETUNE_OUT="molweaver/outputs/finetune_kraken_phosphine_localbulk_size_steric" CUDA_VISIBLE_DEVICES=0 python molweaver/scripts/train_finetune.py \ --config "${PHOSPHINE_CONFIG}" \ --dataset dataset/cache/finetune_phospine/train \ --valid-dataset dataset/cache/finetune_phospine/valid \ --checkpoint "${PRETRAIN_CKPT}" \ --output-dir "${PHOSPHINE_FINETUNE_OUT}" \ --eval-dataset dataset/corpus/finetune_phospine/valid \ --eval-every-epochs 1 \ --eval-max-samples 1482 \ --eval-decode-num-samples 8 \ --eval-decode-temperature 0.8 \ --eval-decode-top-k 8 \ --eval-decode-node-thresholds 0.58 0.62 0.66 0.70 0.74 \ --eval-property-guidance-scales 0.8 1.6 \ --eval-enable-kraken-fast-profile \ --generation-score-prompt-weight 1.0 \ --generation-score-rdkit-weight 0.0
No text-graph alignment:
ABLATION_OUT_ROOT="molweaver/outputs/pretrain_ablations" OMP_NUM_THREADS=1 NCCL_SHM_DISABLE=1 CUDA_VISIBLE_DEVICES=0,1,2,3 \ torchrun --nproc_per_node=4 molweaver/scripts/train_pretrain_ddp.py \ --config "${PRETRAIN_CONFIG}" \ --dataset dataset/cache/pretrain/train \ --valid-dataset dataset/cache/pretrain/valid \ --eval-dataset dataset/corpus/pretrain/valid \ --output-dir "${ABLATION_OUT_ROOT}/no_text_graph_alignment" \ --disable-text-graph-alignment true \ --ddp-timeout-minutes 180 \ --eval-every-epochs 1 \ --eval-max-samples 1000 \ --eval-decode-num-samples 32 \ --eval-decode-temperature 0.9 \ --eval-decode-top-k 12 \ --eval-decode-node-thresholds 0.42 0.50 0.58 \ --eval-property-guidance-scales 0.8 1.6
No constraint parser:
ABLATION_OUT_ROOT="molweaver/outputs/pretrain_ablations" OMP_NUM_THREADS=1 NCCL_SHM_DISABLE=1 CUDA_VISIBLE_DEVICES=0,1,2,3 \ torchrun --nproc_per_node=4 molweaver/scripts/train_pretrain_ddp.py \ --config "${PRETRAIN_CONFIG}" \ --dataset dataset/cache/pretrain/train \ --valid-dataset dataset/cache/pretrain/valid \ --eval-dataset dataset/corpus/pretrain/valid \ --output-dir "${ABLATION_OUT_ROOT}/no_constraint_parser" \ --disable-constraint-parser true \ --property-value-loss-weight 0.0 \ --property-mention-loss-weight 0.0 \ --constraint-compiler-loss-weight 0.0 \ --ddp-timeout-minutes 180 \ --eval-every-epochs 1 \ --eval-max-samples 1000 \ --eval-decode-num-samples 32 \ --eval-decode-temperature 0.9 \ --eval-decode-top-k 12 \ --eval-decode-node-thresholds 0.42 0.50 0.58 \ --eval-property-guidance-scales 0.8 1.6
No constraint compiler:
ABLATION_OUT_ROOT="molweaver/outputs/pretrain_ablations" OMP_NUM_THREADS=1 NCCL_SHM_DISABLE=1 CUDA_VISIBLE_DEVICES=0,1,2,3 \ torchrun --nproc_per_node=4 molweaver/scripts/train_pretrain_ddp.py \ --config "${PRETRAIN_CONFIG}" \ --dataset dataset/cache/pretrain/train \ --valid-dataset dataset/cache/pretrain/valid \ --eval-dataset dataset/corpus/pretrain/valid \ --output-dir "${ABLATION_OUT_ROOT}/no_constraint_compiler" \ --disable-constraint-compiler true \ --constraint-compiler-loss-weight 0.0 \ --ddp-timeout-minutes 180 \ --eval-every-epochs 1 \ --eval-max-samples 1000 \ --eval-decode-num-samples 32 \ --eval-decode-temperature 0.9 \ --eval-decode-top-k 12 \ --eval-decode-node-thresholds 0.42 0.50 0.58 \ --eval-property-guidance-scales 0.8 1.6
All evaluation commands below use the pretrained checkpoints under molweaver/ckpts/.
PRETRAIN_EVAL_OUT="molweaver/outputs/eval/pretrain_full" CUDA_VISIBLE_DEVICES=0,1,2,3 torchrun --nproc_per_node=4 \ molweaver/scripts/evaluate_generation.py \ --multi-gpu \ --config molweaver/ckpts/pretrain_full/resolved_config.yaml \ --checkpoint molweaver/ckpts/pretrain_full/best_generation.pt \ --dataset dataset/corpus/pretrain/test \ --output-dir "${PRETRAIN_EVAL_OUT}" \ --max-samples 1000 \ --batch-size 256 \ --decode-num-samples 32 \ --decode-temperature 0.9 \ --decode-top-k 12 \ --decode-node-thresholds 0.42 0.50 0.58 \ --property-guidance-scales 0.8 1.6
Ring-size control:
CUDA_VISIBLE_DEVICES=0,1,2,3 torchrun --nproc_per_node=4 \ molweaver/scripts/evaluate_generation.py \ --multi-gpu \ --config molweaver/ckpts/finetune_ring_size/resolved_config.yaml \ --checkpoint molweaver/ckpts/finetune_ring_size/best_generation.pt \ --dataset dataset/corpus/finetune_ring_size/test \ --output-dir molweaver/outputs/eval/finetune_ring_size \ --batch-size 256 \ --decode-num-samples 32 \ --decode-temperature 0.9 \ --decode-top-k 12 \ --decode-node-thresholds 0.42 0.50 0.58 \ --property-guidance-scales 0.8 1.6
Lipinski rule-of-five generation:
CUDA_VISIBLE_DEVICES=0,1,2,3 torchrun --nproc_per_node=4 \ molweaver/scripts/evaluate_generation.py \ --multi-gpu \ --config molweaver/ckpts/finetune_lipinski/resolved_config.yaml \ --checkpoint molweaver/ckpts/finetune_lipinski/best_generation.pt \ --dataset dataset/corpus/finetune_lipinski/test \ --output-dir molweaver/outputs/eval/finetune_lipinski \ --batch-size 128 \ --decode-num-samples 32 \ --decode-temperature 0.9 \ --decode-top-k 12 \ --decode-node-thresholds 0.42 0.50 0.58 \ --property-guidance-scales 0.8 1.6
Phosphine ligand generation:
CUDA_VISIBLE_DEVICES=0,1,2,3 torchrun --nproc_per_node=4 \ molweaver/scripts/evaluate_generation.py \ --multi-gpu \ --config molweaver/ckpts/finetune_phosphine/resolved_config.yaml \ --checkpoint molweaver/ckpts/finetune_phosphine/best_generation.pt \ --dataset dataset/corpus/finetune_phospine/test \ --output-dir molweaver/outputs/eval/finetune_phosphine \ --batch-size 256 \ --decode-num-samples 8 \ --decode-temperature 0.8 \ --decode-top-k 8 \ --decode-node-thresholds 0.58 0.62 0.66 0.70 0.74 \ --property-guidance-scales 0.8 1.6 \ --enable-kraken-fast-profile
No text-graph alignment:
CUDA_VISIBLE_DEVICES=0,1,2,3 torchrun --nproc_per_node=4 \ molweaver/scripts/evaluate_generation.py \ --multi-gpu \ --config molweaver/ckpts/pretrain_no_text_graph_alignment/resolved_config.yaml \ --checkpoint molweaver/ckpts/pretrain_no_text_graph_alignment/best_generation.pt \ --dataset dataset/corpus/pretrain/test \ --output-dir molweaver/outputs/eval/ablation_no_text_graph_alignment \ --max-samples 1000 \ --batch-size 256 \ --decode-num-samples 32 \ --decode-temperature 0.9 \ --decode-top-k 12 \ --decode-node-thresholds 0.42 0.50 0.58 \ --property-guidance-scales 0.8 1.6
No constraint parser:
CUDA_VISIBLE_DEVICES=0,1,2,3 torchrun --nproc_per_node=4 \ molweaver/scripts/evaluate_generation.py \ --multi-gpu \ --config molweaver/ckpts/pretrain_no_constraint_parser/resolved_config.yaml \ --checkpoint molweaver/ckpts/pretrain_no_constraint_parser/best_generation.pt \ --dataset dataset/corpus/pretrain/test \ --output-dir molweaver/outputs/eval/ablation_no_constraint_parser \ --max-samples 1000 \ --batch-size 256 \ --decode-num-samples 32 \ --decode-temperature 0.9 \ --decode-top-k 12 \ --decode-node-thresholds 0.42 0.50 0.58 \ --property-guidance-scales 0.8 1.6
No constraint compiler:
CUDA_VISIBLE_DEVICES=0,1,2,3 torchrun --nproc_per_node=4 \ molweaver/scripts/evaluate_generation.py \ --multi-gpu \ --config molweaver/ckpts/pretrain_no_constraint_compiler/resolved_config.yaml \ --checkpoint molweaver/ckpts/pretrain_no_constraint_compiler/best_generation.pt \ --dataset dataset/corpus/pretrain/test \ --output-dir molweaver/outputs/eval/ablation_no_constraint_compiler \ --max-samples 1000 \ --batch-size 256 \ --decode-num-samples 32 \ --decode-temperature 0.9 \ --decode-top-k 12 \ --decode-node-thresholds 0.42 0.50 0.58 \ --property-guidance-scales 0.8 1.6
Released benchmark outputs and metric files are available under benchmark/, including:
benchmark/pretrain_molweaver/benchmark/pretrain_pure_llm/benchmark/pretrain_ablation/benchmark/finetune/
- Processed corpora should be placed under
dataset/corpus/; tensor caches are written underdataset/cache/. - Source-data reconstruction commands are kept separate in DATA_PREPARATION_FROM_SOURCE.md.
- Multi-GPU commands use
torchrun; adjustCUDA_VISIBLE_DEVICES,--nproc_per_nodeand batch size according to local hardware.
