File size: 16,714 Bytes
9627ce0 | 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 | from __future__ import annotations
from abc import ABC
from copy import deepcopy
from typing import List, Sequence
import attr
import torch
from attr import asdict, define
import src.data.esm.utils.constants.api as C
from src.data.esm.tokenization import (
TokenizerCollectionProtocol,
get_esm3_model_tokenizers,
)
from src.data.esm.utils import encoding
from src.data.esm.utils.constants.models import ESM3_OPEN_SMALL
from src.data.esm.utils.misc import (
get_chainbreak_boundaries_from_sequence,
)
from src.data.esm.utils.structure.protein_chain import ProteinChain
from src.data.esm.utils.structure.protein_complex import ProteinComplex
from src.data.esm.utils.types import FunctionAnnotation, PathOrBuffer
class ProteinType(ABC): ...
## Basic Types
@define
class ESMProtein(ProteinType):
# Tracks
sequence: str | None = None
secondary_structure: str | None = None
sasa: list[float | None] | None = None
function_annotations: list[FunctionAnnotation] | None = None
coordinates: torch.Tensor | None = None
# Metrics
plddt: torch.Tensor | None = None
ptm: torch.Tensor | None = None
# When calling EvolutionaryScale API, use this flag to disclose any
# sequences that may potentially have concerns.
# Such sequences may not go through standard safety filter for approved users.
# Reach out if interested in using this.
potential_sequence_of_concern: bool = False
def __len__(self):
if self.sequence is not None:
return len(self.sequence)
elif self.secondary_structure is not None:
return len(self.secondary_structure)
elif self.sasa is not None:
return len(self.sasa)
elif self.coordinates is not None:
return self.coordinates.size(0)
else:
raise ValueError("No track to determine length from.")
@classmethod
def from_pdb(
cls,
path: PathOrBuffer,
chain_id: str = "detect",
id: str | None = None,
is_predicted: bool = False,
) -> ESMProtein:
protein_chain = ProteinChain.from_pdb(
path=path, chain_id=chain_id, id=id, is_predicted=is_predicted
)
return cls.from_protein_chain(protein_chain)
@classmethod
def from_protein_chain(
cls, protein_chain: ProteinChain, with_annotations: bool = False
) -> ESMProtein:
# By default, we don't annotate with DSSP / SASA, which are expensive.
# If mkdssp is installed, we can annotate with a flag.
if with_annotations:
return ESMProtein(
sequence=protein_chain.sequence,
secondary_structure=protein_chain.dssp().tolist(),
sasa=protein_chain.sasa().tolist(),
function_annotations=None,
coordinates=torch.tensor(protein_chain.atom37_positions),
)
else:
return ESMProtein(
sequence=protein_chain.sequence,
secondary_structure=None,
sasa=None,
function_annotations=None,
coordinates=torch.tensor(protein_chain.atom37_positions),
)
@classmethod
def from_protein_complex(
cls, protein_complex: ProteinComplex, with_annotations: bool = False
) -> ESMProtein:
if with_annotations:
raise NotImplementedError(
"Annotations are not supported for ProteinComplex yet."
)
return ESMProtein(
sequence=protein_complex.sequence,
secondary_structure=None,
sasa=None,
function_annotations=None,
coordinates=torch.tensor(
protein_complex.atom37_positions, dtype=torch.float32
),
)
def to_pdb(self, pdb_path: PathOrBuffer) -> None:
# Note: Will work for single chains as well and produce same pdb file
protein_complex = self.to_protein_complex().infer_oxygen()
protein_complex.to_pdb(pdb_path)
def to_pdb_string(self) -> str:
protein_chain = self.to_protein_chain()
return protein_chain.to_pdb_string()
def to_protein_chain(self) -> ProteinChain:
if self.coordinates is None:
raise ValueError("Coordinates are required to convert to a ProteinChain.")
protein_chain = ProteinChain.from_atom37(
atom37_positions=self.coordinates.to("cpu").numpy(),
id=None,
sequence=None if self.sequence is None else self.sequence.replace("_", "X"),
chain_id=None,
entity_id=None,
residue_index=None,
insertion_code=None,
confidence=None
if self.plddt is None
else self.plddt.detach().cpu().numpy(),
)
return protein_chain
def to_protein_complex(
self, copy_annotations_from_ground_truth: ProteinComplex | None = None
) -> ProteinComplex:
assert (
self.sequence is not None
), "ESMProtein must have a sequence to convert to ProteinComplex"
assert (
self.coordinates is not None
), "ESMProtein must have coordinates to convert to ProteinComplex"
coords = self.coordinates.to("cpu").numpy()
chain_boundaries = get_chainbreak_boundaries_from_sequence(self.sequence)
if copy_annotations_from_ground_truth is not None:
gt_chains = list(copy_annotations_from_ground_truth.chain_iter())
else:
gt_chains = None
pred_chains = []
for i, (start, end) in enumerate(chain_boundaries):
pred_chain = ProteinChain.from_atom37(
atom37_positions=coords[start:end],
sequence=self.sequence[start:end],
chain_id=gt_chains[i].chain_id if gt_chains is not None else None,
entity_id=gt_chains[i].entity_id if gt_chains is not None else None,
)
pred_chains.append(pred_chain)
return ProteinComplex.from_chains(pred_chains)
def copy(self) -> "ESMProtein":
"""Create a deep copy of the ESMProtein instance."""
return deepcopy(self)
@define
class ESMProteinTensor(ProteinType):
sequence: torch.Tensor | None = None
structure: torch.Tensor | None = None
secondary_structure: torch.Tensor | None = None
sasa: torch.Tensor | None = None
function: torch.Tensor | None = None
residue_annotations: torch.Tensor | None = None
coordinates: torch.Tensor | None = None
# When calling EvolutionaryScale API, use this flag to disclose any
# sequences that may potentially have concerns.
# Such sequences may not go through standard safety filter for approved users.
# Reach out if interested in using this.
potential_sequence_of_concern: bool = False
def _detect_attribute(self, func, msg):
mapped = {
k: func(k, v)
for k, v in asdict(self).items()
if isinstance(v, torch.Tensor)
}
s = set(mapped.values())
if len(s) <= 0:
return None
if len(s) != 1:
raise ValueError(f"Either no tracks or inconsistent {msg}: {mapped}")
return next(iter(s))
def __len__(self) -> int:
l = self._detect_attribute(lambda _, x: x.size(0), "length")
return l if l is not None else 0
@property
def device(self) -> str | torch.device:
d = self._detect_attribute(lambda _, x: x.device, "device")
assert d is not None
return d
def to(self, device_or_dtype: str | torch.device | torch.dtype) -> ESMProteinTensor:
def _to(name):
v = getattr(self, name)
if v is not None and isinstance(v, torch.Tensor):
setattr(self, name, v.to(device_or_dtype))
for n in attr.fields(ESMProteinTensor):
_to(n.name)
return self
@classmethod
def empty(
cls,
length: int,
tokenizers: TokenizerCollectionProtocol | None = None,
device: torch.device | str = "cpu",
) -> ESMProteinTensor:
if tokenizers is None:
tokenizers = get_esm3_model_tokenizers(ESM3_OPEN_SMALL)
return ESMProteinTensor(
sequence=encoding.get_default_sequence_tokens(
length, tokenizers.sequence
).to(device),
structure=encoding.get_default_structure_tokens(
length, tokenizers.structure
).to(device),
secondary_structure=encoding.get_default_secondary_structure_tokens(
length, tokenizers.secondary_structure
).to(device),
sasa=encoding.get_default_sasa_tokens(length, tokenizers.sasa).to(device),
function=encoding.get_default_function_tokens(
length, tokenizers.function
).to(device),
residue_annotations=encoding.get_default_residue_annotation_tokens(
length, tokenizers.residue_annotations
).to(device),
)
def copy(self) -> ESMProteinTensor:
"""Create a deep copy of the ESMProteinTensor instance."""
return deepcopy(self)
@define
class ESMProteinError(Exception, ProteinType):
error_code: int # Error code follows HTTP convention, i.e., 404 NotFoundError, 500 InternalError.
error_msg: str
## High Level Endpoint Types
@define
class GenerationConfig:
track: str = ""
# By default avoid sampling the amino acid "X"
invalid_ids: Sequence[int] = [24]
# Controls the number of tokens to unmask during each round of iterative generation.
schedule: str = attr.field(
validator=attr.validators.in_(["cosine", "linear"]), default="cosine"
)
# Controls which tokens to unmask during each round of iterative generation.
# "random" will unmask a correct number of tokens randomly.
# "entropy" will unmask the tokens with the lowest logit entropy first.
strategy: str = attr.field(
validator=attr.validators.in_(["random", "entropy"]), default="random"
)
# Setting default to 20, as there is diminishing return for decoding steps more than 20.
# Note that this needs to be less than or equal to the sequence length.
num_steps: int = 20
temperature: float = 1.0
temperature_annealing: bool = False
top_p: float = 1.0
condition_on_coordinates_only: bool = True
def use_entropy_based_unmasking_strategy(self):
"""Use entropy based unmasking strategy during generation."""
self.schedule = "cosine"
self.strategy = "entropy"
self.temperature_annealing = False
def use_generative_unmasking_strategy(self):
"""Use an unmasking strategy that produces more variety of generations."""
self.schedule = "cosine"
self.strategy = "random"
self.temperature_annealing = True
@define
class InverseFoldingConfig:
invalid_ids: Sequence[int] = []
temperature: float = 1.0
## Low Level Endpoint Types
@define
class SamplingTrackConfig:
temperature: float = 1.0
top_p: float = 1.0
only_sample_masked_tokens: bool = True
invalid_ids: Sequence[int] = []
topk_logprobs: int = 0
@define
class SamplingConfig:
sequence: SamplingTrackConfig | None = attr.field(
default=None, metadata={"max_topk": C.MAX_TOPK_SEQUENCE}
)
structure: SamplingTrackConfig | None = attr.field(
default=None, metadata={"max_topk": C.MAX_TOPK_STRUCTURE}
)
secondary_structure: SamplingTrackConfig | None = attr.field(
default=None, metadata={"max_topk": C.MAX_TOPK_SECONDARY_STRUCTURE}
)
sasa: SamplingTrackConfig | None = attr.field(
default=None, metadata={"max_topk": C.MAX_TOPK_SASA}
)
function: SamplingTrackConfig | None = attr.field(
default=None, metadata={"max_topk": C.MAX_TOPK_FUNCTION}
)
return_per_residue_embeddings: bool = False
return_mean_embedding: bool = False
@define
class ForwardTrackData:
sequence: torch.Tensor | None = None
structure: torch.Tensor | None = None
secondary_structure: torch.Tensor | None = None
sasa: torch.Tensor | None = None
function: torch.Tensor | None = None
@define
class LogitsConfig:
# Logits.
sequence: bool = False
# Note that getting logits for tracks other than sequence
# are not supported by Forge today, due to their impractical
# data sizes.
# These are of course supported when running local OSS models.
structure: bool = False
secondary_structure: bool = False
sasa: bool = False
function: bool = False
residue_annotations: bool = False
# Embeddings.
return_embeddings: bool = False
return_hidden_states: bool = False
ith_hidden_layer: int = -1
@define
class LogitsOutput:
logits: ForwardTrackData | None = None
embeddings: torch.Tensor | None = None
# Residue annotations is multi-hot, so deserves special treatment
# It's not a categorical distribution, but instead a bernoulli, so
# softmax across the last dimension is _wrong_
residue_annotation_logits: torch.Tensor | None = None
hidden_states: torch.Tensor | None = None
@define
class ForwardAndSampleOutput(LogitsOutput):
protein_tensor: ESMProteinTensor = ESMProteinTensor()
entropy: ForwardTrackData | None = None
# Probability of sampled token
prob: ForwardTrackData | None = None
logprob: ForwardTrackData | None = None
# Top probability at this position
top_prob: ForwardTrackData | None = None
topk_logprob: ForwardTrackData | None = None
# Which tokens correspond to top probability
topk_tokens: ForwardTrackData | None = None
per_residue_embedding: torch.Tensor | None = None
mean_embedding: torch.Tensor | None = None
class ESM3InferenceClient(ABC):
def generate(self, input: ProteinType, config: GenerationConfig) -> ProteinType:
# This is the easiest and most flexible way to run ESM3. Generate will
# iteratively sample tokens an provide an output with the track specified
# completely filled out, according to the GenerationConfig provided.
# It is a local function wrapping calls for encode -> iterative_sampling -> decode.
# if a ESMProteinTensor is provided, encode and decode are skipped
raise NotImplementedError
def batch_generate(
self, inputs: Sequence[ProteinType], configs: Sequence[GenerationConfig]
) -> Sequence[ProteinType]:
# Same as generate(...), but generates a batch of proteins at once.
raise NotImplementedError
def encode(self, input: ESMProtein) -> ESMProteinTensor:
# Encode allows for encoding RawRepresentation into TokenizedRepresentation.
# This runs the structure_token_encoder, as well as dealing with PDB => atom37 conversion
raise NotImplementedError
def decode(self, input: ESMProteinTensor) -> ESMProtein:
# Decode is the inverse of encode, and runs a structure_token_decoder to output coordinates
raise NotImplementedError
def logits(
self, input: ESMProteinTensor, config: LogitsConfig = LogitsConfig()
) -> LogitsOutput:
# Our API generally discourages using raw forwards.
# This is because sending logits can be prohibitively expensive.
# Please use forward_and_sample instead.
raise NotImplementedError
def forward_and_sample(
self, input: ESMProteinTensor, sampling_configuration: SamplingConfig
) -> ForwardAndSampleOutput:
# forward_and_sample runs a single model forward, sampling tokens according to `SamplingConfiguration`.
# This is the way for power users to run ESM3. We hope to design this in a way to enable high throughput
# inference, as well as arbitrary chain-of-though invocations of ESM3.
raise NotImplementedError
@property
def raw_model(self):
# Get underlying esm3 model of an inference client.
raise NotImplementedError
class ESMCInferenceClient(ABC):
def encode(self, input: ESMProtein) -> ESMProteinTensor:
# Encode allows for encoding RawRepresentation into TokenizedRepresentation.
raise NotImplementedError
def decode(self, input: ESMProteinTensor) -> ESMProtein:
# Decode is the inverse of encode
raise NotImplementedError
def logits(
self, input: ESMProteinTensor, config: LogitsConfig = LogitsConfig()
) -> LogitsOutput:
raise NotImplementedError
@property
def raw_model(self):
# Get underlying esmc model of an inference client.
raise NotImplementedError
|