File size: 19,653 Bytes
de1d205 6887a13 de1d205 6887a13 f07c563 de1d205 6887a13 de1d205 f07c563 de1d205 f07c563 de1d205 6887a13 f07c563 de1d205 f07c563 de1d205 1e39a5b de1d205 1e39a5b de1d205 1e39a5b de1d205 6daea16 de1d205 6daea16 de1d205 f4c79c8 de1d205 1e39a5b f4c79c8 de1d205 f4c79c8 de1d205 1e39a5b f07c563 de1d205 c4fe6eb de1d205 47956c9 f07c563 de1d205 227e6a4 de1d205 f07c563 de1d205 f07c563 de1d205 f07c563 de1d205 f07c563 de1d205 f07c563 227e6a4 de1d205 227e6a4 de1d205 227e6a4 de1d205 227e6a4 de1d205 227e6a4 de1d205 227e6a4 f07c563 de1d205 f07c563 de1d205 f07c563 de1d205 f07c563 de1d205 f07c563 de1d205 f07c563 de1d205 f07c563 de1d205 f07c563 de1d205 f07c563 de1d205 227e6a4 1bdce4c de1d205 227e6a4 de1d205 227e6a4 de1d205 6887a13 de1d205 6887a13 de1d205 6887a13 de1d205 6887a13 de1d205 c4fe6eb de1d205 c4fe6eb 1e39a5b 1bacfa9 de1d205 1e39a5b f07c563 de1d205 f07c563 de1d205 f07c563 de1d205 f07c563 de1d205 6daea16 de1d205 6daea16 de1d205 f07c563 de1d205 227e6a4 1bacfa9 227e6a4 de1d205 47956c9 1bacfa9 f07c563 227e6a4 de1d205 47956c9 1bacfa9 227e6a4 f07c563 7f619d7 f07c563 |
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 |
from typing import Dict, List, Any, Tuple, Optional
import os
import torch
from transformers import AutoTokenizer, PreTrainedTokenizerFast
import pandas as pd
import time
import numpy as np
from precious3_gpt_multi_modal import Precious3MPTForCausalLM
class EndpointHandler:
def __init__(self, path: str = ""):
"""
Initializes the EndpointHandler with the specified model type and device.
Args:
path (str): Path to the pretrained model directory.
"""
self.device = 'cuda'
self.path = path
# Load model and tokenizer from path
self.model = self._load_model(path)
print('Model loaded')
self.tokenizer = AutoTokenizer.from_pretrained("insilicomedicine/precious3-gpt-multi-modal", trust_remote_code=True)
print('Tokenizer loaded')
# Set token IDs in model configuration
self._set_model_token_ids()
# Load unique entities and embeddings
self.unique_compounds_p3, self.unique_genes_p3 = self._load_unique_entities()
self.emb_gpt_genes, self.emb_hgt_genes = self._load_embeddings()
print('Embeddings loaded')
def _load_model(self, path: str) -> Precious3MPTForCausalLM:
""" Load model based on specified model type. """
return Precious3MPTForCausalLM.from_pretrained(path, torch_dtype=torch.bfloat16).to(self.device)
def _set_model_token_ids(self):
""" Set predefined token IDs in the model config. """
self.model.config.pad_token_id = self.tokenizer.pad_token_id
self.model.config.bos_token_id = self.tokenizer.bos_token_id
self.model.config.eos_token_id = self.tokenizer.eos_token_id
def _load_unique_entities(self) -> Tuple[List[str], List[str]]:
""" Load unique entities from online CSV and return lists of compounds and genes. """
unique_entities_p3 = pd.read_csv('https://huggingface.co/insilicomedicine/precious3-gpt/raw/main/all_entities_with_type.csv')
unique_compounds = [i.strip() for i in unique_entities_p3[unique_entities_p3.type == 'compound'].entity.to_list()]
unique_genes = [i.strip() for i in unique_entities_p3[unique_entities_p3.type == 'gene'].entity.to_list()]
return unique_compounds, unique_genes
def _load_embeddings(self) -> Tuple[Dict[str, Any], Dict[str, Any]]:
""" Load gene embeddings and return as dictionaries. """
emb_gpt_genes = pd.read_pickle('https://huggingface.co/insilicomedicine/precious3-gpt-multi-modal/resolve/main/multi-modal-data/emb_gpt_genes.pickle')
emb_hgt_genes = pd.read_pickle('https://huggingface.co/insilicomedicine/precious3-gpt-multi-modal/resolve/main/multi-modal-data/emb_hgt_genes.pickle')
return (dict(zip(emb_gpt_genes.gene_symbol.tolist(), emb_gpt_genes.embs.tolist())),
dict(zip(emb_hgt_genes.gene_symbol.tolist(), emb_hgt_genes.embs.tolist())))
def create_prompt(self, prompt_config: Dict[str, Any]) -> str:
"""
Create a prompt string based on the provided configuration.
Args:
prompt_config (Dict[str, Any]): Configuration dict containing prompt variables.
Returns:
str: The formatted prompt string.
"""
prompt = "[BOS]"
multi_modal_prefix = '<modality0><modality1><modality2><modality3>' * 3
for k, v in prompt_config.items():
if k == 'instruction':
prompt += f'<{v}>' if isinstance(v, str) else "".join([f'<{v_i}>' for v_i in v])
elif k == 'up':
if v:
prompt += f'{multi_modal_prefix}<{k}>{v} </{k}>' if isinstance(v, str) else f'{multi_modal_prefix}<{k}>{" ".join(v)} </{k}>'
elif k == 'down':
if v:
prompt += f'{multi_modal_prefix}<{k}>{v} </{k}>' if isinstance(v, str) else f'{multi_modal_prefix}<{k}>{" ".join(v)} </{k}>'
elif k == 'age':
if isinstance(v, int):
prompt += f'<{k}_individ>{v} </{k}_individ>' if prompt_config['species'].strip() == 'human' else f'<{k}_individ>Macaca-{int(v/20)} </{k}_individ>'
else:
if v:
prompt += f'<{k}>{v.strip()} </{k}>' if isinstance(v, str) else f'<{k}>{" ".join(v)} </{k}>'
else:
prompt += f'<{k}></{k}>'
print('Generated prompt:', prompt)
return prompt
def custom_generate(self,
input_ids: torch.Tensor,
acc_embs_up_kg_mean: Optional[np.ndarray],
acc_embs_down_kg_mean: Optional[np.ndarray],
acc_embs_up_txt_mean: Optional[np.ndarray],
acc_embs_down_txt_mean: Optional[np.ndarray],
device: str,
max_new_tokens: int,
mode: str,
temperature: float = 0.8,
top_p: float = 0.2,
top_k: int = 3550,
n_next_tokens: int = 50,
num_return_sequences: int = 1,
random_seed: int = 137) -> Tuple[Dict[str, List], List[List], int]:
"""
Generate sequences based on input ids and accumulated embeddings.
Args:
input_ids (torch.Tensor): Input token IDs for generation.
acc_embs_up_kg_mean (Optional[np.ndarray]): Accumulated embeddings for UP genes (KG mean).
acc_embs_down_kg_mean (Optional[np.ndarray]): Accumulated embeddings for DOWN genes (KG mean).
acc_embs_up_txt_mean (Optional[np.ndarray]): Accumulated embeddings for UP genes (Text mean).
acc_embs_down_txt_mean (Optional[np.ndarray]): Accumulated embeddings for DOWN genes (Text mean).
device (str): The device to perform computation on.
max_new_tokens (int): Maximum number of new tokens to generate.
mode (str): Mode of generation to determine behavior.
temperature (float): Temperature for randomness in sampling.
top_p (float): Top-p (nucleus) sampling threshold.
top_k (int): Top-k sampling threshold.
n_next_tokens (int): Number of tokens to consider for predicting compounds.
num_return_sequences (int): Number of sequences to return.
random_seed (int): Random seed for reproducibility.
Returns:
Tuple[Dict[str, List], List[List], int]: Processed outputs, predicted compounds, and the random seed.
"""
torch.manual_seed(random_seed)
# Prepare modality embeddings
modality0_emb = torch.unsqueeze(torch.from_numpy(acc_embs_up_kg_mean), 0).to(device) if isinstance(acc_embs_up_kg_mean, np.ndarray) else None
modality1_emb = torch.unsqueeze(torch.from_numpy(acc_embs_down_kg_mean), 0).to(device) if isinstance(acc_embs_down_kg_mean, np.ndarray) else None
modality2_emb = torch.unsqueeze(torch.from_numpy(acc_embs_up_txt_mean), 0).to(device) if isinstance(acc_embs_up_txt_mean, np.ndarray) else None
modality3_emb = torch.unsqueeze(torch.from_numpy(acc_embs_down_txt_mean), 0).to(device) if isinstance(acc_embs_down_txt_mean, np.ndarray) else None
# Initialize outputs
outputs = []
next_token_compounds = []
next_token_up_genes = []
next_token_down_genes = []
# Generate requested sequences
for _ in range(num_return_sequences):
start_time = time.time()
generated_sequence = []
current_token = input_ids.clone()
next_token = current_token[0][-1]
generated_tokens_counter = 0
while generated_tokens_counter < max_new_tokens - 1:
# Stop if EOS token is generated
if next_token == self.tokenizer.eos_token_id:
generated_sequence.append(current_token)
break
# Forward pass through the model
logits = self.model.forward(
input_ids=current_token,
modality0_emb=modality0_emb,
modality0_token_id=self.tokenizer.encode('<modality0>')[0],
modality1_emb=modality1_emb,
modality1_token_id=self.tokenizer.encode('<modality1>')[0],
modality2_emb=modality2_emb,
modality2_token_id=self.tokenizer.encode('<modality2>')[0],
modality3_emb=modality3_emb,
modality3_token_id=self.tokenizer.encode('<modality3>')[0],
)[0]
# Adjust logits based on temperature
if temperature != 1.0:
logits = logits / temperature
# Apply nucleus sampling (top-p)
sorted_logits, sorted_indices = torch.sort(logits, descending=True)
cumulative_probs = torch.cumsum(torch.softmax(sorted_logits, dim=-1), dim=-1)
sorted_indices_to_remove = cumulative_probs > top_p
if top_k > 0:
sorted_indices_to_remove[..., top_k:] = 1
inf_tensor = torch.tensor(float("-inf")).type(torch.bfloat16).to(logits.device)
logits = logits.where(sorted_indices_to_remove, inf_tensor)
# Handle sampling based on current token
if current_token[0][-1] == self.tokenizer.encode('<drug>')[0] and len(next_token_compounds) == 0:
next_token_compounds.append(torch.topk(torch.softmax(logits, dim=-1)[0][-1, :].flatten(), n_next_tokens).indices)
if current_token[0][-1] == self.tokenizer.encode('<up>')[0] and len(next_token_up_genes) == 0:
# TODO: SET N-NEXT-TOKENS AS PARAM FOR GENES
n_next_tokens_4_genes = 250
top_k_up_genes = torch.topk(torch.softmax(logits, dim=-1)[0][-1, :].flatten(), n_next_tokens_4_genes).indices
next_token_up_genes.append(top_k_up_genes)
generated_tokens_counter += len(top_k_up_genes)
current_token = torch.cat((current_token, top_k_up_genes.unsqueeze(0),
torch.tensor([self.tokenizer.encode('</up>')[0]]).unsqueeze(0).to(device)), dim=-1)
continue
if current_token[0][-1] == self.tokenizer.encode('<down>')[0] and len(next_token_down_genes) == 0:
# TODO: SET N-NEXT-TOKENS AS PARAM FOR GENES
n_next_tokens_4_genes = 250
top_k_down_genes = torch.topk(torch.softmax(logits, dim=-1)[0][-1, :].flatten(), n_next_tokens_4_genes).indices
next_token_down_genes.append(top_k_down_genes)
generated_tokens_counter += len(top_k_down_genes)
current_token = torch.cat((current_token, top_k_down_genes.unsqueeze(0),
torch.tensor([self.tokenizer.encode('</down>')[0]]).unsqueeze(0).to(device)), dim=-1)
continue
# Sample the next token
next_token = torch.multinomial(torch.softmax(logits, dim=-1)[0], num_samples=1)[-1, :].unsqueeze(0)
current_token = torch.cat((current_token, next_token), dim=-1)
generated_tokens_counter += 1
print("Generation time:", time.time() - start_time)
outputs.append(generated_sequence)
# Process generated results
processed_outputs = self.process_generated_outputs(next_token_up_genes, next_token_down_genes, mode)
predicted_compounds_ids = [self.tokenizer.convert_ids_to_tokens(j) for j in next_token_compounds]
predicted_compounds = [[i.strip() for i in j] for j in predicted_compounds_ids]
return processed_outputs, predicted_compounds, random_seed
def process_generated_outputs(self, next_token_up_genes: List[List], next_token_down_genes: List[List], mode: str) -> Dict[str, List]:
"""
Process generated outputs for UP and DOWN genes based on the mode.
Args:
next_token_up_genes (List[List]): List of tokens generated for UP genes.
next_token_down_genes (List[List]): List of tokens generated for DOWN genes.
mode (str): Generation mode.
Returns:
Dict[str, List]: Processed outputs based on the model mode.
"""
processed_outputs = {"up": [], "down": []}
if mode in ['meta2diff', 'meta2diff2compound']:
processed_outputs['up'] = self._get_unique_genes(next_token_up_genes)
processed_outputs['down'] = self._get_unique_genes(next_token_down_genes)
else:
processed_outputs = {"generated_sequences": []} # Placeholder if not specific mode
return processed_outputs
def _get_unique_genes(self, tokens: List[List]) -> List[List[str]]:
"""
Get unique gene symbols from generated tokens.
Args:
tokens (List[List]): List of token IDs.
Returns:
List[List[str]]: List of unique gene symbols for each token sequence.
"""
predicted_genes = []
predicted_genes_tokens = [self.tokenizer.convert_ids_to_tokens(j) for j in tokens]
for j in predicted_genes_tokens:
generated_sample = [i.strip() for i in j]
# Intersection with existing genes to validate
predicted_genes.append(sorted(set(generated_sample) & set(self.unique_genes_p3), key=generated_sample.index))
return predicted_genes
def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]:
"""
Handles incoming requests to the endpoint, processing data and generating responses.
Args:
data (Dict[str, Any]): The payload with the text prompt and generation parameters.
Returns:
Dict[str, Any]: The resulting output dictionary for the request.
"""
data = data.copy()
parameters = data.pop("parameters", None)
config_data = data.pop("inputs", None)
mode = data.pop('mode', 'Not specified')
config_data_copy = config_data.copy()
prompt = self.create_prompt(config_data_copy)
if mode != "diff2compound":
prompt += "<up>"
inputs = self.tokenizer(prompt, return_tensors="pt")
if 3 in inputs['input_ids'][0]:
decoded_tokens = self.tokenizer.convert_ids_to_tokens(inputs['input_ids'][0])
print(f"\n>>> Warning! There are unknown tokens in prompt: {''.join(decoded_tokens)} \n")
input_ids = inputs["input_ids"].to(self.device)
max_new_tokens = self.model.config.max_seq_len - len(input_ids[0])
acc_embs_up1_mean, acc_embs_up2_mean, acc_embs_down1_mean, acc_embs_down2_mean = self._get_accumulated_embeddings(config_data)
generated_sequence, raw_next_token_generation, out_seed = self.custom_generate(
input_ids=input_ids,
acc_embs_up_kg_mean=acc_embs_up1_mean,
acc_embs_down_kg_mean=acc_embs_down1_mean,
acc_embs_up_txt_mean=acc_embs_up2_mean,
acc_embs_down_txt_mean=acc_embs_down2_mean,
max_new_tokens=max_new_tokens, mode=mode,
device=self.device, **parameters
)
next_token_generation = [sorted(set(i) & set(self.unique_compounds_p3), key=i.index) for i in raw_next_token_generation]
out = self._prepare_output(generated_sequence, next_token_generation, mode, prompt, out_seed)
return out
def _get_accumulated_embeddings(self, config_data: Dict[str, List[str]]) -> Tuple[Optional[np.ndarray], Optional[np.ndarray], Optional[np.ndarray], Optional[np.ndarray]]:
"""
Retrieve accumulated embeddings for UP and DOWN genes.
Args:
config_data (Dict[str, List[str]]): Configuration dictionary with gene information.
Returns:
Tuple[Optional[np.ndarray], ...]: Mean accumulated embeddings for UP and DOWN genes.
"""
acc_embs_up1 = []
acc_embs_up2 = []
if 'up' in config_data:
for gs in config_data['up']:
try:
acc_embs_up1.append(self.emb_hgt_genes[gs])
acc_embs_up2.append(self.emb_gpt_genes[gs])
except Exception as e:
pass
acc_embs_up1_mean = np.array(acc_embs_up1).mean(0) if acc_embs_up1 else None
acc_embs_up2_mean = np.array(acc_embs_up2).mean(0) if acc_embs_up2 else None
acc_embs_down1 = []
acc_embs_down2 = []
if 'down' in config_data:
for gs in config_data['down']:
try:
acc_embs_down1.append(self.emb_hgt_genes[gs])
acc_embs_down2.append(self.emb_gpt_genes[gs])
except Exception as e:
pass
# for gs in config_data['down']:
# acc_embs_down1.append(self.emb_hgt_genes.get(gs))
# acc_embs_down2.append(self.emb_gpt_genes.get(gs))
acc_embs_down1_mean = np.array(acc_embs_down1).mean(0) if acc_embs_down1 else None
acc_embs_down2_mean = np.array(acc_embs_down2).mean(0) if acc_embs_down2 else None
return acc_embs_up1_mean, acc_embs_up2_mean, acc_embs_down1_mean, acc_embs_down2_mean
def _prepare_output(self, generated_sequence: Any, next_token_generation: List[List], mode: str, prompt: str, out_seed: int) -> Dict[str, Any]:
"""
Prepare the output dictionary based on the mode of operation.
Args:
generated_sequence (Any): The generated sequences from the model.
next_token_generation (List[List]): The next tokens generated.
mode (str): Mode of operation.
prompt (str): The input prompt that was used.
out_seed (int): Random seed used in generation.
Returns:
Dict[str, Any]: Output dictionary with structured results.
"""
try:
outputs = {}
if mode == "meta2diff":
outputs = {"up": generated_sequence['up'], "down": generated_sequence['down']}
out = {"output": outputs, "mode": mode, "message": "Done!", "input": prompt, 'random_seed': out_seed}
elif mode == "meta2diff2compound":
outputs = {"up": generated_sequence['up'], "down": generated_sequence['down']}
out = {
"output": outputs, "compounds": next_token_generation, "mode": mode,
"message": "Done!", "input": prompt, 'random_seed': out_seed}
elif mode == "diff2compound":
outputs = generated_sequence
out = {
"output": outputs, "compounds": next_token_generation, "mode": mode,
"message": "Done!", "input": prompt, 'random_seed': out_seed}
else:
out = {"message": f"Specify one of the following modes: meta2diff, meta2diff2compound, diff2compound. Your mode is: {mode}"}
except Exception as e:
print(e)
outputs, next_token_generation = [None], [None]
out = {"output": outputs, "mode": mode, 'message': f"{e}", "input": prompt, 'random_seed': 137}
return out |