Skip to content

Conversation

@gaxiom
Copy link
Contributor

@gaxiom gaxiom commented Nov 25, 2025

Current hypothesis: we're out of available virtual address space. Solution: reuse regions after defragmentation.

  • Intoducing unmapped_regions - it's a contiguous VA space that is already reserved, but not used as freed or malloced region. So, we can use it for defragmentation
  • Reserving more VA space on the fly if needed
  • VA_SIZE is now env var
  • event.synchronize() for proper unmaping when defragmentation

Current solution is focused on simplicity and guaranteed behavior by the price of synced defragmentation.
Optional: how to make it async again:

  • on current stream use cudaLaunchHost that will unmap region and add to the unmapped_regions
  • on other stream call default_stream_wait_event and use cudaLaunchHost that will drop event and unmap region

Related to INT-5557
Closes INT-5592
Closes INT-5531

@gaxiom gaxiom changed the title chore(cuda): vpmm better log chore(cuda): vpmm v4 - reusing VA & synced defrag Nov 26, 2025
@gaxiom gaxiom requested a review from jonathanpwang November 26, 2025 14:23
@gaxiom gaxiom marked this pull request as ready for review November 26, 2025 14:23
5. **Cross-stream synchronization**: Use CUDA events to track when freed memory becomes safe to reuse across streams.
6. **Grow**: if still insufficient, **allocate more pages** and map them at the end.
1. **Reserve** a large VA chunk once (size configurable via `VPMM_VA_SIZE`). Additional chunks are reserved on demand.
2. **Track** three disjoint maps: `malloc_regions`, `free_regions` (with CUDA events/stream ids), and `unmapped_regions` (holes).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find the term "hole" confusing: what is more important is the distinction between "host-side freed but still VA-reserved space" versus totally VA-unmapped. I think free_regions vs unmapped_regions naming is good to clarify that. "Hole" makes it hard to tell if you're talking about free_regions or unmapped_regions

let original_addr = *addr_holder2.lock().unwrap();
let new_addr = buf.as_raw_ptr() as usize;
if new_addr == original_addr {
println!("Cross-thread: reused same address (event synced)");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you only print, this test will not automatically catch anything: can you force exactly one scenario to occur and assert! it?

let mut handles = Vec::new();

for thread_idx in 0..8 {
let handle = tokio::task::spawn_blocking(move || {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is this testing? if it's not close to any hard memory limits, the behavior could be incorrect but the allocations still work. I expect more asserts of exact behavior? Right now even if something didn't go correctly, since there's no kernels accessing the memory, I doubt you'd catch much with this test

Copy link
Contributor

@jonathanpwang jonathanpwang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe there was an edge case which is fixed in c299a49: the to_defrag list doesn't consider the free region merged with dst, but remaining size didn't account for this.

@jonathanpwang jonathanpwang changed the title chore(cuda): vpmm v4 - reusing VA & synced defrag fix(cuda): vpmm v4 - reusing VA & synced defrag Nov 30, 2025
Copy link
Contributor

@jonathanpwang jonathanpwang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving to merge because I reviewed the code and documentation and it looks correct to me, but I made a follow-up ticket to harden the stress testing.

@jonathanpwang jonathanpwang merged commit 29daf03 into main Nov 30, 2025
5 checks passed
@jonathanpwang jonathanpwang deleted the chore/vpmm-full-usage branch November 30, 2025 21:18
@jonathanpwang jonathanpwang restored the chore/vpmm-full-usage branch November 30, 2025 21:19
jonathanpwang added a commit to openvm-org/openvm that referenced this pull request Nov 30, 2025
Updating to use openvm-org/stark-backend#179

Comparison bench:
https://github.com/axiom-crypto/openvm-reth-benchmark/actions/runs/19803890244

---------

Co-authored-by: Jonathan Wang <31040440+jonathanpwang@users.noreply.github.com>
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.

3 participants