Skip to content

cuda: carve small uploads from shared slabs; share identical trellis uploads - #4003

Merged
ashhart merged 5 commits into
ashhart:mainfrom
shantanugoel:cuda-upload-slabs
Nov 20, 2026
Merged

ashhart merged 5 commits into
ashhart:mainfrom
shantanugoel:cuda-upload-slabs

Conversation

@shantanugoel

@shantanugoel shantanugoel commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Summary

On GB10 every cuMemAlloc is rounded up to 2 MiB. A 512-expert EXL3 checkpoint uploads about 63k tensors of 1.18 MB each, so an 85 GiB pack needs about 150 GB and the machine runs out of memory. Uploader gets an opt-in slab mode and shares repeated trellis bytes.

 Uploader.dev(bytes)
+  if slab != 0 and bytes.len <= slab / 2
+    carve(bytes)      # 256-byte aligned, from the open slab; full -> allocate the next slab
+    return
   cuMemAlloc + copy   # unchanged when slab is unset

 pointers(projs)
-  one upload per projection
+  reuse the device copy when a projection names the same trellis bytes as an earlier one

 rollback(mark)
+  forget the open slab once it is freed

Evidence

  • Allocation cost on GB10 (MemAvailable before and after):

    • 2,000 × 1.18 MB cuMemAlloc: 4.04 GiB used for 2.20 GiB requested (1.84×).
    • One 2.20 GiB block: 2.24 GiB (1.02×).
  • Before: loading LibertAIDAI/Hy4-flash-EXL3-4bpw with one allocation per tensor hit the free-memory floor at layer 30 of 42, with 59.83 GiB uploaded and 15 GiB left.
    After: with 256 MiB slabs all 42 layers load: 85.38 GiB on the device, 29 GiB left.

  • Tests: zig build test-exl3-cuda-host → 336/336 passed. New tests:

    • small uploads share one 256-byte-aligned slab; a large upload or an overflow gets its own allocation;
    • rolling back past an open slab forgets it;
    • projections naming the same trellis bytes share one device copy;
    • with slab != 0, a copy whose wait fails is drained before its slab is freed (bad_frees == 0), and a new slab whose first copy fails is freed and never registered.

    The existing expert-upload test now gives its six projections distinct trellis slices; before, they all shared one literal, which the dedupe now collapses.

Merge Danger

Door: two-way

The slab mode is opt-in (slab = 0 by default) and the change is one file.

Blast Radius: EXL3-uploads

With slabs off, the only behavioural change is the dedupe in pointers(): callers whose projections share one trellis slice now upload it once. Those uploads are byte-identical, so kernels see the same weights, but an Uploader holds fewer buffers and fewer bytes.

@ashhart

ashhart commented Nov 19, 2026

Copy link
Copy Markdown
Owner

Thanks @shantanugoel for the slab uploader and trellis deduplication fix — the motivation on GB10's 2 MiB cuMemAlloc granularity is clear and the diff is clean and concise.

One thing to fix before we can land this: in Uploader.carve(), if b.upload(at, bytes) fails (for instance when cuStreamSynchronize returns NotReady while a copy is still in flight), carve() returns directly without calling completion.drain(...) like DeviceBuffer.fromHost does in zig/src/cuda/memory.zig. That lets rollback() or deinit() free the slab while a DMA copy may still be writing to it, and leaves a newly opened empty slab registered in u.bufs / u.slab_at / u.bytes if the caller catches the error.

Please update carve() to drain on b.upload failure and avoid committing a newly allocated slab until its first upload succeeds (or pop and free it after draining), and add a wait-failure test with slab != 0 covering bad_frees == 0. Once that's in, we can run it on our GB10s and land it.

  • Synthena

@ashhart

ashhart commented Nov 19, 2026

Copy link
Copy Markdown
Owner

Thanks @shantanugoel for this.

One thing before we review it: both commits are authored with a personal email address. Everything we land keeps that line in public history, so we only take commits authored with a GitHub noreply address. To fix it:

  1. Set your noreply address with git config user.email (GitHub lists it under Settings, Emails).
  2. Run git rebase HEAD~2 --exec 'git commit --amend --no-edit --reset-author'.
  3. Force-push.

Once the new head is up we will review the change itself.

  • Synthena

@shantanugoel

Copy link
Copy Markdown
Contributor Author

Thanks, I've made both changes as asked: carve() now drains on a failed upload with wait-failure tests, and all commits now use my GitHub noreply address.

@ashhart

ashhart commented Nov 19, 2026

Copy link
Copy Markdown
Owner

Thanks @shantanugoel for the quick turnaround.

Both asks are addressed. carve() now drains when an upload's wait fails, both on an open slab and on a new slab whose first copy fails, and a failed carve leaves no slab registered. rollback() forgets a slab it frees. Your wait-failure tests run with slabs on and check that no buffer is freed while a copy is still pending. The dedupe shares a copy only for byte-identical trellis slices, so kernels see the same weights. The commits now carry your GitHub noreply address, and the checks on this head are green.

The last step is ours: a GB10 device run on its landing train before it lands.

  • Synthena

ashhart and others added 5 commits November 20, 2026 12:43
…uploads

On GB10 each cuMemAlloc is rounded to 2 MiB: 2,000 x 1.18 MB uploads took
4.04 GiB against 2.24 GiB for one block of the same size (1.84x). A
512-expert EXL3 checkpoint has ~63k such tensors, so an 85 GiB pack needed
~150 GB and ran the machine out of memory.

Uploader.slab (opt-in) carves uploads up to half its size, 256-byte
aligned, from shared blocks; rollback forgets a released slab. pointers()
reuses the device copy when projections name the same trellis bytes.
Unset slab keeps the old behavior.
…t copy

carve() returned straight from a failed b.upload, so rollback() or deinit()
could free the slab while the copy was still writing it, and a newly opened
empty slab stayed in bufs/slab_at/bytes. It now drains as
DeviceBuffer.fromHost does, and a new slab is registered only once its first
upload succeeds (else drained and freed). Wait-failure tests with slab != 0
check bad_frees == 0.
@ashhart
ashhart merged commit 5ee0961 into ashhart:main Nov 20, 2026
2 of 11 checks passed
@ashhart

ashhart commented Nov 20, 2026

Copy link
Copy Markdown
Owner

Thank you @shantanugoel. 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.

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.

2 participants