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 AutoTokenizer, AutoModel tokenizer = AutoTokenizer.from_pretrained("Synthyra/ESMFold2", trust_remote_code=True) model = AutoModel.from_pretrained("Synthyra/ESMFold2", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
Update FastPLMs files
Browse files- fastplms/attention/__init__.py +4 -0
- fastplms/attention/_core.py +24 -0
- fastplms/attention/interfaces.py +14 -7
- fastplms/models.toml +1 -1
- fastplms/models/esmfold2/configuration_esmfold2.py +4 -1
- fastplms_bundle.py +0 -0
- modeling_fastplms.py +1 -1
fastplms/attention/__init__.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
"""Shared attention backends, masks, and optional optimized kernels."""
|
| 2 |
|
| 3 |
from ._core import (
|
|
|
|
| 4 |
VALID_ATTENTION_BACKENDS,
|
| 5 |
AttentionBackend,
|
| 6 |
BlockMask,
|
|
@@ -11,6 +12,7 @@ from ._core import (
|
|
| 11 |
_kernels_flash_varlen_forward,
|
| 12 |
_unpad_input,
|
| 13 |
bool_to_additive_mask,
|
|
|
|
| 14 |
clear_flex_attention_caches,
|
| 15 |
create_block_mask,
|
| 16 |
flex_attention,
|
|
@@ -36,6 +38,7 @@ from .interfaces import (
|
|
| 36 |
__all__ = [
|
| 37 |
"FASTPLMS_ATTENTION_FUNCTIONS",
|
| 38 |
"FASTPLMS_ATTENTION_MASKS",
|
|
|
|
| 39 |
"VALID_ATTENTION_BACKENDS",
|
| 40 |
"AttentionBackend",
|
| 41 |
"BlockMask",
|
|
@@ -47,6 +50,7 @@ __all__ = [
|
|
| 47 |
"_kernels_flash_varlen_forward",
|
| 48 |
"_unpad_input",
|
| 49 |
"bool_to_additive_mask",
|
|
|
|
| 50 |
"clear_flex_attention_caches",
|
| 51 |
"create_block_mask",
|
| 52 |
"flex_attention",
|
|
|
|
| 1 |
"""Shared attention backends, masks, and optional optimized kernels."""
|
| 2 |
|
| 3 |
from ._core import (
|
| 4 |
+
LEGACY_CHECKPOINT_ATTENTION_BACKENDS,
|
| 5 |
VALID_ATTENTION_BACKENDS,
|
| 6 |
AttentionBackend,
|
| 7 |
BlockMask,
|
|
|
|
| 12 |
_kernels_flash_varlen_forward,
|
| 13 |
_unpad_input,
|
| 14 |
bool_to_additive_mask,
|
| 15 |
+
canonical_checkpoint_attention_backend,
|
| 16 |
clear_flex_attention_caches,
|
| 17 |
create_block_mask,
|
| 18 |
flex_attention,
|
|
|
|
| 38 |
__all__ = [
|
| 39 |
"FASTPLMS_ATTENTION_FUNCTIONS",
|
| 40 |
"FASTPLMS_ATTENTION_MASKS",
|
| 41 |
+
"LEGACY_CHECKPOINT_ATTENTION_BACKENDS",
|
| 42 |
"VALID_ATTENTION_BACKENDS",
|
| 43 |
"AttentionBackend",
|
| 44 |
"BlockMask",
|
|
|
|
| 50 |
"_kernels_flash_varlen_forward",
|
| 51 |
"_unpad_input",
|
| 52 |
"bool_to_additive_mask",
|
| 53 |
+
"canonical_checkpoint_attention_backend",
|
| 54 |
"clear_flex_attention_caches",
|
| 55 |
"create_block_mask",
|
| 56 |
"flex_attention",
|
fastplms/attention/_core.py
CHANGED
|
@@ -13,6 +13,7 @@ from collections import OrderedDict
|
|
| 13 |
from collections.abc import Callable
|
| 14 |
from enum import Enum
|
| 15 |
from threading import RLock
|
|
|
|
| 16 |
from einops import rearrange
|
| 17 |
from torch.nn import functional as F
|
| 18 |
|
|
@@ -623,6 +624,29 @@ class AttentionBackend(str, Enum): # noqa: UP042
|
|
| 623 |
|
| 624 |
VALID_ATTENTION_BACKENDS = tuple(b.value for b in AttentionBackend)
|
| 625 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 626 |
|
| 627 |
def warn_attention_backend_fallback(
|
| 628 |
requested_backend: str | AttentionBackend,
|
|
|
|
| 13 |
from collections.abc import Callable
|
| 14 |
from enum import Enum
|
| 15 |
from threading import RLock
|
| 16 |
+
from types import MappingProxyType
|
| 17 |
from einops import rearrange
|
| 18 |
from torch.nn import functional as F
|
| 19 |
|
|
|
|
| 624 |
|
| 625 |
VALID_ATTENTION_BACKENDS = tuple(b.value for b in AttentionBackend)
|
| 626 |
|
| 627 |
+
# Official upstream sources predate the Transformers ``flex_attention`` name and
|
| 628 |
+
# store ``"flex"`` in their checkpoint configurations. The two names select the
|
| 629 |
+
# identical implementation, so translating one to the other is a rename rather
|
| 630 |
+
# than a backend substitution. ``"flash"`` is deliberately absent: it does not
|
| 631 |
+
# identify a FlashAttention version, and guessing one would be a substitution.
|
| 632 |
+
LEGACY_CHECKPOINT_ATTENTION_BACKENDS = MappingProxyType(
|
| 633 |
+
{"flex": AttentionBackend.FLEX_ATTENTION.value}
|
| 634 |
+
)
|
| 635 |
+
|
| 636 |
+
|
| 637 |
+
def canonical_checkpoint_attention_backend(stored_backend: str | None) -> str | None:
|
| 638 |
+
"""Translate a stored checkpoint backend name to its canonical spelling.
|
| 639 |
+
|
| 640 |
+
Call this where a serialized configuration enters FastPLMs. Imperative
|
| 641 |
+
selection through ``set_attn_implementation`` stays strict and keeps
|
| 642 |
+
rejecting the historical spellings. An unmapped name passes through so the
|
| 643 |
+
backend resolver owns the rejection.
|
| 644 |
+
"""
|
| 645 |
+
|
| 646 |
+
if stored_backend is None:
|
| 647 |
+
return None
|
| 648 |
+
return LEGACY_CHECKPOINT_ATTENTION_BACKENDS.get(stored_backend, stored_backend)
|
| 649 |
+
|
| 650 |
|
| 651 |
def warn_attention_backend_fallback(
|
| 652 |
requested_backend: str | AttentionBackend,
|
fastplms/attention/interfaces.py
CHANGED
|
@@ -10,6 +10,7 @@ from transformers import AttentionInterface, AttentionMaskInterface
|
|
| 10 |
|
| 11 |
from ._core import (
|
| 12 |
AttentionBackend,
|
|
|
|
| 13 |
get_attn_implementation,
|
| 14 |
kernels_flash_attention_func,
|
| 15 |
resolve_attention_backend,
|
|
@@ -146,26 +147,32 @@ class FastPLMsAttentionMixin:
|
|
| 146 |
def __init__(self, config, *args: Any, **kwargs: Any) -> None:
|
| 147 |
sentinel = object()
|
| 148 |
internal = getattr(config, "_attn_implementation_internal", sentinel)
|
| 149 |
-
|
| 150 |
getattr(config, "_attn_implementation", None) if internal is sentinel else internal
|
| 151 |
)
|
| 152 |
legacy = getattr(config, "attn_backend", None)
|
| 153 |
-
requested =
|
| 154 |
if requested is not None:
|
| 155 |
if not isinstance(requested, str):
|
| 156 |
raise TypeError(
|
| 157 |
"The configured attention implementation must be a string or None; "
|
| 158 |
f"received {type(requested).__name__}."
|
| 159 |
)
|
| 160 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
# ``PreTrainedModel.__init__`` resolves a missing Transformers
|
| 162 |
# implementation to the family default. Legacy FastPLMs configs
|
| 163 |
# persist their explicit choice in ``attn_backend``, so forward it
|
| 164 |
# into the canonical Transformers field before the base class can
|
| 165 |
-
# replace it with SDPA. A
|
| 166 |
-
#
|
| 167 |
-
|
| 168 |
-
|
|
|
|
| 169 |
super().__init__(config, *args, **kwargs)
|
| 170 |
# Transformers resolves an unspecified implementation during the base
|
| 171 |
# model initialization. Synchronize that choice before family layers
|
|
|
|
| 10 |
|
| 11 |
from ._core import (
|
| 12 |
AttentionBackend,
|
| 13 |
+
canonical_checkpoint_attention_backend,
|
| 14 |
get_attn_implementation,
|
| 15 |
kernels_flash_attention_func,
|
| 16 |
resolve_attention_backend,
|
|
|
|
| 147 |
def __init__(self, config, *args: Any, **kwargs: Any) -> None:
|
| 148 |
sentinel = object()
|
| 149 |
internal = getattr(config, "_attn_implementation_internal", sentinel)
|
| 150 |
+
stored = (
|
| 151 |
getattr(config, "_attn_implementation", None) if internal is sentinel else internal
|
| 152 |
)
|
| 153 |
legacy = getattr(config, "attn_backend", None)
|
| 154 |
+
requested = stored if stored is not None else legacy
|
| 155 |
if requested is not None:
|
| 156 |
if not isinstance(requested, str):
|
| 157 |
raise TypeError(
|
| 158 |
"The configured attention implementation must be a string or None; "
|
| 159 |
f"received {type(requested).__name__}."
|
| 160 |
)
|
| 161 |
+
# A serialized configuration can name a backend with the historical
|
| 162 |
+
# spelling used by the official source it was converted from. That
|
| 163 |
+
# names the same implementation, so translate it here rather than
|
| 164 |
+
# rejecting a checkpoint that asked for an implementation FastPLMs has.
|
| 165 |
+
canonical = canonical_checkpoint_attention_backend(requested)
|
| 166 |
+
self._validate_attention_name(canonical)
|
| 167 |
# ``PreTrainedModel.__init__`` resolves a missing Transformers
|
| 168 |
# implementation to the family default. Legacy FastPLMs configs
|
| 169 |
# persist their explicit choice in ``attn_backend``, so forward it
|
| 170 |
# into the canonical Transformers field before the base class can
|
| 171 |
+
# replace it with SDPA. A stored canonical value already agrees and
|
| 172 |
+
# is left untouched, including an explicit
|
| 173 |
+
# ``attn_implementation=...`` load override.
|
| 174 |
+
if canonical != stored:
|
| 175 |
+
set_config_attn_implementation(config, canonical)
|
| 176 |
super().__init__(config, *args, **kwargs)
|
| 177 |
# Transformers resolves an unspecified implementation during the base
|
| 178 |
# model initialization. Synchronize that choice before family layers
|
fastplms/models.toml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
schema_version = 1
|
| 2 |
legal_files = [
|
| 3 |
"LICENSE=sha256:2d2b50c7b1414bff1189a1db1f0cfb92e3e064b50f4c2b1019827b683e1b629a",
|
| 4 |
-
"THIRD_PARTY_NOTICES.md=sha256:
|
| 5 |
]
|
| 6 |
|
| 7 |
[[attention_kernels]]
|
|
|
|
| 1 |
schema_version = 1
|
| 2 |
legal_files = [
|
| 3 |
"LICENSE=sha256:2d2b50c7b1414bff1189a1db1f0cfb92e3e064b50f4c2b1019827b683e1b629a",
|
| 4 |
+
"THIRD_PARTY_NOTICES.md=sha256:d35e506b728868b52290672d54a89c6aade09529af99dbc8a4db72d9e9ca3460",
|
| 5 |
]
|
| 6 |
|
| 7 |
[[attention_kernels]]
|
fastplms/models/esmfold2/configuration_esmfold2.py
CHANGED
|
@@ -21,6 +21,9 @@ from typing import Any, TypeVar, cast
|
|
| 21 |
|
| 22 |
from transformers.configuration_utils import PretrainedConfig
|
| 23 |
|
|
|
|
|
|
|
|
|
|
| 24 |
_ESMC_ATTENTION_IMPLEMENTATIONS = frozenset({"eager", "flex_attention", "sdpa"})
|
| 25 |
_ESMC_PRECISIONS = frozenset({"auto", "bf16", "fp32", "fp8"})
|
| 26 |
|
|
@@ -56,7 +59,7 @@ def normalize_esmc_attention_implementation(
|
|
| 56 |
"ESMFold2 has one ESMC attention backbone; use a string or {'': implementation}."
|
| 57 |
)
|
| 58 |
implementation = implementation[""]
|
| 59 |
-
canonical =
|
| 60 |
if canonical is not None and canonical not in _ESMC_ATTENTION_IMPLEMENTATIONS:
|
| 61 |
expected = sorted(_ESMC_ATTENTION_IMPLEMENTATIONS)
|
| 62 |
raise ValueError(
|
|
|
|
| 21 |
|
| 22 |
from transformers.configuration_utils import PretrainedConfig
|
| 23 |
|
| 24 |
+
from fastplms.attention import canonical_checkpoint_attention_backend
|
| 25 |
+
|
| 26 |
+
|
| 27 |
_ESMC_ATTENTION_IMPLEMENTATIONS = frozenset({"eager", "flex_attention", "sdpa"})
|
| 28 |
_ESMC_PRECISIONS = frozenset({"auto", "bf16", "fp32", "fp8"})
|
| 29 |
|
|
|
|
| 59 |
"ESMFold2 has one ESMC attention backbone; use a string or {'': implementation}."
|
| 60 |
)
|
| 61 |
implementation = implementation[""]
|
| 62 |
+
canonical = canonical_checkpoint_attention_backend(implementation)
|
| 63 |
if canonical is not None and canonical not in _ESMC_ATTENTION_IMPLEMENTATIONS:
|
| 64 |
expected = sorted(_ESMC_ATTENTION_IMPLEMENTATIONS)
|
| 65 |
raise ValueError(
|
fastplms_bundle.py
CHANGED
|
The diff for this file is too large to render.
See raw diff
|
|
|
modeling_fastplms.py
CHANGED
|
@@ -13,7 +13,7 @@ from zipfile import ZIP_DEFLATED, ZipFile
|
|
| 13 |
|
| 14 |
from .fastplms_bundle import RUNTIME_DATA, RUNTIME_HASH
|
| 15 |
|
| 16 |
-
if RUNTIME_HASH != "
|
| 17 |
raise RuntimeError("FastPLMs runtime identity differs from the bridge.")
|
| 18 |
|
| 19 |
_RUNTIME_TEMPORARIES = []
|
|
|
|
| 13 |
|
| 14 |
from .fastplms_bundle import RUNTIME_DATA, RUNTIME_HASH
|
| 15 |
|
| 16 |
+
if RUNTIME_HASH != "c76f023da79d5db93093291f4f3858a0e6cde4d9b4fa61ade779cf941f2175da":
|
| 17 |
raise RuntimeError("FastPLMs runtime identity differs from the bridge.")
|
| 18 |
|
| 19 |
_RUNTIME_TEMPORARIES = []
|