[Fix] Load Laya without the throwaway random weight init - #22
Merged
Merged
Conversation
laya.load builds the ModernBERT encoder from its config, so transformers draws every weight at random before the checkpoint is loaded over all of them with strict=True. That draw is ~33 of the ~35 s load. Run the load under transformers' no_init_weights (transformers.initialization in 5.x, transformers.modeling_utils in 4.x, plain load if neither exists). The from_env tests stub the helper: importing torch inside patch.dict(sys.modules) and importing it again later segfaults.
This was referenced Sep 27, 2026
Collaborator
|
Follow-ups:
|
Collaborator
|
this issue seems to be fixed with laya 0.3.9 btw |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
--model layaspends about 35 s loading a 0.4B model, and every CLI call pays it again because the model loads in process. Most of that time is spent initializing weights that get overwritten right away.How
laya.loadbuilds the ModernBERT encoder withAutoModel.from_config, so transformers draws every weight at random, and then laya loads the checkpoint over all of them withstrict=True. Profiling the load on CPU puts ~33 s of it in_init_weights/trunc_normal_.LayaModel.from_envnow runslaya.loadunder transformers'no_init_weights(). The helper moved fromtransformers.modeling_utils(4.x) totransformers.initialization(5.x);without_weight_init()ins1a/decision_models/laya.pytries both and falls back to a plain load if neither is there. Because laya loads withstrict=True, a checkpoint that misses a weight still fails loudly instead of leaving it uninitialized.The
from_envtests stub the helper. Without the stub the real one imports torch insidepatch.dict(sys.modules), which drops torch again on exit, and the next torch import in the same process segfaults. This only happens with the laya extra installed, not in CI.What
--model laya(agents,decide,probe, MCPdecide) loads in about 3 s. No flags, outputs or records change.Verification
Default checkpoint, laya 0.3.5, M1 Pro 16 GB. Each load in a fresh process:
s1a decide --model layaend to end: 37.6 s → 9.9 s on CPU, 39.7 s → 8.5 s on MPS, same output.Warm latency is unchanged. On CPU the first request after load is ~0.2–0.3 s slower, since the random init no longer warms up torch's CPU path during load.
uv run ruff format --check . && uv run ruff check . && uv run ty checkuv run pytest -qandscripts/smoke.sh: all pass on the core install (no torch, as in CI). With the laya extra installed,test_an_agent_without_system_one_is_a_config_error_naming_the_methodfails onmaintoo, because it expects laya's version to beunknown.CHANGELOG.md