Skip to content

Studio: vendor laya 0.3.5 and hold its weights in float16 - #12202

Merged
danielhanchen merged 2 commits into
mainfrom
vendor-laya
Sep 28, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
vendor-laya

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Sep 28, 2026 •

Copy link
Copy Markdown
Member

Summary

Two changes to the Studio Decision API (laya): vendor laya instead of installing it, and hold its weights in float16.

1. Vendor laya 0.3.5

Studio installed laya==0.3.5 from studio/backend/requirements/extras-no-deps.txt, and the Decision API also carried a runtime fallback that ran pip install laya==0.3.5 on first use. This vendors laya into Studio instead, the same way truststore is already vendored under studio/backend/vendor/.

laya is small (8 pure Python files) and its dependencies (torch, transformers, safetensors, huggingface_hub, numpy) are all already Studio pins, so nothing new needs installing.

Changes

  • studio/backend/vendor/laya/: the laya package from laya-0.3.5-py3-none-any.whl (sha256 4c57f64c...2903, matches PyPI), byte-identical, no edits.
  • studio/backend/vendor/LICENSE.laya: the Apache-2.0 license from the wheel. The wheel ships no NOTICE file.
  • studio/backend/vendor/laya_manifest.json: version, wheel hash and per-file sha256, same format as truststore_manifest.json.
  • core/systemone/laya_runtime.py:
    • New _laya() loads vendor/laya/__init__.py by file path and registers it as the top-level laya module. It never goes through sys.path, so a laya left in the venv by an older Studio (or installed by the user) cannot shadow the vendored copy. This matters because laya_runtime reaches into laya internals (Agent._to_internal, laya.common.collate_items).
    • Removed the install machinery (package_available, ensure_package, install_in_background, _install_command, LAYA_REQUIREMENT). status() still returns installing: False so the settings API shape and the frontend are unchanged.
  • routes/settings.py: no longer starts a background install.
  • requirements/extras-no-deps.txt: dropped the laya pin.
  • tests/test_vendored_truststore.py: the digest, layout and license checks now cover every *_manifest.json in vendor/, so a formatter rewrite or partial version bump of laya fails the same way it does for truststore.
  • tests/test_systemone.py: removed the pip install tests. Added a test that a decoy laya earlier on sys.path does not win over the vendored copy, and one that settings never report an install.
  • vendor/README.md: provenance, import rule and update steps for laya.

Packaging needs no change: the existing backend/vendor/**/* package-data glob already ships the files, and ruff plus the pre-commit formatter already skip studio/backend/vendor/.

2. Hold Laya weights in float16

All three published Laya checkpoints (laya-english, laya-multilingual, laya-typed-decisions) are stored in float16, but laya builds the ModernBERT encoder in fp32 and upcasts the weights on load, so every served model used twice the memory it needs. laya_runtime now:

  • builds the agent on CPU, casts the nn.Linear / nn.Embedding / attention in_proj weights to float16, then moves it to the device, so the device never holds the fp32 copy. LayerNorm stays fp32 (CPU layer_norm rejects fp16 parameters) and embedding outputs are returned to fp32 with a forward hook, so the residual stream keeps its precision.
  • runs fp16 autocast for fp16 checkpoints on CUDA, and on x86 CPUs with native fp16 (mkldnn fp16 support). Checkpoints not stored in fp16 follow the device: bf16 where native (by compute capability on NVIDIA, is_bf16_supported() on ROCm), else fp16.
  • keeps the previous fp32 path on CPUs without native fp16/bf16, and on MPS, XPU and MLX.
  • reruns a request in fp32 if an fp16 forward ever returns non-finite logits (fp16 weights widen to fp32 exactly).
  • UNSLOTH_SYSTEMONE_FP32=1 restores fp32 everywhere.

Measured on the real laya-multilingual checkpoint through Studio's own loader, 36 requests (30 short, 6 at the 1024 token limit), 108 question answers, against an fp32 reference:

Weights Load peak Max prob diff vs fp32 Answers changed
CUDA before (fp32 weights, bf16 autocast) 1247 MiB ~1247 MiB 2.4e-2 2/108
CUDA after (fp16 weights, fp16 autocast) 614 MiB 626 MiB 1.8e-3 0/108
CPU before (fp32) 1228 MiB 0 0/108
CPU after, native fp16 (fp16 weights, fp16 autocast) 614 MiB 2.1e-3 0/108

So fp16 compute is both half the memory and about 13x closer to fp32 than the bf16 autocast Studio used on GPUs, since the weights were saved in fp16 to begin with. Casting to bf16 instead (as sentence transformers does) was measured too and was the least accurate option (max 3.9e-2), because it drops 3 mantissa bits from every stored weight. Latency is unchanged (B200: 17.7 ms vs 19.1 ms per short request; CPU with AVX512-FP16: 77 ms vs 65 ms short, 1401 ms vs 1478 ms at 1024 tokens). Serving one model on a GPU now needs about 1.2 to 1.3 GB including the CUDA context, down from about 1.9 GB. torch.compile with CUDA graphs measured up to 8x faster per request on B200 but costs a long first compile, so it is left for a follow up PR.

Testing

  • pytest studio/backend/tests/test_systemone.py studio/backend/tests/test_vendored_truststore.py: 93 passed, 3 skipped (the 3 need a real checkpoint).
  • With SYSTEMONE_TEST_LAYA pointed at a downloaded convaiinnovations/laya snapshot (multilingual): test_real_laya_answers_through_the_route and test_fast_path_matches_laya_predict both pass on the vendored copy (CPU). test_mlx_answers_match_laya_on_cpu still skips because it needs Apple Silicon.
  • fp16 tests: dtype read from the safetensors header; the precision table per device (NVIDIA pre-Ampere, Ampere+, kill switch, non-x86 CPU, MPS); fp16 weights are bit-identical to fp32 weights under bf16 autocast on a tiny ModernBERT-like model, with norms fp32 and embedding output fp32; an fp16 overflow reruns in fp32. Flip checks: removing the embedding hook, casting the norms, or removing the overflow rerun each makes its test fail.
  • Stock PyPI laya vs the vendored copy on the real checkpoint, CPU and CUDA: identical Agent.predict output, Studio fast path equal to Agent.predict (max prob diff 0), decide() end to end.
  • tests/test_formatter_fixed_point.py and tests/test_python39_compatibility.py pass. The laya sources also parse under Python 3.9 grammar.
  • Built a wheel locally and confirmed studio/backend/vendor/laya/*.py, LICENSE.laya and laya_manifest.json are included.

Not covered: the MLX path on Apple Silicon was not run (it is unchanged, fp32). ROCm, XPU, pre-Ampere NVIDIA GPUs and CPUs without native fp16 were not run on hardware; their dtype choice is covered by unit tests only.

Ship the laya 0.3.5 wheel sources byte-identical under studio/backend/vendor/laya
with its Apache-2.0 license and a sha256 manifest, next to the vendored truststore.
laya_runtime loads that copy by file path, so a laya left in the venv cannot
shadow it, and the Decision API no longer pip installs anything at runtime.
Drops the laya pin from extras-no-deps.txt.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 28, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-09-28T14:04:08.221105Z 28e2c11 New commits
🔒 Security Review ⚠️ Failed 2026-09-28T13:09:52.786118Z 6dc0bb0 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@shimmyshimmer

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 6dc0bb0812

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +137 to +138
if (module := sys.modules.get("laya")) is not None:
return module

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Reject preloaded non-vendored laya modules

If startup code or a plugin has already imported an installed laya, this fast path returns that module without checking its origin, so the vendored file-path loader never runs. The runtime then accesses version-specific internals such as laya.common.collate_items and Agent._to_internal on an arbitrary installed version, which can fail or change Decision API results; only reuse the module when it came from _VENDORED_LAYA, otherwise load the bundled copy.

Useful? React with 👍 / 👎.

The published Laya checkpoints are stored in float16, but laya builds the
encoder in fp32 and upcasts on load, doubling resident memory for no
precision. laya_runtime now builds the agent on CPU, casts the matmul and
embedding weights to float16 (norms stay fp32, embedding outputs return to
fp32) and only then moves it to the device, and runs fp16 autocast.

On CUDA and on CPUs with native fp16 this halves weight memory (1247 MiB to
614 MiB) and tracks fp32 about 13x closer than the previous bf16 autocast
(max prob diff 1.8e-3 vs 2.4e-2, no changed answers vs 2/108). Checkpoints
not stored in fp16 follow the device: bf16 where native, else fp16. CPUs
without native fp16/bf16, MPS, XPU and MLX keep the previous fp32 path.
An fp16 forward that overflows reruns in fp32. UNSLOTH_SYSTEMONE_FP32=1
restores fp32 everywhere.
@danielhanchen danielhanchen changed the title Studio: vendor laya 0.3.5 instead of installing it Studio: vendor laya 0.3.5 and hold its weights in float16 Sep 28, 2026
@danielhanchen
danielhanchen merged commit 4ab5c77 into main Sep 28, 2026
11 of 79 checks passed
@danielhanchen
danielhanchen deleted the vendor-laya branch September 28, 2026 14:08
shimmyshimmer pushed a commit to shimmyshimmer/unsloth-staging-4 that referenced this pull request Sep 28, 2026
shaojun pushed a commit to shaojun/unsloth that referenced this pull request Sep 29, 2026
…12202)

* Vendor laya 0.3.5 into Studio instead of installing it

Ship the laya 0.3.5 wheel sources byte-identical under studio/backend/vendor/laya
with its Apache-2.0 license and a sha256 manifest, next to the vendored truststore.
laya_runtime loads that copy by file path, so a laya left in the venv cannot
shadow it, and the Decision API no longer pip installs anything at runtime.
Drops the laya pin from extras-no-deps.txt.

* Studio: hold Laya weights in float16

The published Laya checkpoints are stored in float16, but laya builds the
encoder in fp32 and upcasts on load, doubling resident memory for no
precision. laya_runtime now builds the agent on CPU, casts the matmul and
embedding weights to float16 (norms stay fp32, embedding outputs return to
fp32) and only then moves it to the device, and runs fp16 autocast.

On CUDA and on CPUs with native fp16 this halves weight memory (1247 MiB to
614 MiB) and tracks fp32 about 13x closer than the previous bf16 autocast
(max prob diff 1.8e-3 vs 2.4e-2, no changed answers vs 2/108). Checkpoints
not stored in fp16 follow the device: bf16 where native, else fp16. CPUs
without native fp16/bf16, MPS, XPU and MLX keep the previous fp32 path.
An fp16 forward that overflows reruns in fp32. UNSLOTH_SYSTEMONE_FP32=1
restores fp32 everywhere.
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.

2 participants