Skip to content

fix: skip ModernBERT weight init on Windows + Python 3.14 - #129

Closed
sathariels wants to merge 1 commit into
NandhaKishorM:mainfrom
sathariels:cursor/win-py314-init-weights-062c
Closed

sathariels wants to merge 1 commit into
NandhaKishorM:mainfrom
sathariels:cursor/win-py314-init-weights-062c

Conversation

@sathariels

Copy link
Copy Markdown
Contributor

Addresses #123.

On Windows 11 with Python 3.14 and torch 2.14, building a ModernBERT encoder segfaults inside PreTrainedModel.initialize_weights. pip install laya succeeds; Agent(...) dies during model construction. The reporter verified that replacing transformers.modeling_utils.PreTrainedModel.initialize_weights with a no-op before construction lets the checkpoints load. That is safe for Laya because Agent.__init__ immediately calls load_state_dict(strict=True), so the random init values are never used.

This PR applies that same no-op automatically, and only while build_model constructs the encoder, when platform.system() == "Windows" and Python is 3.14 or newer. Other platforms leave initialize_weights in place. init_weights still runs tie_weights. DecisionModel.forward and predict are unchanged.

Callers who construct a ModernBERT module themselves can use laya.win_patch.guard() / install() / restore(). Holds nest, so an explicit install() stays in effect across Agent construction.

The segfault was not remeasured on Windows here. CI for this repo runs on Linux. The change follows the stack and workaround in #123 (transformers 5.17, the crash inside initialize_weights during post_init).

Python 3.14 is listed in the project classifiers. The floor stays 3.10.

Tests

Offline, no Hub download:

python tests/test_win_patch.py
python tests/test_router.py
python tests/test_criteria.py
python tests/test_download.py
python tests/test_shortlist.py
python tests/test_decision_model.py
python tests/test_packaging.py
python tests/test_email.py
ruff check laya/ --select=E9,F63,F7,F82,F401,F811 --line-length=120

tests/test_win_patch.py checks that the guard installs and restores, that it is a no-op when the platform is not Windows + Python 3.14, and that build_model holds the no-op only during local ModernBERT construction (tiny config on disk, no checkpoint download).

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 NandhaKishorM#123). Skip it only while
build_model constructs the encoder on that platform.

Addresses NandhaKishorM#123.

Co-authored-by: Nithilan Kumaran <sathariels@users.noreply.github.com>
@sathariels

Copy link
Copy Markdown
Contributor Author

Also feel free to give me any issues you have, i have a decent amount of free time and really like this project

@NandhaKishorM

Copy link
Copy Markdown
Owner

Thank you for pinning down the Windows + Python 3.14 crash and for getting CI green on it. #195 landed today: it builds the model under no_init_weights, skipping the exact init pass that segfaults, on every platform, and it cuts load time from 22 s to 1.5 s with bit-identical outputs. So this is superseded. If you are on that stack, a quick confirmation that laya.load() now works there would be very welcome in #123.

@sathariels sathariels closed this Sep 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants