Skip to content

napkin-bench: h2d_latency_us includes a cuMemAllocAsync/cuMemFreeAsync per copy (H100: 8.9 µs vs 6.0 µs preallocated) #32

Open

Description

@devin-ai-integration

h2d_latency_us is meant to time one 4-byte synchronous host-to-device copy. estimate.rs documents it as "one 4-byte copy plus the stream synchronize that waits for it" and charges it once per sync copy. The timed closure also allocates and frees the destination on every iteration:

let h2d_lat = wall_samples(5, 20, cfg.rounds, || {
    let one = [1.0f32];
    let mut dst = unsafe { stream0.alloc::<f32>(1) }?;   // cuMemAllocAsync on the stream
    stream0.memcpy_htod(&one, &mut dst)?;
    stream0.synchronize()?;
    Ok(())                                                // drop -> cuMemFreeAsync
})?;

cudarc 0.19's CudaStream::alloc calls result::malloc_async when the device supports stream-ordered allocation, and CudaSlice::drop calls free_async. Each iteration therefore pays for a pool alloc, the copy, a sync, and a pool free. The free is queued behind the next iteration's work. The D2H loop right below reuses dev_a and allocates nothing.

Reproduction (Modal H100!:2, NVIDIA H100 80GB HBM3)

I added three variants after the existing loop. Each uses the same wall_samples(5, 20, cfg.rounds, ..) and full-config rounds (7), and each ran three times in one process:

let mut pre = unsafe { stream0.alloc::<f32>(1) }?;
// a: current code (alloc + copy + sync + free per iteration)
// b: same copy into `pre`, no allocation
let b = wall_samples(5, 20, cfg.rounds, || {
    let one = [1.0f32];
    stream0.memcpy_htod(&one, &mut pre)?;
    stream0.synchronize()?;
    Ok(())
})?;
// c: alloc + drop + sync, no copy
let c = wall_samples(5, 20, cfg.rounds, || {
    let dst = unsafe { stream0.alloc::<f32>(1) }?;
    drop(dst);
    stream0.synchronize()?;
    Ok(())
})?;

Medians of the 7 rounds, per copy:

trial a: current loop b: preallocated dst c: alloc/free + sync only
0 8.89 µs 5.87 µs 4.63 µs
1 8.99 µs 6.00 µs 4.67 µs
2 8.93 µs 5.96 µs 4.72 µs

The rounds are tight. In trial 2, (a) ranged 8.85–9.39 µs and (b) 5.90–6.40 µs, so the ~3 µs gap is far outside the spread. The same run's stock code printed h2d/d2h latency 9.1/9.1 μs.

Expected vs actual

  • Expected: h2d_latency_us covers a 4-byte H2D copy plus a stream synchronize, as CopyMode documents. On this H100 host that is about 6.0 µs.
  • Actual: it also includes cuMemAllocAsync + cuMemFreeAsync and reads about 8.9 µs, roughly 50% higher. The committed H100 value is 8.8 µs. It is the L_copy used by napkin estimate (1 sync copy: 1 × 8.8 μs + ...) and the teaching constant pinned in crates/napkin/tests/readme.rs. With --h2d-copies N, the overstatement is multiplied by N.

I only measured H100. The other three profiles come from the same loop, so they probably carry the same kind of overhead, but I have not measured how large it is on A100, B200 or B300.

Modal run: https://modal.com/apps/tushardzig/main/ap-2FpNpALeEMEe152THor4B5

Written by Devin

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions