Skip to content

laya scorer: AVX2 / AVX-512 clones of the linear kernel (CPU scorer 208 -> 139 ms a decision) - #231

Merged
bong-water-water-bong merged 1 commit into
mainfrom
laya-scorer-simd
Sep 29, 2026
Merged

bong-water-water-bong merged 1 commit into
mainfrom
laya-scorer-simd

Conversation

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator

The engine builds for baseline x86-64 (SSE2), so the scorer's linear() ran 4-wide even on Zen 5. On Linux x86-64 with GCC or Clang, its column kernel (linear_cols, split out of the thread lambda) is now also compiled for AVX2 and AVX-512 with target_clones, and the loader picks the variant for the running CPU through ifunc. Other platforms, including the Windows cross build, compile it as before.

The fixed-order sums from #228 are unchanged, so results still don't depend on the thread count. A given build on a given CPU always takes the same variant. Across CPUs the last bits can differ, because the AVX2 and AVX-512 variants may use FMA.

Measured on strixhalo (Ryzen AI Max, AVX-512) with laya_classify_eval on the 200 routing cases:

accuracy ms per decision
before (main) 191/200 = 95.5% 208
this PR 191/200 = 95.5% 139

It also speeds up the classifier head that the NPU encoder route runs on the CPU: serve --laya with ONEBIT_LAYA_DEVICE=npu goes from a p50 of 52 to 50 ms per request, still 194/200. Local ctest passes 13/13.

🤖 Generated with Claude Code

@context7

context7 Bot commented Sep 29, 2026

Copy link
Copy Markdown

Docs7 for 1bit-monster/engine

Result Status Action
Deployment ➖ Not used —
Content review ➖ Did not run. This site has no agent runs available this month. Wait for the monthly reset or check your Docs7 plan. —

Commit 47e4461

@github-actions

Copy link
Copy Markdown

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

🎫 Ticket compliance analysis 🔶

228 - Partially compliant

Compliant requirements:

  • Implemented vectorizable linear kernel with fixed-order sums
  • Enabled threaded head attention for decision head
  • Maintained deterministic results regardless of thread count
  • Measured performance improvements (208ms -> 139ms per decision)
  • Ensured compatibility with baseline x86-64 and newer SIMD instruction sets
  • Maintained same accuracy (191/200 = 95.5%)

Non-compliant requirements:

  • None

Requires further human verification:

  • Verification of exact performance measurements on target hardware
  • Confirmation that ifunc selection works correctly on different CPU models
⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
🧪 No relevant tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

SIMD Target Clones Implementation

The use of target_clones attribute for AVX2 and AVX-512 compilation may introduce complexity in ensuring correct CPU detection at runtime. While the PR description mentions ifunc is used for CPU detection, there's no explicit verification that this mechanism works correctly across different x86-64 CPU models and that the correct SIMD variant is selected.

#if defined(__x86_64__) && defined(__linux__) && (defined(__GNUC__) || defined(__clang__))
#define ONEBIT_LAYA_SIMD_CLONES __attribute__((target_clones("avx512f", "avx2", "default")))
#else
#define ONEBIT_LAYA_SIMD_CLONES
#endif
Loop Vectorization Assumption

The PR claims that the fixed-order sums enable vectorization, but the implementation uses a nested loop structure that might not be fully vectorized by all compilers. The code uses for (int l = 0; l < V; ++l) inside a loop over c, which could prevent full vectorization on some compilers or CPU architectures, especially when V is 16.

for (int c = 0; c < nv; c += V)
    for (int l = 0; l < V; ++l) {
        const float w = wr[c + l];
        a0[l] += x0[c + l] * w; a1[l] += x1[c + l] * w; a2[l] += x2[c + l] * w; a3[l] += x3[c + l] * w;
    }

@bong-water-water-bong
bong-water-water-bong merged commit 817c117 into main Sep 29, 2026
9 checks passed
@bong-water-water-bong
bong-water-water-bong deleted the laya-scorer-simd branch September 29, 2026 16:23
bong-water-water-bong pushed a commit that referenced this pull request Nov 7, 2026
bong-water-water-bong added a commit that referenced this pull request Nov 7, 2026
…els load again (refs #1339) (#1343)

Pins third_party/llama.cpp to 092eb9dbc (fork #231, merged): GGML_HRX_WEIGHT_HOST_PLACEMENT now defaults off, so models above ~6-16 GB load again (27B loads in 24 s at this pin; 4B with =1 still imports, text identical). Mitigation only: the import's own load stall stays open in Refs #1339.
bong-water-water-bong added a commit that referenced this pull request Nov 8, 2026
…he switch's docs (#1350)

* release-check: measure the HRX weight placement (engine #1339's mitigation), not the switch's docs

#1339: importing the loader's weight mapping instead of copying it to the device (fork #226) saves GTT - the 4B goes
from 3925 to 1243 MiB of the process's own GTT - but on large files the import never finishes: a dense 16.4 GiB 27B and
a 76.3 GiB Flash-Next sat in it for 72 minutes with no error, while the device copy loads in 20-75 s. Fork #231 (pinned
since #1343) makes the import OPT-IN: `GGML_HRX_WEIGHT_HOST_PLACEMENT=1` asks for it, unset or 0 keeps the device copy.

Nothing prints that switch: `llama-server --list-devices` prints name/description/size only (common/arg.cpp:1064-1088 at
the pin), the backend logs nothing at the decision (`device_buffer_from_host_ptr`, ggml-hrx.cpp:915), and
`1bit serve --print-backend-env` has no placement variable. So this row measures the thing itself, the way the issue did:

  * load one NAMED model twice with the shipped engine (`1bit serve --device hrx --ctx-size 512`), once default and once
    with the switch on, and weigh each serve process's own `drm-total-gtt` out of /proc/<pid>/fdinfo;
  * PASS when the default arm costs materially more GTT than the opt-in arm (relative, 15 % floor - the issue's own arms
    are 1.89x and 3.16x, so no absolute MiB threshold can drift with ctx or slots), with both arms up;
  * FAIL naming which arm did not come up when one never answers /health.

The model is named because it must be UNTIED: fork #233 (#1316) moves a tied embedding's placement, so a tied model would
differ between the arms for a second reason. The row uses the staged Qwen3-4B-Instruct-2507-Q4_K_M (token_embd.weight and
output.weight both present, no tie_word_embeddings), SKIPs when that model is not staged rather than substituting another,
and sits before the chat rows so the two arms load into a quiet device.

Outcome evidence for a >16 GiB load cannot live on a stick whose largest staged model is 2.4 GB; the row says so and
cites #1339.

Measured: `--self-test` 82 cases (9 new), PASS. Proof harness in the release lane drives the row's own bytes with a
stub serve: issue arms PASS, equal arms FAIL, reversed FAIL, either arm dead FAIL naming it, no /dev/kfd SKIP, no staged
model SKIP - 19/19.

* release-check: hrx-placement skips when the build has no import switch (fork #234 removed it)

#1351 (merged) pins llama.cpp ed43da0b4, which **removes** GGML_HRX_WEIGHT_HOST_PLACEMENT (git grep at that pin: 0 hits)
and replaces the weight-mapping import with read() + posix_fadvise(DONTNEED). On such a build the row's env var is
ignored, both arms take the same path, their own-GTT readings are identical, and the verdict would return FAIL - a red
release-check row caused by a row measuring a mechanism that no longer exists. A gate that fails a correct build is its
own defect.

So the row now asks the shipped code which mechanism it has: hrxp_has_switch() looks for GGML_HRX_WEIGHT_HOST_PLACEMENT in
the shipped libggml-hrx (the string the code reads), and when it is absent the row SKIPs with the reason - naming fork
#234 / engine #1351 and saying both arms would be identical - instead of failing. On a build that has the switch (which is
what c32 carries: llama.cpp 67d8c42ec, 3 hits) it measures exactly as before.

Not rewritten to the new mechanism yet, on purpose: #1351's own arm is page-cache residency (16.5 GiB -> nothing), peak RSS
(16.4 -> 2.9 GiB) and load time (14.3 -> 18.6 s), and a row for it has to be built and measured on a pin that has #234
before it may gate one - writing an unmeasured branch into a release gate is the failure this lane refuses. That is the
next roll's work; the shape (pure verdict function, --self-test, two arms, skip-not-fail, process-group cleanup) is kept.

Measured: --self-test 82 cases PASS; the release lane's harness now 23/23 - the new cases prove a no-switch build SKIPs
with that reason and starts no arms, while a -switch build still PASSes, FAILs on equal arms, and names a dead arm.

---------

Co-authored-by: pi-release <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant