Skip to contentSkip to conversation

perftest: add data validation for ROCm device memory

PATCH New Stephen Bates 1 participant 2 messages Nov 9

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 perftest: add data validation for ROCm device memory

Perftest validates host and CUDA memory but not ROCm memory. Add a ROCm validation backend, wired through the existing memory_ctx hooks.

src/rocm_kernels.hip is a port of src/cuda_kernels.cu and is the default: a resident kernel in which block 0 monitors the tail markers and blocks 1..N drain a lock-free queue, so each cycle is checked as it lands rather than inspecting the final state afterwards. Where it cannot run -- a build without hipcc, a device the kernel will not launch on, or PERFTEST_ROCM_VALIDATION=staged -- the fallback compares on the host after the run. The fallback is strictly weaker: it sees only each chunk's final state, so transient corruption a later cycle overwrote is missed and bytes_validated is correspondingly lower. Which path ran is printed at stop, and a live validator that observed no completed cycle falls back rather than reporting an empty result. Both paths reuse upstream's classify_validation_mismatch(), so a run stopping mid-cycle is scored as an overwrite race rather than corruption.

The ROCm copy ops were plain memcpy into and out of device memory. They are now hipMemcpy with the right direction.

The port stays deliberately close to the original: same monitor/validator split, same lock-free queue, same structures from cuda_validation.h, same exported entry points with nothing missing. The rocm_kernel_ prefix is not cosmetic -- the CUDA plugin is dlopen'd from libperftest_kernels.so, while these link into libperftest.a and share a namespace with the memory_ctx hooks. Divergences are marked PORT: at the site.

Warp width is the one that matters. The CUDA reduction assumes a 32-lane warp (tid & 31, shuffle offset starting at 16). An AMD wavefront is 64 lanes on CDNA and 32 on RDNA, so the literal reduces only within half a wave and lets two lanes observe lane == 0, double-counting the atomics that follow. The port uses warpSize, which is a built-in rather than a constant, so the unrolls go with it. HIP's warp intrinsics take no mask operand, so the *_sync forms map onto the HIP ones.

The synchronisation is expressed with scoped atomics rather than the CUDA original's volatile idiom. volatile is not a synchronisation primitive: it constrains the compiler for one access and says nothing about which agents the access must be coherent with. It works on NVIDIA because the emitted loads happen to bypass cache. On AMD the memory scope is what selects which cache levels are bypassed, so __hip_atomic_load/store carry it explicitly -- system scope for the tail markers and receive slots the NIC DMAs into, and for the flags shared with the host; agent scope for the work queue only this kernel's blocks touch. The queue handoff is a release/acquire pair on the slot's valid flag. A uniform __threadfence_system() over-synchronised the queue while under-specifying the markers.

Two __ldg uses go with them, both on data the NIC is actively writing. __ldg is a read-only-cache hint, asserting the data will not change for the life of the kernel, which is false for a DMA target. The tail retry re-read through __ldg was the load least likely to observe the write it was waiting for; and the forward-byte snapshot feeds classify_validation_mismatch(), where a stale read biases towards reporting corruption that did not occur. The bulk compare loop keeps __ldg, ordered by the acquire on the marker.

Poll loops back off with s_sleep rather than spinning. A device that runs a wave until it blocks never retires a tight poll loop, so the kernel cannot observe should_stop and the host waits in hipDeviceSynchronize() forever. That includes the slot-valid wait in the work queue, which a consumer reaches holding its block at a __syncthreads(), so stalling there strands every thread in the block rather than just itself. validation_start() also honours num_blocks and threads_per_block, which the CUDA original accepts and ignores:
33 blocks x 1024 spinning threads is not a geometry every device retires.

rocm_kernels_available() is the one addition with no CUDA counterpart. The CUDA plugin answers that question by whether libperftest_kernels.so dlopen'd at all; these kernels link in unconditionally and need a runtime probe.

Error codes are not discarded. hipcc marks the HIP entry points nodiscard where nvcc does not, which exposed three cases the CUDA style hides: the teardown frees genuinely do not care and say so with a cast; a hipMemset that quietly failed would leave a previous run's markers, stats or error record in place for the validator to report as this run's, and now returns VALIDATION_ERR_MEMSET, which cuda_validation.h had defined and nothing returned; and a discarded hipMemcpy in check_status would have reported "not failed", asserting the data is good on the strength of a read that did not happen.

Three supporting changes.

VALIDATION_INLINE only annotated the shared helpers __host__ __device__ for __CUDACC__, so they were not callable from hipcc device code. hipcc compiles ..hip as nvcc compiles .cu; both need the annotation.

configure learns --with-rocm-arch, defaulting to rocm_agent_enumerator. hipcc otherwise targets whatever GPU the build host reports, which is wrong whenever perftest is cross-built or built in a VM, and the symptom is "device kernel image is invalid" at launch rather than anything at build time.

The ROCm dmabuf path no longer greps the kernel config before running. That check required CONFIG_DMABUF_MOVE_NOTIFY, which no longer exists upstream -- dma-buf always builds with it, and .move_notify became .invalidate_mappings -- so it began refusing to run precisely because support had become universal. What it gated was whether amdgpu_dma_buf_pin() kept AMDGPU_GEM_DOMAIN_VRAM or fell back to GTT: driver policy, versioned with the driver rather than the running kernel, which on a DKMS install moves independently of it. CONFIG_PCI_P2PDMA fared no better, being blind to whether the NIC's driver implements .reg_user_mr_dmabuf. Both questions are already answered accurately by hsa_amd_portable_export_dmabuf() and ibv_reg_dmabuf_mr(); their diagnostics are improved instead of being predicted.

data_validation_stop_and_report() is reordered: errors, then nothing-validated, then pass. This affects every backend, not just ROCm. Testing "nothing validated" first reports a run whose chunks all mismatched -- errors > 0 with bytes_validated == 0 -- as a warning about missing data, hiding a real failure. And backends set passed = (errors == 0 && chunks > 0), so a run that observed no completed cycle has passed == 0 and would otherwise be reported as a failure with every error field still zero: "FAILED - 0 error(s) ... (expected 0x00, got 0x00)". bytes_validated == 0 is that same condition counted differently, since a chunk mismatching at its first byte contributes no compared bytes but still increments the chunk count.

Two debugging hooks. PERFTEST_ROCM_VALIDATION_FAULT=n corrupts n bytes of the staged copy before the compare, because a validator that only ever passes is indistinguishable from one that does not look. PERFTEST_ROCM_HOST_ALLOC=1 allocates host-mapped rather than VRAM, for devices whose MR registers cleanly and then fails to gather; it measures host memory, so it diagnoses reachability rather than benchmarking peer-to-peer.

Tested on an emulated gfx1250 behind an emulated RDMA NIC: the live kernel retires and the host-staged fallback passes with real byte counts, and injected faults are caught on both. Builds warning-free with --enable-rocm and hipcc, with --enable-rocm without it, and with neither.

---
 Makefile.am               |   32 +-
 configure.ac              |   25 +
 src/perftest_parameters.c |    5 +-
 src/perftest_resources.c  |   63 ++-
 src/rocm_kernels.h        |   64 +++
 src/rocm_kernels.hip      | 1029 +++++++++++++++++++++++++++++++++++++
 src/rocm_memory.c         |  182 +++----
 src/rocm_validation.c     |  467 +++++++++++++++++
 src/rocm_validation.h     |   57 ++
 src/validation_common.h   |   12 +-
 10 files changed, 1839 insertions(+), 97 deletions(-)
 create mode 100644 src/rocm_kernels.h
 create mode 100644 src/rocm_kernels.hip
 create mode 100644 src/rocm_validation.c
 create mode 100644 src/rocm_validation.h
Makefile.am +31 -1
index b12c302..ada55ed 100755
@@ -35,7 +35,7 @@ AUTOMAKE_OPTIONS= subdir-objects
 
 noinst_LIBRARIES = libperftest.a
 libperftest_a_SOURCES = src/get_clock.c src/perftest_communication.c src/perftest_negotiation.c src/perftest_parameters.c src/perftest_resources.c src/perftest_counters.c src/host_memory.c src/host_validation.c src/mmap_memory.c src/numa_loader.c
-noinst_HEADERS = src/get_clock.h src/perftest_communication.h src/perftest_negotiation.h src/perftest_parameters.h src/perftest_resources.h src/perftest_counters.h src/memory.h src/host_memory.h src/host_validation.h src/mmap_memory.h src/cuda_memory.h src/rocm_memory.h src/neuron_memory.h src/hl_memory.h src/mlu_memory.h src/cuda_validation.h src/validation_common.h src/dm_memory.h src/system_dmabuf_memory.h src/musa_memory.h src/numa_loader.h
+noinst_HEADERS = src/get_clock.h src/perftest_communication.h src/perftest_negotiation.h src/perftest_parameters.h src/perftest_resources.h src/perftest_counters.h src/memory.h src/host_memory.h src/host_validation.h src/mmap_memory.h src/cuda_memory.h src/rocm_memory.h src/rocm_validation.h src/neuron_memory.h src/hl_memory.h src/mlu_memory.h src/cuda_validation.h src/validation_common.h src/dm_memory.h src/system_dmabuf_memory.h src/musa_memory.h src/numa_loader.h
 
 AM_CFLAGS =
 
@@ -52,6 +52,36 @@ endif
 
 if ROCM
 libperftest_a_SOURCES += src/rocm_memory.c
+libperftest_a_SOURCES += src/rocm_validation.c
+noinst_HEADERS += src/rocm_kernels.h
+endif
+
+if ROCM_KERNELS
+
+if ROCM_ARCH_SET
+ROCM_OFFLOAD_FLAG = --offload-arch=@ROCM_OFFLOAD_ARCH@
+else
+ROCM_OFFLOAD_FLAG =
+endif
+
+ROCM_HIPCC_FLAGS = -DHAVE_CONFIG_H $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) \
+	-fPIC -std=c++17 $(ROCM_OFFLOAD_FLAG)
+
+src/rocm_kernels.hip: ;
+
+src/rocm_kernels.hip.o: src/rocm_kernels.hip src/rocm_kernels.h \
+		src/cuda_validation.h
+	$(HIPCC) $(ROCM_HIPCC_FLAGS) -o $@ -c $<
+
+ROCM_KERNEL_OBJ = src/rocm_kernels.hip.o
+libperftest_a_LIBADD = $(ROCM_KERNEL_OBJ)
+
+clean-local-rocm-kernels:
+	-rm -f src/rocm_kernels.hip.o
+
+else
+ROCM_KERNEL_OBJ =
+clean-local-rocm-kernels:
 endif
 
 if NEURON
configure.ac +25 -0
index e44ffe5..4549167 100755
@@ -334,9 +334,34 @@ AS_IF([test "x$enable_rocm" = xyes], [
        AC_SEARCH_LIBS([hipFree], [amdhip64], [],
                       [AC_MSG_ERROR([cannot link with -lamdhip64])])
        AC_DEFINE([HAVE_ROCM], [1], [Enable ROCm])
+       dnl HIP validation kernels are optional: without hipcc the ROCm
+       dnl validator falls back to staging the buffer through host memory,
+       dnl which is slower but needs no device compiler.
+       AC_CHECK_TOOL([HIPCC], [hipcc], [no])
+       dnl Which GPU the kernels are compiled for. hipcc's default target is
+       dnl whatever the *build* host reports, which is wrong whenever perftest
+       dnl is cross-built or built in a VM for an emulated device -- the
+       dnl symptom is "device kernel image is invalid" at launch, long after a
+       dnl clean build. Ask the runtime, and let a caller override.
+       AC_ARG_WITH([rocm-arch],
+                   [AS_HELP_STRING([--with-rocm-arch=ARCH],
+                                   [GPU arch for HIP kernels (default: ask rocm_agent_enumerator)])],
+                   [ROCM_OFFLOAD_ARCH="$withval"],
+                   [ROCM_OFFLOAD_ARCH=`rocm_agent_enumerator 2>/dev/null | grep -v '^gfx000$' | head -1`])
+       AS_IF([test "x$ROCM_OFFLOAD_ARCH" = "x"],
+             [AC_MSG_WARN([no GPU arch detected; HIP kernels will use hipcc's default])],
+             [AC_MSG_NOTICE([HIP kernels target $ROCM_OFFLOAD_ARCH])])
+       AC_SUBST([ROCM_OFFLOAD_ARCH])
+       AS_IF([test "x$HIPCC" != "xno"],
+             [AC_DEFINE([HAVE_ROCM_KERNELS], [1], [HIP validation kernels])
+              rocm_kernels=yes],
+             [AC_MSG_WARN([hipcc not found; ROCm validation will stage through host memory])
+              rocm_kernels=no])
        ])
 
 AM_CONDITIONAL([ROCM], [test x$enable_rocm = xyes])
+AM_CONDITIONAL([ROCM_KERNELS], [test x$rocm_kernels = xyes])
+AM_CONDITIONAL([ROCM_ARCH_SET], [test x$ROCM_OFFLOAD_ARCH != x])
 
 AS_IF([test "x$enable_rocm_dmabuf" = xyes], [
        AS_IF([test "x$HAVE_REG_DMABUF_MR" = "xno"],
src/perftest_parameters.c +3 -2
index ae17db0..8f491c8 100755
@@ -1519,11 +1519,12 @@ static void force_dependecies(struct perftest_parameters *user_param)
 	}
 
 	if (user_param->data_validation) {
-		/* Require CUDA or HOST memory */
+		/* Require CUDA, ROCm or HOST memory */
 		if (user_param->memory_type != MEMORY_CUDA &&
+		    user_param->memory_type != MEMORY_ROCM &&
 		    user_param->memory_type != MEMORY_HOST) {
 			printf(RESULT_LINE);
-			fprintf(stderr, " --data_validation requires --use_cuda or host memory\n");
+			fprintf(stderr, " --data_validation requires --use_cuda, --use_rocm or host memory\n");
 			exit(1);
 		}
 
src/perftest_resources.c +57 -6

90 lines not on this page · raw message

src/rocm_kernels.h new file +64 -0

64 lines not on this page · raw message

src/rocm_kernels.hip new file +1029 -0

1,029 lines not on this page · raw message

src/rocm_memory.c +95 -87

225 lines not on this page · raw message

src/rocm_validation.c new file +467 -0

467 lines not on this page · raw message

src/rocm_validation.h new file +57 -0

57 lines not on this page · raw message

src/validation_common.h +11 -1
index 8aabc4a..dd8a560 100755
@@ -67,7 +67,9 @@ enum validation_mismatch_type {
  * Portable inline qualifier: works in both C (.c) and CUDA (.cu) files.
  */
 #ifndef VALIDATION_INLINE
-#ifdef __CUDACC__
+#if defined(__CUDACC__) || defined(__HIPCC__)
+/* hipcc compiles .hip like nvcc compiles .cu: the shared helpers below are
+ * called from device code on both, so both need the device annotation. */
 #define VALIDATION_INLINE __host__ __device__ static inline
 #else
 #define VALIDATION_INLINE static inline
@@ -307,6 +309,14 @@ classify_validation_mismatch(uint64_t error_offset,
  */
 struct validation_config {
 	void *buffer_base;
+	/*
+	 * Size of the whole validation buffer -- compute_validation_layout()'s
+	 * total_size. A backend that stages the buffer through host memory must
+	 * size that staging on this and not re-derive it: markers_offset is
+	 * mode-dependent and in READ mode points at atomic_returns, which sits
+	 * past recv_slots at the end of the layout.
+	 */
+	uint64_t buffer_size;
 	uint64_t markers_offset;
 	uint64_t recv_slots_offset;
 	uint64_t payload_size;
Signature · 1 line
2.53.0
#1 lore raw

PATCH 2/2 perftest: extend ROCm CI to compile device code per target arch

The ROCm CI job added previously builds everything but passes no --with-rocm-arch flag, so the hip kernel is compiled for the host only. Now that --with-rocm-arch exists and the 7.2.4 slim image ships device support for gfx950 and gfx1250, fan the job into one leg per architecture.

fail-fast: false keeps both legs running so a device-specific failure in one does not hide the other leg's result.

---
 .github/workflows/ubuntu24_04_rocm7_2.yaml | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)
.github/workflows/ubuntu24_04_rocm7_2.yaml +13 -4
index e8f3ea5..3a3ce03 100644
@@ -15,11 +15,19 @@ jobs:
 
     # AMD's slim ROCm image. The -full and -complete tags carry the whole ML
     # stack -- Composable Kernel alone is over 2 GB -- which a compile check
-    # never touches. This one is about 1.1 GB and still ships hipcc and the
-    # HIP and HSA headers needed to compile the ROCm backend.
+    # never touches. This one is about 1.1 GB and still ships hipcc, the HIP
+    # and HSA headers, and device support for gfx950 and gfx1250.
     container: rocm/dev-ubuntu-24.04:7.2.4
 
-    name: ROCm
+    strategy:
+      # Report every architecture's result. A failure in generated device code
+      # is far more likely to be specific to one target than common to both,
+      # and cancelling the other leg hides exactly the comparison that matters.
+      fail-fast: false
+      matrix:
+        rocm_arch: [gfx950, gfx1250]
+
+    name: ROCm (${{ matrix.rocm_arch }})
 
     steps:
       - name: Checkout repository
@@ -63,7 +71,8 @@ jobs:
       - name: Configure the build
         run: |
           ./configure --enable-rocm --enable-rocm-dmabuf \
-                      --with-rocm="$ROCM_PATH"
+                      --with-rocm="$ROCM_PATH" \
+                      --with-rocm-arch=${{ matrix.rocm_arch }}
 
       - name: Build perftest
         run: make
Signature · 1 line
2.53.0