File size: 50,131 Bytes
13362e2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 |
# Copyright 2024 the Llamole Team
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import torch
import torch.nn as nn
import torch.nn.functional as F
from transformers import PreTrainedModel, PreTrainedTokenizerBase
from transformers.utils import ModelOutput
from transformers.generation.utils import LogitsProcessorList, GenerationConfig
from huggingface_hub import snapshot_download
from typing import Any, Callable, Dict, List, Optional, Set, Tuple, Union
import os
import json
import time
from dataclasses import dataclass
from typing import Union, Tuple, Optional
from .loader import load_language_model, load_tokenizer
from .loader import load_graph_decoder, load_graph_predictor, load_graph_encoder
from ..extras.constants import NO_LABEL_INDEX, IGNORE_INDEX, BOND_INDEX
from .planner import molstar
from rdkit import Chem
from torch_geometric.data import Data
from torch_geometric.data import Batch as PyGBatch
from torch_geometric.utils import remove_isolated_nodes
# Save configuration
def convert_to_dict(obj):
if isinstance(obj, (int, float, str, bool, type(None))):
return obj
elif isinstance(obj, (list, tuple)):
return [convert_to_dict(item) for item in obj]
elif isinstance(obj, dict):
return {k: convert_to_dict(v) for k, v in obj.items()}
elif hasattr(obj, "__dict__"):
return {
k: convert_to_dict(v)
for k, v in obj.__dict__.items()
if not k.startswith("_")
}
else:
return str(obj) # Convert any other objects to string
@dataclass
class GraphLMOutput(ModelOutput):
loss: Optional[torch.FloatTensor] = None
logits: torch.FloatTensor = None
last_hidden_state: Optional[torch.FloatTensor] = None
past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None
hidden_states: Optional[Tuple[torch.FloatTensor]] = None
attentions: Optional[Tuple[torch.FloatTensor]] = None
additional_log_info: Optional[Dict[str, float]] = None
class GraphLLMForCausalMLM(PreTrainedModel):
def __init__(
self,
model_args,
finetuning_args,
data_args,
language_model,
graph_decoder,
graph_predictor,
graph_encoder,
token_id_dict,
tokenizer,
):
super().__init__(language_model.config)
self.language_model = language_model
self.graph_decoder = graph_decoder
self.graph_predictor = graph_predictor
self.graph_encoder = graph_encoder
self.token_id_dict = token_id_dict
self.num_body_tokens = data_args.learned_query_size
self.loss_weight_lm = finetuning_args.loss_weight_lm
self.loss_weight_design = finetuning_args.loss_weight_design
self.loss_weight_retro = finetuning_args.loss_weight_retro
self.model_args = model_args
self.finetuning_args = finetuning_args
self.data_args = data_args
self.tokenizer = tokenizer
# Initialize weights and apply final processing
self.post_init()
@classmethod
def from_pretrained(
cls,
tokenizer: PreTrainedTokenizerBase,
model_args,
data_args,
training_args,
finetuning_args,
load_adapter=False,
add_valuehead=False,
):
if load_adapter:
if model_args.adapter_name_or_path is None:
raise ValueError("Please specify the adapter_name_or_path when load_adapter is True.")
if len(model_args.adapter_name_or_path) != 1:
raise ValueError("Only one adapter is supported at a time.")
adapter_path = model_args.adapter_name_or_path[0]
if not os.path.exists(os.path.join(adapter_path, "adapter_config.json")):
# Download from HuggingFace
adapter_name = os.path.basename(adapter_path)
valid_adapters = [
"Llama-3.1-8B-Instruct-Adapter",
"Qwen2-7B-Instruct-Adapter",
"Mistral-7B-Instruct-v0.3-Adapter"
]
if adapter_name not in valid_adapters:
raise ValueError(f"Invalid adapter name. Supported adapters are: {', '.join(valid_adapters)}")
repo_id = f"liuganghuggingface/Llamole-{adapter_name}"
print(f"Downloading adapter {adapter_name} from HuggingFace repo: {repo_id}")
try:
# Download all files including subfolders to the adapter_path
snapshot_download(
repo_id=repo_id,
local_dir=adapter_path,
local_dir_use_symlinks=False,
ignore_patterns=["*.md", "*.txt"] # Optionally ignore certain file types
)
print(f"Successfully downloaded all adapter files to {adapter_path}")
except Exception as e:
raise RuntimeError(f"Failed to download adapter files: {str(e)}")
language_model = load_language_model(
tokenizer,
model_args,
finetuning_args,
training_args.do_train,
add_valuehead,
)
device = next(language_model.parameters()).device
graph_decoder = load_graph_decoder(
model_args,
path=model_args.graph_decoder_path,
device=device,
)
graph_predictor = load_graph_predictor(
model_args,
path=model_args.graph_predictor_path,
device=device,
)
graph_encoder = load_graph_encoder(
model_args,
path=model_args.graph_encoder_path,
device=device,
)
if (
getattr(language_model, "is_quantized", False)
and not training_args.do_train
):
setattr(
language_model, "_hf_peft_config_loaded", True
) # hack here: make model compatible with prediction
token_id_dict = {}
for elem in model_args.new_special_tokens:
if isinstance(elem, str) and len(elem) != 0:
elem_token_ids = tokenizer.encode(elem, add_special_tokens=False)
token_id_dict[elem] = elem_token_ids[0]
model = cls(
model_args=model_args,
finetuning_args=finetuning_args,
data_args=data_args,
language_model=language_model,
graph_decoder=graph_decoder,
graph_predictor=graph_predictor,
graph_encoder=graph_encoder,
token_id_dict=token_id_dict,
tokenizer=tokenizer,
)
graph_to_lm_connector = nn.Sequential(
nn.Linear(graph_encoder.hidden_size, language_model.config.hidden_size),
nn.SiLU(),
)
# Language Model to Graph Decoder connector
lm_to_graph_decoder = nn.Sequential(
nn.Linear(language_model.config.hidden_size, graph_decoder.text_input_size),
nn.SiLU(),
)
# Language Model to Graph Predictor connector
lm_to_graph_predictor = nn.Sequential(
nn.Linear(
language_model.config.hidden_size, graph_predictor.text_input_size
),
nn.SiLU(),
)
for param in graph_to_lm_connector.parameters():
if (
param.dtype == torch.float32
and model_args.compute_dtype != torch.float32
):
param.data = param.data.to(model_args.compute_dtype)
for param in lm_to_graph_decoder.parameters():
if (
param.dtype == torch.float32
and model_args.compute_dtype != torch.float32
):
param.data = param.data.to(model_args.compute_dtype)
for param in lm_to_graph_predictor.parameters():
if (
param.dtype == torch.float32
and model_args.compute_dtype != torch.float32
):
param.data = param.data.to(model_args.compute_dtype)
# Check if connector path is provided and load if available
if load_adapter:
if (
hasattr(model_args, "graph_lm_connector_path")
and model_args.graph_lm_connector_path
):
connector_path = model_args.graph_lm_connector_path
graph_to_lm_connector.load_state_dict(
torch.load(
os.path.join(connector_path, "graph_to_lm_connector.pt"),
map_location=device,
weights_only=True,
)
)
lm_to_graph_decoder.load_state_dict(
torch.load(
os.path.join(connector_path, "lm_to_graph_decoder.pt"),
map_location=device,
weights_only=True,
)
)
lm_to_graph_predictor.load_state_dict(
torch.load(
os.path.join(connector_path, "lm_to_graph_predictor.pt"),
map_location=device,
weights_only=True,
)
)
else:
raise ValueError(f"Connector should be automatically downloaded with the adapter. Please manually download to the path {connector_path}")
model.graph_to_lm_connector = graph_to_lm_connector
model.lm_to_graph_decoder = lm_to_graph_decoder
model.lm_to_graph_predictor = lm_to_graph_predictor
model.graph_to_lm_connector.to(device)
model.lm_to_graph_decoder.to(device)
model.lm_to_graph_predictor.to(device)
return model
def to(self, device):
super().to(device)
self.language_model.to(device)
self.graph_decoder.to(device)
self.graph_predictor.to(device)
self.graph_encoder.to(device)
self.graph_to_lm_connector.to(device)
self.lm_to_graph_decoder.to(device)
self.lm_to_graph_predictor.to(device)
return self
def forward(
self,
input_ids: Optional[torch.LongTensor] = None,
attention_mask: Optional[torch.FloatTensor] = None,
labels: Optional[torch.LongTensor] = None,
molecule_graphs: Optional[PyGBatch] = None,
molecule_properties: Optional[torch.FloatTensor] = None,
design_graphs: Optional[PyGBatch] = None,
retro_labels: Optional[torch.LongTensor] = None,
retro_product_graphs: Optional[PyGBatch] = None,
past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None,
use_cache: Optional[bool] = None,
output_attentions: Optional[bool] = None,
output_hidden_states: Optional[bool] = True,
return_dict: Optional[bool] = None,
) -> Union[Tuple, GraphLMOutput]:
return_dict = (
return_dict if return_dict is not None else self.config.use_return_dict
)
mol_token_id = self.token_id_dict["<molecule>"]
design_start_token_id = self.token_id_dict["<design_start>"]
retro_start_token_id = self.token_id_dict["<retro_start>"]
# PeftModelForCausalLM -> LlamaForCausalLM -> LlamaModel
base_llm = self.language_model.model.model
inputs_embeds = base_llm.embed_tokens(input_ids)
mol_positions = (input_ids == mol_token_id).nonzero()
mol_embeds = self.graph_encoder(
molecule_graphs.x,
molecule_graphs.edge_index,
molecule_graphs.edge_attr,
molecule_graphs.batch,
)
mol_embeds = self.graph_to_lm_connector(mol_embeds)
assert (
mol_positions.shape[0] == mol_embeds.shape[0]
), f"Number of molecule tokens ({mol_positions.shape[0]}) does not match number of molecule embeddings ({mol_embeds.shape[0]})"
inputs_embeds[mol_positions[:, 0], mol_positions[:, 1]] = mol_embeds.to(
inputs_embeds.dtype
)
lm_outputs = self.language_model(
input_ids=None,
attention_mask=attention_mask,
past_key_values=past_key_values,
inputs_embeds=inputs_embeds,
use_cache=use_cache,
output_attentions=output_attentions,
output_hidden_states=output_hidden_states,
return_dict=return_dict,
labels=labels,
)
lm_loss = lm_outputs.loss
lm_hidden_states = lm_outputs.hidden_states[-1]
design_loss = 0
if design_graphs is not None:
design_start_positions = (input_ids == design_start_token_id).nonzero()
design_body_start = design_start_positions[:, 1] + 1
design_body_indices = design_body_start.unsqueeze(1) + torch.arange(
self.num_body_tokens, device=input_ids.device
)
design_hidden = lm_hidden_states[
design_start_positions[:, 0].unsqueeze(1), design_body_indices[:, 1]
].mean(dim=1)
if design_start_positions.numel() > 0:
design_hidden = self.lm_to_graph_decoder(design_hidden)
design_loss = self.graph_decoder(
design_graphs.x,
design_graphs.edge_index,
design_graphs.edge_attr,
design_graphs.batch,
molecule_properties,
design_hidden,
NO_LABEL_INDEX,
)
# Process retro labels
retro_loss = 0
if retro_labels is not None:
# Get retro start positions for valid retro labels: (batch, step)
retro_start_positions = (input_ids == retro_start_token_id).nonzero()
retro_labels = retro_labels[retro_labels != IGNORE_INDEX]
valid_retro_mask = retro_labels != NO_LABEL_INDEX
retro_start_positions = retro_start_positions[valid_retro_mask]
retro_labels = retro_labels[valid_retro_mask]
if len(retro_labels) > 0:
# Get the query hidden states for each retro prediction
retro_body_start = retro_start_positions[:, 1] + 1
retro_body_indices = retro_body_start.unsqueeze(1) + torch.arange(
self.num_body_tokens, device=input_ids.device
)
retro_hidden = lm_hidden_states[
retro_start_positions[:, 0].unsqueeze(1), retro_body_indices
].mean(dim=1)
# Prepare graph inputs
retro_product_graphs = retro_product_graphs[
valid_retro_mask.nonzero().view(-1)
]
retro_product_graphs = PyGBatch.from_data_list(retro_product_graphs)
# Transform hidden states and make predictions
retro_hidden = self.lm_to_graph_predictor(retro_hidden)
retro_pred = self.graph_predictor(
retro_product_graphs.x,
retro_product_graphs.edge_index,
retro_product_graphs.edge_attr,
retro_product_graphs.batch,
retro_hidden,
)
retro_loss = F.cross_entropy(
retro_pred,
retro_labels,
)
total_loss = (
self.loss_weight_lm * lm_loss
+ self.loss_weight_design * retro_loss
+ self.loss_weight_retro * retro_loss
)
if not return_dict:
output = (lm_outputs.logits,) + lm_outputs[1:]
return ((total_loss,) + output) if total_loss is not None else output
return GraphLMOutput(
loss=total_loss,
logits=lm_outputs.logits,
past_key_values=lm_outputs.past_key_values,
hidden_states=lm_outputs.hidden_states,
attentions=lm_outputs.attentions,
)
def save_pretrained(
self,
save_directory: Union[str, os.PathLike],
is_main_process: bool = True,
state_dict: Optional[dict] = None,
save_function: Callable = torch.save,
push_to_hub: bool = False,
max_shard_size: Union[int, str] = "5GB",
safe_serialization: bool = True,
variant: Optional[str] = None,
token: Optional[Union[str, bool]] = None,
save_peft_format: bool = True,
save_graph_modules: bool = False,
**kwargs,
):
"""
Save the model and its configuration file to a directory.
"""
if os.path.isfile(save_directory):
raise ValueError(
f"Provided path ({save_directory}) should be a directory, not a file"
)
os.makedirs(save_directory, exist_ok=True)
# Save language model
language_model_path = os.path.join(save_directory)
self.language_model.save_pretrained(
language_model_path,
is_main_process=is_main_process,
state_dict=state_dict,
save_function=save_function,
push_to_hub=False, # set to false
max_shard_size=max_shard_size,
safe_serialization=safe_serialization,
variant=variant,
token=token,
save_peft_format=save_peft_format,
)
if save_graph_modules:
# Save graph models
graph_models = {
"graph_decoder": self.graph_decoder,
"graph_predictor": self.graph_predictor,
"graph_encoder": self.graph_encoder,
}
for name, model in graph_models.items():
model_path = os.path.join(save_directory, name)
model.save_pretrained(model_path)
# Save additional components
additional_components = {
"graph_to_lm_connector": self.graph_to_lm_connector,
"lm_to_graph_decoder": self.lm_to_graph_decoder,
"lm_to_graph_predictor": self.lm_to_graph_predictor,
}
connector_path = os.path.join(save_directory, "connector")
for name, component in additional_components.items():
os.makedirs(connector_path, exist_ok=True)
component_path = os.path.join(connector_path, f"{name}.pt")
torch.save(component.state_dict(), component_path)
config_dict = {
"model_args": convert_to_dict(self.model_args),
"finetuning_args": convert_to_dict(self.finetuning_args),
"data_args": convert_to_dict(self.data_args),
"token_id_dict": self.token_id_dict,
"num_body_tokens": self.num_body_tokens,
"loss_weight_lm": self.loss_weight_lm,
"loss_weight_design": self.loss_weight_design,
"loss_weight_retro": self.loss_weight_retro,
}
config_path = os.path.join(save_directory, "graphllm_config.json")
with open(config_path, "w") as f:
json.dump(config_dict, f, indent=2)
# Push to hub if required
if push_to_hub:
raise NotImplementedError("Push to hub not implemented yet")
def add_special_body_tokens(
self,
input_ids: torch.LongTensor,
body_token_id: int,
num_body_tokens: int,
start_token_id: Optional[int] = None,
) -> torch.LongTensor:
batch_size, seq_length = input_ids.shape
start_len = 1 if start_token_id is not None else 0
if seq_length < num_body_tokens + start_len:
seq_length = seq_length + num_body_tokens + start_len
# Create a tensor to hold start positions for each batch item
start_positions = torch.full(
(batch_size,),
seq_length - start_len - num_body_tokens,
device=input_ids.device,
)
# Calculate how many tokens to keep from the original input
tokens_to_keep = seq_length - num_body_tokens
# Find start positions
if start_token_id is not None:
start_pos_rows, start_pos_cols = (input_ids == start_token_id).nonzero(
as_tuple=True
)
for row, col in zip(start_pos_rows, start_pos_cols):
start_positions[row] = col
tokens_to_keep = seq_length - num_body_tokens - 1
# Create body tokens
body_tokens = torch.full(
(batch_size, num_body_tokens), body_token_id, device=input_ids.device
)
# Create new input_ids with left padding
new_input_ids = torch.full(
(batch_size, seq_length),
self.tokenizer.eos_token_id,
device=input_ids.device,
)
for i in range(batch_size):
start_pos = start_positions[i]
# Keep the rightmost tokens_to_keep tokens before the start token
keep_start = max(0, start_pos - tokens_to_keep)
if start_token_id is not None:
new_input_ids[
i, -(num_body_tokens + 1 + (start_pos - keep_start)) :
] = torch.cat(
[
input_ids[i, keep_start:start_pos],
torch.LongTensor([start_token_id]).to(input_ids.device),
body_tokens[i],
]
)
else:
new_input_ids[
i, -(num_body_tokens + 1 + (start_pos - keep_start)) :
] = torch.cat([input_ids[i, keep_start:start_pos], body_tokens[i]])
return new_input_ids
@torch.no_grad()
def design_molecule(
self,
input_ids: torch.LongTensor,
attention_mask: torch.FloatTensor,
molecule_properties: Optional[torch.FloatTensor] = None,
molecule_graphs: Optional[PyGBatch] = None,
rollback: bool = False,
**kwargs,
) -> List[Optional[str]]:
design_start_token_id = self.token_id_dict["<design_start>"]
design_body_token_id = self.token_id_dict["<design_body>"]
# 1. Generate molecular design analysis
if molecule_graphs is None:
analysis_tokens = self.language_model.generate(
inputs=input_ids,
attention_mask=attention_mask,
**kwargs,
)
analysis_tokens = analysis_tokens[:, input_ids.shape[1] :]
else:
mol_token_id = self.token_id_dict["<molecule>"]
base_llm = self.language_model.model
inputs_embeds = base_llm.embed_tokens(input_ids)
mol_positions = (input_ids == mol_token_id).nonzero()
mol_embeds = self.graph_encoder(
molecule_graphs.x,
molecule_graphs.edge_index,
molecule_graphs.edge_attr,
molecule_graphs.batch,
)
mol_embeds = self.graph_to_lm_connector(mol_embeds)
assert (
mol_positions.shape[0] == mol_embeds.shape[0]
), f"Number of molecule tokens ({mol_positions.shape[0]}) does not match number of molecule embeddings ({mol_embeds.shape[0]})"
inputs_embeds[mol_positions[:, 0], mol_positions[:, 1]] = mol_embeds.to(
inputs_embeds.dtype
)
analysis_tokens = self.language_model.generate(
attention_mask=attention_mask,
inputs_embeds=inputs_embeds,
**kwargs,
) # no input
# 2. Add special tokens for design body
design_input_ids = self.add_special_body_tokens(
analysis_tokens,
design_body_token_id,
self.num_body_tokens,
start_token_id=design_start_token_id,
)
design_input_ids = torch.cat([input_ids, design_input_ids], dim=1)
# 3. Get LLM embeddings for design body
lm_outputs = self.language_model(
input_ids=design_input_ids,
attention_mask=torch.ones_like(design_input_ids),
output_hidden_states=True,
return_dict=True,
)
lm_hidden_states = lm_outputs.hidden_states[-1]
design_hidden = lm_hidden_states[:, -self.num_body_tokens :].mean(dim=1)
# 4. Generate molecules using graph decoder
design_hidden = self.lm_to_graph_decoder(design_hidden)
molecule_properties = molecule_properties.type_as(design_hidden)
smiles_list = self.graph_decoder.generate(
molecule_properties,
design_hidden,
NO_LABEL_INDEX,
)
# Handle None values in smiles_list
if rollback and None in smiles_list:
smiles_list = self.design_rollback(design_input_ids, smiles_list, **kwargs)
return analysis_tokens, smiles_list
def design_rollback(
self,
analysis_tokens: torch.LongTensor,
smiles_list: List[Optional[str]],
**kwargs,
) -> List[Optional[str]]:
rollback_token_id = self.token_id_dict.get("<rollback_start>")
rollback_end_token_id = self.token_id_dict.get("<rollback_end>")
none_indices = [i for i, smiles in enumerate(smiles_list) if smiles is None]
if not none_indices:
return smiles_list # No None values, return original list
# Get corresponding analysis tokens for None indices
none_indices = torch.LongTensor(none_indices)
rollback_analysis_tokens = analysis_tokens[none_indices]
# Add rollback token to the end of each analysis token sequence
rollback_input_ids = self.add_special_body_tokens(
rollback_analysis_tokens,
rollback_token_id,
1,
)
if "max_new_tokens" in kwargs:
kwargs["max_new_tokens"] *= 2
# Generate new tokens
new_tokens = self.language_model.generate(
inputs=rollback_input_ids,
attention_mask=torch.ones_like(rollback_input_ids),
**kwargs,
)
# Process and decode new tokens
new_smiles = []
for seq in new_tokens[:, rollback_input_ids.shape[1] :]:
decoded_seq = self.tokenizer.decode(seq, skip_special_tokens=False)
end_smiles_pos = decoded_seq.find(
self.tokenizer.decode([rollback_end_token_id])
)
if end_smiles_pos != -1:
# If end token is found, truncate the sequence
new_smiles.append(decoded_seq[:end_smiles_pos].strip())
else:
# If end token is not found, append None
new_smiles.append(None)
# Update smiles_list with new decoded tokens
for i, new_smiles_str in zip(none_indices, new_smiles):
smiles_list[i] = new_smiles_str
return smiles_list
def smiles_to_graph(self, smiles: str) -> Optional[Data]:
mol = Chem.MolFromSmiles(smiles)
if mol is None:
print(f"Invalid SMILES string: {smiles}")
return None
type_idx = []
for atom in mol.GetAtoms():
if atom.GetAtomicNum() != 1: # Exclude hydrogen atoms
type_idx.append(
119 - 2 if atom.GetSymbol() == "*" else atom.GetAtomicNum() - 2
)
x = torch.LongTensor(type_idx)
num_nodes = x.size(0)
# Initialize edge_index and edge_attr as empty tensors
edge_index = torch.empty((2, 0), dtype=torch.long)
edge_attr = torch.empty((0,), dtype=torch.long)
# Only process bonds if they exist
if mol.GetNumBonds() > 0:
bond_src = []
bond_dst = []
bond_type = []
for bond in mol.GetBonds():
start, end = bond.GetBeginAtomIdx(), bond.GetEndAtomIdx()
# Exclude bonds involving hydrogen atoms
if mol.GetAtomWithIdx(start).GetAtomicNum() != 1 and mol.GetAtomWithIdx(end).GetAtomicNum() != 1:
bond_src.extend([start, end])
bond_dst.extend([end, start])
bond_type.extend([BOND_INDEX.get(bond.GetBondType(), 1)] * 2)
if bond_src: # Only create edge_index and edge_attr if there are valid bonds
edge_index = torch.tensor([bond_src, bond_dst], dtype=torch.long)
edge_attr = torch.tensor(bond_type, dtype=torch.long)
# Create PyG Data object
data = Data(x=x, edge_index=edge_index, edge_attr=edge_attr, num_nodes=num_nodes)
return data
def retrosynthesize_rollback(self, input_ids, design_text, smiles, **kwargs):
input_text = f"{design_text} To synthesize {smiles}, follow these procedures: "
input_tokens = self.tokenizer.encode(
input_text, add_special_tokens=False, return_tensors="pt"
)
input_tokens = input_tokens.to(self.device)
if "max_new_tokens" in kwargs:
kwargs["max_new_tokens"] = 256
# Generate tokens
generated_tokens = self.language_model.generate(
inputs=input_tokens,
**kwargs,
)
generated_tokens = generated_tokens[:, input_tokens.shape[1] :]
generated_tokens = generated_tokens.cpu().squeeze().tolist()
new_input_text = f"To synthesize {smiles}, follow these procedures: "
new_input_tokens = self.tokenizer.encode(new_input_text)
generated_tokens = new_input_tokens + generated_tokens
return generated_tokens
def one_step_reaction(
self,
product_smiles,
input_ids,
design_text,
molecule_graphs,
topk,
**kwargs,
):
# 1. Generate retrosynthesis analysis
retro_start_token_id = self.token_id_dict["<retro_start>"]
retro_body_token_id = self.token_id_dict["<retro_body>"]
mol_token_id = self.token_id_dict["<molecule>"]
input_text = f"{design_text} To synthesize <molecule>, follow these procedures: "
prompt_tokens = self.tokenizer.encode(
input_text, add_special_tokens=False, return_tensors="pt"
)
prompt_tokens = prompt_tokens.to(self.device)
# Combine input_ids with new_prompt_tokens if input_ids is provided
if input_ids is not None and molecule_graphs is not None:
input_ids = input_ids.view(1, -1)
prompt_tokens = torch.cat([input_ids, prompt_tokens], dim=-1)
base_llm = self.language_model.model
inputs_embeds = base_llm.embed_tokens(prompt_tokens)
product_graph = self.smiles_to_graph(product_smiles)
if product_graph is None:
return {
"reactants": [],
"scores": [],
"templates": [],
"analysis": self.tokenizer.encode(
"Invalid product SMILES", add_special_tokens=False
),
}
product_graph.to(self.device)
if input_ids is not None and molecule_graphs is not None:
all_graphs = PyGBatch.from_data_list(molecule_graphs.to_data_list() + [product_graph])
else:
all_graphs = PyGBatch.from_data_list([product_graph])
mol_embeds = self.graph_encoder(
all_graphs.x,
all_graphs.edge_index,
all_graphs.edge_attr,
all_graphs.batch,
)
mol_embeds = self.graph_to_lm_connector(mol_embeds)
mol_positions = (prompt_tokens == mol_token_id).nonzero()
assert (
mol_positions.shape[0] == mol_embeds.shape[0]
), f"Number of molecule tokens ({mol_positions.shape[0]}) does not match number of molecule embeddings ({mol_embeds.shape[0]})"
inputs_embeds[mol_positions[:, 0], mol_positions[:, 1]] = mol_embeds.to(
inputs_embeds.dtype
)
attention_mask = torch.ones_like(prompt_tokens)
if "max_new_tokens" in kwargs:
kwargs["max_new_tokens"] = 512
analysis_tokens = self.language_model.generate(
attention_mask=attention_mask,
inputs_embeds=inputs_embeds,
**kwargs,
)
# 2. Encode analysis with query tokens
retro_input_ids = self.add_special_body_tokens(
analysis_tokens,
retro_body_token_id,
self.num_body_tokens,
start_token_id=retro_start_token_id,
)
# Get LLM embeddings for retro body
lm_outputs = self.language_model(
input_ids=retro_input_ids,
attention_mask=torch.ones_like(retro_input_ids),
output_hidden_states=True,
return_dict=True,
)
lm_hidden_states = lm_outputs.hidden_states[-1]
retro_hidden = lm_hidden_states[:, -self.num_body_tokens :].mean(dim=1)
retro_hidden = self.lm_to_graph_predictor(retro_hidden)
# 3. Sample retrosynthetic templates
reactants, scores, templates = self.graph_predictor.sample_templates(
product_graph, retro_hidden, product_smiles, topk
)
# 4. Adjust the input part from the generated tokens
analysis_tokens = analysis_tokens.cpu().squeeze().tolist()
input_text = f"To synthesize {product_smiles}, follow these procedures: "
new_input_tokens = self.tokenizer.encode(input_text)
analysis_tokens = new_input_tokens + analysis_tokens
return {
"reactants": reactants,
"scores": scores,
"templates": templates,
"analysis": analysis_tokens,
}
@torch.no_grad()
def estimate_synthesis_complexity(
self,
smiles: str,
input_ids=None,
reaction=None,
molecule_cost_weight: float = 0,
language_cost_weight: float = 1,
reference_tokens: Optional[torch.LongTensor] = None,
):
cost = 0
if molecule_cost_weight is not None and molecule_cost_weight > 0:
mol_cost = self.graph_predictor.estimate_cost(smiles)
cost += mol_cost * molecule_cost_weight
if language_cost_weight is not None and language_cost_weight > 0:
language_cost = 0
if reaction is None:
message_content = f"""
Estimate remaining steps for the target {smiles} consider the following factors::
1. Intermediate complexity
2. Reagent availability
3. Side reactions
4. Stereochemistry challenges"""
else:
step = reaction.depth + 1
template = reaction.template
# analysis_tokens = reaction.analysis_tokens
reactants = reaction.children
reactants = ", ".join([r.mol for r in reactants])
message_content = f"""
Estimate remaining steps for the target {smiles} given the following parameters:
Current step {step},
Current template: {template},
Reactants: {reactants}.
Consider the following factors:
1. Intermediate complexity
2. Reagent availability
3. Side reactions
4. Stereochemistry challenges"""
# Create the messages list for the chat template
messages = [{"role": "user", "content": message_content}]
# Apply the chat template
chat_text = self.tokenizer.apply_chat_template(
messages, tokenize=False, add_generation_prompt=True
)
answers = [
"All readily available",
"Some commercial, some need 1-2 steps",
"Mix of commercial and multi-step synthesis",
"Mostly require complex synthesis",
"All require extensive multi-step synthesis",
]
answer_costs = [0, 1, 2.5, 4.5, 7]
answer_messages = [
[
{
"role": "user",
"content": "Estimate the synthesis complexity:",
},
{"role": "assistant", "content": answer},
]
for answer in answers
]
answer_chat_texts = [
self.tokenizer.apply_chat_template(
msg, tokenize=False, add_generation_prompt=False
)
for msg in answer_messages
]
# Encode chat texts
input_ids = self.tokenizer.encode(chat_text, return_tensors="pt").to(
self.device
)
answer_tokens = [
self.tokenizer.encode(text) for text in answer_chat_texts
]
# Get logits from the language model
outputs = self.language_model(input_ids)
logits = outputs.logits[:, -1, :]
# Calculate softmax probabilities for each answer
answer_logits = torch.stack(
[logits[:, tokens].mean(dim=1) for tokens in answer_tokens]
)
probs = torch.nn.functional.softmax(answer_logits, dim=0)
language_cost = (
(probs * torch.tensor(answer_costs, device=probs.device))
.sum()
.item()
)
language_cost = language_cost * language_cost_weight
cost += language_cost
return cost
@torch.no_grad()
def retrosynthesize(
self,
input_ids: torch.LongTensor,
smiles: Optional[str] = None,
molecule_graphs: Optional[PyGBatch] = None,
expansion_topk: int = 50,
iterations: int = 100,
starting_mols: Optional[List[str]] = None,
molecule_cost_weight: float = 0,
language_cost_weight: float = 1,
max_planning_time: int = 300,
rollback: bool = True,
design_text: Optional[str] = None,
**kwargs,
) -> Dict[str, Any]:
# Initialize variables
target_smiles = None
success = False
reaction_list = None
template_list = None
analysis_tokens_list = None
route_length = None
total_time = 0.0
cost = None
# Handle starting molecules
if starting_mols is None:
if self.graph_predictor.available is None:
raise ValueError(
"No starting molecules provided and no available starting molecules found."
)
starting_mols = self.graph_predictor.available["smiles"].tolist()
# Handle case when no SMILES is provided
if smiles is None and rollback:
generated_tokens = self.retrosynthesize_rollback(input_ids, design_text, None, **kwargs)
return self._create_failure_result(None, generated_tokens)
# Preprocess SMILES
target_smiles = smiles.replace("*", "[H]") if "*" in smiles else smiles
# Check validity and handle rollback if necessary
if not self.graph_decoder.check_valid(target_smiles) and rollback:
generated_tokens = self.retrosynthesize_rollback(
input_ids, design_text, target_smiles, **kwargs
)
return self._create_failure_result(target_smiles, generated_tokens)
# Perform retrosynthesis
t0 = time.time()
def expand_fn(s):
return self.one_step_reaction(
s, input_ids=input_ids, design_text=design_text, molecule_graphs=molecule_graphs, topk=expansion_topk, **kwargs
)
def value_fn(s, r):
return self.estimate_synthesis_complexity(
s, input_ids, r, molecule_cost_weight, language_cost_weight
)
if target_smiles is None:
return self._create_failure_result(None)
success, best_route, iterations = molstar(
target_mol=target_smiles,
target_mol_id=0,
starting_mols=starting_mols,
expand_fn=expand_fn,
value_fn=value_fn,
iterations=iterations,
max_time=max_planning_time,
)
total_time = time.time() - t0
# Handle successful retrosynthesis
if success:
reaction_list, template_list, cost, analysis_tokens_list = best_route.get_reaction_list()
route_length = best_route.length
# Handle failed retrosynthesis with rollback
elif rollback:
generated_tokens = self.retrosynthesize_rollback(
input_ids, design_text, target_smiles, **kwargs
)
return self._create_failure_result(target_smiles, generated_tokens)
# Prepare and return result
return {
"target": target_smiles,
"success": success,
"time": total_time,
"reaction_list": reaction_list,
"cost": cost,
"templates": template_list,
"analysis_tokens": analysis_tokens_list,
"route_length": route_length,
}
def _create_failure_result(
self,
target_smiles: Optional[str],
generated_tokens: Optional[Union[torch.Tensor, list]] = None,
) -> Dict[str, Any]:
return {
"target": target_smiles,
"success": False,
"time": 0.0,
"reaction_list": None,
"cost": None,
"templates": None,
"analysis_tokens": (
generated_tokens
if generated_tokens is not None
else "<NO ANALYSIS>"
),
"route_length": None,
}
@torch.no_grad()
def generate(
self,
input_ids: Optional[torch.LongTensor] = None,
attention_mask: Optional[torch.FloatTensor] = None,
molecule_properties: Optional[torch.FloatTensor] = None,
molecule_graphs: Optional[PyGBatch] = None,
rollback: bool = False,
starting_mols: Optional[List[str]] = None,
expansion_topk: int = 50,
iterations: int = 100,
molecule_cost_weight: float = 0,
language_cost_weight: float = 1,
do_molecular_design: Optional[bool] = True,
do_retrosynthesis: bool = True,
input_smiles_list: Optional[List[str]] = None,
max_planning_time: int = 30,
design_text_list: Optional[List[str]] = None,
**kwargs,
) -> Dict:
if attention_mask is None:
attention_mask = input_ids.new_ones(input_ids.shape)
all_info_dict = {
"token_lists": [],
"text_lists": [],
"design_analysis_tokens": None,
"smiles_list": None,
"retro_plan_dict": None,
}
# Molecular design
if do_molecular_design is True:
design_analysis_tokens, smiles_list = self.design_molecule(
input_ids,
attention_mask,
molecule_properties,
molecule_graphs,
rollback,
**kwargs,
)
all_info_dict["design_analysis_tokens"] = design_analysis_tokens.cpu()
all_info_dict["smiles_list"] = smiles_list
elif input_smiles_list is not None:
all_info_dict["smiles_list"] = input_smiles_list
else:
raise ValueError(
"Either do_molecular_design must be True/False or input_smiles_list must be provided."
)
# Retrosynthesis
if do_retrosynthesis:
if all_info_dict["smiles_list"] is None:
raise ValueError(
"Either molecular design must be performed or input_smiles_list must be provided for retrosynthesis."
)
all_info_dict["retro_plan_dict"] = {}
for i, smiles in enumerate(all_info_dict["smiles_list"]):
if design_text_list is not None:
design_text = design_text_list[0]
else:
design_text = None
all_info_dict["retro_plan_dict"][smiles] = self.retrosynthesize(
input_ids[i] if input_ids.dim() > 1 else input_ids,
smiles,
molecule_graphs=molecule_graphs,
starting_mols=starting_mols,
expansion_topk=expansion_topk,
iterations=iterations,
molecule_cost_weight=molecule_cost_weight,
language_cost_weight=language_cost_weight,
max_planning_time=max_planning_time,
design_text=design_text,
**kwargs,
)
else:
all_info_dict["retro_plan_dict"] = {
smile: {"success": None} for smile in all_info_dict["smiles_list"]
}
for batch_idx, generated_mol in enumerate(all_info_dict["smiles_list"]):
token_list = []
text_list = []
ignore_positions = {}
if do_molecular_design:
design_tokens = all_info_dict["design_analysis_tokens"][
batch_idx
].tolist()
token_list = design_tokens + [IGNORE_INDEX]
if generated_mol is None:
generated_mol = "<NO MOLECULE>"
text_list = [
self.tokenizer.decode(
design_tokens,
skip_special_tokens=True,
clean_up_tokenization_spaced=True,
),
generated_mol + ". ",
]
ignore_positions = {0: generated_mol}
if do_retrosynthesis:
available_mols = self.graph_predictor.available["smiles"].tolist()
retro_plan = all_info_dict["retro_plan_dict"][generated_mol]
if retro_plan["success"] is not None and retro_plan["success"]:
for i, (reaction, template, cost, analysis_tokens) in enumerate(
zip(
retro_plan["reaction_list"],
retro_plan["templates"],
retro_plan["cost"],
retro_plan["analysis_tokens"],
)
):
if isinstance(analysis_tokens, torch.Tensor):
analysis_tokens = analysis_tokens.tolist()
token_list.extend(analysis_tokens + [IGNORE_INDEX])
text_list.extend(
[
self.tokenizer.decode(
analysis_tokens,
skip_special_tokens=True,
clean_up_tokenization_spaced=True,
),
reaction if reaction is not None else "<NO REACTION>",
" with the template ",
template if template is not None else "<NO TEMPLATE>",
" which requires the reactants: ",
]
)
# Add these two lines to extract and add reactants
if reaction is not None:
reactants = reaction.split(">>")[1].split(".")
formatted_reactants = []
for reactant in reactants:
if reactant in available_mols:
formatted_reactants.append(
f"{reactant} (available)"
)
else:
formatted_reactants.append(reactant)
text_list.extend([", ".join(formatted_reactants), ". "])
else:
text_list.extend(["<NO REACTANTS>. "])
ignore_positions[len(token_list) - 1] = (
reaction,
template,
cost,
)
else:
analysis_tokens = retro_plan["analysis_tokens"]
if isinstance(analysis_tokens, torch.Tensor):
analysis_tokens = analysis_tokens.tolist()
token_list.extend(analysis_tokens)
text_list.extend(
[
self.tokenizer.decode(
analysis_tokens,
skip_special_tokens=True,
clean_up_tokenization_spaced=True,
),
" <NO REACTION FOUND>",
]
)
all_info_dict["token_lists"].append(token_list)
all_info_dict["text_lists"].append(text_list)
all_info_dict[f"batch_{batch_idx}_ignore_positions"] = ignore_positions
all_info_dict["IGNORE_INDEX"] = IGNORE_INDEX
return all_info_dict |