From 156589ee6d367890c1ad619b57ea6dc148af8137 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Tue, 22 Sep 2026 04:50:59 +0000 Subject: [PATCH] fix: skip ModernBERT weight init on Windows Python 3.14 Agent loads checkpoint weights with load_state_dict(strict=True) right after encoder construction, so the random values from PreTrainedModel.initialize_weights are never used. On Windows with Python 3.14 that call segfaults (issue #123). Skip it only while build_model constructs the encoder on that platform. Addresses #123. Co-authored-by: Nithilan Kumaran --- .github/workflows/ci.yml | 1 + .github/workflows/release.yml | 1 + README.md | 15 +- laya/common.py | 22 ++- laya/win_patch.py | 96 +++++++++++++ pyproject.toml | 1 + tests/test_win_patch.py | 254 ++++++++++++++++++++++++++++++++++ 7 files changed, 383 insertions(+), 7 deletions(-) create mode 100644 laya/win_patch.py create mode 100644 tests/test_win_patch.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3a81a3064..c6cd08684 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -49,6 +49,7 @@ jobs: python tests/test_shortlist.py python tests/test_decision_model.py python tests/test_packaging.py + python tests/test_win_patch.py - name: Email cleaning tests run: python tests/test_email.py diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 86e93792e..b69fc6121 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -44,6 +44,7 @@ jobs: python tests/test_shortlist.py python tests/test_decision_model.py python tests/test_packaging.py + python tests/test_win_patch.py - name: Build run: python -m build diff --git a/README.md b/README.md index 16744f965..33c25384e 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,20 @@ Three checkpoints, and a `Router` that picks between them per request: pip install laya ``` -Python 3.10 or newer. The dependencies set that floor: `huggingface_hub` 1.x, `transformers` 5.x and `torch` 2.14 all require 3.10. +Python 3.10 or newer, including 3.14. The dependencies set that floor: `huggingface_hub` 1.x, `transformers` 5.x and `torch` 2.14 all require 3.10. + +### Troubleshooting + +**Windows + Python 3.14.** On Windows 11 with Python 3.14 and torch 2.14, building a ModernBERT encoder can segfault inside `PreTrainedModel.initialize_weights` ([#123](https://github.com/NandhaKishorM/laya/issues/123)). `Agent` loads the checkpoint with `load_state_dict(strict=True)` immediately after construction, so Laya skips that initialization automatically while it builds the encoder when `platform.system() == "Windows"` and Python is 3.14 or newer. To wrap your own ModernBERT construction the same way: + +```python +import laya.win_patch + +with laya.win_patch.guard(): + ... # build the encoder; restore happens when the block exits +``` + +This is the no-op the reporter verified on #123. The segfault was not remeasured on Windows here (CI runs on Linux). --- diff --git a/laya/common.py b/laya/common.py index fb900053d..05bd2a2fd 100644 --- a/laya/common.py +++ b/laya/common.py @@ -137,14 +137,24 @@ def forward(self, input_ids, attention_mask, marker_pos, marker_mask, qtype, det def build_model(cfg: Dict, encoder_dir: Optional[str] = None) -> DecisionModel: + """Build the encoder and decision head. + + On Windows with Python 3.14 or newer, encoder construction skips + ``PreTrainedModel.initialize_weights``. That call segfaults while building + ModernBERT on that stack (#123). ``Agent`` then loads the checkpoint with + ``load_state_dict(strict=True)``, so the skipped values are never read. + """ from transformers import AutoConfig, AutoModel - if encoder_dir and os.path.exists(encoder_dir): - ecfg = AutoConfig.from_pretrained(encoder_dir) - enc = AutoModel.from_config(ecfg, attn_implementation="sdpa") - else: - enc = AutoModel.from_pretrained(cfg["encoder"], attn_implementation="sdpa") - return DecisionModel(enc, cfg.get("head_layers", 2), len(cfg.get("act_costs", {})) + 1) + from .win_patch import guard + + with guard(): + if encoder_dir and os.path.exists(encoder_dir): + ecfg = AutoConfig.from_pretrained(encoder_dir) + enc = AutoModel.from_config(ecfg, attn_implementation="sdpa") + else: + enc = AutoModel.from_pretrained(cfg["encoder"], attn_implementation="sdpa") + return DecisionModel(enc, cfg.get("head_layers", 2), len(cfg.get("act_costs", {})) + 1) def proper_reward( diff --git a/laya/win_patch.py b/laya/win_patch.py new file mode 100644 index 000000000..43f08b484 --- /dev/null +++ b/laya/win_patch.py @@ -0,0 +1,96 @@ +"""Skip ModernBERT random init on Windows + Python 3.14. + +``transformers`` 5.17 segfaults inside ``PreTrainedModel.initialize_weights`` +while constructing a ModernBERT module on Windows 11 with Python 3.14 and +torch 2.14 (issue #123). ``Agent`` loads the checkpoint with +``load_state_dict(strict=True)`` immediately afterwards, so those initialized +values are never read. The verified workaround is to replace +``initialize_weights`` with a no-op before the encoder is built. + +``build_model`` does that automatically, and only while the encoder is being +constructed, when ``platform.system() == "Windows"`` and +``sys.version_info >= (3, 14)``. ``install`` / ``guard`` are there for callers +who build a ModernBERT module themselves. ``tie_weights`` still runs: +``init_weights`` is left alone. +""" + +import platform +import sys +import threading +from contextlib import contextmanager + +_lock = threading.Lock() +_holds = 0 +_original = None + + +def _noop_initialize_weights(self, *args, **kwargs): + """Replacement for ``PreTrainedModel.initialize_weights``. Returns None.""" + return None + + +def _affected(system: str, version_info) -> bool: + return system == "Windows" and version_info >= (3, 14) + + +def needs_guard() -> bool: + """True on Windows with Python 3.14 or newer, where encoder init segfaults.""" + return _affected(platform.system(), sys.version_info) + + +def installed() -> bool: + """True while at least one ``install`` / ``guard`` hold is active.""" + return _holds > 0 + + +def install(*, force: bool = False) -> bool: + """Take one hold on the no-op. Holds nest; pair each True with ``restore``. + + Returns False when this platform is unaffected and ``force`` is false, or + when ``PreTrainedModel.initialize_weights`` is absent. A second call on an + affected platform takes another hold and also returns True. + """ + global _holds, _original + with _lock: + if not force and not needs_guard(): + return False + if _holds == 0: + import transformers.modeling_utils as modeling_utils + + current = getattr(modeling_utils.PreTrainedModel, "initialize_weights", None) + if current is None: + return False + _original = current + modeling_utils.PreTrainedModel.initialize_weights = _noop_initialize_weights + _holds += 1 + return True + + +def restore() -> bool: + """Drop one hold. The original method is put back when the last hold drops.""" + global _holds, _original + with _lock: + if _holds == 0: + return False + _holds -= 1 + if _holds == 0: + import transformers.modeling_utils as modeling_utils + + modeling_utils.PreTrainedModel.initialize_weights = _original + _original = None + return True + + +@contextmanager +def guard(*, force: bool = False): + """Hold the no-op for the duration of the block, then drop that hold. + + Yields True when this block took a hold. On an unaffected platform the + yield is False and ``initialize_weights`` is left as it was. + """ + held = install(force=force) + try: + yield held + finally: + if held: + restore() diff --git a/pyproject.toml b/pyproject.toml index 75e40f191..6086f63ae 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -22,6 +22,7 @@ classifiers = [ "Programming Language :: Python :: 3.11", "Programming Language :: Python :: 3.12", "Programming Language :: Python :: 3.13", + "Programming Language :: Python :: 3.14", "Topic :: Scientific/Engineering :: Artificial Intelligence", ] dependencies = [ diff --git a/tests/test_win_patch.py b/tests/test_win_patch.py new file mode 100644 index 000000000..c9c15f00f --- /dev/null +++ b/tests/test_win_patch.py @@ -0,0 +1,254 @@ +"""Offline tests for the Windows + Python 3.14 init guard. No Hub download. + +Run: python tests/test_win_patch.py +""" +import os +import platform +import sys +import tempfile +from pathlib import Path +from unittest.mock import patch + +os.environ.setdefault("USE_TF", "0") +os.environ.setdefault("USE_TORCH", "1") +sys.path.insert(0, str(Path(__file__).resolve().parents[1])) + +import transformers.modeling_utils as modeling_utils # noqa: E402 +from transformers import AutoModel, ModernBertConfig # noqa: E402 + +from laya.common import DecisionModel, build_model # noqa: E402 +from laya.win_patch import ( # noqa: E402 + _affected, + _noop_initialize_weights, + guard, + install, + installed, + needs_guard, + restore, +) + +_REAL = modeling_utils.PreTrainedModel.initialize_weights + + +def _reset(): + while installed(): + restore() + modeling_utils.PreTrainedModel.initialize_weights = _REAL + + +def _method(): + return modeling_utils.PreTrainedModel.initialize_weights + + +def test_needs_guard_matches_windows_and_python_314(): + assert _affected("Windows", (3, 14)) is True + assert _affected("Windows", (3, 14, 0)) is True + assert _affected("Windows", (3, 15, 1)) is True + assert _affected("Windows", (3, 13, 9)) is False + assert _affected("Windows", (3, 10)) is False + assert _affected("Linux", (3, 14)) is False + assert _affected("Darwin", (3, 14)) is False + assert needs_guard() is _affected(platform.system(), sys.version_info) + + +def test_live_platform_installs_or_is_a_noop(): + """On this machine, install applies only when needs_guard() is true, and restore undoes it.""" + try: + if needs_guard(): + assert install() is True + assert installed() is True + assert _method() is _noop_initialize_weights + assert _method()(object()) is None + assert restore() is True + assert installed() is False + assert _method() is _REAL + assert restore() is False + else: + assert install() is False + assert installed() is False + assert _method() is _REAL + assert restore() is False + assert _method() is _REAL + finally: + _reset() + + +def test_force_install_nests_and_restore_puts_the_original_back(): + calls = [] + + def spy(self, *args, **kwargs): + calls.append("spy") + return "spy" + + try: + modeling_utils.PreTrainedModel.initialize_weights = spy + assert install(force=True) is True + assert install(force=True) is True + assert installed() is True + assert _method() is _noop_initialize_weights + assert _method()(object()) is None + assert calls == [] + assert restore() is True + assert installed() is True + assert _method() is _noop_initialize_weights + assert restore() is True + assert installed() is False + assert _method() is spy + assert restore() is False + finally: + _reset() + + +def test_guard_restores_after_success_and_after_an_error(): + try: + with guard(force=True) as held: + assert held is True + assert _method() is _noop_initialize_weights + assert installed() is False + assert _method() is _REAL + + try: + with guard(force=True): + assert _method() is _noop_initialize_weights + raise RuntimeError("build failed") + except RuntimeError as exc: + assert str(exc) == "build failed" + assert installed() is False + assert _method() is _REAL + finally: + _reset() + + +def test_guard_is_a_noop_when_the_platform_is_unaffected(): + try: + with patch("laya.win_patch.needs_guard", return_value=False): + with guard() as held: + assert held is False + assert _method() is _REAL + assert install() is False + assert _method() is _REAL + finally: + _reset() + + +def test_install_does_nothing_when_initialize_weights_is_missing(): + try: + with patch("laya.win_patch.needs_guard", return_value=True), patch.object( + modeling_utils.PreTrainedModel, "initialize_weights", None + ): + assert install() is False + assert installed() is False + finally: + _reset() + + +def test_explicit_install_outlives_a_nested_guard(): + try: + assert install(force=True) is True + with guard(force=True) as held: + assert held is True + assert _method() is _noop_initialize_weights + assert installed() is True + assert _method() is _noop_initialize_weights + assert restore() is True + assert _method() is _REAL + finally: + _reset() + + +def _tiny_modernbert_dir() -> str: + tmp = tempfile.mkdtemp(prefix="laya-win-patch-") + cfg = ModernBertConfig( + vocab_size=128, + hidden_size=32, + num_hidden_layers=1, + num_attention_heads=2, + intermediate_size=64, + reference_compile=False, + pad_token_id=0, + bos_token_id=1, + eos_token_id=2, + cls_token_id=1, + sep_token_id=2, + ) + cfg.save_pretrained(tmp) + return tmp + + +def _spy_from_config(seen): + original = AutoModel.from_config + + def spy(config, *args, **kwargs): + seen["during"] = _method() + return original(config, *args, **kwargs) + + return spy + + +def test_build_model_holds_the_noop_only_during_construction_when_affected(): + seen = {} + encoder_dir = _tiny_modernbert_dir() + try: + with patch("laya.win_patch.needs_guard", return_value=True), patch.object( + AutoModel, "from_config", _spy_from_config(seen) + ): + model = build_model({"head_layers": 0, "act_costs": {}}, encoder_dir=encoder_dir) + assert isinstance(model, DecisionModel) + assert type(model.encoder).__name__ == "ModernBertModel" + assert seen["during"] is _noop_initialize_weights + assert installed() is False + assert _method() is _REAL + finally: + _reset() + + +def test_build_model_leaves_initialize_weights_alone_when_unaffected(): + seen = {} + encoder_dir = _tiny_modernbert_dir() + try: + with patch("laya.win_patch.needs_guard", return_value=False), patch.object( + AutoModel, "from_config", _spy_from_config(seen) + ): + model = build_model({"head_layers": 0, "act_costs": {}}, encoder_dir=encoder_dir) + assert isinstance(model, DecisionModel) + assert seen["during"] is _REAL + assert installed() is False + assert _method() is _REAL + finally: + _reset() + + +def test_build_model_restores_when_construction_raises(): + encoder_dir = _tiny_modernbert_dir() + + def boom(config, *args, **kwargs): + assert _method() is _noop_initialize_weights + raise RuntimeError("encoder failed") + + try: + with patch("laya.win_patch.needs_guard", return_value=True), patch.object(AutoModel, "from_config", boom): + try: + build_model({"head_layers": 0, "act_costs": {}}, encoder_dir=encoder_dir) + except RuntimeError as exc: + assert str(exc) == "encoder failed" + else: + raise AssertionError("build_model should have raised") + assert installed() is False + assert _method() is _REAL + finally: + _reset() + + +if __name__ == "__main__": + test_needs_guard_matches_windows_and_python_314() + test_live_platform_installs_or_is_a_noop() + test_force_install_nests_and_restore_puts_the_original_back() + test_guard_restores_after_success_and_after_an_error() + test_guard_is_a_noop_when_the_platform_is_unaffected() + test_install_does_nothing_when_initialize_weights_is_missing() + test_explicit_install_outlives_a_nested_guard() + test_build_model_holds_the_noop_only_during_construction_when_affected() + test_build_model_leaves_initialize_weights_alone_when_unaffected() + test_build_model_restores_when_construction_raises() + _reset() + print("all win_patch tests passed")