openfold3-accel 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,153 @@
1
+ """Drop-in CPU-oriented inference acceleration for OpenFold3.
2
+
3
+ Usage::
4
+
5
+ import openfold3_accel # activates automatically (set OPENFOLD3_ACCEL_DISABLE=1 to prevent)
6
+ import openfold3 # (order does not matter; patches resolve lazily)
7
+
8
+ See :func:`status`, :func:`enable` and :func:`disable`.
9
+ """
10
+
11
+ from __future__ import annotations
12
+
13
+ import contextlib
14
+ import os
15
+ from collections.abc import Iterable, Iterator
16
+ from dataclasses import dataclass, field
17
+
18
+ from . import _compat, _patch, _selfcheck
19
+ from .patches import DEFAULT_GROUPS, group_defs
20
+
21
+ try:
22
+ from importlib.metadata import version as _version
23
+
24
+ __version__ = _version("openfold3-accel")
25
+ except Exception: # noqa: BLE001
26
+ __version__ = "0.0.0+unknown"
27
+
28
+ __all__ = [
29
+ "enable",
30
+ "disable",
31
+ "status",
32
+ "Status",
33
+ "enabled",
34
+ "disabled",
35
+ "groups",
36
+ "__version__",
37
+ ]
38
+
39
+
40
+ def groups() -> dict[str, str]:
41
+ """Available patch groups and a one-line description of each."""
42
+ return {name: summary for name, (summary, _make) in group_defs().items()}
43
+
44
+
45
+ @dataclass(frozen=True)
46
+ class Status:
47
+ """Snapshot returned by :func:`status`."""
48
+
49
+ enabled: bool
50
+ reason: str
51
+ openfold3_version: str | None
52
+ openfold3_supported: bool
53
+ openfold3_supported_range: str
54
+ torch_version: str
55
+ groups: dict[str, str] = field(default_factory=dict)
56
+ patches: dict[str, str] = field(default_factory=dict)
57
+
58
+ def __str__(self) -> str:
59
+ lines = [
60
+ f"openfold3_accel {__version__}: {'ENABLED' if self.enabled else 'disabled'} ({self.reason})",
61
+ f" openfold3: {self.openfold3_version} (supported {self.openfold3_supported_range}: "
62
+ f"{self.openfold3_supported})",
63
+ f" torch: {self.torch_version}",
64
+ ]
65
+ lines += [f" group {k}: {v}" for k, v in self.groups.items()]
66
+ lines += [f" patch {k}: {v}" for k, v in self.patches.items()]
67
+ return "\n".join(lines)
68
+
69
+
70
+ def _selected(selection: Iterable[str] | None) -> list[str]:
71
+ avail = list(group_defs())
72
+ if selection is not None:
73
+ names = list(selection)
74
+ else:
75
+ env = os.environ.get("OPENFOLD3_ACCEL_GROUPS", "")
76
+ names = [g.strip() for g in env.split(",") if g.strip()] or list(DEFAULT_GROUPS)
77
+ skip = {
78
+ g.strip() for g in os.environ.get("OPENFOLD3_ACCEL_SKIP", "").split(",") if g.strip()
79
+ }
80
+ names = [g for g in names if g not in skip]
81
+ unknown = [g for g in names if g not in avail]
82
+ if unknown:
83
+ raise ValueError(f"unknown patch group(s) {unknown}; available: {avail}")
84
+ return names
85
+
86
+
87
+ def enable(groups: Iterable[str] | None = None) -> Status:
88
+ """Apply the optimisations (idempotent). Unsupported setups are left untouched and reported.
89
+
90
+ Parameters
91
+ ----------
92
+ groups
93
+ Subset of :func:`groups` to enable. Default: all (honouring ``OPENFOLD3_ACCEL_GROUPS`` and
94
+ ``OPENFOLD3_ACCEL_SKIP``).
95
+ """
96
+ _patch.apply(group_defs(), _selected(groups), _selfcheck.run)
97
+ return status()
98
+
99
+
100
+ def disable(groups: Iterable[str] | None = None) -> Status:
101
+ """Restore the original OpenFold3 callables (removing stacked wrappers in a safe order)."""
102
+ for w in _patch.revert(groups):
103
+ import warnings
104
+
105
+ warnings.warn(w, RuntimeWarning, stacklevel=2)
106
+ return status()
107
+
108
+
109
+ @contextlib.contextmanager
110
+ def disabled(groups: Iterable[str] | None = None) -> Iterator[None]:
111
+ """Temporarily run upstream OpenFold3 code (e.g. for A/B comparisons), then re-enable."""
112
+ before = [n for n, g in _patch.STATE.groups.items() if g.state == "active"]
113
+ names = before if groups is None else [n for n in groups if n in before]
114
+ disable(names)
115
+ try:
116
+ yield
117
+ finally:
118
+ enable(names)
119
+
120
+
121
+ @contextlib.contextmanager
122
+ def enabled(groups: Iterable[str] | None = None) -> Iterator[None]:
123
+ """Temporarily enable (and afterwards restore) the optimisations."""
124
+ before = {n for n, g in _patch.STATE.groups.items() if g.state == "active"}
125
+ enable(groups)
126
+ try:
127
+ yield
128
+ finally:
129
+ now = [n for n, g in _patch.STATE.groups.items() if g.state == "active" and n not in before]
130
+ disable(now)
131
+
132
+
133
+ def status() -> Status:
134
+ import torch
135
+
136
+ v = _compat.openfold3_version()
137
+ ok, _ = _compat.version_supported(v)
138
+ lo = ".".join(map(str, _compat.MIN_OPENFOLD3))
139
+ hi = ".".join(map(str, _compat.MAX_OPENFOLD3_EXCLUSIVE))
140
+ return Status(
141
+ enabled=_patch.STATE.enabled,
142
+ reason=_patch.STATE.reason,
143
+ openfold3_version=v,
144
+ openfold3_supported=ok,
145
+ openfold3_supported_range=f">={lo},<{hi}",
146
+ torch_version=torch.__version__,
147
+ groups={k: g.state for k, g in _patch.STATE.groups.items()},
148
+ patches={p.name: p.state for g in _patch.STATE.groups.values() for p in g.patches},
149
+ )
150
+
151
+
152
+ if os.environ.get("OPENFOLD3_ACCEL_DISABLE", "") not in ("1", "true", "True", "yes"):
153
+ enable()
@@ -0,0 +1,202 @@
1
+ """Rollout-scoped memoisation of tensors that do not change between denoising steps.
2
+
3
+ OpenFold3's diffusion sampler calls the denoiser ~200 times per sample with *identical* trunk
4
+ conditioning (``zij``, ``si_trunk``, reference-conformer features, masks). Only the noisy
5
+ coordinates and the noise-level embedding change. Many sub-expressions of the denoiser depend only
6
+ on the static inputs (pair biases, ``LayerNorm(z)``, the atom-pair MLP, the ``s``-dependent half of
7
+ every atom-level adaptive layer-norm, ...). They are recomputed on every step by upstream code.
8
+
9
+ This module provides the small amount of machinery that lets patched layers compute such terms
10
+ once per rollout:
11
+
12
+ * a :class:`RolloutCache` that is *active only inside* a patched ``SampleDiffusion._sample_rollout``
13
+ (inference, ``torch.no_grad``), so training, activation checkpointing and any other use of the
14
+ layers is untouched;
15
+ * a registry of tensors known to be static (matched by object identity *and* ``_version`` so
16
+ in-place edits invalidate), and a byte budget so that very large inputs degrade gracefully
17
+ (entries that do not fit are simply recomputed, exactly as upstream does).
18
+ """
19
+
20
+ from __future__ import annotations
21
+
22
+ import contextlib
23
+ import contextvars
24
+ import os
25
+ from collections.abc import Callable, Iterator
26
+ from typing import Any
27
+
28
+ import torch
29
+ from torch import Tensor
30
+
31
+ _ACTIVE: contextvars.ContextVar[RolloutCache | None] = contextvars.ContextVar(
32
+ "openfold3_accel_rollout_cache", default=None
33
+ )
34
+
35
+ #: Fraction of *available* RAM the cache may use when no explicit budget is configured.
36
+ _RAM_FRACTION = 0.25
37
+ _MAX_DEFAULT_BYTES = 8 * 2**30
38
+
39
+
40
+ def _available_ram_bytes() -> int | None:
41
+ try:
42
+ with open("/proc/meminfo") as fh:
43
+ for line in fh:
44
+ if line.startswith("MemAvailable:"):
45
+ return int(line.split()[1]) * 1024
46
+ except OSError:
47
+ pass
48
+ try:
49
+ return os.sysconf("SC_PAGE_SIZE") * os.sysconf("SC_AVPHYS_PAGES")
50
+ except (ValueError, OSError, AttributeError):
51
+ return None
52
+
53
+
54
+ def default_budget_bytes() -> int:
55
+ """Cache budget: ``OPENFOLD3_ACCEL_CACHE_MB`` if set, else 25% of available RAM (max 8 GiB)."""
56
+ env = os.environ.get("OPENFOLD3_ACCEL_CACHE_MB")
57
+ if env:
58
+ try:
59
+ return max(0, int(float(env) * 2**20))
60
+ except ValueError:
61
+ pass
62
+ avail = _available_ram_bytes()
63
+ if avail is None:
64
+ return 2 * 2**30
65
+ return int(min(_MAX_DEFAULT_BYTES, avail * _RAM_FRACTION))
66
+
67
+
68
+ def _nbytes(value: Any) -> int:
69
+ if isinstance(value, Tensor):
70
+ return value.numel() * value.element_size()
71
+ if isinstance(value, tuple | list):
72
+ return sum(_nbytes(v) for v in value)
73
+ return 0
74
+
75
+
76
+ def _tensors(value: Any) -> Iterator[Tensor]:
77
+ if isinstance(value, Tensor):
78
+ yield value
79
+ elif isinstance(value, tuple | list):
80
+ for v in value:
81
+ yield from _tensors(v)
82
+
83
+
84
+ def _ver(t: Tensor) -> int:
85
+ """Version counter of ``t``; -1 for inference tensors, which do not track one.
86
+
87
+ Inference tensors (created under ``torch.inference_mode()``, e.g. by Lightning's ``Trainer.predict``)
88
+ raise on ``_version``. They cannot be modified outside inference mode, and OpenFold3 never mutates
89
+ the rollout's conditioning tensors in place, so identity alone is used for them.
90
+ """
91
+ try:
92
+ return t._version
93
+ except RuntimeError:
94
+ return -1
95
+
96
+
97
+ class RolloutCache:
98
+ """Per-rollout memo table. Not thread-safe by design: one cache lives in one context."""
99
+
100
+ def __init__(self, budget_bytes: int | None = None):
101
+ self.budget = default_budget_bytes() if budget_bytes is None else budget_bytes
102
+ self.used = 0
103
+ self.hits = 0
104
+ self.misses = 0
105
+ self.overflow = 0
106
+ self.by_key: dict[str, list[int]] = {}
107
+ self._static: dict[int, tuple[Tensor, int]] = {}
108
+ self._memo: dict[Any, Any] = {}
109
+
110
+ # -- static-tensor registry ---------------------------------------------------------------
111
+ def mark_static(self, *tensors: Tensor | None) -> None:
112
+ """Declare tensors constant for the lifetime of the rollout (strong refs are kept)."""
113
+ for t in tensors:
114
+ if isinstance(t, Tensor):
115
+ self._static[id(t)] = (t, _ver(t))
116
+
117
+ def is_static(self, t: Tensor | None) -> bool:
118
+ if not isinstance(t, Tensor):
119
+ return False
120
+ entry = self._static.get(id(t))
121
+ return entry is not None and entry[0] is t and entry[1] == _ver(t)
122
+
123
+ # -- memoisation ----------------------------------------------------------------------------
124
+ def memo(self, key: Any, compute: Callable[[], Any], *, mark: bool = True) -> Any:
125
+ """Return ``compute()``, computing it at most once per rollout (budget permitting).
126
+
127
+ Tensors in the result are registered as static so that downstream layers can memoise on
128
+ them too. When the budget is exhausted the value is returned uncached and *not*
129
+ registered, so everything derived from it is also recomputed - i.e. upstream behaviour.
130
+ """
131
+ name = str(key[0]) if isinstance(key, tuple) else str(key)
132
+ counts = self.by_key.setdefault(name, [0, 0])
133
+ if key in self._memo:
134
+ self.hits += 1
135
+ counts[0] += 1
136
+ return self._memo[key]
137
+ self.misses += 1
138
+ counts[1] += 1
139
+ value = compute()
140
+ nb = _nbytes(value)
141
+ if self.used + nb > self.budget:
142
+ self.overflow += 1
143
+ return value
144
+ self.used += nb
145
+ self._memo[key] = value
146
+ if mark:
147
+ self.mark_static(*_tensors(value))
148
+ return value
149
+
150
+ def stats(self) -> dict:
151
+ return dict(
152
+ by_key={k: tuple(v) for k, v in self.by_key.items()},
153
+ hits=self.hits,
154
+ misses=self.misses,
155
+ overflow=self.overflow,
156
+ used_bytes=self.used,
157
+ budget_bytes=self.budget,
158
+ )
159
+
160
+
161
+ def _exporting() -> bool:
162
+ """True while tracing / exporting / compiling (ONNX, TorchScript, ``torch.export``, ``torch.compile``).
163
+
164
+ Identity-keyed memoisation is meaningless inside a trace, so upstream code runs instead.
165
+ """
166
+ try:
167
+ if torch.onnx.is_in_onnx_export() or torch.jit.is_tracing():
168
+ return True
169
+ except Exception: # noqa: BLE001
170
+ pass
171
+ for name in ("is_exporting", "is_compiling"):
172
+ f = getattr(torch.compiler, name, None)
173
+ try:
174
+ if f is not None and f():
175
+ return True
176
+ except Exception: # noqa: BLE001
177
+ pass
178
+ return False
179
+
180
+
181
+ def active() -> RolloutCache | None:
182
+ """The cache of the rollout currently executing in this context, if any (and only at inference)."""
183
+ c = _ACTIVE.get()
184
+ if c is None or torch.is_grad_enabled() or _exporting():
185
+ return None
186
+ return c
187
+
188
+
189
+ @contextlib.contextmanager
190
+ def rollout_scope(budget_bytes: int | None = None) -> Iterator[RolloutCache]:
191
+ """Activate a fresh :class:`RolloutCache`; it is dropped (freeing its memory) on exit."""
192
+ cache = RolloutCache(budget_bytes)
193
+ token = _ACTIVE.set(cache)
194
+ try:
195
+ yield cache
196
+ finally:
197
+ _ACTIVE.reset(token)
198
+ LAST_STATS.update(cache.stats())
199
+
200
+
201
+ #: Statistics of the most recently completed rollout (for diagnostics / benchmarks).
202
+ LAST_STATS: dict[str, int] = {}
@@ -0,0 +1,84 @@
1
+ """Version gating and source fingerprints for patch targets.
2
+
3
+ OpenFold3's internals are private and change between releases. Instead of trusting a version range
4
+ alone, every patch is applied only if
5
+
6
+ * the installed ``openfold3`` is inside the tested range, **and**
7
+ * the whitespace-normalised source of the exact function being replaced matches a fingerprint that
8
+ the replacement has been verified against.
9
+
10
+ Anything else is left untouched (the original OpenFold3 code runs) and is reported by
11
+ :func:`openfold3_accel.status`. Regenerate the table for new releases with
12
+ ``python scripts/fingerprint_versions.py`` after reading the upstream diff of each target.
13
+ """
14
+
15
+ from __future__ import annotations
16
+
17
+ import hashlib
18
+ import inspect
19
+ import re
20
+ from importlib import metadata
21
+
22
+ #: Inclusive lower / exclusive upper bound of OpenFold3 releases this package is tested with.
23
+ MIN_OPENFOLD3 = (0, 5, 0)
24
+ MAX_OPENFOLD3_EXCLUSIVE = (0, 6, 0)
25
+
26
+ #: Known-good source fingerprints per patch target (see ``scripts/fingerprint_versions.py``).
27
+ #: ``dev`` fingerprints are those of the commit pinned in ``OPENFOLD3_COMMIT``.
28
+ FINGERPRINTS: dict[str, frozenset[str]] = { # openfold3 0.5.0 and 0.5.1 (identical)
29
+ "SampleDiffusion._sample_rollout": frozenset({"8bd6b31fdcda"}),
30
+ "DiffusionConditioning.forward": frozenset({"f087b75e4de9"}),
31
+ "DiffusionTransformer.forward": frozenset({"595d4303fea0"}),
32
+ "DiffusionAttentionPairBias._prep_bias": frozenset({"bdc267d1d3cf"}),
33
+ "AdaLN.forward": frozenset({"7757a8da48bc"}),
34
+ "ConditionedTransitionBlock._transition": frozenset({"ad932f303448"}),
35
+ "AtomAttentionEncoder.get_atom_reps": frozenset({"6fea8030df81"}),
36
+ "CrossAttentionPairBias.forward": frozenset({"0f7d5d1d435d"}),
37
+ "SampleDiffusion._sample_rollout[bf16]": frozenset({"8bd6b31fdcda"}),
38
+ "DiffusionModule.forward": frozenset({"8f7737923ccd"}),
39
+ "attention._attention": frozenset({"bfffc382fcae"}),
40
+ "initialization.trunc_normal_init_": frozenset({"a7a22ae83444"}),
41
+ }
42
+
43
+
44
+ def fingerprint(fn) -> str | None:
45
+ """Whitespace-insensitive SHA-256 prefix of ``fn``'s source, or ``None`` if unavailable."""
46
+ try:
47
+ src = inspect.getsource(fn)
48
+ except (OSError, TypeError):
49
+ return None
50
+ return fingerprint_source(src)
51
+
52
+
53
+ def fingerprint_source(src: str) -> str:
54
+ return hashlib.sha256(re.sub(r"\s+", "", src).encode()).hexdigest()[:12]
55
+
56
+
57
+ def parse_version(v: str) -> tuple[int, ...]:
58
+ parts = []
59
+ for tok in re.split(r"[.+\-]", v):
60
+ m = re.match(r"\d+", tok)
61
+ if not m:
62
+ break
63
+ parts.append(int(m.group()))
64
+ return tuple(parts)
65
+
66
+
67
+ def openfold3_version() -> str | None:
68
+ try:
69
+ return metadata.version("openfold3")
70
+ except metadata.PackageNotFoundError:
71
+ return None
72
+
73
+
74
+ def version_supported(v: str | None) -> tuple[bool, str]:
75
+ if v is None:
76
+ return False, "openfold3 is not installed"
77
+ t = parse_version(v)
78
+ if not t:
79
+ return False, f"unparseable openfold3 version {v!r}"
80
+ if t < MIN_OPENFOLD3 or t >= MAX_OPENFOLD3_EXCLUSIVE:
81
+ lo = ".".join(map(str, MIN_OPENFOLD3))
82
+ hi = ".".join(map(str, MAX_OPENFOLD3_EXCLUSIVE))
83
+ return False, f"openfold3 {v} is outside the tested range [{lo}, {hi})"
84
+ return True, "ok"
@@ -0,0 +1,210 @@
1
+ """Reversible, idempotent, group-wise patching of OpenFold3 internals.
2
+
3
+ Design
4
+ ------
5
+ * A *patch* replaces one attribute (a method on a class, or a function in a module) with a
6
+ replacement built from the original by a *factory*.
7
+ * Patches are organised in *groups* (``diffusion_cache``, ``attention``, ...). A group is applied
8
+ atomically: every target of the group must match a verified source fingerprint, otherwise the
9
+ whole group is left untouched and the reason is reported by :func:`openfold3_accel.status`.
10
+ * After applying a group, a numerical self-check compares the replacement with the original on
11
+ tiny random modules. A failing check rolls the group back.
12
+ * :func:`revert` restores the *exact original function objects* (unless somebody else has wrapped
13
+ them since, in which case it warns and leaves them).
14
+
15
+ Nothing here touches model parameters, buffers, ``state_dict`` layout, RNG state or thread counts.
16
+ """
17
+
18
+ from __future__ import annotations
19
+
20
+ import itertools
21
+ import threading
22
+ from collections.abc import Callable, Iterable
23
+ from dataclasses import dataclass, field
24
+ from typing import Any
25
+
26
+ from . import _compat
27
+
28
+ _LOCK = threading.RLock()
29
+
30
+
31
+ @dataclass
32
+ class Patch:
33
+ """One replaced attribute.
34
+
35
+ ``spec`` is ``(module, class_or_None, attribute)``; it is also what
36
+ ``scripts/fingerprint_versions.py`` uses to fingerprint upstream sources without importing them.
37
+ """
38
+
39
+ name: str
40
+ group: str
41
+ spec: tuple[str, str | None, str]
42
+ factory: Callable[[Callable], Callable]
43
+ state: str = "inactive"
44
+ original: Callable | None = None
45
+ replacement: Callable | None = None
46
+ owner: Any = None
47
+ attr: str | None = None
48
+
49
+ def resolve(self) -> tuple[Any, str]:
50
+ import importlib
51
+
52
+ module, cls, attr = self.spec
53
+ owner = importlib.import_module(module)
54
+ return (getattr(owner, cls) if cls else owner), attr
55
+
56
+
57
+ @dataclass
58
+ class GroupState:
59
+ name: str
60
+ summary: str
61
+ state: str = "inactive" # inactive | active | skipped: <why>
62
+ patches: list[Patch] = field(default_factory=list)
63
+ seq: int = (
64
+ -1
65
+ ) # application order; wrappers stacked on one attribute must be removed in reverse order
66
+
67
+
68
+ _SEQ = itertools.count()
69
+
70
+
71
+ @dataclass
72
+ class _State:
73
+ reason: str = "not yet enabled"
74
+ groups: dict[str, GroupState] = field(default_factory=dict)
75
+
76
+ @property
77
+ def enabled(self) -> bool:
78
+ return any(g.state == "active" for g in self.groups.values())
79
+
80
+
81
+ STATE = _State()
82
+
83
+
84
+ def _rollback(patches: Iterable[Patch]) -> None:
85
+ for p in patches:
86
+ if p.state == "active" and p.owner is not None and p.original is not None:
87
+ if p.owner.__dict__.get(p.attr) is p.replacement:
88
+ setattr(p.owner, p.attr, p.original)
89
+ p.state = "inactive"
90
+
91
+
92
+ def _apply_group(group: GroupState, selfcheck: Callable[[str], str | None] | None) -> None:
93
+ patches = group.patches
94
+ try:
95
+ for p in patches:
96
+ owner, attr = p.resolve()
97
+ p.owner, p.attr = owner, attr
98
+ fn = owner.__dict__.get(attr)
99
+ if fn is None:
100
+ group.state = f"skipped: {p.name} is not defined where expected"
101
+ return
102
+ fp = _compat.fingerprint(fn)
103
+ if fp is None:
104
+ group.state = f"skipped: source of {p.name} unavailable, cannot verify"
105
+ return
106
+ if fp not in _compat.FINGERPRINTS.get(p.name, frozenset()):
107
+ group.state = f"skipped: unrecognised source fingerprint {fp} for {p.name}"
108
+ return
109
+ p.original = fn
110
+ # every target verified: now (and only now) install the replacements
111
+ for p in patches:
112
+ p.replacement = p.factory(p.original)
113
+ setattr(p.owner, p.attr, p.replacement)
114
+ p.state = "active"
115
+ except Exception as exc: # noqa: BLE001 - never break the user's import
116
+ _rollback(patches)
117
+ group.state = f"skipped: patching failed: {exc!r}"
118
+ return
119
+ problem = None
120
+ if selfcheck is not None:
121
+ try:
122
+ problem = selfcheck(group.name)
123
+ except Exception as exc: # noqa: BLE001
124
+ problem = f"self-check raised {exc!r}"
125
+ if problem:
126
+ _rollback(patches)
127
+ group.state = f"skipped: self-check failed: {problem}"
128
+ return
129
+ group.state = "active"
130
+ group.seq = next(_SEQ)
131
+
132
+
133
+ def apply(
134
+ group_defs: dict[str, tuple[str, Callable[[], list[Patch]]]],
135
+ selected: Iterable[str],
136
+ selfcheck: Callable[[str], str | None] | None,
137
+ ) -> None:
138
+ """Apply the selected groups (idempotent per group)."""
139
+ with _LOCK:
140
+ v = _compat.openfold3_version()
141
+ ok, why = _compat.version_supported(v)
142
+ if not ok:
143
+ STATE.reason = why
144
+ return
145
+ for name in selected:
146
+ summary, make = group_defs[name]
147
+ g = STATE.groups.get(name)
148
+ if g is not None and g.state == "active":
149
+ continue
150
+ g = GroupState(name=name, summary=summary, patches=make())
151
+ STATE.groups[name] = g
152
+ _apply_group(g, selfcheck)
153
+ n_active = sum(1 for g in STATE.groups.values() if g.state == "active")
154
+ STATE.reason = "ok" if n_active else "no patch group matched a verified fingerprint"
155
+
156
+
157
+ def revert(groups: Iterable[str] | None = None) -> list[str]:
158
+ """Restore original callables; returns warnings for patches that others re-wrapped.
159
+
160
+ Groups may wrap the *same* attribute (``bf16_diffusion`` wraps ``diffusion_cache``'s rollout). A wrapper
161
+ can only be removed from the top, so removing a group unwinds every active group applied at or after it
162
+ (last in, first out) and then re-applies the groups that were meant to stay, in their original order.
163
+ A patch that cannot be removed (someone else wrapped it) is reported and its group stays ``active``.
164
+ """
165
+ with _LOCK:
166
+ warnings: list[str] = []
167
+ active = sorted(
168
+ (g for g in STATE.groups.values() if g.state == "active"), key=lambda g: g.seq
169
+ )
170
+ wanted = {g.name for g in active} if groups is None else set(groups)
171
+ remove = [g for g in active if g.name in wanted]
172
+ if not remove:
173
+ return warnings
174
+ first = min(g.seq for g in remove)
175
+ unwound = [g for g in reversed(active) if g.seq >= first]
176
+ # Dry run first so the operation is atomic: if anything we installed has been wrapped by third-party
177
+ # code, change nothing (a half-reverted group would be skipped by a later enable() forever).
178
+ cur = {}
179
+ conflicts = []
180
+ for g in unwound:
181
+ for p in reversed(g.patches):
182
+ if p.state != "active":
183
+ continue
184
+ key = (id(p.owner), p.attr)
185
+ now = cur.get(key, p.owner.__dict__.get(p.attr))
186
+ if now is p.replacement:
187
+ cur[key] = p.original
188
+ else:
189
+ conflicts.append(f"{p.name} was re-wrapped by other code; nothing was reverted")
190
+ if conflicts:
191
+ return conflicts
192
+ for g in unwound:
193
+ for p in reversed(g.patches):
194
+ if p.state != "active":
195
+ continue
196
+ if p.owner.__dict__.get(p.attr) is p.replacement:
197
+ setattr(p.owner, p.attr, p.original)
198
+ p.state = "inactive"
199
+ else:
200
+ warnings.append(f"{p.name} was re-wrapped by other code; left in place")
201
+ if all(p.state != "active" for p in g.patches):
202
+ g.state = "inactive"
203
+ if (
204
+ not warnings
205
+ ): # re-apply survivors (their self-check already passed; fingerprints are re-verified)
206
+ for g in reversed(unwound):
207
+ if g.name not in wanted:
208
+ _apply_group(g, None)
209
+ STATE.reason = "disabled" if not STATE.enabled else "ok"
210
+ return warnings