Repository navigation
Studio: vendor laya 0.3.5 and hold its weights in float16 - #12202
Conversation
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.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
@codex review |
There was a problem hiding this comment.
💡 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".
| if (module := sys.modules.get("laya")) is not None: | ||
| return module |
There was a problem hiding this comment.
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.
…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.
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.5fromstudio/backend/requirements/extras-no-deps.txt, and the Decision API also carried a runtime fallback that ranpip install laya==0.3.5on first use. This vendors laya into Studio instead, the same way truststore is already vendored understudio/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/: thelayapackage fromlaya-0.3.5-py3-none-any.whl(sha2564c57f64c...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 astruststore_manifest.json.core/systemone/laya_runtime.py:_laya()loadsvendor/laya/__init__.pyby file path and registers it as the top-levellayamodule. It never goes throughsys.path, so alayaleft in the venv by an older Studio (or installed by the user) cannot shadow the vendored copy. This matters becauselaya_runtimereaches into laya internals (Agent._to_internal,laya.common.collate_items).package_available,ensure_package,install_in_background,_install_command,LAYA_REQUIREMENT).status()still returnsinstalling: Falseso 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.jsoninvendor/, 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 decoylayaearlier onsys.pathdoes 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 skipstudio/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_runtimenow:nn.Linear/nn.Embedding/ attentionin_projweights to float16, then moves it to the device, so the device never holds the fp32 copy. LayerNorm stays fp32 (CPUlayer_normrejects fp16 parameters) and embedding outputs are returned to fp32 with a forward hook, so the residual stream keeps its precision.mkldnnfp16 support). Checkpoints not stored in fp16 follow the device: bf16 where native (by compute capability on NVIDIA,is_bf16_supported()on ROCm), else fp16.UNSLOTH_SYSTEMONE_FP32=1restores fp32 everywhere.Measured on the real
laya-multilingualcheckpoint through Studio's own loader, 36 requests (30 short, 6 at the 1024 token limit), 108 question answers, against an fp32 reference: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.compilewith 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).SYSTEMONE_TEST_LAYApointed at a downloadedconvaiinnovations/layasnapshot (multilingual):test_real_laya_answers_through_the_routeandtest_fast_path_matches_laya_predictboth pass on the vendored copy (CPU).test_mlx_answers_match_laya_on_cpustill skips because it needs Apple Silicon.layavs the vendored copy on the real checkpoint, CPU and CUDA: identicalAgent.predictoutput, Studio fast path equal toAgent.predict(max prob diff 0),decide()end to end.tests/test_formatter_fixed_point.pyandtests/test_python39_compatibility.pypass. The laya sources also parse under Python 3.9 grammar.studio/backend/vendor/laya/*.py,LICENSE.layaandlaya_manifest.jsonare 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.