[PR #1439] [CLOSED] perf(prefill): cache immutable BF16-to-FP16 weight conversions #3457

Closed
opened 2026-11-07 14:41:36 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/1439
Author: @agorevski
Created: 10/7/2026
Status: ❌ Closed

Base: main ← Head: perf/immutable-bf16-conversion-cache


📝 Commits (3)

  • 8a9ef1f Add opt-in persistent immutable BF16 conversion cache
  • 819b33d Preserve strided FP16 inputs alongside rebased conversion cache
  • a9274aa Separate persistent writeback reserve for independent peer composition

📊 Changes

8 files changed (+506 additions, -30 deletions)

View changed files

📝 CMakeLists.txt (+10 -0)
📝 docs/DETAILS.md (+33 -0)
📝 include/strata/prefill/gemm.hpp (+34 -0)
📝 include/strata/prefill/prefill.hpp (+3 -0)
📝 src/prefill/gemm.cu (+163 -11)
📝 src/prefill/gemm_bf16_parity.cu (+226 -4)
📝 src/prefill/prefill.cpp (+9 -3)
📝 src/program/generate.cpp (+28 -12)

📄 Description

Title

Issue: Related context: #1349 and #1910; no issue resolution is claimed.

Opt-in persistent cache for immutable BF16-to-FP16 prompt weights

Summary

Avoid repeated conversion of explicitly immutable BF16 weights on the active CUDA FP16 BF16_TC path.
Rebased onto origin/main pinned at a0116b2782a5b82622358c04a06496485d12b2d6.
Current head: a9274aaaa2423de9c94b61d8d171041fcf7c1b16.
This is independent of other optimization PRs. The existing default is unchanged.

What changed

  • include/strata/prefill/gemm.hpp, src/prefill/gemm.cu: explicit bf16_immutable API,
    invalidation, statistics and a bounded persistent per-owner conversion arena. Entries use source pointer
    and weight shape on the owner's device; first-admitted entries remain until invalidation/destruction.
    Full/oversize misses convert per call without evicting retained weights.
  • Preserve newer upstream HIP/hipBLASLt, strided GEMM APIs, CUDA FP16 algorithm retry and Pascal/FP16
    low-memory tiling rather than restoring the old implementation. Cached and uncached FP16 activation
    conversion respects padded input rows; add a focused packed-versus-strided regression case.
  • include/strata/prefill/prefill.hpp, src/prefill/prefill.cpp: expose persistent reserve pricing and
    opt resident immutable model weights into caching. Mutable dequantization scratch, activations and
    drafter weights are not cached. Preserve newer source-dependent prefill sizing.
  • src/program/generate.cpp: reserve the persistent arena independently of loans in skip-if-fits,
    stage room, automatic/adaptive reserves, explicit/native cache caps, post-writeback and owned-fit checks.
    owned_prefill_mib() includes the cache even when buffers are borrowed, alongside estimated/exact
    owned buffers otherwise. Callers inherit the price without adding it again, including a separately
    merged peer-capacity PR. No peer planner or peer-specific header/API is added.
    The post-writeback reserve is initialized before the allocation loop via cache_writeback_reserve;
    the independent peer PR shares that baseline extraction and replaces the reserve with its full
    pending-allocation holdback. Non-peer persistence is preserved, while peer persistence is counted
    once through owned_prefill_mib() rather than added again.
  • src/prefill/gemm_bf16_parity.cu, CMakeLists.txt: configuration parsing, reuse, mutable bypass,
    shape keys, budget fallback/retention, invalidate/pointer reuse, separate streams, prompt-shape parity,
    inactive reservations and strided input checks. Keep default cuBLAS parity.
  • docs/DETAILS.md: configuration, lifetime, pricing and explicitly historical throughput-neutral evidence.

Only exact STRATA_BF16_TC_CACHE=1 enables the cache. STRATA_BF16_TC_CACHE_MIB defaults to 256 MiB per
Gemm, accepts 1..65536 MiB, and disables caching with a diagnostic for invalid values. The arena exists
only on the active FP16 BF16_TC path: CUDA Volta by default and Turing with STRATA_BF16_TC=1;
not an RTX 8000 name gate. Native BF16, Pascal FP32 and HIP reserve no arena.
STRATA_BF16_TC=2 is only the tests' forced FP16 mode.

Each owner has one model lifetime, device and fixed stream, with host-serialized calls. The immutable
source stays allocated and unchanged until invalidation/destruction. Invalidate before mutation, unload
or pointer reuse; invalidation waits for the stream and reuses the arena. Cross-stream uploads require
explicit ordering. The arena is never a loan and competes with expert VRAM. BF16_TC rounding remains
unchanged, not a guarantee of native-BF16 bit identity or cross-library determinism.

Extra Notes

Historical integrated measurements — not rebased benchmarks

2026-10-07, physical GPU 3, RTX 8000 at 260 W, IQ3_S, speculation 4, fixed 19,000 expert slots,
8,192-token prefill. The engine was built from v0.1.40.3 with other local changes integrated.
Three requests per size, 128 output tokens each; median prompt throughput:

Prompt tokens BF16_TC=1, no cache 1,024 MiB conversion cache
537 661 tokens/s 654 tokens/s
4,057 1,262 tokens/s 1,261 tokens/s
32,057 1,416 tokens/s 1,413 tokens/s

321 retained conversions, 5,778 hits. Whole-model throughput was neutral; no speedup is claimed.
Historical evidence: bench-tc1-result.json, bench-cache1024-result.json,
bench-cache1024-engine.log in session files. Neither the original isolated branch nor this rebase
produced these whole-model numbers. Older 5/5 GPU-suite results apply to the older branch, not this head.

Rebase validation (2026-10-12)

Working directory:
/home/algore/.copilot/session-state/a7c2bcbb-4264-4eb2-879d-c71a90285b0b/files/pr-worktrees/bf16-cache.

Configure — passed with CUDA 12.4.131, system GCC/G++ 13, tests ON, architecture 75, shared vendor:

mkdir -p build-scratch
env CUDA_DEVICE_ORDER=PCI_BUS_ID CUDA_VISIBLE_DEVICES=3 TMPDIR="$PWD/build-scratch" \
  cmake -S . -B build \
    -DSTRATA_ENABLE_CUDA=ON -DSTRATA_BUILD_TESTS=ON -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_C_COMPILER=/usr/bin/gcc-13 -DCMAKE_CXX_COMPILER=/usr/bin/g++-13 \
    -DCMAKE_CUDA_HOST_COMPILER=/usr/bin/g++-13 \
    -DCMAKE_CUDA_COMPILER=/home/algore/miniconda3/bin/nvcc \
    -DCMAKE_CUDA_ARCHITECTURES=75 \
    -DSTRATA_GGML_DIR=/home/algore/GIT/strata/third_party/llama.cpp

Build — passed, both targets; repeated after removal of an unused diagnostic argument:

env CUDA_DEVICE_ORDER=PCI_BUS_ID CUDA_VISIBLE_DEVICES=3 TMPDIR="$PWD/build-scratch" \
  cmake --build build --target strata gemm_bf16_parity -j2

CPU-only strict configuration parsing — passed, 1/1, repeated after final cleanup:

ctest --test-dir build --output-on-failure -R '^gemm_bf16_cache_config$'

Single targeted CUDA attempt — passed, 1/1, 0.86 seconds, including the new strided regression,
before the final unused-argument/documentation cleanup; this specific test was not rerun:

env -u STRATA_BF16_TC -u STRATA_BF16_TC_CACHE -u STRATA_BF16_TC_CACHE_MIB \
  CUDA_DEVICE_ORDER=PCI_BUS_ID CUDA_VISIBLE_DEVICES=3 \
  flock -x ../../pr-gpu3.lock \
    ctest --test-dir build --output-on-failure -R '^gemm_bf16_cache_parity$'

The whole GPU command held the exclusive session lock and selected only physical GPU 3.
The host's reported NVML 580.178 driver/library mismatch did not prevent this CUDA runtime test.
No driver/library/environment repair, reboot, model server or power change was attempted.
The remaining tests were subsequently run once (see below); no benchmarks were rerun.

Whitespace — passed:

git diff --check
git diff a0116b2782a5b82622358c04a06496485d12b2d6 --check

Logs: session files rebase-1439-configure.log, rebase-1439-build.log,
rebase-1439-final-build.log, rebase-1439-cpu.log, rebase-1439-gpu.log.
Old head preserved as backup/rebase-1439-6a6062f5-20261012T1852.

Independent peer composition followup

Followup commit a9274aaaa2423de9c94b61d8d171041fcf7c1b16 changes only three added lines and
one replaced line in src/program/generate.cpp, without importing peer symbols.
With peer head b19c396f84641fe3ee7d30c4e8aa1f234ee2667d, the merged code initializes the baseline,
adds persistent cache bytes for non-peer operation, then replaces that result when the peer planner is
active with peer_primary_reserve. That complete reserve already includes persistence via
owned_prefill_mib(), so it is a replacement, not an additive/double-booked holdback.
Only the peer agent modified the separate peer worktree.

Rebuild after the reserve followup — passed, both targets, CUDA 12.4/GCC13/arch75:

mkdir -p build-scratch
env CUDA_DEVICE_ORDER=PCI_BUS_ID CUDA_VISIBLE_DEVICES=3 TMPDIR="$PWD/build-scratch" \
  cmake --build build --target strata gemm_bf16_parity -j2

Remaining targeted GPU tests — passed, 3/3, 11.88 seconds. Executed once under the exclusive
whole-command GPU3 lock; the GEMM executable was unchanged by the reserve-only followup:

env -u STRATA_BF16_TC -u STRATA_BF16_TC_CACHE -u STRATA_BF16_TC_CACHE_MIB \
  CUDA_DEVICE_ORDER=PCI_BUS_ID CUDA_VISIBLE_DEVICES=3 \
  flock -x ../../pr-gpu3.lock \
    ctest --test-dir build --output-on-failure \
      -R '^gemm_bf16_(parity|cache_shapes|cache_inactive_reserve)$'
  • gemm_bf16_parity: passed, default cuBLAS parity, 5.36 seconds.
  • gemm_bf16_cache_shapes: passed, cached-versus-uncached prompt shapes, 5.34 seconds.
  • gemm_bf16_cache_inactive_reserve: passed, inactive path reserves no arena, 1.18 seconds.

Across the rebase attempts all five targeted tests passed, not as one combined suite run.
CUDA worked despite the reported NVML mismatch; no driver/library repair was attempted.
Logs: rebase-1439-compose-build.log, rebase-1439-remaining-gpu.log.

Composition — passed, no checkout or actual merge performed:

git merge-tree --write-tree perf/immutable-bf16-conversion-cache perf/capacity-aware-peer-placement

Merged tree: e13f01de931eac8550b7ab4d0f800401fe5bbf5f; the merged reserve ordering was inspected
directly with git show. This is a clean composition/code-inspection result, not a combined runtime benchmark.
git diff --check and
git diff a0116b2782a5b82622358c04a06496485d12b2d6 HEAD --check — passed.
git status --short — passed, clean. Local scratch directory removed.

gh issue list --repo Niko1221/Strata --state all --search 'BF16' --limit 30 --json number,title,url,
and equivalent searches for 'RTX 8000' and 'Turing', were followed by reading #1349 and #1910.
#1349 reports Turing BF16_TC and owned/borrowed prefill trade-offs, relevant to cache reserve/lifetime
context. #1910 reports dual-Turing prefill regression and mentions BF16/borrowed-buffer trade-offs;
this throughput-neutral opt-in cache has not been shown to fix it. Neither is a speculative Resolves.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Niko1221/Strata/pull/1439 **Author:** [@agorevski](https://github.com/agorevski) **Created:** 10/7/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `perf/immutable-bf16-conversion-cache` --- ### 📝 Commits (3) - [`8a9ef1f`](https://github.com/Niko1221/Strata/commit/8a9ef1fcf5fcc0c266998664bc667323b7034db5) Add opt-in persistent immutable BF16 conversion cache - [`819b33d`](https://github.com/Niko1221/Strata/commit/819b33de57bf3c843aaef54b6d2251bbb1c5b827) Preserve strided FP16 inputs alongside rebased conversion cache - [`a9274aa`](https://github.com/Niko1221/Strata/commit/a9274aaaa2423de9c94b61d8d171041fcf7c1b16) Separate persistent writeback reserve for independent peer composition ### 📊 Changes **8 files changed** (+506 additions, -30 deletions) <details> <summary>View changed files</summary> 📝 `CMakeLists.txt` (+10 -0) 📝 `docs/DETAILS.md` (+33 -0) 📝 `include/strata/prefill/gemm.hpp` (+34 -0) 📝 `include/strata/prefill/prefill.hpp` (+3 -0) 📝 `src/prefill/gemm.cu` (+163 -11) 📝 `src/prefill/gemm_bf16_parity.cu` (+226 -4) 📝 `src/prefill/prefill.cpp` (+9 -3) 📝 `src/program/generate.cpp` (+28 -12) </details> ### 📄 Description ## Title <!-- If Applicable, reference the GitHub issue --> Issue: Related context: #1349 and #1910; no issue resolution is claimed. Opt-in persistent cache for immutable BF16-to-FP16 prompt weights ## Summary <!-- Quick Summary of changes --> Avoid repeated conversion of explicitly immutable BF16 weights on the active CUDA FP16 BF16_TC path. Rebased onto `origin/main` pinned at `a0116b2782a5b82622358c04a06496485d12b2d6`. Current head: `a9274aaaa2423de9c94b61d8d171041fcf7c1b16`. This is independent of other optimization PRs. The existing default is unchanged. ## What changed <!-- Specifics on files changed, and what changes were made there --> - `include/strata/prefill/gemm.hpp`, `src/prefill/gemm.cu`: explicit `bf16_immutable` API, invalidation, statistics and a bounded persistent per-owner conversion arena. Entries use source pointer and weight shape on the owner's device; first-admitted entries remain until invalidation/destruction. Full/oversize misses convert per call without evicting retained weights. - Preserve newer upstream HIP/hipBLASLt, strided GEMM APIs, CUDA FP16 algorithm retry and Pascal/FP16 low-memory tiling rather than restoring the old implementation. Cached and uncached FP16 activation conversion respects padded input rows; add a focused packed-versus-strided regression case. - `include/strata/prefill/prefill.hpp`, `src/prefill/prefill.cpp`: expose persistent reserve pricing and opt resident immutable model weights into caching. Mutable dequantization scratch, activations and drafter weights are not cached. Preserve newer source-dependent prefill sizing. - `src/program/generate.cpp`: reserve the persistent arena independently of loans in skip-if-fits, stage room, automatic/adaptive reserves, explicit/native cache caps, post-writeback and owned-fit checks. `owned_prefill_mib()` includes the cache even when buffers are borrowed, alongside estimated/exact owned buffers otherwise. Callers inherit the price without adding it again, including a separately merged peer-capacity PR. No peer planner or peer-specific header/API is added. The post-writeback reserve is initialized before the allocation loop via `cache_writeback_reserve`; the independent peer PR shares that baseline extraction and replaces the reserve with its full pending-allocation holdback. Non-peer persistence is preserved, while peer persistence is counted once through `owned_prefill_mib()` rather than added again. - `src/prefill/gemm_bf16_parity.cu`, `CMakeLists.txt`: configuration parsing, reuse, mutable bypass, shape keys, budget fallback/retention, invalidate/pointer reuse, separate streams, prompt-shape parity, inactive reservations and strided input checks. Keep default cuBLAS parity. - `docs/DETAILS.md`: configuration, lifetime, pricing and explicitly historical throughput-neutral evidence. Only exact `STRATA_BF16_TC_CACHE=1` enables the cache. `STRATA_BF16_TC_CACHE_MIB` defaults to 256 MiB per `Gemm`, accepts 1..65536 MiB, and disables caching with a diagnostic for invalid values. The arena exists only on the active FP16 BF16_TC path: CUDA Volta by default and Turing with `STRATA_BF16_TC=1`; not an RTX 8000 name gate. Native BF16, Pascal FP32 and HIP reserve no arena. `STRATA_BF16_TC=2` is only the tests' forced FP16 mode. Each owner has one model lifetime, device and fixed stream, with host-serialized calls. The immutable source stays allocated and unchanged until invalidation/destruction. Invalidate before mutation, unload or pointer reuse; invalidation waits for the stream and reuses the arena. Cross-stream uploads require explicit ordering. The arena is never a loan and competes with expert VRAM. BF16_TC rounding remains unchanged, not a guarantee of native-BF16 bit identity or cross-library determinism. ## Extra Notes <!-- Any extra notes, delete if there are none --> ### Historical integrated measurements — not rebased benchmarks 2026-10-07, physical GPU 3, RTX 8000 at 260 W, IQ3_S, speculation 4, fixed 19,000 expert slots, 8,192-token prefill. The engine was built from v0.1.40.3 with other local changes integrated. Three requests per size, 128 output tokens each; median prompt throughput: | Prompt tokens | BF16_TC=1, no cache | 1,024 MiB conversion cache | | ---: | ---: | ---: | | 537 | 661 tokens/s | 654 tokens/s | | 4,057 | 1,262 tokens/s | 1,261 tokens/s | | 32,057 | 1,416 tokens/s | 1,413 tokens/s | 321 retained conversions, 5,778 hits. Whole-model throughput was neutral; **no speedup is claimed**. Historical evidence: `bench-tc1-result.json`, `bench-cache1024-result.json`, `bench-cache1024-engine.log` in session files. Neither the original isolated branch nor this rebase produced these whole-model numbers. Older 5/5 GPU-suite results apply to the older branch, not this head. ### Rebase validation (2026-10-12) Working directory: `/home/algore/.copilot/session-state/a7c2bcbb-4264-4eb2-879d-c71a90285b0b/files/pr-worktrees/bf16-cache`. Configure — **passed** with CUDA 12.4.131, system GCC/G++ 13, tests ON, architecture 75, shared vendor: ```sh mkdir -p build-scratch env CUDA_DEVICE_ORDER=PCI_BUS_ID CUDA_VISIBLE_DEVICES=3 TMPDIR="$PWD/build-scratch" \ cmake -S . -B build \ -DSTRATA_ENABLE_CUDA=ON -DSTRATA_BUILD_TESTS=ON -DCMAKE_BUILD_TYPE=Release \ -DCMAKE_C_COMPILER=/usr/bin/gcc-13 -DCMAKE_CXX_COMPILER=/usr/bin/g++-13 \ -DCMAKE_CUDA_HOST_COMPILER=/usr/bin/g++-13 \ -DCMAKE_CUDA_COMPILER=/home/algore/miniconda3/bin/nvcc \ -DCMAKE_CUDA_ARCHITECTURES=75 \ -DSTRATA_GGML_DIR=/home/algore/GIT/strata/third_party/llama.cpp ``` Build — **passed**, both targets; repeated after removal of an unused diagnostic argument: ```sh env CUDA_DEVICE_ORDER=PCI_BUS_ID CUDA_VISIBLE_DEVICES=3 TMPDIR="$PWD/build-scratch" \ cmake --build build --target strata gemm_bf16_parity -j2 ``` CPU-only strict configuration parsing — **passed, 1/1**, repeated after final cleanup: ```sh ctest --test-dir build --output-on-failure -R '^gemm_bf16_cache_config$' ``` Single targeted CUDA attempt — **passed, 1/1, 0.86 seconds**, including the new strided regression, before the final unused-argument/documentation cleanup; this specific test was not rerun: ```sh env -u STRATA_BF16_TC -u STRATA_BF16_TC_CACHE -u STRATA_BF16_TC_CACHE_MIB \ CUDA_DEVICE_ORDER=PCI_BUS_ID CUDA_VISIBLE_DEVICES=3 \ flock -x ../../pr-gpu3.lock \ ctest --test-dir build --output-on-failure -R '^gemm_bf16_cache_parity$' ``` The whole GPU command held the exclusive session lock and selected only physical GPU 3. The host's reported NVML 580.178 driver/library mismatch did **not** prevent this CUDA runtime test. No driver/library/environment repair, reboot, model server or power change was attempted. The remaining tests were subsequently run once (see below); no benchmarks were rerun. Whitespace — **passed**: ```sh git diff --check git diff a0116b2782a5b82622358c04a06496485d12b2d6 --check ``` Logs: session files `rebase-1439-configure.log`, `rebase-1439-build.log`, `rebase-1439-final-build.log`, `rebase-1439-cpu.log`, `rebase-1439-gpu.log`. Old head preserved as `backup/rebase-1439-6a6062f5-20261012T1852`. ### Independent peer composition followup Followup commit `a9274aaaa2423de9c94b61d8d171041fcf7c1b16` changes only three added lines and one replaced line in `src/program/generate.cpp`, without importing peer symbols. With peer head `b19c396f84641fe3ee7d30c4e8aa1f234ee2667d`, the merged code initializes the baseline, adds persistent cache bytes for non-peer operation, then replaces that result when the peer planner is active with `peer_primary_reserve`. That complete reserve already includes persistence via `owned_prefill_mib()`, so it is a replacement, not an additive/double-booked holdback. Only the peer agent modified the separate peer worktree. Rebuild after the reserve followup — **passed**, both targets, CUDA 12.4/GCC13/arch75: ```sh mkdir -p build-scratch env CUDA_DEVICE_ORDER=PCI_BUS_ID CUDA_VISIBLE_DEVICES=3 TMPDIR="$PWD/build-scratch" \ cmake --build build --target strata gemm_bf16_parity -j2 ``` Remaining targeted GPU tests — **passed, 3/3, 11.88 seconds**. Executed once under the exclusive whole-command GPU3 lock; the GEMM executable was unchanged by the reserve-only followup: ```sh env -u STRATA_BF16_TC -u STRATA_BF16_TC_CACHE -u STRATA_BF16_TC_CACHE_MIB \ CUDA_DEVICE_ORDER=PCI_BUS_ID CUDA_VISIBLE_DEVICES=3 \ flock -x ../../pr-gpu3.lock \ ctest --test-dir build --output-on-failure \ -R '^gemm_bf16_(parity|cache_shapes|cache_inactive_reserve)$' ``` - `gemm_bf16_parity`: passed, default cuBLAS parity, 5.36 seconds. - `gemm_bf16_cache_shapes`: passed, cached-versus-uncached prompt shapes, 5.34 seconds. - `gemm_bf16_cache_inactive_reserve`: passed, inactive path reserves no arena, 1.18 seconds. Across the rebase attempts all five targeted tests passed, not as one combined suite run. CUDA worked despite the reported NVML mismatch; no driver/library repair was attempted. Logs: `rebase-1439-compose-build.log`, `rebase-1439-remaining-gpu.log`. Composition — **passed**, no checkout or actual merge performed: ```sh git merge-tree --write-tree perf/immutable-bf16-conversion-cache perf/capacity-aware-peer-placement ``` Merged tree: `e13f01de931eac8550b7ab4d0f800401fe5bbf5f`; the merged reserve ordering was inspected directly with `git show`. This is a clean composition/code-inspection result, not a combined runtime benchmark. `git diff --check` and `git diff a0116b2782a5b82622358c04a06496485d12b2d6 HEAD --check` — **passed**. `git status --short` — **passed**, clean. Local scratch directory removed. ### Related issues `gh issue list --repo Niko1221/Strata --state all --search 'BF16' --limit 30 --json number,title,url`, and equivalent searches for `'RTX 8000'` and `'Turing'`, were followed by reading #1349 and #1910. #1349 reports Turing BF16_TC and owned/borrowed prefill trade-offs, relevant to cache reserve/lifetime context. #1910 reports dual-Turing prefill regression and mentions BF16/borrowed-buffer trade-offs; this throughput-neutral opt-in cache has not been shown to fix it. Neither is a speculative `Resolves`. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-07 14:41:36 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Niko1221/Strata#3457
No description provided.