Safetensors
English
qwen3
Situo commited on
Commit
9f0fd3d
·
verified ·
1 Parent(s): 314cf0e

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +95 -0
README.md ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ language:
4
+ - en
5
+ base_model:
6
+ - Qwen/Qwen3-8B
7
+ ---
8
+ # RetroDFM-R: Reasoning-Driven Retrosynthesis Prediction with Large Language Models via Reinforcement Learning
9
+
10
+ RetroDFM-R is a reasoning-driven large language model designed for chemical retrosynthesis. Unlike traditional graph-based or sequence models, it incorporates large-scale reinforcement learning with rule-based rewards and generative reward model, enabling stronger generalization, higher prediction reliability, and improved interpretability. Comprehensive evaluations show that RetroDFM-R outperforms existing state-of-the-art approaches across standard benchmarks. Double-blind human assessments further confirm the chemical plausibility and practical usefulness of its predictions. The model also successfully reconstructs multistep routes for real drug molecules and complex materials reported in the literature. Its explicit reasoning process offers clear, human-interpretable insights, enhancing trust and real-world applicability in retrosynthesis planning.
11
+
12
+
13
+ ## Training Details
14
+
15
+ RetroDFM-R is trained through a three-stage pipeline: (1) continual pretraining on retrosynthesis-focused chemical data, (2) supervised fine-tuning on distilled chain-of-thought reasoning samples, and (3) reinforcement learning to further enhance step-by-step reasoning and prediction quality.
16
+
17
+ ## Usage Details
18
+
19
+ ### Local Inference
20
+
21
+ To load and run RetroDFM-R locally, here is an example:
22
+
23
+ ```python
24
+ import re
25
+ import torch
26
+ from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig
27
+
28
+ model_name_or_id = "OpenDFM/RetroDFM-R-8B"
29
+ tokenizer = AutoTokenizer.from_pretrained(model_name_or_id)
30
+ model = AutoModelForCausalLM.from_pretrained(model_name_or_id, torch_dtype=torch.bfloat16, device_map="auto")
31
+
32
+ target_smiles = "<target mol in SMILES format>"
33
+ instruction = f"<SMILES> {target_smiles} </SMILES> Given the product SMILES, your task is to predict the reactants SMILES using your experienced chemical retrosynthesis knowledge. Please reason step by step, and put your final answer within <answer> answer here </answer>."
34
+
35
+ message = [
36
+ {"role": "user", "content": instruction}
37
+ ]
38
+
39
+ input_text = tokenizer.apply_chat_template(message, tokenize=False, add_generation_prompt=True)
40
+ inputs = tokenizer(input_text, return_tensors="pt").to("cuda")
41
+ generation_config = GenerationConfig(
42
+ do_sample=True,
43
+ top_k=20,
44
+ top_p=0.9,
45
+ temperature=0.6,
46
+ max_new_tokens=1024,
47
+ eos_token_id=tokenizer.eos_token_id
48
+ )
49
+ outputs = model.generate(**inputs, generation_config=generation_config)
50
+
51
+ generated_text = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
52
+ input_text = tokenizer.decode(inputs["input_ids"][0], skip_special_tokens=True)
53
+ generated_text = generated_text[len(input_text):].strip()
54
+ print(f"{generated_text=}")
55
+
56
+ thinking, answer = re.match(r'<think>(.*?)</think>\s?<answer>(.*?)</answer>', generated_text, re.DOTALL).groups()
57
+ thinking, answer = thinking.strip(), answer.strip()
58
+ print(f"{thinking=}")
59
+ print(f"{answer=}")
60
+ ```
61
+
62
+ ### SMILES preprocess
63
+
64
+ When there involves SMILES notation in your input, we recommend to preprocess the SMILES with the `rdkit` package to canonicalize the SMILES. Here is an example:
65
+ ```python
66
+ from rdkit import Chem
67
+ def canonicalize_smiles(smiles):
68
+ mol = Chem.MolFromSmiles(smiles)
69
+ if mol is None:
70
+ return None
71
+ return Chem.MolToSmiles(mol, isomericSmiles=True, kekuleSmiles=False)
72
+ ```
73
+ or directly:
74
+ ```python
75
+ from rdkit import Chem
76
+ def canonicalize_smiles(smiles):
77
+ return Chem.CanonSmiles(smiles, useChiral=True)
78
+ ```
79
+
80
+ ## Citation
81
+ ```bibtex
82
+ @misc{zhang2025retrodfmr,
83
+ title={Reasoning-Driven Retrosynthesis Prediction with Large Language Models via Reinforcement Learning},
84
+ author={Zhang, Situo and Li, Hanqi and Chen, Lu and Zhao, Zihan and Lin, Xuanze and Zhu, Zichen and Chen, Bo and Chen, Xin and Yu, Kai},
85
+ year={2025},
86
+ eprint={2507.17448},
87
+ archivePrefix={arXiv},
88
+ primaryClass={cs.CE},
89
+ url={https://arxiv.org/abs/2507.17448},
90
+ }
91
+ ```
92
+
93
+ ## Disclaimer
94
+ Current version of RetroDFM-R may generate incorrect or misleading information. Please use it with caution and verify the results with domain experts before making any decisions based on the results.
95
+