Skip to content

Qwen3.5: offload BF16 input embeddings to host RAM - #5052

Merged
ashhart merged 5 commits into
ashhart:mainfrom
tacosta:feat/4942-qwen35-host-embeddings-evidence
Nov 29, 2026
Merged

ashhart merged 5 commits into
ashhart:mainfrom
tacosta:feat/4942-qwen35-host-embeddings-evidence

Conversation

@tacosta

@tacosta tacosta commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

Before this change, Qwen3.5 CUDA kept BF16 input embedding rows on the GPU. This commit adds --embedding-residency auto|device|host for ModelOpt NVFP4 and compressed-tensors/Unsloth NVFP4 checkpoints. Rows stay BF16 and are copied unchanged. auto uses host-mapped rows when device capacity needs the space and retains device placement when they fit. Existing EXL3 automatic offload is unchanged.

The shared MTP sizing helper includes the active drafter's startup and resident memory when choosing placement. MTP scratch layout and recurrent scratch are unchanged; the separate MTP admission-charge work in #5134 is out of scope.

ATC overlap

atc overlap --json ran on this base and head. Selected checks completed for all 112 visible candidates: 56 conflicts with separate open PRs, 53 clean, and 3 with no shared files or no result. GitHub result caps may hide other PRs. Conflicting PRs: #728, #1394, #1522, #1886, #2008, #2020, #2340, #2523, #2634, #2982, #3041, #3075, #3076, #3218, #3333, #3563, #3564, #3565, #3566, #3567, #3640, #3723, #3729, #3867, #3878, #3884, #3885, #4416, #4449, #4471, #4500, #4502, #4523, #4592, #4594, #4598, #4608, #4618, #4631, #4644, #4645, #4649, #4660, #4664, #4666, #4685, #4689, #4695, #4698, #4701, #4746, #4749, #4841, #4883, #4966, and #5095. A 22-file stale local worktree conflict was excluded as directed. Since then, upstream main advanced from 2d15aaeca2eecedb1ca0346fb9c337da5fad5e32 to 6c225463591863b1473ff8040df53ebb063d0986, including MTP memory-admission commit 95f14d9c73. GitHub now reports mergeable_state=dirty; a local merge-tree check against current main finds content conflicts in zig/build/cuda_family_host.zig and zig/src/families/qwen3_5/cuda_mtp.zig. The PR is ready for review but not merge-ready until the base conflict is resolved. The latest ATC scan is incomplete because of PR and changed-file coverage limits; the overlap list above is from the earlier base.

Checks on this head

  • zig fmt --check build.zig zig and git diff --check: passed.
  • Lean checker: 93 findings on base and 93 on head; zero new.
  • Focused host targets for embedding offload, ModelOpt admission, formats, MTP selection/memory, CLI residency, and execution witness: passed. Baseline CLI rejects the new option; the PR binary recognizes it.
  • CUDA SM120 product build with CUDA 13.3: passed.
  • Host suite: 14,211 passed and 107 skipped. XPU refusal reports zeInit: 0x78000001 on both base and head; classified as the same environment-level initialization failure.
  • tools/zig/env_names.py: inherited check failure on base and head for NOT_READ LD_AUDIT and LD_PRELOAD (761 variables scanned); env_names_test.py: 17 tests passed.
  • CI: Zig format run #14441 passed. In No GPU run #14455 on this head, pytest, zig-windows, both Windows and Ubuntu benchmark-client jobs, zig-macos-dev-tools, comfyui-moss-installer, release-paths, and zig-b passed. zig fails only at the environment-name check for the same two base/head NOT_READ findings listed above; later steps in that job were skipped. Both nightly jobs were skipped by workflow conditions. The sole completed failure is classified as inherited.

Runtime receipt

Measured on Linux x86_64, Zig 0.17.0, CUDA 13.3, NVIDIA RTX 5090 Laptop GPU (24,463 MiB, SM120). Checkpoints are Qwen3.8-27B loaded by the Qwen3.5 CUDA path: ModelOpt revision dbb8f445b3145f8a4c18ddc769f032d57d32867c; Unsloth revision 60e813d4dbbdc5d64cf3f5a8caf2897bedf03679. KV stayed BF16. ModelOpt MTP3 used its BF16 drafter. ModelOpt one-stream decode used the default 1 GiB reserve; context and concurrency checks used a matched 0.5 GiB reserve. Unsloth checks used 0.5 GiB.

One-stream decode

Seeded MTP3 used 10 repetitions per prompt and temperature. Prompt-seeded token and output hashes matched 10/10 for both prompts, greedy and sampled.

ModelOpt MTP3 prompt Temp Main median Host median Delta
Raw code 0 88.78 tok/s 88.73 tok/s -0.05%
Chat, thinking off 0 74.85 tok/s 74.84 tok/s -0.01%
Raw code 1 91.86 tok/s 91.81 tok/s -0.06%
Chat, thinking off 1 75.13 tok/s 75.03 tok/s -0.15%

Five-run no-draft chat medians were also close: ModelOpt 27.87 → 27.86 tok/s greedy and 35.04 → 35.00 sampled; Unsloth 32.11 → 32.11 greedy and 32.09 → 32.09 sampled. No speedup is claimed.

Cold prompt and context admission

Prompt caching was disabled for these cold prompts; each result below completed the full prompt.

Checkpoint / mode Main or device Host or auto
ModelOpt MTP3 32k refused (2.73 GiB needed; 2.24 GiB available) 32k admitted with host rows; 32,752-token prefill completed in 20.94 s. 64k MTP3 refused (4.86 GiB needed; 4.74 GiB available).
ModelOpt no draft 32k completed; 64k refused 32k auto kept embeddings on device; 64k auto chose host and completed 65,520 tokens in 46.82 s.
Unsloth no draft 1k baseline comparison completed auto chose host at 32k and completed 32,752 tokens in 22.92 s; explicit-host 2k and 8k cold prompts completed.

At 8k context, maximum admitted parallelism was one stream on main and four with host placement for ModelOpt MTP3, ModelOpt no-draft, and Unsloth no-draft. Three-repetition concurrent checks matched solo and serial outputs. Four-stream median aggregate chat throughput was 183.31 tok/s for ModelOpt MTP3, 110.40 for ModelOpt no-draft, and 107.24 for Unsloth no-draft. Main could not admit four streams in these configurations.

Base/head cache-resume checks passed for ModelOpt MTP and Unsloth no-draft. Drafted versus draft:false, concurrent versus solo, and greedy main/head output hashes matched. No GPU kernel or precision changed; kernel contract is n/a.

Platforms run: Linux/CUDA SM120 on RTX 5090 Laptop GPU; Windows CI and macOS dev-tools CI passed. Other CUDA SMs, Metal, and HIP have not been run for this change.

Checklist

  • One behavior; issue, main base, single-commit stack, and current overlap result recorded.
  • Current head, base/head lean counts, zero new findings, format, and diff checks recorded.
  • CI on this head complete: zig-b and all other runnable checks passed except zig, whose only failure is the inherited environment-name check above. Both nightly jobs were skipped by workflow conditions.
  • Host/device placement, cache resume, drafted/draft:false, concurrent/solo, and seeded greedy/sampled MTP3 comparisons passed.
  • BF16 embeddings and BF16 KV are unchanged; no lossy format, precision trade, or kernel change.
  • Platforms run and not run are listed; commit author and committer use the public GitHub identity.

@tacosta
tacosta force-pushed the feat/4942-qwen35-host-embeddings-evidence branch from 3081dd2 to bf0e6f6 Compare November 25, 2026 14:34
@tacosta tacosta changed the title Qwen3.5: offload NVFP4 embeddings to host RAM Qwen3.5: offload BF16 input embeddings to host RAM Nov 25, 2026
@tacosta
tacosta force-pushed the feat/4942-qwen35-host-embeddings-evidence branch from bf0e6f6 to 7c5b820 Compare November 25, 2026 15:32
@tacosta

tacosta commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

24 GB Qwen3.8-27B context and VRAM measurement

Measured on an NVIDIA GeForce RTX 5090 Laptop GPU (24,463 MiB) using TensorFold commit 7c5b8202da6968912980b9cb4b04df0ff29ec2ab.

Both variants were started with explicit host embedding residency, one stream (--parallel 1), BF16 target KV, the checkpoint's own MTP drafter (--drafter <same checkpoint> --mtp-drafts 3), and the default 1 GiB CUDA admission reserve. The server accepted the listed maximum context at startup and rejected the next token on its CUDA memory admission check.

Checkpoint Max / first rejected Target BF16 KV MTP KV ring Non-context CUDA active CUDA active / peak NVML process / card free
NVIDIA ModelOpt NVFP4 56,014 / 56,015 3.419 GiB 0.214 GiB 17.769 GiB 21.402 / 21.447 GiB 21.686 / 1.662 GiB
Unsloth NVFP4 25,053 / 25,054 1.529 GiB 0.096 GiB 19.778 GiB 21.402 / 21.565 GiB 21.684 / 1.664 GiB

Safetensors source payload breakdown:

Checkpoint Target body LM head Target subtotal Host input embeddings MTP drafter source
NVIDIA ModelOpt NVFP4 16.591 GiB 0.666 GiB 17.257 GiB 2.368 GiB 0.791 GiB
Unsloth NVFP4 17.465 GiB 1.185 GiB 18.650 GiB 2.368 GiB 0.791 GiB

Pinned host memory measured 2.429 GiB for both runs (about 0.061 GiB beyond the embedding payload). The startup lines reported 17.83 GiB and 19.83 GiB of model allocation before per-stream reservation, respectively. The 1 GiB admission reserve reduces the usable budget; it is not a separate allocation. NVML process memory is about 0.28 GiB above TensorFold's active allocator metric, and whole-card use includes about 0.08 GiB outside the process.

Target BF16 KV costs 64 KiB/token (16 full-attention layers × 4 KV heads × 256 dimensions × K/V × 2 bytes); the MTP ring adds 4 KiB/token. The non-context column is measured CUDA active memory minus those cache amounts. It includes target/drafter allocations, fixed state and workspaces. TensorFold does not expose separate live counters for target weights, MTP weights and scratch, so this remainder is not a scratch-only figure. The source payloads are not exact device allocations; MTP source tensors are BF16 and are quantized to affine-4 tables at load.

Loader note: the Unsloth checkpoint config declares compressed-tensors, and its Safetensors index contains weight_packed tensors. TensorFold's Qwen CUDA loader maps these to NVFP4. The generic tensorfold info formatter's “full precision” label does not identify compressed-tensors formats.

No prompt was submitted at the maximum, so these results establish startup allocation capacity, not full-context prefill, quality or throughput.

This means we can store a context of 56,014 tokens with NVIDIA ModelOpt NVFP4, or 25,053 tokens with Unsloth NVFP4, in this 24 GB card under the measured settings.

@tacosta

tacosta commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

24 GB VRAM follow-up: reserve and FP8 KV experiment

Hardware: NVIDIA GeForce RTX 5090 Laptop GPU, 24,463 MiB capacity (23.89 GiB, sm_120).
Common setup: Qwen3.8-27B ModelOpt NVFP4 or Unsloth NVFP4, input embeddings in host RAM, one stream, MTP drafts=3.

Checkpoint KV / reserve Context started Next tested rejection CUDA active / NVML free
NVIDIA ModelOpt NVFP4 BF16 / 1.0 GiB 56,014 56,015 21.402 GiB / 1,662 MiB
Unsloth NVFP4 BF16 / 1.0 GiB 25,053 25,054 21.402 GiB / 1,704 MiB
NVIDIA ModelOpt NVFP4 BF16 / 0.5 GiB 63,724 64,000 21.902 GiB / 1,190 MiB
Unsloth NVFP4 BF16 / 0.5 GiB 32,763 not searched above 21.902 GiB / 1,192 MiB
NVIDIA ModelOpt NVFP4 FP8 / 0.5 GiB* 118,500 119,000 21.894 GiB / 1,198 MiB
Unsloth NVFP4 FP8 / 0.5 GiB* 59,000 60,000 21.831 GiB / 1,262 MiB

The default 1 GiB reserve is an admission margin, not a separate VRAM allocation. With BF16 KV and a 0.5 GiB reserve, ModelOpt started at 63,724 tokens, close to 64k; the 64,000-token probe was refused (rounded planner estimate: 4.76 GiB required versus 4.74 GiB available).

FP8 KV is lossy and may change generated tokens. We are aware the current project policy disallows introducing lossy cache formats, including opt-in support; the existing Qwen FP8 path predates that policy (issue #4107). These rows are an interesting experiment in the specific context of constrained 24 GB VRAM capacity, not a proposal to expand FP8 support or change policy. No prompt or output-fidelity comparison was run.

Every row reports startup admission only. No prompt was submitted, so these figures do not establish that a full-length prefill succeeds.

@tacosta

tacosta commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

24 GB VRAM follow-up: tap ring and recurrent scratch

A source audit explains the earlier 0.13 GiB startup-charge difference and found a separate 60 MiB per-stream omission in MTP admission accounting.

MTP tap ring. Qwen3.5 uses 6,144 rows for this scenario (2,048 tap-window rows + 4,096 prompt-pass rows). One BF16 tap at hidden size 5,120 is 62,914,560 bytes = 60 MiB. The target cache allocates it after the MTP drafter installs its tap list, but the target's stream_bytes was computed before that and MTP's per-stream facts omit the target tap. The physical CUDA-active sample after cache reservation includes the allocated ring in its non-context remainder; the per-stream admission charge does not. The previously reported startup acceptance points therefore remain observations of the current code, not corrected admission limits; they need a rerun after this charge is fixed.

The 0.13 GiB variation. The earlier 17.83 GiB run logged CUDA window costs: measured 64 widths; all three controlled repeats logged cached 64 widths and charged 17.96 GiB. A cache hit warms widths 1–64, then checkKept probes width 32. For this geometry, logBytes(32) is 138,706,944 bytes = 0.12918 GiB. The 64-row log plus that 32-row log totals 416,120,832 bytes, below the pool's 431,357,952-byte bound, so the cached validation retains both; the cold width sweep collects the smaller idle logs when it acquires the 64-row log. That is the observed delta. The model charge is sampled before stream reservation, so calibration scratch affects admission. Forward graphs read per-forward segment metadata from the stable e.tables buffer, so they do not retain a direct pointer to these recurrent log allocations; an idle/collect after calibration can reclaim them safely.

Rollback and cancellation. MTP depth 3 alone would need at most four verify rows, but the measured dense Qwen3.8 setup also has root/context copies enabled. With a 64-row family width, copy proposals can reach 64 rows, so log64 must stay for this exact configuration unless copy width is capped or copies are disabled. The separate one-row before-state copy is unnecessary for ordinary inference: the lane acceptance path always starts with row 0, and inference passes undo = false; the before copy is used by the empty-path single-row rollback case. Prefill runs are unrecorded, and cancellation/yield checks happen between completed cuts, retaining only the last completed prefix.

For this 27B geometry, dropping only the inference before-copy allowance would reduce the current recurrent bound by 153,944,064 bytes = 0.14337 GiB per stream while preserving log64. After adding the omitted 60 MiB tap charge, the net modeled per-stream reduction would be about 0.0848 GiB. This is a source-derived opportunity, not a new context-capacity result. No code or context limit was changed in this audit.

Sources: tap sizing and allocation, target stream charge, MTP stream facts, cost-cache validation, recurrent bound, and copy width setup.

@tacosta
tacosta force-pushed the feat/4942-qwen35-host-embeddings-evidence branch 4 times, most recently from 00a7fe7 to 3fdd3d9 Compare November 26, 2026 00:54
@tacosta
tacosta force-pushed the feat/4942-qwen35-host-embeddings-evidence branch from 3fdd3d9 to a7c26dd Compare November 27, 2026 07:01
@tacosta

tacosta commented Nov 27, 2026

Copy link
Copy Markdown
Contributor Author

Qualification evidence for a7c26dd98e7bd5c8b97a0edbbd5686bdca5e0735

Build and host checks

  • zig fmt --check build.zig zig and git diff --check upstream/main...HEAD: passed.
  • Focused host targets passed: test-qwen35-embedding-offload-host, test-qwen35-modelopt-admission-host, test-qwen35-formats-host, test-qwen35-mtp-selection-host, test-qwen35-mtp-memory-host, test-embedding-residency-cli-host, and test-execution-witness-host.
  • CUDA product build with -Dgpu=sm_120 -Dnvcc=/usr/local/cuda/bin/nvcc: passed (NVCC warnings only).
  • Lean checker: 123 findings on base and 123 on head, 0 new.
  • zig build -j2 test test-golden: 14,211 tests passed, 107 skipped. The aggregate job exited 1 only at test-lfm3-xpu-refusal-host, where Level Zero initialization returned zeInit: 0x78000001. This is an XPU initialization failure; a baseline reproduction was not completed. Golden comparison: 327/340 equal, 0 unexpected differences (13 known differences).

Runtime smoke checks on 24 GB VRAM

Hardware: RTX 5090 Laptop GPU, 24,463 MiB reported VRAM. All figures below are single-run smoke measurements; they are not a controlled performance comparison.

Checkpoint revisions from local Hugging Face metadata: ModelOpt dbb8f445b3145f8a4c18ddc769f032d57d32867c; Unsloth 60e813d4dbbdc5d64cf3f5a8caf2897bedf03679.

  • ModelOpt NVFP4 Qwen3.8-27B, 4K context, MTP3: device embeddings and explicit host embeddings each completed 32 output tokens; both reported 20/35 accepted drafts and the same output SHA-256 (7d0808c8bd37770f196096221330c17c52e2246a95f4907540b6d3086e0f4604). Measured decode rates were 77.3 tok/s (device) and 83.9 tok/s (host), one request each.
  • ModelOpt NVFP4 Qwen3.8-27B, 32K context, auto host embeddings, MTP3: prompt 32,759 tokens, context 32,768, completion 8; 4/4 drafts accepted, 52.3 tok/s. The engine log confirmed host-mapped embeddings and the MTP drafter. During the request, device use was 20,737 MiB with 3,248 MiB free.
  • Unsloth compressed-tensors NVFP4 Qwen3.8-27B, 4K context, host embeddings: completed an 8-token smoke request at 32.3 tok/s; MTP was not enabled for this run.

The result supports successful host embedding placement for both requested NVFP4 families. The short, single-run samples do not establish a general speedup or a main-versus-branch performance comparison.

Scope and overlap

MTP sizing is shared with the embedding residency planner so the auto decision includes the active drafter’s startup and resident requirements. Its scratch layout is unchanged; this does not reduce recurrent scratch or implement #5134’s separate MTP charge change. The admission formula now includes at most 255 bytes of alignment padding (zero for this tested model geometry).

ATC overlap scan at 2026-11-27 03:12 UTC, base 77eae824e425d7d3bea11f15e191983b99b53ba8: incomplete due to GitHub result limits; conflicts listed with #3729, #3567, #3566, #3565, #3564, and #3563; 108 overlaps have unknown status. One worktree overlap is stale per the task instructions.

@tacosta
tacosta force-pushed the feat/4942-qwen35-host-embeddings-evidence branch 6 times, most recently from 672a918 to 1b33142 Compare November 28, 2026 04:48
@tacosta
tacosta marked this pull request as ready for review November 28, 2026 07:24
@ashhart
ashhart force-pushed the feat/4942-qwen35-host-embeddings-evidence branch from 1b33142 to 2d49c06 Compare November 29, 2026 00:06
ashhart added a commit to tacosta/TensorFold that referenced this pull request Nov 29, 2026
@ashhart
ashhart force-pushed the feat/4942-qwen35-host-embeddings-evidence branch from 2d49c06 to 7a81575 Compare November 29, 2026 01:38
ashhart added a commit to tacosta/TensorFold that referenced this pull request Nov 29, 2026
ashhart added a commit to tacosta/TensorFold that referenced this pull request Nov 29, 2026
@ashhart
ashhart force-pushed the feat/4942-qwen35-host-embeddings-evidence branch from 7a81575 to 7dccce2 Compare November 29, 2026 04:15
@ashhart
ashhart force-pushed the feat/4942-qwen35-host-embeddings-evidence branch from 7dccce2 to 6541480 Compare November 29, 2026 06:23
@ashhart
ashhart merged commit 9f07b1b into ashhart:main Nov 29, 2026
1 check failed
ashhart added a commit that referenced this pull request Nov 29, 2026
@ashhart

ashhart commented Nov 29, 2026

Copy link
Copy Markdown
Owner

Thank you @tacosta. This is merged into main with your commits under your own name, so you're listed as a TensorFold contributor. It ships in the next release, credited to you in the changelog and the release notes.

ashhart added a commit to kotevcode/TensorFold that referenced this pull request Nov 29, 2026
ashhart added a commit that referenced this pull request Nov 29, 2026
ashhart added a commit that referenced this pull request Nov 29, 2026
ashhart added a commit that referenced this pull request Nov 29, 2026
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.

CUDA Qwen3.5: offload BF16 embeddings to host RAM for NVIDIA and Unsloth NVFP4

2 participants