// Model-free probe (claude/ledger): sustained host-to-device DMA rate of the expert stream by SOURCE MEMORY KIND and COPY SHAPE. // The ledger found the port's ring at ~37 GB/s (3 copies of 0.5-0.9 MB from the registered bank) while the Original's ring runs at // ~50 GB/s (1 copy of ~2.1 MB from its arena). This isolates which property matters. // nvcc -O2 -arch=sm_120 h2d_src_probe.cu -o h2d_src_probe ; ./h2d_src_probe [shard.gguf] // Source kinds: hostalloc | anon4k+register(0) | anon4k+register(PORTABLE) | anon4k+register(DEVICEMAP, the port's bank) | // anon+MADV_HUGEPAGE+register(DEVICEMAP) | file mapping (MAP_SHARED, PROT_READ) + register(READ_ONLY). // Shapes (per "expert" of ENTRY bytes): 3 x ENTRY/3 from three role-major regions (the port) | 1 x ENTRY contiguous (the Original) | // 1 x 2 MiB 4K-aligned | 1 x 8 MiB. #include #include #include #include #include #include #include #include #include #include #include #define CK(x) do { cudaError_t e_ = (x); if (e_ != cudaSuccess) { printf("CUDA error %s at %d: %s\n", #x, __LINE__, cudaGetErrorString(e_)); exit(1);} } while (0) using Clock = std::chrono::steady_clock; static uint8_t* anon(size_t bytes, bool thp) { void* p = mmap(nullptr, bytes + (2 << 20), PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); if (p == MAP_FAILED) { perror("mmap"); exit(1); } uint8_t* a = (uint8_t*)(((size_t)p + (2 << 20) - 1) & ~((size_t)(2 << 20) - 1)); if (thp) madvise(a, bytes, MADV_HUGEPAGE); memset(a, 0x5a, bytes); return a; } // run: copy `n` entries, each `pieces` copies of ENTRY/pieces bytes, cycling over `regions` role-major regions (pieces==3) or one region static double run(uint8_t* base, size_t region_bytes, size_t entry, int pieces, bool role_major, size_t stride, uint8_t* ring, int slots, cudaStream_t s, int n, int depth) { std::vector ev(depth + 1); for (auto& e : ev) CK(cudaEventCreateWithFlags(&e, cudaEventDisableTiming)); cudaEvent_t a, b; CK(cudaEventCreate(&a)); CK(cudaEventCreate(&b)); size_t piece = entry / pieces; size_t unit = role_major ? stride : stride * pieces; // distance between consecutive experts inside one region size_t per_region = region_bytes / (role_major ? pieces : 1) / unit; CK(cudaEventRecord(a, s)); for (int k = 0; k < n; ++k) { int slot = k % slots; if (k >= depth) CK(cudaStreamWaitEvent(s, ev[(k - depth) % (depth + 1)], 0)); for (int p = 0; p < pieces; ++p) { const uint8_t* src; if (role_major) src = base + (size_t)p * (region_bytes / pieces) + (size_t)(k % per_region) * stride; else src = base + (size_t)(k % per_region) * unit + (size_t)p * stride; CK(cudaMemcpyAsync(ring + (size_t)slot * entry + p * piece, src, piece, cudaMemcpyHostToDevice, s)); } CK(cudaEventRecord(ev[k % (depth + 1)], s)); if (k >= depth + 2) CK(cudaEventSynchronize(ev[(k - depth - 2) % (depth + 1)])); } CK(cudaEventRecord(b, s)); CK(cudaEventSynchronize(b)); float ms; CK(cudaEventElapsedTime(&ms, a, b)); for (auto& e : ev) cudaEventDestroy(e); cudaEventDestroy(a); cudaEventDestroy(b); return (double)n * entry / (ms * 1e6); } int main(int argc, char** argv) { const size_t ENTRY = 2046000; const size_t REGION = 16ull << 30; // 16 GiB per source kind: the footprint matters (IOMMU/IOTLB), the real bank is ~50 GB CK(cudaSetDevice(0)); cudaStream_t s; CK(cudaStreamCreateWithFlags(&s, cudaStreamNonBlocking)); const int SLOTS = 64; uint8_t* ring; CK(cudaMalloc((void**)&ring, (9ull << 20) * SLOTS)); struct Shape { const char* name; size_t entry; int pieces; bool role_major; size_t stride; }; std::vector shapes = {{"3 x 682000 role-major (port)", ENTRY, 3, true, ENTRY / 3}, {"3 x 682000 role-major, 4K-aligned stride", ENTRY, 3, true, 684032}, {"1 x 2046000 contiguous (Original)", ENTRY, 1, false, ENTRY}, {"1 x 2046000, 4K-aligned stride", ENTRY, 1, false, 2048000 + 4096 - 2048000 % 4096}, {"1 x 2 MiB aligned", 2u << 20, 1, false, 2u << 20}, {"1 x 8 MiB", 8u << 20, 1, false, 8u << 20}}; struct Kind { const char* name; int id; }; std::vector kinds = {{"cudaHostAlloc", 0}, {"anon 4K + register(0)", 1}, {"anon 4K + register(PORTABLE)", 2}, {"anon 4K + register(DEVICEMAP) (port bank)", 3}, {"anon THP + register(DEVICEMAP)", 4}, {"file map RO + register(READ_ONLY)", 5}}; for (auto& kd : kinds) { uint8_t* base = nullptr; size_t bytes = REGION; void* map = nullptr; size_t map_bytes = 0; int fd = -1; if (kd.id == 0) { CK(cudaHostAlloc((void**)&base, bytes, cudaHostAllocDefault)); memset(base, 0x5a, bytes); } else if (kd.id >= 1 && kd.id <= 4) { base = anon(bytes, kd.id == 4); unsigned flags = kd.id == 1 ? 0 : kd.id == 2 ? cudaHostRegisterPortable : cudaHostRegisterMapped; CK(cudaHostRegister(base, bytes, flags)); } else { if (argc < 2) { printf("%-44s skipped (no shard path)\n", kd.name); continue; } fd = open(argv[1], O_RDONLY); struct stat st; fstat(fd, &st); map_bytes = (size_t)st.st_size < bytes ? st.st_size : bytes; map = mmap(nullptr, map_bytes, PROT_READ, MAP_SHARED, fd, 0); if (map == MAP_FAILED) { printf("%-44s mmap failed\n", kd.name); continue; } base = (uint8_t*)map; bytes = map_bytes; volatile uint8_t sink = 0; for (size_t i = 0; i < bytes; i += 4096) sink += base[i]; // fault the pages in cudaError_t e = cudaHostRegister(base, bytes, cudaHostRegisterReadOnly); if (e != cudaSuccess) { printf("%-44s register failed: %s\n", kd.name, cudaGetErrorString(e)); munmap(map, map_bytes); continue; } } for (auto& sh : shapes) { double best = 0, sum = 0; for (int rep = 0; rep < 3; ++rep) { double r = run(base, bytes, sh.entry, sh.pieces, sh.role_major, sh.stride, ring, SLOTS, s, 6000, 16); if (r > best) best = r; sum += r; } printf("%-44s %-36s %6.1f GB/s (best %6.1f)\n", kd.name, sh.name, sum / 3, best); fflush(stdout); } if (kd.id == 0) cudaFreeHost(base); else if (kd.id <= 4) { cudaHostUnregister(base); } else { cudaHostUnregister(base); munmap(map, map_bytes); close(fd); } } return 0; }