Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 <sathariels@users.noreply.github.com>
  • Loading branch information
cursoragent and sathariels committed Sep 22, 2026
commit 156589ee6d367890c1ad619b57ea6dc148af8137
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

---

Expand Down
22 changes: 16 additions & 6 deletions laya/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
96 changes: 96 additions & 0 deletions laya/win_patch.py
Original file line number Diff line number Diff line change
@@ -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()
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
Loading
Loading