mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
Labels
No labels
needs-format
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Niko1221/Strata#4336
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/Niko1221/Strata/pull/2918
Author: @thc1006
Created: 10/17/2026
Status: 🔄 Open
Base:
main← Head:ple-skip-hint-when-locked📝 Commits (1)
7114105ple: no per-row MADV_WILLNEED on a locked table (#2281)📊 Changes
2 files changed (+153 additions, -2 deletions)
View changed files
📝
src/kernels/ngram.cpp(+3 -0)📝
src/ngram/ple_reader_test.cpp(+150 -2)📄 Description
Problem and result
With
--ple-io ram, the PLE table is locked in RAM when it opens. On Linux and macOS,PleTable::gather_batchstill callsmadvise(MADV_WILLNEED)for every row it reads: 16 calls per token, on the prompt path and in the decode windows. Those pages are already resident. This change makes the POSIX branch of the hint return when the table is locked.On ZFS, each of those calls becomes an ARC prefetch of the record that holds the row. When the record is not in the ARC, it is read from disk. #2281 has the trace and the source links.
Measured on 4x Tesla P100 with Qwen3.8-Flash-Next IQ3_XXS, the root file system on ZFS and an 8 GiB ARC (details below):
--ple-io ram, new ~16K-token prompts--no-ple-prefetchEach row is two fresh server starts with the same three prompts. Per prompt, this branch is within 0.3% of
--no-ple-prefetch, and the answers were byte-identical in every run. Decode was 2.3% slower with the hint. This branch decoded 0.2-0.3% below 0.1.45 with--no-ple-prefetch, and so did this branch with the flag (run 7), which never reaches the new line.I also copied the table to an ext4 file system in a loop file on the same ZFS pool. There the hint made little difference: 0.1.45 prefilled at 684-707 tok/s with and without
--no-ple-prefetch, within 0.5% per prompt, and decoded within 0.4%. If your 2x A4000 machine is not on ZFS, this fits what you saw there.ben0112's machine also has the model on ZFS (#2571). On POSIX,
--no-ple-prefetchturns off only this hint, so on a locked table this branch skips the same calls as the flag that fixed it there. I have not run the branch on that machine.Related discussion
Issue: #2281 (closed). There,
--no-ple-prefetch, the second toggle you suggested, brought ben0112's speed back, and @ben0112 suggested skipping the hint once--ple-io ramhas locked the table. ben0112 also offered to test a patch build. This is that change.Approach
src/kernels/ngram.cpp: the POSIX branch ofhintingather_batchreturns whenimpl_->lockedis set. On POSIX,lockedis only set aftermlockof the whole table succeeds, so a table whose lock failed keeps its hints. Besides configs that ask for--ple-io ram, setup writes it on Linux when the model is on a rotational disk and the RAM has room for the table on top of the model (#605).src/ngram/ple_reader_test.cpp:format_round_tripgets a third leg, "ram" (mmap withlock). It checksread_row,gatherandgather_batchon a locked table against each format's dequantizer. Ifmlockis refused, the leg reads the table unlocked and prints a note. Values cannot show the hint, so on Linux with glibc the test also counts theMADV_WILLNEEDcalls (it definesmadviseand passes each call on):gather_batchmust make some on a mapped table that is not locked, and none on a locked one. A new check,row_hints_follow_the_lock, opens one small table withRLIMIT_MEMLOCKat 0 (the lock is refused and the hints stay), then locked (no hints, the same values), then again without the lock (the hints are back), and checkslocked()againstVmLck. Wheremlockcannot succeed, the cases that need a lock print a note instead. Where it succeeds but locks nothing (ASan and TSan make it a no-op),row_hints_follow_the_lockprints a note.Validation
Re-run on 0.2.0-beta (
3d1fb48) with this branch applied (it applies without conflicts), on Linux with glibc, as a user withoutCAP_IPC_LOCK. The selftest was built for the CPU only fromsrc/kernels/ngram.cpp,src/ngram/ple_reader.cpp,src/platform/direct_file.cppandsrc/ngram/ple_reader_test.cpp(g++ 11.4,-std=c++20 -O2 -pthread).Passed:
9ae93f7, 0.2.1 with the 0.1.46.1 hotfix), built the same way:ple_reader_test --selftestpasses withRLIMIT_MEMLOCKat 8037 MiB, 64 KiB and 0, and with thelockedcheck removed it fails at 8037 MiB and 64 KiB (a locked table: gather_batch made 256 row hints, not 0).ple_reader_test --selfteston 0.2.0-beta with this branch, withRLIMIT_MEMLOCKat 7.85 GiB, 64 KiB and 0: every check OK, the NVFP4 format that 0.2.0-beta adds included. At 7.85 GiB:ple row hints (Linux): kept on a refused lock, none on a locked table, back after reopening it without the lock: OK.lockedcheck removed fromngram.cppfails:a locked table: gather_batch made 256 row hints, not 0.477ac13):lockedcheck removed,ple_reader_test --selftestfails: "a locked table: gather_batch made 256 row hints, not 0". It also fails for six other wrong versions ofngram.cpp, among them one that checks whether the lock was requested instead of whether it succeeded, and one whoseclose()leaveslockedset. These runs were on Linux with glibc, as a user withoutCAP_IPC_LOCK, withRLIMIT_MEMLOCKat 7.85 GiB and at 64 KiB. The versions and the output are inresults/pr2918-mutants.diff.txtandresults/pr2918-test-mutants.txt.ctest -R '^ple_reader_selftest$'passes on5c5adedin the CUDA and HIP builds (the selftest runs on the CPU), withRLIMIT_MEMLOCKat 7.85 GiB, 64 KiB and 0. At 64 KiB only the 512-row table inrow_hints_follow_the_lockcan be locked, and at 0 none can.5c5aded, which differs from the head only in its commit message: the CUDA engine (CUDA 12.9.1, sm_60, the flags setup.py uses for a CUDA 12 build), the HIP engine for gfx1100 (ROCm 7.10.0a20251120 from TheRock's wheels, configured as setup.py does) and the SYCL engine (oneAPI DPC++ 2026.1.1 with oneMKL 2026.1.0, SPIR-V JIT, configured assycl/tools/build.shdoes). The HIP build shows no compiler warning in the two changed files, and the SYCL build, which does not build the test, none inngram.cpp. The Mac engine is not built here; with clang 21 from zig 0.16.0 and-Wall -Wextra, both files compile without warnings for macOS (arm64 and x86_64).Failed:
Not run:
9ae93f7), which this branch is now rebased onto: the engine builds (CUDA, HIP, SYCL), their ctest runs and the speed runs. The hint code this branch changes is the same in both as in 0.1.45.Limits
Not changed: the whole-table hint when the table opens,
--ple-io mmap,--ple-io direct,--no-ple-prefetch, and Windows. On Windows the hint ingather_batchis onePrefetchVirtualMemorycall per window of up to 4096 rows, andgather,issueandprefetch_rowshint there too; I have no Windows machine to measure a change on. With this branch and--ple-io mmap, the engine made the same 767,936madvisecalls in three prompts as 0.1.45 with--ple-io ram.Where the time goes on ZFS
ben0112's last comment in #2281 puts the cost on the ZFS ARC, competing for disk I/O and memory bandwidth. The disk reads are ARC prefetch misses: 17-18 GB per prompt read into an 8 GiB ARC. Inside the hint, though, the threads almost never blocked. In a pass with kprobes on the engine's threads, the threads that issue the hints entered
zpl_fadvise768,192 times, slept there 3 times and were preempted 8,553 times.arc_wait_for_evictionnever slept and took 1.0% of their time inzpl_fadvise.The cost showed up as CPU time, including ZFS's work to complete the prefetch reads. System time on the whole machine (8 vCPUs) averaged 3-5 s per prompt without the hint and 37 s with it. At least 15.8 s of that was in ZFS's read-completion threads (
z_rd_int). Under ftrace,zpl_fadviseaveraged 39 µs a call counting only time on a CPU, and 95-96 µs counting time off the CPU too. I did not measure memory bandwidth.Environment and method
zfs_arc_maxis 8 GiB, to leave RAM for the locked table and the experts.VmLckwas 28,125,136 kB in everyramrun.477ac13) and this branch on top of it, built from two checkouts with the same flags. The engine measured here was built fromc1c9e57, the first push, which has the samengram.cpp.--max-context 32768 --kv int8 --expert-cache auto --prefill 2048 --pool-workers 6 --batch-groups auto --vram-reserve-mib 1536 --ple-io ram --spec 4 --spec-min-p 0.5 --resident-experts --trim-stage-weights --no-kv-grow --conversation-cache-mib 8192.results/AB-0.1.45-ple-hint.mdandresults/pr2918-*.txt.LimitMEMLOCK=infinity, one request at a time. Prompts are slices of Strata's own docs with a fixed nonce in front, so every run gets the same bytes. Nothing was reused: the engine logged 0 reused tokens for all 141 prompt reads. Temperature 0, thinking off. Three prompts of ~16K tokens with 32 tokens out, and three decodes of up to 512 tokens./sys/block/sda/sda4/stat(the pool's only partition, as the VM sees it) times 512 bytes, read before and after each request. System time is thesystemcolumn of/proc/stat. No run that recorded steal time had any.perf stat -p <engine pid>for themadvisecounts, ftrace's function profiler (once more in run 3, counting only time on a CPU), and the kprobe pass in run 8. Tracing slows the hint, so none of the speeds in this description comes from a traced request.--nativeand--ple-ggufname the same directory. The locked table then comes from ext4's page cache.Every run, in order
Prefill and decode are ratios to the first
--no-ple-prefetchrun on the same file system (run 1 on ZFS, run 9 on ext4). Each value is the mean ratio over the same three requests.madvise, 3 prompts--no-ple-prefetch--no-ple-prefetch--ple-io mmap--no-ple-prefetch--no-ple-prefetch--no-ple-prefetchRun 6 only checks that the hint is still sent without a lock. Its page cache was not controlled, so its speed is not comparable.
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.