lhallee commited on
Commit
883e118
·
verified ·
1 Parent(s): a497a44

Update FastPLMs files

Browse files
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
- canonical = (
150
  getattr(config, "_attn_implementation", None) if internal is sentinel else internal
151
  )
152
  legacy = getattr(config, "attn_backend", None)
153
- requested = canonical if canonical is not None else legacy
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
- self._validate_attention_name(requested)
 
 
 
 
 
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 non-None canonical value still wins,
166
- # including an explicit ``attn_implementation=...`` load override.
167
- if canonical is None and legacy is not None:
168
- set_config_attn_implementation(config, legacy)
 
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:25704b3c76404696cae52e7fca13088d329f70f412687340351259e86cd62baa",
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_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 != "18aa093a660db92f9f4d1a4fd6602cbfbe7c6dfd3a5c93ce3d3bb5a34ee93c22":
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 != "1d1e93d324d699f48e94cf425f436d33b1b5e76ce96735a80a0dbb9727d5a26f":
17
  raise RuntimeError("FastPLMs runtime identity differs from the bridge.")
18
 
19
  _RUNTIME_TEMPORARIES = []