Instructions to use Synthyra/ESMFold2 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use Synthyra/ESMFold2 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("feature-extraction", model="Synthyra/ESMFold2", trust_remote_code=True)# Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("Synthyra/ESMFold2", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
File size: 20,018 Bytes
2c1249c | 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 | """Shared transformer probes for residue and sequence prediction tasks."""
from __future__ import annotations
import math
from typing import Any
import torch
from torch import nn
from torch.nn import functional as F
from transformers.modeling_outputs import (
BaseModelOutput,
SequenceClassifierOutput,
TokenClassifierOutput,
)
try:
from fastplms.attention import (
AttentionBackend,
_get_flex_attention_fn,
flex_attention,
get_attention_mask,
resolve_attention_backend,
)
from fastplms.embeddings.pooling import Pooler
from fastplms.models._esm_rotary import RotaryEmbedding
except ModuleNotFoundError as error:
_COMPOSITE_REQUIRED_NAMES = (
"AttentionBackend",
"Pooler",
"RotaryEmbedding",
"_get_flex_attention_fn",
"flex_attention",
"get_attention_mask",
"resolve_attention_backend",
)
if error.name != "fastplms" or any(
name not in globals() for name in _COMPOSITE_REQUIRED_NAMES
):
raise
# Flat Hub composites define every shared symbol above this source.
_SUPPORTED_BACKENDS = frozenset(
{
AttentionBackend.EAGER,
AttentionBackend.SDPA,
AttentionBackend.FLEX_ATTENTION,
}
)
_SUPPORTED_PROBLEM_TYPES = frozenset(
{
"regression",
"single_label_classification",
"multi_label_classification",
}
)
_UNSUPPORTED_POOLING = frozenset({"cls", "parti"})
def _config_value(config: Any, name: str, default: Any) -> Any:
value = getattr(config, name, None)
return default if value is None else value
def _attention_backend(config: Any) -> AttentionBackend:
requested = getattr(config, "_attn_implementation", None)
if requested is None:
requested = getattr(config, "attn_backend", "sdpa")
backend = resolve_attention_backend(requested)
if backend not in _SUPPORTED_BACKENDS:
expected = ", ".join(sorted(item.value for item in _SUPPORTED_BACKENDS))
raise ValueError(
f"Classification probes support only {expected}; received {backend.value!r}."
)
return backend
def resolve_problem_type(
config: Any,
labels: torch.Tensor,
*,
num_labels: int,
) -> str:
"""Resolve and persist the standard Transformers classification problem type."""
problem_type = getattr(config, "problem_type", None)
if problem_type is None:
if num_labels == 1:
problem_type = "regression"
elif labels.dtype in {torch.long, torch.int}:
problem_type = "single_label_classification"
else:
problem_type = "multi_label_classification"
config.problem_type = problem_type
if problem_type not in _SUPPORTED_PROBLEM_TYPES:
raise ValueError(
f"Unsupported problem_type {problem_type!r}; expected one of "
f"{sorted(_SUPPORTED_PROBLEM_TYPES)}."
)
return problem_type
def sequence_classification_loss(
logits: torch.Tensor,
labels: torch.Tensor,
*,
problem_type: str,
num_labels: int,
) -> torch.Tensor:
"""Compute a Hugging Face-compatible sequence task loss."""
labels = labels.to(logits.device)
if problem_type == "regression":
if num_labels == 1:
return F.mse_loss(logits.squeeze(-1), labels.squeeze(-1).to(logits.dtype))
return F.mse_loss(logits, labels.to(logits.dtype))
if problem_type == "single_label_classification":
return F.cross_entropy(logits.reshape(-1, num_labels), labels.reshape(-1).long())
if problem_type == "multi_label_classification":
return F.binary_cross_entropy_with_logits(logits, labels.to(logits.dtype))
raise ValueError(f"Unsupported problem_type {problem_type!r}.")
def _masked_elementwise_loss(
losses: torch.Tensor,
labels: torch.Tensor,
) -> torch.Tensor:
valid = labels.ne(-100)
if not bool(valid.any()):
return losses.sum() * 0
return losses.masked_select(valid).mean()
def token_classification_loss(
logits: torch.Tensor,
labels: torch.Tensor,
*,
problem_type: str,
num_labels: int,
) -> torch.Tensor:
"""Compute a token task loss, excluding every label element equal to ``-100``."""
labels = labels.to(logits.device)
if problem_type == "regression":
targets = labels.to(logits.dtype)
if num_labels == 1 and targets.ndim == logits.ndim - 1:
targets = targets.unsqueeze(-1)
if targets.shape != logits.shape:
raise ValueError(
"Token regression labels must match logits, except that the final "
"singleton dimension may be omitted when num_labels=1."
)
return _masked_elementwise_loss(F.mse_loss(logits, targets, reduction="none"), targets)
if problem_type == "single_label_classification":
if not bool(labels.ne(-100).any()):
return logits.sum() * 0
return F.cross_entropy(
logits.reshape(-1, num_labels),
labels.reshape(-1).long(),
ignore_index=-100,
)
if problem_type == "multi_label_classification":
if labels.shape != logits.shape:
raise ValueError("Multilabel token labels must have the same shape as logits.")
losses = F.binary_cross_entropy_with_logits(
logits,
labels.to(logits.dtype),
reduction="none",
)
return _masked_elementwise_loss(losses, labels)
raise ValueError(f"Unsupported problem_type {problem_type!r}.")
class SwiGLU(nn.Module):
"""SwiGLU activation used by the Protify-aligned feed-forward layer."""
def forward(self, inputs: torch.Tensor) -> torch.Tensor:
gate, values = inputs.chunk(2, dim=-1)
return F.silu(gate) * values
class ProbeSelfAttention(nn.Module):
"""Four-head RoPE self-attention with explicit, fail-closed dispatch."""
def __init__(
self,
hidden_size: int,
num_heads: int,
dropout: float,
backend: AttentionBackend,
use_bias: bool,
) -> None:
super().__init__()
if hidden_size % num_heads:
raise ValueError("classifier_probe_hidden_size must be divisible by its head count.")
self.hidden_size = hidden_size
self.num_heads = num_heads
self.head_size = hidden_size // num_heads
self.dropout = dropout
self.backend = backend
self.qkv = nn.Linear(hidden_size, 3 * hidden_size, bias=use_bias)
self.output = nn.Linear(hidden_size, hidden_size, bias=use_bias)
self.rotary = RotaryEmbedding(self.head_size)
def _reshape(self, tensor: torch.Tensor) -> torch.Tensor:
batch_size, sequence_length, _ = tensor.shape
return tensor.view(
batch_size,
sequence_length,
self.num_heads,
self.head_size,
).transpose(1, 2)
def forward(
self,
hidden_states: torch.Tensor,
*,
attention_mask: torch.Tensor | None,
output_attentions: bool,
) -> tuple[torch.Tensor, torch.Tensor | None]:
batch_size, sequence_length, _ = hidden_states.shape
query, key, value = self.qkv(hidden_states).chunk(3, dim=-1)
query = self._reshape(query)
key = self._reshape(key)
value = self._reshape(value)
query, key = self.rotary(query, key)
if output_attentions and self.backend != AttentionBackend.EAGER:
raise ValueError(
f"output_attentions=True is unavailable for {self.backend.value!r}; "
"select 'eager' explicitly."
)
_, attention_mask_4d, flex_block_mask = get_attention_mask(
self.backend,
batch_size,
sequence_length,
hidden_states.device,
attention_mask,
hidden_states.dtype,
)
dropout = self.dropout if self.training else 0.0
attention_weights = None
if self.backend == AttentionBackend.EAGER:
scores = query @ key.transpose(-2, -1) / math.sqrt(self.head_size)
if attention_mask_4d is not None:
scores = scores.masked_fill(~attention_mask_4d, float("-inf"))
attention_weights = scores.softmax(dim=-1)
context = F.dropout(attention_weights, p=dropout, training=self.training) @ value
elif self.backend == AttentionBackend.SDPA:
context = F.scaled_dot_product_attention(
query,
key,
value,
attn_mask=attention_mask_4d,
dropout_p=dropout,
)
elif self.backend == AttentionBackend.FLEX_ATTENTION:
if flex_attention is None:
raise RuntimeError("'flex_attention' was requested but is unavailable.")
flex_fn = _get_flex_attention_fn(
device=query.device,
dtype=query.dtype,
shape=tuple(query.shape),
mask_semantics="padding",
)
if flex_fn is None:
raise RuntimeError("'flex_attention' was requested but is unavailable.")
context = flex_fn(
query,
key,
value,
block_mask=flex_block_mask,
scale=1.0 / math.sqrt(self.head_size),
kernel_options={"PRESCALE_QK": True, "BLOCK_N": 32},
)
else:
raise AssertionError(f"Unhandled attention backend {self.backend.value!r}.")
context = context.transpose(1, 2).contiguous().view(
batch_size,
sequence_length,
self.hidden_size,
)
return self.output(context), attention_weights
class ProteinTransformerProbe(nn.Module):
"""Project residue embeddings and refine them with exactly one pre-LN block."""
def __init__(self, config: Any, input_size: int) -> None:
super().__init__()
hidden_size = int(_config_value(config, "classifier_probe_hidden_size", 512))
num_heads = int(_config_value(config, "classifier_probe_num_heads", 4))
dropout = float(_config_value(config, "classifier_probe_dropout", 0.1))
use_bias = bool(
_config_value(
config,
"classifier_use_bias",
_config_value(config, "use_bias", False),
)
)
if hidden_size != 512 or num_heads != 4 or hidden_size // num_heads != 128:
raise ValueError(
"The folding classification probe requires a 512-wide projection with "
"four 128-wide attention heads."
)
self.hidden_size = hidden_size
self.input_norm = nn.LayerNorm(input_size)
self.input_projection = nn.Linear(input_size, hidden_size, bias=use_bias)
self.attention_norm = nn.LayerNorm(hidden_size)
self.attention = ProbeSelfAttention(
hidden_size,
num_heads,
dropout,
_attention_backend(config),
use_bias,
)
intermediate_size = int(math.ceil((8 / 3) * hidden_size / 256) * 256)
self.feed_forward_norm = nn.LayerNorm(hidden_size)
self.feed_forward = nn.Sequential(
nn.Linear(hidden_size, 2 * intermediate_size, bias=use_bias),
SwiGLU(),
nn.Dropout(dropout),
nn.Linear(intermediate_size, hidden_size, bias=use_bias),
)
self.residual_dropout = nn.Dropout(dropout)
@property
def attn_backend(self) -> str:
return self.attention.backend.value
def forward(
self,
embeddings: torch.Tensor,
attention_mask: torch.Tensor | None = None,
*,
output_attentions: bool = False,
output_hidden_states: bool = False,
return_dict: bool = True,
) -> BaseModelOutput | tuple[torch.Tensor, ...]:
if embeddings.ndim != 3:
raise ValueError("embeddings must have shape (batch, residue, channel).")
embeddings = embeddings.to(dtype=self.input_projection.weight.dtype)
hidden_states = self.input_projection(self.input_norm(embeddings))
attention_output, attention_weights = self.attention(
self.attention_norm(hidden_states),
attention_mask=attention_mask,
output_attentions=output_attentions,
)
hidden_states = hidden_states + self.residual_dropout(attention_output)
hidden_states = hidden_states + self.residual_dropout(
self.feed_forward(self.feed_forward_norm(hidden_states))
)
output = BaseModelOutput(
last_hidden_state=hidden_states,
hidden_states=(hidden_states,) if output_hidden_states else None,
attentions=(attention_weights,) if output_attentions else None,
)
return output if return_dict else output.to_tuple()
class _ClassificationProbe(nn.Module):
def __init__(self, config: Any, input_size: int, *, sequence_task: bool) -> None:
super().__init__()
self.config = config
self.num_labels = int(_config_value(config, "num_labels", 2))
self.transformer = ProteinTransformerProbe(config, input_size)
self.sequence_task = sequence_task
pooling_types = _config_value(config, "classifier_pooling_types", ["mean"])
self.pooler = Pooler(pooling_types) if sequence_task else None
if self.pooler is not None:
unsupported = sorted(set(self.pooler.names) & _UNSUPPORTED_POOLING)
if unsupported:
raise ValueError(
"Classification probes consume residue-only representations and do not "
f"support pooling operation(s) {unsupported}."
)
hidden_size = self.transformer.hidden_size
classifier_input = hidden_size * (len(self.pooler.names) if self.pooler else 1)
classifier_hidden = int(_config_value(config, "classifier_hidden_size", 4096))
classifier_dropout = float(_config_value(config, "classifier_dropout", 0.2))
use_bias = bool(
_config_value(
config,
"classifier_use_bias",
_config_value(config, "use_bias", False),
)
)
projection_size = int(math.ceil((2 * self.num_labels) / 256) * 256)
classifier_layers: list[nn.Module] = [
nn.LayerNorm(classifier_input),
nn.Linear(classifier_input, classifier_hidden, bias=use_bias),
nn.ReLU(),
nn.Dropout(classifier_dropout),
nn.Linear(classifier_hidden, projection_size, bias=use_bias),
nn.ReLU(),
nn.Dropout(classifier_dropout),
]
if not sequence_task:
classifier_layers.extend(
[
nn.Linear(projection_size, projection_size, bias=use_bias),
nn.ReLU(),
]
)
classifier_layers.append(nn.Linear(projection_size, self.num_labels, bias=use_bias))
self.classifier = nn.Sequential(*classifier_layers)
def _forward_transformer(
self,
embeddings: torch.Tensor,
attention_mask: torch.Tensor | None,
output_attentions: bool | None,
output_hidden_states: bool | None,
) -> BaseModelOutput:
output_attentions = (
bool(output_attentions)
if output_attentions is not None
else bool(getattr(self.config, "output_attentions", False))
)
output_hidden_states = (
bool(output_hidden_states)
if output_hidden_states is not None
else bool(getattr(self.config, "output_hidden_states", False))
)
return self.transformer(
embeddings,
attention_mask,
output_attentions=output_attentions,
output_hidden_states=output_hidden_states,
return_dict=True,
)
class SequenceClassificationProbe(_ClassificationProbe):
"""Protify-style sequence classifier over externally supplied residue embeddings."""
def __init__(self, config: Any, input_size: int) -> None:
super().__init__(config, input_size, sequence_task=True)
def forward(
self,
embeddings: torch.Tensor,
attention_mask: torch.Tensor | None = None,
labels: torch.Tensor | None = None,
output_attentions: bool | None = None,
output_hidden_states: bool | None = None,
return_dict: bool | None = None,
) -> SequenceClassifierOutput | tuple[torch.Tensor, ...]:
if attention_mask is None:
attention_mask = torch.ones(
embeddings.shape[:2],
device=embeddings.device,
dtype=torch.bool,
)
outputs = self._forward_transformer(
embeddings,
attention_mask,
output_attentions,
output_hidden_states,
)
if self.pooler is None:
raise AssertionError("Sequence classification requires a configured pooler.")
pooled = self.pooler(outputs.last_hidden_state, attention_mask)
logits = self.classifier(pooled)
loss = None
if labels is not None:
problem_type = resolve_problem_type(self.config, labels, num_labels=self.num_labels)
loss = sequence_classification_loss(
logits,
labels,
problem_type=problem_type,
num_labels=self.num_labels,
)
result = SequenceClassifierOutput(
loss=loss,
logits=logits,
hidden_states=outputs.hidden_states,
attentions=outputs.attentions,
)
use_return_dict = (
bool(return_dict)
if return_dict is not None
else bool(getattr(self.config, "use_return_dict", True))
)
return result if use_return_dict else result.to_tuple()
class TokenClassificationProbe(_ClassificationProbe):
"""Protify-style residue classifier or regressor over supplied embeddings."""
def __init__(self, config: Any, input_size: int) -> None:
super().__init__(config, input_size, sequence_task=False)
def forward(
self,
embeddings: torch.Tensor,
attention_mask: torch.Tensor | None = None,
labels: torch.Tensor | None = None,
output_attentions: bool | None = None,
output_hidden_states: bool | None = None,
return_dict: bool | None = None,
) -> TokenClassifierOutput | tuple[torch.Tensor, ...]:
outputs = self._forward_transformer(
embeddings,
attention_mask,
output_attentions,
output_hidden_states,
)
logits = self.classifier(outputs.last_hidden_state)
loss = None
if labels is not None:
problem_type = resolve_problem_type(self.config, labels, num_labels=self.num_labels)
loss = token_classification_loss(
logits,
labels,
problem_type=problem_type,
num_labels=self.num_labels,
)
result = TokenClassifierOutput(
loss=loss,
logits=logits,
hidden_states=outputs.hidden_states,
attentions=outputs.attentions,
)
use_return_dict = (
bool(return_dict)
if return_dict is not None
else bool(getattr(self.config, "use_return_dict", True))
)
return result if use_return_dict else result.to_tuple()
__all__ = [
"ProbeSelfAttention",
"ProteinTransformerProbe",
"SequenceClassificationProbe",
"SwiGLU",
"TokenClassificationProbe",
"resolve_problem_type",
"sequence_classification_loss",
"token_classification_loss",
]
|